204 lines
7.3 KiB
JavaScript
204 lines
7.3 KiB
JavaScript
// dsh-go 原生面板:纯视图树构造函数。
|
||
// 仅依赖共享的角色定义(纯 JS),不 import 任何宿主 / React / DOM 代码。
|
||
// 硬约束(在下方逻辑中自然满足,并由 test/native.test.js 断言):
|
||
// 节点总数 ≤120、最大深度 ≤8、任何 options / items 列表 ≤8 项、无自由文本输入控件。
|
||
import { SEATS, SEAT_IDS, SEAT_GROUPS } from '../shared/roles.js'
|
||
|
||
const GROUP_ORDER = ['lead', 'sansheng', 'liubu']
|
||
const GROUP_TITLE = { lead: '统领', sansheng: '三省', liubu: '六部' }
|
||
|
||
const MAX_OPTIONS = 8 // 原生单选最多 8 项(含“请选择模型”占位)
|
||
|
||
const PROJECT_STATUS_TEXT = {
|
||
pending: '待启用',
|
||
provisioning: '启用中',
|
||
ready: '已就绪',
|
||
error: '异常',
|
||
}
|
||
|
||
const SEAT_STATUS_TEXT = {
|
||
pending: '待启用',
|
||
creating: '创建中',
|
||
bound: '已绑定',
|
||
failed: '失败',
|
||
missing: '缺失',
|
||
}
|
||
|
||
function asText(v) {
|
||
return typeof v === 'string' ? v : ''
|
||
}
|
||
|
||
/** 兼容 project.seats 为数组(项目视图)或对象(内部状态)。 */
|
||
function seatRecords(project) {
|
||
const out = {}
|
||
const seats = project ? project.seats : null
|
||
if (Array.isArray(seats)) {
|
||
for (const s of seats) if (s && SEATS[s.id]) out[s.id] = s
|
||
} else if (seats && typeof seats === 'object') {
|
||
for (const id of SEAT_IDS) if (seats[id]) out[id] = seats[id]
|
||
}
|
||
return out
|
||
}
|
||
|
||
function isBound(seat) {
|
||
// 只有 bound 且真的有 sessionId 才算可用;missing 即使还留着旧 id 也不能进入。
|
||
return !!seat && seat.status === 'bound' && typeof seat.sessionId === 'string' && seat.sessionId.length > 0
|
||
}
|
||
|
||
function modelValue(model) {
|
||
if (!model || typeof model !== 'object') return ''
|
||
if (typeof model.provider !== 'string' || typeof model.model !== 'string') return ''
|
||
return model.provider + '::' + model.model
|
||
}
|
||
|
||
function flattenModels(models) {
|
||
const opts = []
|
||
const providers = models && Array.isArray(models.providers) ? models.providers : []
|
||
for (const p of providers) {
|
||
const pid = asText(p && p.provider)
|
||
if (!pid) continue
|
||
const list = Array.isArray(p && p.models) ? p.models : []
|
||
for (const m of list) {
|
||
const mid = typeof m === 'string' ? m : asText(m && m.model)
|
||
if (!mid) continue
|
||
opts.push({ value: pid + '::' + mid, label: pid + ' / ' + mid })
|
||
if (opts.length >= MAX_OPTIONS - 1) break // 留一位给占位项
|
||
}
|
||
if (opts.length >= MAX_OPTIONS - 1) break
|
||
}
|
||
return opts
|
||
}
|
||
|
||
/** 模型原生单选:value 为空显示“请选择模型”。整体 ≤8 项。 */
|
||
function buildModelSelect(project, seat, models) {
|
||
const real = flattenModels(models)
|
||
const options = [{ value: '', label: '请选择模型' }].concat(real).slice(0, MAX_OPTIONS)
|
||
const want = modelValue(seat && seat.model)
|
||
const value = options.some((o) => o.value === want) ? want : ''
|
||
return {
|
||
type: 'select',
|
||
label: '模型',
|
||
value,
|
||
options,
|
||
action: { type: 'sansheng/select-model', seatId: (seat && seat.id) || null, projectId: (project && project.id) || null },
|
||
}
|
||
}
|
||
|
||
/** 单个角色按钮:label 恰为两字;选中者高亮(selected + primary)。 */
|
||
function seatButton(project, seat, selectedSeatId) {
|
||
const meta = SEATS[seat.id]
|
||
const selected = seat.id === selectedSeatId
|
||
return {
|
||
type: 'button',
|
||
seatId: seat.id,
|
||
label: meta.label,
|
||
selected,
|
||
variant: selected ? 'primary' : 'ghost',
|
||
action: { type: 'sansheng/select-seat', seatId: seat.id, projectId: (project && project.id) || null },
|
||
}
|
||
}
|
||
|
||
/** 一个分组(stack):标题 + 若干 row,每行 ≤3 个角色按钮(六部拆两行)。 */
|
||
function groupNode(project, records, key, selectedSeatId) {
|
||
const ids = (SEAT_GROUPS[key] || []).filter((id) => records[id] && SEATS[id])
|
||
if (ids.length === 0) return null
|
||
const perRow = key === 'liubu' ? 3 : ids.length
|
||
const rows = []
|
||
for (let i = 0; i < ids.length; i += perRow) {
|
||
const slice = ids.slice(i, i + perRow)
|
||
rows.push({ type: 'row', children: slice.map((id) => seatButton(project, records[id], selectedSeatId)) })
|
||
}
|
||
return {
|
||
type: 'stack',
|
||
group: key,
|
||
children: [{ type: 'text', text: GROUP_TITLE[key] + ' · ' + ids.length + ' 席' }].concat(rows),
|
||
}
|
||
}
|
||
|
||
/** 选中席详情:名称/职责/只读补充说明/模型单选/进入会话按钮。 */
|
||
function selectedDetail(project, records, selectedSeatId, models) {
|
||
const seat = selectedSeatId ? records[selectedSeatId] : null
|
||
if (!seat) {
|
||
return { type: 'callout', tone: 'info', text: '点击上方角色卡片查看职责、模型与进入会话。' }
|
||
}
|
||
const meta = SEATS[seat.id]
|
||
const bound = isBound(seat)
|
||
const statusLabel = SEAT_STATUS_TEXT[seat.status] || asText(seat.status) || '未知'
|
||
const instructions = asText(seat.instructions).trim() || '(未填写)'
|
||
return {
|
||
type: 'stack',
|
||
detail: seat.id,
|
||
children: [
|
||
{ type: 'text', text: meta.label + ' · ' + meta.duty + ' · ' + statusLabel },
|
||
{ type: 'callout', tone: 'info', text: '职责:' + meta.responsibility },
|
||
{ type: 'text', text: '角色补充说明(只读,请在 Web 端编辑)' },
|
||
{ type: 'callout', tone: 'warn', text: instructions },
|
||
buildModelSelect(project, seat, models),
|
||
{
|
||
type: 'button',
|
||
label: bound
|
||
? '进入' + meta.label + '会话'
|
||
: seat.status === 'missing'
|
||
? meta.label + '会话缺失,请到 Web 工作台重建'
|
||
: meta.label + '尚未绑定会话',
|
||
variant: bound ? 'primary' : 'ghost',
|
||
disabled: !bound,
|
||
action: {
|
||
type: 'sansheng/enter-session',
|
||
seatId: seat.id,
|
||
projectId: (project && project.id) || null,
|
||
sessionId: isBound(seat) ? seat.sessionId : null,
|
||
},
|
||
},
|
||
],
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 构造原生面板视图树。
|
||
* @param {{ project: object|null, selectedSeatId?: string|null, models?: object|null }} input
|
||
* @returns {object} 原生 view 节点
|
||
*/
|
||
export function buildPanelView(input) {
|
||
const { project = null, selectedSeatId = null, models = null } = input || {}
|
||
|
||
if (!project || typeof project !== 'object') {
|
||
return {
|
||
type: 'stack',
|
||
children: [
|
||
{ type: 'text', text: '三省六部' },
|
||
{
|
||
type: 'callout',
|
||
tone: 'warn',
|
||
text: '无法确定当前项目:原生面板作用域不含 workspaceId,且未能由 sessionId 反查到所属项目。请从已启用「三省六部」的项目会话中打开,或在 Web 工作台查看。',
|
||
},
|
||
],
|
||
}
|
||
}
|
||
|
||
const records = seatRecords(project)
|
||
// 选中席:显式传入优先;否则默认丞相(首屏有一个可见的选中态)。
|
||
const sel = selectedSeatId && records[selectedSeatId] ? selectedSeatId : records.lead ? 'lead' : null
|
||
const ready = project.status === 'ready'
|
||
const boundCount = SEAT_IDS.filter((id) => isBound(records[id])).length
|
||
|
||
const children = [
|
||
{ type: 'text', text: '三省六部 · ' + (asText(project.name) || '未命名项目') },
|
||
{
|
||
type: 'callout',
|
||
tone: ready ? 'good' : 'warn',
|
||
text: (PROJECT_STATUS_TEXT[project.status] || '状态未知') + ' · 已绑定 ' + boundCount + '/10 席',
|
||
},
|
||
]
|
||
|
||
for (const key of GROUP_ORDER) {
|
||
const g = groupNode(project, records, key, sel)
|
||
if (g) children.push(g)
|
||
}
|
||
|
||
children.push(selectedDetail(project, records, sel, models))
|
||
return { type: 'stack', children }
|
||
}
|
||
|
||
export default buildPanelView
|