fix(panel): 改模型时优先空闲会话并对宿主调用加超时

This commit is contained in:
qyh15 2026-11-10 12:19:56 +08:00
parent bad8c26835
commit 963ec5821a
1 changed files with 18 additions and 6 deletions

View File

@ -1286,16 +1286,25 @@ button.sl-stat-members:focus-visible{outline:2px solid color-mix(in srgb,var(--s
return session && typeof session.selectModel === "function" ? session : undefined;
};
/** 改模型时按这个顺序找执行者:部门自己 > 其它会话 > 丞相(只当执行上下文,不投递消息)。 */
/** 改模型时按这个顺序找执行者:部门自己 > 空闲会话 > 其它会话 > 丞相(只当执行上下文,不投递消息)。 */
const routeCandidates = (preferredSessionId) => {
const lead = currentSession();
const list = [];
if (preferredSessionId) list.push(preferredSessionId);
const busy = [];
const idle = [];
try {
const snapshot = sessions.list && typeof sessions.list.getSnapshot === "function" ? sessions.list.getSnapshot() : undefined;
const ids = (snapshot && snapshot.ids) || [];
for (const id of ids) if (id !== preferredSessionId && id !== lead) list.push(id);
const byId = (snapshot && snapshot.byId) || {};
for (const id of ids) {
if (id === preferredSessionId || id === lead) continue;
const running = Boolean(byId[id] && byId[id].running);
(running ? busy : idle).push(id);
}
} catch (error) { /* 忽略 */ }
const list = [];
if (preferredSessionId) list.push(preferredSessionId);
for (const id of idle) list.push(id);
for (const id of busy) list.push(id);
if (lead && list.indexOf(lead) < 0) list.push(lead);
return list;
};
@ -1564,9 +1573,11 @@ button.sl-stat-members:focus-visible{outline:2px solid color-mix(in srgb,var(--s
const attempt = (index, lastError) => {
if (index >= candidates.length) return Promise.resolve({ ok: false, error: lastError || "没有可执行这条指令的会话" });
const agent = candidates[index];
return Promise.resolve()
.then(() => commands.execute(agent, line, []))
// 宿主可能在忙(会话正在跑一轮)时把指令排进队列而不返回,界面就会永远停在「自动保存中…」。
const call = withDeadline(Promise.resolve().then(() => commands.execute(agent, line, [])), 3000, () => TIMED_OUT);
return call
.then((result) => {
if (result === TIMED_OUT) return attempt(index + 1, "宿主正忙,没在 3 秒内回应(模型没改,稍后再试)");
if (result && result.ok === false) {
const failure = result.error;
const detail = failure
@ -1713,6 +1724,7 @@ button.sl-stat-members:focus-visible{outline:2px solid color-mix(in srgb,var(--s
const EMPTY_SESSIONS = { byId: {}, projectionsBySession: {}, ids: [], phase: "ready" };
const TIMED_OUT = Symbol("timeout");
/** 给可能永远不 settle 的宿主 Promise 加超时,超时后走 fallback,避免界面卡在「读取中」。 */
function withDeadline(promise, ms, fallback) {
return new Promise((resolve) => {