diff --git a/client.js b/client.js index 773a919..3450020 100644 --- a/client.js +++ b/client.js @@ -1327,7 +1327,7 @@ function apply(ctx) { setConfigOpen(false); } const seat = seatById[id]; - if (wb.ready && seat && seat.status === "bound") { + if (seat && seat.status === "bound") { const rec = seatRecordOf(view, id); if (rec && rec.sessionId) { navigateToSession(rec.sessionId); @@ -1335,7 +1335,7 @@ function apply(ctx) { return; } } - if (wb.ready && seat && seat.status !== "bound") setNotice("该席尚未绑定会话,先启用或重试启用"); + if (seat && seat.status !== "bound" && seat.status !== "creating") setNotice("「" + seat.name + "」还没开启,点下面的按钮开启这个部门"); } function setModelValue(value) { setDraft((prev) => updateDraft(prev, selectedSeatId, { modelValue: value })); @@ -1343,6 +1343,24 @@ function apply(ctx) { function setInstructions(value) { setDraft((prev) => updateDraft(prev, selectedSeatId, { instructions: value })); } + async function enableSelectedSeat() { + const projectId = view && view.project ? view.project.id : null; + if (!projectId) return; + const seatId = wb.selected.id; + setBusy(true); + setError(null); + setNotice("正在开启「" + wb.selected.name + "」…"); + try { + const next = await request("enableSeat", { projectId, seatId }); + if (!alive.current) return; + setView(next); + setNotice("已开启「" + wb.selected.name + "」的独立会话"); + } catch (enableError) { + if (alive.current) setError(enableError && enableError.message ? enableError.message : "开启失败"); + } finally { + if (alive.current) setBusy(false); + } + } async function enableSeats() { const projectId = view && view.project ? view.project.id : null; if (!projectId) return; @@ -1523,10 +1541,11 @@ function apply(ctx) { (0, import_react.createElement)("span", { className: "ssw-count" }, wb.project.seatCount + " 个角色 · " + wb.project.sessionCount + " 个会话") ) ); + const selectedStatus = wb.selected.status; const detailActions = (0, import_react.createElement)( "div", { className: "ssw-detail-actions" }, - wb.ready ? (0, import_react.createElement)("button", { + selectedStatus === "bound" ? (0, import_react.createElement)("button", { type: "button", className: "ssw-primary", disabled: busy, @@ -1534,9 +1553,9 @@ function apply(ctx) { }, "开始对话") : (0, import_react.createElement)("button", { type: "button", className: "ssw-primary", - disabled: busy, - onClick: enableSeats - }, busy ? "正在启用…" : "启用此项目的十席") + disabled: busy || selectedStatus === "creating", + onClick: enableSelectedSeat + }, selectedStatus === "creating" ? "正在开启…" : "开启「" + wb.selected.name + "」") ); const optionEls = wb.selected.modelOptions.map((option) => (0, import_react.createElement)("option", { key: option.value, value: option.value }, option.label)); if (wb.selected.modelValue && !wb.selected.modelOptions.some((option) => option.value === wb.selected.modelValue)) { @@ -1560,7 +1579,7 @@ function apply(ctx) { configOpen ? (0, import_react.createElement)( "div", { className: "ssw-config-body" }, - (0, import_react.createElement)("label", { className: "ssw-label" }, "会话模型"), + (0, import_react.createElement)("label", { className: "ssw-label" }, "默认模型(开启该部门时应用)"), (0, import_react.createElement)( "select", { @@ -1581,7 +1600,7 @@ function apply(ctx) { placeholder: "补充该角色在当前项目中的工作要求", onChange: (event) => setInstructions(event.target.value) }), - (0, import_react.createElement)("p", { className: "ssw-scope-note" }, "每个角色是独立会话;模型与说明只影响当前项目当前角色,不改变宿主默认模型。"), + (0, import_react.createElement)("p", { className: "ssw-scope-note" }, "这里预配置的模型会在开启该部门或按需派工时应用;丞相派工不选择模型。只影响当前项目当前角色。"), (0, import_react.createElement)( "div", { className: "ssw-save-row" }, diff --git a/package.json b/package.json index 747de4b..2123a37 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-sansheng-liubu", - "version": "0.2.4", + "version": "0.3.0", "description": "三省六部:每个项目固定十席角色与独立会话的 DSH 插件(第一版)。", "keywords": [ "dsh-plugin", diff --git a/src/client/index.js b/src/client/index.js index 8e21d53..1313e8f 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -397,7 +397,7 @@ export function apply(ctx) { setConfigOpen(false) } const seat = seatById[id] - if (wb.ready && seat && seat.status === 'bound') { + if (seat && seat.status === 'bound') { const rec = seatRecordOf(view, id) if (rec && rec.sessionId) { navigateToSession(rec.sessionId) @@ -405,7 +405,7 @@ export function apply(ctx) { return } } - if (wb.ready && seat && seat.status !== 'bound') setNotice('该席尚未绑定会话,先启用或重试启用') + if (seat && seat.status !== 'bound' && seat.status !== 'creating') setNotice('「' + seat.name + '」还没开启,点下面的按钮开启这个部门') } function setModelValue(value) { @@ -415,6 +415,23 @@ export function apply(ctx) { setDraft(prev => updateDraft(prev, selectedSeatId, { instructions: value })) } + async function enableSelectedSeat() { + const projectId = view && view.project ? view.project.id : null + if (!projectId) return + const seatId = wb.selected.id + setBusy(true); setError(null); setNotice('正在开启「' + wb.selected.name + '」…') + try { + const next = await request('enableSeat', { projectId, seatId }) + if (!alive.current) return + setView(next) + setNotice('已开启「' + wb.selected.name + '」的独立会话') + } catch (enableError) { + if (alive.current) setError(enableError && enableError.message ? enableError.message : '开启失败') + } finally { + if (alive.current) setBusy(false) + } + } + async function enableSeats() { const projectId = view && view.project ? view.project.id : null if (!projectId) return @@ -569,15 +586,17 @@ export function apply(ctx) { ), ) + const selectedStatus = wb.selected.status const detailActions = h('div', { className: 'ssw-detail-actions' }, - wb.ready + selectedStatus === 'bound' ? h('button', { type: 'button', className: 'ssw-primary', disabled: busy, onClick: () => pickSeat(wb.selected.id), }, '开始对话') : h('button', { - type: 'button', className: 'ssw-primary', disabled: busy, onClick: enableSeats, - }, busy ? '正在启用…' : '启用此项目的十席'), + 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)) @@ -595,7 +614,7 @@ export function apply(ctx) { ), configOpen ? h('div', { className: 'ssw-config-body' }, - h('label', { className: 'ssw-label' }, '会话模型'), + h('label', { className: 'ssw-label' }, '默认模型(开启该部门时应用)'), h('select', { className: 'ssw-field', value: wb.selected.modelValue, disabled: busy, onChange: event => setModelValue(event.target.value), @@ -609,7 +628,7 @@ export function apply(ctx) { placeholder: '补充该角色在当前项目中的工作要求', onChange: event => setInstructions(event.target.value), }), - h('p', { className: 'ssw-scope-note' }, '每个角色是独立会话;模型与说明只影响当前项目当前角色,不改变宿主默认模型。'), + h('p', { className: 'ssw-scope-note' }, '这里预配置的模型会在开启该部门或按需派工时应用;丞相派工不选择模型。只影响当前项目当前角色。'), h('div', { className: 'ssw-save-row' }, h('button', { type: 'button', className: 'ssw-primary', disabled: busy, onClick: saveConfig }, busy ? '保存中…' : '保存配置'), wb.selected.dirty ? h('span', { className: 'ssw-save-status' }, '有未保存修改') : null, diff --git a/src/host/api.js b/src/host/api.js index 884f31e..8fe9b82 100644 --- a/src/host/api.js +++ b/src/host/api.js @@ -29,6 +29,11 @@ export function createApiHandler(service) { return { ok: true, value: await service.open(payload ?? {}) } case 'enable': return { ok: true, value: await service.enable(requireProjectId(payload)) } + case 'enableSeat': { + const seatId = payload?.seatId + if (!SEAT_IDS.includes(seatId)) throw bad(`未知席 ${String(seatId)}`) + return { ok: true, value: await service.enableSeat(requireProjectId(payload), seatId) } + } case 'configure': { const projectId = requireProjectId(payload) const seatId = payload?.seatId diff --git a/src/host/provision.js b/src/host/provision.js index ed26207..afa1164 100644 --- a/src/host/provision.js +++ b/src/host/provision.js @@ -2,6 +2,7 @@ // ID 不一致立即停止后续创建并保留记录;失败保留已完成绑定,重试只补未完成项。 import { randomUUID } from 'node:crypto' import { SEAT_IDS, SEATS } from '../shared/roles.js' +import { applyModel } from './model.js' export class ProvisionError extends Error { constructor(code, message) { super(message); this.name = 'ProvisionError'; this.code = code } @@ -54,6 +55,15 @@ export function createProvisioner({ store, host, now = () => Date.now() }) { }) } + // 建好会话后立即应用该席预先配置的模型;没有配置就保持原样(绝不在这里替角色挑模型)。 + async function applyPreconfiguredModel(projectId, seatId) { + const seat = store.get(projectId)?.seats?.[seatId] + if (!seat || !seat.sessionId || !seat.model) return + try { await applyModel(host, seat.sessionId, seat.model) } catch (error) { + console.warn('[sansheng] 应用预配置模型失败:', error && error.message ? error.message : error) + } + } + async function ensureSeat(projectId, seatId) { await store.ensureLoaded() const seat = store.get(projectId).seats[seatId] @@ -79,6 +89,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) return { ok: true, recovered: true } } if (resolved && 'error' in resolved && resolved.error && !isNotFound(resolved.error)) { @@ -99,9 +110,35 @@ export function createProvisioner({ store, host, now = () => Date.now() }) { return { ok: false, fatal: true } } await markBound(projectId, seatId, planned) + await applyPreconfiguredModel(projectId, seatId) return { ok: true } } + // 只开启一个部门(用户手动点,或派工按需触发)。 + async function enableSeat(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 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 { /* 标题失败不影响绑定 */ } + } + } + await store.mutate(s => { + const p = s.projects[projectId] + const allBound = SEAT_IDS.every(id => p.seats[id]?.status === 'bound') + p.status = allBound ? 'ready' : (p.status === 'error' ? 'pending' : p.status) + p.updatedAt = now() + p.revision = (p.revision ?? 0) + 1 + }) + return { projectId, seatId, ...result } + }) + } + async function enable(projectId) { return withLock(projectId, async () => { await store.ensureLoaded() @@ -150,5 +187,5 @@ export function createProvisioner({ store, host, now = () => Date.now() }) { }) } - return { enable, ensureSeat } + return { enable, enableSeat, ensureSeat } } diff --git a/src/host/service.js b/src/host/service.js index 8bd9af7..09f9d55 100644 --- a/src/host/service.js +++ b/src/host/service.js @@ -124,6 +124,14 @@ export class SanshengService { return this.project(projectId) } + async enableSeat(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)}`) + await this.provisioner.enableSeat(projectId, seatId) + return this.project(projectId) + } + async configure(projectId, seatId, { model, instructions } = {}) { await this.store.ensureLoaded() if (!SEAT_IDS.includes(seatId)) throw new ServiceError('sansheng/invalid', `未知席 ${seatId}`) @@ -192,9 +200,14 @@ export class SanshengService { const task = (project.tasks ?? []).find(item => item.id === taskId) if (!task) throw new ServiceError('sansheng/not-found', `任务 ${taskId} 不存在`) if (task.status === 'done' || task.status === 'cancelled') throw new ServiceError('sansheng/invalid', '已完成或已取消的任务不能再次派工') - const seat = project.seats[task.ownerSeatId] + let seat = project.seats[task.ownerSeatId] if (!seat || seat.status !== 'bound' || !seat.sessionId) { - throw new ServiceError('sansheng/seat-unbound', `请先为「${SEATS[task.ownerSeatId]?.label ?? task.ownerSeatId}」接通会话再派工`) + // 按需开启:丞相派工时只给出主责部门,由系统用该部门预配置的模型开启会话;丞相不选模型。 + await this.provisioner.enableSeat(projectId, task.ownerSeatId) + seat = this.store.get(projectId)?.seats?.[task.ownerSeatId] + if (!seat || seat.status !== 'bound' || !seat.sessionId) { + throw new ServiceError('sansheng/seat-unbound', `「${SEATS[task.ownerSeatId]?.label ?? task.ownerSeatId}」的会话还没开启成功,请稍后重试`) + } } const text = `【任务】${task.title}${task.goal ? '\n' + task.goal : ''}` let delivered = false diff --git a/test/tasks.test.js b/test/tasks.test.js index 03660eb..33a9a22 100644 --- a/test/tasks.test.js +++ b/test/tasks.test.js @@ -30,13 +30,32 @@ test('任务:新建草稿、派工记录、取消,按项目保存', async () await assert.rejects(() => service.taskDispatch(projectId, created.task.id), err => err.code === 'sansheng/invalid') }) -test('任务:缺标题、未知主责席被拒绝;未接通会话不能派工', async () => { +test('任务:缺标题、未知主责席被拒绝;派工按需开启该部门会话', async () => { const ctx = await setupService() const opened = await ctx.service.open({ path: '/tasks2', name: '未启用项目' }) await assert.rejects(() => ctx.service.taskCreate(opened.project.id, { title: '', ownerSeatId: 'lead' }), err => err.code === 'sansheng/invalid') await assert.rejects(() => ctx.service.taskCreate(opened.project.id, { title: 'x', ownerSeatId: 'nope' }), err => err.code === 'sansheng/invalid') const created = await ctx.service.taskCreate(opened.project.id, { title: '待派工', ownerSeatId: 'lead' }) - await assert.rejects(() => ctx.service.taskDispatch(opened.project.id, created.task.id), err => err.code === 'sansheng/seat-unbound') + const dispatched = await ctx.service.taskDispatch(opened.project.id, created.task.id) + // 派工时按需开启主责部门,不要求先手动启用十席 + assert.equal(dispatched.task.status, 'prepared') + assert.ok(dispatched.task.sessionId) + const view = await ctx.service.project(opened.project.id) + assert.equal(view.project.seats.find(s => s.id === 'lead').status, 'bound') +}) + +test('单席开启:应用预先配置的模型,且不要求开启十席', async () => { + const ctx = await setupService() + const opened = await ctx.service.open({ path: '/seat-model', name: '按需开启' }) + await ctx.service.configure(opened.project.id, 'gongbu', { model: { provider: 'deepseek', model: 'deepseek-reasoner', reasoningEffort: 'high' } }) + const view = await ctx.service.enableSeat(opened.project.id, 'gongbu') + const seat = view.project.seats.find(s => s.id === 'gongbu') + assert.equal(seat.status, 'bound') + // 其它席保持未开启 + assert.equal(view.project.seats.filter(s => s.status === 'bound').length, 1) + const applied = ctx.fake.calls.agentSelect.find(item => item.sessionId === seat.sessionId) + assert.ok(applied, '建会话后应把预配置模型应用到这个会话') + assert.deepEqual(applied.selection, { provider: 'deepseek', model: 'deepseek-reasoner', reasoningEffort: 'high' }) }) test('成果:登记、列表、关联任务校验', async () => {