mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-18 14:25:51 +00:00
* feat: 第一版大重构 * fix: 修复类型问题 * chore: 更新版本到 1.3.2 * Add brave as alternative WebSearchTool * fix: 修正顺序 * fix: 修复对穷鬼模式的 auto dream 和 session memory 越过 * feat: 穷鬼模式去除 session-summary * feat: 创建 builtin-tools 包,搬运所有工具实现 将 src/tools/ 下的全部 60 个工具目录迁移至 packages/builtin-tools/src/tools/, 内部导入路径已更新为 src/ alias 模式。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 更新 src/ 中所有工具引用至 builtin-tools 包,删除 src/tools/ - src/tools.ts 及 178 个 src/ 文件的 import 路径从 ./tools/ 改为 builtin-tools/tools/ - 删除 src/tools/ 整个目录(已迁移至 packages/builtin-tools/) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: 添加 builtin-tools 路径别名至 tsconfig,更新 bun.lock - tsconfig.json 新增 builtin-tools/* 和 builtin-tools 路径映射 - 新增 packages/builtin-tools/src 至 include Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 为 builtin-tools、mcp-client、agent-tools 添加 @claude-code-best 作用域前缀 所有包名及 import 路径统一添加 @claude-code-best/ 前缀: - builtin-tools → @claude-code-best/builtin-tools - mcp-client → @claude-code-best/mcp-client - agent-tools → @claude-code-best/agent-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: 修复 node 环境没有 bun 的问题 --------- Co-authored-by: Eric-Guo <eric.guocz@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
222 lines
6.4 KiB
TypeScript
222 lines
6.4 KiB
TypeScript
import type { PermissionResult } from 'src/utils/permissions/PermissionResult.js'
|
|
import { z } from 'zod/v4'
|
|
import { buildTool, type ToolDef } from 'src/Tool.js'
|
|
import { lazySchema } from 'src/utils/lazySchema.js'
|
|
import { jsonStringify } from 'src/utils/slowOperations.js'
|
|
import { createAdapter } from './adapters/index.js'
|
|
import { getWebSearchPrompt, WEB_SEARCH_TOOL_NAME } from './prompt.js'
|
|
import {
|
|
getToolUseSummary,
|
|
renderToolResultMessage,
|
|
renderToolUseMessage,
|
|
renderToolUseProgressMessage,
|
|
} from './UI.js'
|
|
|
|
const inputSchema = lazySchema(() =>
|
|
z.strictObject({
|
|
query: z.string().min(2).describe('The search query to use'),
|
|
allowed_domains: z
|
|
.array(z.string())
|
|
.optional()
|
|
.describe('Only include search results from these domains'),
|
|
blocked_domains: z
|
|
.array(z.string())
|
|
.optional()
|
|
.describe('Never include search results from these domains'),
|
|
}),
|
|
)
|
|
type InputSchema = ReturnType<typeof inputSchema>
|
|
|
|
const searchResultSchema = lazySchema(() => {
|
|
const searchHitSchema = z.object({
|
|
title: z.string().describe('The title of the search result'),
|
|
url: z.string().describe('The URL of the search result'),
|
|
snippet: z.string().optional().describe('A short description of the search result'),
|
|
})
|
|
|
|
return z.object({
|
|
tool_use_id: z.string().describe('ID of the tool use'),
|
|
content: z.array(searchHitSchema).describe('Array of search hits'),
|
|
})
|
|
})
|
|
|
|
export type SearchResult = z.infer<ReturnType<typeof searchResultSchema>>
|
|
|
|
const outputSchema = lazySchema(() =>
|
|
z.object({
|
|
query: z.string().describe('The search query that was executed'),
|
|
results: z
|
|
.array(z.union([searchResultSchema(), z.string()]))
|
|
.describe('Search results and/or text commentary from the model'),
|
|
durationSeconds: z
|
|
.number()
|
|
.describe('Time taken to complete the search operation'),
|
|
}),
|
|
)
|
|
type OutputSchema = ReturnType<typeof outputSchema>
|
|
|
|
export type Output = z.infer<OutputSchema>
|
|
|
|
// Re-export WebSearchProgress from centralized types to break import cycles
|
|
export type { WebSearchProgress } from 'src/types/tools.js'
|
|
|
|
import type { WebSearchProgress } from 'src/types/tools.js'
|
|
|
|
export const WebSearchTool = buildTool({
|
|
name: WEB_SEARCH_TOOL_NAME,
|
|
searchHint: 'search the web for current information',
|
|
maxResultSizeChars: 100_000,
|
|
shouldDefer: true,
|
|
async description(input) {
|
|
return `Claude wants to search the web for: ${input.query}`
|
|
},
|
|
userFacingName() {
|
|
return 'Web Search'
|
|
},
|
|
getToolUseSummary,
|
|
getActivityDescription(input) {
|
|
const summary = getToolUseSummary(input)
|
|
return summary ? `Searching for ${summary}` : 'Searching the web'
|
|
},
|
|
isEnabled() {
|
|
// Always enabled — the adapter factory selects the appropriate backend
|
|
// (API server-side search or Bing fallback) based on provider capabilities.
|
|
return true
|
|
},
|
|
get inputSchema(): InputSchema {
|
|
return inputSchema()
|
|
},
|
|
get outputSchema(): OutputSchema {
|
|
return outputSchema()
|
|
},
|
|
isConcurrencySafe() {
|
|
return true
|
|
},
|
|
isReadOnly() {
|
|
return true
|
|
},
|
|
toAutoClassifierInput(input) {
|
|
return input.query
|
|
},
|
|
async checkPermissions(_input): Promise<PermissionResult> {
|
|
return {
|
|
behavior: 'passthrough',
|
|
message: 'WebSearchTool requires permission.',
|
|
suggestions: [
|
|
{
|
|
type: 'addRules',
|
|
rules: [{ toolName: WEB_SEARCH_TOOL_NAME }],
|
|
behavior: 'allow',
|
|
destination: 'localSettings',
|
|
},
|
|
],
|
|
}
|
|
},
|
|
async prompt() {
|
|
return getWebSearchPrompt()
|
|
},
|
|
renderToolUseMessage,
|
|
renderToolUseProgressMessage,
|
|
renderToolResultMessage,
|
|
extractSearchText() {
|
|
return ''
|
|
},
|
|
async validateInput(input) {
|
|
const { query, allowed_domains, blocked_domains } = input
|
|
if (!query.length) {
|
|
return {
|
|
result: false,
|
|
message: 'Error: Missing query',
|
|
errorCode: 1,
|
|
}
|
|
}
|
|
if (allowed_domains?.length && blocked_domains?.length) {
|
|
return {
|
|
result: false,
|
|
message:
|
|
'Error: Cannot specify both allowed_domains and blocked_domains in the same request',
|
|
errorCode: 2,
|
|
}
|
|
}
|
|
return { result: true }
|
|
},
|
|
async call(input, context, _canUseTool, _parentMessage, onProgress) {
|
|
const startTime = performance.now()
|
|
const { query } = input
|
|
|
|
const adapter = createAdapter()
|
|
const adapterResults = await adapter.search(query, {
|
|
allowedDomains: input.allowed_domains,
|
|
blockedDomains: input.blocked_domains,
|
|
signal: context.abortController.signal,
|
|
onProgress(progress) {
|
|
if (onProgress) {
|
|
const progressCounter = Date.now()
|
|
onProgress({
|
|
toolUseID: `search-progress-${progressCounter}`,
|
|
data: progress,
|
|
})
|
|
}
|
|
},
|
|
})
|
|
|
|
const endTime = performance.now()
|
|
const durationSeconds = (endTime - startTime) / 1000
|
|
|
|
// Convert adapter SearchResult[] to legacy Output format
|
|
const results: (SearchResult | string)[] = []
|
|
if (adapterResults.length > 0) {
|
|
results.push({
|
|
tool_use_id: 'adapter-search-1',
|
|
content: adapterResults.map(r => ({ title: r.title, url: r.url, snippet: r.snippet })),
|
|
})
|
|
} else {
|
|
results.push('No search results found.')
|
|
}
|
|
|
|
const data: Output = {
|
|
query,
|
|
results,
|
|
durationSeconds,
|
|
}
|
|
return { data }
|
|
},
|
|
mapToolResultToToolResultBlockParam(output, toolUseID) {
|
|
const { query, results } = output
|
|
|
|
let formattedOutput = `Web search results for query: "${query}"\n\n`
|
|
|
|
;(results ?? []).forEach(result => {
|
|
if (result == null) {
|
|
return
|
|
}
|
|
if (typeof result === 'string') {
|
|
formattedOutput += result + '\n\n'
|
|
} else {
|
|
if (result.content?.length > 0) {
|
|
formattedOutput += 'Links:\n'
|
|
for (const link of result.content) {
|
|
formattedOutput += ` - [${link.title}](${link.url})`
|
|
if (link.snippet) {
|
|
formattedOutput += `: ${link.snippet}`
|
|
}
|
|
formattedOutput += '\n'
|
|
}
|
|
formattedOutput += '\n'
|
|
} else {
|
|
formattedOutput += 'No links found.\n\n'
|
|
}
|
|
}
|
|
})
|
|
|
|
formattedOutput +=
|
|
'\nREMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.'
|
|
|
|
return {
|
|
tool_use_id: toolUseID,
|
|
type: 'tool_result',
|
|
content: formattedOutput.trim(),
|
|
}
|
|
},
|
|
} satisfies ToolDef<InputSchema, Output, WebSearchProgress>)
|