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.
- package/LICENSE +21 -21
- package/README.en.md +2 -0
- package/README.md +2 -0
- package/docs/en/CHANGELOG.md +20 -13
- package/docs/en/guide.md +7 -5
- package/docs/en/her-life.md +2 -2
- package/docs/en/webui.md +1 -1
- package/docs/zh/CHANGELOG.md +20 -13
- package/docs/zh/guide.md +7 -5
- package/docs/zh/her-life.md +2 -2
- package/docs/zh/webui.md +1 -1
- package/package.json +2 -1
- package/public/app/assets/ChatsPage-CFMVozoG.css +1 -0
- package/public/app/assets/{ChatsPage-CshdtTzV.js → ChatsPage-DCYMkJ91.js} +2 -2
- package/public/app/assets/{Empty-C7yzOBzy.js → Empty-CLyZkxR5.js} +1 -1
- package/public/app/assets/{Empty-DmUB2-Bk.css → Empty-DIobYG3x.css} +1 -1
- package/public/app/assets/{Field-BjFv4Rfn.js → Field-BpaRwLI_.js} +1 -1
- package/public/app/assets/{HeartPage-eqo6ru16.js → HeartPage-BIOMTiwr.js} +1 -1
- package/public/app/assets/HeartPage-C-2jvgzF.css +1 -0
- package/public/app/assets/{LifePage-CN8GE5NK.js → LifePage-BCgM4JkR.js} +1 -1
- package/public/app/assets/LifePage-CG1OHbRK.css +1 -0
- package/public/app/assets/MemoryPage-C_Bu4rfb.css +1 -0
- package/public/app/assets/{MemoryPage-CCpMN4Wg.js → MemoryPage-vShvRqMt.js} +1 -1
- package/public/app/assets/NaturePage-CPZlImT0.css +1 -0
- package/public/app/assets/{NaturePage-C0JqdBTR.js → NaturePage-Cy9ptr4G.js} +2 -2
- package/public/app/assets/PeoplePage-3RRUITDz.css +1 -0
- package/public/app/assets/{PeoplePage-BXGBbO4u.js → PeoplePage-CUZekB2E.js} +1 -1
- package/public/app/assets/{PluginsPage-Dksp5AV5.js → PluginsPage-1RM4rmJP.js} +1 -1
- package/public/app/assets/{SystemPage-BenIyTvk.css → SystemPage-BI2CB5Ig.css} +1 -1
- package/public/app/assets/{SystemPage-Cd7aa8eE.js → SystemPage-BeT5TmIV.js} +1 -1
- package/public/app/assets/TimePage-C-guwPk2.css +1 -0
- package/public/app/assets/{TimePage-Bs_zImb4.js → TimePage-taTmPMM8.js} +2 -2
- package/public/app/assets/{index-BHZcHUMh.css → index-DY5AM0Hz.css} +1 -1
- package/public/app/assets/{index-CyRtYxP3.js → index-DdYwC5ZU.js} +3 -3
- package/public/app/assets/{knowledge-BJOIyuJo.js → knowledge-DJmfu2KN.js} +1 -1
- package/public/app/assets/{media-Bwtx8blz.js → media-BEtnE0Gv.js} +1 -1
- package/public/app/assets/{mind-DtTVrW6Z.js → mind-Dtiw0-QK.js} +1 -1
- package/public/app/index.html +6 -5
- package/public/guide.en.html +11 -3
- package/public/guide.html +6 -3
- package/public/tutorial.en.md +2 -0
- package/public/tutorial.md +2 -0
- package/scripts/import-data.js +128 -128
- package/scripts/proxy-config.js +167 -167
- package/scripts/service-supervisor.js +50 -0
- package/scripts/start-server.js +71 -16
- 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 +325 -325
- package/server/core/conversation-manager.js +87 -87
- 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 +2 -1
- package/server/core/reply-delivery.js +404 -403
- package/server/core/reply-target-resolver.js +50 -50
- package/server/core/response-generator.js +50 -50
- package/server/core/response-validator.js +14 -2
- 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/http.js +18 -18
- package/server/i18n/decisions.js +209 -206
- package/server/i18n/errors.js +4 -0
- package/server/i18n/index.js +144 -144
- 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/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 +60 -60
- package/server/mind/initiative.js +338 -338
- package/server/mind/life/activities.js +21 -21
- package/server/mind/life/context.js +330 -323
- package/server/mind/life/diary.js +20 -0
- package/server/mind/life/growth.js +261 -254
- package/server/mind/life/index.js +10 -2
- 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 -0
- package/server/mind/relationships.js +390 -279
- package/server/mind/salience.js +201 -201
- package/server/mind/schema.js +567 -567
- package/server/mind/self.js +484 -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/model-material.js +77 -77
- package/server/mind/time/presentation.js +51 -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 +432 -432
- package/server/mind/traits.js +191 -191
- package/server/mind/view.js +9 -1
- package/server/runtime/installation.js +116 -116
- package/server/runtime/update-execution.js +2 -1
- 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-DfyPCS9N.css +0 -1
- package/public/app/assets/HeartPage-Bft9wZp5.css +0 -1
- package/public/app/assets/LifePage-BUi5euBF.css +0 -1
- package/public/app/assets/MemoryPage-cZvXfkXb.css +0 -1
- package/public/app/assets/NaturePage-CHkpb5NK.css +0 -1
- package/public/app/assets/PeoplePage-BTU0H1pM.css +0 -1
- package/public/app/assets/TimePage-BOms_n4I.css +0 -1
|
@@ -1,432 +1,432 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { evidence, hasCredential, parse, text } from "../util.js";
|
|
3
|
-
import { activityPresentation, gameText } from "./presentation.js";
|
|
4
|
-
|
|
5
|
-
const decode = (row) =>
|
|
6
|
-
row
|
|
7
|
-
? {
|
|
8
|
-
...activityPresentation(row),
|
|
9
|
-
sources: parse(row.sources, []),
|
|
10
|
-
bible: parse(row.bible, {}),
|
|
11
|
-
}
|
|
12
|
-
: null;
|
|
13
|
-
export class Works {
|
|
14
|
-
constructor(time) {
|
|
15
|
-
this.time = time;
|
|
16
|
-
this.db = time.db;
|
|
17
|
-
this.migrate();
|
|
18
|
-
this.linkLegacy();
|
|
19
|
-
}
|
|
20
|
-
linkLegacy() {
|
|
21
|
-
for (const row of this.db
|
|
22
|
-
.prepare(
|
|
23
|
-
"SELECT w.id,c.plan_id FROM mind_time_works w JOIN mind_creations c ON c.id=w.legacy_creation WHERE w.task_id IS NULL",
|
|
24
|
-
)
|
|
25
|
-
.all()) {
|
|
26
|
-
const task = this.db
|
|
27
|
-
.prepare(
|
|
28
|
-
"SELECT id FROM mind_time_tasks WHERE anticipation_id=? OR id=? ORDER BY created LIMIT 1",
|
|
29
|
-
)
|
|
30
|
-
.get(row.plan_id, row.plan_id);
|
|
31
|
-
if (!task) continue;
|
|
32
|
-
this.db
|
|
33
|
-
.prepare("UPDATE mind_time_works SET task_id=? WHERE id=?")
|
|
34
|
-
.run(task.id, row.id);
|
|
35
|
-
this.db
|
|
36
|
-
.prepare(
|
|
37
|
-
"UPDATE mind_time_tasks SET work_id=COALESCE(work_id,?) WHERE id=?",
|
|
38
|
-
)
|
|
39
|
-
.run(row.id, task.id);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
migrate() {
|
|
43
|
-
for (const row of this.db
|
|
44
|
-
.prepare(
|
|
45
|
-
"SELECT * FROM mind_creations WHERE id NOT IN (SELECT legacy_creation FROM mind_time_works WHERE legacy_creation IS NOT NULL)",
|
|
46
|
-
)
|
|
47
|
-
.all()) {
|
|
48
|
-
const id = randomUUID();
|
|
49
|
-
this.db.exec("SAVEPOINT migrate_work");
|
|
50
|
-
try {
|
|
51
|
-
this.db
|
|
52
|
-
.prepare(
|
|
53
|
-
"INSERT INTO mind_time_works(id,created,updated,title,state,version,session_id,discretion,legacy_creation) VALUES (?,?,?,?,'complete',1,?,?,?)",
|
|
54
|
-
)
|
|
55
|
-
.run(
|
|
56
|
-
id,
|
|
57
|
-
row.created,
|
|
58
|
-
row.created,
|
|
59
|
-
row.title,
|
|
60
|
-
row.session_id,
|
|
61
|
-
row.discretion,
|
|
62
|
-
row.id,
|
|
63
|
-
);
|
|
64
|
-
this.db
|
|
65
|
-
.prepare(
|
|
66
|
-
"INSERT INTO mind_time_versions(id,work_id,version,created,title,content,sources,run_id) VALUES (?,?,1,?,?,?,?,?)",
|
|
67
|
-
)
|
|
68
|
-
.run(
|
|
69
|
-
randomUUID(),
|
|
70
|
-
id,
|
|
71
|
-
row.created,
|
|
72
|
-
row.title,
|
|
73
|
-
row.content,
|
|
74
|
-
row.sources,
|
|
75
|
-
row.run_id,
|
|
76
|
-
);
|
|
77
|
-
this.db.exec("RELEASE migrate_work");
|
|
78
|
-
} catch (error) {
|
|
79
|
-
this.db.exec("ROLLBACK TO migrate_work; RELEASE migrate_work");
|
|
80
|
-
throw error;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
project(id) {
|
|
85
|
-
return decode(
|
|
86
|
-
this.db.prepare("SELECT * FROM mind_time_projects WHERE id=?").get(id),
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
projects({ limit = 30, offset = 0, q = "" } = {}) {
|
|
90
|
-
return this.db
|
|
91
|
-
.prepare(
|
|
92
|
-
"SELECT * FROM mind_time_projects WHERE title LIKE ? ORDER BY updated DESC LIMIT ? OFFSET ?",
|
|
93
|
-
)
|
|
94
|
-
.all(
|
|
95
|
-
"%" + text(q, 80) + "%",
|
|
96
|
-
Math.max(1, Math.min(100, Number(limit))),
|
|
97
|
-
Math.max(0, Number(offset)),
|
|
98
|
-
)
|
|
99
|
-
.map(decode);
|
|
100
|
-
}
|
|
101
|
-
ensureProject(task, now) {
|
|
102
|
-
if (task.project_id) return this.project(task.project_id);
|
|
103
|
-
const id = randomUUID();
|
|
104
|
-
this.db
|
|
105
|
-
.prepare(
|
|
106
|
-
"INSERT INTO mind_time_projects(id,created,updated,kind,title,why,session_id,discretion,sources,serial) VALUES (?,?,?,?,?,?,?,?,?,?)",
|
|
107
|
-
)
|
|
108
|
-
.run(
|
|
109
|
-
id,
|
|
110
|
-
now,
|
|
111
|
-
now,
|
|
112
|
-
task.activity,
|
|
113
|
-
task.title,
|
|
114
|
-
task.why,
|
|
115
|
-
task.session_id,
|
|
116
|
-
task.discretion,
|
|
117
|
-
JSON.stringify(task.sources),
|
|
118
|
-
/连载|续写|长篇|系列/.test(task.title) ? 1 : 0,
|
|
119
|
-
);
|
|
120
|
-
this.db
|
|
121
|
-
.prepare("UPDATE mind_time_tasks SET project_id=? WHERE id=?")
|
|
122
|
-
.run(id, task.id);
|
|
123
|
-
task.project_id = id;
|
|
124
|
-
return this.project(id);
|
|
125
|
-
}
|
|
126
|
-
list({ q = "", project = "", limit = 30, offset = 0 } = {}) {
|
|
127
|
-
return this.db
|
|
128
|
-
.prepare(
|
|
129
|
-
`SELECT w.*,COALESCE(p.kind,(SELECT kind FROM mind_creations WHERE id=w.legacy_creation)) kind,p.title project_title,v.summary,v.sources,length(v.content) characters FROM mind_time_works w LEFT JOIN mind_time_projects p ON p.id=w.project_id JOIN mind_time_versions v ON v.work_id=w.id AND v.version=w.version WHERE (w.title LIKE ? OR p.title LIKE ?) ${project ? "AND w.project_id=?" : ""} ORDER BY w.updated DESC LIMIT ? OFFSET ?`,
|
|
130
|
-
)
|
|
131
|
-
.all(
|
|
132
|
-
"%" + text(q, 80) + "%",
|
|
133
|
-
"%" + text(q, 80) + "%",
|
|
134
|
-
...(project ? [project] : []),
|
|
135
|
-
Math.max(1, Math.min(100, Number(limit))),
|
|
136
|
-
Math.max(0, Number(offset)),
|
|
137
|
-
)
|
|
138
|
-
.map((row) =>
|
|
139
|
-
row.kind === "game"
|
|
140
|
-
? {
|
|
141
|
-
...row,
|
|
142
|
-
title: gameText(row.title),
|
|
143
|
-
summary: gameText(row.summary),
|
|
144
|
-
}
|
|
145
|
-
: row,
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
get(id, version) {
|
|
149
|
-
const work = this.db
|
|
150
|
-
.prepare("SELECT * FROM mind_time_works WHERE id=?")
|
|
151
|
-
.get(id);
|
|
152
|
-
if (!work) return null;
|
|
153
|
-
const v = this.db
|
|
154
|
-
.prepare("SELECT * FROM mind_time_versions WHERE work_id=? AND version=?")
|
|
155
|
-
.get(id, Number(version) || work.version);
|
|
156
|
-
const saved = v
|
|
157
|
-
? this.db
|
|
158
|
-
.prepare(
|
|
159
|
-
"SELECT reason FROM mind_time_events WHERE kind='draft' AND json_extract(data,'$.workId')=? AND json_extract(data,'$.version')=? ORDER BY id DESC LIMIT 1",
|
|
160
|
-
)
|
|
161
|
-
.get(id, v.version)
|
|
162
|
-
: null;
|
|
163
|
-
return v
|
|
164
|
-
? {
|
|
165
|
-
...work,
|
|
166
|
-
...v,
|
|
167
|
-
id: work.id,
|
|
168
|
-
versionId: v.id,
|
|
169
|
-
state: saved
|
|
170
|
-
? saved.reason === "保存完成稿"
|
|
171
|
-
? "complete"
|
|
172
|
-
: "draft"
|
|
173
|
-
: work.state,
|
|
174
|
-
sources: parse(v.sources, []),
|
|
175
|
-
provenance: parse(v.provenance, {}),
|
|
176
|
-
versions: this.db
|
|
177
|
-
.prepare(
|
|
178
|
-
"SELECT version,created,title,length(content) characters FROM mind_time_versions WHERE work_id=? ORDER BY version DESC",
|
|
179
|
-
)
|
|
180
|
-
.all(id),
|
|
181
|
-
}
|
|
182
|
-
: null;
|
|
183
|
-
}
|
|
184
|
-
context(task) {
|
|
185
|
-
const project = this.project(task.project_id);
|
|
186
|
-
if (!project) return {};
|
|
187
|
-
const work = task.checkpoint.workId
|
|
188
|
-
? this.get(task.checkpoint.workId)
|
|
189
|
-
: null;
|
|
190
|
-
const chapters = this.list({ project: project.id, limit: 3 }).sort(
|
|
191
|
-
(a, b) => a.ordinal - b.ordinal,
|
|
192
|
-
);
|
|
193
|
-
return {
|
|
194
|
-
project: {
|
|
195
|
-
id: project.id,
|
|
196
|
-
title: project.title,
|
|
197
|
-
why: project.why,
|
|
198
|
-
bible: project.bible,
|
|
199
|
-
summary: project.summary,
|
|
200
|
-
serial: !!project.serial,
|
|
201
|
-
},
|
|
202
|
-
previous: chapters
|
|
203
|
-
.filter((w) => w.id !== work?.id)
|
|
204
|
-
.map((w) => ({
|
|
205
|
-
ordinal: w.ordinal,
|
|
206
|
-
title: w.title,
|
|
207
|
-
summary: w.summary,
|
|
208
|
-
ending: this.get(w.id)?.content.slice(-900),
|
|
209
|
-
})),
|
|
210
|
-
draft: work
|
|
211
|
-
? {
|
|
212
|
-
title: work.title,
|
|
213
|
-
version: work.version,
|
|
214
|
-
summary: work.summary,
|
|
215
|
-
ending: work.content.slice(-1800),
|
|
216
|
-
}
|
|
217
|
-
: null,
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
save(task, result, { sources, runId, now, provenance = {} }) {
|
|
221
|
-
if (!this.time.valid(task)) throw Error("任务租约已经改变");
|
|
222
|
-
const title = text(result.title, 80),
|
|
223
|
-
segment = text(result.content, 1800);
|
|
224
|
-
if (!title || !segment || hasCredential(title + segment))
|
|
225
|
-
throw Error("作品正文格式无效");
|
|
226
|
-
const project = this.ensureProject(task, now),
|
|
227
|
-
existing = task.checkpoint.workId
|
|
228
|
-
? this.get(task.checkpoint.workId)
|
|
229
|
-
: null;
|
|
230
|
-
const id = existing?.id || randomUUID(),
|
|
231
|
-
version = (existing?.version || 0) + 1;
|
|
232
|
-
const content =
|
|
233
|
-
task.checkpoint.intent === "revise"
|
|
234
|
-
? segment
|
|
235
|
-
: (existing ? existing.content + "\n\n" : "") + segment;
|
|
236
|
-
if (content.length > 100000) throw Error("单篇过长,请接续新篇章");
|
|
237
|
-
const complete = result.done === true,
|
|
238
|
-
state = complete ? "complete" : "draft";
|
|
239
|
-
if (!existing) {
|
|
240
|
-
const ordinal = this.db
|
|
241
|
-
.prepare(
|
|
242
|
-
"SELECT COALESCE(MAX(ordinal),0)+1 n FROM mind_time_works WHERE project_id=?",
|
|
243
|
-
)
|
|
244
|
-
.get(project.id).n;
|
|
245
|
-
this.db
|
|
246
|
-
.prepare(
|
|
247
|
-
"INSERT INTO mind_time_works(id,created,updated,task_id,project_id,ordinal,title,state,version,session_id,discretion) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
|
|
248
|
-
)
|
|
249
|
-
.run(
|
|
250
|
-
id,
|
|
251
|
-
now,
|
|
252
|
-
now,
|
|
253
|
-
task.id,
|
|
254
|
-
project.id,
|
|
255
|
-
ordinal,
|
|
256
|
-
title,
|
|
257
|
-
state,
|
|
258
|
-
version,
|
|
259
|
-
task.session_id,
|
|
260
|
-
task.discretion,
|
|
261
|
-
);
|
|
262
|
-
} else
|
|
263
|
-
this.db
|
|
264
|
-
.prepare(
|
|
265
|
-
"UPDATE mind_time_works SET updated=?,title=?,state=?,version=? WHERE id=?",
|
|
266
|
-
)
|
|
267
|
-
.run(now, title, state, version, id);
|
|
268
|
-
const summary = text(result.summary || segment, 500),
|
|
269
|
-
checkpoint = {
|
|
270
|
-
...task.checkpoint,
|
|
271
|
-
workId: id,
|
|
272
|
-
version,
|
|
273
|
-
summary,
|
|
274
|
-
next: text(result.next, 240) || "接着上一段往下写",
|
|
275
|
-
segments: (task.checkpoint.segments || 0) + 1,
|
|
276
|
-
};
|
|
277
|
-
this.db
|
|
278
|
-
.prepare(
|
|
279
|
-
"INSERT INTO mind_time_versions(id,work_id,version,created,title,content,summary,sources,run_id,provenance) VALUES (?,?,?,?,?,?,?,?,?,?)",
|
|
280
|
-
)
|
|
281
|
-
.run(
|
|
282
|
-
randomUUID(),
|
|
283
|
-
id,
|
|
284
|
-
version,
|
|
285
|
-
now,
|
|
286
|
-
title,
|
|
287
|
-
content,
|
|
288
|
-
summary,
|
|
289
|
-
JSON.stringify(evidence(sources)),
|
|
290
|
-
runId,
|
|
291
|
-
JSON.stringify(provenance),
|
|
292
|
-
);
|
|
293
|
-
// Only supplied continuity fields change; omitted fields never erase established settings.
|
|
294
|
-
const bible = { ...project.bible };
|
|
295
|
-
for (const key of ["characters", "world", "threads"])
|
|
296
|
-
if (result.bible?.[key] !== undefined)
|
|
297
|
-
bible[key] = text(
|
|
298
|
-
typeof result.bible[key] === "string"
|
|
299
|
-
? result.bible[key]
|
|
300
|
-
: JSON.stringify(result.bible[key]),
|
|
301
|
-
2000,
|
|
302
|
-
);
|
|
303
|
-
this.db
|
|
304
|
-
.prepare(
|
|
305
|
-
"UPDATE mind_time_projects SET updated=?,summary=?,bible=?,revision=revision+1 WHERE id=?",
|
|
306
|
-
)
|
|
307
|
-
.run(now, summary, JSON.stringify(bible), project.id);
|
|
308
|
-
this.db
|
|
309
|
-
.prepare("UPDATE mind_time_tasks SET checkpoint=?,work_id=? WHERE id=?")
|
|
310
|
-
.run(JSON.stringify(checkpoint), id, task.id);
|
|
311
|
-
this.time.event(
|
|
312
|
-
task.id,
|
|
313
|
-
"draft",
|
|
314
|
-
complete ? "保存完成稿" : "保存一个完整小段",
|
|
315
|
-
{ workId: id, version, characters: content.length },
|
|
316
|
-
now,
|
|
317
|
-
);
|
|
318
|
-
return this.get(id);
|
|
319
|
-
}
|
|
320
|
-
settle(task, workId, complete, now) {
|
|
321
|
-
if (!this.time.valid(task)) throw Error("任务租约已经改变");
|
|
322
|
-
const work = this.get(workId);
|
|
323
|
-
if (!work || work.task_id !== task.id) throw Error("活动草稿已不可用");
|
|
324
|
-
if (complete) {
|
|
325
|
-
this.db
|
|
326
|
-
.prepare(
|
|
327
|
-
"UPDATE mind_time_works SET state='complete',updated=? WHERE id=?",
|
|
328
|
-
)
|
|
329
|
-
.run(now, workId);
|
|
330
|
-
this.time.event(
|
|
331
|
-
task.id,
|
|
332
|
-
"draft",
|
|
333
|
-
"保存完成稿",
|
|
334
|
-
{ workId, version: work.version, characters: work.content.length },
|
|
335
|
-
now,
|
|
336
|
-
);
|
|
337
|
-
}
|
|
338
|
-
return this.get(workId);
|
|
339
|
-
}
|
|
340
|
-
suggest(
|
|
341
|
-
projectId,
|
|
342
|
-
{ action = "continue", idea = "", why = "管理台提出的建议" } = {},
|
|
343
|
-
now = this.time.now(),
|
|
344
|
-
) {
|
|
345
|
-
const project = this.project(projectId);
|
|
346
|
-
if (!project) throw Error("项目不存在");
|
|
347
|
-
if (
|
|
348
|
-
!["continue", "revise", "pause", "finish", "direction"].includes(action)
|
|
349
|
-
)
|
|
350
|
-
throw Error("建议类型无效");
|
|
351
|
-
const pending = this.db
|
|
352
|
-
.prepare(
|
|
353
|
-
"SELECT id FROM mind_time_tasks WHERE project_id=? AND state NOT IN ('done','abandoned') LIMIT 1",
|
|
354
|
-
)
|
|
355
|
-
.get(projectId);
|
|
356
|
-
if (pending) return { duplicate: pending.id };
|
|
357
|
-
const result = this.time.tasks.add(
|
|
358
|
-
{
|
|
359
|
-
kind: "suggestion",
|
|
360
|
-
activeOnly: true,
|
|
361
|
-
activity: project.kind,
|
|
362
|
-
title: `${action === "revise" ? "修改" : "接续"}《${project.title}》${text(idea, 120)}`,
|
|
363
|
-
why: text(why, 240),
|
|
364
|
-
sources: project.sources,
|
|
365
|
-
session: project.session_id,
|
|
366
|
-
discretion: project.discretion,
|
|
367
|
-
projectId,
|
|
368
|
-
},
|
|
369
|
-
now,
|
|
370
|
-
);
|
|
371
|
-
if (result.id) {
|
|
372
|
-
const latest = this.list({ project: projectId, limit: 1 })[0];
|
|
373
|
-
this.db.prepare("UPDATE mind_time_tasks SET checkpoint=? WHERE id=?").run(
|
|
374
|
-
JSON.stringify({
|
|
375
|
-
suggestion: {
|
|
376
|
-
action,
|
|
377
|
-
idea: text(idea, 500),
|
|
378
|
-
origin: "admin",
|
|
379
|
-
accepted: null,
|
|
380
|
-
},
|
|
381
|
-
...(action === "revise" && latest
|
|
382
|
-
? { workId: latest.id, intent: "revise" }
|
|
383
|
-
: {}),
|
|
384
|
-
}),
|
|
385
|
-
result.id,
|
|
386
|
-
);
|
|
387
|
-
this.time.event(
|
|
388
|
-
result.id,
|
|
389
|
-
"suggestion",
|
|
390
|
-
"外部建议,等待她决定是否采纳",
|
|
391
|
-
{ action },
|
|
392
|
-
now,
|
|
393
|
-
);
|
|
394
|
-
}
|
|
395
|
-
return result;
|
|
396
|
-
}
|
|
397
|
-
fragments({ session, now = this.time.now(), cue = [] } = {}) {
|
|
398
|
-
const words = (Array.isArray(cue) ? cue : [cue])
|
|
399
|
-
.map((v) => (typeof v === "string" ? v : JSON.stringify(v)))
|
|
400
|
-
.join(" ");
|
|
401
|
-
return this.list({ limit: 30 })
|
|
402
|
-
.filter(
|
|
403
|
-
(w) =>
|
|
404
|
-
w.created <= now &&
|
|
405
|
-
this.time.visible(
|
|
406
|
-
{ ...w, sources: parse(w.sources, []) },
|
|
407
|
-
session,
|
|
408
|
-
now,
|
|
409
|
-
),
|
|
410
|
-
)
|
|
411
|
-
.map((w) => ({
|
|
412
|
-
...w,
|
|
413
|
-
score: [...new Set(words.match(/[\p{L}\p{N}]{2,}/gu) || [])].filter(
|
|
414
|
-
(term) =>
|
|
415
|
-
(w.title + " " + w.project_title + " " + w.summary).includes(term),
|
|
416
|
-
).length,
|
|
417
|
-
}))
|
|
418
|
-
.sort((a, b) => b.score - a.score || b.updated - a.updated)
|
|
419
|
-
.slice(0, 2)
|
|
420
|
-
.map((w) => ({
|
|
421
|
-
id: w.id,
|
|
422
|
-
title: w.title,
|
|
423
|
-
state: w.state,
|
|
424
|
-
kind: w.kind,
|
|
425
|
-
version: w.version,
|
|
426
|
-
ordinal: w.ordinal,
|
|
427
|
-
summary: text(w.summary, 180),
|
|
428
|
-
fragment: text(this.get(w.id).content, 240),
|
|
429
|
-
characters: w.characters,
|
|
430
|
-
}));
|
|
431
|
-
}
|
|
432
|
-
}
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { evidence, hasCredential, parse, text } from "../util.js";
|
|
3
|
+
import { activityPresentation, gameText } from "./presentation.js";
|
|
4
|
+
|
|
5
|
+
const decode = (row) =>
|
|
6
|
+
row
|
|
7
|
+
? {
|
|
8
|
+
...activityPresentation(row),
|
|
9
|
+
sources: parse(row.sources, []),
|
|
10
|
+
bible: parse(row.bible, {}),
|
|
11
|
+
}
|
|
12
|
+
: null;
|
|
13
|
+
export class Works {
|
|
14
|
+
constructor(time) {
|
|
15
|
+
this.time = time;
|
|
16
|
+
this.db = time.db;
|
|
17
|
+
this.migrate();
|
|
18
|
+
this.linkLegacy();
|
|
19
|
+
}
|
|
20
|
+
linkLegacy() {
|
|
21
|
+
for (const row of this.db
|
|
22
|
+
.prepare(
|
|
23
|
+
"SELECT w.id,c.plan_id FROM mind_time_works w JOIN mind_creations c ON c.id=w.legacy_creation WHERE w.task_id IS NULL",
|
|
24
|
+
)
|
|
25
|
+
.all()) {
|
|
26
|
+
const task = this.db
|
|
27
|
+
.prepare(
|
|
28
|
+
"SELECT id FROM mind_time_tasks WHERE anticipation_id=? OR id=? ORDER BY created LIMIT 1",
|
|
29
|
+
)
|
|
30
|
+
.get(row.plan_id, row.plan_id);
|
|
31
|
+
if (!task) continue;
|
|
32
|
+
this.db
|
|
33
|
+
.prepare("UPDATE mind_time_works SET task_id=? WHERE id=?")
|
|
34
|
+
.run(task.id, row.id);
|
|
35
|
+
this.db
|
|
36
|
+
.prepare(
|
|
37
|
+
"UPDATE mind_time_tasks SET work_id=COALESCE(work_id,?) WHERE id=?",
|
|
38
|
+
)
|
|
39
|
+
.run(row.id, task.id);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
migrate() {
|
|
43
|
+
for (const row of this.db
|
|
44
|
+
.prepare(
|
|
45
|
+
"SELECT * FROM mind_creations WHERE id NOT IN (SELECT legacy_creation FROM mind_time_works WHERE legacy_creation IS NOT NULL)",
|
|
46
|
+
)
|
|
47
|
+
.all()) {
|
|
48
|
+
const id = randomUUID();
|
|
49
|
+
this.db.exec("SAVEPOINT migrate_work");
|
|
50
|
+
try {
|
|
51
|
+
this.db
|
|
52
|
+
.prepare(
|
|
53
|
+
"INSERT INTO mind_time_works(id,created,updated,title,state,version,session_id,discretion,legacy_creation) VALUES (?,?,?,?,'complete',1,?,?,?)",
|
|
54
|
+
)
|
|
55
|
+
.run(
|
|
56
|
+
id,
|
|
57
|
+
row.created,
|
|
58
|
+
row.created,
|
|
59
|
+
row.title,
|
|
60
|
+
row.session_id,
|
|
61
|
+
row.discretion,
|
|
62
|
+
row.id,
|
|
63
|
+
);
|
|
64
|
+
this.db
|
|
65
|
+
.prepare(
|
|
66
|
+
"INSERT INTO mind_time_versions(id,work_id,version,created,title,content,sources,run_id) VALUES (?,?,1,?,?,?,?,?)",
|
|
67
|
+
)
|
|
68
|
+
.run(
|
|
69
|
+
randomUUID(),
|
|
70
|
+
id,
|
|
71
|
+
row.created,
|
|
72
|
+
row.title,
|
|
73
|
+
row.content,
|
|
74
|
+
row.sources,
|
|
75
|
+
row.run_id,
|
|
76
|
+
);
|
|
77
|
+
this.db.exec("RELEASE migrate_work");
|
|
78
|
+
} catch (error) {
|
|
79
|
+
this.db.exec("ROLLBACK TO migrate_work; RELEASE migrate_work");
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
project(id) {
|
|
85
|
+
return decode(
|
|
86
|
+
this.db.prepare("SELECT * FROM mind_time_projects WHERE id=?").get(id),
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
projects({ limit = 30, offset = 0, q = "" } = {}) {
|
|
90
|
+
return this.db
|
|
91
|
+
.prepare(
|
|
92
|
+
"SELECT * FROM mind_time_projects WHERE title LIKE ? ORDER BY updated DESC LIMIT ? OFFSET ?",
|
|
93
|
+
)
|
|
94
|
+
.all(
|
|
95
|
+
"%" + text(q, 80) + "%",
|
|
96
|
+
Math.max(1, Math.min(100, Number(limit))),
|
|
97
|
+
Math.max(0, Number(offset)),
|
|
98
|
+
)
|
|
99
|
+
.map(decode);
|
|
100
|
+
}
|
|
101
|
+
ensureProject(task, now) {
|
|
102
|
+
if (task.project_id) return this.project(task.project_id);
|
|
103
|
+
const id = randomUUID();
|
|
104
|
+
this.db
|
|
105
|
+
.prepare(
|
|
106
|
+
"INSERT INTO mind_time_projects(id,created,updated,kind,title,why,session_id,discretion,sources,serial) VALUES (?,?,?,?,?,?,?,?,?,?)",
|
|
107
|
+
)
|
|
108
|
+
.run(
|
|
109
|
+
id,
|
|
110
|
+
now,
|
|
111
|
+
now,
|
|
112
|
+
task.activity,
|
|
113
|
+
task.title,
|
|
114
|
+
task.why,
|
|
115
|
+
task.session_id,
|
|
116
|
+
task.discretion,
|
|
117
|
+
JSON.stringify(task.sources),
|
|
118
|
+
/连载|续写|长篇|系列/.test(task.title) ? 1 : 0,
|
|
119
|
+
);
|
|
120
|
+
this.db
|
|
121
|
+
.prepare("UPDATE mind_time_tasks SET project_id=? WHERE id=?")
|
|
122
|
+
.run(id, task.id);
|
|
123
|
+
task.project_id = id;
|
|
124
|
+
return this.project(id);
|
|
125
|
+
}
|
|
126
|
+
list({ q = "", project = "", limit = 30, offset = 0 } = {}) {
|
|
127
|
+
return this.db
|
|
128
|
+
.prepare(
|
|
129
|
+
`SELECT w.*,COALESCE(p.kind,(SELECT kind FROM mind_creations WHERE id=w.legacy_creation)) kind,p.title project_title,v.summary,v.sources,length(v.content) characters FROM mind_time_works w LEFT JOIN mind_time_projects p ON p.id=w.project_id JOIN mind_time_versions v ON v.work_id=w.id AND v.version=w.version WHERE (w.title LIKE ? OR p.title LIKE ?) ${project ? "AND w.project_id=?" : ""} ORDER BY w.updated DESC LIMIT ? OFFSET ?`,
|
|
130
|
+
)
|
|
131
|
+
.all(
|
|
132
|
+
"%" + text(q, 80) + "%",
|
|
133
|
+
"%" + text(q, 80) + "%",
|
|
134
|
+
...(project ? [project] : []),
|
|
135
|
+
Math.max(1, Math.min(100, Number(limit))),
|
|
136
|
+
Math.max(0, Number(offset)),
|
|
137
|
+
)
|
|
138
|
+
.map((row) =>
|
|
139
|
+
row.kind === "game"
|
|
140
|
+
? {
|
|
141
|
+
...row,
|
|
142
|
+
title: gameText(row.title),
|
|
143
|
+
summary: gameText(row.summary),
|
|
144
|
+
}
|
|
145
|
+
: row,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
get(id, version) {
|
|
149
|
+
const work = this.db
|
|
150
|
+
.prepare("SELECT * FROM mind_time_works WHERE id=?")
|
|
151
|
+
.get(id);
|
|
152
|
+
if (!work) return null;
|
|
153
|
+
const v = this.db
|
|
154
|
+
.prepare("SELECT * FROM mind_time_versions WHERE work_id=? AND version=?")
|
|
155
|
+
.get(id, Number(version) || work.version);
|
|
156
|
+
const saved = v
|
|
157
|
+
? this.db
|
|
158
|
+
.prepare(
|
|
159
|
+
"SELECT reason FROM mind_time_events WHERE kind='draft' AND json_extract(data,'$.workId')=? AND json_extract(data,'$.version')=? ORDER BY id DESC LIMIT 1",
|
|
160
|
+
)
|
|
161
|
+
.get(id, v.version)
|
|
162
|
+
: null;
|
|
163
|
+
return v
|
|
164
|
+
? {
|
|
165
|
+
...work,
|
|
166
|
+
...v,
|
|
167
|
+
id: work.id,
|
|
168
|
+
versionId: v.id,
|
|
169
|
+
state: saved
|
|
170
|
+
? saved.reason === "保存完成稿"
|
|
171
|
+
? "complete"
|
|
172
|
+
: "draft"
|
|
173
|
+
: work.state,
|
|
174
|
+
sources: parse(v.sources, []),
|
|
175
|
+
provenance: parse(v.provenance, {}),
|
|
176
|
+
versions: this.db
|
|
177
|
+
.prepare(
|
|
178
|
+
"SELECT version,created,title,length(content) characters FROM mind_time_versions WHERE work_id=? ORDER BY version DESC",
|
|
179
|
+
)
|
|
180
|
+
.all(id),
|
|
181
|
+
}
|
|
182
|
+
: null;
|
|
183
|
+
}
|
|
184
|
+
context(task) {
|
|
185
|
+
const project = this.project(task.project_id);
|
|
186
|
+
if (!project) return {};
|
|
187
|
+
const work = task.checkpoint.workId
|
|
188
|
+
? this.get(task.checkpoint.workId)
|
|
189
|
+
: null;
|
|
190
|
+
const chapters = this.list({ project: project.id, limit: 3 }).sort(
|
|
191
|
+
(a, b) => a.ordinal - b.ordinal,
|
|
192
|
+
);
|
|
193
|
+
return {
|
|
194
|
+
project: {
|
|
195
|
+
id: project.id,
|
|
196
|
+
title: project.title,
|
|
197
|
+
why: project.why,
|
|
198
|
+
bible: project.bible,
|
|
199
|
+
summary: project.summary,
|
|
200
|
+
serial: !!project.serial,
|
|
201
|
+
},
|
|
202
|
+
previous: chapters
|
|
203
|
+
.filter((w) => w.id !== work?.id)
|
|
204
|
+
.map((w) => ({
|
|
205
|
+
ordinal: w.ordinal,
|
|
206
|
+
title: w.title,
|
|
207
|
+
summary: w.summary,
|
|
208
|
+
ending: this.get(w.id)?.content.slice(-900),
|
|
209
|
+
})),
|
|
210
|
+
draft: work
|
|
211
|
+
? {
|
|
212
|
+
title: work.title,
|
|
213
|
+
version: work.version,
|
|
214
|
+
summary: work.summary,
|
|
215
|
+
ending: work.content.slice(-1800),
|
|
216
|
+
}
|
|
217
|
+
: null,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
save(task, result, { sources, runId, now, provenance = {} }) {
|
|
221
|
+
if (!this.time.valid(task)) throw Error("任务租约已经改变");
|
|
222
|
+
const title = text(result.title, 80),
|
|
223
|
+
segment = text(result.content, 1800);
|
|
224
|
+
if (!title || !segment || hasCredential(title + segment))
|
|
225
|
+
throw Error("作品正文格式无效");
|
|
226
|
+
const project = this.ensureProject(task, now),
|
|
227
|
+
existing = task.checkpoint.workId
|
|
228
|
+
? this.get(task.checkpoint.workId)
|
|
229
|
+
: null;
|
|
230
|
+
const id = existing?.id || randomUUID(),
|
|
231
|
+
version = (existing?.version || 0) + 1;
|
|
232
|
+
const content =
|
|
233
|
+
task.checkpoint.intent === "revise"
|
|
234
|
+
? segment
|
|
235
|
+
: (existing ? existing.content + "\n\n" : "") + segment;
|
|
236
|
+
if (content.length > 100000) throw Error("单篇过长,请接续新篇章");
|
|
237
|
+
const complete = result.done === true,
|
|
238
|
+
state = complete ? "complete" : "draft";
|
|
239
|
+
if (!existing) {
|
|
240
|
+
const ordinal = this.db
|
|
241
|
+
.prepare(
|
|
242
|
+
"SELECT COALESCE(MAX(ordinal),0)+1 n FROM mind_time_works WHERE project_id=?",
|
|
243
|
+
)
|
|
244
|
+
.get(project.id).n;
|
|
245
|
+
this.db
|
|
246
|
+
.prepare(
|
|
247
|
+
"INSERT INTO mind_time_works(id,created,updated,task_id,project_id,ordinal,title,state,version,session_id,discretion) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
|
|
248
|
+
)
|
|
249
|
+
.run(
|
|
250
|
+
id,
|
|
251
|
+
now,
|
|
252
|
+
now,
|
|
253
|
+
task.id,
|
|
254
|
+
project.id,
|
|
255
|
+
ordinal,
|
|
256
|
+
title,
|
|
257
|
+
state,
|
|
258
|
+
version,
|
|
259
|
+
task.session_id,
|
|
260
|
+
task.discretion,
|
|
261
|
+
);
|
|
262
|
+
} else
|
|
263
|
+
this.db
|
|
264
|
+
.prepare(
|
|
265
|
+
"UPDATE mind_time_works SET updated=?,title=?,state=?,version=? WHERE id=?",
|
|
266
|
+
)
|
|
267
|
+
.run(now, title, state, version, id);
|
|
268
|
+
const summary = text(result.summary || segment, 500),
|
|
269
|
+
checkpoint = {
|
|
270
|
+
...task.checkpoint,
|
|
271
|
+
workId: id,
|
|
272
|
+
version,
|
|
273
|
+
summary,
|
|
274
|
+
next: text(result.next, 240) || "接着上一段往下写",
|
|
275
|
+
segments: (task.checkpoint.segments || 0) + 1,
|
|
276
|
+
};
|
|
277
|
+
this.db
|
|
278
|
+
.prepare(
|
|
279
|
+
"INSERT INTO mind_time_versions(id,work_id,version,created,title,content,summary,sources,run_id,provenance) VALUES (?,?,?,?,?,?,?,?,?,?)",
|
|
280
|
+
)
|
|
281
|
+
.run(
|
|
282
|
+
randomUUID(),
|
|
283
|
+
id,
|
|
284
|
+
version,
|
|
285
|
+
now,
|
|
286
|
+
title,
|
|
287
|
+
content,
|
|
288
|
+
summary,
|
|
289
|
+
JSON.stringify(evidence(sources)),
|
|
290
|
+
runId,
|
|
291
|
+
JSON.stringify(provenance),
|
|
292
|
+
);
|
|
293
|
+
// Only supplied continuity fields change; omitted fields never erase established settings.
|
|
294
|
+
const bible = { ...project.bible };
|
|
295
|
+
for (const key of ["characters", "world", "threads"])
|
|
296
|
+
if (result.bible?.[key] !== undefined)
|
|
297
|
+
bible[key] = text(
|
|
298
|
+
typeof result.bible[key] === "string"
|
|
299
|
+
? result.bible[key]
|
|
300
|
+
: JSON.stringify(result.bible[key]),
|
|
301
|
+
2000,
|
|
302
|
+
);
|
|
303
|
+
this.db
|
|
304
|
+
.prepare(
|
|
305
|
+
"UPDATE mind_time_projects SET updated=?,summary=?,bible=?,revision=revision+1 WHERE id=?",
|
|
306
|
+
)
|
|
307
|
+
.run(now, summary, JSON.stringify(bible), project.id);
|
|
308
|
+
this.db
|
|
309
|
+
.prepare("UPDATE mind_time_tasks SET checkpoint=?,work_id=? WHERE id=?")
|
|
310
|
+
.run(JSON.stringify(checkpoint), id, task.id);
|
|
311
|
+
this.time.event(
|
|
312
|
+
task.id,
|
|
313
|
+
"draft",
|
|
314
|
+
complete ? "保存完成稿" : "保存一个完整小段",
|
|
315
|
+
{ workId: id, version, characters: content.length },
|
|
316
|
+
now,
|
|
317
|
+
);
|
|
318
|
+
return this.get(id);
|
|
319
|
+
}
|
|
320
|
+
settle(task, workId, complete, now) {
|
|
321
|
+
if (!this.time.valid(task)) throw Error("任务租约已经改变");
|
|
322
|
+
const work = this.get(workId);
|
|
323
|
+
if (!work || work.task_id !== task.id) throw Error("活动草稿已不可用");
|
|
324
|
+
if (complete) {
|
|
325
|
+
this.db
|
|
326
|
+
.prepare(
|
|
327
|
+
"UPDATE mind_time_works SET state='complete',updated=? WHERE id=?",
|
|
328
|
+
)
|
|
329
|
+
.run(now, workId);
|
|
330
|
+
this.time.event(
|
|
331
|
+
task.id,
|
|
332
|
+
"draft",
|
|
333
|
+
"保存完成稿",
|
|
334
|
+
{ workId, version: work.version, characters: work.content.length },
|
|
335
|
+
now,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
return this.get(workId);
|
|
339
|
+
}
|
|
340
|
+
suggest(
|
|
341
|
+
projectId,
|
|
342
|
+
{ action = "continue", idea = "", why = "管理台提出的建议" } = {},
|
|
343
|
+
now = this.time.now(),
|
|
344
|
+
) {
|
|
345
|
+
const project = this.project(projectId);
|
|
346
|
+
if (!project) throw Error("项目不存在");
|
|
347
|
+
if (
|
|
348
|
+
!["continue", "revise", "pause", "finish", "direction"].includes(action)
|
|
349
|
+
)
|
|
350
|
+
throw Error("建议类型无效");
|
|
351
|
+
const pending = this.db
|
|
352
|
+
.prepare(
|
|
353
|
+
"SELECT id FROM mind_time_tasks WHERE project_id=? AND state NOT IN ('done','abandoned') LIMIT 1",
|
|
354
|
+
)
|
|
355
|
+
.get(projectId);
|
|
356
|
+
if (pending) return { duplicate: pending.id };
|
|
357
|
+
const result = this.time.tasks.add(
|
|
358
|
+
{
|
|
359
|
+
kind: "suggestion",
|
|
360
|
+
activeOnly: true,
|
|
361
|
+
activity: project.kind,
|
|
362
|
+
title: `${action === "revise" ? "修改" : "接续"}《${project.title}》${text(idea, 120)}`,
|
|
363
|
+
why: text(why, 240),
|
|
364
|
+
sources: project.sources,
|
|
365
|
+
session: project.session_id,
|
|
366
|
+
discretion: project.discretion,
|
|
367
|
+
projectId,
|
|
368
|
+
},
|
|
369
|
+
now,
|
|
370
|
+
);
|
|
371
|
+
if (result.id) {
|
|
372
|
+
const latest = this.list({ project: projectId, limit: 1 })[0];
|
|
373
|
+
this.db.prepare("UPDATE mind_time_tasks SET checkpoint=? WHERE id=?").run(
|
|
374
|
+
JSON.stringify({
|
|
375
|
+
suggestion: {
|
|
376
|
+
action,
|
|
377
|
+
idea: text(idea, 500),
|
|
378
|
+
origin: "admin",
|
|
379
|
+
accepted: null,
|
|
380
|
+
},
|
|
381
|
+
...(action === "revise" && latest
|
|
382
|
+
? { workId: latest.id, intent: "revise" }
|
|
383
|
+
: {}),
|
|
384
|
+
}),
|
|
385
|
+
result.id,
|
|
386
|
+
);
|
|
387
|
+
this.time.event(
|
|
388
|
+
result.id,
|
|
389
|
+
"suggestion",
|
|
390
|
+
"外部建议,等待她决定是否采纳",
|
|
391
|
+
{ action },
|
|
392
|
+
now,
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
return result;
|
|
396
|
+
}
|
|
397
|
+
fragments({ session, now = this.time.now(), cue = [] } = {}) {
|
|
398
|
+
const words = (Array.isArray(cue) ? cue : [cue])
|
|
399
|
+
.map((v) => (typeof v === "string" ? v : JSON.stringify(v)))
|
|
400
|
+
.join(" ");
|
|
401
|
+
return this.list({ limit: 30 })
|
|
402
|
+
.filter(
|
|
403
|
+
(w) =>
|
|
404
|
+
w.created <= now &&
|
|
405
|
+
this.time.visible(
|
|
406
|
+
{ ...w, sources: parse(w.sources, []) },
|
|
407
|
+
session,
|
|
408
|
+
now,
|
|
409
|
+
),
|
|
410
|
+
)
|
|
411
|
+
.map((w) => ({
|
|
412
|
+
...w,
|
|
413
|
+
score: [...new Set(words.match(/[\p{L}\p{N}]{2,}/gu) || [])].filter(
|
|
414
|
+
(term) =>
|
|
415
|
+
(w.title + " " + w.project_title + " " + w.summary).includes(term),
|
|
416
|
+
).length,
|
|
417
|
+
}))
|
|
418
|
+
.sort((a, b) => b.score - a.score || b.updated - a.updated)
|
|
419
|
+
.slice(0, 2)
|
|
420
|
+
.map((w) => ({
|
|
421
|
+
id: w.id,
|
|
422
|
+
title: w.title,
|
|
423
|
+
state: w.state,
|
|
424
|
+
kind: w.kind,
|
|
425
|
+
version: w.version,
|
|
426
|
+
ordinal: w.ordinal,
|
|
427
|
+
summary: text(w.summary, 180),
|
|
428
|
+
fragment: text(this.get(w.id).content, 240),
|
|
429
|
+
characters: w.characters,
|
|
430
|
+
}));
|
|
431
|
+
}
|
|
432
|
+
}
|