release: dsh-sansheng-liubu v0.5.1
This commit is contained in:
parent
4034a0d7cf
commit
97986b53dc
|
|
@ -18,10 +18,13 @@
|
|||
| `project` | `{ projectId }` | 项目视图(见下) |
|
||||
| `open` | `{ workspaceId, name?, path? }` | 项目视图;不存在则按 workspaceId 建项目记录(不创建会话) |
|
||||
| `enable` | `{ projectId }` | 项目视图;严格创建/恢复十席会话 |
|
||||
| `enableSeat` | `{ projectId, seatId }` | 项目视图;只开启一个部门(手动/派工按需) |
|
||||
| `openSeat` | `{ projectId, seatId }` | 项目视图 + `sessionId`;只核实并进入已绑定原会话,绝不创建 |
|
||||
| `rebuildSeat` | `{ projectId, seatId, expectedSessionId? }` | 项目视图 + `sessionId`;仅 missing 时重建一次,旧历史保留 |
|
||||
| `configure` | `{ projectId, seatId, model:({provider,model,reasoningEffort?}|null), instructions? }` | `{ seat }` |
|
||||
| `models` | — | `{ providers:[{ provider, models:[{ model, reasoningEffort?:[...] }] }], default:({provider,model}|null), failures:[...] }` |
|
||||
|
||||
错误码:`sansheng/invalid`、`sansheng/not-found`、`sansheng/conflict`、`sansheng/unbound`(席尚未绑定会话)、`sansheng/model-invalid`、`sansheng/state-corrupt`。
|
||||
错误码:`sansheng/invalid`、`sansheng/not-found`、`sansheng/conflict`、`sansheng/unbound`(席尚未绑定会话)、`sansheng/session-missing`(绑定会话已不存在,需用户确认重建)、`sansheng/session-check-failed`(暂无法确认会话,禁止自动重建)、`sansheng/model-invalid`、`sansheng/state-corrupt`。
|
||||
|
||||
## 项目视图
|
||||
|
||||
|
|
@ -41,6 +44,8 @@
|
|||
## 会话/模型规则(两端 UI 必须遵守)
|
||||
|
||||
- 每席一个独立 root Session;切换角色 = 导航到该席 `sessionId`,不新建、不重建聊天引擎。
|
||||
- 已有绑定必须先核实原会话:存在就进入原 session;被删除才标记 `missing`,由用户点“重建会话”确认后只创建一次。
|
||||
- 部门会话标题固定为两字角色名(丞相、中书、门下、尚书、吏部、户部、礼部、兵部、刑部、工部);新建/恢复/重建都通过宿主 `rename` 钉住标题,停止自动标题。
|
||||
- 模型未选择显示“请选择模型”;选择后显示具体名称。界面不提供“跟随宿主模型”选项。
|
||||
- “启用”只建立十个会话,不自动运行模型。
|
||||
- 角色补充说明:Web 可编辑;原生面板只读(原生无自由文本控件)。
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
# 三省六部(dsh-sansheng)· v0.5.0
|
||||
# 三省六部(dsh-sansheng)· v0.5.1
|
||||
|
||||
在 DSH 里为每个项目提供固定的十席角色(丞相、中书、门下、尚书、吏部、户部、礼部、兵部、刑部、工部),
|
||||
每席绑定一个独立的原生 root Session,模型与补充说明按项目独立保存。中间复用宿主原生会话,不替换聊天引擎。
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
- 项目共识:目标、硬约束、验收标准、决定与来源按版本保存,并注入部门提示词和派工上下文。
|
||||
- 部门工具:丞相用 `sansheng_dispatch_task` 派工到目标部门已绑定的真实会话;部门用 `sansheng_task_report` 回报执行中/完成/失败,结果关联真实部门会话;丞相可查询真实任务与结果,不能用同名子代理或普通 agent 冒充部门。
|
||||
- 任务记录区分待执行/已派工/执行中/已完成/已取消/失败,支持重复投递去重、取消后迟到结果留档(不改回取消状态)。
|
||||
- 打开部门对话只核实并进入原绑定会话,不会重复新建;会话被删除时标记「会话缺失」,由用户确认重建,旧历史保留;部门标题固定为两字角色名。
|
||||
|
||||
**本包尚未安装到任何 DSH profile,也未改动用户的日常 DSH 配置。**
|
||||
|
||||
|
|
|
|||
73
client.js
73
client.js
|
|
@ -534,7 +534,7 @@ function seatRecords(project) {
|
|||
return out;
|
||||
}
|
||||
function isBound(seat) {
|
||||
return !!seat && typeof seat.sessionId === "string" && seat.sessionId.length > 0;
|
||||
return !!seat && seat.status === "bound" && typeof seat.sessionId === "string" && seat.sessionId.length > 0;
|
||||
}
|
||||
function modelValue(model) {
|
||||
if (!model || typeof model !== "object") return "";
|
||||
|
|
@ -618,7 +618,7 @@ function selectedDetail(project, records, selectedSeatId, models) {
|
|||
buildModelSelect(project, seat, models),
|
||||
{
|
||||
type: "button",
|
||||
label: bound ? "进入" + meta.label + "会话" : meta.label + "尚未绑定会话",
|
||||
label: bound ? "进入" + meta.label + "会话" : seat.status === "missing" ? meta.label + "会话缺失,请到 Web 工作台重建" : meta.label + "尚未绑定会话",
|
||||
variant: bound ? "primary" : "ghost",
|
||||
disabled: !bound,
|
||||
action: {
|
||||
|
|
@ -1324,19 +1324,69 @@ function apply(ctx) {
|
|||
const pid = view && view.project ? view.project.id : null;
|
||||
if (pid) selectedSeatCache.set(pid, id);
|
||||
const seat = seatById[id];
|
||||
if (seat && seat.status !== "bound" && seat.status !== "creating") setNotice("「" + seat.name + "」还没开启,点下面的按钮开启这个部门");
|
||||
if (seat && seat.status === "missing") setNotice("「" + seat.name + "」的会话已不存在,点下面的「重建会话」可重新建立");
|
||||
else if (seat && seat.status !== "bound" && seat.status !== "creating") setNotice("「" + seat.name + "」还没开启,点下面的按钮开启这个部门");
|
||||
}
|
||||
function openSelectedSeat() {
|
||||
async function openSelectedSeat() {
|
||||
const projectId = view && view.project ? view.project.id : null;
|
||||
const seat = seatById[wb.selected.id];
|
||||
if (!seat) return;
|
||||
if (!projectId || !seat) return;
|
||||
if (seat.status === "missing") {
|
||||
setNotice("「" + seat.name + "」的会话已不存在,点下面的「重建会话」可重新建立");
|
||||
return;
|
||||
}
|
||||
if (seat.status !== "bound") {
|
||||
setNotice("「" + seat.name + "」还没开启,先点「开启『" + seat.name + "』」");
|
||||
return;
|
||||
}
|
||||
const rec = seatRecordOf(view, seat.id);
|
||||
if (rec && rec.sessionId) {
|
||||
navigateToSession(rec.sessionId);
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const result = await request("openSeat", { projectId, seatId: seat.id });
|
||||
if (!alive.current) return;
|
||||
if (!result || !result.sessionId) throw new Error("宿主没有返回会话");
|
||||
navigateToSession(result.sessionId);
|
||||
setNotice("已切换到" + seat.name + "会话");
|
||||
} catch (openError) {
|
||||
if (!alive.current) return;
|
||||
setError(openError && openError.message ? openError.message : "打开会话失败");
|
||||
if (openError && openError.code === "sansheng/session-missing") {
|
||||
try {
|
||||
const next = await request("project", { projectId });
|
||||
if (alive.current) setView(next);
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (alive.current) setBusy(false);
|
||||
}
|
||||
}
|
||||
async function rebuildSelectedSeat() {
|
||||
const projectId = view && view.project ? view.project.id : null;
|
||||
const seat = seatById[wb.selected.id];
|
||||
if (!projectId || !seat) return;
|
||||
const rec = seatRecordOf(view, seat.id);
|
||||
const expectedSessionId = rec && rec.sessionId ? rec.sessionId : void 0;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
setNotice("正在重建「" + seat.name + "」的会话…");
|
||||
try {
|
||||
const next = await request("rebuildSeat", { projectId, seatId: seat.id, expectedSessionId });
|
||||
if (!alive.current) return;
|
||||
setView(next);
|
||||
setNotice("已重建「" + seat.name + "」的会话;旧会话历史仍保留");
|
||||
} catch (rebuildError) {
|
||||
if (!alive.current) return;
|
||||
setError(rebuildError && rebuildError.message ? rebuildError.message : "重建会话失败");
|
||||
if (rebuildError && rebuildError.code === "sansheng/session-conflict") {
|
||||
try {
|
||||
const next = await request("project", { projectId });
|
||||
if (alive.current) setView(next);
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (alive.current) setBusy(false);
|
||||
}
|
||||
}
|
||||
function modelFromValue(value) {
|
||||
|
|
@ -1607,7 +1657,12 @@ function apply(ctx) {
|
|||
className: "ssw-primary",
|
||||
disabled: busy,
|
||||
onClick: openSelectedSeat
|
||||
}, "开始对话") : (0, import_react.createElement)("button", {
|
||||
}, "开始对话") : selectedStatus === "missing" ? (0, import_react.createElement)("button", {
|
||||
type: "button",
|
||||
className: "ssw-primary",
|
||||
disabled: busy,
|
||||
onClick: rebuildSelectedSeat
|
||||
}, "重建会话") : (0, import_react.createElement)("button", {
|
||||
type: "button",
|
||||
className: "ssw-primary",
|
||||
disabled: busy || selectedStatus === "creating",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "dsh-sansheng-liubu",
|
||||
"version": "0.5.0",
|
||||
"version": "0.5.1",
|
||||
"description": "三省六部:每个项目固定十席角色与独立会话的 DSH 插件(第一版)。",
|
||||
"keywords": [
|
||||
"dsh-plugin",
|
||||
|
|
|
|||
|
|
@ -428,21 +428,68 @@ export function apply(ctx) {
|
|||
const pid = view && view.project ? view.project.id : null
|
||||
if (pid) selectedSeatCache.set(pid, id)
|
||||
const seat = seatById[id]
|
||||
if (seat && seat.status !== 'bound' && seat.status !== 'creating') setNotice('「' + seat.name + '」还没开启,点下面的按钮开启这个部门')
|
||||
if (seat && seat.status === 'missing') setNotice('「' + seat.name + '」的会话已不存在,点下面的「重建会话」可重新建立')
|
||||
else if (seat && seat.status !== 'bound' && seat.status !== 'creating') setNotice('「' + seat.name + '」还没开启,点下面的按钮开启这个部门')
|
||||
}
|
||||
|
||||
// 只有明确点“开始对话”时才进入该部门的宿主会话。
|
||||
function openSelectedSeat() {
|
||||
// 只有明确点“开始对话”时才进入该部门的宿主会话;先用后端核实原会话存在,绝不新建。
|
||||
async function openSelectedSeat() {
|
||||
const projectId = view && view.project ? view.project.id : null
|
||||
const seat = seatById[wb.selected.id]
|
||||
if (!seat) return
|
||||
if (!projectId || !seat) return
|
||||
if (seat.status === 'missing') {
|
||||
setNotice('「' + seat.name + '」的会话已不存在,点下面的「重建会话」可重新建立')
|
||||
return
|
||||
}
|
||||
if (seat.status !== 'bound') {
|
||||
setNotice('「' + seat.name + '」还没开启,先点「开启『' + seat.name + '』」')
|
||||
return
|
||||
}
|
||||
const rec = seatRecordOf(view, seat.id)
|
||||
if (rec && rec.sessionId) {
|
||||
navigateToSession(rec.sessionId)
|
||||
setBusy(true); setError(null)
|
||||
try {
|
||||
const result = await request('openSeat', { projectId, seatId: seat.id })
|
||||
if (!alive.current) return
|
||||
if (!result || !result.sessionId) throw new Error('宿主没有返回会话')
|
||||
navigateToSession(result.sessionId)
|
||||
setNotice('已切换到' + seat.name + '会话')
|
||||
} catch (openError) {
|
||||
if (!alive.current) return
|
||||
setError(openError && openError.message ? openError.message : '打开会话失败')
|
||||
if (openError && openError.code === 'sansheng/session-missing') {
|
||||
try {
|
||||
const next = await request('project', { projectId })
|
||||
if (alive.current) setView(next)
|
||||
} catch { /* 刷新失败保留当前视图 */ }
|
||||
}
|
||||
} finally {
|
||||
if (alive.current) setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
// 只有后端确认旧会话不存在(missing)时,用户才能点这个按钮重建一次。
|
||||
async function rebuildSelectedSeat() {
|
||||
const projectId = view && view.project ? view.project.id : null
|
||||
const seat = seatById[wb.selected.id]
|
||||
if (!projectId || !seat) return
|
||||
const rec = seatRecordOf(view, seat.id)
|
||||
const expectedSessionId = rec && rec.sessionId ? rec.sessionId : undefined
|
||||
setBusy(true); setError(null); setNotice('正在重建「' + seat.name + '」的会话…')
|
||||
try {
|
||||
const next = await request('rebuildSeat', { projectId, seatId: seat.id, expectedSessionId })
|
||||
if (!alive.current) return
|
||||
setView(next)
|
||||
setNotice('已重建「' + seat.name + '」的会话;旧会话历史仍保留')
|
||||
} catch (rebuildError) {
|
||||
if (!alive.current) return
|
||||
setError(rebuildError && rebuildError.message ? rebuildError.message : '重建会话失败')
|
||||
if (rebuildError && rebuildError.code === 'sansheng/session-conflict') {
|
||||
try {
|
||||
const next = await request('project', { projectId })
|
||||
if (alive.current) setView(next)
|
||||
} catch { /* 刷新失败保留当前视图 */ }
|
||||
}
|
||||
} finally {
|
||||
if (alive.current) setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -686,10 +733,15 @@ export function apply(ctx) {
|
|||
type: 'button', className: 'ssw-primary', disabled: busy,
|
||||
onClick: openSelectedSeat,
|
||||
}, '开始对话')
|
||||
: h('button', {
|
||||
type: 'button', className: 'ssw-primary', disabled: busy || selectedStatus === 'creating',
|
||||
onClick: enableSelectedSeat,
|
||||
}, selectedStatus === 'creating' ? '正在开启…' : '开启「' + wb.selected.name + '」'),
|
||||
: selectedStatus === 'missing'
|
||||
? h('button', {
|
||||
type: 'button', className: 'ssw-primary', disabled: busy,
|
||||
onClick: rebuildSelectedSeat,
|
||||
}, '重建会话')
|
||||
: h('button', {
|
||||
type: 'button', className: 'ssw-primary', disabled: busy || selectedStatus === 'creating',
|
||||
onClick: enableSelectedSeat,
|
||||
}, selectedStatus === 'creating' ? '正在开启…' : '开启「' + wb.selected.name + '」'),
|
||||
)
|
||||
|
||||
const optionEls = wb.selected.modelOptions.map(option => h('option', { key: option.value, value: option.value }, option.label))
|
||||
|
|
|
|||
|
|
@ -34,6 +34,20 @@ export function createApiHandler(service) {
|
|||
if (!SEAT_IDS.includes(seatId)) throw bad(`未知席 ${String(seatId)}`)
|
||||
return { ok: true, value: await service.enableSeat(requireProjectId(payload), seatId) }
|
||||
}
|
||||
case 'openSeat': {
|
||||
const seatId = payload?.seatId
|
||||
if (!SEAT_IDS.includes(seatId)) throw bad(`未知席 ${String(seatId)}`)
|
||||
return { ok: true, value: await service.openSeat(requireProjectId(payload), seatId) }
|
||||
}
|
||||
case 'rebuildSeat': {
|
||||
const seatId = payload?.seatId
|
||||
if (!SEAT_IDS.includes(seatId)) throw bad(`未知席 ${String(seatId)}`)
|
||||
const expectedSessionId = payload?.expectedSessionId
|
||||
if (expectedSessionId !== undefined && expectedSessionId !== null && typeof expectedSessionId !== 'string') {
|
||||
throw bad('expectedSessionId 必须是字符串')
|
||||
}
|
||||
return { ok: true, value: await service.rebuildSeat(requireProjectId(payload), seatId, { expectedSessionId }) }
|
||||
}
|
||||
case 'configure': {
|
||||
const projectId = requireProjectId(payload)
|
||||
const seatId = payload?.seatId
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ function isNotFound(error) {
|
|||
|
||||
export function createProvisioner({ store, host, now = () => Date.now() }) {
|
||||
const locks = new Map()
|
||||
const titlesEnsured = new Set()
|
||||
|
||||
function withLock(key, fn) {
|
||||
const prev = locks.get(key) ?? Promise.resolve()
|
||||
|
|
@ -27,10 +28,14 @@ export function createProvisioner({ store, host, now = () => Date.now() }) {
|
|||
return guarded
|
||||
}
|
||||
|
||||
async function markBound(projectId, seatId, sessionId) {
|
||||
async function markBound(projectId, seatId, sessionId, previousSessionId = null) {
|
||||
await store.mutate(s => {
|
||||
const p = s.projects[projectId]
|
||||
const st = p.seats[seatId]
|
||||
if (previousSessionId && previousSessionId !== sessionId) {
|
||||
if (!Array.isArray(st.previousSessionIds)) st.previousSessionIds = []
|
||||
if (!st.previousSessionIds.includes(previousSessionId)) st.previousSessionIds.push(previousSessionId)
|
||||
}
|
||||
st.sessionId = sessionId
|
||||
st.status = 'bound'
|
||||
st.error = null
|
||||
|
|
@ -46,6 +51,18 @@ export function createProvisioner({ store, host, now = () => Date.now() }) {
|
|||
st.updatedAt = now()
|
||||
})
|
||||
}
|
||||
// 绑定记录还在,但宿主里的会话已经不存在:只标记,绝不自动补建。
|
||||
async function markMissing(projectId, seatId, message) {
|
||||
await store.mutate(s => {
|
||||
const p = s.projects[projectId]
|
||||
const st = p.seats[seatId]
|
||||
st.status = 'missing'
|
||||
st.error = String(message)
|
||||
st.updatedAt = now()
|
||||
p.status = 'error'
|
||||
p.updatedAt = now()
|
||||
})
|
||||
}
|
||||
async function markMismatch(projectId, seatId, planned, actual) {
|
||||
await store.mutate(s => {
|
||||
const st = s.projects[projectId].seats[seatId]
|
||||
|
|
@ -64,12 +81,55 @@ export function createProvisioner({ store, host, now = () => Date.now() }) {
|
|||
}
|
||||
}
|
||||
|
||||
// 标题只用两字角色名;rename 会钉住标题并停止宿主自动生成标题。
|
||||
// 同一个 sessionId 只成功写一次,避免每次打开工作台都重复 rename。
|
||||
async function renameSeatTitle(projectId, seatId) {
|
||||
if (typeof host.sessionController?.rename !== 'function') return
|
||||
const seat = store.get(projectId)?.seats?.[seatId]
|
||||
if (!seat?.sessionId || seat.status !== 'bound') return
|
||||
const key = projectId + ':' + seatId + ':' + seat.sessionId
|
||||
if (titlesEnsured.has(key)) return
|
||||
try {
|
||||
await host.sessionController.rename({ sessionId: seat.sessionId, title: SEATS[seatId].label })
|
||||
titlesEnsured.add(key)
|
||||
} catch { /* 标题失败不影响绑定;下次打开会重试 */ }
|
||||
}
|
||||
|
||||
async function ensureSeat(projectId, seatId) {
|
||||
await store.ensureLoaded()
|
||||
const seat = store.get(projectId).seats[seatId]
|
||||
if (seat.status === 'bound' && seat.sessionId) return { ok: true, skipped: true }
|
||||
|
||||
// 先持久化计划 sessionId 与 creating 状态,再发起宿主调用。
|
||||
// 已有绑定:先向宿主核实这个会话是否还存在。存在就只恢复绑定,绝不重建;
|
||||
// 不存在就标记 missing 等用户决定;读取失败就明确报错,绝不盲目创建。
|
||||
if (seat.sessionId) {
|
||||
let resolved
|
||||
try { resolved = await host.sessionController.resolveAgent(seat.sessionId) } catch (error) {
|
||||
if (isNotFound(error)) {
|
||||
await markMissing(projectId, seatId, '会话不存在:' + seat.sessionId)
|
||||
return { ok: false, missing: true }
|
||||
}
|
||||
await markFailed(projectId, seatId, `resolve: ${error?.message ?? error}`)
|
||||
return { ok: false }
|
||||
}
|
||||
if (resolved && 'error' in resolved && resolved.error) {
|
||||
if (isNotFound(resolved.error)) {
|
||||
await markMissing(projectId, seatId, '会话不存在:' + seat.sessionId)
|
||||
return { ok: false, missing: true }
|
||||
}
|
||||
await markFailed(projectId, seatId, `resolve: ${resolved.error?.message ?? resolved.error?.code ?? 'error'}`)
|
||||
return { ok: false }
|
||||
}
|
||||
if (!resolved || (!resolved.agent && !('error' in resolved))) {
|
||||
await markFailed(projectId, seatId, 'resolve: 宿主未返回会话')
|
||||
return { ok: false }
|
||||
}
|
||||
if (seat.status !== 'bound') await markBound(projectId, seatId, seat.sessionId)
|
||||
await applyPreconfiguredModel(projectId, seatId)
|
||||
await renameSeatTitle(projectId, seatId)
|
||||
return { ok: true, skipped: true }
|
||||
}
|
||||
|
||||
// 没有绑定记录时才走受控创建:先持久化计划 sessionId 与 creating 状态,再查重、再创建。
|
||||
await store.mutate(s => {
|
||||
const st = s.projects[projectId].seats[seatId]
|
||||
if (!st.plannedSessionId) st.plannedSessionId = randomUUID()
|
||||
|
|
@ -90,6 +150,7 @@ export function createProvisioner({ store, host, now = () => Date.now() }) {
|
|||
if (resolved && !('error' in resolved) && resolved.agent) {
|
||||
await markBound(projectId, seatId, planned)
|
||||
await applyPreconfiguredModel(projectId, seatId)
|
||||
await renameSeatTitle(projectId, seatId)
|
||||
return { ok: true, recovered: true }
|
||||
}
|
||||
if (resolved && 'error' in resolved && resolved.error && !isNotFound(resolved.error)) {
|
||||
|
|
@ -102,6 +163,15 @@ export function createProvisioner({ store, host, now = () => Date.now() }) {
|
|||
try {
|
||||
returned = await host.sessionController.create({ sessionId: planned, workspaceId: project.workspaceId ?? undefined })
|
||||
} catch (error) {
|
||||
// 创建结果不确定:先按计划 ID 向宿主核实一次,确认没建出来才标记失败。
|
||||
let check = null
|
||||
try { check = await host.sessionController.resolveAgent(planned) } catch { check = null }
|
||||
if (check && !('error' in check) && check.agent) {
|
||||
await markBound(projectId, seatId, planned)
|
||||
await applyPreconfiguredModel(projectId, seatId)
|
||||
await renameSeatTitle(projectId, seatId)
|
||||
return { ok: true, recovered: true }
|
||||
}
|
||||
await markFailed(projectId, seatId, `create: ${error?.message ?? error}`)
|
||||
return { ok: false }
|
||||
}
|
||||
|
|
@ -111,9 +181,115 @@ export function createProvisioner({ store, host, now = () => Date.now() }) {
|
|||
}
|
||||
await markBound(projectId, seatId, planned)
|
||||
await applyPreconfiguredModel(projectId, seatId)
|
||||
await renameSeatTitle(projectId, seatId)
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
// 打开部门:只核实并返回已经绑定的原会话,绝不创建新会话。
|
||||
// 读取失败明确报错;会话已删除则标记 missing,由用户决定是否重建。
|
||||
async function openSeat(projectId, seatId) {
|
||||
return withLock(projectId, async () => {
|
||||
await store.ensureLoaded()
|
||||
const project = store.get(projectId)
|
||||
if (!project) throw new ProvisionError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
||||
if (!SEAT_IDS.includes(seatId)) throw new ProvisionError('sansheng/invalid', `未知席 ${String(seatId)}`)
|
||||
const seat = project.seats[seatId]
|
||||
const label = SEATS[seatId].label
|
||||
if (!seat.sessionId) throw new ProvisionError('sansheng/seat-unbound', `「${label}」还没开启,请先开启该部门`)
|
||||
|
||||
let resolved
|
||||
try { resolved = await host.sessionController.resolveAgent(seat.sessionId) } catch (error) {
|
||||
if (isNotFound(error)) {
|
||||
await markMissing(projectId, seatId, '会话不存在:' + seat.sessionId)
|
||||
throw new ProvisionError('sansheng/session-missing', `「${label}」的会话已不存在,请选择重建会话`)
|
||||
}
|
||||
await markFailed(projectId, seatId, `resolve: ${error?.message ?? error}`)
|
||||
throw new ProvisionError('sansheng/session-check-failed', `无法确认「${label}」的会话:${error?.message ?? error}`)
|
||||
}
|
||||
if (resolved && 'error' in resolved && resolved.error) {
|
||||
if (isNotFound(resolved.error)) {
|
||||
await markMissing(projectId, seatId, '会话不存在:' + seat.sessionId)
|
||||
throw new ProvisionError('sansheng/session-missing', `「${label}」的会话已不存在,请选择重建会话`)
|
||||
}
|
||||
await markFailed(projectId, seatId, `resolve: ${resolved.error?.message ?? resolved.error?.code ?? 'error'}`)
|
||||
throw new ProvisionError('sansheng/session-check-failed', `无法确认「${label}」的会话:${resolved.error?.message ?? resolved.error?.code ?? 'error'}`)
|
||||
}
|
||||
if (!resolved || (!resolved.agent && !('error' in resolved))) {
|
||||
await markFailed(projectId, seatId, 'resolve: 宿主未返回会话')
|
||||
throw new ProvisionError('sansheng/session-check-failed', `无法确认「${label}」的会话:宿主未返回会话`)
|
||||
}
|
||||
if (seat.status !== 'bound') await markBound(projectId, seatId, seat.sessionId)
|
||||
await renameSeatTitle(projectId, seatId)
|
||||
return { projectId, seatId, sessionId: seat.sessionId, status: 'bound' }
|
||||
})
|
||||
}
|
||||
|
||||
// 重建部门:只在 missing 状态下允许,必须核对用户看到的旧 sessionId,只创建一次。
|
||||
// 旧会话本身不删除、不合并、不解绑历史;只把旧 sessionId 记到 previousSessionIds 供追溯。
|
||||
async function rebuildSeat(projectId, seatId, { expectedSessionId } = {}) {
|
||||
return withLock(projectId, async () => {
|
||||
await store.ensureLoaded()
|
||||
const project = store.get(projectId)
|
||||
if (!project) throw new ProvisionError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
||||
if (!SEAT_IDS.includes(seatId)) throw new ProvisionError('sansheng/invalid', `未知席 ${String(seatId)}`)
|
||||
const seat = project.seats[seatId]
|
||||
const label = SEATS[seatId].label
|
||||
if (seat.status !== 'missing') throw new ProvisionError('sansheng/not-missing', `「${label}」当前不需要重建`)
|
||||
if (expectedSessionId !== undefined && expectedSessionId !== seat.sessionId) {
|
||||
throw new ProvisionError('sansheng/session-conflict', '会话记录已变化,请刷新后再试')
|
||||
}
|
||||
const previousSessionId = seat.sessionId ?? null
|
||||
const planned = randomUUID()
|
||||
await store.mutate(s => {
|
||||
const p = s.projects[projectId]
|
||||
const st = p.seats[seatId]
|
||||
st.plannedSessionId = planned
|
||||
st.status = 'creating'
|
||||
st.error = null
|
||||
st.updatedAt = now()
|
||||
p.updatedAt = now()
|
||||
p.revision = (p.revision ?? 0) + 1
|
||||
})
|
||||
|
||||
let returned
|
||||
try {
|
||||
returned = await host.sessionController.create({ sessionId: planned, workspaceId: project.workspaceId ?? undefined })
|
||||
} catch (error) {
|
||||
// 创建结果不确定:先按计划 ID 核实一次,确认没建出来才失败。
|
||||
let check = null
|
||||
try { check = await host.sessionController.resolveAgent(planned) } catch { check = null }
|
||||
if (check && !('error' in check) && check.agent) {
|
||||
await markBound(projectId, seatId, planned, previousSessionId)
|
||||
await applyPreconfiguredModel(projectId, seatId)
|
||||
await renameSeatTitle(projectId, seatId)
|
||||
await store.mutate(s => {
|
||||
const p = s.projects[projectId]
|
||||
p.status = SEAT_IDS.every(id => p.seats[id]?.status === 'bound') ? 'ready' : 'pending'
|
||||
p.updatedAt = now()
|
||||
p.revision = (p.revision ?? 0) + 1
|
||||
})
|
||||
return { projectId, seatId, sessionId: planned, previousSessionId }
|
||||
}
|
||||
await markFailed(projectId, seatId, `create: ${error?.message ?? error}`)
|
||||
throw new ProvisionError('sansheng/create-failed', `重建「${label}」会话失败:${error?.message ?? error}`)
|
||||
}
|
||||
if (!returned || returned.sessionId !== planned) {
|
||||
await markMismatch(projectId, seatId, planned, returned?.sessionId ?? null)
|
||||
throw new ProvisionError('sansheng/session-mismatch', `重建「${label}」会话返回的 ID 不一致,已停止后续操作`)
|
||||
}
|
||||
await markBound(projectId, seatId, planned, previousSessionId)
|
||||
await applyPreconfiguredModel(projectId, seatId)
|
||||
await renameSeatTitle(projectId, seatId)
|
||||
await store.mutate(s => {
|
||||
const p = s.projects[projectId]
|
||||
p.status = SEAT_IDS.every(id => p.seats[id]?.status === 'bound') ? 'ready' : 'pending'
|
||||
p.updatedAt = now()
|
||||
p.revision = (p.revision ?? 0) + 1
|
||||
})
|
||||
return { projectId, seatId, sessionId: planned, previousSessionId }
|
||||
})
|
||||
}
|
||||
|
||||
// 只开启一个部门(用户手动点,或派工按需触发)。
|
||||
async function enableSeat(projectId, seatId) {
|
||||
return withLock(projectId, async () => {
|
||||
|
|
@ -122,12 +298,7 @@ export function createProvisioner({ store, host, now = () => Date.now() }) {
|
|||
if (!project) throw new ProvisionError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
||||
if (!SEAT_IDS.includes(seatId)) throw new ProvisionError('sansheng/invalid', `未知席 ${String(seatId)}`)
|
||||
const result = await ensureSeat(projectId, seatId)
|
||||
if (result.ok && typeof host.sessionController.rename === 'function') {
|
||||
const seat = store.get(projectId)?.seats?.[seatId]
|
||||
if (seat?.sessionId) {
|
||||
try { await host.sessionController.rename({ sessionId: seat.sessionId, title: SEATS[seatId].label }) } catch { /* 标题失败不影响绑定 */ }
|
||||
}
|
||||
}
|
||||
if (result.ok) await renameSeatTitle(projectId, seatId)
|
||||
await store.mutate(s => {
|
||||
const p = s.projects[projectId]
|
||||
const allBound = SEAT_IDS.every(id => p.seats[id]?.status === 'bound')
|
||||
|
|
@ -191,13 +362,8 @@ export function createProvisioner({ store, host, now = () => Date.now() }) {
|
|||
await store.ensureLoaded()
|
||||
const project = store.get(projectId)
|
||||
if (!project) return
|
||||
if (typeof host.sessionController?.rename !== 'function') return
|
||||
for (const seatId of SEAT_IDS) {
|
||||
const seat = project.seats[seatId]
|
||||
if (!seat || seat.status !== 'bound' || !seat.sessionId) continue
|
||||
try { await host.sessionController.rename({ sessionId: seat.sessionId, title: SEATS[seatId].label }) } catch { /* 标题修正失败不影响读取 */ }
|
||||
}
|
||||
for (const seatId of SEAT_IDS) await renameSeatTitle(projectId, seatId)
|
||||
}
|
||||
|
||||
return { enable, enableSeat, ensureSeat, ensureTitles }
|
||||
return { enable, enableSeat, ensureSeat, openSeat, rebuildSeat, ensureTitles }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ export function seatView(seatId, seat) {
|
|||
responsibility: def.responsibility,
|
||||
status: seat.status,
|
||||
sessionId: seat.sessionId ?? null,
|
||||
previousSessionIds: Array.isArray(seat.previousSessionIds) ? [...seat.previousSessionIds] : [],
|
||||
model: seat.model ?? null,
|
||||
instructions: seat.instructions ?? '',
|
||||
error: seat.error ?? null,
|
||||
|
|
@ -135,6 +136,7 @@ export class SanshengService {
|
|||
path: ws.path ?? path ?? null,
|
||||
})
|
||||
})
|
||||
await this.provisioner.ensureTitles(id)
|
||||
return projectView(this.store.get(id))
|
||||
}
|
||||
|
||||
|
|
@ -142,7 +144,8 @@ export class SanshengService {
|
|||
await this.store.ensureLoaded()
|
||||
const p = this.store.get(projectId)
|
||||
if (!p) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
||||
return projectView(p)
|
||||
await this.provisioner.ensureTitles(projectId)
|
||||
return projectView(this.store.get(projectId))
|
||||
}
|
||||
|
||||
async enable(projectId) {
|
||||
|
|
@ -160,6 +163,27 @@ export class SanshengService {
|
|||
return this.project(projectId)
|
||||
}
|
||||
|
||||
async openSeat(projectId, seatId) {
|
||||
await this.store.ensureLoaded()
|
||||
if (!this.store.get(projectId)) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
||||
if (!SEAT_IDS.includes(seatId)) throw new ServiceError('sansheng/invalid', `未知席 ${String(seatId)}`)
|
||||
const result = await this.provisioner.openSeat(projectId, seatId)
|
||||
return { ...this.project(projectId), sessionId: result.sessionId, seatId: result.seatId }
|
||||
}
|
||||
|
||||
async rebuildSeat(projectId, seatId, { expectedSessionId } = {}) {
|
||||
await this.store.ensureLoaded()
|
||||
if (!this.store.get(projectId)) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
||||
if (!SEAT_IDS.includes(seatId)) throw new ServiceError('sansheng/invalid', `未知席 ${String(seatId)}`)
|
||||
const result = await this.provisioner.rebuildSeat(projectId, seatId, { expectedSessionId })
|
||||
return {
|
||||
...this.project(projectId),
|
||||
sessionId: result.sessionId,
|
||||
seatId: result.seatId,
|
||||
previousSessionId: result.previousSessionId ?? null,
|
||||
}
|
||||
}
|
||||
|
||||
async configure(projectId, seatId, { model, instructions } = {}) {
|
||||
await this.store.ensureLoaded()
|
||||
if (!SEAT_IDS.includes(seatId)) throw new ServiceError('sansheng/invalid', `未知席 ${seatId}`)
|
||||
|
|
|
|||
|
|
@ -22,6 +22,9 @@ function isObject(v) { return v !== null && typeof v === 'object' && !Array.isAr
|
|||
// 旧版本状态没有任务/成果字段:读入时就地补空数组,不需要改文件版本。
|
||||
function normalizeProjects(state) {
|
||||
for (const project of Object.values(state.projects ?? {})) {
|
||||
for (const seat of Object.values(project.seats ?? {})) {
|
||||
if (!Array.isArray(seat.previousSessionIds)) seat.previousSessionIds = []
|
||||
}
|
||||
if (!Array.isArray(project.tasks)) project.tasks = []
|
||||
if (!Array.isArray(project.artifacts)) project.artifacts = []
|
||||
if (!Number.isFinite(project.taskSeq)) project.taskSeq = 0
|
||||
|
|
|
|||
|
|
@ -41,7 +41,8 @@ function seatRecords(project) {
|
|||
}
|
||||
|
||||
function isBound(seat) {
|
||||
return !!seat && typeof seat.sessionId === 'string' && seat.sessionId.length > 0
|
||||
// 只有 bound 且真的有 sessionId 才算可用;missing 即使还留着旧 id 也不能进入。
|
||||
return !!seat && seat.status === 'bound' && typeof seat.sessionId === 'string' && seat.sessionId.length > 0
|
||||
}
|
||||
|
||||
function modelValue(model) {
|
||||
|
|
@ -135,7 +136,11 @@ function selectedDetail(project, records, selectedSeatId, models) {
|
|||
buildModelSelect(project, seat, models),
|
||||
{
|
||||
type: 'button',
|
||||
label: bound ? '进入' + meta.label + '会话' : meta.label + '尚未绑定会话',
|
||||
label: bound
|
||||
? '进入' + meta.label + '会话'
|
||||
: seat.status === 'missing'
|
||||
? meta.label + '会话缺失,请到 Web 工作台重建'
|
||||
: meta.label + '尚未绑定会话',
|
||||
variant: bound ? 'primary' : 'ghost',
|
||||
disabled: !bound,
|
||||
action: {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ export function emptySeat(seatId) {
|
|||
seatId,
|
||||
sessionId: null,
|
||||
plannedSessionId: null,
|
||||
previousSessionIds: [],
|
||||
status: 'pending',
|
||||
model: null,
|
||||
instructions: '',
|
||||
|
|
@ -86,6 +87,12 @@ function seatProblem(seat, seatId, projectId) {
|
|||
for (const field of ['sessionId', 'plannedSessionId']) {
|
||||
if (!(seat[field] === null || isStr(seat[field]))) return `项目 ${projectId} 的席 ${seatId} 的 ${field} 非法`
|
||||
}
|
||||
if (seat.previousSessionIds !== undefined) {
|
||||
if (!Array.isArray(seat.previousSessionIds)) return `项目 ${projectId} 的席 ${seatId} 的 previousSessionIds 非法`
|
||||
for (const sid of seat.previousSessionIds) {
|
||||
if (!isStr(sid)) return `项目 ${projectId} 的席 ${seatId} 的 previousSessionIds 含非法值`
|
||||
}
|
||||
}
|
||||
if (!validModel(seat.model)) return `项目 ${projectId} 的席 ${seatId} 的 model 非法`
|
||||
if (typeof seat.instructions !== 'string') return `项目 ${projectId} 的席 ${seatId} 的 instructions 非法`
|
||||
if (!(seat.error === null || typeof seat.error === 'string')) return `项目 ${projectId} 的席 ${seatId} 的 error 非法`
|
||||
|
|
|
|||
|
|
@ -0,0 +1,149 @@
|
|||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { setupService, seatById } from './helpers.js'
|
||||
|
||||
test('打开部门:进入原绑定会话,不创建新会话', async () => {
|
||||
const { service, fake } = await setupService()
|
||||
const opened = await service.open({ path: '/open-existing', name: '打开项目' })
|
||||
const id = opened.project.id
|
||||
await service.enableSeat(id, 'lead')
|
||||
const before = seatById(await service.project(id), 'lead')
|
||||
const createdBefore = fake.calls.create.length
|
||||
|
||||
const result = await service.openSeat(id, 'lead')
|
||||
assert.equal(result.sessionId, before.sessionId)
|
||||
assert.equal(fake.calls.create.length, createdBefore, '打开已有会话绝不能 create')
|
||||
const titleCalls = fake.calls.rename.filter(c => c.sessionId === before.sessionId)
|
||||
assert.equal(titleCalls.length, 1, '同一会话标题只写一次')
|
||||
assert.equal(titleCalls[0].title, '丞相')
|
||||
})
|
||||
|
||||
test('打开部门:宿主会话被删除时标记 missing,不自动补建', async () => {
|
||||
const { service, fake } = await setupService()
|
||||
const opened = await service.open({ path: '/open-missing', name: '缺失项目' })
|
||||
const id = opened.project.id
|
||||
await service.enableSeat(id, 'lead')
|
||||
const sessionId = seatById(await service.project(id), 'lead').sessionId
|
||||
fake.sessions.delete(sessionId)
|
||||
const createdBefore = fake.calls.create.length
|
||||
|
||||
await assert.rejects(
|
||||
() => service.openSeat(id, 'lead'),
|
||||
(error) => error.code === 'sansheng/session-missing',
|
||||
)
|
||||
const seat = seatById(await service.project(id), 'lead')
|
||||
assert.equal(seat.status, 'missing')
|
||||
assert.equal(seat.sessionId, sessionId)
|
||||
assert.equal(fake.calls.create.length, createdBefore, '缺失时不能自动创建')
|
||||
})
|
||||
|
||||
test('开启部门:已有会话被删除时只标记 missing,不自动重建', async () => {
|
||||
const { service, fake } = await setupService()
|
||||
const opened = await service.open({ path: '/enable-missing', name: '开启缺失项目' })
|
||||
const id = opened.project.id
|
||||
await service.enableSeat(id, 'lead')
|
||||
const sessionId = seatById(await service.project(id), 'lead').sessionId
|
||||
fake.sessions.delete(sessionId)
|
||||
const createdBefore = fake.calls.create.length
|
||||
|
||||
const view = await service.enableSeat(id, 'lead')
|
||||
assert.equal(fake.calls.create.length, createdBefore, '已有绑定缺失时不能补建')
|
||||
assert.equal(seatById(view, 'lead').status, 'missing')
|
||||
assert.equal(seatById(view, 'lead').sessionId, sessionId)
|
||||
})
|
||||
|
||||
test('并发开启同一部门:只创建一个会话', async () => {
|
||||
const { service, fake } = await setupService()
|
||||
const opened = await service.open({ path: '/enable-concurrent', name: '并发开启项目' })
|
||||
const id = opened.project.id
|
||||
const createdBefore = fake.calls.create.length
|
||||
const views = await Promise.all(Array.from({ length: 4 }, () => service.enableSeat(id, 'lead')))
|
||||
const sessionIds = new Set(views.map(view => seatById(view, 'lead').sessionId))
|
||||
assert.equal(sessionIds.size, 1, '并发开启必须复用同一个会话')
|
||||
assert.equal(fake.calls.create.length, createdBefore + 1, '并发开启只允许创建一次')
|
||||
assert.equal(seatById(views[0], 'lead').status, 'bound')
|
||||
})
|
||||
|
||||
test('打开部门:读取会话失败时明确报错,不创建', async () => {
|
||||
const { service, fake } = await setupService()
|
||||
const opened = await service.open({ path: '/open-error', name: '读取失败项目' })
|
||||
const id = opened.project.id
|
||||
await service.enableSeat(id, 'lead')
|
||||
const sessionId = seatById(await service.project(id), 'lead').sessionId
|
||||
const createdBefore = fake.calls.create.length
|
||||
const original = fake.host.sessionController.resolveAgent
|
||||
fake.host.sessionController.resolveAgent = async (sid) => {
|
||||
if (sid === sessionId) {
|
||||
const error = new Error('宿主读取失败')
|
||||
error.code = 'session/io-error'
|
||||
throw error
|
||||
}
|
||||
return original.call(fake.host.sessionController, sid)
|
||||
}
|
||||
try {
|
||||
await assert.rejects(
|
||||
() => service.openSeat(id, 'lead'),
|
||||
(error) => error.code === 'sansheng/session-check-failed',
|
||||
)
|
||||
} finally {
|
||||
fake.host.sessionController.resolveAgent = original
|
||||
}
|
||||
assert.equal(fake.calls.create.length, createdBefore, '读取失败时不能创建')
|
||||
assert.equal(seatById(await service.project(id), 'lead').status, 'failed')
|
||||
})
|
||||
|
||||
test('重建部门:只在 missing 时创建一次,保留旧 sessionId 记录', async () => {
|
||||
const { service, fake } = await setupService()
|
||||
const opened = await service.open({ path: '/rebuild', name: '重建项目' })
|
||||
const id = opened.project.id
|
||||
await service.enableSeat(id, 'lead')
|
||||
const oldSessionId = seatById(await service.project(id), 'lead').sessionId
|
||||
fake.sessions.delete(oldSessionId)
|
||||
await assert.rejects(
|
||||
() => service.openSeat(id, 'lead'),
|
||||
(error) => error.code === 'sansheng/session-missing',
|
||||
)
|
||||
const createdBefore = fake.calls.create.length
|
||||
|
||||
const rebuilt = await service.rebuildSeat(id, 'lead', { expectedSessionId: oldSessionId })
|
||||
assert.ok(rebuilt.sessionId)
|
||||
assert.notEqual(rebuilt.sessionId, oldSessionId)
|
||||
assert.equal(fake.calls.create.length, createdBefore + 1, '重建只创建一次')
|
||||
const seat = seatById(await service.project(id), 'lead')
|
||||
assert.equal(seat.status, 'bound')
|
||||
assert.equal(seat.sessionId, rebuilt.sessionId)
|
||||
assert.deepEqual(seat.previousSessionIds, [oldSessionId])
|
||||
await assert.rejects(
|
||||
() => service.rebuildSeat(id, 'lead', { expectedSessionId: rebuilt.sessionId }),
|
||||
(error) => error.code === 'sansheng/not-missing',
|
||||
)
|
||||
assert.equal(fake.calls.create.length, createdBefore + 1, '已绑定后不允许再次重建')
|
||||
})
|
||||
|
||||
test('重建部门:旧 sessionId 对不上时拒绝,不创建', async () => {
|
||||
const { service, fake } = await setupService()
|
||||
const opened = await service.open({ path: '/rebuild-conflict', name: '重建冲突项目' })
|
||||
const id = opened.project.id
|
||||
await service.enableSeat(id, 'lead')
|
||||
const sessionId = seatById(await service.project(id), 'lead').sessionId
|
||||
fake.sessions.delete(sessionId)
|
||||
await assert.rejects(() => service.openSeat(id, 'lead'), (error) => error.code === 'sansheng/session-missing')
|
||||
const createdBefore = fake.calls.create.length
|
||||
await assert.rejects(
|
||||
() => service.rebuildSeat(id, 'lead', { expectedSessionId: 'stale-id' }),
|
||||
(error) => error.code === 'sansheng/session-conflict',
|
||||
)
|
||||
assert.equal(fake.calls.create.length, createdBefore)
|
||||
})
|
||||
|
||||
test('并发打开同一部门:全部返回同一个已有 session,不重复创建', async () => {
|
||||
const { service, fake } = await setupService()
|
||||
const opened = await service.open({ path: '/open-concurrent', name: '并发项目' })
|
||||
const id = opened.project.id
|
||||
await service.enableSeat(id, 'lead')
|
||||
const sessionId = seatById(await service.project(id), 'lead').sessionId
|
||||
const createdBefore = fake.calls.create.length
|
||||
const results = await Promise.all(Array.from({ length: 5 }, () => service.openSeat(id, 'lead')))
|
||||
for (const result of results) assert.equal(result.sessionId, sessionId)
|
||||
assert.equal(fake.calls.create.length, createdBefore)
|
||||
})
|
||||
Loading…
Reference in New Issue