minimal-agent 0.18.11 → 0.18.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "minimal-agent",
3
- "version": "0.18.11",
3
+ "version": "0.18.12",
4
4
  "description": "Bun-first 的本地 Agent 内核,支持工具、插件、工作流、上下文压缩、长期记忆与 OpenAI 兼容接口;Tauri 桌面宿主和 Node.js npm 发布可复用同一内核",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Bill Wang <leiwang0359@gmail.com>",
package/src/loop.js CHANGED
@@ -1111,7 +1111,7 @@ export async function* runQuery(userInput, options) {
1111
1111
  const rawContent = result.ok ? result.content : `Error: ${result.error}`;
1112
1112
  const basePolicy = result.policy ?? getToolResultPolicy(tc.function.name);
1113
1113
  // 超大结果先归档再压缩:归档失败时保留原文,绝不因为磁盘问题破坏上下文。
1114
- const archived = result.ok && sessionState
1114
+ const archived = result.ok && sessionState && !(basePolicy.boundedWindow && basePolicy.archiveRef)
1115
1115
  ? await archiveToolResult(rawContent, sessionState.microCompact.archiveKey, tc.function.name)
1116
1116
  : { content: rawContent, originalChars: rawContent.length };
1117
1117
  // 归档退化必须发声:撞会话上限后每条超长结果都退回原文塞进上下文,
package/src/loop.ts CHANGED
@@ -1193,7 +1193,7 @@ export async function* runQuery(userInput: string, options: RunQueryOptions): As
1193
1193
  const basePolicy = result.policy ?? getToolResultPolicy(tc.function.name);
1194
1194
  // 超大结果先归档再压缩:归档失败时保留原文,绝不因为磁盘问题破坏上下文。
1195
1195
  const archived =
1196
- result.ok && sessionState
1196
+ result.ok && sessionState && !(basePolicy.boundedWindow && basePolicy.archiveRef)
1197
1197
  ? await archiveToolResult(rawContent, sessionState.microCompact.archiveKey, tc.function.name)
1198
1198
  : { content: rawContent, originalChars: rawContent.length };
1199
1199
  // 归档退化必须发声:撞会话上限后每条超长结果都退回原文塞进上下文,
@@ -114,7 +114,7 @@ export const CONTRACT_DESCRIPTION_OVERRIDES = {
114
114
  AdvisorDecision: '记录主 Agent 对顾问第二意见的 accept、partial 或 reject 判断;是否采纳由主 Agent 决定,登记不是执行许可。partial 同时填 acceptedPoints/rejectedPoints;reject 填 rejectedPoints,列出被拒绝点及依据;evidenceRefs 只引用真实 tool_call_id,没有引用则记录待验证。登记失败不阻止任务继续。',
115
115
  MultiEdit: '同文件有序批量精确替换,先 Read 相关范围;全部匹配成功才写盘。old_string 非空且唯一,或显式 replace_all。后续匹配前序生成文本会被拒绝,请合并 edits。操作条数不等于替换次数,diff 预览可能不完整。',
116
116
  LSP: '查询定义、引用、类型、符号和诊断。line/character 为 1-based UTF-16;尚未收到诊断不等于没有错误,编辑前读取源码。',
117
- WebFetch: '读取已知 HTTP(S) URL 的静态文本,HTML 转 Markdown;不执行 JavaScript、不继承登录态。PDF/图片用 Download 后 Read;动态页面用 WebBrowser。',
117
+ WebFetch: '读取已知 HTTP(S) URL 的静态文本并完整保存会话快照,HTML 转 Markdown;结果仅为窗口,使用 snapshot_id + next_offset 续读,query 在全文中精确搜索,max_chars 可扩大读取预算。不要重复读取同一窗口或声称已读未展示部分。不执行 JavaScript、不继承登录态。PDF/图片用 Download 后 Read;动态页面用 WebBrowser。',
118
118
  WebSearch: 'Tavily 搜索返回标题、URL、片段及可选综合答案,不是完整原文或浏览器。重要结论读取来源正文;按子问题查询,重试改变关键词或来源。recency 控制时效,历史范围用 start_date/end_date,none 关闭自动时效;advanced 更慢,news 偏新闻。需配置 Tavily。',
119
119
  PowerShell: '运行宿主 PowerShell 独立进程并检查结果;长任务用 TaskOutput/TaskStop。永久删除用 Trash;不可逆操作先确认,非交互时取消。',
120
120
  Download: '下载 HTTP(S) 原始字节到指定路径;相对路径基于工作目录,会覆盖已有文件;失败或中断不保留部分下载。',
@@ -132,7 +132,7 @@ export const CONTRACT_DESCRIPTION_OVERRIDES: Readonly<Record<string, string | ((
132
132
  AdvisorDecision: '记录主 Agent 对顾问第二意见的 accept、partial 或 reject 判断;是否采纳由主 Agent 决定,登记不是执行许可。partial 同时填 acceptedPoints/rejectedPoints;reject 填 rejectedPoints,列出被拒绝点及依据;evidenceRefs 只引用真实 tool_call_id,没有引用则记录待验证。登记失败不阻止任务继续。',
133
133
  MultiEdit: '同文件有序批量精确替换,先 Read 相关范围;全部匹配成功才写盘。old_string 非空且唯一,或显式 replace_all。后续匹配前序生成文本会被拒绝,请合并 edits。操作条数不等于替换次数,diff 预览可能不完整。',
134
134
  LSP: '查询定义、引用、类型、符号和诊断。line/character 为 1-based UTF-16;尚未收到诊断不等于没有错误,编辑前读取源码。',
135
- WebFetch: '读取已知 HTTP(S) URL 的静态文本,HTML 转 Markdown;不执行 JavaScript、不继承登录态。PDF/图片用 Download 后 Read;动态页面用 WebBrowser。',
135
+ WebFetch: '读取已知 HTTP(S) URL 的静态文本并完整保存会话快照,HTML 转 Markdown;结果仅为窗口,使用 snapshot_id + next_offset 续读,query 在全文中精确搜索,max_chars 可扩大读取预算。不要重复读取同一窗口或声称已读未展示部分。不执行 JavaScript、不继承登录态。PDF/图片用 Download 后 Read;动态页面用 WebBrowser。',
136
136
  WebSearch: 'Tavily 搜索返回标题、URL、片段及可选综合答案,不是完整原文或浏览器。重要结论读取来源正文;按子问题查询,重试改变关键词或来源。recency 控制时效,历史范围用 start_date/end_date,none 关闭自动时效;advanced 更慢,news 偏新闻。需配置 Tavily。',
137
137
  PowerShell: '运行宿主 PowerShell 独立进程并检查结果;长任务用 TaskOutput/TaskStop。永久删除用 Trash;不可逆操作先确认,非交互时取消。',
138
138
  Download: '下载 HTTP(S) 原始字节到指定路径;相对路径基于工作目录,会覆盖已有文件;失败或中断不保留部分下载。',
@@ -0,0 +1,56 @@
1
+ import { createHash, randomUUID } from 'node:crypto';
2
+ import { mkdir, readFile, writeFile, rename, unlink } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { sessionRuntimeDir } from '../../session/runtimeStore.js';
5
+ function directory(identity) { return join(sessionRuntimeDir(identity), 'web-pages'); }
6
+ export function pageSnapshotPath(id, identity) {
7
+ if (!/^[a-f0-9]{64}$/.test(id))
8
+ throw new Error('无效的 snapshot_id');
9
+ return join(directory(identity), `${id}.txt`);
10
+ }
11
+ export async function savePageSnapshot(page, identity) {
12
+ const id = createHash('sha256').update(JSON.stringify(page)).digest('hex');
13
+ const dir = directory(identity);
14
+ await mkdir(dir, { recursive: true, mode: 0o700 });
15
+ // Publish the metadata last. Readers never observe a half-written snapshot.
16
+ const path = join(dir, `${id}.txt`);
17
+ const temp = join(dir, `.${randomUUID()}.tmp`);
18
+ try {
19
+ await writeFile(temp, page.text, { mode: 0o600 });
20
+ await rename(temp, path);
21
+ const { text, ...metadata } = page;
22
+ await writeFile(temp, JSON.stringify(metadata), { mode: 0o600 });
23
+ await rename(temp, join(dir, `${id}.json`));
24
+ }
25
+ finally {
26
+ await unlink(temp).catch(() => undefined);
27
+ }
28
+ return { id, path };
29
+ }
30
+ export async function loadPageSnapshot(id, url, identity) {
31
+ if (!/^[a-f0-9]{64}$/.test(id))
32
+ throw new Error('无效的 snapshot_id');
33
+ const page = JSON.parse(await readFile(join(directory(identity), `${id}.json`), 'utf8'));
34
+ if (page.url !== url)
35
+ throw new Error('快照 URL 不匹配');
36
+ page.text = await readFile(join(directory(identity), `${id}.txt`), 'utf8');
37
+ if (createHash('sha256').update(JSON.stringify({ url: page.url, text: page.text, format: page.format, fetchedAt: page.fetchedAt })).digest('hex') !== id)
38
+ throw new Error('快照完整性检查失败,请重新抓取');
39
+ return page;
40
+ }
41
+ /** UTF-16 offsets match JS slicing and Read.column; never split a surrogate pair. */
42
+ export function pageWindow(text, offset, budget, query) {
43
+ let start = Math.min(offset, text.length);
44
+ if (query) {
45
+ const hit = text.indexOf(query, start);
46
+ if (hit < 0)
47
+ return { text: '', start, end: start, next: null, found: false };
48
+ start = hit;
49
+ }
50
+ if (start > 0 && /[\uDC00-\uDFFF]/.test(text[start] ?? '') && /[\uD800-\uDBFF]/.test(text[start - 1]))
51
+ start--;
52
+ let end = Math.min(text.length, start + budget);
53
+ if (end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1] ?? ''))
54
+ end--;
55
+ return { text: text.slice(start, end), start, end, next: end < text.length ? end : null, found: true };
56
+ }
@@ -0,0 +1,51 @@
1
+ import { createHash, randomUUID } from 'node:crypto';
2
+ import { mkdir, readFile, writeFile, rename, unlink } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { sessionRuntimeDir, type RuntimeIdentity } from '../../session/runtimeStore.js';
5
+
6
+ /** Immutable, session-local source. Preview limits never truncate this copy. */
7
+ export interface PageSnapshot { url: string; text: string; format: string; fetchedAt: number }
8
+ function directory(identity: RuntimeIdentity) { return join(sessionRuntimeDir(identity), 'web-pages'); }
9
+ export function pageSnapshotPath(id: string, identity: RuntimeIdentity) {
10
+ if (!/^[a-f0-9]{64}$/.test(id)) throw new Error('无效的 snapshot_id');
11
+ return join(directory(identity), `${id}.txt`);
12
+ }
13
+ export async function savePageSnapshot(page: PageSnapshot, identity: RuntimeIdentity) {
14
+ const id = createHash('sha256').update(JSON.stringify(page)).digest('hex');
15
+ const dir = directory(identity);
16
+ await mkdir(dir, { recursive: true, mode: 0o700 });
17
+ // Publish the metadata last. Readers never observe a half-written snapshot.
18
+ const path = join(dir, `${id}.txt`);
19
+ const temp = join(dir, `.${randomUUID()}.tmp`);
20
+ try {
21
+ await writeFile(temp, page.text, { mode: 0o600 });
22
+ await rename(temp, path);
23
+ const { text, ...metadata } = page;
24
+ await writeFile(temp, JSON.stringify(metadata), { mode: 0o600 });
25
+ await rename(temp, join(dir, `${id}.json`));
26
+ } finally { await unlink(temp).catch(() => undefined); }
27
+ return { id, path };
28
+ }
29
+ export async function loadPageSnapshot(id: string, url: string, identity: RuntimeIdentity): Promise<PageSnapshot> {
30
+ if (!/^[a-f0-9]{64}$/.test(id)) throw new Error('无效的 snapshot_id');
31
+ const page = JSON.parse(await readFile(join(directory(identity), `${id}.json`), 'utf8')) as PageSnapshot;
32
+ if (page.url !== url) throw new Error('快照 URL 不匹配');
33
+ page.text = await readFile(join(directory(identity), `${id}.txt`), 'utf8');
34
+ if (createHash('sha256').update(JSON.stringify({ url: page.url, text: page.text, format: page.format, fetchedAt: page.fetchedAt })).digest('hex') !== id)
35
+ throw new Error('快照完整性检查失败,请重新抓取');
36
+ return page;
37
+ }
38
+
39
+ /** UTF-16 offsets match JS slicing and Read.column; never split a surrogate pair. */
40
+ export function pageWindow(text: string, offset: number, budget: number, query?: string) {
41
+ let start = Math.min(offset, text.length);
42
+ if (query) {
43
+ const hit = text.indexOf(query, start);
44
+ if (hit < 0) return { text: '', start, end: start, next: null, found: false };
45
+ start = hit;
46
+ }
47
+ if (start > 0 && /[\uDC00-\uDFFF]/.test(text[start] ?? '') && /[\uD800-\uDBFF]/.test(text[start - 1]!)) start--;
48
+ let end = Math.min(text.length, start + budget);
49
+ if (end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1] ?? '')) end--;
50
+ return { text: text.slice(start, end), start, end, next: end < text.length ? end : null, found: true };
51
+ }
@@ -6,17 +6,18 @@
6
6
  *
