// 项目共识(本地临时实现,Mnemon Documents 插件接口就绪后迁移)。 // 只保存项目内约定的事实与来源;不保存用户画像,不跨项目共享。 export const PROJECT_MEMORY_VERSION = 1 function isObject(value) { return value !== null && typeof value === 'object' && !Array.isArray(value) } function strings(value) { if (!Array.isArray(value)) return [] return value.map(item => (typeof item === 'string' ? item.trim() : '')).filter(Boolean) } export function emptyProjectMemory(projectKey = '') { return { version: PROJECT_MEMORY_VERSION, projectKey, revision: 0, contentHash: '', updatedAt: 0, goal: '', constraints: [], acceptance: [], openQuestions: [], decisions: [], sources: [], } } function normalizeDecision(decision, index) { if (!isObject(decision)) return null const text = typeof decision.text === 'string' ? decision.text.trim() : '' if (!text) return null const type = ['user', 'assistant', 'verified'].includes(decision.type) ? decision.type : 'assistant' const status = ['active', 'pending', 'superseded'].includes(decision.status) ? decision.status : (type === 'assistant' ? 'pending' : 'active') return { id: typeof decision.id === 'string' && decision.id.trim() ? decision.id.trim() : 'd' + (index + 1), type, status, text, sourceRefs: Array.isArray(decision.sourceRefs) ? decision.sourceRefs.filter(isObject) : [], createdAt: Number.isFinite(decision.createdAt) ? decision.createdAt : 0, supersededBy: typeof decision.supersededBy === 'string' ? decision.supersededBy : null, } } export function normalizeProjectMemory(raw, projectKey = '') { const base = emptyProjectMemory(projectKey) if (!isObject(raw)) return base if (raw.version !== undefined && raw.version !== PROJECT_MEMORY_VERSION) return base return { ...base, projectKey: typeof raw.projectKey === 'string' && raw.projectKey ? raw.projectKey : projectKey, revision: Number.isFinite(raw.revision) && raw.revision >= 0 ? raw.revision : 0, contentHash: typeof raw.contentHash === 'string' ? raw.contentHash : '', updatedAt: Number.isFinite(raw.updatedAt) ? raw.updatedAt : 0, goal: typeof raw.goal === 'string' ? raw.goal : '', constraints: strings(raw.constraints), acceptance: strings(raw.acceptance), openQuestions: strings(raw.openQuestions), decisions: Array.isArray(raw.decisions) ? raw.decisions.map(normalizeDecision).filter(Boolean) : [], sources: Array.isArray(raw.sources) ? raw.sources.filter(isObject) : [], } } export function mergeProjectMemory(current, patch, { now = Date.now(), makeId } = {}) { const base = normalizeProjectMemory(current, current?.projectKey) const next = { ...base, decisions: base.decisions.map(item => ({ ...item })), sources: base.sources.map(item => ({ ...item })) } if (isObject(patch)) { if (typeof patch.goal === 'string') next.goal = patch.goal.trim() for (const key of ['constraints', 'acceptance', 'openQuestions']) { if (Array.isArray(patch[key])) next[key] = strings(patch[key]) } if (Array.isArray(patch.decisions)) { for (const incoming of patch.decisions) { const normalized = normalizeDecision(incoming, next.decisions.length) if (!normalized) continue const existingIndex = next.decisions.findIndex(item => item.id === normalized.id) if (existingIndex >= 0) next.decisions[existingIndex] = { ...next.decisions[existingIndex], ...normalized } else { if (!normalized.id || normalized.id.startsWith('d') && next.decisions.some(item => item.id === normalized.id)) { normalized.id = typeof makeId === 'function' ? makeId() : 'd' + (next.decisions.length + 1) } normalized.createdAt = normalized.createdAt || now next.decisions.push(normalized) } } } if (Array.isArray(patch.sources)) { for (const source of patch.sources) { if (!isObject(source)) continue const ref = { ...source, recordedAt: Number.isFinite(source.recordedAt) ? source.recordedAt : now } if (typeof ref.id !== 'string' || !ref.id) ref.id = typeof makeId === 'function' ? makeId() : 's' + (next.sources.length + 1) if (!next.sources.some(item => item.id === ref.id)) next.sources.push(ref) } } } next.revision = base.revision + 1 next.updatedAt = now return next } export function renderProjectMemory(memory, { maxDecisionChars = 2400 } = {}) { const m = normalizeProjectMemory(memory, memory?.projectKey) if (!m || (m.revision === 0 && !m.goal && m.constraints.length === 0 && m.acceptance.length === 0 && m.decisions.length === 0)) return '' const lines = ['【项目共识 v' + m.revision + '】'] if (m.goal) lines.push('目标:' + m.goal) if (m.constraints.length) { lines.push('硬约束(必须遵守):') for (const item of m.constraints) lines.push('- ' + item) } if (m.acceptance.length) { lines.push('验收标准:') for (const item of m.acceptance) lines.push('- ' + item) } const active = m.decisions.filter(item => item.status === 'active') if (active.length) { lines.push('有效决定:') let used = 0 for (const item of active) { const line = '- [' + (item.type === 'user' ? '用户' : item.type === 'verified' ? '已验证' : '模型建议') + '] ' + item.text if (used + line.length > maxDecisionChars) { lines.push('- (其余决定已省略,详见项目共识)'); break } lines.push(line) used += line.length } } if (m.openQuestions.length) { lines.push('待定事项:') for (const item of m.openQuestions) lines.push('- ' + item) } return lines.join('\n') }