dsh-world 0.7.4 → 0.7.6

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 (68) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/CONTRIBUTING.md +11 -0
  3. package/NOTES.md +73 -16
  4. package/README.md +14 -7
  5. package/client/index.js +43 -3
  6. package/lib/abortable.d.ts +1 -0
  7. package/lib/abortable.js +17 -0
  8. package/lib/compaction.d.ts +6 -1
  9. package/lib/compaction.js +8 -3
  10. package/lib/compactionSwap.d.ts +9 -17
  11. package/lib/compactionSwap.js +27 -35
  12. package/lib/console/homeApply.js +1 -12
  13. package/lib/console/routes.d.ts +1 -0
  14. package/lib/console/routes.js +10 -0
  15. package/lib/console/snapshot.d.ts +4 -0
  16. package/lib/console/snapshot.js +3 -0
  17. package/lib/ecosystem/bridge/bridge.d.ts +2 -2
  18. package/lib/ecosystem/bridge/bridge.js +24 -11
  19. package/lib/ecosystem/qq-native/home.d.ts +2 -1
  20. package/lib/ecosystem/qq-native/home.js +6 -2
  21. package/lib/ecosystem/qq-native/images.d.ts +13 -0
  22. package/lib/ecosystem/qq-native/images.js +76 -0
  23. package/lib/ecosystem/qq-native/index.d.ts +1 -0
  24. package/lib/ecosystem/qq-native/index.js +8 -2
  25. package/lib/ecosystem/qq-native/snowluma.d.ts +1 -0
  26. package/lib/ecosystem/qq-native/snowluma.js +13 -4
  27. package/lib/ecosystem/wechat/index.d.ts +5 -0
  28. package/lib/ecosystem/wechat/index.js +3 -1
  29. package/lib/ecosystem/wechat/presence.d.ts +5 -1
  30. package/lib/ecosystem/wechat/presence.js +22 -13
  31. package/lib/ecosystem/wechat/supervisor.d.ts +1 -1
  32. package/lib/ecosystem/wechat/supervisor.js +5 -1
  33. package/lib/elsewhere.d.ts +34 -0
  34. package/lib/elsewhere.js +40 -0
  35. package/lib/history.d.ts +5 -0
  36. package/lib/history.js +5 -0
  37. package/lib/hostContract.d.ts +6 -0
  38. package/lib/index.d.ts +1 -1
  39. package/lib/index.js +84 -46
  40. package/lib/life/home.js +8 -2
  41. package/lib/life/photoTools.js +60 -26
  42. package/lib/life/runtime.js +2 -0
  43. package/lib/memoryTools.d.ts +4 -3
  44. package/lib/memoryTools.js +11 -4
  45. package/lib/mood.d.ts +38 -5
  46. package/lib/mood.js +56 -13
  47. package/lib/moodTools.d.ts +6 -0
  48. package/lib/moodTools.js +30 -0
  49. package/lib/persona.js +7 -0
  50. package/lib/promptSection.d.ts +11 -13
  51. package/lib/promptSection.js +63 -80
  52. package/lib/runtime.d.ts +21 -2
  53. package/lib/runtime.js +162 -53
  54. package/lib/sessionNews.d.ts +17 -0
  55. package/lib/sessionNews.js +55 -0
  56. package/lib/sourceArchive.d.ts +1 -5
  57. package/lib/sourceArchive.js +31 -14
  58. package/lib/store.d.ts +6 -0
  59. package/lib/store.js +17 -3
  60. package/lib/worldScope.d.ts +2 -4
  61. package/lib/worldScope.js +6 -6
  62. package/package.json +1 -1
  63. package/src/ecosystem/wechat/windows/README.md +4 -2
  64. package/src/ecosystem/wechat/windows/adapter.py +6 -1
  65. package/src/ecosystem/wechat/windows/engine/NOTICE.md +6 -0
  66. package/src/ecosystem/wechat/windows/engine/profiles.json +28 -0
  67. package/src/ecosystem/wechat/windows/sender.py +6 -2
  68. package/templates/world-prompt.md +1 -1
package/lib/runtime.js CHANGED
@@ -1,28 +1,36 @@
1
- import { createSystemMessage, createUserMessage, isAgentLoopRequest, LlmError } from '@deepseek-ai/dsh-llm';
1
+ import { createHash } from 'node:crypto';
2
+ import { createSystemMessage, createUserMessage, isAgentLoopRequest } from '@deepseek-ai/dsh-llm';
3
+ import { appendPluginRecord, pluginRecordOf, SessionLogOffset } from '@deepseek-ai/dsh-session';
2
4
  import "./hostContract.js";
3
5
  import { isOrdinarySession } from "./archive.js";
4
- import { compactsWhole } from "./compaction.js";
6
+ import { compactsWhole, llmError } from "./compaction.js";
5
7
  import { compactionFor } from "./console/homeApply.js";
6
8
  import { Faults } from "./faults.js";
7
- import { createHistory, excludePresentMessages, renderHistory } from "./history.js";
8
- import { MOOD_CLEARED, moodText, readMood } from "./mood.js";
9
+ import { createHistory, excludePresentMessages, GROUP_PRIVACY, renderHistory } from "./history.js";
10
+ import { MOOD_CLEARED, moodAge, moodText, moodUpdatedAt, readMood } from "./mood.js";
9
11
  import { createWorldToolRegistry, memoryTools } from "./memoryTools.js";
