dsh-world 0.6.5 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/CONTRIBUTING.md +163 -161
  3. package/INSTALL.md +314 -314
  4. package/LICENSE +21 -21
  5. package/NOTES.md +173 -0
  6. package/README.md +236 -235
  7. package/client/index.js +849 -738
  8. package/cordis.patch.yml +6 -6
  9. package/lib/compaction.d.ts +19 -0
  10. package/lib/compaction.js +34 -10
  11. package/lib/compactionSwap.d.ts +29 -2
  12. package/lib/compactionSwap.js +62 -7
  13. package/lib/console/homeApply.d.ts +11 -2
  14. package/lib/console/homeApply.js +36 -7
  15. package/lib/console/routes.d.ts +8 -0
  16. package/lib/console/routes.js +32 -0
  17. package/lib/console/snapshot.d.ts +3 -0
  18. package/lib/console/snapshot.js +2 -1
  19. package/lib/ecosystem/bridge/route.d.ts +7 -0
  20. package/lib/ecosystem/bridge/route.js +30 -0
  21. package/lib/hostContract.d.ts +1 -0
  22. package/lib/index.js +37 -12
  23. package/lib/memory.d.ts +57 -0
  24. package/lib/memory.js +210 -0
  25. package/lib/memoryFiles.d.ts +53 -0
  26. package/lib/memoryFiles.js +193 -0
  27. package/lib/memoryStore.d.ts +114 -0
  28. package/lib/memoryStore.js +159 -0
  29. package/lib/memoryTools.d.ts +31 -0
  30. package/lib/memoryTools.js +106 -0
  31. package/lib/promptSection.d.ts +5 -5
  32. package/lib/promptSection.js +12 -12
  33. package/lib/runtime.d.ts +1 -1
  34. package/lib/runtime.js +84 -29
  35. package/lib/sourceArchive.d.ts +1 -0
  36. package/lib/sourceArchive.js +1 -1
  37. package/lib/store.d.ts +14 -0
  38. package/lib/store.js +11 -1
  39. package/lib/worldPrompt.d.ts +6 -0
  40. package/lib/worldPrompt.js +7 -1
  41. package/lib/worldSwitch.d.ts +3 -2
  42. package/lib/worldSwitch.js +7 -6
  43. package/package.json +128 -115
  44. package/scripts/compact-home.mjs +29 -0
  45. package/scripts/enable-tool-free-compaction.ps1 +33 -33
  46. package/scripts/import-stickers.mjs +17 -17
  47. package/scripts/update-world.mjs +5 -3
  48. package/src/ecosystem/wechat/windows/README.md +6 -4
  49. package/src/ecosystem/wechat/windows/adapter.py +80 -31
  50. package/src/ecosystem/wechat/windows/engine/NOTICE.md +1 -1
  51. package/src/ecosystem/wechat/windows/engine/bridge.cpp +9 -1
  52. package/src/ecosystem/wechat/windows/engine/profiles.json +114 -58
  53. package/src/ecosystem/wechat/windows/native.py +4 -1
  54. package/src/ecosystem/wechat/windows/outgoing.py +21 -0
  55. package/src/ecosystem/wechat/windows/presence.py +109 -109
  56. package/src/ecosystem/wechat/windows/sender.py +9 -1
  57. package/templates/alarm.md +3 -4
  58. package/templates/world-prompt-memory.md +10 -0
  59. package/templates/world-prompt.md +7 -2
package/lib/index.js CHANGED
@@ -3,12 +3,12 @@ import { join } from 'node:path';
3
3
  import z from '@deepseek-ai/schemastery';
4
4
  import { resolveDshHome } from '@deepseek-ai/dsh-home-paths';
5
5
  import { createBridge } from "./ecosystem/bridge/bridge.js";
6
- import { bridgeRoute } from "./ecosystem/bridge/route.js";
6
+ import { bridgeRoute, compactRoute } from "./ecosystem/bridge/route.js";
7
7
  import { consoleRoute } from "./console/routes.js";
8
8
  import { consoleSources } from "./console/snapshot.js";
9
- import { createHomeApply } from "./console/homeApply.js";
9
+ import { compactionFor, createHomeApply } from "./console/homeApply.js";
10
10
  import { errorText, Faults } from "./faults.js";
11
- import { swapCompactionOnce } from "./compactionSwap.js";
11
+ import { swapCompactionOnce, swapPresetCompaction, swapStockCompaction } from "./compactionSwap.js";
12
12
  import { hostLife } from "./life/host.js";
13
13
  import { createLifeStore, durableWrite } from "./life/lifeStore.js";
14
14
  import { toastSupported, windowsToast } from "./life/notify.js";
@@ -17,9 +17,10 @@ import { createOutlet } from "./ecosystem/shared/outlets.js";
17
17
  import { readPersona, writePersona } from "./persona.js";
18
18
  import { qqRoute } from "./ecosystem/qq/route.js";
19
19
  import { createConsoleDoors, discoverSnowlumaHome } from "./ecosystem/qq/panel.js";
20
+ import { createMemory } from "./memory.js";
20
21
  import { createWorldRuntime } from "./runtime.js";
21
22
  import { createArchiveStore } from "./store.js";
22
- import { DEFAULT_TEMPLATE_FILE, roomTitle, worldNames } from "./worldPrompt.js";
23
+ import { DEFAULT_TEMPLATE_FILE, MEMORY_SNIPPET_FILE, memoryTemplateGaps, roomTitle, worldNames } from "./worldPrompt.js";
23
24
  import { createWorldSwitch } from "./worldSwitch.js";
