// 纯视图模型:不 import React、不触碰 DOM,可被 node 直接 import 测试。 // 依据 CONTRACT.md「Web 工作台」一节生成 buildWorkbenchView(view, ui) 的返回结构; // 十席名称 / 职责全文一律取自共享的 src/shared/roles.js,保证三端一致。 import { SEATS, SEAT_IDS, SEAT_GROUPS } from '../shared/roles.js' // 未选择模型时的固定文案,两端 UI 必须一致。 export const UNCHOSEN_MODEL_LABEL = '请选择模型' export const SEAT_STATUS_LABELS = Object.freeze({ pending: '待启用', creating: '创建中', bound: '已绑定', failed: '创建失败', missing: '会话缺失', }) const DEFAULT_SEAT_STATUS = 'pending' export function seatStatusLabel(status) { if (typeof status !== 'string') return '未知状态' return SEAT_STATUS_LABELS[status] ?? '未知状态' } export function projectStatusLabel(status) { switch (status) { case 'pending': return '待启用' case 'provisioning': return '启用中' case 'ready': return '已启用' case 'error': return '启用失败' default: return '未知状态' } } // 把 models 端点返回的目录摊平成下拉选项。value 形如 provider::model[::effort], // 永远不等于 'inherit'(界面不提供“跟随宿主模型”)。 export function collectModelOptions(models) { const out = [] const seen = new Set() const providers = Array.isArray(models?.providers) ? models.providers : [] for (const entry of providers) { const provider = typeof entry?.provider === 'string' ? entry.provider : '' if (!provider) continue const list = Array.isArray(entry?.models) ? entry.models : [] for (const item of list) { const model = typeof item?.model === 'string' ? item.model : '' if (!model) continue const efforts = Array.isArray(item?.reasoningEffort) ? item.reasoningEffort.filter(e => typeof e === 'string' && e.trim() !== '') : [] if (efforts.length > 0) { for (const effort of efforts) { const value = provider + '::' + model + '::' + effort if (value === 'inherit' || seen.has(value)) continue seen.add(value) out.push({ value, label: model + ' · ' + effort, provider, model, reasoningEffort: effort }) } } else { const value = provider + '::' + model if (value === 'inherit' || seen.has(value)) continue seen.add(value) out.push({ value, label: model, provider, model }) } } } return out } export function findModelOption(options, modelValue) { if (!Array.isArray(options)) return undefined return options.find(option => option.value === modelValue) } // 把某个席已保存的 model 对象映射成 { modelValue, modelLabel }。 export function describeModel(model, options) { if (!model || typeof model !== 'object' || typeof model.provider !== 'string' || typeof model.model !== 'string') { return { modelValue: '', modelLabel: UNCHOSEN_MODEL_LABEL } } const effort = typeof model.reasoningEffort === 'string' && model.reasoningEffort ? model.reasoningEffort : undefined const match = (Array.isArray(options) ? options : []).find(option => option.provider === model.provider && option.model === model.model && (option.reasoningEffort ?? undefined) === effort) if (match) return { modelValue: match.value, modelLabel: match.label } const modelLabel = effort ? model.model + ' · ' + effort : model.model const modelValue = effort ? model.provider + '::' + model.model + '::' + effort : model.provider + '::' + model.model return { modelValue, modelLabel } } // 兼容两种入参:CONTRACT 的项目视图用数组;共享 schema 里可能以 seatId 为键。 function normalizeSeats(seats) { const map = {} if (Array.isArray(seats)) { for (const seat of seats) { if (seat && typeof seat === 'object' && typeof seat.id === 'string') map[seat.id] = seat } } else if (seats && typeof seats === 'object') { for (const [id, seat] of Object.entries(seats)) { if (seat && typeof seat === 'object') map[id] = { id, ...seat } } } return map } function cloneGroups() { const out = {} for (const key of Object.keys(SEAT_GROUPS)) out[key] = Array.from(SEAT_GROUPS[key]) return out } function pickSelectedId(requested) { if (typeof requested === 'string' && SEAT_IDS.includes(requested)) return requested return SEAT_IDS[0] } function hasOwn(obj, key) { return obj !== null && obj !== undefined && Object.prototype.hasOwnProperty.call(obj, key) } export function draftOf(draft, seatId) { if (!draft || typeof draft !== 'object') return {} const entry = draft[seatId] return entry && typeof entry === 'object' ? entry : {} } // 返回新的草稿映射(不可变):保留其它席的草稿,便于“切换角色不丢草稿”。 export function updateDraft(draft, seatId, patch) { const base = draft && typeof draft === 'object' ? draft : {} const previous = base[seatId] && typeof base[seatId] === 'object' ? base[seatId] : {} return { ...base, [seatId]: { ...previous, ...(patch ?? {}) } } } export function clearDraft(draft, seatId) { if (!draft || typeof draft !== 'object' || !hasOwn(draft, seatId)) return draft ?? {} const next = { ...draft } delete next[seatId] return next } export function buildWorkbenchView(view, ui) { const safeView = view && typeof view === 'object' ? view : {} const project = safeView.project && typeof safeView.project === 'object' ? safeView.project : {} const status = typeof project.status === 'string' ? project.status : 'pending' const ready = status === 'ready' const seatMap = normalizeSeats(project.seats) const selectedSeatId = pickSelectedId(ui?.selectedSeatId) const models = ui?.models ?? null const modelOptions = collectModelOptions(models) const draft = ui?.draft ?? null const seats = SEAT_IDS.map(id => { const def = SEATS[id] const rec = seatMap[id] ?? {} const seatStatus = typeof rec.status === 'string' ? rec.status : DEFAULT_SEAT_STATUS return { id, name: def.label, duty: def.duty, status: seatStatus, statusLabel: seatStatusLabel(seatStatus), selected: id === selectedSeatId, } }) const seatDef = SEATS[selectedSeatId] const seatRec = seatMap[selectedSeatId] ?? {} const draftEntry = draftOf(draft, selectedSeatId) let described if (hasOwn(draftEntry, 'modelValue')) { const raw = draftEntry.modelValue if (raw === '' || raw === null || raw === undefined) { described = { modelValue: '', modelLabel: UNCHOSEN_MODEL_LABEL } } else { const match = findModelOption(modelOptions, raw) described = match ? { modelValue: match.value, modelLabel: match.label } : { modelValue: String(raw), modelLabel: String(raw) } } } else if (hasOwn(draftEntry, 'model')) { described = describeModel(draftEntry.model, modelOptions) } else { described = describeModel(seatRec.model ?? null, modelOptions) } const instructions = hasOwn(draftEntry, 'instructions') ? draftEntry.instructions : (typeof seatRec.instructions === 'string' ? seatRec.instructions : '') const selectedStatus = typeof seatRec.status === 'string' ? seatRec.status : DEFAULT_SEAT_STATUS let boundCount = 0 let sessionCount = 0 for (const id of SEAT_IDS) { const rec = seatMap[id] if (rec?.status === 'bound') boundCount += 1 if (rec?.sessionId) sessionCount += 1 } return { projectTitle: typeof project.name === 'string' ? project.name : '', ready, project: { id: project.id ?? '', name: typeof project.name === 'string' ? project.name : '', status, statusLabel: projectStatusLabel(status), workspaceId: project.workspaceId ?? null, path: project.path ?? null, seatCount: seats.length, boundCount, sessionCount, }, seats, groups: cloneGroups(), selected: { id: selectedSeatId, name: seatDef.label, duty: seatDef.duty, responsibility: seatDef.responsibility, status: selectedStatus, statusLabel: seatStatusLabel(selectedStatus), sessionId: seatRec.sessionId ?? null, modelValue: described.modelValue, modelLabel: described.modelLabel, instructions, modelOptions, dirty: Object.keys(draftEntry).length > 0, }, } }