From 96bb4eb68291397c2d68eb7f5d898291cbd45ac8 Mon Sep 17 00:00:00 2001 From: QQ Date: Tue, 10 Nov 2026 21:50:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(workbench):=20=E5=88=86=E5=B1=82=E8=AE=B0?= =?UTF-8?q?=E5=BF=86=E2=80=94=E2=80=94=E9=95=BF=E6=9C=9F=E8=AE=B0=E5=BF=86?= =?UTF-8?q?+=E9=A1=B9=E7=9B=AE=E8=AE=B0=E5=BF=86=EF=BC=8C=E6=B4=BE?= =?UTF-8?q?=E5=8F=91=E6=B3=A8=E5=85=A5=E4=B8=8E=E6=88=90=E6=9E=9C=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=AE=B0=E8=B4=A6=EF=BC=880.10.0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workbench/host/service.mjs | 83 +++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/workbench/host/service.mjs b/workbench/host/service.mjs index 1aca849..6501b8c 100644 --- a/workbench/host/service.mjs +++ b/workbench/host/service.mjs @@ -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,