Use saved web and Gitea access from a fresh chat

This commit is contained in:
Mark Gregory
2026-11-16 16:18:39 +00:00
parent 00ea78671b
commit 4ebf2b82be
6 changed files with 79 additions and 15 deletions
+29 -3
View File
@@ -243,7 +243,8 @@ enum ChatWebLookup {
init?(message: String) {
let text = message.trimmingCharacters(in: .whitespacesAndNewlines)
let prefixes = ["/search ", "search the web for ", "search online for "]
let prefixes = ["/search ", "can you search the web for ", "please search the web for ",
"search the web for ", "search online for ", "look up online ", "web search for "]
if let prefix = prefixes.first(where: { text.lowercased().hasPrefix($0) }) {
let query = String(text.dropFirst(prefix.count)).trimmingCharacters(in: .whitespacesAndNewlines)
if !query.isEmpty { self = .search(query); return }
@@ -263,9 +264,10 @@ enum ChatWebLookup {
self = .pages(urls)
}
func resolve(searchEndpoint: String) async throws -> (reference: String, status: String) {
func resolve(searchEndpoint: String) async throws -> (reference: String, status: String, browserFallback: URL?) {
var references: [String] = []
var failures: [String] = []
var browserFallback: URL?
switch self {
case .pages(let urls):
for url in urls {
@@ -289,6 +291,11 @@ enum ChatWebLookup {
try Task.checkCancellation()
failures.append(error.localizedDescription)
}
if references.isEmpty {
var browser = URLComponents(string: "https://duckduckgo.com/")
browser?.queryItems = [.init(name: "q", value: query)]
browserFallback = browser?.url
}
}
let status = failures.isEmpty ? "Retrieved \(references.count) web source(s)." : failures.joined(separator: " ")
let notice = failures.isEmpty ? "" : "\n\nWeb lookup errors: \(failures.joined(separator: " "))"
@@ -297,7 +304,7 @@ enum ChatWebLookup {
\(references.joined(separator: "\n\n"))\(notice)
"""
return (reference, status)
return (reference, status, browserFallback)
}
}
@@ -334,6 +341,25 @@ enum GiteaRepositoryLookup {
return repository && inventory && account
}
static func configuredClient(pathOverride: String? = nil) async throws -> any MCPToolClient {
let saved = pathOverride ?? UserDefaults.standard.string(forKey: "mcpConfigPath") ?? ""
let path = saved.isEmpty
? FileManager.default.homeDirectoryForCurrentUser.appending(path: ".lmstudio/mcp.json")
: URL(fileURLWithPath: saved)
guard FileManager.default.fileExists(atPath: path.path) else {
throw StudioError("Import your Gitea MCP configuration in Toolbox → MCP Servers.")
}
let profiles = try MCPServerConfiguration.File.read(path).mcpServers
guard let profile = profiles["absdev-gitea"] ?? profiles["gitea"] else {
throw StudioError("Connect your Gitea server in Toolbox → MCP Servers.")
}
let client = try profile.makeClient()
guard try await client.listTools().contains(where: { $0.name == "list_repositories" }) else {
throw StudioError("The configured Gitea server does not expose list_repositories.")
}
return client
}
static func references(client: any MCPToolClient) async throws -> String {
let data = try await client.callTool("list_repositories", argumentsJSON: Data("{}".utf8))
guard let result = try JSONSerialization.jsonObject(with: data) as? [String: Any],