Add web search and page retrieval to Toolbox
This commit is contained in:
@@ -1170,7 +1170,11 @@ struct ToolboxSheet: View {
|
||||
Button("Done") { dismiss() }
|
||||
}
|
||||
.padding()
|
||||
MCPSettings(studio: studio)
|
||||
TabView {
|
||||
MCPSettings(studio: studio).tabItem { Label("MCP Servers", systemImage: "wrench.and.screwdriver") }
|
||||
WebToolsView(studio: studio, done: { dismiss() })
|
||||
.tabItem { Label("Web", systemImage: "globe") }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1610,3 +1614,97 @@ struct WorkspaceDataView: View {
|
||||
} catch { status = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
|
||||
struct WebToolsView: View {
|
||||
@ObservedObject var studio: StudioModel
|
||||
let done: () -> Void
|
||||
@AppStorage("webSearchEndpoint") private var endpoint = ""
|
||||
@State private var query = ""
|
||||
@State private var address = ""
|
||||
@State private var results: [WebSearchResult] = []
|
||||
@State private var page: WebPage?
|
||||
@State private var status = ""
|
||||
@State private var operation: Task<Void, Never>?
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section("Web Search") {
|
||||
TextField("SearXNG search URL", text: $endpoint, prompt: Text("https://your-server/search"))
|
||||
TextField("Search query", text: $query)
|
||||
HStack {
|
||||
Button("Search") {
|
||||
let source = endpoint
|
||||
let term = query
|
||||
run {
|
||||
results = []
|
||||
results = try await WebSearch.results(endpoint: source, query: term)
|
||||
if results.isEmpty { status = "No usable search results." }
|
||||
}
|
||||
}.disabled(operation != nil || endpoint.isEmpty || query.isEmpty)
|
||||
Button("Search in Browser") { searchInBrowser() }.disabled(query.isEmpty)
|
||||
if operation != nil { Button("Cancel") { operation?.cancel() } }
|
||||
}
|
||||
Text("In-app search requires a SearXNG server with JSON output enabled.")
|
||||
.foregroundStyle(.secondary)
|
||||
ForEach(results) { result in
|
||||
VStack(alignment: .leading, spacing: 5) {
|
||||
Link(result.title, destination: result.url)
|
||||
Text(result.url.host ?? "").font(.caption).foregroundStyle(.secondary)
|
||||
if !result.snippet.isEmpty { Text(result.snippet) }
|
||||
HStack {
|
||||
Button("Retrieve") { retrieve(result.url.absoluteString) }.disabled(operation != nil)
|
||||
Button("Attach Result") { attach(result.title, result.reference) }
|
||||
.disabled(studio.running || operation != nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Section("Retrieve Page") {
|
||||
TextField("Page URL", text: $address, prompt: Text("https://example.com/article"))
|
||||
Button("Retrieve Page") { retrieve(address) }.disabled(operation != nil || address.isEmpty)
|
||||
if let page {
|
||||
Link(page.url.absoluteString, destination: page.url).lineLimit(2)
|
||||
ScrollView {
|
||||
Text(page.text).textSelection(.enabled).frame(maxWidth: .infinity, alignment: .leading)
|
||||
}.frame(height: 145)
|
||||
Button("Attach Page to Chat") { attach("Web page", page.reference) }
|
||||
.disabled(studio.running || operation != nil)
|
||||
}
|
||||
}
|
||||
if !status.isEmpty { Text(status).foregroundStyle(.secondary).textSelection(.enabled) }
|
||||
}
|
||||
.formStyle(.grouped)
|
||||
.onDisappear { operation?.cancel() }
|
||||
}
|
||||
|
||||
private func searchInBrowser() {
|
||||
var url = URLComponents(string: "https://duckduckgo.com/")
|
||||
url?.queryItems = [URLQueryItem(name: "q", value: query)]
|
||||
if let destination = url?.url { NSWorkspace.shared.open(destination) }
|
||||
}
|
||||
|
||||
private func retrieve(_ source: String) {
|
||||
address = source
|
||||
run {
|
||||
page = nil
|
||||
page = try await WebClipCapture.page(from: source)
|
||||
}
|
||||
}
|
||||
|
||||
private func run(_ work: @escaping @MainActor () async throws -> Void) {
|
||||
guard operation == nil else { return }
|
||||
status = "Loading…"
|
||||
operation = Task {
|
||||
defer { operation = nil }
|
||||
do {
|
||||
status = ""
|
||||
try await work()
|
||||
} catch { status = Task.isCancelled ? "Cancelled." : error.localizedDescription }
|
||||
}
|
||||
}
|
||||
|
||||
private func attach(_ title: String, _ reference: String) {
|
||||
studio.attachToolResult(name: title, output: reference)
|
||||
done()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user