Skip to content

Open source CapCut alternative.

TypeScript

feat(app): route web.search through the backend service-account endpoint

David Le875d8acc72c2
diff --git a/apps/Donkey/Sources/Donkey/GeminiLiveVoiceController.swift b/apps/Donkey/Sources/Donkey/GeminiLiveVoiceController.swift
index 2edee9df..560ba077 100644
--- a/apps/Donkey/Sources/Donkey/GeminiLiveVoiceController.swift
+++ b/apps/Donkey/Sources/Donkey/GeminiLiveVoiceController.swift
@@ -78,15 +78,16 @@ final class GeminiLiveVoiceController {
// Wire the generic LLM tool into the fast path too, so the Live model can compose/transform
// text mid-task (a tracklist, a clean note body) through the hosted route.
var services = HarnessBuiltInToolServices(commandExecutor: DonkeyCommandBackends.makeExecutor())
+ let webTools = WebTools()
+ services.webFetcher = { await webTools.fetch($0) }
if let backendConfiguration = try? DonkeyBackendInferenceConfiguration.fromEnvironment() {
- let textGenerator = HostedTextGenerator(
- backend: DonkeyBackendInferenceClient(configuration: backendConfiguration)
- )
+ let backend = DonkeyBackendInferenceClient(configuration: backendConfiguration)
+ let textGenerator = HostedTextGenerator(backend: backend)
services.textGenerator = { await textGenerator.generate($0) }
+ // Search goes through the backend (service-account Google Search grounding).
+ let hostedWebSearch = HostedWebSearch(backend: backend)
+ services.webSearcher = { await hostedWebSearch.search($0) }
}
- let webTools = WebTools()
- services.webSearcher = { await webTools.search($0) }
- services.webFetcher = { await webTools.fetch($0) }
let extraToolNames: Set<String> = ["llm.generate", "web.search", "web.fetch"]
let extraDescriptors = BuiltInHarnessToolCatalog.descriptors.filter { extraToolNames.contains($0.name) }
let liveDescriptors = DonkeyCommandLayer.descriptors + extraDescriptors
diff --git a/apps/Donkey/Sources/Donkey/UserQueryCommandHandler.swift b/apps/Donkey/Sources/Donkey/UserQueryCommandHandler.swift
index 095a51fd..acaca56f 100644
--- a/apps/Donkey/Sources/Donkey/UserQueryCommandHandler.swift
+++ b/apps/Donkey/Sources/Donkey/UserQueryCommandHandler.swift
@@ -351,10 +351,11 @@ struct LocalAppUserQueryCommandHandler: UserQueryCommandHandling {
harnessServices.textGenerator = { prompt in
await textGenerator.generate(prompt)
}
- // Web research: find current facts and read pages (e.g. an artist's latest album, then its
- // tracklist), so tasks needing fresh information aren't stuck guessing.
+ // Web research: search goes through the backend (Google Search grounding on the
+ // service-account credential — no key in the app); fetch reads a page directly.
+ let hostedWebSearch = HostedWebSearch(backend: backend)
let webTools = WebTools()
- harnessServices.webSearcher = { query in await webTools.search(query) }
+ harnessServices.webSearcher = { query in await hostedWebSearch.search(query) }
harnessServices.webFetcher = { url in await webTools.fetch(url) }
let granted = Self.userQueryGrantedPermissions
let replacedBuiltIns: Set<String> = ["screen.observe", "elements.get", "element.perform", "text.enter", "keyboard.press"]
diff --git a/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift b/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift
index 949667c8..d066773a 100644
--- a/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift
+++ b/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift
@@ -151,6 +151,16 @@ public struct DonkeyBackendInferenceClient: @unchecked Sendable {
return try JSONDecoder().decode(RemoteInferenceJSONValue.self, from: data)
}
+ /// Web search via the backend's Google Search grounding (service-account credentials stay on the
+ /// server; no key in the app). Returns a grounded summary and the source pages it used.
+ public func searchWeb(query: String) async throws -> RemoteWebSearchResult {
+ var request = makeRequest(path: "/api/web/search/")
+ request.httpMethod = "POST"
+ request.httpBody = try JSONEncoder().encode(["query": query])
+ let data = try await send(request)
+ return try JSONDecoder().decode(RemoteWebSearchResult.self, from: data)
+ }
+
public func parseScreenshot(
_ understandingRequest: LocalUIUnderstandingRequest,
imageData: Data? = nil,
diff --git a/apps/Donkey/Sources/DonkeyAI/HostedWebSearch.swift b/apps/Donkey/Sources/DonkeyAI/HostedWebSearch.swift
new file mode 100644
index 00000000..ea7d75c0
--- /dev/null
+++ b/apps/Donkey/Sources/DonkeyAI/HostedWebSearch.swift
@@ -0,0 +1,54 @@
+import DonkeyContracts
+import DonkeyHarness
+import Foundation
+
+/// The backend's web-search response: a grounded summary plus the source pages the answer used.
+public struct RemoteWebSearchResult: Codable, Sendable {
+ public struct Source: Codable, Sendable {
+ public var title: String
+ public var url: String
+ }
+ public var summary: String
+ public var sources: [Source]
+
+ public init(summary: String, sources: [Source]) {
+ self.summary = summary
+ self.sources = sources
+ }
+}
+
+/// Backs the `web.search` harness tool by calling the Donkey backend's grounded-search endpoint,
+/// where the Google credential lives. Returns a single text block — a short grounded answer followed
+/// by the source pages — for the agent to read and follow up on with web.fetch.
+public struct HostedWebSearch: Sendable {
+ private let backend: DonkeyBackendInferenceClient
+ private let timeoutSeconds: TimeInterval
+
+ public init(backend: DonkeyBackendInferenceClient, timeoutSeconds: TimeInterval = 30) {
+ self.backend = backend
+ self.timeoutSeconds = timeoutSeconds
+ }
+
+ public func search(_ query: String) async -> String? {
+ let backend = self.backend
+ guard let result = try? await AIDeadline.enforce(seconds: timeoutSeconds, {
+ try await backend.searchWeb(query: query)
+ }) else {
+ return nil
+ }
+ var lines: [String] = []
+ let summary = result.summary.trimmingCharacters(in: .whitespacesAndNewlines)
+ if !summary.isEmpty {
+ lines.append(summary)
+ }
+ if !result.sources.isEmpty {
+ lines.append("")
+ lines.append("Sources:")
+ for source in result.sources {
+ lines.append("- \(source.title) — \(source.url)")
+ }
+ }
+ let text = lines.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
+ return text.isEmpty ? nil : text
+ }
+}