fetch:超时覆盖全生命周期,流式字节上限,charset解码,取消监视;新增cfg.fetch;description重写
This commit is contained in:
@@ -27,6 +27,10 @@ terminal:
|
||||
default_timeout_ms: 30000 # 命令默认超时(ms)
|
||||
max_output_chars: 10000 # 回传模型的最大输出字符数
|
||||
|
||||
fetch:
|
||||
timeout_ms: 30000 # 抓取总超时(ms),覆盖响应头到 body 读完
|
||||
max_bytes: 1048576 # 单次响应最多抓多少字节,超限即断开
|
||||
|
||||
agent:
|
||||
temperature: 1.0 # 采样温度
|
||||
max_tool_iterations: 100 # 单轮最大迭代数(含工具调用)
|
||||
|
||||
@@ -70,6 +70,8 @@ const DEFAULTS = {
|
||||
// 命令执行服务:url 非空走远端 executor,留空本地 spawn;留空时回退 env EXECUTOR_URL / EXECUTOR_TOKEN。
|
||||
executor: { url: '', token: '' },
|
||||
terminal: { default_timeout_ms: 30000, max_output_chars: 10000 },
|
||||
// fetch:timeout_ms 覆盖请求发出到 body 读完的全生命周期;max_bytes 为流式抓取字节上限(超限即断开)。
|
||||
fetch: { timeout_ms: 30000, max_bytes: 1048576 },
|
||||
// auto_compress_tokens:本轮 In token 峰值 ≥ 此值时推送「自动压缩」提议(0=关)。
|
||||
agent: { temperature: 1.0, max_tool_iterations: 100, max_retries: 2, language: 'Simplified Chinese', auto_compress_tokens: 60000 },
|
||||
// 大图上传预处理(浏览器端 canvas 压缩,参数经 /api/system/status 下发前端)。
|
||||
|
||||
@@ -38,14 +38,14 @@
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "fetch",
|
||||
"description": "Fetches a URL and returns the content as Markdown. The request may be subject to timeout and response size limits.",
|
||||
"description": "Fetches a URL and returns its content as Markdown. Only http and https are supported; if the scheme is omitted, https is assumed.\n\nHTML pages are converted to Markdown. JSON, XML, plain text, and Markdown responses are returned as-is. Binary responses are not returned — only their HTTP status, content-type, and size.\n\nThe response always starts with a line `HTTP <status>`, followed by the body. Check the status line: a non-2xx status means the fetch did not succeed, even if body content is present.\n\nRequests are subject to a timeout, and oversized responses are truncated. Redirects are followed automatically.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["url"],
|
||||
"properties": {
|
||||
"url": {
|
||||
"type": "string",
|
||||
"description": "The URL to fetch."
|
||||
"description": "The URL to fetch. Must be http or https; if no scheme is given, https is assumed."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -512,7 +512,45 @@ function killGroup(child) {
|
||||
}
|
||||
}
|
||||
|
||||
// fetch:抓取并粗略转 Markdown
|
||||
// fetch:抓取并转 Markdown。超时/取消覆盖到 body 读取完成(非仅响应头);
|
||||
// 流式读取至多 cfg.fetch.max_bytes 字节;按响应 charset 解码(gbk 等依赖内置 full-icu)。
|
||||
function isTextualContentType(ct) {
|
||||
if (!ct) return true; // 未声明:先按文本读,读完用 NUL 字节兜底判定
|
||||
if (ct.startsWith('text/')) return true;
|
||||
if (/^application\/(?:[\w.-]+\+)?(?:json|xml|yaml|x-ndjson|ndjson)\b/.test(ct)) return true;
|
||||
if (/^application\/(?:javascript|ecmascript|xhtml\+xml)\b/.test(ct)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function charsetOf(ct) {
|
||||
const m = /charset\s*=\s*["']?([\w-]+)/i.exec(ct || '');
|
||||
return m ? m[1].toLowerCase() : '';
|
||||
}
|
||||
|
||||
// 流式读到 maxBytes 即停;超限 reader.cancel() 主动断开,不再继续下载。
|
||||
async function readBodyLimited(stream, maxBytes) {
|
||||
const reader = stream.getReader();
|
||||
const chunks = [];
|
||||
let total = 0;
|
||||
let truncated = false;
|
||||
try {
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
if (total + value.byteLength > maxBytes) {
|
||||
if (maxBytes > total) chunks.push(value.subarray(0, maxBytes - total));
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
chunks.push(value);
|
||||
total += value.byteLength;
|
||||
}
|
||||
} finally {
|
||||
if (truncated) { try { await reader.cancel(); } catch (_) {} }
|
||||
}
|
||||
return { buf: Buffer.concat(chunks.map(c => Buffer.from(c))), truncated };
|
||||
}
|
||||
|
||||
async function toolFetch(ctx, args) {
|
||||
if (ctx.run.cancelled) throw new Error('已终止');
|
||||
let url = String(args.url || '').trim();
|
||||
@@ -522,26 +560,58 @@ async function toolFetch(ctx, args) {
|
||||
if (/^[a-z0-9+.-]+:\/\//i.test(url)) throw new Error('只支持 http/https URL:' + url);
|
||||
url = 'https://' + url;
|
||||
}
|
||||
const fcfg = (ctx.cfg && ctx.cfg.fetch) || {};
|
||||
const timeoutMs = fcfg.timeout_ms || 30000;
|
||||
const maxBytes = fcfg.max_bytes || 1024 * 1024;
|
||||
|
||||
const ctrl = new AbortController();
|
||||
const timer = setTimeout(() => ctrl.abort(), 30000);
|
||||
let res;
|
||||
const timer = setTimeout(() => ctrl.abort(), timeoutMs);
|
||||
// 与 toolTerminal 同模式:等待中用户取消也能 abort 掉进行中的请求/读流。
|
||||
const cancelWatch = setInterval(() => { if (ctx.run.cancelled) ctrl.abort(); }, 200);
|
||||
try {
|
||||
res = await fetch(url, {
|
||||
const res = await fetch(url, {
|
||||
signal: ctrl.signal,
|
||||
redirect: 'follow',
|
||||
headers: { 'User-Agent': 'Mozilla/5.0 (agent-app)' },
|
||||
});
|
||||
const ct = (res.headers.get('content-type') || '').toLowerCase();
|
||||
// 非文本:不读 body,直接回报元信息(避免二进制白下载 + 乱码)。
|
||||
if (!isTextualContentType(ct)) {
|
||||
const len = Number(res.headers.get('content-length')) || 0;
|
||||
return 'HTTP ' + res.status + '\n[非文本响应] content-type=' + ct +
|
||||
(len ? ',' + len + ' 字节' : '') + ',未读取内容。';
|
||||
}
|
||||
if (!res.body) return 'HTTP ' + res.status + '\n(无响应体)';
|
||||
|
||||
const { buf, truncated } = await readBodyLimited(res.body, maxBytes);
|
||||
// content-type 未声明的兜底:前 8KB 含 NUL 判为二进制(与 read_file 的 isProbablyBinary 同口径)。
|
||||
if (!ct && buf.subarray(0, 8192).includes(0)) {
|
||||
return 'HTTP ' + res.status + '\n[非文本响应] content-type 未声明,body 疑似二进制(' + buf.length + ' 字节),未返回内容。';
|
||||
}
|
||||
// 按 charset 解码;未知/非法编码名退回 UTF-8,不抛错。
|
||||
const charset = charsetOf(ct);
|
||||
let text;
|
||||
if (!charset || charset === 'utf-8' || charset === 'utf8') {
|
||||
text = buf.toString('utf8');
|
||||
} else {
|
||||
try { text = new TextDecoder(charset, { fatal: false }).decode(buf); }
|
||||
catch (_) { text = buf.toString('utf8'); }
|
||||
}
|
||||
let body = ct.includes('html') ? htmlToMarkdown(text) : text;
|
||||
if (truncated) body += '\n(响应超过 ' + maxBytes + ' 字节,已截断)';
|
||||
// 输出字符数兜底:engine 不截工具结果,这是返回值上最后一道护栏。
|
||||
return 'HTTP ' + res.status + '\n' + truncate(body, 1024 * 1024);
|
||||
} catch (err) {
|
||||
// 超时与取消都表现为 signal.aborted(body 读流阶段的报错形态不定,按信号判定最稳)。
|
||||
if (ctrl.signal.aborted) {
|
||||
if (ctx.run.cancelled) throw new Error('已终止');
|
||||
throw new Error('fetch 超时(' + timeoutMs + 'ms):' + url);
|
||||
}
|
||||
throw new Error('fetch 失败:' + (err && err.message || err));
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
clearInterval(cancelWatch);
|
||||
}
|
||||
const text = await res.text();
|
||||
const ct = (res.headers.get('content-type') || '').toLowerCase();
|
||||
let body;
|
||||
if (ct.includes('html')) body = htmlToMarkdown(text);
|
||||
else if (ct.includes('json') || ct.includes('text') || ct.includes('xml') || ct.includes('markdown')) body = text;
|
||||
else return '[非文本响应] HTTP ' + res.status + ' content-type=' + ct + ',' + text.length + ' 字节';
|
||||
if (text.length > 1024 * 1024) body += '\n(响应过大,已截断)';
|
||||
return 'HTTP ' + res.status + '\n' + truncate(body, 1024 * 1024);
|
||||
}
|
||||
|
||||
function htmlToMarkdown(html) {
|
||||
|
||||
@@ -289,7 +289,7 @@ stateDiagram-v2
|
||||
| `write_file` / `edit_file` | `edit_file` 用模糊定位(Zed 风格):先精确匹配,失败后用 Levenshtein DP 找最相似行;`old_text`/`new_text` 协议见 `definitions.json` |
|
||||
| 已移除的工具 | `list_directory` / `create_directory` / `delete_path` / `move_path` / `copy_path` / `find_path` / `grep`:环境固定,文件操作与检索统一走 `terminal`(ls / mkdir / rm / mv / cp / find / rg),内置工具 17 → 10 |
|
||||
| `terminal` | **唯一一个走 executor 的内置工具**(当 `rexec.isRemote(cfg)`);本地分支用 `spawn` + `detached` + 进程组 kill;远端分支 `rexec.run` SSE 收流,结果套上超时/截断/取消注解后 `truncate(text, cfg.terminal.max_output_chars)` 返回 |
|
||||
| `fetch` | `AbortController` + UA + redirect + content-type 简单判断;HTML 用 `htmlToMarkdown` 转 |
|
||||
| `fetch` | `AbortController` 覆盖全生命周期(响应头之后的 body 读流同样被超时/取消打断,取消用与 terminal 同款的 200ms 轮询);content-type 先行,非文本只回状态/类型/尺寸不读 body;流式读到 `cfg.fetch.max_bytes`(默认 1MB)即断开并标注截断;按响应 charset 解码(full-icu 支持 gbk/gb18030 等,未知编码退回 UTF-8);HTML 用 `htmlToMarkdown` 转;engine 不截工具结果,返回值末尾另有 `truncate` 1MB 字符护栏 |
|
||||
| `mint_public_link` | **唯一的 mint 入口**:与 core 同进程,直接 `publicFiles.mint()`,不绕任何 HTTP;`ctx.taskDir` 就是会话目录,`relOf` 的结果正是取用侧要的 `rel`。校验:`public_link.base_url` 已配 + `resolveInTask` 沙箱 + 确认是文件 + `realpath` 未沿符号链接跳出任务目录;返回单行 URL |
|
||||
| `list_skills` / `get_skill_info` / `run_skill` | 委托 `tools/skills.js` |
|
||||
| `schedule_task` | 为**当前会话**(`ctx.sessionId`,AI 不可指定他会话)建定时任务;校验/写入全在 `scheduler.createTask`(cron 可解析 → 连续两次 `next()` 间隔 ≥ 1h → 查重 → INSERT 主键兑底),失败抛中文 Error 回给 AI;描述里限定「仅用户明确要求时调用」(安全缓解,见 §5.12) |
|
||||
@@ -336,7 +336,7 @@ stateDiagram-v2
|
||||
- 兼容:旧版 `models[]`(扁平)自动迁移到 `platforms[]` 层级;旧版 `web_search.*` 迁移到 `mcp_servers[]`(log.info 提示)。
|
||||
- 规范化:`normalizePlatforms` 校验 `name/url/type`,key 层 `concurrency`,model 层 `{id, display_name, vision, enable, extra_params, thinking_params, profile}`;同平台内 model id 必须唯一(内部唯一键 = 平台名/模型id);`profile` 引用不存在的名字直接报错。
|
||||
- `model_profiles`(顶层,`normalizeModelProfiles`):多套可复用的 prompt+tools 组合,每项 `{tools_allow?, system_prompt_file?}`,两者至少配一个。`tools_allow` 按**最终发给模型的工具名**匹配(内置 + MCP,含 `服务名__` 前缀,与 prompt.txt dump 同源可对);未配 = 全量,**空数组 `[]` = 零工具(纯文本组合)**——与 MCP 的 `tools_allow`(空名单视作未配)有意不同。`system_prompt_file` 相对 `server/templates/`(compose 已 bind-mount ./app/server,宿主直接可改),**配置加载时一次性读入常驻内存**,缺失/为空报错拦在保存/重启预检;自定义文件 = 完全接管模板正文,技能清单靠模板内 `{APP_SKILLS}` 占位符就地插入(不放 = 无技能段;默认模板同机制,见 §5.2),语言指令始终尾拼。运行期切换(前端传参)零 IO、不碰本配置。
|
||||
- 默认值(`DEFAULTS`):port=8090、workspace_root/data_root=`/workspace/tasks` `/workspace/data`、log_level=info、terminal 30s/10K、agent temperature=1.0、max_tool_iterations=100、max_retries=2、model_hold 3min/30min/10s、上传 1.5MB/2000px/0.85;内置 `model_profiles` 含 `mini`(文件/终端/网页白名单 + `mini-system.txt`)与 `zero`(零工具 + `zero-system.txt`)。`config.yml` 只覆盖 deploy 相关段,其余走默认值;整份清单见 `app/config.example.yml`。
|
||||
- 默认值(`DEFAULTS`):port=8090、workspace_root/data_root=`/workspace/tasks` `/workspace/data`、log_level=info、terminal 30s/10K、fetch 30s/1MB、agent temperature=1.0、max_tool_iterations=100、max_retries=2、model_hold 3min/30min/10s、上传 1.5MB/2000px/0.85;内置 `model_profiles` 含 `mini`(文件/终端/网页白名单 + `mini-system.txt`)与 `zero`(零工具 + `zero-system.txt`)。`config.yml` 只覆盖 deploy 相关段,其余走默认值;整份清单见 `app/config.example.yml`。
|
||||
- 读写:`readConfigText` / `writeConfigText`(先校验后写,无原子 rename,因 bind mount 上 rename 会 EBUSY);`applyEditsToText` 用 `YAML.Document` 保留注释。
|
||||
- `getConfig` 是浅引用,路由层拿到的就是运行中实例,**改完 in-memory 不影响**(要重启)。
|
||||
|
||||
@@ -474,7 +474,8 @@ AI 通过 `schedule_task` 工具为所在会话建周期性任务(`scheduled_t
|
||||
- `platforms[].keys[].models[]` —— 三层层级。内部唯一键 = `平台名/模型id`(用于历史会话回绑、用量统计),**改名等于重建其下所有模型**。
|
||||
- `model_profiles`(顶层)—— 多套可复用的 prompt+tools 组合(`{tools_allow?, system_prompt_file?}`);模型层用 `profile: <名>` 引用,不配 = 默认全量。内置 `mini`/`zero` 两套默认组合,可用同名覆盖。小模型省上下文用,详见 §5.8;运行期可前端切换(见 §5.1)。
|
||||
- `agent.max_tool_iterations`(默认 100)—— 单轮迭代上限(含工具调用),到顶自动停止并提示用户继续输入。
|
||||
- `terminal.max_output_chars`(默认 10000)—— AI 看到的命令输出上限;executor 侧另有 `EXECUTOR_MAX_OUTPUT_CHARS=200000` 兜底。
|
||||
- `terminal.max_output_chars`(默认 10000)—— AI 看到的命令输出上限;executor 侧另有 `EXECUTOR_MAX_OUTPUT_CHARS=200000` 兕底。
|
||||
- `fetch.timeout_ms`(默认 30000)/ `fetch.max_bytes`(默认 1MB)—— 网页抓取的全生命周期超时与流式抓取字节上限;返回值另有 1MB 字符 `truncate` 护栏。
|
||||
- `skills.skills_root` —— 缺省取 `workspace_root` 的同级 `skills/`。
|
||||
- `model_hold` —— 保持租约;详情见 §5.1。
|
||||
- 上下文压缩 —— 无配置项:压缩回合沿用当前 profile 的 system prompt(无 profile = 默认模板)+ 正常历史,末尾追加一条 user(固定模板 `server/templates/compress-prompt.txt` 原文),该回合不传 tools;调整压缩策略直接改该文件。
|
||||
|
||||
Reference in New Issue
Block a user