mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-18 06:15: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>
319 lines
9.1 KiB
TypeScript
319 lines
9.1 KiB
TypeScript
import { z } from 'zod/v4'
|
|
import { buildTool, type ToolDef } from 'src/Tool.js'
|
|
import type { PermissionUpdate } from 'src/types/permissions.js'
|
|
import { formatFileSize } from 'src/utils/format.js'
|
|
import { lazySchema } from 'src/utils/lazySchema.js'
|
|
import type { PermissionDecision } from 'src/utils/permissions/PermissionResult.js'
|
|
import { getRuleByContentsForTool } from 'src/utils/permissions/permissions.js'
|
|
import { isPreapprovedHost } from './preapproved.js'
|
|
import { DESCRIPTION, WEB_FETCH_TOOL_NAME } from './prompt.js'
|
|
import {
|
|
getToolUseSummary,
|
|
renderToolResultMessage,
|
|
renderToolUseMessage,
|
|
renderToolUseProgressMessage,
|
|
} from './UI.js'
|
|
import {
|
|
applyPromptToMarkdown,
|
|
type FetchedContent,
|
|
getURLMarkdownContent,
|
|
isPreapprovedUrl,
|
|
MAX_MARKDOWN_LENGTH,
|
|
} from './utils.js'
|
|
|
|
const inputSchema = lazySchema(() =>
|
|
z.strictObject({
|
|
url: z.string().url().describe('The URL to fetch content from'),
|
|
prompt: z.string().describe('The prompt to run on the fetched content'),
|
|
}),
|
|
)
|
|
type InputSchema = ReturnType<typeof inputSchema>
|
|
|
|
const outputSchema = lazySchema(() =>
|
|
z.object({
|
|
bytes: z.number().describe('Size of the fetched content in bytes'),
|
|
code: z.number().describe('HTTP response code'),
|
|
codeText: z.string().describe('HTTP response code text'),
|
|
result: z
|
|
.string()
|
|
.describe('Processed result from applying the prompt to the content'),
|
|
durationMs: z
|
|
.number()
|
|
.describe('Time taken to fetch and process the content'),
|
|
url: z.string().describe('The URL that was fetched'),
|
|
}),
|
|
)
|
|
type OutputSchema = ReturnType<typeof outputSchema>
|
|
|
|
export type Output = z.infer<OutputSchema>
|
|
|
|
function webFetchToolInputToPermissionRuleContent(input: {
|
|
[k: string]: unknown
|
|
}): string {
|
|
try {
|
|
const parsedInput = WebFetchTool.inputSchema.safeParse(input)
|
|
if (!parsedInput.success) {
|
|
return `input:${input.toString()}`
|
|
}
|
|
const { url } = parsedInput.data
|
|
const hostname = new URL(url).hostname
|
|
return `domain:${hostname}`
|
|
} catch {
|
|
return `input:${input.toString()}`
|
|
}
|
|
}
|
|
|
|
export const WebFetchTool = buildTool({
|
|
name: WEB_FETCH_TOOL_NAME,
|
|
searchHint: 'fetch and extract content from a URL',
|
|
// 100K chars - tool result persistence threshold
|
|
maxResultSizeChars: 100_000,
|
|
shouldDefer: true,
|
|
async description(input) {
|
|
const { url } = input as { url: string }
|
|
try {
|
|
const hostname = new URL(url).hostname
|
|
return `Claude wants to fetch content from ${hostname}`
|
|
} catch {
|
|
return `Claude wants to fetch content from this URL`
|
|
}
|
|
},
|
|
userFacingName() {
|
|
return 'Fetch'
|
|
},
|
|
getToolUseSummary,
|
|
getActivityDescription(input) {
|
|
const summary = getToolUseSummary(input)
|
|
return summary ? `Fetching ${summary}` : 'Fetching web page'
|
|
},
|
|
get inputSchema(): InputSchema {
|
|
return inputSchema()
|
|
},
|
|
get outputSchema(): OutputSchema {
|
|
return outputSchema()
|
|
},
|
|
isConcurrencySafe() {
|
|
return true
|
|
},
|
|
isReadOnly() {
|
|
return true
|
|
},
|
|
toAutoClassifierInput(input) {
|
|
return input.prompt ? `${input.url}: ${input.prompt}` : input.url
|
|
},
|
|
async checkPermissions(input, context): Promise<PermissionDecision> {
|
|
const appState = context.getAppState()
|
|
const permissionContext = appState.toolPermissionContext
|
|
|
|
// Check if the hostname is in the preapproved list
|
|
try {
|
|
const { url } = input as { url: string }
|
|
const parsedUrl = new URL(url)
|
|
if (isPreapprovedHost(parsedUrl.hostname, parsedUrl.pathname)) {
|
|
return {
|
|
behavior: 'allow',
|
|
updatedInput: input,
|
|
decisionReason: { type: 'other', reason: 'Preapproved host' },
|
|
}
|
|
}
|
|
} catch {
|
|
// If URL parsing fails, continue with normal permission checks
|
|
}
|
|
|
|
// Check for a rule specific to the tool input (matching hostname)
|
|
const ruleContent = webFetchToolInputToPermissionRuleContent(input)
|
|
|
|
const denyRule = getRuleByContentsForTool(
|
|
permissionContext,
|
|
WebFetchTool,
|
|
'deny',
|
|
).get(ruleContent)
|
|
if (denyRule) {
|
|
return {
|
|
behavior: 'deny',
|
|
message: `${WebFetchTool.name} denied access to ${ruleContent}.`,
|
|
decisionReason: {
|
|
type: 'rule',
|
|
rule: denyRule,
|
|
},
|
|
}
|
|
}
|
|
|
|
const askRule = getRuleByContentsForTool(
|
|
permissionContext,
|
|
WebFetchTool,
|
|
'ask',
|
|
).get(ruleContent)
|
|
if (askRule) {
|
|
return {
|
|
behavior: 'ask',
|
|
message: `Claude requested permissions to use ${WebFetchTool.name}, but you haven't granted it yet.`,
|
|
decisionReason: {
|
|
type: 'rule',
|
|
rule: askRule,
|
|
},
|
|
suggestions: buildSuggestions(ruleContent),
|
|
}
|
|
}
|
|
|
|
const allowRule = getRuleByContentsForTool(
|
|
permissionContext,
|
|
WebFetchTool,
|
|
'allow',
|
|
).get(ruleContent)
|
|
if (allowRule) {
|
|
return {
|
|
behavior: 'allow',
|
|
updatedInput: input,
|
|
decisionReason: {
|
|
type: 'rule',
|
|
rule: allowRule,
|
|
},
|
|
}
|
|
}
|
|
|
|
return {
|
|
behavior: 'ask',
|
|
message: `Claude requested permissions to use ${WebFetchTool.name}, but you haven't granted it yet.`,
|
|
suggestions: buildSuggestions(ruleContent),
|
|
}
|
|
},
|
|
async prompt(_options) {
|
|
// Always include the auth warning regardless of whether ToolSearch is
|
|
// currently in the tools list. Conditionally toggling this prefix based
|
|
// on ToolSearch availability caused the tool description to flicker
|
|
// between SDK query() calls (when ToolSearch enablement varies due to
|
|
// MCP tool count thresholds), invalidating the Anthropic API prompt
|
|
// cache on each toggle — two consecutive cache misses per flicker event.
|
|
return `IMPORTANT: WebFetch WILL FAIL for authenticated or private URLs. Before using this tool, check if the URL points to an authenticated service (e.g. Google Docs, Confluence, Jira, GitHub). If so, look for a specialized MCP tool that provides authenticated access.
|
|
${DESCRIPTION}`
|
|
},
|
|
async validateInput(input) {
|
|
const { url } = input
|
|
try {
|
|
new URL(url)
|
|
} catch {
|
|
return {
|
|
result: false,
|
|
message: `Error: Invalid URL "${url}". The URL provided could not be parsed.`,
|
|
meta: { reason: 'invalid_url' },
|
|
errorCode: 1,
|
|
}
|
|
}
|
|
return { result: true }
|
|
},
|
|
renderToolUseMessage,
|
|
renderToolUseProgressMessage,
|
|
renderToolResultMessage,
|
|
async call(
|
|
{ url, prompt },
|
|
{ abortController, options: { isNonInteractiveSession } },
|
|
) {
|
|
const start = Date.now()
|
|
|
|
const response = await getURLMarkdownContent(url, abortController)
|
|
|
|
// Check if we got a redirect to a different host
|
|
if ('type' in response && response.type === 'redirect') {
|
|
const statusText =
|
|
response.statusCode === 301
|
|
? 'Moved Permanently'
|
|
: response.statusCode === 308
|
|
? 'Permanent Redirect'
|
|
: response.statusCode === 307
|
|
? 'Temporary Redirect'
|
|
: 'Found'
|
|
|
|
const message = `REDIRECT DETECTED: The URL redirects to a different host.
|
|
|
|
Original URL: ${response.originalUrl}
|
|
Redirect URL: ${response.redirectUrl}
|
|
Status: ${response.statusCode} ${statusText}
|
|
|
|
To complete your request, I need to fetch content from the redirected URL. Please use WebFetch again with these parameters:
|
|
- url: "${response.redirectUrl}"
|
|
- prompt: "${prompt}"`
|
|
|
|
const output: Output = {
|
|
bytes: Buffer.byteLength(message),
|
|
code: response.statusCode,
|
|
codeText: statusText,
|
|
result: message,
|
|
durationMs: Date.now() - start,
|
|
url,
|
|
}
|
|
|
|
return {
|
|
data: output,
|
|
}
|
|
}
|
|
|
|
const {
|
|
content,
|
|
bytes,
|
|
code,
|
|
codeText,
|
|
contentType,
|
|
persistedPath,
|
|
persistedSize,
|
|
} = response as FetchedContent
|
|
|
|
const isPreapproved = isPreapprovedUrl(url)
|
|
|
|
let result: string
|
|
if (
|
|
isPreapproved &&
|
|
contentType.includes('text/markdown') &&
|
|
content.length < MAX_MARKDOWN_LENGTH
|
|
) {
|
|
result = content
|
|
} else {
|
|
result = await applyPromptToMarkdown(
|
|
prompt,
|
|
content,
|
|
abortController.signal,
|
|
isNonInteractiveSession,
|
|
isPreapproved,
|
|
)
|
|
}
|
|
|
|
// Binary content (PDFs, etc.) was additionally saved to disk with a
|
|
// mime-derived extension. Note it so Claude can inspect the raw file
|
|
// if the Haiku summary above isn't enough.
|
|
if (persistedPath) {
|
|
result += `\n\n[Binary content (${contentType}, ${formatFileSize(persistedSize ?? bytes)}) also saved to ${persistedPath}]`
|
|
}
|
|
|
|
const output: Output = {
|
|
bytes,
|
|
code,
|
|
codeText,
|
|
result,
|
|
durationMs: Date.now() - start,
|
|
url,
|
|
}
|
|
|
|
return {
|
|
data: output,
|
|
}
|
|
},
|
|
mapToolResultToToolResultBlockParam({ result }, toolUseID) {
|
|
return {
|
|
tool_use_id: toolUseID,
|
|
type: 'tool_result',
|
|
content: result,
|
|
}
|
|
},
|
|
} satisfies ToolDef<InputSchema, Output>)
|
|
|
|
function buildSuggestions(ruleContent: string): PermissionUpdate[] {
|
|
return [
|
|
{
|
|
type: 'addRules',
|
|
destination: 'localSettings',
|
|
rules: [{ toolName: WEB_FETCH_TOOL_NAME, ruleContent }],
|
|
behavior: 'allow',
|
|
},
|
|
]
|
|
}
|