forked from lessweb/deepcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-search-handler.ts
More file actions
394 lines (345 loc) · 10.8 KB
/
web-search-handler.ts
File metadata and controls
394 lines (345 loc) · 10.8 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { randomUUID } from "crypto";
import { spawn } from "child_process";
import type OpenAI from "openai";
import type { CreateOpenAIClient, ToolExecutionContext, ToolExecutionResult } from "./executor";
const MAX_OUTPUT_CHARS = 30000;
const MAX_CAPTURE_CHARS = 10 * 1024 * 1024;
const WEB_SEARCH_TOOL_ACTIVITY_PREFIX = "WebSearch:";
const DEFAULT_WEB_SEARCH_API_URL = "https://deepcode.vegamo.cn/api/plugin/web-search";
type SearchLanguage = "en" | "zh";
type SearchDecision = {
dominantLanguage: SearchLanguage;
reason: string;
};
type SearchPreparation = {
resolvedQuery: string;
decision: SearchDecision;
translated: boolean;
};
type LLMClientContext = {
client: OpenAI;
model: string;
thinkingEnabled: boolean;
notify?: string;
webSearchTool?: string;
machineId?: string;
};
export async function handleWebSearchTool(
args: Record<string, unknown>,
context: ToolExecutionContext
): Promise<ToolExecutionResult> {
const query = typeof args.query === "string" ? args.query : "";
if (!query.trim()) {
return {
ok: false,
name: "WebSearch",
error: "Missing required \"query\" string."
};
}
const llmContext = context.createOpenAIClient?.();
const scriptPath = llmContext?.webSearchTool?.trim();
if (scriptPath) {
return executeConfiguredWebSearch(query, scriptPath, context);
}
if (!hasUsableClient(llmContext)) {
return {
ok: false,
name: "WebSearch",
error:
"WebSearch default mode requires a valid LLM configuration in ~/.deepcode/settings.json."
};
}
return executeDefaultWebSearch(query, llmContext, context);
}
function hasUsableClient(value: ReturnType<CreateOpenAIClient> | undefined): value is LLMClientContext {
return Boolean(value?.client);
}
async function executeConfiguredWebSearch(
query: string,
scriptPath: string,
context: ToolExecutionContext
): Promise<ToolExecutionResult> {
const execution = await runWebSearchScript(scriptPath, query, context);
const output = execution.stdout.slice(0, MAX_OUTPUT_CHARS);
const truncated = execution.stdout.length > MAX_OUTPUT_CHARS;
if (execution.error) {
return {
ok: false,
name: "WebSearch",
error: execution.error,
output: output || undefined,
metadata: {
exitCode: execution.exitCode,
signal: execution.signal,
stderr: execution.stderr || undefined,
truncated
}
};
}
if (execution.exitCode !== 0 || execution.signal !== null) {
return {
ok: false,
name: "WebSearch",
error: buildCommandError(execution.exitCode, execution.signal),
output: output || undefined,
metadata: {
exitCode: execution.exitCode,
signal: execution.signal,
stderr: execution.stderr || undefined,
truncated
}
};
}
return {
ok: true,
name: "WebSearch",
output: output || undefined,
metadata: {
exitCode: execution.exitCode,
signal: execution.signal,
truncated,
stderr: execution.stderr || undefined
}
};
}
async function executeDefaultWebSearch(
query: string,
llmContext: LLMClientContext,
context: ToolExecutionContext
): Promise<ToolExecutionResult> {
try {
const prepared = await prepareSearchQuery(query, llmContext);
const output = await runDefaultWebSearchRequest(
prepared.resolvedQuery,
llmContext.machineId,
context
);
return {
ok: true,
name: "WebSearch",
output,
metadata: {
originalQuery: query,
resolvedQuery: prepared.resolvedQuery,
translated: prepared.translated,
dominantLanguage: prepared.decision.dominantLanguage,
languageReason: prepared.decision.reason
}
};
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
return {
ok: false,
name: "WebSearch",
error: `WebSearch default mode failed: ${message}`
};
}
}
async function runWebSearchScript(
scriptPath: string,
query: string,
context: ToolExecutionContext
): Promise<{ stdout: string; stderr: string; exitCode: number | null; signal: string | null; error?: string }> {
return new Promise((resolve) => {
const child = spawn(scriptPath, [query], {
cwd: context.projectRoot,
env: process.env,
stdio: ["ignore", "pipe", "pipe"]
});
const pid = child.pid;
if (typeof pid === "number") {
context.onProcessStart?.(pid, formatWebSearchActivityLabel(query));
}
let stdout = "";
let stderr = "";
let error: string | undefined;
child.stdout?.on("data", (chunk: string | Buffer) => {
stdout = appendChunk(stdout, chunk);
});
child.stderr?.on("data", (chunk: string | Buffer) => {
stderr = appendChunk(stderr, chunk);
});
child.on("error", (spawnError) => {
error = spawnError.message;
});
child.on("close", (code, signal) => {
if (typeof pid === "number") {
context.onProcessExit?.(pid);
}
resolve({
stdout,
stderr,
exitCode: typeof code === "number" ? code : null,
signal: signal ?? null,
error
});
});
});
}
async function prepareSearchQuery(query: string, llmContext: LLMClientContext): Promise<SearchPreparation> {
const decision = await decideSearchLanguage(query, llmContext);
const containsChinese = containsChineseChar(query);
if (decision.dominantLanguage === "en" && containsChinese) {
const translatedQuery = await translateQuery(query, "English", llmContext);
if (translatedQuery) {
return {
resolvedQuery: translatedQuery,
decision,
translated: true
};
}
}
if (decision.dominantLanguage === "zh" && !containsChinese) {
const translatedQuery = await translateQuery(query, "Chinese", llmContext);
if (translatedQuery) {
return {
resolvedQuery: translatedQuery,
decision,
translated: true
};
}
}
return {
resolvedQuery: query,
decision,
translated: false
};
}
function containsChineseChar(text: string): boolean {
return /[\u4e00-\u9fff]/.test(text);
}
async function decideSearchLanguage(
query: string,
llmContext: LLMClientContext
): Promise<SearchDecision> {
const prompt = `Decide whether the topic below has more useful online material in English or Chinese.
Topic:
\`\`\`text
${query}
\`\`\`
Return strict JSON:
{"dominant_language":"en"|"zh","reason":"one short sentence"}
Do not include markdown or any extra text.`;
const result = parseJsonResponse(await chat(llmContext, prompt));
const dominantLanguage = result.dominant_language;
if (dominantLanguage !== "en" && dominantLanguage !== "zh") {
throw new Error(`Unexpected dominant language: ${String(dominantLanguage)}`);
}
return {
dominantLanguage,
reason: typeof result.reason === "string" ? result.reason : ""
};
}
async function translateQuery(
query: string,
targetLanguage: "English" | "Chinese",
llmContext: LLMClientContext
): Promise<string> {
const prompt = `Translate the query text below into ${targetLanguage}.
Requirements:
- Preserve product names, library names, API names, versions, and abbreviations when appropriate.
- Return only the translated query, without quotes or explanation.
Query:
\`\`\`text
${query}
\`\`\``;
return stripCodeFence(await chat(llmContext, prompt)).trim().replace(/^['"]|['"]$/g, "");
}
async function chat(llmContext: LLMClientContext, prompt: string): Promise<string> {
const response = await llmContext.client.chat.completions.create({
model: llmContext.model,
messages: [{ role: "user", content: prompt }]
});
const content = response.choices?.[0]?.message?.content as unknown;
if (typeof content === "string") {
return content.trim();
}
if (Array.isArray(content)) {
return (content as Array<{ text?: string }>)
.map((part) => (typeof part.text === "string" ? part.text : ""))
.join("\n")
.trim();
}
return "";
}
function parseJsonResponse(text: string): Record<string, unknown> {
const cleaned = stripCodeFence(text).trim();
try {
return JSON.parse(cleaned) as Record<string, unknown>;
} catch {
const firstBrace = cleaned.indexOf("{");
const lastBrace = cleaned.lastIndexOf("}");
if (firstBrace >= 0 && lastBrace > firstBrace) {
return JSON.parse(cleaned.slice(firstBrace, lastBrace + 1)) as Record<string, unknown>;
}
throw new Error(`Failed to parse JSON response: ${cleaned || "<empty>"}`);
}
}
function stripCodeFence(text: string): string {
const trimmed = text.trim();
const fenceMatch = trimmed.match(/^```(?:[\w-]+)?\n([\s\S]*?)\n```$/);
return fenceMatch ? fenceMatch[1] : trimmed;
}
async function runDefaultWebSearchRequest(
query: string,
machineId: string | undefined,
context: ToolExecutionContext
): Promise<string> {
if (!machineId) {
throw new Error("Missing vscode.env.machineId for the default WebSearch request.");
}
const activityId = `web-search-${randomUUID()}`;
context.onProcessStart?.(activityId, formatWebSearchActivityLabel(query));
try {
const response = await fetch(DEFAULT_WEB_SEARCH_API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Token: machineId
},
body: JSON.stringify({ query })
});
if (!response.ok) {
const body = await response.text().catch(() => "");
throw new Error(
`WebSearch API request failed with status ${response.status}${body ? `: ${body}` : ""}`
);
}
const payload = (await response.json()) as {
success?: unknown;
result?: unknown;
};
if (typeof payload.result === "string" && payload.result.trim()) {
return payload.result.trim();
}
} finally {
context.onProcessExit?.(activityId);
}
throw new Error("The web search response was empty.");
}
function appendChunk(existing: string, chunk: string | Buffer): string {
if (existing.length >= MAX_CAPTURE_CHARS) {
return existing;
}
const text = typeof chunk === "string" ? chunk : chunk.toString("utf8");
const remaining = MAX_CAPTURE_CHARS - existing.length;
return `${existing}${text.slice(0, remaining)}`;
}
function formatWebSearchActivityLabel(query: string): string {
const normalizedQuery = query.replace(/\s+/g, " ").trim();
const maxQueryLength = 180;
const clippedQuery =
normalizedQuery.length > maxQueryLength
? `${normalizedQuery.slice(0, maxQueryLength - 3)}...`
: normalizedQuery;
return `${WEB_SEARCH_TOOL_ACTIVITY_PREFIX} ${clippedQuery}`;
}
function buildCommandError(exitCode: number | null, signal: string | null): string {
if (signal) {
return `WebSearch command terminated by signal ${signal}.`;
}
if (exitCode !== null) {
return `WebSearch command failed with exit code ${exitCode}.`;
}
return "WebSearch command failed.";
}