luckytri 1.0.0 → 1.0.2

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 (139) hide show
  1. package/.github/SECURITY.md +4 -4
  2. package/README.en.md +252 -250
  3. package/README.md +252 -250
  4. package/bin/luckytri.js +5 -1
  5. package/docs/en/CHANGELOG.md +8 -0
  6. package/docs/en/connect.md +18 -18
  7. package/docs/en/guide.md +202 -216
  8. package/docs/en/npm-release.md +25 -25
  9. package/docs/zh/CHANGELOG.md +8 -0
  10. package/docs/zh/connect.md +17 -17
  11. package/docs/zh/guide.md +202 -216
  12. package/docs/zh/npm-release.md +33 -33
  13. package/package.json +110 -108
  14. package/plugins/sdk/index.d.ts +155 -155
  15. package/plugins/sdk/testing.js +58 -58
  16. package/plugins/weather/index.js +82 -82
  17. package/plugins/weather/luckytri-plugin.json +29 -29
  18. package/public/app/assets/ChatsPage-BhlJHseG.js +3 -0
  19. package/public/app/assets/Empty-DP6WMGHi.js +2 -0
  20. package/public/app/assets/Field-CtbdqaiB.js +1 -0
  21. package/public/app/assets/HeartPage-32BeTPn2.js +1 -0
  22. package/public/app/assets/LifePage-CxSm0XKI.js +1 -0
  23. package/public/app/assets/MemoryPage-BeKBcPCY.js +1 -0
  24. package/public/app/assets/NaturePage-NEPYl2HS.js +3 -0
  25. package/public/app/assets/PeoplePage-DxtpekwM.js +1 -0
  26. package/public/app/assets/PluginsPage-CVVQwEBI.css +1 -0
  27. package/public/app/assets/PluginsPage-DksEZbUU.js +2 -0
  28. package/public/app/assets/{SystemPage-CAMPoYqM.css → SystemPage-BkRu0Rvx.css} +1 -1
  29. package/public/app/assets/SystemPage-BvzJlHuY.js +1 -0
  30. package/public/app/assets/TimePage-7-awH5XL.js +3 -0
  31. package/public/app/assets/index-4rItIkD-.js +4 -0
  32. package/public/app/assets/{index-YDz1ree1.css → index-DY4e15Kh.css} +1 -1
  33. package/public/app/assets/{knowledge-DG0e0uKw.js → knowledge-C6_HvoyB.js} +1 -1
  34. package/public/app/assets/media-SMfTT-yc.js +1 -0
  35. package/public/app/index.html +3 -3
  36. package/public/guide.en.html +72 -99
  37. package/public/guide.html +44 -86
  38. package/public/tutorial.en.md +25 -39
  39. package/public/tutorial.md +25 -39
  40. package/scripts/backup.js +311 -311
  41. package/scripts/browser.js +41 -41
  42. package/scripts/import-data.js +128 -0
  43. package/scripts/launch.js +101 -94
  44. package/scripts/plugin.js +150 -150
  45. package/scripts/reset-password.js +22 -0
  46. package/scripts/runtime.js +25 -25
  47. package/scripts/service.js +56 -47
  48. package/scripts/setup.js +50 -50
  49. package/scripts/start-server.js +41 -41
  50. package/scripts/stop.js +36 -36
  51. package/server/app.js +112 -96
  52. package/server/auth.js +217 -0
  53. package/server/channels/contract.js +57 -57
  54. package/server/channels/hub.js +165 -165
  55. package/server/channels/index.js +28 -25
  56. package/server/channels/onebot/index.js +335 -336
  57. package/server/channels/onebot/token.js +22 -3
  58. package/server/core/capabilities.js +208 -208
  59. package/server/core/events.js +64 -60
  60. package/server/core/image-preprocessor.js +208 -208
  61. package/server/core/model-manager.js +1213 -1213
  62. package/server/core/net-guard.js +49 -49
  63. package/server/core/orchestrator.js +569 -569
  64. package/server/core/perceivers.js +91 -91
  65. package/server/core/persona-manager.js +243 -243
  66. package/server/core/repository.js +293 -293
  67. package/server/core/response-validator.js +508 -508
  68. package/server/core/turn-processor.js +630 -630
  69. package/server/core/turn.js +236 -236
  70. package/server/core/vision-manager.js +633 -633
  71. package/server/i18n/errors.js +548 -497
  72. package/server/i18n/labels.js +649 -653
  73. package/server/index.js +151 -102
  74. package/server/knowledge/manager.js +386 -386
  75. package/server/knowledge/schema.js +12 -8
  76. package/server/local-access.js +32 -0
  77. package/server/mind/anticipations.js +507 -507
  78. package/server/mind/api.js +703 -703
  79. package/server/mind/budget.js +152 -152
  80. package/server/mind/context-selection.js +61 -61
  81. package/server/mind/day-planner.js +324 -324
  82. package/server/mind/evidence.js +148 -148
  83. package/server/mind/index.js +445 -445
  84. package/server/mind/life/diary.js +786 -786
  85. package/server/mind/life/index.js +1049 -1049
  86. package/server/mind/observations.js +84 -84
  87. package/server/mind/own-day.js +324 -324
  88. package/server/mind/own-voice.js +318 -318
  89. package/server/mind/senses.js +53 -53
  90. package/server/mind/time/activity-clock.js +196 -196
  91. package/server/mind/time/availability.js +69 -69
  92. package/server/mind/time/commitment-review.js +198 -198
  93. package/server/mind/time/executor.js +512 -512
  94. package/server/mind/time/experiences.js +128 -128
  95. package/server/mind/time/games.js +562 -562
  96. package/server/mind/time/index.js +739 -739
  97. package/server/mind/time/intent.js +166 -166
  98. package/server/mind/time/kinds.js +171 -171
  99. package/server/mind/time/plugin-activity.js +207 -207
  100. package/server/mind/time/tasks.js +587 -587
  101. package/server/mind/util.js +110 -110
  102. package/server/mind/view.js +273 -273
  103. package/server/paths.js +102 -102
  104. package/server/plugins/api.js +204 -204
  105. package/server/plugins/host.js +967 -967
  106. package/server/plugins/install.js +295 -295
  107. package/server/plugins/manifest.js +201 -201
  108. package/server/plugins/permissions.js +42 -42
  109. package/server/plugins/runner/guard.mjs +36 -36
  110. package/server/plugins/runner/worker.mjs +250 -250
  111. package/server/plugins/schema.js +91 -91
  112. package/server/plugins/zip.js +112 -112
  113. package/server/startup-lock.js +118 -118
  114. package/server/storage/archive.js +242 -241
  115. package/server/storage/backup-cleanup-worker.js +8 -8
  116. package/server/storage/backup-cleanup.js +415 -414
  117. package/server/storage/backup-lock.js +76 -76
  118. package/server/storage/backup-scheduler.js +131 -131
  119. package/server/storage/data-package.js +298 -0
  120. package/server/storage/data-transfer-api.js +50 -0
  121. package/server/storage/data-transfer-worker.js +18 -0
  122. package/server/storage/data-transfer.js +415 -0
  123. package/server/storage/store.js +202 -199
  124. package/server/studio/readiness.js +181 -190
  125. package/server/studio/settings.js +236 -236
  126. package/public/app/assets/ChatsPage-C-FcRapR.js +0 -3
  127. package/public/app/assets/Empty-CpP5kQHH.js +0 -2
  128. package/public/app/assets/Field-D4gY-INU.js +0 -1
  129. package/public/app/assets/HeartPage-CPy_thQd.js +0 -1
  130. package/public/app/assets/LifePage-CxWvwmSf.js +0 -1
  131. package/public/app/assets/MemoryPage-DC4rhQ68.js +0 -1
  132. package/public/app/assets/NaturePage-IvYw04Zq.js +0 -3
  133. package/public/app/assets/PeoplePage-Cqefm9-q.js +0 -1
  134. package/public/app/assets/PluginsPage-DKSaKvXb.js +0 -2
  135. package/public/app/assets/PluginsPage-HyBe5D8w.css +0 -1
  136. package/public/app/assets/SystemPage-CJmtJkrK.js +0 -1
  137. package/public/app/assets/TimePage-Cw44inM4.js +0 -3
  138. package/public/app/assets/index-BheRW-1t.js +0 -4
  139. package/public/app/assets/media-BzPDBm7e.js +0 -1