10
- import { createPromptSections, WORLD_PROMPT_PREFIX } from "./promptSection.js";
12
+ import { moodTools } from "./moodTools.js";
13
+ import { createSessionNews } from "./sessionNews.js";
14
+ import { assembledWorlds, createPromptSections } from "./promptSection.js";
11
15
  import { belongsToWorld, createSourceArchive, inWorld, sessionPreset } from "./sourceArchive.js";
12
16
  import { worldNames } from "./worldPrompt.js";
13
17
  import { reassembleOnChange } from "./worldScope.js";
14
18
  import { DEFAULT_COLD_HOURS, DEFAULT_MEMORY_TURNS } from "./worldSwitch.js";
15
19
  const memorySource = 'dsh-world-history';
16
20
  const moodSource = 'dsh-world-mood';
21
+ const elsewhereSource = 'dsh-world-elsewhere';
17
22
  const isRecall = (source, worldId) => source?.kind === memorySource && (worldId === undefined || source.worldId === undefined || source.worldId === worldId);
23
+ const promptRecord = 'plugin:dsh-world/system-prompt';
24
+ const legacyPromptReset = 'plugin:dsh-world/legacy-prompt-reset';
25
+ const digestPrompt = (text) => createHash('sha256').update(text).digest('hex');
18
26
  function recallNodes(session, match) {
19
27
  return session.surface.nodes.filter(seq => {
20
28
  const event = session.eventAt(seq);
21
29
  return event?.type === 'user/message' && match(event.data.source);
22
30
  });
23
31
  }
