0.5.7:模型目录读不到时说清是哪一步断了

This commit is contained in:
qyh15 2026-11-08 22:14:47 +08:00
parent b8e90e2c5b
commit 907805b02f
5 changed files with 152 additions and 26 deletions

View File

@ -2,6 +2,13 @@
> 版本号以 `package.json` 为准(当前 0.5.x 线);下面 0.6.x 条目沿用旧编号,保留作历史。
## 0.5.7 — 模型目录读不到时说清是哪一步断了
- 部门设置里的「当前宿主未提供模型目录」不再是一句万能提示。原来 `modelDirectoryOf` 把三种情况都吞成 `undefined`:取不到 `modelDirectories` 服务、`directoryFor` 方法不存在、`directoryFor(sessionId)` 抛异常。现在分别返回 `{ service: false }`、`{ error }`、`{ directory }`。
- 宿主的 `ModelDirectoryResolver.directoryFor` 在该会话没登记时抛 `ui-model-selection: session "<id>" resolved no scope / no binding`。这种情况不再报「宿主未提供模型目录」,而是**借另一个已登记会话的目录把全局模型清单列出来**:列表照常可选看,`current` 留空不冒充别人的选择,保存按钮禁用并写明「进入该部门会话后才能改模型」。其它异常(服务崩溃、超时、快照读取失败)原样带出原文,同时用 `[三省六部]` 前缀在控制台留下会话 id 与原始错误。
- 新增 `sessionsOtherThan`(最近活跃会话 → 任意已登记会话)做兜底;`loadCatalog` 的投影收敛成一个 `project()`,成功/超时/兜底三条路径共用,`unresolved` 随结果返回给界面。
- 新增 4 项测试:服务缺失、会话未登记时兜底且不冒充当前选择(含 `selectModel` 拒绝)、其它异常原样带出、正常路径。`node --test` 43 项、`node --check`、`git diff --check`、`check-install` 全过。
## 0.5.6 — 左栏那一行改名「上朝」并置顶
- 按用户要求把左侧栏导航行的标签从「三省六部」改成「上朝」,`order` 从 10 改为 −100,排在「插件」以外所有插件行之前(`插件` 恒列首行,是宿主写死的)。

View File

