@kadaliao/dsh-bots 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -4
- package/README.zh-CN.md +64 -4
- package/client.js +1308 -36
- package/index.js +878 -18
- package/lib/channels.mjs +32 -0
- package/lib/memory.mjs +508 -0
- package/lib/models.mjs +250 -0
- package/lib/prices.mjs +149 -0
- package/lib/settings.mjs +142 -0
- package/lib/store.mjs +4 -1
- package/lib/topics.mjs +103 -0
- package/lib/usage.mjs +267 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
package/lib/channels.mjs
CHANGED
|
@@ -263,6 +263,23 @@ export function welcomeText(bot, roster, { paired = false } = {}) {
|
|
|
263
263
|
}
|
|
264
264
|
export const PRIVATE_TEXT = 'This bot is private. / 这是私人 Bot,不对外开放。';
|
|
265
265
|
|
|
266
|
+
/**
|
|
267
|
+
* A "start a new topic" command at the start of a message: `/new`, `/clear`,
|
|
268
|
+
* `/reset` (with an optional `@botname` suffix) or `/新话题`. Returns the rest of
|
|
269
|
+
* the message, or null when it is no such command.
|
|
270
|
+
*/
|
|
271
|
+
export function topicCommand(text) {
|
|
272
|
+
const m = /^\/(new|clear|reset|新话题|新对话)(@\S+)?(?=$|\s)\s*/i.exec(String(text ?? '').trim());
|
|
273
|
+
return m ? { rest: String(text).trim().slice(m[0].length).trim() } : null;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** What a chat app hears when a new topic started. */
|
|
277
|
+
export function newTopicText(bot) {
|
|
278
|
+
return zhBot(bot)
|
|
279
|
+
? `已开始新话题:${bot.name} 的对话从头开始,它记得的事都还在。`
|
|
280
|
+
: `New topic: ${bot.name} starts a fresh conversation and keeps what it remembers.`;
|
|
281
|
+
}
|
|
282
|
+
|
|
266
283
|
// --- hint for the panel ----------------------------------------------------------------
|
|
267
284
|
|
|
268
285
|
/** A short code the panel words in its own language, from a connector's error text. */
|
|
@@ -639,6 +656,21 @@ export function createChannels(deps) {
|
|
|
639
656
|
}
|
|
640
657
|
const routed = routeMessage(text, owner, deps.bots());
|
|
641
658
|
if (!routed.text) return;
|
|
659
|
+
// `/new` starts the bot's next topic; text after it goes to the new one.
|
|
660
|
+
const cmd = typeof deps.newTopic === 'function' ? topicCommand(routed.text) : null;
|
|
661
|
+
if (cmd) {
|
|
662
|
+
try {
|
|
663
|
+
await deps.newTopic(routed.bot);
|
|
664
|
+
} catch (error) {
|
|
665
|
+
await reply(rec, chatId, redact(error?.message ?? error, rec.secrets).slice(0, 200));
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
if (!cmd.rest) {
|
|
669
|
+
await reply(rec, chatId, newTopicText(routed.bot));
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
routed.text = cmd.rest;
|
|
673
|
+
}
|
|
642
674
|
const header = sourceHeader(rec.kind, msg);
|
|
643
675
|
const origin = {
|
|
644
676
|
key: rec.key,
|
package/lib/memory.mjs
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bot memory: what a bot keeps across conversations and topics.
|
|
3
|
+
*
|
|
4
|
+
* - **Notes**: short durable facts, one sentence each. Every bot has its own;
|
|
5
|
+
* the team shares one more list every bot reads. Notes are written by the
|
|
6
|
+
* bot itself (`memory_save`), by the user in the panel, after every task by
|
|
7
|
+
* a reflection pass, and rewritten once a day by consolidation ("dreaming").
|
|
8
|
+
* - **Journal**: one line per finished task — what was asked and what came of
|
|
9
|
+
* it — so a bot knows what it did yesterday after its conversation moved on.
|
|
10
|
+
* - **Digests**: once a day the team's journal is written up as a short
|
|
11
|
+
* digest every bot reads the latest of.
|
|
12
|
+
*
|
|
13
|
+
* Files under `<bots cache>/memory/`: `notes.json` ({bots: {botId: Note[]}, team: Note[]}),
|
|
14
|
+
* `journal.jsonl`, `digests.json`, `dream.json`.
|
|
15
|
+
*
|
|
16
|
+
* The prompt builders and reply parsers here are pure; the model calls are the Host's.
|
|
17
|
+
*
|
|
18
|
+
* @module @kadaliao/dsh-bots/lib/memory
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { appendFileSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
22
|
+
import { join } from 'node:path';
|
|
23
|
+
|
|
24
|
+
export const KINDS = ['fact', 'preference', 'person', 'project', 'procedure', 'lesson'];
|
|
25
|
+
export const MAX_NOTE = 400;
|
|
26
|
+
export const BOT_CAP = 150;
|
|
27
|
+
export const TEAM_CAP = 100;
|
|
28
|
+
/** What the prompt shows at most, in characters. */
|
|
29
|
+
export const PROMPT_BUDGET = { bot: 6000, team: 4000, journal: 6, digest: 1500 };
|
|
30
|
+
const KEEP_JOURNAL = 20_000;
|
|
31
|
+
const KEEP_DIGESTS = 90;
|
|
32
|
+
|
|
33
|
+
/** Text that looks like a credential is never stored. */
|
|
34
|
+
const SECRET = /\b(sk-[A-Za-z0-9_-]{16,}|sk-ant-[A-Za-z0-9_-]{10,}|gh[pousr]_[A-Za-z0-9]{20,}|xox[abpr]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16}|AIza[0-9A-Za-z_-]{30,}|eyJ[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{20,})|-----BEGIN [A-Z ]*PRIVATE KEY-----/;
|
|
35
|
+
export const looksSecret = (text) => SECRET.test(String(text ?? ''));
|
|
36
|
+
|
|
37
|
+
const clean = (text) =>
|
|
38
|
+
String(text ?? '')
|
|
39
|
+
.replace(/\s+/g, ' ')
|
|
40
|
+
.trim()
|
|
41
|
+
.slice(0, MAX_NOTE);
|
|
42
|
+
/** Two notes say the same when their letters and digits match. */
|
|
43
|
+
const same = (a, b) => a.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, '') === b.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, '');
|
|
44
|
+
const newId = (prefix) => `${prefix}-${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
|
|
45
|
+
|
|
46
|
+
function normalizeNote(n, prefix) {
|
|
47
|
+
const text = clean(n?.text);
|
|
48
|
+
if (!text) return null;
|
|
49
|
+
const at = Number(n?.at) || Date.now();
|
|
50
|
+
return {
|
|
51
|
+
id: typeof n?.id === 'string' && n.id ? n.id.slice(0, 40) : newId(prefix),
|
|
52
|
+
text,
|
|
53
|
+
kind: KINDS.includes(n?.kind) ? n.kind : 'fact',
|
|
54
|
+
...(n?.pinned === true ? { pinned: true } : {}),
|
|
55
|
+
source: ['auto', 'tool', 'user', 'dream'].includes(n?.source) ? n.source : 'user',
|
|
56
|
+
at,
|
|
57
|
+
updated: Number(n?.updated) || at,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Model output → edits: `{op:'add', text, kind?}`, `{op:'update', id, text}`,
|
|
63
|
+
* `{op:'remove', id}`, `{op:'merge', ids, text}`. Anything else is dropped.
|
|
64
|
+
*/
|
|
65
|
+
export function normalizeOps(list) {
|
|
66
|
+
const out = [];
|
|
67
|
+
for (const o of Array.isArray(list) ? list.slice(0, 60) : []) {
|
|
68
|
+
if (!o || typeof o !== 'object') continue;
|
|
69
|
+
const op = String(o.op ?? (o.id ? 'update' : 'add')).toLowerCase();
|
|
70
|
+
const text = clean(o.text);
|
|
71
|
+
if (op === 'add' && text) out.push({ op, text, ...(KINDS.includes(o.kind) ? { kind: o.kind } : {}) });
|
|
72
|
+
else if (op === 'update' && typeof o.id === 'string' && text) out.push({ op, id: o.id, text, ...(KINDS.includes(o.kind) ? { kind: o.kind } : {}) });
|
|
73
|
+
else if ((op === 'remove' || op === 'delete' || op === 'forget') && typeof o.id === 'string') out.push({ op: 'remove', id: o.id });
|
|
74
|
+
else if (op === 'merge' && Array.isArray(o.ids) && o.ids.length > 0 && text) out.push({ op, ids: o.ids.map(String).slice(0, 20), text, ...(KINDS.includes(o.kind) ? { kind: o.kind } : {}) });
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Apply edits to a note list. Pinned notes are never removed or merged away by
|
|
81
|
+
* `auto`/`dream` edits; a note that would repeat another refreshes it instead.
|
|
82
|
+
* @returns {{notes: object[], added: number, updated: number, removed: number}}
|
|
83
|
+
*/
|
|
84
|
+
export function applyOps(notes, ops, { source = 'auto', prefix = 'n', cap = BOT_CAP, now = Date.now() } = {}) {
|
|
85
|
+
let list = notes.slice();
|
|
86
|
+
let added = 0;
|
|
87
|
+
let updated = 0;
|
|
88
|
+
let removed = 0;
|
|
89
|
+
/** Ids of the notes an edit added, refreshed or changed, in order. */
|
|
90
|
+
const touched = [];
|
|
91
|
+
// Only the user changes a pinned note; the machine and the bot leave it alone.
|
|
92
|
+
const protectedNote = (n) => n.pinned && source !== 'user';
|
|
93
|
+
for (const o of ops) {
|
|
94
|
+
if ((o.text && looksSecret(o.text)) || (o.op === 'add' && !o.text)) continue;
|
|
95
|
+
if (o.op === 'add') {
|
|
96
|
+
const dup = list.find((n) => same(n.text, o.text));
|
|
97
|
+
if (dup) {
|
|
98
|
+
dup.updated = now;
|
|
99
|
+
touched.push(dup.id);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const id = newId(prefix);
|
|
103
|
+
list.push({ id, text: o.text, kind: o.kind ?? 'fact', source, at: now, updated: now });
|
|
104
|
+
touched.push(id);
|
|
105
|
+
added += 1;
|
|
106
|
+
} else if (o.op === 'update') {
|
|
107
|
+
const n = list.find((x) => x.id === o.id);
|
|
108
|
+
if (!n) continue;
|
|
109
|
+
if (protectedNote(n)) continue;
|
|
110
|
+
n.text = o.text;
|
|
111
|
+
if (o.kind) n.kind = o.kind;
|
|
112
|
+
n.updated = now;
|
|
113
|
+
touched.push(n.id);
|
|
114
|
+
updated += 1;
|
|
115
|
+
} else if (o.op === 'remove') {
|
|
116
|
+
const n = list.find((x) => x.id === o.id);
|
|
117
|
+
if (!n || protectedNote(n)) continue;
|
|
118
|
+
list = list.filter((x) => x !== n);
|
|
119
|
+
removed += 1;
|
|
120
|
+
} else if (o.op === 'merge') {
|
|
121
|
+
const parts = list.filter((x) => o.ids.includes(x.id) && !protectedNote(x));
|
|
122
|
+
if (parts.length === 0) continue;
|
|
123
|
+
const keep = parts[0];
|
|
124
|
+
keep.text = o.text;
|
|
125
|
+
if (o.kind) keep.kind = o.kind;
|
|
126
|
+
keep.updated = now;
|
|
127
|
+
keep.source = source === 'dream' ? 'dream' : keep.source;
|
|
128
|
+
list = list.filter((x) => !parts.includes(x) || x === keep);
|
|
129
|
+
touched.push(keep.id);
|
|
130
|
+
removed += parts.length - 1;
|
|
131
|
+
updated += 1;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Over the cap: the oldest unpinned notes the machine wrote go first, then the oldest unpinned.
|
|
135
|
+
while (list.length > cap) {
|
|
136
|
+
const pool = list.filter((n) => !n.pinned);
|
|
137
|
+
if (pool.length === 0) break;
|
|
138
|
+
const auto = pool.filter((n) => n.source === 'auto' || n.source === 'dream');
|
|
139
|
+
const victim = (auto.length ? auto : pool).reduce((a, b) => (a.updated <= b.updated ? a : b));
|
|
140
|
+
list = list.filter((n) => n !== victim);
|
|
141
|
+
removed += 1;
|
|
142
|
+
}
|
|
143
|
+
return { notes: list, added, updated, removed, touched: touched.filter((id) => list.some((n) => n.id === id)) };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The first JSON object in a model's answer (code fences and prose around it allowed). */
|
|
147
|
+
export function parseJsonReply(text) {
|
|
148
|
+
const s = String(text ?? '');
|
|
149
|
+
const fenced = /```(?:json)?\s*([\s\S]*?)```/i.exec(s);
|
|
150
|
+
const body = fenced ? fenced[1] : s;
|
|
151
|
+
const start = body.indexOf('{');
|
|
152
|
+
if (start === -1) return null;
|
|
153
|
+
let depth = 0;
|
|
154
|
+
let inString = false;
|
|
155
|
+
for (let i = start; i < body.length; i += 1) {
|
|
156
|
+
const c = body[i];
|
|
157
|
+
if (inString) {
|
|
158
|
+
if (c === '\\') i += 1;
|
|
159
|
+
else if (c === '"') inString = false;
|
|
160
|
+
} else if (c === '"') inString = true;
|
|
161
|
+
else if (c === '{') depth += 1;
|
|
162
|
+
else if (c === '}') {
|
|
163
|
+
depth -= 1;
|
|
164
|
+
if (depth === 0) {
|
|
165
|
+
try {
|
|
166
|
+
return JSON.parse(body.slice(start, i + 1));
|
|
167
|
+
} catch {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const day = (ms, tz) => new Date(ms + tz * 60_000).toISOString().slice(0, 10);
|
|
177
|
+
const stamp = (ms, tz) => new Date(ms + tz * 60_000).toISOString().slice(5, 16).replace('T', ' ');
|
|
178
|
+
const listNotes = (notes, budget) => {
|
|
179
|
+
const sorted = notes.slice().sort((a, b) => Number(Boolean(b.pinned)) - Number(Boolean(a.pinned)) || b.updated - a.updated);
|
|
180
|
+
const lines = [];
|
|
181
|
+
let used = 0;
|
|
182
|
+
for (const n of sorted) {
|
|
183
|
+
const line = `- [${n.id}] ${n.text}`;
|
|
184
|
+
if (used + line.length > budget && lines.length > 0) break;
|
|
185
|
+
lines.push(line);
|
|
186
|
+
used += line.length + 1;
|
|
187
|
+
}
|
|
188
|
+
return { lines, hidden: sorted.length - lines.length };
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The memory section of one bot's system prompt.
|
|
193
|
+
* @param {{bot: object, notes: object[], team: object[], journal: object[], digest?: {date, text}|null, tz?: number}} m
|
|
194
|
+
*/
|
|
195
|
+
export function memoryText({ bot, notes, team, journal, digest, tz = 0 }) {
|
|
196
|
+
const out = [
|
|
197
|
+
'## Your memory',
|
|
198
|
+
'You keep long-term memory across conversations: a new topic starts a fresh conversation, but these notes, your recent work and the team digest stay. After every task a summary of it is saved by itself.',
|
|
199
|
+
'- `memory_save` (scope "self" or "team") when the user tells you to remember something, or you learn a lasting fact, preference, decision or lesson. Give `replace_id` when it corrects a note below. Use "team" only for what every bot should know (about the user, the business, shared conventions).',
|
|
200
|
+
'- `memory_forget` removes a note that is wrong or no longer true. `memory_search` looks through your notes and the team journal when the user refers to earlier work you cannot see.',
|
|
201
|
+
'- Never store secrets (keys, passwords, tokens).',
|
|
202
|
+
];
|
|
203
|
+
const own = listNotes(notes, PROMPT_BUDGET.bot);
|
|
204
|
+
out.push('', `### What you remember${own.hidden ? ` (newest ${own.lines.length} of ${notes.length}; memory_search finds the rest)` : ''}`);
|
|
205
|
+
out.push(...(own.lines.length ? own.lines : ['(nothing yet)']));
|
|
206
|
+
const shared = listNotes(team, PROMPT_BUDGET.team);
|
|
207
|
+
if (shared.lines.length) out.push('', `### Team memory (shared by every bot)${shared.hidden ? ` (newest ${shared.lines.length} of ${team.length})` : ''}`, ...shared.lines);
|
|
208
|
+
const recent = journal.slice(-PROMPT_BUDGET.journal);
|
|
209
|
+
if (recent.length) {
|
|
210
|
+
out.push('', '### Your recent work');
|
|
211
|
+
for (const j of recent) out.push(`- ${stamp(j.at, tz)} · ${clean(j.summary || j.title).slice(0, 240)}`);
|
|
212
|
+
}
|
|
213
|
+
if (digest?.text) out.push('', `### Latest team digest (${digest.date})`, String(digest.text).trim().slice(0, PROMPT_BUDGET.digest));
|
|
214
|
+
void bot;
|
|
215
|
+
return out.join('\n');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// --- prompts for the model -----------------------------------------------------------------
|
|
219
|
+
|
|
220
|
+
const opsSpec = [
|
|
221
|
+
'Edits are objects in a list:',
|
|
222
|
+
'- {"op":"add","text":"…","kind":"fact|preference|person|project|procedure|lesson"}',
|
|
223
|
+
'- {"op":"update","id":"<id>","text":"…"} — a note that changed or was wrong',
|
|
224
|
+
'- {"op":"remove","id":"<id>"} — a note that is no longer true',
|
|
225
|
+
'- {"op":"merge","ids":["<id>","<id>"],"text":"…"} — notes that say the same thing',
|
|
226
|
+
'Each note is one self-contained sentence of at most 200 characters, written in the language the user uses with the bots (Chinese stays Chinese). Never store secrets (keys, passwords, tokens), one-off details or anything already in memory.',
|
|
227
|
+
].join('\n');
|
|
228
|
+
|
|
229
|
+
const notesBlock = (notes) => (notes.length ? notes.map((n) => `[${n.id}]${n.pinned ? ' (pinned)' : ''} ${n.text}`).join('\n') : '(none)');
|
|
230
|
+
|
|
231
|
+
/** Shorten a transcript to its head and tail. */
|
|
232
|
+
export function clip(text, max = 12_000) {
|
|
233
|
+
const s = String(text ?? '');
|
|
234
|
+
if (s.length <= max) return s;
|
|
235
|
+
const head = Math.floor(max * 0.3);
|
|
236
|
+
return `${s.slice(0, head)}\n…(${s.length - max} characters left out)…\n${s.slice(s.length - (max - head))}`;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** After a task: summarize it and update the bot's notes (and propose team notes). */
|
|
240
|
+
export function reflectionPrompt({ bot, notes, team, transcript, trigger }) {
|
|
241
|
+
return {
|
|
242
|
+
system: [
|
|
243
|
+
`You keep the long-term memory of ${bot.name} (@${bot.handle}), one of the user's AI bots${bot.bio ? ` — ${bot.bio}` : ''}.`,
|
|
244
|
+
'You are given a task the bot just finished and its current notes. Answer with one JSON object and nothing else:',
|
|
245
|
+
'{"summary": "<one or two sentences: what was asked and what came of it, with names, numbers and links that matter>", "notes": [<edits to the bot\'s own notes>], "team": [<edits to the team notes>]}',
|
|
246
|
+
'',
|
|
247
|
+
opsSpec,
|
|
248
|
+
'Save only what will still matter in a week: stable preferences and facts about the user, people, projects and decisions; procedures that worked; lessons from mistakes. Most tasks need no note at all — then answer with empty lists. Team notes are only for what every bot should know.',
|
|
249
|
+
].join('\n'),
|
|
250
|
+
user: [`# The bot's notes\n${notesBlock(notes)}`, `# Team notes\n${notesBlock(team)}`, `# The task (${trigger})\n${clip(transcript)}`].join('\n\n'),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Once a day per bot: tidy the notes with the last days of its journal. */
|
|
255
|
+
export function dreamBotPrompt({ bot, notes, journal, tz = 0 }) {
|
|
256
|
+
return {
|
|
257
|
+
system: [
|
|
258
|
+
`You consolidate the long-term memory of ${bot.name} (@${bot.handle}), one of the user's AI bots${bot.bio ? ` — ${bot.bio}` : ''}, as it sleeps.`,
|
|
259
|
+
'Merge notes that repeat each other, fix ones the journal shows are outdated, drop one-off or trivial notes, and add durable lessons or facts the journal reveals that are missing. Keep pinned notes as they are. Keep the list short and useful (well under 80 notes).',
|
|
260
|
+
'Answer with one JSON object and nothing else: {"notes": [<edits>]}',
|
|
261
|
+
'',
|
|
262
|
+
opsSpec,
|
|
263
|
+
].join('\n'),
|
|
264
|
+
user: [`# Notes\n${notesBlock(notes)}`, `# Journal of recent tasks\n${clip(journal.map((j) => `${stamp(j.at, tz)} ${j.summary || j.title}`).join('\n'), 14_000) || '(none)'}`].join('\n\n'),
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Once a day for the team: a digest of what every bot did, and the shared notes tidied. */
|
|
269
|
+
export function dreamTeamPrompt({ bots, team, journal, date, tz = 0 }) {
|
|
270
|
+
const name = (id) => {
|
|
271
|
+
const b = bots.find((x) => x.id === id);
|
|
272
|
+
return b ? `@${b.handle}` : 'a removed bot';
|
|
273
|
+
};
|
|
274
|
+
return {
|
|
275
|
+
system: [
|
|
276
|
+
"You keep the shared memory of the user's team of AI bots.",
|
|
277
|
+
`Write the team's digest for ${date}: what was done (by whom), results worth remembering, what is still open or blocked, and what comes next — in Markdown, at most 1000 characters, in the language the user uses with the bots. Then tidy the shared team notes: merge repeats, drop outdated ones, add facts every bot should know that the journal shows.`,
|
|
278
|
+
'Answer with one JSON object and nothing else: {"digest": "…", "team": [<edits>]}',
|
|
279
|
+
'',
|
|
280
|
+
opsSpec,
|
|
281
|
+
].join('\n'),
|
|
282
|
+
user: [
|
|
283
|
+
`# Team: ${bots.map((b) => `@${b.handle} (${b.name})`).join(', ')}`,
|
|
284
|
+
`# Team notes\n${notesBlock(team)}`,
|
|
285
|
+
`# Journal for ${date}\n${clip(journal.map((j) => `${stamp(j.at, tz)} ${name(j.bot)}: ${j.summary || j.title}`).join('\n'), 16_000) || '(nothing happened)'}`,
|
|
286
|
+
].join('\n\n'),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// --- the store ----------------------------------------------------------------------------
|
|
291
|
+
|
|
292
|
+
/** The memory files under `dir`. */
|
|
293
|
+
export function createMemory(dir) {
|
|
294
|
+
const notesFile = join(dir, 'notes.json');
|
|
295
|
+
const journalFile = join(dir, 'journal.jsonl');
|
|
296
|
+
const digestsFile = join(dir, 'digests.json');
|
|
297
|
+
const dreamFile = join(dir, 'dream.json');
|
|
298
|
+
let state = { bots: {}, team: [] };
|
|
299
|
+
let seen = -2;
|
|
300
|
+
let journal = null;
|
|
301
|
+
let journalSeen = -2;
|
|
302
|
+
const listeners = new Set();
|
|
303
|
+
const mtime = (p) => {
|
|
304
|
+
try {
|
|
305
|
+
const s = statSync(p);
|
|
306
|
+
return s.mtimeMs * 1e6 + s.size;
|
|
307
|
+
} catch {
|
|
308
|
+
return -1;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
const readJson = (p, d) => {
|
|
312
|
+
try {
|
|
313
|
+
return JSON.parse(readFileSync(p, 'utf8'));
|
|
314
|
+
} catch {
|
|
315
|
+
return d;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
const writeJson = (p, v) => {
|
|
319
|
+
mkdirSync(dir, { recursive: true });
|
|
320
|
+
const tmp = `${p}.tmp`;
|
|
321
|
+
writeFileSync(tmp, JSON.stringify(v, null, 1), { encoding: 'utf8', mode: 0o600 });
|
|
322
|
+
renameSync(tmp, p);
|
|
323
|
+
};
|
|
324
|
+
function reload() {
|
|
325
|
+
const now = mtime(notesFile);
|
|
326
|
+
if (now === seen) return;
|
|
327
|
+
seen = now;
|
|
328
|
+
const raw = readJson(notesFile, {});
|
|
329
|
+
const bots = {};
|
|
330
|
+
for (const [id, list] of Object.entries(raw?.bots ?? {})) bots[id] = (Array.isArray(list) ? list : []).map((n) => normalizeNote(n, 'n')).filter(Boolean);
|
|
331
|
+
state = { bots, team: (Array.isArray(raw?.team) ? raw.team : []).map((n) => normalizeNote(n, 't')).filter(Boolean) };
|
|
332
|
+
}
|
|
333
|
+
function save() {
|
|
334
|
+
writeJson(notesFile, state);
|
|
335
|
+
seen = mtime(notesFile);
|
|
336
|
+
for (const fn of listeners) {
|
|
337
|
+
try {
|
|
338
|
+
fn();
|
|
339
|
+
} catch {
|
|
340
|
+
// A listener never breaks a write.
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const listOf = (scope) => (scope === 'team' ? state.team : (state.bots[scope] ??= []));
|
|
345
|
+
const setList = (scope, list) => {
|
|
346
|
+
if (scope === 'team') state.team = list;
|
|
347
|
+
else state.bots[scope] = list;
|
|
348
|
+
};
|
|
349
|
+
function readJournal() {
|
|
350
|
+
const now = mtime(journalFile);
|
|
351
|
+
if (journal && now === journalSeen) return journal;
|
|
352
|
+
journalSeen = now;
|
|
353
|
+
journal = [];
|
|
354
|
+
try {
|
|
355
|
+
for (const line of readFileSync(journalFile, 'utf8').split('\n')) {
|
|
356
|
+
if (!line) continue;
|
|
357
|
+
try {
|
|
358
|
+
journal.push(JSON.parse(line));
|
|
359
|
+
} catch {
|
|
360
|
+
// A torn line.
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
} catch {
|
|
364
|
+
// None yet.
|
|
365
|
+
}
|
|
366
|
+
return journal;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return {
|
|
370
|
+
dir,
|
|
371
|
+
onChange(fn) {
|
|
372
|
+
listeners.add(fn);
|
|
373
|
+
return () => listeners.delete(fn);
|
|
374
|
+
},
|
|
375
|
+
/** Notes of a bot (its id) or of the team ('team'). */
|
|
376
|
+
notes(scope) {
|
|
377
|
+
reload();
|
|
378
|
+
return listOf(scope).map((n) => ({ ...n }));
|
|
379
|
+
},
|
|
380
|
+
/** Apply edits; returns {added, updated, removed}. */
|
|
381
|
+
edit(scope, ops, options = {}) {
|
|
382
|
+
reload();
|
|
383
|
+
const r = applyOps(listOf(scope), normalizeOps(ops), { prefix: scope === 'team' ? 't' : 'n', cap: scope === 'team' ? TEAM_CAP : BOT_CAP, ...options });
|
|
384
|
+
setList(scope, r.notes);
|
|
385
|
+
if (r.added + r.updated + r.removed > 0 || ops.length) save();
|
|
386
|
+
return { added: r.added, updated: r.updated, removed: r.removed, touched: r.touched };
|
|
387
|
+
},
|
|
388
|
+
/** One note as the user wrote it in the panel (add when no id), or its pin. */
|
|
389
|
+
put(scope, note) {
|
|
390
|
+
reload();
|
|
391
|
+
const list = listOf(scope);
|
|
392
|
+
const text = clean(note.text);
|
|
393
|
+
if (looksSecret(text)) throw new Error('that looks like a secret; memory never stores keys, passwords or tokens');
|
|
394
|
+
const existing = note.id ? list.find((n) => n.id === note.id) : null;
|
|
395
|
+
if (note.id && !existing) throw new Error('no such note');
|
|
396
|
+
if (existing) {
|
|
397
|
+
if (text) existing.text = text;
|
|
398
|
+
if (KINDS.includes(note.kind)) existing.kind = note.kind;
|
|
399
|
+
if (typeof note.pinned === 'boolean') {
|
|
400
|
+
if (note.pinned) existing.pinned = true;
|
|
401
|
+
else delete existing.pinned;
|
|
402
|
+
}
|
|
403
|
+
existing.updated = Date.now();
|
|
404
|
+
save();
|
|
405
|
+
return { ...existing };
|
|
406
|
+
}
|
|
407
|
+
if (!text) throw new Error('write the note first');
|
|
408
|
+
const n = normalizeNote({ text, kind: note.kind, pinned: note.pinned === true, source: note.source ?? 'user' }, scope === 'team' ? 't' : 'n');
|
|
409
|
+
const dup = list.find((x) => same(x.text, n.text));
|
|
410
|
+
if (dup) return { ...dup };
|
|
411
|
+
list.push(n);
|
|
412
|
+
if (list.length > (scope === 'team' ? TEAM_CAP : BOT_CAP)) setList(scope, applyOps(list, [], { cap: scope === 'team' ? TEAM_CAP : BOT_CAP }).notes);
|
|
413
|
+
save();
|
|
414
|
+
return { ...n };
|
|
415
|
+
},
|
|
416
|
+
remove(scope, id) {
|
|
417
|
+
reload();
|
|
418
|
+
const list = listOf(scope);
|
|
419
|
+
const next = list.filter((n) => n.id !== id);
|
|
420
|
+
if (next.length === list.length) return false;
|
|
421
|
+
setList(scope, next);
|
|
422
|
+
save();
|
|
423
|
+
return true;
|
|
424
|
+
},
|
|
425
|
+
/** Forget everything a removed bot kept (its journal lines stay for the team digest). */
|
|
426
|
+
dropBot(botId) {
|
|
427
|
+
reload();
|
|
428
|
+
if (!state.bots[botId]) return;
|
|
429
|
+
delete state.bots[botId];
|
|
430
|
+
save();
|
|
431
|
+
},
|
|
432
|
+
addJournal(entry) {
|
|
433
|
+
const row = { id: newId('j'), at: Date.now(), ...entry, summary: clean(entry.summary).slice(0, 400), title: clean(entry.title).slice(0, 200) };
|
|
434
|
+
const all = readJournal();
|
|
435
|
+
all.push(row);
|
|
436
|
+
try {
|
|
437
|
+
mkdirSync(dir, { recursive: true });
|
|
438
|
+
if (all.length > KEEP_JOURNAL * 1.2) {
|
|
439
|
+
journal = all.slice(-KEEP_JOURNAL);
|
|
440
|
+
writeFileSync(journalFile, journal.map((x) => JSON.stringify(x)).join('\n') + '\n', 'utf8');
|
|
441
|
+
} else appendFileSync(journalFile, JSON.stringify(row) + '\n', 'utf8');
|
|
442
|
+
journalSeen = mtime(journalFile);
|
|
443
|
+
} catch {
|
|
444
|
+
// A lost line.
|
|
445
|
+
}
|
|
446
|
+
for (const fn of listeners) {
|
|
447
|
+
try {
|
|
448
|
+
fn();
|
|
449
|
+
} catch {
|
|
450
|
+
// Ignored.
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return row;
|
|
454
|
+
},
|
|
455
|
+
/** Journal lines, oldest first: of one bot (`bot`), since a time, at most `limit`. */
|
|
456
|
+
journal({ bot = '', since = 0, until = Infinity, limit = 200 } = {}) {
|
|
457
|
+
const rows = readJournal().filter((j) => (!bot || j.bot === bot) && j.at >= since && j.at < until);
|
|
458
|
+
return rows.slice(-Math.max(1, limit));
|
|
459
|
+
},
|
|
460
|
+
digests() {
|
|
461
|
+
const d = readJson(digestsFile, []);
|
|
462
|
+
return Array.isArray(d) ? d : [];
|
|
463
|
+
},
|
|
464
|
+
addDigest(digest) {
|
|
465
|
+
const all = this.digests().filter((d) => d.date !== digest.date);
|
|
466
|
+
all.push({ date: digest.date, text: String(digest.text ?? '').trim().slice(0, 4000), at: Date.now() });
|
|
467
|
+
all.sort((a, b) => a.date.localeCompare(b.date));
|
|
468
|
+
writeJson(digestsFile, all.slice(-KEEP_DIGESTS));
|
|
469
|
+
return all[all.length - 1];
|
|
470
|
+
},
|
|
471
|
+
dreamState() {
|
|
472
|
+
return readJson(dreamFile, {});
|
|
473
|
+
},
|
|
474
|
+
setDreamState(v) {
|
|
475
|
+
writeJson(dreamFile, v);
|
|
476
|
+
},
|
|
477
|
+
/** Notes and journal lines that contain every word of `query` (case-insensitive), newest first. */
|
|
478
|
+
search(query, { bot = '', limit = 20 } = {}) {
|
|
479
|
+
reload();
|
|
480
|
+
const words = String(query ?? '')
|
|
481
|
+
.toLowerCase()
|
|
482
|
+
.split(/\s+/)
|
|
483
|
+
.filter(Boolean);
|
|
484
|
+
const hit = (text) => {
|
|
485
|
+
const s = String(text ?? '').toLowerCase();
|
|
486
|
+
return words.length > 0 && words.every((w) => s.includes(w));
|
|
487
|
+
};
|
|
488
|
+
const notes = [...(bot ? (state.bots[bot] ?? []).map((n) => ({ scope: 'self', ...n })) : []), ...state.team.map((n) => ({ scope: 'team', ...n }))].filter((n) => hit(n.text));
|
|
489
|
+
const lines = readJournal()
|
|
490
|
+
.filter((j) => hit(`${j.title} ${j.summary}`))
|
|
491
|
+
.slice(-limit)
|
|
492
|
+
.reverse();
|
|
493
|
+
const digests = this.digests()
|
|
494
|
+
.filter((d) => hit(d.text))
|
|
495
|
+
.slice(-5)
|
|
496
|
+
.reverse();
|
|
497
|
+
return { notes: notes.slice(0, limit), journal: lines, digests };
|
|
498
|
+
},
|
|
499
|
+
/** The prompt section of `bot`. */
|
|
500
|
+
promptFor(bot, { tz = 0 } = {}) {
|
|
501
|
+
reload();
|
|
502
|
+
const digests = this.digests();
|
|
503
|
+
return memoryText({ bot, notes: state.bots[bot.id] ?? [], team: state.team, journal: this.journal({ bot: bot.id, limit: PROMPT_BUDGET.journal }), digest: digests[digests.length - 1] ?? null, tz });
|
|
504
|
+
},
|
|
505
|
+
/** Local date (YYYY-MM-DD) in zone offset `tz` minutes. */
|
|
506
|
+
dayOf: (ms, tz = 0) => day(ms, tz),
|
|
507
|
+
};
|
|
508
|
+
}
|