dsh-sansheng-liubu/sansheng/src/host/service.js

99 lines
3.9 KiB
JavaScript

// 三省六部宿主业务门面:聚合各小型业务子模块,对外保持原有导出与接口契约不变。
import { SEAT_IDS } from '../shared/roles.js'
import { ServiceError } from './services/shared.js'
import { projectView, seatView, normalizeCatalog } from './services/view.js'
import { projectServiceMethods } from './services/project-service.js'
import { modelServiceMethods } from './services/model-service.js'
import { memoryArtifactServiceMethods } from './services/memory-artifact-service.js'
import { requirementServiceMethods } from './services/requirement-service.js'
import { workflowServiceMethods } from './services/workflow-service.js'
import { consultationServiceMethods } from './services/consultation-service.js'
import { taskServiceMethods } from './services/task-service.js'
import { deliveryServiceMethods } from './services/delivery-service.js'
import { cardApprovalServiceMethods } from './services/card-approval-service.js'
export { ServiceError, seatView, projectView, normalizeCatalog }
/** 三省六部核心业务服务(由各领域子模块组合方法实现)。 */
export class SanshengService {
constructor({ store, host, provisioner, globalStore = null, modelSync = null, seatActivity = null, approvalCenter = null, leadReporter = null, seatDelivery = null }) {
this.store = store
this.host = host
this.provisioner = provisioner
this.globalStore = globalStore
this.modelSync = modelSync
this.seatActivity = seatActivity
this.approvalCenter = approvalCenter
this.leadReporter = leadReporter
this.seatDelivery = seatDelivery
this._seedStatus = { status: 'unknown', candidates: [] }
}
_flushLeadReports(projectId) {
if (!this.leadReporter) return
void this.leadReporter.flush(projectId).catch(error => {
console.warn('[sansheng] 回报发送失败:', error?.message ?? error)
})
}
// 组合每席的配置来源、实际会话模型、同步状态与运行状态。
_seatExtras(projectId, statusMap = {}) {
const project = this.store.get(projectId)
const out = {}
for (const seatId of SEAT_IDS) {
const seat = project?.seats?.[seatId]
const sync = statusMap?.[seatId] ?? null
let configuredModel = null
let modelSource = 'none'
if (seat?.model) { configuredModel = seat.model; modelSource = 'project' }
else if (this.globalStore?.getSeat?.(seatId)) { configuredModel = this.globalStore.getSeat(seatId); modelSource = 'global' }
else if (sync?.targetModel) { configuredModel = sync.targetModel; modelSource = sync.targetSource ?? 'none' }
out[seatId] = {
configuredModel,
modelSource,
sessionModel: sync?.currentModel ?? null,
modelSync: sync ? {
status: sync.status, error: sync.error ?? null, updatedAt: sync.updatedAt ?? null,
targetModel: sync.targetModel ?? null, targetSource: sync.targetSource ?? null,
} : null,
}
}
return out
}
async _requireProject(projectId) {
if (typeof projectId !== 'string' || projectId.trim() === '') {
throw new ServiceError('sansheng/invalid', '缺少 projectId')
}
await this.store.ensureLoaded()
const project = this.store.get(projectId)
if (!project) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`)
return project
}
_requireSeat(seatId, code = 'sansheng/invalid', message = `未知席 ${String(seatId)}`) {
if (!SEAT_IDS.includes(seatId)) throw new ServiceError(code, message)
return seatId
}
_requireId(value, label) {
if (typeof value !== 'string' || value.trim() === '') {
throw new ServiceError('sansheng/invalid', `缺少 ${label}`)
}
return value
}
}
Object.assign(
SanshengService.prototype,
projectServiceMethods,
modelServiceMethods,
memoryArtifactServiceMethods,
requirementServiceMethods,
workflowServiceMethods,
consultationServiceMethods,
taskServiceMethods,
deliveryServiceMethods,
cardApprovalServiceMethods,
)