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.
- package/LICENSE +21 -21
- package/docs/en/CHANGELOG.md +25 -16
- package/docs/en/chat-style.md +28 -9
- package/docs/en/guide.md +5 -5
- package/docs/en/her-life.md +2 -2
- package/docs/en/webui.md +1 -1
- package/docs/zh/CHANGELOG.md +25 -16
- package/docs/zh/chat-style.md +30 -9
- package/docs/zh/guide.md +5 -5
- package/docs/zh/her-life.md +2 -2
- package/docs/zh/webui.md +1 -1
- package/package.json +1 -1
- package/public/app/assets/{ChatsPage-DCYMkJ91.js → ChatsPage-CshdtTzV.js} +2 -2
- package/public/app/assets/ChatsPage-DfyPCS9N.css +1 -0
- package/public/app/assets/{Empty-CLyZkxR5.js → Empty-C7yzOBzy.js} +1 -1
- package/public/app/assets/{Empty-DIobYG3x.css → Empty-DmUB2-Bk.css} +1 -1
- package/public/app/assets/{Field-BpaRwLI_.js → Field-BjFv4Rfn.js} +1 -1
- package/public/app/assets/HeartPage-Bft9wZp5.css +1 -0
- package/public/app/assets/{HeartPage-BIOMTiwr.js → HeartPage-eqo6ru16.js} +1 -1
- package/public/app/assets/LifePage-BUi5euBF.css +1 -0
- package/public/app/assets/{LifePage-BCgM4JkR.js → LifePage-CN8GE5NK.js} +1 -1
- package/public/app/assets/{MemoryPage-vShvRqMt.js → MemoryPage-CCpMN4Wg.js} +1 -1
- package/public/app/assets/MemoryPage-cZvXfkXb.css +1 -0
- package/public/app/assets/{NaturePage-Cy9ptr4G.js → NaturePage-C0JqdBTR.js} +2 -2
- package/public/app/assets/NaturePage-CHkpb5NK.css +1 -0
- package/public/app/assets/PeoplePage-BTU0H1pM.css +1 -0
- package/public/app/assets/{PeoplePage-CUZekB2E.js → PeoplePage-BXGBbO4u.js} +1 -1
- package/public/app/assets/{PluginsPage-1RM4rmJP.js → PluginsPage-Dksp5AV5.js} +1 -1
- package/public/app/assets/{SystemPage-BI2CB5Ig.css → SystemPage-BenIyTvk.css} +1 -1
- package/public/app/assets/{SystemPage-BeT5TmIV.js → SystemPage-Cd7aa8eE.js} +1 -1
- package/public/app/assets/TimePage-BOms_n4I.css +1 -0
- package/public/app/assets/{TimePage-taTmPMM8.js → TimePage-Bs_zImb4.js} +2 -2
- package/public/app/assets/{index-DY5AM0Hz.css → index-BHZcHUMh.css} +1 -1
- package/public/app/assets/{index-DdYwC5ZU.js → index-CyRtYxP3.js} +3 -3
- package/public/app/assets/{knowledge-DJmfu2KN.js → knowledge-BJOIyuJo.js} +1 -1
- package/public/app/assets/{media-BEtnE0Gv.js → media-Bwtx8blz.js} +1 -1
- package/public/app/assets/{mind-Dtiw0-QK.js → mind-DtTVrW6Z.js} +1 -1
- package/public/app/index.html +5 -6
- package/public/guide.en.html +3 -3
- package/public/guide.html +3 -3
- package/scripts/import-data.js +128 -128
- package/scripts/proxy-config.js +167 -167
- package/scripts/update-instance.js +201 -201
- package/server/channels/adapters.js +27 -27
- package/server/channels/onebot/adapter.js +288 -288
- package/server/channels/qqbot/adapter.js +253 -253
- package/server/channels/qqbot/api.js +199 -199
- package/server/channels/qqbot/credentials.js +11 -11
- package/server/channels/qqbot/gateway.js +274 -274
- package/server/channels/qqbot/index.js +389 -389
- package/server/channels/segments.js +40 -40
- package/server/channels/session-key.js +153 -153
- package/server/channels/state.js +42 -42
- package/server/core/api.js +564 -564
- package/server/core/context-builder.js +288 -288
- package/server/core/context-compactor.js +454 -454
- package/server/core/conversation-cues.js +365 -325
- package/server/core/conversation-grounding.js +178 -0
- package/server/core/conversation-manager.js +87 -87
- package/server/core/dialogue-context.js +52 -0
- package/server/core/feedback.js +8 -8
- package/server/core/group-style.js +72 -72
- package/server/core/initiative-context.js +66 -66
- package/server/core/input-budget.js +32 -32
- package/server/core/local-demo.js +98 -98
- package/server/core/message-manager.js +118 -118
- package/server/core/message-scheduler.js +78 -78
- package/server/core/model-json.js +190 -190
- package/server/core/model-presets.js +900 -900
- package/server/core/name-call.js +29 -29
- package/server/core/network.js +157 -157
- package/server/core/person-name.js +22 -22
- package/server/core/persona-manager.js +324 -269
- package/server/core/reply-delivery.js +472 -404
- package/server/core/reply-target-resolver.js +50 -50
- package/server/core/response-generator.js +58 -50
- package/server/core/response-validator.js +30 -27
- package/server/core/retired-prompts.js +51 -51
- package/server/core/sessions.js +109 -109
- package/server/core/speaker-names.js +92 -92
- package/server/core/topic-tracker.js +65 -65
- package/server/core/turn.js +42 -8
- package/server/http.js +18 -18
- package/server/i18n/decisions.js +209 -209
- package/server/i18n/index.js +144 -144
- package/server/i18n/labels.js +0 -5
- package/server/i18n/models.js +111 -111
- package/server/knowledge/api.js +342 -342
- package/server/knowledge/embedding-profile.js +114 -114
- package/server/knowledge/index.js +13 -13
- package/server/knowledge/retrieval.js +93 -93
- package/server/knowledge/schema.js +158 -158
- package/server/knowledge/vector-index.js +90 -90
- package/server/mind/affect.js +265 -265
- package/server/mind/attention.js +200 -200
- package/server/mind/bonds.js +565 -565
- package/server/mind/clock.js +83 -83
- package/server/mind/context-selection.js +16 -4
- package/server/mind/continuity.js +562 -562
- package/server/mind/conversation-origin.js +175 -175
- package/server/mind/days.js +210 -210
- package/server/mind/evidence-read.js +45 -45
- package/server/mind/faces.js +373 -373
- package/server/mind/guard.js +68 -60
- package/server/mind/initiative.js +338 -338
- package/server/mind/life/activities.js +21 -21
- package/server/mind/life/context.js +330 -330
- package/server/mind/life/growth.js +261 -261
- package/server/mind/life/presence.js +305 -305
- package/server/mind/meetings.js +1069 -1069
- package/server/mind/memory-policy.js +48 -48
- package/server/mind/memory.js +888 -888
- package/server/mind/nature.js +358 -358
- package/server/mind/novelty.js +19 -19
- package/server/mind/periods.js +118 -118
- package/server/mind/reading.js +120 -120
- package/server/mind/relationship-context.js +71 -71
- package/server/mind/relationships.js +390 -390
- package/server/mind/salience.js +201 -201
- package/server/mind/schema.js +567 -567
- package/server/mind/self.js +496 -484
- package/server/mind/thoughts.js +274 -274
- package/server/mind/time/agenda.js +242 -242
- package/server/mind/time/api.js +200 -200
- package/server/mind/time/attention.js +34 -34
- package/server/mind/time/delivery-links.js +130 -130
- package/server/mind/time/games.js +7 -3
- package/server/mind/time/index.js +3 -0
- package/server/mind/time/model-material.js +77 -77
- package/server/mind/time/presentation.js +35 -51
- package/server/mind/time/schedule.js +83 -83
- package/server/mind/time/schema.js +84 -84
- package/server/mind/time/search.js +383 -383
- package/server/mind/time/sharing.js +472 -472
- package/server/mind/time/task-links.js +311 -311
- package/server/mind/time/works.js +438 -432
- package/server/mind/traits.js +191 -191
- package/server/mind/view.js +43 -6
- package/server/runtime/installation.js +116 -116
- package/server/runtime/update-manager.js +140 -140
- package/server/storage/data-package.js +298 -298
- package/server/storage/data-transfer-api.js +50 -50
- package/server/storage/data-transfer-worker.js +18 -18
- package/server/storage/data-transfer.js +416 -416
- package/server/studio/display-names.js +139 -139
- package/server/studio/management.js +102 -102
- package/server/studio/updates.js +144 -144
- package/server/version.js +10 -10
- package/public/app/assets/ChatsPage-CFMVozoG.css +0 -1
- package/public/app/assets/HeartPage-C-2jvgzF.css +0 -1
- package/public/app/assets/LifePage-CG1OHbRK.css +0 -1
- package/public/app/assets/MemoryPage-C_Bu4rfb.css +0 -1
- package/public/app/assets/NaturePage-CPZlImT0.css +0 -1
- package/public/app/assets/PeoplePage-3RRUITDz.css +0 -1
- 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
|
+
}
|