|
| 1 | +import z from "zod" |
| 2 | +import { Tool } from "./tool" |
| 3 | +import DESCRIPTION from "./serpersearch.txt" |
| 4 | +import { abortAfterAny } from "../util/abort" |
| 5 | + |
| 6 | +const NUM_RESULTS_PER_QUERY = 5 |
| 7 | + |
| 8 | +interface SerperResponse { |
| 9 | + knowledgeGraph?: { |
| 10 | + title?: string |
| 11 | + description?: string |
| 12 | + attributes?: Record<string, string> |
| 13 | + } |
| 14 | + organic?: Array<{ |
| 15 | + title?: string |
| 16 | + link?: string |
| 17 | + snippet?: string |
| 18 | + }> |
| 19 | + peopleAlsoAsk?: Array<{ |
| 20 | + question?: string |
| 21 | + snippet?: string |
| 22 | + }> |
| 23 | +} |
| 24 | + |
| 25 | +function formatSerperResults(data: SerperResponse, query: string): string { |
| 26 | + const sections: string[] = [] |
| 27 | + |
| 28 | + const kg = data.knowledgeGraph |
| 29 | + if (kg) { |
| 30 | + const kgLines: string[] = [] |
| 31 | + const title = kg.title?.trim() |
| 32 | + if (title) kgLines.push(`Knowledge Graph: ${title}`) |
| 33 | + const description = kg.description?.trim() |
| 34 | + if (description) kgLines.push(description) |
| 35 | + const attributes = kg.attributes ?? {} |
| 36 | + for (const [key, value] of Object.entries(attributes)) { |
| 37 | + const text = String(value).trim() |
| 38 | + if (text) kgLines.push(`${key}: ${text}`) |
| 39 | + } |
| 40 | + if (kgLines.length) sections.push(kgLines.join("\n")) |
| 41 | + } |
| 42 | + |
| 43 | + for (const [index, result] of (data.organic ?? []).slice(0, NUM_RESULTS_PER_QUERY).entries()) { |
| 44 | + const title = result.title?.trim() || "Untitled" |
| 45 | + const lines = [`Result ${index}: ${title}`] |
| 46 | + const link = result.link?.trim() |
| 47 | + if (link) lines.push(`URL: ${link}`) |
| 48 | + const snippet = result.snippet?.trim() |
| 49 | + if (snippet) lines.push(snippet) |
| 50 | + sections.push(lines.join("\n")) |
| 51 | + } |
| 52 | + |
| 53 | + const peopleAlsoAsk = data.peopleAlsoAsk ?? [] |
| 54 | + if (peopleAlsoAsk.length) { |
| 55 | + const maxQuestions = Math.max(1, Math.min(3, peopleAlsoAsk.length)) |
| 56 | + const questions: string[] = [] |
| 57 | + for (const item of peopleAlsoAsk.slice(0, maxQuestions)) { |
| 58 | + const question = item.question?.trim() |
| 59 | + if (!question) continue |
| 60 | + let entry = `Q: ${question}` |
| 61 | + const answer = item.snippet?.trim() |
| 62 | + if (answer) entry += `\nA: ${answer}` |
| 63 | + questions.push(entry) |
| 64 | + } |
| 65 | + if (questions.length) sections.push("People Also Ask:\n" + questions.join("\n")) |
| 66 | + } |
| 67 | + |
| 68 | + if (!sections.length) return `No results returned for query: ${query}` |
| 69 | + |
| 70 | + return sections.join("\n\n---\n\n") |
| 71 | +} |
| 72 | + |
| 73 | +async function fetchSerperSearch(query: string, apiKey: string, signal: AbortSignal): Promise<string> { |
| 74 | + const response = await fetch("https://google.serper.dev/search", { |
| 75 | + method: "POST", |
| 76 | + headers: { |
| 77 | + "X-API-KEY": apiKey, |
| 78 | + "Content-Type": "application/json", |
| 79 | + }, |
| 80 | + body: JSON.stringify({ q: query }), |
| 81 | + signal, |
| 82 | + }) |
| 83 | + |
| 84 | + if (!response.ok) { |
| 85 | + const errorText = await response.text() |
| 86 | + throw new Error(`Serper search error (${response.status}): ${errorText}`) |
| 87 | + } |
| 88 | + |
| 89 | + const data: SerperResponse = await response.json() |
| 90 | + return formatSerperResults(data, query) |
| 91 | +} |
| 92 | + |
| 93 | +export const SerperSearchTool = Tool.define("serpersearch", async () => { |
| 94 | + return { |
| 95 | + get description() { |
| 96 | + return DESCRIPTION.replace("{{year}}", new Date().getFullYear().toString()) |
| 97 | + }, |
| 98 | + parameters: z.object({ |
| 99 | + queries: z |
| 100 | + .array(z.string()) |
| 101 | + .min(1) |
| 102 | + .max(10) |
| 103 | + .describe( |
| 104 | + "Google search queries (up to 10). Use multiple queries to search different angles in parallel.", |
| 105 | + ), |
| 106 | + }), |
| 107 | + async execute(params, ctx) { |
| 108 | + const apiKey = process.env.SERPER_API_KEY |
| 109 | + if (!apiKey) { |
| 110 | + throw new Error("SERPER_API_KEY environment variable is not set") |
| 111 | + } |
| 112 | + |
| 113 | + await ctx.ask({ |
| 114 | + permission: "serpersearch", |
| 115 | + patterns: params.queries, |
| 116 | + always: ["*"], |
| 117 | + metadata: { queries: params.queries }, |
| 118 | + }) |
| 119 | + |
| 120 | + const { signal, clearTimeout } = abortAfterAny(45000, ctx.abort) |
| 121 | + |
| 122 | + try { |
| 123 | + const results = await Promise.all( |
| 124 | + params.queries.slice(0, 10).map((query) => fetchSerperSearch(query, apiKey, signal)), |
| 125 | + ) |
| 126 | + |
| 127 | + clearTimeout() |
| 128 | + |
| 129 | + const output = results |
| 130 | + .map((result, i) => { |
| 131 | + const query = params.queries[i] |
| 132 | + return `Results for query "${query}":\n\n${result}` |
| 133 | + }) |
| 134 | + .join("\n\n---\n\n") |
| 135 | + |
| 136 | + return { |
| 137 | + output, |
| 138 | + title: `Google Search (${params.queries.length} ${params.queries.length === 1 ? "query" : "queries"})`, |
| 139 | + metadata: {}, |
| 140 | + } |
| 141 | + } catch (error) { |
| 142 | + clearTimeout() |
| 143 | + |
| 144 | + if (error instanceof Error && error.name === "AbortError") { |
| 145 | + throw new Error("Search request timed out") |
| 146 | + } |
| 147 | + |
| 148 | + throw error |
| 149 | + } |
| 150 | + }, |
| 151 | + } |
| 152 | +}) |
0 commit comments