refactor(engine): 全面移除 advising 顾问模式与 handoff 工具——会话默认走模型配置 profile(未配=全量 profile-default.txt)
- engine: resolveProfileName 去掉 advising 强制档;删 enterExecuting/archiveTaskBrief 与 runLoopInner 的阶段守卫、批尾切换逻辑 - tools: definitions.json/executor.js 删 handoff 工具(内置工具 15→14);全局 tools_deny 回退为 ['lab'] - db: sessions.phase 列随概念移除(DROP COLUMN 迁移,旧 SQLite 兜底跳过);createSession/clearSessionMessages 不再读写 phase - 模板 profile-advising.txt 删除;config.js/config.example.yml 去 advising 组合 - scheduler/wechat 投递前不再有强制 executing;SSE status 事件与会话接口不再携带 phase - 前端去阶段徽标/phase 事件监听;README 与 docs/ARCHITECTURE.md 同步
This commit is contained in:
@@ -555,7 +555,6 @@ CREATE TABLE sessions (
|
||||
id TEXT PRIMARY KEY, -- 任务 ID,如 "task-20260830-001"
|
||||
title TEXT DEFAULT '', -- 会话标题(可由 AI 生成或用户填写)
|
||||
status TEXT NOT NULL, -- queued | active | held | completed | cancelled
|
||||
phase TEXT NOT NULL DEFAULT 'planning', -- planning | execution
|
||||
model_id TEXT, -- 当前绑定的模型 ID(ds1/ds2/ds3),释放/排队时为空
|
||||
last_model_id TEXT, -- 最近用过的模型;释放后新消息优先回绑,被占才另选空闲
|
||||
created_at INTEGER NOT NULL,
|
||||
|
||||
@@ -8,16 +8,11 @@
|
||||
platforms: []
|
||||
|
||||
# 全局工具黑名单:profile 未配 tools_deny 时继承它;allow 先、deny 后叠加(deny 赢,与 mcp_servers 同构)。
|
||||
# 写数组即整体覆盖默认值,写 [] 即清空。默认屏蔽元工具 lab 与阶段出口 handoff(后者仅 advising 显式清空后可见)。
|
||||
tools_deny: [lab, handoff]
|
||||
# 写数组即整体覆盖默认值,写 [] 即清空。默认屏蔽元工具 lab。
|
||||
tools_deny: [lab]
|
||||
|
||||
# 模型 profile:prompt + tools 名单组合,模型用 profile: <名> 引用(不配 = 全量)。
|
||||
# 内置 advising 不靠引用:会话处于 advising 阶段(顾问澄清需求/规划)时强制命中,见 server/engine.js resolveProfileName。
|
||||
# 模型 profile:prompt + tools 名单组合,模型用 profile: <名> 引用(不配 = 默认全量,profile-default.txt)。
|
||||
model_profiles:
|
||||
advising: # 顾问阶段:只聊和读不写(profile 画像元信息除外),唯一出口 handoff,配专属提示词
|
||||
tools_allow: [read_file, fetch, handoff, profile]
|
||||
tools_deny: [] # 必须显式清空全局黑名单,否则 handoff 自己也被 deny(同 lab 的教训)
|
||||
system_prompt_file: profile-advising.txt
|
||||
lab: # 统一入口自测:tools 只有 lab,其余能力经 lab(help) 现场发现
|
||||
tools_allow: [lab]
|
||||
tools_deny: [] # 必须显式清空全局黑名单,否则 lab 自己也被 deny → 零工具
|
||||
|
||||
+2
-11
@@ -109,19 +109,10 @@ const DEFAULTS = {
|
||||
// 模型平台池(无默认值,结构见 config.example.yml)。
|
||||
platforms: [],
|
||||
// 全局工具黑名单:profile 未配 tools_deny 时继承它;allow 先、deny 后叠加(deny 赢,与 mcp_servers 同构)。
|
||||
// 默认屏蔽元工具 lab 与阶段出口 handoff:它们只该出现在显式清空 deny 的 profile 里,
|
||||
// 不进任何常规请求的 tools 数组(lab → lab 组合;handoff → advising 组合)。
|
||||
tools_deny: ['lab', 'handoff'],
|
||||
// 默认屏蔽元工具 lab:它只该出现在显式清空 deny 的 profile 里,不进常规请求的 tools 数组(lab → lab 组合)。
|
||||
tools_deny: ['lab'],
|
||||
// 模型 profile(prompt + tools 名单组合):platforms 里的模型用 profile: <名> 引用。
|
||||
model_profiles: {
|
||||
// advising:顾问阶段(sessions.phase)专用组合——不由模型配置引用,而是 resolveProfileName 在
|
||||
// advising 时强制命中:顾问只聊和读、不写(write_file/edit_file/copy_path 不在名单,批内幻觉调用另由引擎挡下;
|
||||
// list_directory 入选——翻阅历史目录是顾问高频刚需),
|
||||
// 例外是 profile(画像元信息,不碰任务目录;顾问问答恰是收集称谓/偏好密度最高的时刻)
|
||||
// 与 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: 'profile-advising.txt' },
|
||||
// lab:统一入口元工具自测组合——tools 收缩到 1 个,其余能力经 lab(help) 现场发现。
|
||||
// tools_deny 必须显式清空:不配 = 继承全局 ['lab'],deny 后叠加会把 lab 自己禁掉 → 零工具。
|
||||
lab: { tools_allow: ['lab'], tools_deny: [], system_prompt_file: 'profile-lab.txt' },
|
||||
|
||||
+8
-10
@@ -14,7 +14,6 @@ function initDb(dbPath) {
|
||||
id TEXT PRIMARY KEY,
|
||||
title TEXT DEFAULT '',
|
||||
status TEXT NOT NULL,
|
||||
phase TEXT NOT NULL DEFAULT 'advising',
|
||||
model_id TEXT,
|
||||
last_model_id TEXT,
|
||||
assistant_type TEXT DEFAULT 'secretary',
|
||||
@@ -218,17 +217,17 @@ function initDb(dbPath) {
|
||||
if (!cols.some(c => c.name === 'user')) {
|
||||
db.exec("ALTER TABLE sessions ADD COLUMN user TEXT DEFAULT ''");
|
||||
}
|
||||
// 迁移:profile 曾记会话绑定的 prompt+tools 组合名(bind 弹窗「模式」区)。已废弃:
|
||||
// 前端手动切换模式全部移除,profile 选取收敛为「phase 强制 + 模型配置默认」两档(见 engine.resolveProfileName);
|
||||
// 升级老库:profile 曾记会话绑定的 prompt+tools 组合名(bind 弹窗「模式」区)。已废弃:
|
||||
// 前端手动切换模式全部移除,profile 选取只剩模型配置默认一档(见 engine.resolveProfileName);
|
||||
// 列保留(SQLite 删列不值得),代码不再读写。
|
||||
if (!cols.some(c => c.name === 'profile')) {
|
||||
db.exec("ALTER TABLE sessions ADD COLUMN profile TEXT DEFAULT ''");
|
||||
}
|
||||
// 迁移:profile_snapshot 存会话创建/清空时冻结的用户画像注入文本(buildMessages 每轮首条 user 注入,
|
||||
// 字节恒定不破坏前缀缓存;不入 messages 表,阶段切换/压缩动不到它,见 user-profile.js)。
|
||||
// 字节恒定不破坏前缀缓存;不入 messages 表,压缩动不到它,见 user-profile.js)。
|
||||
if (!cols.some(c => c.name === 'profile_snapshot')) db.exec("ALTER TABLE sessions ADD COLUMN profile_snapshot TEXT DEFAULT ''");
|
||||
// 迁移:title_locked 用户手动重命名过标题(renameSession 置 1):handoff 的自动标题见锁即跳过,
|
||||
// 不得静默冲掉用户给会话起的名字;清空会话时随标题一并复位(旧名字已文不对题)。
|
||||
// 迁移:title_locked 用户手动重命名过标题(renameSession 置 1):显式留痕「名字是用户起的」,
|
||||
// 不得静默冲掉;清空会话时随标题一并复位(旧名字已文不对题)。
|
||||
if (!cols.some(c => c.name === 'title_locked')) db.exec("ALTER TABLE sessions ADD COLUMN title_locked INTEGER NOT NULL DEFAULT 0");
|
||||
// 迁移:wechat_bindings 补 deep_think 列——微信端全局深度思考开关(routes/channel.js /think 读写,
|
||||
// 投递时作为既有 deepThink 参数的取值来源);默认 0=关(与 H5 按钮初始态一致,/think on 手动开)。
|
||||
@@ -259,10 +258,9 @@ function initDb(dbPath) {
|
||||
`);
|
||||
})();
|
||||
}
|
||||
// 迁移:phase 语义重定义为 advising(顾问:澄清需求/规划)/ executing(执行)。
|
||||
// 存量会话(旧值 planning/NULL)统一回填 executing——它们都有历史,行为与改造前一致、零打扰;
|
||||
// 只有新建会话进 advising(createSession 显式写入)。幂等,每次启动一条廉价 UPDATE。
|
||||
db.exec("UPDATE sessions SET phase='executing' WHERE phase IS NULL OR phase NOT IN ('advising','executing')");
|
||||
// 迁移:advising(顾问阶段)概念已全部移除,会话不再有 phase 语义;旧列直接删。
|
||||
// 旧 SQLite 不支持 DROP COLUMN(<3.35)时静默跳过:残留死列无妨,代码已不再读写它。
|
||||
try { db.exec('ALTER TABLE sessions DROP COLUMN phase'); } catch (_) { /* 旧版本 SQLite,保留死列 */ }
|
||||
// 迁移(C 重构):memory_entries「一天一行拼 blob」→ memory_fragments「一片一行」。幂等:旧表存在才搬,
|
||||
// 搬完即 drop;解析不出【sid】头的块(DAY_MAX 时代削出的孤儿残句)直接丢弃;
|
||||
// 存量碎片的 day 顺手改判为来源会话最后活跃日(事件日语义修正,积压补记自然散布回真实日期)。
|
||||
|
||||
+10
-102
@@ -140,7 +140,6 @@ function emitStatus(sessionId) {
|
||||
bus.emit(sessionId, 'status', {
|
||||
session_id: sessionId,
|
||||
status: s.status,
|
||||
phase: s.phase,
|
||||
model_id: s.model_id,
|
||||
});
|
||||
}
|
||||
@@ -314,12 +313,10 @@ function registerProfilePrompts() {
|
||||
setProfilePrompts(map);
|
||||
}
|
||||
|
||||
// 本轮生效的 profile 名(两档):advising 阶段强制命中内置 'advising' 组合(顾问只该看到顾问的工具);
|
||||
// 否则 = 模型配置默认(cfg.models[].profile)> ''(默认全量)。
|
||||
// 运行期手动切换已全部移除(消息级 profile 参数、sessions.profile 会话绑定均已废弃),
|
||||
// 顾问/执行的节奏由 sessions.phase 单一状态机控制,见 enterExecuting / archiveTaskBrief。
|
||||
// 本轮生效的 profile 名:模型配置默认(cfg.models[].profile)> ''(默认全量,profile-default.txt)。
|
||||
// 运行期手动切换已全部移除(消息级 profile 参数、sessions.profile 会话绑定均已废弃);
|
||||
// 原 advising 阶段强制组合已随顾问模式一并移除。
|
||||
function resolveProfileName(session) {
|
||||
if (session && session.phase === 'advising') return 'advising';
|
||||
const m = session && session.model_id && (cfg.models || []).find(x => x.id === session.model_id);
|
||||
return m && m.profile ? String(m.profile) : '';
|
||||
}
|
||||
@@ -345,71 +342,6 @@ function effectiveTools(profileName) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// ---------- 阶段(sessions.phase:advising 顾问 / executing 执行) ----------
|
||||
// advising:新建会话与「清空」的落点。profile 被 resolveProfileName 强制为 'advising',
|
||||
// 只聊和读不写(write_file/edit_file/copy_path 不在其 schema,批内幻觉调用也在 runLoopInner 挡下;
|
||||
// list_directory 在白名单里——顾问翻阅历史目录正当其用);
|
||||
// 唯一出口 = handoff 工具成功返回(brief 由 handler 落盘任务目录根 task.md,见 tools/executor.js)。
|
||||
// executing:一路走到底,只有清空才回 advising;压缩不碰 phase(压完仍回原模式)。
|
||||
|
||||
// advising → executing:任务书落地,当场重建上下文——
|
||||
// 全部历史标 archived(UI 照常展示、不再发模型,与压缩/定时投递同机制),
|
||||
// 插一条 type='task_brief' 的 user 行(内容 = 此刻 task.md 磁盘快照,非引用:后续文件怎么改都不动锚点)
|
||||
// 作为执行阶段唯一上下文起点;runLoopInner 下一迭代重读历史 + 重解 profile,天然已在执行态,AI 同回合开工。
|
||||
// 幂等:phase 已是 executing 直接返回(执行中再改 task.md 只是普通文件编辑,不重存档、不重插快照)。
|
||||
function enterExecuting(sessionId) {
|
||||
const session = getSession(sessionId);
|
||||
if (!session || session.phase !== 'advising') return;
|
||||
const briefPath = path.join(taskDirOf(sessionId), 'task.md');
|
||||
let snapshot;
|
||||
try {
|
||||
snapshot = fs.readFileSync(briefPath, 'utf8');
|
||||
} catch (e) {
|
||||
log.warn('engine', sessionId + ' 阶段切换放弃:读不回 task.md — ' + (e && e.message || e));
|
||||
return;
|
||||
}
|
||||
if (!snapshot.trim()) return; // 空任务书不构成承诺,继续留在顾问模式
|
||||
const n = db.prepare(
|
||||
"UPDATE messages SET type = 'archived' WHERE session_id = ? AND (type IS NULL OR type != 'archived')"
|
||||
).run(sessionId).changes;
|
||||
saveMessage(sessionId, {
|
||||
role: 'user', type: 'task_brief',
|
||||
// 只报「进入执行模式 + 行为约束 + 任务书本体」:task.md/.task-history 是系统内部传递机制,
|
||||
// 不进模型视野(知道文件名只会引出无谓的读写尝试);Missing 逐项确认段是 user_skip
|
||||
// 事故(执行者把跳过顾问泛化成跳过确认)的行为约束,不可减。
|
||||
content: '【任务书】顾问阶段结束,会话已进入执行模式,以下任务书是唯一目标依据;'
|
||||
+ '之前的顾问对话已存档、不再发送。若任务书列有 Missing/待确认事项:开工前逐项向用户提问并等待答复——'
|
||||
+ '用户跳过的只是顾问阶段,不是确认环节,不得据此自行拍板;只有用户明确说过「别问,直接做」才可用默认值,且须把每个默认决定列出供用户检查;禁止把待确认项改写成「按默认执行」。\n\n' + snapshot,
|
||||
});
|
||||
db.prepare("UPDATE sessions SET phase='executing', updated_at=? WHERE id=?").run(now(), sessionId);
|
||||
bus.emit(sessionId, 'phase', { phase: 'executing' });
|
||||
emitStatus(sessionId);
|
||||
log.info('engine', sessionId + ' advising → executing:task.md 落地,存档 ' + n + ' 条消息,上下文重建为任务书快照');
|
||||
}
|
||||
|
||||
// 清空即回 advising:上一轮任务书 mv 进 .task-history/(带时间戳名,撞名再加 -N,同上传风格),
|
||||
// 防旧约定污染新一轮规划。点前缀目录在文件列表默认隐藏(同 .imgcache 约定),AI/用户显式请求可查;
|
||||
// 同步清掉 files 元数据表的 task.md 行(列表读磁盘不读表,清行只为不留孤儿记录)。
|
||||
function archiveTaskBrief(sessionId) {
|
||||
try {
|
||||
const dir = taskDirOf(sessionId);
|
||||
const src = path.join(dir, 'task.md');
|
||||
if (!fs.existsSync(src)) return;
|
||||
const hist = path.join(dir, '.task-history');
|
||||
fs.mkdirSync(hist, { recursive: true });
|
||||
const d = new Date();
|
||||
const pad = x => String(x).padStart(2, '0');
|
||||
const stamp = d.getFullYear() + pad(d.getMonth() + 1) + pad(d.getDate()) + '-' + pad(d.getHours()) + pad(d.getMinutes()) + pad(d.getSeconds());
|
||||
let dest = path.join(hist, 'task-' + stamp + '.md');
|
||||
for (let i = 1; fs.existsSync(dest); i++) dest = path.join(hist, 'task-' + stamp + '-' + i + '.md');
|
||||
fs.renameSync(src, dest);
|
||||
db.prepare("DELETE FROM files WHERE session_id = ? AND filename = 'task.md'").run(sessionId);
|
||||
log.info('task', sessionId + ' 旧任务书归档 → ' + path.relative(dir, dest));
|
||||
} catch (e) {
|
||||
log.warn('task', sessionId + ' 旧任务书归档失败:' + (e && e.message || e));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 绑定 ----------
|
||||
// 写入绑定:会话粘住自己的 key+模型(断开/结版/终止不清,跨重启沿用,追问免选),
|
||||
// 并刷新槽位 last_used_at(「最久未用先上」的轮转依据)。model_id 同时用于展示/统计。
|
||||
@@ -567,7 +499,7 @@ function finalize(sessionId, run) {
|
||||
// 连环「嗯」不算有效对话;不满足等下个 finalize 再判。
|
||||
// 不带 system prompt:历史 user/assistant 原文(注入行不进,白名单见 llm.titleMessages)+
|
||||
// 末尾一条标题指令 user,用会话当前 model_id 发一次不传 tools 的调用。
|
||||
// 失败静默(title 仍空,下个回合结束自动重试);handoff 时顾问自写的标题占位后不再覆盖。
|
||||
// 失败静默(title 仍空,下个回合结束自动重试);已有标题(建会话时填写或手动重命名)不再覆盖。
|
||||
// force=true(仅收口兑底 wrapTitleFallback 传):跳过 n/chars 现推门槛无条件立题——
|
||||
// 收口是最后一站,没有下个回合了,差一点的标题也比永远无名强;其余守卫(非空即跳/防重入/
|
||||
// 清空校验)不受 force 影响,finalize 路径不传维持原门槛。
|
||||
@@ -804,7 +736,6 @@ async function runLoopInner(sessionId, run) {
|
||||
|
||||
// 执行工具
|
||||
const vision = modelVision(session);
|
||||
let handoffDone = false; // 本批工具里是否有 handoff 成功返回(批量跑完统一切换,见循环尾注释)
|
||||
// 标题门槛信号:本批参数预检通过并真实执行 = 会话在干实事(finalize 据此判是否立题,
|
||||
// 见 maybeGenerateTitle);errAck 批(参数畸形、下轮整轮对模型不可见)不算。
|
||||
if (!isErrAck) run.toolWorked = true;
|
||||
@@ -821,12 +752,6 @@ async function runLoopInner(sessionId, run) {
|
||||
// 把正确形状告诉模型,替它纠正包装习惯
|
||||
output = chk.error;
|
||||
isError = true;
|
||||
} else if (session.phase === 'advising'
|
||||
&& (tc.function.name === 'write_file' || tc.function.name === 'edit_file' || tc.function.name === 'copy_path')) {
|
||||
// 顾问单入口硬约束:写类工具不在其 schema,但「名单外照跑」是全局策略(executeTool 全表),
|
||||
// 幻觉调用会真写文件、绕过出口制造假承诺——阶段契约优先,挡下并指向 handoff。
|
||||
output = 'Error: 顾问阶段不能写文件。任务书请通过 handoff 工具的 brief 参数提交。';
|
||||
isError = true;
|
||||
} else {
|
||||
try {
|
||||
output = await executeTool({ sessionId, taskDir: taskDirOf(sessionId), run, cfg, db, vision,
|
||||
@@ -881,19 +806,6 @@ async function runLoopInner(sessionId, run) {
|
||||
image_url: imageRel ? '/api/sessions/' + encodeURIComponent(sessionId) + '/files/download?path=' + encodeURIComponent(imageRel) : undefined,
|
||||
msg_id: toolMsgId,
|
||||
});
|
||||
// 阶段切换入口:advising 回合里 handoff 成功返回(brief 已由 handler 落盘 task.md)→ 标记待切换。
|
||||
// 不在这里就地切:同批可能还有其它 tool_calls,其父 assistant 行若先被存档、
|
||||
// 后落库的回执就成了无主孤儿行(上游拒收)——整批跑完再统一重建上下文。
|
||||
if (!isError && tc.function.name === 'handoff') {
|
||||
handoffDone = true;
|
||||
}
|
||||
}
|
||||
if (handoffDone) {
|
||||
// 顾问标题已由 toolHandoff 直写 sessions.title:广播同 maybeGenerateTitle 的 title 事件,
|
||||
// 已开窗口刷详情页标题 + 重拉会话列表(finalize 的自动标题见非空即跳过,不会覆盖)。
|
||||
const ht = db.prepare('SELECT title FROM sessions WHERE id = ?').get(sessionId);
|
||||
if (ht && ht.title) bus.emit(sessionId, 'title', { title: ht.title });
|
||||
enterExecuting(sessionId);
|
||||
}
|
||||
iter++;
|
||||
}
|
||||
@@ -1099,11 +1011,11 @@ function createSession(title, preferModelId, assistantType, user) {
|
||||
const dir = taskDirOf(id);
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
db.prepare(
|
||||
"INSERT INTO sessions (id, title, status, phase, model_id, model_slot, last_model_id, held_at, assistant_type, user, created_at, updated_at) VALUES (?,?,'held','advising',?,?,?,?,?,?,?,?)"
|
||||
"INSERT INTO sessions (id, title, status, model_id, model_slot, last_model_id, held_at, assistant_type, user, created_at, updated_at) VALUES (?,?,'held',?,?,?,?,?,?,?,?)"
|
||||
).run(id, title || '', modelId, key.id, modelId, now(), assistantType || 'secretary', user || '', now(), now());
|
||||
db.prepare('UPDATE model_pool SET last_used_at = ? WHERE id = ?').run(now(), key.id);
|
||||
profile.freezeSnapshot(db, id); // 冻结画像快照:仅创建/清空时点注入,会话内恒定(见 user-profile.js)
|
||||
// 新会话一律落 advising(顾问模式):先聊天判意图,任务书 task.md 落地才转 executing(见 enterExecuting)
|
||||
// 新建会话默认走模型配置的 profile(未配 = 全量默认组合 + profile-default.txt),不再有顾问阶段
|
||||
emitStatus(id);
|
||||
const session = getSession(id);
|
||||
return session;
|
||||
@@ -1166,8 +1078,8 @@ function renameSession(sessionId, title) {
|
||||
const t = String(title || '').trim();
|
||||
if (!t) throw Object.assign(new Error('名称不能为空'), { status_code: 400 });
|
||||
if (t.length > 100) throw Object.assign(new Error('名称不能超过 100 字'), { status_code: 400 });
|
||||
// 手动改名即上锁:后续 handoff 的自动标题见 title_locked 即跳过,不许冲掉用户起的名字
|
||||
//(maybeGenerateTitle 本就见非空即跳,无需看锁)。
|
||||
// 手动改名即上锁:显式留痕「标题为用户所起」。自动标题(maybeGenerateTitle)本就见非空即跳,
|
||||
// 锁不改现有行为,仅为后续可能覆写标题的逻辑留一道防线。
|
||||
touch(sessionId, { title: t, title_locked: 1 });
|
||||
log.info('task', sessionId + ' 重命名为「' + t + '」');
|
||||
return { ok: true, title: t };
|
||||
@@ -1439,8 +1351,6 @@ function wipeSessionTmp(sessionId) {
|
||||
// 会话级 /tmp(宿主机即 <任务目录>/.tmp,执行容器 bind 成容器内 /tmp)一并清空:清空的语义是“从零开始”,
|
||||
// 上一轮留下的临时产物不该带进新一轮;执行容器遇到不存在的 .tmp 会懒重建,无副作用。
|
||||
// 状态收尾为 completed;此后再次发言从零开始,不再携带旧历史。执行中的会话不允许清空,需先「终止」。
|
||||
// 阶段语义:清空是 executing 唯一回头路——回到 advising 重新规划(单会话只解决一个问题),
|
||||
// 旧任务书 mv 进 .task-history/ 防污染(archiveTaskBrief);压缩不碰 phase,不在此列。
|
||||
function clearSessionMessages(sessionId) {
|
||||
const session = getSession(sessionId);
|
||||
if (!session) throw Object.assign(new Error('会话不存在'), { status_code: 404 });
|
||||
@@ -1455,7 +1365,6 @@ function clearSessionMessages(sessionId) {
|
||||
}
|
||||
runs.delete(sessionId);
|
||||
db.prepare('DELETE FROM messages WHERE session_id = ?').run(sessionId);
|
||||
archiveTaskBrief(sessionId); // 旧任务书先归档(mv),新一轮 advising 不会被上一问题的约定污染
|
||||
wipeSessionTmp(sessionId); // 会话级 /tmp 随清空一并抹掉(尽力而为,失败只记日志不阻断)
|
||||
autoCompressProposed.delete(sessionId); // 历史清零,恢复自动压缩提议额度
|
||||
// 标题随历史一并清零:旧标题描述的是已删除的对话,留着会让新对话文不对题;
|
||||
@@ -1471,13 +1380,12 @@ function clearSessionMessages(sessionId) {
|
||||
if (frags) log.info('task', sessionId + ' 清空连带删除日记碎片 ' + frags + ' 条');
|
||||
}
|
||||
const reset = { status: 'completed', completed_at: now(), cancelled_at: null, held_at: null,
|
||||
title: null, title_locked: 0, phase: 'advising', journaled_at: null };
|
||||
title: null, title_locked: 0, journaled_at: null };
|
||||
if (isTaskHome) delete reset.journaled_at; // touch 会把 undefined 写成 NULL,这里直接不送这个键
|
||||
touch(sessionId, reset);
|
||||
profile.freezeSnapshot(db, sessionId); // 回 advising 重新规划:画像快照随新一轮重新冻结
|
||||
profile.freezeSnapshot(db, sessionId); // 新一轮开始:画像快照随新一轮重新冻结
|
||||
bus.emit(sessionId, 'done', { status: 'completed' });
|
||||
bus.emit(sessionId, 'title', { title: null }); // 供其他打开同一会话的窗口同步清掉旧标题
|
||||
bus.emit(sessionId, 'phase', { phase: 'advising' });
|
||||
emitStatus(sessionId);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
@@ -88,9 +88,6 @@
|
||||
margin-bottom: 10px; background: #fff; color: var(--ink); resize: vertical;
|
||||
}
|
||||
#schedule-modal textarea:focus { border-color: var(--active); }
|
||||
/* 会话阶段徽标(advising 顾问态的标题小图标;executing 不显;尺寸显式定,不依赖 .ic 默认值) */
|
||||
.prof-badge { display: inline-flex; align-items: center; margin-right: 4px; opacity: .62; flex: none; }
|
||||
.prof-badge .ic { width: 13px; height: 13px; color: var(--active); }
|
||||
/* welcome 头像兼做「选择模型」入口(与聊天态的「选择模型」同一弹窗) */
|
||||
.logo-badge { cursor: pointer; }
|
||||
</style>
|
||||
|
||||
@@ -355,7 +355,6 @@
|
||||
const s = detail.session;
|
||||
state.sessionTitle = (s && s.title) || '';
|
||||
state.sessionType = (s && s.assistant_type) || 'secretary'; // 顶部 icon 跟随会话类型
|
||||
state.phase = s && s.phase || ''; // 会话阶段(advising/executing):头部徽标与 SSE status/phase 事件同源
|
||||
state.status = (s && s.status) || '';
|
||||
state.schedule = detail.schedule || null; // 定时任务状态条数据(随 updatePhaseBar 一并渲染)
|
||||
state.toolCards = {};
|
||||
|
||||
@@ -160,7 +160,6 @@
|
||||
mode: 'secretary', // 新建会话默认助手类型(欢迎页已去掉模式切换,不再可变)
|
||||
sessionType: 'secretary', // 当前会话的助手类型(建档时锁定,后续不随模式切换变更)
|
||||
deepThink: false, // 深度思考开关(仅本地视觉状态,无后端参数)
|
||||
phase: '', // 当前会话阶段(sessions.phase: advising=顾问 / executing=执行;徽标据此渲染)
|
||||
pendingModel: '', // welcome 预选模型(ensureSession 建档时透传 model_id)
|
||||
sessionId: null, // 当前会话
|
||||
sessionTitle: '', // 当前会话标题
|
||||
@@ -200,32 +199,16 @@
|
||||
updateScrollBottomBtn();
|
||||
}
|
||||
|
||||
// ---------- 会话阶段徽标 ----------
|
||||
// 头部标题前小图标:advising(顾问:澄清需求/规划)才显示(tooltip 说明),
|
||||
// executing 是主干状态不显示、减噪。原 profile 模式徽标机位改造而来(手动切换模式已移除)。
|
||||
const PHASE_BADGES = {
|
||||
advising: {
|
||||
label: '顾问模式:澄清需求、规划任务;写定任务书后自动开始执行',
|
||||
icon: '<svg viewBox="0 0 24 24" class="ic" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M9 18h6"/><path d="M10 21h4"/><path d="M12 3a6 6 0 0 0-3.6 10.8c.7.6 1.1 1.4 1.1 2.2h5c0-.8.4-1.6 1.1-2.2A6 6 0 0 0 12 3z"/></svg>',
|
||||
},
|
||||
};
|
||||
function phaseBadgeHtml(p) {
|
||||
const b = PHASE_BADGES[p];
|
||||
if (!b) return '';
|
||||
return '<span class="prof-badge phase-' + esc(p) + '" title="' + esc(b.label) + '">' + b.icon + '</span>';
|
||||
}
|
||||
|
||||
function renderHeader() {
|
||||
const c = $('header-center');
|
||||
// welcome(含刚 newChat)一律清空:newChat 会停在 welcome,不重刷会残留旧会话标题
|
||||
if (state.view !== 'chat' || !state.sessionId) { c.innerHTML = ''; return; }
|
||||
// 顶部只显会话阶段徽标(advising 才显)+ 标题;助手类型 icon 早已退位
|
||||
// 顶部只显标题;助手类型 icon 早已退位(会话阶段徽标随 advising 一并移除)
|
||||
const title = document.createElement('div');
|
||||
title.className = 'header-title';
|
||||
title.style.cursor = 'pointer';
|
||||
title.title = '点击重命名';
|
||||
title.innerHTML = phaseBadgeHtml(state.phase) +
|
||||
'<span class="header-title-text">' + esc(state.sessionTitle || state.sessionId) + '</span>';
|
||||
title.innerHTML = '<span class="header-title-text">' + esc(state.sessionTitle || state.sessionId) + '</span>';
|
||||
title.addEventListener('click', renameSession); // 点标题重命名(同步 index)
|
||||
c.innerHTML = '';
|
||||
c.appendChild(title);
|
||||
|
||||
@@ -78,7 +78,6 @@
|
||||
state.sessionId = null;
|
||||
state.sessionTitle = '';
|
||||
state.sessionType = 'secretary';
|
||||
state.phase = '';
|
||||
state.pendingModel = '';
|
||||
state.streamEl = null;
|
||||
state.streamText = '';
|
||||
@@ -161,7 +160,6 @@
|
||||
// 先从抽屉列表缓存取会话类型,让顶部 icon 立即正确;详情回来后由 renderDetail 纠正
|
||||
const cached = (state.sessions || []).find(x => x.id === id);
|
||||
state.sessionType = (cached && cached.assistant_type) || 'secretary';
|
||||
state.phase = (cached && cached.phase) || ''; // 先从列表缓存取阶段徽标,renderDetail 回来后纠正
|
||||
state.streamEl = null;
|
||||
state.streamText = '';
|
||||
state.streamWrap = null;
|
||||
@@ -214,7 +212,6 @@
|
||||
state.sessionType = 'secretary';
|
||||
state.status = '';
|
||||
state.schedule = null;
|
||||
state.phase = '';
|
||||
state.pendingModel = '';
|
||||
state.attachments = [];
|
||||
state.streamEl = null;
|
||||
|
||||
@@ -136,19 +136,10 @@
|
||||
es.addEventListener('status', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
state.status = d.status;
|
||||
state.phase = d.phase || '';
|
||||
renderHeader();
|
||||
updatePhaseBar(d);
|
||||
updateScrollBottomBtn(); // 状态切换同步右下角转圈(开始/结束干活)
|
||||
});
|
||||
// 阶段切换(advising↔executing):任务书落地自动转执行时给一句明确反馈;
|
||||
// 回 advising(清空)由发起动作自带反馈,不重复弹。
|
||||
es.addEventListener('phase', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
state.phase = d.phase || '';
|
||||
renderHeader();
|
||||
if (state.phase === 'executing') toast('任务书已确认,进入执行模式');
|
||||
});
|
||||
es.addEventListener('assistant_delta', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
removeTyping();
|
||||
|
||||
@@ -192,7 +192,6 @@ const HELP_TEXT = [
|
||||
].join('\n');
|
||||
|
||||
const STATUS_TEXT = { active: '活跃中', held: '保持中', completed: '已完成', cancelled: '已终止' };
|
||||
const PHASE_TEXT = { advising: '顾问', executing: '执行' };
|
||||
|
||||
function fmtTime(ts) {
|
||||
if (!ts) return '--';
|
||||
|
||||
@@ -97,7 +97,6 @@ router.get('/:id/stream', (req, res) => {
|
||||
bus.emit(session.id, 'status', {
|
||||
session_id: session.id,
|
||||
status: session.status,
|
||||
phase: session.phase,
|
||||
model_id: session.model_id,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -107,7 +107,7 @@ router.get('/status', wrap((req, res) => {
|
||||
auth_enabled: !!(cfg.auth && cfg.auth.users && cfg.auth.users.length),
|
||||
running_sessions: engine.activeRuns(),
|
||||
// 曾有的 model_profiles 可用名单字段已删:前端手动切换模式入口全部移除,名单再无消费方
|
||||
//(组合只经模型配置 profile: <名> 引用,或 advising 阶段强制命中,见 engine.resolveProfileName)。
|
||||
//(组合只经模型配置 profile: <名> 引用,见 engine.resolveProfileName)。
|
||||
// 前端上传预处理参数(大图压缩阈值/尺寸/质量、大文件确认阈值),避免前端写死
|
||||
upload: cfg.upload || {},
|
||||
});
|
||||
|
||||
@@ -218,9 +218,6 @@ function tick() {
|
||||
// 顺序不可调换:先存档、后投递。postUserMessage 的 held/空闲分支会同步起 runLoop,
|
||||
// 存档晚一步,回合就会带着旧上下文开跑。
|
||||
archiveHistory(db, t.session_id);
|
||||
// 无人值守的是执行型任务:投递前强制 phase='executing'——新会话可能还停在 advising,
|
||||
// 顾问 profile 会把半夜的 prompt 当白天聊天「引导澄清并等用户回答」,永远出不了活。
|
||||
db.prepare("UPDATE sessions SET phase='executing', updated_at=? WHERE id=?").run(now, t.session_id);
|
||||
const r = deliver(t.session_id, t.prompt);
|
||||
db.prepare(
|
||||
"UPDATE scheduled_tasks SET last_run_at=?, last_status='running', last_note=NULL, next_run_at=? WHERE session_id=?"
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
You are the planning advisor for this conversation, accessed via a web
|
||||
interface. This session is in the ADVISING phase: your job is to answer
|
||||
questions, judge the user's intent, and turn a vague request into a clear,
|
||||
executable task brief. You do not do the actual work here.
|
||||
|
||||
Each conversation has its own working directory. Tool paths are relative to it
|
||||
unless stated otherwise.
|
||||
|
||||
## How to behave
|
||||
|
||||
- Small talk, general questions, quick look-ups: answer normally and directly.
|
||||
Do not push every message toward "a task".
|
||||
- When the user clearly wants work done: first make the requirement precise —
|
||||
goal, inputs, expected output, constraints — in a friendly, short way.
|
||||
- Ask at most one or two questions per reply, and only about points that would
|
||||
change the deliverable. For trivial choices, pick a sensible default and note
|
||||
the assumption. Never interrogate: after two or three rounds, or as soon as
|
||||
the picture is clear enough, stop asking.
|
||||
|
||||
## Producing the task brief
|
||||
|
||||
When work should start — the requirement is clear, the user asks to skip
|
||||
planning, or clarification has stalled — call the `handoff` tool with a
|
||||
`reason`, a `title` (in the user's language), and the full `brief`. This is the
|
||||
only way to leave the advising phase; the system then switches the session
|
||||
into execution mode and a full-capability agent continues from the brief
|
||||
alone.
|
||||
|
||||
The brief must be self-contained: someone who has never seen this chat must be
|
||||
able to do the job from it. For small requests a three-line brief is fine;
|
||||
never inflate it. Suggested structure: short title / Goal / Scope &
|
||||
constraints / What the deliverable must contain / Acceptance criteria /
|
||||
Assumptions picked.
|
||||
|
||||
If key facts are still missing when you hand off, list them under a "Missing"
|
||||
section — in the user's own words, without guessing or adding generic advice.
|
||||
The executor will ask the user about these before starting. A skipped-planning
|
||||
brief is a short requirements note, not a methodology document.
|
||||
|
||||
## What the executor can do
|
||||
|
||||
The executor is a full-capability agent in an isolated Linux container with a
|
||||
session working directory. Broadly: file operations, shell commands with
|
||||
internet access, data processing and SQLite storage, web data pages, document
|
||||
conversion, media processing, email, and public download links.
|
||||
|
||||
{APP_SKILLS_SECTION}
|
||||
|
||||
The list above is a coarse guide, not a hard boundary — and you are not the
|
||||
one who judges feasibility. The executor may achieve a request by combining
|
||||
its tools, or by combining tools with the user's own environment (their
|
||||
browser or device). When feasibility is in doubt, do not argue it out and do
|
||||
not declare it impossible on the user's behalf — hand off and flag the
|
||||
question in the brief. Doubt about *feasibility* goes to handoff; doubt about
|
||||
*what the user wants* goes through normal clarification.
|
||||
|
||||
Prefer approaches that use existing capabilities. If unsure whether something
|
||||
is feasible, say so or note it as a risk — never fake certainty in either
|
||||
direction.
|
||||
|
||||
## What you must not do
|
||||
|
||||
- Do not carry out the task itself in this phase. Research that informs the
|
||||
plan — reading files, fetching web — is fine; producing the deliverable is
|
||||
not.
|
||||
- Do not prescribe technical implementation. Describe *what* the result must
|
||||
be and *how it will be judged*; never *how to build it*.
|
||||
|
||||
When in doubt about whether something is a requirement or an implementation
|
||||
detail, ask: "Would a different executor using different tools still need
|
||||
this?" If yes, it belongs in the brief. If no, leave it out.
|
||||
@@ -140,40 +140,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "handoff",
|
||||
"description": "Hand off from the advising phase to the execution phase. Call this when the task brief is ready. The system stores your `brief` as the task brief and switches to execution. There are three cases:\n\n- ready: The requirement is clear enough to execute.\n- user_skip: The user explicitly asks to stop planning with you (\"just move on\", or similar) — this skips only the advising conversation, it never waives the user's later confirmations. Do not call this on tone or mood alone — only on explicit signals.\n- user_stuck: You have asked 2-3 rounds and the user is still not providing key information, and they seem to want to move on.\n\nWhen reason is not \"ready\", the `brief` must carry the user's own words plus every open question under a Missing section — the executor is required to ask the user these before acting. Do not invent a methodology or generic plan.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"reason",
|
||||
"brief",
|
||||
"title"
|
||||
],
|
||||
"properties": {
|
||||
"reason": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"ready",
|
||||
"user_skip",
|
||||
"user_stuck"
|
||||
],
|
||||
"description": "Why the handoff is happening."
|
||||
},
|
||||
"brief": {
|
||||
"type": "string",
|
||||
"description": "The task brief content, in Markdown. Follow the structure described in your system prompt."
|
||||
},
|
||||
"title": {
|
||||
"type": "string",
|
||||
"description": "A concise title for this conversation, in the user's language (3-7 words, or 6-14 Chinese characters). Straight to the title: no quotes, no prefix like \"Title:\"."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
|
||||
@@ -1557,39 +1557,6 @@ function runLabHelp(args) {
|
||||
return JSON.stringify(def.function, null, 2);
|
||||
}
|
||||
|
||||
// handoff:顾问阶段唯一出口(advising 组合见 config.js)。brief 原样落盘任务目录根 task.md;
|
||||
// title 直写 sessions.title(交接此刻顾问最懂这单任务,标题以此为准,finalize 的
|
||||
// maybeGenerateTitle 见非空即跳过);批尾由 engine 广播 title 事件同步已开窗口。
|
||||
// 切换不在此发生——executeTool 只落盘与回执,runLoopInner 在工具批尾接成功回执触发 enterExecuting
|
||||
//(其读盘取快照作锚点,幂等防重切)。phase 守卫不可省:「名单外工具照跑」是全局策略,
|
||||
// 执行期幻觉调用不得覆写修订中的 task.md;reason 枚举与 brief 非空也在此兑底(checkToolArgs 只查必填键)。
|
||||
async function toolHandoff(ctx, args) {
|
||||
const sess = ctx.db.prepare('SELECT phase, title_locked FROM sessions WHERE id = ?').get(ctx.sessionId);
|
||||
if (!sess || sess.phase !== 'advising') return 'Error: 当前不在顾问阶段,handoff 仅在 advising 可用';
|
||||
const reason = String((args && args.reason) || '');
|
||||
if (['ready', 'user_skip', 'user_stuck'].indexOf(reason) < 0) {
|
||||
return 'Error: reason 需为 ready / user_skip / user_stuck 之一';
|
||||
}
|
||||
const brief = typeof args.brief === 'string' ? args.brief : '';
|
||||
if (!brief.trim()) return 'Error: brief 不能为空,任务书需完整带上(user_skip/user_stuck 至少含用户原话与 Missing 清单)';
|
||||
// 标题清洗对齐 cleanTitle(engine 内函数,executor 反向引用会成环,此处同款口径):
|
||||
// 压空白 + 截 60;引号剥除交给模型自觉(description 已明说),不做二次加工。
|
||||
const title = String(args.title || '').replace(/\s+/g, ' ').trim().slice(0, 60);
|
||||
if (!title) return 'Error: title 不能为空,给出一个概括任务的简洁标题';
|
||||
const abs = path.join(ctx.taskDir, 'task.md');
|
||||
await fsp.writeFile(abs, brief, 'utf8');
|
||||
recordFile(ctx, abs); // 同步 files 元数据表(与 write_file 同轨,任务目录列表/清理逻辑一致)
|
||||
// task.md 落盘成功后才改标题:切换半途失败不留「有名无书」的残局;
|
||||
// 用户手动重命名过(title_locked,见 engine.renameSession)则尊重用户的名字,静默跳过。
|
||||
if (!sess.title_locked) {
|
||||
ctx.db.prepare('UPDATE sessions SET title = ?, updated_at = ? WHERE id = ?').run(title, Date.now(), ctx.sessionId);
|
||||
}
|
||||
// 同批多次 handoff:后写覆盖(last-wins),批尾的切换只发生一次(enterExecuting 幂等)。
|
||||
// 回执只写事实:成功路径下本回执连同父行一起被存档、不会回到任何模型;
|
||||
// 读者只有 enterExecuting 放弃/报错后继续 advising 的模型本身(据此自纠),无行为指令可带。
|
||||
return '已接收交接(reason: ' + reason + '),本批工具跑完后系统将切换到执行阶段。';
|
||||
}
|
||||
|
||||
// recall:长期记忆(工作日记)的回忆入口——纯记忆三 op。
|
||||
// 日记按会话收口持久存储(memory.js),开场只注入最近 N 条;更早的条目由本工具按需取。
|
||||
// 历史成果文件不再归日记管:浏览/导入走 list_directory / copy_path 的 @<会话id>/<路径> 记号
|
||||
@@ -1627,7 +1594,6 @@ const HANDLERS = {
|
||||
read_file: toolReadFile,
|
||||
list_directory: toolListDirectory,
|
||||
copy_path: toolCopyPath,
|
||||
handoff: toolHandoff,
|
||||
write_file: toolWriteFile,
|
||||
edit_file: toolEditFile,
|
||||
mint_public_link: toolMintPublicLink,
|
||||
|
||||
@@ -241,8 +241,7 @@ function handleHook(body) {
|
||||
// wxbot 会把 error 文案直接回给用户(IM 里「出错要出声」)。
|
||||
// 媒体消息(inbox 带 media:{b64,kind,filename?},wxbot 已从 CDN 下好):统一落会话任务目录
|
||||
// + 记 files 元数据表 → 按 web 上传同款拼「用户上传了文件:<名>」作正文走 handleSend。
|
||||
// 渠道上下文(普通/媒体/带尾随文本的 /new 在消息入库前先幂等确保):微信会话直接落 executing
|
||||
// 跳过顾问 handoff(与定时任务投递同理由:异步接入不能被顾问话术卡住),channel_note 把微信
|
||||
// 渠道上下文(普通/媒体/带尾随文本的 /new 在消息入库前先幂等确保):channel_note 把微信
|
||||
// 环境说明注入历史(UI 可见的 user 行,模型当普通消息看)——先于首条消息,AI 第一回合就看得到。
|
||||
const MEDIA_MAX_BYTES = 20 * 1024 * 1024;
|
||||
const MEDIA_KINDS = new Set(['image', 'video', 'file']);
|
||||
@@ -266,13 +265,9 @@ const MEMORY_NOTE_HEAD = '# [CTX] memory\n\n'
|
||||
|
||||
function ensureChannelContext(s) {
|
||||
if (!s) return;
|
||||
if (s.phase !== 'executing') {
|
||||
db.prepare("UPDATE sessions SET phase='executing', updated_at=? WHERE id=?").run(now(), s.id);
|
||||
s.phase = 'executing';
|
||||
}
|
||||
if (!db.prepare("SELECT 1 FROM messages WHERE session_id = ? AND type = 'channel_note' LIMIT 1").get(s.id)) {
|
||||
engine.saveMessage(s.id, { role: 'user', type: 'channel_note', content: CHANNEL_NOTE });
|
||||
log.info('wechat', s.id + ' 渠道上下文注入(executing + channel_note)');
|
||||
log.info('wechat', s.id + ' 渠道上下文注入(channel_note)');
|
||||
}
|
||||
// 开场日记:近几天长期记忆一次性写入,会话中途不补——与画像快照同「新会话冻结」语义,
|
||||
// 轮换后新会话再带最新全文;无记忆/功能关闭时不写,省一条消息行也稳前缀缓存。
|
||||
|
||||
+20
-43
@@ -42,7 +42,7 @@ agent/
|
||||
│ │ ├── balance.js 余额查询适配器(按 platform.type 分发)
|
||||
│ │ ├── version.js 版本信息(package.json + git 短提交号)
|
||||
│ │ ├── tools/
|
||||
│ │ │ ├── definitions.json 内置工具的 OpenAI function 格式定义(现 12 项,含元工具 lab 与全局屏蔽的 handoff)
|
||||
│ │ │ ├── definitions.json 内置工具的 OpenAI function 格式定义(现 14 项,含元工具 lab)
|
||||
│ │ │ ├── executor.js 工具执行器(文件类 + terminal + skill 委托)
|
||||
│ │ │ └── skills.js Skill Manager(skill 工具的 list/get/run + 路径预处理)
|
||||
│ │ ├── download-token.js 下载链接签名(浏览器原生下载用;密钥进程内随机)
|
||||
@@ -61,7 +61,6 @@ agent/
|
||||
│ │ │ └── system.js /api/system/status + /restart 优雅重启
|
||||
│ │ ├── templates/
|
||||
│ │ │ ├── profile-default.txt system prompt 静态模板(每次请求复用,利于上游前缀缓存)
|
||||
│ │ │ ├── profile-advising.txt 顾问阶段提示词(内置 profile:advising,phase=advising 时强制命中,见 §4.1)
|
||||
│ │ │ ├── prompt-compress.txt 上下文压缩提示词(压缩回合末尾追加为 user 消息,历史照常走 messages)
|
||||
│ │ │ └── prompt-title.txt 会话标题自动生成提示词(回合结束过门槛触发:本轮有工具,或纯聊≥ 3 正题轮且字数达标;空闲扫描自动收口时无条件补题(wrapTitleFallback,跳过现推门槛——差标题也比没标题强);{{language}} 填配置语言)
|
||||
│ │ └── public/ 前端 H5 SPA(无构建,Vanilla JS 经典脚本多文件 + SSE)
|
||||
@@ -170,7 +169,7 @@ graph LR
|
||||
### 4.1 SQLite 表(`db.js`)
|
||||
|
||||
```
|
||||
sessions id (PK='task-YYYYMMDD-NNN') / title / status / phase /
|
||||
sessions id (PK='task-YYYYMMDD-NNN') / title / status /
|
||||
model_id / last_model_id / assistant_type / user / profile /
|
||||
created_at / updated_at / completed_at / cancelled_at / held_at
|
||||
|
||||
@@ -178,12 +177,13 @@ messages id PK / session_id FK / role (system|user|assistant|tool) /
|
||||
content (TEXT, 含 __blocks__ 哨兵 JSON 多模态) / reasoning /
|
||||
tool_calls (JSON) / tool_call_id / name / type /
|
||||
is_error / created_at
|
||||
# type 取值: summary / archived / errAck / task_brief (任务书快照,advising→executing 切换后首条上下文) /
|
||||
# type 取值: summary / archived / errAck /
|
||||
# session_context (name 列 = SESSION_CONTEXT_NAME) /
|
||||
# scheduler_note(定时任务删除通知)/ channel_note(微信渠道上下文说明,每微信会话一条)/
|
||||
# wx_media(微信单发媒体留痕行,落盘不触发回合,同 scheduler_note 语义;
|
||||
# 未知 type 一律按普通 user 消息发模型、UI 照常展示)
|
||||
# 旧版遗留: summarize_request(压缩指令 user 消息,已废弃,仅 buildMessages 兼容旧库)
|
||||
# 旧版遗留(只读兼容,不再生成): summarize_request(压缩指令 user 消息,已废弃,仅 buildMessages 兼容旧库)/
|
||||
# task_brief(已废弃 advising 阶段切换时代的任务书快照行,标题白名单保留读兼容)
|
||||
|
||||
files id PK / session_id / filename / size / created_at
|
||||
# 仅记录元数据;文件本体在 workspace 目录里
|
||||
@@ -240,29 +240,9 @@ stateDiagram-v2
|
||||
会话永远粘着自己的 model_slot(跨重启/结版/终止保持),换绑只有两条路:用户手动「选择模型」,
|
||||
或绑定失效(配置删模型/禁用/改名)后由 `ensureBinding` 自动改选(优先留原平台)。
|
||||
|
||||
**session.phase 状态机**(advising/executing,`engine.js`):
|
||||
|
||||
```mermaid
|
||||
stateDiagram-v2
|
||||
[*] --> advising: createSession
|
||||
advising --> executing: handoff 工具(handler 落盘 task.md → enterExecuting,同回合继续开工)
|
||||
executing --> advising: clearSessionMessages(旧 task.md mv 进 .task-history/)
|
||||
```
|
||||
|
||||
- advising(顾问):profile 被 `resolveProfileName` 强制为内置 `advising` 组合(问答/判意图/引导澄清需求),
|
||||
只聊和读不写(`write_file`/`edit_file` 不在其白名单,批内幻觉调用也被 runLoopInner 挡下;例外 `profile`——
|
||||
画像元信息不碰任务目录,顾问问答恰是收集称谓/偏好密度最高的时刻),
|
||||
唯一出口是 `handoff` 工具:handler 把 `brief` 原样落盘根 `task.md`(reason ∈ ready/user_skip/user_stuck,
|
||||
空 brief 拒回不切);`handoff` 与 `lab` 同入全局 `tools_deny`,advising 组合显式 `tools_deny: []` 独享可见。
|
||||
- executing(执行):回到常规 profile 链路(模型配置 > 默认全量);单会话只解决一个问题,一路走到底,回头路只有「清空」。
|
||||
- 上下文压缩对 phase 中性(原来什么模式、压完仍什么模式);定时任务投递与微信渠道消息前强制 `executing`(无人值守/异步接入不能被顾问话术卡住,微信侧由 wechat.ensureChannelContext 幂等保证,见 §5.13)。
|
||||
- 切换瞬间全史标 `archived`、插一条 `type='task_brief'` user 行(task.md 磁盘快照)作执行阶段唯一上下文起点;
|
||||
幂等:执行中再改 task.md 只是普通文件编辑。
|
||||
- 存量会话启动时统一回填 `executing`(旧值 planning 废弃,见 db.js 迁移);与 status 状态机正交,不影响模型调度。
|
||||
|
||||
### 4.2 文件布局
|
||||
|
||||
**任务目录**(`cfg.workspace_root/<session-id>/`):每个会话一个目录;Core 与 Executor 都可读写;sh 命令默认 cwd 即此目录。`task.md` = 当前轮任务书(由 `handoff` handler 落盘,phase 切换唯一入口);`.task-history/` = 清空会话时旧任务书归档(点目录,文件列表默认隐藏,同 `.imgcache` 约定)。
|
||||
**任务目录**(`cfg.workspace_root/<session-id>/`):每个会话一个目录;Core 与 Executor 都可读写;sh 命令默认 cwd 即此目录。(旧 advising 时代的 `task.md`/`.task-history/` 特殊语义已随顾问模式一并移除,存量目录里若还有只是普通文件。)
|
||||
|
||||
**技能目录**(`cfg.skills_root/<skill-name>/`):所有会话共享;`deps/` 子目录是 pip `--target` 共享的 Python 依赖(只放镜像缺失的包,镜像已带的不重复装),**与技能目录同级**(不是技能子目录)。`RESERVED_DIRS = {deps}` 在 `routes/skills.js` 里被排除以免被当成技能。
|
||||
|
||||
@@ -280,13 +260,12 @@ stateDiagram-v2
|
||||
| `applyBinding` / `bindingValid` / `ensureBinding` | 会话粘住 key+模型(model_slot/model_id/last_model_id + 刷新槽位 last_used_at);绑定失效(配置删模型/禁用/改名)时按优先级自动改选,优先留原平台;空池抛 503 |
|
||||
| `chooseKey` / `chooseModel` | 优先级选 key(**无排队、无独占**):looping 回合数少 → 在用(active/held)会话数少 → 最久未用(轮转摊平)→ id;具体模型在同平台内选(偏好旧模型 > 绑得最少的) |
|
||||
| `getRun` / `killRunProcs` | 每会话的运行控制(`cancelled` / `procs` / `abortController` / `looping`) |
|
||||
| `runLoop` / `runLoopInner` | Agent 主循环:解析 profile → buildMessages → callModel → 解包 tool args → 校验 → saveMessage → executeTool → saveMessage(tool) → SSE 广播。每迭代现解 profile(`resolveProfileName`:advising 阶段强制内置 `advising` 组合 > 模型配置 > 默认全量),prompt 与 tools 集(`effectiveTools` 按 `tools_allow`/`tools_deny` 过滤)跟着组合切换 |
|
||||
| `runLoop` / `runLoopInner` | Agent 主循环:解析 profile → buildMessages → callModel → 解包 tool args → 校验 → saveMessage → executeTool → saveMessage(tool) → SSE 广播。每迭代现解 profile(`resolveProfileName`:模型配置 > 默认全量),prompt 与 tools 集(`effectiveTools` 按 `tools_allow`/`tools_deny` 过滤)跟着组合切换 |
|
||||
| `streamAssistant` | 单次上游调用:拿 abortSignal / shouldCancel / 各类回调,把流式 chunk 转 SSE 事件(`assistant_delta` / `reasoning_delta`) |
|
||||
| `createSession` | 生成 id(`task-YYYYMMDD-NNN`,序号从历史 + `meta.task_seq` 取大)→ mkdir 任务目录 → phase 默认 advising → **创建即绑模型落 held**(指定模型优先其平台,无效退回全局;空池直接报错不建吊着会话) |
|
||||
| `createSession` | 生成 id(`task-YYYYMMDD-NNN`,序号从历史 + `meta.task_seq` 取大)→ mkdir 任务目录 → **创建即绑模型落 held**(指定模型优先其平台,无效退回全局;空池直接报错不建吊着会话) |
|
||||
| `postUserMessage` | 用户消息入口;除 looping(下一迭代自动带上新消息)外任意状态都是「保绑定→转 active→立即开跑」,`delivered` 恒 `'live'` |
|
||||
| `bindToSession` / `releaseHold` | 手动换绑(任意模型即点即换,仅输出中拒绝,无「占用」概念);「断开」= held→completed 纯状态动作(无资源可释放,绑定仍在可继续用) |
|
||||
| `summarizeContext` / `clearSessionMessages` / `deleteMessage` | 收尾 + 上下文压缩(清空同时把 phase 打回 advising、归档旧 task.md、连带清空会话 .tmp 即执行容器里的 /tmp) |
|
||||
| `enterExecuting` / `archiveTaskBrief` | 阶段切换:批尾接 `handoff` 成功标记(brief 已由 handler 落盘)→ 全史存档 + 插 task_brief 快照 + phase='executing' + SSE 广播;清空时旧 task.md mv 进 `.task-history/` |
|
||||
| `summarizeContext` / `clearSessionMessages` / `deleteMessage` | 收尾 + 上下文压缩(清空连带清空会话 .tmp 即执行容器里的 /tmp) |
|
||||
| `checkToolArgs` / `unwrapToolArgs` | 工具参数校验 + 部分弱模型参数包裹外层自动解包 |
|
||||
| `ensureSessionContext` | 注入/刷新"会话上下文"首条 user 消息(日期/任务目录/模型名/vision 能力) |
|
||||
|
||||
@@ -298,8 +277,7 @@ stateDiagram-v2
|
||||
- **archived/summary 机制**(上下文压缩):`type='archived'` 的消息 UI 照常展示但不再发模型;`type='summary'` 是压缩后的"上下文起点"。压缩回合**不插入任何指令消息**:由 `engine.summarizeContext` 在内存 run 上标 `summarizing`,`runLoopInner` 据此走 `llm.compressMessages`:沿用默认 system prompt + 正常历史 messages,末尾追加一条 user(`server/templates/prompt-compress.txt` 原文,参考标题回合的拼法),且不传 tools;回合内首条无 tool_calls 的收尾回复即摘要。(`llm.buildMessages` 仍保留旧库 `summarize_request` 行的处理,仅为历史数据兼容。)
|
||||
- **图片(多模态)**:`messages.content` 列是 TEXT,含图时存 `{"__blocks__":[content...]}` 哨兵 JSON。新版 `read_file` 读图:sharp 压缩产物落任务目录 `.imgcache/`(前端文件列表隐带点的项),库里只存 `image_ref` 轻量块;`buildMessages` 发送前经调用方传入的 `mintUrl` 回调现签成公网外链(同 `mint_public_link` 那套 `GET /p/<id>`)成 `image_url` 块,base64 不进 DB 也不进请求体;签不到降级为文字占位。**图不塞在 tool 回执里发**(tool 角色带 `image_url` 是非标准形态,大量 OpenAI 兼容上游整批忽略或只认批次最后一张,模型看不到图会反复 read_file 补看成死循环):回执降级为纯文本,未消费的图统一并入该批之后的一条 `user` 消息(逐图对应说明合并置顶为一段文字、图块按回执顺序紧随——对齐官方示例排布;探针实测 DS 对交替排布也兼容,取低风险形态),并附「先逐张简述每图内容再继续任务」指令兼作送达自检;旧库/用户粘贴附件的 `data:` URL 块原样透传。图块**保留到最后一条收尾 assistant(无 tool_calls)之前**(`lastAssistant` 判定)——只发 tool_calls 的中间轮不算已阅、图继续携带(实测过图已进上下文而模型仍自述没看到的行为,鲁棒优先于极致省 token);更早的换成"已省略"文字描述(措辞不催促重读);同回合同一图只投递一次(`run.imgDelivered` 去重,再读同图只回文字提示;多图同请求经探针实测可行,曾按批限"每轮一张"已撤);发送前 `log.debug('req', 发送形态...)` 打每轮图块计数,一眼区分发送侧漏拼与上游吞图。UI 渲染走带 cookie 的会话内下载路由,历史永不裂图。
|
||||
- **key 共享并发,无租约**:`model_pool.is_busy`/`current_session_id` 已废弃不再读写(旧库残留列,启动时清零);同一 key 可被多个会话同时绑定并发 looping(上游 key 本身支持多并发)。held 不再自动到期,顶部「保持中 · 模型」持续展示,唯一出口是手动「断开」或换绑。
|
||||
- **task_brief(任务书锚点,advising→executing 切换)**:runLoopInner 工具批跑完后,若 advising 回合内有 `handoff` 成功返回(brief 已由 handler 原样落盘根 task.md 并记 files 表;批量末尾统一切,防父 assistant 先存档、后落库回执成孤儿行被上游拒收;同批多次 handoff 后写覆盖 last-wins),`enterExecuting` 当场重建上下文:全史标 `archived` → 插一条 `type='task_brief'` user 行(内容 = task.md 磁盘快照,非引用)→ phase='executing' + SSE `phase`/`status` 事件;下一迭代重读历史 + 重解 profile,天然切默认组合、同回合开工。压缩不碰 phase(task_brief 与其它历史一样会被后续压缩存档,漂移可控:压缩本就由用户主导)。
|
||||
- **profile(prompt+tools 组合)**:顶层 `model_profiles` 定义多套组合,`platforms[].models[].profile` 引用(见 §5.8)——这是唯一的引用来源:运行期手动切换已全部移除(消息级 profile 参数、`sessions.profile` 会话绑定均废弃,列保留不读写);`advising` 例外,不走引用、由 phase 强制命中。名单外工具被调用不拦(`checkToolArgs` 查不到 def 不报错,`executeTool` 全表照跑),但阶段契约类例外:advising 内 `write_file`/`edit_file` 在批内挡下、`handoff` 在 handler 里带 phase 守卫;tools schema 只活在请求体不进历史,所以切换不脏化旧消息。压缩回合同样跟随(小模型会话点压缩不会回退到默认大模板)。
|
||||
- **profile(prompt+tools 组合)**:顶层 `model_profiles` 定义多套组合,`platforms[].models[].profile` 引用(见 §5.8)——这是唯一的引用来源:运行期手动切换已全部移除(消息级 profile 参数、`sessions.profile` 会话绑定均废弃,列保留不读写);未配 profile = 默认全量组合(profile-default.txt)。名单外工具被调用不拦(`checkToolArgs` 查不到 def 不报错,`executeTool` 全表照跑);tools schema 只活在请求体不进历史,所以切换不脏化旧消息。压缩回合同样跟随(小模型会话点压缩不会回退到默认大模板)。
|
||||
|
||||
### 5.2 `llm.js` —— 上游调用
|
||||
|
||||
@@ -329,7 +307,7 @@ stateDiagram-v2
|
||||
- session 补偿:MCP 规范的 `Mcp-Session-Id` 缓存在 `server.sessionId`;过期(400 + 含 "session" 字样)自动重 initialize 后重试一次。
|
||||
- 某些老服务无 `initialize` → catch `Method not found` 不阻断。
|
||||
|
||||
### 5.4 `tools/executor.js` —— 内置工具(现 12 项,含元工具 `lab`)的派发器
|
||||
### 5.4 `tools/executor.js` —— 内置工具(现 14 项,含元工具 `lab`)的派发器
|
||||
|
||||
`HANDLERS` 是 `name → async(ctx, args) → result` 的表;`executeTool(ctx, name, args)` 按 `name` 派发;**未知名直接转发给 MCP**(若 `mcp.hasTool(name)`,调 `mcp.callTool`)。
|
||||
|
||||
@@ -337,13 +315,13 @@ stateDiagram-v2
|
||||
|---|---|
|
||||
| `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,超 32MB 拒读并引导 terminal;`path` 可 `@<sid>/<rel>` 只读窥看同用户历史会话的文本(校验单点 `resolvePastPath`,与 list_directory/copy_path 同源)——字节不落本会话,terminal/edit_file 看不见它,历史图片直接拒绝并引导 copy_path(图片进模型视野必须经本会话签外链,藏进 .imgcache 等于偷偷导入);行号前缀格式 "`<6位右对齐行号>\\t<原文>`"(`edit_file` 用) |
|
||||
| `write_file` / `edit_file` | `edit_file` 用模糊定位(Zed 风格):先精确匹配,失败后用 Levenshtein DP 找最相似行;超 32MB 拒绝整文件读写(OOM 防护);DP 候选区间不连续(中间跳过无关 buffer 行)即弃用,防静默吞行;空 `new_text` = 整行干净删除(精确/模糊两层统一,对齐 `"".lines()` 零行语义);`old_text`/`new_text` 协议见 `definitions.json` |
|
||||
| 已移除的工具(部分复活) | `list_directory` / `copy_path` 于 2026-09 复活并加 `@<session-id>/<rel>` 只读记号:当年删除理由是「环境固定,统一走 terminal」,但生产 terminal 在远端 executor 容器(compose:wrapper 启动即 unmount host-tasks),其他会话目录在 shell 物理视图里不存在——跨会话取旧成果只剩 core 进程一个口子。校验单点 `resolvePastPath`(形状+存在+同用户,承自 recall 旧 files/link);list_directory 单层 ls(≤200 条、隐藏项不列,@ 模式条目带 '@<sid>/' 全形前缀可直接抄进 copy_path);copy_path 源可 @、目标拒 @(历史只读,导入落本会话后 read_file/vision/mint 全量生效),预算 200 文件/200MB,符号链接拒收防逃逸(mint 同款防线);2026-10 给 read_file 开了只读窥看特例:跨墙不变量从「数据过墙只走 copy_path 一道门」放宽为「查看可过墙(list/read 文本),字节落地仍只走 copy_path」,写侧 write_file/edit_file 显式拒 @(否则 resolveInTask 会把 @ 当相对路径、在本会话造出字面 @ 目录)。同批拆除 recall 的 files/link 两 op(沙箱外克隆必漂移:link 漏抄 mint 的 realpath 防线),recall 收敛为纯记忆 search/days/day。仍移除:`create_directory` / `delete_path` / `move_path` / `find_path` / `grep`(写删检索继续走 terminal);17→10 大盘不变,复活两件+后续 db/profile → 现 15 项 |
|
||||
| 已移除的工具(部分复活) | `list_directory` / `copy_path` 于 2026-09 复活并加 `@<session-id>/<rel>` 只读记号:当年删除理由是「环境固定,统一走 terminal」,但生产 terminal 在远端 executor 容器(compose:wrapper 启动即 unmount host-tasks),其他会话目录在 shell 物理视图里不存在——跨会话取旧成果只剩 core 进程一个口子。校验单点 `resolvePastPath`(形状+存在+同用户,承自 recall 旧 files/link);list_directory 单层 ls(≤200 条、隐藏项不列,@ 模式条目带 '@<sid>/' 全形前缀可直接抄进 copy_path);copy_path 源可 @、目标拒 @(历史只读,导入落本会话后 read_file/vision/mint 全量生效),预算 200 文件/200MB,符号链接拒收防逃逸(mint 同款防线);2026-10 给 read_file 开了只读窥看特例:跨墙不变量从「数据过墙只走 copy_path 一道门」放宽为「查看可过墙(list/read 文本),字节落地仍只走 copy_path」,写侧 write_file/edit_file 显式拒 @(否则 resolveInTask 会把 @ 当相对路径、在本会话造出字面 @ 目录)。同批拆除 recall 的 files/link 两 op(沙箱外克隆必漂移:link 漏抄 mint 的 realpath 防线),recall 收敛为纯记忆 search/days/day。仍移除:`create_directory` / `delete_path` / `move_path` / `find_path` / `grep`(写删检索继续走 terminal);17→10 大盘不变,复活两件+后续 db/profile → 2026-10 随顾问模式(advising)拆除 handoff → 现 14 项 |
|
||||
| `terminal` | **唯一一个走 executor 的内置工具**(当 `rexec.isRemote(cfg)`);本地分支用 `spawn` + `detached` + 进程组 kill;远端分支 `rexec.run` SSE 收流,结果套上超时/截断/取消注解后 `truncate(text, cfg.terminal.max_output_chars)` 返回 |
|
||||
| `fetch` | `AbortController` 覆盖全生命周期(响应头之后的 body 读流同样被超时/取消打断,取消用与 terminal 同款的 200ms 轮询);content-type 先行,非文本只回状态/类型/尺寸不读 body;**下载/返回双上限解耦**:流式读到 `cfg.fetch.max_bytes`(默认 1MB)即断开(下载不占上下文可宽松),返回窗默认 `max_chars`(12000)字符,模型显式传 `max_chars` 夹在 `max_chars_hard`(64000)硬帽内;超窗尾部拼 `offset` 续读提示,转换后全文缓在 `ctx.run.fetchCache`(回合级 LRU 3 条),续读纯本地切片不重抓;**SSRF 防护**:主机名解析结果含回环/私有/链路本地地址即拒(`allow_private: true` 才放行),`redirect:'manual'` 逐跳复查至多 5 次(残余风险:undici 建连时二次解析,DNS 重绑定不可根治);按响应 charset 解码(full-icu 支持 gbk/gb18030 等,未知编码退回 UTF-8);HTML 用 `htmlToMarkdown` 转(剥 script/style/head 及 nav/header/footer/aside/form 整块,class/id 样板词启发式丢块,`<title>` 提成首行);HTML 实体单遍解码(常见命名实体+数字实体通吃,修 `&` 双重解码);字节截断切在多字节字符中时尾 U+FFFD 去噪;网络错误解包 `err.cause` 直述 ENOTFOUND/TLS 等真因,SSRF/重定向类错误带 `fetchUserError` 标记直接冒泡;engine 不截工具结果,返回值末尾另有 `truncate` 1MB 字符兜底护栏 |
|
||||
| `mint_public_link` | **唯一的 mint 入口**:与 core 同进程,直接 `publicFiles.mint()`,不绕任何 HTTP;`ctx.taskDir` 就是会话目录,`relOf` 的结果正是取用侧要的 `rel`。校验:`public_link.base_url` 已配 + `resolveInTask` 沙箱 + 确认是文件 + `realpath` 未沿符号链接跳出任务目录;返回单行 URL |
|
||||
| `skill` | op 分派 list/get/run(三件套合并:同一 `name` 标识空间 + 同一生命周期,见 `docs/tool-design.md` §一);委托 `tools/skills.js`;`input_args` 仅 op=run 且可缺省 `{}`,缺参由各技能 `input_schema` 校验点名报错;front matter 带 `visit_url` 的技能在 op=run 成功后由 executor 层拼 `public_link.base_url`(与 mint 同源)追加完整「访问地址」行(技能进程不知公网前缀,AI 拿相对路径会原样转给用户) |
|
||||
| `schedule` | **当前会话**(`ctx.sessionId`,AI 不可指定他会话)定时任务的完整生命周期:op 分派 get/create/update/delete(表主键 session_id → 每会话至多一条,无 id 参数);条件必填在代码里校验、错误回显 op 与缺失字段;数据层在 `scheduler.js`(cron 可解析 → 间隔 ≥1h → prompt ≤20000 → 查重/存在性检查),delete 不落 scheduler_note;描述里限定「仅用户明确要求时调用」(安全缓解,见 §5.12) |
|
||||
| `profile` | 用户画像增量维护(op 分派:list / set(honorific\|industry 单值≤20字,空串清除) / add / remove(preference 顿号标签));user 取自会话行,AI 不可指定他人;护栏单标≤15字×≤24个×总≤300字,整批原子(任一超限不落库);停用(enabled=0)拒写不拒读;写入自新会话生效(快照冻结语义不变),list 永远给 live 状态——取代了原 handoff 落槌一次性提取链路 |
|
||||
| `profile` | 用户画像增量维护(op 分派:list / set(honorific\|industry 单值≤20字,空串清除) / add / remove(preference 顿号标签));user 取自会话行,AI 不可指定他人;护栏单标≤15字×≤24个×总≤300字,整批原子(任一超限不落库);停用(enabled=0)拒写不拒读;写入自新会话生效(快照冻结语义不变),list 永远给 live 状态——取代了早期的画像一次性提取链路 |
|
||||
| `lab` | 元工具统一入口:`tool` + 自由 `args` 转发内置 + MCP 全部能力(目标 schema 定义期不可知,条件校验归内层 handler);`help` 伪工具做现场发现(无参列名单、带 `args.tool` 出完整 schema),未知名回显全清单,禁嵌套、内层错误冒泡不包装。默认被全局 `tools_deny: ['lab']` 屏蔽,仅 `lab` profile(allow `[lab]` + deny `[]` 清空)下发,见 §5.8 |
|
||||
|
||||
`resolveInTask(taskDir, relOrAbs)`:把相对路径或绝对路径解析为绝对路径并校验落在 `taskDir` 之内(含根),防穿越。
|
||||
@@ -419,7 +397,7 @@ AI 通过 `schedule` 工具为所在会话建周期性任务(`scheduled_tasks`
|
||||
- **形态**:`start(engine)` 注入 engine(避免 engine→executor→scheduler 顶层循环 require),`setInterval` 30s + `unref`,单次异常只 `log.error` 不中断定时器。周期/最小间隔是模块常量,不进 config.yml。
|
||||
- **cron**:`cron-parser@5.10.1`(锁版;v5 API 是 `CronExpressionParser.parse`,与 v4 的 `parseExpression` 不兼容),显式 `tz: 'Asia/Shanghai'`(与容器 TZ 一致,不靠隐式时区)。最小间隔 1h 在创建时校验。
|
||||
- **tick 两阶段**:A 收尾上一轮 `running`(runLoop 无结束回调,轮询推断:会话转非 active 即标 ok);B 取到期任务投递——`next_run_at <= now` 且非 running;**防堆积以会话状态为准**:active 直接标 `skipped` 不投递(比 DB 标记时序可靠)。
|
||||
- **投递 = 存档 + 强制 executing + postUserMessage**(顺序不可换):先把会话全部消息标 `type='archived'`(`buildMessages` 跳过 → AI 只看到本次 prompt,等价全新会话,上下文不随触发次数增长;UI 照常展示全量),再把 `phase` 强制置 `executing`(无人值守的半夜 prompt 不能被顾问 profile 当白天聊天「引导澄清等人回答」),最后 `engine.postUserMessage`——新调度下恒立即开跑(key 不独占不排队,任何状态都拿模型直接跑;绑定失效自动改选);仅模型池整体为空会投递失败标 `error`,下个周期重试。
|
||||
- **投递 = 存档 + postUserMessage**(顺序不可换):先把会话全部消息标 `type='archived'`(`buildMessages` 跳过 → AI 只看到本次 prompt,等价全新会话,上下文不随触发次数增长;UI 照常展示全量),然后 `engine.postUserMessage`——新调度下恒立即开跑(key 不独占不排队,任何状态都拿模型直接跑;绑定失效自动改选);仅模型池整体为空会投递失败标 `error`,下个周期重试。
|
||||
- **补跑语义**:`computeNext` 一律以 `now` 为基准,停机跨过多个触发点后重启只算未来一次,天然不补跑;重启后任务从库自动恢复。
|
||||
- **安全**:本功能等价给 AI 开持久化后门(prompt injection 可诱使建任务定时执行任意指令)。缓解三件:工具描述限定「仅用户明确要求时调用」+ 会话页常驻状态条(点击弹详情窗,可见 cron/上次下次时间/提示词,能就地改提示词或删除)+ 创建/触发/跳过/删除全打 `scheduler` info 日志。无权限分级/审批/配额。
|
||||
- **边界**:两次触发之间的用户对话也会被下次触发一并存档(UI 可见、AI 下次看不到,符合设计意图);多用户:调度器全局遍历不按 user 过滤(投递目标是会话本身,隔离在建任务时的 ctx.sessionId 已有);`last_status='running'` 的收尾不精确(异常结束的回合也会被标 ok)。
|
||||
@@ -432,7 +410,7 @@ AI 通过 `schedule` 工具为所在会话建周期性任务(`scheduled_tasks`
|
||||
|
||||
- **模型**:一条 `wechat_bindings` = 一个 core 用户 = 一个 bot 实例(一人一绑,换号先解绑)。binding id 三用:实例控制/投递的寻址键、wxbot 凭证目录名、channel 键(`channel='wx:'+id`,因此命令层、会话自动新建、投递游标查询全部白借 §5.4 的 channel 机制,零新逻辑)。iLink 协议保证扫码登录后仅本人可与 bot 单线对话,无陌生人入口,故无联系人授权面。
|
||||
- **方向三条线**:① Web(设置页→账户→微信内联卡)→ `/api/wechat` → wxbot `/api/instances*`(实例控制;QR SVG 代理内联渲染,只在 has_qr 就绪后取码,服务 token 不出内网);② wxbot 收信 → `POST core:/wxhook/inbox` → 直调 `channel.handleSend()`(文本;image/video/file 媒体先落会话任务目录:带说明文字的拼「用户上传了文件」告知行+正文并进一回合,单发媒体只补一行 `wx_media` 留痕不触发回合(下次真实对话模型自然看到,「刚才那张图」指代不断),同 web 附件链路;voice 走 iLink 服务端自动转写(msg.text 即识别文字),按文本类转发——命令回执同步回文本;普通消息不回执,等异步投递);③ core 总线监听 assistant_message/done → 按回合回复预算分配投递(见下两条),分段(~1800 字/段,行优先聚合)→ `POST wxbot:/api/deliver` → `bot.send`;wxbot 出口 markdown 默认透传(微信已支持渲染;env `WXBOT_STRIP_MARKDOWN=1` 恢复 stripMarkdown 纯文化降级);tool_call/tool_result 与 reasoning_delta(思考流,只进心跳不进台账)事件另发 `/api/typing` sendTyping 心跳(每绑定 2s 冷却、失败静默),长工具/思考回合里微信持续显示「正在输入」,文本投递后自动取消;同一对事件还记账工具台账(见下段)。
|
||||
- **渠道上下文**(ensureChannelContext,普通/媒体/带尾随文本的 /new 收信时幂等确保,先于首条消息入库):微信会话直接落 executing(跳过顾问 handoff,与定时任务同理由)+ 注入一条 `channel_note` user 行(微信环境说明:单线纯文本、给链接即交付卡片、入站文件已存工作目录等,AI 第一回合即知);/clear 删光历史后下条消息自动重注入。
|
||||
- **渠道上下文**(ensureChannelContext,普通/媒体/带尾随文本的 /new 收信时幂等确保,先于首条消息入库):注入一条 `channel_note` user 行(微信环境说明:单线纯文本、给链接即交付卡片、入站文件已存工作目录等,AI 第一回合即知);/clear 删光历史后下条消息自动重注入。
|
||||
- **出站文件(链接→卡片)**:sendAttachments 扫正文 `/p/<22位id>` 链 → 同进程 `publicFiles.resolve` + 归属硬校验(只转本会话签发的链接,防外流)→ 本地读字节(单文件≤20MB、每条回复最多 5 个)→ `POST wxbot:/api/deliver_media` → SDK 按扩展名自动路由 image/video/file 卡片;尽力而为:失败只 warn,链接文字留在正文作兜底(AI 无需感知卡片机制,channel_note 已告知「给链接即交付」)。
|
||||
- **投递游标** `last_msg_id` 落库 + 每绑定串行队列:重启不重发旧回复,wxbot 短暂不可达不丢(下一条连带补齐);errAck/纯工具轮(空文本)不进微信,`__blocks__` 图消息降级为占位文案。markdown 的出口处理在 wxbot:默认透传(微信已支持渲染),`WXBOT_STRIP_MARKDOWN=1` 时 stripMarkdown 纯文化。
|
||||
- **回合回复预算**(配额在 iLink 上游、不可查,core 记账分配):回复额度挂在入站消息的 context token 上,每轮约 10 条(分段与附件卡片各占 1),AI 多迭代中间回复 + 长结论天然撞上限,故不能按位置丢消息。策略:handleInbox 每条入站重置预算(命令回执占 1);带 `tool_calls` 的 assistant_message 必然是过程消息,直发至多 `PROC_HEAD_LIMIT=1` 条且投完仍给结论留 `CONCLUSION_RESERVE=4` 条,超出的并入结论开头「已省略 N 条过程」注记(长任务进展靠 typing 心跳);不带 `tool_calls` 的挂起到 `done` 事件确认是收尾结论再保底投递——预算够全文+卡片;不够但 ≥2 降级为「截断头 + 全文落会话 `微信回复全文-<id>.md` 转文件卡片」(内容零丢失);<2 放弃并 warn(网页端完整可见)。状态全内存按 binding 记,调度器触发的回合无入站不重置(记账继续累积,与上游 token 真实剩余一致)。
|
||||
@@ -490,7 +468,7 @@ AI 通过 `schedule` 工具为所在会话建周期性任务(`scheduled_tasks`
|
||||
| 路径前缀 | 文件 | 主要端点 |
|
||||
|---|---|---|
|
||||
| `/api/sessions` | `routes/sessions.js` | `POST /` 创建、`GET /` 列表(分页 + 搜索 + user 隔离)、`GET /:id` 详情(含 `schedule` 字段,列表接口不带以免 N+1)、`PATCH /:id` 改名、`DELETE /:id?delete_dir=...`、`POST /:id/bind`(`model_id`=手动换绑:任意模型即点即换、仅正在输出拒绝;原 profile 会话模式绑定已废弃)、`DELETE /:id/schedule`(删定时任务,只落 `scheduler_note` 通知消息不触发 AI)、`PATCH /:id/schedule`(改提示词/频次,cron 经 validateCron 重校验并重算 next_run_at) |
|
||||
| `/api/sessions/:id/messages` | `routes/messages.js` | `POST /` 发消息(body 可选 `deepThink`;响应 `delivered` 恒 `'live'`(排队已废弃);原 `profile` 参数已废弃)、`POST /stop`、`POST /release`(held→completed,纯状态动作、无资源可释放)、`DELETE /messages`(清空 + 回 advising)、`POST /summarize`(压缩,phase 中性)、`DELETE /messages/:mid`、`GET /stream` SSE 事件流 |
|
||||
| `/api/sessions/:id/messages` | `routes/messages.js` | `POST /` 发消息(body 可选 `deepThink`;响应 `delivered` 恒 `'live'`(排队已废弃);原 `profile` 参数已废弃)、`POST /stop`、`POST /release`(held→completed,纯状态动作、无资源可释放)、`DELETE /messages`(清空)、`POST /summarize`(压缩)、`DELETE /messages/:mid`、`GET /stream` SSE 事件流 |
|
||||
| `/api/sessions/:id/files` | `routes/files.js` | `POST /` 上传(multer,50MB/文件,文件名清洗 sanitizeName)、`GET /` 列表(递归)、`GET /download[-name/...]` 签名下载(5 分钟 TTL)、`GET /zip-token` + `GET /zip[-name/...]` 选中项(含文件夹)打包 zip 流式下载(纯 Node STORE,两遍读:先 CRC 后磁盘流发送)、`DELETE /` 删文件(目录递归删) |
|
||||
| `/api/skills` | `routes/skills.js` | 技能列表/创建/重命名/删除 + 技能内文件 CRUD + 上传(`?overwrite=true` 覆盖) |
|
||||
| `/api/config` | `routes/config.js` | `GET /` 原文+生效(脱敏)、`POST /validate`、`PUT /` 整文件保存、`PATCH /` 单项修改(path/value/op[delete\|append]) |
|
||||
@@ -511,13 +489,12 @@ AI 通过 `schedule` 工具为所在会话建周期性任务(`scheduled_tasks`
|
||||
|
||||
| event | data | 触发时机 |
|
||||
|---|---|---|
|
||||
| `status` | `{session_id, status, phase, model_id}` | 状态变化时;SSE 连接建立时立即发一次 |
|
||||
| `status` | `{session_id, status, model_id}` | 状态变化时;SSE 连接建立时立即发一次 |
|
||||
| `assistant_delta` | `{text}` | 模型流式文本 |
|
||||
| `reasoning_delta` | `{text}` | 模型流式思考(`reasoning`/`reasoning_content`) |
|
||||
| `assistant_message` | `{id, content, reasoning, tool_calls, type, is_summary, mode}` | 整轮 assistant 落库后(含 `type='errAck'` 标识) |
|
||||
| `tool_call` | `{id, name, arguments}` | 即将执行工具前 |
|
||||
| `tool_result` | `{tool_call_id, name, content, is_error, image_url, msg_id}` | 工具回执落库后 |
|
||||
| `phase` | `{phase}` | 阶段切换(advising↔executing;`status` 事件也携带 phase) |
|
||||
| `done` | `{status: 'held'\|'cancelled'\|'completed'}` | 回合收尾 |
|
||||
| `title` | `{title}` | 首次交互结束后自动生成的标题落库时(生成流程见 §9) |
|
||||
| `error` | `{message}` | 异常 |
|
||||
@@ -528,7 +505,7 @@ AI 通过 `schedule` 工具为所在会话建周期性任务(`scheduled_tasks`
|
||||
|
||||
`app/server/public/` 是 H5 SPA(无构建步骤,本地 `vendor/marked.min.js` 渲染 Markdown,不引外部 CDN)。`index.html` 只包含 HTML/CSS 与按序加载的 6 个经典脚本(`<script src>`,无 import/打包):顶层 `function` 声明仍挂 window 供内联 `onclick` 调用,顶层 `const/let` 进全局词法环境跨脚本可见;每文件各自 `'use strict'`。拆自旧版单一内联脚本,**纯搬运零改动,切分均在顶层语句边界**:
|
||||
|
||||
- `js/core.js` —— 架构注释头、通用工具(`$`/`esc`/`req`/`toast`/`copyText`/`fmtTime`/markdown 渲染)、MODES、全局 `state`、视图/模式切换、头部阶段徽标(仅 advising 显灯泡小图标,SSE `phase`/`status` 驱动)。
|
||||
- `js/core.js` —— 架构注释头、通用工具(`$`/`esc`/`req`/`toast`/`copyText`/`fmtTime`/markdown 渲染)、MODES、全局 `state`、视图/模式切换、头部标题渲染(点击重命名)。
|
||||
- `js/chat.js` —— 消息渲染(气泡/思考面板/工具卡)、历史懒加载分页、滚动跟随与回底、typing。
|
||||
- `js/sessions.js` —— 会话列表(抽屉)、打开/新建/重命名/删除、SSE 静默看门狗与后台挂起、phase-bar、定时任务、上下文压缩。
|
||||
- `js/stream.js` —— 发送、SSE 流式渲染(delta 合帧/工具早卡)、消息复制、全屏查看器。
|
||||
@@ -546,7 +523,7 @@ AI 通过 `schedule` 工具为所在会话建周期性任务(`scheduled_tasks`
|
||||
详见 `app/config.example.yml`(core 专属;字段与 `server/config.js` 的 `DEFAULTS` 一一对应;顶层 `config.yml` 由 `scripts/init.sh` 生成或手建,**已 gitignore**)。要点:
|
||||
|
||||
- `platforms[].keys[].models[]` —— 三层层级。内部唯一键 = `平台名/模型id`(用于历史会话回绑、用量统计),**改名等于重建其下所有模型**。
|
||||
- `model_profiles`(顶层)—— 多套可复用的 prompt+tools 组合(`{tools_allow?, tools_deny?, system_prompt_file?}`);模型层用 `profile: <名>` 引用,不配 = 默认全量(仍受顶层 `tools_deny` 屏蔽)。内置 `advising`/`lab`/`zero` 三套默认组合,可用同名覆盖;`advising` 不经模型引用——会话处于顾问阶段时强制命中(见 §4.1 phase 状态机)。元工具自测/纯文本场景用,详见 §5.8;运行期手动切换已全部移除(消息级参数与会话绑定均废弃)。
|
||||
- `model_profiles`(顶层)—— 多套可复用的 prompt+tools 组合(`{tools_allow?, tools_deny?, system_prompt_file?}`);模型层用 `profile: <名>` 引用,不配 = 默认全量(仍受顶层 `tools_deny` 屏蔽)。内置 `lab`/`zero` 两套默认组合,可用同名覆盖。元工具自测/纯文本场景用,详见 §5.8;运行期手动切换已全部移除(消息级参数与会话绑定均废弃)。
|
||||
- `agent.max_tool_iterations`(默认 100)—— 单轮迭代上限(含工具调用),到顶自动停止并提示用户继续输入。
|
||||
- `terminal.max_output_chars`(默认 10000)—— AI 看到的命令输出上限;executor 侧另有 `EXECUTOR_MAX_OUTPUT_CHARS=200000` 兕底。
|
||||
- `fetch.timeout_ms`(默认 30000)/ `fetch.max_bytes`(默认 1MB)—— 网页抓取的全生命周期超时与流式抓取**字节**上限(超限即断开;下载不占上下文,保持宽松)。
|
||||
@@ -555,7 +532,7 @@ AI 通过 `schedule` 工具为所在会话建周期性任务(`scheduled_tasks`
|
||||
- `skills.skills_root` —— 缺省取 `workspace_root` 的同级 `skills/`。
|
||||
- ~~`model_hold`~~ —— 已废弃(旧版保持租约:held 到期自动释放);新调度下 key 不独占、不排队,held 不再到期,字段仅为旧配置解析兼容保留。
|
||||
- 上下文压缩 —— 无配置项:压缩回合沿用当前 profile 的 system prompt(无 profile = 默认模板)+ 正常历史,末尾追加一条 user(固定模板 `server/templates/prompt-compress.txt` 原文),该回合不传 tools;调整压缩策略直接改该文件。
|
||||
- 会话标题自动生成 —— 无配置项:每回合结束(`finalize` 成功分支)过门槛才触发,防「你好」开局把题定了:①本轮真实执行过工具(`run.toolWorked`,errAck 批不算)即立题;②零工具纯聊由历史现推(无计数器,重启/压缩免疫):非注入 user 正文行(type IS NULL,[CTX] 各段与 task_brief 天然排除)≥3 条且总字数 ≥30。触发后用会话当前 model_id 发一次**不带 system、不传 tools** 的标题请求(历史 user/assistant 原文,注入行不进——`llm.titleMessages` 白名单只留 null/summary/task_brief;末尾追加模板 `server/templates/prompt-title.txt` 作 user,`{{language}}` 填 `agent.language`);结果清洗后写入 `sessions.title` 并 SSE 推 `title` 事件。失败静默(title 仍空,下个回合结束自动重试);已有标题(建会话时填写、handoff 顾问自写或手动重命名 title_locked)不覆盖——阶段切换的「过程题→任务书题」升级由 toolHandoff 的 title 参数直写完成,非自动重生成。
|
||||
- 会话标题自动生成 —— 无配置项:每回合结束(`finalize` 成功分支)过门槛才触发,防「你好」开局把题定了:①本轮真实执行过工具(`run.toolWorked`,errAck 批不算)即立题;②零工具纯聊由历史现推(无计数器,重启/压缩免疫):非注入 user 正文行(type IS NULL,[CTX] 各段与 task_brief 天然排除)≥3 条且总字数 ≥30。触发后用会话当前 model_id 发一次**不带 system、不传 tools** 的标题请求(历史 user/assistant 原文,注入行不进——`llm.titleMessages` 白名单只留 null/summary/task_brief;末尾追加模板 `server/templates/prompt-title.txt` 作 user,`{{language}}` 填 `agent.language`);结果清洗后写入 `sessions.title` 并 SSE 推 `title` 事件。失败静默(title 仍空,下个回合结束自动重试);已有标题(建会话时填写或手动重命名 title_locked)不覆盖。
|
||||
- `auth.users` —— 明文密码配在配置里(自用场景);`secret` 留空则随机生成(重启掉登录态,建议固定随机串)。
|
||||
- `mcp_servers[].enable: false` —— 启动不加载该服务(配置保留),其余工具不受影响。
|
||||
- `mcp_servers[].timeout_ms` —— 该服务单次 `tools/call` 的最大等待,默认 60000(夹在 1s~10min);工具发现/握手用固定 15s,不受本项影响。
|
||||
|
||||
Reference in New Issue
Block a user