refactor(templates): 提示词按用途分组命名 + 压缩回合剔除 [CTX] 注入行
- 命名混乱整治:prompt-*(压缩/标题系统侧指令)与 profile-*(各 profile 的 system prompt)两组:system-prompt.txt→profile-default.txt、 advising/lab/zero-system.txt→profile-*.txt、compress/title-prompt.txt→ prompt-*.txt;全库引用(llm/config/engine/README×2/example/ARCHITECTURE)同步 - compressMessages 输入过滤:memory_note/scheduler_note/errAck/旧 summarize_request 不进摘要(跨会话噪音+滚雪球,同 engine JOURNAL_SKIP_TYPES 虑);channel_note 刻意 保留——压缩落库存档全部行,微信约束只能靠摘要转世;task_brief/summary 照常 - 画像快照本就不在 messages 表,压缩/标题均天然不带
This commit is contained in:
+2
-2
@@ -21,7 +21,7 @@ app/
|
||||
│ ├── tools/
|
||||
│ │ ├── definitions.json # 内置 16 个工具的 JSON Schema
|
||||
│ │ └── executor.js # 内置工具执行器(任务目录沙箱;未知工具名转 MCP)
|
||||
│ ├── templates/system-prompt.txt # System prompt 模板(直接编辑)
|
||||
│ ├── templates/profile-default.txt # System prompt 模板(直接编辑)
|
||||
│ └── public/ # H5 前端(Vanilla JS + SSE;无构建,index.html + js/ 六模块按序加载)
|
||||
├── test/ # mock 上游 + 端到端冒烟测试
|
||||
├── config.example.yml # core 专属配置模板(部署时生成/复制为仓库根 config.yml 挂进容器)
|
||||
@@ -46,7 +46,7 @@ npm test # 启动 mock 上游 + 真实 server 的端到端冒烟测
|
||||
|
||||
## 提示词与工具 schema
|
||||
|
||||
System prompt 完全静态(所有会话共用,利于上游前缀缓存),在 `server/templates/system-prompt.txt` 直接编辑;工具定义在 `server/tools/definitions.json`。改完重启服务。
|
||||
System prompt 完全静态(所有会话共用,利于上游前缀缓存),在 `server/templates/profile-default.txt` 直接编辑;工具定义在 `server/tools/definitions.json`。改完重启服务。
|
||||
|
||||
## 关键行为速查
|
||||
|
||||
|
||||
@@ -17,14 +17,14 @@ model_profiles:
|
||||
advising: # 顾问阶段:只聊和读不写(profile 画像元信息除外),唯一出口 handoff,配专属提示词
|
||||
tools_allow: [read_file, fetch, handoff, profile]
|
||||
tools_deny: [] # 必须显式清空全局黑名单,否则 handoff 自己也被 deny(同 lab 的教训)
|
||||
system_prompt_file: advising-system.txt
|
||||
system_prompt_file: profile-advising.txt
|
||||
lab: # 统一入口自测:tools 只有 lab,其余能力经 lab(help) 现场发现
|
||||
tools_allow: [lab]
|
||||
tools_deny: [] # 必须显式清空全局黑名单,否则 lab 自己也被 deny → 零工具
|
||||
system_prompt_file: lab-system.txt # 相对 server/templates/(也接受绝对路径)
|
||||
system_prompt_file: profile-lab.txt # 相对 server/templates/(也接受绝对路径)
|
||||
zero: # 纯文本对话:一个工具都不发
|
||||
tools_allow: []
|
||||
system_prompt_file: zero-system.txt
|
||||
system_prompt_file: profile-zero.txt
|
||||
|
||||
port: 8090 # 服务监听端口
|
||||
workspace_root: "/workspace/tasks" # 任务工作区根目录,AI 文件操作限制在此
|
||||
|
||||
@@ -121,12 +121,12 @@ const DEFAULTS = {
|
||||
// 与 recall(日记只读翻阅;用户和顾问聊「上次那个事」恰是回忆查询的高峰场景);
|
||||
// 唯一出口是 handoff;tools_deny 必须显式清空:不配 = 继承全局 ['lab','handoff'],
|
||||
// allow 滤完再叠 deny 会把 handoff 自己禁掉(同 lab 组合的教训)。
|
||||
advising: { tools_allow: ['read_file', 'list_directory', 'fetch', 'handoff', 'profile', 'recall'], tools_deny: [], system_prompt_file: 'advising-system.txt' },
|
||||
advising: { tools_allow: ['read_file', 'list_directory', 'fetch', 'handoff', 'profile', 'recall'], tools_deny: [], system_prompt_file: 'profile-advising.txt' },
|
||||
// lab:统一入口元工具自测组合——tools 收缩到 1 个,其余能力经 lab(help) 现场发现。
|
||||
// tools_deny 必须显式清空:不配 = 继承全局 ['lab'],deny 后叠加会把 lab 自己禁掉 → 零工具。
|
||||
lab: { tools_allow: ['lab'], tools_deny: [], system_prompt_file: 'lab-system.txt' },
|
||||
lab: { tools_allow: ['lab'], tools_deny: [], system_prompt_file: 'profile-lab.txt' },
|
||||
// 纯文本对话:一个工具都不发。
|
||||
zero: { tools_allow: [], system_prompt_file: 'zero-system.txt' },
|
||||
zero: { tools_allow: [], system_prompt_file: 'profile-zero.txt' },
|
||||
},
|
||||
// 登录认证:{users:[{user,password}], secret?, ttl_ms?};null / users 空 = 不启用。
|
||||
auth: null,
|
||||
|
||||
@@ -1154,7 +1154,7 @@ function deleteSession(sessionId, deleteDir) {
|
||||
|
||||
// 上下文压缩:phase-bar「压缩」按钮触发。不再往会话里插入指令消息:
|
||||
// 压缩回合沿用默认 system prompt + 正常历史消息,末尾追加一条 user
|
||||
//(templates/compress-prompt.txt 原文,见 llm.compressMessages),且不传 tools
|
||||
//(templates/prompt-compress.txt 原文,见 llm.compressMessages),且不传 tools
|
||||
//(见 runLoopInner 的 compressing 分支),模型直接基于历史输出摘要。
|
||||
// 识别标记 = run.summarizing(仅在内存 run 上,不入库):runLoopInner 据此换 prompt/去 tools,
|
||||
// 并在压缩回合的收尾答复 emit 时带 is_summary: true,前端据此把气泡装进摘要卡片并淡出存档旧历史。
|
||||
|
||||
+14
-8
@@ -3,18 +3,18 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const SYSTEM_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'system-prompt.txt'), 'utf8');
|
||||
const SYSTEM_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'profile-default.txt'), 'utf8');
|
||||
|
||||
// 上下文压缩提示词(templates/compress-prompt.txt):点「压缩」时不插入指令消息。
|
||||
// 上下文压缩提示词(templates/prompt-compress.txt):点「压缩」时不插入指令消息。
|
||||
// 压缩回合(见 compressMessages)沿用默认 system prompt + 正常历史消息,
|
||||
// 末尾追加一条 user(本模板原文,参考 titleMessages 的拼法),且该回合不传 tools。
|
||||
// 需要调整压缩策略时直接改这个模板文件。
|
||||
const COMPRESS_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'compress-prompt.txt'), 'utf8');
|
||||
const COMPRESS_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'prompt-compress.txt'), 'utf8');
|
||||
|
||||
// 自动生成会话标题提示词(templates/title-prompt.txt):会话建立后第 1 次交互完成时触发。
|
||||
// 自动生成会话标题提示词(templates/prompt-title.txt):会话建立后第 1 次交互完成时触发。
|
||||
// 与压缩回合不同,这里**不带 system prompt**:直接复用历史里的 user/assistant 文本消息,
|
||||
// 末尾追加一条 user(本模板,{{language}} 占位符替换为配置语言),见 titleMessages。
|
||||
const TITLE_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'title-prompt.txt'), 'utf8');
|
||||
const TITLE_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'prompt-title.txt'), 'utf8');
|
||||
|
||||
// 拼标题回合的 messages:历史原样(只留 user/assistant 纯文本行)+ 末尾一条标题指令 user。
|
||||
// 与 buildMessages 同取舍:archived 行不进;__blocks__ 哨兵 JSON 还原为文字,图片以 [图片] 占位;
|
||||
@@ -365,14 +365,20 @@ function trimToolContent(content, row, callById, roundsAfter) {
|
||||
|
||||
// 拼压缩回合的 messages(参考 titleMessages 的拼法):默认 system prompt + 正常历史
|
||||
//(与 buildMessages 同取舍:archived 不发、旧摘要/工具轮照常),末尾追加一条 user
|
||||
//(compress-prompt.txt 原文,历史就在它前面的 messages 里,不再填占位符)。
|
||||
//(prompt-compress.txt 原文,历史就在它前面的 messages 里,不再填占位符)。
|
||||
// 调用方(engine.runLoopInner 的 compressing 分支)需同时不传 tools。
|
||||
// 不注入画像快照(withProfile=false):画像是每轮瞬态注入的,压缩后照常带上,
|
||||
// 无需烧进摘要;一旦模型把画像转述进摘要,摘要会以 type='summary' 落库,
|
||||
// 再经 titleMessages(只排 archived)混进标题生成的输入里。
|
||||
// 再经 titleMessages(summary 在白名单内)混进标题生成的输入里。
|
||||
// 不进压缩的注入行:跨会话/系统噪音。memory_note 是近几天开场日记——当时事实已陈旧,喂给摘要
|
||||
// 会被当本会话内容、且每轮压缩滚雪球(engine 的 JOURNAL_SKIP_TYPES 同虑);scheduler_note/
|
||||
// errAck/旧 summarize_request 同理非正文。channel_note 刻意保留:压缩落库会存档全部行,
|
||||
// 微信约束只能靠摘要转世;task_brief/summary 是会话本体,照常进。
|
||||
const COMPRESS_SKIP_TYPES = new Set(['memory_note', 'scheduler_note', 'errAck', 'summarize_request']);
|
||||
function compressMessages(session, language, mintUrl, profile) {
|
||||
const history = (session.history || []).filter(r => !COMPRESS_SKIP_TYPES.has(r.type));
|
||||
// trim 显式关:摘要需要 fetch/read 的原料原文,且压缩是一次性成本,省它得不偿失。
|
||||
const messages = buildMessages(session, language, mintUrl, profile, false, false);
|
||||
const messages = buildMessages({ ...session, history }, language, mintUrl, profile, false, false);
|
||||
messages.push({ role: 'user', content: COMPRESS_PROMPT_TEMPLATE });
|
||||
return messages;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user