luckytri 1.0.4 → 1.0.6

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 (156) hide show
  1. package/LICENSE +21 -21
  2. package/README.en.md +2 -0
  3. package/README.md +2 -0
  4. package/docs/en/CHANGELOG.md +20 -13
  5. package/docs/en/guide.md +7 -5
  6. package/docs/en/her-life.md +2 -2
  7. package/docs/en/webui.md +1 -1
  8. package/docs/zh/CHANGELOG.md +20 -13
  9. package/docs/zh/guide.md +7 -5
  10. package/docs/zh/her-life.md +2 -2
  11. package/docs/zh/webui.md +1 -1
  12. package/package.json +2 -1
  13. package/public/app/assets/ChatsPage-CFMVozoG.css +1 -0
  14. package/public/app/assets/{ChatsPage-CshdtTzV.js → ChatsPage-DCYMkJ91.js} +2 -2
  15. package/public/app/assets/{Empty-C7yzOBzy.js → Empty-CLyZkxR5.js} +1 -1
  16. package/public/app/assets/{Empty-DmUB2-Bk.css → Empty-DIobYG3x.css} +1 -1
  17. package/public/app/assets/{Field-BjFv4Rfn.js → Field-BpaRwLI_.js} +1 -1
  18. package/public/app/assets/{HeartPage-eqo6ru16.js → HeartPage-BIOMTiwr.js} +1 -1
  19. package/public/app/assets/HeartPage-C-2jvgzF.css +1 -0
  20. package/public/app/assets/{LifePage-CN8GE5NK.js → LifePage-BCgM4JkR.js} +1 -1
  21. package/public/app/assets/LifePage-CG1OHbRK.css +1 -0
  22. package/public/app/assets/MemoryPage-C_Bu4rfb.css +1 -0
  23. package/public/app/assets/{MemoryPage-CCpMN4Wg.js → MemoryPage-vShvRqMt.js} +1 -1
  24. package/public/app/assets/NaturePage-CPZlImT0.css +1 -0
  25. package/public/app/assets/{NaturePage-C0JqdBTR.js → NaturePage-Cy9ptr4G.js} +2 -2
  26. package/public/app/assets/PeoplePage-3RRUITDz.css +1 -0
  27. package/public/app/assets/{PeoplePage-BXGBbO4u.js → PeoplePage-CUZekB2E.js} +1 -1
  28. package/public/app/assets/{PluginsPage-Dksp5AV5.js → PluginsPage-1RM4rmJP.js} +1 -1
  29. package/public/app/assets/{SystemPage-BenIyTvk.css → SystemPage-BI2CB5Ig.css} +1 -1
  30. package/public/app/assets/{SystemPage-Cd7aa8eE.js → SystemPage-BeT5TmIV.js} +1 -1
  31. package/public/app/assets/TimePage-C-guwPk2.css +1 -0
  32. package/public/app/assets/{TimePage-Bs_zImb4.js → TimePage-taTmPMM8.js} +2 -2
  33. package/public/app/assets/{index-BHZcHUMh.css → index-DY5AM0Hz.css} +1 -1
  34. package/public/app/assets/{index-CyRtYxP3.js → index-DdYwC5ZU.js} +3 -3
  35. package/public/app/assets/{knowledge-BJOIyuJo.js → knowledge-DJmfu2KN.js} +1 -1
  36. package/public/app/assets/{media-Bwtx8blz.js → media-BEtnE0Gv.js} +1 -1
  37. package/public/app/assets/{mind-DtTVrW6Z.js → mind-Dtiw0-QK.js} +1 -1
  38. package/public/app/index.html +6 -5
  39. package/public/guide.en.html +11 -3
  40. package/public/guide.html +6 -3
  41. package/public/tutorial.en.md +2 -0
  42. package/public/tutorial.md +2 -0
  43. package/scripts/import-data.js +128 -128
  44. package/scripts/proxy-config.js +167 -167
  45. package/scripts/service-supervisor.js +50 -0
  46. package/scripts/start-server.js +71 -16
  47. package/scripts/update-instance.js +201 -201
  48. package/server/channels/adapters.js +27 -27
  49. package/server/channels/onebot/adapter.js +288 -288
  50. package/server/channels/qqbot/adapter.js +253 -253
  51. package/server/channels/qqbot/api.js +199 -199
  52. package/server/channels/qqbot/credentials.js +11 -11
  53. package/server/channels/qqbot/gateway.js +274 -274
  54. package/server/channels/qqbot/index.js +389 -389
  55. package/server/channels/segments.js +40 -40
  56. package/server/channels/session-key.js +153 -153
  57. package/server/channels/state.js +42 -42
  58. package/server/core/api.js +564 -564
  59. package/server/core/context-builder.js +288 -288
  60. package/server/core/context-compactor.js +454 -454
  61. package/server/core/conversation-cues.js +325 -325
  62. package/server/core/conversation-manager.js +87 -87
  63. package/server/core/feedback.js +8 -8
  64. package/server/core/group-style.js +72 -72
  65. package/server/core/initiative-context.js +66 -66
  66. package/server/core/input-budget.js +32 -32
  67. package/server/core/local-demo.js +98 -98
  68. package/server/core/message-manager.js +118 -118
  69. package/server/core/message-scheduler.js +78 -78
  70. package/server/core/model-json.js +190 -190
  71. package/server/core/model-presets.js +900 -900
  72. package/server/core/name-call.js +29 -29
  73. package/server/core/network.js +157 -157
  74. package/server/core/person-name.js +22 -22
  75. package/server/core/persona-manager.js +2 -1
  76. package/server/core/reply-delivery.js +404 -403
  77. package/server/core/reply-target-resolver.js +50 -50
  78. package/server/core/response-generator.js +50 -50
  79. package/server/core/response-validator.js +14 -2
  80. package/server/core/retired-prompts.js +51 -51
  81. package/server/core/sessions.js +109 -109
  82. package/server/core/speaker-names.js +92 -92
  83. package/server/core/topic-tracker.js +65 -65
  84. package/server/http.js +18 -18
  85. package/server/i18n/decisions.js +209 -206
  86. package/server/i18n/errors.js +4 -0
  87. package/server/i18n/index.js +144 -144
  88. package/server/i18n/models.js +111 -111
  89. package/server/knowledge/api.js +342 -342
  90. package/server/knowledge/embedding-profile.js +114 -114
  91. package/server/knowledge/index.js +13 -13
  92. package/server/knowledge/retrieval.js +93 -93
  93. package/server/knowledge/schema.js +158 -158
  94. package/server/knowledge/vector-index.js +90 -90
  95. package/server/mind/affect.js +265 -265
  96. package/server/mind/attention.js +200 -200
  97. package/server/mind/bonds.js +565 -565
  98. package/server/mind/clock.js +83 -83
  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 +60 -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 -323
  108. package/server/mind/life/diary.js +20 -0
  109. package/server/mind/life/growth.js +261 -254
  110. package/server/mind/life/index.js +10 -2
  111. package/server/mind/life/presence.js +305 -305
  112. package/server/mind/meetings.js +1069 -1069
  113. package/server/mind/memory-policy.js +48 -48
  114. package/server/mind/memory.js +888 -888
  115. package/server/mind/nature.js +358 -358
  116. package/server/mind/novelty.js +19 -19
  117. package/server/mind/periods.js +118 -118
  118. package/server/mind/reading.js +120 -120
  119. package/server/mind/relationship-context.js +71 -0
  120. package/server/mind/relationships.js +390 -279
  121. package/server/mind/salience.js +201 -201
  122. package/server/mind/schema.js +567 -567
  123. package/server/mind/self.js +484 -484
  124. package/server/mind/thoughts.js +274 -274
  125. package/server/mind/time/agenda.js +242 -242
  126. package/server/mind/time/api.js +200 -200
  127. package/server/mind/time/attention.js +34 -34
  128. package/server/mind/time/delivery-links.js +130 -130
  129. package/server/mind/time/model-material.js +77 -77
  130. package/server/mind/time/presentation.js +51 -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 +432 -432
  137. package/server/mind/traits.js +191 -191
  138. package/server/mind/view.js +9 -1
  139. package/server/runtime/installation.js +116 -116
  140. package/server/runtime/update-execution.js +2 -1
  141. package/server/runtime/update-manager.js +140 -140
  142. package/server/storage/data-package.js +298 -298
  143. package/server/storage/data-transfer-api.js +50 -50
  144. package/server/storage/data-transfer-worker.js +18 -18
  145. package/server/storage/data-transfer.js +416 -416
  146. package/server/studio/display-names.js +139 -139
  147. package/server/studio/management.js +102 -102
  148. package/server/studio/updates.js +144 -144
  149. package/server/version.js +10 -10
  150. package/public/app/assets/ChatsPage-DfyPCS9N.css +0 -1
  151. package/public/app/assets/HeartPage-Bft9wZp5.css +0 -1
  152. package/public/app/assets/LifePage-BUi5euBF.css +0 -1
  153. package/public/app/assets/MemoryPage-cZvXfkXb.css +0 -1
  154. package/public/app/assets/NaturePage-CHkpb5NK.css +0 -1
  155. package/public/app/assets/PeoplePage-BTU0H1pM.css +0 -1
  156. package/public/app/assets/TimePage-BOms_n4I.css +0 -1