@ -1,6 +1,6 @@
{
"name": "dsh-sansheng-liubu-mode",
"version": "0.5.6",
"version": "0.5.7",
"private": true,
"description": "上朝:丞相统筹的三省六部多 Agent 模式",
"type": "module",

View File

@ -675,7 +675,7 @@ button.sl-stat-members:focus-visible{outline:2px solid color-mix(in srgb,var(--s
setCatalog((prev) => ({ ...prev, status: "loading", error: null }));
host.loadCatalog(targetId).then((result) => {
if (!alive) return;
setCatalog({ status: "ready", options: result.options, current: result.current, error: result.error, subagent: result.subagent });
setCatalog({ status: "ready", options: result.options, current: result.current, error: result.error, subagent: result.subagent, unresolved: result.unresolved === true });
setDraftModel(result.current ? result.current.provider + "/" + result.current.model : "");
setDraftEffort((result.current && result.current.reasoningEffort) || "");
}, (error) => {
@ -692,7 +692,7 @@ button.sl-stat-members:focus-visible{outline:2px solid color-mix(in srgb,var(--s
const currentModelValue = catalog.current ? catalog.current.provider + "/" + catalog.current.model : "";
const currentEffortValue = (catalog.current && catalog.current.reasoningEffort) || "";
const modelDirty = draftModel !== currentModelValue || draftEffort !== currentEffortValue;
const canSaveModel = Boolean(chosenOption && seat && seat.id && modelDirty && !catalog.subagent && !busy);
const canSaveModel = Boolean(chosenOption && seat && seat.id && modelDirty && !catalog.subagent && !catalog.unresolved && !busy);
const applyModel = () => {
if (!chosenOption || !seat || !seat.id) return;
@ -939,7 +939,7 @@ button.sl-stat-members:focus-visible{outline:2px solid color-mix(in srgb,var(--s
onClick: applyModel,
children: busy ? "保存中…" : "保存模型设置",
}),
h("small", { className: modelDirty ? "is-dirty" : "", children: catalog.subagent ? "子会话沿用丞相模型" : modelDirty ? "有未保存的更改" : "当前:" + (catalog.current ? modelLabel(catalog.current) : "未设置") }),
h("small", { className: modelDirty ? "is-dirty" : "", children: catalog.subagent ? "子会话沿用丞相模型" : catalog.unresolved ? "进入该部门会话后才能改模型" : modelDirty ? "有未保存的更改" : "当前:" + (catalog.current ? modelLabel(catalog.current) : "未设置") }),
]),
]),
catalog.error ? h("div", { className: "sl-error", children: catalog.error }) : null,
@ -1129,6 +1129,23 @@ button.sl-stat-members:focus-visible{outline:2px solid color-mix(in srgb,var(--s
} catch (error) { return undefined; }
};
/** 兜底读全局模型清单用的另一个会话:先找最近活跃的,再找任意已登记的。 */
const sessionsOtherThan = (sessionId) => {
const newest = newestSessionId();
if (newest && newest !== sessionId) return newest;
try {
const snapshot = sessionsStore && typeof sessionsStore.getSnapshot === "function" ? sessionsStore.getSnapshot() : undefined;
const byId = snapshot && snapshot.byId ? snapshot.byId : {};
for (const key of Object.keys(byId)) {
const item = byId[key];
if (!item || item.blank === true) continue;
const id = item.id || key;
if (id && id !== sessionId) return id;
}
} catch (error) { /* 忽略:没有可兜底的会话 */ }
return undefined;
};
/** 主视图当前选中的会话:以 uiWorkspace 的选中记录为准(面板是 root 级槽)。 */
const currentSession = () => {
try {
@ -1148,13 +1165,29 @@ button.sl-stat-members:focus-visible{outline:2px solid color-mix(in srgb,var(--s
} catch (error) { return undefined; }
};
/**
* 取模型目录服务与某会话的目录。三种结果必须分开,否则 «宿主没有这个服务» 会被
* 当成 «这个会话读不到目录»:{ service: false } 没有服务;{ error } 有服务但解析
* 不到该会话;{ directory } 成功。
*/
const modelDirectoryOf = (sessionId) => {
let service;
try { service = ctx.modelDirectories || (typeof ctx.get === "function" ? ctx.get("modelDirectories") : undefined); } catch (error) { return undefined; }
if (!service || typeof service.directoryFor !== "function") return undefined;
try { return service.directoryFor(sessionId); } catch (error) { return undefined; }
try {
service = ctx.modelDirectories || (typeof ctx.get === "function" ? ctx.get("modelDirectories") : undefined);
} catch (error) {
return { error: "读取模型目录服务失败:" + String((error && error.message) || error) };
}
if (!service || typeof service.directoryFor !== "function") return { service: false };
try {
return { directory: service.directoryFor(sessionId) };
} catch (error) {
return { error: String((error && error.message) || error) };
}
};
/** 宿主对没登记的会话抛的原话是 session "<id>" resolved no scope / no binding。 */
const unregisteredSession = (detail) => /resolved no (scope|binding)/.test(String(detail || ""));
const sidebarNav = () => {
const candidates = [];
try { if (typeof ctx.get === "function") candidates.push(ctx.get("sidebarRight")); } catch (error) { /* 忽略 */ }
@ -1304,37 +1337,67 @@ button.sl-stat-members:focus-visible{outline:2px solid color-mix(in srgb,var(--s
}));
},
loadCatalog(sessionId) {
const directory = modelDirectoryOf(sessionId);
if (!directory) return Promise.resolve({ options: [], current: undefined, error: "当前宿主未提供模型目录", subagent: false });
const cached = (message) => {
const subagentOf = (id) => (typeof sessions.subagentAddress === "function" ? sessions.subagentAddress(id) !== undefined : false);
/** 把目录快照投影成面板要的形状;keepCurrent: false 时不冒充该会话的当前选择。 */
const project = (directory, extra) => {
try {
const snapshot = directory.store.getSnapshot();
return {
options: modelOptions(snapshot),
current: snapshot.current,
error: message,
subagent: typeof sessions.subagentAddress === "function" ? sessions.subagentAddress(sessionId) !== undefined : false,
current: extra && extra.keepCurrent === false ? undefined : snapshot.current,
error: (extra && extra.error) || snapshot.error || null,
subagent: subagentOf(sessionId),
unresolved: Boolean(extra && extra.unresolved),
};
} catch (inner) {
return { options: [], current: undefined, error: message, subagent: false };
return {
options: [],
current: undefined,
error: (extra && extra.error) || "读取模型目录缓存失败:" + String((inner && inner.message) || inner),
subagent: subagentOf(sessionId),
unresolved: false,
};
}
};
const resolved = modelDirectoryOf(sessionId);
if (resolved.service === false) {
return Promise.resolve({ options: [], current: undefined, error: "当前宿主没有模型目录服务(modelDirectories)", subagent: false, unresolved: false });
}
if (!resolved.directory) {
const detail = resolved.error || "原因未知";
note("模型目录读取失败(" + sessionId + "):" + detail);
if (!unregisteredSession(detail)) {
return Promise.resolve({ options: [], current: undefined, error: "读取模型目录失败:" + detail, subagent: subagentOf(sessionId), unresolved: false });
}
// 该部门会话还没在本页登记:模型清单是全局的,先借另一个会话的目录把它列出来,
// 但不把别人的当前选择当成它的,也不允许在这里保存。
const sibling = sessionsOtherThan(sessionId);
const fallback = sibling ? modelDirectoryOf(sibling).directory : undefined;
if (!fallback) {
return Promise.resolve({ options: [], current: undefined, error: "该部门会话还没有在本页打开,也找不到别的会话读取模型清单;先进入该会话再试。", subagent: subagentOf(sessionId), unresolved: false });
}
return Promise.resolve()
.then(() => fallback.load())
.then(
() => project(fallback, { keepCurrent: false, unresolved: true, error: "该部门会话还没有在本页打开,这里只列出全局模型清单;进入该会话后才能修改它自己的模型。" }),
(error) => project(fallback, { keepCurrent: false, unresolved: true, error: "读取模型清单失败:" + String((error && error.message) || error) }),
);
}
const directory = resolved.directory;
const cached = (message) => project(directory, { error: message });
const chain = Promise.resolve()
.then(() => directory.load())
.then(() => {
const snapshot = directory.store.getSnapshot();
return {
options: modelOptions(snapshot),
current: snapshot.current,
error: snapshot.error || null,
subagent: typeof sessions.subagentAddress === "function" ? sessions.subagentAddress(sessionId) !== undefined : false,
};
}, (error) => cached(String(error && error.message || error)));
.then(() => project(directory), (error) => cached(String(error && error.message || error)));
return withDeadline(chain, 6000, () => cached("读取模型目录超时(显示已知缓存)"));
},
selectModel(sessionId, payload) {
const directory = modelDirectoryOf(sessionId);
if (!directory) return Promise.resolve({ ok: false, error: "当前宿主未提供模型目录" });
const resolved = modelDirectoryOf(sessionId);
if (resolved.service === false) return Promise.resolve({ ok: false, error: "当前宿主没有模型目录服务(modelDirectories)" });
if (!resolved.directory) {
const detail = resolved.error || "原因未知";
return Promise.resolve({ ok: false, error: unregisteredSession(detail) ? "该部门会话还没有在本页打开,先进入该会话再改它的模型。" : "读取模型目录失败:" + detail });
}
const directory = resolved.directory;
return Promise.resolve()
.then(() => directory.select(payload))
.then(() => {

View File

@ -1,6 +1,6 @@
{
"name": "dsh-sansheng-liubu-panel",
"version": "0.5.6",
"version": "0.5.7",
"private": true,
"description": "三省六部 Agent 面板:在 DSH Web GUI 中常驻显示并切换团队 Agent",
"type": "module",

View File

@ -787,3 +787,59 @@ test('会话头开关失败时不再回退到不存在的整页面板', () => {
expand(toggle.component({})).find((node) => node.type === 'button').props.onClick()
assert.equal(selections.includes(exports.PANEL_ID), false, '失败时不得 selectPanel 到无 main 槽的面板')
})
/** 模型目录相关用例:宿主面按用例替换 modelDirectories。 */
function catalogHost(overrides = {}) {
const { exports } = loadPlugin()
const context = mockContext()
Object.assign(context.ctx, overrides)
exports.apply(context.ctx)
return { ...context, host: exports.createHost(context.ctx) }
}
test('宿主没有模型目录服务时,错误要说清是服务缺失', async () => {
const { host } = catalogHost({ modelDirectories: undefined })
const result = await host.loadCatalog('lead-1')
assert.equal(result.options.length, 0)
assert.match(result.error, /没有模型目录服务/)
assert.notEqual(result.error, '当前宿主未提供模型目录', '不能再用那句笼统的误导文案')
})
test('会话没登记时借别的会话列出全局清单,但不冒充它的当前选择', async () => {
const calls = []
const snapshot = { current: { provider: 'lead-provider', model: 'lead-model' }, groups: [], failures: [], status: 'ready', error: null }
const { host } = catalogHost({
modelDirectories: {
directoryFor(sessionId) {
calls.push(sessionId)
if (sessionId === 'dept-9') throw new Error('ui-model-selection: session "dept-9" resolved no scope')
return { load: async () => {}, select: async () => {}, store: { getSnapshot: () => snapshot } }
},
},
})
const result = await host.loadCatalog('dept-9')
assert.equal(result.unresolved, true, '要标记成只读,界面才敢禁用保存')
assert.equal(result.current, undefined, '不能把兜底会话的当前模型当成这个部门的')
assert.match(result.error, /本页打开/)
assert.deepEqual(calls, ['dept-9', 'lead-1'], '先问目标会话,再借已登记的会话')
const saved = await host.selectModel('dept-9', { provider: 'lead-provider', model: 'lead-model' })
assert.equal(saved.ok, false)
assert.match(saved.error, /先进入该会话/)
})
test('模型目录读取抛别的错时原样带出,不再伪装成服务缺失', async () => {
const { host } = catalogHost({
modelDirectories: { directoryFor() { throw new Error('目录服务已崩溃') } },
})
const result = await host.loadCatalog('lead-1')
assert.match(result.error, /读取模型目录失败:目录服务已崩溃/)
assert.equal(result.unresolved, false)
})
test('模型目录正常时给出清单与当前选择', async () => {
const { host } = catalogHost()
const result = await host.loadCatalog('lead-1')
assert.equal(result.error, null)
assert.equal(result.unresolved, false)
assert.deepEqual(result.current, { provider: 'p', model: 'm' })
})