feat(prompt): 环境段按部署形态二选一,修正本地模式的环境事实

This commit is contained in:
2026-11-11 16:08:42 +08:00
parent c0488938ff
commit 01757d4f4b
6 changed files with 58 additions and 26 deletions
+6 -3
View File
@@ -62,7 +62,7 @@ AGENT_CONFIG=./config.yml node server/server.js
| `server/log.js` | 分级日志 `log.debug/info/warn/error(category, ...)` |
| `server/balance.js` | 平台余额查询适配器(按 `platform.type` 分发) |
| `server/version.js` | git commit/date:优先读 `version.json`(post-commit 钩子生成),宿主机有 git 时兜底 `git rev-parse`;页面版本号另外由 `routes/system.js` 的 `VERSION` 字面量决定 |
| `server/templates/` | system prompt 模板、压缩/标题指令、profile prompt |
| `server/templates/` | system prompt 模板、环境段(按部署形态二选一)、压缩/标题指令、profile prompt |
## 一次用户消息的生命周期
@@ -162,7 +162,7 @@ IM 用户在实时对话,回合中插话应当“当前这步跑完就插”
`buildMessages(session, language, mintUrl, profile)` 的输出顺序固定:
1. `system`:模板(`profile-default.txt` 或 profile 自定义 prompt)+ 技能清单段 + 语言指令;
1. `system`:模板(`profile-default.txt` 或 profile 自定义 prompt)+ 环境段 + 技能清单段 + 语言指令;
进程内按 `(profile, language)` 缓存,保证前缀缓存稳定。
2. 画像快照(`sessions.profile_snapshot`,建会话/清空时冻结):作为首条 `user`。**不在 messages 表里**,
压缩与归档碰不到它。
@@ -386,7 +386,10 @@ preview-proxy.mount(末尾兜底)
**改 prompt / profile**
- 默认 prompt:`templates/profile-default.txt`(`{APP_SKILLS_SECTION}` 占位技能清单)。
- 默认 prompt:`templates/profile-default.txt`(`{APP_SKILLS_SECTION}` 占位技能清单,
`{APP_ENV_SECTION}` 占位环境段)。
- 环境段:`templates/env-server.txt`(executor 容器形态)/ `env-local.txt`(本机 spawn 形态),
启动时按 `EXECUTOR_URL` 有无选定(`server.js` 的 `llm.setEnvMode`),改完重启。
- 压缩/标题指令:`templates/prompt-compress.txt` / `prompt-title.txt`。
- 自定义组合:`config.yml` 的 `model_profiles` + 模型的 `profile: <名>`;改完重启。
+24 -7
View File
@@ -58,6 +58,26 @@ function setProfilePrompts(map) {
// system prompt 每次请求都随 messages 发出,拼接结果按 (profile, 语言) 缓存,不重复分配大字符串。
let _promptTextCache = new Map(); // 'profile\u0000lang' -> text
// 部署形态决定的环境段:模板里的 {APP_ENV_SECTION} 由两份小模板填充(mode 由 server.js 启动时选定)。
// 为什么不能写成一段「两边都成立」的话:服务器形态是容器(每会话隔离、会话级 /tmp、只读 skills),
// 本地形态是用户自己那台机器(无沙箱、真实共享 /tmp)——两边的环境事实互相矛盾,混着写会误导模型。
// 只选一次、进程内恒定,和技能段同属「前缀缓存不得抖动」的部分。
const APP_ENV_TOKEN = '{APP_ENV_SECTION}';
const ENV_TEXTS = {
server: fs.readFileSync(path.join(__dirname, 'templates', 'env-server.txt'), 'utf8').trim(),
local: fs.readFileSync(path.join(__dirname, 'templates', 'env-local.txt'), 'utf8').trim(),
};
let ENV_TEXT = ENV_TEXTS.server;
function setEnvMode(mode) {
ENV_TEXT = ENV_TEXTS[mode] || ENV_TEXTS.server;
_promptTextCache = new Map(); // 环境段换了,拼接结果必须重建
}
// 自定义 profile prompt 里写了占位符也照样生效(没写就是没有环境段,同技能段规则)。
function withEnvSection(text) {
return text.indexOf(APP_ENV_TOKEN) >= 0 ? text.split(APP_ENV_TOKEN).join(ENV_TEXT) : text;
}
// “可用技能”段落(skills.formatSkillListForPrompt):启动时快照(server.js)与技能变更后
//(routes/skills.js)经 setSkillListText 更新。插入位置由模板内的 {APP_SKILLS_SECTION} 占位符决定
//(拼在技能规则旁边,模型一眼看到清单,不再盲猜尾部多出来的孤儿段落):
@@ -85,12 +105,9 @@ function systemPromptText(language, profile) {
const key = (profile || 'default') + '\u0000' + t;
const hit = _promptTextCache.get(key);
if (hit) return hit;
let text;
if (profile && PROFILE_PROMPTS[profile]) {
text = withSkillList(PROFILE_PROMPTS[profile], false); // 自定义文件:没放占位符就真没有技能段
} else {
text = withSkillList(SYSTEM_PROMPT_TEMPLATE, true);
}
// 环境段先替(模板里的占位符),再拼技能段:自定义 prompt 没放占位符就真没有环境段/技能段
const custom = profile && PROFILE_PROMPTS[profile];
let text = withSkillList(withEnvSection(custom || SYSTEM_PROMPT_TEMPLATE), !custom);
if (t) text += '\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; ' +
@@ -795,4 +812,4 @@ async function callModel(cfg, modelId, messages, tools, options) {
return { content, reasoning, tool_calls: Array.from(toolCalls.values()), finish_reason: finishReason, usage, aborted, sseFrames };
}
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, setSkillListText, setPayloadTrim, setProfilePrompts, encodeBlocks, decodeBlocks, compressMessages, titleMessages, patchImageReceipt };
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, setSkillListText, setEnvMode, setPayloadTrim, setProfilePrompts, encodeBlocks, decodeBlocks, compressMessages, titleMessages, patchImageReceipt };
+5
View File
@@ -19,6 +19,11 @@ const log = require('./log');
const cfg = loadConfig(process.env.AGENT_CONFIG);
log.setLevel(cfg.log_level); // 分级日志开关(log.js;config.yml log_level,重启生效)
llm.setPayloadTrim(cfg.agent); // 历史重放的重负载占位裁剪规则(llm.buildMessages;config agent.trim.rules)
// system prompt 的「环境段」:executor.url 为空 = 命令在本机 spawn(本地桌面形态,无沙箱),
// 有值 = 送进 agent-executor 容器(每会话隔离)。用这个信号而不是另立开关,因为它就是
// 隔离与否的成因(resolveInTask 的 /tmp 重写、WIDE_SCOPE 同理)。启动日志跟一行,方便核对。
llm.setEnvMode(cfg.executor.url ? 'server' : 'local');
log.info('startup', '环境形态:' + (cfg.executor.url ? 'server(executor 容器隔离)' : 'local(本机 spawn,无沙箱)'));
// 启动时把生效规则打出来:工具名拼错不会报错(MCP 工具要等发现后才存在),只能靠这行核对。
log.info('startup', '历史重放占位裁剪:' + (((cfg.agent && cfg.agent.trim && cfg.agent.trim.rules) || [])
.map(r => r.tool + '/' + r.side + '(轮≥' + r.rounds + ',回合≥' + r.turns + ')').join(', ') || '关闭'));
+6
View File
@@ -0,0 +1,6 @@
- You run on the user's own machine, not in a disposable sandbox: `terminal` is a
real shell with their permissions, other conversations share the same machine,
and `/tmp` is the system temp directory (shared, never cleaned for you). Work
inside the working directory; treat everything outside it as the user's.
- `python3` may be missing; when the skills list below is empty it is not
available. Never install runtimes or system packages yourself.
+16
View File
@@ -0,0 +1,16 @@
- Linux container, isolated per conversation: you see the working directory, a
session-scoped `/tmp`, read-only `/workspace/skills`, and system paths — never
other sessions.
- `terminal` runs POSIX sh (`/bin/sh` is dash); use `bash` for scripts.
- `/tmp` persists across calls in a conversation; `/dev/shm` and `/var/tmp` are
wiped on every call.
- Python 3.11; system packages are read-only. Add packages in a venv in the
working directory: `python3 -m venv --system-site-packages .venv`, then
`.venv/bin/pip` / `.venv/bin/python`. Don't reinstall packages that are
already importable.
- System packages can't be installed at runtime (`apt-get` fails). A missing CLI
tool must be added on the server — tell the user.
- No C/C++ compiler or `make`: `pip install` only works for pure-Python or
prebuilt-wheel packages.
- Chinese text in matplotlib: set `font.sans-serif` to `Noto Sans CJK SC`. For
ZIPs with GBK filenames, use `unar` or `7z x` to avoid garbled names.
+1 -16
View File
@@ -56,19 +56,4 @@ domain must never be hand-rolled.
## Environment
- Linux container, isolated per conversation: you see the working directory, a
session-scoped `/tmp`, read-only `/workspace/skills`, and system paths — never
other sessions.
- `terminal` runs POSIX sh (`/bin/sh` is dash); use `bash` for scripts.
- `/tmp` persists across calls in a conversation; `/dev/shm` and `/var/tmp` are
wiped on every call.
- Python 3.11; system packages are read-only. Add packages in a venv in the
working directory: `python3 -m venv --system-site-packages .venv`, then
`.venv/bin/pip` / `.venv/bin/python`. Don't reinstall packages that are
already importable.
- System packages can't be installed at runtime (`apt-get` fails). A missing CLI
tool must be added on the server — tell the user.
- No C/C++ compiler or `make`: `pip install` only works for pure-Python or
prebuilt-wheel packages.
- Chinese text in matplotlib: set `font.sans-serif` to `Noto Sans CJK SC`. For
ZIPs with GBK filenames, use `unar` or `7z x` to avoid garbled names.
{APP_ENV_SECTION}