diff --git a/app/config.example.yml b/app/config.example.yml index 896bab1..4f34bc2 100644 --- a/app/config.example.yml +++ b/app/config.example.yml @@ -8,6 +8,7 @@ models: display_name: "AMD DeepSeek 1" url: "https://proxy-a.example.com/v1" # 独立的 OpenAI 兼容 /v1 地址 api_key: "sk-..." # 各模型可不同,留空则不带鉴权头 + # vision: true # 选填,默认 false;支持图片时会话上下文会告知 AI - id: ds2 display_name: "AMD DeepSeek 2" url: "https://proxy-b.example.com/v1" diff --git a/app/server/config.js b/app/server/config.js index 07f61df..45fd133 100644 --- a/app/server/config.js +++ b/app/server/config.js @@ -52,6 +52,8 @@ function loadConfig(configPath) { if (!m.display_name) m.display_name = m.id; m.url = String(m.url).replace(/\/+$/, ''); m.api_key = m.api_key || ''; + // 模型是否支持 vision(图片内容回传);默认 false,会话上下文中会告知 AI + m.vision = m.vision === true; } cfg.workspace_root = path.resolve(cfg.workspace_root); diff --git a/app/server/engine.js b/app/server/engine.js index 5212480..ae9d676 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -410,11 +410,17 @@ function sessionContextContent(session) { const model = session.model_id ? modelDisplayName(session.model_id) : '尚未绑定(排队中,绑定后自动开始处理)'; + // vision 能力随会话上下文告知 AI:不支持时读图只能拿到元信息,免得白跑一轮 read_file + const bound = session.model_id && cfg.models.find(m => m.id === session.model_id); + const visionLine = bound + ? (bound.vision ? '支持(可直接读取图片内容)' : '不支持(无法回传图片内容;读图仅返回元信息,需要图内文字请用 OCR 等工具)') + : '未知(模型绑定后生效;默认按不支持处理)'; return [ '【会话上下文】以下信息由系统自动注入,无需回复本条消息。', '- 当前日期:' + dateStr + '(UTC)', '- 任务目录:' + taskDirOf(session.id) + '(本会话的文件与终端操作都在此目录内)', '- 当前模型:' + model, + '- 图片(vision):' + visionLine, ].join('\n'); } diff --git a/app/server/routes/files.js b/app/server/routes/files.js index 4bd1183..c8b2bfe 100644 --- a/app/server/routes/files.js +++ b/app/server/routes/files.js @@ -18,8 +18,16 @@ function taskDir(req) { function sanitizeName(original) { let name = Buffer.from(original || 'file', 'latin1').toString('utf8'); - name = path.basename(name).replace(/[\/\x00-\x1f]/g, '_'); - while (name.startsWith('.')) name = name.slice(1); + name = path.basename(name).replace(/[\x00-\x1f\x7f]/g, '_'); + // 非常通字符(&?@#=%、emoji、不可见序列等)统一换成 _: + // 微信图片 URL 直接当文件名时,& ? @ 会让 shell/AI 引用路径处处踩坑。 + // 保留:各国文字/数字(\p{L}\p{N},含中文)、空格与 . - _ ( ) [ ] 、常见中文标点。 + name = name.replace(/[^\p{L}\p{N} \-_.()\[\],。、()【】]+/gu, '_'); + name = name.trim().replace(/^\.+/, ''); + // 限长(字节友好按字符数算):主体过长截断,保留扩展名 + const ext = path.extname(name); + const stem = name.slice(0, name.length - ext.length); + if (stem.length > 80) name = stem.slice(0, 80) + ext; return name || 'file'; } @@ -55,14 +63,15 @@ router.post('/:id/files', upload.array('files', 10), function (req, res) { db.prepare('INSERT INTO files (session_id, filename, size, created_at) VALUES (?,?,?,?)') .run(req.params.id, f.filename, f.size, Date.now()); } - // 主动告知 AI:文件名、大小等随上传以一条用户消息进入会话(live/held/queued 都能送达)。 - // 失败不影响上传本身。 + // 主动告知 AI:文件名、大小以单行随上传送达会话(live/held/queued 都能送达)。 + // 附任务目录绝对路径、明确无需列目录验证与确认回复,减少 AI 多余探索;通知失败不影响上传本身。 if (uploaded.length) { try { - const list = uploaded.map(f => '- ' + f.filename + '(' + f.size + ' 字节)').join('\n'); + const dir = engine.taskDirOf(req.params.id); + const list = uploaded.map(f => f.filename + '(' + f.size + ' 字节)').join(';'); engine.postUserMessage(req.params.id, - '【系统通知】用户通过页面上传了 ' + uploaded.length + ' 个文件到任务目录:\n' + list + - '\n请告知用户已收到,并按需处理这些文件。'); + '【系统通知】用户刚通过页面上传了 ' + uploaded.length + ' 个文件,已存在于任务目录 ' + dir + ' 中:' + list + + '。文件已确认存在,无需再列目录或读取验证;无需为确认收到而回复,除非用户另有诉求。'); } catch (e) { console.warn('[files] 上传通知 AI 失败:', e && e.message || e); } } res.json({ uploaded });