206 lines
11 KiB
JavaScript
206 lines
11 KiB
JavaScript
// 项目状态 schema(版本 1)。前后端共用:宿主只接受明确合法的结构,
|
||
// 合法 JSON 但结构错误一律视为损坏,绝不静默回退成“健康空状态”。
|
||
import { SEAT_IDS } from './roles.js'
|
||
import { emptyProjectMemory } from './project-memory.js'
|
||
|
||
export const STATE_VERSION = 1
|
||
// 十席全局默认模型文件版本;只存 version/revision/updatedAt/seats。
|
||
export const GLOBAL_SETTING_VERSION = 1
|
||
|
||
export const SEAT_STATUS = Object.freeze(['pending', 'creating', 'bound', 'failed', 'missing'])
|
||
export const PROJECT_STATUS = Object.freeze(['pending', 'provisioning', 'ready', 'error'])
|
||
// 任务:草稿 → 已派工/待执行 → 完成 / 取消 / 失败;需求门控增加等待用户、退回、待重确认。
|
||
export const TASK_STATUS = Object.freeze(['draft', 'dispatched', 'running', 'prepared', 'done', 'cancelled', 'failed', 'waiting_user', 'returned', 'reconfirm'])
|
||
// 需求清单:商讨中 / 待发布 / 已发布 / 已退回 / 已取消。
|
||
export const REQUIREMENT_STATUS = Object.freeze(['discussing', 'ready', 'published', 'returned', 'cancelled'])
|
||
// 流程阶段:只由宿主服务推进,模型不能跳阶段。
|
||
export const WORKFLOW_STAGE = Object.freeze(['zhongshu_draft', 'menxia_review', 'awaiting_execution_approval', 'shangshu_dispatch', 'department_exec', 'shangshu_summary', 'menxia_acceptance', 'lead_delivery', 'delivered', 'reconfirm'])
|
||
|
||
const SEAT_STATUS_SET = new Set(SEAT_STATUS)
|
||
const PROJECT_STATUS_SET = new Set(PROJECT_STATUS)
|
||
const TASK_STATUS_SET = new Set(TASK_STATUS)
|
||
|
||
function isObject(v) { return v !== null && typeof v === 'object' && !Array.isArray(v) }
|
||
function isStr(v) { return typeof v === 'string' && v.trim() !== '' }
|
||
|
||
export function emptyState() {
|
||
return { version: STATE_VERSION, seq: 0, projects: {} }
|
||
}
|
||
|
||
export function emptyGlobalSetting() {
|
||
const seats = {}
|
||
for (const seatId of SEAT_IDS) seats[seatId] = null
|
||
return { version: GLOBAL_SETTING_VERSION, revision: 0, updatedAt: 0, seats }
|
||
}
|
||
|
||
export function validateGlobalSetting(raw) {
|
||
if (!isObject(raw)) return { ok: false, error: '设置顶层不是对象' }
|
||
if (raw.version !== GLOBAL_SETTING_VERSION) return { ok: false, error: `不支持的设置版本 ${String(raw.version)}` }
|
||
if (raw.revision !== undefined && !Number.isFinite(raw.revision)) return { ok: false, error: 'revision 不是数字' }
|
||
if (raw.updatedAt !== undefined && !Number.isFinite(raw.updatedAt)) return { ok: false, error: 'updatedAt 不是数字' }
|
||
if (!isObject(raw.seats)) return { ok: false, error: 'seats 不是对象' }
|
||
const keys = Object.keys(raw.seats).sort()
|
||
if (keys.length !== SEAT_IDS.length || keys.join(',') !== [...SEAT_IDS].sort().join(',')) {
|
||
return { ok: false, error: 'seats 不是固定十席' }
|
||
}
|
||
for (const seatId of SEAT_IDS) {
|
||
if (!validModel(raw.seats[seatId])) return { ok: false, error: `席 ${seatId} 的模型非法` }
|
||
}
|
||
return { ok: true, setting: raw }
|
||
}
|
||
|
||
export function emptySeat(seatId) {
|
||
return {
|
||
seatId,
|
||
sessionId: null,
|
||
plannedSessionId: null,
|
||
previousSessionIds: [],
|
||
status: 'pending',
|
||
model: null,
|
||
instructions: '',
|
||
error: null,
|
||
updatedAt: 0,
|
||
}
|
||
}
|
||
|
||
export function emptyProject({ id, name, workspaceId = null, path = null, now = Date.now() }) {
|
||
const seats = {}
|
||
for (const seatId of SEAT_IDS) seats[seatId] = emptySeat(seatId)
|
||
return {
|
||
id,
|
||
name,
|
||
workspaceId,
|
||
path,
|
||
status: 'pending',
|
||
seats,
|
||
// 任务与交付成果:按项目隔离保存;旧数据没有这两个字段时按空数组处理。
|
||
tasks: [],
|
||
artifacts: [],
|
||
requirements: [],
|
||
approvals: [],
|
||
taskSeq: 0,
|
||
artifactSeq: 0,
|
||
requirementSeq: 0,
|
||
approvalSeq: 0,
|
||
// 项目共识(本地临时存储;Mnemon Documents 适配后迁移)。
|
||
memory: emptyProjectMemory(id),
|
||
revision: 0,
|
||
createdAt: now,
|
||
updatedAt: now,
|
||
}
|
||
}
|
||
|
||
export function emptyTask({ id, title = '', goal = '', requirement = '', ownerSeatId = 'lead', now = Date.now(), kind = 'legacy', requirementId = null, requirementRevision = null, approvalId = null, parentTaskId = null, stage = null }) {
|
||
return {
|
||
id, title, goal, requirement, ownerSeatId, status: 'draft', note: '', sessionId: null,
|
||
kind, requirementId, requirementRevision, approvalId, parentTaskId, stage,
|
||
blockedReason: '', draft: '', reviewNote: '', summary: '', deliveryNote: '',
|
||
createdAt: now, updatedAt: now, dispatchedAt: null,
|
||
contextRevision: 0, contextHash: '', decisionIds: [], sourceRefs: [],
|
||
deliveryId: null, deliveryState: 'none', deliveryOwner: null, deliveryAttemptAt: null,
|
||
deliveryAttemptId: null, sendingOwner: null, sendingAt: null, lastTransitionId: null,
|
||
contextSyncState: 'none',
|
||
result: null, resultAt: null, resultSessionId: null, reportStatus: 'none',
|
||
}
|
||
}
|
||
|
||
export function emptyRequirement({ id, title = '', summary = '', originalText = '', now = Date.now() }) {
|
||
return {
|
||
id, title, summary, originalText, judgement: 'needs_discussion', judgementReason: '',
|
||
scope: '', nonGoals: '', acceptanceCriteria: '', risks: '', dependencies: '', missingInfo: '',
|
||
revision: 1, status: 'discussing', blockedReason: '等待与用户商讨并补全发布信息',
|
||
createdAt: now, updatedAt: now, publishedRevision: null, publishedAt: null, cancelledAt: null,
|
||
}
|
||
}
|
||
|
||
export function emptyArtifact({ id, title = '', path = '', summary = '', taskId = null, now = Date.now() }) {
|
||
return { id, title, path, summary, taskId, createdAt: now }
|
||
}
|
||
|
||
export function validModel(model) {
|
||
if (model === null || model === undefined) return true
|
||
if (!isObject(model)) return false
|
||
if (!isStr(model.provider) || !isStr(model.model)) return false
|
||
if (model.reasoningEffort !== undefined && typeof model.reasoningEffort !== 'string') return false
|
||
return true
|
||
}
|
||
|
||
function seatProblem(seat, seatId, projectId) {
|
||
if (!isObject(seat)) return `项目 ${projectId} 的席 ${seatId} 不是对象`
|
||
if (seat.seatId !== seatId) return `项目 ${projectId} 的席 ${seatId} 的 seatId 不一致`
|
||
if (!SEAT_STATUS_SET.has(seat.status)) return `项目 ${projectId} 的席 ${seatId} 状态非法:${String(seat.status)}`
|
||
for (const field of ['sessionId', 'plannedSessionId']) {
|
||
if (!(seat[field] === null || isStr(seat[field]))) return `项目 ${projectId} 的席 ${seatId} 的 ${field} 非法`
|
||
}
|
||
if (seat.previousSessionIds !== undefined) {
|
||
if (!Array.isArray(seat.previousSessionIds)) return `项目 ${projectId} 的席 ${seatId} 的 previousSessionIds 非法`
|
||
for (const sid of seat.previousSessionIds) {
|
||
if (!isStr(sid)) return `项目 ${projectId} 的席 ${seatId} 的 previousSessionIds 含非法值`
|
||
}
|
||
}
|
||
if (!validModel(seat.model)) return `项目 ${projectId} 的席 ${seatId} 的 model 非法`
|
||
if (typeof seat.instructions !== 'string') return `项目 ${projectId} 的席 ${seatId} 的 instructions 非法`
|
||
if (!(seat.error === null || typeof seat.error === 'string')) return `项目 ${projectId} 的席 ${seatId} 的 error 非法`
|
||
return undefined
|
||
}
|
||
|
||
function projectProblem(project, id) {
|
||
if (!isObject(project)) return `项目 ${id} 不是对象`
|
||
if (!isStr(project.id)) return `项目 ${id} 缺少 id`
|
||
if (project.id !== id) return `项目键 ${id} 与 id ${project.id} 不一致`
|
||
if (typeof project.name !== 'string') return `项目 ${id} 的 name 非法`
|
||
if (!PROJECT_STATUS_SET.has(project.status)) return `项目 ${id} 状态非法:${String(project.status)}`
|
||
if (!(project.workspaceId === null || isStr(project.workspaceId))) return `项目 ${id} 的 workspaceId 非法`
|
||
if (!(project.path === null || typeof project.path === 'string')) return `项目 ${id} 的 path 非法`
|
||
if (!Number.isFinite(project.revision)) return `项目 ${id} 的 revision 非法`
|
||
if (!isObject(project.seats)) return `项目 ${id} 缺少 seats`
|
||
const keys = Object.keys(project.seats).sort()
|
||
if (keys.length !== SEAT_IDS.length || keys.join(',') !== [...SEAT_IDS].sort().join(',')) {
|
||
return `项目 ${id} 的席不是固定的十席`
|
||
}
|
||
for (const seatId of SEAT_IDS) {
|
||
const problem = seatProblem(project.seats[seatId], seatId, id)
|
||
if (problem) return problem
|
||
}
|
||
if (project.requirements !== undefined && !Array.isArray(project.requirements)) return `项目 ${id} 的 requirements 不是数组`
|
||
for (const [index, req] of (project.requirements ?? []).entries()) {
|
||
if (!isObject(req)) return `项目 ${id} 的 requirements[${index}] 不是对象`
|
||
if (!isStr(req.id)) return `项目 ${id} 的 requirements[${index}] 缺少 id`
|
||
if (!REQUIREMENT_STATUS.includes(req.status)) return `项目 ${id} 的需求 ${req.id} 状态非法:${String(req.status)}`
|
||
if (!Number.isFinite(req.revision)) return `项目 ${id} 的需求 ${req.id} 的 revision 非法`
|
||
}
|
||
if (project.approvals !== undefined && !Array.isArray(project.approvals)) return `项目 ${id} 的 approvals 不是数组`
|
||
for (const [index, approval] of (project.approvals ?? []).entries()) {
|
||
if (!isObject(approval)) return `项目 ${id} 的 approvals[${index}] 不是对象`
|
||
if (!isStr(approval.id)) return `项目 ${id} 的 approvals[${index}] 缺少 id`
|
||
if (!['approved', 'invalidated'].includes(approval.status)) return `项目 ${id} 的批准 ${approval.id} 状态非法`
|
||
}
|
||
if (project.tasks !== undefined && !Array.isArray(project.tasks)) return `项目 ${id} 的 tasks 不是数组`
|
||
for (const [index, task] of (project.tasks ?? []).entries()) {
|
||
if (!isObject(task)) return `项目 ${id} 的 tasks[${index}] 不是对象`
|
||
if (!isStr(task.id)) return `项目 ${id} 的 tasks[${index}] 缺少 id`
|
||
if (!TASK_STATUS_SET.has(task.status)) return `项目 ${id} 的任务 ${task.id} 状态非法:${String(task.status)}`
|
||
}
|
||
if (project.memory !== undefined && (project.memory === null || typeof project.memory !== 'object' || Array.isArray(project.memory))) return `项目 ${id} 的 memory 非法`
|
||
if (project.leadReports !== undefined && !Array.isArray(project.leadReports)) return `项目 ${id} 的 leadReports 不是数组`
|
||
if (project.commandLedgers !== undefined && (project.commandLedgers === null || typeof project.commandLedgers !== 'object' || Array.isArray(project.commandLedgers))) return `项目 ${id} 的 commandLedgers 非法`
|
||
if (project.artifacts !== undefined && !Array.isArray(project.artifacts)) return `项目 ${id} 的 artifacts 不是数组`
|
||
for (const [index, artifact] of (project.artifacts ?? []).entries()) {
|
||
if (!isObject(artifact)) return `项目 ${id} 的 artifacts[${index}] 不是对象`
|
||
if (!isStr(artifact.id)) return `项目 ${id} 的 artifacts[${index}] 缺少 id`
|
||
}
|
||
return undefined
|
||
}
|
||
|
||
export function validateState(raw) {
|
||
if (!isObject(raw)) return { ok: false, error: '状态顶层不是对象' }
|
||
if (raw.version !== STATE_VERSION) return { ok: false, error: `不支持的状态版本 ${String(raw.version)}` }
|
||
if (!isObject(raw.projects)) return { ok: false, error: 'projects 不是对象' }
|
||
if (raw.seq !== undefined && !Number.isFinite(raw.seq)) return { ok: false, error: 'seq 不是数字' }
|
||
for (const [id, project] of Object.entries(raw.projects)) {
|
||
const problem = projectProblem(project, id)
|
||
if (problem) return { ok: false, error: problem }
|
||
}
|
||
return { ok: true, state: raw }
|
||
}
|