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
+3
View File
@@ -216,6 +216,9 @@ struct ChatView: View {
Text(activity.joined(separator: " · "))
.font(.caption).foregroundStyle(.secondary).lineLimit(2).textSelection(.enabled)
}
if let link = studio.webSearchLink {
Link("Open search in browser", destination: link).font(.caption)
}
ChatComposerInput(text: $studio.prompt, canSend: !studio.running, spellCheck: spellCheck,
autoCorrect: autoCorrect, font: chatFont.appKit(size: chatSize), send: sendMessage)
.frame(height: 62)
+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],
+12 -4
View File
@@ -20,6 +20,7 @@ final class StudioModel: ObservableObject {
@Published private(set) var managing = false
@Published private(set) var running = false
@Published private(set) var webStatus = ""
@Published private(set) var webSearchLink: URL?
@Published private(set) var knowledgeStatus = ""
var connectedKnowledge: (any MCPToolClient)? {
@@ -177,8 +178,9 @@ final class StudioModel: ObservableObject {
\(toolAttachment.output)
""")
}
let lookup = mcpConnections[.web]?.toolset != nil ? ChatWebLookup(message: text) : nil
let repositoryClient = GiteaRepositoryLookup.matches(text) ?
let lookup = ChatWebLookup(message: text)
let wantsRepositories = GiteaRepositoryLookup.matches(text)
let repositoryClient = wantsRepositories ?
activeToolsets.first(where: { entry in
entry.toolset.tools.contains { $0.name == "list_repositories" }
})?.toolset.client : nil
@@ -188,6 +190,7 @@ final class StudioModel: ObservableObject {
prompt = ""
toolAttachment = nil
status = ""
webSearchLink = nil
webStatus = lookup == nil ? "" : "Looking up web sources…"
knowledgeStatus = knowledge == nil ? "" : "Searching the connected knowledge index…"
running = true
@@ -199,6 +202,7 @@ final class StudioModel: ObservableObject {
let sources = try await lookup.resolve(searchEndpoint: searchEndpoint)
try Task.checkCancellation()
webStatus = sources.status
webSearchLink = sources.browserFallback
systemParts.append(sources.reference)
} catch {
webStatus = ""
@@ -228,10 +232,13 @@ final class StudioModel: ObservableObject {
systemParts.append("External knowledge retrieval failed for this turn. Do not claim to have searched the index.")
}
}
if let repositoryClient {
if wantsRepositories {
webStatus = "Listing Gitea repositories…"
do {
let repositories = try await GiteaRepositoryLookup.references(client: repositoryClient)
let client: any MCPToolClient
if let repositoryClient { client = repositoryClient }
else { client = try await GiteaRepositoryLookup.configuredClient() }
let repositories = try await GiteaRepositoryLookup.references(client: client)
try Task.checkCancellation()
systemParts.append(repositories)
webStatus = "Gitea repositories checked."
@@ -553,6 +560,7 @@ final class StudioModel: ObservableObject {
private func loadMessages() {
messages = []
webStatus = ""
webSearchLink = nil
knowledgeStatus = ""
toolAttachment = nil
documents = []