dsh-workbench-tasks 0.0.0-stage → 5.2.20
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 +11963 -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 +232 -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 +74 -0
- package/lib/stats/difficulty-bins.js +49 -0
- package/lib/stats/events.js +40 -0
- package/lib/stats/overview.js +99 -0
- package/lib/stats/stats-today.js +26 -0
- package/lib/stats/stats.js +589 -0
- package/lib/stats/timeline-stats.js +174 -0
- package/lib/tagging/source.js +89 -0
- package/lib/tagging/tag-options.js +99 -0
- package/lib/tagging/tagging.js +371 -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 +456 -0
- package/lib/timeline/task-timelines.js +110 -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
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 任务插件的数据页:只读浏览本插件负责的表。
|
|
3
|
+
*
|
|
4
|
+
* 设计见 docs/specs/21-数据页设计.md。两个 action:
|
|
5
|
+
* - tasks.dataTables:表清单 + 列元信息 + 行数;
|
|
6
|
+
* - tasks.dataQuery:分页/排序/关键字过滤,只读,表名与排序列走白名单。
|
|
7
|
+
*
|
|
8
|
+
* 刻意不提供任意 SQL、导出与写操作;meta 与 sync_* 平台表不在此暴露。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { DOMAIN } from "./config.js";
|
|
12
|
+
|
|
13
|
+
export const SCHEMA_VERSION = 6;
|
|
14
|
+
|
|
15
|
+
const TABLES = [
|
|
16
|
+
{ name: "task", zh: "任务", en: "Tasks" },
|
|
17
|
+
{ name: "task_timeline", zh: "任务时间线", en: "Task timelines" },
|
|
18
|
+
{ name: "task_timeline_link", zh: "时间线成员关系", en: "Timeline membership rows" },
|
|
19
|
+
{ name: "task_workspace", zh: "工作区", en: "Workspaces" },
|
|
20
|
+
{ name: "task_workspace_link", zh: "任务与工作区关系", en: "Task-workspace links" },
|
|
21
|
+
{ name: "task_node", zh: "任务单元", en: "Task nodes" },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
const COLUMN_LABELS = {
|
|
25
|
+
id: { zh: "ID", en: "ID" },
|
|
26
|
+
title: { zh: "标题", en: "Title" },
|
|
27
|
+
description: { zh: "描述", en: "Description" },
|
|
28
|
+
status: { zh: "状态", en: "Status" },
|
|
29
|
+
priority: { zh: "优先级", en: "Priority" },
|
|
30
|
+
difficulty: { zh: "难度", en: "Difficulty" },
|
|
31
|
+
review: { zh: "复盘", en: "Review" },
|
|
32
|
+
table_id: { zh: "任务表 ID", en: "Table ID" },
|
|
33
|
+
timeline_key: { zh: "旧时间线键(兼容)", en: "Legacy timeline key (compatibility)" },
|
|
34
|
+
timeline_id: { zh: "时间线 ID", en: "Timeline ID" },
|
|
35
|
+
restart_key: { zh: "重启键", en: "Restart key" },
|
|
36
|
+
publish_date: { zh: "发布日期", en: "Published" },
|
|
37
|
+
target_date: { zh: "目标日期", en: "Target date" },
|
|
38
|
+
done_date: { zh: "完成日期", en: "Done date" },
|
|
39
|
+
shutdown_date: { zh: "关闭日期", en: "Closed date" },
|
|
40
|
+
sort: { zh: "排序", en: "Sort" },
|
|
41
|
+
dedupe_key: { zh: "幂等键", en: "Dedupe key" },
|
|
42
|
+
workspace_id: { zh: "工作区 ID", en: "Workspace ID" },
|
|
43
|
+
dir: { zh: "工作区目录(本机绝对路径,可空)", en: "Workspace directory (absolute local path, optional)" },
|
|
44
|
+
task_id: { zh: "任务 ID", en: "Task ID" },
|
|
45
|
+
name: { zh: "名称", en: "Name" },
|
|
46
|
+
type: { zh: "类型", en: "Type" },
|
|
47
|
+
parent_id: { zh: "父 ID", en: "Parent" },
|
|
48
|
+
path: { zh: "路径", en: "Path" },
|
|
49
|
+
depth: { zh: "深度", en: "Depth" },
|
|
50
|
+
created_at: { zh: "创建时间", en: "Created at" },
|
|
51
|
+
updated_at: { zh: "更新时间", en: "Updated at" },
|
|
52
|
+
deleted_at: { zh: "删除时间", en: "Deleted at" },
|
|
53
|
+
rev: { zh: "版本号", en: "Rev" },
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const SYSTEM_COLUMNS = new Set(["rev", "created_at", "updated_at", "deleted_at"]);
|
|
57
|
+
const PAGE_SIZES = [10, 25, 50, 100, 200];
|
|
58
|
+
const BOOL_COLUMNS = new Set(["bindable", "deleted"]);
|
|
59
|
+
const MAX_SEARCH = 100;
|
|
60
|
+
const MAX_FILTERS = 20;
|
|
61
|
+
|
|
62
|
+
function labelOf(name) {
|
|
63
|
+
const hit = COLUMN_LABELS[name];
|
|
64
|
+
return hit ? { zh: hit.zh, en: hit.en } : { zh: name, en: name };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function kindOf(name, declared) {
|
|
68
|
+
const type = String(declared || "").toUpperCase();
|
|
69
|
+
if (BOOL_COLUMNS.has(name)) return "bool";
|
|
70
|
+
if (name === "id" || name.endsWith("_id")) return "id";
|
|
71
|
+
if (name === "path") return "path";
|
|
72
|
+
if (name.endsWith("_at") && type === "INTEGER") return "timestamp";
|
|
73
|
+
if (name.endsWith("_date")) return "date";
|
|
74
|
+
if (type === "INTEGER" || type === "REAL") return "number";
|
|
75
|
+
return "text";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function pickPageSize(input) {
|
|
79
|
+
const n = Math.floor(Number(input));
|
|
80
|
+
if (!Number.isFinite(n) || n <= 0) return 50;
|
|
81
|
+
let best = PAGE_SIZES[0];
|
|
82
|
+
for (const size of PAGE_SIZES) if (Math.abs(size - n) < Math.abs(best - n)) best = size;
|
|
83
|
+
return best;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function likeEscape(text) {
|
|
87
|
+
return String(text).slice(0, MAX_SEARCH).replace(/[\\%_]/g, function (m) { return "\\" + m; });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 逐列值筛选:{列名: 值};列名必须是真实列,值按包含匹配(CAST 成文本 LIKE)。 */
|
|
91
|
+
function normalizeFilters(raw, names) {
|
|
92
|
+
if (raw === undefined || raw === null) return [];
|
|
93
|
+
if (typeof raw !== "object" || Array.isArray(raw)) throw new Error("filters 需要是 {列名: 值} 对象");
|
|
94
|
+
const keys = Object.keys(raw);
|
|
95
|
+
if (keys.length > MAX_FILTERS) throw new Error("列筛选最多 " + MAX_FILTERS + " 列");
|
|
96
|
+
const out = [];
|
|
97
|
+
for (const key of keys) {
|
|
98
|
+
const column = String(key);
|
|
99
|
+
if (names.indexOf(column) < 0) throw new Error("未知筛选列:" + column);
|
|
100
|
+
const value = raw[key];
|
|
101
|
+
if (value === undefined || value === null) continue;
|
|
102
|
+
const text = String(value).trim().slice(0, MAX_SEARCH);
|
|
103
|
+
if (!text) continue;
|
|
104
|
+
out.push({ column: column, value: text });
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function toPlain(row) {
|
|
110
|
+
const out = {};
|
|
111
|
+
for (const key of Object.keys(row)) {
|
|
112
|
+
const value = row[key];
|
|
113
|
+
out[key] = typeof value === "bigint" ? Number(value) : value;
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function registerDataActions(api) {
|
|
119
|
+
const db = api.db;
|
|
120
|
+
|
|
121
|
+
function columnsOf(table) {
|
|
122
|
+
let info = [];
|
|
123
|
+
try {
|
|
124
|
+
info = db.prepare("PRAGMA table_info(" + table + ")").all();
|
|
125
|
+
} catch (e) {
|
|
126
|
+
info = [];
|
|
127
|
+
}
|
|
128
|
+
return info.map(function (c) {
|
|
129
|
+
return {
|
|
130
|
+
name: c.name,
|
|
131
|
+
type: String(c.type || ""),
|
|
132
|
+
kind: kindOf(c.name, c.type),
|
|
133
|
+
label: labelOf(c.name),
|
|
134
|
+
system: SYSTEM_COLUMNS.has(c.name),
|
|
135
|
+
pk: !!c.pk,
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function countsOf(table, hasDeleted) {
|
|
141
|
+
if (!hasDeleted) {
|
|
142
|
+
const row = db.prepare("SELECT COUNT(*) AS n FROM " + table).get();
|
|
143
|
+
const n = Number(row && row.n) || 0;
|
|
144
|
+
return { count: n, deletedCount: 0 };
|
|
145
|
+
}
|
|
146
|
+
const row = db.prepare(
|
|
147
|
+
"SELECT COUNT(*) AS total, SUM(CASE WHEN deleted_at IS NULL THEN 1 ELSE 0 END) AS live FROM " + table,
|
|
148
|
+
).get();
|
|
149
|
+
const total = Number(row && row.total) || 0;
|
|
150
|
+
const live = row && row.live === null ? 0 : Number(row && row.live) || 0;
|
|
151
|
+
return { count: live, deletedCount: Math.max(0, total - live) };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 表结构统计(「表索引」视图):行数、列数、主键、索引、最近写入。
|
|
156
|
+
* 只读 PRAGMA 与 COUNT/MAX,不做任何写操作;表缺失时返回零值而不是报错,
|
|
157
|
+
* 这样单个表迁移失败不会让整页不可用。
|
|
158
|
+
*/
|
|
159
|
+
function tableStatsOf(table) {
|
|
160
|
+
const out = { rows: 0, primaryKey: [], indexes: [], lastWrite: null };
|
|
161
|
+
try {
|
|
162
|
+
out.rows = Number(db.prepare("SELECT COUNT(*) AS n FROM " + table).get().n) || 0;
|
|
163
|
+
} catch (e) {
|
|
164
|
+
/* 表缺失:保持 0 */
|
|
165
|
+
}
|
|
166
|
+
let info = [];
|
|
167
|
+
try {
|
|
168
|
+
info = db.prepare("PRAGMA table_info(" + table + ")").all();
|
|
169
|
+
} catch (e) {
|
|
170
|
+
info = [];
|
|
171
|
+
}
|
|
172
|
+
out.columnCount = info.length;
|
|
173
|
+
out.primaryKey = info
|
|
174
|
+
.filter(function (c) { return Number(c.pk) > 0; })
|
|
175
|
+
.sort(function (colA, colB) { return Number(colA.pk) - Number(colB.pk); })
|
|
176
|
+
.map(function (c) { return c.name; });
|
|
177
|
+
try {
|
|
178
|
+
out.indexes = db.prepare("PRAGMA index_list(" + table + ")").all().map(function (ix) {
|
|
179
|
+
let cols = [];
|
|
180
|
+
try {
|
|
181
|
+
cols = db.prepare("PRAGMA index_info(" + JSON.stringify(String(ix.name)) + ")").all().map(function (c) { return c.name; });
|
|
182
|
+
} catch (e) {
|
|
183
|
+
cols = [];
|
|
184
|
+
}
|
|
185
|
+
return { name: String(ix.name), unique: Number(ix.unique) === 1, origin: String(ix.origin || "c"), columns: cols };
|
|
186
|
+
});
|
|
187
|
+
} catch (e) {
|
|
188
|
+
out.indexes = [];
|
|
189
|
+
}
|
|
190
|
+
const stampCols = ["updated_at", "created_at", "publish_date", "done_date", "target_date"];
|
|
191
|
+
for (let i = 0; i < stampCols.length; i += 1) {
|
|
192
|
+
const hit = info.filter(function (c) { return c.name === stampCols[i]; })[0];
|
|
193
|
+
if (!hit) continue;
|
|
194
|
+
try {
|
|
195
|
+
const row = db.prepare("SELECT MAX(" + hit.name + ") AS v FROM " + table).get();
|
|
196
|
+
const v = row && row.v;
|
|
197
|
+
if (v !== null && v !== undefined && String(v) !== "") {
|
|
198
|
+
out.lastWrite = { column: hit.name, at: v };
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
} catch (e) {
|
|
202
|
+
/* 单列统计失败不影响其它信息 */
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function tablesPayload(rawArgs) {
|
|
209
|
+
const args = rawArgs || {};
|
|
210
|
+
const withStats = args.stats === true;
|
|
211
|
+
const warnings = [];
|
|
212
|
+
const tables = TABLES.map(function (t) {
|
|
213
|
+
const columns = columnsOf(t.name);
|
|
214
|
+
let counts = { count: 0, deletedCount: 0 };
|
|
215
|
+
if (!columns.length) {
|
|
216
|
+
warnings.push({ code: "table_missing", message: "数据表不存在:" + t.name });
|
|
217
|
+
} else {
|
|
218
|
+
try {
|
|
219
|
+
counts = countsOf(t.name, columns.some(function (c) { return c.name === "deleted_at"; }));
|
|
220
|
+
} catch (e) {
|
|
221
|
+
warnings.push({ code: "count_failed", message: t.name + " 行数统计失败:" + String(e && e.message || e) });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const row = { name: t.name, label: { zh: t.zh, en: t.en }, count: counts.count, deletedCount: counts.deletedCount, columns };
|
|
225
|
+
if (withStats && columns.length) row.stats = tableStatsOf(t.name);
|
|
226
|
+
return row;
|
|
227
|
+
});
|
|
228
|
+
return { domain: DOMAIN, schemaVersion: SCHEMA_VERSION, tables, warnings };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function findTable(name) {
|
|
232
|
+
return TABLES.filter(function (t) { return t.name === name; })[0] || null;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function queryPayload(raw) {
|
|
236
|
+
const args = raw || {};
|
|
237
|
+
const named = String(args.table || "");
|
|
238
|
+
const table = findTable(named);
|
|
239
|
+
if (!table) throw new Error("未知数据表:" + named);
|
|
240
|
+
|
|
241
|
+
const columns = columnsOf(table.name);
|
|
242
|
+
if (!columns.length) {
|
|
243
|
+
return {
|
|
244
|
+
domain: DOMAIN, table: table.name, columns: [], rows: [],
|
|
245
|
+
page: 1, pageSize: pickPageSize(args.pageSize), pages: 1, total: 0,
|
|
246
|
+
sort: "id", order: "desc", includeDeleted: !!args.includeDeleted,
|
|
247
|
+
search: "", filters: [], queryMs: 0,
|
|
248
|
+
warnings: [{ code: "table_missing", message: "数据表不存在:" + table.name }],
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const names = columns.map(function (c) { return c.name; });
|
|
253
|
+
const hasDeleted = names.indexOf("deleted_at") >= 0;
|
|
254
|
+
const fallbackSort = names.indexOf("created_at") >= 0 ? "created_at" : (names.indexOf("id") >= 0 ? "id" : names[0]);
|
|
255
|
+
let sort = String(args.sort || fallbackSort);
|
|
256
|
+
if (names.indexOf(sort) < 0) throw new Error("未知排序列:" + sort);
|
|
257
|
+
const order = String(args.order || "desc").toLowerCase() === "asc" ? "asc" : "desc";
|
|
258
|
+
|
|
259
|
+
const pageSize = pickPageSize(args.pageSize);
|
|
260
|
+
const includeDeleted = !!args.includeDeleted;
|
|
261
|
+
const search = typeof args.search === "string" ? args.search.trim().slice(0, MAX_SEARCH) : "";
|
|
262
|
+
const filters = normalizeFilters(args.filters, names);
|
|
263
|
+
|
|
264
|
+
const where = [];
|
|
265
|
+
const params = [];
|
|
266
|
+
if (!includeDeleted && hasDeleted) where.push("deleted_at IS NULL");
|
|
267
|
+
if (search) {
|
|
268
|
+
const textCols = columns.filter(function (c) { return String(c.type).toUpperCase() === "TEXT"; });
|
|
269
|
+
if (textCols.length) {
|
|
270
|
+
where.push("(" + textCols.map(function (c) { return c.name + " LIKE ? ESCAPE '\\'"; }).join(" OR ") + ")");
|
|
271
|
+
const hit = "%" + likeEscape(search) + "%";
|
|
272
|
+
for (let i = 0; i < textCols.length; i++) params.push(hit);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
for (let i = 0; i < filters.length; i++) {
|
|
276
|
+
where.push("CAST(" + filters[i].column + " AS TEXT) LIKE ? ESCAPE '\\'");
|
|
277
|
+
params.push("%" + likeEscape(filters[i].value) + "%");
|
|
278
|
+
}
|
|
279
|
+
const whereSql = where.length ? " WHERE " + where.join(" AND ") : "";
|
|
280
|
+
const started = (typeof performance !== "undefined" && performance.now ? performance.now() : Date.now());
|
|
281
|
+
|
|
282
|
+
const countStmt = db.prepare("SELECT COUNT(*) AS n FROM " + table.name + whereSql);
|
|
283
|
+
const total = Number(countStmt.get.apply(countStmt, params).n) || 0;
|
|
284
|
+
const pages = Math.max(1, Math.ceil(total / pageSize));
|
|
285
|
+
let page = Math.floor(Number(args.page)) || 1;
|
|
286
|
+
if (page < 1) page = 1;
|
|
287
|
+
if (page > pages) page = pages;
|
|
288
|
+
|
|
289
|
+
const offset = (page - 1) * pageSize;
|
|
290
|
+
const stmt = db.prepare(
|
|
291
|
+
"SELECT * FROM " + table.name + whereSql + " ORDER BY " + sort + " " + order.toUpperCase() + " LIMIT ? OFFSET ?",
|
|
292
|
+
);
|
|
293
|
+
const rows = stmt.all.apply(stmt, params.concat([pageSize, offset])).map(toPlain);
|
|
294
|
+
const ended = (typeof performance !== "undefined" && performance.now ? performance.now() : Date.now());
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
domain: DOMAIN,
|
|
298
|
+
schemaVersion: SCHEMA_VERSION,
|
|
299
|
+
table: table.name,
|
|
300
|
+
columns: columns.map(function (c) {
|
|
301
|
+
return { name: c.name, type: c.type, kind: c.kind, label: c.label, system: c.system };
|
|
302
|
+
}),
|
|
303
|
+
rows,
|
|
304
|
+
page,
|
|
305
|
+
pageSize,
|
|
306
|
+
pages,
|
|
307
|
+
total,
|
|
308
|
+
sort,
|
|
309
|
+
order,
|
|
310
|
+
includeDeleted,
|
|
311
|
+
search,
|
|
312
|
+
filters,
|
|
313
|
+
queryMs: Math.max(0, Math.round(ended - started)),
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const tableNames = TABLES.map(function (t) { return t.name; });
|
|
318
|
+
|
|
319
|
+
api.action("tasks.dataTables", {
|
|
320
|
+
description: "列出任务插件负责的数据表、列元信息与行数(只读,供「数据」视图使用)",
|
|
321
|
+
input: {
|
|
322
|
+
type: "object",
|
|
323
|
+
properties: {
|
|
324
|
+
stats: { type: "boolean", description: "true 时附带每表结构统计:行数、列数、主键、索引、最近写入(表索引视图用)" },
|
|
325
|
+
},
|
|
326
|
+
},
|
|
327
|
+
examples: [{}],
|
|
328
|
+
execute: function (args) { return tablesPayload(args); },
|
|
329
|
+
render: function (r) {
|
|
330
|
+
return r.tables.map(function (t) { return t.name + " " + t.count + " 行"; }).join("、");
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
api.action("tasks.dataQuery", {
|
|
335
|
+
description: "只读分页查询任务插件的数据表:白名单表名/排序列,支持关键字过滤、逐列值筛选与包含已删除行",
|
|
336
|
+
input: {
|
|
337
|
+
type: "object",
|
|
338
|
+
required: ["table"],
|
|
339
|
+
properties: {
|
|
340
|
+
table: { type: "string", enum: tableNames, description: "表名,必须是本插件负责的表" },
|
|
341
|
+
page: { type: "number", description: "页码,从 1 开始,越界自动夹取" },
|
|
342
|
+
pageSize: { type: "number", enum: PAGE_SIZES, description: "每页条数,默认 50,超出就近取档" },
|
|
343
|
+
sort: { type: "string", description: "排序列名,必须是该表真实列;默认 created_at(无则 id)" },
|
|
344
|
+
order: { type: "string", enum: ["asc", "desc"], description: "排序方向,默认 desc" },
|
|
345
|
+
search: { type: "string", description: "关键字,匹配所有 TEXT 列的包含关系,最长 100 字" },
|
|
346
|
+
filters: {
|
|
347
|
+
type: "object",
|
|
348
|
+
description: "逐列值筛选:{列名: 值},按包含匹配(列值转文本后 LIKE),与其他条件 AND;列名必须是该表真实列,最多 20 列",
|
|
349
|
+
additionalProperties: { type: "string" },
|
|
350
|
+
},
|
|
351
|
+
includeDeleted: { type: "boolean", description: "是否包含已软删除的行,默认 false" },
|
|
352
|
+
},
|
|
353
|
+
},
|
|
354
|
+
examples: [
|
|
355
|
+
{ table: "task", page: 1, pageSize: 50, sort: "created_at", order: "desc" },
|
|
356
|
+
{ table: "task", search: "发布", includeDeleted: true },
|
|
357
|
+
{ table: "task", filters: { id: "01" } },
|
|
358
|
+
],
|
|
359
|
+
execute: function (args) { return queryPayload(args || {}); },
|
|
360
|
+
render: function (r) {
|
|
361
|
+
var line = r.table + " 第 " + r.page + "/" + r.pages + " 页,共 " + r.total + " 行";
|
|
362
|
+
if (r.filters && r.filters.length) {
|
|
363
|
+
line += ";列筛选:" + r.filters.map(function (f) { return f.column + " 含 \"" + f.value + "\""; }).join(" · ");
|
|
364
|
+
}
|
|
365
|
+
return line;
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 任务域的同步声明:task 行级,task_node 树表整表收敛。
|
|
3
|
+
*
|
|
4
|
+
* reconcile 只做数据结构收敛(合并版本、修孤儿、重排 sort、重算 path/depth),
|
|
5
|
+
* 不写数据库;宿主拿到 rows 后整表替换并 full push。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { ownershipError } from '../task/model/task-ownership-path.js';
|
|
9
|
+
import { normalizeTimelineTask, normalizeTimelineLink, afterTimelineTask, afterTimelineLink, afterTimelineEntity } from '../timeline/timeline-sync.js';
|
|
10
|
+
|
|
11
|
+
// 多设备同名不同 id 不自动合并;阻断同步收敛并交给用户用 id 消歧。
|
|
12
|
+
function checkSiblingConflicts(rows) {
|
|
13
|
+
const names = new Map();
|
|
14
|
+
for (const row of rows) {
|
|
15
|
+
const key = JSON.stringify([String(row.parent_id || '0'),String(row.name)]);
|
|
16
|
+
if (!names.has(key)) names.set(key, []);
|
|
17
|
+
names.get(key).push(row);
|
|
18
|
+
}
|
|
19
|
+
for (const group of names.values()) if (group.length > 1) {
|
|
20
|
+
throw ownershipError('PATH_AMBIGUOUS', '同步发现同级同名节点,未自动合并或改名,请先确认节点归属', {
|
|
21
|
+
candidates:group.map(row=>({id:String(row.id),type:String(row.type),path:String(row.path || ''),name:String(row.name)})),
|
|
22
|
+
nextActions:['使用 taskNodes.overview 检查冲突 id,明确确认后通过 taskNodes.rename 或 taskNodes.move 消歧,再重试同步'],
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function stableTs(rows) {
|
|
28
|
+
let max = 0;
|
|
29
|
+
for (const row of rows) max = Math.max(max, Number(row.updated_at) || 0);
|
|
30
|
+
return max;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function mergeRows(remoteRows, localRows) {
|
|
34
|
+
const map = new Map();
|
|
35
|
+
for (const row of localRows || []) if (row && row.id) map.set(String(row.id), row);
|
|
36
|
+
for (const row of remoteRows || []) {
|
|
37
|
+
if (!row || !row.id) continue;
|
|
38
|
+
const id = String(row.id);
|
|
39
|
+
const cur = map.get(id);
|
|
40
|
+
if (!cur || (Number(row.updated_at) || 0) >= (Number(cur.updated_at) || 0)) map.set(id, row);
|
|
41
|
+
}
|
|
42
|
+
return [...map.values()];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function reconcileTaskTree(remoteRows, localRows) {
|
|
46
|
+
const all = mergeRows(remoteRows, localRows);
|
|
47
|
+
const baseTs = stableTs(all);
|
|
48
|
+
const live = all.filter((row) => row.deleted_at === null || row.deleted_at === undefined);
|
|
49
|
+
checkSiblingConflicts(live);
|
|
50
|
+
if (!live.length) return { rows: all.sort((a, b) => String(a.id).localeCompare(String(b.id))), dirtyRowTables: [] };
|
|
51
|
+
|
|
52
|
+
const byId = new Map(live.map((row) => [String(row.id), Object.assign({}, row)]));
|
|
53
|
+
let roots = [...byId.values()].filter((row) => String(row.parent_id || "0") === "0");
|
|
54
|
+
roots.sort((a, b) => Number(a.sort || 0) - Number(b.sort || 0) || String(a.id).localeCompare(String(b.id)));
|
|
55
|
+
let root = roots[0];
|
|
56
|
+
if (!root) {
|
|
57
|
+
root = {
|
|
58
|
+
id: "1",
|
|
59
|
+
name: "任务",
|
|
60
|
+
type: "0",
|
|
61
|
+
parent_id: "0",
|
|
62
|
+
path: "/1/",
|
|
63
|
+
sort: 0,
|
|
64
|
+
depth: 0,
|
|
65
|
+
created_at: baseTs,
|
|
66
|
+
updated_at: baseTs,
|
|
67
|
+
deleted_at: null,
|
|
68
|
+
rev: 1,
|
|
69
|
+
};
|
|
70
|
+
byId.set("1", root);
|
|
71
|
+
roots = [root];
|
|
72
|
+
}
|
|
73
|
+
root.parent_id = "0";
|
|
74
|
+
root.depth = 0;
|
|
75
|
+
root.sort = 0;
|
|
76
|
+
root.path = "/" + root.id + "/";
|
|
77
|
+
|
|
78
|
+
// 其他根级节点挂到唯一根下,保证单树;孤儿也挂到根。
|
|
79
|
+
for (const node of byId.values()) {
|
|
80
|
+
if (node === root) continue;
|
|
81
|
+
const parent = byId.get(String(node.parent_id || "0"));
|
|
82
|
+
if (!parent || node.parent_id === "0" || node.parent_id === undefined) node.parent_id = String(root.id);
|
|
83
|
+
}
|
|
84
|
+
// 断环:沿 parent 链走,遇到自己则挂回根。
|
|
85
|
+
for (const node of byId.values()) {
|
|
86
|
+
if (node === root) continue;
|
|
87
|
+
let cur = node;
|
|
88
|
+
const seen = new Set();
|
|
89
|
+
while (cur && String(cur.parent_id) !== "0") {
|
|
90
|
+
if (seen.has(String(cur.id))) break;
|
|
91
|
+
seen.add(String(cur.id));
|
|
92
|
+
const parent = byId.get(String(cur.parent_id));
|
|
93
|
+
if (!parent) break;
|
|
94
|
+
if (parent === node) {
|
|
95
|
+
node.parent_id = String(root.id);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
cur = parent;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const children = new Map();
|
|
103
|
+
for (const node of byId.values()) {
|
|
104
|
+
if (node === root) continue;
|
|
105
|
+
const pid = String(node.parent_id);
|
|
106
|
+
if (!children.has(pid)) children.set(pid, []);
|
|
107
|
+
children.get(pid).push(node);
|
|
108
|
+
}
|
|
109
|
+
for (const list of children.values()) {
|
|
110
|
+
list.sort((a, b) => Number(a.sort || 0) - Number(b.sort || 0) || Number(a.created_at || 0) - Number(b.created_at || 0) || String(a.id).localeCompare(String(b.id)));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const ordered = [root];
|
|
114
|
+
const queue = [root];
|
|
115
|
+
while (queue.length) {
|
|
116
|
+
const parent = queue.shift();
|
|
117
|
+
const kids = children.get(String(parent.id)) || [];
|
|
118
|
+
let index = 0;
|
|
119
|
+
for (const child of kids) {
|
|
120
|
+
child.parent_id = String(parent.id);
|
|
121
|
+
child.sort = index++;
|
|
122
|
+
child.depth = Number(parent.depth) + 1;
|
|
123
|
+
child.path = String(parent.path) + String(child.id) + "/";
|
|
124
|
+
ordered.push(child);
|
|
125
|
+
queue.push(child);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 不可达节点(数据损坏)挂根并追加。
|
|
130
|
+
for (const node of byId.values()) {
|
|
131
|
+
if (ordered.indexOf(node) >= 0) continue;
|
|
132
|
+
node.parent_id = String(root.id);
|
|
133
|
+
node.depth = 1;
|
|
134
|
+
node.path = String(root.path) + String(node.id) + "/";
|
|
135
|
+
ordered.push(node);
|
|
136
|
+
}
|
|
137
|
+
for (const node of ordered) {
|
|
138
|
+
const before = all.find((row) => String(row.id) === String(node.id));
|
|
139
|
+
const changed =
|
|
140
|
+
!before ||
|
|
141
|
+
String(before.parent_id) !== String(node.parent_id) ||
|
|
142
|
+
String(before.path) !== String(node.path) ||
|
|
143
|
+
Number(before.sort) !== Number(node.sort) ||
|
|
144
|
+
Number(before.depth) !== Number(node.depth);
|
|
145
|
+
if (changed && before) {
|
|
146
|
+
node.updated_at = Math.max(Number(before.updated_at) || 0, baseTs) + 1;
|
|
147
|
+
node.rev = (Number(before.rev) || 1) + 1;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 挂根/断环会改变同级关系:返回替换行之前再次验证,不能等下一轮同步才发现碰撞。
|
|
152
|
+
checkSiblingConflicts(ordered);
|
|
153
|
+
const tombstones = all.filter((row) => row.deleted_at !== null && row.deleted_at !== undefined);
|
|
154
|
+
return {
|
|
155
|
+
rows: [...ordered, ...tombstones].sort((a, b) => String(a.id).localeCompare(String(b.id))),
|
|
156
|
+
dirtyRowTables: [],
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 工作区关联的行级同步:id 由 workspace_id + ":" + task_id 决定,
|
|
162
|
+
* 校验不通过直接阻断本轮同步,避免多设备合并出找不到父行的半行。
|
|
163
|
+
*/
|
|
164
|
+
function normalizeWorkspaceLink(row) {
|
|
165
|
+
const workspaceId = String(row.workspace_id || row.workspaceId || '').trim();
|
|
166
|
+
const taskId = String(row.task_id || row.taskId || '').trim();
|
|
167
|
+
if (!workspaceId || !taskId) {
|
|
168
|
+
throw ownershipError('WORKSPACE_LINK_INVALID', '工作区关联缺少 workspace_id 或 task_id,已拒绝本次同步', {
|
|
169
|
+
id: row && row.id !== undefined && row.id !== null ? String(row.id) : null,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
const expected = workspaceId + ':' + taskId;
|
|
173
|
+
const cur = row.id === undefined || row.id === null ? '' : String(row.id);
|
|
174
|
+
if (cur && cur !== expected) {
|
|
175
|
+
throw ownershipError('WORKSPACE_LINK_ID_MISMATCH', '工作区关联 id 必须是 workspace_id + ":" + task_id', { id: cur, expected });
|
|
176
|
+
}
|
|
177
|
+
return Object.assign({}, row, { id: expected, workspace_id: workspaceId, task_id: taskId });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export const taskSync = {
|
|
181
|
+
tables: [
|
|
182
|
+
{ name: "task", mode: "row", normalizeIncoming: normalizeTimelineTask, afterApply: afterTimelineTask },
|
|
183
|
+
{ name: "task_timeline", mode: "row", afterApply: afterTimelineEntity },
|
|
184
|
+
{ name: "task_timeline_link", mode: "row", normalizeIncoming: normalizeTimelineLink, afterApply: afterTimelineLink },
|
|
185
|
+
{ name: "task_workspace", mode: "row" },
|
|
186
|
+
{ name: "task_workspace_link", mode: "row", normalizeIncoming: normalizeWorkspaceLink },
|
|
187
|
+
{ name: "task_node", mode: "tree", reconcile: reconcileTaskTree },
|
|
188
|
+
],
|
|
189
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/wb-task-publisher` 的 brief 构造:把用户原话与发布 SOP 拼成一条
|
|
3
|
+
* 模型可见的用户消息。纯函数,不依赖 ctx,便于单测。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** 需求正文长度上限:超过时让用户拆分或改用附件,不静默截断。 */
|
|
7
|
+
export const MAX_REQUIREMENT_LENGTH = 8000;
|
|
8
|
+
|
|
9
|
+
const SOP_STEPS = [
|
|
10
|
+
"A. 调研现状,不要默认这是代码工作:按需求类型看页面、数据、文档、流程或既有任务;只有需求明确是代码改动时才看仓库。结论必须区分「用户说的 / 我查到的 / 我推测的」,并给出可核对证据(页面位置、数据口径、任务 id;代码类才给 路径:行号)。",
|
|
11
|
+
"B. 润色与补充:标题写成「动词 + 对象 + 结果」;补齐背景、现状、目标、范围与非目标、验收标准、风险与依赖、计划完成日期、难度(0.5 步进)、优先级。用户没说的关键项标注「待确认」,不擅自扩大范围,不把推测写成事实。",
|
|
12
|
+
"C. 归属(任务表 + 工作区):先调用 taskNodes.overview 拿全量任务树,按职责选择任务表;出现多个候选且职责相当必须问用户,禁止按热度、第一个候选或分数自动选择。没有合适节点时自行建模:给出路径、命名与理由,直接调用 taskNodes.create 建组或建表(已明确授权的场景也可用 tasks.create 的 createMissing + pathDescriptions),不必等用户确认;只有确有新职能域才新建上级组,不得为单条任务建整条组层级。再调用 taskWorkspaces.list(默认按 updated_at 倒序,候选多时 pageSize 用到 100)取工作区,结合任务职责与工作区的 title/name/description/dir 判断归属,合法结果只有三种:不关联工作区、关联一个、关联多个。没有合适候选时自行判断:能明确对应某个工作目录就用 taskWorkspaces.create 建一个再挂,判断不了就不挂;禁止为了挂而造,禁止按名称相似或列表第一条自动挂。创建任务时把确认的工作区 id 传进 tasks.create 的 workspaceIds(整体替换语义,任一 id 非法会让整次创建回滚)。历史任务只作线索,不作现行规则。",
|
|
13
|
+
"D. 时间线:判断现有时间线是否匹配、是否需要新建、能否与历史任务形成同一条线 / 重启链(restartKey)/ 后续任务。能挂现有时间线就挂;预计会形成 2 个及以上任务的链条时直接调用 taskTimelines.create 新开一条并挂入;只有一单的线不建,明确写「无时间线归属」,不要为了凑线硬挂。",
|
|
14
|
+
"E. 标签:标签不在意 id,所以不要被现有标签列表限制。tasks.tagOptions / tasks.tags 只是参考(了解既有命名风格),不是候选清单,不要先查一遍再从里面挑。按业务域、功能面、动作、影响面自由联想,越具体越好、越多越好(通常 5–8 个,上不封顶);但每条必须语义准确、能独立看懂,不要为凑数堆同义词、空泛词或近似重复,回看时合并重复的。创建时直接传 tags: [\"标签名\"],不要用 tagIds,也不必先 tags.create(未知名字由 form.autoCreateTags 决定,默认自动创建)。标签域未安装时仍传 tags:任务照常创建、回执 notices 会说明标签缺失,不要因此停问、重试或声称已打标。",
|
|
15
|
+
"F. 发布:先调用 tasks.list 以标题关键词查重;创建时 source 用 0,dedupeKey 用「dsh-session:」前缀拼接 sessionId(用户手写发布可用自定义键),并把 C 步确认的 workspaceIds、E 步写出的 tags 一起传入。创建回执必须给出任务 id、任务表路径、工作区归属、时间线归属、标签(完整名称;新建的单独标出,没标注的视为沿用)、计划完成日期与「本次自建」段,并给用户一屏摘要,不要原样贴 action JSON。「本次自建」要写明本次新建的任务组或表(完整路径)、时间线(标题 + id)、工作区(名称 + id + dir);没有自建就写「本次自建:无」。",
|
|
16
|
+
"G. 何时停下来问:只有三种情况先给草案并提问、不创建:任务表多候选且职责相当、信息互相冲突、查重命中同主题任务。缺任务表 / 时间线 / 工作区不属于停问条件,按 C/D 自行补齐并在回执的「本次自建」里说明;信息齐全且归属唯一时直接发布。",
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
// 自建节点/表/时间线/工作区与标签的口径、以及「何时可以直接发布」只由 SOP 的 C/D/E/F/G 定义,
|
|
20
|
+
// 边界规则里不再重复授权话题,否则两处口径会互相打架(F 说自行补齐就能发,边界说必须先停下要授权)。
|
|
21
|
+
const BOUNDARIES = [
|
|
22
|
+
"不要改 tasks.create / tasks.update 的 schema 与业务语义,也不要为了完成流程伪造数据或证据。",
|
|
23
|
+
"一次性问答、临时查询不建任务;物品登记走 storage.create,只有开发/修复工作项才进任务表。",
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
function textOf(value) {
|
|
27
|
+
return typeof value === "string" ? value : "";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 预览时替换「生成时间」,避免把一次性的时间戳印进只读模板。 */
|
|
31
|
+
export const PREVIEW_STAMP = "<生成时间>";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 构造送进下一轮模型的 brief 文本。
|
|
35
|
+
*
|
|
36
|
+
* `preview: true` 只把生成时间换成占位符,供「命令」页展示只读模板;
|
|
37
|
+
* 需求与会话 id 由调用方传占位符(`{需求描述}` / `<sessionId>`),
|
|
38
|
+
* 正式发布的默认行为完全不变。
|
|
39
|
+
*/
|
|
40
|
+
export function buildPublisherBrief({ requirement, sessionId, now, preview } = {}) {
|
|
41
|
+
const raw = textOf(requirement);
|
|
42
|
+
const sid = textOf(sessionId).trim() || "unknown";
|
|
43
|
+
const stamp = preview
|
|
44
|
+
? PREVIEW_STAMP
|
|
45
|
+
: now instanceof Date && !Number.isNaN(now.getTime())
|
|
46
|
+
? now.toISOString()
|
|
47
|
+
: new Date().toISOString();
|
|
48
|
+
return [
|
|
49
|
+
"请按任务发布 SOP 处理下面这条需求,并在本轮结束时给出发布结果或明确的待确认问题。",
|
|
50
|
+
"",
|
|
51
|
+
"## 用户原话(逐字保留,不要先改写)",
|
|
52
|
+
raw.trim(),
|
|
53
|
+
"",
|
|
54
|
+
"## 本轮角色与产出目标",
|
|
55
|
+
"你是任务发布助手:把用户的一句话需求整理成可执行、可核对、可追踪的任务,再决定是否发布。产出是「润色后的任务草案 + 归属与时间线判断 + 发布结果或待确认问题」,不是直接改代码。",
|
|
56
|
+
"",
|
|
57
|
+
"## 发布 SOP(按顺序执行)",
|
|
58
|
+
...SOP_STEPS,
|
|
59
|
+
"",
|
|
60
|
+
"## 输出格式",
|
|
61
|
+
"1. 现状调研:三条以内,标注证据来源;没有证据就写「未核实」。",
|
|
62
|
+
"2. 任务草案:标题、背景、目标、范围与非目标、验收标准、优先级、难度、计划完成日期。",
|
|
63
|
+
"3. 归属、时间线与标签:任务表完整路径(含 tableId)、工作区归属(无工作区,或名称 + id 列表)、时间线归属或「无时间线归属」、标签(列出全部名称,新建的单独标出)、理由。",
|
|
64
|
+
"4. 发布回执或待确认问题:已发布时给任务 id、路径、工作区归属(无工作区时写明「无工作区」,多个时列全名称与 id)、时间线、标签(完整名称;新建的单独标出,未打标须说明原因)、完成日期与「本次自建」段(没有自建就写「本次自建:无」);未发布时给需要用户拍板的具体问题。",
|
|
65
|
+
"",
|
|
66
|
+
"## 边界规则",
|
|
67
|
+
...BOUNDARIES.map((line) => "- " + line),
|
|
68
|
+
"",
|
|
69
|
+
"## 会话信息",
|
|
70
|
+
"sessionId: " + sid,
|
|
71
|
+
// 不要把 dsh-session: 前缀和裸 sessionId 连写:DSH 会把 "dsh-session:<base64url>" 当会话引用 URI,
|
|
72
|
+
// 裸 id 不是合法 payload,会让下一轮在模型调用前就解析失败。这里只描述拼法,由模型自行组装。
|
|
73
|
+
"dedupeKey 建议: 用 \"dsh-session:\" 前缀拼接上面的 sessionId(前缀后接的是会话 id 本身)",
|
|
74
|
+
"生成时间: " + stamp,
|
|
75
|
+
].join("\n");
|
|
76
|
+
}
|