@@ -1,324 +1,324 @@
1
- import { withFallback } from "../core/model-manager.js";
2
- import { prompts, replyPrompt } from "../core/persona-manager.js";
3
- import { localClock } from "../core/conversation-cues.js";
4
- import { evidence, parse, text, hasCredential, zonedTime } from "./util.js";
5
- import {
6
- executionBlock,
7
- unsupportedAction,
8
- gameName,
9
- } from "./time/availability.js";
10
- import { rhythmPhase } from "./nature.js";
11
- import { isExecutable, pluginAvailable, withActivities } from "./time/kinds.js";
12
- const MINUTE = 60000;
13
-
14
- export function chosenSlot(value, life, now, requestedAt = now) {
15
- const at = value.startAt
16
- ? zonedTime(value.startAt, life.mind.timeZone())
17
- : now +
18
- (Number.isFinite(value.startInMinutes) ? value.startInMinutes : 0) *
19
- MINUTE;
20
- const minutes = value.durationMinutes;
21
- if (
22
- !Number.isFinite(at) ||
23
- at < requestedAt - MINUTE ||
24
- at > now + 7 * 86400000
25
- )
26
- throw Error("需要选定有效的开始时间");
27
- if (!Number.isInteger(minutes) || minutes < 5 || minutes > 240)
28
- throw Error("需要选定 5 至 240 分钟的投入时间");
29
- const start = Math.max(now, at);
30
- if (
31
- rhythmPhase(life.mind.nature.current(now), start, life.mind.timeZone())
32
- .key === "asleep"
33
- )
34
- throw Error("所选开始时间在睡眠安排中,需要重新选择");
35
- return {
36
- startAt: start,
37
- durationMinutes: minutes,
38
- reason: text(value.reason, 240) || "自己决定先做这一段",
39
- };
40
- }
41
-
42
- export class DayPlanner {
43
- constructor(life) {
44
- this.life = life;
45
- this.db = life.db;
46
- }
47
- state() {
48
- return this.life.repo.config("day-planner", {});
49
- }
50
- request() {
51
- this.life.repo.saveConfig("day-planner", {
52
- ...this.state(),
53
- requested: true,
54
- });
55
- return { queued: true };
56
- }
57
- needs(task, now) {
58
- if (
59
- ["done", "abandoned", "doing"].includes(task.state) ||
60
- task.lease ||
61
- !this.life.mind.time.allowed(task, now)
62
- )
63
- return false;
64
- if (task.state === "paused" && task.next_step >= Number.MAX_SAFE_INTEGER)
65
- return false;
66
- const time = this.life.mind.time,
67
- slot = task.checkpoint.schedule;
68
- return (
69
- task.activity === "unknown" ||
70
- (task.activity === "game" && !gameName(time, task)) ||
71
- !slot?.chosenAt ||
72
- (slot.endedAt <= now && !task.checkpoint.pendingStep) ||
73
- task.checkpoint.reschedule
74
- );
75
- }
76
- context(task, now) {
77
- const roots = evidence(task.sources),
78
- messages = [];
79
- for (const ref of roots.filter((r) => r.startsWith("m:")).slice(-6)) {
80
- const row = this.db
81
- .prepare(
82
- "SELECT seq,session_id,payload FROM core_events WHERE seq=? AND time<=? AND seq NOT IN (SELECT seq FROM mind_unlived)",
83
- )
84
- .get(Number(ref.slice(2)), now);
85
- if (!row || (task.session_id && task.session_id !== row.session_id))
86
- continue;
87
- const m = parse(row.payload, {});
88
- if (m.simulated || m.artifact) continue;
89
- const prior = this.db
90
- .prepare(
91
- "SELECT seq,role,payload FROM core_events WHERE session_id=? AND seq<? AND time<=? AND seq NOT IN (SELECT seq FROM mind_unlived) AND COALESCE(json_extract(payload,'$.simulated'),0)=0 ORDER BY seq DESC LIMIT 2",
92
- )
93
- .all(row.session_id, row.seq, now)
94
- .reverse();
95
- messages.push({
96
- ref,
97
- text: text(m.text, 500),
98
- prior: prior.map((r) => ({
99
- role: r.role,
100
- text: text(parse(r.payload, {}).text, 240),
101
- })),
102
- });
103
- }
104
- return messages;
105
- }
106
- async run(now = this.life.now()) {
107
- const life = this.life,
108
- time = life.mind.time;
109
- const primary = time.primary();
110
- if (
111
- this.disabledForTest ||
112
- life.closed ||
113
- life.busy ||
114
- primary?.lease ||
115
- time.attention.activityBusy ||
116
- !life.settings().solitude ||
117
- life.repo.store.settings().demo ||
118
- life.phase(now).key === "asleep"
119
- )
120
- return null;
121
- const state = this.state(),
122
- attempts = state.attempts || {};
123
- const tasks = time.tasks
124
- .list({ limit: 100 })
125
- .filter(
126
- (t) =>
127
- (!primary || t.priority > primary.priority) &&
128
- this.needs(t, now) &&
129
- (state.requested || now - (attempts[t.id] || 0) >= 10 * MINUTE),
130
- )
131
- .sort((a, b) => time.tasks.score(b, now) - time.tasks.score(a, now));
132
- const task = tasks.find((t) => !unsupportedAction(t)) || tasks[0];
133
- if (!task) return null;
134
- const unsupported = unsupportedAction(task);
135
- if (unsupported) {
136
- if (task.wait_reason !== unsupported) {
137
- time.tasks.wait(task, unsupported, Number.MAX_SAFE_INTEGER, now);
138
- time.event(task.id, "capability-wait", unsupported, {}, now);
139
- }
140
- this.mark(task.id, now);
141
- return null;
142
- }
143
- if (!life.mind.budget.allows("inner", now)) return null;
144
- let profile;
145
- try {
146
- profile = life.profile();
147
- } catch {
148
- return null;
149
- }
150
- return time.attention.activity(async () => {
151
- this.mark(task.id, now);
152
- this.working = true;
153
- const revision = task.revision,
154
- version = life.mind.nature.version(),
155
- trace = life.repo.trace("__mind__", "activity"),
156
- runId = life.run("day-plan", "想具体做什么、几点开始和做多久");
157
- let status = "error",
158
- reason = "";
159
- try {
160
- const projects = time.works
161
- .projects({ limit: 20 })
162
- .filter(
163
- (p) =>
164
- p.state === "active" &&
165
- time.visible(p, task.session_id || "", now),
166
- )
167
- .slice(0, 6);
168
- const answer = await withFallback(
169
- life.chat.models,
170
- life.chat.fallbackFor(null, profile, trace),
171
- ).call(
172
- profile,
173
- "reflection",
174
- replyPrompt(
175
- life.mind.nature.current(now),
176
- {
177
- ...prompts(life.repo),
178
- activity: withActivities(
179
- '为这件真实待办作一次自己的安排。先把想做的事想具体,再决定几点开始、投入多久。可以马上做,也可以安排晚一些;这不是机械按队列估时。已有主线项目可继续,游戏必须自己选定名称,不能停在“玩一会游戏”。承诺只按原话与语境理解,不能把做不到的承诺改成别的成果算兑现;除了此刻真正能做的事,没有下单、实拍、客户端操作能力。不虚构已执行。只安排本段,可以提前完成或以后续接。输出JSON {"action":"schedule|wait|rest","activity":"write|read|think|game|unknown","title":"具体行动","topic":"游戏名称或空","projectId":"提供的项目ID或null","startAt":"本地YYYY-MM-DD HH:mm","durationMinutes":20,"reason":"自己的选择理由"}。wait表示真正缺少条件,说明reason;rest表示暂时想休息,说明reason。已经明确的承诺不得换目标,私人来源继续属于这件私人事项。',
180
- ),
181
- },
182
- "activity",
183
- ),
184
- {
185
- clock: localClock(now, life.mind.timeZone()),
186
- task: {
187
- id: task.id,
188
- title: task.title,
189
- why: task.why,
190
- kind: task.kind,
191
- activity: task.activity,
192
- wait: task.wait_reason,
193
- checkpoint: task.checkpoint,
194
- earliestAt: task.ready_at,
195
- elapsedMs: time.elapsed(task.id, now),
196
- timing: time.clock.view(task, now),
197
- original: this.context(task, now),
198
- },
199
- affect: life.mind.affect.state(now),
200
- self: life
201
- .selfView(now, { room: task.session_id || "" })
202
- .slice(0, 6),
203
- projects: projects.map((p) => ({
204
- id: p.id,
205
- kind: p.kind,
206
- title: p.title,
207
- summary: text(p.summary, 240),
208
- topic: p.bible.topic || "",
209
- })),
210
- available: {
211
- read: life.mind.reading.unreadCount() > 0,
212
- write: true,
213
- think: true,
214
- game: !time.search.ready(now),
215
- ...pluginAvailable(),
216
- },
217
- busy: time.tasks
218
- .slots(now)
219
- .filter((s) => s.id !== task.id)
220
- .map(({ startAt, endAt, priority, ongoing }) => ({
221
- startAt,
222
- endAt,
223
- priority,
224
- ongoing,
225
- })),
226
- },
227
- trace,
228
- );
229
- const fresh = time.tasks.get(task.id);
230
- if (
231
- life.closed ||
232
- (time.primary()?.id || null) !== (primary?.id || null) ||
233
- fresh?.revision !== revision ||
234
- version !== life.mind.nature.version() ||
235
- !time.allowed(fresh, life.now())
236
- ) {
237
- status = "cancelled";
238
- return { status };
239
- }
240
- reason = text(answer?.reason, 240);
241
- if (hasCredential(JSON.stringify(answer)))
242
- throw Error("安排包含无效内容");
243
- if (answer?.action === "wait") {
244
- time.tasks.wait(
245
- fresh,
246
- reason || "还缺少实际执行条件",
247
- life.now() + 10 * MINUTE,
248
- life.now(),
249
- );
250
- status = "waiting";
251
- } else if (answer?.action === "rest") {
252
- time.tasks.control(
253
- task.id,
254
- {
255
- action: "pause",
256
- readyAt: life.now() + 10 * MINUTE,
257
- reason: reason || "这会儿想先歇着",
258
- },
259
- life.now(),
260
- );
261
- status = "resting";
262
- } else if (answer?.action === "schedule") {
263
- const activity =
264
- task.activity === "unknown" ? answer.activity : task.activity;
265
- if (!isExecutable(activity)) throw Error("需要把行动内容想具体");
266
- const topic = text(answer.topic, 80),
267
- title = text(answer.title, 240) || task.title;
268
- if (
269
- activity === "game" &&
270
- !topic &&
271
- !gameName(time, { ...task, title })
272
- )
273
- throw Error("需要自己选定要玩的游戏");
274
- const slot = chosenSlot(answer, life, life.now(), now);
275
- const project = projects.find(
276
- (p) => p.id === answer.projectId && p.kind === activity,
277
- );
278
- time.tasks.arrange(
279
- task.id,
280
- {
281
- ...slot,
282
- startAt: Math.max(slot.startAt, task.ready_at),
283
- activity,
284
- title:
285
- task.kind === "promise" && task.activity !== "unknown"
286
- ? task.title
287
- : title,
288
- topic,
289
- projectId: project?.id,
290
- },
291
- life.now(),
292
- );
293
- status = "planned";
294
- } else throw Error("没有得到明确的时间安排");
295
- return { status, reason, task: task.id };
296
- } catch (error) {
297
- reason = text(error.message, 160);
298
- trace.error = reason;
299
- return { status: "error", reason };
300
- } finally {
301
- this.working = false;
302
- life.chat.finishQuietly(
303
- trace,
304
- status === "error" ? "error" : "complete",
305
- );
306
- life.end(runId, status, reason, trace);
307
- }
308
- });
309
- }
310
- mark(id, now) {
311
- const state = this.state(),
312
- attempts = { ...state.attempts, [id]: now };
313
- const recent = Object.fromEntries(
314
- Object.entries(attempts)
315
- .sort((a, b) => b[1] - a[1])
316
- .slice(0, 100),
317
- );
318
- this.life.repo.saveConfig("day-planner", {
319
- ...state,
320
- attempts: recent,
321
- requested: false,
322
- });
323
- }
324
- }
1
+ import { withFallback } from "../core/model-manager.js";
2
+ import { prompts, replyPrompt } from "../core/persona-manager.js";
3
+ import { localClock } from "../core/conversation-cues.js";
4
+ import { evidence, parse, text, hasCredential, zonedTime } from "./util.js";
5
+ import {
6
+ executionBlock,
7
+ unsupportedAction,
8
+ gameName,
9
+ } from "./time/availability.js";
10
+ import { rhythmPhase } from "./nature.js";
11
+ import { isExecutable, pluginAvailable, withActivities } from "./time/kinds.js";
12
+ const MINUTE = 60000;
13
+
14
+ export function chosenSlot(value, life, now, requestedAt = now) {
15
+ const at = value.startAt
16
+ ? zonedTime(value.startAt, life.mind.timeZone())
17
+ : now +
18
+ (Number.isFinite(value.startInMinutes) ? value.startInMinutes : 0) *
19
+ MINUTE;
20
+ const minutes = value.durationMinutes;
21
+ if (
22
+ !Number.isFinite(at) ||
23
+ at < requestedAt - MINUTE ||
24
+ at > now + 7 * 86400000
25
+ )
26
+ throw Error("需要选定有效的开始时间");
27
+ if (!Number.isInteger(minutes) || minutes < 5 || minutes > 240)
28
+ throw Error("需要选定 5 至 240 分钟的投入时间");
29
+ const start = Math.max(now, at);
30
+ if (
31
+ rhythmPhase(life.mind.nature.current(now), start, life.mind.timeZone())
32
+ .key === "asleep"
33
+ )
34
+ throw Error("所选开始时间在睡眠安排中,需要重新选择");
35
+ return {
36
+ startAt: start,
37
+ durationMinutes: minutes,
38
+ reason: text(value.reason, 240) || "自己决定先做这一段",
39
+ };
40
+ }
41
+
42
+ export class DayPlanner {
43
+ constructor(life) {
44
+ this.life = life;
45
+ this.db = life.db;
46
+ }
47
+ state() {
48
+ return this.life.repo.config("day-planner", {});
49
+ }
50
+ request() {
51
+ this.life.repo.saveConfig("day-planner", {
52
+ ...this.state(),
53
+ requested: true,
54
+ });
55
+ return { queued: true };
56
+ }
57
+ needs(task, now) {
58
+ if (
59
+ ["done", "abandoned", "doing"].includes(task.state) ||
60
+ task.lease ||
61
+ !this.life.mind.time.allowed(task, now)
62
+ )
63
+ return false;
64
+ if (task.state === "paused" && task.next_step >= Number.MAX_SAFE_INTEGER)
65
+ return false;
66
+ const time = this.life.mind.time,
67
+ slot = task.checkpoint.schedule;
68
+ return (
69
+ task.activity === "unknown" ||
70
+ (task.activity === "game" && !gameName(time, task)) ||
71
+ !slot?.chosenAt ||
72
+ (slot.endedAt <= now && !task.checkpoint.pendingStep) ||
73
+ task.checkpoint.reschedule
74
+ );
75
+ }
76
+ context(task, now) {
77
+ const roots = evidence(task.sources),
78
+ messages = [];
79
+ for (const ref of roots.filter((r) => r.startsWith("m:")).slice(-6)) {
80
+ const row = this.db
81
+ .prepare(
82
+ "SELECT seq,session_id,payload FROM core_events WHERE seq=? AND time<=? AND seq NOT IN (SELECT seq FROM mind_unlived)",
83
+ )
84
+ .get(Number(ref.slice(2)), now);
85
+ if (!row || (task.session_id && task.session_id !== row.session_id))
86
+ continue;
87
+ const m = parse(row.payload, {});
88
+ if (m.simulated || m.artifact) continue;
89
+ const prior = this.db
90
+ .prepare(
91
+ "SELECT seq,role,payload FROM core_events WHERE session_id=? AND seq<? AND time<=? AND seq NOT IN (SELECT seq FROM mind_unlived) AND COALESCE(json_extract(payload,'$.simulated'),0)=0 ORDER BY seq DESC LIMIT 2",
92
+ )
93
+ .all(row.session_id, row.seq, now)
94
+ .reverse();
95
+ messages.push({
96
+ ref,
97
+ text: text(m.text, 500),
98
+ prior: prior.map((r) => ({
99
+ role: r.role,
100
+ text: text(parse(r.payload, {}).text, 240),
101
+ })),
102
+ });
103
+ }
104
+ return messages;
105
+ }
106
+ async run(now = this.life.now()) {
107
+ const life = this.life,
108
+ time = life.mind.time;
109
+ const primary = time.primary();
110
+ if (
111
+ this.disabledForTest ||
112
+ life.closed ||
113
+ life.busy ||
114
+ primary?.lease ||
115
+ time.attention.activityBusy ||
116
+ !life.settings().solitude ||
117
+ life.repo.store.settings().demo ||
118
+ life.phase(now).key === "asleep"
119
+ )
120
+ return null;
121
+ const state = this.state(),
122
+ attempts = state.attempts || {};
123
+ const tasks = time.tasks
124
+ .list({ limit: 100 })
125
+ .filter(
126
+ (t) =>
127
+ (!primary || t.priority > primary.priority) &&
128
+ this.needs(t, now) &&
129
+ (state.requested || now - (attempts[t.id] || 0) >= 10 * MINUTE),
130
+ )
131
+ .sort((a, b) => time.tasks.score(b, now) - time.tasks.score(a, now));
132
+ const task = tasks.find((t) => !unsupportedAction(t)) || tasks[0];
133
+ if (!task) return null;
134
+ const unsupported = unsupportedAction(task);
135
+ if (unsupported) {
136
+ if (task.wait_reason !== unsupported) {
137
+ time.tasks.wait(task, unsupported, Number.MAX_SAFE_INTEGER, now);
138
+ time.event(task.id, "capability-wait", unsupported, {}, now);
139
+ }
140
+ this.mark(task.id, now);
141
+ return null;
142
+ }
143
+ if (!life.mind.budget.allows("inner", now)) return null;
144
+ let profile;
145
+ try {
146
+ profile = life.profile();
147
+ } catch {
148
+ return null;
149
+ }
150
+ return time.attention.activity(async () => {
151
+ this.mark(task.id, now);
152
+ this.working = true;
153
+ const revision = task.revision,
154
+ version = life.mind.nature.version(),
155
+ trace = life.repo.trace("__mind__", "activity"),
156
+ runId = life.run("day-plan", "想具体做什么、几点开始和做多久");
157
+ let status = "error",
158
+ reason = "";
159
+ try {
160
+ const projects = time.works
161
+ .projects({ limit: 20 })
162
+ .filter(
163
+ (p) =>
164
+ p.state === "active" &&
165
+ time.visible(p, task.session_id || "", now),
166
+ )
167
+ .slice(0, 6);
168
+ const answer = await withFallback(
169
+ life.chat.models,
170
+ life.chat.fallbackFor(null, profile, trace),
171
+ ).call(
172
+ profile,
173
+ "reflection",
174
+ replyPrompt(
175
+ life.mind.nature.current(now),
176
+ {
177
+ ...prompts(life.repo),
178
+ activity: withActivities(
179
+ '为这件真实待办作一次自己的安排。先把想做的事想具体,再决定几点开始、投入多久。可以马上做,也可以安排晚一些;这不是机械按队列估时。已有主线项目可继续,游戏必须自己选定名称,不能停在“玩一会游戏”。承诺只按原话与语境理解,不能把做不到的承诺改成别的成果算兑现;除了此刻真正能做的事,没有下单、实拍、客户端操作能力。不虚构已执行。只安排本段,可以提前完成或以后续接。输出JSON {"action":"schedule|wait|rest","activity":"write|read|think|game|unknown","title":"具体行动","topic":"游戏名称或空","projectId":"提供的项目ID或null","startAt":"本地YYYY-MM-DD HH:mm","durationMinutes":20,"reason":"自己的选择理由"}。wait表示真正缺少条件,说明reason;rest表示暂时想休息,说明reason。已经明确的承诺不得换目标,私人来源继续属于这件私人事项。',
180
+ ),
181
+ },
182
+ "activity",
183
+ ),
184
+ {
185
+ clock: localClock(now, life.mind.timeZone()),
186
+ task: {
187
+ id: task.id,
188
+ title: task.title,
189
+ why: task.why,
190
+ kind: task.kind,
191
+ activity: task.activity,
192
+ wait: task.wait_reason,
193
+ checkpoint: task.checkpoint,
194
+ earliestAt: task.ready_at,
195
+ elapsedMs: time.elapsed(task.id, now),
196
+ timing: time.clock.view(task, now),
197
+ original: this.context(task, now),
198
+ },
199
+ affect: life.mind.affect.state(now),
200
+ self: life
201
+ .selfView(now, { room: task.session_id || "" })
202
+ .slice(0, 6),
203
+ projects: projects.map((p) => ({
204
+ id: p.id,
205
+ kind: p.kind,
206
+ title: p.title,
207
+ summary: text(p.summary, 240),
208
+ topic: p.bible.topic || "",
209
+ })),
210
+ available: {
211
+ read: life.mind.reading.unreadCount() > 0,
212
+ write: true,
213
+ think: true,
214
+ game: !time.search.ready(now),
215
+ ...pluginAvailable(),
216
+ },
217
+ busy: time.tasks
218
+ .slots(now)
219
+ .filter((s) => s.id !== task.id)
220
+ .map(({ startAt, endAt, priority, ongoing }) => ({
221
+ startAt,
222
+ endAt,
223
+ priority,
224
+ ongoing,
225
+ })),
226
+ },
227
+ trace,
228
+ );
229
+ const fresh = time.tasks.get(task.id);
230
+ if (
231
+ life.closed ||
232
+ (time.primary()?.id || null) !== (primary?.id || null) ||
233
+ fresh?.revision !== revision ||
234
+ version !== life.mind.nature.version() ||
235
+ !time.allowed(fresh, life.now())
236
+ ) {
237
+ status = "cancelled";
238
+ return { status };
239
+ }
240
+ reason = text(answer?.reason, 240);
241
+ if (hasCredential(JSON.stringify(answer)))
242
+ throw Error("安排包含无效内容");
243
+ if (answer?.action === "wait") {
244
+ time.tasks.wait(
245
+ fresh,
246
+ reason || "还缺少实际执行条件",
247
+ life.now() + 10 * MINUTE,
248
+ life.now(),
249
+ );
250
+ status = "waiting";
251
+ } else if (answer?.action === "rest") {
252
+ time.tasks.control(
253
+ task.id,
254
+ {
255
+ action: "pause",
256
+ readyAt: life.now() + 10 * MINUTE,
257
+ reason: reason || "这会儿想先歇着",
258
+ },
259
+ life.now(),
260
+ );
261
+ status = "resting";
262
+ } else if (answer?.action === "schedule") {
263
+ const activity =
264
+ task.activity === "unknown" ? answer.activity : task.activity;
265
+ if (!isExecutable(activity)) throw Error("需要把行动内容想具体");
266
+ const topic = text(answer.topic, 80),
267
+ title = text(answer.title, 240) || task.title;
268
+ if (
269
+ activity === "game" &&
270
+ !topic &&
271
+ !gameName(time, { ...task, title })
272
+ )
273
+ throw Error("需要自己选定要玩的游戏");
274
+ const slot = chosenSlot(answer, life, life.now(), now);
275
+ const project = projects.find(
276
+ (p) => p.id === answer.projectId && p.kind === activity,
277
+ );
278
+ time.tasks.arrange(
279
+ task.id,
280
+ {
281
+ ...slot,
282
+ startAt: Math.max(slot.startAt, task.ready_at),
283
+ activity,
284
+ title:
285
+ task.kind === "promise" && task.activity !== "unknown"
286
+ ? task.title
287
+ : title,
288
+ topic,
289
+ projectId: project?.id,
290
+ },
291
+ life.now(),
292
+ );
293
+ status = "planned";
294
+ } else throw Error("没有得到明确的时间安排");
295
+ return { status, reason, task: task.id };
296
+ } catch (error) {
297
+ reason = text(error.message, 160);
298
+ trace.error = reason;
299
+ return { status: "error", reason };
300
+ } finally {
301
+ this.working = false;
302
+ life.chat.finishQuietly(
303
+ trace,
304
+ status === "error" ? "error" : "complete",
305
+ );
306
+ life.end(runId, status, reason, trace);
307
+ }
308
+ });
309
+ }
310
+ mark(id, now) {
311
+ const state = this.state(),
312
+ attempts = { ...state.attempts, [id]: now };
313
+ const recent = Object.fromEntries(
314
+ Object.entries(attempts)
315
+ .sort((a, b) => b[1] - a[1])
316
+ .slice(0, 100),
317
+ );
318
+ this.life.repo.saveConfig("day-planner", {
319
+ ...state,
320
+ attempts: recent,
321
+ requested: false,
322
+ });
323
+ }
324
+ }