dsh-workbench-tasks 0.0.0-stage → 5.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +26 -2
  3. package/cordis.patch.yml +7 -0
  4. package/lib/client.js +11313 -0
  5. package/lib/db/db-core.js +432 -0
  6. package/lib/db/db-legacy.js +473 -0
  7. package/lib/db/db.js +97 -0
  8. package/lib/db/ownership-path.js +33 -0
  9. package/lib/db/timeline-schema.js +7 -0
  10. package/lib/db/workspace-schema.js +15 -0
  11. package/lib/index.js +213 -0
  12. package/lib/infra/backup.js +289 -0
  13. package/lib/infra/config.js +462 -0
  14. package/lib/infra/data.js +368 -0
  15. package/lib/infra/sync.js +189 -0
  16. package/lib/publisher/brief.js +76 -0
  17. package/lib/publisher/catalog.js +99 -0
  18. package/lib/publisher/command.js +81 -0
  19. package/lib/stats/activity-calendar.js +72 -0
  20. package/lib/stats/overview.js +99 -0
  21. package/lib/stats/stats-today.js +26 -0
  22. package/lib/stats/stats.js +542 -0
  23. package/lib/stats/timeline-stats.js +174 -0
  24. package/lib/tagging/tag-options.js +99 -0
  25. package/lib/tagging/tagging.js +333 -0
  26. package/lib/task/model/task-difficulty.js +15 -0
  27. package/lib/task/model/task-model.js +256 -0
  28. package/lib/task/model/task-ownership-path.js +22 -0
  29. package/lib/task/model/task-priority.js +33 -0
  30. package/lib/task/read/task-discovery.js +123 -0
  31. package/lib/task/read/task-publish-filter.js +31 -0
  32. package/lib/task/read/task-query.js +351 -0
  33. package/lib/task/read/task-search-schema.js +34 -0
  34. package/lib/task/read/task-view.js +146 -0
  35. package/lib/task/write/task-bulk.js +247 -0
  36. package/lib/task/write/task-claim.js +229 -0
  37. package/lib/task/write/task-create-schema.js +34 -0
  38. package/lib/task/write/task-create.js +178 -0
  39. package/lib/task/write/task-operations.js +207 -0
  40. package/lib/task/write/task-target.js +56 -0
  41. package/lib/task/write/task-transaction.js +18 -0
  42. package/lib/task/write/task-update.js +104 -0
  43. package/lib/tasks.js +454 -0
  44. package/lib/timeline/task-timelines.js +103 -0
  45. package/lib/timeline/timeline-actions.js +69 -0
  46. package/lib/timeline/timeline-migration.js +70 -0
  47. package/lib/timeline/timeline-store.js +57 -0
  48. package/lib/timeline/timeline-sync.js +46 -0
  49. package/lib/tree/task-node-write.js +26 -0
  50. package/lib/tree/tree.js +517 -0
  51. package/lib/workspace/workspace-actions.js +217 -0
  52. package/lib/workspace/workspace-store.js +196 -0
  53. package/package.json +144 -4
  54. package/shared/tree/limits.cjs +3 -0
