Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
206 lines (183 loc) · 7.41 KB
/
Copy pathindex.ts
File metadata and controls
206 lines (183 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { tool, type Plugin } from "@opencode-ai/plugin"
import { readFile } from "node:fs/promises"
import { dirname, join } from "node:path"
import { fileURLToPath } from "node:url"
import { parse as parseYaml } from "yaml"
import type { Context as V2Context, Plugin as V2Plugin } from "@opencode/plugin/promise/plugin"
import type { Skill as V2Skill } from "@opencode/plugin/promise/index"
import { queryParamFor } from "./engines"
const SERPAPI_MCP_URL = "https://mcp.serpapi.com/mcp"
const CURRENT_DIR = dirname(fileURLToPath(import.meta.url))
type SearchParams = Record<string, string>
type SearchArguments = {
engine?: string
q?: string
params?: Record<string, string | number | boolean>
}
const SERPAPI_PLUGIN_ID = "opencode-serpapi"
const SERPAPI_SKILL_PATH = join(CURRENT_DIR, "skills", "serpapi-web-search", "SKILL.md")
function parseSkill(source: string) {
const frontmatter = source.match(/^---\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/)
if (!frontmatter) throw new Error(`Missing YAML frontmatter in ${SERPAPI_SKILL_PATH}`)
const value: unknown = parseYaml(frontmatter[1])
const metadata = typeof value === "object" && value !== null && !Array.isArray(value)
? value as Record<string, unknown>
: {}
return {
name: typeof metadata.name === "string" ? metadata.name : "serpapi-web-search",
description: typeof metadata.description === "string"
? metadata.description
: "SerpApi search-engine routing, credentials, and response guidance.",
content: source.slice(frontmatter[0].length).trim(),
}
}
async function executeSearch(args: SearchArguments, signal: AbortSignal): Promise<string> {
const apiKey = process.env.SERPAPI_API_KEY
if (!apiKey) return "SERPAPI_API_KEY is not available to the OpenCode process."
const engine = args.engine?.trim() || "google_light"
const queryParam = queryParamFor(engine)
const requestParams: Record<string, string> = {
engine,
...(args.params ? Object.fromEntries(Object.entries(args.params).map(([key, value]) => [key, String(value)])) : {}),
}
if (args.q && !requestParams[queryParam]) requestParams[queryParam] = args.q
delete requestParams.api_key
try {
return await searchWithRestApi(requestParams, apiKey, signal)
} catch (error) {
const message = (error instanceof Error ? error.message : String(error)).replaceAll(apiKey, "[REDACTED]")
return `SerpApi search failed: ${message}`
}
}
async function searchWithRestApi(
params: SearchParams,
apiKey: string,
signal: AbortSignal,
): Promise<string> {
const url = new URL("https://serpapi.com/search.json")
for (const [key, value] of Object.entries(params)) url.searchParams.set(key, value)
url.searchParams.set("api_key", apiKey)
const response = await fetch(url, { signal })
const output = await response.text()
if (!response.ok) throw new Error(`HTTP ${response.status}: ${output}`)
return output
}
async function executeWebSearch(query: string, signal: AbortSignal) {
const apiKey = process.env.SERPAPI_API_KEY
if (!apiKey) throw new Error("SERPAPI_API_KEY is not available to the OpenCode process.")
try {
const output = await searchWithRestApi({ engine: "google_light", q: query }, apiKey, signal)
const response: unknown = JSON.parse(output)
if (typeof response !== "object" || response === null || !("organic_results" in response)) return []
const results = response.organic_results
if (!Array.isArray(results)) return []
return results.flatMap((result) => {
if (typeof result !== "object" || result === null || !("link" in result) || typeof result.link !== "string") {
return []
}
return [{
url: result.link,
...("title" in result && typeof result.title === "string" ? { title: result.title } : {}),
...("snippet" in result && typeof result.snippet === "string" ? { content: result.snippet } : {}),
time: {},
}]
})
} catch (error) {
const message = (error instanceof Error ? error.message : String(error)).replaceAll(apiKey, "[REDACTED]")
throw new Error(`SerpApi web search failed: ${message}`)
}
}
export const SerpApiPlugin: Plugin = async () => {
return {
tool: {
serpapi_search: tool({
description:
"Search the web and supported search engines through the SerpApi HTTPS API.",
args: {
engine: tool.schema.string().optional().describe("SerpApi engine id; defaults to google_light."),
q: tool.schema.string().optional().describe("Search query."),
params: tool.schema
.record(tool.schema.string(), tool.schema.union([tool.schema.string(), tool.schema.number(), tool.schema.boolean()]))
.optional()
.describe("Additional engine-specific SerpApi parameters."),
},
async execute(args, context) {
return executeSearch(args, context.abort)
},
}),
},
async config(input) {
// OpenCode's published Plugin type may lag its runtime skill support.
// @ts-expect-error skills is supported at runtime but may be absent from older types
input.skills ??= {}
// @ts-expect-error skills is supported at runtime but may be absent from older types
input.skills.paths ??= []
// @ts-expect-error skills is supported at runtime but may be absent from older types
input.skills.paths.push(join(CURRENT_DIR, "skills"))
},
}
}
export const SerpApiV2Plugin: V2Plugin = {
id: SERPAPI_PLUGIN_ID,
async setup(context) {
const skill = parseSkill(await readFile(SERPAPI_SKILL_PATH, "utf8"))
await context.mcp.transform((editor) => {
if (!editor.get("serpapi")) {
editor.set("serpapi", {
type: "remote",
url: SERPAPI_MCP_URL,
oauth: false,
...(process.env.SERPAPI_API_KEY
? { headers: { Authorization: `Bearer ${process.env.SERPAPI_API_KEY}` } }
: {}),
})
}
})
await context.tool.transform((editor) => {
editor.add({
name: "serpapi_rest_search",
description: "Search the web and supported search engines through the SerpApi HTTPS API.",
input: {
type: "object",
properties: {
engine: { type: "string", description: "SerpApi engine id; defaults to google_light." },
q: { type: "string", description: "Search query." },
params: {
type: "object",
additionalProperties: {
anyOf: [{ type: "string" }, { type: "number" }, { type: "boolean" }],
},
description: "Additional engine-specific SerpApi parameters.",
},
},
additionalProperties: false,
},
async execute(input, toolContext) {
return { content: await executeSearch(input as SearchArguments, toolContext.signal) }
},
})
})
await context.websearch.transform((editor) => {
editor.add({
id: "serpapi",
name: "SerpApi",
async execute({ query }, toolContext) {
return executeWebSearch(query, toolContext.signal)
},
})
})
await context.skill.transform((editor) => {
editor.add({
id: "serpapi-web-search" as V2Skill.ID,
name: skill.name as V2Skill.Name,
description: skill.description,
path: SERPAPI_SKILL_PATH as V2Context["location"]["directory"],
content: skill.content,
})
})
},
}
export default {
...SerpApiV2Plugin,
server: SerpApiPlugin,
}