@@ -1,311 +1,311 @@
1
- import { evidence, parse, text } from "../util.js";
2
- import { taskIntent } from "./intent.js";
3
- import { deadlineAt } from "./schedule.js";
4
-
5
- export class TaskLinks {
6
- constructor(tasks) {
7
- this.tasks = tasks;
8
- this.time = tasks.time;
9
- this.db = tasks.db;
10
- for (const row of this.db
11
- .prepare("SELECT id,sources FROM mind_time_tasks")
12
- .all())
13
- for (const ref of parse(row.sources, []))
14
- this.db
15
- .prepare(
16
- "INSERT OR IGNORE INTO mind_time_task_aliases(kind,alias_id,task_id,created) VALUES ('source',?,?,0)",
17
- )
18
- .run(row.id + ":" + ref, row.id);
19
- for (const task of this.db
20
- .prepare("SELECT * FROM mind_time_tasks WHERE intent_key IS NULL")
21
- .all()) {
22
- const refs = parse(task.sources, []),
23
- intent = taskIntent(this.db, { ...task, sources: refs });
24
- this.db
25
- .prepare("UPDATE mind_time_tasks SET intent_key=? WHERE id=?")
26
- .run(intent.key, task.id);
27
- }
28
- for (const row of this.db
29
- .prepare(
30
- "SELECT t.id,t.due_at FROM mind_time_tasks t JOIN mind_anticipations a ON a.id=t.anticipation_id WHERE a.due_precision='day' AND t.due_at=a.due_at",
31
- )
32
- .all())
33
- this.db
34
- .prepare(
35
- "UPDATE mind_time_tasks SET due_at=?,due_precision='day' WHERE id=?",
36
- )
37
- .run(deadlineAt(row.due_at, "day", this.time.mind.timeZone()), row.id);
38
- }
39
- canonical(id) {
40
- const seen = new Set();
41
- while (id && !seen.has(id)) {
42
- seen.add(id);
43
- const alias = this.db
44
- .prepare(
45
- "SELECT task_id FROM mind_time_task_aliases WHERE kind='task' AND alias_id=?",
46
- )
47
- .get(id);
48
- if (!alias || alias.task_id === id) return id;
49
- id = alias.task_id;
50
- }
51
- return id;
52
- }
53
- find(input) {
54
- const intent = taskIntent(this.db, input);
55
- if (!intent.key) return null;
56
- const row = this.db
57
- .prepare(
58
- "SELECT id FROM mind_time_tasks WHERE intent_key=? AND json_extract(checkpoint,'$.mergedInto') IS NULL ORDER BY CASE state WHEN 'abandoned' THEN 2 WHEN 'done' THEN 1 ELSE 0 END,created LIMIT 1",
59
- )
60
- .get(intent.key);
61
- return row ? this.tasks.get(row.id) : null;
62
- }
63
- link(kind, alias, id, now = this.time.now()) {
64
- if (alias)
65
- this.db
66
- .prepare(
67
- "INSERT INTO mind_time_task_aliases(kind,alias_id,task_id,created) VALUES (?,?,?,?) ON CONFLICT(kind,alias_id) DO UPDATE SET task_id=excluded.task_id",
68
- )
69
- .run(kind, alias, id, now);
70
- }
71
- byAnticipation(id) {
72
- return (
73
- this.db
74
- .prepare(
75
- "SELECT task_id id FROM mind_time_task_aliases WHERE kind='anticipation' AND alias_id=?",
76
- )
77
- .get(id) ||
78
- this.db
79
- .prepare("SELECT id FROM mind_time_tasks WHERE anticipation_id=?")
80
- .get(id)
81
- );
82
- }
83
- anticipations(task) {
84
- return [
85
- ...new Set(
86
- [
87
- task.anticipation_id,
88
- ...this.db
89
- .prepare(
90
- "SELECT alias_id FROM mind_time_task_aliases WHERE kind='anticipation' AND task_id=?",
91
- )
92
- .all(task.id)
93
- .map((a) => a.alias_id),
94
- ].filter(Boolean),
95
- ),
96
- ];
97
- }
98
- close(task, reason, now = this.time.now()) {
99
- for (const id of this.anticipations(task))
100
- this.time.mind.anticipations.close(id, {
101
- status: "let_go",
102
- note: reason,
103
- sources: task.sources,
104
- time: now,
105
- });
106
- }
107
- mergeSource(
108
- task,
109
- {
110
- sources = [],
111
- kind,
112
- subject,
113
- dueAt,
114
- duePrecision = "time",
115
- anticipationId,
116
- origin = "capture",
117
- },
118
- now,
119
- ) {
120
- const fresh = evidence(sources).filter(
121
- (s) =>
122
- !task.sources.includes(s) &&
123
- !this.db
124
- .prepare(
125
- "SELECT 1 FROM mind_time_task_aliases WHERE kind='source' AND alias_id=?",
126
- )
127
- .get(task.id + ":" + s),
128
- ),
129
- all = evidence([...task.sources, ...sources]);
130
- if (anticipationId) this.link("anticipation", anticipationId, task.id, now);
131
- if (!fresh.length) return;
132
- for (const ref of fresh)
133
- this.link("source", task.id + ":" + ref, task.id, now);
134
- const inputDue = deadlineAt(dueAt, duePrecision, this.time.mind.timeZone());
135
- const closed = ["done", "abandoned"].includes(task.state),
136
- contract = task.checkpoint.contract;
137
- const changesDeadline =
138
- !closed && !contract?.originalGoal && inputDue !== null;
139
- this.db
140
- .prepare(
141
- "UPDATE mind_time_tasks SET sources=?,updated=?,kind=?,subject=COALESCE(subject,?),due_at=?,due_precision=? WHERE id=?",
142
- )
143
- .run(
144
- JSON.stringify(all),
145
- now,
146
- kind === "promise" && !contract?.originalGoal ? "promise" : task.kind,
147
- subject ? String(subject) : null,
148
- changesDeadline ? inputDue : task.due_at,
149
- changesDeadline ? duePrecision : task.due_precision,
150
- task.id,
151
- );
152
- this.time.event(
153
- task.id,
154
- closed ? "source-linked" : "reaffirmed",
155
- closed
156
- ? "归回原事项,保留已完成或已放下的决定"
157
- : "同一未完成事项补充来源,不新建待办",
158
- {
159
- sources: fresh,
160
- origin,
161
- deadlineChanged: changesDeadline,
162
- previousDue: task.due_at,
163
- },
164
- now,
165
- );
166
- }
167
- consolidate(
168
- ids,
169
- { restore = false, title, why, kind, subject, contract, schedule } = {},
170
- now = this.time.now(),
171
- ) {
172
- const rows = [...new Set(ids)]
173
- .map((id) => this.tasks.get(this.canonical(id)))
174
- .filter(Boolean);
175
- if (!rows.length) throw Error("没有可整理的任务");
176
- const unique = [...new Map(rows.map((row) => [row.id, row])).values()];
177
- const keys = new Set(unique.map((t) => t.intent_key));
178
- if (unique.length > 1 && (keys.size !== 1 || keys.has(null)))
179
- throw Error("任务目标或权限范围不同,不能合并");
180
- const leader = [...unique].sort(
181
- (a, b) =>
182
- Number(!!b.work_id) - Number(!!a.work_id) ||
183
- Number(b.kind === "promise") - Number(a.kind === "promise") ||
184
- a.created - b.created,
185
- )[0];
186
- if (restore && leader.state === "done")
187
- throw Error("已有成果的任务不能改成未执行");
188
- this.db.exec("SAVEPOINT consolidate_tasks");
189
- try {
190
- const refs = evidence(unique.flatMap((t) => t.sources));
191
- const checkpoint = {
192
- ...leader.checkpoint,
193
- originalTask: leader.checkpoint.originalTask || {
194
- title: leader.title,
195
- why: leader.why,
196
- kind: leader.kind,
197
- state: leader.state,
198
- readyAt: leader.ready_at,
199
- dueAt: leader.due_at,
200
- sources: leader.sources,
201
- },
202
- ...(kind === "suggestion" && restore
203
- ? {
204
- suggestion: {
205
- origin: "admin",
206
- action: "reference-plan",
207
- idea: title,
208
- accepted: null,
209
- },
210
- }
211
- : {}),
212
- mergedHistory: [
213
- ...new Set([
214
- ...(leader.checkpoint.mergedHistory || []),
215
- ...unique.filter((t) => t.id !== leader.id).map((t) => t.id),
216
- ]),
217
- ],
218
- previousDeadlines: unique.map((t) => ({
219
- task: t.id,
220
- at: t.due_at,
221
- precision: t.due_precision,
222
- })),
223
- ...(contract ? { contract } : {}),
224
- ...(schedule ? { schedule } : {}),
225
- };
226
- for (const task of unique) {
227
- this.time.stopSpan(task, now);
228
- for (const a of this.anticipations(task)) {
229
- this.link("anticipation", a, leader.id, now);
230
- this.time.mind.anticipations.close(a, {
231
- status: "let_go",
232
- note: "重复事项已整理为一个执行计划;原约定未因此兑现",
233
- sources: task.sources,
234
- time: now,
235
- });
236
- }
237
- this.link("task", task.id, leader.id, now);
238
- if (task.id !== leader.id) {
239
- this.db
240
- .prepare(
241
- "UPDATE mind_time_tasks SET state='abandoned',updated=?,wait_reason=?,lease=NULL,lease_at=NULL,revision=revision+1,checkpoint=? WHERE id=?",
242
- )
243
- .run(
244
- now,
245
- "重复事项已合并,原记录保留",
246
- JSON.stringify({ ...task.checkpoint, mergedInto: leader.id }),
247
- task.id,
248
- );
249
- this.time.event(
250
- task.id,
251
- "merged",
252
- "归入唯一事项,保留原文与时间",
253
- { taskId: leader.id },
254
- now,
255
- );
256
- }
257
- }
258
- const proposed = schedule?.proposedAt || now,
259
- wait =
260
- contract?.mode === "reference" ? this.time.search.ready(now) : "";
261
- const state = restore
262
- ? wait
263
- ? "waiting"
264
- : proposed > now
265
- ? "scheduled"
266
- : "todo"
267
- : leader.state;
268
- this.db
269
- .prepare(
270
- "UPDATE mind_time_tasks SET title=?,why=?,kind=?,subject=?,sources=?,checkpoint=?,state=?,ready_at=?,next_step=?,due_at=?,due_precision=?,updated=?,wait_reason=?,lease=NULL,lease_at=NULL,revision=revision+1 WHERE id=?",
271
- )
272
- .run(
273
- text(title || leader.title, 240),
274
- text(why || leader.why, 300),
275
- kind || leader.kind,
276
- subject || leader.subject,
277
- JSON.stringify(refs),
278
- JSON.stringify(checkpoint),
279
- state,
280
- restore ? proposed : leader.ready_at,
281
- restore ? proposed : leader.next_step,
282
- restore ? null : leader.due_at,
283
- restore ? "none" : leader.due_precision,
284
- now,
285
- restore ? wait : leader.wait_reason,
286
- leader.id,
287
- );
288
- if (title) {
289
- const updated = this.tasks.get(leader.id),
290
- intent = taskIntent(this.db, updated);
291
- this.db
292
- .prepare("UPDATE mind_time_tasks SET intent_key=? WHERE id=?")
293
- .run(intent.key, leader.id);
294
- }
295
- this.time.event(
296
- leader.id,
297
- "consolidated",
298
- restore
299
- ? "根据管理台明确请求重新整理并安排"
300
- : "合并重复来源,保留任务决定",
301
- { from: unique.map((t) => t.id), restore, schedule, contract },
302
- now,
303
- );
304
- this.db.exec("RELEASE consolidate_tasks");
305
- return this.tasks.get(leader.id);
306
- } catch (error) {
307
- this.db.exec("ROLLBACK TO consolidate_tasks; RELEASE consolidate_tasks");
308
- throw error;
309
- }
310
- }
311
- }
1
+ import { evidence, parse, text } from "../util.js";
2
+ import { taskIntent } from "./intent.js";
3
+ import { deadlineAt } from "./schedule.js";
4
+
5
+ export class TaskLinks {
6
+ constructor(tasks) {
7
+ this.tasks = tasks;
8
+ this.time = tasks.time;
9
+ this.db = tasks.db;
10
+ for (const row of this.db
11
+ .prepare("SELECT id,sources FROM mind_time_tasks")
12
+ .all())
13
+ for (const ref of parse(row.sources, []))
14
+ this.db
15
+ .prepare(
16
+ "INSERT OR IGNORE INTO mind_time_task_aliases(kind,alias_id,task_id,created) VALUES ('source',?,?,0)",
17
+ )
18
+ .run(row.id + ":" + ref, row.id);
19
+ for (const task of this.db
20
+ .prepare("SELECT * FROM mind_time_tasks WHERE intent_key IS NULL")
21
+ .all()) {
22
+ const refs = parse(task.sources, []),
23
+ intent = taskIntent(this.db, { ...task, sources: refs });
24
+ this.db
25
+ .prepare("UPDATE mind_time_tasks SET intent_key=? WHERE id=?")
26
+ .run(intent.key, task.id);
27
+ }
28
+ for (const row of this.db
29
+ .prepare(
30
+ "SELECT t.id,t.due_at FROM mind_time_tasks t JOIN mind_anticipations a ON a.id=t.anticipation_id WHERE a.due_precision='day' AND t.due_at=a.due_at",
31
+ )
32
+ .all())
33
+ this.db
34
+ .prepare(
35
+ "UPDATE mind_time_tasks SET due_at=?,due_precision='day' WHERE id=?",
36
+ )
37
+ .run(deadlineAt(row.due_at, "day", this.time.mind.timeZone()), row.id);
38
+ }
39
+ canonical(id) {
40
+ const seen = new Set();
41
+ while (id && !seen.has(id)) {
42
+ seen.add(id);
43
+ const alias = this.db
44
+ .prepare(
45
+ "SELECT task_id FROM mind_time_task_aliases WHERE kind='task' AND alias_id=?",
46
+ )
47
+ .get(id);
48
+ if (!alias || alias.task_id === id) return id;
49
+ id = alias.task_id;
50
+ }
51
+ return id;
52
+ }
53
+ find(input) {
54
+ const intent = taskIntent(this.db, input);
55
+ if (!intent.key) return null;
56
+ const row = this.db
57
+ .prepare(
58
+ "SELECT id FROM mind_time_tasks WHERE intent_key=? AND json_extract(checkpoint,'$.mergedInto') IS NULL ORDER BY CASE state WHEN 'abandoned' THEN 2 WHEN 'done' THEN 1 ELSE 0 END,created LIMIT 1",
59
+ )
60
+ .get(intent.key);
61
+ return row ? this.tasks.get(row.id) : null;
62
+ }
63
+ link(kind, alias, id, now = this.time.now()) {
64
+ if (alias)
65
+ this.db
66
+ .prepare(
67
+ "INSERT INTO mind_time_task_aliases(kind,alias_id,task_id,created) VALUES (?,?,?,?) ON CONFLICT(kind,alias_id) DO UPDATE SET task_id=excluded.task_id",
68
+ )
69
+ .run(kind, alias, id, now);
70
+ }
71
+ byAnticipation(id) {
72
+ return (
73
+ this.db
74
+ .prepare(
75
+ "SELECT task_id id FROM mind_time_task_aliases WHERE kind='anticipation' AND alias_id=?",
76
+ )
77
+ .get(id) ||
78
+ this.db
79
+ .prepare("SELECT id FROM mind_time_tasks WHERE anticipation_id=?")
80
+ .get(id)
81
+ );
82
+ }
83
+ anticipations(task) {
84
+ return [
85
+ ...new Set(
86
+ [
87
+ task.anticipation_id,
88
+ ...this.db
89
+ .prepare(
90
+ "SELECT alias_id FROM mind_time_task_aliases WHERE kind='anticipation' AND task_id=?",
91
+ )
92
+ .all(task.id)
93
+ .map((a) => a.alias_id),
94
+ ].filter(Boolean),
95
+ ),
96
+ ];
97
+ }
98
+ close(task, reason, now = this.time.now()) {
99
+ for (const id of this.anticipations(task))
100
+ this.time.mind.anticipations.close(id, {
101
+ status: "let_go",
102
+ note: reason,
103
+ sources: task.sources,
104
+ time: now,
105
+ });
106
+ }
107
+ mergeSource(
108
+ task,
109
+ {
110
+ sources = [],
111
+ kind,
112
+ subject,
113
+ dueAt,
114
+ duePrecision = "time",
115
+ anticipationId,
116
+ origin = "capture",
117
+ },
118
+ now,
119
+ ) {
120
+ const fresh = evidence(sources).filter(
121
+ (s) =>
122
+ !task.sources.includes(s) &&
123
+ !this.db
124
+ .prepare(
125
+ "SELECT 1 FROM mind_time_task_aliases WHERE kind='source' AND alias_id=?",
126
+ )
127
+ .get(task.id + ":" + s),
128
+ ),
129
+ all = evidence([...task.sources, ...sources]);
130
+ if (anticipationId) this.link("anticipation", anticipationId, task.id, now);
131
+ if (!fresh.length) return;
132
+ for (const ref of fresh)
133
+ this.link("source", task.id + ":" + ref, task.id, now);
134
+ const inputDue = deadlineAt(dueAt, duePrecision, this.time.mind.timeZone());
135
+ const closed = ["done", "abandoned"].includes(task.state),
136
+ contract = task.checkpoint.contract;
137
+ const changesDeadline =
138
+ !closed && !contract?.originalGoal && inputDue !== null;
139
+ this.db
140
+ .prepare(
141
+ "UPDATE mind_time_tasks SET sources=?,updated=?,kind=?,subject=COALESCE(subject,?),due_at=?,due_precision=? WHERE id=?",
142
+ )
143
+ .run(
144
+ JSON.stringify(all),
145
+ now,
146
+ kind === "promise" && !contract?.originalGoal ? "promise" : task.kind,
147
+ subject ? String(subject) : null,
148
+ changesDeadline ? inputDue : task.due_at,
149
+ changesDeadline ? duePrecision : task.due_precision,
150
+ task.id,
151
+ );
152
+ this.time.event(
153
+ task.id,
154
+ closed ? "source-linked" : "reaffirmed",
155
+ closed
156
+ ? "归回原事项,保留已完成或已放下的决定"
157
+ : "同一未完成事项补充来源,不新建待办",
158
+ {
159
+ sources: fresh,
160
+ origin,
161
+ deadlineChanged: changesDeadline,
162
+ previousDue: task.due_at,
163
+ },
164
+ now,
165
+ );
166
+ }
167
+ consolidate(
168
+ ids,
169
+ { restore = false, title, why, kind, subject, contract, schedule } = {},
170
+ now = this.time.now(),
171
+ ) {
172
+ const rows = [...new Set(ids)]
173
+ .map((id) => this.tasks.get(this.canonical(id)))
174
+ .filter(Boolean);
175
+ if (!rows.length) throw Error("没有可整理的任务");
176
+ const unique = [...new Map(rows.map((row) => [row.id, row])).values()];
177
+ const keys = new Set(unique.map((t) => t.intent_key));
178
+ if (unique.length > 1 && (keys.size !== 1 || keys.has(null)))
179
+ throw Error("任务目标或权限范围不同,不能合并");
180
+ const leader = [...unique].sort(
181
+ (a, b) =>
182
+ Number(!!b.work_id) - Number(!!a.work_id) ||
183
+ Number(b.kind === "promise") - Number(a.kind === "promise") ||
184
+ a.created - b.created,
185
+ )[0];
186
+ if (restore && leader.state === "done")
187
+ throw Error("已有成果的任务不能改成未执行");
188
+ this.db.exec("SAVEPOINT consolidate_tasks");
189
+ try {
190
+ const refs = evidence(unique.flatMap((t) => t.sources));
191
+ const checkpoint = {
192
+ ...leader.checkpoint,
193
+ originalTask: leader.checkpoint.originalTask || {
194
+ title: leader.title,
195
+ why: leader.why,
196
+ kind: leader.kind,
197
+ state: leader.state,
198
+ readyAt: leader.ready_at,
199
+ dueAt: leader.due_at,
200
+ sources: leader.sources,
201
+ },
202
+ ...(kind === "suggestion" && restore
203
+ ? {
204
+ suggestion: {
205
+ origin: "admin",
206
+ action: "reference-plan",
207
+ idea: title,
208
+ accepted: null,
209
+ },
210
+ }
211
+ : {}),
212
+ mergedHistory: [
213
+ ...new Set([
214
+ ...(leader.checkpoint.mergedHistory || []),
215
+ ...unique.filter((t) => t.id !== leader.id).map((t) => t.id),
216
+ ]),
217
+ ],
218
+ previousDeadlines: unique.map((t) => ({
219
+ task: t.id,
220
+ at: t.due_at,
221
+ precision: t.due_precision,
222
+ })),
223
+ ...(contract ? { contract } : {}),
224
+ ...(schedule ? { schedule } : {}),
225
+ };
226
+ for (const task of unique) {
227
+ this.time.stopSpan(task, now);
228
+ for (const a of this.anticipations(task)) {
229
+ this.link("anticipation", a, leader.id, now);
230
+ this.time.mind.anticipations.close(a, {
231
+ status: "let_go",
232
+ note: "重复事项已整理为一个执行计划;原约定未因此兑现",
233
+ sources: task.sources,
234
+ time: now,
235
+ });
236
+ }
237
+ this.link("task", task.id, leader.id, now);
238
+ if (task.id !== leader.id) {
239
+ this.db
240
+ .prepare(
241
+ "UPDATE mind_time_tasks SET state='abandoned',updated=?,wait_reason=?,lease=NULL,lease_at=NULL,revision=revision+1,checkpoint=? WHERE id=?",
242
+ )
243
+ .run(
244
+ now,
245
+ "重复事项已合并,原记录保留",
246
+ JSON.stringify({ ...task.checkpoint, mergedInto: leader.id }),
247
+ task.id,
248
+ );
249
+ this.time.event(
250
+ task.id,
251
+ "merged",
252
+ "归入唯一事项,保留原文与时间",
253
+ { taskId: leader.id },
254
+ now,
255
+ );
256
+ }
257
+ }
258
+ const proposed = schedule?.proposedAt || now,
259
+ wait =
260
+ contract?.mode === "reference" ? this.time.search.ready(now) : "";
261
+ const state = restore
262
+ ? wait
263
+ ? "waiting"
264
+ : proposed > now
265
+ ? "scheduled"
266
+ : "todo"
267
+ : leader.state;
268
+ this.db
269
+ .prepare(
270
+ "UPDATE mind_time_tasks SET title=?,why=?,kind=?,subject=?,sources=?,checkpoint=?,state=?,ready_at=?,next_step=?,due_at=?,due_precision=?,updated=?,wait_reason=?,lease=NULL,lease_at=NULL,revision=revision+1 WHERE id=?",
271
+ )
272
+ .run(
273
+ text(title || leader.title, 240),
274
+ text(why || leader.why, 300),
275
+ kind || leader.kind,
276
+ subject || leader.subject,
277
+ JSON.stringify(refs),
278
+ JSON.stringify(checkpoint),
279
+ state,
280
+ restore ? proposed : leader.ready_at,
281
+ restore ? proposed : leader.next_step,
282
+ restore ? null : leader.due_at,
283
+ restore ? "none" : leader.due_precision,
284
+ now,
285
+ restore ? wait : leader.wait_reason,
286
+ leader.id,
287
+ );
288
+ if (title) {
289
+ const updated = this.tasks.get(leader.id),
290
+ intent = taskIntent(this.db, updated);
291
+ this.db
292
+ .prepare("UPDATE mind_time_tasks SET intent_key=? WHERE id=?")
293
+ .run(intent.key, leader.id);
294
+ }
295
+ this.time.event(
296
+ leader.id,
297
+ "consolidated",
298
+ restore
299
+ ? "根据管理台明确请求重新整理并安排"
300
+ : "合并重复来源,保留任务决定",
301
+ { from: unique.map((t) => t.id), restore, schedule, contract },
302
+ now,
303
+ );
304
+ this.db.exec("RELEASE consolidate_tasks");
305
+ return this.tasks.get(leader.id);
306
+ } catch (error) {
307
+ this.db.exec("ROLLBACK TO consolidate_tasks; RELEASE consolidate_tasks");
308
+ throw error;
309
+ }
310
+ }
311
+ }