@@ -0,0 +1,432 @@
1
+ /**
2
+ * 任务子插件:schema、迁移、通用行转换。
3
+ *
4
+ * 只依赖 api.db(node:sqlite DatabaseSync)。不 import 宿主模块。
5
+ */
6
+
7
+ import { TIMELINE_DDL } from "./timeline-schema.js";
8
+ import { WORKSPACE_DDL } from "./workspace-schema.js";
9
+ export const TASK_TABLE = "task";
10
+ export const NODE_TABLE = "task_node";
11
+
12
+ export const TASK_DDL = [
13
+ "CREATE TABLE IF NOT EXISTS task (" +
14
+ "id TEXT PRIMARY KEY," +
15
+ "title TEXT NOT NULL," +
16
+ "description TEXT NOT NULL DEFAULT ''," +
17
+ "status TEXT NOT NULL DEFAULT '0'," +
18
+ "priority INTEGER NOT NULL DEFAULT 0," +
19
+ "difficulty REAL," +
20
+ "review TEXT NOT NULL DEFAULT ''," +
21
+ "table_id TEXT NOT NULL," +
22
+ "timeline_key TEXT," +
23
+ "restart_key TEXT," +
24
+ "publish_date TEXT NOT NULL," +
25
+ "target_date TEXT NOT NULL," +
26
+ "done_date TEXT," +
27
+ "shutdown_date TEXT," +
28
+ "dedupe_key TEXT," +
29
+ "created_at INTEGER NOT NULL," +
30
+ "updated_at INTEGER NOT NULL," +
31
+ "deleted_at INTEGER," +
32
+ "rev INTEGER NOT NULL DEFAULT 1," +
33
+ "owner TEXT," +
34
+ "claimed_at INTEGER," +
35
+ "claim_token TEXT" +
36
+ ");",
37
+ "CREATE INDEX IF NOT EXISTS idx_task_table_id ON task(table_id);",
38
+ "CREATE INDEX IF NOT EXISTS idx_task_status ON task(status);",
39
+ "CREATE INDEX IF NOT EXISTS idx_task_updated_at ON task(updated_at);",
40
+ "CREATE INDEX IF NOT EXISTS idx_task_timeline ON task(timeline_key);",
41
+ "CREATE INDEX IF NOT EXISTS idx_task_restart ON task(restart_key);",
42
+ "CREATE INDEX IF NOT EXISTS idx_task_open ON task(status, target_date) WHERE deleted_at IS NULL;",
43
+ "CREATE INDEX IF NOT EXISTS idx_task_dedupe ON task(dedupe_key) WHERE dedupe_key IS NOT NULL;",
44
+ "CREATE INDEX IF NOT EXISTS idx_task_created_at ON task(created_at);",
45
+ "CREATE INDEX IF NOT EXISTS idx_task_done_date ON task(done_date);",
46
+ ];
47
+
48
+ export const NODE_DDL = [
49
+ "CREATE TABLE IF NOT EXISTS task_node (" +
50
+ "id TEXT PRIMARY KEY," +
51
+ "name TEXT NOT NULL," +
52
+ "description TEXT NOT NULL DEFAULT ''," +
53
+ "type TEXT NOT NULL," +
54
+ "parent_id TEXT NOT NULL DEFAULT '0'," +
55
+ "path TEXT NOT NULL," +
56
+ "sort INTEGER NOT NULL DEFAULT 0," +
57
+ "depth INTEGER NOT NULL DEFAULT 0," +
58
+ "created_at INTEGER NOT NULL," +
59
+ "updated_at INTEGER NOT NULL," +
60
+ "deleted_at INTEGER," +
61
+ "rev INTEGER NOT NULL DEFAULT 1" +
62
+ ");",
63
+ "CREATE INDEX IF NOT EXISTS idx_task_node_parent ON task_node(parent_id);",
64
+ "CREATE INDEX IF NOT EXISTS idx_task_node_path ON task_node(path);",
65
+ "CREATE INDEX IF NOT EXISTS idx_task_node_sort ON task_node(parent_id, sort);",
66
+ "CREATE INDEX IF NOT EXISTS idx_task_node_type ON task_node(type);",
67
+ ];
68
+
69
+ let lastTs = 0;
70
+ let clockNow = null;
71
+
72
+ /** 宿主注入同步时钟;未注入时退回本地单调时钟。 */
73
+ export function bindClock(fn) {
74
+ clockNow = typeof fn === "function" ? fn : null;
75
+ }
76
+
77
+ export function now() {
78
+ if (clockNow) return clockNow();
79
+ const t = Date.now();
80
+ if (t <= lastTs) {
81
+ lastTs += 1;
82
+ return lastTs;
83
+ }
84
+ lastTs = t;
85
+ return t;
86
+ }
87
+
88
+ export function newId() {
89
+ const bytes = new Uint8Array(16);
90
+ globalThis.crypto.getRandomValues(bytes);
91
+ let ts = Date.now();
92
+ for (let i = 5; i >= 0; i--) {
93
+ bytes[i] = ts % 256;
94
+ ts = Math.floor(ts / 256);
95
+ }
96
+ bytes[6] = (bytes[6] & 0x0f) | 0x70;
97
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
98
+ const hex = Array.from(bytes, function (b) {
99
+ return b.toString(16).padStart(2, "0");
100
+ }).join("");
101
+ return hex.slice(0, 8) + "-" + hex.slice(8, 12) + "-" + hex.slice(12, 16) + "-" + hex.slice(16, 20) + "-" + hex.slice(20);
102
+ }
103
+
104
+ export function idOf(v) {
105
+ if (v === undefined || v === null || v === "") return "";
106
+ return String(v);
107
+ }
108
+
109
+ export function todayStr() {
110
+ const d = new Date();
111
+ return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0");
112
+ }
113
+
114
+ export function addDays(dateText, days) {
115
+ const d = new Date(String(dateText) + "T00:00:00");
116
+ d.setDate(d.getDate() + Number(days || 0));
117
+ return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0");
118
+ }
119
+
120
+ export function dateText(value) {
121
+ if (value === undefined || value === null || value === "") return null;
122
+ if (typeof value === "number") {
123
+ if (!Number.isFinite(value)) return null;
124
+ const d = new Date(value);
125
+ if (Number.isNaN(d.getTime())) return null;
126
+ return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0");
127
+ }
128
+ const s = String(value).trim();
129
+ if (/^\d{4}-\d{2}-\d{2}$/.test(s)) return s;
130
+ const normalized = s.replace(" ", "T");
131
+ const d = new Date(normalized);
132
+ if (!Number.isNaN(d.getTime())) {
133
+ return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0");
134
+ }
135
+ return /^\d{4}-\d{2}-\d{2}/.test(s) ? s.slice(0, 10) : null;
136
+ }
137
+
138
+ export function timeMs(value, fallback) {
139
+ if (typeof value === "number" && Number.isFinite(value)) return value;
140
+ if (value === undefined || value === null || value === "") return fallback;
141
+ const s = String(value).trim();
142
+ const d = new Date(s.replace(" ", "T"));
143
+ if (!Number.isNaN(d.getTime())) return d.getTime();
144
+ const d2 = new Date(s);
145
+ if (!Number.isNaN(d2.getTime())) return d2.getTime();
146
+ return fallback;
147
+ }
148
+
149
+ export function tableExists(db, name) {
150
+ try {
151
+ return !!db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name=?").get(String(name));
152
+ } catch {
153
+ return false;
154
+ }
155
+ }
156
+
157
+ export function quoteIdent(name) {
158
+ return '"' + String(name).replace(/"/g, '""') + '"';
159
+ }
160
+
161
+ export function dropIndexesFor(db, table) {
162
+ const rows = db.prepare("SELECT name FROM sqlite_master WHERE type='index' AND tbl_name=? AND name NOT LIKE 'sqlite_%'").all(String(table));
163
+ for (const row of rows) {
164
+ db.exec("DROP INDEX IF EXISTS " + quoteIdent(row.name));
165
+ }
166
+ }
167
+
168
+ export function columnsOf(db, name) {
169
+ if (!tableExists(db, name)) return [];
170
+ try {
171
+ return db.prepare("PRAGMA table_info(" + quoteIdent(name) + ")").all().map(function (r) {
172
+ return String(r.name);
173
+ });
174
+ } catch {
175
+ return [];
176
+ }
177
+ }
178
+
179
+ export function hasColumn(db, table, column) {
180
+ return columnsOf(db, table).indexOf(column) >= 0;
181
+ }
182
+
183
+ export function metaGet(db, key) {
184
+ try {
185
+ const row = db.prepare("SELECT v FROM meta WHERE k=?").get(String(key));
186
+ return row ? String(row.v) : null;
187
+ } catch {
188
+ return null;
189
+ }
190
+ }
191
+
192
+ export function metaSet(db, key, value) {
193
+ db.prepare("INSERT INTO meta(k,v) VALUES(?,?) ON CONFLICT(k) DO UPDATE SET v=excluded.v").run(String(key), String(value));
194
+ }
195
+
196
+ export function tx(db, fn) {
197
+ db.exec("BEGIN IMMEDIATE");
198
+ try {
199
+ const result = fn();
200
+ db.exec("COMMIT");
201
+ return result;
202
+ } catch (e) {
203
+ try {
204
+ db.exec("ROLLBACK");
205
+ } catch {
206
+ /* ignore rollback failure */
207
+ }
208
+ throw e;
209
+ }
210
+ }
211
+
212
+ export function logChange(db, table, rowId, op, payload, origin, ts) {
213
+ if (String(origin || "ui") === "sync") return;
214
+ let value = payload;
215
+ if (String(op) === "delete" && (value === undefined || value === null)) {
216
+ try {
217
+ const row = db.prepare("SELECT * FROM " + String(table) + " WHERE id=?").get(String(rowId));
218
+ if (row) value = row;
219
+ } catch (err) {
220
+ // 表不存在或列不匹配时仍记录墓碑时间和 id。
221
+ }
222
+ }
223
+ const p = value === undefined ? null : typeof value === "string" ? value : JSON.stringify(value);
224
+ db.prepare(
225
+ "INSERT INTO sync_change_log(table_name,row_id,op,payload,origin,updated_at) VALUES(?,?,?,?,?,?)",
226
+ ).run(String(table), String(rowId), String(op), p, String(origin || "ui"), Number(ts || now()));
227
+ }
228
+
229
+ export function rowTask(row) {
230
+ if (!row) return null;
231
+ return {
232
+ id: String(row.id),
233
+ title: String(row.title || ""),
234
+ description: String(row.description || ""),
235
+ status: String(row.status || "0"),
236
+ priority: Number(row.priority || 0),
237
+ difficulty: row.difficulty === null || row.difficulty === undefined ? null : Number(row.difficulty),
238
+ review: String(row.review || ""),
239
+ tableId: String(row.table_id || ""),
240
+ timelineKey: row.timeline_key === null || row.timeline_key === undefined ? null : String(row.timeline_key),
241
+ restartKey: row.restart_key === null || row.restart_key === undefined ? null : String(row.restart_key),
242
+ publishDate: row.publish_date || null,
243
+ targetDate: row.target_date || null,
244
+ doneDate: row.done_date || null,
245
+ shutdownDate: row.shutdown_date || null,
246
+ dedupeKey: row.dedupe_key === null || row.dedupe_key === undefined ? null : String(row.dedupe_key),
247
+ claimedBy: row.owner === null || row.owner === undefined ? null : String(row.owner),
248
+ claimedAt: row.claimed_at === null || row.claimed_at === undefined ? null : Number(row.claimed_at),
249
+ claimToken: row.claim_token === null || row.claim_token === undefined ? null : String(row.claim_token),
250
+ path: [],
251
+ };
252
+ }
253
+
254
+ export function rowNode(row, children) {
255
+ if (!row) return null;
256
+ return {
257
+ id: String(row.id),
258
+ name: String(row.name || ""),
259
+ description: String(row.description || ""),
260
+ type: String(row.type || "0"),
261
+ parent: String(row.parent_id || "0"),
262
+ parentId: String(row.parent_id || "0"),
263
+ path: String(row.path || ""),
264
+ sort: Number(row.sort || 0),
265
+ depth: Number(row.depth || 0),
266
+ children: Array.isArray(children) ? children : [],
267
+ };
268
+ }
269
+
270
+ export function nodeMap(db) {
271
+ const rows = db.prepare("SELECT * FROM task_node").all();
272
+ const map = new Map();
273
+ for (const row of rows) map.set(String(row.id), row);
274
+ map.timelines = new Map(db.prepare('SELECT l.task_id,e.* FROM task_timeline_link l JOIN task_timeline e ON e.id=l.timeline_id JOIN task t ON t.id=l.task_id WHERE l.deleted_at IS NULL AND e.deleted_at IS NULL AND t.deleted_at IS NULL').all().map(r=>[String(r.task_id),r]));
275
+ return map;
276
+ }
277
+
278
+ export function pathForTable(tableId, map) {
279
+ const id = idOf(tableId);
280
+ const node = map.get(id);
281
+ if (!node) return [];
282
+ const ids = String(node.path || "").split("/").filter(Boolean);
283
+ if (!ids.length) {
284
+ const chain = [];
285
+ let cur = node;
286
+ let guard = 0;
287
+ while (cur && guard++ < 100) {
288
+ chain.push(cur);
289
+ if (String(cur.parent_id) === "0") break;
290
+ cur = map.get(String(cur.parent_id));
291
+ }
292
+ chain.reverse();
293
+ return chain.map(function (n) {
294
+ return { id: String(n.id), name: String(n.name || ""), type: String(n.type || "0"), description: String(n.description || "") };
295
+ });
296
+ }
297
+ const out = [];
298
+ for (const part of ids) {
299
+ const n = map.get(part);
300
+ if (n) out.push({ id: String(n.id), name: String(n.name || ""), type: String(n.type || "0"), description: String(n.description || "") });
301
+ }
302
+ return out;
303
+ }
304
+
305
+ import { pathFromMap } from './ownership-path.js';
306
+ export function taskOut(row, map) {
307
+ const t = rowTask(row);
308
+ if (!t) return null;
309
+ const nodes = map || new Map();
310
+ const timeline = nodes.timelines?.get(String(row.id));
311
+ t.timelineId = t.timelineKey = timeline?.id ?? null;
312
+ t.timelineTitle = timeline?.title ?? null;
313
+ t.timelineDescription = timeline?.description ?? null;
314
+ t.path = pathForTable(t.tableId, nodes);
315
+ const table = nodes.get(String(t.tableId));
316
+ t.tableId = String(t.tableId || "");
317
+ t.tableName = table ? String(table.name || "") : "";
318
+ t.tableDescription = table ? String(table.description || "") : "";
319
+ try { const derived = pathFromMap(t.tableId, nodes); t.tablePath = derived.tablePath; t.pathSegments = derived.pathSegments; }
320
+ catch (error) { t.tablePath = null; t.pathSegments = []; t.ownershipDiagnostic = { code:error.code, message:error.message }; }
321
+ return t;
322
+ }
323
+
324
+ export function childrenOf(db, parentId, includeDeleted) {
325
+ const where = includeDeleted ? "" : " AND deleted_at IS NULL";
326
+ return db.prepare(
327
+ "SELECT * FROM task_node WHERE parent_id=?" + where + " ORDER BY sort ASC, id ASC",
328
+ ).all(String(parentId));
329
+ }
330
+
331
+ export function allNodes(db, includeDeleted) {
332
+ const where = includeDeleted ? "" : " WHERE deleted_at IS NULL";
333
+ return db.prepare("SELECT * FROM task_node" + where + " ORDER BY sort ASC, id ASC").all();
334
+ }
335
+
336
+ export function treeRoot(db) {
337
+ const root = db.prepare("SELECT * FROM task_node WHERE parent_id='0' AND deleted_at IS NULL ORDER BY id ASC LIMIT 1").get();
338
+ return root || null;
339
+ }
340
+
341
+ export function pickFreeId(db, start) {
342
+ let n = Math.max(1, Number(start) || 1);
343
+ for (let i = 0; i < 10000; i++) {
344
+ const id = String(n + i);
345
+ if (!db.prepare("SELECT 1 AS x FROM task_node WHERE id=?").get(id)) return id;
346
+ }
347
+ return newId();
348
+ }
349
+
350
+ export function insertNode(db, row) {
351
+ db.prepare(
352
+ "INSERT INTO task_node(id,name,description,type,parent_id,path,sort,depth,created_at,updated_at,deleted_at,rev) " +
353
+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?)",
354
+ ).run(
355
+ String(row.id),
356
+ String(row.name),
357
+ String(row.description === undefined || row.description === null ? "" : row.description),
358
+ String(row.type),
359
+ String(row.parent_id || "0"),
360
+ String(row.path),
361
+ Number(row.sort || 0),
362
+ Number(row.depth || 0),
363
+ Number(row.created_at || now()),
364
+ Number(row.updated_at || row.created_at || now()),
365
+ row.deleted_at === undefined ? null : row.deleted_at,
366
+ Number(row.rev || 1),
367
+ );
368
+ }
369
+
370
+ export function createSchema(db) {
371
+ for (const sql of TASK_DDL) db.exec(sql);
372
+ for (const sql of NODE_DDL) db.exec(sql);
373
+ for (const sql of TIMELINE_DDL) db.exec(sql);
374
+ for (const sql of WORKSPACE_DDL) db.exec(sql);
375
+ }
376
+
377
+ export function seedDefaultTree(db) {
378
+ const ts = now();
379
+ let root = db.prepare("SELECT * FROM task_node WHERE id='1'").get();
380
+ if (!root) {
381
+ root = {
382
+ id: "1",
383
+ name: "全部任务组",
384
+ type: "2",
385
+ parent_id: "0",
386
+ path: "/1/",
387
+ sort: 0,
388
+ depth: 0,
389
+ created_at: ts,
390
+ updated_at: ts,
391
+ deleted_at: null,
392
+ rev: 1,
393
+ };
394
+ insertNode(db, root);
395
+ } else if (String(root.type) !== "2" && String(root.parent_id) === "0") {
396
+ db.prepare("UPDATE task_node SET type='2', path='/1/', depth=0, updated_at=? WHERE id='1'").run(ts);
397
+ }
398
+
399
+ const table = db.prepare("SELECT * FROM task_node WHERE type='1' AND deleted_at IS NULL ORDER BY depth ASC, sort ASC LIMIT 1").get();
400
+ if (!table) {
401
+ const groupId = pickFreeId(db, 2);
402
+ const group = {
403
+ id: groupId,
404
+ name: "默认任务组",
405
+ type: "0",
406
+ parent_id: "1",
407
+ path: "/1/" + groupId + "/",
408
+ sort: 0,
409
+ depth: 1,
410
+ created_at: ts,
411
+ updated_at: ts,
412
+ deleted_at: null,
413
+ rev: 1,
414
+ };
415
+ insertNode(db, group);
416
+ const tableId = pickFreeId(db, Number(groupId) + 1);
417
+ const tableNode = {
418
+ id: tableId,
419
+ name: "默认任务表",
420
+ type: "1",
421
+ parent_id: groupId,
422
+ path: group.path + tableId + "/",
423
+ sort: 0,
424
+ depth: 2,
425
+ created_at: ts,
426
+ updated_at: ts,
427
+ deleted_at: null,
428
+ rev: 1,
429
+ };
430
+ insertNode(db, tableNode);
431
+ }
432
+ }