File size: 7,358 Bytes
eb3a949
 
 
 
 
 
064bfd6
 
 
 
 
eb3a949
064bfd6
 
 
3bd60b5
064bfd6
 
eb3a949
 
 
 
064bfd6
eb3a949
 
064bfd6
 
 
eb3a949
 
 
064bfd6
eb3a949
064bfd6
 
eb3a949
064bfd6
eb3a949
064bfd6
 
 
 
eb3a949
064bfd6
eb3a949
064bfd6
eb3a949
 
064bfd6
 
 
 
eb3a949
064bfd6
 
eb3a949
064bfd6
eb3a949
064bfd6
 
 
eb3a949
064bfd6
eb3a949
064bfd6
 
 
 
 
eb3a949
064bfd6
 
 
 
 
 
eb3a949
064bfd6
eb3a949
064bfd6
 
eb3a949
064bfd6
 
 
 
3fcac91
 
064bfd6
3fcac91
 
 
064bfd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bd60b5
064bfd6
 
 
3bd60b5
 
064bfd6
3bd60b5
 
 
 
 
 
 
 
 
 
064bfd6
3bd60b5
064bfd6
 
 
 
 
 
3bd60b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
064bfd6
 
3bd60b5
 
 
 
064bfd6
 
 
 
 
 
 
3bd60b5
 
 
 
 
 
 
 
 
 
 
 
064bfd6
3bd60b5
 
 
064bfd6
 
 
 
 
 
 
 
 
 
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
import { z } from 'zod/v4' // 引入 Zod: 定义 & 检验输入输出结构
import { buildTool, type ToolDef } from '../../Tool.js' // 构建工具对象, 工具类型约束
import { formatFileSize } from '../../utils/format.js' // 字节 -> 可读格式(KB/MB)
import { lazySchema } from '../../utils/lazySchema.js' // 延迟初始化 schema (避免循环依赖)
import type { PermissionDecision } from '../../utils/permissions/PermissionResult.js' // 权限检查返回结构
import { DESCRIPTION, WEB_FETCH_TOOL_NAME } from './prompt.js' // 工具描述 & 名字
import {
  getToolUseSummary,
  renderToolResultMessage,
  renderToolUseMessage,
  renderToolUseProgressMessage,
} from './UI.js' // UI 渲染函数
import {
  type FetchedContent,
  getURLMarkdownContent,
} from './utils.js' // 抓网页, 处理 markdown

const inputSchema = lazySchema(() =>
  z.strictObject({ // 严格对象 不允许多字段
    url: z.string().url().describe('The URL to fetch content from'), // url: string, 必须是合法 url
    prompt: z.string().describe('The prompt to run on the fetched content'), // 对网页内容执行的任务 总结/提取
  }), // 延迟创建 schema
)

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'), // HTTP 状态码
    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'), // url
  }),
)
type OutputSchema = ReturnType<typeof outputSchema>

export type Output = z.infer<OutputSchema> // 输出类型推导

// Tool 定义开始
export const WebFetchTool = buildTool({
  name: WEB_FETCH_TOOL_NAME, // 工具名
  searchHint: 'fetch and extract content from a URL', // 给 llm 的提示
  // 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 `VersperClaw wants to fetch content from ${hostname}` // 用户提示
    } catch {
      return `VersperClaw wants to fetch content from this URL`
    }
  },
  userFacingName() {
    return 'Fetch' // UI 名称
  },
  // 摘要 & 活动描述
  getToolUseSummary,
  getActivityDescription(input) {
    const summary = getToolUseSummary(input)
    return summary ? `Fetching ${summary}` : 'Fetching web page'
  },
  // schema getter
  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
  },
  // 权限检查 - 完全开放,允许所有 WebFetch 请求
  async checkPermissions(_input, _context): Promise<PermissionDecision> {
    return {
      behavior: 'allow',
      updatedInput: _input,
      decisionReason: { type: 'other', reason: 'All web fetches allowed' },
    }
  },
  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 },
  ) {
    const start = Date.now()

    try {
      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}"`

        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,
        persistedPath,
        persistedSize,
      } = response as FetchedContent

      // Directly return the content fetched by Jina API without Claude processing
      let result = content

      // Binary content (PDFs, etc.) was additionally saved to disk with a
      // mime-derived extension. Note it so the user can inspect the raw file.
      if (persistedPath) {
        result += `\n\n[Binary content also saved to ${persistedPath}]`
      }

      const output: Output = {
        bytes,
        code,
        codeText,
        result,
        durationMs: Date.now() - start,
        url,
      }

      return {
        data: output,
      }
    } catch (error) {
      // Handle errors from getURLMarkdownContent
      const errorMessage = `Failed to fetch URL: ${error instanceof Error ? error.message : String(error)}`
      
      const output: Output = {
        bytes: Buffer.byteLength(errorMessage),
        code: 0,
        codeText: 'Error',
        result: errorMessage,
        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>)