workspaceKnowledge.ts 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import { createHash, randomUUID } from 'node:crypto'
  2. import { closeSync, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs'
  3. import { dirname, join, relative, sep } from 'node:path'
  4. import type { MemoryScope, WorkspaceMemory, WorkspaceMemoryJob, WorkspaceMemoryRevision, WorkspaceMemorySource, WorkspaceProject } from '../shared/workspaceKnowledge.js'
  5. import type { WorkspaceAgent, WorkspaceConversation } from '../shared/workspace.js'
  6. import { HttpError } from './errors.js'
  7. import type { OpenVikingService } from './openVikingService.js'
  8. import { OpenVikingMemoryProvider } from './openVikingMemoryProvider.js'
  9. import type { WorkspaceStore } from './workspaceStore.js'
  10. import { emptyMemoryContext, renderMemoryFacts, MEMORY_CONTEXT_PREFIX, MEMORY_CONTEXT_SUFFIX, type WorkspaceMemoryContext, type MemoryContextFact } from './workspaceMemoryContext.js'
  11. export const KNOWLEDGE_FEATURES = ['bot-collaboration-v1', 'bot-discussion-v1', 'bot-file-memory-v1', 'bot-projects-v1']
  12. /** Includes scope headings, provenance and instructions, across all three scopes. */
  13. export const MEMORY_CONTEXT_MAX_CHARS = 16_000
  14. type Scope = { scope: MemoryScope; agentId: string; projectId?: string }
  15. type Actor = { agentId?: string }
  16. export type MemoryScopeInput = Scope
  17. export type MemoryActor = Actor
  18. export type MemoryWriteInput = Scope & { requestId: string; id?: string; expectedRevision?: number; content: string; topic?: string; tier: WorkspaceMemory['tier']; sources?: WorkspaceMemorySource[]; automatic?: boolean }
  19. export interface MemoryWriteResult { memory: WorkspaceMemory; outcome: 'written' | 'duplicate' | 'replayed' }
  20. type Write = { path: string; content: string | null }
  21. type Transaction = { id: string; writes: Write[]; event: string; data: unknown }
  22. type MemoryMetadata = Omit<WorkspaceMemory, 'content'> & { fingerprint: string }
  23. type Revision = WorkspaceMemoryRevision
  24. const terminalJobs = new Set(['complete', 'skipped'])
  25. const segment = (value: string): string => {
  26. if (!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(value)) throw new HttpError(400, '记录标识无效', 'knowledge_invalid_id')
  27. return value
  28. }
  29. const hash = (value: string): string => createHash('sha256').update(value).digest('hex')
  30. const normalize = (value: string): string => value.normalize('NFC').replace(/\s+/gu, ' ').trim()
  31. const json = (value: unknown): string => JSON.stringify(value, null, 2) + '\n'
  32. const date = (at: number): string => new Date(at).toISOString().slice(0, 10)
  33. const errorCode = (error: unknown): string | undefined => (error as NodeJS.ErrnoException)?.code
  34. /** Files are authoritative. SQLite only contains rebuildable project/event projections. */
  35. export class WorkspaceKnowledge {
  36. readonly root: string
  37. readonly store: WorkspaceStore
  38. private readonly openViking?: OpenVikingService
  39. private locked = new Set<string>()
  40. private readonly memoryProvider?: OpenVikingMemoryProvider
  41. onChanged: (owner: string) => void = () => {}
  42. /** Fault injection exercises recovery after a real, partially applied transaction. */
  43. fault?: (path: string) => void
  44. constructor(home: string, store: WorkspaceStore, openViking?: OpenVikingService) {
  45. this.root = join(home, 'bot-workspace')
  46. this.store = store
  47. this.openViking = openViking
  48. if (openViking) this.memoryProvider = new OpenVikingMemoryProvider(openViking, this)
  49. for (const owner of this.names(this.root)) {
  50. if (!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(owner)) continue
  51. this.withLock(owner, () => this.recover(owner))
  52. this.rebuild(owner)
  53. }
  54. }
  55. private get activeMemoryProvider(): OpenVikingMemoryProvider | undefined {
  56. return this.openViking?.enabled ? this.memoryProvider : undefined
  57. }
  58. private async remoteMemory<T>(operation: string, run: () => Promise<T>): Promise<T> {
  59. try { return await run() } catch (error) {
  60. if (error instanceof HttpError) throw error
  61. throw new HttpError(502, `OpenViking 记忆${operation}失败:${error instanceof Error ? error.message : '未知错误'}`, 'openviking_unavailable')
  62. }
  63. }
  64. ownerDir(owner: string): string { return join(this.root, segment(owner)) }
  65. private path(owner: string, path: string): string {
  66. const root = this.ownerDir(owner), target = join(root, path), rel = relative(root, target)
  67. if (!rel || rel.startsWith('..') || rel.split(sep).some(p => p === '..')) throw new HttpError(400, '数据路径无效', 'knowledge_path_invalid')
  68. try { if (lstatSync(root).isSymbolicLink()) throw new HttpError(400, '记忆根目录不能是符号链接', 'knowledge_path_invalid') } catch (error) { if (errorCode(error) !== 'ENOENT') throw error }
  69. let current = root
  70. for (const part of rel.split(sep)) {
  71. current = join(current, part)
  72. try { if (lstatSync(current).isSymbolicLink()) throw new HttpError(400, '记忆目录不能包含符号链接', 'knowledge_path_invalid') } catch (error) { if (errorCode(error) !== 'ENOENT') throw error }
  73. }
  74. return target
  75. }
  76. private names(path: string): string[] { try { return readdirSync(path).sort() } catch (error) { if (errorCode(error) === 'ENOENT') return []; throw error } }
  77. private text(owner: string, path: string): string {
  78. try { return readFileSync(this.path(owner, path), 'utf8') } catch (error) { if (errorCode(error) === 'ENOENT') return ''; throw error }
  79. }
  80. private read<T>(owner: string, path: string, fallback: T): T {
  81. const text = this.text(owner, path)
  82. if (!text) {
  83. if (existsSync(this.path(owner, path))) throw new HttpError(409, '记忆或项目文件为空,请从备份恢复', 'knowledge_file_corrupt')
  84. return fallback
  85. }
  86. try { return JSON.parse(text) as T } catch { throw new HttpError(409, '记忆或项目文件损坏,请从备份恢复', 'knowledge_file_corrupt') }
  87. }
  88. private atomicFile(owner: string, path: string, content: string | null): void {
  89. const target = this.path(owner, path)
  90. if (content === null) { rmSync(target, { force: true }); return }
  91. mkdirSync(dirname(target), { recursive: true, mode: 0o700 })
  92. const temporary = `${target}.${randomUUID()}.tmp`, fd = openSync(temporary, 'wx', 0o600)
  93. try { writeFileSync(fd, content, 'utf8'); fsyncSync(fd) } finally { closeSync(fd) }
  94. renameSync(temporary, target)
  95. const directory = openSync(dirname(target), 'r')
  96. try { fsyncSync(directory) } finally { closeSync(directory) }
  97. }
  98. private withLock<T>(owner: string, fn: () => T): T {
  99. if (this.locked.has(owner)) throw new HttpError(409, '记忆正在更新,请稍后重试', 'knowledge_busy')
  100. const root = this.ownerDir(owner)
  101. mkdirSync(root, { recursive: true, mode: 0o700 })
  102. if (lstatSync(root).isSymbolicLink()) throw new HttpError(400, '记忆根目录不能是符号链接', 'knowledge_path_invalid')
  103. const lock = join(root, '.write-lock')
  104. try { mkdirSync(lock, { mode: 0o700 }) } catch (error) {
  105. if (errorCode(error) !== 'EEXIST') throw error
  106. let pid = 0
  107. try { pid = Number(readFileSync(join(lock, 'pid'), 'utf8')); if (pid > 0) process.kill(pid, 0) } catch (failure) {
  108. if (errorCode(failure) === 'ESRCH') { rmSync(lock, { recursive: true }); return this.withLock(owner, fn) }
  109. if (errorCode(failure) === 'ENOENT' && Date.now() - lstatSync(lock).mtimeMs > 30000) { rmSync(lock, { recursive: true }); return this.withLock(owner, fn) }
  110. }
  111. throw new HttpError(409, '记忆正由另一进程更新,请稍后重试', 'knowledge_busy')
  112. }
  113. writeFileSync(join(lock, 'pid'), String(process.pid), { mode: 0o600 })
  114. this.locked.add(owner)
  115. try { return fn() } finally { this.locked.delete(owner); rmSync(lock, { recursive: true, force: true }) }
  116. }
  117. private apply(owner: string, tx: Transaction): void {
  118. for (const write of tx.writes) { this.atomicFile(owner, write.path, write.content); this.fault?.(write.path) }
  119. this.store.atomic(() => {
  120. this.rebuild(owner)
  121. if (!this.store.get(owner, 'knowledge-transaction', tx.id)) {
  122. this.store.event(owner, tx.event, tx.data)
  123. this.store.put(owner, 'knowledge-transaction', tx.id, { id: tx.id })
  124. }
  125. })
  126. this.atomicFile(owner, `.transactions/${tx.id}.json`, null)
  127. }
  128. private recover(owner: string): void {
  129. for (const name of this.names(join(this.ownerDir(owner), '.transactions')).filter(n => n.endsWith('.json'))) {
  130. const tx = this.read<Transaction | null>(owner, `.transactions/${name}`, null)
  131. if (!tx || !Array.isArray(tx.writes) || typeof tx.id !== 'string') throw new HttpError(409, '记忆恢复日志损坏', 'knowledge_file_corrupt')
  132. this.apply(owner, tx)
  133. }
  134. }
  135. private command<T>(owner: string, requestId: string, input: unknown, event: string, operation: () => { result: T; writes: Write[] }): T {
  136. segment(requestId)
  137. return this.withLock(owner, () => {
  138. this.recover(owner)
  139. const path = `.commands/${requestId}.json`, fingerprint = hash(JSON.stringify(input)), saved = this.read<{ fingerprint: string; result: T } | null>(owner, path, null)
  140. if (saved) {
  141. if (saved.fingerprint !== fingerprint) throw new HttpError(409, '请求标识已用于其他修改', 'request_conflict')
  142. return saved.result
  143. }
  144. const { result, writes } = operation()
  145. const tx: Transaction = { id: randomUUID(), writes: [...writes, { path, content: json({ fingerprint, result }) }], event, data: result }
  146. this.atomicFile(owner, `.transactions/${tx.id}.json`, json(tx))
  147. this.apply(owner, tx)
  148. this.onChanged(owner)
  149. return result
  150. })
  151. }
  152. agent(owner: string, id: string): WorkspaceAgent {
  153. const agent = this.store.require<WorkspaceAgent>(owner, 'agent', segment(id))
  154. if (agent.archived) throw new HttpError(409, '机器人已归档', 'agent_archived')
  155. return agent
  156. }
  157. projects(owner: string): WorkspaceProject[] {
  158. return this.names(join(this.ownerDir(owner), 'projects')).filter(id => /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(id)).flatMap(id => {
  159. const text = this.text(owner, `projects/${id}/project.md`)
  160. if (!text) return []
  161. const match = /^---\n([\s\S]*?)\n---\n([\s\S]*)$/.exec(text)
  162. if (!match) throw new HttpError(409, '项目文件格式损坏', 'knowledge_file_corrupt')
  163. const meta: Record<string, unknown> = {}
  164. try { for (const line of match[1]!.split('\n')) { const split = line.indexOf(':'); if (split < 1) throw new Error(); meta[line.slice(0, split)] = JSON.parse(line.slice(split + 1).trim()) } } catch { throw new HttpError(409, '项目文件格式损坏', 'knowledge_file_corrupt') }
  165. const memberIds = this.names(join(this.ownerDir(owner), 'agents')).filter(agentId => /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(agentId) && this.memberships(owner, agentId).includes(id))
  166. return [{ ...meta, id, description: match[2]!.trim(), memberIds, groupIds: this.read<{ groups: string[] }>(owner, `projects/${id}/groups.json`, { groups: [] }).groups } as unknown as WorkspaceProject]
  167. })
  168. }
  169. project(owner: string, id: string): WorkspaceProject {
  170. const project = this.projects(owner).find(p => p.id === segment(id))
  171. if (!project) throw new HttpError(404, '项目不存在', 'project_not_found')
  172. return project
  173. }
  174. memberships(owner: string, agentId: string): string[] { return this.read<{ projects: string[] }>(owner, `agents/${segment(agentId)}/projects.json`, { projects: [] }).projects }
  175. requireProjectMember(owner: string, projectId: string, agentId: string): WorkspaceProject {
  176. const project = this.project(owner, projectId)
  177. if (project.archived || !project.memberIds.includes(agentId)) throw new HttpError(403, '当前机器人没有此项目的权限', 'project_forbidden')
  178. return project
  179. }
  180. saveProject(owner: string, input: { requestId: string; id?: string; expectedRevision?: number; name: string; description: string; memberIds: string[]; groupIds: string[]; archived?: boolean }, actor: Actor = {}): WorkspaceProject {
  181. if (actor.agentId && this.agent(owner, actor.agentId).temporaryGoalId) throw new HttpError(403, '临时助手不能管理项目', 'project_forbidden')
  182. const id = input.id ? segment(input.id) : input.requestId
  183. return this.command(owner, input.requestId, { operation: 'project.save', input, actor }, 'project.changed', () => {
  184. const old = input.id ? this.project(owner, id) : undefined
  185. if (old && old.revision !== input.expectedRevision) throw new HttpError(409, '项目已更新,请重新读取后保存', 'project_revision_conflict')
  186. if (actor.agentId && old && !old.memberIds.includes(actor.agentId)) throw new HttpError(403, '只能管理自己参与的项目', 'project_forbidden')
  187. const memberIds = [...new Set(input.memberIds)], groupIds = [...new Set(input.groupIds)]
  188. for (const agentId of memberIds) if (this.agent(owner, agentId).temporaryGoalId) throw new HttpError(403, '临时助手不能加入持久项目', 'helper_task_bound')
  189. for (const groupId of groupIds) {
  190. const group = this.store.require<WorkspaceConversation>(owner, 'conversation', groupId)
  191. if (group.kind !== 'group' || group.archived || group.memberIds.some(member => !memberIds.includes(member))) throw new HttpError(400, '关联群的全部成员需要先加入项目', 'project_group_members')
  192. if (this.projects(owner).some(p => p.id !== id && p.groupIds.includes(groupId))) throw new HttpError(409, '此群已关联其他项目', 'project_group_linked')
  193. }
  194. const changingGroups = [...new Set([...(old?.groupIds ?? []), ...groupIds])]
  195. if (this.store.list<{ conversationId: string; status: string }>(owner, 'run').some(run => changingGroups.includes(run.conversationId) && !['complete', 'failed', 'interrupted'].includes(run.status))) throw new HttpError(409, '请等关联群当前执行结束后修改项目', 'project_busy')
  196. if (this.store.list<{ conversationId: string; status: string }>(owner, 'goal').some(goal => changingGroups.includes(goal.conversationId) && !['complete', 'blocked', 'cancelled'].includes(goal.status))) throw new HttpError(409, '请先结束关联群的交付目标', 'project_busy')
  197. const now = Date.now(), result: WorkspaceProject = { id, name: input.name.trim(), description: input.description.trim(), memberIds, groupIds, revision: (old?.revision ?? 0) + 1, archived: input.archived ?? old?.archived ?? false, createdAt: old?.createdAt ?? now, updatedAt: now }
  198. const { description, memberIds: _members, groupIds: _groups, ...metadata } = result
  199. const writes: Write[] = [{ path: `projects/${id}/project.md`, content: `---\n${Object.entries(metadata).map(([key, value]) => `${key}: ${JSON.stringify(value)}`).join('\n')}\n---\n${description}\n` }, { path: `projects/${id}/groups.json`, content: json({ groups: groupIds }) }]
  200. for (const agentId of new Set([...(old?.memberIds ?? []), ...memberIds])) writes.push({ path: `agents/${segment(agentId)}/projects.json`, content: json({ projects: [...new Set([...this.memberships(owner, agentId).filter(p => p !== id), ...(memberIds.includes(agentId) ? [id] : [])])].sort() }) })
  201. return { result, writes }
  202. })
  203. }
  204. private rebuild(owner: string): void {
  205. const projects = this.projects(owner)
  206. for (const old of this.store.list<{ id: string }>(owner, 'project')) if (!projects.some(p => p.id === old.id)) this.store.remove(owner, 'project', old.id)
  207. for (const project of projects) this.store.put(owner, 'project', project.id, project)
  208. for (const conversation of this.store.list<WorkspaceConversation>(owner, 'conversation').filter(c => c.kind === 'group')) {
  209. const projectId = projects.find(p => !p.archived && p.groupIds.includes(conversation.id))?.id
  210. if (conversation.projectId !== projectId) {
  211. conversation.projectId = projectId
  212. this.store.put(owner, 'conversation', conversation.id, conversation)
  213. this.store.event(owner, 'conversation.changed', conversation, conversation.id)
  214. }
  215. }
  216. }
  217. private shard(scope: Scope): string {
  218. const agent = segment(scope.agentId)
  219. if (scope.scope === 'agent') return `agents/${agent}/memory`
  220. if (scope.scope === 'user') return `user-memory/agents/${agent}`
  221. if (!scope.projectId) throw new HttpError(400, '需要选择项目', 'project_required')
  222. return `projects/${segment(scope.projectId)}/memory/agents/${agent}`
  223. }
  224. authorizeMemory(owner: string, scope: Scope, actor: Actor, write = false): void {
  225. this.agent(owner, scope.agentId)
  226. if (actor.agentId) {
  227. const agent = this.agent(owner, actor.agentId)
  228. if (agent.temporaryGoalId) throw new HttpError(403, '临时助手不直接维护长期记忆', 'helper_task_bound')
  229. if ((write || scope.scope === 'agent') && actor.agentId !== scope.agentId) throw new HttpError(403, '只能维护自己贡献的记忆', 'memory_forbidden')
  230. if (scope.scope === 'project') this.requireProjectMember(owner, scope.projectId!, actor.agentId)
  231. }
  232. if (scope.scope === 'project') this.project(owner, scope.projectId!)
  233. }
  234. private shardMemories(owner: string, scope: Scope): WorkspaceMemory[] {
  235. const shard = this.shard(scope), metadata = this.read<MemoryMetadata[]>(owner, `${shard}/.dreaming/records.json`, [])
  236. const paths = ['profile.md', ...this.names(join(this.ownerDir(owner), shard, 'log')).filter(n => /^\d{4}-\d{2}\.md$/.test(n)).map(n => `log/${n}`)]
  237. return paths.flatMap(path => {
  238. const text = this.text(owner, `${shard}/${path}`)
  239. return text.split('\n').filter(line => line.trim() && !line.startsWith('#') && !line.startsWith('<!--')).map(line => {
  240. const match = /^- \((\d{4}-\d{2}-\d{2})\) (.+)$/.exec(line)
  241. if (!match || !Number.isFinite(Date.parse(match[1]!))) throw new HttpError(409, '记忆正文格式损坏', 'knowledge_file_corrupt')
  242. const content = normalize(match[2]!), fingerprint = hash(content.toLowerCase()), meta = metadata.find(m => m.fingerprint === fingerprint)
  243. const at = Date.parse(match[1]! + 'T00:00:00Z')
  244. return { ...scope, id: meta?.id ?? fingerprint.slice(0, 32), content, topic: meta?.topic, tier: path === 'profile.md' ? 'profile' : meta?.tier ?? 'log', origin: meta?.origin ?? 'manual', sources: meta?.sources ?? [], revision: meta?.revision ?? 1, createdAt: meta?.createdAt ?? at, updatedAt: meta?.updatedAt ?? at } as WorkspaceMemory
  245. })
  246. })
  247. }
  248. private memoryScopes(owner: string, query: { scope: MemoryScope; agentId?: string; projectId?: string }, actor: Actor = {}): Scope[] {
  249. if (query.agentId) return [query.agentId].map(agentId => ({ scope: query.scope, agentId, projectId: query.projectId }))
  250. if (query.scope === 'agent') return (actor.agentId ? [actor.agentId] : this.store.list<WorkspaceAgent>(owner, 'agent').filter(agent => !agent.archived && !agent.temporaryGoalId).map(agent => agent.id)).map(agentId => ({ scope: query.scope, agentId }))
  251. if (this.activeMemoryProvider) return this.store.list<WorkspaceAgent>(owner, 'agent').filter(agent => !agent.archived && !agent.temporaryGoalId).map(agent => agent.id).map(agentId => ({ scope: query.scope, agentId, projectId: query.projectId }))
  252. return this.names(join(this.ownerDir(owner), query.scope === 'user' ? 'user-memory/agents' : `projects/${segment(query.projectId ?? '')}/memory/agents`)).map(agentId => ({ scope: query.scope, agentId, projectId: query.projectId }))
  253. }
  254. async memories(owner: string, query: { scope: MemoryScope; agentId?: string; projectId?: string; search?: string }, actor: Actor = {}): Promise<WorkspaceMemory[]> {
  255. if (this.activeMemoryProvider) {
  256. const scopes = this.memoryScopes(owner, query, actor).filter(scope => {
  257. try { this.authorizeMemory(owner, scope, actor); return true } catch { return false }
  258. })
  259. const provider = this.activeMemoryProvider
  260. const result = await this.remoteMemory(query.search ? '搜索' : '读取', () => query.search ? provider.search(owner, query, scopes, actor) : provider.list(owner, scopes))
  261. // The provider already selected keyword and semantic matches in authorized scopes.
  262. return this.decorateMemories(result)
  263. }
  264. const ids = query.agentId ? [query.agentId] : query.scope === 'agent' ? actor.agentId ? [actor.agentId] : this.store.list<WorkspaceAgent>(owner, 'agent').filter(a => !a.archived && !a.temporaryGoalId).map(a => a.id)
  265. : this.names(join(this.ownerDir(owner), query.scope === 'user' ? 'user-memory/agents' : `projects/${segment(query.projectId ?? '')}/memory/agents`))
  266. const result = ids.flatMap(agentId => {
  267. const scope = { scope: query.scope, agentId, projectId: query.projectId }
  268. if (!this.store.get<WorkspaceAgent>(owner, 'agent', agentId) || this.store.get<WorkspaceAgent>(owner, 'agent', agentId)?.archived) return []
  269. this.authorizeMemory(owner, scope, actor)
  270. return this.shardMemories(owner, scope)
  271. })
  272. return this.decorateMemories(result).filter(memory => !query.search || memory.content.toLocaleLowerCase().includes(query.search.toLocaleLowerCase()))
  273. }
  274. private decorateMemories(result: WorkspaceMemory[]): WorkspaceMemory[] {
  275. for (const memory of result) if (memory.scope !== 'agent' && memory.topic) memory.conflict = result.some(other => other.agentId !== memory.agentId && other.topic?.toLocaleLowerCase() === memory.topic!.toLocaleLowerCase() && other.content.toLocaleLowerCase() !== memory.content.toLocaleLowerCase())
  276. return result.sort((a, b) => Number(b.tier === 'profile') - Number(a.tier === 'profile') || b.updatedAt - a.updatedAt || a.id.localeCompare(b.id))
  277. }
  278. private memoryWrites(owner: string, scope: Scope, all: WorkspaceMemory[]): Write[] {
  279. const shard = this.shard(scope), grouped = new Map<string, string[]>()
  280. grouped.set('profile.md', [])
  281. for (const old of this.names(join(this.ownerDir(owner), shard, 'log')).filter(n => /^\d{4}-\d{2}\.md$/.test(n))) grouped.set(`log/${old}`, [])
  282. for (const memory of all) {
  283. const file = memory.tier === 'profile' ? 'profile.md' : `log/${date(memory.createdAt).slice(0, 7)}.md`
  284. grouped.set(file, [...(grouped.get(file) ?? []), `- (${date(memory.createdAt)}) ${memory.content}`])
  285. }
  286. const writes = [...grouped].map(([file, lines]) => ({ path: `${shard}/${file}`, content: `${file === 'profile.md' ? '# 长期记忆' : '# 记忆记录'}\n\n${lines.join('\n')}${lines.length ? '\n' : ''}` }))
  287. writes.push({ path: `${shard}/.dreaming/records.json`, content: json(all.map(({ content, ...memory }) => ({ ...memory, fingerprint: hash(content.toLowerCase()) }))) })
  288. return writes
  289. }
  290. async writeMemory(owner: string, input: MemoryWriteInput, actor: Actor = {}): Promise<WorkspaceMemory> {
  291. return (await this.writeMemoryWithResult(owner, input, actor)).memory
  292. }
  293. async writeMemoryWithResult(owner: string, input: MemoryWriteInput, actor: Actor = {}): Promise<MemoryWriteResult> {
  294. const writeProvider = this.activeMemoryProvider
  295. if (writeProvider) return this.remoteMemory('写入', () => writeProvider.writeWithResult(owner, input, actor))
  296. this.authorizeMemory(owner, input, actor, true)
  297. // Cached commands bypass the callback, retaining their original memory shape.
  298. let outcome: MemoryWriteResult['outcome'] = 'replayed'
  299. const memory = this.command(owner, input.requestId, { operation: 'memory.write', input, actor }, 'memory.changed', () => {
  300. this.authorizeMemory(owner, input, actor, true)
  301. const all = this.shardMemories(owner, input), old = input.id ? all.find(m => m.id === input.id) : undefined
  302. if (input.scope === 'project' && !old) this.requireProjectMember(owner, input.projectId!, input.agentId)
  303. if (input.id && !old) throw new HttpError(404, '记忆不存在', 'memory_not_found')
  304. if (old && old.revision !== input.expectedRevision) throw new HttpError(409, '记忆已更新,请重新读取后保存;草稿已保留', 'memory_revision_conflict')
  305. if (actor.agentId && old?.origin === 'manual') throw new HttpError(403, '用户维护的记忆不能被机器人覆盖', 'memory_manual_protected')
  306. if (input.automatic && old && old.origin !== 'synthesis') throw new HttpError(403, '自动整理不能覆盖明确记忆', 'memory_explicit_protected')
  307. const content = normalize(input.content)
  308. if (!content || content.length > 2000) throw new HttpError(400, '每条记忆需要 1 至 2000 个字符', 'memory_content_invalid')
  309. const duplicate = all.find(m => m.id !== old?.id && m.content.toLowerCase() === content.toLowerCase())
  310. if (duplicate) { outcome = 'duplicate'; return { result: duplicate, writes: [] } }
  311. const shard = this.shard(input), fingerprint = hash(content.toLowerCase())
  312. const forgottenSources = this.read<string[]>(owner, `${shard}/.dreaming/forgotten-sources.json`, [])
  313. if (input.automatic && (this.text(owner, `${shard}/.dreaming/tombstones/${fingerprint}.deleted`) || input.sources?.some(source => forgottenSources.includes(source.messageId)))) throw new HttpError(409, '此记忆或其旧来源已被遗忘', 'memory_forgotten')
  314. const sources = input.sources ?? old?.sources ?? []
  315. if (actor.agentId && !sources.length) throw new HttpError(400, '保存记忆需要来源消息', 'memory_source_required')
  316. for (const source of sources) {
  317. const message = this.store.require<import('../shared/workspace.js').WorkspaceMessage>(owner, 'message', source.messageId)
  318. if (message.conversationId !== source.conversationId || message.visible === false || (source.quote && !message.content.includes(source.quote))) throw new HttpError(400, '记忆来源无效', 'memory_source_invalid')
  319. if (actor.agentId && input.scope === 'user' && (message.role !== 'user' || !source.quote?.trim())) throw new HttpError(400, '用户共享记忆需要用户原话依据', 'memory_user_evidence_required')
  320. }
  321. const now = Date.now(), result: WorkspaceMemory = { id: old?.id ?? randomUUID(), scope: input.scope, agentId: input.agentId, ...(input.projectId ? { projectId: input.projectId } : {}), content, tier: input.tier, origin: actor.agentId ? input.automatic ? 'synthesis' : 'explicit' : 'manual', sources, revision: (old?.revision ?? 0) + 1, createdAt: old?.createdAt ?? now, updatedAt: now }
  322. if (input.topic || old?.topic) result.topic = normalize(input.topic ?? old!.topic!).slice(0, 100)
  323. const revision: Revision = { id: randomUUID(), memoryId: result.id, revision: result.revision, operation: 'write', actor: actor.agentId ?? 'user', at: now, before: old, after: result }
  324. const writes = this.memoryWrites(owner, input, [...all.filter(m => m.id !== old?.id), result])
  325. if (old && old.content.toLowerCase() !== content.toLowerCase() && result.origin !== 'synthesis') {
  326. writes.push({ path: `${shard}/.dreaming/tombstones/${hash(old.content.toLowerCase())}.deleted`, content: json({ at: now }) })
  327. writes.push({ path: `${shard}/.dreaming/forgotten-sources.json`, content: json([...new Set([...forgottenSources, ...old.sources.map(source => source.messageId)])]) })
  328. }
  329. writes.push({ path: `${shard}/.dreaming/revisions/${result.id}-${result.revision}.json`, content: json(revision) }, { path: `${shard}/.dreaming/${result.origin === 'synthesis' ? 'synthesized' : 'explicit'}/${fingerprint}.memory`, content: json({ id: result.id, revision: result.revision }) })
  330. if (!input.automatic) writes.push({ path: `${shard}/.dreaming/tombstones/${fingerprint}.deleted`, content: null })
  331. outcome = 'written'
  332. return { result, writes }
  333. })
  334. return { memory, outcome }
  335. }
  336. async forget(owner: string, input: Scope & { requestId: string; id: string; expectedRevision: number }, actor: Actor = {}): Promise<{ id: string; scope: MemoryScope; agentId: string; projectId?: string }> {
  337. const forgetProvider = this.activeMemoryProvider
  338. if (forgetProvider) return this.remoteMemory('遗忘', () => forgetProvider.forget(owner, input, actor))
  339. this.authorizeMemory(owner, input, actor, true)
  340. return this.command(owner, input.requestId, { operation: 'memory.forget', input, actor }, 'memory.changed', () => {
  341. const all = this.shardMemories(owner, input), old = all.find(m => m.id === input.id)
  342. if (!old) throw new HttpError(404, '记忆不存在', 'memory_not_found')
  343. if (old.revision !== input.expectedRevision) throw new HttpError(409, '记忆已更新,请重新读取后操作', 'memory_revision_conflict')
  344. if (actor.agentId && old.origin === 'manual') throw new HttpError(403, '用户维护的记忆不能被机器人删除', 'memory_manual_protected')
  345. const shard = this.shard(input), fingerprint = hash(old.content.toLowerCase()), now = Date.now()
  346. const revision: Revision = { id: randomUUID(), memoryId: old.id, revision: old.revision + 1, operation: 'forget', actor: actor.agentId ?? 'user', at: now, before: old }
  347. const forgottenSources = [...new Set([...this.read<string[]>(owner, `${shard}/.dreaming/forgotten-sources.json`, []), ...old.sources.map(source => source.messageId)])]
  348. return { result: { id: old.id, scope: old.scope, agentId: old.agentId, projectId: old.projectId }, writes: [...this.memoryWrites(owner, input, all.filter(m => m.id !== old.id)), { path: `${shard}/.dreaming/tombstones/${fingerprint}.deleted`, content: json({ at: now }) }, { path: `${shard}/.dreaming/forgotten-sources.json`, content: json(forgottenSources) }, { path: `${shard}/.dreaming/revisions/${old.id}-${revision.revision}.json`, content: json(revision) }] }
  349. })
  350. }
  351. async revisions(owner: string, scope: Scope, id: string): Promise<Revision[]> {
  352. const revisionProvider = this.activeMemoryProvider
  353. if (revisionProvider) return this.remoteMemory('历史读取', () => revisionProvider.revisions(owner, scope, id))
  354. this.authorizeMemory(owner, scope, {})
  355. const dir = `${this.shard(scope)}/.dreaming/revisions`
  356. return this.names(join(this.ownerDir(owner), dir)).filter(n => n.startsWith(segment(id) + '-') && n.endsWith('.json')).map(n => this.read<Revision>(owner, `${dir}/${n}`, {} as Revision)).sort((a, b) => b.revision - a.revision)
  357. }
  358. async context(owner: string, agentId: string, projectId?: string): Promise<WorkspaceMemoryContext> {
  359. const actor = { agentId }, agent = this.agent(owner, agentId)
  360. if (agent.temporaryGoalId) return emptyMemoryContext('temporary')
  361. const own = await this.memories(owner, { scope: 'agent', agentId }, actor), user = await this.memories(owner, { scope: 'user' }, actor)
  362. const project = projectId ? await this.memories(owner, { scope: 'project', projectId }, actor) : []
  363. if (projectId) this.requireProjectMember(owner, projectId, agentId)
  364. const prefix = MEMORY_CONTEXT_PREFIX, suffix = MEMORY_CONTEXT_SUFFIX
  365. const groups = [['此 Bot 的记忆', own], ['当前项目记忆', project], ['用户共享记忆', user]] as const
  366. const rows: MemoryContextFact[][] = groups.map(() => [])
  367. const identity = (memory: WorkspaceMemory) => JSON.stringify([memory.scope, memory.agentId, memory.projectId ?? null, memory.id])
  368. const fingerprint = (memory: WorkspaceMemory) => hash(json([memory.content, memory.tier]))
  369. let budget = MEMORY_CONTEXT_MAX_CHARS - prefix.length - suffix.length, populated = 0
  370. // Round-robin scopes so one large private history cannot consume all shared context.
  371. for (const tier of ['profile', 'log'] as const) {
  372. const candidates = groups.map(([, memories]) => memories.filter(memory => memory.tier === tier).slice(0, tier === 'profile' ? 20 : 40))
  373. for (let index = 0; index < Math.max(...candidates.map(items => items.length)); index++) {
  374. for (const [scope, items] of candidates.entries()) {
  375. const memory = items[index], selected = rows[scope]!
  376. if (!memory || selected.length >= 40) continue
  377. const row = `- ${memory.content}(${memory.tier};来源 Bot ${memory.agentId};记忆 ID ${memory.id})`
  378. const cost = row.length + (selected.length ? 1 : groups[scope]![0].length + 2 + (populated ? 2 : 0))
  379. if (cost > budget) continue
  380. if (!selected.length) populated++
  381. selected.push({ id: identity(memory), fingerprint: fingerprint(memory), group: groups[scope]![0], text: row })
  382. budget -= cost
  383. }
  384. }
  385. }
  386. const facts = rows.flat(), scope = hash(json([owner, agentId, projectId ?? null]))
  387. const records = Object.fromEntries([...own, ...user, ...project].map(memory => [identity(memory), fingerprint(memory)]))
  388. // Metadata, unselected facts and provider list ordering do not change the
  389. // injected version. Full records remain available for revocation checks.
  390. return { text: renderMemoryFacts(facts), version: hash(json([scope, facts.map(fact => [fact.id, fact.fingerprint]).sort()])), scope, records, facts }
  391. }
  392. emitMemoryChanged(owner: string): void { this.onChanged(owner) }
  393. enqueueJob(owner: string, job: Omit<WorkspaceMemoryJob, 'id' | 'status' | 'attempts' | 'nextAt' | 'createdAt' | 'updatedAt'>): WorkspaceMemoryJob {
  394. const id = hash(`${job.sourceMessageId}:${job.agentId}`).slice(0, 32), path = `agents/${segment(job.agentId)}/memory/.dreaming/jobs/${id}.json`
  395. const existing = this.read<WorkspaceMemoryJob | null>(owner, path, null)
  396. if (existing) return existing
  397. const now = Date.now(), result: WorkspaceMemoryJob = { ...job, id, status: 'pending', attempts: 0, nextAt: now, createdAt: now, updatedAt: now }
  398. return this.command(owner, `job-${id}`, result, 'memory.job.changed', () => ({ result, writes: [{ path, content: json(result) }] }))
  399. }
  400. jobs(owner: string, agentId?: string): WorkspaceMemoryJob[] {
  401. const ids = agentId ? [segment(agentId)] : this.names(join(this.ownerDir(owner), 'agents')).filter(id => /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(id))
  402. return ids.flatMap(id => {
  403. const dir = `agents/${segment(id)}/memory/.dreaming/jobs`
  404. return this.names(join(this.ownerDir(owner), dir)).filter(n => n.endsWith('.json')).map(n => this.read<WorkspaceMemoryJob>(owner, `${dir}/${n}`, {} as WorkspaceMemoryJob))
  405. }).sort((a, b) => b.createdAt - a.createdAt)
  406. }
  407. saveJob(owner: string, job: WorkspaceMemoryJob): void {
  408. const result = { ...job, updatedAt: Date.now() }
  409. this.command(owner, randomUUID(), result, 'memory.job.changed', () => ({ result, writes: [{ path: `agents/${segment(job.agentId)}/memory/.dreaming/jobs/${segment(job.id)}.json`, content: json(result) }, ...(terminalJobs.has(job.status) ? [{ path: `agents/${segment(job.agentId)}/memory/.dreaming/next-refresh-at`, content: String(Date.now() + 86400000) + '\n' }] : [])] }))
  410. }
  411. }