Skip to content

Open source CapCut alternative.

TypeScript

feat(site): web search endpoint via Gemini Google Search grounding

David Le0ec3521688ee
diff --git a/site/src/app/api/web/search/route.ts b/site/src/app/api/web/search/route.ts
new file mode 100644
index 00000000..ddc3c4eb
--- /dev/null
+++ b/site/src/app/api/web/search/route.ts
@@ -0,0 +1,37 @@
+import { NextResponse } from "next/server";
+import { z } from "zod";
+
+import { withDonkeyAuth } from "@/lib/donkey-api-auth";
+import {
+ requireInferenceClientId,
+ validationErrorResponse,
+} from "@/lib/inference/responses";
+import { searchWeb } from "@/lib/inference/web-search";
+
+export const dynamic = "force-dynamic";
+
+const webSearchRequestSchema = z.object({
+ query: z.string().trim().min(1).max(500),
+});
+
+// Web search for the Mac app's web.search tool. Runs Gemini's Google Search grounding on Vertex with
+// the backend's service-account credential, so no key reaches the app. Provider-neutral in/out: a
+// query in, a grounded summary plus ranked source pages out.
+export const POST = withDonkeyAuth(async (request) => {
+ const client = requireInferenceClientId(request.donkey.clientId);
+ if (!client.ok) {
+ return client.response;
+ }
+
+ const parsed = webSearchRequestSchema.safeParse(await request.json());
+ if (!parsed.success) {
+ return validationErrorResponse(parsed.error);
+ }
+
+ const result = await searchWeb(parsed.data.query);
+ if (!result) {
+ return NextResponse.json({ summary: "", sources: [] }, { headers: { "Cache-Control": "no-store" } });
+ }
+
+ return NextResponse.json(result, { headers: { "Cache-Control": "no-store" } });
+});
diff --git a/site/src/lib/inference/web-search.ts b/site/src/lib/inference/web-search.ts
new file mode 100644
index 00000000..6f77049a
--- /dev/null
+++ b/site/src/lib/inference/web-search.ts
@@ -0,0 +1,69 @@
+import { GoogleGenAI } from "@google/genai";
+
+import { geminiModelRoles } from "@/lib/inference/gemini-models";
+import { geminiClientConfig } from "@/lib/inference/screenshot-parsing/gemini-flash";
+
+export type WebSearchSource = {
+ title: string;
+ url: string;
+};
+
+export type WebSearchResult = {
+ /** A short grounded answer to the query (from the model). */
+ summary: string;
+ /** The web sources the answer is grounded in. */
+ sources: WebSearchSource[];
+};
+
+/**
+ * Web search via Gemini's built-in Google Search grounding on Vertex AI. This uses the backend's
+ * service-account credential (the same one behind live-token and screenshot parsing) — no API key
+ * and no separate Programmable Search Engine. The model searches the web for the query and returns a
+ * grounded summary plus the source pages it used, which the caller turns into ranked results.
+ */
+export async function searchWeb(query: string): Promise<WebSearchResult | null> {
+ const config = geminiClientConfig();
+ if (!config.configured) {
+ return null;
+ }
+ const client = new GoogleGenAI(config.options);
+
+ const response = await client.models.generateContent({
+ model: geminiModelRoles.chat,
+ contents: [
+ {
+ role: "user",
+ parts: [
+ {
+ text:
+ `Search the web and answer concisely: ${query}\n` +
+ "Base the answer only on current web results.",
+ },
+ ],
+ },
+ ],
+ config: {
+ tools: [{ googleSearch: {} }],
+ temperature: 0,
+ },
+ });
+
+ const candidate = response.candidates?.[0];
+ const summary = (response.text ?? "").trim();
+ const chunks = candidate?.groundingMetadata?.groundingChunks ?? [];
+ const sources: WebSearchSource[] = [];
+ const seen = new Set<string>();
+ for (const chunk of chunks) {
+ const web = chunk.web;
+ if (!web?.uri || seen.has(web.uri)) {
+ continue;
+ }
+ seen.add(web.uri);
+ sources.push({ title: web.title ?? web.uri, url: web.uri });
+ }
+
+ if (!summary && sources.length === 0) {
+ return null;
+ }
+ return { summary, sources };
+}