@echo-agent/coding 0.5.0 → 0.7.0
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 +4 -4
- package/src/agent.ts +25 -5
- package/src/cli.ts +4 -4
- package/src/extensions.ts +4 -2
- package/src/permission.ts +5 -1
- package/src/prompt.ts +29 -6
- package/src/tools/bash.ts +127 -18
- package/src/tools/fs.ts +236 -43
- package/src/tools/search.ts +137 -58
- package/src/tools/web.ts +188 -21
package/src/tools/search.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
// 搜索两件套:glob(找文件)/ grep(找内容)。用 Bun.Glob,零依赖。
|
|
2
|
-
//
|
|
1
|
+
// 搜索两件套:glob(找文件)/ grep(找内容)。用 Bun.Glob 与 git,零依赖。
|
|
2
|
+
//
|
|
3
|
+
// **候选文件从哪来**(`candidateFiles`):起点在 git 工作树里,就取 `git ls-files --cached --others --exclude-standard`
|
|
4
|
+
// ——跟踪的文件加上没被忽略的新文件,与 ripgrep 的忽略语义一致:被 .gitignore 的 dist / build 不进来,
|
|
5
|
+
// .github 这类点目录照进。不在 git 工作树里(或 git 不可用)就目录遍历,点目录同样照进。
|
|
6
|
+
// git 列出来一个都没有(点名的目录整个被忽略)也退回目录遍历:点名了就是要搜它。
|
|
7
|
+
// node_modules / .git 两种来源都恒排除——那里面的匹配对写代码没用,还会把结果挤爆。软链不跟(目录遍历本就不跟,
|
|
8
|
+
// git 列出的软链也跳过):经软链可以出工作区,边界只在起点 `path` 上查过一次。
|
|
9
|
+
// 结果**按路径排序之后再截**,截断时说出总数——按扫描顺序截,留下的是哪些全凭目录遍历的顺序。
|
|
3
10
|
//
|
|
4
11
|
// **`path` 指到文件就只搜这一个文件**(2026-09-01,实测 bug):模型把 grep 当 `grep <pattern> <file>` 用是常态,
|
|
5
12
|
// 此前把文件当 cwd 交给 Bun.Glob,甩给模型一句 `ENOTDIR: not a directory`,相对、绝对路径都炸。
|
|
@@ -8,23 +15,33 @@
|
|
|
8
15
|
// **结果路径一律相对工作区**:read_file / edit_file 收的是工作区相对路径,搜索若按 `path` 相对报
|
|
9
16
|
// (在 `packages/core/src` 里搜到 `agent.ts:260`),模型拿着 `agent.ts` 去读就找不到。
|
|
10
17
|
//
|
|
11
|
-
// **边界与文件三件套同一条**(review 2026-09-07):`path` 经 fs.ts 的 `
|
|
18
|
+
// **边界与文件三件套同一条**(review 2026-09-07):`path` 经 fs.ts 的 `resolveInWorkspace` 解析,越界(含经软链)一律拒。
|
|
12
19
|
// 此前这三件没有任何边界,而 system 段对模型说的是「工作区之外的路径会被拒」——契约与实现两张皮,
|
|
13
20
|
// 且凡是把 bash 设成 ask 的策略,grep 都是绕开那道门读整块盘的路。
|
|
14
21
|
|
|
15
|
-
import { readdir, readFile, stat } from "node:fs/promises";
|
|
22
|
+
import { lstat, readdir, readFile, stat } from "node:fs/promises";
|
|
16
23
|
import { relative, resolve } from "node:path";
|
|
17
24
|
import { toolError, toolOk, type ModelTool, type ToolExecutionContext } from "@echo-agent/core";
|
|
18
|
-
import {
|
|
25
|
+
import { resolveInWorkspace } from "./fs.ts";
|
|
19
26
|
|
|
20
27
|
const GLOB_CAP = 500;
|
|
28
|
+
/** grep 最多回这么多条(content 是行,另两种是文件)。 */
|
|
21
29
|
const GREP_CAP = 200;
|
|
22
|
-
const GREP_FILE_MAX = 1_000_000; // >1MB 的文件跳过(多半是产物/数据,不是代码)
|
|
30
|
+
const GREP_FILE_MAX = 1_000_000; // >1MB 的文件跳过(多半是产物/数据,不是代码),跳过几个在结果尾部说
|
|
23
31
|
const LINE_CAP = 250;
|
|
24
32
|
const LIST_CAP = 500;
|
|
25
33
|
|
|
26
34
|
const EXCLUDED = /(^|\/)(node_modules|\.git)(\/|$)/;
|
|
27
35
|
|
|
36
|
+
const OUTPUT_MODES = ["content", "files_with_matches", "count"] as const;
|
|
37
|
+
type OutputMode = (typeof OUTPUT_MODES)[number];
|
|
38
|
+
|
|
39
|
+
/** 说明排除规则的那一句:glob 与 grep 的 description 共用。 */
|
|
40
|
+
const SCOPE_TEXT =
|
|
41
|
+
"Inside a git repository it searches the files git sees (tracked plus untracked, honoring .gitignore), otherwise every file under path; " +
|
|
42
|
+
"dot-directories such as .github are included, .git and node_modules never are, symlinks are not followed. " +
|
|
43
|
+
"Naming an ignored directory as path searches it.";
|
|
44
|
+
|
|
28
45
|
/** 搜索三件:glob / grep / list_dir。`list_dir` 是 2026-09-02 补的——glob 只出文件,模型看不到目录结构,只能 `bash ls`。 */
|
|
29
46
|
export function makeSearchTools(): ModelTool[] {
|
|
30
47
|
return [globTool(), grepTool(), listDirTool()] as ModelTool[];
|
|
@@ -39,18 +56,53 @@ type Target = { ok: true; kind: "file" | "dir"; abs: string } | { ok: false; err
|
|
|
39
56
|
|
|
40
57
|
/** `path` → 搜索起点。越界、不存在、是文件、是目录分开说,错误文案要让模型知道下一步怎么办。 */
|
|
41
58
|
async function resolveTarget(ctx: ToolExecutionContext, path?: string): Promise<Target> {
|
|
42
|
-
const
|
|
43
|
-
if (
|
|
59
|
+
const safe = path === undefined || path === "" ? { ok: true as const, abs: resolve(ctx.workspace) } : await resolveInWorkspace(ctx, path);
|
|
60
|
+
if (!safe.ok) return { ok: false, error: safe.error };
|
|
61
|
+
const abs = safe.abs;
|
|
44
62
|
const st = await stat(abs).catch(() => null);
|
|
45
63
|
if (st === null) return { ok: false, error: `path not found: ${path ?? abs}` };
|
|
46
64
|
return { ok: true, kind: st.isFile() ? "file" : "dir", abs };
|
|
47
65
|
}
|
|
48
66
|
|
|
49
|
-
/**
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
67
|
+
/**
|
|
68
|
+
* 目录 `dir` 下匹配 `pattern` 的文件,绝对路径、按路径排好。来源见文件头:git 工作树里取 git 认的文件,否则目录遍历;
|
|
69
|
+
* node_modules / .git 恒排除,软链不跟。
|
|
70
|
+
*/
|
|
71
|
+
async function candidateFiles(dir: string, pattern: string): Promise<string[]> {
|
|
72
|
+
const glob = new Bun.Glob(pattern);
|
|
73
|
+
const rels: string[] = [];
|
|
74
|
+
const listed = await gitListFiles(dir);
|
|
75
|
+
if (listed !== null && listed.length > 0) {
|
|
76
|
+
for (const p of listed) {
|
|
77
|
+
if (EXCLUDED.test(p) || !glob.match(p)) continue;
|
|
78
|
+
// 已删未提交(index 里还有)、子模块(目录)、软链:都不是这里要读的普通文件,与目录遍历同一口径
|
|
79
|
+
const st = await lstat(resolve(dir, p)).catch(() => null);
|
|
80
|
+
if (st?.isFile() === true) rels.push(p);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
for await (const p of glob.scan({ cwd: dir, onlyFiles: true, dot: true })) {
|
|
84
|
+
if (!EXCLUDED.test(p)) rels.push(p);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
rels.sort();
|
|
88
|
+
return rels.map((p) => resolve(dir, p));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** `dir` 下 git 认的文件(相对 `dir`,去重)。不是 git 工作树、git 不可用 → null。 */
|
|
92
|
+
async function gitListFiles(dir: string): Promise<string[] | null> {
|
|
93
|
+
try {
|
|
94
|
+
const proc = Bun.spawn(["git", "ls-files", "--cached", "--others", "--exclude-standard", "-z"], {
|
|
95
|
+
cwd: dir,
|
|
96
|
+
stdin: "ignore",
|
|
97
|
+
stdout: "pipe",
|
|
98
|
+
stderr: "ignore",
|
|
99
|
+
});
|
|
100
|
+
const out = await new Response(proc.stdout).text();
|
|
101
|
+
if ((await proc.exited) !== 0) return null;
|
|
102
|
+
// 有冲突时 --cached 会把同一路径按 stage 列好几遍
|
|
103
|
+
return [...new Set(out.split("\0").filter((p) => p !== ""))];
|
|
104
|
+
} catch {
|
|
105
|
+
return null;
|
|
54
106
|
}
|
|
55
107
|
}
|
|
56
108
|
|
|
@@ -61,7 +113,8 @@ function globTool(): ModelTool<{ pattern: string; path?: string }> {
|
|
|
61
113
|
label: "找文件",
|
|
62
114
|
concurrent: true, // 只读
|
|
63
115
|
description:
|
|
64
|
-
|
|
116
|
+
`Find files by glob pattern, e.g. "**/*.ts" or "src/**/*.test.ts". Returns paths relative to the workspace, sorted, at most ${GLOB_CAP}. ` +
|
|
117
|
+
SCOPE_TEXT,
|
|
65
118
|
parameters: {
|
|
66
119
|
type: "object",
|
|
67
120
|
properties: {
|
|
@@ -76,18 +129,15 @@ function globTool(): ModelTool<{ pattern: string; path?: string }> {
|
|
|
76
129
|
if (target.kind === "file") {
|
|
77
130
|
return toolError(`path is a file, glob needs a directory: ${path}. To search inside one file, use grep with path=${path}.`);
|
|
78
131
|
}
|
|
79
|
-
|
|
132
|
+
let all: string[];
|
|
80
133
|
try {
|
|
81
|
-
|
|
82
|
-
out.push(display(ctx, abs));
|
|
83
|
-
if (out.length >= GLOB_CAP) break;
|
|
84
|
-
}
|
|
134
|
+
all = await candidateFiles(target.abs, pattern);
|
|
85
135
|
} catch (e) {
|
|
86
136
|
return toolError(`glob failed: ${String(e)}`);
|
|
87
137
|
}
|
|
88
|
-
|
|
89
|
-
const cap =
|
|
90
|
-
return toolOk(
|
|
138
|
+
const shown = all.slice(0, GLOB_CAP).map((abs) => display(ctx, abs));
|
|
139
|
+
const cap = all.length > GLOB_CAP ? `\n…[${all.length} files match; showing the first ${GLOB_CAP} by path. Narrow the pattern or path]` : "";
|
|
140
|
+
return toolOk(shown.length === 0 ? "(no matches)" : shown.join("\n") + cap, { count: all.length });
|
|
91
141
|
},
|
|
92
142
|
};
|
|
93
143
|
}
|
|
@@ -127,67 +177,96 @@ function listDirTool(): ModelTool<{ path?: string }> {
|
|
|
127
177
|
};
|
|
128
178
|
}
|
|
129
179
|
|
|
130
|
-
|
|
180
|
+
type GrepParams = { pattern: string; path?: string; glob?: string; ignore_case?: boolean; output_mode?: string };
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* grep 的两个参数(2026-11-03 拍板,`docs/decisions/implemented/2026-11-03-grep-ignore-case-and-output-mode.md`):
|
|
184
|
+
* `ignore_case`(JS 正则没有 `(?i)` 内联写法)与 `output_mode`——只要知道「哪些文件有」或「各有几处」时,
|
|
185
|
+
* 不必把每一行都搬进上下文。缺省 `content`,与此前的输出逐字相同。
|
|
186
|
+
*/
|
|
187
|
+
function grepTool(): ModelTool<GrepParams> {
|
|
131
188
|
return {
|
|
132
189
|
kind: "model",
|
|
133
190
|
name: "grep",
|
|
134
191
|
label: "找内容",
|
|
135
192
|
concurrent: true, // 只读
|
|
136
193
|
description:
|
|
137
|
-
"Search file contents with a regular expression;
|
|
138
|
-
"path may be a directory (searched recursively, narrowed by glob) or a single file (searched alone)."
|
|
194
|
+
"Search file contents with a regular expression; paths in the results are relative to the workspace. " +
|
|
195
|
+
"path may be a directory (searched recursively, narrowed by glob) or a single file (searched alone). " +
|
|
196
|
+
`output_mode "content" (default) returns file:line:text, "files_with_matches" returns the matching files, "count" returns file:count. ` +
|
|
197
|
+
`Results are sorted by path, at most ${GREP_CAP} entries; files over 1 MB are skipped and counted at the end. ${SCOPE_TEXT}`,
|
|
139
198
|
parameters: {
|
|
140
199
|
type: "object",
|
|
141
200
|
properties: {
|
|
142
|
-
pattern: { type: "string", description: "JavaScript regular expression (without slashes)" },
|
|
201
|
+
pattern: { type: "string", description: "JavaScript regular expression (without slashes or inline flags)" },
|
|
143
202
|
path: { type: "string", description: "Directory or file to search, default the workspace" },
|
|
144
203
|
glob: { type: "string", description: 'Limit to these files when path is a directory, default "**/*"' },
|
|
204
|
+
ignore_case: { type: "boolean", description: "Match case-insensitively, default false" },
|
|
205
|
+
output_mode: { type: "string", enum: [...OUTPUT_MODES], description: 'What to return, default "content"' },
|
|
145
206
|
},
|
|
146
207
|
required: ["pattern"],
|
|
147
208
|
},
|
|
148
|
-
async execute({ pattern, path, glob }, ctx) {
|
|
209
|
+
async execute({ pattern, path, glob, ignore_case, output_mode }, ctx) {
|
|
210
|
+
const mode = (output_mode ?? "content") as OutputMode;
|
|
211
|
+
if (!OUTPUT_MODES.includes(mode)) return toolError(`output_mode must be one of ${OUTPUT_MODES.join(", ")} (got '${output_mode}')`);
|
|
149
212
|
let re: RegExp;
|
|
150
213
|
try {
|
|
151
|
-
re = new RegExp(pattern);
|
|
214
|
+
re = new RegExp(pattern, ignore_case === true ? "i" : "");
|
|
152
215
|
} catch (e) {
|
|
153
|
-
|
|
216
|
+
const inline = /^\(\?[a-z]+\)/.test(pattern) ? " JavaScript regular expressions have no inline flags such as (?i); use ignore_case: true." : "";
|
|
217
|
+
return toolError(`Invalid regular expression: ${String(e)}.${inline}`);
|
|
154
218
|
}
|
|
155
219
|
const target = await resolveTarget(ctx, path);
|
|
156
220
|
if (!target.ok) return toolError(target.error);
|
|
157
|
-
|
|
158
|
-
const files: Iterable<string> | AsyncIterable<string> =
|
|
159
|
-
target.kind === "file" ? [target.abs] : scanDir(target.abs, glob ?? "**/*");
|
|
160
|
-
const lines: string[] = [];
|
|
161
|
-
let scanned = 0;
|
|
221
|
+
let files: string[];
|
|
162
222
|
try {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (st === null || st.size > GREP_FILE_MAX) continue;
|
|
166
|
-
scanned += 1;
|
|
167
|
-
let raw: string;
|
|
168
|
-
try {
|
|
169
|
-
raw = await readFile(abs, "utf8");
|
|
170
|
-
} catch {
|
|
171
|
-
continue; // 读不动的(二进制、权限)跳过
|
|
172
|
-
}
|
|
173
|
-
if (raw.includes("\u0000")) continue; // 二进制
|
|
174
|
-
const shown = display(ctx, abs);
|
|
175
|
-
const fileLines = raw.split("\n");
|
|
176
|
-
for (let i = 0; i < fileLines.length; i++) {
|
|
177
|
-
const line = fileLines[i]!;
|
|
178
|
-
if (!re.test(line)) continue;
|
|
179
|
-
lines.push(`${shown}:${i + 1}:${line.length > LINE_CAP ? line.slice(0, LINE_CAP) + "…" : line}`);
|
|
180
|
-
if (lines.length >= GREP_CAP) break;
|
|
181
|
-
}
|
|
182
|
-
if (lines.length >= GREP_CAP) break;
|
|
183
|
-
}
|
|
223
|
+
// 单文件:用户点名的那一个,不过 glob 也不过排除表——点名了就是要搜它
|
|
224
|
+
files = target.kind === "file" ? [target.abs] : await candidateFiles(target.abs, glob ?? "**/*");
|
|
184
225
|
} catch (e) {
|
|
185
226
|
return toolError(`grep failed: ${String(e)}`);
|
|
186
227
|
}
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
228
|
+
const entries: string[] = [];
|
|
229
|
+
let total = 0; // content = 匹配的行数;另两种 = 有匹配的文件数
|
|
230
|
+
let scanned = 0;
|
|
231
|
+
let tooLarge = 0;
|
|
232
|
+
for (const abs of files) {
|
|
233
|
+
const st = await stat(abs).catch(() => null);
|
|
234
|
+
if (st === null) continue;
|
|
235
|
+
if (st.size > GREP_FILE_MAX) {
|
|
236
|
+
tooLarge += 1;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
let raw: string;
|
|
240
|
+
try {
|
|
241
|
+
raw = await readFile(abs, "utf8");
|
|
242
|
+
} catch {
|
|
243
|
+
continue; // 读不动的(权限)跳过
|
|
244
|
+
}
|
|
245
|
+
if (raw.includes("\u0000")) continue; // 二进制
|
|
246
|
+
scanned += 1;
|
|
247
|
+
const shown = display(ctx, abs);
|
|
248
|
+
let inFile = 0;
|
|
249
|
+
for (const [i, line] of raw.split("\n").entries()) {
|
|
250
|
+
if (!re.test(line)) continue;
|
|
251
|
+
inFile += 1;
|
|
252
|
+
if (mode === "files_with_matches") break;
|
|
253
|
+
if (mode === "content") {
|
|
254
|
+
total += 1;
|
|
255
|
+
if (entries.length < GREP_CAP) entries.push(`${shown}:${i + 1}:${line.length > LINE_CAP ? line.slice(0, LINE_CAP) + "…" : line}`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (inFile > 0 && mode !== "content") {
|
|
259
|
+
total += 1;
|
|
260
|
+
if (entries.length < GREP_CAP) entries.push(mode === "count" ? `${shown}:${inFile}` : shown);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const unit = mode === "content" ? "matching lines" : "files match";
|
|
264
|
+
const cap = total > GREP_CAP ? `\n…[${total} ${unit} in total; showing the first ${GREP_CAP} by path. Narrow the pattern, path or glob]` : "";
|
|
265
|
+
const skipped = tooLarge === 0 ? "" : `\n(${tooLarge} ${tooLarge === 1 ? "file" : "files"} over 1 MB not searched; search them with bash if you need to)`;
|
|
266
|
+
return toolOk((entries.length === 0 ? `(no matches in ${scanned} files)` : entries.join("\n") + cap) + skipped, {
|
|
267
|
+
matches: total,
|
|
190
268
|
scanned,
|
|
269
|
+
tooLarge,
|
|
191
270
|
});
|
|
192
271
|
},
|
|
193
272
|
};
|
package/src/tools/web.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
// web 一组(2026-09-03/04):`web_fetch` 取一个 URL 的正文;`web_search` 走 Brave Search API 拿结果列表。
|
|
2
|
-
//
|
|
2
|
+
// 不做出网确认(用户拍板:缺省全放行);但 web_fetch 不取内网地址(2026-11-03,见 `isPrivateAddress`)。
|
|
3
|
+
// HTML 在这层剥成可读文本,不加依赖。
|
|
3
4
|
// 两件都是延迟工具(`deferred`):多数编码任务用不上,经 tool_search 取过才上菜单。
|
|
4
5
|
//
|
|
5
6
|
// 搜索源选 Brave(2026-09-04 用户拍板):与模型无关,一把 key 五家 provider 通用;厂商自带搜索要改 core 的
|
|
6
7
|
// dialect 且 deepseek 没有,另议。key 的解析顺序与模型 key 一样:环境变量 `BRAVE_API_KEY` → 凭据 store 的
|
|
7
8
|
// `brave` 条目 → 没有(工具如实报「没配」,不猜)。key 的值不进任何错误信息。
|
|
8
9
|
|
|
10
|
+
import { lookup } from "node:dns/promises";
|
|
11
|
+
import { isIP } from "node:net";
|
|
9
12
|
import { toolError, toolOk, type CredentialStore, type ModelTool } from "@echo-agent/core";
|
|
10
13
|
|
|
11
14
|
const TIMEOUT_MS = 30_000;
|
|
15
|
+
/** 重定向最多跟这么多跳(与 opencode 的 webfetch 同数):每跳都重查地址,所以手动跟、不交给 fetch。 */
|
|
16
|
+
const MAX_REDIRECTS = 10;
|
|
17
|
+
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
12
18
|
/** 响应体最多读这么多字节:超过就截,避免一个大文件把内存与上下文一起吃掉。 */
|
|
13
19
|
const MAX_BYTES = 4_000_000;
|
|
14
20
|
const DEFAULT_MAX_CHARS = 20_000;
|
|
@@ -26,10 +32,98 @@ export type WebDeps = {
|
|
|
26
32
|
credentials?: Pick<CredentialStore, "read">;
|
|
27
33
|
/** 测试用:把 Brave 的接口指到假服务。 */
|
|
28
34
|
searchEndpoint?: string;
|
|
35
|
+
/**
|
|
36
|
+
* 测试用:web_fetch 判「是不是内网」之前那一步主机名解析(缺省:IP 字面量原样,其余走系统 DNS)。
|
|
37
|
+
* 本机起的假服务在 127.0.0.1,测试把它解析成公网地址才发得出去;它只换「查什么」,判据本身不放开。
|
|
38
|
+
*/
|
|
39
|
+
resolveHost?: (host: string) => Promise<readonly string[]>;
|
|
29
40
|
};
|
|
30
41
|
|
|
31
42
|
export function makeWebTools(deps: WebDeps = {}): ModelTool[] {
|
|
32
|
-
return [webFetchTool(), webSearchTool(deps)] as ModelTool[];
|
|
43
|
+
return [webFetchTool(deps), webSearchTool(deps)] as ModelTool[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/* ─────────────────── 不取内网地址 ─────────────────── */
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* web_fetch 只取公网地址(2026-11-03 拍板,`docs/decisions/implemented/2026-11-03-web-fetch-refuses-private-addresses.md`):
|
|
50
|
+
* 抓回来的网页是不可信内容,一句注入就能让模型去取 `http://169.254.169.254/` 的云凭据或本机的管理口,
|
|
51
|
+
* 再把结果带进对话。所以每一跳发出之前都查:主机名解析出的**任一**地址落在下面这些段就拒。
|
|
52
|
+
* 已知的缺口:DNS rebinding(查的那次与 fetch 自己解析的那次之间换了答案)不在防线内——fetch 不让指定连哪个地址。
|
|
53
|
+
*/
|
|
54
|
+
export function isPrivateAddress(ip: string): boolean {
|
|
55
|
+
const bare = ip.replace(/%.*$/, ""); // IPv6 的 zone id(fe80::1%lo0)
|
|
56
|
+
const kind = isIP(bare);
|
|
57
|
+
if (kind === 4) return privateV4(bare.split(".").map(Number) as [number, number, number, number]);
|
|
58
|
+
if (kind === 6) return privateV6(bare);
|
|
59
|
+
return true; // 认不出的不放行
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function privateV4([a, b]: [number, number, number, number]): boolean {
|
|
63
|
+
return (
|
|
64
|
+
a === 0 || // 0.0.0.0/8:未指定 / 本网络
|
|
65
|
+
a === 10 || // 10/8
|
|
66
|
+
a === 127 || // loopback
|
|
67
|
+
(a === 169 && b === 254) || // 链路本地,含云元数据 169.254.169.254
|
|
68
|
+
(a === 172 && b >= 16 && b <= 31) || // 172.16/12
|
|
69
|
+
(a === 192 && b === 168) || // 192.168/16
|
|
70
|
+
(a === 100 && b >= 64 && b <= 127) // CGNAT 100.64/10
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function privateV6(ip: string): boolean {
|
|
75
|
+
const g = ipv6Groups(ip);
|
|
76
|
+
if (g === null) return true;
|
|
77
|
+
const zeroHead = g.slice(0, 5).every((x) => x === 0);
|
|
78
|
+
if (zeroHead && g[5] === 0xffff) return privateV4([g[6]! >> 8, g[6]! & 0xff, g[7]! >> 8, g[7]! & 0xff]); // ::ffff:a.b.c.d(IPv4 映射)
|
|
79
|
+
if (zeroHead && g[5] === 0) {
|
|
80
|
+
if (g[6] === 0 && (g[7] === 0 || g[7] === 1)) return true; // :: 未指定、::1 loopback
|
|
81
|
+
return privateV4([g[6]! >> 8, g[6]! & 0xff, g[7]! >> 8, g[7]! & 0xff]); // ::a.b.c.d(已废弃的 IPv4 兼容写法)
|
|
82
|
+
}
|
|
83
|
+
return (g[0]! & 0xfe00) === 0xfc00 || (g[0]! & 0xffc0) === 0xfe80; // fc00::/7 唯一本地、fe80::/10 链路本地
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** IPv6 文本 → 8 组 16 位。末尾可以是点分 IPv4。格式不对 = null。 */
|
|
87
|
+
function ipv6Groups(ip: string): number[] | null {
|
|
88
|
+
let text = ip.toLowerCase();
|
|
89
|
+
const v4 = /(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(text);
|
|
90
|
+
if (v4 !== null) {
|
|
91
|
+
const [a, b, c, d] = v4.slice(1).map(Number) as [number, number, number, number];
|
|
92
|
+
text = `${text.slice(0, v4.index)}${((a << 8) | b).toString(16)}:${((c << 8) | d).toString(16)}`;
|
|
93
|
+
}
|
|
94
|
+
const halves = text.split("::");
|
|
95
|
+
if (halves.length > 2) return null;
|
|
96
|
+
const parse = (s: string): number[] => (s === "" ? [] : s.split(":").map((h) => Number.parseInt(h, 16)));
|
|
97
|
+
const head = parse(halves[0]!);
|
|
98
|
+
const tail = halves.length === 2 ? parse(halves[1]!) : [];
|
|
99
|
+
const fill = 8 - head.length - tail.length;
|
|
100
|
+
if (fill < 0 || (halves.length === 1 && fill !== 0)) return null;
|
|
101
|
+
const groups = [...head, ...Array<number>(halves.length === 2 ? fill : 0).fill(0), ...tail];
|
|
102
|
+
return groups.length === 8 && groups.every((x) => Number.isInteger(x) && x >= 0 && x <= 0xffff) ? groups : null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 缺省的主机名解析:IP 字面量原样,其余走系统 DNS,取全部地址。 */
|
|
106
|
+
async function systemResolve(host: string): Promise<readonly string[]> {
|
|
107
|
+
if (isIP(host) !== 0) return [host];
|
|
108
|
+
return (await lookup(host, { all: true, verbatim: true })).map((a) => a.address);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 这一跳能不能发:null = 能;否则是给模型看的拒绝理由。 */
|
|
112
|
+
async function refusedHost(url: URL, resolveHost: (host: string) => Promise<readonly string[]>, via: string | null): Promise<string | null> {
|
|
113
|
+
const host = url.hostname.replace(/^\[|\]$/g, "");
|
|
114
|
+
let addresses: readonly string[];
|
|
115
|
+
try {
|
|
116
|
+
addresses = await resolveHost(host);
|
|
117
|
+
} catch (e) {
|
|
118
|
+
return `Fetch failed for ${url.href}: could not resolve ${host} (${e instanceof Error ? e.message : String(e)})`;
|
|
119
|
+
}
|
|
120
|
+
const bad = addresses.find(isPrivateAddress);
|
|
121
|
+
if (bad === undefined) return null;
|
|
122
|
+
const how = via === null ? url.href : `${via}, which redirects to ${url.href}`;
|
|
123
|
+
return (
|
|
124
|
+
`Refused to fetch ${how}: ${host} resolves to ${bad}, a loopback, private, link-local or otherwise non-public address. ` +
|
|
125
|
+
"web_fetch only reaches public internet addresses; this is a safety restriction, not a network error."
|
|
126
|
+
);
|
|
33
127
|
}
|
|
34
128
|
|
|
35
129
|
/** 环境变量 → 凭据 store → 没有。值只在这里经手,不进返回给模型的任何文字。 */
|
|
@@ -119,16 +213,19 @@ function stripTags(s: string): string {
|
|
|
119
213
|
return decodeEntities(s.replace(/<[^>]+>/g, "")).replace(/\s+/g, " ").trim();
|
|
120
214
|
}
|
|
121
215
|
|
|
122
|
-
function webFetchTool(): ModelTool<{ url: string; max_chars?: number }> {
|
|
216
|
+
function webFetchTool(deps: WebDeps): ModelTool<{ url: string; max_chars?: number }> {
|
|
217
|
+
const resolveHost = deps.resolveHost ?? systemResolve;
|
|
123
218
|
return {
|
|
124
219
|
kind: "model",
|
|
125
220
|
name: "web_fetch",
|
|
126
221
|
label: "取网页",
|
|
127
222
|
deferred: true,
|
|
128
223
|
description:
|
|
129
|
-
"Fetch a URL (http or https) and return its content as text: HTML is reduced to readable text with headings
|
|
130
|
-
"links kept as [text](url); JSON and plain text come back as they are.
|
|
131
|
-
|
|
224
|
+
"Fetch a URL (http or https) and return its content as text: HTML is reduced to readable text with headings, " +
|
|
225
|
+
"links kept as [text](url) and <pre> blocks kept verbatim as code blocks; JSON and plain text come back as they are. " +
|
|
226
|
+
`Follows up to ${MAX_REDIRECTS} redirects, 30 s timeout, output capped at max_chars (default 20000). ` +
|
|
227
|
+
"Only public internet addresses: loopback, private-network and link-local hosts (localhost, 10.x, 192.168.x, 169.254.x, ...) are refused. " +
|
|
228
|
+
"Find pages with web_search.",
|
|
132
229
|
parameters: {
|
|
133
230
|
type: "object",
|
|
134
231
|
properties: {
|
|
@@ -150,12 +247,30 @@ function webFetchTool(): ModelTool<{ url: string; max_chars?: number }> {
|
|
|
150
247
|
let res: Response;
|
|
151
248
|
let bytes: Uint8Array;
|
|
152
249
|
let clipped = false;
|
|
250
|
+
let current = target; // 跟到第几跳了:最终 URL 也是相对链接的基准
|
|
153
251
|
try {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
252
|
+
// 重定向手动逐跳跟:fetch 自己跟的话中间那几跳不经检查,公网页面一个 302 就能把请求带进内网
|
|
253
|
+
for (let hop = 0; ; hop++) {
|
|
254
|
+
const refused = await refusedHost(current, resolveHost, hop === 0 ? null : target.href);
|
|
255
|
+
if (refused !== null) return toolError(refused);
|
|
256
|
+
res = await fetch(current, {
|
|
257
|
+
signal,
|
|
258
|
+
redirect: "manual",
|
|
259
|
+
headers: { "user-agent": "echo-coding", accept: "text/html, text/plain, application/json;q=0.9, */*;q=0.5" },
|
|
260
|
+
});
|
|
261
|
+
const location = res.headers.get("location");
|
|
262
|
+
if (!REDIRECT_STATUSES.has(res.status) || location === null) break;
|
|
263
|
+
await res.body?.cancel();
|
|
264
|
+
if (hop >= MAX_REDIRECTS) return toolError(`Too many redirects (more than ${MAX_REDIRECTS}) starting from ${target.href}`);
|
|
265
|
+
let next: URL;
|
|
266
|
+
try {
|
|
267
|
+
next = new URL(location, current);
|
|
268
|
+
} catch {
|
|
269
|
+
return toolError(`${current.href} redirects to an invalid location: ${location}`);
|
|
270
|
+
}
|
|
271
|
+
if (next.protocol !== "http:" && next.protocol !== "https:") return toolError(`${current.href} redirects to an unsupported scheme: ${next.href}`);
|
|
272
|
+
current = next;
|
|
273
|
+
}
|
|
159
274
|
// 流式读、到上限就取消:`arrayBuffer()` 会先把整个响应体收进内存再截,MAX_BYTES 那时限不住内存(review 2026-09-07)
|
|
160
275
|
const chunks: Uint8Array[] = [];
|
|
161
276
|
let total = 0;
|
|
@@ -185,19 +300,20 @@ function webFetchTool(): ModelTool<{ url: string; max_chars?: number }> {
|
|
|
185
300
|
} catch (e) {
|
|
186
301
|
return toolError(`Fetch failed for ${url}: ${e instanceof Error ? e.message : String(e)}`);
|
|
187
302
|
}
|
|
188
|
-
const raw = new TextDecoder().decode(bytes);
|
|
189
303
|
const type = (res.headers.get("content-type") ?? "").toLowerCase();
|
|
190
|
-
|
|
304
|
+
const raw = decodeBody(bytes, type);
|
|
305
|
+
const finalUrl = current.href;
|
|
306
|
+
if (!res.ok) return toolError(`HTTP ${res.status} ${res.statusText} for ${finalUrl}\n${raw.slice(0, 500)}`);
|
|
191
307
|
let text: string;
|
|
192
|
-
if (type.includes("html")) text = htmlToText(raw);
|
|
308
|
+
if (type.includes("html")) text = htmlToText(raw, finalUrl);
|
|
193
309
|
else if (type === "" || type.startsWith("text/") || type.includes("json") || type.includes("xml")) text = raw;
|
|
194
310
|
else return toolError(`Unsupported content type '${type}' (${clipped ? `over ${MAX_BYTES}` : String(bytes.byteLength)} bytes) at ${url}`);
|
|
195
311
|
const cap = Math.max(1, max_chars ?? DEFAULT_MAX_CHARS);
|
|
196
|
-
const head =
|
|
312
|
+
const head = finalUrl !== target.href ? `(redirected to ${finalUrl})\n` : "";
|
|
197
313
|
const body =
|
|
198
314
|
text.length > cap ? `${text.slice(0, cap)}\n…[truncated: ${text.length} characters in total; raise max_chars to see more]` : text;
|
|
199
315
|
return toolOk(`${head}${body === "" ? "(empty)" : body}${clipped ? `\n…[response body over ${MAX_BYTES} bytes, cut]` : ""}`, {
|
|
200
|
-
url:
|
|
316
|
+
url: finalUrl,
|
|
201
317
|
status: res.status,
|
|
202
318
|
contentType: type,
|
|
203
319
|
});
|
|
@@ -205,19 +321,69 @@ function webFetchTool(): ModelTool<{ url: string; max_chars?: number }> {
|
|
|
205
321
|
};
|
|
206
322
|
}
|
|
207
323
|
|
|
208
|
-
/**
|
|
209
|
-
|
|
324
|
+
/**
|
|
325
|
+
* 响应体字节 → 文本:先认 `Content-Type` 的 charset;没写、又是 HTML(或没有类型)就看文档开头的 `<meta charset>` /
|
|
326
|
+
* `<meta http-equiv="Content-Type" content="…; charset=…">`;都没有或认不出这个编码名,按 UTF-8 解。
|
|
327
|
+
*/
|
|
328
|
+
function decodeBody(bytes: Uint8Array, contentType: string): string {
|
|
329
|
+
const declared =
|
|
330
|
+
/charset\s*=\s*["']?([^;"'\s]+)/i.exec(contentType)?.[1] ??
|
|
331
|
+
(contentType === "" || contentType.includes("html") ? sniffMetaCharset(bytes) : undefined);
|
|
332
|
+
if (declared !== undefined) {
|
|
333
|
+
try {
|
|
334
|
+
return new TextDecoder(declared).decode(bytes);
|
|
335
|
+
} catch {
|
|
336
|
+
// 编码名认不出:退 UTF-8
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return new TextDecoder().decode(bytes);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** 文档开头 4 KB 里的 `<meta … charset=…>`。按 latin1 读——编码名本身是 ASCII,读的时候还不知道编码。 */
|
|
343
|
+
function sniffMetaCharset(bytes: Uint8Array): string | undefined {
|
|
344
|
+
const head = new TextDecoder("latin1").decode(bytes.subarray(0, 4096));
|
|
345
|
+
return /<meta\b[^>]*?charset\s*=\s*["']?([^"'\s/>;]+)/i.exec(head)?.[1];
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** 链接目标 → 绝对 http(s) URL;本来就是绝对的原样返回。页内锚点、解析不了、不是 http(s) = null(只留文字)。 */
|
|
349
|
+
function absoluteLink(href: string, baseUrl: string | undefined): string | null {
|
|
350
|
+
if (/^https?:\/\//i.test(href)) return href;
|
|
351
|
+
if (href.startsWith("#") || baseUrl === undefined) return null;
|
|
352
|
+
try {
|
|
353
|
+
const u = new URL(href, baseUrl);
|
|
354
|
+
return u.protocol === "http:" || u.protocol === "https:" ? u.href : null;
|
|
355
|
+
} catch {
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* HTML → 可读文本:去 script/style/head,标题成 `#`、列表成 `-`、链接留成 `[text](url)`,块级标签换行,实体解码。
|
|
362
|
+
* `<pre>` 整块原样(缩进、空行都不压),成一段 ``` 代码块。给了页面 URL(`baseUrl`)就把相对链接解析成绝对链接;
|
|
363
|
+
* 没给只认本来就是绝对的 http(s) 链接。页内锚点(`#…`)与非 http(s) 链接只留文字。
|
|
364
|
+
*/
|
|
365
|
+
export function htmlToText(html: string, baseUrl?: string): string {
|
|
210
366
|
let s = html.replace(/<!--[\s\S]*?-->/g, "").replace(/<(script|style|noscript|svg|template)\b[\s\S]*?<\/\1\s*>/gi, "");
|
|
211
367
|
const title = /<title[^>]*>([\s\S]*?)<\/title>/i.exec(s)?.[1];
|
|
212
368
|
s = s.replace(/<head\b[\s\S]*?<\/head\s*>/i, "");
|
|
369
|
+
// pre 先摘出来换成占位(私用区字符):后面逐行压空白、折叠空行都碰不到它,最后原样放回
|
|
370
|
+
const blocks: string[] = [];
|
|
371
|
+
s = s.replace(/<pre\b[^>]*>([\s\S]*?)<\/pre\s*>/gi, (_m, inner: string) => {
|
|
372
|
+
const code = decodeEntities(inner.replace(/<br\b[^>]*\/?>/gi, "\n").replace(/<[^>]+>/g, ""))
|
|
373
|
+
.replace(/^\r?\n/, "") // 紧跟 <pre> 的那个换行按 HTML 规则不算内容
|
|
374
|
+
.replace(/\s+$/, "");
|
|
375
|
+
blocks.push(code);
|
|
376
|
+
return `\n\n${blocks.length - 1}\n\n`; // 前后各空一行:成段
|
|
377
|
+
});
|
|
213
378
|
s = s.replace(/<a\b[^>]*?href=["']([^"']+)["'][^>]*>([\s\S]*?)<\/a\s*>/gi, (_m, href: string, inner: string) => {
|
|
214
379
|
const label = inner.replace(/<[^>]+>/g, "").trim();
|
|
215
|
-
|
|
380
|
+
const link = absoluteLink(href, baseUrl);
|
|
381
|
+
return link !== null && label !== "" && label !== href && label !== link ? `[${label}](${link})` : label;
|
|
216
382
|
});
|
|
217
383
|
s = s
|
|
218
384
|
.replace(/<(br|hr)\b[^>]*\/?>/gi, "\n")
|
|
219
385
|
.replace(/<h([1-6])\b[^>]*>/gi, (_m, n: string) => `\n${"#".repeat(Number(n))} `)
|
|
220
|
-
.replace(/<\/(p|div|section|article|header|footer|main|nav|aside|h[1-6]|li|tr|blockquote|
|
|
386
|
+
.replace(/<\/(p|div|section|article|header|footer|main|nav|aside|h[1-6]|li|tr|blockquote|table|ul|ol|dd|dt|figure)\s*>/gi, "\n")
|
|
221
387
|
.replace(/<li\b[^>]*>/gi, "- ")
|
|
222
388
|
.replace(/<(td|th)\b[^>]*>/gi, " ")
|
|
223
389
|
.replace(/<[^>]+>/g, "");
|
|
@@ -227,7 +393,8 @@ export function htmlToText(html: string): string {
|
|
|
227
393
|
.map((l) => l.replace(/[ \t\r ]+/g, " ").trim())
|
|
228
394
|
.join("\n")
|
|
229
395
|
.replace(/\n{3,}/g, "\n\n")
|
|
230
|
-
.trim()
|
|
396
|
+
.trim()
|
|
397
|
+
.replace(/(\d+)/g, (_m, i: string) => `\`\`\`\n${blocks[Number(i)]}\n\`\`\``);
|
|
231
398
|
const t = title === undefined ? "" : decodeEntities(title).trim();
|
|
232
399
|
return t === "" ? text : `${t}\n\n${text}`;
|
|
233
400
|
}
|