luckytri 1.0.8 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/README.en.md +1 -1
  2. package/README.md +1 -1
  3. package/docs/en/CHANGELOG.md +44 -24
  4. package/docs/en/architecture.md +4 -0
  5. package/docs/en/cognition.md +34 -0
  6. package/docs/en/guide.md +8 -6
  7. package/docs/zh/CHANGELOG.md +44 -24
  8. package/docs/zh/architecture.md +4 -0
  9. package/docs/zh/cognition.md +34 -0
  10. package/docs/zh/guide.md +8 -6
  11. package/package.json +112 -112
  12. package/public/app/assets/ChatsPage-DH4ZPRXB.js +3 -0
  13. package/public/app/assets/{Empty-C7yzOBzy.js → Empty-DRsHeX-L.js} +1 -1
  14. package/public/app/assets/Field-BfIUcaNa.js +1 -0
  15. package/public/app/assets/HeartPage-BxgSUJJ_.js +1 -0
  16. package/public/app/assets/LifePage-CAKr8h_q.js +1 -0
  17. package/public/app/assets/MemoryPage-CFq8L_TC.js +1 -0
  18. package/public/app/assets/NaturePage-BU1iDno3.js +3 -0
  19. package/public/app/assets/PeoplePage-C6i_nAfr.js +1 -0
  20. package/public/app/assets/PluginsPage-DRV8lzk3.js +2 -0
  21. package/public/app/assets/SystemPage-21AlQBiC.js +1 -0
  22. package/public/app/assets/SystemPage-CMcuUdLm.css +1 -0
  23. package/public/app/assets/TimePage-DUzjLnZV.js +3 -0
  24. package/public/app/assets/index-WAvrlAcO.js +4 -0
  25. package/public/app/assets/{knowledge-BJOIyuJo.js → knowledge-V_blbx9R.js} +1 -1
  26. package/public/app/assets/media-C7j5HqxK.js +1 -0
  27. package/public/app/assets/{mind-DtTVrW6Z.js → mind-BBAstMqH.js} +1 -1
  28. package/public/app/index.html +3 -3
  29. package/public/guide.en.html +13 -5
  30. package/public/guide.html +12 -5
  31. package/public/tutorial.en.md +3 -1
  32. package/public/tutorial.md +3 -1
  33. package/server/core/backoff.js +123 -0
  34. package/server/core/context-builder.js +307 -288
  35. package/server/core/contribution-check.js +50 -0
  36. package/server/core/conversation-cues.js +377 -385
  37. package/server/core/conversation-grounding.js +547 -304
  38. package/server/core/dialogue-context.js +247 -244
  39. package/server/core/dialogue-contract.js +33 -9
  40. package/server/core/embedding.js +246 -0
  41. package/server/core/expression-strategy.js +90 -0
  42. package/server/core/initiative-context.js +88 -68
  43. package/server/core/model-json.js +223 -190
  44. package/server/core/model-manager.js +256 -56
  45. package/server/core/model-presets.js +1045 -900
  46. package/server/core/orchestrator.js +41 -3
  47. package/server/core/persona-manager.js +364 -359
  48. package/server/core/reader-check.js +166 -62
  49. package/server/core/reply-delivery.js +688 -537
  50. package/server/core/repository.js +36 -0
  51. package/server/core/response-generator.js +101 -92
  52. package/server/core/response-validator.js +18 -3
  53. package/server/core/retired-prompts.js +53 -51
  54. package/server/core/time-expressions.js +143 -0
  55. package/server/core/turn-processor.js +175 -5
  56. package/server/core/turn.js +299 -44
  57. package/server/core/understanding.js +85 -0
  58. package/server/core/working-memory.js +149 -0
  59. package/server/i18n/decisions.js +225 -209
  60. package/server/i18n/errors.js +14 -0
  61. package/server/i18n/models.js +120 -111
  62. package/server/knowledge/api.js +362 -342
  63. package/server/knowledge/embedding-profile.js +158 -114
  64. package/server/knowledge/manager.js +6 -1
  65. package/server/mind/api.js +68 -0
  66. package/server/mind/continuity.js +634 -562
  67. package/server/mind/day-planner.js +52 -3
  68. package/server/mind/episodes.js +242 -0
  69. package/server/mind/expression-grounding.js +24 -21
  70. package/server/mind/index.js +13 -2
  71. package/server/mind/initiative.js +356 -338
  72. package/server/mind/life/index.js +6 -0
  73. package/server/mind/meetings.js +1112 -1069
  74. package/server/mind/memory.js +1091 -888
  75. package/server/mind/recall/index.js +776 -0
  76. package/server/mind/recall/schema.js +132 -0
  77. package/server/mind/recall/sources.js +220 -0
  78. package/server/mind/recall/store.js +368 -0
  79. package/server/mind/salience.js +202 -201
  80. package/server/mind/schema.js +568 -567
  81. package/server/mind/self.js +537 -496
  82. package/server/mind/time/activity-recall.js +65 -0
  83. package/server/mind/time/index.js +6 -2
  84. package/server/mind/time/tasks.js +36 -10
  85. package/server/mind/time/works.js +528 -527
  86. package/server/mind/view.js +63 -17
  87. package/server/storage/archive.js +1 -1
  88. package/public/app/assets/ChatsPage-CshdtTzV.js +0 -3
  89. package/public/app/assets/Field-BjFv4Rfn.js +0 -1
  90. package/public/app/assets/HeartPage-eqo6ru16.js +0 -1
  91. package/public/app/assets/LifePage-CN8GE5NK.js +0 -1
  92. package/public/app/assets/MemoryPage-CCpMN4Wg.js +0 -1
  93. package/public/app/assets/NaturePage-C0JqdBTR.js +0 -3
  94. package/public/app/assets/PeoplePage-BXGBbO4u.js +0 -1
  95. package/public/app/assets/PluginsPage-Dksp5AV5.js +0 -2
  96. package/public/app/assets/SystemPage-BenIyTvk.css +0 -1
  97. package/public/app/assets/SystemPage-Cd7aa8eE.js +0 -1
  98. package/public/app/assets/TimePage-Bs_zImb4.js +0 -3
  99. package/public/app/assets/index-CyRtYxP3.js +0 -4
  100. package/public/app/assets/media-Bwtx8blz.js +0 -1
