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,305 +1,305 @@
1
- import { isNameCall } from "../../core/name-call.js";
2
- import { agoLabel } from "../clock.js";
3
- import { isPrivateSession } from "../memory.js";
4
- import { messageSeqs, parse, text } from "../util.js";
5
- const MINUTE = 60000;
6
-
7
- // Owns this part of the lifecycle; the facade keeps the shared runtime state.
8
- export class LifePresence {
9
- constructor(owner) {
10
- this.owner = owner;
11
- }
12
- async considerPresence(now = this.owner.now()) {
13
- const s = this.owner.settings();
14
- if (!s.proactive || !this.owner.online()) return null;
15
- if (this.owner.phase(now).key === "asleep") return null;
16
- if (!this.owner.mind.budget.allows("inner", now)) return null;
17
- const last = this.owner.repo.config("own-voice", {}).attemptedAt;
18
- if (last && now - last < s.initiativeIntervalMinutes * MINUTE) return null;
19
- const contacts = this.owner.initiative.contacts(now, { ready: true });
20
- if (!contacts.length) return null;
21
- const resting = this.owner.ownVoice.resting(now);
22
- this.owner.repo.saveConfig("own-voice", {
23
- ...this.owner.repo.config("own-voice", {}),
24
- attemptedAt: now,
25
- });
26
- if (resting) return { status: "presence-silent", reason: resting };
27
- this.owner.busy = true;
28
- let formed;
29
- try {
30
- formed = await this.owner.ownVoice.form(now);
31
- } finally {
32
- this.owner.busy = false;
33
- }
34
- if (!formed.note?.share)
35
- return {
36
- status:
37
- formed.status === "error" ? "presence-error" : "presence-silent",
38
- reason: formed.reason,
39
- };
40
- // The thought already exists. Only now do we choose where it might fit;
41
- // someone else's last question cannot become its reason for existing.
42
- const contact = this.owner.initiative.chooseContact(
43
- formed.note,
44
- contacts,
45
- this.owner.now(),
46
- );
47
- if (!contact)
48
- return {
49
- status: "presence-silent",
50
- reason: "此刻没有适合分享这个念头的地方",
51
- };
52
- this.owner.mind.thoughts.planOutreach(formed.note.id, {
53
- session: contact.session,
54
- words: formed.note.words,
55
- reason: formed.note.reason,
56
- time: now,
57
- });
58
- const result = await this.owner.reachOut(this.owner.now());
59
- return result
60
- ? {
61
- ...result,
62
- status:
63
- result.status === "outreach-declined"
64
- ? "presence-silent"
65
- : result.status.replace(/^outreach-/, "presence-"),
66
- }
67
- : {
68
- status: "presence-deferred",
69
- reason: "念头已留下,等待合适的交流空隙",
70
- session: contact.session,
71
- };
72
- }
73
- presenceCooling(session, now, s) {
74
- const tried = this.owner.repo.config("presence", {});
75
- return !!(
76
- tried[session] &&
77
- now - tried[session] < s.initiativeIntervalMinutes * MINUTE
78
- );
79
- }
80
- markPresence(session, now) {
81
- const tried = { ...this.owner.repo.config("presence", {}), [session]: now };
82
- this.owner.repo.saveConfig("presence", tried);
83
- }
84
- wasHere(session, now, names, aside = null) {
85
- const skipped =
86
- aside ||
87
- new Set(
88
- this.owner.db
89
- .prepare("SELECT seq FROM mind_unlived")
90
- .all()
91
- .map((row) => row.seq),
92
- );
93
- const lived = this.owner.db
94
- .prepare(
95
- "SELECT 1 FROM core_events WHERE session_id=? AND seq NOT IN (SELECT seq FROM mind_unlived) AND COALESCE(json_extract(payload,'$.simulated'),0)=0 AND (role='assistant' OR ?=1) LIMIT 1",
96
- )
97
- .get(session, +isPrivateSession(session));
98
- if (lived) return true;
99
- if (
100
- this.owner.db
101
- .prepare("SELECT 1 FROM mind_meetings WHERE session_id=? LIMIT 1")
102
- .get(session)
103
- )
104
- return true;
105
- return this.owner.repo
106
- .recentEvents(session, 80, { simulated: false })
107
- .some((m) => {
108
- if (skipped.has(m.seq)) return false;
109
- if (m.role === "assistant") return true;
110
- const mentions = (m.mentions || []).map(String);
111
- if (m.accountId && mentions.includes(String(m.accountId))) return true;
112
- if (String(m.text || "").includes("@我")) return true;
113
- return isNameCall(m.text, names);
114
- });
115
- }
116
- async reachOut(now = this.owner.now()) {
117
- const s = this.owner.settings();
118
- if (!s.proactive || !this.owner.online()) return null;
119
- if (this.owner.phase(now).key === "asleep") return null;
120
- for (const t of this.owner.mind.thoughts.dueOutreach(now)) {
121
- const session = t.outreach_session;
122
- const audience = this.owner.initiative.audience(t, session, now);
123
- if (!audience.allowed) {
124
- this.owner.mind.thoughts.setOutreach(t.id, "skipped", audience.reason);
125
- continue;
126
- }
127
- if (this.owner.initiative.recentlyToldAnotherGroup(t, now)) {
128
- this.owner.mind.thoughts.setOutreach(t.id, "declined");
129
- continue;
130
- }
131
- const block = this.owner.outreachBlocked(session, now, s);
132
- if (block) {
133
- if (block.permanent)
134
- this.owner.mind.thoughts.setOutreach(t.id, "skipped");
135
- else
136
- this.owner.mind.thoughts.deferOutreach(
137
- t.id,
138
- block.reason,
139
- block.retryAt,
140
- );
141
- continue;
142
- }
143
- this.owner.mind.thoughts.setOutreach(t.id, "sending");
144
- this.owner.busy = true;
145
- try {
146
- const returned = this.owner.returnedSince(t, now);
147
- const trace = await this.owner.chat.initiate(session, {
148
- type: t.kind === "expression" ? "presence" : "outreach",
149
- data: {
150
- thought: t.content,
151
- planned: t.outreach,
152
- wantedBecause: t.outreach_reason,
153
- expression: {
154
- ref: `t:${t.id}`,
155
- origin:
156
- t.kind === "expression" ? "self_expression" : "earlier_wish",
157
- formedAt: t.created,
158
- thought: t.content,
159
- words: t.outreach_draft?.length ? t.outreach_draft : [t.outreach],
160
- reason: t.outreach_reason,
161
- sources: t.sources,
162
- audience,
163
- sourceMaterial: this.owner.initiative.sourceMaterial(t, now),
164
- },
165
- initiative: this.owner.initiative.context(session, now),
166
- ...(returned.length ? { returned } : {}),
167
- },
168
- });
169
- if (!trace) {
170
- this.owner.mind.thoughts.deferOutreach(
171
- t.id,
172
- "对话忙着,稍后重新看看",
173
- now + MINUTE,
174
- );
175
- return {
176
- status: "outreach-deferred",
177
- reason: "愿望保留,等待对话空隙",
178
- session,
179
- };
180
- }
181
- this.owner.markPresence(session, now);
182
- const uncertain =
183
- trace &&
184
- this.owner.db
185
- .prepare(
186
- "SELECT 1 FROM core_outbox WHERE trace_id=? AND status='uncertain'",
187
- )
188
- .get(trace.id);
189
- if (
190
- ["error", "stale"].includes(trace.status) &&
191
- !trace.sent?.length &&
192
- !uncertain &&
193
- !this.owner.db
194
- .prepare(
195
- "SELECT 1 FROM core_outbox WHERE trace_id=? AND status IN ('sending','confirmed') LIMIT 1",
196
- )
197
- .get(trace.id)
198
- ) {
199
- this.owner.mind.thoughts.deferOutreach(
200
- t.id,
201
- trace.reason || "调用失败,稍后重新决定",
202
- this.owner.now() + (trace.status === "stale" ? 1 : 15) * MINUTE,
203
- );
204
- return {
205
- status: "outreach-deferred",
206
- reason: "尚未发送,想说的话仍保留",
207
- session,
208
- };
209
- }
210
- const status = uncertain
211
- ? "uncertain"
212
- : trace?.status === "sent" || trace?.sent?.length
213
- ? "sent"
214
- : trace?.status === "silent"
215
- ? "declined"
216
- : "cancelled";
217
- this.owner.mind.thoughts.setOutreach(
218
- t.id,
219
- status,
220
- status === "declined" ? trace?.reason || "此刻不想聊这件事" : "",
221
- );
222
- return {
223
- status: `outreach-${status}`,
224
- reason: trace?.reason || "没能联系",
225
- session,
226
- };
227
- } catch {
228
- // Retry only when no delivery could have happened. An ambiguous
229
- // network acknowledgement must never produce duplicate QQ messages.
230
- const maybeSent = this.owner.db
231
- .prepare(
232
- "SELECT 1 FROM core_outbox WHERE session_id=? AND time>=? AND status IN ('sending','confirmed','uncertain') LIMIT 1",
233
- )
234
- .get(session, now);
235
- if (!maybeSent) {
236
- this.owner.mind.thoughts.deferOutreach(
237
- t.id,
238
- "调用未完成,稍后重新决定",
239
- now + 15 * MINUTE,
240
- );
241
- return {
242
- status: "outreach-deferred",
243
- reason: "调用未完成,想说的话仍保留",
244
- session,
245
- };
246
- }
247
- this.owner.mind.thoughts.setOutreach(t.id, "uncertain");
248
- return {
249
- status: "outreach-uncertain",
250
- reason: "主动联系的投递不确定,不再重试",
251
- };
252
- } finally {
253
- this.owner.busy = false;
254
- }
255
- }
256
- return null;
257
- }
258
- returnedSince(thought, now) {
259
- const sources = parse(thought?.sources, []);
260
- const ids = new Set();
261
- const meetingIds = sources
262
- .filter((ref) => String(ref).startsWith("g:"))
263
- .map((ref) => String(ref).slice(2));
264
- if (meetingIds.length) {
265
- const rows = this.owner.db
266
- .prepare(
267
- `SELECT people, will_people FROM mind_meetings WHERE id IN (${meetingIds.map(() => "?").join(",")})`,
268
- )
269
- .all(...meetingIds);
270
- for (const row of rows) {
271
- const named = parse(row.will_people, []);
272
- const who = named.length ? named : parse(row.people, []);
273
- for (const id of who) ids.add(String(id));
274
- }
275
- }
276
- const seqs = messageSeqs(sources);
277
- if (seqs.length) {
278
- const events = this.owner.db
279
- .prepare(
280
- `SELECT payload, role FROM core_events WHERE seq IN (${seqs.map(() => "?").join(",")})`,
281
- )
282
- .all(...seqs);
283
- for (const row of events) {
284
- if (row.role === "assistant") continue;
285
- const payload = parse(row.payload, {});
286
- if (payload.userId && payload.userId !== "bot")
287
- ids.add(String(payload.userId));
288
- }
289
- }
290
- const out = [];
291
- for (const id of ids) {
292
- const person = this.owner.mind.bonds.person(id, now);
293
- if (!person?.seenAt || person.seenAt <= thought.created) continue;
294
- out.push({
295
- name: person.name || id,
296
- since: agoLabel(now - person.seenAt),
297
- });
298
- if (out.length >= 3) break;
299
- }
300
- return out;
301
- }
302
- outreachBlocked(session, now, s) {
303
- return this.owner.initiative.blocked(session, now, s);
304
- }
305
- }
1
+ import { isNameCall } from "../../core/name-call.js";
2
+ import { agoLabel } from "../clock.js";
3
+ import { isPrivateSession } from "../memory.js";
4
+ import { messageSeqs, parse, text } from "../util.js";
5
+ const MINUTE = 60000;
6
+
7
+ // Owns this part of the lifecycle; the facade keeps the shared runtime state.
8
+ export class LifePresence {
9
+ constructor(owner) {
10
+ this.owner = owner;
11
+ }
12
+ async considerPresence(now = this.owner.now()) {
13
+ const s = this.owner.settings();
14
+ if (!s.proactive || !this.owner.online()) return null;
15
+ if (this.owner.phase(now).key === "asleep") return null;
16
+ if (!this.owner.mind.budget.allows("inner", now)) return null;
17
+ const last = this.owner.repo.config("own-voice", {}).attemptedAt;
18
+ if (last && now - last < s.initiativeIntervalMinutes * MINUTE) return null;
19
+ const contacts = this.owner.initiative.contacts(now, { ready: true });
20
+ if (!contacts.length) return null;
21
+ const resting = this.owner.ownVoice.resting(now);
22
+ this.owner.repo.saveConfig("own-voice", {
23
+ ...this.owner.repo.config("own-voice", {}),
24
+ attemptedAt: now,
25
+ });
26
+ if (resting) return { status: "presence-silent", reason: resting };
27
+ this.owner.busy = true;
28
+ let formed;
29
+ try {
30
+ formed = await this.owner.ownVoice.form(now);
31
+ } finally {
32
+ this.owner.busy = false;
33
+ }
34
+ if (!formed.note?.share)
35
+ return {
36
+ status:
37
+ formed.status === "error" ? "presence-error" : "presence-silent",
38
+ reason: formed.reason,
39
+ };
40
+ // The thought already exists. Only now do we choose where it might fit;
41
+ // someone else's last question cannot become its reason for existing.
42
+ const contact = this.owner.initiative.chooseContact(
43
+ formed.note,
44
+ contacts,
45
+ this.owner.now(),
46
+ );
47
+ if (!contact)
48
+ return {
49
+ status: "presence-silent",
50
+ reason: "此刻没有适合分享这个念头的地方",
51
+ };
52
+ this.owner.mind.thoughts.planOutreach(formed.note.id, {
53
+ session: contact.session,
54
+ words: formed.note.words,
55
+ reason: formed.note.reason,
56
+ time: now,
57
+ });
58
+ const result = await this.owner.reachOut(this.owner.now());
59
+ return result
60
+ ? {
61
+ ...result,
62
+ status:
63
+ result.status === "outreach-declined"
64
+ ? "presence-silent"
65
+ : result.status.replace(/^outreach-/, "presence-"),
66
+ }
67
+ : {
68
+ status: "presence-deferred",
69
+ reason: "念头已留下,等待合适的交流空隙",
70
+ session: contact.session,
71
+ };
72
+ }
73
+ presenceCooling(session, now, s) {
74
+ const tried = this.owner.repo.config("presence", {});
75
+ return !!(
76
+ tried[session] &&
77
+ now - tried[session] < s.initiativeIntervalMinutes * MINUTE
78
+ );
79
+ }
80
+ markPresence(session, now) {
81
+ const tried = { ...this.owner.repo.config("presence", {}), [session]: now };
82
+ this.owner.repo.saveConfig("presence", tried);
83
+ }
84
+ wasHere(session, now, names, aside = null) {
85
+ const skipped =
86
+ aside ||
87
+ new Set(
88
+ this.owner.db
89
+ .prepare("SELECT seq FROM mind_unlived")
90
+ .all()
91
+ .map((row) => row.seq),
92
+ );
93
+ const lived = this.owner.db
94
+ .prepare(
95
+ "SELECT 1 FROM core_events WHERE session_id=? AND seq NOT IN (SELECT seq FROM mind_unlived) AND COALESCE(json_extract(payload,'$.simulated'),0)=0 AND (role='assistant' OR ?=1) LIMIT 1",
96
+ )
97
+ .get(session, +isPrivateSession(session));
98
+ if (lived) return true;
99
+ if (
100
+ this.owner.db
101
+ .prepare("SELECT 1 FROM mind_meetings WHERE session_id=? LIMIT 1")
102
+ .get(session)
103
+ )
104
+ return true;
105
+ return this.owner.repo
106
+ .recentEvents(session, 80, { simulated: false })
107
+ .some((m) => {
108
+ if (skipped.has(m.seq)) return false;
109
+ if (m.role === "assistant") return true;
110
+ const mentions = (m.mentions || []).map(String);
111
+ if (m.accountId && mentions.includes(String(m.accountId))) return true;
112
+ if (String(m.text || "").includes("@我")) return true;
113
+ return isNameCall(m.text, names);
114
+ });
115
+ }
116
+ async reachOut(now = this.owner.now()) {
117
+ const s = this.owner.settings();
118
+ if (!s.proactive || !this.owner.online()) return null;
119
+ if (this.owner.phase(now).key === "asleep") return null;
120
+ for (const t of this.owner.mind.thoughts.dueOutreach(now)) {
121
+ const session = t.outreach_session;
122
+ const audience = this.owner.initiative.audience(t, session, now);
123
+ if (!audience.allowed) {
124
+ this.owner.mind.thoughts.setOutreach(t.id, "skipped", audience.reason);
125
+ continue;
126
+ }
127
+ if (this.owner.initiative.recentlyToldAnotherGroup(t, now)) {
128
+ this.owner.mind.thoughts.setOutreach(t.id, "declined");
129
+ continue;
130
+ }
131
+ const block = this.owner.outreachBlocked(session, now, s);
132
+ if (block) {
133
+ if (block.permanent)
134
+ this.owner.mind.thoughts.setOutreach(t.id, "skipped");
135
+ else
136
+ this.owner.mind.thoughts.deferOutreach(
137
+ t.id,
138
+ block.reason,
139
+ block.retryAt,
140
+ );
141
+ continue;
142
+ }
143
+ this.owner.mind.thoughts.setOutreach(t.id, "sending");
144
+ this.owner.busy = true;
145
+ try {
146
+ const returned = this.owner.returnedSince(t, now);
147
+ const trace = await this.owner.chat.initiate(session, {
148
+ type: t.kind === "expression" ? "presence" : "outreach",
149
+ data: {
150
+ thought: t.content,
151
+ planned: t.outreach,
152
+ wantedBecause: t.outreach_reason,
153
+ expression: {
154
+ ref: `t:${t.id}`,
155
+ origin:
156
+ t.kind === "expression" ? "self_expression" : "earlier_wish",
157
+ formedAt: t.created,
158
+ thought: t.content,
159
+ words: t.outreach_draft?.length ? t.outreach_draft : [t.outreach],
160
+ reason: t.outreach_reason,
161
+ sources: t.sources,
162
+ audience,
163
+ sourceMaterial: this.owner.initiative.sourceMaterial(t, now),
164
+ },
165
+ initiative: this.owner.initiative.context(session, now),
166
+ ...(returned.length ? { returned } : {}),
167
+ },
168
+ });
169
+ if (!trace) {
170
+ this.owner.mind.thoughts.deferOutreach(
171
+ t.id,
172
+ "对话忙着,稍后重新看看",
173
+ now + MINUTE,
174
+ );
175
+ return {
176
+ status: "outreach-deferred",
177
+ reason: "愿望保留,等待对话空隙",
178
+ session,
179
+ };
180
+ }
181
+ this.owner.markPresence(session, now);
182
+ const uncertain =
183
+ trace &&
184
+ this.owner.db
185
+ .prepare(
186
+ "SELECT 1 FROM core_outbox WHERE trace_id=? AND status='uncertain'",
187
+ )
188
+ .get(trace.id);
189
+ if (
190
+ ["error", "stale"].includes(trace.status) &&
191
+ !trace.sent?.length &&
192
+ !uncertain &&
193
+ !this.owner.db
194
+ .prepare(
195
+ "SELECT 1 FROM core_outbox WHERE trace_id=? AND status IN ('sending','confirmed') LIMIT 1",
196
+ )
197
+ .get(trace.id)
198
+ ) {
199
+ this.owner.mind.thoughts.deferOutreach(
200
+ t.id,
201
+ trace.reason || "调用失败,稍后重新决定",
202
+ this.owner.now() + (trace.status === "stale" ? 1 : 15) * MINUTE,
203
+ );
204
+ return {
205
+ status: "outreach-deferred",
206
+ reason: "尚未发送,想说的话仍保留",
207
+ session,
208
+ };
209
+ }
210
+ const status = uncertain
211
+ ? "uncertain"
212
+ : trace?.status === "sent" || trace?.sent?.length
213
+ ? "sent"
214
+ : trace?.status === "silent"
215
+ ? "declined"
216
+ : "cancelled";
217
+ this.owner.mind.thoughts.setOutreach(
218
+ t.id,
219
+ status,
220
+ status === "declined" ? trace?.reason || "此刻不想聊这件事" : "",
221
+ );
222
+ return {
223
+ status: `outreach-${status}`,
224
+ reason: trace?.reason || "没能联系",
225
+ session,
226
+ };
227
+ } catch {
228
+ // Retry only when no delivery could have happened. An ambiguous
229
+ // network acknowledgement must never produce duplicate QQ messages.
230
+ const maybeSent = this.owner.db
231
+ .prepare(
232
+ "SELECT 1 FROM core_outbox WHERE session_id=? AND time>=? AND status IN ('sending','confirmed','uncertain') LIMIT 1",
233
+ )
234
+ .get(session, now);
235
+ if (!maybeSent) {
236
+ this.owner.mind.thoughts.deferOutreach(
237
+ t.id,
238
+ "调用未完成,稍后重新决定",
239
+ now + 15 * MINUTE,
240
+ );
241
+ return {
242
+ status: "outreach-deferred",
243
+ reason: "调用未完成,想说的话仍保留",
244
+ session,
245
+ };
246
+ }
247
+ this.owner.mind.thoughts.setOutreach(t.id, "uncertain");
248
+ return {
249
+ status: "outreach-uncertain",
250
+ reason: "主动联系的投递不确定,不再重试",
251
+ };
252
+ } finally {
253
+ this.owner.busy = false;
254
+ }
255
+ }
256
+ return null;
257
+ }
258
+ returnedSince(thought, now) {
259
+ const sources = parse(thought?.sources, []);
260
+ const ids = new Set();
261
+ const meetingIds = sources
262
+ .filter((ref) => String(ref).startsWith("g:"))
263
+ .map((ref) => String(ref).slice(2));
264
+ if (meetingIds.length) {
265
+ const rows = this.owner.db
266
+ .prepare(
267
+ `SELECT people, will_people FROM mind_meetings WHERE id IN (${meetingIds.map(() => "?").join(",")})`,
268
+ )
269
+ .all(...meetingIds);
270
+ for (const row of rows) {
271
+ const named = parse(row.will_people, []);
272
+ const who = named.length ? named : parse(row.people, []);
273
+ for (const id of who) ids.add(String(id));
274
+ }
275
+ }
276
+ const seqs = messageSeqs(sources);
277
+ if (seqs.length) {
278
+ const events = this.owner.db
279
+ .prepare(
280
+ `SELECT payload, role FROM core_events WHERE seq IN (${seqs.map(() => "?").join(",")})`,
281
+ )
282
+ .all(...seqs);
283
+ for (const row of events) {
284
+ if (row.role === "assistant") continue;
285
+ const payload = parse(row.payload, {});
286
+ if (payload.userId && payload.userId !== "bot")
287
+ ids.add(String(payload.userId));
288
+ }
289
+ }
290
+ const out = [];
291
+ for (const id of ids) {
292
+ const person = this.owner.mind.bonds.person(id, now);
293
+ if (!person?.seenAt || person.seenAt <= thought.created) continue;
294
+ out.push({
295
+ name: person.name || id,
296
+ since: agoLabel(now - person.seenAt),
297
+ });
298
+ if (out.length >= 3) break;
299
+ }
300
+ return out;
301
+ }
302
+ outreachBlocked(session, now, s) {
303
+ return this.owner.initiative.blocked(session, now, s);
304
+ }
305
+ }