24
- /** Empty system nodes retire recall from the model's context and leave the original events intact. */
25
- function retireRecall(session, retiring, position) {
32
+ /** Empty system nodes retire old context from the model's view and preserve the original events. */
33
+ function retireContext(session, retiring, position) {
26
34
  for (const seq of retiring) {
27
35
  if (!session.surface.nodes.includes(seq))
28
36
  continue;
@@ -30,22 +38,70 @@ function retireRecall(session, retiring, position) {
30
38
  }
31
39
  }
32
40
  /** A session can outlive its world. Authorization returns false for a stopped world and undefined outside it. */
33
- export function guardWorldContext(ctx, authorize) {
41
+ export function guardWorldContext(ctx, authorize, hasWorldRoom) {
34
42
  const assemblies = new WeakMap();
35
- const previousPrompts = new WeakMap();
43
+ const promptLogs = new WeakMap();
44
+ function promptLog(session) {
45
+ const log = promptLogs.get(session) ?? { nextSeq: 0, owners: new Map(), reset: false, sawWorldContext: false };
46
+ const end = session.seq;
47
+ for (let from = log.nextSeq; from < end; from += 128) {
48
+ for (const event of session.snapshotEvents(SessionLogOffset(from), SessionLogOffset(Math.min(from + 128, end)))) {
49
+ if (event.type === 'step/start')
50
+ delete log.pending;
51
+ if (event.type === 'system/message' && log.pending && event.data.turn === log.pending.turn && event.data.step === log.pending.step) {
52
+ const block = event.data.message.content[0];
53
+ if (block?.type === 'text' && digestPrompt(block.text) === log.pending.digest) {
54
+ log.owners.set(event.data.message.id, { worldId: log.pending.worldId, digest: log.pending.digest });
55
+ }
56
+ }
57
+ if (!log.reset && event.type === 'user/message') {
58
+ const kind = event.data.source.kind;
59
+ log.sawWorldContext ||= kind === memorySource || kind === moodSource || kind === elsewhereSource || kind === 'dsh-world-channel-context';
60
+ }
61
+ if (event.type === legacyPromptReset && pluginRecordOf(event))
62
+ log.reset = true;
63
+ if (event.type !== promptRecord)
64
+ continue;
65
+ const data = pluginRecordOf(event)?.data;
66
+ if (typeof data?.worldId === 'string' && typeof data.digest === 'string' && typeof data.turn === 'number' && typeof data.step === 'number') {
67
+ log.pending = { worldId: data.worldId, digest: data.digest, turn: data.turn, step: data.step };
68
+ log.reset = true;
69
+ }
70
+ }
71
+ }
72
+ log.nextSeq = end;
73
+ promptLogs.set(session, log);
74
+ return log;
75
+ }
76
+ // Old DSH logs contain a single rendered string with no section boundaries or saved world ownership.
77
+ const needsLegacyReset = (session, log) => !log.reset && (log.sawWorldContext || hasWorldRoom(session));
78
+ function retiredPrompts(session, log, current, reset) {
79
+ if (!reset && !current && !log.owners.size)
80
+ return [];
81
+ return session.surface.nodes.filter(seq => {
82
+ const event = session.eventAt(seq);
83
+ if (event?.type !== 'system/message')
84
+ return false;
85
+ const block = event.data.message.content[0];
86
+ if (block?.type !== 'text')
87
+ return false;
88
+ if (reset)
89
+ return true;
90
+ const owner = log.owners.get(event.data.message.id);
91
+ return current ? (owner?.digest ?? digestPrompt(block.text)) !== current.digest || (!!owner && owner.worldId !== current.worldId) : !!owner;
92
+ });
93
+ }
36
94
  const revoked = (source, session) => {
37
- if (source?.kind === memorySource || source?.kind === moodSource)
95
+ if (source?.kind === memorySource || source?.kind === moodSource || source?.kind === elsewhereSource)
38
96
  return !session || !authorize(session, source.worldId);
39
97
  return source?.kind === 'dsh-world-channel-context' && (!session || !authorize(session));
40
98
  };
41
99
  ctx.on('system-prompt/assemble', async (_assembly, context, next) => {
42
100
  const result = await next();
43
101
  if (context.signal && context.agent) {
44
- const section = result.sections.find(section => section.name.startsWith(WORLD_PROMPT_PREFIX));
45
- if (section) {
46
- const worldId = section.name.slice(WORLD_PROMPT_PREFIX.length).split(':', 1)[0];
47
- assemblies.set(context.signal, { worldId, token: authorize(context.agent.session, worldId) || undefined });
48
- }
102
+ const prompt = assembledWorlds.get(context);
103
+ if (prompt)
104
+ assemblies.set(context.signal, { worldId: prompt.worldId, token: prompt.token, digest: digestPrompt(prompt.text) });
49
105
  else
50
106
  assemblies.delete(context.signal);
51
107
  }
@@ -56,13 +112,20 @@ export function guardWorldContext(ctx, authorize) {
56
112
  if (decision.kind !== 'enter')
57
113
  return decision;
58
114
  const { session } = agent;
59
- const token = assemblies.get(signal)?.token;
60
- const previous = previousPrompts.get(session);
61
- const promptChanged = previous && (!token || previous.token !== token);
62
- return promptChanged || recallNodes(session, source => revoked(source, session)).length ? { ...decision, startsRequestSeries: true } : decision;
115
+ const log = promptLog(session);
116
+ return retiredPrompts(session, log, assemblies.get(signal), needsLegacyReset(session, log)).length || recallNodes(session, source => revoked(source, session)).length
117
+ ? { ...decision, startsRequestSeries: true } : decision;
63
118
  });
64
- ctx.on('agent/request', ({ agent: { session }, turn, step }, next) => {
65
- retireRecall(session, recallNodes(session, source => revoked(source, session)), { turn, step });
119
+ ctx.on('agent/request', ({ agent: { session }, turn, step, signal }, next) => {
120
+ const prompt = assemblies.get(signal);
121
+ const log = promptLog(session);
122
+ const reset = needsLegacyReset(session, log);
123
+ retireContext(session, [...retiredPrompts(session, log, prompt, reset), ...recallNodes(session, source => revoked(source, session))], { turn, step });
124
+ // Record ownership before DSH commits the projected system message, including attempts that never reach a provider.
125
+ if (prompt)
126
+ appendPluginRecord(session, promptRecord, { worldId: prompt.worldId, digest: prompt.digest, turn, step });
127
+ else if (reset)
128
+ appendPluginRecord(session, legacyPromptReset, {});
66
129
  return next();
67
130
  });
68
131
  ctx.on('llm/stream', async function* (request, next) {
@@ -72,24 +135,29 @@ export function guardWorldContext(ctx, authorize) {
72
135
  return;
73
136
  }
74
137
  const session = request.sessionId ? ctx.get('sessions')?.get(request.sessionId) : undefined;
75
- // Both compaction engines replay the saved prompt without assembling a new one.
76
- const prompt = compaction ? session && previousPrompts.get(session) : request.signal && assemblies.get(request.signal);
77
- if (!compaction && session && prompt)
78
- previousPrompts.set(session, prompt);
79
- const scope = session && (compaction || prompt) ? authorize(session, prompt?.worldId) : undefined;
80
- const stalePrompt = prompt && (!scope || (!compaction && scope !== prompt.token));
81
- const failure = { code: 'DSH_WORLD_SCOPE_CHANGED', message: compaction
82
- ? 'dsh-world: 世界已停止或会话已离开世界工作区,已取消压缩。'
83
- : 'dsh-world: 世界权限已变化,已阻止携带旧世界上下文的请求,请重新发送。' };
84
- if (scope === false || stalePrompt || request.messages.some(message => revoked(message.source, session))) {
138
+ const prompt = !compaction && request.signal ? assemblies.get(request.signal) : undefined;
139
+ const scope = session && authorize(session, prompt?.worldId);
140
+ const stalePrompt = !compaction && scope !== prompt?.token && (Boolean(prompt) || typeof scope === 'symbol');
141
+ // Compaction can replay system messages before a resumed session has taken a new turn.
142
+ const log = session && promptLog(session);
143
+ const legacyCompaction = compaction && scope === undefined && session && log
144
+ && needsLegacyReset(session, log) && request.messages.some(message => message.role === 'system' && message.content.length > 0);
145
+ const unauthorizedPrompt = !!session && !!log?.owners.size && request.messages.some(message => {
146
+ const owner = message.role === 'system' && log.owners.get(message.id);
147
+ return !!owner && !authorize(session, owner.worldId);
148
+ });
149
+ const failure = { code: 'DSH_WORLD_SCOPE_CHANGED', message: legacyCompaction
150
+ ? 'dsh-world: 会话仍带有旧版世界提示词。请先发送一条消息恢复 DSH 提示词,再压缩。'
151
+ : compaction
152
+ ? 'dsh-world: 世界已停止或会话已离开世界工作区,已取消压缩。'
153
+ : 'dsh-world: 世界权限已变化,已阻止携带旧世界上下文的请求,请重新发送。' };
154
+ if (stalePrompt || (compaction && scope === false) || legacyCompaction || unauthorizedPrompt || request.messages.some(message => revoked(message.source, session))) {
85
155
  yield { type: 'finish', reason: { kind: 'error', failure } };
86
156
  return;
87
157
  }
88
- if (!compaction && session && !prompt)
89
- previousPrompts.delete(session);
90
158
  yield* next();
91
159
  if (compaction && session && scope && authorize(session, prompt?.worldId) !== scope)
92
- throw new LlmError(failure.message, failure.code);
160
+ throw llmError(failure);
93
161
  });
94
162
  }
95
163
  /**
@@ -125,15 +193,23 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
125
193
  const included = (session) => inWorld(session, config, world?.inWorkspace);
126
194
  const ownMemory = (source) => isRecall(source, config.worldId);
127
195
  const faults = new Faults();
128
- const prompts = world && createPromptSections(config, world, { logger: ctx.logger });
196
+ const scopeToken = Symbol();
197
+ const prompts = world && createPromptSections(config, world, scopeToken, { logger: ctx.logger });
129
198
  const memory = new WeakMap();
130
199
  const service = world?.memory;
131
- const tools = service ? createWorldToolRegistry({ tools: inside => memoryTools(service, inside), inWorld: agent => !stopped && included(agent.session), faults, logger: ctx.logger }) : undefined;
132
- /** The memory tools follow world membership, whatever the life line does. */
200
+ const sessionNews = world?.elsewhere ? createSessionNews({ store, isHome: id => !!world.isHome(id), partner: () => world.names?.().partner ?? world.prompt.names.partner }) : undefined;
201
+ const worldTools = (inside, agent) => [
202
+ ...service ? memoryTools(service, inside, sourceId => sessionNews?.looked(agent.session, sourceId.slice('chat:'.length))) : [],
203
+ ...config.moodFile ? moodTools(config.moodFile, inside) : [],
204
+ ];
205
+ const tools = service || config.moodFile ? createWorldToolRegistry({ tools: worldTools, inWorld: agent => !stopped && included(agent.session), faults, logger: ctx.logger }) : undefined;
206
+ /** The world tools (memory, mood) follow world membership, whatever the life line does. */
133
207
  const syncTools = (agent) => !tools ? false : included(agent.session) ? tools.attach(agent) : tools.detach(agent);
134
208
  const disposers = [];
135
209
  let stopping;
136
210
  let stopped = false;
211
+ const catchUpController = new AbortController();
212
+ const cancelCatchUp = () => catchUpController.abort();
137
213
  /**
138
214
  * Whether the session may carry her world: her preset, or a memory block inherited onto its surface (a delegated
139
215
  * child of her session). Everything else is never read from disk or tracked. The check stays in memory.
@@ -194,8 +270,9 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
194
270
  return event?.type === 'user/message' ? event.data.content.flatMap(block => block.type === 'text' ? [block.text] : []).join('') : undefined;
195
271
  }
196
272
  /**
197
- * Her mood rides just before the time block, appended like it: only when it differs from the latest one on the
198
- * surface, never replacing an old one, so the cached prefix stays whole and compaction clears them all.
273
+ * Her mood rides just before the time block on every step, appended like it and never replacing an old one, so the
274
+ * cached prefix stays whole and compaction clears them all. Every step, because one wake can run a hundred steps: a
275
+ * snapshot shown only when it changed sank far behind her work and she stopped noticing it, so it never moved.
199
276
  */
200
277
  function moodMessage(session) {
201
278
  if (!config.moodFile)
@@ -205,14 +282,27 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
205
282
  faults.set('mood', `mood.md 读不出来:${mood.message}`);
206
283
  else
207
284
  faults.clear('mood');
208
- const latest = latestMood(session);
209
- const text = moodText(mood) ?? (latest === undefined ? undefined : MOOD_CLEARED);
210
- return text === undefined || text === latest ? undefined : createUserMessage({ content: [{ type: 'text', text }], source: { kind: moodSource, form: 'snapshot', worldId: config.worldId } });
285
+ const updatedAt = mood.kind === 'mood' ? moodUpdatedAt(config.moodFile) : undefined;
286
+ const shown = moodText(mood, updatedAt === undefined ? undefined : moodAge(updatedAt, now()));
287
+ const latest = shown === undefined ? latestMood(session) : undefined;
288
+ // A removed file is said once, so the old values stop counting.
289
+ const text = shown ?? (latest === undefined || latest === MOOD_CLEARED ? undefined : MOOD_CLEARED);
290
+ return text === undefined ? undefined : createUserMessage({ content: [{ type: 'text', text }], source: { kind: moodSource, form: 'snapshot', worldId: config.worldId } });
291
+ }
292
+ const inGroup = (session) => store.channelType(session.id) === 'group';
293
+ /**
294
+ * News from elsewhere is an event, told once: at a turn's start it comes before the new message (what gathered
295
+ * before it arrived), later in the turn at the step's end, ahead of her mood and the time.
296
+ */
297
+ function elsewhereMessage(session) {
298
+ const news = world?.elsewhere?.take(session, new Date(now()));
299
+ const text = news && inGroup(session) ? `${news}\n${GROUP_PRIVACY}` : news;
300
+ return text === undefined ? undefined : createUserMessage({ content: [{ type: 'text', text }], source: { kind: elsewhereSource, form: 'notice', worldId: config.worldId } });
211
301
  }
212
302
  function flushRetired(session, state, position) {
213
303
  const retiring = state.retiring;
214
304
  state.retiring = [];
215
- retireRecall(session, retiring, position);
305
+ retireContext(session, retiring, position);
216
306
  }
217
307
  async function attach(agent) {
218
308
  const { session } = agent;
@@ -245,7 +335,8 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
245
335
  async function freshBlock(session, state) {
246
336
  const turns = await history.loadRecentTurns(memoryTurns(), archivedByScope);
247
337
  const present = new Set(session.deriveMessages().map(message => message.id));
248
- const text = renderHistory(excludePresentMessages(turns, present), config.timeZone, names(), config.archiveScope === 'home');
338
+ const rendered = renderHistory(excludePresentMessages(turns, present), config.timeZone, names(), config.archiveScope === 'home');
339
+ const text = rendered && inGroup(session) ? `${rendered}\n\n${GROUP_PRIVACY}` : rendered;
249
340
  // A session entering this workspace can still carry an inherited recall from another world.
250
341
  retireMemory(session, state, included(session));
251
342
  return text ? createUserMessage({ content: [{ type: 'text', text }], source: { kind: memorySource, form: 'recall', worldId: config.worldId } }) : undefined;
@@ -255,11 +346,13 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
255
346
  if (!undecided)
256
347
  return;
257
348
  undecided = false;
258
- await archive.catchUp();
349
+ await archive.catchUp(false, catchUpController.signal);
259
350
  }
260
351
  /** `fresh`: her world was just started from the console, so what was said while it was stopped is not archived. */
261
352
  async function start(options = {}) {
262
353
  try {
354
+ if (sessionNews && world?.elsewhere)
355
+ disposers.push(world.elsewhere.register(sessionNews.source));
263
356
  disposers.push(ctx.on('session/event', (session, event) => {
264
357
  if (event.type === 'agent-preset/selected') {
265
358
  const agent = ctx.agents.get(session.id);
@@ -276,6 +369,7 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
276
369
  }
277
370
  if (state && compacted) {
278
371
  state.reload = true;
372
+ world?.elsewhere?.retell(session);
279
373
  }
280
374
  // The host assembles the system prompt after turn/start, so a cold session's first request already has both.
281
375
  const cold = !!state && event.type === 'turn/start' && state.lastRequestAt !== undefined && now() - state.lastRequestAt >= coldHours() * 60 * 60_000;
@@ -298,6 +392,8 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
298
392
  void attach(agent);
299
393
  return undefined;
300
394
  }));
395
+ if (prompts)
396
+ disposers.push(ctx.on('agent/disposed', ({ agent }) => prompts.forget(agent)));
301
397
  if (tools) {
302
398
  disposers.push(reassembleOnChange(ctx, syncTools));
303
399
  disposers.push(ctx.on('agent/pre-step', ({ agent }, next) => { syncTools(agent); return next(); }, { prepend: true }));
@@ -316,6 +412,8 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
316
412
  await attach(agent);
317
413
  const state = memory.get(agent.session);
318
414
  const leave = () => {
415
+ if (state.inScope)
416
+ world?.elsewhere?.retell(agent.session);
319
417
  state.restart ||= state.inScope;
320
418
  state.inScope = false;
321
419
  retireMemory(agent.session, state);
@@ -330,9 +428,10 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
330
428
  signal.throwIfAborted();
331
429
  if (!included(agent.session))
332
430
  return leave();
333
- const mood = moodMessage(agent.session);
334
- if (mood)
335
- decision = { ...decision, messages: [...decision.messages, mood] };
431
+ const news = elsewhereMessage(agent.session), mood = moodMessage(agent.session);
432
+ const head = news && step === 1 ? [news] : [], tail = [...(news && step > 1 ? [news] : []), ...(mood ? [mood] : [])];
433
+ if (head.length || tail.length)
434
+ decision = { ...decision, messages: [...head, ...decision.messages, ...tail] };
336
435
  if (!state.reload)
337
436
  return decision;
338
437
  // A block that cannot be built leaves the old one in place; she talks on, the fault shows, and the next step tries again.
@@ -387,14 +486,23 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
387
486
  yield chunk;
388
487
  }
389
488
  }));
390
- await archive.catchUp(options.fresh);
489
+ // Already-open sessions can start a turn before the background archive catch-up finishes.
490
+ for (const agent of ctx.agents.list()) {
491
+ prompts?.add(agent);
492
+ syncTools(agent);
493
+ }
494
+ const { signal } = catchUpController;
495
+ await archive.catchUp(options.fresh, signal);
391
496
  if (options.fresh)
392
- for (const agent of ctx.agents.list())
497
+ for (const agent of ctx.agents.list()) {
498
+ if (signal.aborted)
499
+ return;
393
500
  if (included(agent.session))
394
501
  await archive.skip(agent.id);
502
+ }
395
503
  for (const agent of ctx.agents.list()) {
396
- prompts?.add(agent);
397
- syncTools(agent);
504
+ if (signal.aborted)
505
+ return;
398
506
  await attach(agent);
399
507
  }
400
508
  }
@@ -406,6 +514,7 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
406
514
  function stop() {
407
515
  return stopping ??= (async () => {
408
516
  stopped = true;
517
+ cancelCatchUp();
409
518
  prompts?.clear();
410
519
  tools?.clear();
411
520
  for (const dispose of disposers.splice(0).reverse())
@@ -413,5 +522,5 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
413
522
  await archive.flush();
414
523
  })();
415
524
  }
416
- return { scopeToken: Symbol(), start, retryCatchUp, refreshPrompt: (session) => { prompts?.refresh(session); }, flush: archive.flush, stop, faults: () => [...faults.list(), ...archive.faults()] };
525
+ return { scopeToken, start, retryCatchUp, cancelCatchUp, refreshPrompt: (session) => { prompts?.refresh(session); }, flush: archive.flush, stop, faults: () => [...faults.list(), ...archive.faults()] };
417
526
  }
@@ -0,0 +1,17 @@
1
+ import type { Session } from '@deepseek-ai/dsh-session';
2
+ import { type ElsewhereSource } from './elsewhere.ts';
3
+ import type { ArchiveStore } from './store.ts';
4
+ /**
5
+ * Her other sessions as a source of news: what was archived there since this session started hearing, counted per
6
+ * session, never the words. It starts at the archive's end, so an upgrade or a restart tells nothing old; she reads a
7
+ * place with memory_find on its sourceId, and once she has paged its newest lines it is caught up.
8
+ */
9
+ export declare function createSessionNews(deps: {
10
+ store: Pick<ArchiveStore, 'readSince' | 'lastSequence'>;
11
+ isHome(sessionId: string): boolean;
12
+ partner(): string;
13
+ }): {
14
+ source: ElsewhereSource;
15
+ /** She paged the newest lines of that session from this one: it is caught up. */
16
+ looked(session: Session, sessionId: string): void;
17
+ };
@@ -0,0 +1,55 @@
1
+ import { chatLabel, chatName } from "./ecosystem/shared/identity.js";
2
+ import { ago } from "./elsewhere.js";
3
+ import { sessionMarker } from "./history.js";
4
+ /**
5
+ * Her other sessions as a source of news: what was archived there since this session started hearing, counted per
6
+ * session, never the words. It starts at the archive's end, so an upgrade or a restart tells nothing old; she reads a
7
+ * place with memory_find on its sourceId, and once she has paged its newest lines it is caught up.
8
+ */
9
+ export function createSessionNews(deps) {
10
+ const seen = new WeakMap();
11
+ const labelOf = (sessionId, record) => deps.isHome(sessionId) ? '家'
12
+ : record.channel && record.conversationType === 'group' ? chatLabel(record.channel, 'group', chatName(record) ?? record.conversationId ?? '') : undefined;
13
+ /** Reads what was archived since the last look, keeping only other sessions' lines. */
14
+ function catchUp(session) {
15
+ const state = seen.get(session);
16
+ if (!state) {
17
+ const fresh = { scanned: deps.store.lastSequence(), places: new Map() };
18
+ seen.set(session, fresh);
19
+ return fresh;
20
+ }
21
+ for (let batch = deps.store.readSince(state.scanned); batch.length; batch = deps.store.readSince(state.scanned)) {
22
+ for (const { sequence, record } of batch) {
23
+ state.scanned = sequence;
24
+ const id = record.sourceSessionId;
25
+ if (id === session.id)
26
+ continue;
27
+ const place = state.places.get(id) ?? { count: 0, lastSequence: sequence, last: record };
28
+ place.count += 1;
29
+ place.lastSequence = sequence;
30
+ place.last = record;
31
+ place.label ??= labelOf(id, record);
32
+ state.places.set(id, place);
33
+ }
34
+ }
35
+ return state;
36
+ }
37
+ const source = {
38
+ look: '别的会话用 memory_find 带上那一行的 sourceId 翻最新的',
39
+ news(now, session) {
40
+ return [...catchUp(session).places].sort(([, a], [, b]) => b.lastSequence - a.lastSequence).map(([id, place]) => {
41
+ const who = place.last.role === 'assistant' ? '我自己' : place.last.speakerName ?? deps.partner();
42
+ const label = place.label ?? `另一个窗口(${sessionMarker(id)})`;
43
+ return {
44
+ key: `session:${id}`, mark: `${place.count}:${place.lastSequence}`,
45
+ text: `${label}有 ${place.count} 条我在这边没看到,最后是${who}(${ago(now.getTime() - Date.parse(place.last.occurredAt))});sourceId:chat:${id}`,
46
+ };
47
+ });
48
+ },
49
+ };
50
+ return {
51
+ source,
52
+ /** She paged the newest lines of that session from this one: it is caught up. */
53
+ looked(session, sessionId) { catchUp(session).places.delete(sessionId); },
54
+ };
55
+ }
@@ -17,12 +17,8 @@ export declare const isNotFound: (error: unknown) => boolean;
17
17
  */
18
18
  export declare function createSourceArchive(ctx: Context, config: WorldConfig, store: ArchiveStore, archived?: (sessionId: string) => boolean): {
19
19
  schedule: (sessionId: string) => Promise<void>;
20
+ catchUp: (skip?: boolean, signal?: AbortSignal) => Promise<void>;
20
21
  faults: () => string[];
21
- /**
22
- * Every session the archive has seen, replayed again so nothing written while the plugin was not loaded is
23
- * missed; with `skip`, what was said while her world was stopped is passed over instead.
24
- */
25
- catchUp(skip?: boolean): Promise<void>;
26
22
  /** Moves past everything a session holds now without keeping it (said while her world was stopped). */
27
23
  skip: (sessionId: string) => Promise<void>;
28
24
  /** Waits for every queued replay; rejects while any session still has an unresolved failure. */
@@ -1,4 +1,5 @@
1
1
  import { SessionId, SessionLogOffset } from '@deepseek-ai/dsh-session';
2
+ import { abortable } from "./abortable.js";
2
3
  import { extractConversationRecords, isOrdinarySession } from "./archive.js";
3
4
  import { wechatDeliveryPatch } from "./ecosystem/wechat/archive.js";
4
5
  import { errorText, Faults } from "./faults.js";
@@ -132,21 +133,20 @@ export function createSourceArchive(ctx, config, store, archived = () => true) {
132
133
  tail = job.catch(error => { ctx.logger.error(error); });
133
134
  return job;
134
135
  }
135
- return {
136
- schedule,
137
- faults: () => faults.list(),
138
- /**
139
- * Every session the archive has seen, replayed again so nothing written while the plugin was not loaded is
140
- * missed; with `skip`, what was said while her world was stopped is passed over instead.
141
- */
142
- async catchUp(skip = false) {
143
- // One session's failure is kept for flush(); it must not stop the sessions after it.
144
- const replayOne = (id) => (skip ? enqueue(() => replay(id, true)) : schedule(id)).catch(() => { });
136
+ /** Replays what was missed, or passes over messages written while the world was stopped when `skip` is set. */
137
+ async function catchUp(skip = false, signal) {
138
+ if (signal?.aborted)
139
+ return;
140
+ // Keep the current replay in the queue so its transaction and file close finish before shutdown.
141
+ const replayOne = (id) => (skip ? enqueue(() => replay(id, true)) : schedule(id)).catch(() => { });
142
+ try {
145
143
  // The host exposes a metadata inventory; every selected message log is still read in pages.
146
- const snapshots = await ctx.sessionPersistence.list();
144
+ const snapshots = await abortable(ctx.sessionPersistence.list({ signal }), signal);
147
145
  const visited = new Set();
148
146
  for (let offset = 0; offset < snapshots.length; offset += pageSize) {
149
147
  for (const { header } of snapshots.slice(offset, offset + pageSize)) {
148
+ if (signal?.aborted)
149
+ return;
150
150
  if (!isOrdinarySession(header) || !archived(header.id))
151
151
  continue;
152
152
  visited.add(header.id);
@@ -154,13 +154,21 @@ export function createSourceArchive(ctx, config, store, archived = () => true) {
154
154
  }
155
155
  }
156
156
  let after = '';
157
- for (let ids = store.sourcePage(after); ids.length; ids = store.sourcePage(after)) {
157
+ while (!signal?.aborted) {
158
+ const ids = store.sourcePage(after);
159
+ if (!ids.length)
160
+ return;
158
161
  for (const id of ids) {
162
+ if (signal?.aborted)
163
+ return;
159
164
  after = id;
160
165
  if (visited.has(id))
161
166
  continue;
162
167
  // A session deleted from the host (not merely archived there) keeps its records but has nothing left to resume.
163
- if (!await ctx.sessionPersistence.stat(SessionId(id))) {
168
+ const exists = await abortable(ctx.sessionPersistence.stat(SessionId(id), { signal }), signal);
169
+ if (signal?.aborted)
170
+ return;
171
+ if (!exists) {
164
172
  forget(id);
165
173
  continue;
166
174
  }
@@ -169,7 +177,16 @@ export function createSourceArchive(ctx, config, store, archived = () => true) {
169
177
  await replayOne(id);
170
178
  }
171
179
  }
172
- },
180
+ }
181
+ catch (error) {
182
+ if (!signal?.aborted || error !== signal.reason)
183
+ throw error;
184
+ }
185
+ }
186
+ return {
187
+ schedule,
188
+ catchUp,
189
+ faults: () => faults.list(),
173
190
  /** Moves past everything a session holds now without keeping it (said while her world was stopped). */
174
191
  skip: (sessionId) => enqueue(() => replay(sessionId, true)),
175
192
  /** Waits for every queued replay; rejects while any session still has an unresolved failure. */
package/lib/store.d.ts CHANGED
@@ -12,6 +12,11 @@ export declare function createArchiveStore(config: WorldConfig & {
12
12
  hasRecord: (recordId: string) => boolean;
13
13
  updateRecord: (recordId: string, revise: (record: ConversationRecord) => Pick<ConversationRecord, "content" | "wechatDelivery"> & Partial<SpeakerIdentity>) => Promise<void>;
14
14
  readPage: (query: PageQuery) => Promise<RecordPage>;
15
+ readSince: (sequence: number, limit?: number) => {
16
+ sequence: number;
17
+ record: ConversationRecord;
18
+ }[];
19
+ lastSequence: () => number;
15
20
  readTurn: (turnId: string, afterSeq?: number, limit?: number) => Promise<ConversationRecord[]>;
16
21
  progress: (sessionId: string) => ExportProgress;
17
22
  forgetProgress: (sessionId: string) => void;
@@ -22,6 +27,7 @@ export declare function createArchiveStore(config: WorldConfig & {
22
27
  };
23
28
  close: () => void;
24
29
  channelSession: (key: string) => string | undefined;
30
+ channelType: (sessionId: string) => "private" | "group" | undefined;
25
31
  bindChannelSession: (key: string, sessionId: string) => void;
26
32
  delivery: (recordId: string) => DeliveryState | undefined;
27
33
  registerDelivery: (recordId: string, total: number) => void;
package/lib/store.js CHANGED
@@ -115,7 +115,7 @@ export function createArchiveStore(config) {
115
115
  async function readPage(query) {
116
116
  flushExports();
117
117
  const before = query.before;
118
- const maxSequence = before?.maxSequence ?? Number(db.prepare('SELECT COALESCE(MAX(sequence),0) AS n FROM records').get()?.n);
118
+ const maxSequence = before?.maxSequence ?? lastSequence();
119
119
  const rows = (before
120
120
  ? db.prepare('SELECT * FROM records WHERE sequence<=? AND (day, occurredAt, recordId)<(?,?,?) ORDER BY day DESC, occurredAt DESC, recordId DESC LIMIT ?').all(maxSequence, before.date, before.occurredAt, before.recordId, query.limit + 1)
121
121
  : db.prepare('SELECT * FROM records WHERE sequence<=? ORDER BY day DESC, occurredAt DESC, recordId DESC LIMIT ?').all(maxSequence, query.limit + 1));
@@ -146,6 +146,13 @@ export function createArchiveStore(config) {
146
146
  });
147
147
  flushExports();
148
148
  }
149
+ function lastSequence() {
150
+ return Number(db.prepare('SELECT COALESCE(MAX(sequence),0) AS n FROM records').get()?.n);
151
+ }
152
+ /** Records in the order they were archived, after a point: what is new since then. */
153
+ function readSince(sequence, limit = pageSize) {
154
+ return db.prepare('SELECT * FROM records WHERE sequence>? ORDER BY sequence LIMIT ?').all(sequence, limit).map(row => ({ sequence: Number(row.sequence), record: decode(row) }));
155
+ }
149
156
  async function readTurn(turnId, afterSeq = -1, limit = pageSize) {
150
157
  flushExports();
151
158
  return db.prepare('SELECT * FROM records WHERE turnId=? AND sourceSeq>? ORDER BY sourceSeq LIMIT ?').all(turnId, afterSeq, limit).map(decode);
@@ -179,6 +186,13 @@ export function createArchiveStore(config) {
179
186
  function channelSession(key) {
180
187
  return db.prepare('SELECT sessionId FROM channelSessions WHERE conversationKey=? LIMIT 1').get(key)?.sessionId;
181
188
  }
189
+ /** Whether a session is one of her channel conversations, and which kind; undefined for home and DSH windows. */
190
+ function channelType(sessionId) {
191
+ const key = db.prepare('SELECT conversationKey FROM channelSessions WHERE sessionId=? LIMIT 1').get(sessionId)?.conversationKey;
192
+ if (typeof key !== 'string')
193
+ return undefined;
194
+ return JSON.parse(key)[2] === 'group' ? 'group' : 'private';
195
+ }
182
196
  function bindChannelSession(key, sessionId) {
183
197
  transaction(() => { db.prepare('INSERT INTO channelSessions VALUES (?,?) ON CONFLICT(conversationKey) DO UPDATE SET sessionId=excluded.sessionId').run(key, sessionId); });
184
198
  }
@@ -197,6 +211,6 @@ export function createArchiveStore(config) {
197
211
  db.prepare('UPDATE channelDeliveries SET body=? WHERE recordId=?').run(JSON.stringify(revise(previous)), recordId);
198
212
  });
199
213
  }
200
- return { commitRecords, hasRecord, updateRecord, readPage, readTurn, progress, forgetProgress, sourcePage, stats, close,
201
- channelSession, bindChannelSession, delivery, registerDelivery, updateDelivery, memory, snapshotTo };
214
+ return { commitRecords, hasRecord, updateRecord, readPage, readSince, lastSequence, readTurn, progress, forgetProgress, sourcePage, stats, close,
215
+ channelSession, channelType, bindChannelSession, delivery, registerDelivery, updateDelivery, memory, snapshotTo };
202
216
  }
@@ -15,8 +15,6 @@ export declare class OutsideWorldError extends Error {
15
15
  export declare function admit(inWorld: InWorld | undefined, agent: Agent): Agent;
16
16
  /** The same scope check controls step admission and execution; older hosts ignore the registration condition. */
17
17
  export declare function registerScopedTool(agent: Agent, tool: ToolDefinition, inside: () => boolean, left?: () => void): () => void;
18
- /**
19
- * DSH 0.2.2 selects tools before prompt assembly, so synchronize at turn start and between steps as well.
20
- * The assembly check still catches membership changes while a request is being prepared.
21
- */
18
+ export declare function syncBetweenSteps(ctx: Context, sync: (agent: Agent) => void): () => void;
19
+ /** DSH 0.2.2 selects tools before prompt assembly. Check again during assembly for late changes. */
22
20
  export declare function reassembleOnChange(ctx: Context, sync: (agent: Agent) => boolean): () => void;