146 lines
5.9 KiB
JavaScript
146 lines
5.9 KiB
JavaScript
// 项目管理与十席会话生命周期服务方法。
|
|
import { SEAT_IDS } from '../../shared/roles.js'
|
|
import { applyModel, normalizeSelection } from '../model.js'
|
|
import { ServiceError } from './shared.js'
|
|
import { projectView, seatView } from './view.js'
|
|
|
|
/** 挂载到 SanshengService 的项目与席位操作方法集。 */
|
|
export const projectServiceMethods = {
|
|
async list() {
|
|
await this.store.ensureLoaded()
|
|
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 } = {}) {
|
|
await this.store.ensureLoaded()
|
|
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,
|
|
})
|
|
})
|
|
await this.provisioner.ensureTitles(id)
|
|
return projectView(this.store.get(id))
|
|
},
|
|
|
|
async project(projectId) {
|
|
await this._requireProject(projectId)
|
|
if (this.modelSync?.syncProject) {
|
|
try { await this.modelSync.syncProject(projectId) } catch (error) {
|
|
console.warn('[sansheng] 模型同步失败:', error?.message ?? error)
|
|
}
|
|
}
|
|
await this.provisioner.ensureTitles(projectId)
|
|
const statusMap = this.modelSync?.statusOf?.(projectId) ?? {}
|
|
const extras = this._seatExtras(projectId, statusMap)
|
|
if (this.seatActivity?.reconcileProject) {
|
|
try {
|
|
const activity = await this.seatActivity.reconcileProject(projectId)
|
|
for (const seatId of SEAT_IDS) if (activity?.[seatId]) extras[seatId].runtimeState = activity[seatId]
|
|
} catch { /* 运行状态读取失败不阻断项目视图 */ }
|
|
}
|
|
return projectView(this.store.get(projectId), extras)
|
|
},
|
|
|
|
async enable(projectId) {
|
|
await this._requireProject(projectId)
|
|
await this.provisioner.enable(projectId)
|
|
return this.project(projectId)
|
|
},
|
|
|
|
async enableSeat(projectId, seatId) {
|
|
await this._requireProject(projectId)
|
|
this._requireSeat(seatId)
|
|
await this.provisioner.enableSeat(projectId, seatId)
|
|
return this.project(projectId)
|
|
},
|
|
|
|
async openSeat(projectId, seatId) {
|
|
await this._requireProject(projectId)
|
|
this._requireSeat(seatId)
|
|
const result = await this.provisioner.openSeat(projectId, seatId)
|
|
const currentView = await this.project(projectId)
|
|
return { ...currentView, sessionId: result.sessionId, seatId: result.seatId }
|
|
},
|
|
|
|
async rebuildSeat(projectId, seatId, { expectedSessionId } = {}) {
|
|
await this._requireProject(projectId)
|
|
this._requireSeat(seatId)
|
|
if (expectedSessionId !== undefined && expectedSessionId !== null && typeof expectedSessionId !== 'string') {
|
|
throw new ServiceError('sansheng/invalid', 'expectedSessionId 必须是字符串')
|
|
}
|
|
const result = await this.provisioner.rebuildSeat(projectId, seatId, { expectedSessionId })
|
|
const currentView = await this.project(projectId)
|
|
return {
|
|
...currentView,
|
|
sessionId: result.sessionId,
|
|
seatId: result.seatId,
|
|
previousSessionId: result.previousSessionId ?? null,
|
|
}
|
|
},
|
|
|
|
async configure(projectId, seatId, { model, instructions } = {}) {
|
|
const project = await this._requireProject(projectId)
|
|
this._requireSeat(seatId)
|
|
|
|
let normalized
|
|
if (model !== undefined) {
|
|
normalized = normalizeSelection(model) // null=清除项目覆盖;非法抛 sansheng/model-invalid
|
|
// 非空选择必须在写状态前验证路由;清除覆盖不需要。
|
|
if (normalized !== null) await this.host.llm.resolveCallConfig(normalized)
|
|
}
|
|
if (instructions !== undefined && typeof instructions !== 'string') {
|
|
throw new ServiceError('sansheng/invalid', 'instructions 必须是字符串')
|
|
}
|
|
// 兼容无 modelSync 的旧调用路径:仍在写状态前把选择应用到已绑定会话。
|
|
let applied = null
|
|
if (model !== undefined && normalized && !this.modelSync?.syncSeat && project.seats[seatId].sessionId) {
|
|
applied = await applyModel(this.host, project.seats[seatId].sessionId, normalized)
|
|
}
|
|
|
|
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
|
|
})
|
|
let sync = null
|
|
if (model !== undefined && this.modelSync?.syncSeat) {
|
|
try { sync = await this.modelSync.syncSeat(projectId, seatId, 'configure') } catch (error) {
|
|
sync = { status: 'failed', error: error?.message ?? String(error) }
|
|
}
|
|
}
|
|
const extras = this._seatExtras(projectId, sync ? { [seatId]: sync } : {})
|
|
return { seat: seatView(seatId, this.store.get(projectId).seats[seatId], extras[seatId] ?? {}), sync, applied }
|
|
},
|
|
|
|
async statusSnapshot(projectId) {
|
|
await this.store.ensureLoaded()
|
|
if (!this.seatActivity) return { projects: {}, available: false }
|
|
if (projectId) {
|
|
if (!this.store.get(projectId)) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`)
|
|
return { projectId, seats: await this.seatActivity.reconcileProject(projectId), available: true }
|
|
}
|
|
return { projects: await this.seatActivity.reconcileAll(), available: true }
|
|
},
|
|
}
|