|
| 1 | +import z from 'zod/v4' |
| 2 | + |
| 3 | +import { $getNativeToolCallExampleString, jsonToolResultSchema } from '../utils' |
| 4 | + |
| 5 | +import type { $ToolParams } from '../../constants' |
| 6 | + |
| 7 | +const toolName = 'read_url' |
| 8 | +const endsAgentStep = true |
| 9 | +const inputSchema = z |
| 10 | + .object({ |
| 11 | + url: z |
| 12 | + .url() |
| 13 | + .refine((value) => { |
| 14 | + try { |
| 15 | + const parsedUrl = new URL(value) |
| 16 | + return ( |
| 17 | + parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:' |
| 18 | + ) |
| 19 | + } catch { |
| 20 | + return false |
| 21 | + } |
| 22 | + }, 'URL must use http:// or https://') |
| 23 | + .describe( |
| 24 | + 'The full http:// or https:// URL to fetch and extract readable text from.', |
| 25 | + ), |
| 26 | + max_chars: z |
| 27 | + .number() |
| 28 | + .int() |
| 29 | + .min(1_000) |
| 30 | + .max(50_000) |
| 31 | + .default(20_000) |
| 32 | + .optional() |
| 33 | + .describe( |
| 34 | + 'Maximum number of extracted text characters to return. Defaults to 20000.', |
| 35 | + ), |
| 36 | + }) |
| 37 | + .describe('Fetch a URL and extract readable text from the page.') |
| 38 | + |
| 39 | +const description = ` |
| 40 | +Purpose: Fetch a URL returned by web_search and extract the readable page text so you can answer with source-backed evidence. |
| 41 | +
|
| 42 | +Use this after web_search when snippets are not enough. Prefer authoritative, relevant pages from the search results. The tool follows redirects, extracts titles and metadata, strips scripts/styles/navigation boilerplate from HTML, and returns normalized readable text. |
| 43 | +
|
| 44 | +Do not use run_terminal_command with curl just to inspect web pages; use read_url instead. If read_url reports unsupported content or extraction failure, then choose a different search result or explain the limitation. |
| 45 | +
|
| 46 | +Example: |
| 47 | +${$getNativeToolCallExampleString({ |
| 48 | + toolName, |
| 49 | + inputSchema, |
| 50 | + input: { |
| 51 | + url: 'https://react.dev/reference/react/useActionState', |
| 52 | + max_chars: 12000, |
| 53 | + }, |
| 54 | + endsAgentStep, |
| 55 | +})} |
| 56 | +`.trim() |
| 57 | + |
| 58 | +export const readUrlParams = { |
| 59 | + toolName, |
| 60 | + endsAgentStep, |
| 61 | + description, |
| 62 | + inputSchema, |
| 63 | + outputSchema: jsonToolResultSchema( |
| 64 | + z.union([ |
| 65 | + z.object({ |
| 66 | + url: z.string(), |
| 67 | + finalUrl: z.string(), |
| 68 | + status: z.number(), |
| 69 | + contentType: z.string().optional(), |
| 70 | + title: z.string().optional(), |
| 71 | + description: z.string().optional(), |
| 72 | + text: z.string(), |
| 73 | + truncated: z.boolean(), |
| 74 | + }), |
| 75 | + z.object({ |
| 76 | + url: z.string().optional(), |
| 77 | + errorMessage: z.string(), |
| 78 | + }), |
| 79 | + ]), |
| 80 | + ), |
| 81 | +} satisfies $ToolParams |
0 commit comments