From 7f8cf64e88b03e9124f7938b1520efaface2c1f0 Mon Sep 17 00:00:00 2001 From: kicer Date: Fri, 25 Sep 2026 11:54:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E9=98=B6=E6=AE=B5=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=9C=BAadvising/executing(=E5=8D=95=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E5=8D=95=E9=97=AE=E9=A2=98:=E5=85=88=E8=81=8A?= =?UTF-8?q?=E6=B8=85=E5=86=8D=E5=B9=B2=E6=B4=BB):=E6=96=B0=E5=BB=BA?= =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E4=B8=8E=E3=80=8C=E6=B8=85=E7=A9=BA=E3=80=8D?= =?UTF-8?q?=E8=90=BDadvising=E2=80=94=E2=80=94resolveProfileName=E4=B8=A4?= =?UTF-8?q?=E6=A1=A3=E5=8C=96(phase=3Dadvising=E5=BC=BA=E5=88=B6=E5=91=BD?= =?UTF-8?q?=E4=B8=AD=E5=86=85=E7=BD=AEadvising=E7=BB=84=E5=90=88,=E5=90=A6?= =?UTF-8?q?=E5=88=99=E9=9A=8F=E6=A8=A1=E5=9E=8B=E9=85=8D=E7=BD=AEprofile,t?= =?UTF-8?q?ools=5Fallow=E5=8F=AA=E7=95=99read=5Ffile/fetch/write=5Ffile?= =?UTF-8?q?=E3=80=81=E4=B8=8D=E5=B8=A6skill=E6=95=85=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=97=A0=E6=8A=80=E8=83=BD=E6=B8=85=E5=8D=95=E6=AE=B5,?= =?UTF-8?q?=E9=85=8Dadvising-system.txt:=E9=97=AE=E7=AD=94=E5=88=A4?= =?UTF-8?q?=E6=84=8F=E5=9B=BE=E5=BC=95=E5=AF=BC=E6=BE=84=E6=B8=85=E3=80=81?= =?UTF-8?q?=E6=AF=8F=E8=BD=AE=E8=87=B3=E5=A4=9A1-2=E9=97=AE=E9=98=B2?= =?UTF-8?q?=E8=BF=87=E5=BA=A6=E8=BF=BD=E9=97=AE);=E5=94=AF=E4=B8=80?= =?UTF-8?q?=E5=87=BA=E5=8F=A3=E6=98=AF=E4=BB=BB=E5=8A=A1=E4=B9=A6=E6=AD=A3?= =?UTF-8?q?=E9=97=A8=E2=80=94=E2=80=94runLoopInner=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E6=89=B9=E8=B7=91=E5=AE=8C=E5=90=8E,advising=E5=9B=9E=E5=90=88?= =?UTF-8?q?=E5=86=85write=5Ffile/edit=5Ffile=E6=88=90=E5=8A=9F=E8=90=BD?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=9B=AE=E5=BD=95=E6=A0=B9task.md(argsHitTas?= =?UTF-8?q?kBrief,=E4=B8=8A=E4=BC=A0=E5=90=8C=E5=90=8D=E4=B8=8Eterminal?= =?UTF-8?q?=E9=97=B4=E6=8E=A5=E5=88=9B=E5=BB=BA=E4=B8=8D=E7=AE=97;?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=9C=AB=E5=B0=BE=E7=BB=9F=E4=B8=80=E5=88=87?= =?UTF-8?q?,=E9=98=B2=E7=88=B6assistant=E5=85=88=E5=AD=98=E6=A1=A3?= =?UTF-8?q?=E5=90=8E=E8=90=BD=E5=BA=93=E5=9B=9E=E6=89=A7=E6=88=90=E5=AD=A4?= =?UTF-8?q?=E5=84=BF=E8=A1=8C=E8=A2=AB=E4=B8=8A=E6=B8=B8=E6=8B=92=E6=94=B6?= =?UTF-8?q?)=E5=8D=B3enterExecuting:=E5=85=A8=E5=8F=B2=E6=A0=87archived+?= =?UTF-8?q?=E6=8F=92type=3Dtask=5Fbrief=E7=A3=81=E7=9B=98=E5=BF=AB?= =?UTF-8?q?=E7=85=A7=E8=A1=8C=E4=BD=9C=E6=89=A7=E8=A1=8C=E9=98=B6=E6=AE=B5?= =?UTF-8?q?=E5=94=AF=E4=B8=80=E4=B8=8A=E4=B8=8B=E6=96=87=E8=B5=B7=E7=82=B9?= =?UTF-8?q?+phase=E7=BF=BB=E8=BD=AC=E4=B8=8ESSE=20phase=E5=B9=BF=E6=92=AD,?= =?UTF-8?q?=E4=B8=8B=E4=B8=80=E8=BF=AD=E4=BB=A3=E9=87=8D=E8=AF=BB=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E9=87=8D=E8=A7=A3profile=E5=A4=A9=E7=84=B6=E5=88=87?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E7=BB=84=E5=90=88=E3=80=81=E5=90=8C=E5=9B=9E?= =?UTF-8?q?=E5=90=88=E5=BC=80=E5=B7=A5(=E5=B9=82=E7=AD=89:=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E4=B8=AD=E5=86=8D=E6=94=B9task.md=E5=8F=AA=E6=98=AF?= =?UTF-8?q?=E6=99=AE=E9=80=9A=E7=BC=96=E8=BE=91);=E5=9B=9E=E5=A4=B4?= =?UTF-8?q?=E8=B7=AF=E5=8F=AA=E6=9C=89=E6=B8=85=E7=A9=BA(archiveTaskBrief?= =?UTF-8?q?=E6=8A=8A=E6=97=A7task.md=20mv=E8=BF=9B.task-history/task-.?= =?UTF-8?q?md=E9=98=B2=E6=B1=A1=E6=9F=93=E6=96=B0=E8=A7=84=E5=88=92,?= =?UTF-8?q?=E7=82=B9=E7=9B=AE=E5=BD=95=E6=B2=BF=E7=94=A8.imgcache=E9=9A=90?= =?UTF-8?q?=E5=BC=8F=E7=BA=A6=E5=AE=9A);=E5=8E=8B=E7=BC=A9=E5=AF=B9phase?= =?UTF-8?q?=E4=B8=AD=E6=80=A7(=E5=8E=8B=E5=AE=8C=E5=9B=9E=E5=8E=9F?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F,task=5Fbrief=E4=B8=8D=E8=B1=81=E5=85=8D?= =?UTF-8?q?=E5=AD=98=E6=A1=A3,=E6=BC=82=E7=A7=BB=E5=8F=AF=E6=8E=A7?= =?UTF-8?q?=E5=9B=A0=E5=8E=8B=E7=BC=A9=E6=9C=AC=E5=B0=B1=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=B8=BB=E5=AF=BC);=E5=AE=9A=E6=97=B6=E6=8A=95=E9=80=92?= =?UTF-8?q?=E5=89=8D=E5=BC=BA=E5=88=B6executing(=E6=97=A0=E4=BA=BA?= =?UTF-8?q?=E5=80=BC=E5=AE=88=E4=B8=8D=E8=83=BD=E8=A2=AB=E9=A1=BE=E9=97=AE?= =?UTF-8?q?=E8=AF=9D=E6=9C=AF=E5=8D=A1=E4=BD=8F);db=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E5=AD=98=E9=87=8Fphase=E5=9B=9E=E5=A1=ABexecuting=E9=9B=B6?= =?UTF-8?q?=E6=89=93=E6=89=B0=E3=80=81sessions.profile=E5=88=97=E5=BA=9F?= =?UTF-8?q?=E5=BC=83=E4=B8=8D=E8=AF=BB=E5=86=99;=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=9C=9F=E6=89=8B=E5=8A=A8=E5=88=87profile=E5=85=A8=E6=8B=86(?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=BA=A7=E5=8F=82=E6=95=B0/bind=E7=9A=84prof?= =?UTF-8?q?ile=E5=88=86=E6=94=AF/setSessionProfile=E4=B8=8EhasModelProfile?= =?UTF-8?q?/status=E7=9A=84model=5Fprofiles=E5=90=8D=E5=8D=95/bind?= =?UTF-8?q?=E5=BC=B9=E7=AA=97=E6=A8=A1=E5=BC=8F=E5=8C=BA),=E5=A4=B4?= =?UTF-8?q?=E9=83=A8=E5=BE=BD=E6=A0=87=E6=9C=BA=E4=BD=8D=E5=A4=8D=E7=94=A8?= =?UTF-8?q?=E4=B8=BA=E9=98=B6=E6=AE=B5=E5=BE=BD=E6=A0=87(=E4=BB=85advising?= =?UTF-8?q?=E6=98=BE=E7=81=AF=E6=B3=A1,SSE=20phase=E9=A9=B1=E5=8A=A8,?= =?UTF-8?q?=E8=BD=AC=E6=89=A7=E8=A1=8Ctoast=E5=8F=8D=E9=A6=88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config.example.yml | 4 + app/server/config.js | 3 + app/server/db.js | 11 +- app/server/engine.js | 131 +++++++++++++++++------ app/server/public/index.html | 18 ++-- app/server/public/js/chat.js | 2 +- app/server/public/js/core.js | 29 ++--- app/server/public/js/sessions.js | 59 ++-------- app/server/public/js/stream.js | 9 ++ app/server/routes/messages.js | 8 +- app/server/routes/sessions.js | 22 +--- app/server/routes/system.js | 5 +- app/server/scheduler.js | 3 + app/server/templates/advising-system.txt | 51 +++++++++ docs/ARCHITECTURE.md | 47 +++++--- 15 files changed, 245 insertions(+), 157 deletions(-) create mode 100644 app/server/templates/advising-system.txt diff --git a/app/config.example.yml b/app/config.example.yml index 8926601..7255dd0 100644 --- a/app/config.example.yml +++ b/app/config.example.yml @@ -9,7 +9,11 @@ platforms: [] tools_deny: [lab] # 模型 profile:prompt + tools 名单组合,模型用 profile: <名> 引用(不配 = 全量)。 +# 内置 advising 不靠引用:会话处于 advising 阶段(顾问澄清需求/规划)时强制命中,见 server/engine.js resolveProfileName。 model_profiles: + advising: # 顾问阶段:只留问答/调研/写任务书工具,配专属提示词 + tools_allow: [read_file, fetch, write_file] + system_prompt_file: advising-system.txt lab: # 统一入口自测:tools 只有 lab,其余能力经 lab(help) 现场发现 tools_allow: [lab] tools_deny: [] # 必须显式清空全局黑名单,否则 lab 自己也被 deny → 零工具 diff --git a/app/server/config.js b/app/server/config.js index f81d72d..a4df752 100644 --- a/app/server/config.js +++ b/app/server/config.js @@ -96,6 +96,9 @@ const DEFAULTS = { tools_deny: ['lab'], // 模型 profile(prompt + tools 名单组合):platforms 里的模型用 profile: <名> 引用。 model_profiles: { + // advising:顾问阶段(sessions.phase)专用组合——不由模型配置引用,而是 resolveProfileName 在 + // advising 时强制命中:只留问答/调研/写任务书所需工具(terminal 不进名单),配专用顾问提示词模板。 + advising: { tools_allow: ['read_file', 'fetch', 'write_file'], system_prompt_file: 'advising-system.txt' }, // lab:统一入口元工具自测组合——tools 收缩到 1 个,其余能力经 lab(help) 现场发现。 // tools_deny 必须显式清空:不配 = 继承全局 ['lab'],deny 后叠加会把 lab 自己禁掉 → 零工具。 lab: { tools_allow: ['lab'], tools_deny: [], system_prompt_file: 'lab-system.txt' }, diff --git a/app/server/db.js b/app/server/db.js index 3554403..bb7e44d 100644 --- a/app/server/db.js +++ b/app/server/db.js @@ -14,7 +14,7 @@ function initDb(dbPath) { id TEXT PRIMARY KEY, title TEXT DEFAULT '', status TEXT NOT NULL, - phase TEXT NOT NULL DEFAULT 'planning', + phase TEXT NOT NULL DEFAULT 'advising', model_id TEXT, last_model_id TEXT, assistant_type TEXT DEFAULT 'secretary', @@ -143,11 +143,16 @@ function initDb(dbPath) { if (!cols.some(c => c.name === 'user')) { db.exec("ALTER TABLE sessions ADD COLUMN user TEXT DEFAULT ''"); } - // 迁移:profile 记会话绑定的 prompt+tools 组合名(config.yml 顶层 model_profiles;''=随模型配置)。 - // 选取优先级见 engine.resolveProfileName,切换入口见 routes/sessions.js bind 路由(弹窗「模式」区)。 + // 迁移:profile 曾记会话绑定的 prompt+tools 组合名(bind 弹窗「模式」区)。已废弃: + // 前端手动切换模式全部移除,profile 选取收敛为「phase 强制 + 模型配置默认」两档(见 engine.resolveProfileName); + // 列保留(SQLite 删列不值得),代码不再读写。 if (!cols.some(c => c.name === 'profile')) { db.exec("ALTER TABLE sessions ADD COLUMN profile TEXT DEFAULT ''"); } + // 迁移: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')"); return db; } diff --git a/app/server/engine.js b/app/server/engine.js index 5b9b95f..d398547 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -241,15 +241,13 @@ function registerProfilePrompts() { setProfilePrompts(map); } -// 本轮生效的 profile 名:每消息参数(run.profile,与 deepThink 同约定:每轮迭代取最新值) -// > 会话绑定(sessions.profile,弹窗「模式」区写入,跨回合/刷新持久)> 模型配置默认(cfg.models[].profile) -// > ''(默认全量)。保留值 'default' 归一为 ''(显式摆脱模型默认组合;无 profile 模型下二者等效)。 -function resolveProfileName(session, run) { - const manual = run && String(run.profile || '').trim(); - if (manual) return manual === 'default' ? '' : manual; - const bound = String((session && session.profile) || '').trim(); - if (bound) return bound === 'default' ? '' : bound; - const m = session.model_id && (cfg.models || []).find(x => x.id === session.model_id); +// 本轮生效的 profile 名(两档):advising 阶段强制命中内置 'advising' 组合(顾问只该看到顾问的工具); +// 否则 = 模型配置默认(cfg.models[].profile)> ''(默认全量)。 +// 运行期手动切换已全部移除(消息级 profile 参数、sessions.profile 会话绑定均已废弃), +// 顾问/执行的节奏由 sessions.phase 单一状态机控制,见 enterExecuting / archiveTaskBrief。 +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) : ''; } @@ -274,8 +272,75 @@ function effectiveTools(profileName) { return out; } -function hasModelProfile(name) { - return !!(name && cfg.model_profiles && cfg.model_profiles[String(name)]); +// ---------- 阶段(sessions.phase:advising 顾问 / executing 执行) ---------- +// advising:新建会话与「清空」的落点。profile 被 resolveProfileName 强制为 'advising', +// 只问答/判意图/引导澄清;唯一出口是 write_file/edit_file 把任务书 task.md 落在任务目录根(正门判定)。 +// executing:一路走到底,只有清空才回 advising;压缩不碰 phase(压完仍回原模式)。 + +// 写类工具(write_file/edit_file)的参数是否恰好落在任务目录根的 task.md——阶段切换的正门判定。 +// resolveInTask 统一解相对/绝对路径并拦越界;子目录 task.md 不算。 +// 不扫文件存在性:用户随时上传同名素材不算承诺,只有 AI 亲自写一次才算(上传也永远覆盖不了 +// task.md:files.js 同名自动加后缀)。 +function argsHitTaskBrief(taskDir, args) { + try { + const abs = path.resolve(resolveInTask(taskDir, String((args && args.path) || ''))); + return abs === path.resolve(taskDir, 'task.md'); + } catch (_) { return false; } +} + +// 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', + content: '【任务书】需求澄清完毕,会话已进入执行模式。以下任务书快照(task.md)是唯一目标依据;' + + '之前的顾问对话已存档、不再发送。执行中可修订 task.md(以文件现状为准);' + + '上一轮任务书归档在 .task-history/,仅限用户明确要求时查阅。\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)); + } } function enqueue(sessionId) { @@ -330,17 +395,7 @@ function bindToSession(sessionId, modelId) { return { ok: true, model_id: want }; } -// 会话级 profile 绑定(bind 弹窗「模式」区):''=解绑回随模型配置;保留值 'default' 归一为 ''。 -// 不设会话状态门槛——模式是每轮选择,切换下一轮迭代/下条消息即生效;不碰模型槽位占用。 -// 名字存在性由路由校验(hasModelProfile,与 messages 路由同款 400),这里只管落库。 -function setSessionProfile(sessionId, profile) { - const s = getSession(sessionId); - if (!s) throw Object.assign(new Error('会话不存在'), { status_code: 404 }); - const name = profile === 'default' ? '' : String(profile || '').trim(); - db.prepare('UPDATE sessions SET profile = ?, updated_at = ? WHERE id = ?').run(name, now(), sessionId); - log.info('model', sessionId + ' 会话模式绑定 → ' + (name || '默认(随模型配置)')); - return name; -} + // 是否还有待回应的新用户消息(最后一条非 tool 消息是 user)。 function hasPendingUser(sessionId) { @@ -593,7 +648,7 @@ async function runLoopInner(sessionId, run) { const compressing = !!run.summarizing; // profile(prompt+tools 组合):每迭代现解,前端切模式后下一个请求即生效; // 压缩回合同样跟随(小模型会话点「压缩」若回退默认大模板,当场撑爆)。 - const profileName = resolveProfileName(session, run); + const profileName = resolveProfileName(session); const messages = compressing ? compressMessages(session, (cfg.agent || {}).language, mintImageUrl, profileName) : buildMessages(session, (cfg.agent || {}).language, mintImageUrl, profileName); @@ -703,6 +758,7 @@ async function runLoopInner(sessionId, run) { // 执行工具 const vision = modelVision(session); + let taskBriefWritten = false; // 本批工具里是否有「正门落 task.md」(批量跑完统一切换,见循环尾注释) for (const tc of msgRow.tool_calls) { if (run.cancelled) break; const id = tc.id; @@ -755,7 +811,15 @@ async function runLoopInner(sessionId, run) { image_url: imageRel ? '/api/sessions/' + encodeURIComponent(sessionId) + '/files/download?path=' + encodeURIComponent(imageRel) : undefined, msg_id: toolMsgId, }); + // 阶段切换正门:advising 回合里 write_file/edit_file 成功且路径恰为根 task.md → 标记待切换。 + // 不在这里就地切:同批可能还有其它 tool_calls,其父 assistant 行若先被存档、 + // 后落库的回执就成了无主孤儿行(上游拒收)——整批跑完再统一重建上下文。 + if (!isError && (tc.function.name === 'write_file' || tc.function.name === 'edit_file') + && argsHitTaskBrief(taskDirOf(sessionId), chk.args)) { + taskBriefWritten = true; + } } + if (taskBriefWritten) enterExecuting(sessionId); iter++; } if (iter >= maxIter && !run.cancelled) { @@ -927,8 +991,9 @@ 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, assistant_type, user, created_at, updated_at) VALUES (?,?,'queued','planning',NULL,?,?,?,?)" + "INSERT INTO sessions (id, title, status, phase, model_id, assistant_type, user, created_at, updated_at) VALUES (?,?,'queued','advising',NULL,?,?,?,?)" ).run(id, title || '', assistantType || 'secretary', user || '', now(), now()); + // 新会话一律落 advising(顾问模式):先聊天判意图,任务书 task.md 落地才转 executing(见 enterExecuting) // 指定了模型且其平台恰好有空闲 key → 直接绑定,跳过排队;否则回退到 FIFO 队列 const prefer = preferModelId && platformModels(platformOf(preferModelId)).includes(preferModelId) ? freeKeyOf(platformOf(preferModelId)) @@ -947,18 +1012,16 @@ function createSession(title, preferModelId, assistantType, user) { return session; } -// 用户消息(或“开始执行”的模板消息)入口。 -// opts.deepThink: true 时启用深度思考;opts.profile: 本轮 prompt+tools 组合名(model_profiles, -// 空 = 随模型配置,'default' = 显式全量);参数都记在会话 run 上, -// 正在跑的回合下一轮迭代会取到最新值。新建会话需在 runLoop 之前先预建 run 以携带该值。 +// 用户消息入口。 +// opts.deepThink: true 时启用模型该「深度思考」参数(记在会话 run 上,正在跑的回合下一轮迭代会取到最新值)。 +// 新建会话需在 runLoop 之前先预建 run 以携带该值。 function postUserMessage(sessionId, content, opts) { const session = getSession(sessionId); if (!session) throw Object.assign(new Error('会话不存在'), { status_code: 404 }); const messageId = saveMessage(sessionId, { role: 'user', content }); - // 预建/更新 run 记录,记下本轮的 deepThink/profile(被 runLoop / callModel 读取) + // 预建/更新 run 记录,记下本轮的 deepThink(被 runLoop / callModel 读取) const run = getRun(sessionId, true); run.deepThink = !!(opts && opts.deepThink); - run.profile = String((opts && opts.profile) || '').trim(); // 真实用户消息到达:若还有个「已排队但尚未开跑」的压缩回合(见 summarizeContext), // 让它作废,避免新消息被误当成压缩回合处理;已在跑(looping)的不动, // 由其收尾时自行清空 summarizing。 @@ -1142,6 +1205,8 @@ function maybeAutoCompress(sessionId, run, wasCompressTurn) { // 清空会话的对话内容:删除该会话的全部消息记录,但保留会话本身与任务目录文件。 // 状态收尾为 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 }); @@ -1158,12 +1223,14 @@ function clearSessionMessages(sessionId) { dequeue(sessionId); releaseModel(sessionId); db.prepare('DELETE FROM messages WHERE session_id = ?').run(sessionId); + archiveTaskBrief(sessionId); // 旧任务书先归档(mv),新一轮 advising 不会被上一问题的约定污染 autoCompressProposed.delete(sessionId); // 历史清零,恢复自动压缩提议额度 // 标题随历史一并清零:旧标题描述的是已删除的对话,留着会让新对话文不对题; // 新一轮交互结束后 maybeGenerateTitle 见 title 为空会自动重新生成。 - touch(sessionId, { status: 'completed', completed_at: now(), cancelled_at: null, held_at: null, title: null }); + touch(sessionId, { status: 'completed', completed_at: now(), cancelled_at: null, held_at: null, title: null, phase: 'advising' }); bus.emit(sessionId, 'done', { status: 'completed' }); bus.emit(sessionId, 'title', { title: null }); // 供其他打开同一会话的窗口同步清掉旧标题 + bus.emit(sessionId, 'phase', { phase: 'advising' }); emitStatus(sessionId); dispatchQueued(); return { ok: true }; @@ -1249,8 +1316,6 @@ module.exports = { modelUsageStats, dispatchQueued, bindToSession, - setSessionProfile, TOOL_DEFINITIONS, effectiveTools, - hasModelProfile, }; diff --git a/app/server/public/index.html b/app/server/public/index.html index ef02698..ad4462c 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -54,7 +54,7 @@ /* 状态栏「压缩」图标按钮:主操作色(蓝色),与其他 stop-link 区分(终止/选择模型是灰) */ .stop-link.icon-only.icon-primary { color: var(--active); } .stop-link.icon-only.icon-primary:hover { color: #2854d6; } - /* 推到 phase-bar 最右侧(独立动作,与左侧状态区分;「选择模型/模式」与压缩同列右靠) */ + /* 推到 phase-bar 最右侧(独立动作,与左侧状态区分;「选择模型」与压缩同列右靠) */ .phase-bar .phase-bar-right { margin-left: auto; } /* 定时任务状态条:整条可点,弹详情窗(改频次/提示词、删除;创建走 AI 的 schedule 工具),紫色调区别于状态栏; 左侧 3px 竖条与侧边栏定时任务会话的紫色标记同一语义。 @@ -88,16 +88,10 @@ margin-bottom: 10px; background: #fff; color: var(--ink); resize: vertical; } #schedule-modal textarea:focus { border-color: var(--active); } - /* 会话模式徽标(profile 绑定时的标题小图标;尺寸显式定,不依赖 .ic 默认值) */ + /* 会话阶段徽标(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; } - /* 模式徽标统一用主题蓝(lab/zero 同色,靠形状区分) */ - .prof-badge.prof-lab .ic, .prof-badge.prof-zero .ic { color: var(--active); } - /* 弹窗模式行:下拉选中(取代纵向选项组,省垂直空间) */ - .msw-prof { display: flex; align-items: center; gap: 6px; padding: 10px 12px; font-size: 13px; color: var(--ink); border-top: 1px solid #dde1ea; } - .msw-prof-label { flex: none; line-height: 30px; } - .msw-prof select { flex: 1; min-width: 0; height: 30px; box-sizing: border-box; border: 1px solid #dde1ea; border-radius: 8px; padding: 0 8px; font-size: 13px; line-height: 28px; background: #fff; color: var(--ink); } - /* welcome 头像兼做「选择模型/模式」入口(与聊天态的「选择模型」同一弹窗) */ + .prof-badge .ic { width: 13px; height: 13px; color: var(--active); } + /* welcome 头像兼做「选择模型」入口(与聊天态的「选择模型」同一弹窗) */ .logo-badge { cursor: pointer; }