diff --git a/app/config.example.yml b/app/config.example.yml index e969621..59f482c 100644 --- a/app/config.example.yml +++ b/app/config.example.yml @@ -39,6 +39,9 @@ agent: temperature: 1.0 max_tool_iterations: 50 max_retries: 2 + # 回复语言:注入到 system prompt 末尾,强制模型用该语言回复用户 + # (工具名/代码/命令保持原样);留空则不限制。改后重启生效。 + language: "中文" # 大图上传预处理:图片超过 threshold_kb(默认 1.5M)时,浏览器端询问是否压缩; # 同意后在用户本地 canvas 压缩(长边 ≤ image_max_dim,JPEG image_quality)再上传。 diff --git a/app/server/config.js b/app/server/config.js index 07f86de..8e1dbf3 100644 --- a/app/server/config.js +++ b/app/server/config.js @@ -9,7 +9,7 @@ const DEFAULTS = { workspace_root: './data/tasks', db_path: '', // 默认为 workspace_root/.agent.db terminal: { default_timeout_ms: 30000, max_output_chars: 10000 }, - agent: { temperature: 1.0, max_tool_iterations: 50, max_retries: 2 }, + agent: { temperature: 1.0, max_tool_iterations: 50, max_retries: 2, language: '中文' }, // 大图上传预处理:图片文件超过 threshold_kb 时,浏览器端询问用户后用 canvas 压缩 // (长边 ≤ max_dim,JPEG quality)再上传,减轻服务器带宽与存储压力。参数经 /api/system/status 下发给前端。 upload: { image_compress_threshold_kb: 1536, image_max_dim: 2000, image_quality: 0.85 }, diff --git a/app/server/engine.js b/app/server/engine.js index 6d6fcd8..ebaf461 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -415,7 +415,7 @@ async function runLoopInner(sessionId, run) { if (!session || session.status !== 'active' || !session.model_id) break; session.history = loadHistory(sessionId); for (const m of session.history) if (m.id > lastIncludedId) lastIncludedId = m.id; - const messages = buildMessages(session); + const messages = buildMessages(session, (cfg.agent || {}).language); // 内置工具 + 启动时发现的 MCP 工具,一起随 system prompt 发给模型 const tools = TOOL_DEFINITIONS.concat(mcp.toolDefinitions()); diff --git a/app/server/llm.js b/app/server/llm.js index 78de834..5eed3ac 100644 --- a/app/server/llm.js +++ b/app/server/llm.js @@ -12,6 +12,18 @@ function buildSystemPrompt() { return SYSTEM_PROMPT_TEMPLATE; } +// 部署级回复语言指令:拼在 system prompt 末尾(尾部对弱模型约束力最强)。 +// 同一进程内恒定,不破坏前缀缓存;改配置需重启生效。 +function systemPromptText(language) { + const t = String(language || '').trim(); + if (!t) return SYSTEM_PROMPT_TEMPLATE; + return SYSTEM_PROMPT_TEMPLATE + + '\n\n## Reply Language\n' + + '- ALWAYS respond to the user in ' + t + '. Tool descriptions, tool outputs and errors are usually in English — ' + + 'never let them switch your reply language. Tool names, parameters, code, file paths and shell commands stay as-is; ' + + 'every sentence addressed to the user MUST be in ' + t + '.'; +} + // 多模态内容块存取:messages.content 列为 TEXT,含图片的 tool 消息 // 以 {"__blocks__":[content...]} 哨兵 JSON 落库(见需求 §5), // 发请求时还原为 OpenAI 内容块数组(text + image_url)。tool/user 消息共用。 @@ -28,8 +40,8 @@ function decodeBlocks(content) { // 省 token:image_url 块仅在其位于最后一条 assistant 消息之后(模型尚未对它回复)时保留; // 之前的视为已消费,发请求时替换为文字标记。DB 始终存全量:UI 历史照常渲染图片, // 模型如需重看可再次 read_file(新消息在尾部,会重新携带图片)。 -function buildMessages(session) { - const messages = [{ role: 'system', content: SYSTEM_PROMPT_TEMPLATE }]; +function buildMessages(session, language) { + const messages = [{ role: 'system', content: systemPromptText(language) }]; const history = session.history; let lastAssistant = -1; for (let i = history.length - 1; i >= 0; i--) { @@ -161,4 +173,4 @@ async function callModel(cfg, modelId, messages, tools, options) { return { content, tool_calls: Array.from(toolCalls.values()), finish_reason: finishReason, usage, aborted }; } -module.exports = { callModel, buildMessages, buildSystemPrompt, encodeBlocks, decodeBlocks }; +module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, encodeBlocks, decodeBlocks }; diff --git a/app/server/public/index.html b/app/server/public/index.html index 9945ee0..5804c86 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -301,10 +301,10 @@ - - - - - + + + + + diff --git a/app/server/public/js/settings.js b/app/server/public/js/settings.js index 92d8a20..1eb7782 100644 --- a/app/server/public/js/settings.js +++ b/app/server/public/js/settings.js @@ -37,6 +37,7 @@ const CFG_FORM = [ { path: 'agent.temperature', label: '温度 temperature', type: 'float' }, { path: 'agent.max_tool_iterations', label: '单会话最大工具轮次', type: 'int' }, { path: 'agent.max_retries', label: '上游失败重试次数', type: 'int' }, + { path: 'agent.language', label: '回复语言(留空不限)', type: 'text' }, ] }, { group: '终端工具', items: [ { path: 'terminal.default_timeout_ms', label: '命令默认超时(ms)', type: 'int' }, @@ -120,6 +121,12 @@ function svcCard(st) { function cfgInput(it) { const cur = getByPath(effCfg, it.path); const v = cur == null ? '' : String(cur); + if (it.type === 'text') { + return '
' + + '
'; + } const step = it.type === 'float' ? ' step="any"' : ' step="1"'; return '
' + '