@niwa-labs/memory 0.0.0-stage → 0.0.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.
- package/LICENSE +21 -0
- package/README.md +107 -2
- package/package.json +33 -4
- package/src/cockpit/actions.ts +95 -0
- package/src/cockpit/index.tsx +23 -0
- package/src/cockpit/notes.ts +78 -0
- package/src/cockpit/page.tsx +191 -0
- package/src/consolidate.ts +113 -0
- package/src/context.ts +41 -0
- package/src/fold.ts +308 -0
- package/src/holds.ts +13 -0
- package/src/index.ts +775 -0
- package/src/records.ts +538 -0
- package/src/same.ts +15 -0
- package/src/search.ts +48 -0
- package/src/share-question.ts +27 -0
- package/src/top.ts +44 -0
- package/test/cockpit-actions.test.ts +53 -0
- package/test/cockpit-notes.test.ts +89 -0
- package/test/consolidate.test.ts +171 -0
- package/test/context.test.ts +241 -0
- package/test/corroboration.test.ts +61 -0
- package/test/delegate-context.test.ts +16 -0
- package/test/fixtures/host.ts +229 -0
- package/test/intake.test.ts +69 -0
- package/test/notes.test.ts +124 -0
- package/test/projects.test.ts +37 -0
- package/test/recovery.test.ts +174 -0
- package/test/search.test.ts +88 -0
- package/test/top.test.ts +84 -0
- package/tsconfig.json +15 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The memory module: one memory implementation on a host, and a memory in it for every agent.
|
|
3
|
+
*
|
|
4
|
+
* A note is one conclusion with its source: whom it is about, the project it holds for, and whether the
|
|
5
|
+
* owner stands behind it. Everything the module knows is a record of the journal, folded when it is read,
|
|
6
|
+
* so a memory can be read as it stood at any position.
|
|
7
|
+
*
|
|
8
|
+
* An agent reaches its memory with its own address: the binding says which address a caller speaks as,
|
|
9
|
+
* and a caller bound to an agent reaches that agent's memory and no other. The owner reaches every
|
|
10
|
+
* memory; the host and the handlers of other plugins name the memory they mean.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { createHash } from "node:crypto";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
|
|
16
|
+
import type { z } from "zod";
|
|
17
|
+
|
|
18
|
+
import { action, agent as agentAddress, addressText, confirmOptions, isYes, owner, recordId, recordTypeName, seq, stableJson } from "@niwa-labs/core";
|
|
19
|
+
import type { ActionContext, Address, Caller, JournalReader, Step } from "@niwa-labs/core";
|
|
20
|
+
import { COCKPIT_MODULE, plugin } from "@niwa-labs/plugin";
|
|
21
|
+
import type { CallerBinding, Plugin } from "@niwa-labs/plugin";
|
|
22
|
+
import { AGENT_BRIEFING, AGENT_PROMPT_SECTION, AgentAttached, AgentDetached, AgentTurnQueued, SessionDigestTaken, agentGet } from "@niwa-labs/agent/records";
|
|
23
|
+
import type { TurnAuthor } from "@niwa-labs/agent/records";
|
|
24
|
+
|
|
25
|
+
import { QuestionName, requestAsk, requestCancel, requestFind, requestGet } from "@niwa-labs/requests/records";
|
|
26
|
+
|
|
27
|
+
import { consolidate } from "./consolidate.ts";
|
|
28
|
+
import type { ConsolidationLimits } from "./consolidate.ts";
|
|
29
|
+
import { conclusion, standing } from "./context.ts";
|
|
30
|
+
import type { Reader } from "./context.ts";
|
|
31
|
+
import { Memories, dataAs, sourceKey } from "./fold.ts";
|
|
32
|
+
import type { TakenDigest } from "./fold.ts";
|
|
33
|
+
import {
|
|
34
|
+
MEMORY_TYPES,
|
|
35
|
+
Consolidated,
|
|
36
|
+
ConsolidationStarted,
|
|
37
|
+
DigestTaken,
|
|
38
|
+
NoteConfirmed,
|
|
39
|
+
NoteCorroborated,
|
|
40
|
+
NoteCorrected,
|
|
41
|
+
NoteDropped,
|
|
42
|
+
NoteErased,
|
|
43
|
+
NoteLifted,
|
|
44
|
+
NoteRetracted,
|
|
45
|
+
NoteWritten,
|
|
46
|
+
PageWritten,
|
|
47
|
+
ShareAnswered,
|
|
48
|
+
ShareWanted,
|
|
49
|
+
memoryBrief,
|
|
50
|
+
memoryConfirm,
|
|
51
|
+
memoryConsolidate,
|
|
52
|
+
memoryContext,
|
|
53
|
+
memoryCorrect,
|
|
54
|
+
memoryDrop,
|
|
55
|
+
memoryErase,
|
|
56
|
+
memoryGet,
|
|
57
|
+
memoryLift,
|
|
58
|
+
memoryList,
|
|
59
|
+
memoryNote,
|
|
60
|
+
memoryPageGet,
|
|
61
|
+
memoryPageList,
|
|
62
|
+
memoryPageWrite,
|
|
63
|
+
memoryRetract,
|
|
64
|
+
memorySearch,
|
|
65
|
+
memoryShare,
|
|
66
|
+
memoryTop,
|
|
67
|
+
canonicalDirectory,
|
|
68
|
+
} from "./records.ts";
|
|
69
|
+
import type { ContextItem, MemoryName, NoteId, NoteView, PageView, SearchHit, Withheld } from "./records.ts";
|
|
70
|
+
import { holds, holdsFor } from "./holds.ts";
|
|
71
|
+
import { rank } from "./search.ts";
|
|
72
|
+
import { SHARE_QUESTION, shareDetail } from "./share-question.ts";
|
|
73
|
+
import { same } from "./same.ts";
|
|
74
|
+
import { topLayer } from "./top.ts";
|
|
75
|
+
|
|
76
|
+
export * from "./records.ts";
|
|
77
|
+
|
|
78
|
+
/** What the module is given. */
|
|
79
|
+
export interface MemoryOptions {
|
|
80
|
+
/**
|
|
81
|
+
* The address a caller speaks as: the binding the composition gives the messages module, so a session
|
|
82
|
+
* of an agent speaks as the agent. Default: a client with the owner's token speaks as the owner, and
|
|
83
|
+
* nobody else speaks as anybody.
|
|
84
|
+
*/
|
|
85
|
+
readonly bind?: CallerBinding;
|
|
86
|
+
/** How much a memory holds and shows. */
|
|
87
|
+
readonly limits?: Partial<MemoryLimits>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** How much a memory holds and shows, and when it consolidates. */
|
|
91
|
+
export interface MemoryLimits extends ConsolidationLimits {
|
|
92
|
+
/** The most characters the top layer of a memory shows, headings included. Default 4000, about a thousand tokens. */
|
|
93
|
+
readonly top: number;
|
|
94
|
+
/** How many of the latest digests of sessions a search reads. Default 200. */
|
|
95
|
+
readonly sessions: number;
|
|
96
|
+
/** How many records change a memory before a digest coming in consolidates it. Default 20. */
|
|
97
|
+
readonly consolidateEvery: number;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The limits a memory keeps unless the composition says otherwise: three sources lift a note, each part
|
|
104
|
+
* of the top layer holds twelve, a memory holds five hundred notes, and an unconfirmed note of one source
|
|
105
|
+
* goes stale after thirty days without a change.
|
|
106
|
+
*/
|
|
107
|
+
export const DEFAULT_LIMITS: MemoryLimits = { top: 4_000, sessions: 200, consolidateEvery: 20, liftAfter: 3, topNotes: 12, notes: 500, staleAfter: 30 * DAY_MS };
|
|
108
|
+
|
|
109
|
+
/** A refusal of the memory module: a memory the caller may not reach, a note it may not change. */
|
|
110
|
+
export class MemoryRefusal extends Error {
|
|
111
|
+
override name = "MemoryRefusal";
|
|
112
|
+
constructor(
|
|
113
|
+
readonly code: "not-yours" | "no-memory" | "not-owner" | "no-note" | "no-page" | "stale-page" | "erased" | "unbound" | "no-addressee",
|
|
114
|
+
message: string,
|
|
115
|
+
) {
|
|
116
|
+
super(message);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The default binding: a client with the owner's token is the owner; nobody else is anybody. */
|
|
121
|
+
function ownerOnly(caller: Caller): Address | undefined {
|
|
122
|
+
return caller.kind === "client" && caller.principal?.kind === "owner" ? owner : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The id of a note: a hash of the operation that wrote it, so a call made again names the same note. */
|
|
126
|
+
function noteIdOf(operation: string): NoteId {
|
|
127
|
+
return `note-${createHash("sha256").update(operation).digest("hex").slice(0, 12)}`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
/** The canonical form of a directory another module names, when it names one. */
|
|
133
|
+
function directoryOf(path: string | undefined): string | undefined {
|
|
134
|
+
return path === undefined ? undefined : canonicalDirectory(path);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The start of a long text, cut at a word. */
|
|
138
|
+
function opening(text: string, max = 280): string {
|
|
139
|
+
if (text.length <= max) return text;
|
|
140
|
+
const cut = text.slice(0, max);
|
|
141
|
+
const space = cut.lastIndexOf(" ");
|
|
142
|
+
return `${(space > max / 2 ? cut.slice(0, space) : cut).trimEnd()}…`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A digest a search reads, with its text when the journal still has it. */
|
|
146
|
+
export interface ReadDigest extends TakenDigest {
|
|
147
|
+
readonly text: string;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The notes that hold, the pages and the digests of a memory, as a search ranks them. */
|
|
151
|
+
export function searchable(
|
|
152
|
+
notes: readonly NoteView[],
|
|
153
|
+
pages: readonly PageView[],
|
|
154
|
+
digests: readonly ReadDigest[],
|
|
155
|
+
project: string | undefined,
|
|
156
|
+
): Array<{ text: string; value: SearchHit }> {
|
|
157
|
+
const ofNotes = notes
|
|
158
|
+
.filter((note) => holds(note) && holdsFor(note.project, project))
|
|
159
|
+
.map((note) => ({
|
|
160
|
+
order: note.written,
|
|
161
|
+
text: `${note.text}\n${note.detail ?? ""}`,
|
|
162
|
+
value: {
|
|
163
|
+
kind: "note" as const,
|
|
164
|
+
id: note.note,
|
|
165
|
+
text: note.text,
|
|
166
|
+
about: note.about,
|
|
167
|
+
...(note.project === undefined ? {} : { project: note.project }),
|
|
168
|
+
score: 0,
|
|
169
|
+
seq: note.seq,
|
|
170
|
+
},
|
|
171
|
+
}));
|
|
172
|
+
const ofPages = pages
|
|
173
|
+
.filter((page) => holdsFor(page.project, project))
|
|
174
|
+
.map((page) => ({
|
|
175
|
+
order: page.seq,
|
|
176
|
+
text: `${page.title}\n${page.text}`,
|
|
177
|
+
value: { kind: "page" as const, id: page.page, title: page.title, text: opening(page.text), ...(page.project === undefined ? {} : { project: page.project }), score: 0, seq: page.seq },
|
|
178
|
+
}));
|
|
179
|
+
const ofDigests = digests
|
|
180
|
+
.filter((digest) => holdsFor(digest.project, project))
|
|
181
|
+
.map((digest) => ({
|
|
182
|
+
order: digest.seq,
|
|
183
|
+
text: digest.text,
|
|
184
|
+
value: {
|
|
185
|
+
kind: "session" as const,
|
|
186
|
+
id: digest.turn,
|
|
187
|
+
title: digest.session,
|
|
188
|
+
text: opening(digest.text),
|
|
189
|
+
...(digest.project === undefined ? {} : { project: digest.project }),
|
|
190
|
+
score: 0,
|
|
191
|
+
seq: digest.seq,
|
|
192
|
+
},
|
|
193
|
+
}));
|
|
194
|
+
return [...ofNotes, ...ofPages, ...ofDigests].sort((a, b) => a.order - b.order).map(({ text, value }) => ({ text, value }));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The name of the question that asks the owner whether a note about him may leave a memory. */
|
|
198
|
+
const SHARE_QUESTION_NAME = QuestionName.parse(SHARE_QUESTION);
|
|
199
|
+
|
|
200
|
+
/** The memory module. */
|
|
201
|
+
export function memory(options: MemoryOptions = {}): Plugin {
|
|
202
|
+
const bind = options.bind ?? ownerOnly;
|
|
203
|
+
const limits: MemoryLimits = { ...DEFAULT_LIMITS, ...options.limits };
|
|
204
|
+
|
|
205
|
+
return plugin("memory", (api) => {
|
|
206
|
+
api.declare(...MEMORY_TYPES);
|
|
207
|
+
// The page of this plugin in the cockpit: a client module, offered by the path of its source.
|
|
208
|
+
api.offer(COCKPIT_MODULE, { id: "memory", entry: fileURLToPath(new URL("./cockpit/index.tsx", import.meta.url)) });
|
|
209
|
+
|
|
210
|
+
const journal = (): JournalReader => {
|
|
211
|
+
const reader = api.journal?.();
|
|
212
|
+
if (reader === undefined) throw new Error("the memory is read from the journal, and the host is not serving it");
|
|
213
|
+
return reader;
|
|
214
|
+
};
|
|
215
|
+
const types = Memories.TYPES.map((name) => recordTypeName(name));
|
|
216
|
+
|
|
217
|
+
// The clock of the host: what a consolidation measures staleness by.
|
|
218
|
+
let clock: () => number = Date.now;
|
|
219
|
+
api.service((ctx) => {
|
|
220
|
+
clock = () => ctx.now();
|
|
221
|
+
return () => {};
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// The memories as they stand now, folded on from where the last read stopped.
|
|
225
|
+
const live = new Memories();
|
|
226
|
+
const now = (): Memories => {
|
|
227
|
+
for (const stored of journal().since(seq(live.seq), types)) live.apply(stored);
|
|
228
|
+
return live;
|
|
229
|
+
};
|
|
230
|
+
/** The memories as they stood right after the record at `at`. */
|
|
231
|
+
const asOf = (at: number | undefined): Memories => {
|
|
232
|
+
if (at === undefined) return now();
|
|
233
|
+
const then = new Memories();
|
|
234
|
+
for (const stored of journal().since(seq(0), types)) {
|
|
235
|
+
if (Number(stored.seq) > at) break;
|
|
236
|
+
then.apply(stored);
|
|
237
|
+
}
|
|
238
|
+
then.eraseAsIn(now());
|
|
239
|
+
return then;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
/** Who writes, as a note says it: the address the caller speaks as, or niwa itself. */
|
|
243
|
+
const authorOf = (caller: Caller): TurnAuthor => {
|
|
244
|
+
const bound = bind(caller);
|
|
245
|
+
if (bound !== undefined) return bound;
|
|
246
|
+
if (caller.kind === "handler") return { kind: "niwa", by: String(caller.handler) };
|
|
247
|
+
if (caller.kind === "host") return { kind: "niwa", by: "host" };
|
|
248
|
+
throw new MemoryRefusal("unbound", "the caller is not bound to an address, so it reaches no memory");
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The memory a call reaches, and who makes it. An agent reaches its own; the owner and niwa itself
|
|
253
|
+
* name the one they mean; any other address reaches none.
|
|
254
|
+
*/
|
|
255
|
+
const reach = (ctx: ActionContext, named: MemoryName | undefined): { memory: MemoryName; by: TurnAuthor } => {
|
|
256
|
+
const by = authorOf(ctx.caller);
|
|
257
|
+
if (by.kind === "agent") {
|
|
258
|
+
if (named !== undefined && named !== by.id) {
|
|
259
|
+
throw new MemoryRefusal("not-yours", `agent ${by.id} reaches only its own memory, not the memory of ${named}`);
|
|
260
|
+
}
|
|
261
|
+
return { memory: by.id as MemoryName, by };
|
|
262
|
+
}
|
|
263
|
+
if (by.kind !== "owner" && by.kind !== "niwa") {
|
|
264
|
+
throw new MemoryRefusal("not-yours", `${by.kind} ${"id" in by ? by.id : ""} has no memory of its own and reaches no other`);
|
|
265
|
+
}
|
|
266
|
+
if (named === undefined) throw new MemoryRefusal("no-memory", "name the memory: the agent whose memory it is");
|
|
267
|
+
return { memory: named, by };
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const noteOf = (memories: Memories, memory: MemoryName, note: NoteId): NoteView => {
|
|
271
|
+
const found = memories.note(memory, note);
|
|
272
|
+
if (found === undefined) throw new MemoryRefusal("no-note", `the memory of ${memory} has no note ${note}`);
|
|
273
|
+
return found;
|
|
274
|
+
};
|
|
275
|
+
const changeable = (memory: MemoryName, note: NoteId): NoteView => {
|
|
276
|
+
const found = noteOf(now(), memory, note);
|
|
277
|
+
if (found.status === "erased") throw new MemoryRefusal("erased", `the note ${note} was erased`);
|
|
278
|
+
return found;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* What this operation wrote as its `n`-th record, when it is being made again after the record was written and
|
|
283
|
+
* before its result was saved. The id of a record follows the operation, so the record is found by it: the repeat
|
|
284
|
+
* answers from what was written, and does not plan again against a memory its own first run has changed.
|
|
285
|
+
*/
|
|
286
|
+
const wrote = <T>(ctx: ActionContext, type: { readonly name: string; readonly schema: { safeParse(value: unknown): { success: true; data: T } | { success: false } } }, n = 1): { data: T; seq: number } | undefined => {
|
|
287
|
+
const stored = journal().get(recordId(`${ctx.operationId}/emit/${String(n)}`));
|
|
288
|
+
const data = stored === undefined ? undefined : dataAs(stored, type);
|
|
289
|
+
return stored === undefined || data === undefined ? undefined : { data, seq: Number(stored.seq) };
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
api.action(
|
|
293
|
+
action(memoryNote, {
|
|
294
|
+
// The id follows the operation: a call made again writes the same note once.
|
|
295
|
+
recovery: "repeat",
|
|
296
|
+
effect: "record",
|
|
297
|
+
world: "niwa",
|
|
298
|
+
run: async (input, ctx) => {
|
|
299
|
+
const { memory, by } = reach(ctx, input.memory);
|
|
300
|
+
if (input.source.kind === "owner" && by.kind !== "owner") {
|
|
301
|
+
throw new MemoryRefusal("not-owner", "only the owner gives a note the owner as its source: what someone else says of him is their word");
|
|
302
|
+
}
|
|
303
|
+
const written = wrote(ctx, NoteWritten);
|
|
304
|
+
if (written !== undefined) return { memory: written.data.memory, note: written.data.note, corroborated: false };
|
|
305
|
+
const joined = wrote(ctx, NoteCorroborated);
|
|
306
|
+
if (joined !== undefined) return { memory: joined.data.memory, note: joined.data.note, corroborated: true };
|
|
307
|
+
// The memory holds it already, in the same words: it gains a source, not a copy.
|
|
308
|
+
const memories = now();
|
|
309
|
+
const held = memories
|
|
310
|
+
.notesOf(memory)
|
|
311
|
+
.find((one) => holds(one) && one.about === input.about && one.project === input.project && same(one.text, input.text));
|
|
312
|
+
if (held !== undefined) {
|
|
313
|
+
if (!memories.counts(memory, held.note, sourceKey(input.source, by))) {
|
|
314
|
+
await ctx.emit(NoteCorroborated, { memory, note: held.note, source: input.source, by });
|
|
315
|
+
}
|
|
316
|
+
return { memory, note: held.note, corroborated: true };
|
|
317
|
+
}
|
|
318
|
+
const note = noteIdOf(String(ctx.operationId));
|
|
319
|
+
await ctx.emit(NoteWritten, {
|
|
320
|
+
memory,
|
|
321
|
+
note,
|
|
322
|
+
text: input.text,
|
|
323
|
+
...(input.detail === undefined ? {} : { detail: input.detail }),
|
|
324
|
+
about: input.about,
|
|
325
|
+
...(input.project === undefined ? {} : { project: input.project }),
|
|
326
|
+
source: input.source,
|
|
327
|
+
by,
|
|
328
|
+
});
|
|
329
|
+
return { memory, note, corroborated: false };
|
|
330
|
+
},
|
|
331
|
+
}),
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
api.action(
|
|
335
|
+
action(memoryGet, {
|
|
336
|
+
recovery: "repeat",
|
|
337
|
+
effect: "read",
|
|
338
|
+
world: "niwa",
|
|
339
|
+
run: (input, ctx) => {
|
|
340
|
+
const { memory } = reach(ctx, input.memory);
|
|
341
|
+
return noteOf(asOf(input.at), memory, input.note);
|
|
342
|
+
},
|
|
343
|
+
}),
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
api.action(
|
|
347
|
+
action(memoryList, {
|
|
348
|
+
recovery: "repeat",
|
|
349
|
+
effect: "read",
|
|
350
|
+
world: "niwa",
|
|
351
|
+
run: (input, ctx) => {
|
|
352
|
+
const { memory } = reach(ctx, input.memory);
|
|
353
|
+
const notes = asOf(input.at)
|
|
354
|
+
.notesOf(memory)
|
|
355
|
+
.filter(holds)
|
|
356
|
+
.filter((note) => input.about === undefined || note.about === input.about)
|
|
357
|
+
.filter((note) => holdsFor(note.project, input.project))
|
|
358
|
+
.reverse()
|
|
359
|
+
.slice(0, input.limit);
|
|
360
|
+
return { memory, notes };
|
|
361
|
+
},
|
|
362
|
+
}),
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
api.action(
|
|
366
|
+
action(memoryConfirm, {
|
|
367
|
+
recovery: "repeat",
|
|
368
|
+
effect: "record",
|
|
369
|
+
world: "niwa",
|
|
370
|
+
idempotent: true,
|
|
371
|
+
run: async (input, ctx) => {
|
|
372
|
+
if (bind(ctx.caller)?.kind !== "owner") throw new MemoryRefusal("not-owner", "only the owner confirms a note: a confirmation is his word");
|
|
373
|
+
const found = changeable(input.memory, input.note);
|
|
374
|
+
if (!found.confirmed) await ctx.emit(NoteConfirmed, { memory: input.memory, note: input.note, by: owner });
|
|
375
|
+
return { note: input.note, confirmed: true };
|
|
376
|
+
},
|
|
377
|
+
}),
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
api.action(
|
|
381
|
+
action(memoryCorrect, {
|
|
382
|
+
recovery: "repeat",
|
|
383
|
+
effect: "record",
|
|
384
|
+
world: "niwa",
|
|
385
|
+
run: async (input, ctx) => {
|
|
386
|
+
const { memory, by } = reach(ctx, input.memory);
|
|
387
|
+
const corrected = wrote(ctx, NoteCorrected);
|
|
388
|
+
if (corrected !== undefined) return { note: input.note, revision: asOf(corrected.seq).note(memory, input.note)?.revision ?? 0 };
|
|
389
|
+
const found = changeable(memory, input.note);
|
|
390
|
+
await ctx.emit(NoteCorrected, { memory, note: input.note, text: input.text, ...(input.detail === undefined ? {} : { detail: input.detail }), by });
|
|
391
|
+
return { note: input.note, revision: found.revision + 1 };
|
|
392
|
+
},
|
|
393
|
+
}),
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
api.action(
|
|
397
|
+
action(memoryRetract, {
|
|
398
|
+
recovery: "repeat",
|
|
399
|
+
effect: "record",
|
|
400
|
+
world: "niwa",
|
|
401
|
+
idempotent: true,
|
|
402
|
+
run: async (input, ctx) => {
|
|
403
|
+
const { memory, by } = reach(ctx, input.memory);
|
|
404
|
+
const found = changeable(memory, input.note);
|
|
405
|
+
if (found.status === "live") await ctx.emit(NoteRetracted, { memory, note: input.note, ...(input.reason === undefined ? {} : { reason: input.reason }), by });
|
|
406
|
+
return { note: input.note };
|
|
407
|
+
},
|
|
408
|
+
}),
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
api.action(
|
|
412
|
+
action(memoryErase, {
|
|
413
|
+
recovery: "repeat",
|
|
414
|
+
effect: "record",
|
|
415
|
+
world: "niwa",
|
|
416
|
+
idempotent: true,
|
|
417
|
+
destructive: true,
|
|
418
|
+
run: async (input, ctx) => {
|
|
419
|
+
const { memory, by } = reach(ctx, input.memory);
|
|
420
|
+
const found = noteOf(now(), memory, input.note);
|
|
421
|
+
if (found.status !== "erased") await ctx.emit(NoteErased, { memory, note: input.note, by });
|
|
422
|
+
return { note: input.note };
|
|
423
|
+
},
|
|
424
|
+
}),
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
api.action(
|
|
428
|
+
action(memoryPageWrite, {
|
|
429
|
+
recovery: "repeat",
|
|
430
|
+
effect: "record",
|
|
431
|
+
world: "niwa",
|
|
432
|
+
run: async (input, ctx) => {
|
|
433
|
+
const { memory, by } = reach(ctx, input.memory);
|
|
434
|
+
const done = wrote(ctx, PageWritten);
|
|
435
|
+
if (done !== undefined) return { page: done.data.page, revision: done.data.revision };
|
|
436
|
+
const basedOn = input.revision ?? now().page(memory, input.page)?.revision ?? 0;
|
|
437
|
+
const revision = basedOn + 1;
|
|
438
|
+
await ctx.emit(PageWritten, {
|
|
439
|
+
basedOn,
|
|
440
|
+
memory,
|
|
441
|
+
page: input.page,
|
|
442
|
+
title: input.title,
|
|
443
|
+
text: input.text,
|
|
444
|
+
...(input.project === undefined ? {} : { project: input.project }),
|
|
445
|
+
revision,
|
|
446
|
+
by,
|
|
447
|
+
});
|
|
448
|
+
return { page: input.page, revision };
|
|
449
|
+
},
|
|
450
|
+
}),
|
|
451
|
+
);
|
|
452
|
+
|
|
453
|
+
api.action(
|
|
454
|
+
action(memoryPageGet, {
|
|
455
|
+
recovery: "repeat",
|
|
456
|
+
effect: "read",
|
|
457
|
+
world: "niwa",
|
|
458
|
+
run: (input, ctx) => {
|
|
459
|
+
const { memory } = reach(ctx, input.memory);
|
|
460
|
+
const found = asOf(input.at).page(memory, input.page);
|
|
461
|
+
if (found === undefined) throw new MemoryRefusal("no-page", `the memory of ${memory} has no page ${input.page}`);
|
|
462
|
+
return found;
|
|
463
|
+
},
|
|
464
|
+
}),
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
api.action(
|
|
468
|
+
action(memoryPageList, {
|
|
469
|
+
recovery: "repeat",
|
|
470
|
+
effect: "read",
|
|
471
|
+
world: "niwa",
|
|
472
|
+
run: (input, ctx) => {
|
|
473
|
+
const { memory } = reach(ctx, input.memory);
|
|
474
|
+
const pages = now()
|
|
475
|
+
.pagesOf(memory)
|
|
476
|
+
.filter((page) => holdsFor(page.project, input.project))
|
|
477
|
+
.map(({ text: _text, ...summary }) => summary)
|
|
478
|
+
.sort((a, b) => a.page.localeCompare(b.page));
|
|
479
|
+
return { memory, pages };
|
|
480
|
+
},
|
|
481
|
+
}),
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
/** The latest digests, within the limit, with their texts read from the turns that told them. */
|
|
485
|
+
const readDigests = (digests: readonly TakenDigest[]): ReadDigest[] =>
|
|
486
|
+
digests.slice(-limits.sessions).flatMap((digest) => {
|
|
487
|
+
const told = journal().get(recordId(digest.record));
|
|
488
|
+
const parsed = told === undefined ? undefined : AgentTurnQueued.schema.safeParse(told.data);
|
|
489
|
+
return parsed?.success === true ? [{ ...digest, text: parsed.data.text }] : [];
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
api.action(
|
|
493
|
+
action(memorySearch, {
|
|
494
|
+
recovery: "repeat",
|
|
495
|
+
effect: "read",
|
|
496
|
+
world: "niwa",
|
|
497
|
+
run: (input, ctx) => {
|
|
498
|
+
const { memory } = reach(ctx, input.memory);
|
|
499
|
+
const memories = asOf(input.at);
|
|
500
|
+
const found = rank(input.query, searchable(memories.notesOf(memory), memories.pagesOf(memory), readDigests(memories.digestsOf(memory)), input.project), input.limit);
|
|
501
|
+
return { memory, hits: found.map(({ value, score }) => ({ ...value, score })) };
|
|
502
|
+
},
|
|
503
|
+
}),
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
/** Put a note in or out of the top layer by hand. */
|
|
507
|
+
const place = async (ctx: ActionContext, named: MemoryName | undefined, note: NoteId, top: boolean) => {
|
|
508
|
+
const { memory, by } = reach(ctx, named);
|
|
509
|
+
const found = changeable(memory, note);
|
|
510
|
+
if (found.status !== "live") throw new MemoryRefusal("no-note", `the note ${note} no longer holds`);
|
|
511
|
+
if (found.top !== top || !found.pinned) await ctx.emit(top ? NoteLifted : NoteDropped, { memory, note, by });
|
|
512
|
+
return { note, top };
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
api.action(
|
|
516
|
+
action(memoryLift, { recovery: "repeat", effect: "record", world: "niwa", idempotent: true, run: (input, ctx) => place(ctx, input.memory, input.note, true) }),
|
|
517
|
+
);
|
|
518
|
+
api.action(
|
|
519
|
+
action(memoryDrop, { recovery: "repeat", effect: "record", world: "niwa", idempotent: true, run: (input, ctx) => place(ctx, input.memory, input.note, false) }),
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
api.action(
|
|
523
|
+
action(memoryTop, {
|
|
524
|
+
recovery: "repeat",
|
|
525
|
+
effect: "read",
|
|
526
|
+
world: "niwa",
|
|
527
|
+
run: (input, ctx) => {
|
|
528
|
+
const { memory } = reach(ctx, input.memory);
|
|
529
|
+
return { memory, ...topLayer(now().notesOf(memory), input.project, limits.top) };
|
|
530
|
+
},
|
|
531
|
+
}),
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
/** The directory the session attached to an agent last works in. */
|
|
535
|
+
const attachedLast = (agent: string): string | undefined => {
|
|
536
|
+
let cwd: string | undefined;
|
|
537
|
+
for (const stored of journal().since(seq(0), [recordTypeName(AgentAttached.name)])) {
|
|
538
|
+
const parsed = AgentAttached.schema.safeParse(stored.data);
|
|
539
|
+
if (parsed.success && parsed.data.agent === agent) cwd = parsed.data.cwd;
|
|
540
|
+
}
|
|
541
|
+
return directoryOf(cwd);
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* The directory a session was attached to an agent in when the record at `before` was written: its latest
|
|
546
|
+
* attachment up to there, none when it was attached to another agent since, or let go of.
|
|
547
|
+
*/
|
|
548
|
+
const attachedAt = (agent: string, session: string, before: number): string | undefined => {
|
|
549
|
+
let attached: { agent: string; cwd: string } | undefined;
|
|
550
|
+
for (const stored of journal().since(seq(0), [recordTypeName(AgentAttached.name), recordTypeName(AgentDetached.name)])) {
|
|
551
|
+
if (Number(stored.seq) >= before) break;
|
|
552
|
+
const was = dataAs(stored, AgentAttached);
|
|
553
|
+
if (was !== undefined && was.session === session) attached = { agent: was.agent, cwd: was.cwd };
|
|
554
|
+
const gone = dataAs(stored, AgentDetached);
|
|
555
|
+
if (gone !== undefined && gone.session === session && gone.agent === attached?.agent) attached = undefined;
|
|
556
|
+
}
|
|
557
|
+
return attached?.agent === agent ? directoryOf(attached.cwd) : undefined;
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// The digests of attached sessions reach the memory of their agent: where the digest is, not a copy.
|
|
561
|
+
api.on(SessionDigestTaken, { id: "digest-intake", from: "start", concurrency: { by: turn => turn.agent } }, async (turn, step, told) => {
|
|
562
|
+
if (turn.source?.kind !== "session-digest") return;
|
|
563
|
+
const project = attachedAt(turn.agent, turn.source.session, Number(told.seq));
|
|
564
|
+
await step.emit(
|
|
565
|
+
DigestTaken,
|
|
566
|
+
{
|
|
567
|
+
memory: turn.agent,
|
|
568
|
+
turn: turn.turn,
|
|
569
|
+
record: String(told.id),
|
|
570
|
+
session: turn.source.session,
|
|
571
|
+
...(turn.source.from === undefined ? {} : { from: turn.source.from }),
|
|
572
|
+
upto: turn.source.upto,
|
|
573
|
+
...(project === undefined ? {} : { project }),
|
|
574
|
+
},
|
|
575
|
+
{ key: "taken" },
|
|
576
|
+
);
|
|
577
|
+
// Enough has changed in the memory since it was last consolidated: it consolidates now.
|
|
578
|
+
if (now().changesOf(turn.agent) >= limits.consolidateEvery) await step(memoryConsolidate, { memory: turn.agent }, { key: "consolidate" });
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
// The same top layer, in the briefing and in the prompt a new session of the agent starts with.
|
|
582
|
+
api.offer(AGENT_BRIEFING, { title: "What it remembers", action: memoryBrief.name });
|
|
583
|
+
api.offer(AGENT_PROMPT_SECTION, { id: "top", action: memoryBrief.name });
|
|
584
|
+
api.action(
|
|
585
|
+
action(memoryBrief, {
|
|
586
|
+
recovery: "repeat",
|
|
587
|
+
effect: "read",
|
|
588
|
+
world: "niwa",
|
|
589
|
+
run: async (input, ctx) => {
|
|
590
|
+
const { memory } = reach(ctx, input.agent);
|
|
591
|
+
// The project the agent is bound to; without one, where the session attached to it last works.
|
|
592
|
+
const got = await ctx.step.outcome(agentGet, { agent: input.agent });
|
|
593
|
+
const project = directoryOf(got.status === "completed" ? got.value.definition.project?.cwd : undefined) ?? attachedLast(input.agent);
|
|
594
|
+
return { text: topLayer(now().notesOf(memory), project, limits.top).text };
|
|
595
|
+
},
|
|
596
|
+
}),
|
|
597
|
+
);
|
|
598
|
+
|
|
599
|
+
const planConsolidation = (memory: MemoryName) => {
|
|
600
|
+
const memories = now();
|
|
601
|
+
return consolidate(memories.notesOf(memory), limits, clock(), (note, merged) =>
|
|
602
|
+
memories.sourcesWith(memory, note.note, merged.map((one) => one.note)),
|
|
603
|
+
);
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
api.action(
|
|
607
|
+
action(memoryConsolidate, {
|
|
608
|
+
// A consolidation made again finds its work done: it is planned from the memory as it stands.
|
|
609
|
+
recovery: "repeat",
|
|
610
|
+
effect: "record",
|
|
611
|
+
world: "niwa",
|
|
612
|
+
run: async (input, ctx) => {
|
|
613
|
+
const { memory } = reach(ctx, input.memory);
|
|
614
|
+
const recorded = wrote(ctx, Consolidated, 2);
|
|
615
|
+
if (recorded !== undefined) return memoryConsolidate.output.parse(recorded.data);
|
|
616
|
+
const reader = journal();
|
|
617
|
+
const [latest] = reader.tail({ types: [ConsolidationStarted.name, Consolidated.name], filter: { memory } }, 1);
|
|
618
|
+
if (latest?.type === ConsolidationStarted.name && reader.openingSeq !== undefined && latest.seq <= reader.openingSeq) {
|
|
619
|
+
const { operation } = ConsolidationStarted.schema.parse(latest.data);
|
|
620
|
+
if (latest.id === recordId(`${operation}/emit/1`)) {
|
|
621
|
+
const result = await api.emit(Consolidated, { memory, operation, ...planConsolidation(memory) }, {
|
|
622
|
+
id: recordId(`${operation}/emit/2`),
|
|
623
|
+
links: [latest.id],
|
|
624
|
+
});
|
|
625
|
+
if (operation === ctx.operationId) return memoryConsolidate.output.parse(result.data);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
await ctx.emit(ConsolidationStarted, { memory, operation: ctx.operationId });
|
|
629
|
+
const done = planConsolidation(memory);
|
|
630
|
+
await ctx.emit(Consolidated, { memory, operation: ctx.operationId, ...done });
|
|
631
|
+
return { memory, ...done };
|
|
632
|
+
},
|
|
633
|
+
}),
|
|
634
|
+
);
|
|
635
|
+
|
|
636
|
+
/** Whom a context request is for: the address the caller speaks as, or the one a handler names. */
|
|
637
|
+
const addresseeOf = (ctx: ActionContext, named: Address | undefined): Address => {
|
|
638
|
+
const bound = bind(ctx.caller);
|
|
639
|
+
if (bound !== undefined) {
|
|
640
|
+
if (named !== undefined && stableJson(named) !== stableJson(bound)) {
|
|
641
|
+
throw new MemoryRefusal("not-yours", `${addressText(bound)} asks for itself: a caller bound to an address names no other addressee`);
|
|
642
|
+
}
|
|
643
|
+
return bound;
|
|
644
|
+
}
|
|
645
|
+
if (ctx.caller.kind === "client") throw new MemoryRefusal("unbound", "the caller is not bound to an address, so it is told nothing");
|
|
646
|
+
if (named === undefined) throw new MemoryRefusal("no-addressee", "name the addressee: whom the context is for");
|
|
647
|
+
return named;
|
|
648
|
+
};
|
|
649
|
+
|
|
650
|
+
api.action(
|
|
651
|
+
action(memoryContext, {
|
|
652
|
+
// It reads, and may write that a note about the owner is wanted: written once for each note and address.
|
|
653
|
+
recovery: "repeat",
|
|
654
|
+
effect: "record",
|
|
655
|
+
world: "niwa",
|
|
656
|
+
run: async (input, ctx) => {
|
|
657
|
+
const reader = addresseeOf(ctx, input.for);
|
|
658
|
+
const home = reader.kind === "owner" || (reader.kind === "agent" && reader.id === input.memory);
|
|
659
|
+
// What the addressee works on is awaited first. From here to the answer nothing waits: the memory is
|
|
660
|
+
// read once, at one position, and every choice below is made of that one reading.
|
|
661
|
+
let project: string | undefined;
|
|
662
|
+
if (!home && reader.kind === "agent") {
|
|
663
|
+
const got = await ctx.step.outcome(agentGet, { agent: reader.id });
|
|
664
|
+
project = directoryOf(got.status === "completed" ? got.value.definition.project?.cwd : undefined);
|
|
665
|
+
}
|
|
666
|
+
const at = input.at ?? Number(journal().last());
|
|
667
|
+
const memories = asOf(at);
|
|
668
|
+
const who: Reader = home ? { kind: "home" } : { kind: "other", ...(project === undefined ? {} : { project }) };
|
|
669
|
+
const holding = memories.notesOf(input.memory).filter((note) => holds(note) && holdsFor(note.project, input.project));
|
|
670
|
+
const stands = holding.map((note) => ({ note, standing: standing(note, who, memories.share(input.memory, note.note, note.revision, reader, input.purpose)) }));
|
|
671
|
+
// What backs a note is searched only for its own readers: a match on it would tell of it.
|
|
672
|
+
const readable = stands.filter((one) => one.standing === "told").map(({ note }) => ({ text: home ? `${note.text}\n${note.detail ?? ""}` : note.text, value: note }));
|
|
673
|
+
const items = rank(input.query, readable, input.limit).map(({ value }) => conclusion(value, who));
|
|
674
|
+
// The notes about the owner that match are ranked apart: they do not use up the room of what may be told.
|
|
675
|
+
const aboutOwner = stands.filter((one) => one.standing === "private").map(({ note }) => ({ text: note.text, value: note }));
|
|
676
|
+
const matched = rank(input.query, aboutOwner, input.limit).map(({ value }) => value);
|
|
677
|
+
const withheld: Withheld[] = [];
|
|
678
|
+
const asks: NoteView[] = [];
|
|
679
|
+
// An answer read again at its position asks nobody: it says only whether the owner had been asked.
|
|
680
|
+
const asking = input.at === undefined;
|
|
681
|
+
for (const note of matched) {
|
|
682
|
+
const share = memories.share(input.memory, note.note, note.revision, reader, input.purpose);
|
|
683
|
+
const status = share === "refused" ? "refused" : share === "asked" || (share === undefined && asking) ? "asked" : "not-asked";
|
|
684
|
+
withheld.push({ note: note.note, reason: "about-the-owner", status });
|
|
685
|
+
if (share === undefined && asking) asks.push(note);
|
|
686
|
+
}
|
|
687
|
+
// Made again after a cut, it writes what it wrote before, in the same order, and then what is still to ask.
|
|
688
|
+
const before: Array<z.output<typeof ShareWanted.schema>> = [];
|
|
689
|
+
for (let n = 1, again = wrote(ctx, ShareWanted, n); again !== undefined; again = wrote(ctx, ShareWanted, ++n)) before.push(again.data);
|
|
690
|
+
for (const wanted of before) await ctx.emit(ShareWanted, wanted);
|
|
691
|
+
for (const note of asks) {
|
|
692
|
+
if (before.some((wanted) => wanted.note === note.note && wanted.revision === note.revision)) continue;
|
|
693
|
+
await ctx.emit(ShareWanted, { memory: input.memory, note: note.note, revision: note.revision, for: reader, purpose: input.purpose });
|
|
694
|
+
}
|
|
695
|
+
// A note erased while the answer was being written is not told.
|
|
696
|
+
const erased = now();
|
|
697
|
+
return { memory: agentAddress(input.memory), for: reader, at, items: items.filter((item) => erased.note(input.memory, item.note)?.status !== "erased"), withheld };
|
|
698
|
+
},
|
|
699
|
+
}),
|
|
700
|
+
);
|
|
701
|
+
|
|
702
|
+
api.action(
|
|
703
|
+
action(memoryShare, {
|
|
704
|
+
recovery: "repeat",
|
|
705
|
+
effect: "record",
|
|
706
|
+
world: "niwa",
|
|
707
|
+
idempotent: true,
|
|
708
|
+
run: async (input, ctx) => {
|
|
709
|
+
if (bind(ctx.caller)?.kind !== "owner") throw new MemoryRefusal("not-owner", "only the owner says whether a note about him may leave");
|
|
710
|
+
const found = changeable(input.memory, input.note);
|
|
711
|
+
await ctx.emit(ShareAnswered, { memory: input.memory, note: input.note, revision: found.revision, for: input.for, ...(input.purpose === undefined ? {} : { purpose: input.purpose }), allowed: input.allowed });
|
|
712
|
+
return { note: input.note, allowed: input.allowed };
|
|
713
|
+
},
|
|
714
|
+
}),
|
|
715
|
+
);
|
|
716
|
+
|
|
717
|
+
// A note about the owner that is wanted is asked of him, once for each revision and address; the
|
|
718
|
+
// asks wait apart, so one he leaves open holds up no other.
|
|
719
|
+
api.on(ShareWanted, { id: "share-ask", concurrency: Number.POSITIVE_INFINITY }, async (wanted, step) => {
|
|
720
|
+
const note = now().note(wanted.memory, wanted.note);
|
|
721
|
+
// Changed since it was wanted: the next request asks for it as it is then.
|
|
722
|
+
if (note === undefined || note.status !== "live" || note.revision !== wanted.revision) return;
|
|
723
|
+
const asked = await step.outcome(requestAsk, {
|
|
724
|
+
to: owner,
|
|
725
|
+
answerPolicy: "owner-only",
|
|
726
|
+
question: SHARE_QUESTION_NAME,
|
|
727
|
+
from: agentAddress(wanted.memory),
|
|
728
|
+
input: {
|
|
729
|
+
kind: "confirm",
|
|
730
|
+
title: `May ${addressText(wanted.for)} be told what ${addressText(agentAddress(wanted.memory))} remembers about you?`,
|
|
731
|
+
// The note itself is not in the question: whoever reads the open requests would read it. The owner's page
|
|
732
|
+
// reads the note by its reference, and shows it only while it holds as it was when he was asked.
|
|
733
|
+
detail: shareDetail({ memory: wanted.memory, note: wanted.note, revision: wanted.revision }, wanted.purpose),
|
|
734
|
+
options: confirmOptions(),
|
|
735
|
+
},
|
|
736
|
+
});
|
|
737
|
+
const decided = { memory: wanted.memory, note: wanted.note, revision: wanted.revision, for: wanted.for, purpose: wanted.purpose };
|
|
738
|
+
if (asked.status !== "completed" || !("answer" in asked.value)) {
|
|
739
|
+
await step.emit(ShareAnswered, { ...decided, allowed: false, reason: "unanswered" }, { key: "answered" });
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
const { id, answer } = asked.value;
|
|
743
|
+
await step.emit(
|
|
744
|
+
ShareAnswered,
|
|
745
|
+
{ ...decided, allowed: isYes(answer), request: id },
|
|
746
|
+
{ key: "answered" },
|
|
747
|
+
);
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
// A question about a note that no longer holds as it was asked is closed: the owner is not left to answer for
|
|
751
|
+
// a text he can no longer be shown, nor for one that was erased.
|
|
752
|
+
const closeStale = (id: string) => async (changed: { memory: MemoryName; note: NoteId }, step: Step) => {
|
|
753
|
+
const note = now().note(changed.memory, changed.note);
|
|
754
|
+
for (const stored of journal().since(seq(0), [recordTypeName(ShareWanted.name)])) {
|
|
755
|
+
const wanted = dataAs(stored, ShareWanted);
|
|
756
|
+
if (wanted === undefined || wanted.memory !== changed.memory || wanted.note !== changed.note) continue;
|
|
757
|
+
if (note !== undefined && note.status === "live" && note.revision === wanted.revision) continue;
|
|
758
|
+
const key = `${id}-${String(stored.seq)}`;
|
|
759
|
+
const found = await step.outcome(requestFind, { cause: String(stored.id), question: SHARE_QUESTION_NAME }, { key: `find:${key}` });
|
|
760
|
+
if (found.status !== "completed" || found.value.id === undefined) continue;
|
|
761
|
+
const asked = await step.outcome(requestGet, { id: found.value.id }, { key: `get:${key}` });
|
|
762
|
+
if (asked.status !== "completed" || asked.value.status !== "open") continue;
|
|
763
|
+
await step.outcome(requestCancel, { id: found.value.id, by: agentAddress(changed.memory), reason: "the note changed since it was asked" }, { key: `cancel:${key}` });
|
|
764
|
+
}
|
|
765
|
+
};
|
|
766
|
+
api.on(NoteErased, { id: "share-close-erased" }, closeStale("erased"));
|
|
767
|
+
api.on(NoteCorrected, { id: "share-close-corrected" }, closeStale("corrected"));
|
|
768
|
+
api.on(NoteRetracted, { id: "share-close-retracted" }, closeStale("retracted"));
|
|
769
|
+
|
|
770
|
+
api.summarize(() => {
|
|
771
|
+
const memories = api.journal?.() === undefined ? undefined : now();
|
|
772
|
+
return { notes: memories === undefined ? 0 : memories.count() };
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
}
|