7
7
  * 输入:
8
8
  * url 必填,要获取内容的 URL
9
+ * snapshot_id / offset / max_chars / query 用于稳定续读与搜索
9
10
  * prompt 可选,旧版兼容字段,运行时忽略
10
11
  *
11
12
  * 输出:
12
- * { bytes, code, codeText, result, durationMs, url }
13
+ * 完整快照引用、字符范围、下一段游标与本次正文窗口
13
14
  *
14
15
  * 流程:
15
16
  * 1. URL 验证(合法格式、2000字符以内、无凭据)
16
17
  * 2. fetch(保持 HTTP(S) 协议,超时 60s)
17
18
  * 3. 检测跨 host 重定向,返回提示而非自动跟随
18
19
  * 4. HTML → Markdown(turndown);非 HTML 原样返回
19
- * 5. 输出截断到 30K chars(超长内容直接截断)
20
+ * 5. 完整正文保存为本机会话快照,按字符游标读取或搜索
20
21
  * ============================================================
21
22
  */
22
23
  import { z } from 'zod';
@@ -24,6 +25,7 @@ import { DEFAULT_MAX_RESULT_SIZE_CHARS } from '../types.js';
24
25
  import { toToolParameters } from '../../utils/zodToJson.js';
25
26
  import { isPreapprovedDomain } from './preapproved.js';
