diff --git a/app/server/engine.js b/app/server/engine.js index cb7ec78..7b9a68e 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -516,11 +516,16 @@ async function runLoopInner(sessionId, run) { // 且不传 tools,模型只能输出摘要文本。 // 发送前为历史里的 image_ref 块现签公网外链(GET /p/,同 mint_public_link): // 每次请求重签,回合内多轮迭代/重试永不过期;DB 只存几十字符的 rel,base64 不进库不进请求体。 - // base_url 未配/mint 异常时返回 null,buildMessages 降级为文字占位。 + // base_url 未配/校验不过/mint 异常时返回 null,buildMessages 降级为文字占位。 + // image_rel 可能是压缩无收益时直指的原文件:与 mint_public_link 同款 realpath 复核, + // 防任务目录里指向外部的符号链接图片被签上公网。 const mintImageUrl = (rel) => { const pl = cfg.public_link || {}; if (!pl.base_url) return null; try { + const dir = taskDirOf(sessionId); + const real = fs.realpathSync(resolveInTask(dir, rel)); + if (real !== dir && !real.startsWith(dir + path.sep)) return null; return pl.base_url + '/p/' + publicFiles.mint({ sessionId: sessionId, rel, ttlMs: pl.ttl_ms }); } catch (_) { return null; } }; diff --git a/app/server/tools/executor.js b/app/server/tools/executor.js index d3a91da..3c6d043 100644 --- a/app/server/tools/executor.js +++ b/app/server/tools/executor.js @@ -142,6 +142,7 @@ function outlineWrapper(outlineText) { // 用 sharp 压缩(按 EXIF 摆正、长边 ≤2000px、JPEG 质量 80)并落盘 .imgcache/。 // 成功返回 {text, image_rel};产物文件名带源文件指纹(mtime+size),同图重复读复用同一份不增殖。 +// 无收益守卫(与浏览器端压缩同款):重编码 JPEG 反而 ≥ 原图时不落盘,image_rel 直指原文件。 // sharp 缺失或解码失败时退回纯文本提示,不让整个工具回合报错。 async function readImageAsBlocks(ctx, abs, rel, size) { let sharp; @@ -153,6 +154,13 @@ async function readImageAsBlocks(ctx, abs, rel, size) { .resize({ width: IMG_MAX_SIDE, height: IMG_MAX_SIDE, fit: 'inside', withoutEnlargement: true }) .jpeg({ quality: IMG_JPEG_QUALITY }) .toBuffer({ resolveWithObject: true }); + if (r.data.length >= size) { + // 低内容熵图(截图/线稿/纯色)JPEG 重编码可能比原图还大:不产出缓存,外链直接指向原文件 + return { + text: '[图片文件] ' + rel + '(' + size + ' 字节,压缩无收益,按原图回传)', + image_rel: rel, + }; + } let st = null; try { st = await fsp.stat(abs); } catch (_) {} const stem = path.basename(rel).replace(/\.[^.]+$/, '').replace(/[^\w.\-\u4e00-\u9fff]/g, '_').slice(0, 60); diff --git a/app/test/smoke.js b/app/test/smoke.js index e8c04d5..92acf20 100644 --- a/app/test/smoke.js +++ b/app/test/smoke.js @@ -130,8 +130,8 @@ async function main() { .map(m => { try { return JSON.parse(m.content); } catch (_) { return null; } }) .find(j => j && Array.isArray(j.__blocks__)); ok(!!imgTool && imgTool.__blocks__.some(b => - b.type === 'image_ref' && String(b.rel).startsWith('.imgcache/')), - 'tool 消息含压缩图的 image_ref 内容块(外链发送时现签,库里不存 base64)', + b.type === 'image_ref' && String(b.rel || '').length > 0), + 'tool 消息含 image_ref 内容块(压缩产物或无收益直指原图;外链发送时现签,库里不存 base64)', imgTool ? JSON.stringify(imgTool.__blocks__.map(b => b.type)) : '无多模态块'); const cacheInPanel = await api('GET', '/api/sessions/' + si.id + '/files'); ok(!cacheInPanel.some(f => String(f.filename).startsWith('.imgcache/')), '.imgcache 不进前端文件列表'); @@ -229,16 +229,16 @@ async function main() { ok(fpath.includes('g.txt'), 'find_path 命中', fpath); const esc = await executeTool(ctx, 'read_file', { path: '../outside.txt' }).catch(e => 'ERR:' + e.message); ok(String(esc).includes('越界'), 'read_file 拒绝越界', String(esc)); - // read_file 读图:非 vision → 文本提示;vision → {text, image_rel}(依赖 sharp,产物落 .imgcache/) + // read_file 读图:非 vision → 文本提示;vision → {text, image_rel}(依赖 sharp;小图压缩无收益时 image_rel 直指原文件,不落 .imgcache) const unitPng = await require('sharp')({ create: { width: 4, height: 4, channels: 3, background: { r: 10, g: 120, b: 200 } } }).png().toBuffer(); fs.writeFileSync(path.join(ctx.taskDir, 'pic.png'), unitPng); const imgNoVision = await executeTool(ctx, 'read_file', { path: 'pic.png' }); ok(typeof imgNoVision === 'string' && imgNoVision.includes('不支持视觉识别'), 'read_file 非 vision 图片返回文本提示', String(imgNoVision).slice(0, 80)); const ctxVision = Object.assign({}, ctx, { vision: true }); const imgVision = await executeTool(ctxVision, 'read_file', { path: 'pic.png' }); - ok(imgVision && typeof imgVision === 'object' && String(imgVision.image_rel).startsWith('.imgcache/'), - 'read_file vision 图片返回压缩产物 image_rel', imgVision && imgVision.text ? String(imgVision.text).slice(0, 90) : String(imgVision).slice(0, 90)); - ok(fs.existsSync(path.join(ctx.taskDir, imgVision.image_rel)), '压缩产物已落盘 .imgcache/'); + ok(imgVision && typeof imgVision === 'object' && String(imgVision.image_rel).length > 0, + 'read_file vision 图片返回 image_rel(压缩产物或无收益原图)', imgVision && imgVision.text ? String(imgVision.text).slice(0, 90) : String(imgVision).slice(0, 90)); + ok(fs.existsSync(path.join(ctx.taskDir, imgVision.image_rel)), 'image_rel 指向的文件存在'); // ---- 8. 配置读写 API(阶段 2)---- console.log('\n[8] 配置读写'); diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 4034e95..45c5367 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -283,7 +283,7 @@ stateDiagram-v2 | 工具 | 实现要点 | |---|---| -| `read_file` | 走 `resolveInTask` 防路径穿越;图像(PNG/JPEG/WebP/GIF/BMP/TIFF)按 vision 能力走 `sharp` 压缩后落盘 `.imgcache/` 并回传 `image_rel`(长边 ≤ 2000px,JPEG q=80;文件名带源文件 mtime+size 指纹,重读复用不增殖),发送前由 `llm.buildMessages` 现签成公网外链;大文本/二进制给 outline;行号前缀格式 "`<6位右对齐行号>\\t<原文>`"(`edit_file` 用) | +| `read_file` | 走 `resolveInTask` 防路径穿越;图像(PNG/JPEG/WebP/GIF/BMP/TIFF)按 vision 能力走 `sharp` 压缩后落盘 `.imgcache/` 并回传 `image_rel`(长边 ≤ 2000px,JPEG q=80;文件名带源文件 mtime+size 指纹,重读复用不增殖;重编码无收益时不落盘、直指原文件),发送前由 `llm.buildMessages` 现签成公网外链(engine 侧 mint 前 realpath 复核);大文本/二进制给 outline;行号前缀格式 "`<6位右对齐行号>\\t<原文>`"(`edit_file` 用) | | `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` | 标准文件操作,全部 `resolveInTask` 约束;`grep` 走 `ripgrep`(`rg`)优先 | | `terminal` | **唯一一个走 executor 的内置工具**(当 `rexec.isRemote(cfg)`);本地分支用 `spawn` + `detached` + 进程组 kill;远端分支 `rexec.run` SSE 收流,结果套上超时/截断/取消注解后 `truncate(text, cfg.terminal.max_output_chars)` 返回 |