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,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
|
+
}
|