import { z } from 'zod'; import type { Tool, ToolResult } from '../../types.js'; import { toToolParameters } from '../../utils/zodToJson.js'; import { type FileReadState, recordRead } from '../shared/fileState.js'; import { resolveToolPath } from '../shared/fileUtils.js'; import { readStreamedText } from '../read/read.js'; import { DEFAULT_MAX_RESULT_SIZE_CHARS } from '../types.js'; const schema = z .object({ file_path: z.string().min(1), start_line: z.number().int().min(1), end_line: z.number().int().min(1), context: z.number().int().min(0).max(500).default(50).optional(), }) .refine((v) => v.end_line >= v.start_line, { message: 'end_line 必须大于等于 start_line', }); type Input = z.infer; async function call( input: Input, cwd?: string, state?: FileReadState, signal?: AbortSignal, grant = DEFAULT_MAX_RESULT_SIZE_CHARS, ): Promise { const budget = Math.min(DEFAULT_MAX_RESULT_SIZE_CHARS, Math.floor(grant)); if (!Number.isFinite(budget) || budget < 1000) return { ok: false, error: '本批读取上下文预算已用完;请先处理已有结果再继续读取。' }; const resolved = resolveToolPath(input.file_path, cwd); if (!resolved.ok) return resolved; const before = input.context ?? 50, from = Math.max(1, input.start_line - before), to = input.end_line + before; try { signal?.throwIfAborted(); // Budget the actual model result, including padding and footer. Only lines // shown in full can contribute evidence for a later write. const limit = Math.min(2000, to - from + 1, Math.floor((budget - 500) / 10)); const result = await readStreamedText(resolved.resolvedPath, from, limit, signal, budget - 500 - limit * 5); signal?.throwIfAborted(); const { selectedCount, clippedLine, hasMore } = result; const fullLines = selectedCount - (clippedLine === undefined ? 0 : 1); const shown = selectedCount ? `${from}-${from + selectedCount - 1}` : 'none'; const next = clippedLine !== undefined ? `partial line ${clippedLine}; use Read offset=${clippedLine} column=${result.numbered.length - String(clippedLine).length}` : hasMore ? `more lines; continue at ${from + fullLines}` : `total ${result.totalKnown} lines`; recordRead(resolved.resolvedPath, { partial: true, offset: from - 1, limit: fullLines, range: fullLines ? { start: from, end: from + fullLines - 1 } : undefined, totalUnits: hasMore ? undefined : result.totalKnown, }, state); const body = result.numbered.replace(/^(\d+)\t/gm, (_, n: string) => n.padStart(6) + '\t'); return { ok: true, content: `${body}\n\n[requested ${input.start_line}-${input.end_line}; shown ${shown}; ${next}]` }; } catch (e) { return { ok: false, error: `Snip 读取失败:${(e as Error).message}` }; } } export const snipTool: Tool = { name: 'Snip', description: 'Reads a 1-based text line range with bounded surrounding context. Up to 2,000 lines per page, also limited by character budget. Follow the returned next line, or use Read with offset/column for a long line.', inputSchema: schema, parameters: toToolParameters(schema), isReadOnly: true, isConcurrencySafe: true, call: (input, signal) => call(input, undefined, undefined, signal), callWithContext: (input, signal, ctx) => call(input, ctx.cwd, ctx.sessionState?.fileReads, signal, ctx.reserveReadChars?.(DEFAULT_MAX_RESULT_SIZE_CHARS) ?? DEFAULT_MAX_RESULT_SIZE_CHARS), };