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.
- package/LICENSE +21 -0
- package/README.md +26 -2
- package/cordis.patch.yml +7 -0
- package/lib/client.js +11313 -0
- package/lib/db/db-core.js +432 -0
- package/lib/db/db-legacy.js +473 -0
- package/lib/db/db.js +97 -0
- package/lib/db/ownership-path.js +33 -0
- package/lib/db/timeline-schema.js +7 -0
- package/lib/db/workspace-schema.js +15 -0
- package/lib/index.js +213 -0
- package/lib/infra/backup.js +289 -0
- package/lib/infra/config.js +462 -0
- package/lib/infra/data.js +368 -0
- package/lib/infra/sync.js +189 -0
- package/lib/publisher/brief.js +76 -0
- package/lib/publisher/catalog.js +99 -0
- package/lib/publisher/command.js +81 -0
- package/lib/stats/activity-calendar.js +72 -0
- package/lib/stats/overview.js +99 -0
- package/lib/stats/stats-today.js +26 -0
- package/lib/stats/stats.js +542 -0
- package/lib/stats/timeline-stats.js +174 -0
- package/lib/tagging/tag-options.js +99 -0
- package/lib/tagging/tagging.js +333 -0
- package/lib/task/model/task-difficulty.js +15 -0
- package/lib/task/model/task-model.js +256 -0
- package/lib/task/model/task-ownership-path.js +22 -0
- package/lib/task/model/task-priority.js +33 -0
- package/lib/task/read/task-discovery.js +123 -0
- package/lib/task/read/task-publish-filter.js +31 -0
- package/lib/task/read/task-query.js +351 -0
- package/lib/task/read/task-search-schema.js +34 -0
- package/lib/task/read/task-view.js +146 -0
- package/lib/task/write/task-bulk.js +247 -0
- package/lib/task/write/task-claim.js +229 -0
- package/lib/task/write/task-create-schema.js +34 -0
- package/lib/task/write/task-create.js +178 -0
- package/lib/task/write/task-operations.js +207 -0
- package/lib/task/write/task-target.js +56 -0
- package/lib/task/write/task-transaction.js +18 -0
- package/lib/task/write/task-update.js +104 -0
- package/lib/tasks.js +454 -0
- package/lib/timeline/task-timelines.js +103 -0
- package/lib/timeline/timeline-actions.js +69 -0
- package/lib/timeline/timeline-migration.js +70 -0
- package/lib/timeline/timeline-store.js +57 -0
- package/lib/timeline/timeline-sync.js +46 -0
- package/lib/tree/task-node-write.js +26 -0
- package/lib/tree/tree.js +517 -0
- package/lib/workspace/workspace-actions.js +217 -0
- package/lib/workspace/workspace-store.js +196 -0
- package/package.json +144 -4
- package/shared/tree/limits.cjs +3 -0
package/lib/tasks.js
ADDED
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import { registerEntityTimelineActions, TIMELINE_ID_SCHEMA, TIMELINE_ID_REQUIRED } from "./timeline/timeline-actions.js";
|
|
2
|
+
import { DIFFICULTY_SCHEMA, DIFFICULTY_GUIDANCE } from "./task/model/task-difficulty.js";
|
|
3
|
+
import { TODAY_STATS_DESCRIPTION } from "./stats/stats-today.js";
|
|
4
|
+
import { TASK_CREATE_SCHEMA } from './task/write/task-create-schema.js';
|
|
5
|
+
import { registerDiscoveryActions, GUIDANCE } from './task/read/task-discovery.js';
|
|
6
|
+
import { renderOwnership } from './task/model/task-ownership-path.js';
|
|
7
|
+
/**
|
|
8
|
+
* 任务 action:纯本地读写。所有方法都不做网络请求。
|
|
9
|
+
*
|
|
10
|
+
* 兼容旧 addon 的 tasks.list/create/update/complete/delete/stats,
|
|
11
|
+
* 同时提供 docs/09 约定的 tasks.* 全量 action。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
idOf,
|
|
16
|
+
} from "./db/db.js";
|
|
17
|
+
// task-query 循环依赖仅在调用期读取 tasks 的函数/常量。
|
|
18
|
+
import { TASK_FILTER_SCHEMA } from "./task/read/task-search-schema.js";
|
|
19
|
+
import { listTasks } from "./task/read/task-query.js";
|
|
20
|
+
import { statsReport, renderStats } from "./stats/stats.js";
|
|
21
|
+
import { registerTagOptionsAction } from "./tagging/tag-options.js";
|
|
22
|
+
import { taskHistory, changelog, renderArray, renderList, renderTaskDetail } from "./task/read/task-view.js";
|
|
23
|
+
import { bulkUpdate, bulkCreate, bulkDelete, renderBulkUpdate, renderBulkCreate, renderBulkDelete } from "./task/write/task-bulk.js";
|
|
24
|
+
import {
|
|
25
|
+
attachTags,
|
|
26
|
+
tagNavStats,
|
|
27
|
+
migrateLegacyTypes,
|
|
28
|
+
} from "./tagging/tagging.js";
|
|
29
|
+
import { getTask, outOne } from "./task/model/task-model.js";
|
|
30
|
+
import { createTask } from "./task/write/task-create.js";
|
|
31
|
+
import { registerTimelineActions, TIMELINE_PAGE_SCHEMA } from "./timeline/task-timelines.js";
|
|
32
|
+
import { updateTask } from "./task/write/task-update.js";
|
|
33
|
+
import { claimTask, claimBatch, grabTask } from "./task/write/task-claim.js";
|
|
34
|
+
import { registerWorkspaceActions } from "./workspace/workspace-actions.js";
|
|
35
|
+
import { attachWorkspaces } from "./workspace/workspace-store.js";
|
|
36
|
+
import { selectedTasks, timelineTasks, restartTasks, timelineRefactor, migrateTasks, sweepShutdown, stats, removeTask, completeTask, exportTasks } from "./task/write/task-operations.js";
|
|
37
|
+
export { tagMissingNotices, withNotices, STATUSES, STATUS_NAMES, asCode, clampDifficulty, dateArg, getTask, requireTable, saveTaskRow, emitChange, writeChange, checkRequiredDates, currentTaskTagIds, resolveTagPatch, PATCH_FIELDS, DIFF_COLUMNS } from "./task/model/task-model.js";
|
|
38
|
+
export { priorityForStatus } from "./task/model/task-priority.js";
|
|
39
|
+
export { createTask } from "./task/write/task-create.js";
|
|
40
|
+
export function registerTaskActions(api) {
|
|
41
|
+
registerDiscoveryActions(api);
|
|
42
|
+
registerTimelineActions(api);
|
|
43
|
+
registerEntityTimelineActions(api);
|
|
44
|
+
registerWorkspaceActions(api);
|
|
45
|
+
const db = api.db;
|
|
46
|
+
const action = api.action;
|
|
47
|
+
|
|
48
|
+
function getTaskAction(args) {
|
|
49
|
+
const id = idOf(args.id || args.taskId);
|
|
50
|
+
if (!id) throw new Error("缺少 id");
|
|
51
|
+
const cur = getTask(db, id);
|
|
52
|
+
if (!cur) throw new Error("任务不存在:" + id);
|
|
53
|
+
const task = outOne(db, cur);
|
|
54
|
+
attachTags(db, api, task);
|
|
55
|
+
attachWorkspaces(db, [task]);
|
|
56
|
+
return { task: task };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const PATCH_SCHEMA = {
|
|
60
|
+
type: "object",
|
|
61
|
+
description: "要写入的字段;未列出的字段会被拒绝",
|
|
62
|
+
properties: {
|
|
63
|
+
title: { type: "string" },
|
|
64
|
+
description: { type: "string" },
|
|
65
|
+
review: { type: "string", description: "完成后的感受/复盘(自由文本)" },
|
|
66
|
+
status: { type: ["string", "number"], description: "0=已开启(ACTIVE,也接受 open)、1=已关闭(SHUTDOWN)、2=已完成(DONE,也接受 done)、3=已重启(RESTARTED,创建时不允许);改成 2 时 priority 自动清零并补 doneDate" },
|
|
67
|
+
priority: { type: "number", description: "1..10 整数,数值越大越紧急(>=6 进「紧急」页签);已开启任务传 0 会报错;任务改为 SHUTDOWN/DONE 时写入值被忽略并清零" },
|
|
68
|
+
difficulty: DIFFICULTY_SCHEMA,
|
|
69
|
+
publishDate: { type: "string", description: "YYYY-MM-DD" },
|
|
70
|
+
targetDate: { type: "string", description: "YYYY-MM-DD" },
|
|
71
|
+
doneDate: { type: ["string", "null"], description: "YYYY-MM-DD" },
|
|
72
|
+
shutdownDate: { type: ["string", "null"], description: "YYYY-MM-DD" },
|
|
73
|
+
tableId: { type: ["string", "number"], description: "目标任务表 id" },
|
|
74
|
+
tagIds: { type: "array", items: { type: "string" }, description: "用这批标签 id 整体替换当前标签(需要标签域);id 不存在报错并列出可用标签" },
|
|
75
|
+
tags: { type: "array", items: { type: "string" }, description: "按标签名整体替换;默认自动创建未知名字,form.autoCreateTags=false 时报错并列出可用标签" },
|
|
76
|
+
addTags: { type: "array", items: { type: "string" }, description: "增量添加标签(按名字,未知名字受 form.autoCreateTags 控制),保留各任务原有标签" },
|
|
77
|
+
removeTags: { type: "array", items: { type: "string" }, description: "增量移除标签(按名字),其余标签保留" },
|
|
78
|
+
addTagIds: { type: "array", items: { type: "string" }, description: "增量添加标签 id" },
|
|
79
|
+
removeTagIds: { type: "array", items: { type: "string" }, description: "增量移除标签 id" },
|
|
80
|
+
},
|
|
81
|
+
additionalProperties: false,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
action("tasks.list", {
|
|
85
|
+
description: "统一检索任务:statuses 各状态与 urgent 紧急谓词 OR;不传 statuses/urgent 时不过滤状态,返回全部状态(含 3 已重启),空数组且 urgent=false 同样为全部。旧参数 tab 与 filterRestarted 已移除,传入会报错并给出等价写法。任务表/任务组/关键词(标题、描述、感受)/标签/优先级/目标日期/发布、完成、关闭日期范围之间 AND;完成范围限定 status=2,关闭范围限定 status=1,反序交换并返回 notices;sort 支持 priority/target/publish/updated/created(前缀 - 表示降序,例如 sort=-priority 是数值越大越紧急的在前);不传 sort 时未分页按 priority 降序、分页时按 publishDate 降序;返回里带 filter 回显实际生效条件与排序;分页语义:total=筛选后真实总数、returned=本次返回条数、truncated=是否被截断,pageSize 上限 200(超出回显 filter.pageSizeCapped)",
|
|
86
|
+
input: {
|
|
87
|
+
type: "object",
|
|
88
|
+
properties: {
|
|
89
|
+
...TASK_FILTER_SCHEMA,
|
|
90
|
+
pageNumber: { type: "number", description: "分页页码,从 1 开始;传 pageNumber 或 pageSize 即分页" },
|
|
91
|
+
pageSize: { type: "number", description: "每页条数(默认 12,最大 200);传 pageNumber 或 pageSize 即分页" },
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
examples: [{ statuses: [1, 2], urgent: true, pageSize: 50 }, { statuses: [2], doneFrom: "2026-07-01", doneTo: "2026-07-01" }, { statuses: [0, 4], priorityMin: 8, sort: "-priority", pageSize: 50 }, { tagIds: ["3"], status: "open" }],
|
|
95
|
+
execute: function (args) { return listTasks(db, args || {}, api); },
|
|
96
|
+
render: renderList,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
action("tasks.get", {
|
|
100
|
+
description: "按 id 取单个任务(含任务树路径、表职责、时间线、标签、完整描述)。回执给全文描述与归属路径,便于核对历史任务意图;历史描述不代表当前实现。",
|
|
101
|
+
input: {
|
|
102
|
+
type: "object",
|
|
103
|
+
properties: {
|
|
104
|
+
id: { type: "string", description: "任务 id" },
|
|
105
|
+
taskId: { type: ["string", "number"], description: "id 的别名" },
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
examples: [{ id: "1001" }],
|
|
109
|
+
execute: function (args) { return getTaskAction(args || {}); },
|
|
110
|
+
render: renderTaskDetail,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
action("tasks.create", {
|
|
114
|
+
description: "创建任务必须显式 tableId 或 tablePath(恰好一个),无目标报 TABLE_REQUIRED 且零写入。先 tasks.capabilities/resolveTable 核对职责、完整路径与描述;拿不准询问用户;仅获授权才createMissing+pathDescriptions。source=0 普通任务,1 时间线任务,2 重启任务。date 字段均为 YYYY-MM-DD;重启需优先级 >0(1–10,缺省 1);关闭/完成自动清零" + GUIDANCE.join(" "),
|
|
115
|
+
input: TASK_CREATE_SCHEMA,
|
|
116
|
+
examples: [{ title: "整理发布清单", tableId: "已核对的任务表id", priority: 7, difficulty: 2, targetDate: "2026-11-10" }],
|
|
117
|
+
execute: function (args, ctx) { return createTask(db, args || {}, ctx.origin, api); },
|
|
118
|
+
render: renderOwnership,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
action("tasks.update", {
|
|
122
|
+
description: "更新单个任务。两种用法:①type 是「更新模式」而不是任务类型(0 状态 / 1 优先级 / 2 标题 / 3 描述 / 4 感受),只有该模式对应的字段生效,其余参数被忽略;②省略 type 直接传字段(含 tags 标签名,tags/tagIds 为整体替换)。批量请用 tasks.bulkUpdate。" + DIFFICULTY_GUIDANCE,
|
|
123
|
+
input: {
|
|
124
|
+
type: "object",
|
|
125
|
+
required: ["id"],
|
|
126
|
+
properties: {
|
|
127
|
+
id: { type: "string" },
|
|
128
|
+
taskId: { type: ["string", "number"] },
|
|
129
|
+
type: { type: ["string", "number"], description: "更新模式 0/1/2/3/4(4=感受;不是任务字段;改标签请传 tags 或 tagIds)" },
|
|
130
|
+
status: { type: ["string", "number"], description: "0/ACTIVE、1/SHUTDOWN、2/DONE(改成 DONE 时 priority 自动清零)" },
|
|
131
|
+
date: { type: "string" },
|
|
132
|
+
priority: { type: "number", description: "1..10;目标状态为 1/2 时写入被忽略(恒为 0);目标状态为 0 时传 0 报错" },
|
|
133
|
+
title: { type: "string" },
|
|
134
|
+
description: { type: "string" },
|
|
135
|
+
review: { type: "string", description: "完成后的感受/复盘(自由文本);给出时按字段更新,或配合 type=4" },
|
|
136
|
+
difficulty: DIFFICULTY_SCHEMA,
|
|
137
|
+
publishDate: { type: "string" },
|
|
138
|
+
targetDate: { type: "string" },
|
|
139
|
+
doneDate: { type: "string" },
|
|
140
|
+
shutdownDate: { type: "string" },
|
|
141
|
+
tableId: { type: ["string", "number"] },
|
|
142
|
+
tagIds: { type: "array", items: { type: "string" }, description: "用这批标签 id 整体替换当前标签;id 不存在报错并列出可用标签" },
|
|
143
|
+
tags: { type: "array", items: { type: "string" }, description: "按标签名整体替换;默认自动创建未知名字,form.autoCreateTags=false 时报错并列出可用标签" },
|
|
144
|
+
addTags: { type: "array", items: { type: "string" }, description: "增量添加标签名(保留原有标签);未知名字同样受 form.autoCreateTags 控制" },
|
|
145
|
+
removeTags: { type: "array", items: { type: "string" }, description: "增量移除标签名(保留其他标签);名字不存在时报错" },
|
|
146
|
+
addTagIds: { type: "array", items: { type: "string" }, description: "增量添加标签 id;id 不存在报错并列出可用标签" },
|
|
147
|
+
removeTagIds: { type: "array", items: { type: "string" }, description: "增量移除标签 id;id 不存在报错" },
|
|
148
|
+
workspaceIds: { type: "array", items: { type: "string" }, description: "用这批工作区 id 整体替换当前关联(事务内先软删旧关联再建新关联);不传则不动;任一 id 不存在/已删除则整次更新回滚" },
|
|
149
|
+
claimToken: { type: "string", description: "接取凭据(tasks.claim/grab 返回):任务当前 status=4 时用于 ABA 校验,不匹配返回 TASK_CLAIM_LOST;不传视为人工放行" },
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
examples: [{ id: "1001", status: "done", difficulty: 3 }, { id: "1001", addTags: ["开发"], difficulty: 2 }],
|
|
153
|
+
execute: function (args, ctx) { return updateTask(db, args || {}, ctx.origin, api); },
|
|
154
|
+
render: r => renderOwnership(r,"已更新"),
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
action("tasks.bulkUpdate", {
|
|
158
|
+
description: "按 id 批量更新任务:同一个 patch 原样套用到每个 id;tags/tagIds 是整体替换,会覆盖各任务原有标签,只想加/减标签请用 addTags/addTagIds/removeTags/removeTagIds;dryRun:true 只返回将变更的预览,不写库",
|
|
159
|
+
input: {
|
|
160
|
+
type: "object",
|
|
161
|
+
required: ["ids", "patch"],
|
|
162
|
+
properties: {
|
|
163
|
+
ids: { type: "array", items: { type: "string" }, description: "任务 id 列表" },
|
|
164
|
+
patch: PATCH_SCHEMA,
|
|
165
|
+
dryRun: { type: "boolean", description: "true 时只预览,不写库、不发事件" },
|
|
166
|
+
skipMissing: { type: "boolean", description: "true 时忽略不存在的 id(默认报错)" },
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
examples: [
|
|
170
|
+
{ ids: ["1001", "1002"], patch: { status: "done", priority: 6, difficulty: 3 }, dryRun: true },
|
|
171
|
+
{ ids: ["1001", "1002"], patch: { targetDate: "2026-11-15", difficulty: 2 } },
|
|
172
|
+
],
|
|
173
|
+
execute: function (args, ctx) { return bulkUpdate(db, args || {}, ctx.origin, api); },
|
|
174
|
+
render: renderBulkUpdate,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
action("tasks.claim", {
|
|
178
|
+
description: "按 id 接取单个任务:CAS 抢占(条件 UPDATE,失败不留半改),成功把 status 改为 4(已接取)并写入 owner/claimed_at/claim_token,保留原优先级;任务不存在返回 TASK_NOT_FOUND,非「已开启」返回 TASK_NOT_CLAIMABLE(附当前 status 与 nextActions)。owner 缺省 local,agent 可传 agent:<sessionId>。返回 claimed:true、id、task、previousStatus 与新 claimToken;后续自动化写操作可带 claimToken,失效返回 TASK_CLAIM_LOST。",
|
|
179
|
+
input: {
|
|
180
|
+
type: "object",
|
|
181
|
+
required: ["id"],
|
|
182
|
+
properties: {
|
|
183
|
+
id: { type: ["string", "number"], description: "任务 id" },
|
|
184
|
+
owner: { type: "string", description: "接取者标识,建议 local(人工)或 agent:<sessionId>;缺省 local,最长 120 字符" },
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
examples: [{ id: "1001" }, { id: "1001", owner: "agent:session-abc" }],
|
|
188
|
+
execute: function (args, ctx) { return claimTask(db, args || {}, ctx.origin, api); },
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
action("tasks.claimBatch", {
|
|
192
|
+
description: "批量接取:ids 去重保序,整批在一个 BEGIN IMMEDIATE 事务里逐条 CAS;单条失败不抛错、只进 failed(reason=already_claimed/not_claimable/not_found/deleted/busy),成功项不因其他失败回滚。dryRun:true 只返回可接取/不可接取预览,不写库。整批结束只发一次 tasks-changed 聚合事件。返回 requested、claimed[]、failed[]、counts{claimed,alreadyClaimed,notClaimable,notFound,deleted,busy}。",
|
|
193
|
+
input: {
|
|
194
|
+
type: "object",
|
|
195
|
+
required: ["ids"],
|
|
196
|
+
properties: {
|
|
197
|
+
ids: { type: "array", items: { type: ["string", "number"] }, description: "任务 id 列表(去重保序)" },
|
|
198
|
+
owner: { type: "string", description: "接取者标识,缺省 local" },
|
|
199
|
+
dryRun: { type: "boolean", description: "true 时只预览,不写库、不发事件" },
|
|
200
|
+
skipMissing: { type: "boolean", description: "保留兼容:不存在/已删除的 id 始终进 failed.notFound/deleted,不抛错" },
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
examples: [{ ids: ["1001", "1002"], dryRun: true }, { ids: ["1001", "1002"], owner: "agent:session-abc" }],
|
|
204
|
+
execute: function (args, ctx) { return claimBatch(db, args || {}, ctx.origin, api); },
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
action("tasks.grab", {
|
|
208
|
+
description: "抢夺(强制占有)单个任务,把 status 0 或 4 覆盖为 4 并换新 claim_token;返回 previousStatus/previousOwner/previousClaimToken 供审计,reason 写入变更日志。仅在用户明确要求「抢夺/强取/占用某任务」时调用;普通开工一律用 tasks.claim,不得自行判断后抢夺。UI 没有任何入口。",
|
|
209
|
+
input: {
|
|
210
|
+
type: "object",
|
|
211
|
+
required: ["id"],
|
|
212
|
+
properties: {
|
|
213
|
+
id: { type: ["string", "number"], description: "任务 id" },
|
|
214
|
+
owner: { type: "string", description: "接取者标识,缺省 local" },
|
|
215
|
+
reason: { type: "string", description: "抢夺原因,写入变更日志供审计" },
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
examples: [{ id: "1001", owner: "agent:session-abc", reason: "用户要求接手" }],
|
|
219
|
+
execute: function (args, ctx) { return grabTask(db, args || {}, ctx.origin, api); },
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
action("tasks.bulkCreate", {
|
|
223
|
+
description: "一次创建多条任务,每条必须显式tableId或tablePath(恰好一个);发现、选择、新路径授权规则与tasks.create完全相同;单事务含新节点、标签、日志、重启,全成或全不成。dryRun:true真校验后回滚,返回wouldCreateNodes及temporaryIds;created仅统计非deduped新任务" + GUIDANCE.join(" "),
|
|
224
|
+
input: {
|
|
225
|
+
type: "object",
|
|
226
|
+
required: ["records"],
|
|
227
|
+
properties: {
|
|
228
|
+
records: {
|
|
229
|
+
type: "array",
|
|
230
|
+
description: "每条字段同 tasks.create",
|
|
231
|
+
items: TASK_CREATE_SCHEMA,
|
|
232
|
+
},
|
|
233
|
+
dryRun: { type: "boolean", description: "true 时只校验预览,不写库、不发事件" },
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
examples: [
|
|
237
|
+
{ records: [{ title: "任务 A", tableId:"已核对的任务表id", priority: 6, difficulty: 2 }, { title: "任务 B", tableId:"已核对的任务表id", difficulty: 3, targetDate: "2026-11-12" }], dryRun: true },
|
|
238
|
+
],
|
|
239
|
+
execute: function (args, ctx) { return bulkCreate(db, args || {}, ctx.origin, api); },
|
|
240
|
+
render: renderBulkCreate,
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
action("tasks.bulkDelete", {
|
|
244
|
+
description: "按 id 批量软删除任务(保留同步墓碑);dryRun:true 只返回将删除的清单",
|
|
245
|
+
input: {
|
|
246
|
+
type: "object",
|
|
247
|
+
properties: {
|
|
248
|
+
ids: { type: "array", items: { type: "string" }, description: "任务 id 列表" },
|
|
249
|
+
dryRun: { type: "boolean", description: "true 时只预览,不写库" },
|
|
250
|
+
skipMissing: { type: "boolean", description: "true 时忽略不存在的 id(默认报错)" },
|
|
251
|
+
},
|
|
252
|
+
required: ["ids"],
|
|
253
|
+
},
|
|
254
|
+
examples: [{ ids: ["1001", "1002"], dryRun: true }],
|
|
255
|
+
execute: function (args, ctx) { return bulkDelete(db, args || {}, ctx.origin, api); },
|
|
256
|
+
render: renderBulkDelete,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
action("tasks.history", {
|
|
260
|
+
description: "读单个任务的变更历史(来自 sync_change_log,倒序)",
|
|
261
|
+
input: {
|
|
262
|
+
type: "object",
|
|
263
|
+
properties: {
|
|
264
|
+
id: { type: "string" },
|
|
265
|
+
taskId: { type: ["string", "number"] },
|
|
266
|
+
limit: { type: "number", description: "默认 50,最大 500" },
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
examples: [{ id: "1001", limit: 20 }],
|
|
270
|
+
execute: function (args) { return taskHistory(db, args || {}); },
|
|
271
|
+
render: function (r) { return "任务 " + r.id + " 变更 " + r.count + " 条"; },
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
action("tasks.changelog", {
|
|
275
|
+
description: "增量读变更日志(task / tag_mapping),可按 since 与表过滤",
|
|
276
|
+
input: {
|
|
277
|
+
type: "object",
|
|
278
|
+
properties: {
|
|
279
|
+
since: { type: "string", description: "只返回该时间点之后的记录(ISO 或 YYYY-MM-DD)" },
|
|
280
|
+
tables: { type: "array", items: { type: "string" }, description: "默认 task / tag_mapping" },
|
|
281
|
+
limit: { type: "number", description: "默认 100,最大 1000" },
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
examples: [{ since: "2026-11-07T00:00:00.000Z", limit: 200 }],
|
|
285
|
+
execute: function (args) { return changelog(db, args || {}); },
|
|
286
|
+
render: function (r) { return "变更 " + r.count + " 条" + (r.since ? "(since " + r.since + ")" : ""); },
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
action("tasks.export", {
|
|
290
|
+
description: "导出任务:format=json|csv|markdown;非空 ids 优先,否则复用 tasks.list 的过滤条件与 filter/notices(含 statuses/urgent OR、三日期范围 AND、标题/描述/感受关键词)。保持列表默认限制:未传分页参数时最多 500 条;不接受分页参数",
|
|
291
|
+
input: {
|
|
292
|
+
type: "object",
|
|
293
|
+
properties: {
|
|
294
|
+
format: { type: "string", enum: ["json", "csv", "markdown"], description: "默认 json" },
|
|
295
|
+
ids: { type: "array", items: { type: "string" } },
|
|
296
|
+
...TASK_FILTER_SCHEMA,
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
examples: [{ format: "csv", statuses: [] }, { format: "markdown", ids: ["1001", "1002"] }],
|
|
300
|
+
execute: function (args) { return exportTasks(db, args || {}, api); },
|
|
301
|
+
render: function (r) { return "已导出 " + r.count + " 条任务(" + r.format + ")"; },
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
action("tasks.selected", {
|
|
305
|
+
description: "按 id 列表批量取任务",
|
|
306
|
+
input: { type: "object", properties: { ids: { type: "array", items: { type: "string" } } } },
|
|
307
|
+
execute: function (args) { return selectedTasks(db, args || {}); },
|
|
308
|
+
render: function (rows) { return "选中任务 " + rows.length + " 条"; },
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
action("tasks.timeline", {
|
|
312
|
+
description: "取某条时间线有效任务,按publishDate/id正序;省略分页参数返回数组,传pageNumber或pageSize返回{records,total,current,size},默认20条。排除已重启及已删除任务。",
|
|
313
|
+
input: { type: "object", properties: { ...TIMELINE_ID_SCHEMA, ...TIMELINE_PAGE_SCHEMA } },
|
|
314
|
+
execute: function (args) { return timelineTasks(db, args || {}); },
|
|
315
|
+
render: function (r) { return renderArray(Array.isArray(r) ? r : r.records); },
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
action("tasks.restart", {
|
|
319
|
+
description: "取某条重启链上的任务",
|
|
320
|
+
input: { type: "object", properties: { restartKey: { type: ["string", "number"] } } },
|
|
321
|
+
execute: function (args) { return restartTasks(db, args || {}); },
|
|
322
|
+
render: renderArray,
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
action("tasks.timelineRefactor", {
|
|
326
|
+
description: "把ids指定任务或timelineKeys指定整条时间线归并到timelineKey,ids和timelineKeys互斥。整线模式排除已重启/删除成员,目标必须属于选中keys;无成员key报错。dryRun不写入,返回wouldUpdate真实迁移数;整线回执total/selectedCount含目标已有成员,不返回全量ids,执行返回updated。ids模式兼容原回执。",
|
|
327
|
+
input: {
|
|
328
|
+
type: "object",
|
|
329
|
+
properties: {
|
|
330
|
+
ids: { type: "array", items: { type: "string" } },
|
|
331
|
+
...TIMELINE_ID_SCHEMA,
|
|
332
|
+
timelineKeys: { type: "array", minItems: 1, items: { type: "string" }, description: "完整选中时间线key,包含目标;与ids互斥,不受list分页上限影响" },
|
|
333
|
+
dryRun: { type: "boolean" },
|
|
334
|
+
},
|
|
335
|
+
anyOf: TIMELINE_ID_REQUIRED,
|
|
336
|
+
},
|
|
337
|
+
execute: function (args, ctx) { return timelineRefactor(db, args || {}, ctx.origin, api); },
|
|
338
|
+
render: function (r) {
|
|
339
|
+
return r.dryRun ? "预览:将归并 " + r.wouldUpdate + " 条任务到时间线 " + r.timelineKey : "已归并 " + r.updated + " 条任务到时间线 " + r.timelineKey;
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
action("tasks.migrate", {
|
|
344
|
+
description: "把一批任务迁移到另一个任务表;dryRun:true 只返回将影响的 id",
|
|
345
|
+
input: {
|
|
346
|
+
type: "object",
|
|
347
|
+
properties: {
|
|
348
|
+
ids: { type: "array", items: { type: "string" } },
|
|
349
|
+
tableId: { type: ["string", "number"] },
|
|
350
|
+
dryRun: { type: "boolean" },
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
execute: function (args, ctx) { return migrateTasks(db, args || {}, ctx.origin, api); },
|
|
354
|
+
render: renderBulkUpdate,
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
action("tasks.sweepShutdown", {
|
|
358
|
+
description: "把 targetDate 已过的 ACTIVE 任务批量关闭(默认今天);dryRun:true 先看会影响哪些任务",
|
|
359
|
+
input: {
|
|
360
|
+
type: "object",
|
|
361
|
+
properties: {
|
|
362
|
+
date: { type: "string", description: "截止日期 YYYY-MM-DD,默认今天" },
|
|
363
|
+
dryRun: { type: "boolean", description: "true 时只返回将关闭的任务清单" },
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
examples: [{ dryRun: true }, { date: "2026-11-07" }],
|
|
367
|
+
execute: function (args, ctx) { return sweepShutdown(db, args || {}, ctx.origin, api); },
|
|
368
|
+
render: function (r) {
|
|
369
|
+
if (r.dryRun) {
|
|
370
|
+
const lines = ["预览:将关闭 " + r.wouldUpdate + " 条到期任务(" + r.date + ")"];
|
|
371
|
+
for (const it of r.items) lines.push("- " + it.title + "(target " + it.targetDate + ",id=" + it.id + ")");
|
|
372
|
+
return lines.join("\n");
|
|
373
|
+
}
|
|
374
|
+
return "已关闭 " + r.updated + " 条到期任务(" + r.date + ")";
|
|
375
|
+
},
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
action("tasks.tags", {
|
|
379
|
+
description: "标签导航数据:每个标签的任务数(按状态拆分),外加「未打标签」计数。标签域未安装时 available=false",
|
|
380
|
+
input: { type: "object", properties: {} },
|
|
381
|
+
execute: function () {
|
|
382
|
+
return tagNavStats(db, api);
|
|
383
|
+
},
|
|
384
|
+
render: function (r) {
|
|
385
|
+
if (!r.available) return "标签域未安装";
|
|
386
|
+
const parts = r.tags.map(function (t) { return t.name + " " + t.total; });
|
|
387
|
+
parts.push("未打标签 " + r.untagged.total);
|
|
388
|
+
return "标签 " + r.tags.length + " 个:" + parts.join(",");
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
registerTagOptionsAction(action, db, api, migrateLegacyTypes);
|
|
393
|
+
|
|
394
|
+
action("tasks.stats", {
|
|
395
|
+
description: "任务统计:保留 7 个计数器(today 为今日到期的已开启任务数,含已接取);groupBy=tag/tableId/status/priority 时额外返回 groups(可按 from/to 限定 targetDate);另返回 todayActivity 对象:" + TODAY_STATS_DESCRIPTION,
|
|
396
|
+
input: {
|
|
397
|
+
type: "object",
|
|
398
|
+
properties: {
|
|
399
|
+
groupBy: { type: "string", enum: ["tag", "tableId", "status", "priority"] },
|
|
400
|
+
from: { type: "string", description: "targetDate >= 该日期" },
|
|
401
|
+
to: { type: "string", description: "targetDate <= 该日期" },
|
|
402
|
+
},
|
|
403
|
+
},
|
|
404
|
+
examples: [{ groupBy: "priority" }, { groupBy: "tag", from: "2026-11-01", to: "2026-11-30" }],
|
|
405
|
+
execute: function (args) { return stats(db, args || {}, api); },
|
|
406
|
+
render: function (s) {
|
|
407
|
+
const base = "已开启 " + s.active + " / 已关闭 " + s.shutdown + " / 已完成 " + s.done + " / 已重启 " + s.restarted + " / 逾期 " + s.overdue;
|
|
408
|
+
if (!s.groupBy) return base;
|
|
409
|
+
return base + "\n" + s.groupBy + " 分布:" + s.groups.map(function (g) { return (g.label || g.key || "-") + "=" + g.count; }).join(",");
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
action("tasks.statsReport", {
|
|
414
|
+
description: "任务统计报表:总量/状态/标签/优先级/难度、趋势、逾期账龄、近期发布/完成/关闭清单(lists.recentPublish 不限状态,按发布日期倒序;lists.recentDone / lists.recentShutdown 分别按完成/关闭日期倒序;lists.overdue 为逾期清单)、任务表 / 任务组 / 工作区排行(rankings.workspaces 为存量口径,同一任务按存活关联在每个工作区各计一次,各行合计会大于 totals.total;rankings.noWorkspace 是未关联任何存活工作区的任务数,属正常状态,与 tasks.list 的 noWorkspace 同口径);另返回 today 对象:" + TODAY_STATS_DESCRIPTION,
|
|
415
|
+
input: {
|
|
416
|
+
type: "object",
|
|
417
|
+
properties: {
|
|
418
|
+
range: { type: "string", enum: ["7d", "30d", "90d", "365d", "all"], description: "默认 30d" },
|
|
419
|
+
from: { type: "string", description: "YYYY-MM-DD,与 to 同时提供" },
|
|
420
|
+
to: { type: "string", description: "YYYY-MM-DD,默认今天" },
|
|
421
|
+
top: { type: "number", description: "排行/明细条数,1–20,默认 10" },
|
|
422
|
+
recentPublishLimit: { type: "number", description: "「最近发布」清单条数,1–50,默认 10;包含所有状态,缺少发布日期的旧任务排末尾" },
|
|
423
|
+
recentDoneLimit: { type: "number", description: "「最近完成」清单条数,1–50,默认 10" },
|
|
424
|
+
recentShutdownLimit: { type: "number", description: "「最近关闭」清单条数,1–50,默认 10" },
|
|
425
|
+
},
|
|
426
|
+
},
|
|
427
|
+
examples: [{ range: "30d", top: 10 }, { from: "2026-10-01", to: "2026-10-31" }],
|
|
428
|
+
execute: function (args) { return statsReport(db, args || {}, api); },
|
|
429
|
+
render: renderStats,
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
action("tasks.delete", {
|
|
433
|
+
description: "软删除单个任务(保留同步墓碑);批量请用 tasks.bulkDelete",
|
|
434
|
+
input: { type: "object", required: ["id"], properties: { id: { type: "string" }, taskId: { type: ["string", "number"], description: "id 的别名" } } },
|
|
435
|
+
execute: function (args, ctx) { return removeTask(db, args || {}, ctx.origin, api); },
|
|
436
|
+
render: function (r) { return "已删除任务:" + r.id; },
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
action("tasks.complete", {
|
|
440
|
+
description: "把任务标记为完成(兼容旧 action);完成后 priority 自动清零",
|
|
441
|
+
input: {
|
|
442
|
+
type: "object",
|
|
443
|
+
required: ["id"],
|
|
444
|
+
properties: {
|
|
445
|
+
id: { type: "string" },
|
|
446
|
+
taskId: { type: ["string", "number"], description: "id 的别名" },
|
|
447
|
+
date: { type: "string", description: "完成日期 YYYY-MM-DD,缺省今天" },
|
|
448
|
+
difficulty: DIFFICULTY_SCHEMA,
|
|
449
|
+
},
|
|
450
|
+
},
|
|
451
|
+
execute: function (args, ctx) { return completeTask(db, args || {}, ctx.origin, api); },
|
|
452
|
+
render: function (r) { return "已完成任务:" + r.task.title; },
|
|
453
|
+
});
|
|
454
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Timeline reads and whole-line merges. No dependency on action registration.
|
|
2
|
+
import { timelineIdArg, requireTimeline, activeTimeline, setMembership, touchTimelines } from "./timeline-store.js";
|
|
3
|
+
import { idOf, nodeMap, taskOut, now } from "../db/db.js";
|
|
4
|
+
import { taskTransaction } from "../task/write/task-transaction.js";
|
|
5
|
+
import { getTask } from "../task/model/task-model.js";
|
|
6
|
+
|
|
7
|
+
const LIVE = "t.deleted_at IS NULL AND t.status!='3' AND l.deleted_at IS NULL AND e.deleted_at IS NULL";
|
|
8
|
+
const MEMBERS = "task t JOIN task_timeline_link l ON l.task_id=t.id JOIN task_timeline e ON e.id=l.timeline_id";
|
|
9
|
+
export const TIMELINE_PAGE_SCHEMA = {
|
|
10
|
+
pageNumber: { type: "integer", minimum: 1, default: 1 },
|
|
11
|
+
pageSize: { type: "integer", minimum: 1, maximum: 200, default: 20 },
|
|
12
|
+
};
|
|
13
|
+
function pagination(args) {
|
|
14
|
+
const current = args.pageNumber === undefined ? 1 : args.pageNumber;
|
|
15
|
+
const size = args.pageSize === undefined ? 20 : args.pageSize;
|
|
16
|
+
if (!Number.isSafeInteger(current) || current < 1) throw new Error("pageNumber 必须为正整数");
|
|
17
|
+
if (!Number.isSafeInteger(size) || size < 1 || size > 200) throw new Error("pageSize 必须为 1..200 整数");
|
|
18
|
+
if (!Number.isSafeInteger((current - 1) * size)) throw new Error("pageNumber 过大");
|
|
19
|
+
return { current, size, offset: (current - 1) * size };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** lastActivityDate: latest publish/done/shutdown or local creation/update day;
|
|
23
|
+
* target_date is a plan, not an activity. Closed tasks count only in taskCount.
|
|
24
|
+
* One statement includes the filtered total even on an empty/out-of-range page.
|
|
25
|
+
*/
|
|
26
|
+
export function timelines(db, args) {
|
|
27
|
+
const { current, size, offset } = pagination(args);
|
|
28
|
+
if (args.includeSingletons !== undefined && typeof args.includeSingletons !== "boolean") throw new Error("includeSingletons 必须为布尔值");
|
|
29
|
+
const titleKey = String(args.titleKey ?? "").trim();
|
|
30
|
+
const rows = db.prepare(
|
|
31
|
+
"WITH eligible AS (SELECT t.*,l.timeline_id FROM " + MEMBERS + " WHERE " + LIVE + "), " +
|
|
32
|
+
"counts AS (SELECT timeline_id,COUNT(*) AS taskCount,SUM(status='0') AS openCount,SUM(status='2') AS doneCount,MIN(publish_date) AS firstPublishDate," +
|
|
33
|
+
"MAX(MAX(COALESCE(publish_date,''),COALESCE(done_date,''),COALESCE(shutdown_date,''),COALESCE(date(created_at/1000.0,'unixepoch','localtime'),''),COALESCE(date(updated_at/1000.0,'unixepoch','localtime'),''))) AS lastActivityDate FROM eligible GROUP BY timeline_id), " +
|
|
34
|
+
"grouped AS (SELECT e.id AS timelineKey,e.id AS timelineId,e.title,e.description,e.updated_at AS timelineUpdatedAt,COALESCE(c.taskCount,0) AS taskCount,COALESCE(c.openCount,0) AS openCount,COALESCE(c.doneCount,0) AS doneCount,c.firstPublishDate,c.lastActivityDate FROM task_timeline e LEFT JOIN counts c ON c.timeline_id=e.id WHERE e.deleted_at IS NULL), " +
|
|
35
|
+
"filtered AS (SELECT * FROM grouped WHERE taskCount>=? AND (instr(lower(title),lower(?))>0 OR instr(lower(COALESCE(description,'')),lower(?))>0)), " +
|
|
36
|
+
"page AS (SELECT * FROM filtered ORDER BY lastActivityDate DESC,timelineKey ASC LIMIT ? OFFSET ?) " +
|
|
37
|
+
"SELECT page.*,totals.total FROM (SELECT COUNT(*) AS total FROM filtered) totals LEFT JOIN page ON 1=1 ORDER BY page.lastActivityDate DESC,page.timelineKey ASC"
|
|
38
|
+
).all(args.includeSingletons === true ? 0 : 2,titleKey,titleKey,size,offset);
|
|
39
|
+
return { records: rows.filter(r => r.timelineKey !== null).map(({ total, ...row }) => ({ ...row })), total: Number(rows[0].total), current, size };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function timelineTasks(db, args) {
|
|
43
|
+
const key = timelineIdArg(args, false);
|
|
44
|
+
const paged = args.pageNumber !== undefined || args.pageSize !== undefined;
|
|
45
|
+
const { current, size, offset } = pagination(args);
|
|
46
|
+
if (!key) return paged ? { records: [], total: 0, current, size } : [];
|
|
47
|
+
const where = " WHERE l.timeline_id=? AND " + LIVE;
|
|
48
|
+
const rows = db.prepare("SELECT t.* FROM " + MEMBERS + where + " ORDER BY t.publish_date ASC,t.id ASC" + (paged ? " LIMIT ? OFFSET ?" : "")).all(...(paged ? [key,size,offset] : [key]));
|
|
49
|
+
const map = nodeMap(db);
|
|
50
|
+
const records = rows.map(r => taskOut(r, map));
|
|
51
|
+
if (!paged) return records;
|
|
52
|
+
const total = Number(db.prepare("SELECT COUNT(*) AS total FROM " + MEMBERS + where).get(key).total);
|
|
53
|
+
return { records, total, current, size };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function timelineRefactor(db, args, origin, api) {
|
|
57
|
+
const key = timelineIdArg(args, false);
|
|
58
|
+
if (!key) throw new Error("timelineKey 不能为空");
|
|
59
|
+
const byKeys = args.timelineKeys !== undefined;
|
|
60
|
+
if (byKeys && args.ids !== undefined) throw new Error("ids 与 timelineKeys 互斥");
|
|
61
|
+
const input = byKeys ? args.timelineKeys : args.ids;
|
|
62
|
+
if (!Array.isArray(input) || !input.length || input.some(x => !idOf(x))) throw new Error((byKeys ? "timelineKeys" : "ids") + " 不能为空");
|
|
63
|
+
const selection = [...new Set(input.map(idOf))];
|
|
64
|
+
if (byKeys && !selection.includes(key)) throw new Error("timelineKey 必须属于 timelineKeys");
|
|
65
|
+
const dryRun = args.dryRun === true;
|
|
66
|
+
const plan = (transactionApi = api) => {
|
|
67
|
+
requireTimeline(db,key);
|
|
68
|
+
let rows;
|
|
69
|
+
if (byKeys) {
|
|
70
|
+
// json_each avoids SQLite's bind-variable ceiling without chunking or list caps.
|
|
71
|
+
rows = db.prepare("SELECT t.*,l.timeline_id AS entity_id FROM " + MEMBERS + " WHERE " + LIVE + " AND l.timeline_id IN (SELECT value FROM json_each(?)) ORDER BY t.publish_date ASC,t.id ASC").all(JSON.stringify(selection));
|
|
72
|
+
const found = new Set(rows.map(r => r.entity_id));
|
|
73
|
+
for (const selected of selection) if (!found.has(selected)) throw new Error("时间线不存在或没有有效任务:" + selected);
|
|
74
|
+
} else {
|
|
75
|
+
rows = selection.map(id => {
|
|
76
|
+
const row = getTask(db,id);
|
|
77
|
+
if (!row) throw new Error("任务不存在:" + id);
|
|
78
|
+
return row;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
// Preserve the existing explicit-id receipt and update semantics.
|
|
82
|
+
const changed = rows.filter(r => (byKeys ? r.entity_id : activeTimeline(db,r.id)?.id) !== key);
|
|
83
|
+
if (!dryRun) {
|
|
84
|
+
const ts = now();
|
|
85
|
+
const touched = new Set();
|
|
86
|
+
for (const cur of changed) setMembership(db,cur.id,key,origin,transactionApi,{ts,touched});
|
|
87
|
+
touchTimelines(db,touched,origin,transactionApi,ts);
|
|
88
|
+
}
|
|
89
|
+
const receipt = { timelineId: key, timelineKey: key, ...(dryRun ? { dryRun:true, updated:0, wouldUpdate:changed.length } : { updated:changed.length }) };
|
|
90
|
+
return byKeys ? { ...receipt, timelineKeys:selection, total:rows.length, selectedCount:rows.length } : { ...receipt, ids:rows.map(r => String(r.id)) };
|
|
91
|
+
};
|
|
92
|
+
if (dryRun) return plan();
|
|
93
|
+
return taskTransaction(db, api, plan);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function registerTimelineActions(api) {
|
|
97
|
+
api.action("tasks.timelines", {
|
|
98
|
+
description: "分页列出时间线,默认20条、过滤单任务线;排除已重启和已删除任务。按独立实体标题或描述的字面子串搜索(不搜索成员任务标题);includeSingletons=true 包含单成员及空线;openCount仅status0,doneCount仅status2。lastActivityDate为发布/完成/关闭日与创建/修改时间的本地日最大值,不计计划targetDate;按最近活动倒序、timelineKey正序稳定排序。返回{records,total,current,size}。",
|
|
99
|
+
input: { type:"object", properties: { titleKey:{type:"string",description:"时间线实体标题或描述的字面子串搜索,ASCII 大小写不敏感,不搜索成员任务标题"}, ...TIMELINE_PAGE_SCHEMA, includeSingletons:{type:"boolean",default:false,description:"true 包含单成员及空线;默认仅列至少两个可见成员的线"} } },
|
|
100
|
+
execute: args => timelines(api.db,args || {}),
|
|
101
|
+
render: result => "时间线 " + result.total + " 条",
|
|
102
|
+
});
|
|
103
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { newId, now } from '../db/db.js';
|
|
2
|
+
import { taskTransaction } from '../task/write/task-transaction.js';
|
|
3
|
+
import { timelineIdArg, requireTimeline, timelineOut, timelineError, timelineChange, setMembership } from './timeline-store.js';
|
|
4
|
+
import { migrateTimelines } from './timeline-migration.js';
|
|
5
|
+
export const TIMELINE_ID_SCHEMA = {
|
|
6
|
+
timelineId:{type:['string','number'],description:'已手动创建的时间线 id,保留原字符串'},
|
|
7
|
+
timelineKey:{type:['string','number'],description:'deprecated:timelineId 的兼容别名;同时传值必须相同'},
|
|
8
|
+
};
|
|
9
|
+
export const TIMELINE_ID_REQUIRED = [{required:['timelineId']},{required:['timelineKey']}];
|
|
10
|
+
export function createTimeline(db,args,origin,api) {
|
|
11
|
+
return taskTransaction(db,api,buffered=>{
|
|
12
|
+
const id = args.id === undefined ? newId() : String(args.id);
|
|
13
|
+
if (!id) throw timelineError('TIMELINE_ID_REQUIRED','id 不能为空');
|
|
14
|
+
const existing = db.prepare('SELECT * FROM task_timeline WHERE id=?').get(id);
|
|
15
|
+
if (existing) throw timelineError(existing.deleted_at === null ? 'TIMELINE_EXISTS':'TIMELINE_DELETED','时间线 id 已存在,不能隐式恢复');
|
|
16
|
+
const ts=now(),row={id,title:String(args.title??'').trim()||'未命名时间线',description:String(args.description??''),created_at:ts,updated_at:ts,deleted_at:null,rev:1};
|
|
17
|
+
db.prepare('INSERT INTO task_timeline(id,title,description,created_at,updated_at,deleted_at,rev) VALUES(?,?,?,?,?,?,?)').run(...Object.values(row));
|
|
18
|
+
timelineChange(db,'task_timeline',row,origin,buffered,'create');
|
|
19
|
+
return {timeline:timelineOut(row)};
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export function updateTimeline(db,args,origin,api) {
|
|
23
|
+
return taskTransaction(db,api,buffered=>{
|
|
24
|
+
const row = requireTimeline(db,timelineIdArg(args));
|
|
25
|
+
const title=args.title===undefined?row.title:String(args.title).trim()||'未命名时间线';
|
|
26
|
+
const description=args.description===undefined?row.description:String(args.description);
|
|
27
|
+
const changed=title!==row.title||description!==row.description;
|
|
28
|
+
if(changed) {
|
|
29
|
+
Object.assign(row,{title,description,updated_at:now(),rev:row.rev+1});
|
|
30
|
+
db.prepare('UPDATE task_timeline SET title=?,description=?,updated_at=?,rev=? WHERE id=?').run(title,description,row.updated_at,row.rev,row.id);
|
|
31
|
+
timelineChange(db,'task_timeline',row,origin,buffered);
|
|
32
|
+
}
|
|
33
|
+
return {timeline:timelineOut(row),changed};
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export function deleteTimeline(db,args,origin,api) {
|
|
37
|
+
const plan=buffered=>{
|
|
38
|
+
const row=requireTimeline(db,timelineIdArg(args));
|
|
39
|
+
const links=db.prepare('SELECT * FROM task_timeline_link WHERE timeline_id=? AND deleted_at IS NULL').all(row.id);
|
|
40
|
+
if(args.dryRun===true) return {dryRun:true,timelineId:row.id,affectedTasks:links.length,wouldDetach:links.length};
|
|
41
|
+
const ts=now(),touched=new Set();
|
|
42
|
+
for(const link of links) setMembership(db,link.task_id,null,origin,buffered,{ts,touched});
|
|
43
|
+
Object.assign(row,{deleted_at:ts,updated_at:ts,rev:row.rev+1});
|
|
44
|
+
db.prepare('UPDATE task_timeline SET deleted_at=?,updated_at=?,rev=? WHERE id=?').run(ts,ts,row.rev,row.id);
|
|
45
|
+
timelineChange(db,'task_timeline',row,origin,buffered,'delete');
|
|
46
|
+
return {timelineId:row.id,affectedTasks:links.length,detached:links.length};
|
|
47
|
+
};
|
|
48
|
+
return args.dryRun===true?plan(api):taskTransaction(db,api,plan);
|
|
49
|
+
}
|
|
50
|
+
export function membershipAction(db,args,origin,api,detach=false) {
|
|
51
|
+
return taskTransaction(db,api,buffered=>{
|
|
52
|
+
const taskId=String(args.taskId??'');
|
|
53
|
+
if(!taskId || !db.prepare('SELECT id FROM task WHERE id=? AND deleted_at IS NULL').get(taskId)) throw timelineError('TASK_NOT_FOUND','任务不存在:'+taskId);
|
|
54
|
+
const id=detach?null:timelineIdArg(args);
|
|
55
|
+
const changed=setMembership(db,taskId,id,origin,buffered);
|
|
56
|
+
return {taskId,timelineId:id,timelineKey:id,changed};
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export function registerEntityTimelineActions(api) {
|
|
60
|
+
const input=properties=>({type:'object',properties});
|
|
61
|
+
const identified=properties=>({...input({...TIMELINE_ID_SCHEMA,...properties}),anyOf:TIMELINE_ID_REQUIRED});
|
|
62
|
+
api.action('taskTimelines.create',{description:'独立手动创建空时间线;不自动添加任务,同 id 墓碑不能隐式恢复。',input:input({id:{type:['string','number']},title:{type:'string'},description:{type:'string'}}),execute:(args,ctx)=>createTimeline(api.db,args||{},ctx.origin,api)});
|
|
63
|
+
api.action('taskTimelines.get',{description:'读取有效时间线实体,空线也可读取;未知/已删除返回明确错误。',input:identified({}),execute:args=>({timeline:timelineOut(requireTimeline(api.db,timelineIdArg(args||{})))})});
|
|
64
|
+
api.action('taskTimelines.update',{description:'仅修改时间线标题/描述;相同值不更新版本,不修改成员任务。',input:identified({title:{type:'string'},description:{type:'string'}}),execute:(args,ctx)=>updateTimeline(api.db,args||{},ctx.origin,api)});
|
|
65
|
+
api.action('taskTimelines.delete',{description:'软删除时间线并同事务移出所有成员,旧兼容列清空,任务和重启链保留;dryRun 不写入。',input:identified({dryRun:{type:'boolean',default:false}}),execute:(args,ctx)=>deleteTimeline(api.db,args||{},ctx.origin,api)});
|
|
66
|
+
api.action('taskTimelines.attach',{description:'把已有任务加入已有有效时间线或换线;复用确定 link.id=taskId,重复加入幂等,双写旧兼容列。',input:{...identified({taskId:{type:['string','number']}}),required:['taskId']},execute:(args,ctx)=>membershipAction(api.db,args||{},ctx.origin,api)});
|
|
67
|
+
api.action('taskTimelines.detach',{description:'软删任务的成员关系并清空旧兼容列,保留任务与空线;重复移出幂等。',input:{...input({taskId:{type:['string','number']}}),required:['taskId']},execute:(args,ctx)=>membershipAction(api.db,args||{},ctx.origin,api,true)});
|
|
68
|
+
api.action('taskTimelines.migrate',{description:'审阅历史分组;默认 dryRun 且零回填。apply 必须明确 dryRun=false、approvedKeys 与最新 snapshotHash,仅补缺失实体/成员,冲突整批回滚,不覆盖已有实体/归属/墓碑。',input:{...input({dryRun:{type:'boolean',default:true},approvedKeys:{type:'array',items:{type:'string'}},snapshotHash:{type:'string'}}),if:{properties:{dryRun:{const:false}},required:['dryRun']},then:{required:['approvedKeys','snapshotHash']}},execute:(args,ctx)=>migrateTimelines(api.db,args||{},ctx.origin,api)});
|
|
69
|
+
}
|