26
27
  import { readHttpBody } from '../shared/httpBody.js';
28
+ import { loadPageSnapshot, savePageSnapshot, pageSnapshotPath, pageWindow } from './pageSnapshot.js';
27
29
  // ---------------- 1. 常量 ----------------
28
30
  const MAX_URL_LENGTH = 2000;
29
31
  const FETCH_TIMEOUT_MS = 60_000;
@@ -58,14 +60,16 @@ function cleanCache() {
58
60
  // ---------------- 3. Zod 输入 schema ----------------
59
61
  const inputSchema = z.object({
60
62
  url: z.string().describe('要获取内容的 URL'),
63
+ snapshot_id: z.string().regex(/^[a-f0-9]{64}$/).optional().describe('续读时使用上次返回的快照 ID,保证读取同一版本。'),
64
+ offset: z.number().int().nonnegative().optional().describe('0-based UTF-16 字符位置;使用 next_offset 续读,不是行号。'),
65
+ max_chars: z.number().int().min(1000).max(1_000_000).optional().describe('本次正文字符预算,Runtime 按剩余上下文收紧。'),
66
+ query: z.string().min(1).max(500).optional().describe('在完整快照中从 offset 开始查找精确文本(区分大小写,非正则),返回命中处正文。'),
61
67
  /** @deprecated Accepted for callers compiled against the legacy contract. */
62
68
  prompt: z.string().optional(),
63
69
  });
64
- // Model-visible v2 contract. WebFetch returns the page body and does not run a
70
+ // Model-visible snapshot contract. WebFetch returns the page body and does not run a
65
71
  // hidden extraction model; the runtime schema still accepts legacy `prompt`.
66
- const parameters = toToolParameters(z.object({
67
- url: z.string().describe('要读取正文的已知 URL'),
68
- }));
72
+ const parameters = toToolParameters(inputSchema.omit({ prompt: true }));
69
73
  // ---------------- 4. Description ----------------
70
74
  const description = `Reads the full text content of a known URL without opening a browser.
71
75
  - Takes a URL, fetches its response body, and converts HTML to Markdown.
@@ -74,8 +78,11 @@ const description = `Reads the full text content of a known URL without opening
74
78
  - It does not discover sources, authenticate, click, upload, or operate an interactive page.
75
79
  - HTTP and HTTPS URLs are fetched as supplied, including local development servers.
76
80
  - When a URL redirects to a different host, returns a warning with the redirect URL.
77
- - This tool is read-only and does not modify any files.
78
- - Very large results may be truncated with an explicit marker.
81
+ - Read-only with respect to the source/workspace; complete extracted text is saved in a private local runtime snapshot.
82
+ - Results are windows, not the whole page. Continue with the same url + snapshot_id + next_offset until next_offset=null. Use query for exact-text search anywhere in the snapshot.
83
+ - max_chars can request larger windows for large-context models; the runtime may reduce the per-call budget.
84
+ - A complete snapshot means the fetched/extracted body, not authenticated or JavaScript-rendered content. Do not claim unread ranges were inspected.
85
+ - Conversion failures return clearly labeled raw HTML. If dynamic content is missing, use available browser tools; do not repeat the same window or invent missing content.
79
86
  - ⚠️ IMPORTANT: This tool WILL FAIL for authenticated or private URLs.
80
87
  Do not claim access to private content when the current tool catalog does not
81
88
  contain an authenticated integration for that service.
@@ -192,123 +199,76 @@ async function htmlToMarkdown(html) {
192
199
  return td.turndown(html);
193
200
  }
194
201
  // ---------------- 6. call() 实现 ----------------
195
- async function call(input, signal) {
202
+ async function call(input, signal, context = {}) {
196
203
  if (signal?.aborted)
197
204
  return { ok: false, error: '请求被中断' };
198
- const { url } = input;
199
- const start = Date.now();
200
- const cacheKey = url;
201
- let targetHostname;
205
+ const valid = validateURL(input.url);
206
+ if (!valid.ok)
207
+ return { ok: false, error: valid.error };
208
+ if ((input.offset ?? 0) > 0 && !input.snapshot_id)
209
+ return { ok: false, error: '续读需要 snapshot_id;先读取 URL 获取稳定快照,避免拼接不同版本。' };
210
+ const budget = context.reserveReadChars?.(input.max_chars) ?? input.max_chars ?? DEFAULT_MAX_RESULT_SIZE_CHARS;
211
+ if (budget < 1000)
212
+ return { ok: false, error: '本批读取预算已用完,请处理已有结果或压缩上下文后续读。' };
202
213
  try {
203
- targetHostname = new URL(url).hostname;
204
- }
205
- catch {
206
- targetHostname = '';
207
- }
208
- const preapproved = isPreapprovedDomain(targetHostname);
209
- // 检查缓存
210
- const cached = URL_CACHE.get(cacheKey);
211
- if (cached && Date.now() - cached.fetchedAt <= CACHE_TTL_MS) {
212
- let content = cached.content;
213
- const contentType = cached.contentType;
214
- if (contentType.includes('text/html')) {
215
- try {
216
- content = await htmlToMarkdown(content);
214
+ let page;
215
+ let status = '';
216
+ if (input.snapshot_id) {
217
+ page = await loadPageSnapshot(input.snapshot_id, input.url, context);
218
+ }
219
+ else {
220
+ const cached = URL_CACHE.get(input.url);
221
+ const hit = cached && Date.now() - cached.fetchedAt <= CACHE_TTL_MS;
222
+ const fetched = hit ? { type: 'success', ...cached } : await fetchURL(input.url, signal ?? new AbortController().signal);
223
+ if (fetched.type === 'error')
224
+ return { ok: false, error: fetched.error };
225
+ if (fetched.type === 'redirect')
226
+ return { ok: true, content: `【重定向检测】\n原始 URL: ${fetched.originalUrl}\n重定向到: ${fetched.redirectUrl}\n状态: ${fetched.statusCode}\n请使用重定向后的 URL 再次调用 WebFetch 工具。` };
227
+ const fetchedAt = hit ? cached.fetchedAt : Date.now();
228
+ if (!hit) {
229
+ URL_CACHE.set(input.url, { ...fetched, fetchedAt });
230
+ cleanCache();
217
231
  }
218
- catch (e) {
219
- console.warn(`turndown 失败: ${e.message}`);
232
+ let text = fetched.content;
233
+ let format = 'text';
234
+ if (fetched.contentType.includes('text/html')) {
235
+ try {
236
+ text = await htmlToMarkdown(text);
237
+ format = 'markdown';
238
+ }
239
+ catch {
240
+ format = 'raw-html (正文转换失败,保留原始 HTML)';
241
+ }
220
242
  }
243
+ page = { url: input.url, text, format, fetchedAt };
244
+ status = `状态: ${fetched.code} ${fetched.codeText}${hit ? ' (缓存命中)' : ''}\n`;
221
245
  }
222
- const formattedSize = cached.bytes < 1024
223
- ? `${cached.bytes} B`
224
- : cached.bytes < 1024 * 1024
225
- ? `${(cached.bytes / 1024).toFixed(1)} KB`
226
- : `${(cached.bytes / 1024 / 1024).toFixed(1)} MB`;
227
- const domainNote = preapproved
246
+ signal?.throwIfAborted();
247
+ const snapshot = input.snapshot_id
248
+ ? { id: input.snapshot_id, path: pageSnapshotPath(input.snapshot_id, context) }
249
+ : await savePageSnapshot(page, context);
250
+ signal?.throwIfAborted();
251
+ const window = pageWindow(page.text, input.offset ?? 0, budget, input.query);
252
+ const domain = isPreapprovedDomain(valid.parsed.hostname)
228
253
  ? '[网络访问:预批准域名]'
229
254
  : '[网络访问:普通域名;请按来源本身评估证据质量]';
230
- const output = `【WebFetch 结果】
231
- URL: ${url}
232
- 状态: ${cached.code} ${cached.codeText}
233
- 大小: ${formattedSize}
234
- 耗时: 0.00s (缓存命中)
235
- ${domainNote}
236
- [📦 来自缓存(15分钟 TTL)]
237
-
238
- --- 内容 ---
239
- ${content}`;
240
- let final = output;
241
- if (final.length > DEFAULT_MAX_RESULT_SIZE_CHARS) {
242
- final = final.slice(0, DEFAULT_MAX_RESULT_SIZE_CHARS) + `\n\n... (输出超过 ${DEFAULT_MAX_RESULT_SIZE_CHARS} 字符,已截断)`;
243
- }
244
- return { ok: true, content: final };
245
- }
246
- // 1. fetch
247
- const fetched = await fetchURL(url, signal ?? new AbortController().signal);
248
- const durationMs = Date.now() - start;
249
- if (fetched.type === 'redirect') {
250
- const statusText = fetched.statusCode === 301 ? 'Moved Permanently' :
251
- fetched.statusCode === 308 ? 'Permanent Redirect' :
252
- fetched.statusCode === 307 ? 'Temporary Redirect' : 'Found';
253
- return {
254
- ok: true,
255
- content: `【重定向检测】
256
-
257
- 原始 URL: ${fetched.originalUrl}
258
- 重定向到: ${fetched.redirectUrl}
259
- 状态: ${fetched.statusCode} ${statusText}
260
-
261
- 请使用重定向后的 URL 再次调用 WebFetch 工具。`,
262
- };
263
- }
264
- if (fetched.type === 'error') {
265
- return { ok: false, error: fetched.error };
266
- }
267
- let { content, bytes, code, codeText, contentType } = fetched;
268
- // 写入缓存
269
- URL_CACHE.set(cacheKey, {
270
- bytes,
271
- code,
272
- codeText,
273
- content,
274
- contentType,
275
- fetchedAt: Date.now(),
276
- });
277
- cleanCache();
278
- // 2. HTML → Markdown
279
- if (contentType.includes('text/html')) {
280
- try {
281
- content = await htmlToMarkdown(content);
282
- }
283
- catch (e) {
284
- console.warn(`turndown 失败: ${e.message}`);
285
- }
255
+ return { ok: true, policy: { highValue: true, reclaimable: false, boundedWindow: true,
256
+ archiveRef: snapshot.path, originalChars: page.text.length }, content: `【WebFetch 结果】
257
+ URL: ${page.url}
258
+ ${status}${domain}
259
+ snapshot_id: ${snapshot.id}
260
+ snapshot_complete: true (完整抓取正文,不代表动态页面内容完整)
261
+ format: ${page.format}
262
+ total_chars: ${page.text.length}
263
+ range: [${window.start}, ${window.end})
264
+ next_offset: ${window.next ?? 'null'}
265
+ 完整正文文件: ${snapshot.path}
266
+ ${window.found ? '' : '未找到指定文本(精确匹配,区分大小写)。\n'}--- 内容 ---
267
+ ${window.text}` };
286
268
  }
287
- // 3. 内容直接返回(超长内容在这里直接截断)
288
- // 4. 追加 metadata
289
- const formattedSize = bytes < 1024
290
- ? `${bytes} B`
291
- : bytes < 1024 * 1024
292
- ? `${(bytes / 1024).toFixed(1)} KB`
293
- : `${(bytes / 1024 / 1024).toFixed(1)} MB`;
294
- const domainNote = preapproved
295
- ? '[网络访问:预批准域名]'
296
- : '[网络访问:普通域名;请按来源本身评估证据质量]';
297
- const output = `【WebFetch 结果】
298
- URL: ${url}
299
- 状态: ${code} ${codeText}
300
- 大小: ${formattedSize}
301
- 耗时: ${(durationMs / 1000).toFixed(2)}s
302
- ${domainNote}
303
-
304
- --- 内容 ---
305
- ${content}`;
306
- // 5. 截断
307
- let final = output;
308
- if (final.length > DEFAULT_MAX_RESULT_SIZE_CHARS) {
309
- final = final.slice(0, DEFAULT_MAX_RESULT_SIZE_CHARS) + `\n\n... (输出超过 ${DEFAULT_MAX_RESULT_SIZE_CHARS} 字符,已截断)`;
269
+ catch (error) {
270
+ return { ok: false, error: `网页快照读取失败:${error.message}。未返回不完整正文;请重试原 URL 或检查本地存储。` };
310
271
  }
311
- return { ok: true, content: final };
312
272
  }
313
273
  // ---------------- 7. 导出 ----------------
314
274
  export const webfetchTool = {
@@ -318,7 +278,8 @@ export const webfetchTool = {
318
278
  parameters,
319
279
  isReadOnly: true,
320
280
  isConcurrencySafe: true,
321
- maxResultSizeChars: DEFAULT_MAX_RESULT_SIZE_CHARS,
281
+ maxResultSizeChars: 1_010_000,
322
282
  interruptBehavior: () => 'cancel',
323
283
  call,
284
+ callWithContext: (input, signal, context) => call(input, signal, context),
324
285
  };
@@ -6,27 +6,29 @@
6
6
  *
7
7
  * 输入:
8
8
  * url 必填,要获取内容的 URL
9
+ * snapshot_id / offset / max_chars / query 用于稳定续读与搜索
9
10
  * prompt 可选,旧版兼容字段,运行时忽略
10
11
  *
11
12
  * 输出:
12
- * { bytes, code, codeText, result, durationMs, url }
13
+ * 完整快照引用、字符范围、下一段游标与本次正文窗口
13
14
  *
14
15
  * 流程:
15
16
  * 1. URL 验证(合法格式、2000字符以内、无凭据)
16
17
  * 2. fetch(保持 HTTP(S) 协议,超时 60s)
17
18
  * 3. 检测跨 host 重定向,返回提示而非自动跟随
18
19
  * 4. HTML → Markdown(turndown);非 HTML 原样返回
19
- * 5. 输出截断到 30K chars(超长内容直接截断)
20
+ * 5. 完整正文保存为本机会话快照,按字符游标读取或搜索
20
21
  * ============================================================
21
22
  */
22
23
 
23
24
  import { z } from 'zod';
24
25
 
25
- import type { Tool, ToolResult } from '../../types.js';
26
+ import type { Tool, ToolResult, ToolCallContext } from '../../types.js';
26
27
  import { DEFAULT_MAX_RESULT_SIZE_CHARS } from '../types.js';
27
28
  import { toToolParameters } from '../../utils/zodToJson.js';
28
29
  import { isPreapprovedDomain } from './preapproved.js';
29
30
  import { readHttpBody } from '../shared/httpBody.js';
31
+ import { loadPageSnapshot, savePageSnapshot, pageSnapshotPath, pageWindow, type PageSnapshot } from './pageSnapshot.js';
30
32
 
31
33
  // ---------------- 1. 常量 ----------------
32
34
  const MAX_URL_LENGTH = 2000;
@@ -76,16 +78,18 @@ function cleanCache(): void {
76
78
  // ---------------- 3. Zod 输入 schema ----------------
77
79
  const inputSchema = z.object({
78
80
  url: z.string().describe('要获取内容的 URL'),
81
+ snapshot_id: z.string().regex(/^[a-f0-9]{64}$/).optional().describe('续读时使用上次返回的快照 ID,保证读取同一版本。'),
82
+ offset: z.number().int().nonnegative().optional().describe('0-based UTF-16 字符位置;使用 next_offset 续读,不是行号。'),
83
+ max_chars: z.number().int().min(1000).max(1_000_000).optional().describe('本次正文字符预算,Runtime 按剩余上下文收紧。'),
84
+ query: z.string().min(1).max(500).optional().describe('在完整快照中从 offset 开始查找精确文本(区分大小写,非正则),返回命中处正文。'),
79
85
  /** @deprecated Accepted for callers compiled against the legacy contract. */
80
86
  prompt: z.string().optional(),
81
87
  });
82
88
  type WebFetchInput = z.infer<typeof inputSchema>;
83
89
 
84
- // Model-visible v2 contract. WebFetch returns the page body and does not run a
90
+ // Model-visible snapshot contract. WebFetch returns the page body and does not run a
85
91
  // hidden extraction model; the runtime schema still accepts legacy `prompt`.
86
- const parameters = toToolParameters(z.object({
87
- url: z.string().describe('要读取正文的已知 URL'),
88
- }));
92
+ const parameters = toToolParameters(inputSchema.omit({ prompt: true }));
89
93
 
90
94
  // ---------------- 4. Description ----------------
91
95
  const description = `Reads the full text content of a known URL without opening a browser.
@@ -95,8 +99,11 @@ const description = `Reads the full text content of a known URL without opening
95
99
  - It does not discover sources, authenticate, click, upload, or operate an interactive page.
96
100
  - HTTP and HTTPS URLs are fetched as supplied, including local development servers.
97
101
  - When a URL redirects to a different host, returns a warning with the redirect URL.
98
- - This tool is read-only and does not modify any files.
99
- - Very large results may be truncated with an explicit marker.
102
+ - Read-only with respect to the source/workspace; complete extracted text is saved in a private local runtime snapshot.
103
+ - Results are windows, not the whole page. Continue with the same url + snapshot_id + next_offset until next_offset=null. Use query for exact-text search anywhere in the snapshot.
104
+ - max_chars can request larger windows for large-context models; the runtime may reduce the per-call budget.
105
+ - A complete snapshot means the fetched/extracted body, not authenticated or JavaScript-rendered content. Do not claim unread ranges were inspected.
106
+ - Conversion failures return clearly labeled raw HTML. If dynamic content is missing, use available browser tools; do not repeat the same window or invent missing content.
100
107
  - ⚠️ IMPORTANT: This tool WILL FAIL for authenticated or private URLs.
101
108
  Do not claim access to private content when the current tool catalog does not
102
109
  contain an authenticated integration for that service.
@@ -234,140 +241,66 @@ async function htmlToMarkdown(html: string): Promise<string> {
234
241
  async function call(
235
242
  input: WebFetchInput,
236
243
  signal?: AbortSignal,
244
+ context: ToolCallContext = {},
237
245
  ): Promise<ToolResult> {
238
246
  if (signal?.aborted) return { ok: false, error: '请求被中断' };
239
- const { url } = input;
240
- const start = Date.now();
241
- const cacheKey = url;
242
-
243
- let targetHostname: string;
247
+ const valid = validateURL(input.url);
248
+ if (!valid.ok) return { ok: false, error: valid.error };
249
+ if ((input.offset ?? 0) > 0 && !input.snapshot_id)
250
+ return { ok: false, error: '续读需要 snapshot_id;先读取 URL 获取稳定快照,避免拼接不同版本。' };
251
+ const budget = context.reserveReadChars?.(input.max_chars) ?? input.max_chars ?? DEFAULT_MAX_RESULT_SIZE_CHARS;
252
+ if (budget < 1000) return { ok: false, error: '本批读取预算已用完,请处理已有结果或压缩上下文后续读。' };
244
253
  try {
245
- targetHostname = new URL(url).hostname;
246
- } catch {
247
- targetHostname = '';
248
- }
249
- const preapproved = isPreapprovedDomain(targetHostname);
250
-
251
- // 检查缓存
252
- const cached = URL_CACHE.get(cacheKey);
253
- if (cached && Date.now() - cached.fetchedAt <= CACHE_TTL_MS) {
254
- let content = cached.content;
255
- const contentType = cached.contentType;
256
-
257
- if (contentType.includes('text/html')) {
258
- try {
259
- content = await htmlToMarkdown(content);
260
- } catch (e) {
261
- console.warn(`turndown 失败: ${(e as Error).message}`);
254
+ let page: PageSnapshot;
255
+ let status = '';
256
+ if (input.snapshot_id) {
257
+ page = await loadPageSnapshot(input.snapshot_id, input.url, context);
258
+ } else {
259
+ const cached = URL_CACHE.get(input.url);
260
+ const hit = cached && Date.now() - cached.fetchedAt <= CACHE_TTL_MS;
261
+ const fetched = hit ? { type: 'success' as const, ...cached } : await fetchURL(input.url, signal ?? new AbortController().signal);
262
+ if (fetched.type === 'error') return { ok: false, error: fetched.error };
263
+ if (fetched.type === 'redirect') return { ok: true, content:
264
+ `【重定向检测】\n原始 URL: ${fetched.originalUrl}\n重定向到: ${fetched.redirectUrl}\n状态: ${fetched.statusCode}\n请使用重定向后的 URL 再次调用 WebFetch 工具。` };
265
+ const fetchedAt = hit ? cached.fetchedAt : Date.now();
266
+ if (!hit) {
267
+ URL_CACHE.set(input.url, { ...fetched, fetchedAt });
268
+ cleanCache();
262
269
  }
270
+ let text = fetched.content;
271
+ let format = 'text';
272
+ if (fetched.contentType.includes('text/html')) {
273
+ try { text = await htmlToMarkdown(text); format = 'markdown'; }
274
+ catch { format = 'raw-html (正文转换失败,保留原始 HTML)'; }
275
+ }
276
+ page = { url: input.url, text, format, fetchedAt };
277
+ status = `状态: ${fetched.code} ${fetched.codeText}${hit ? ' (缓存命中)' : ''}\n`;
263
278
  }
264
-
265
- const formattedSize = cached.bytes < 1024
266
- ? `${cached.bytes} B`
267
- : cached.bytes < 1024 * 1024
268
- ? `${(cached.bytes / 1024).toFixed(1)} KB`
269
- : `${(cached.bytes / 1024 / 1024).toFixed(1)} MB`;
270
-
271
- const domainNote = preapproved
279
+ signal?.throwIfAborted();
280
+ const snapshot = input.snapshot_id
281
+ ? { id: input.snapshot_id, path: pageSnapshotPath(input.snapshot_id, context) }
282
+ : await savePageSnapshot(page, context);
283
+ signal?.throwIfAborted();
284
+ const window = pageWindow(page.text, input.offset ?? 0, budget, input.query);
285
+ const domain = isPreapprovedDomain(valid.parsed.hostname)
272
286
  ? '[网络访问:预批准域名]'
273
287
  : '[网络访问:普通域名;请按来源本身评估证据质量]';
274
-
275
- const output = `【WebFetch 结果】
276
- URL: ${url}
277
- 状态: ${cached.code} ${cached.codeText}
278
- 大小: ${formattedSize}
279
- 耗时: 0.00s (缓存命中)
280
- ${domainNote}
281
- [📦 来自缓存(15分钟 TTL)]
282
-
283
- --- 内容 ---
284
- ${content}`;
285
-
286
- let final = output;
287
- if (final.length > DEFAULT_MAX_RESULT_SIZE_CHARS) {
288
- final = final.slice(0, DEFAULT_MAX_RESULT_SIZE_CHARS) + `\n\n... (输出超过 ${DEFAULT_MAX_RESULT_SIZE_CHARS} 字符,已截断)`;
289
- }
290
-
291
- return { ok: true, content: final };
288
+ return { ok: true, policy: { highValue: true, reclaimable: false, boundedWindow: true,
289
+ archiveRef: snapshot.path, originalChars: page.text.length }, content: `【WebFetch 结果】
290
+ URL: ${page.url}
291
+ ${status}${domain}
292
+ snapshot_id: ${snapshot.id}
293
+ snapshot_complete: true (完整抓取正文,不代表动态页面内容完整)
294
+ format: ${page.format}
295
+ total_chars: ${page.text.length}
296
+ range: [${window.start}, ${window.end})
297
+ next_offset: ${window.next ?? 'null'}
298
+ 完整正文文件: ${snapshot.path}
299
+ ${window.found ? '' : '未找到指定文本(精确匹配,区分大小写)。\n'}--- 内容 ---
300
+ ${window.text}` };
301
+ } catch (error) {
302
+ return { ok: false, error: `网页快照读取失败:${(error as Error).message}。未返回不完整正文;请重试原 URL 或检查本地存储。` };
292
303
  }
293
-
294
- // 1. fetch
295
- const fetched = await fetchURL(url, signal ?? new AbortController().signal);
296
- const durationMs = Date.now() - start;
297
-
298
- if (fetched.type === 'redirect') {
299
- const statusText =
300
- fetched.statusCode === 301 ? 'Moved Permanently' :
301
- fetched.statusCode === 308 ? 'Permanent Redirect' :
302
- fetched.statusCode === 307 ? 'Temporary Redirect' : 'Found';
303
-
304
- return {
305
- ok: true,
306
- content: `【重定向检测】
307
-
308
- 原始 URL: ${fetched.originalUrl}
309
- 重定向到: ${fetched.redirectUrl}
310
- 状态: ${fetched.statusCode} ${statusText}
311
-
312
- 请使用重定向后的 URL 再次调用 WebFetch 工具。`,
313
- };
314
- }
315
-
316
- if (fetched.type === 'error') {
317
- return { ok: false, error: fetched.error };
318
- }
319
-
320
- let { content, bytes, code, codeText, contentType } = fetched;
321
-
322
- // 写入缓存
323
- URL_CACHE.set(cacheKey, {
324
- bytes,
325
- code,
326
- codeText,
327
- content,
328
- contentType,
329
- fetchedAt: Date.now(),
330
- });
331
- cleanCache();
332
-
333
- // 2. HTML → Markdown
334
- if (contentType.includes('text/html')) {
335
- try {
336
- content = await htmlToMarkdown(content);
337
- } catch (e) {
338
- console.warn(`turndown 失败: ${(e as Error).message}`);
339
- }
340
- }
341
-
342
- // 3. 内容直接返回(超长内容在这里直接截断)
343
- // 4. 追加 metadata
344
- const formattedSize = bytes < 1024
345
- ? `${bytes} B`
346
- : bytes < 1024 * 1024
347
- ? `${(bytes / 1024).toFixed(1)} KB`
348
- : `${(bytes / 1024 / 1024).toFixed(1)} MB`;
349
-
350
- const domainNote = preapproved
351
- ? '[网络访问:预批准域名]'
352
- : '[网络访问:普通域名;请按来源本身评估证据质量]';
353
-
354
- const output = `【WebFetch 结果】
355
- URL: ${url}
356
- 状态: ${code} ${codeText}
357
- 大小: ${formattedSize}
358
- 耗时: ${(durationMs / 1000).toFixed(2)}s
359
- ${domainNote}
360
-
361
- --- 内容 ---
362
- ${content}`;
363
-
364
- // 5. 截断
365
- let final = output;
366
- if (final.length > DEFAULT_MAX_RESULT_SIZE_CHARS) {
367
- final = final.slice(0, DEFAULT_MAX_RESULT_SIZE_CHARS) + `\n\n... (输出超过 ${DEFAULT_MAX_RESULT_SIZE_CHARS} 字符,已截断)`;
368
- }
369
-
370
- return { ok: true, content: final };
371
304
  }
372
305
 
373
306
  // ---------------- 7. 导出 ----------------
@@ -378,7 +311,8 @@ export const webfetchTool: Tool<WebFetchInput> = {
378
311
  parameters,
379
312
  isReadOnly: true,
380
313
  isConcurrencySafe: true,
381
- maxResultSizeChars: DEFAULT_MAX_RESULT_SIZE_CHARS,
314
+ maxResultSizeChars: 1_010_000,
382
315
  interruptBehavior: () => 'cancel',
383
316
  call,
317
+ callWithContext: (input, signal, context) => call(input, signal, context),
384
318
  };
package/src/types.ts CHANGED
@@ -480,6 +480,9 @@ export type ToolResultMetadata =
480
480
  export interface ToolResultPolicy {
481
481
  reclaimable: boolean;
482
482
  highValue: boolean;
483
+ /** Tool already budgeted a window backed by a complete local snapshot.
484
+ * Keep this window inline: re-archiving would invalidate its visible cursor. */
485
+ boundedWindow?: boolean;
483
486
  archiveRef?: string;
484
487
  originalChars?: number;
485
488
  }