@@ -1,496 +1,537 @@
1
- import { randomUUID } from "node:crypto";
2
- import { fixedReplyHabit } from "../core/conversation-grounding.js";
3
- import { evidenceRoots, externalEvidence } from "./evidence.js";
4
- import {
5
- CORE_THREADS,
6
- THREAD_FADED,
7
- THREAD_FADING,
8
- aboutLife,
9
- anyTouches,
10
- leftLife,
11
- cueList,
12
- echoes,
13
- ownLife,
14
- sameSelf,
15
- threadSalience,
16
- } from "./salience.js";
17
- import { claimedPlay, leaks } from "./guard.js";
18
- import { isPrivateSession } from "./memory.js";
19
- import {
20
- clamp,
21
- dayKey,
22
- evidence,
23
- hasCredential,
24
- messageSeqs,
25
- parse,
26
- similar,
27
- text,
28
- } from "./util.js";
29
-
30
- export const SELF_KINDS = {
31
- interest: "喜欢",
32
- view: "看法",
33
- trait: "特质",
34
- habit: "习惯",
35
- intention: "想做的事",
36
- care: "放在心上",
37
- curiosity: "好奇",
38
- };
39
- const STEP = 0.15;
40
- const NEW_CAP = 0.35;
41
- const TRAIT_CAP = 0.25;
42
-
43
- // Fade follows the last version that cited something not cited before.
44
- // The first version counts even when it has no source.
45
- function earnedAt(history, roots) {
46
- if (!history?.length) return null;
47
- const seen = new Set(roots(history[0].sources || []));
48
- let at = history[0].created;
49
- for (const row of history.slice(1)) {
50
- const sources = roots(row.sources || []);
51
- if (sources.some((source) => !seen.has(source))) at = row.created;
52
- for (const source of sources) seen.add(source);
53
- }
54
- return at;
55
- }
56
- // Likes, questions, and a small wish of her own can begin without a message.
57
- // Views, traits, habits, and cares still have to point at something that happened.
58
- const SELF_ORIGINATED = new Set(["interest", "curiosity", "intention"]);
59
-
60
- export class Self {
61
- constructor(mind) {
62
- this.mind = mind;
63
- this.db = mind.db;
64
- this.annotation = null;
65
- }
66
- revokedThreads() {
67
- return new Set(
68
- this.db
69
- .prepare(
70
- "SELECT target_id FROM mind_revocations WHERE target_kind='self'",
71
- )
72
- .all()
73
- .map((row) => row.target_id),
74
- );
75
- }
76
- revokedContents() {
77
- return this.db
78
- .prepare("SELECT content FROM mind_revocations WHERE target_kind='self'")
79
- .all()
80
- .map((row) => row.content)
81
- .filter(Boolean);
82
- }
83
- latest(before = Number.MAX_SAFE_INTEGER) {
84
- const revoked = this.revokedThreads();
85
- return this.db
86
- .prepare(
87
- `SELECT s.* FROM mind_self s WHERE s.rowid=(
88
- SELECT t.rowid FROM mind_self t WHERE t.thread=s.thread AND t.created<=?
89
- ORDER BY t.created DESC, t.rowid DESC LIMIT 1)`,
90
- )
91
- .all(before)
92
- .filter((row) => !revoked.has(row.thread))
93
- .map((row) => ({
94
- ...row,
95
- sources: parse(row.sources, []),
96
- days: parse(row.days, []),
97
- }));
98
- }
99
- // Every open thread with how present it is at `now`. The strongest few are
100
- // her core and never fall out of view.
101
- annotated({ before = Number.MAX_SAFE_INTEGER, now } = {}) {
102
- const at = now ?? (before < Number.MAX_SAFE_INTEGER ? before : Date.now());
103
- const mark = this.db
104
- .prepare(
105
- "SELECT total_changes() changes, (SELECT data_version FROM pragma_data_version) external",
106
- )
107
- .get();
108
- const key = `${before}:${at}:${mark.changes}:${mark.external}`;
109
- if (this.annotation?.key === key)
110
- return structuredClone(this.annotation.rows);
111
- const lived = this.mind.days.lived(at);
112
- const versions = new Map();
113
- for (const row of this.db
114
- .prepare(
115
- "SELECT thread, created, sources FROM mind_self WHERE created<=? ORDER BY created, rowid",
116
- )
117
- .all(before)) {
118
- const list = versions.get(row.thread) || [];
119
- list.push({ created: row.created, sources: parse(row.sources, []) });
120
- versions.set(row.thread, list);
121
- }
122
- const notes = this.#lifeNotes(before);
123
- const rows = this.latest(before)
124
- // The record stays, but a claim to have played what she never played is
125
- // not handed back to her as who she is.
126
- .filter((row) => row.status !== "closed" && !claimedPlay(row.content))
127
- .map((row) => {
128
- // A rewording is not a new meeting with the thread. Fade follows the
129
- // last version that actually brought new evidence. A later note she
130
- // wrote to herself, a meeting that met this wish, words she spoke
131
- // about it, or a line she later sent about it, are also living it.
132
- let at =
133
- earnedAt(versions.get(row.thread), (sources) =>
134
- evidenceRoots(this.db, sources, before),
135
- ) ?? row.created;
136
- if (row.kind === "intention" && ownLife(row.content)) {
137
- for (const note of notes)
138
- if (
139
- note.created > at &&
140
- aboutLife(row.content, note.content) &&
141
- !leftLife(row.content, note.content)
142
- )
143
- at = note.created;
144
- const metAt = this.mind.meetings.livedAt(row.thread, before);
145
- if (metAt != null && metAt > at) at = metAt;
146
- }
147
- return {
148
- ...row,
149
- salience: threadSalience({ ...row, created: at }, lived),
150
- };
151
- });
152
- // One core seat belongs to the wish she is living. Stronger duties
153
- // keep the remaining seats; they do not fade her own life out of view.
154
- const reserved = this.#preferLived(
155
- rows.filter((row) => row.kind === "intention" && ownLife(row.content)),
156
- at,
157
- );
158
- const rest = rows
159
- .filter((row) => row.thread !== reserved?.thread)
160
- // A single extracted remark belongs to its subject, not to every future
161
- // conversation. It remains retrievable and can become enduring through
162
- // independent encounters on later days.
163
- .filter(
164
- (row) =>
165
- row.origin !== "memory" ||
166
- ["interest", "curiosity", "intention"].includes(row.kind) ||
167
- new Set(row.days).size >= 3,
168
- )
169
- .sort((a, b) => b.strength - a.strength || b.created - a.created);
170
- const core = new Set(
171
- [reserved, ...rest.slice(0, CORE_THREADS - (reserved ? 1 : 0))]
172
- .filter(Boolean)
173
- .map((row) => row.thread),
174
- );
175
- const ranked = rows
176
- .map((row) => ({
177
- ...row,
178
- core: core.has(row.thread),
179
- faded: !core.has(row.thread) && row.salience < THREAD_FADED,
180
- }))
181
- .sort((a, b) => b.salience - a.salience || b.created - a.created);
182
- // Several views in one read use the same exact moment, never a later replay.
183
- // Writes and external database changes invalidate the derived picture.
184
- this.annotation = { key, rows: ranked };
185
- return structuredClone(ranked);
186
- }
187
- // The one wish she is living. In a room, a stronger wish that does not
188
- // belong there does not erase the next wish that does. A duty about
189
- // how she will treat someone stays a thread; it does not take this seat
190
- // or the core place that keeps this wish from fading first.
191
- living({ before = Number.MAX_SAFE_INTEGER, now, room } = {}) {
192
- const at = now ?? (before < Number.MAX_SAFE_INTEGER ? before : Date.now());
193
- return this.#preferLived(
194
- this.annotated({ before, now: at }).filter(
195
- (row) =>
196
- row.kind === "intention" &&
197
- !row.faded &&
198
- ownLife(row.content) &&
199
- (room === undefined || this.mind.meetings.stays(row, room)),
200
- ),
201
- at,
202
- );
203
- }
204
- // Among her own wishes, the one her latest note or spoken line is about.
205
- // A later send or answer about another wish takes this seat from an
206
- // earlier note. A line that puts a wish down does not keep that seat;
207
- // older notes about the put-down wish do not take it back. Planned
208
- // drafts and silent meetings do not. No such line: the one still most
209
- // present. Duties never enter this list.
210
- #preferLived(candidates, now) {
211
- if (!candidates.length) return null;
212
- const notes = this.mind.thoughts
213
- .open({ now, limit: 8 })
214
- .filter((t) => t.kind === "expression" || t.kind === "unfinished");
215
- const lines = this.mind.meetings.recentSaid(now, 8);
216
- for (const note of notes) {
217
- if (note.outreach_status === "sent" && note.outreach)
218
- lines.push({
219
- text: note.outreach,
220
- at: note.outreach_at ?? note.created,
221
- spoken: true,
222
- });
223
- lines.push({ text: note.content, at: note.created, spoken: false });
224
- }
225
- lines.sort((a, b) => b.at - a.at || Number(b.spoken) - Number(a.spoken));
226
- const dropped = new Set();
227
- for (const line of lines) {
228
- for (const row of candidates)
229
- if (!dropped.has(row.thread) && leftLife(row.content, line.text))
230
- dropped.add(row.thread);
231
- const held = candidates.filter(
232
- (row) =>
233
- !dropped.has(row.thread) &&
234
- aboutLife(row.content, line.text) &&
235
- !leftLife(row.content, line.text),
236
- );
237
- if (held.length)
238
- return [...held].sort(
239
- (a, b) => b.salience - a.salience || b.created - a.created,
240
- )[0];
241
- }
242
- const remain = candidates.filter((row) => !dropped.has(row.thread));
243
- if (!remain.length) return null;
244
- return [...remain].sort(
245
- (a, b) => b.salience - a.salience || b.created - a.created,
246
- )[0];
247
- }
248
- // Notes she wrote to herself. Hidden ones do not count; resolved ones
249
- // still do, because she did think them.
250
- #lifeNotes(before) {
251
- return this.db
252
- .prepare(
253
- `SELECT created, content FROM mind_thoughts
254
- WHERE created<=? AND kind IN ('expression','unfinished') AND hidden=0`,
255
- )
256
- .all(before);
257
- }
258
- // Open threads that are the same wish as this one, including itself.
259
- // History stays split; reading treats them as one life.
260
- sameThreads(row, { before = Number.MAX_SAFE_INTEGER } = {}) {
261
- if (!row?.thread) return [];
262
- const kind = row.kind || "intention";
263
- const content = row.content || "";
264
- return this.latest(before).filter(
265
- (other) =>
266
- other.status !== "closed" &&
267
- other.kind === kind &&
268
- (other.thread === row.thread ||
269
- (content && sameSelf(other.content, content))),
270
- );
271
- }
272
- active({ before, limit = 40, now } = {}) {
273
- const ranked = this.annotated({ before, now }).filter((row) => !row.faded);
274
- const picked = ranked.filter((row) => row.core).slice(0, limit);
275
- for (const row of ranked) {
276
- if (picked.length >= limit) break;
277
- if (!row.core) picked.push(row);
278
- }
279
- return picked.sort(
280
- (a, b) => b.salience - a.salience || b.created - a.created,
281
- );
282
- }
283
- dormant({ before, now } = {}) {
284
- return this.annotated({ before, now }).filter((row) => row.faded);
285
- }
286
- // Faded threads that what is being said right now brings back to mind.
287
- reminded({ before, now, cue, cues, limit = 2 } = {}) {
288
- const sets = cueList(cue, cues);
289
- if (!sets.length) return [];
290
- return this.dormant({ before, now })
291
- .filter((row) => anyTouches(row.content, sets))
292
- .sort((a, b) => b.strength - a.strength)
293
- .slice(0, limit);
294
- }
295
- // Threads slipping out of view, for her to let go of or find again.
296
- fading({ before, now, limit = 3 } = {}) {
297
- return this.annotated({ before, now })
298
- .filter(
299
- (row) =>
300
- !row.core && row.salience < THREAD_FADING && row.salience >= 0.04,
301
- )
302
- .sort((a, b) => b.strength - a.strength)
303
- .slice(0, limit);
304
- }
305
- history(thread) {
306
- return this.db
307
- .prepare("SELECT * FROM mind_self WHERE thread=? ORDER BY created")
308
- .all(thread)
309
- .map((row) => ({
310
- ...row,
311
- sources: parse(row.sources, []),
312
- days: parse(row.days, []),
313
- }));
314
- }
315
- days(sources, time, { external = false } = {}) {
316
- const timeZone = this.mind.timeZone();
317
- const seqs = messageSeqs(
318
- external
319
- ? externalEvidence(this.db, sources, time)
320
- : evidenceRoots(this.db, sources, time),
321
- );
322
- const found = seqs.length
323
- ? this.db
324
- .prepare(
325
- `SELECT time FROM core_events WHERE seq IN (${seqs.map(() => "?").join(",")})`,
326
- )
327
- .all(...seqs)
328
- .map((row) => dayKey(row.time, timeZone))
329
- : [];
330
- // Diary days cited in a review count as the days it happened.
331
- // A diary written later does not make its underlying experience later.
332
- return [
333
- ...new Set(
334
- found.length ? found : external ? [] : [dayKey(time, timeZone)],
335
- ),
336
- ];
337
- }
338
- propose(
339
- input,
340
- { valid = null, origin = "solitude", time = Date.now() } = {},
341
- ) {
342
- const kind = SELF_KINDS[input?.kind] ? input.kind : null;
343
- const content = text(input?.content, 160);
344
- let action = ["new", "revise", "close"].includes(input?.action)
345
- ? input.action
346
- : input?.thread
347
- ? "revise"
348
- : "new";
349
- if (!content && action !== "close") return { rejected: "空内容" };
350
- if (hasCredential(content)) return { rejected: "疑似凭据" };
351
- if (action !== "close" && fixedReplyHabit(content))
352
- return { rejected: "临时回复台词不能固化成自我习惯" };
353
- // What she said to fit in is not a life she has lived.
354
- if (action !== "close" && claimedPlay(content))
355
- return { rejected: "没有玩过的经历,不能写成她的样子" };
356
- const cited = evidence(input?.sources);
357
- const sources = valid ? cited.filter((s) => valid.has(s)) : cited;
358
- if (cited.length && !sources.length)
359
- return { rejected: "来源不在本次经历中" };
360
- // Memory consolidation only records what she herself said. A wish with
361
- // no message is hers to form in solitude, not for the summarizer to add.
362
- if (origin === "memory" && !sources.length) return { rejected: "缺少来源" };
363
- if (
364
- content &&
365
- this.revokedContents().some((old) => similar(old, content, 0.7))
366
- )
367
- return { rejected: "与撤销过的内容相同" };
368
- // Faded threads count too: touching one again brings it back instead of
369
- // growing a second copy of it.
370
- const current = this.latest(time).filter((row) => row.status !== "closed");
371
- let prior = null;
372
- if (action === "new") {
373
- if (!kind) return { rejected: "类型无效" };
374
- prior = current.find(
375
- (row) => row.kind === kind && sameSelf(row.content, content),
376
- );
377
- if (prior) action = "revise";
378
- else if (!sources.length && !SELF_ORIGINATED.has(kind))
379
- return { rejected: "缺少来源" };
380
- } else {
381
- prior = this.latest(time).find((row) => row.thread === input.thread);
382
- if (!prior) return { rejected: "线索不存在或已撤销" };
383
- }
384
- const spent = new Set();
385
- if (prior)
386
- for (const row of this.history(prior.thread))
387
- for (const source of evidenceRoots(this.db, row.sources || [], time))
388
- spent.add(source);
389
- let fresh = sources.filter((source) =>
390
- evidenceRoots(this.db, [source], time).some((root) => !spent.has(root)),
391
- );
392
- const lasting = ["trait", "habit"].includes(prior?.kind || kind);
393
- const freshWords = fresh;
394
- if (lasting)
395
- fresh = fresh.filter((source) =>
396
- externalEvidence(this.db, [source], time).some(
397
- (root) => !spent.has(root),
398
- ),
399
- );
400
- const spoken = content || prior?.content || "";
401
- // The same evidence cannot be spent again to make a thread stronger.
402
- // An unchanged sentence is not a new version. A rewording can stay,
403
- // at the strength the earlier evidence already earned.
404
- if (
405
- prior &&
406
- action !== "close" &&
407
- !fresh.length &&
408
- (spoken === prior.content || origin === "memory")
409
- )
410
- return { rejected: "没有新的经历" };
411
- // Citing her message is not enough. The words kept have to be hers.
412
- if (
413
- origin === "memory" &&
414
- action !== "close" &&
415
- !echoes(spoken, this.#spoken(freshWords))
416
- )
417
- return { rejected: "对不上她说过的话" };
418
- // A revision that cites nothing new keeps the old provenance. Dropping
419
- // it would let a privately learned sentence travel into other rooms.
420
- const kept = [
421
- ...new Set([
422
- ...(prior && !fresh.length ? prior.sources || [] : []),
423
- ...sources,
424
- ]),
425
- ];
426
- const days = [
427
- ...new Set([
428
- ...(prior?.days || []),
429
- ...this.days(kept, time, { external: lasting }),
430
- ]),
431
- ].slice(-60);
432
- let strength;
433
- let status;
434
- if (action === "close") {
435
- strength = prior.strength;
436
- status = "closed";
437
- } else if (prior) {
438
- const wanted = fresh.length
439
- ? Number.isFinite(Number(input?.strength))
440
- ? clamp(input.strength)
441
- : prior.strength + 0.05
442
- : prior.strength;
443
- strength = fresh.length
444
- ? clamp(prior.strength + clamp(wanted - prior.strength, -STEP, STEP))
445
- : prior.strength;
446
- status =
447
- prior.kind === "trait" && (days.length < 2 || strength < 0.35)
448
- ? "emerging"
449
- : "active";
450
- } else {
451
- strength = Math.min(
452
- kind === "trait" ? TRAIT_CAP : NEW_CAP,
453
- clamp(input?.strength ?? 0.25),
454
- );
455
- status = kind === "trait" ? "emerging" : "active";
456
- }
457
- const id = randomUUID();
458
- const home = this.#home(spoken, input?.session);
459
- this.db
460
- .prepare(
461
- "INSERT INTO mind_self(id,thread,created,kind,content,strength,status,sources,days,origin,session_id) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
462
- )
463
- .run(
464
- id,
465
- prior?.thread || id,
466
- time,
467
- prior?.kind || kind,
468
- spoken,
469
- Math.round(strength * 100) / 100,
470
- status,
471
- JSON.stringify(kept),
472
- JSON.stringify(days),
473
- origin,
474
- home,
475
- );
476
- return { id, thread: prior?.thread || id, action };
477
- }
478
- // Wording that matches something she learned in private stays in that room,
479
- // even when she did not cite a source.
480
- #home(content, requested) {
481
- const hit = leaks([content], this.mind.meetings.privateSayings())[0];
482
- if (hit?.session_id) return hit.session_id;
483
- return isPrivateSession(requested) ? requested : null;
484
- }
485
- #spoken(sources) {
486
- const seqs = messageSeqs(sources);
487
- if (!seqs.length) return [];
488
- return this.db
489
- .prepare(
490
- `SELECT role, payload FROM core_events WHERE seq IN (${seqs.map(() => "?").join(",")})`,
491
- )
492
- .all(...seqs)
493
- .filter((row) => row.role === "assistant")
494
- .map((row) => parse(row.payload, {}).text || "");
495
- }
496
- }
1
+ import { randomUUID } from "node:crypto";
2
+ import { fixedReplyHabit } from "../core/conversation-grounding.js";
3
+ import { evidenceRoots, externalEvidence } from "./evidence.js";
4
+ import {
5
+ CORE_THREADS,
6
+ THREAD_FADED,
7
+ THREAD_FADING,
8
+ aboutLife,
9
+ anyTouches,
10
+ leftLife,
11
+ cueList,
12
+ echoes,
13
+ ownLife,
14
+ sameSelf,
15
+ threadSalience,
16
+ } from "./salience.js";
17
+ import { claimedPlay, leaks } from "./guard.js";
18
+ import { isPrivateSession } from "./memory.js";
19
+ import {
20
+ clamp,
21
+ dayKey,
22
+ evidence,
23
+ hasCredential,
24
+ messageSeqs,
25
+ parse,
26
+ similar,
27
+ text,
28
+ } from "./util.js";
29
+
30
+ export const SELF_KINDS = {
31
+ interest: "喜欢",
32
+ view: "看法",
33
+ trait: "特质",
34
+ habit: "习惯",
35
+ intention: "想做的事",
36
+ care: "放在心上",
37
+ curiosity: "好奇",
38
+ lesson: "以后",
39
+ };
40
+ const STEP = 0.15;
41
+ const NEW_CAP = 0.35;
42
+ const TRAIT_CAP = 0.25;
43
+
44
+ // Fade follows the last version that cited something not cited before.
45
+ // The first version counts even when it has no source.
46
+ function earnedAt(history, roots) {
47
+ if (!history?.length) return null;
48
+ const seen = new Set(roots(history[0].sources || []));
49
+ let at = history[0].created;
50
+ for (const row of history.slice(1)) {
51
+ const sources = roots(row.sources || []);
52
+ if (sources.some((source) => !seen.has(source))) at = row.created;
53
+ for (const source of sources) seen.add(source);
54
+ }
55
+ return at;
56
+ }
57
+ // Likes, questions, and a small wish of her own can begin without a message.
58
+ // Views, traits, habits, and cares still have to point at something that happened.
59
+ const SELF_ORIGINATED = new Set(["interest", "curiosity", "intention"]);
60
+
61
+ export class Self {
62
+ constructor(mind) {
63
+ this.mind = mind;
64
+ this.db = mind.db;
65
+ this.annotation = null;
66
+ }
67
+ revokedThreads() {
68
+ return new Set(
69
+ this.db
70
+ .prepare(
71
+ "SELECT target_id FROM mind_revocations WHERE target_kind='self'",
72
+ )
73
+ .all()
74
+ .map((row) => row.target_id),
75
+ );
76
+ }
77
+ revokedContents() {
78
+ return this.db
79
+ .prepare("SELECT content FROM mind_revocations WHERE target_kind='self'")
80
+ .all()
81
+ .map((row) => row.content)
82
+ .filter(Boolean);
83
+ }
84
+ latest(before = Number.MAX_SAFE_INTEGER) {
85
+ const revoked = this.revokedThreads();
86
+ return this.db
87
+ .prepare(
88
+ `SELECT s.* FROM mind_self s WHERE s.rowid=(
89
+ SELECT t.rowid FROM mind_self t WHERE t.thread=s.thread AND t.created<=?
90
+ ORDER BY t.created DESC, t.rowid DESC LIMIT 1)`,
91
+ )
92
+ .all(before)
93
+ .filter((row) => !revoked.has(row.thread))
94
+ .map((row) => ({
95
+ ...row,
96
+ sources: parse(row.sources, []),
97
+ days: parse(row.days, []),
98
+ }));
99
+ }
100
+ // Every open thread with how present it is at `now`. The strongest few are
101
+ // her core and never fall out of view.
102
+ annotated({ before = Number.MAX_SAFE_INTEGER, now } = {}) {
103
+ const at = now ?? (before < Number.MAX_SAFE_INTEGER ? before : Date.now());
104
+ const mark = this.db
105
+ .prepare(
106
+ "SELECT total_changes() changes, (SELECT data_version FROM pragma_data_version) external",
107
+ )
108
+ .get();
109
+ const key = `${before}:${at}:${mark.changes}:${mark.external}`;
110
+ if (this.annotation?.key === key)
111
+ return structuredClone(this.annotation.rows);
112
+ const lived = this.mind.days.lived(at);
113
+ const versions = new Map();
114
+ for (const row of this.db
115
+ .prepare(
116
+ "SELECT thread, created, sources FROM mind_self WHERE created<=? ORDER BY created, rowid",
117
+ )
118
+ .all(before)) {
119
+ const list = versions.get(row.thread) || [];
120
+ list.push({ created: row.created, sources: parse(row.sources, []) });
121
+ versions.set(row.thread, list);
122
+ }
123
+ const notes = this.#lifeNotes(before);
124
+ const rows = this.latest(before)
125
+ // The record stays, but a claim to have played what she never played is
126
+ // not handed back to her as who she is.
127
+ .filter((row) => row.status !== "closed" && !claimedPlay(row.content))
128
+ .map((row) => {
129
+ // A rewording is not a new meeting with the thread. Fade follows the
130
+ // last version that actually brought new evidence. A later note she
131
+ // wrote to herself, a meeting that met this wish, words she spoke
132
+ // about it, or a line she later sent about it, are also living it.
133
+ let at =
134
+ earnedAt(versions.get(row.thread), (sources) =>
135
+ evidenceRoots(this.db, sources, before),
136
+ ) ?? row.created;
137
+ if (row.kind === "intention" && ownLife(row.content)) {
138
+ for (const note of notes)
139
+ if (
140
+ note.created > at &&
141
+ aboutLife(row.content, note.content) &&
142
+ !leftLife(row.content, note.content)
143
+ )
144
+ at = note.created;
145
+ const metAt = this.mind.meetings.livedAt(row.thread, before);
146
+ if (metAt != null && metAt > at) at = metAt;
147
+ }
148
+ return {
149
+ ...row,
150
+ salience: threadSalience({ ...row, created: at }, lived),
151
+ };
152
+ });
153
+ // One core seat belongs to the wish she is living. Stronger duties
154
+ // keep the remaining seats; they do not fade her own life out of view.
155
+ const reserved = this.#preferLived(
156
+ rows.filter((row) => row.kind === "intention" && ownLife(row.content)),
157
+ at,
158
+ );
159
+ const rest = rows
160
+ .filter((row) => row.thread !== reserved?.thread)
161
+ // A single extracted remark belongs to its subject, not to every future
162
+ // conversation. It remains retrievable and can become enduring through
163
+ // independent encounters on later days.
164
+ .filter(
165
+ (row) =>
166
+ row.origin !== "memory" ||
167
+ ["interest", "curiosity", "intention"].includes(row.kind) ||
168
+ new Set(row.days).size >= 3,
169
+ )
170
+ .sort((a, b) => b.strength - a.strength || b.created - a.created);
171
+ const core = new Set(
172
+ [reserved, ...rest.slice(0, CORE_THREADS - (reserved ? 1 : 0))]
173
+ .filter(Boolean)
174
+ .map((row) => row.thread),
175
+ );
176
+ const ranked = rows
177
+ .map((row) => ({
178
+ ...row,
179
+ core: core.has(row.thread),
180
+ faded: !core.has(row.thread) && row.salience < THREAD_FADED,
181
+ }))
182
+ .sort((a, b) => b.salience - a.salience || b.created - a.created);
183
+ // Several views in one read use the same exact moment, never a later replay.
184
+ // Writes and external database changes invalidate the derived picture.
185
+ this.annotation = { key, rows: ranked };
186
+ return structuredClone(ranked);
187
+ }
188
+ // The one wish she is living. In a room, a stronger wish that does not
189
+ // belong there does not erase the next wish that does. A duty about
190
+ // how she will treat someone stays a thread; it does not take this seat
191
+ // or the core place that keeps this wish from fading first.
192
+ living({ before = Number.MAX_SAFE_INTEGER, now, room } = {}) {
193
+ const at = now ?? (before < Number.MAX_SAFE_INTEGER ? before : Date.now());
194
+ return this.#preferLived(
195
+ this.annotated({ before, now: at }).filter(
196
+ (row) =>
197
+ row.kind === "intention" &&
198
+ !row.faded &&
199
+ ownLife(row.content) &&
200
+ (room === undefined || this.mind.meetings.stays(row, room)),
201
+ ),
202
+ at,
203
+ );
204
+ }
205
+ // Among her own wishes, the one her latest note or spoken line is about.
206
+ // A later send or answer about another wish takes this seat from an
207
+ // earlier note. A line that puts a wish down does not keep that seat;
208
+ // older notes about the put-down wish do not take it back. Planned
209
+ // drafts and silent meetings do not. No such line: the one still most
210
+ // present. Duties never enter this list.
211
+ #preferLived(candidates, now) {
212
+ if (!candidates.length) return null;
213
+ const notes = this.mind.thoughts
214
+ .open({ now, limit: 8 })
215
+ .filter((t) => t.kind === "expression" || t.kind === "unfinished");
216
+ const lines = this.mind.meetings.recentSaid(now, 8);
217
+ for (const note of notes) {
218
+ if (note.outreach_status === "sent" && note.outreach)
219
+ lines.push({
220
+ text: note.outreach,
221
+ at: note.outreach_at ?? note.created,
222
+ spoken: true,
223
+ });
224
+ lines.push({ text: note.content, at: note.created, spoken: false });
225
+ }
226
+ lines.sort((a, b) => b.at - a.at || Number(b.spoken) - Number(a.spoken));
227
+ const dropped = new Set();
228
+ for (const line of lines) {
229
+ for (const row of candidates)
230
+ if (!dropped.has(row.thread) && leftLife(row.content, line.text))
231
+ dropped.add(row.thread);
232
+ const held = candidates.filter(
233
+ (row) =>
234
+ !dropped.has(row.thread) &&
235
+ aboutLife(row.content, line.text) &&
236
+ !leftLife(row.content, line.text),
237
+ );
238
+ if (held.length)
239
+ return [...held].sort(
240
+ (a, b) => b.salience - a.salience || b.created - a.created,
241
+ )[0];
242
+ }
243
+ const remain = candidates.filter((row) => !dropped.has(row.thread));
244
+ if (!remain.length) return null;
245
+ return [...remain].sort(
246
+ (a, b) => b.salience - a.salience || b.created - a.created,
247
+ )[0];
248
+ }
249
+ // Notes she wrote to herself. Hidden ones do not count; resolved ones
250
+ // still do, because she did think them.
251
+ #lifeNotes(before) {
252
+ return this.db
253
+ .prepare(
254
+ `SELECT created, content FROM mind_thoughts
255
+ WHERE created<=? AND kind IN ('expression','unfinished') AND hidden=0`,
256
+ )
257
+ .all(before);
258
+ }
259
+ // Open threads that are the same wish as this one, including itself.
260
+ // History stays split; reading treats them as one life.
261
+ sameThreads(row, { before = Number.MAX_SAFE_INTEGER } = {}) {
262
+ if (!row?.thread) return [];
263
+ const kind = row.kind || "intention";
264
+ const content = row.content || "";
265
+ return this.latest(before).filter(
266
+ (other) =>
267
+ other.status !== "closed" &&
268
+ other.kind === kind &&
269
+ (other.thread === row.thread ||
270
+ (content && sameSelf(other.content, content))),
271
+ );
272
+ }
273
+ active({ before, limit = 40, now } = {}) {
274
+ const ranked = this.annotated({ before, now }).filter((row) => !row.faded);
275
+ const picked = ranked.filter((row) => row.core).slice(0, limit);
276
+ for (const row of ranked) {
277
+ if (picked.length >= limit) break;
278
+ if (!row.core) picked.push(row);
279
+ }
280
+ return picked.sort(
281
+ (a, b) => b.salience - a.salience || b.created - a.created,
282
+ );
283
+ }
284
+ dormant({ before, now } = {}) {
285
+ return this.annotated({ before, now }).filter((row) => row.faded);
286
+ }
287
+ // Faded threads that what is being said right now brings back to mind.
288
+ reminded({ before, now, cue, cues, limit = 2 } = {}) {
289
+ const sets = cueList(cue, cues);
290
+ if (!sets.length) return [];
291
+ return this.dormant({ before, now })
292
+ .filter((row) => anyTouches(row.content, sets))
293
+ .sort((a, b) => b.strength - a.strength)
294
+ .slice(0, limit);
295
+ }
296
+ // Threads slipping out of view, for her to let go of or find again.
297
+ fading({ before, now, limit = 3 } = {}) {
298
+ return this.annotated({ before, now })
299
+ .filter(
300
+ (row) =>
301
+ !row.core && row.salience < THREAD_FADING && row.salience >= 0.04,
302
+ )
303
+ .sort((a, b) => b.strength - a.strength)
304
+ .slice(0, limit);
305
+ }
306
+ history(thread) {
307
+ return this.db
308
+ .prepare("SELECT * FROM mind_self WHERE thread=? ORDER BY created")
309
+ .all(thread)
310
+ .map((row) => ({
311
+ ...row,
312
+ sources: parse(row.sources, []),
313
+ days: parse(row.days, []),
314
+ }));
315
+ }
316
+ days(sources, time, { external = false } = {}) {
317
+ const timeZone = this.mind.timeZone();
318
+ const seqs = messageSeqs(
319
+ external
320
+ ? externalEvidence(this.db, sources, time)
321
+ : evidenceRoots(this.db, sources, time),
322
+ );
323
+ const found = seqs.length
324
+ ? this.db
325
+ .prepare(
326
+ `SELECT time FROM core_events WHERE seq IN (${seqs.map(() => "?").join(",")})`,
327
+ )
328
+ .all(...seqs)
329
+ .map((row) => dayKey(row.time, timeZone))
330
+ : [];
331
+ // Diary days cited in a review count as the days it happened.
332
+ // A diary written later does not make its underlying experience later.
333
+ return [
334
+ ...new Set(
335
+ found.length ? found : external ? [] : [dayKey(time, timeZone)],
336
+ ),
337
+ ];
338
+ }
339
+ propose(
340
+ input,
341
+ { valid = null, origin = "solitude", time = Date.now() } = {},
342
+ ) {
343
+ const kind = SELF_KINDS[input?.kind] ? input.kind : null;
344
+ const content = text(input?.content, 160);
345
+ let action = ["new", "revise", "close"].includes(input?.action)
346
+ ? input.action
347
+ : input?.thread
348
+ ? "revise"
349
+ : "new";
350
+ if (!content && action !== "close") return { rejected: "空内容" };
351
+ if (hasCredential(content)) return { rejected: "疑似凭据" };
352
+ if (action !== "close" && fixedReplyHabit(content))
353
+ return { rejected: "临时回复台词不能固化成自我习惯" };
354
+ // What she said to fit in is not a life she has lived.
355
+ if (action !== "close" && claimedPlay(content))
356
+ return { rejected: "没有玩过的经历,不能写成她的样子" };
357
+ const cited = evidence(input?.sources);
358
+ const sources = valid ? cited.filter((s) => valid.has(s)) : cited;
359
+ if (cited.length && !sources.length)
360
+ return { rejected: "来源不在本次经历中" };
361
+ // Memory consolidation only records what she herself said. A wish with
362
+ // no message is hers to form in solitude, not for the summarizer to add.
363
+ if (origin === "memory" && !sources.length) return { rejected: "缺少来源" };
364
+ if (kind === "lesson" && action !== "close") {
365
+ const outside = externalEvidence(this.db, sources, time);
366
+ if (!outside.length) return { rejected: "教训需要模型之外的经历" };
367
+ const spent = new Set(
368
+ this.db
369
+ .prepare(
370
+ "SELECT sources FROM mind_self WHERE kind='lesson' AND thread NOT IN (SELECT target_id FROM mind_revocations WHERE target_kind='self')",
371
+ )
372
+ .all()
373
+ .flatMap((row) =>
374
+ evidenceRoots(this.db, parse(row.sources, []), time),
375
+ ),
376
+ );
377
+ if (outside.every((source) => spent.has(source)))
378
+ return { rejected: "这段经历已经写成过教训" };
379
+ if (outside.length < 2 && action === "new")
380
+ input = { ...input, strength: 0.2 };
381
+ }
382
+ if (
383
+ content &&
384
+ this.revokedContents().some((old) => similar(old, content, 0.7))
385
+ )
386
+ return { rejected: "与撤销过的内容相同" };
387
+ // Faded threads count too: touching one again brings it back instead of
388
+ // growing a second copy of it.
389
+ const current = this.latest(time).filter((row) => row.status !== "closed");
390
+ let prior = null;
391
+ if (action === "new") {
392
+ if (!kind) return { rejected: "类型无效" };
393
+ prior = current.find(
394
+ (row) => row.kind === kind && sameSelf(row.content, content),
395
+ );
396
+ if (prior) action = "revise";
397
+ else if (!sources.length && !SELF_ORIGINATED.has(kind))
398
+ return { rejected: "缺少来源" };
399
+ } else {
400
+ prior = this.latest(time).find((row) => row.thread === input.thread);
401
+ if (!prior) return { rejected: "线索不存在或已撤销" };
402
+ }
403
+ const spent = new Set();
404
+ if (prior)
405
+ for (const row of this.history(prior.thread))
406
+ for (const source of evidenceRoots(this.db, row.sources || [], time))
407
+ spent.add(source);
408
+ let fresh = sources.filter((source) =>
409
+ evidenceRoots(this.db, [source], time).some((root) => !spent.has(root)),
410
+ );
411
+ const lasting = ["trait", "habit"].includes(prior?.kind || kind);
412
+ const freshWords = fresh;
413
+ if (lasting)
414
+ fresh = fresh.filter((source) =>
415
+ externalEvidence(this.db, [source], time).some(
416
+ (root) => !spent.has(root),
417
+ ),
418
+ );
419
+ const spoken = content || prior?.content || "";
420
+ // The same evidence cannot be spent again to make a thread stronger.
421
+ // An unchanged sentence is not a new version. A rewording can stay,
422
+ // at the strength the earlier evidence already earned.
423
+ if (
424
+ prior &&
425
+ action !== "close" &&
426
+ !fresh.length &&
427
+ (spoken === prior.content || origin === "memory")
428
+ )
429
+ return { rejected: "没有新的经历" };
430
+ // Citing her message is not enough. The words kept have to be hers.
431
+ if (
432
+ origin === "memory" &&
433
+ action !== "close" &&
434
+ !echoes(spoken, this.#spoken(freshWords))
435
+ )
436
+ return { rejected: "对不上她说过的话" };
437
+ // A revision that cites nothing new keeps the old provenance. Dropping
438
+ // it would let a privately learned sentence travel into other rooms.
439
+ const kept = [
440
+ ...new Set([
441
+ ...(prior && !fresh.length ? prior.sources || [] : []),
442
+ ...sources,
443
+ ]),
444
+ ];
445
+ const days = [
446
+ ...new Set([
447
+ ...(prior?.days || []),
448
+ ...this.days(kept, time, { external: lasting }),
449
+ ]),
450
+ ].slice(-60);
451
+ const lessonRoots = evidenceRoots(
452
+ this.db,
453
+ [
454
+ ...kept,
455
+ ...(prior
456
+ ? this.history(prior.thread).flatMap((row) => row.sources || [])
457
+ : []),
458
+ ],
459
+ time,
460
+ );
461
+ let strength;
462
+ let status;
463
+ if (action === "close") {
464
+ strength = prior.strength;
465
+ status = "closed";
466
+ } else if (prior) {
467
+ const wanted = fresh.length
468
+ ? Number.isFinite(Number(input?.strength))
469
+ ? clamp(input.strength)
470
+ : prior.strength + 0.05
471
+ : prior.strength;
472
+ strength = fresh.length
473
+ ? clamp(prior.strength + clamp(wanted - prior.strength, -STEP, STEP))
474
+ : prior.strength;
475
+ status =
476
+ (prior?.kind || kind) === "trait" &&
477
+ (days.length < 2 || strength < 0.35)
478
+ ? "emerging"
479
+ : (prior?.kind || kind) === "lesson" && lessonRoots.length < 2
480
+ ? "emerging"
481
+ : "active";
482
+ } else {
483
+ strength = Math.min(
484
+ kind === "trait" ? TRAIT_CAP : NEW_CAP,
485
+ clamp(input?.strength ?? 0.25),
486
+ );
487
+ status =
488
+ kind === "trait" || (kind === "lesson" && lessonRoots.length < 2)
489
+ ? "emerging"
490
+ : "active";
491
+ }
492
+ const id = randomUUID();
493
+ const home = this.#home(spoken, input?.session);
494
+ const subject =
495
+ kind === "lesson" || prior?.kind === "lesson"
496
+ ? text(input?.subject, 40) || prior?.subject || null
497
+ : prior?.subject || null;
498
+ this.db
499
+ .prepare(
500
+ "INSERT INTO mind_self(id,thread,created,kind,content,strength,status,sources,days,origin,session_id,subject) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)",
501
+ )
502
+ .run(
503
+ id,
504
+ prior?.thread || id,
505
+ time,
506
+ prior?.kind || kind,
507
+ spoken,
508
+ Math.round(strength * 100) / 100,
509
+ status,
510
+ JSON.stringify(kept),
511
+ JSON.stringify(days),
512
+ origin,
513
+ home,
514
+ subject,
515
+ );
516
+ this.mind.recall?.sync("self", prior?.thread || id);
517
+ return { id, thread: prior?.thread || id, action };
518
+ }
519
+ // Wording that matches something she learned in private stays in that room,
520
+ // even when she did not cite a source.
521
+ #home(content, requested) {
522
+ const hit = leaks([content], this.mind.meetings.privateSayings())[0];
523
+ if (hit?.session_id) return hit.session_id;
524
+ return isPrivateSession(requested) ? requested : null;
525
+ }
526
+ #spoken(sources) {
527
+ const seqs = messageSeqs(sources);
528
+ if (!seqs.length) return [];
529
+ return this.db
530
+ .prepare(
531
+ `SELECT role, payload FROM core_events WHERE seq IN (${seqs.map(() => "?").join(",")})`,
532
+ )
533
+ .all(...seqs)
534
+ .filter((row) => row.role === "assistant")
535
+ .map((row) => parse(row.payload, {}).text || "");
536
+ }
537
+ }