dsh-world 0.6.6 → 0.7.1
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.
- package/CHANGELOG.md +39 -0
- package/CONTRIBUTING.md +163 -161
- package/NOTES.md +184 -152
- package/README.md +237 -227
- package/client/index.js +894 -738
- package/lib/archive.d.ts +3 -1
- package/lib/archive.js +6 -4
- package/lib/compaction.js +8 -0
- package/lib/compactionSwap.d.ts +29 -2
- package/lib/compactionSwap.js +62 -7
- package/lib/console/homeApply.d.ts +11 -2
- package/lib/console/homeApply.js +36 -7
- package/lib/console/routes.d.ts +13 -0
- package/lib/console/routes.js +43 -0
- package/lib/console/snapshot.d.ts +8 -0
- package/lib/console/snapshot.js +4 -2
- package/lib/ecosystem/bridge/route.d.ts +7 -0
- package/lib/ecosystem/bridge/route.js +30 -0
- package/lib/ecosystem/qq/route.d.ts +2 -1
- package/lib/ecosystem/qq/route.js +10 -2
- package/lib/ecosystem/qq-native/batch.d.ts +2 -0
- package/lib/ecosystem/qq-native/batch.js +3 -1
- package/lib/ecosystem/qq-native/index.d.ts +2 -0
- package/lib/ecosystem/qq-native/index.js +4 -0
- package/lib/ecosystem/qq-native/replies.d.ts +6 -1
- package/lib/ecosystem/qq-native/replies.js +19 -1
- package/lib/ecosystem/shared/context.d.ts +1 -1
- package/lib/ecosystem/shared/context.js +1 -1
- package/lib/ecosystem/shared/identity.d.ts +12 -0
- package/lib/ecosystem/shared/identity.js +17 -1
- package/lib/ecosystem/shared/turn.d.ts +8 -5
- package/lib/ecosystem/shared/turn.js +16 -17
- package/lib/ecosystem/wechat/archive.d.ts +2 -0
- package/lib/ecosystem/wechat/archive.js +3 -1
- package/lib/ecosystem/wechat/home.js +9 -4
- package/lib/ecosystem/wechat/messages.d.ts +11 -0
- package/lib/ecosystem/wechat/messages.js +9 -1
- package/lib/ecosystem/wechat/sending.js +36 -9
- package/lib/ecosystem/wechat/supervisor.d.ts +11 -2
- package/lib/ecosystem/wechat/supervisor.js +90 -9
- package/lib/history.d.ts +6 -4
- package/lib/history.js +61 -12
- package/lib/hostContract.d.ts +1 -0
- package/lib/index.js +41 -14
- package/lib/life/activity.d.ts +2 -0
- package/lib/life/activity.js +1 -1
- package/lib/life/alarm.d.ts +12 -4
- package/lib/life/alarm.js +21 -6
- package/lib/life/diary.d.ts +6 -0
- package/lib/life/diary.js +23 -2
- package/lib/life/runtime.d.ts +1 -0
- package/lib/life/runtime.js +57 -9
- package/lib/life/toolRegistry.d.ts +4 -3
- package/lib/life/toolRegistry.js +13 -3
- package/lib/life/types.d.ts +6 -1
- package/lib/memory.d.ts +57 -0
- package/lib/memory.js +207 -0
- package/lib/memoryFiles.d.ts +50 -0
- package/lib/memoryFiles.js +168 -0
- package/lib/memoryStore.d.ts +112 -0
- package/lib/memoryStore.js +159 -0
- package/lib/memoryTools.d.ts +31 -0
- package/lib/memoryTools.js +106 -0
- package/lib/promptSection.d.ts +2 -0
- package/lib/runtime.d.ts +1 -1
- package/lib/runtime.js +62 -75
- package/lib/sourceArchive.d.ts +1 -0
- package/lib/sourceArchive.js +4 -5
- package/lib/store.d.ts +15 -1
- package/lib/store.js +12 -2
- package/lib/types.d.ts +1 -4
- package/lib/worldPrompt.d.ts +6 -0
- package/lib/worldPrompt.js +8 -2
- package/lib/worldSwitch.d.ts +9 -1
- package/lib/worldSwitch.js +14 -1
- package/package.json +128 -117
- package/scripts/compact-home.mjs +29 -0
- package/scripts/update-world.mjs +6 -4
- package/src/ecosystem/wechat/windows/README.md +7 -5
- package/src/ecosystem/wechat/windows/adapter.py +86 -31
- package/src/ecosystem/wechat/windows/engine/NOTICE.md +1 -1
- package/src/ecosystem/wechat/windows/engine/bridge.cpp +9 -1
- package/src/ecosystem/wechat/windows/engine/build.ps1 +8 -2
- package/src/ecosystem/wechat/windows/engine/profiles.json +56 -0
- package/src/ecosystem/wechat/windows/native.py +4 -1
- package/src/ecosystem/wechat/windows/outgoing.py +21 -0
- package/src/ecosystem/wechat/windows/sender.py +26 -6
- package/templates/alarm.md +3 -4
- package/templates/world-prompt-memory.md +10 -0
- package/templates/world-prompt.md +7 -2
package/lib/runtime.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { createSystemMessage, createUserMessage, isAgentLoopRequest } from '@deepseek-ai/dsh-llm';
|
|
2
2
|
import "./hostContract.js";
|
|
3
|
+
import { isOrdinarySession } from "./archive.js";
|
|
3
4
|
import { Faults } from "./faults.js";
|
|
4
5
|
import { createHistory, excludePresentMessages, renderHistory } from "./history.js";
|
|
6
|
+
import { createWorldToolRegistry, memoryTools } from "./memoryTools.js";
|
|
5
7
|
import { createPromptSections } from "./promptSection.js";
|
|
6
|
-
import { belongsToWorld, createSourceArchive, inWorld
|
|
8
|
+
import { belongsToWorld, createSourceArchive, inWorld } from "./sourceArchive.js";
|
|
7
9
|
import { worldNames } from "./worldPrompt.js";
|
|
10
|
+
import { reassembleOnChange } from "./worldScope.js";
|
|
8
11
|
import { DEFAULT_MEMORY_TURNS } from "./worldSwitch.js";
|
|
9
12
|
const memorySource = 'dsh-world-history';
|
|
13
|
+
const isMemoryBlock = (event) => event?.type === 'user/message' && event.data.source.kind === memorySource;
|
|
10
14
|
/**
|
|
11
15
|
* The memory line: her conversations are archived as they happen, and every world session gets a memory
|
|
12
16
|
* block of the newest turns at its start and after each successful compaction, never in between, so it stays
|
|
@@ -35,37 +39,30 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
|
|
|
35
39
|
const faults = new Faults();
|
|
36
40
|
const prompts = world && createPromptSections(config, world, { logger: ctx.logger });
|
|
37
41
|
const memory = new WeakMap();
|
|
38
|
-
const
|
|
42
|
+
const toolSets = [world?.memory ? (inside) => memoryTools(world.memory, inside) : undefined].filter(make => !!make);
|
|
43
|
+
const tools = toolSets.length ? createWorldToolRegistry({ tools: inside => toolSets.flatMap(make => make(inside)), inWorld: agent => !stopped && included(agent.session), faults, logger: ctx.logger }) : undefined;
|
|
44
|
+
/** The memory tools follow world membership, whatever the life line does. */
|
|
45
|
+
const syncTools = (agent) => !tools ? false : included(agent.session) ? tools.attach(agent) : tools.detach(agent);
|
|
39
46
|
const disposers = [];
|
|
40
47
|
let stopping;
|
|
48
|
+
let stopped = false;
|
|
41
49
|
/**
|
|
42
50
|
* Whether the session may carry her world: her preset, or a memory block inherited onto its surface (a delegated
|
|
43
51
|
* child of her session). Everything else is never read from disk or tracked. The check stays in memory.
|
|
44
52
|
*/
|
|
45
|
-
const mayCarryWorld = (session) => belongsToWorld(session, config) || memory.has(session) || session.surface.nodes.some(seq =>
|
|
46
|
-
|
|
47
|
-
return event?.type === 'user/message' && event.data.source.kind === memorySource;
|
|
48
|
-
});
|
|
49
|
-
/** Restore the loop position; recalled blocks are located directly on the current surface. */
|
|
50
|
-
async function readMemoryState(session) {
|
|
51
|
-
let position = { turn: 1, step: 1 };
|
|
52
|
-
const handle = await ctx.sessionPersistence.open(session.id, 'read');
|
|
53
|
-
try {
|
|
54
|
-
for (let offset = 0;; offset += 128) {
|
|
55
|
-
const { events } = await handle.read(offset, 128);
|
|
56
|
-
if (!events.length)
|
|
57
|
-
break;
|
|
58
|
-
for (const event of events) {
|
|
59
|
-
if (event.type === 'step/start')
|
|
60
|
-
position = { turn: event.data.turn, step: event.data.step };
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
finally {
|
|
65
|
-
await handle.close();
|
|
66
|
-
}
|
|
53
|
+
const mayCarryWorld = (session) => belongsToWorld(session, config) || memory.has(session) || session.surface.nodes.some(seq => isMemoryBlock(session.eventAt(seq)));
|
|
54
|
+
function newMemoryState(session) {
|
|
67
55
|
const inScope = included(session);
|
|
68
|
-
return { reload: true, position, retiring: [], inScope, restart: !inScope && belongsToWorld(session, config) };
|
|
56
|
+
return { reload: true, position: { turn: 1, step: 1 }, retiring: [], inScope, restart: !inScope && belongsToWorld(session, config) };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Archive failures never stop her talking: they stay in the health list, and what she said meanwhile is replayed
|
|
60
|
+
* from the host's log, from the archive's last committed position, once a later replay succeeds.
|
|
61
|
+
*/
|
|
62
|
+
async function archiveNow(sessionId) {
|
|
63
|
+
if (archived(sessionId))
|
|
64
|
+
await faults.track(`archive:${sessionId}`, `会话 ${sessionId} 归档登记失败`, () => store.commitRecords([], store.progress(sessionId))).catch(() => { });
|
|
65
|
+
await archive.schedule(sessionId).catch(() => { });
|
|
69
66
|
}
|
|
70
67
|
/**
|
|
71
68
|
* Marks our recalled blocks on the surface for retirement. The empty system node that retires one is accepted by
|
|
@@ -73,11 +70,9 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
|
|
|
73
70
|
* the step's request hook, after the step has started and before the request is assembled (#22).
|
|
74
71
|
*/
|
|
75
72
|
function retireMemory(session, state) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
});
|
|
80
|
-
if (state.retiring.length)
|
|
73
|
+
const found = session.surface.nodes.filter(seq => isMemoryBlock(session.eventAt(seq)));
|
|
74
|
+
state.retiring = [...new Set([...state.retiring, ...found])];
|
|
75
|
+
if (found.length)
|
|
81
76
|
state.restart = true;
|
|
82
77
|
state.reload = true;
|
|
83
78
|
state.pendingId = undefined;
|
|
@@ -115,35 +110,15 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
|
|
|
115
110
|
const { session } = agent;
|
|
116
111
|
if (memory.has(session) || !mayCarryWorld(session))
|
|
117
112
|
return;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (archived(session.id))
|
|
124
|
-
await store.commitRecords([], store.progress(session.id));
|
|
125
|
-
await archive.schedule(session.id);
|
|
126
|
-
}
|
|
127
|
-
const state = await readMemoryState(session);
|
|
128
|
-
// Retiring writes a system/message, which the host only accepts inside an open turn and step; attach runs
|
|
129
|
-
// between turns, so an out-of-scope block is retired by the next pre-step (before any request) instead (#22).
|
|
130
|
-
memory.set(session, state);
|
|
131
|
-
});
|
|
132
|
-
attaching.set(session, job);
|
|
133
|
-
try {
|
|
134
|
-
await job;
|
|
135
|
-
}
|
|
136
|
-
finally {
|
|
137
|
-
attaching.delete(session);
|
|
138
|
-
}
|
|
113
|
+
// Retiring writes a system/message, which the host only accepts inside an open turn and step; attach runs
|
|
114
|
+
// between turns, so an out-of-scope block is retired by the next pre-step (before any request) instead (#22).
|
|
115
|
+
memory.set(session, newMemoryState(session));
|
|
116
|
+
if (included(session))
|
|
117
|
+
await archiveNow(session.id);
|
|
139
118
|
}
|
|
140
|
-
/** Retire old blocks, then admit fresh memory through the host's normal append path (#21). */
|
|
119
|
+
/** Retire old blocks, then admit fresh memory through the host's normal append path (#21). The caller checks the scope after the wait. */
|
|
141
120
|
async function freshBlock(session, state) {
|
|
142
|
-
const turns = await history.loadRecentTurns(
|
|
143
|
-
if (!included(session)) {
|
|
144
|
-
retireMemory(session, state);
|
|
145
|
-
return undefined;
|
|
146
|
-
}
|
|
121
|
+
const turns = await history.loadRecentTurns(memoryTurns(), archivedByScope);
|
|
147
122
|
const present = new Set(session.deriveMessages().map(message => message.id));
|
|
148
123
|
const text = renderHistory(excludePresentMessages(turns, present), config.timeZone, names(), config.archiveScope === 'home');
|
|
149
124
|
retireMemory(session, state);
|
|
@@ -161,23 +136,34 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
|
|
|
161
136
|
try {
|
|
162
137
|
disposers.push(ctx.on('session/event', (session, event) => {
|
|
163
138
|
const state = memory.get(session);
|
|
164
|
-
if (state && event.type === 'step/start')
|
|
165
|
-
state.position = { turn: event.data.turn, step: event.data.step };
|
|
166
139
|
const compacted = event.type === 'compaction/end' && event.data.error === undefined;
|
|
167
140
|
if (state && event.type === 'user/message' && event.data.id === state.pendingId) {
|
|
168
141
|
state.reload = false;
|
|
169
142
|
state.pendingId = undefined;
|
|
170
143
|
}
|
|
171
|
-
if (state && compacted)
|
|
144
|
+
if (state && compacted) {
|
|
172
145
|
state.reload = true;
|
|
146
|
+
}
|
|
173
147
|
if (compacted)
|
|
174
148
|
prompts?.refresh(session);
|
|
175
|
-
if (
|
|
149
|
+
if (!isOrdinarySession(session.header))
|
|
176
150
|
return;
|
|
177
151
|
if (event.type === 'assistant/message' || event.type === 'turn/end' || event.type === 'turn/start' || event.type === 'compaction/end' || (event.type === 'user/message' && event.data.source.kind === 'user'))
|
|
178
152
|
void archive.schedule(session.id).catch(() => { });
|
|
179
153
|
}));
|
|
180
|
-
|
|
154
|
+
// The host fails the whole session creation when a creation listener rejects, leaving an empty session behind on
|
|
155
|
+
// every retry (#27); attach never rejects, and its archive failures stay in the health list.
|
|
156
|
+
disposers.push(ctx.on('agent/created', ({ agent }) => {
|
|
157
|
+
prompts?.add(agent);
|
|
158
|
+
syncTools(agent);
|
|
159
|
+
void attach(agent);
|
|
160
|
+
return undefined;
|
|
161
|
+
}));
|
|
162
|
+
if (tools) {
|
|
163
|
+
disposers.push(reassembleOnChange(ctx, syncTools));
|
|
164
|
+
disposers.push(ctx.on('agent/pre-step', ({ agent }, next) => { syncTools(agent); return next(); }, { prepend: true }));
|
|
165
|
+
disposers.push(ctx.on('agent/disposed', ({ agent }) => tools.forget(agent)));
|
|
166
|
+
}
|
|
181
167
|
// Admitted ahead of the step's new input; the host's own admission order (contexts, time-context last) is kept.
|
|
182
168
|
disposers.push(ctx.on('agent/pre-step', async (payload, next) => {
|
|
183
169
|
const decision = await next();
|
|
@@ -198,16 +184,20 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
|
|
|
198
184
|
if (!included(agent.session))
|
|
199
185
|
return leave();
|
|
200
186
|
state.inScope = true;
|
|
201
|
-
await
|
|
187
|
+
await archiveNow(agent.id);
|
|
202
188
|
signal.throwIfAborted();
|
|
203
189
|
if (!included(agent.session))
|
|
204
190
|
return leave();
|
|
205
191
|
if (!state.reload)
|
|
206
192
|
return decision;
|
|
207
|
-
|
|
208
|
-
|
|
193
|
+
// A block that cannot be built leaves the old one in place; she talks on, the fault shows, and the next step tries again.
|
|
194
|
+
let block, built = true;
|
|
195
|
+
await faults.track(`memory:${agent.id}`, `会话 ${agent.id} 记忆刷新失败`, async () => { block = await freshBlock(agent.session, state); }).catch(() => { built = false; });
|
|
209
196
|
if (!included(agent.session))
|
|
210
197
|
return leave();
|
|
198
|
+
signal.throwIfAborted();
|
|
199
|
+
if (!built)
|
|
200
|
+
return decision;
|
|
211
201
|
if (!block) {
|
|
212
202
|
state.reload = false;
|
|
213
203
|
return decision;
|
|
@@ -218,8 +208,10 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
|
|
|
218
208
|
// The step is open here and its request is not built yet: the one place retired blocks can be written validly.
|
|
219
209
|
disposers.push(ctx.on('agent/request', async function (payload, next) {
|
|
220
210
|
const state = memory.get(payload.agent.session);
|
|
211
|
+
if (state)
|
|
212
|
+
state.position = { turn: payload.turn, step: payload.step };
|
|
221
213
|
if (state?.retiring.length)
|
|
222
|
-
flushRetired(payload.agent.session, state,
|
|
214
|
+
flushRetired(payload.agent.session, state, state.position);
|
|
223
215
|
return next();
|
|
224
216
|
}));
|
|
225
217
|
disposers.push(ctx.on('llm/stream', async function* (request, next) {
|
|
@@ -255,14 +247,7 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
|
|
|
255
247
|
await archive.skip(agent.id);
|
|
256
248
|
for (const agent of ctx.agents.list()) {
|
|
257
249
|
prompts?.add(agent);
|
|
258
|
-
|
|
259
|
-
await attach(agent);
|
|
260
|
-
}
|
|
261
|
-
catch (error) {
|
|
262
|
-
if (!isCorruptSessionLog(error))
|
|
263
|
-
throw error;
|
|
264
|
-
ctx.logger.warn(`dsh-world: 会话 ${agent.id} 记忆初始化失败,已隔离:`, error);
|
|
265
|
-
}
|
|
250
|
+
await attach(agent);
|
|
266
251
|
}
|
|
267
252
|
}
|
|
268
253
|
catch (error) {
|
|
@@ -272,11 +257,13 @@ export function createWorldRuntime(ctx, config, store, world, memoryTurns = () =
|
|
|
272
257
|
}
|
|
273
258
|
function stop() {
|
|
274
259
|
return stopping ??= (async () => {
|
|
260
|
+
stopped = true;
|
|
275
261
|
prompts?.clear();
|
|
262
|
+
tools?.clear();
|
|
276
263
|
for (const dispose of disposers.splice(0).reverse())
|
|
277
264
|
dispose();
|
|
278
265
|
await archive.flush();
|
|
279
266
|
})();
|
|
280
267
|
}
|
|
281
|
-
return { start, retryCatchUp, refreshPrompt: (session) => prompts?.refresh(session), flush: archive.flush, stop, faults: () => [...faults.list(), ...archive.faults()] };
|
|
268
|
+
return { start, retryCatchUp, refreshPrompt: (session) => { prompts?.refresh(session); }, flush: archive.flush, stop, faults: () => [...faults.list(), ...archive.faults()] };
|
|
282
269
|
}
|
package/lib/sourceArchive.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare function isCorruptSessionLog(error: unknown): boolean;
|
|
|
7
7
|
export declare function belongsToWorld(session: Pick<Session, 'header'>, config: WorldConfig): boolean;
|
|
8
8
|
/** One of her sessions that is also in her room's workspace; without a membership test, any of her sessions. */
|
|
9
9
|
export declare function inWorld(session: Pick<Session, 'header' | 'id'>, config: WorldConfig, inWorkspace?: (sessionId: string) => boolean): boolean;
|
|
10
|
+
export declare const isNotFound: (error: unknown) => boolean;
|
|
10
11
|
/**
|
|
11
12
|
* Copies her conversations from the host's session logs into the archive. Each session is replayed from where
|
|
12
13
|
* the archive left off; replays run one at a time, and a session already queued is not queued twice. Sessions
|
package/lib/sourceArchive.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SessionId } from '@deepseek-ai/dsh-session';
|
|
2
|
-
import { extractConversationRecords } from "./archive.js";
|
|
2
|
+
import { extractConversationRecords, isOrdinarySession } from "./archive.js";
|
|
3
3
|
import { wechatDeliveryPatch } from "./ecosystem/wechat/archive.js";
|
|
4
4
|
import { errorText, Faults } from "./faults.js";
|
|
5
5
|
const pageSize = 128;
|
|
@@ -10,14 +10,13 @@ export function isCorruptSessionLog(error) {
|
|
|
10
10
|
}
|
|
11
11
|
export function belongsToWorld(session, config) {
|
|
12
12
|
const header = session.header;
|
|
13
|
-
return header.agentPreset === config.agentPreset &&
|
|
13
|
+
return header.agentPreset === config.agentPreset && isOrdinarySession(header);
|
|
14
14
|
}
|
|
15
15
|
/** One of her sessions that is also in her room's workspace; without a membership test, any of her sessions. */
|
|
16
16
|
export function inWorld(session, config, inWorkspace) {
|
|
17
17
|
return belongsToWorld(session, config) && (inWorkspace?.(session.id) ?? true);
|
|
18
18
|
}
|
|
19
|
-
const isNotFound = (error) => error instanceof Error && error.name === 'SessionPersistenceNotFoundError';
|
|
20
|
-
const ordinary = (header) => header.origin !== 'subagent' && (header.delegationDepth ?? 0) === 0;
|
|
19
|
+
export const isNotFound = (error) => error instanceof Error && error.name === 'SessionPersistenceNotFoundError';
|
|
21
20
|
/**
|
|
22
21
|
* Copies her conversations from the host's session logs into the archive. Each session is replayed from where
|
|
23
22
|
* the archive left off; replays run one at a time, and a session already queued is not queued twice. Sessions
|
|
@@ -134,7 +133,7 @@ export function createSourceArchive(ctx, config, store, archived = () => true) {
|
|
|
134
133
|
const visited = new Set();
|
|
135
134
|
for (let offset = 0; offset < snapshots.length; offset += pageSize) {
|
|
136
135
|
for (const { header } of snapshots.slice(offset, offset + pageSize)) {
|
|
137
|
-
if (!
|
|
136
|
+
if (!isOrdinarySession(header) || !archived(header.id))
|
|
138
137
|
continue;
|
|
139
138
|
visited.add(header.id);
|
|
140
139
|
await replayOne(header.id);
|
package/lib/store.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ 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
|
-
readTurn: (
|
|
15
|
+
readTurn: (turnId: string, afterSeq?: number, limit?: number) => Promise<ConversationRecord[]>;
|
|
16
16
|
progress: (sessionId: string) => ExportProgress;
|
|
17
17
|
forgetProgress: (sessionId: string) => void;
|
|
18
18
|
sourcePage: (afterId?: string, limit?: number) => string[];
|
|
@@ -26,5 +26,19 @@ export declare function createArchiveStore(config: WorldConfig & {
|
|
|
26
26
|
delivery: (recordId: string) => DeliveryState | undefined;
|
|
27
27
|
registerDelivery: (recordId: string, total: number) => void;
|
|
28
28
|
updateDelivery: (recordId: string, revise: (previous: DeliveryState) => DeliveryState) => void;
|
|
29
|
+
memory: {
|
|
30
|
+
find: (params: import("./memoryStore.ts").FindParams, sessions: readonly string[], upTo: number, take: number) => ConversationRecord[];
|
|
31
|
+
item: (record: ConversationRecord, query: string | undefined) => import("./memoryStore.ts").MemoryItem;
|
|
32
|
+
byRecordId: (recordId: string) => ConversationRecord | undefined;
|
|
33
|
+
refSession: (ref: string) => string | undefined;
|
|
34
|
+
read: (ref: string, canSee: (sessionId: string) => boolean) => import("./memoryStore.ts").ReadResult;
|
|
35
|
+
sessionIds: () => string[];
|
|
36
|
+
snapshot: () => {
|
|
37
|
+
head: number;
|
|
38
|
+
revisions: number;
|
|
39
|
+
};
|
|
40
|
+
revised: () => void;
|
|
41
|
+
};
|
|
42
|
+
snapshotTo: (file: string) => void;
|
|
29
43
|
};
|
|
30
44
|
export type ArchiveStore = ReturnType<typeof createArchiveStore>;
|
package/lib/store.js
CHANGED
|
@@ -2,6 +2,7 @@ import { DatabaseSync } from 'node:sqlite';
|
|
|
2
2
|
import { closeSync, existsSync, fstatSync, fsyncSync, ftruncateSync, mkdirSync, openSync, writeSync } from 'node:fs';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { formatLocalTime } from "./timeContext.js";
|
|
5
|
+
import { createMemoryStore } from "./memoryStore.js";
|
|
5
6
|
const pageSize = 128;
|
|
6
7
|
export function createArchiveStore(config) {
|
|
7
8
|
// The world id becomes a directory name.
|
|
@@ -37,6 +38,8 @@ export function createArchiveStore(config) {
|
|
|
37
38
|
throw error;
|
|
38
39
|
}
|
|
39
40
|
}
|
|
41
|
+
/** Long-term memory reads the records table on this connection; it keeps nothing of its own in the database. */
|
|
42
|
+
const memory = createMemoryStore(db, { timeZone: config.timeZone });
|
|
40
43
|
const decode = (row) => JSON.parse(row.body);
|
|
41
44
|
const dayOf = (record) => formatLocalTime(new Date(record.occurredAt), config.timeZone).slice(0, 10);
|
|
42
45
|
/**
|
|
@@ -135,13 +138,14 @@ export function createArchiveStore(config) {
|
|
|
135
138
|
if (body === row.body)
|
|
136
139
|
return;
|
|
137
140
|
db.prepare('UPDATE records SET body=? WHERE recordId=?').run(body, recordId);
|
|
141
|
+
memory.revised();
|
|
138
142
|
// A changed line invalidates later byte offsets; rebuild this day from the canonical database.
|
|
139
143
|
db.prepare('UPDATE records SET fileOffset=NULL, fileBytes=NULL WHERE day=?').run(row.day);
|
|
140
144
|
db.prepare('UPDATE days SET exportedBytes=0 WHERE day=?').run(row.day);
|
|
141
145
|
});
|
|
142
146
|
flushExports();
|
|
143
147
|
}
|
|
144
|
-
async function readTurn(
|
|
148
|
+
async function readTurn(turnId, afterSeq = -1, limit = pageSize) {
|
|
145
149
|
flushExports();
|
|
146
150
|
return db.prepare('SELECT * FROM records WHERE turnId=? AND sourceSeq>? ORDER BY sourceSeq LIMIT ?').all(turnId, afterSeq, limit).map(decode);
|
|
147
151
|
}
|
|
@@ -160,6 +164,12 @@ export function createArchiveStore(config) {
|
|
|
160
164
|
const row = db.prepare("SELECT count(*) AS records, count(DISTINCT json_extract(body, '$.sourceSessionId')) AS sessions FROM records").get();
|
|
161
165
|
return { records: Number(row?.records ?? 0), sessions: Number(row?.sessions ?? 0) };
|
|
162
166
|
}
|
|
167
|
+
/** A consistent copy of the whole archive database (originals and index), for forking an experiment branch from one snapshot. */
|
|
168
|
+
function snapshotTo(file) {
|
|
169
|
+
if (existsSync(file))
|
|
170
|
+
throw new Error('snapshot target already exists');
|
|
171
|
+
db.prepare('VACUUM INTO ?').run(file);
|
|
172
|
+
}
|
|
163
173
|
function close() { if (!closed) {
|
|
164
174
|
closed = true;
|
|
165
175
|
db.close();
|
|
@@ -187,5 +197,5 @@ export function createArchiveStore(config) {
|
|
|
187
197
|
});
|
|
188
198
|
}
|
|
189
199
|
return { commitRecords, hasRecord, updateRecord, readPage, readTurn, progress, forgetProgress, sourcePage, stats, close,
|
|
190
|
-
channelSession, bindChannelSession, delivery, registerDelivery, updateDelivery };
|
|
200
|
+
channelSession, bindChannelSession, delivery, registerDelivery, updateDelivery, memory, snapshotTo };
|
|
191
201
|
}
|
package/lib/types.d.ts
CHANGED
|
@@ -97,11 +97,9 @@ export interface ConversationRecord extends Partial<SpeakerIdentity> {
|
|
|
97
97
|
}
|
|
98
98
|
export interface ArchiveScope {
|
|
99
99
|
worldId: string;
|
|
100
|
-
agentPreset: string;
|
|
101
100
|
header: SessionHeader;
|
|
102
101
|
inheritedEventCount: number;
|
|
103
102
|
turn: number;
|
|
104
|
-
archiveScope?: MemoryArchiveScope;
|
|
105
103
|
}
|
|
106
104
|
export interface ArchiveCursor {
|
|
107
105
|
date: string;
|
|
@@ -110,7 +108,6 @@ export interface ArchiveCursor {
|
|
|
110
108
|
maxSequence: number;
|
|
111
109
|
}
|
|
112
110
|
export interface PageQuery {
|
|
113
|
-
worldId: string;
|
|
114
111
|
before?: ArchiveCursor;
|
|
115
112
|
limit: number;
|
|
116
113
|
}
|
|
@@ -125,5 +122,5 @@ export interface ConversationTurn {
|
|
|
125
122
|
}
|
|
126
123
|
export interface ArchiveReader {
|
|
127
124
|
readPage(query: PageQuery): Promise<RecordPage>;
|
|
128
|
-
readTurn(
|
|
125
|
+
readTurn(turnId: string, afterSeq?: number, limit?: number): Promise<ConversationRecord[]>;
|
|
129
126
|
}
|
package/lib/worldPrompt.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { LifeOptions } from './life/types.ts';
|
|
2
2
|
/** The template shipped with the plugin; a `world-prompt.md` in the world directory replaces it. */
|
|
3
3
|
export declare const DEFAULT_TEMPLATE_FILE: string;
|
|
4
|
+
/** The sections an older custom template lacks; she can paste them in (see README). */
|
|
5
|
+
export declare const MEMORY_SNIPPET_FILE: string;
|
|
6
|
+
/** Which of the memory variables a template does not use, so the console can say what her prompt does not mention. */
|
|
7
|
+
export declare function memoryTemplateGaps(template: string): string[];
|
|
4
8
|
export interface WorldNames {
|
|
5
9
|
companion: string;
|
|
6
10
|
partner: string;
|
|
@@ -23,6 +27,8 @@ export interface WorldPrompt {
|
|
|
23
27
|
names: WorldNames;
|
|
24
28
|
/** Present when the life line runs: room, diary, world line, ask-first rules. */
|
|
25
29
|
life?: LifeOptions;
|
|
30
|
+
/** The memory_find and memory_read tools are on her sessions. */
|
|
31
|
+
memoryTools?: boolean;
|
|
26
32
|
}
|
|
27
33
|
export interface TemplateRead {
|
|
28
34
|
text: string;
|
package/lib/worldPrompt.js
CHANGED
|
@@ -2,6 +2,12 @@ import { existsSync, readFileSync } from 'node:fs';
|
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
3
|
/** The template shipped with the plugin; a `world-prompt.md` in the world directory replaces it. */
|
|
4
4
|
export const DEFAULT_TEMPLATE_FILE = fileURLToPath(new URL('../templates/world-prompt.md', import.meta.url));
|
|
5
|
+
/** The sections an older custom template lacks; she can paste them in (see README). */
|
|
6
|
+
export const MEMORY_SNIPPET_FILE = fileURLToPath(new URL('../templates/world-prompt-memory.md', import.meta.url));
|
|
7
|
+
/** Which of the memory variables a template does not use, so the console can say what her prompt does not mention. */
|
|
8
|
+
export function memoryTemplateGaps(template) {
|
|
9
|
+
return ['memoryTools'].filter(name => !new RegExp(String.raw `\{\{[#^]?${name}\}\}`).test(template));
|
|
10
|
+
}
|
|
5
11
|
/** No name is assumed for anyone's companion: an unnamed one is the neutral TA. */
|
|
6
12
|
export function worldNames(config) {
|
|
7
13
|
return { companion: config.companionName?.trim() || 'TA', partner: config.partnerName?.trim() || '伙伴' };
|
|
@@ -42,8 +48,8 @@ export function renderTemplate(template, flags, values) {
|
|
|
42
48
|
*/
|
|
43
49
|
export function renderWorldPrompt(prompt, template, self, home) {
|
|
44
50
|
const { life, names } = prompt;
|
|
45
|
-
return renderTemplate(template, { life: Boolean(life), home, selfFile: Boolean(self), conversationsDir: Boolean(prompt.conversationsDir) }, {
|
|
51
|
+
return renderTemplate(template, { life: Boolean(life), home, selfFile: Boolean(self), conversationsDir: Boolean(prompt.conversationsDir), memoryTools: Boolean(prompt.memoryTools) }, {
|
|
46
52
|
name: names.companion, partner: names.partner, homeTitle: homeTitle(names), selfFile: self?.file ?? '', self: self?.text ?? '', conversationsDir: prompt.conversationsDir ?? '',
|
|
47
|
-
...(life ? { roomDir: life.roomDir, diaryDir: life.diaryDir, fallbackWakeTime: life.
|
|
53
|
+
...(life ? { roomDir: life.roomDir, diaryDir: life.diaryDir, fallbackWakeTime: life.schedule().wake } : {}),
|
|
48
54
|
});
|
|
49
55
|
}
|
package/lib/worldSwitch.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare const DEFAULT_WECHAT_BUFFER_SECONDS = 3, MAX_WECHAT_BUFFER_SECOND
|
|
|
10
10
|
* spend a day, and how many recent turns her memory block carries. A world that already has a life line from before the switch existed is running; a new one waits
|
|
11
11
|
* for its partner to press start.
|
|
12
12
|
*/
|
|
13
|
-
export declare function createWorldSwitch(worldDir: string, configuredScope?: MemoryArchiveScope): {
|
|
13
|
+
export declare function createWorldSwitch(worldDir: string, configuredScope?: MemoryArchiveScope, configuredWake?: string): {
|
|
14
14
|
archiveScope: () => MemoryArchiveScope;
|
|
15
15
|
selectedArchiveScope: () => MemoryArchiveScope;
|
|
16
16
|
restartRequired: () => boolean;
|
|
@@ -42,6 +42,14 @@ export declare function createWorldSwitch(worldDir: string, configuredScope?: Me
|
|
|
42
42
|
wechat?: boolean;
|
|
43
43
|
qq?: boolean;
|
|
44
44
|
}): void;
|
|
45
|
+
/** Her evening look back and her morning wake when nothing is pending; the configured `fallbackWakeTime` is the morning's default. */
|
|
46
|
+
reviewTime: () => string;
|
|
47
|
+
wakeTime: () => string;
|
|
48
|
+
/** A field left out stays as it is; null goes back to its default. */
|
|
49
|
+
setSchedule(next: {
|
|
50
|
+
review?: string | null;
|
|
51
|
+
wake?: string | null;
|
|
52
|
+
}): void;
|
|
45
53
|
/** Read by the WeChat adapter on its next poll; a batch already waiting keeps the value it started with. */
|
|
46
54
|
wechatContext: () => number;
|
|
47
55
|
wechatBufferSeconds: () => number;
|
package/lib/worldSwitch.js
CHANGED
|
@@ -2,6 +2,7 @@ import { existsSync, readFileSync } from 'node:fs';
|
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { durableWrite } from "./life/lifeStore.js";
|
|
4
4
|
import { validArchiveScope } from "./archiveSelection.js";
|
|
5
|
+
import { DEFAULT_REVIEW_TIME, validClockTime } from "./life/alarm.js";
|
|
5
6
|
/** How many of the newest turns the memory block carries when nothing else is set. */
|
|
6
7
|
export const DEFAULT_MEMORY_TURNS = 200;
|
|
7
8
|
/** WeChat group messages read before a mention, and seconds a private image waits for its companions. */
|
|
@@ -13,7 +14,7 @@ export const DEFAULT_WECHAT_BUFFER_SECONDS = 3, MAX_WECHAT_BUFFER_SECONDS = 30;
|
|
|
13
14
|
* spend a day, and how many recent turns her memory block carries. A world that already has a life line from before the switch existed is running; a new one waits
|
|
14
15
|
* for its partner to press start.
|
|
15
16
|
*/
|
|
16
|
-
export function createWorldSwitch(worldDir, configuredScope = 'workspace') {
|
|
17
|
+
export function createWorldSwitch(worldDir, configuredScope = 'workspace', configuredWake = '09:00') {
|
|
17
18
|
const file = join(worldDir, 'world.json');
|
|
18
19
|
const stored = existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : undefined;
|
|
19
20
|
let settings = {
|
|
@@ -28,6 +29,8 @@ export function createWorldSwitch(worldDir, configuredScope = 'workspace') {
|
|
|
28
29
|
...(typeof stored?.qqEnabled === 'boolean' ? { qqEnabled: stored.qqEnabled } : {}),
|
|
29
30
|
...(stored?.compactAfterReview === false ? { compactAfterReview: false } : {}),
|
|
30
31
|
...(stored?.compactionSwapped === true ? { compactionSwapped: true } : {}),
|
|
32
|
+
...(validClockTime(stored?.reviewTime) ? { reviewTime: stored.reviewTime } : {}),
|
|
33
|
+
...(validClockTime(stored?.wakeTime) ? { wakeTime: stored.wakeTime } : {}),
|
|
31
34
|
};
|
|
32
35
|
const activeScope = settings.archiveScope ?? configuredScope;
|
|
33
36
|
function save(next) {
|
|
@@ -84,6 +87,16 @@ export function createWorldSwitch(worldDir, configuredScope = 'workspace') {
|
|
|
84
87
|
const wechat = next.wechat ?? wechatEnabled ?? existsSync(join(worldDir, 'wechat-outbox.sqlite')), qq = next.qq ?? qqEnabled === true;
|
|
85
88
|
save({ ...rest, wechatEnabled: wechat, ...(qq ? { qqEnabled: true } : {}) });
|
|
86
89
|
},
|
|
90
|
+
/** Her evening look back and her morning wake when nothing is pending; the configured `fallbackWakeTime` is the morning's default. */
|
|
91
|
+
reviewTime: () => settings.reviewTime ?? DEFAULT_REVIEW_TIME,
|
|
92
|
+
wakeTime: () => settings.wakeTime ?? configuredWake,
|
|
93
|
+
/** A field left out stays as it is; null goes back to its default. */
|
|
94
|
+
setSchedule(next) {
|
|
95
|
+
const { reviewTime: oldReview, wakeTime: oldWake, ...rest } = settings;
|
|
96
|
+
const review = next.review === undefined ? oldReview : next.review ?? undefined;
|
|
97
|
+
const wake = next.wake === undefined ? oldWake : next.wake ?? undefined;
|
|
98
|
+
save({ ...rest, ...(review === undefined || review === DEFAULT_REVIEW_TIME ? {} : { reviewTime: review }), ...(wake === undefined || wake === configuredWake ? {} : { wakeTime: wake }) });
|
|
99
|
+
},
|
|
87
100
|
/** Read by the WeChat adapter on its next poll; a batch already waiting keeps the value it started with. */
|
|
88
101
|
wechatContext: () => settings.wechatContext ?? DEFAULT_WECHAT_CONTEXT,
|
|
89
102
|
wechatBufferSeconds: () => settings.wechatBufferSeconds ?? DEFAULT_WECHAT_BUFFER_SECONDS,
|