feat(session): held 超时自动收口 + 状态胶囊模型名点击换绑
1) engine 新增 held sweeper:超过 agent.auto_complete_ms(默认 30min,0=关)自动 held→completed,纯状态动作不清绑定,SSE 与手动断开同链路;扫描间隔随阈值自适应 (生产≤60s,测试短阈值秒级)。示例配置同步。 2) 前端保持中/已终止时胶囊里的模型名可直接点击,复用选择模型弹窗换绑; 该场景下隐藏冗余的独立「选择模型」按钮(completed 等无模型名场景保留)。
This commit is contained in:
@@ -50,6 +50,7 @@ agent:
|
||||
max_retries: 2 # 上游失败重试次数
|
||||
language: "Simplified Chinese" # 强制回复语言;留空不限
|
||||
auto_compress_tokens: 60000 # In token 超此值弹「自动压缩」提议;0=关
|
||||
auto_complete_ms: 1800000 # 保持中(held)超此时长自动收口为已完成;0=关(纯状态动作,历史/模型绑定不动)
|
||||
|
||||
upload:
|
||||
image_compress_threshold_kb: 1536 # 大图压缩提示阈值(KB)
|
||||
|
||||
@@ -80,7 +80,9 @@ const DEFAULTS = {
|
||||
// fetch:timeout_ms 覆盖请求发出到 body 读完的全生命周期;max_bytes 为流式抓取字节上限(超限即断开)。
|
||||
fetch: { timeout_ms: 30000, max_bytes: 1048576 },
|
||||
// auto_compress_tokens:本轮 In token 峰值 ≥ 此值时推送「自动压缩」提议(0=关)。
|
||||
agent: { temperature: 1.0, max_tool_iterations: 100, max_retries: 2, language: 'Simplified Chinese', auto_compress_tokens: 60000 },
|
||||
// auto_complete_ms:held(保持中)超过此时长自动收口为 completed(0=关)。key 非独占后 held 不占任何资源,
|
||||
// 收口是纯视觉动作:历史/绑定不动,再发言照常由 completed 分支续跑(见 engine.sweepHeldTimeouts)。
|
||||
agent: { temperature: 1.0, max_tool_iterations: 100, max_retries: 2, language: 'Simplified Chinese', auto_compress_tokens: 60000, auto_complete_ms: 1800000 },
|
||||
// 大图上传预处理(浏览器端 canvas 压缩,参数经 /api/system/status 下发前端)。
|
||||
upload: { image_compress_threshold_kb: 1536, image_max_dim: 2000, image_quality: 0.85, confirm_size_mb: 10 },
|
||||
// 技能根目录:留空取 workspace_root 同级 skills(/workspace/tasks → /workspace/skills)。
|
||||
|
||||
@@ -164,6 +164,43 @@ function syncModelPool() {
|
||||
// 下次发消息时由 ensureBinding 自动改选(优先留原平台),用户无感。
|
||||
db.prepare('UPDATE model_pool SET is_busy = 0, current_session_id = NULL').run(); // 旧版独占列清残留(调度已不读)
|
||||
db.prepare("UPDATE sessions SET status='held', held_at=COALESCE(held_at, updated_at), updated_at=? WHERE status IN ('active','queued')").run(now());
|
||||
startHeldSweeper();
|
||||
}
|
||||
|
||||
// ---------- held 自动收口(保持中 → 已完成) ----------
|
||||
// held 自「key 共享非独占」后不再持有任何资源(旧 model_hold 租约已废弃,见 config.js 注释),
|
||||
// 只是给用户留着「还能追问」的视觉信号;但长期挂着会显得会话永不收口,出口只剩手动「断开」。
|
||||
// 超时翻 completed 是零行为影响的纯状态动作:历史与模型绑定都不动,用户再发言走
|
||||
// postUserMessage 的 completed 分支照常原模型续跑。
|
||||
// 护栏:阈值远大于 AUTO_COMPRESS_GRACE_MS,不会挤掉自动压缩提议的确认窗口(提议弹出后用户随时可确认,
|
||||
// 收口不拦 /summarize);判据只看 status(held 定义上不在输出中,抢先发言已把状态翻离 held,不会误收);
|
||||
// held_at 为 NULL(无从判时长的存量数据)跳过;agent.auto_complete_ms = 0 关闭。
|
||||
// 扫描间隔随阈值自适应:生产默认 30 分钟封顶 60s;测试把阈值调小即可获得分钟级以下的扫描节奏。
|
||||
// 运行期改了 auto_complete_ms 不会重调间隔,重启生效(阈值本身每次 tick 重读,支持热关/热改)。
|
||||
const HOLD_SWEEP_MIN_MS = 1000, HOLD_SWEEP_MAX_MS = 60000;
|
||||
let holdSweeper = null;
|
||||
function startHeldSweeper() {
|
||||
if (holdSweeper) return; // init 重复调用幂等
|
||||
const th = Math.floor(Number((cfg.agent || {}).auto_complete_ms) || 0);
|
||||
if (th <= 0) return; // 关闭态不建定时器
|
||||
const interval = Math.max(HOLD_SWEEP_MIN_MS, Math.min(HOLD_SWEEP_MAX_MS, Math.floor(th / 4)));
|
||||
holdSweeper = setInterval(sweepHeldTimeouts, interval);
|
||||
if (holdSweeper.unref) holdSweeper.unref(); // 同 scheduler:常驻轮询不阻止进程退出
|
||||
}
|
||||
function sweepHeldTimeouts() {
|
||||
try {
|
||||
const ms = Math.floor(Number((cfg.agent || {}).auto_complete_ms) || 0);
|
||||
if (ms <= 0) return;
|
||||
const rows = db.prepare("SELECT id FROM sessions WHERE status='held' AND held_at IS NOT NULL AND held_at <= ?").all(now() - ms);
|
||||
for (const r of rows) {
|
||||
touch(r.id, { status: 'completed', completed_at: now(), held_at: null });
|
||||
bus.emit(r.id, 'done', { status: 'completed' }); // 与手动 releaseHold 同链路,前端胶囊/列表自然收起
|
||||
emitStatus(r.id);
|
||||
}
|
||||
if (rows.length) log.info('engine', 'held 超时自动收口 ' + rows.length + ' 个会话(阈值 ' + ms + 'ms)');
|
||||
} catch (e) {
|
||||
log.warn('engine', 'held 收口扫描异常: ' + (e && e.message || e));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 模型池调度(优先级选择,无排队、无独占) ----------
|
||||
|
||||
@@ -833,6 +833,9 @@ footer { flex: none; padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0
|
||||
.status-pill.held { background: #fff4dc; color: #8a6400; }
|
||||
.status-pill.queued { background: #e9edff; color: var(--active); }
|
||||
.status-pill.cancelled { background: #fdecea; color: #b3382c; }
|
||||
/* 状态胶囊里的模型名 = 「更换模型」入口(held/cancelled 下可点,与独立「选择模型」同一弹窗,见 sessions.js updatePhaseBar) */
|
||||
.status-pill .pill-model { cursor: pointer; border-bottom: 1px dashed currentColor; }
|
||||
.status-pill .pill-model:hover { opacity: .7; }
|
||||
@keyframes pill-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }
|
||||
/* 微信绑定内联卡(账户页):登录码用服务端 SVG 内联渲染(非 <img>,无坏图帧);未就绪时占位不跳高 */
|
||||
.wx-qr { display: flex; justify-content: center; align-items: center; margin: 10px 0 4px; min-height: 236px; }
|
||||
|
||||
@@ -259,13 +259,19 @@
|
||||
bar.classList.remove('hidden');
|
||||
const pill = $('status-pill');
|
||||
const stMap = { active: '活跃中', held: '保持中', completed: '已完成', cancelled: '已终止' };
|
||||
let text = stMap[state.status] || state.status || '--';
|
||||
const statusText = stMap[state.status] || state.status || '--';
|
||||
// 持有绑定的态都附带当前模型:key 非独占、无租约,绑定跨结版持续可见;
|
||||
// 仅手动「断开」(held→completed)收起展示,「终止」不算断开(绑定仍在,再发言沿用)
|
||||
if (d && d.model_id && state.status !== 'completed') {
|
||||
text += ' · ' + d.model_id;
|
||||
// held/cancelled 下模型名即「更换模型」入口(复用选择弹窗),输出中(active)只展示不给点;
|
||||
// completed 收走展示(手动断开/超时长自动收口),再发言仍沿用原绑定
|
||||
const showModel = !!(d && d.model_id && state.status !== 'completed');
|
||||
const switchable = showModel && state.status !== 'active';
|
||||
if (switchable) {
|
||||
pill.innerHTML = esc(statusText) + ' · <span class="pill-model" title="点击更换模型" onclick="openModelSwitch()">' + esc(d.model_id) + '</span>';
|
||||
} else if (showModel) {
|
||||
pill.innerHTML = esc(statusText) + ' · ' + esc(d.model_id);
|
||||
} else {
|
||||
pill.textContent = statusText;
|
||||
}
|
||||
pill.textContent = text;
|
||||
// 状态胶囊配色:活跃中绿(执行中)、保持中黄(已答完、保持模型中)等;状态栏整体同步染色
|
||||
pill.className = 'status-pill ' + (state.status || '');
|
||||
bar.className = 'phase-bar ' + (state.status || '');
|
||||
@@ -275,9 +281,10 @@
|
||||
btn.classList.toggle('hidden', !actionable);
|
||||
btn.textContent = state.status === 'held' ? '断开' : '终止';
|
||||
btn.classList.toggle('muted', state.status === 'held');
|
||||
// 非输出中都可「选择模型」:key 不再独占/排队,任意模型即点即换(后端仅 looping 拒绝)
|
||||
// 「选择模型」独立按钮:胶囊里的模型名可点时(held/cancelled)不再重复给入口;
|
||||
// completed(无模型名)或事件未带 model_id 时仍靠它继续/换模;active 维持原逻辑隐藏
|
||||
const msBtn = $('model-switch-btn');
|
||||
if (msBtn) msBtn.classList.toggle('hidden', !(state.sessionId && state.status && state.status !== 'active'));
|
||||
if (msBtn) msBtn.classList.toggle('hidden', !(state.sessionId && state.status && !switchable && state.status !== 'active'));
|
||||
// 状态栏「压缩」按钮:active 时不允许(避免与正在跑的回合抢上下文);
|
||||
// 其余状态可点。未发起总结时可点;发起后按钮 disabled 防双击。
|
||||
const cpBtn = $('compress-btn');
|
||||
|
||||
Reference in New Issue
Block a user