fix: WebSearch return data.
Browse files
log.md
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
src/tools/WebSearchTool/WebSearchTool.ts
CHANGED
|
@@ -286,10 +286,9 @@ export const WebSearchTool = buildTool({
|
|
| 286 |
},
|
| 287 |
|
| 288 |
async checkPermissions(): Promise<PermissionResult> {
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
return result
|
| 293 |
},
|
| 294 |
|
| 295 |
async prompt() {
|
|
@@ -322,7 +321,6 @@ export const WebSearchTool = buildTool({
|
|
| 322 |
|
| 323 |
logToMarkdown('INFO', 'WebSearchTool.call() 被调用')
|
| 324 |
logToMarkdown('DEBUG', '输入参数', input)
|
| 325 |
-
logToMarkdown('DEBUG', '上下文信息', { hasContext: !!_context, hasCanUseTool: !!_canUseTool, hasParentMessage: !!_parentMessage, hasOnProgress: !!onProgress })
|
| 326 |
|
| 327 |
// 断言:input 必须存在
|
| 328 |
console.assert(
|
|
@@ -335,9 +333,11 @@ export const WebSearchTool = buildTool({
|
|
| 335 |
if (!input?.query || input.query.trim() === '') {
|
| 336 |
logToMarkdown('WARN', '查询参数为空,返回错误')
|
| 337 |
return {
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
|
|
|
|
|
|
| 341 |
}
|
| 342 |
}
|
| 343 |
|
|
@@ -393,12 +393,13 @@ export const WebSearchTool = buildTool({
|
|
| 393 |
}
|
| 394 |
logToMarkdown('INFO', '搜索成功完成')
|
| 395 |
logToMarkdown('INFO', '最终统计', finalStats)
|
| 396 |
-
logToMarkdown('DEBUG', '返回结果结构', output)
|
| 397 |
|
| 398 |
return {
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
|
|
|
|
|
|
| 402 |
}
|
| 403 |
} catch (error) {
|
| 404 |
const duration = (performance.now() - start) / 1000
|
|
@@ -413,20 +414,17 @@ export const WebSearchTool = buildTool({
|
|
| 413 |
logError(error)
|
| 414 |
|
| 415 |
return {
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
|
|
|
|
|
|
| 419 |
}
|
| 420 |
}
|
| 421 |
},
|
| 422 |
|
| 423 |
mapToolResultToToolResultBlockParam(output, toolUseID) {
|
| 424 |
-
logToMarkdown('INFO', 'mapToolResultToToolResultBlockParam() 被调用')
|
| 425 |
-
logToMarkdown('DEBUG', 'toolUseID', toolUseID)
|
| 426 |
-
logToMarkdown('DEBUG', 'output 参数', output)
|
| 427 |
-
|
| 428 |
if (!output) {
|
| 429 |
-
logToMarkdown('WARN', 'output 为空,返回错误')
|
| 430 |
return {
|
| 431 |
tool_use_id: toolUseID,
|
| 432 |
type: 'tool_result',
|
|
@@ -434,50 +432,22 @@ export const WebSearchTool = buildTool({
|
|
| 434 |
}
|
| 435 |
}
|
| 436 |
|
| 437 |
-
logToMarkdown('DEBUG', '开始格式化结果')
|
| 438 |
-
logToMarkdown('DEBUG', '查询内容', output.query)
|
| 439 |
-
logToMarkdown('DEBUG', '结果数量', output.results.length)
|
| 440 |
-
|
| 441 |
let text = `Results for "${output.query}"\n\n`
|
| 442 |
|
| 443 |
-
for (
|
| 444 |
-
const r = output.results[i]
|
| 445 |
-
logToMarkdown('DEBUG', `处理结果 ${i + 1}/${output.results.length}`, {
|
| 446 |
-
type: typeof r,
|
| 447 |
-
isString: typeof r === 'string',
|
| 448 |
-
hasContent: !!(r as any).content
|
| 449 |
-
})
|
| 450 |
-
|
| 451 |
if (typeof r === 'string') {
|
| 452 |
text += r + '\n\n'
|
| 453 |
-
logToMarkdown('DEBUG', `结果 ${i + 1} 是字符串:`, r)
|
| 454 |
} else {
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
content.forEach((item: any, j: number) => {
|
| 459 |
-
const itemText = `${j + 1}. ${item.title}\n${item.url}\n${item.snippet || ''}\n\n`
|
| 460 |
-
text += itemText
|
| 461 |
-
logToMarkdown('DEBUG', `添加项目 ${j + 1}:`, {
|
| 462 |
-
hasTitle: !!item.title,
|
| 463 |
-
hasUrl: !!item.url,
|
| 464 |
-
hasSnippet: !!item.snippet
|
| 465 |
-
})
|
| 466 |
})
|
| 467 |
}
|
| 468 |
}
|
| 469 |
|
| 470 |
-
|
| 471 |
tool_use_id: toolUseID,
|
| 472 |
type: 'tool_result',
|
| 473 |
content: text,
|
| 474 |
}
|
| 475 |
-
|
| 476 |
-
logToMarkdown('INFO', '格式化完成')
|
| 477 |
-
logToMarkdown('DEBUG', '最终结果类型', typeof finalResult.content)
|
| 478 |
-
logToMarkdown('DEBUG', '最终结果长度', finalResult.content.length)
|
| 479 |
-
logToMarkdown('DEBUG', '最终结果预览 (前100字符)', finalResult.content.slice(0, 100))
|
| 480 |
-
|
| 481 |
-
return finalResult
|
| 482 |
},
|
| 483 |
}) satisfies ToolDef<any, Output, WebSearchProgress>
|
|
|
|
| 286 |
},
|
| 287 |
|
| 288 |
async checkPermissions(): Promise<PermissionResult> {
|
| 289 |
+
return {
|
| 290 |
+
behavior: 'allow',
|
| 291 |
+
}
|
|
|
|
| 292 |
},
|
| 293 |
|
| 294 |
async prompt() {
|
|
|
|
| 321 |
|
| 322 |
logToMarkdown('INFO', 'WebSearchTool.call() 被调用')
|
| 323 |
logToMarkdown('DEBUG', '输入参数', input)
|
|
|
|
| 324 |
|
| 325 |
// 断言:input 必须存在
|
| 326 |
console.assert(
|
|
|
|
| 333 |
if (!input?.query || input.query.trim() === '') {
|
| 334 |
logToMarkdown('WARN', '查询参数为空,返回错误')
|
| 335 |
return {
|
| 336 |
+
data: {
|
| 337 |
+
query: input?.query || '',
|
| 338 |
+
results: ['Error: Missing query'],
|
| 339 |
+
durationSeconds: (performance.now() - start) / 1000,
|
| 340 |
+
},
|
| 341 |
}
|
| 342 |
}
|
| 343 |
|
|
|
|
| 393 |
}
|
| 394 |
logToMarkdown('INFO', '搜索成功完成')
|
| 395 |
logToMarkdown('INFO', '最终统计', finalStats)
|
|
|
|
| 396 |
|
| 397 |
return {
|
| 398 |
+
data: {
|
| 399 |
+
query: input.query,
|
| 400 |
+
results: output,
|
| 401 |
+
durationSeconds: duration,
|
| 402 |
+
},
|
| 403 |
}
|
| 404 |
} catch (error) {
|
| 405 |
const duration = (performance.now() - start) / 1000
|
|
|
|
| 414 |
logError(error)
|
| 415 |
|
| 416 |
return {
|
| 417 |
+
data: {
|
| 418 |
+
query: input?.query || '',
|
| 419 |
+
results: [`Error: ${errorMessage}`],
|
| 420 |
+
durationSeconds: duration,
|
| 421 |
+
},
|
| 422 |
}
|
| 423 |
}
|
| 424 |
},
|
| 425 |
|
| 426 |
mapToolResultToToolResultBlockParam(output, toolUseID) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
if (!output) {
|
|
|
|
| 428 |
return {
|
| 429 |
tool_use_id: toolUseID,
|
| 430 |
type: 'tool_result',
|
|
|
|
| 432 |
}
|
| 433 |
}
|
| 434 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
let text = `Results for "${output.query}"\n\n`
|
| 436 |
|
| 437 |
+
for (const r of output.results) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
if (typeof r === 'string') {
|
| 439 |
text += r + '\n\n'
|
|
|
|
| 440 |
} else {
|
| 441 |
+
r.content.forEach((item: any, i: number) => {
|
| 442 |
+
text += `${i + 1}. ${item.title}\n${item.url}\n${item.snippet || ''}\n\n`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
})
|
| 444 |
}
|
| 445 |
}
|
| 446 |
|
| 447 |
+
return {
|
| 448 |
tool_use_id: toolUseID,
|
| 449 |
type: 'tool_result',
|
| 450 |
content: text,
|
| 451 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
},
|
| 453 |
}) satisfies ToolDef<any, Output, WebSearchProgress>
|
src/tools/WebSearchTool/__tests__/WebSearchTool.test.ts
CHANGED
|
@@ -74,9 +74,9 @@ describe('WebSearchTool', () => {
|
|
| 74 |
)
|
| 75 |
|
| 76 |
expect(result).toBeDefined()
|
| 77 |
-
expect(result.query).toBe('typescript programming')
|
| 78 |
-
expect(Array.isArray(result.results)).toBe(true)
|
| 79 |
-
expect(result.durationSeconds).toBeGreaterThan(0)
|
| 80 |
}, 60000)
|
| 81 |
|
| 82 |
test('should return structured results', async () => {
|
|
@@ -87,9 +87,9 @@ describe('WebSearchTool', () => {
|
|
| 87 |
null
|
| 88 |
)
|
| 89 |
|
| 90 |
-
expect(result.results.length).toBeGreaterThanOrEqual(0)
|
| 91 |
|
| 92 |
-
const first = result.results[0]
|
| 93 |
if (first && typeof first !== 'string') {
|
| 94 |
expect(first).toHaveProperty('tool_use_id')
|
| 95 |
expect(Array.isArray(first.content)).toBe(true)
|
|
@@ -106,7 +106,7 @@ describe('WebSearchTool', () => {
|
|
| 106 |
null
|
| 107 |
)
|
| 108 |
|
| 109 |
-
expect(result.results.some(r => typeof r === 'string' && r.includes('Error'))).toBe(true)
|
| 110 |
})
|
| 111 |
|
| 112 |
test('should not crash on failure', async () => {
|
|
@@ -219,9 +219,9 @@ describe('WebSearchTool', () => {
|
|
| 219 |
)
|
| 220 |
|
| 221 |
expect(result).toBeDefined()
|
| 222 |
-
expect(result.query).toBe('milet 的最新动态')
|
| 223 |
-
expect(Array.isArray(result.results)).toBe(true)
|
| 224 |
-
expect(result.durationSeconds).toBeGreaterThan(0)
|
| 225 |
}, 60000)
|
| 226 |
})
|
| 227 |
})
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
expect(result).toBeDefined()
|
| 77 |
+
expect(result.data.query).toBe('typescript programming')
|
| 78 |
+
expect(Array.isArray(result.data.results)).toBe(true)
|
| 79 |
+
expect(result.data.durationSeconds).toBeGreaterThan(0)
|
| 80 |
}, 60000)
|
| 81 |
|
| 82 |
test('should return structured results', async () => {
|
|
|
|
| 87 |
null
|
| 88 |
)
|
| 89 |
|
| 90 |
+
expect(result.data.results.length).toBeGreaterThanOrEqual(0)
|
| 91 |
|
| 92 |
+
const first = result.data.results[0]
|
| 93 |
if (first && typeof first !== 'string') {
|
| 94 |
expect(first).toHaveProperty('tool_use_id')
|
| 95 |
expect(Array.isArray(first.content)).toBe(true)
|
|
|
|
| 106 |
null
|
| 107 |
)
|
| 108 |
|
| 109 |
+
expect(result.data.results.some(r => typeof r === 'string' && r.includes('Error'))).toBe(true)
|
| 110 |
})
|
| 111 |
|
| 112 |
test('should not crash on failure', async () => {
|
|
|
|
| 219 |
)
|
| 220 |
|
| 221 |
expect(result).toBeDefined()
|
| 222 |
+
expect(result.data.query).toBe('milet 的最新动态')
|
| 223 |
+
expect(Array.isArray(result.data.results)).toBe(true)
|
| 224 |
+
expect(result.data.durationSeconds).toBeGreaterThan(0)
|
| 225 |
}, 60000)
|
| 226 |
})
|
| 227 |
})
|