154 lines
5.3 KiB
JavaScript
154 lines
5.3 KiB
JavaScript
import { SEAT_IDS, SEATS, SEAT_GROUPS } from '../shared/roles.js'
|
|
import { applyModel, normalizeSelection } from './model.js'
|
|
|
|
export class ServiceError extends Error {
|
|
constructor(code, message) { super(message); this.name = 'ServiceError'; this.code = code }
|
|
}
|
|
|
|
export function seatView(seatId, seat) {
|
|
const def = SEATS[seatId]
|
|
return {
|
|
id: seatId,
|
|
label: def.label,
|
|
duty: def.duty,
|
|
responsibility: def.responsibility,
|
|
status: seat.status,
|
|
sessionId: seat.sessionId ?? null,
|
|
model: seat.model ?? null,
|
|
instructions: seat.instructions ?? '',
|
|
error: seat.error ?? null,
|
|
}
|
|
}
|
|
|
|
export function projectView(project) {
|
|
return {
|
|
revision: project.revision ?? 0,
|
|
project: {
|
|
id: project.id,
|
|
name: project.name,
|
|
workspaceId: project.workspaceId ?? null,
|
|
path: project.path ?? null,
|
|
status: project.status,
|
|
seats: SEAT_IDS.map(id => seatView(id, project.seats[id])),
|
|
groups: {
|
|
lead: [...SEAT_GROUPS.lead],
|
|
sansheng: [...SEAT_GROUPS.sansheng],
|
|
liubu: [...SEAT_GROUPS.liubu],
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
export function normalizeCatalog(catalog) {
|
|
const providers = []
|
|
const entryFor = provider => {
|
|
let entry = providers.find(p => p.provider === provider)
|
|
if (!entry) { entry = { provider, models: [] }; providers.push(entry) }
|
|
return entry
|
|
}
|
|
const groups = catalog?.providers ?? catalog?.groups ?? catalog?.entries ?? []
|
|
if (Array.isArray(groups)) {
|
|
for (const g of groups) {
|
|
const provider = g?.provider ?? g?.id ?? g?.ref ?? g?.name
|
|
const list = g?.models ?? g?.entries ?? []
|
|
for (const m of Array.isArray(list) ? list : []) {
|
|
const modelId = typeof m === 'string' ? m : (m?.model ?? m?.id ?? m?.name)
|
|
if (!provider || !modelId) continue
|
|
const entry = entryFor(provider)
|
|
if (entry.models.some(x => x.model === modelId)) continue
|
|
const effort = typeof m === 'object' ? (m.reasoningEffort ?? m.reasoning) : undefined
|
|
entry.models.push(effort ? { model: modelId, reasoningEffort: effort } : { model: modelId })
|
|
}
|
|
}
|
|
}
|
|
const def = catalog?.default ?? catalog?.deploymentDefault ?? null
|
|
return {
|
|
providers,
|
|
default: def && (def.provider || def.model) ? { provider: def.provider ?? null, model: def.model ?? null } : null,
|
|
failures: catalog?.failures ?? catalog?.isolated ?? [],
|
|
}
|
|
}
|
|
|
|
export class SanshengService {
|
|
constructor({ store, host, provisioner }) {
|
|
this.store = store
|
|
this.host = host
|
|
this.provisioner = provisioner
|
|
}
|
|
|
|
async list() {
|
|
return {
|
|
projects: [...this.store.list()]
|
|
.sort((a, b) => String(a.name).localeCompare(String(b.name), 'zh-Hans-CN'))
|
|
.map(p => ({ id: p.id, name: p.name, status: p.status, revision: p.revision ?? 0 })),
|
|
}
|
|
}
|
|
|
|
async open({ workspaceId, name, path } = {}) {
|
|
if (!workspaceId && !path) throw new ServiceError('sansheng/invalid', '需要 workspaceId 或 path')
|
|
const registry = this.host.workspaceRegistry
|
|
let ws
|
|
if (workspaceId) {
|
|
ws = registry.get(workspaceId)
|
|
if (!ws) throw new ServiceError('sansheng/not-found', `未找到工作区 ${workspaceId}`)
|
|
} else {
|
|
ws = await registry.resolveByPath(path)
|
|
if (!ws) ws = await registry.create(path, name)
|
|
}
|
|
const id = ws.id ?? ws.workspaceId
|
|
await this.store.mutate(() => {
|
|
this.store.ensureProjectRecord({
|
|
id,
|
|
name: name ?? ws.title ?? ws.name ?? id,
|
|
workspaceId: id,
|
|
path: ws.path ?? path ?? null,
|
|
})
|
|
})
|
|
return projectView(this.store.get(id))
|
|
}
|
|
|
|
async project(projectId) {
|
|
const p = this.store.get(projectId)
|
|
if (!p) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
|
return projectView(p)
|
|
}
|
|
|
|
async enable(projectId) {
|
|
if (!this.store.get(projectId)) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
|
await this.provisioner.enable(projectId)
|
|
return this.project(projectId)
|
|
}
|
|
|
|
async configure(projectId, seatId, { model, instructions } = {}) {
|
|
if (!SEAT_IDS.includes(seatId)) throw new ServiceError('sansheng/invalid', `未知席 ${seatId}`)
|
|
const project = this.store.get(projectId)
|
|
if (!project) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
|
const seat = project.seats[seatId]
|
|
|
|
let normalized
|
|
let applied = null
|
|
if (model !== undefined) {
|
|
normalized = normalizeSelection(model) // 可能抛 sansheng/model-invalid
|
|
if (seat.sessionId) applied = await applyModel(this.host, seat.sessionId, normalized)
|
|
}
|
|
if (instructions !== undefined && typeof instructions !== 'string') {
|
|
throw new ServiceError('sansheng/invalid', 'instructions 必须是字符串')
|
|
}
|
|
|
|
await this.store.mutate(s => {
|
|
const target = s.projects[projectId].seats[seatId]
|
|
if (model !== undefined) target.model = normalized
|
|
if (instructions !== undefined) target.instructions = instructions
|
|
target.updatedAt = Date.now()
|
|
s.projects[projectId].updatedAt = Date.now()
|
|
s.projects[projectId].revision = (s.projects[projectId].revision ?? 0) + 1
|
|
})
|
|
return { seat: seatView(seatId, this.store.get(projectId).seats[seatId]), applied }
|
|
}
|
|
|
|
async models() {
|
|
const catalog = await this.host.sessionController.modelCatalog()
|
|
return normalizeCatalog(catalog)
|
|
}
|
|
}
|