luckytri 1.0.6 → 1.0.7

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 (155) hide show
  1. package/LICENSE +21 -21
  2. package/docs/en/CHANGELOG.md +25 -16
  3. package/docs/en/chat-style.md +28 -9
  4. package/docs/en/guide.md +5 -5
  5. package/docs/en/her-life.md +2 -2
  6. package/docs/en/webui.md +1 -1
  7. package/docs/zh/CHANGELOG.md +25 -16
  8. package/docs/zh/chat-style.md +30 -9
  9. package/docs/zh/guide.md +5 -5
  10. package/docs/zh/her-life.md +2 -2
  11. package/docs/zh/webui.md +1 -1
  12. package/package.json +1 -1
  13. package/public/app/assets/{ChatsPage-DCYMkJ91.js → ChatsPage-CshdtTzV.js} +2 -2
  14. package/public/app/assets/ChatsPage-DfyPCS9N.css +1 -0
  15. package/public/app/assets/{Empty-CLyZkxR5.js → Empty-C7yzOBzy.js} +1 -1
  16. package/public/app/assets/{Empty-DIobYG3x.css → Empty-DmUB2-Bk.css} +1 -1
  17. package/public/app/assets/{Field-BpaRwLI_.js → Field-BjFv4Rfn.js} +1 -1
  18. package/public/app/assets/HeartPage-Bft9wZp5.css +1 -0
  19. package/public/app/assets/{HeartPage-BIOMTiwr.js → HeartPage-eqo6ru16.js} +1 -1
  20. package/public/app/assets/LifePage-BUi5euBF.css +1 -0
  21. package/public/app/assets/{LifePage-BCgM4JkR.js → LifePage-CN8GE5NK.js} +1 -1
  22. package/public/app/assets/{MemoryPage-vShvRqMt.js → MemoryPage-CCpMN4Wg.js} +1 -1
  23. package/public/app/assets/MemoryPage-cZvXfkXb.css +1 -0
  24. package/public/app/assets/{NaturePage-Cy9ptr4G.js → NaturePage-C0JqdBTR.js} +2 -2
  25. package/public/app/assets/NaturePage-CHkpb5NK.css +1 -0
  26. package/public/app/assets/PeoplePage-BTU0H1pM.css +1 -0
  27. package/public/app/assets/{PeoplePage-CUZekB2E.js → PeoplePage-BXGBbO4u.js} +1 -1
  28. package/public/app/assets/{PluginsPage-1RM4rmJP.js → PluginsPage-Dksp5AV5.js} +1 -1
  29. package/public/app/assets/{SystemPage-BI2CB5Ig.css → SystemPage-BenIyTvk.css} +1 -1
  30. package/public/app/assets/{SystemPage-BeT5TmIV.js → SystemPage-Cd7aa8eE.js} +1 -1
  31. package/public/app/assets/TimePage-BOms_n4I.css +1 -0
  32. package/public/app/assets/{TimePage-taTmPMM8.js → TimePage-Bs_zImb4.js} +2 -2
  33. package/public/app/assets/{index-DY5AM0Hz.css → index-BHZcHUMh.css} +1 -1
  34. package/public/app/assets/{index-DdYwC5ZU.js → index-CyRtYxP3.js} +3 -3
  35. package/public/app/assets/{knowledge-DJmfu2KN.js → knowledge-BJOIyuJo.js} +1 -1
  36. package/public/app/assets/{media-BEtnE0Gv.js → media-Bwtx8blz.js} +1 -1
  37. package/public/app/assets/{mind-Dtiw0-QK.js → mind-DtTVrW6Z.js} +1 -1
  38. package/public/app/index.html +5 -6
  39. package/public/guide.en.html +3 -3
  40. package/public/guide.html +3 -3
  41. package/scripts/import-data.js +128 -128
  42. package/scripts/proxy-config.js +167 -167
  43. package/scripts/update-instance.js +201 -201
  44. package/server/channels/adapters.js +27 -27
  45. package/server/channels/onebot/adapter.js +288 -288
  46. package/server/channels/qqbot/adapter.js +253 -253
  47. package/server/channels/qqbot/api.js +199 -199
  48. package/server/channels/qqbot/credentials.js +11 -11
  49. package/server/channels/qqbot/gateway.js +274 -274
  50. package/server/channels/qqbot/index.js +389 -389
  51. package/server/channels/segments.js +40 -40
  52. package/server/channels/session-key.js +153 -153
  53. package/server/channels/state.js +42 -42
  54. package/server/core/api.js +564 -564
  55. package/server/core/context-builder.js +288 -288
  56. package/server/core/context-compactor.js +454 -454
  57. package/server/core/conversation-cues.js +365 -325
  58. package/server/core/conversation-grounding.js +178 -0
  59. package/server/core/conversation-manager.js +87 -87
  60. package/server/core/dialogue-context.js +52 -0
  61. package/server/core/feedback.js +8 -8
  62. package/server/core/group-style.js +72 -72
  63. package/server/core/initiative-context.js +66 -66
  64. package/server/core/input-budget.js +32 -32
  65. package/server/core/local-demo.js +98 -98
  66. package/server/core/message-manager.js +118 -118
  67. package/server/core/message-scheduler.js +78 -78
  68. package/server/core/model-json.js +190 -190
  69. package/server/core/model-presets.js +900 -900
  70. package/server/core/name-call.js +29 -29
  71. package/server/core/network.js +157 -157
  72. package/server/core/person-name.js +22 -22
  73. package/server/core/persona-manager.js +324 -269
  74. package/server/core/reply-delivery.js +472 -404
  75. package/server/core/reply-target-resolver.js +50 -50
  76. package/server/core/response-generator.js +58 -50
  77. package/server/core/response-validator.js +30 -27
  78. package/server/core/retired-prompts.js +51 -51
  79. package/server/core/sessions.js +109 -109
  80. package/server/core/speaker-names.js +92 -92
  81. package/server/core/topic-tracker.js +65 -65
  82. package/server/core/turn.js +42 -8
  83. package/server/http.js +18 -18
  84. package/server/i18n/decisions.js +209 -209
  85. package/server/i18n/index.js +144 -144
  86. package/server/i18n/labels.js +0 -5
  87. package/server/i18n/models.js +111 -111
  88. package/server/knowledge/api.js +342 -342
  89. package/server/knowledge/embedding-profile.js +114 -114
  90. package/server/knowledge/index.js +13 -13
  91. package/server/knowledge/retrieval.js +93 -93
  92. package/server/knowledge/schema.js +158 -158
  93. package/server/knowledge/vector-index.js +90 -90
  94. package/server/mind/affect.js +265 -265
  95. package/server/mind/attention.js +200 -200
  96. package/server/mind/bonds.js +565 -565
  97. package/server/mind/clock.js +83 -83
  98. package/server/mind/context-selection.js +16 -4
  99. package/server/mind/continuity.js +562 -562
  100. package/server/mind/conversation-origin.js +175 -175
  101. package/server/mind/days.js +210 -210
  102. package/server/mind/evidence-read.js +45 -45
  103. package/server/mind/faces.js +373 -373
  104. package/server/mind/guard.js +68 -60
  105. package/server/mind/initiative.js +338 -338
  106. package/server/mind/life/activities.js +21 -21
  107. package/server/mind/life/context.js +330 -330
  108. package/server/mind/life/growth.js +261 -261
  109. package/server/mind/life/presence.js +305 -305
  110. package/server/mind/meetings.js +1069 -1069
  111. package/server/mind/memory-policy.js +48 -48
  112. package/server/mind/memory.js +888 -888
  113. package/server/mind/nature.js +358 -358
  114. package/server/mind/novelty.js +19 -19
  115. package/server/mind/periods.js +118 -118
  116. package/server/mind/reading.js +120 -120
  117. package/server/mind/relationship-context.js +71 -71
  118. package/server/mind/relationships.js +390 -390
  119. package/server/mind/salience.js +201 -201
  120. package/server/mind/schema.js +567 -567
  121. package/server/mind/self.js +496 -484
  122. package/server/mind/thoughts.js +274 -274
  123. package/server/mind/time/agenda.js +242 -242
  124. package/server/mind/time/api.js +200 -200
  125. package/server/mind/time/attention.js +34 -34
  126. package/server/mind/time/delivery-links.js +130 -130
  127. package/server/mind/time/games.js +7 -3
  128. package/server/mind/time/index.js +3 -0
  129. package/server/mind/time/model-material.js +77 -77
  130. package/server/mind/time/presentation.js +35 -51
  131. package/server/mind/time/schedule.js +83 -83
  132. package/server/mind/time/schema.js +84 -84
  133. package/server/mind/time/search.js +383 -383
  134. package/server/mind/time/sharing.js +472 -472
  135. package/server/mind/time/task-links.js +311 -311
  136. package/server/mind/time/works.js +438 -432
  137. package/server/mind/traits.js +191 -191
  138. package/server/mind/view.js +43 -6
  139. package/server/runtime/installation.js +116 -116
  140. package/server/runtime/update-manager.js +140 -140
  141. package/server/storage/data-package.js +298 -298
  142. package/server/storage/data-transfer-api.js +50 -50
  143. package/server/storage/data-transfer-worker.js +18 -18
  144. package/server/storage/data-transfer.js +416 -416
  145. package/server/studio/display-names.js +139 -139
  146. package/server/studio/management.js +102 -102
  147. package/server/studio/updates.js +144 -144
  148. package/server/version.js +10 -10
  149. package/public/app/assets/ChatsPage-CFMVozoG.css +0 -1
  150. package/public/app/assets/HeartPage-C-2jvgzF.css +0 -1
  151. package/public/app/assets/LifePage-CG1OHbRK.css +0 -1
  152. package/public/app/assets/MemoryPage-C_Bu4rfb.css +0 -1
  153. package/public/app/assets/NaturePage-CPZlImT0.css +0 -1
  154. package/public/app/assets/PeoplePage-3RRUITDz.css +0 -1
  155. package/public/app/assets/TimePage-C-guwPk2.css +0 -1
