feat(workbench): 分层记忆——长期记忆+项目记忆,派发注入与成果自动记账(0.10.0)

This commit is contained in:
QQ 2026-11-10 21:50:13 +08:00
parent b9a1fb71fa
commit 96bb4eb682
1 changed files with 81 additions and 2 deletions

View File

@ -123,7 +123,7 @@ function normalizeRunTerminal(value, field = 'runTerminal') {
const ATTEMPT_STATE_OF_RUN = Object.freeze({ succeeded: 'succeeded', failed: 'failed', cancelled: 'cancelled' })
export function createWorkbenchService(options = {}) {
const { store, clock = Date.now, env = process.env, fileExists = null } = options
const { store, clock = Date.now, env = process.env, fileExists = null, memory = null } = options
if (store === undefined || store === null || typeof store.mutate !== 'function') {
throw new ServiceError(SERVICE_CODES.INVALID_INPUT, 'createWorkbenchService 需要 WorkbenchStore')
}
@ -663,6 +663,17 @@ export function createWorkbenchService(options = {}) {
if (typeof packet !== 'object' || Array.isArray(packet)) {
throw new ServiceError(SERVICE_CODES.INVALID_INPUT, 'packet 必须是对象', { field: 'packet' })
}
// 记忆注入:派发时把项目记忆与长期记忆的摘录并进交接材料,
// 部门开工前就能看到「这个项目之前发生过什么」与「用户长期偏好」。
// 记忆是参考资料:读失败不拦派发,按没有记忆处理。
let memoryExcerpts = []
if (memory !== null) {
try {
memoryExcerpts = memory.excerptsFor(project.id)
} catch {
memoryExcerpts = []
}
}
// 修订任务默认把「要改的那份成果 + 版本」带进交接材料:
// 主责部门不需要回头翻记录,也不会改到别的版本上。
const revisionMaterials = task.scope?.kind === 'artifact-revision' && typeof task.scope.artifactId === 'string'
@ -696,7 +707,7 @@ export function createWorkbenchService(options = {}) {
acceptance: packet.acceptance === undefined ? task.acceptance : optionalArray(packet.acceptance, 'packet.acceptance'),
constraints: packet.constraints === undefined ? task.constraints : optionalArray(packet.constraints, 'packet.constraints'),
originMessageRef: packet.originMessageRef ?? task.originMessageRef,
excerpts: optionalArray(packet.excerpts, 'packet.excerpts'),
excerpts: [...optionalArray(packet.excerpts, 'packet.excerpts'), ...memoryExcerpts],
materialRefs: normalizeMaterialRefs(
packet.materialRefs === undefined ? revisionMaterials : packet.materialRefs,
project,
@ -881,6 +892,16 @@ export function createWorkbenchService(options = {}) {
}
}
// 自动记账:新成果落账后往项目记忆里记一行(哪个部门、做了什么),
// 项目记忆由此随干活自然生长。尽力而为:写失败不影响成果本身。
if (memory !== null) {
try {
memory.appendProject(project.id, `${task.ownerRoleId} 汇报成果(任务「${task.title}」):${summary.slice(0, 500)}`)
} catch {
// 记忆是参考资料,不拖垮业务。
}
}
if (isTerminal('task', task.status)) {
return {
result: copy(recorded.result),
@ -1422,6 +1443,60 @@ export function createWorkbenchService(options = {}) {
}
}
// ---------- 记忆 ----------
function requireMemoryScope(value) {
if (value !== 'long-term' && value !== 'project') {
throw new ServiceError(SERVICE_CODES.INVALID_INPUT, 'scope 必须是 long-term 或 project', { field: 'scope' })
}
return value
}
function requireMemory() {
if (memory === null) {
throw new ServiceError(SERVICE_CODES.INVALID_STATE, '宿主未启用记忆存储')
}
return memory
}
const getMemory = guard((input = {}) => {
const scope = requireMemoryScope(input.scope)
const mem = requireMemory()
if (scope === 'long-term') {
const found = mem.readLongTerm()
return { scope, text: found.text, exists: found.exists }
}
const project = projectIdOf(input)
const found = mem.readProject(project.id)
return { scope, projectId: project.id, text: found.text, exists: found.exists }
})
const putMemory = guard((input = {}) => {
const scope = requireMemoryScope(input.scope)
const mem = requireMemory()
if (typeof input.text !== 'string') {
throw new ServiceError(SERVICE_CODES.INVALID_INPUT, 'text 必须是字符串', { field: 'text' })
}
if (scope === 'long-term') {
mem.writeLongTerm(input.text)
return { scope, text: input.text }
}
const project = projectIdOf(input)
mem.writeProject(project.id, input.text)
return { scope, projectId: project.id, text: input.text }
})
const appendMemory = guard((input = {}) => {
const scope = requireMemoryScope(input.scope)
const mem = requireMemory()
const text = requireText(input.text, 'text')
if (scope === 'long-term') {
return { scope, text: mem.appendLongTerm(text).text }
}
const project = projectIdOf(input)
return { scope, projectId: project.id, text: mem.appendProject(project.id, text).text }
})
const api = {
version: WORKBENCH_SERVICE_VERSION,
store,
@ -1454,6 +1529,10 @@ export function createWorkbenchService(options = {}) {
requestTaskInput,
supplyTaskInput,
syncDecision,
// 记忆
getMemory,
putMemory,
appendMemory,
// 成果
registerArtifact,
requestArtifactRevision,