24
25
  import { createWechatModule } from "./ecosystem/wechat/index.js";
25
26
  import { createWechatRecorder } from "./ecosystem/wechat/archive.js";
@@ -118,7 +119,8 @@ export async function apply(ctx, config) {
118
119
  const selfFile = config.selfFile || join(worldDir, 'room', '我是谁.md');
119
120
  const personaFile = config.personaFile || join(worldDir, 'persona.md');
120
121
  const worldPromptFile = config.worldPromptFile || join(worldDir, 'world-prompt.md');
121
- const options = { worldDir, roomDir: join(worldDir, 'room'), diaryDir: config.diaryDir || join(worldDir, 'diary'), selfFile, fallbackWakeTime: config.fallbackWakeTime, timeZone: config.timeZone, alarmFile: config.alarmTemplateFile || join(worldDir, 'alarm.md') };
122
+ const roomDir = join(worldDir, 'room');
123
+ const options = { worldDir, roomDir, diaryDir: config.diaryDir || join(worldDir, 'diary'), selfFile, fallbackWakeTime: config.fallbackWakeTime, timeZone: config.timeZone, alarmFile: config.alarmTemplateFile || join(worldDir, 'alarm.md') };
122
124
  /** The name in her persona comes first, then the configured one. */
123
125
  const names = () => worldNames({ ...config, companionName: readPersona(personaFile).名字 || config.companionName });
124
126
  const lifeStore = (() => {
@@ -142,13 +144,17 @@ export async function apply(ctx, config) {
142
144
  /** Where her world reaches: her room's workspace, and in it the sessions of her preset. */
143
145
  const inWorkspace = createWorkspaceMembership(ctx, options.roomDir);
144
146
  const inWorldSession = (session) => inWorld(session, config, inWorkspace);
147
+ /** Long-term memory over the archive and her own files; the tools and the console are two doors to it. */
148
+ const memoryService = createMemory({ store: store.memory, timeZone: config.timeZone, sources: { diaryDir: options.diaryDir }, visible: id => archiveSession(id) });
149
+ /** What is wrong with long-term memory, in the console's health list: a source that could not be read. */
150
+ const memoryHealth = () => memoryService.status().failures.map(failure => `记忆来源 ${failure.sourceId}:${failure.message}`);
145
151
  let memory;
146
152
  let stopLifeLine;
147
153
  async function startMemory(fresh) {
148
154
  if (memory)
149
155
  return;
150
- const prompt = { file: worldPromptFile, persona: personaFile, conversationsDir: join(worldDir, 'conversations'), names: names(), ...(config.life ? { life: options } : {}) };
151
- memory = createWorldRuntime(ctx, { ...memoryConfig, selfFile }, store, { prompt, names, isHome: id => isHome(id), archiveSession, inWorkspace }, power.memoryTurns);
156
+ const prompt = { file: worldPromptFile, persona: personaFile, conversationsDir: join(worldDir, 'conversations'), names: names(), memoryTools: true, ...(config.life ? { life: options } : {}) };
157
+ memory = createWorldRuntime(ctx, { ...memoryConfig, selfFile }, store, { prompt, names, isHome: id => isHome(id), archiveSession, inWorkspace, memory: memoryService }, power.memoryTurns);
152
158
  await memory.start({ fresh });
153
159
  }
154
160
  async function stopMemory() {
@@ -172,8 +178,21 @@ export async function apply(ctx, config) {
172
178
  ctx.inject(['sessionController', 'permissionPresets', 'webServer'], async (lifeCtx) => {
173
179
  const webServer = lifeCtx.webServer;
174
180
  const host = hostLife(lifeCtx);
175
- swapCompactionOnce({ enabled: config.replaceCompaction, profileDir: lifeCtx.get('profileContext')?.dir, done: power.compactionSwapped, markDone: power.markCompactionSwapped,
181
+ const configEditor = lifeCtx.get('configEditor');
182
+ const swapCompaction = () => swapCompactionOnce({ enabled: config.replaceCompaction, profileDir: lifeCtx.get('profileContext')?.dir, done: power.compactionSwapped, markDone: power.markCompactionSwapped,
183
+ swap: async (dir) => { const row = swapStockCompaction(dir); return row.status === 'unchanged' && configEditor ? swapPresetCompaction(configEditor, config.agentPreset, dir) : row; },
176
184
  report: message => { faults.set('compaction-swap', message); lifeCtx.logger.warn(`dsh-world: ${message}`); } });
185
+ // The preset row the editor rewrites is active only once the host has finished loading.
186
+ const appReady = lifeCtx.get('appReady');
187
+ if (!appReady)
188
+ void swapCompaction();
189
+ else
190
+ lifeCtx.effect(() => {
191
+ let closed = false;
192
+ const cancel = appReady.onReady(() => { if (!closed)
193
+ void swapCompaction(); });
194
+ return () => { closed = true; cancel(); };
195
+ }, 'dsh-world compaction swap');
177
196
  const permissions = createWorkspacePermissions(lifeCtx, options.roomDir, power);
178
197
  // QQ 的两扇门:她自己那条 OneBot 通道的状态与开关,以及 SnowLuma 的控制台。**无条件注册** —— 通道
179
198
  // 关着的时候 /native 回 null,而 SnowLuma 那扇门跟通道没关系。(这里原来还有一半是给外面那个
@@ -202,10 +221,14 @@ export async function apply(ctx, config) {
202
221
  home: async () => { if (!life)
203
222
  throw new Error('请先启动世界生活线'); return life.home(); },
204
223
  isHome: agent => !!life?.isHome(agent.id) && ctx.agents.get(agent.id) === agent && !host.isArchived(agent.id) && inWorldSession(agent.session),
205
- compaction: () => lifeCtx.get('compaction'),
224
+ compaction: agent => compactionFor(lifeCtx, agent),
206
225
  refresh: session => { if (!memory)
207
226
  throw new Error('世界记忆线已停止'); memory.refreshPrompt(session); },
208
227
  changed: () => sources.invalidate(),
228
+ fault: message => { if (message)
229
+ faults.set('home-apply', message);
230
+ else
231
+ faults.clear('home-apply'); },
209
232
  });
210
233
  let bridge;
211
234
  let wechat;
@@ -350,9 +373,11 @@ export async function apply(ctx, config) {
350
373
  // The host matches exact routes before the longest prefix, so this path never reaches the console.
351
374
  if (config.bridge)
352
375
  lifeCtx.effect(() => webServer.register(bridgeRoute(() => bridge, config.allowedHosts)), 'dsh-world bridge route');
376
+ if (config.bridge)
377
+ lifeCtx.effect(() => webServer.register(compactRoute(() => bridge, () => !power.running() || !life ? '请先启动世界生活线' : homeApply.request(), config.allowedHosts)), 'dsh-world compact route');
353
378
  // Her album lives in her room, so the console shows it whether or not the life line is running.
354
- const sources = consoleSources({ ctx: lifeCtx, config: memoryConfig, archive: store, names, ...(lifeStore ? { lifeStore } : {}), life: () => life, homeApply: homeApply.snapshot, compactAfterReview: power.compactAfterReview, photos, diaryDir: options.diaryDir, running: power.running, dailyBudget: power.dailyBudget, memoryTurns: power.memoryTurns, selectedArchiveScope: power.selectedArchiveScope, workspacePermissions: permissions.snapshot, wechat: () => wechat ? { ...wechat.status(), adapter: adapter?.status() ?? null } : undefined, archiveSession, native: () => qqNative?.status() ?? null,
355
- modules: () => ({ wechat: { available: config.wechat, enabled: power.wechatEnabled(), running: wechat !== undefined }, qq: { available: Boolean(config.qqNative?.enabled), enabled: power.qqEnabled(), running: qqNative !== undefined, ...(qqSdk.needsRestart() ? { restartRequired: true } : {}) } }), faults: () => [...faults.list(), ...(memory?.faults() ?? []), ...(bridge?.faults() ?? [])] });
379
+ const sources = consoleSources({ ctx: lifeCtx, config: memoryConfig, archive: store, longTerm: memoryService, names, ...(lifeStore ? { lifeStore } : {}), life: () => life, homeApply: homeApply.snapshot, compactAfterReview: power.compactAfterReview, photos, diaryDir: options.diaryDir, running: power.running, dailyBudget: power.dailyBudget, memoryTurns: power.memoryTurns, selectedArchiveScope: power.selectedArchiveScope, workspacePermissions: permissions.snapshot, wechat: () => wechat ? { ...wechat.status(), adapter: adapter?.status() ?? null } : undefined, archiveSession, native: () => qqNative?.status() ?? null,
380
+ modules: () => ({ wechat: { available: config.wechat, enabled: power.wechatEnabled(), running: wechat !== undefined }, qq: { available: Boolean(config.qqNative?.enabled), enabled: power.qqEnabled(), running: qqNative !== undefined, ...(qqSdk.needsRestart() ? { restartRequired: true } : {}) } }), faults: () => [...faults.list(), ...(memory?.faults() ?? []), ...(bridge?.faults() ?? []), ...memoryHealth()] });
356
381
  const actions = {
357
382
  async start() {
358
383
  if (power.running())
@@ -381,7 +406,7 @@ export async function apply(ctx, config) {
381
406
  applyHome: () => !power.running() || !life ? '请先启动世界生活线' : homeApply.request(),
382
407
  prompts: () => ({
383
408
  persona: readPersona(personaFile),
384
- worldPrompt: existsSync(worldPromptFile) ? { text: readFileSync(worldPromptFile, 'utf8'), custom: true } : { text: readFileSync(DEFAULT_TEMPLATE_FILE, 'utf8'), custom: false },
409
+ worldPrompt: existsSync(worldPromptFile) ? (text => ({ text, custom: true, memory: { missing: memoryTemplateGaps(text), snippet: readFileSync(MEMORY_SNIPPET_FILE, 'utf8') } }))(readFileSync(worldPromptFile, 'utf8')) : { text: readFileSync(DEFAULT_TEMPLATE_FILE, 'utf8'), custom: false },
385
410
  }),
386
411
  savePersona: persona => { writePersona(personaFile, { ...readPersona(personaFile), ...persona }); sources.invalidate(); },
387
412
  setDailyBudget: yuan => { power.setDailyBudget(yuan); sources.invalidate(); },
@@ -0,0 +1,57 @@
1
+ import { type MemorySources } from './memoryFiles.ts';
2
+ import { type FindParams, type MemoryItem, type MemoryKind, type MemoryStatus, type MemoryStore, type ReadResult } from './memoryStore.ts';
3
+ export { MEMORY_KINDS, MemoryError } from './memoryStore.ts';
4
+ export { readStable } from './memoryFiles.ts';
5
+ export type { MemoryErrorCode, MemoryItem, MemoryKind, MemoryStatus } from './memoryStore.ts';
6
+ export type { MemorySources } from './memoryFiles.ts';
7
+ export interface MemoryPage {
8
+ items: MemoryItem[];
9
+ nextCursor?: string;
10
+ coverage: MemoryStatus;
11
+ search: {
12
+ method: 'browse' | 'scan';
13
+ elapsedMs: number;
14
+ };
15
+ }
16
+ export interface MemoryContent {
17
+ ref: string;
18
+ sourceId: string;
19
+ kind: MemoryKind;
20
+ version: string;
21
+ title: string;
22
+ time: string;
23
+ timeBasis: ReadResult['timeBasis'];
24
+ current: boolean;
25
+ content: string;
26
+ offset: number;
27
+ nextOffset?: number;
28
+ totalChars: number;
29
+ turnId?: string;
30
+ origin?: ReadResult['origin'];
31
+ }
32
+ export interface MemoryService {
33
+ find(input: Record<string, unknown>): Promise<MemoryPage>;
34
+ read(input: Record<string, unknown>): Promise<MemoryContent>;
35
+ status(): MemoryStatus;
36
+ }
37
+ export interface MemoryDeps {
38
+ store: MemoryStore;
39
+ timeZone: string;
40
+ sources: MemorySources;
41
+ /** Whether the caller may see this chat session; may throw while the host cannot say yet, which counts as no. */
42
+ visible(sessionId: string): boolean;
43
+ now?: () => Date;
44
+ }
45
+ export declare const DEFAULT_FIND_LIMIT = 20, MAX_FIND_LIMIT = 100, DEFAULT_READ_LIMIT = 4000, MAX_READ_LIMIT = 16000;
46
+ /** The one place a query is checked, for the tools and the console alike. */
47
+ export declare function normalizeFind(input: Record<string, unknown>, timeZone: string): FindParams;
48
+ export declare function normalizeRead(input: Record<string, unknown>): {
49
+ ref: string;
50
+ offset: number;
51
+ limit: number;
52
+ };
53
+ /**
54
+ * The one entry to long-term memory: tools and console call the same `find` and `read`. Nothing is indexed: chat is
55
+ * scanned in the archive and her files are read when asked, so there is no queue to wait for and no index to rebuild.
56
+ */
57
+ export declare function createMemory(deps: MemoryDeps): MemoryService;
package/lib/memory.js ADDED
@@ -0,0 +1,210 @@
1
+ import { createAuthorFiles } from "./memoryFiles.js";
2
+ import { MAX_QUERY_CHARS, MEMORY_KINDS, MemoryError, chatRef, decodeRef, excerptOf, sha, } from "./memoryStore.js";
3
+ import { errorText } from "./faults.js";
4
+ export { MEMORY_KINDS, MemoryError } from "./memoryStore.js";
5
+ export { readStable } from "./memoryFiles.js";
6
+ export const DEFAULT_FIND_LIMIT = 20, MAX_FIND_LIMIT = 100, DEFAULT_READ_LIMIT = 4000, MAX_READ_LIMIT = 16000;
7
+ const iso = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/;
8
+ function text(input, key, max) {
9
+ const value = input[key];
10
+ if (value === undefined || value === null || value === '')
11
+ return undefined;
12
+ if (typeof value !== 'string' || value.length > max)
13
+ throw new MemoryError('invalidQuery', `${key} 必须是不超过 ${max} 个字符的字符串`);
14
+ return value;
15
+ }
16
+ function time(input, key, timeZone) {
17
+ const value = text(input, key, 40);
18
+ if (value === undefined)
19
+ return undefined;
20
+ const ms = iso.test(value) ? Date.parse(value) : NaN;
21
+ if (!Number.isFinite(ms))
22
+ throw new MemoryError('invalidQuery', `${key} 需要带时区的 ISO 时间(如 2026-10-30T00:00:00+08:00);日期请按 ${timeZone} 换算成当天零点`);
23
+ return ms;
24
+ }
25
+ function whole(input, key, fallback, min, max) {
26
+ const value = input[key];
27
+ if (value === undefined || value === null)
28
+ return fallback;
29
+ if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < min || value > max)
30
+ throw new MemoryError('invalidQuery', `${key} 必须是 ${min} 到 ${max} 的整数`);
31
+ return value;
32
+ }
33
+ /** The one place a query is checked, for the tools and the console alike. */
34
+ export function normalizeFind(input, timeZone) {
35
+ const query = text(input, 'query', MAX_QUERY_CHARS * 4);
36
+ if (query !== undefined && (Array.from(query).length > MAX_QUERY_CHARS || query.includes('\0')))
37
+ throw new MemoryError('invalidQuery', `query 不能超过 ${MAX_QUERY_CHARS} 个字符,也不能含空字符`);
38
+ const kindsInput = input.kinds;
39
+ if (kindsInput !== undefined && kindsInput !== null && (!Array.isArray(kindsInput) || kindsInput.some(kind => !MEMORY_KINDS.includes(kind))))
40
+ throw new MemoryError('invalidQuery', `kinds 只能是 ${MEMORY_KINDS.join('、')}`);
41
+ const history = input.history;
42
+ if (history !== undefined && history !== null && typeof history !== 'boolean')
43
+ throw new MemoryError('invalidQuery', 'history 必须是 true 或 false');
44
+ const order = input.order ?? 'newest';
45
+ if (order !== 'newest' && order !== 'oldest')
46
+ throw new MemoryError('invalidQuery', 'order 只能是 newest 或 oldest');
47
+ const fromMs = time(input, 'from', timeZone), toMs = time(input, 'to', timeZone);
48
+ if (fromMs !== undefined && toMs !== undefined && fromMs >= toMs)
49
+ throw new MemoryError('invalidQuery', 'from 必须早于 to(区间左闭右开)');
50
+ const kinds = kindsInput?.length ? [...new Set(kindsInput)] : undefined;
51
+ const sourceId = text(input, 'sourceId', 300), turnId = text(input, 'turnId', 300), linkedTo = text(input, 'linkedTo', 2000), cursor = text(input, 'cursor', 4000);
52
+ return {
53
+ history: history === true, order, limit: whole(input, 'limit', DEFAULT_FIND_LIMIT, 1, MAX_FIND_LIMIT),
54
+ ...(query === undefined ? {} : { query }), ...(kinds ? { kinds } : {}), ...(fromMs === undefined ? {} : { fromMs }), ...(toMs === undefined ? {} : { toMs }),
55
+ ...(sourceId ? { sourceId } : {}), ...(turnId ? { turnId } : {}), ...(linkedTo ? { linkedTo } : {}), ...(cursor ? { cursor } : {}),
56
+ };
57
+ }
58
+ export function normalizeRead(input) {
59
+ const ref = text(input, 'ref', 2000);
60
+ if (!ref)
61
+ throw new MemoryError('invalidQuery', 'ref 必须是 memory_find 给出的引用');
62
+ return { ref, offset: whole(input, 'offset', 0, 0, Number.MAX_SAFE_INTEGER), limit: whole(input, 'limit', DEFAULT_READ_LIMIT, 1, MAX_READ_LIMIT) };
63
+ }
64
+ const chatKind = (params) => !params.kinds || params.kinds.includes('conversation');
65
+ const asksForChat = (params) => chatKind(params) && (!params.sourceId || params.sourceId.startsWith('chat:'));
66
+ /** The filters every candidate passes the same way, whichever source it came from. */
67
+ function accepts(candidate, params) {
68
+ const { item } = candidate;
69
+ return (!params.kinds || params.kinds.includes(item.kind)) && (!params.sourceId || item.sourceId === params.sourceId)
70
+ && (params.fromMs === undefined || candidate.timeMs >= params.fromMs) && (params.toMs === undefined || candidate.timeMs < params.toMs)
71
+ && (!params.query || candidate.content.includes(params.query));
72
+ }
73
+ function authorItem(doc, query) {
74
+ return { ref: doc.ref, sourceId: doc.sourceId, kind: doc.kind, title: doc.title, time: doc.time, timeBasis: doc.timeBasis, current: true, excerpt: excerptOf(doc.content, query) };
75
+ }
76
+ const authorCandidate = (doc, query) => ({ item: authorItem(doc, query), content: doc.content, timeMs: doc.timeMs });
77
+ /**
78
+ * The one entry to long-term memory: tools and console call the same `find` and `read`. Nothing is indexed: chat is
79
+ * scanned in the archive and her files are read when asked, so there is no queue to wait for and no index to rebuild.
80
+ */
81
+ export function createMemory(deps) {
82
+ const { store, timeZone } = deps;
83
+ const authors = createAuthorFiles(deps.sources, timeZone);
84
+ const now = deps.now ?? (() => new Date());
85
+ let checkedAt = now().toISOString();
86
+ const scopeFailures = new Map();
87
+ const canSee = (sessionId) => {
88
+ try {
89
+ const yes = deps.visible(sessionId);
90
+ scopeFailures.delete('scope');
91
+ return yes;
92
+ }
93
+ catch (error) {
94
+ scopeFailures.set('scope', `暂时说不清哪些会话在范围内:${errorText(error)}`);
95
+ return false;
96
+ }
97
+ };
98
+ const sessionsInScope = () => store.sessionIds().filter(canSee);
99
+ const docs = async () => { const all = await authors.docs(); checkedAt = now().toISOString(); return all; };
100
+ function status() {
101
+ const failures = [...authors.failures(), ...[...scopeFailures].map(([sourceId, message]) => ({ sourceId, message }))];
102
+ return { state: failures.length ? 'partial' : 'ready', failures, checkedAt };
103
+ }
104
+ function readCursor(cursor, hash, files) {
105
+ let parsed;
106
+ try {
107
+ parsed = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'));
108
+ }
109
+ catch {
110
+ throw new MemoryError('invalidCursor', '翻页游标无效');
111
+ }
112
+ if (typeof parsed.hash !== 'string' || !Number.isSafeInteger(parsed.upTo) || !Number.isSafeInteger(parsed.revisions) || typeof parsed.files !== 'string' || !Number.isSafeInteger(parsed.offset) || parsed.offset < 0)
113
+ throw new MemoryError('invalidCursor', '翻页游标无效');
114
+ if (parsed.hash !== hash || parsed.revisions !== store.snapshot().revisions || parsed.files !== files)
115
+ throw new MemoryError('cursorExpired', '查询条件、访问范围或资料已经变化,请重新查询(cursorExpired)');
116
+ return parsed;
117
+ }
118
+ const cursorOf = (state) => Buffer.from(JSON.stringify(state)).toString('base64url');
119
+ /** What a link-following query looks at: the files that cite the ref and what the ref's own file cites, never a line she may not see. */
120
+ function linked(ref, all, query) {
121
+ const found = new Map();
122
+ for (const doc of all)
123
+ if (doc.links.includes(ref))
124
+ found.set(doc.ref, authorCandidate(doc, query));
125
+ for (const target of all.find(doc => doc.ref === ref)?.links ?? []) {
126
+ let decoded;
127
+ try {
128
+ decoded = decodeRef(target);
129
+ }
130
+ catch {
131
+ continue;
132
+ }
133
+ const file = decoded.kind === 'a' ? all.find(doc => doc.ref === target) : undefined;
134
+ if (file) {
135
+ found.set(file.ref, authorCandidate(file, query));
136
+ continue;
137
+ }
138
+ const record = decoded.kind === 'c' ? store.byRecordId(decoded.recordId) : undefined;
139
+ if (record && canSee(record.sourceSessionId) && chatRef(record.recordId, record.content) === target)
140
+ found.set(target, { item: store.item(record, query), content: record.content, timeMs: Date.parse(record.occurredAt) });
141
+ }
142
+ return [...found.values()];
143
+ }
144
+ async function find(input) {
145
+ const started = performance.now();
146
+ const params = normalizeFind(input, timeZone);
147
+ const all = await docs();
148
+ const sessions = sessionsInScope();
149
+ if (params.linkedTo) {
150
+ const session = store.refSession(params.linkedTo);
151
+ if (session !== undefined && !canSee(session))
152
+ throw new MemoryError('outOfScope', 'linkedTo 指向的聊天不在当前可读范围内');
153
+ }
154
+ const hash = sha(JSON.stringify([params.query ?? null, params.kinds ?? null, params.fromMs ?? null, params.toMs ?? null, params.sourceId ?? null, params.turnId ?? null, params.linkedTo ?? null, params.order, [...sessions].sort()]));
155
+ const files = sha(all.map(doc => doc.sourceId + doc.ref).sort().join('\n'));
156
+ const resumed = params.cursor ? readCursor(params.cursor, hash, files) : undefined;
157
+ const upTo = resumed?.upTo ?? store.snapshot().head, offset = resumed?.offset ?? 0;
158
+ const want = offset + params.limit + 1;
159
+ let candidates;
160
+ if (params.linkedTo)
161
+ candidates = linked(params.linkedTo, all, params.query).filter(candidate => accepts(candidate, params));
162
+ else {
163
+ const chat = asksForChat(params) ? store.find(params, sessions, upTo, want).map((record) => ({ item: store.item(record, params.query), content: record.content, timeMs: Date.parse(record.occurredAt) })) : [];
164
+ const written = params.turnId ? [] : all.map(doc => authorCandidate(doc, params.query)).filter(candidate => accepts(candidate, params));
165
+ candidates = [...chat, ...written];
166
+ }
167
+ // A turn is read in the order it was said; everything else by time, ties keeping the order the sources gave.
168
+ if (!params.turnId) {
169
+ const way = params.order === 'newest' ? -1 : 1;
170
+ candidates = candidates.map((candidate, index) => ({ candidate, index })).sort((a, b) => way * (a.candidate.timeMs - b.candidate.timeMs) || a.index - b.index).map(entry => entry.candidate);
171
+ }
172
+ const page = candidates.slice(offset, offset + params.limit);
173
+ // Membership is asked again on what is about to be returned: a session that left the scope meanwhile is refused, not trimmed.
174
+ for (const { item } of page)
175
+ if (item.kind === 'conversation' && item.origin?.sessionId && !canSee(item.origin.sessionId))
176
+ throw new MemoryError('scopeChanged', '查询期间可读范围变了,请重新查询');
177
+ const more = candidates.length > offset + params.limit;
178
+ return {
179
+ items: page.map(candidate => candidate.item),
180
+ ...(more ? { nextCursor: cursorOf({ hash, upTo, revisions: store.snapshot().revisions, files, offset: offset + params.limit }) } : {}),
181
+ coverage: status(), search: { method: params.query ? 'scan' : 'browse', elapsedMs: Math.round((performance.now() - started) * 10) / 10 },
182
+ };
183
+ }
184
+ async function read(input) {
185
+ const { ref, offset, limit } = normalizeRead(input);
186
+ const decoded = decodeRef(ref);
187
+ let found;
188
+ if (decoded.kind === 'c') {
189
+ found = store.read(ref, canSee);
190
+ if (found.origin?.sessionId && !canSee(found.origin.sessionId))
191
+ throw new MemoryError('scopeChanged', '读取期间可读范围变了,请重新读取');
192
+ }
193
+ else {
194
+ const doc = (await docs()).find(candidate => candidate.sourceId === decoded.sourceId);
195
+ if (!doc)
196
+ throw new MemoryError('notFound', '这条引用对应的文件不存在');
197
+ if (doc.ref !== ref)
198
+ throw new MemoryError('sourceChanged', '这个文件在引用之后被改过,旧版正文没有保留(sourceChanged)');
199
+ found = { ref, sourceId: doc.sourceId, kind: doc.kind, version: decoded.hash, title: doc.title, time: doc.time, timeBasis: doc.timeBasis, current: true, content: doc.content };
200
+ }
201
+ const chars = Array.from(found.content);
202
+ const end = Math.min(chars.length, offset + limit);
203
+ return {
204
+ ref: found.ref, sourceId: found.sourceId, kind: found.kind, version: found.version, title: found.title, time: found.time, timeBasis: found.timeBasis, current: found.current,
205
+ content: chars.slice(offset, end).join(''), offset, ...(end < chars.length ? { nextOffset: end } : {}), totalChars: chars.length,
206
+ ...(found.turnId ? { turnId: found.turnId } : {}), ...(found.origin ? { origin: found.origin } : {}),
207
+ };
208
+ }
209
+ return { find, read, status };
210
+ }
@@ -0,0 +1,53 @@
1
+ import { type MemoryKind, type TimeBasis } from './memoryStore.ts';
2
+ /** Where she keeps what she writes. A file authorizes that file alone; a directory its markdown and text files, a few levels deep. */
3
+ export interface MemorySources {
4
+ diaryDir?: string;
5
+ selfFile?: string;
6
+ focusFile?: string;
7
+ knowledgeDir?: string;
8
+ }
9
+ export type AuthorKind = Exclude<MemoryKind, 'conversation'>;
10
+ /** One file as she wrote it, with the time it has and the ref that is bound to this exact text. */
11
+ export interface AuthorDoc {
12
+ sourceId: string;
13
+ kind: AuthorKind;
14
+ path: string;
15
+ title: string;
16
+ content: string;
17
+ ref: string;
18
+ links: string[];
19
+ timeMs: number;
20
+ time: string;
21
+ timeBasis: TimeBasis;
22
+ }
23
+ type Read = {
24
+ kind: 'ok';
25
+ content: string;
26
+ stat: string;
27
+ mtimeMs: number;
28
+ } | {
29
+ kind: 'missing';
30
+ } | {
31
+ kind: 'failed';
32
+ message: string;
33
+ };
34
+ /**
35
+ * Reads a file the way its text may be used: stat before and after must agree and the bytes must be valid UTF-8. A
36
+ * file that is changing under us, or cannot be read or decoded, is a failure to retry, never new content and never a
37
+ * deletion. A writer that appends valid text piece by piece cannot be told apart from a finished file.
38
+ */
39
+ export declare function readStable(path: string, between?: () => Promise<void>): Promise<Read>;
40
+ /**
41
+ * What she wrote, read on demand. Files are few and small, so a query looks at the configured places directly; a
42
+ * file whose size and time are unchanged is not read again. A file that cannot be read keeps its last text and is
43
+ * reported; only a file that is really gone leaves the results.
44
+ */
45
+ export declare function createAuthorFiles(sources: MemorySources, timeZone: string): {
46
+ docs: () => Promise<AuthorDoc[]>;
47
+ failures: () => {
48
+ sourceId: string;
49
+ message: string;
50
+ }[];
51
+ };
52
+ export type AuthorFiles = ReturnType<typeof createAuthorFiles>;
53
+ export {};
@@ -0,0 +1,193 @@
1
+ import { readFile, readdir, realpath, stat } from 'node:fs/promises';
2
+ import { isAbsolute, join, relative } from 'node:path';
3
+ import { errorText } from "./faults.js";
4
+ import { formatLocalTime } from "./timeContext.js";
5
+ import { extractLinks, fileRef, localDayStart } from "./memoryStore.js";
6
+ const MAX_FILE_BYTES = 2 * 1024 * 1024, MAX_FILES_PER_DIR = 2000;
7
+ const DIR_DEPTH = { diary: 1, knowledge: 4 };
8
+ const sameFile = (a, b) => a.size === b.size && a.mtimeMs === b.mtimeMs;
9
+ /** Windows compares paths without case. */
10
+ const normalized = (path) => process.platform === 'win32' ? path.toLowerCase() : path;
11
+ const within = (root, path) => { const rel = relative(normalized(root), normalized(path)); return rel === '' || (!/^\.\.(?:[\\/]|$)/.test(rel) && !isAbsolute(rel)); };
12
+ const diaryDate = (path) => /(?:^|[\\/])(\d{4}-\d{2}-\d{2})(?:\.[^\\/]*)?$/.exec(path)?.[1];
13
+ /**
14
+ * Reads a file the way its text may be used: stat before and after must agree and the bytes must be valid UTF-8. A
15
+ * file that is changing under us, or cannot be read or decoded, is a failure to retry, never new content and never a
16
+ * deletion. A writer that appends valid text piece by piece cannot be told apart from a finished file.
17
+ */
18
+ export async function readStable(path, between) {
19
+ try {
20
+ const before = await stat(path);
21
+ if (!before.isFile())
22
+ return { kind: 'failed', message: '不是普通文件' };
23
+ if (before.size > MAX_FILE_BYTES)
24
+ return { kind: 'failed', message: `文件超过 ${MAX_FILE_BYTES / 1024 / 1024} MB,没有读取` };
25
+ const bytes = await readFile(path);
26
+ await between?.();
27
+ const after = await stat(path);
28
+ if (!sameFile(before, after) || bytes.length !== after.size)
29
+ return { kind: 'failed', message: '读取时文件还在变化,下次再试' };
30
+ const content = new TextDecoder('utf-8', { fatal: true }).decode(bytes).replace(/^/, '');
31
+ return { kind: 'ok', content, stat: `${after.size}:${after.mtimeMs}`, mtimeMs: after.mtimeMs };
32
+ }
33
+ catch (error) {
34
+ if (error.code === 'ENOENT')
35
+ return { kind: 'missing' };
36
+ return { kind: 'failed', message: error instanceof TypeError && /encoded data|decode/i.test(error.message) ? '不是有效的 UTF-8 文本' : errorText(error) };
37
+ }
38
+ }
39
+ /** Lists a configured directory only: real paths must stay inside it, so links cannot lead out, and nothing is followed from file contents. */
40
+ async function listDirectory(root, depth) {
41
+ let realRoot;
42
+ try {
43
+ realRoot = await realpath(root);
44
+ }
45
+ catch (error) {
46
+ return error.code === 'ENOENT' ? { kind: 'missing' } : { kind: 'failed', message: errorText(error) };
47
+ }
48
+ const files = [];
49
+ const visited = new Set();
50
+ async function walk(directory, prefix, level) {
51
+ const real = await realpath(directory);
52
+ if (!within(realRoot, real) || visited.has(normalized(real)))
53
+ return;
54
+ visited.add(normalized(real));
55
+ for (const entry of await readdir(directory, { withFileTypes: true })) {
56
+ if (entry.name.startsWith('.'))
57
+ continue;
58
+ const path = join(directory, entry.name);
59
+ let target;
60
+ try {
61
+ target = await realpath(path);
62
+ }
63
+ catch {
64
+ continue;
65
+ }
66
+ if (!within(realRoot, target))
67
+ continue;
68
+ const info = await stat(path).catch(() => undefined);
69
+ if (info?.isDirectory() && level < depth)
70
+ await walk(path, `${prefix}${entry.name}/`, level + 1);
71
+ else if (info?.isFile() && /\.(md|txt)$/i.test(entry.name)) {
72
+ if (files.length >= MAX_FILES_PER_DIR)
73
+ throw new Error(`文件超过 ${MAX_FILES_PER_DIR} 个,后面的没有读取`);
74
+ files.push({ path, id: `${prefix}${entry.name}` });
75
+ }
76
+ }
77
+ }
78
+ try {
79
+ await walk(root, '', 1);
80
+ return { kind: 'ok', files };
81
+ }
82
+ catch (error) {
83
+ return { kind: 'failed', message: errorText(error) };
84
+ }
85
+ }
86
+ /**
87
+ * What she wrote, read on demand. Files are few and small, so a query looks at the configured places directly; a
88
+ * file whose size and time are unchanged is not read again. A file that cannot be read keeps its last text and is
89
+ * reported; only a file that is really gone leaves the results.
90
+ */
91
+ export function createAuthorFiles(sources, timeZone) {
92
+ const cache = new Map();
93
+ const failures = new Map();
94
+ let running, again = false;
95
+ async function load(sourceId, kind, path) {
96
+ const known = cache.get(sourceId);
97
+ if (known?.path === path) {
98
+ const info = await stat(path).catch(() => undefined);
99
+ if (info?.isFile() && `${info.size}:${info.mtimeMs}` === known.stat) {
100
+ failures.delete(sourceId);
101
+ return;
102
+ }
103
+ }
104
+ const result = await readStable(path);
105
+ if (result.kind === 'failed') {
106
+ failures.set(sourceId, `${path}:${result.message}`);
107
+ return;
108
+ }
109
+ failures.delete(sourceId);
110
+ if (result.kind === 'missing' || !result.content.trim())
111
+ cache.delete(sourceId);
112
+ else
113
+ cache.set(sourceId, { kind, path, stat: result.stat, mtimeMs: result.mtimeMs, content: result.content });
114
+ }
115
+ async function loadDirectory(kind, root) {
116
+ const listing = await listDirectory(root, DIR_DEPTH[kind]);
117
+ const key = `dir:${kind}`;
118
+ if (listing.kind === 'failed') {
119
+ failures.set(key, `${root}:${listing.message}`);
120
+ return;
121
+ }
122
+ failures.delete(key);
123
+ const present = new Set();
124
+ for (const file of listing.kind === 'ok' ? listing.files : []) {
125
+ const sourceId = `${kind}:${file.id}`;
126
+ present.add(sourceId);
127
+ await load(sourceId, kind, file.path);
128
+ }
129
+ for (const [sourceId, entry] of cache)
130
+ if (entry.kind === kind && !present.has(sourceId)) {
131
+ cache.delete(sourceId);
132
+ failures.delete(sourceId);
133
+ }
134
+ }
135
+ async function refreshOnce() {
136
+ for (const [kind, path] of [['self', sources.selfFile], ['focus', sources.focusFile]]) {
137
+ if (!path)
138
+ continue;
139
+ try {
140
+ await load(kind, kind, path);
141
+ }
142
+ catch (error) {
143
+ failures.set(kind, `${path}:${errorText(error)}`);
144
+ }
145
+ }
146
+ for (const [kind, root] of [['diary', sources.diaryDir], ['knowledge', sources.knowledgeDir]]) {
147
+ if (!root)
148
+ continue;
149
+ try {
150
+ await loadDirectory(kind, root);
151
+ }
152
+ catch (error) {
153
+ failures.set(`dir:${kind}`, `${root}:${errorText(error)}`);
154
+ }
155
+ }
156
+ }
157
+ /** Calls arriving during a refresh join it and make it run once more, so a file saved just now is never missed. */
158
+ function refresh() {
159
+ if (running) {
160
+ again = true;
161
+ return running;
162
+ }
163
+ running = (async () => {
164
+ try {
165
+ do {
166
+ again = false;
167
+ await refreshOnce();
168
+ } while (again);
169
+ }
170
+ finally {
171
+ running = undefined;
172
+ }
173
+ })();
174
+ return running;
175
+ }
176
+ function docOf(sourceId, entry) {
177
+ if (entry.doc)
178
+ return entry.doc;
179
+ const date = entry.kind === 'diary' ? diaryDate(entry.path) : undefined;
180
+ const day = date ? localDayStart(date, timeZone) : undefined;
181
+ entry.doc = {
182
+ sourceId, kind: entry.kind, path: entry.path, title: entry.path.replace(/^.*[\\/]/, ''), content: entry.content, ref: fileRef(sourceId, entry.content), links: extractLinks(entry.content),
183
+ timeMs: day?.ms ?? entry.mtimeMs, time: day?.time ?? formatLocalTime(new Date(entry.mtimeMs), timeZone), timeBasis: day ? 'diaryDate' : 'fileObserved',
184
+ };
185
+ return entry.doc;
186
+ }
187
+ /** Every file she has written, as it is now. */
188
+ async function docs() {
189
+ await refresh();
190
+ return [...cache].map(([sourceId, entry]) => docOf(sourceId, entry));
191
+ }
192
+ return { docs, failures: () => [...failures].map(([sourceId, message]) => ({ sourceId, message })) };
193
+ }