@@ -1,210 +1,210 @@
1
- import { localClock } from "../core/conversation-cues.js";
2
- import { lifeDayKey, lifeDayStart, lifeSpan } from "./nature.js";
3
- import { DAY } from "./util.js";
4
-
5
- const MINUTE = 60000;
6
- const LIVE = "COALESCE(json_extract(payload,'$.simulated'),0)=0";
7
- const BACKFILL_DAYS = 14;
8
- const MILESTONES = [100, 200, 300, 500, 1000];
9
-
10
- // The days she has actually lived. Things fade by how much life has passed
11
- // since, not by the calendar, so a silent month does not wear her away.
12
- export class Days {
13
- constructor(mind) {
14
- this.mind = mind;
15
- this.db = mind.db;
16
- this.rolled = null;
17
- }
18
- key(time, nature = this.mind.nature.current(time)) {
19
- return lifeDayKey(nature, time, this.mind.timeZone());
20
- }
21
- start(time, nature = this.mind.nature.current(time)) {
22
- return lifeDayStart(nature, time, this.mind.timeZone());
23
- }
24
- // Each finished day is written once; cheap enough to call every minute.
25
- rollup(now = Date.now()) {
26
- const nature = this.mind.nature.current(now);
27
- const today = this.key(now, nature);
28
- if (this.rolled === today) return 0;
29
- const has = this.db.prepare("SELECT 1 FROM mind_days WHERE day=?");
30
- let end = this.start(now, nature);
31
- let written = 0;
32
- for (let i = 0; i < BACKFILL_DAYS; i++) {
33
- const start = this.start(end - MINUTE, nature);
34
- const day = this.key(start, nature);
35
- if (!has.get(day) && this.write(day, start, end, now)) written++;
36
- end = start;
37
- }
38
- this.rolled = today;
39
- return written;
40
- }
41
- write(day, start, end, now) {
42
- const events = this.db
43
- .prepare(
44
- `SELECT COUNT(*) n, COUNT(DISTINCT CASE WHEN role='user' THEN json_extract(payload,'$.userId') END) people FROM core_events WHERE time>=? AND time<? AND ${LIVE} AND seq NOT IN (SELECT seq FROM mind_unlived)`,
45
- )
46
- .get(start, end);
47
- const felt = this.db
48
- .prepare(
49
- "SELECT SUM(valence*intensity) v, SUM(intensity) i, AVG(arousal) a, COUNT(*) n FROM mind_affect WHERE created>=? AND created<?",
50
- )
51
- .get(start, end);
52
- const choices = this.db
53
- .prepare(
54
- "SELECT COUNT(*) n, COALESCE(SUM(choice!='silent'),0) spoke FROM mind_choices WHERE created>=? AND created<?",
55
- )
56
- .get(start, end);
57
- const present = this.participated(start, end);
58
- if (!events.n && !felt.n && !present) return false;
59
- const round = (value) => Math.round(value * 100) / 100;
60
- this.db
61
- .prepare(
62
- "INSERT OR IGNORE INTO mind_days(day,created,lived,events,valence,arousal,looked,spoke,people) VALUES (?,?,?,?,?,?,?,?,?)",
63
- )
64
- .run(
65
- day,
66
- now,
67
- Number(present),
68
- events.n,
69
- felt.i > 0 ? round(felt.v / felt.i) : null,
70
- felt.n ? round(felt.a) : null,
71
- choices.n,
72
- choices.spoke,
73
- events.people,
74
- );
75
- return true;
76
- }
77
- // Days written when any messages counted as a life are put back, once she
78
- // did not look, speak, or sit with them.
79
- reconcile(now = Date.now()) {
80
- const nature = this.mind.nature.current(now);
81
- const zone = this.mind.timeZone();
82
- const clear = this.db.prepare("UPDATE mind_days SET lived=0 WHERE day=?");
83
- let changed = 0;
84
- for (const row of this.db
85
- .prepare(
86
- "SELECT day FROM mind_days WHERE lived=1 AND looked=0 AND spoke=0",
87
- )
88
- .all()) {
89
- const span = lifeSpan(nature, row.day, zone);
90
- if (!span || this.participated(span.start, span.end)) continue;
91
- clear.run(row.day);
92
- changed++;
93
- }
94
- return changed;
95
- }
96
- // She looked, spoke, or sat with the day. Other people's messages do not count.
97
- participated(start, end, { inclusive = false } = {}) {
98
- const endOp = inclusive ? "<=" : "<";
99
- return !!(
100
- this.db
101
- .prepare(
102
- `SELECT 1 FROM mind_choices WHERE created>=? AND created${endOp}? AND (choice!='silent' OR COALESCE(occasion,'') NOT IN ('presence','outreach'))`,
103
- )
104
- .get(start, end) ||
105
- this.db
106
- .prepare(
107
- `SELECT 1 FROM core_events WHERE role='assistant' AND time>=? AND time${endOp}? AND ${LIVE}`,
108
- )
109
- .get(start, end) ||
110
- this.db
111
- .prepare(
112
- `SELECT 1 FROM mind_runs WHERE started>=? AND started${endOp}? AND (status IN ('written','complete','draft','experienced') OR (status='empty' AND (summary IS NULL OR COALESCE(json_extract(summary,'$.freshMessages'),0)>0)))`,
113
- )
114
- .get(start, end)
115
- );
116
- }
117
- // Counts lived days after the day of a given moment, as known at `now`.
118
- lived(now = Date.now()) {
119
- const nature = this.mind.nature.current(now);
120
- const days = this.db
121
- .prepare(
122
- "SELECT day FROM mind_days WHERE lived=1 AND created<=? ORDER BY day",
123
- )
124
- .all(now)
125
- .map((row) => row.day);
126
- const since = (time) => {
127
- if (!days.length) return 0;
128
- if (!time) return days.length;
129
- const key = this.key(time, nature);
130
- let lo = 0;
131
- let hi = days.length;
132
- while (lo < hi) {
133
- const mid = (lo + hi) >> 1;
134
- if (days[mid] <= key) lo = mid + 1;
135
- else hi = mid;
136
- }
137
- return days.length - lo;
138
- };
139
- return { count: days.length, since };
140
- }
141
- recent({ now = Date.now(), limit = 14 } = {}) {
142
- return this.db
143
- .prepare(
144
- "SELECT * FROM mind_days WHERE created<=? ORDER BY day DESC LIMIT ?",
145
- )
146
- .all(now, limit);
147
- }
148
- // Her first day: the first thing that happened to her, or her first nature.
149
- born() {
150
- const event = this.db
151
- .prepare(
152
- `SELECT MIN(time) t FROM core_events WHERE ${LIVE} AND seq NOT IN (SELECT seq FROM mind_unlived)`,
153
- )
154
- .get().t;
155
- const nature = this.db
156
- .prepare("SELECT MIN(created) t FROM mind_nature")
157
- .get().t;
158
- const times = [event, nature].filter((t) => Number.isFinite(t));
159
- return times.length ? Math.min(...times) : null;
160
- }
161
- dayOfLife(now = Date.now()) {
162
- const born = this.born();
163
- if (!born || born > now) return 1;
164
- const zone = this.mind.timeZone();
165
- const from = Date.parse(
166
- `${localClock(born, zone).local.slice(0, 10)}T00:00:00Z`,
167
- );
168
- const to = Date.parse(
169
- `${localClock(now, zone).local.slice(0, 10)}T00:00:00Z`,
170
- );
171
- return Math.round((to - from) / DAY) + 1;
172
- }
173
- // Round numbers and yearly returns: of her own days, and of meeting people
174
- // she has come to know.
175
- anniversaries(now = Date.now(), { limit = 3 } = {}) {
176
- const zone = this.mind.timeZone();
177
- const today = localClock(now, zone).local.slice(0, 10);
178
- const out = [];
179
- const mark = (first, label) => {
180
- const day = localClock(first, zone).local.slice(0, 10);
181
- const days =
182
- Math.round(
183
- (Date.parse(`${today}T00:00:00Z`) - Date.parse(`${day}T00:00:00Z`)) /
184
- DAY,
185
- ) + 1;
186
- const years = Number(today.slice(0, 4)) - Number(day.slice(0, 4));
187
- if (years >= 1 && day.slice(5) === today.slice(5))
188
- return `${label}满 ${years} 年`;
189
- if (MILESTONES.includes(days)) return `${label}的第 ${days} 天`;
190
- return null;
191
- };
192
- const born = this.born();
193
- if (born && born <= now) {
194
- const own = mark(born, "我来到这里");
195
- if (own) out.push(own);
196
- }
197
- for (const row of this.db
198
- .prepare(
199
- "SELECT user_id,first_seen FROM mind_people WHERE first_seen<=? ORDER BY first_seen LIMIT 500",
200
- )
201
- .all(now)) {
202
- if (out.length >= limit) break;
203
- const person = this.mind.bonds.person(row.user_id, now);
204
- if (!person || person.familiarity < 0.3) continue;
205
- const label = mark(row.first_seen, `认识${person.name}`);
206
- if (label) out.push(label);
207
- }
208
- return out.slice(0, limit);
209
- }
210
- }
1
+ import { localClock } from "../core/conversation-cues.js";
2
+ import { lifeDayKey, lifeDayStart, lifeSpan } from "./nature.js";
3
+ import { DAY } from "./util.js";
4
+
5
+ const MINUTE = 60000;
6
+ const LIVE = "COALESCE(json_extract(payload,'$.simulated'),0)=0";
7
+ const BACKFILL_DAYS = 14;
8
+ const MILESTONES = [100, 200, 300, 500, 1000];
9
+
10
+ // The days she has actually lived. Things fade by how much life has passed
11
+ // since, not by the calendar, so a silent month does not wear her away.
12
+ export class Days {
13
+ constructor(mind) {
14
+ this.mind = mind;
15
+ this.db = mind.db;
16
+ this.rolled = null;
17
+ }
18
+ key(time, nature = this.mind.nature.current(time)) {
19
+ return lifeDayKey(nature, time, this.mind.timeZone());
20
+ }
21
+ start(time, nature = this.mind.nature.current(time)) {
22
+ return lifeDayStart(nature, time, this.mind.timeZone());
23
+ }
24
+ // Each finished day is written once; cheap enough to call every minute.
25
+ rollup(now = Date.now()) {
26
+ const nature = this.mind.nature.current(now);
27
+ const today = this.key(now, nature);
28
+ if (this.rolled === today) return 0;
29
+ const has = this.db.prepare("SELECT 1 FROM mind_days WHERE day=?");
30
+ let end = this.start(now, nature);
31
+ let written = 0;
32
+ for (let i = 0; i < BACKFILL_DAYS; i++) {
33
+ const start = this.start(end - MINUTE, nature);
34
+ const day = this.key(start, nature);
35
+ if (!has.get(day) && this.write(day, start, end, now)) written++;
36
+ end = start;
37
+ }
38
+ this.rolled = today;
39
+ return written;
40
+ }
41
+ write(day, start, end, now) {
42
+ const events = this.db
43
+ .prepare(
44
+ `SELECT COUNT(*) n, COUNT(DISTINCT CASE WHEN role='user' THEN json_extract(payload,'$.userId') END) people FROM core_events WHERE time>=? AND time<? AND ${LIVE} AND seq NOT IN (SELECT seq FROM mind_unlived)`,
45
+ )
46
+ .get(start, end);
47
+ const felt = this.db
48
+ .prepare(
49
+ "SELECT SUM(valence*intensity) v, SUM(intensity) i, AVG(arousal) a, COUNT(*) n FROM mind_affect WHERE created>=? AND created<?",
50
+ )
51
+ .get(start, end);
52
+ const choices = this.db
53
+ .prepare(
54
+ "SELECT COUNT(*) n, COALESCE(SUM(choice!='silent'),0) spoke FROM mind_choices WHERE created>=? AND created<?",
55
+ )
56
+ .get(start, end);
57
+ const present = this.participated(start, end);
58
+ if (!events.n && !felt.n && !present) return false;
59
+ const round = (value) => Math.round(value * 100) / 100;
60
+ this.db
61
+ .prepare(
62
+ "INSERT OR IGNORE INTO mind_days(day,created,lived,events,valence,arousal,looked,spoke,people) VALUES (?,?,?,?,?,?,?,?,?)",
63
+ )
64
+ .run(
65
+ day,
66
+ now,
67
+ Number(present),
68
+ events.n,
69
+ felt.i > 0 ? round(felt.v / felt.i) : null,
70
+ felt.n ? round(felt.a) : null,
71
+ choices.n,
72
+ choices.spoke,
73
+ events.people,
74
+ );
75
+ return true;
76
+ }
77
+ // Days written when any messages counted as a life are put back, once she
78
+ // did not look, speak, or sit with them.
79
+ reconcile(now = Date.now()) {
80
+ const nature = this.mind.nature.current(now);
81
+ const zone = this.mind.timeZone();
82
+ const clear = this.db.prepare("UPDATE mind_days SET lived=0 WHERE day=?");
83
+ let changed = 0;
84
+ for (const row of this.db
85
+ .prepare(
86
+ "SELECT day FROM mind_days WHERE lived=1 AND looked=0 AND spoke=0",
87
+ )
88
+ .all()) {
89
+ const span = lifeSpan(nature, row.day, zone);
90
+ if (!span || this.participated(span.start, span.end)) continue;
91
+ clear.run(row.day);
92
+ changed++;
93
+ }
94
+ return changed;
95
+ }
96
+ // She looked, spoke, or sat with the day. Other people's messages do not count.
97
+ participated(start, end, { inclusive = false } = {}) {
98
+ const endOp = inclusive ? "<=" : "<";
99
+ return !!(
100
+ this.db
101
+ .prepare(
102
+ `SELECT 1 FROM mind_choices WHERE created>=? AND created${endOp}? AND (choice!='silent' OR COALESCE(occasion,'') NOT IN ('presence','outreach'))`,
103
+ )
104
+ .get(start, end) ||
105
+ this.db
106
+ .prepare(
107
+ `SELECT 1 FROM core_events WHERE role='assistant' AND time>=? AND time${endOp}? AND ${LIVE}`,
108
+ )
109
+ .get(start, end) ||
110
+ this.db
111
+ .prepare(
112
+ `SELECT 1 FROM mind_runs WHERE started>=? AND started${endOp}? AND (status IN ('written','complete','draft','experienced') OR (status='empty' AND (summary IS NULL OR COALESCE(json_extract(summary,'$.freshMessages'),0)>0)))`,
113
+ )
114
+ .get(start, end)
115
+ );
116
+ }
117
+ // Counts lived days after the day of a given moment, as known at `now`.
118
+ lived(now = Date.now()) {
119
+ const nature = this.mind.nature.current(now);
120
+ const days = this.db
121
+ .prepare(
122
+ "SELECT day FROM mind_days WHERE lived=1 AND created<=? ORDER BY day",
123
+ )
124
+ .all(now)
125
+ .map((row) => row.day);
126
+ const since = (time) => {
127
+ if (!days.length) return 0;
128
+ if (!time) return days.length;
129
+ const key = this.key(time, nature);
130
+ let lo = 0;
131
+ let hi = days.length;
132
+ while (lo < hi) {
133
+ const mid = (lo + hi) >> 1;
134
+ if (days[mid] <= key) lo = mid + 1;
135
+ else hi = mid;
136
+ }
137
+ return days.length - lo;
138
+ };
139
+ return { count: days.length, since };
140
+ }
141
+ recent({ now = Date.now(), limit = 14 } = {}) {
142
+ return this.db
143
+ .prepare(
144
+ "SELECT * FROM mind_days WHERE created<=? ORDER BY day DESC LIMIT ?",
145
+ )
146
+ .all(now, limit);
147
+ }
148
+ // Her first day: the first thing that happened to her, or her first nature.
149
+ born() {
150
+ const event = this.db
151
+ .prepare(
152
+ `SELECT MIN(time) t FROM core_events WHERE ${LIVE} AND seq NOT IN (SELECT seq FROM mind_unlived)`,
153
+ )
154
+ .get().t;
155
+ const nature = this.db
156
+ .prepare("SELECT MIN(created) t FROM mind_nature")
157
+ .get().t;
158
+ const times = [event, nature].filter((t) => Number.isFinite(t));
159
+ return times.length ? Math.min(...times) : null;
160
+ }
161
+ dayOfLife(now = Date.now()) {
162
+ const born = this.born();
163
+ if (!born || born > now) return 1;
164
+ const zone = this.mind.timeZone();
165
+ const from = Date.parse(
166
+ `${localClock(born, zone).local.slice(0, 10)}T00:00:00Z`,
167
+ );
168
+ const to = Date.parse(
169
+ `${localClock(now, zone).local.slice(0, 10)}T00:00:00Z`,
170
+ );
171
+ return Math.round((to - from) / DAY) + 1;
172
+ }
173
+ // Round numbers and yearly returns: of her own days, and of meeting people
174
+ // she has come to know.
175
+ anniversaries(now = Date.now(), { limit = 3 } = {}) {
176
+ const zone = this.mind.timeZone();
177
+ const today = localClock(now, zone).local.slice(0, 10);
178
+ const out = [];
179
+ const mark = (first, label) => {
180
+ const day = localClock(first, zone).local.slice(0, 10);
181
+ const days =
182
+ Math.round(
183
+ (Date.parse(`${today}T00:00:00Z`) - Date.parse(`${day}T00:00:00Z`)) /
184
+ DAY,
185
+ ) + 1;
186
+ const years = Number(today.slice(0, 4)) - Number(day.slice(0, 4));
187
+ if (years >= 1 && day.slice(5) === today.slice(5))
188
+ return `${label}满 ${years} 年`;
189
+ if (MILESTONES.includes(days)) return `${label}的第 ${days} 天`;
190
+ return null;
191
+ };
192
+ const born = this.born();
193
+ if (born && born <= now) {
194
+ const own = mark(born, "我来到这里");
195
+ if (own) out.push(own);
196
+ }
197
+ for (const row of this.db
198
+ .prepare(
199
+ "SELECT user_id,first_seen FROM mind_people WHERE first_seen<=? ORDER BY first_seen LIMIT 500",
200
+ )
201
+ .all(now)) {
202
+ if (out.length >= limit) break;
203
+ const person = this.mind.bonds.person(row.user_id, now);
204
+ if (!person || person.familiarity < 0.3) continue;
205
+ const label = mark(row.first_seen, `认识${person.name}`);
206
+ if (label) out.push(label);
207
+ }
208
+ return out.slice(0, limit);
209
+ }
210
+ }
@@ -1,45 +1,45 @@
1
- const states = new WeakMap();
2
- const LIMIT = 4096;
3
-
4
- // Provenance walks often visit the same ancestors from many threads. Reuse
5
- // their reads, while physical DB changes and the exact cutoff keep history,
6
- // revocations and other-process updates isolated.
7
- export function evidenceReader(db) {
8
- let state = states.get(db);
9
- if (!state) {
10
- state = {
11
- mark: db.prepare(
12
- "SELECT total_changes() changes,(SELECT data_version FROM pragma_data_version) external",
13
- ),
14
- stamp: "",
15
- statements: new Map(),
16
- rows: new Map(),
17
- };
18
- states.set(db, state);
19
- }
20
- const mark = state.mark.get(),
21
- stamp = `${mark.changes}:${mark.external}`,
22
- transaction = db.isTransaction === true;
23
- // total_changes includes rolled-back writes. Never retain reads from an
24
- // explicit transaction, otherwise its abandoned data could survive rollback.
25
- if (transaction || stamp !== state.stamp) {
26
- state.rows.clear();
27
- state.stamp = stamp;
28
- }
29
- return (sql, args = [], many = false) => {
30
- let query = state.statements.get(sql);
31
- if (!query) {
32
- query = { id: state.statements.size, statement: db.prepare(sql) };
33
- state.statements.set(sql, query);
34
- }
35
- const key = JSON.stringify([query.id, many, args]);
36
- if (!transaction && state.rows.has(key)) return state.rows.get(key);
37
- const row = query.statement[many ? "all" : "get"](...args);
38
- if (!transaction) {
39
- state.rows.set(key, row);
40
- if (state.rows.size > LIMIT)
41
- state.rows.delete(state.rows.keys().next().value);
42
- }
43
- return row;
44
- };
45
- }
1
+ const states = new WeakMap();
2
+ const LIMIT = 4096;
3
+
4
+ // Provenance walks often visit the same ancestors from many threads. Reuse
5
+ // their reads, while physical DB changes and the exact cutoff keep history,
6
+ // revocations and other-process updates isolated.
7
+ export function evidenceReader(db) {
8
+ let state = states.get(db);
9
+ if (!state) {
10
+ state = {
11
+ mark: db.prepare(
12
+ "SELECT total_changes() changes,(SELECT data_version FROM pragma_data_version) external",
13
+ ),
14
+ stamp: "",
15
+ statements: new Map(),
16
+ rows: new Map(),
17
+ };
18
+ states.set(db, state);
19
+ }
20
+ const mark = state.mark.get(),
21
+ stamp = `${mark.changes}:${mark.external}`,
22
+ transaction = db.isTransaction === true;
23
+ // total_changes includes rolled-back writes. Never retain reads from an
24
+ // explicit transaction, otherwise its abandoned data could survive rollback.
25
+ if (transaction || stamp !== state.stamp) {
26
+ state.rows.clear();
27
+ state.stamp = stamp;
28
+ }
29
+ return (sql, args = [], many = false) => {
30
+ let query = state.statements.get(sql);
31
+ if (!query) {
32
+ query = { id: state.statements.size, statement: db.prepare(sql) };
33
+ state.statements.set(sql, query);
34
+ }
35
+ const key = JSON.stringify([query.id, many, args]);
36
+ if (!transaction && state.rows.has(key)) return state.rows.get(key);
37
+ const row = query.statement[many ? "all" : "get"](...args);
38
+ if (!transaction) {
39
+ state.rows.set(key, row);
40
+ if (state.rows.size > LIMIT)
41
+ state.rows.delete(state.rows.keys().next().value);
42
+ }
43
+ return row;
44
+ };
45
+ }