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,517 @@
1
+ import limits from '../../shared/tree/limits.cjs';
2
+ const { NAME_MAX } = limits;
3
+ import { taskTransaction } from '../task/write/task-transaction.js';
4
+ import { insertOwnedNode, validateNodePlacement } from './task-node-write.js';
5
+ import { pathFromMap } from '../task/model/task-ownership-path.js';
6
+ import { nodeMap } from '../db/db.js';
7
+ /**
8
+ * 任务树 action:邻接表 + 路径枚举 + sort + depth。
9
+ *
10
+ * 对外仍保持旧前端契约:根节点 id=1、parent=0、type='2'(ROOT_GROUP),
11
+ * 叶子节点 type='1'(TABLE),组节点 type='0'(GROUP)。
12
+ */
13
+
14
+ import {
15
+ now,
16
+ idOf,
17
+ tx,
18
+ logChange,
19
+ rowNode,
20
+ childrenOf,
21
+ treeRoot,
22
+ } from "../db/db.js";
23
+
24
+ const NODE_TYPES = { GROUP: "0", TABLE: "1", ROOT_GROUP: "2" };
25
+ const TYPE_NAMES = { 组: "0", 任务组: "0", 表: "1", 任务表: "1" };
26
+
27
+ function typeCode(value) {
28
+ if (value === undefined || value === null || value === "") return "0";
29
+ if (NODE_TYPES[String(value)] !== undefined) return NODE_TYPES[String(value)];
30
+ if (TYPE_NAMES[String(value)] !== undefined) return TYPE_NAMES[String(value)];
31
+ const s = String(value);
32
+ if (s === "0" || s === "1") return s;
33
+ throw new Error("节点类型非法:" + value);
34
+ }
35
+
36
+ function nodeDescription(value) {
37
+ if (value === undefined || value === null) return "";
38
+ const text = String(value);
39
+ if (text.length > 2000) throw new Error("description 不能超过 2000 个字符");
40
+ return text;
41
+ }
42
+
43
+ function nodeById(db, id, includeDeleted) {
44
+ const key = idOf(id);
45
+ if (!key) return null;
46
+ const sql = "SELECT * FROM task_node WHERE id=?" + (includeDeleted ? "" : " AND deleted_at IS NULL");
47
+ return db.prepare(sql).get(key) || null;
48
+ }
49
+
50
+ function subtreeRows(db, node) {
51
+ return db.prepare(
52
+ "SELECT * FROM task_node WHERE deleted_at IS NULL AND path LIKE ? ORDER BY depth ASC, sort ASC, id ASC",
53
+ ).all(String(node.path) + "%");
54
+ }
55
+
56
+ function withChildren(db, row, seen) {
57
+ if (!row) return null;
58
+ const mark = String(row.id);
59
+ if (seen.has(mark)) return rowNode(row, []);
60
+ seen.add(mark);
61
+ const kids = childrenOf(db, row.id, false);
62
+ const children = kids.map(function (kid) {
63
+ return withChildren(db, kid, seen);
64
+ });
65
+ return rowNode(row, children);
66
+ }
67
+
68
+ function directChildren(db, row) {
69
+ return childrenOf(db, row.id, false).map(function (kid) {
70
+ return rowNode(kid, []);
71
+ });
72
+ }
73
+
74
+ function rootRow(db) {
75
+ return treeRoot(db);
76
+ }
77
+
78
+ function treeAction(db) {
79
+ const root = rootRow(db);
80
+ if (!root) return [];
81
+ return [withChildren(db, root, new Set())];
82
+ }
83
+
84
+ function childrenAction(db, args) {
85
+ const id = idOf(args.id !== undefined ? args.id : args.nodeId);
86
+ if (!id || id === "0") {
87
+ const root = rootRow(db);
88
+ return root ? [withChildren(db, root, new Set())] : [];
89
+ }
90
+ const node = nodeById(db, id, false);
91
+ if (!node) return [];
92
+ return directChildren(db, node);
93
+ }
94
+
95
+ function multipleChildrenAction(db, args) {
96
+ let ids = args.ids;
97
+ if (typeof ids === "string") ids = ids.split(",").map(function (s) { return s.trim(); });
98
+ if (!Array.isArray(ids)) ids = [];
99
+ const out = [];
100
+ for (const raw of ids) {
101
+ const id = idOf(raw);
102
+ if (!id || id === "0") {
103
+ const root = rootRow(db);
104
+ if (root) out.push({ id: "0", children: [withChildren(db, root, new Set())] });
105
+ continue;
106
+ }
107
+ const node = nodeById(db, id, false);
108
+ out.push({ id: id, children: node ? directChildren(db, node) : [] });
109
+ }
110
+ return out;
111
+ }
112
+
113
+ function pathAction(db, args) {
114
+ const id = idOf(args.id !== undefined ? args.id : args.nodeId);
115
+ const node = nodeById(db, id, true);
116
+ if (!node) return [];
117
+ const chain = [];
118
+ let cur = node;
119
+ let guard = 0;
120
+ const seen = new Set();
121
+ while (cur && guard++ < 200) {
122
+ const key = String(cur.id);
123
+ if (seen.has(key)) throw new Error("任务树存在环:" + key);
124
+ seen.add(key);
125
+ chain.push({
126
+ id: key,
127
+ name: String(cur.name || ""),
128
+ type: String(cur.type || "0"),
129
+ description: String(cur.description || ""),
130
+ });
131
+ if (String(cur.parent_id) === "0") break;
132
+ cur = db.prepare("SELECT * FROM task_node WHERE id=?").get(String(cur.parent_id));
133
+ }
134
+ chain.reverse();
135
+ return chain;
136
+ }
137
+
138
+ function createAction(db, args, origin, api) {
139
+ return taskTransaction(db,api,buffered => {
140
+ const parentId = idOf(args.parentId !== undefined ? args.parentId : args.parent);
141
+ const name = String(args.name === undefined || args.name === null || args.name === "" ? "未命名" : args.name).trim();
142
+ const row = insertOwnedNode(db,parentId,name,typeCode(args.type),nodeDescription(args.description),origin);
143
+ buffered.emit('task-nodes-changed',{id:row.id,parentId,op:'create'});
144
+ return {node:rowNode(row,[])};
145
+ });
146
+ }
147
+
148
+ function renameAction(db, args, origin, api, inside = false) {
149
+ if (!inside) return taskTransaction(db,api,buffered => renameAction(db,args,origin,buffered,true));
150
+ const write = (_db,body) => body();
151
+ const id = idOf(args.id !== undefined ? args.id : args.nodeId);
152
+ const node = nodeById(db, id, false);
153
+ if (!node) throw new Error("节点不存在:" + (id || "(空)"));
154
+ const name = String(args.name === undefined || args.name === null ? "" : args.name).trim();
155
+ if (!name) throw new Error("name 不能为空");
156
+ if (name.length > NAME_MAX) throw new Error(`name 不能超过 ${NAME_MAX} 个字符`);
157
+ if (name === String(node.name)) throw new Error("新名称与旧名称相同");
158
+ pathFromMap(node.id,nodeMap(db));
159
+ if (String(node.parent_id) !== '0') validateNodePlacement(db,node.parent_id,name,node.id);
160
+ const ts = now();
161
+ write(db, function () {
162
+ db.prepare("UPDATE task_node SET name=?, updated_at=?, rev=rev+1 WHERE id=?").run(name, ts, id);
163
+ logChange(db, "task_node", id, "upsert", Object.assign({}, node, { name: name, updated_at: ts, rev: Number(node.rev) + 1 }), origin, ts);
164
+ });
165
+ api.emit("task-nodes-changed", { id: id, op: "rename" });
166
+ return { node: rowNode(Object.assign({}, node, { name: name, updated_at: ts, rev: Number(node.rev) + 1 }), []) };
167
+ }
168
+
169
+ function resort(db, parentId, ts, origin) {
170
+ const rows = db.prepare(
171
+ "SELECT * FROM task_node WHERE parent_id=? AND deleted_at IS NULL ORDER BY sort ASC, id ASC",
172
+ ).all(String(parentId));
173
+ let i = 0;
174
+ for (const row of rows) {
175
+ if (Number(row.sort) !== i) {
176
+ const next = Object.assign({}, row, { sort: i, updated_at: ts, rev: Number(row.rev) + 1 });
177
+ db.prepare("UPDATE task_node SET sort=?, updated_at=?, rev=rev+1 WHERE id=?").run(i, ts, String(row.id));
178
+ logChange(db, "task_node", String(row.id), "upsert", next, origin, ts);
179
+ }
180
+ i++;
181
+ }
182
+ }
183
+
184
+ function moveAction(db, args, origin, api, inside = false) {
185
+ if (!inside) return taskTransaction(db,api,buffered => moveAction(db,args,origin,buffered,true));
186
+ const write = (_db,body) => body();
187
+ const sourceId = idOf(args.sourceId !== undefined ? args.sourceId : args.nodeToMoveId);
188
+ const targetId = idOf(args.targetId !== undefined ? args.targetId : args.newParentId);
189
+ const moveType = Number(args.moveType === undefined ? 0 : args.moveType);
190
+ if (moveType !== 0 && moveType !== 2) throw new Error("moveType 只能是 0(作为子节点)或 2(作为同级)");
191
+ const source = nodeById(db, sourceId, false);
192
+ const target = nodeById(db, targetId, false);
193
+ if (!source) throw new Error("要移动的节点不存在:" + (sourceId || "(空)"));
194
+ if (!target) throw new Error("目标节点不存在:" + (targetId || "(空)"));
195
+ if (String(source.id) === String(target.id)) throw new Error("不能移动到自己");
196
+ if (String(source.parent_id) === "0") throw new Error("根节点不能移动");
197
+ if (moveType === 2 && String(target.parent_id) === "0") throw new Error("不能把节点移动到根节点同级");
198
+ if (String(target.path).indexOf(String(source.path)) === 0) throw new Error("不能移动到自己的子树中");
199
+
200
+ const newParent = moveType === 0 ? target : nodeById(db, target.parent_id, false);
201
+ if (!newParent) throw new Error("目标父节点不存在");
202
+ if (String(newParent.type) === "1") throw new Error("任务表下面不能再挂节点");
203
+
204
+ const sourceChain = pathFromMap(source.id,nodeMap(db));
205
+ const parentChain = pathFromMap(newParent.id,nodeMap(db));
206
+ if (parentChain.pathSegments.some(n=>n.id === String(source.id))) throw new Error('不能移动到自己的子树中');
207
+ validateNodePlacement(db,newParent.id,source.name,source.id);
208
+ const oldParentId = String(source.parent_id);
209
+ const siblings = db.prepare(
210
+ "SELECT * FROM task_node WHERE parent_id=? AND deleted_at IS NULL AND id<>? ORDER BY sort ASC, id ASC",
211
+ ).all(String(newParent.id), String(source.id));
212
+ let newSort;
213
+ if (moveType === 0) {
214
+ newSort = siblings.length;
215
+ } else {
216
+ const idx = siblings.findIndex(function (n) { return String(n.id) === String(target.id); });
217
+ newSort = idx < 0 ? siblings.length : idx + 1;
218
+ }
219
+
220
+ const newPath = String(newParent.path) + String(source.id) + "/";
221
+ const delta = Number(newParent.depth) + 1 - Number(source.depth);
222
+ const ts = now();
223
+ const subtree = subtreeRows(db, source);
224
+
225
+ write(db, function () {
226
+ for (const row of subtree) {
227
+ const nextPath = String(row.id) === String(source.id)
228
+ ? newPath
229
+ : newPath + String(row.path).slice(String(source.path).length);
230
+ const nextDepth = Number(row.depth) + delta;
231
+ db.prepare(
232
+ "UPDATE task_node SET parent_id=?, path=?, depth=?, sort=?, updated_at=?, rev=rev+1 WHERE id=?",
233
+ ).run(
234
+ String(row.id) === String(source.id) ? String(newParent.id) : String(row.parent_id),
235
+ nextPath,
236
+ nextDepth,
237
+ String(row.id) === String(source.id) ? newSort : Number(row.sort),
238
+ ts,
239
+ String(row.id),
240
+ );
241
+ logChange(db, "task_node", String(row.id), "upsert", Object.assign({}, row, {
242
+ parent_id: String(row.id) === String(source.id) ? String(newParent.id) : String(row.parent_id),
243
+ path: nextPath,
244
+ depth: nextDepth,
245
+ sort: String(row.id) === String(source.id) ? newSort : Number(row.sort),
246
+ updated_at: ts,
247
+ rev: Number(row.rev) + 1,
248
+ }), origin, ts);
249
+ }
250
+
251
+ const ordered = db.prepare(
252
+ "SELECT * FROM task_node WHERE parent_id=? AND deleted_at IS NULL AND id<>? ORDER BY sort ASC, id ASC",
253
+ ).all(String(newParent.id), String(source.id));
254
+ ordered.splice(Math.max(0, Math.min(newSort, ordered.length)), 0, { id: String(source.id) });
255
+ let i = 0;
256
+ for (const row of ordered) {
257
+ if (String(row.id) === String(source.id)) { i++; continue; }
258
+ if (Number(row.sort) !== i) {
259
+ const next = Object.assign({}, row, { sort: i, updated_at: ts, rev: Number(row.rev) + 1 });
260
+ db.prepare("UPDATE task_node SET sort=?, updated_at=?, rev=rev+1 WHERE id=?").run(i, ts, String(row.id));
261
+ logChange(db, "task_node", String(row.id), "upsert", next, origin, ts);
262
+ }
263
+ i++;
264
+ }
265
+ resort(db, oldParentId, ts, origin);
266
+ });
267
+ api.emit("task-nodes-changed", { id: String(source.id), parentId: String(newParent.id), op: "move" });
268
+ return { ok: true, node: rowNode(nodeById(db, source.id, false), []) };
269
+ }
270
+
271
+ function deleteAction(db, args, origin, api) {
272
+ const id = idOf(args.id !== undefined ? args.id : args.nodeId);
273
+ const node = nodeById(db, id, false);
274
+ if (!node) throw new Error("节点不存在:" + (id || "(空)"));
275
+ if (String(node.parent_id) === "0") throw new Error("不能删除根节点");
276
+ const rows = subtreeRows(db, node);
277
+ const tableIds = rows.filter(function (r) { return String(r.type) === "1"; }).map(function (r) { return String(r.id); });
278
+ let taskCount = 0;
279
+ const ts = now();
280
+ tx(db, function () {
281
+ if (tableIds.length) {
282
+ const marks = tableIds.map(function () { return "?"; }).join(",");
283
+ const tasks = db.prepare("SELECT * FROM task WHERE deleted_at IS NULL AND table_id IN (" + marks + ")").all(...tableIds);
284
+ for (const t of tasks) {
285
+ const tomb = Object.assign({}, t, { deleted_at: ts, updated_at: ts, rev: Number(t.rev) + 1 });
286
+ db.prepare("UPDATE task SET deleted_at=?, updated_at=?, rev=rev+1 WHERE id=?").run(ts, ts, String(t.id));
287
+ logChange(db, "task", String(t.id), "delete", tomb, origin, ts);
288
+ api.emit("tasks-changed", { id: String(t.id), op: "delete" });
289
+ taskCount++;
290
+ }
291
+ }
292
+ for (const row of rows) {
293
+ const tomb = Object.assign({}, row, { deleted_at: ts, updated_at: ts, rev: Number(row.rev) + 1 });
294
+ db.prepare("UPDATE task_node SET deleted_at=?, updated_at=?, rev=rev+1 WHERE id=?").run(ts, ts, String(row.id));
295
+ logChange(db, "task_node", String(row.id), "delete", tomb, origin, ts);
296
+ }
297
+ });
298
+ api.emit("task-nodes-changed", { id: id, op: "delete" });
299
+ return { ok: true, deletedNodes: rows.length, deletedTasks: taskCount };
300
+ }
301
+
302
+ /** 按关键词同时匹配节点名称与描述(表描述命中即可定位表)。 */
303
+ function searchNodesAction(db, args) {
304
+ const keyword = String(args.keyword === undefined || args.keyword === null ? "" : args.keyword).trim();
305
+ if (!keyword) throw new Error("keyword 不能为空");
306
+ const limit = Math.min(200, Math.max(1, Number(args.limit) || 20));
307
+ const withTasks = args.includeTasks !== false;
308
+ const like = "%" + keyword.replace(/[\\%_]/g, function (c) { return "\\" + c; }) + "%";
309
+ const rows = db.prepare(
310
+ "SELECT * FROM task_node WHERE deleted_at IS NULL AND (name LIKE ? ESCAPE '\\' OR description LIKE ? ESCAPE '\\') ORDER BY depth ASC, sort ASC, id ASC LIMIT ?",
311
+ ).all(like, like, limit);
312
+ const nodes = rows.map(function (row) {
313
+ const out = {
314
+ id: String(row.id),
315
+ name: String(row.name || ""),
316
+ type: String(row.type || "0"),
317
+ description: String(row.description || ""),
318
+ depth: Number(row.depth || 0),
319
+ path: pathAction(db, { id: row.id }),
320
+ taskCount: countTasksUnder(db, row),
321
+ };
322
+ if (withTasks && String(row.type) === "1") {
323
+ out.tasks = db.prepare(
324
+ "SELECT id, title, status, priority FROM task WHERE table_id=? AND deleted_at IS NULL ORDER BY publish_date DESC, id DESC LIMIT ?",
325
+ ).all(String(row.id), limit);
326
+ }
327
+ return out;
328
+ });
329
+ return { keyword, total: nodes.length, nodes };
330
+ }
331
+
332
+ /** 某节点子树(含自身)下的任务数。 */
333
+ function countTasksUnder(db, node) {
334
+ const row = db.prepare(
335
+ "SELECT COUNT(*) AS n FROM task t JOIN task_node n ON n.id=t.table_id " +
336
+ "WHERE t.deleted_at IS NULL AND n.deleted_at IS NULL AND n.path LIKE ?",
337
+ ).get(String(node.path) + "%");
338
+ return row ? Number(row.n || 0) : 0;
339
+ }
340
+
341
+ function updateNodeAction(db, args, origin, api, inside = false) {
342
+ if (!inside) return taskTransaction(db,api,buffered => updateNodeAction(db,args,origin,buffered,true));
343
+ const write = (_db,body) => body();
344
+ const id = idOf(args.id !== undefined ? args.id : args.nodeId);
345
+ const node = nodeById(db, id, false);
346
+ if (!node) throw new Error("节点不存在:" + (id || "(空)"));
347
+ pathFromMap(node.id,nodeMap(db));
348
+ const hasName = args.name !== undefined;
349
+ const hasDesc = args.description !== undefined;
350
+ if (!hasName && !hasDesc) throw new Error("需要至少提供 name 或 description");
351
+ const next = Object.assign({}, node);
352
+ if (hasName) {
353
+ const name = String(args.name === null ? "" : args.name).trim();
354
+ if (!name) throw new Error("name 不能为空");
355
+ if (name.length > NAME_MAX) throw new Error(`name 不能超过 ${NAME_MAX} 个字符`);
356
+ if (name !== String(node.name)) {
357
+ if (String(node.parent_id) !== '0') validateNodePlacement(db,node.parent_id,name,node.id);
358
+ next.name = name;
359
+ }
360
+ }
361
+ if (hasDesc) next.description = nodeDescription(args.description);
362
+ const nameChanged = String(next.name) !== String(node.name);
363
+ const descChanged = String(next.description || "") !== String(node.description || "");
364
+ if (!nameChanged && !descChanged) return { ok: true, changed: false, node: rowNode(node, []) };
365
+ const ts = now();
366
+ next.updated_at = ts;
367
+ next.rev = Number(node.rev) + 1;
368
+ write(db, function () {
369
+ db.prepare("UPDATE task_node SET name=?, description=?, updated_at=?, rev=rev+1 WHERE id=?").run(
370
+ String(next.name),
371
+ String(next.description || ""),
372
+ ts,
373
+ id,
374
+ );
375
+ logChange(db, "task_node", id, "upsert", next, origin, ts);
376
+ });
377
+ api.emit("task-nodes-changed", { id: id, op: "update" });
378
+ return { ok: true, changed: true, node: rowNode(next, []) };
379
+ }
380
+
381
+ function renderTree(rows) {
382
+ function walk(nodes, depth) {
383
+ const out = [];
384
+ for (const n of nodes) {
385
+ const desc = String(n.description || "").trim();
386
+ const brief = desc.length > 60 ? desc.slice(0, 60) + "…" : desc;
387
+ out.push(
388
+ " ".repeat(depth) + "- " + n.name + (brief ? "(" + brief + ")" : "") + " (" + n.type + ", id=" + n.id + ")",
389
+ );
390
+ if (n.children && n.children.length) out.push.apply(out, walk(n.children, depth + 1));
391
+ }
392
+ return out;
393
+ }
394
+ return walk(rows || [], 0).join("\n");
395
+ }
396
+
397
+ export function registerTreeActions(api) {
398
+ const db = api.db;
399
+ const action = api.action;
400
+
401
+ action("taskNodes.tree", {
402
+ description: "取完整任务树(根 id=1,含 children)",
403
+ input: { type: "object", properties: {} },
404
+ execute: function () { return treeAction(db); },
405
+ render: renderTree,
406
+ });
407
+
408
+ action("taskNodes.children", {
409
+ description: "取某节点的直接子节点;id=0 返回根节点整棵树",
410
+ input: { type: "object", properties: { id: { type: ["string", "number"] }, nodeId: { type: ["string", "number"], description: "id 的别名" } } },
411
+ execute: function (args) { return childrenAction(db, args || {}); },
412
+ render: renderTree,
413
+ });
414
+
415
+ action("taskNodes.multipleChildren", {
416
+ description: "一次取多个节点的直接子节点,返回 [{id,children}]",
417
+ input: { type: "object", properties: { ids: { type: "array", items: { type: ["string", "number"] } } } },
418
+ execute: function (args) { return multipleChildrenAction(db, args || {}); },
419
+ render: function (rows) {
420
+ return (rows || []).map(function (r) { return r.id + ": " + r.children.length + " 个直接子节点"; }).join("\n");
421
+ },
422
+ });
423
+
424
+ action("taskNodes.path", {
425
+ description: "取根到某节点的路径(含 id/name/type)",
426
+ input: { type: "object", properties: { id: { type: ["string", "number"] }, nodeId: { type: ["string", "number"], description: "id 的别名" } } },
427
+ execute: function (args) { return pathAction(db, args || {}); },
428
+ render: function (rows) {
429
+ return (rows || []).map(function (r) { return r.name; }).join(" / ");
430
+ },
431
+ });
432
+
433
+ action("taskNodes.create", {
434
+ description: "在任务组/根节点下新建 GROUP 或 TABLE 节点",
435
+ input: {
436
+ type: "object",
437
+ properties: {
438
+ parentId: { type: ["string", "number"] },
439
+ type: { type: ["string", "number"] },
440
+ name: { type: "string", maxLength: NAME_MAX, description: `名称最多 ${NAME_MAX} 个字符(JS length)` },
441
+ description: { type: "string", description: "表/组描述,可多行;表描述可用于按关键词定位表,最长 2000 字" },
442
+ },
443
+ },
444
+ execute: function (args, ctx) { return createAction(db, args || {}, ctx.origin, api); },
445
+ render: function (r) { return "已新建节点:" + r.node.name + "(id=" + r.node.id + ")"; },
446
+ });
447
+
448
+ action("taskNodes.rename", {
449
+ description: "重命名任务组/任务表",
450
+ input: { type: "object", properties: { id: { type: ["string", "number"] }, nodeId: { type: ["string", "number"], description: "id 的别名" }, name: { type: "string", maxLength: NAME_MAX, description: `名称最多 ${NAME_MAX} 个字符(JS length)` } } },
451
+ execute: function (args, ctx) { return renameAction(db, args || {}, ctx.origin, api); },
452
+ render: function (r) { return "已重命名为:" + r.node.name; },
453
+ });
454
+
455
+ action("taskNodes.update", {
456
+ description: "更新任务组/任务表的名称与描述(可只传其一;description 传空串即清空)",
457
+ input: {
458
+ type: "object",
459
+ properties: {
460
+ id: { type: ["string", "number"] },
461
+ nodeId: { type: ["string", "number"], description: "id 的别名" },
462
+ name: { type: "string", maxLength: NAME_MAX, description: `新名称,最多 ${NAME_MAX} 个字符(JS length);不传则不改` },
463
+ description: { type: "string", description: "新描述,可多行空串清空;不传则不改,≤2000 字" },
464
+ },
465
+ },
466
+ execute: function (args, ctx) { return updateNodeAction(db, args || {}, ctx.origin, api); },
467
+ render: function (r) {
468
+ if (!r) return "";
469
+ return (r.changed ? "已更新节点:" : "节点无变化:") + r.node.name + (r.node.description ? "(" + String(r.node.description).slice(0, 60) + ")" : "");
470
+ },
471
+ });
472
+
473
+ action("taskNodes.search", {
474
+ description: "按关键词同时匹配节点名称与描述,返回命中的表/组(含路径、描述、任务数;表默认附带其下任务)",
475
+ input: {
476
+ type: "object",
477
+ properties: {
478
+ keyword: { type: "string", description: "关键词,匹配 name 与 description" },
479
+ limit: { type: "number", description: "最多返回多少个节点,默认 20,最大 200" },
480
+ includeTasks: { type: "boolean", description: "命中任务表时是否附带其下任务,默认 true" },
481
+ },
482
+ required: ["keyword"],
483
+ },
484
+ execute: function (args) { return searchNodesAction(db, args || {}); },
485
+ render: function (r) {
486
+ if (!r) return "";
487
+ if (!r.nodes.length) return "未命中任务表/组:" + r.keyword;
488
+ return r.nodes.map(function (n) {
489
+ const names = (n.path || []).map(function (p) { return p.name; }).join(" / ");
490
+ return "- " + n.name + "(" + n.taskCount + " 条任务)" + (n.description ? ":" + String(n.description).slice(0, 80) : "") + "\n 路径:" + names + " · id=" + n.id;
491
+ }).join("\n");
492
+ },
493
+ });
494
+
495
+ action("taskNodes.move", {
496
+ description: "移动节点;moveType=0 作为目标子节点,2 作为目标同级(目标之后)",
497
+ input: {
498
+ type: "object",
499
+ properties: {
500
+ sourceId: { type: ["string", "number"] },
501
+ nodeToMoveId: { type: ["string", "number"] },
502
+ targetId: { type: ["string", "number"] },
503
+ newParentId: { type: ["string", "number"] },
504
+ moveType: { type: ["number", "string"] },
505
+ },
506
+ },
507
+ execute: function (args, ctx) { return moveAction(db, args || {}, ctx.origin, api); },
508
+ render: function (r) { return "已移动节点:" + r.node.id; },
509
+ });
510
+
511
+ action("taskNodes.delete", {
512
+ description: "删除节点及其子树;子树内任务表下的任务会被软删",
513
+ input: { type: "object", properties: { id: { type: ["string", "number"] }, nodeId: { type: ["string", "number"], description: "id 的别名" } } },
514
+ execute: function (args, ctx) { return deleteAction(db, args || {}, ctx.origin, api); },
515
+ render: function (r) { return "已删除 " + r.deletedNodes + " 个节点、" + r.deletedTasks + " 条任务"; },
516
+ });
517
+ }