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,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 斜杠命令目录:`/命令` 页与 `tasks.commands` 的唯一事实来源。
|
|
3
|
+
*
|
|
4
|
+
* 只放元信息(名称、用途、用法、输入提示、示例、字符上限与说明),
|
|
5
|
+
* 命令正文一律由 `buildBrief` 现场生成——例如发布命令的 SOP 正文仍在 brief.js,
|
|
6
|
+
* 目录里不复制一份,避免两处文字随版本漂移。
|
|
7
|
+
*
|
|
8
|
+
* 新增一个斜杠命令:在 TASK_COMMANDS 里加一条,再把 handler 的注册交给 command.js 的循环,
|
|
9
|
+
* 页面、tasks.commands 与注册逻辑会自动跟上。
|
|
10
|
+
*/
|
|
11
|
+
import { buildPublisherBrief, MAX_REQUIREMENT_LENGTH } from "./brief.js";
|
|
12
|
+
|
|
13
|
+
/** 只读模板里替换需求与会话 id 的占位符;页面提示「换回你的原话/当前会话 id」即为此。 */
|
|
14
|
+
export const TEMPLATE_PLACEHOLDERS = {
|
|
15
|
+
requirement: "{需求描述}",
|
|
16
|
+
sessionId: "<sessionId>",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const TASK_COMMANDS = [
|
|
20
|
+
{
|
|
21
|
+
name: "wb-task-publisher",
|
|
22
|
+
title: { zh: "发布任务", en: "Publish task" },
|
|
23
|
+
summary: {
|
|
24
|
+
zh: "按调研、润色、归属、时间线、标签、查重的固定流程发布任务",
|
|
25
|
+
en: "Publish a task through the fixed flow: research, polish, ownership, timeline, tags, dedupe",
|
|
26
|
+
},
|
|
27
|
+
usage: {
|
|
28
|
+
zh: "/wb-task-publisher <需求描述>",
|
|
29
|
+
en: "/wb-task-publisher <requirement>",
|
|
30
|
+
},
|
|
31
|
+
inputHint: {
|
|
32
|
+
zh: "<需求描述,可附截图/文档>",
|
|
33
|
+
en: "<requirement; screenshots or docs allowed>",
|
|
34
|
+
},
|
|
35
|
+
attachments: true,
|
|
36
|
+
limit: { field: { zh: "需求描述", en: "requirement" }, max: MAX_REQUIREMENT_LENGTH },
|
|
37
|
+
examples: [
|
|
38
|
+
{ text: "/wb-task-publisher 记录页筛选栏加一组常用筛选快捷按钮" },
|
|
39
|
+
{ text: "/wb-task-publisher 时间线页支持按成员任务状态过滤(可附截图)" },
|
|
40
|
+
{ text: "/wb-task-publisher 修复配置页重置按钮不生效(附复现步骤)" },
|
|
41
|
+
],
|
|
42
|
+
sentText: {
|
|
43
|
+
zh: "已把发布引导送入下一轮:先调研现状,再润色、判归属与时间线、自由联想标签、查重,缺任务表/时间线/工作区会自行补齐,最后发布并在回执说明标签与自建内容。",
|
|
44
|
+
en: "Publisher guidance sent to the next turn: research, polish, ownership, timeline, tags, dedupe — missing tables, timelines or workspaces are created without asking — then publish, listing tags and anything it created.",
|
|
45
|
+
},
|
|
46
|
+
notes: {
|
|
47
|
+
zh: "命令会把你的原话与发布 SOP 送入下一轮模型:先调研现状,再润色、判归属与时间线、自由联想标签(未知名字自动创建)、查重,缺任务表 / 时间线 / 工作区时会自行补齐、不再追着你确认,并在回执里列出全部标签(新建的单独标出)与「本次自建」内容。可以在需求后附截图或文档。",
|
|
48
|
+
en: "The command sends your words plus the publishing SOP into the next model turn: research the current state, polish, decide ownership and timeline, invent tags freely (unknown names are created automatically), check for duplicates, missing tables, timelines or workspaces are created without asking and listed in the receipt along with the final tags. Screenshots or docs can be attached.",
|
|
49
|
+
},
|
|
50
|
+
buildBrief: (options) => buildPublisherBrief(options),
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
/** 按名称取目录条目;找不到返回 undefined。 */
|
|
55
|
+
export function findCommand(name) {
|
|
56
|
+
const key = String(name || "");
|
|
57
|
+
return TASK_COMMANDS.find((entry) => entry.name === key);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 投影成可序列化的纯数据(去掉 buildBrief 等函数),供 tasks.commands 与页面使用。 */
|
|
61
|
+
export function commandCatalog() {
|
|
62
|
+
return TASK_COMMANDS.map((entry) => ({
|
|
63
|
+
name: entry.name,
|
|
64
|
+
title: entry.title,
|
|
65
|
+
summary: entry.summary,
|
|
66
|
+
usage: entry.usage,
|
|
67
|
+
inputHint: entry.inputHint,
|
|
68
|
+
attachments: entry.attachments !== false,
|
|
69
|
+
limit: entry.limit ? { field: entry.limit.field, max: entry.limit.max } : null,
|
|
70
|
+
examples: (entry.examples || []).map((item) => item.text),
|
|
71
|
+
notes: entry.notes,
|
|
72
|
+
// 模板由 buildBrief 现场渲染,不另存一份正文:预览时需求与会话 id 用占位符、生成时间用 <生成时间>。
|
|
73
|
+
template: renderTemplate(entry),
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 用占位符渲染命令正文;没有 buildBrief 的条目没有模板。 */
|
|
78
|
+
function renderTemplate(entry) {
|
|
79
|
+
if (typeof entry.buildBrief !== "function") return "";
|
|
80
|
+
return entry.buildBrief({
|
|
81
|
+
requirement: TEMPLATE_PLACEHOLDERS.requirement,
|
|
82
|
+
sessionId: TEMPLATE_PLACEHOLDERS.sessionId,
|
|
83
|
+
preview: true,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 命令的多行用法说明(空输入时回给用户):第一行用法,随后示例,末段说明。
|
|
89
|
+
* @param {object} entry TASK_COMMANDS 条目
|
|
90
|
+
* @param {"zh"|"en"} lang 语言
|
|
91
|
+
*/
|
|
92
|
+
export function usageText(entry, lang) {
|
|
93
|
+
const zh = lang !== "en";
|
|
94
|
+
const pick = (value) => (zh ? value.zh : value.en);
|
|
95
|
+
const lines = [(zh ? "用法:" : "Usage: ") + pick(entry.usage), "", zh ? "示例:" : "Examples:"];
|
|
96
|
+
for (const item of entry.examples || []) lines.push(" " + item.text);
|
|
97
|
+
lines.push("", pick(entry.notes));
|
|
98
|
+
return lines.join("\n");
|
|
99
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 任务插件注册的斜杠命令:把「用户原话 + 各自 SOP」送进下一轮模型。
|
|
3
|
+
* 命令本身不建任务、不写业务数据;无 commands 服务(ACP/无 UI)时静默跳过。
|
|
4
|
+
*
|
|
5
|
+
* 命令的元信息与正文生成函数都在 catalog.js:这里只做注册循环,
|
|
6
|
+
* 新增命令时改目录即可,不用再写一遍 register 调用。
|
|
7
|
+
*/
|
|
8
|
+
import { TASK_COMMANDS, usageText } from "./catalog.js";
|
|
9
|
+
|
|
10
|
+
export const PUBLISHER_COMMAND = TASK_COMMANDS[0].name;
|
|
11
|
+
|
|
12
|
+
export const PUBLISHER_USAGE = usageText(TASK_COMMANDS[0], "zh");
|
|
13
|
+
|
|
14
|
+
function newMessageId() {
|
|
15
|
+
const c = globalThis.crypto;
|
|
16
|
+
if (c && typeof c.randomUUID === "function") return c.randomUUID();
|
|
17
|
+
return "msg-" + Date.now().toString(36) + "-" + Math.random().toString(36).slice(2, 10);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 取当前会话 id;拿不到时用 unknown,仅用于 dedupeKey 提示。 */
|
|
21
|
+
function sessionIdOf(agent) {
|
|
22
|
+
return String((agent && agent.session && agent.session.id) || (agent && agent.id) || "");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 注册目录里的全部命令,返回清理函数。commands 服务缺失时返回空清理函数且不报错。 */
|
|
26
|
+
export function registerPublisherCommand(ctx) {
|
|
27
|
+
let commands;
|
|
28
|
+
try {
|
|
29
|
+
commands = ctx && typeof ctx.get === "function" ? ctx.get("commands") : undefined;
|
|
30
|
+
} catch {
|
|
31
|
+
commands = undefined;
|
|
32
|
+
}
|
|
33
|
+
if (!commands || typeof commands.register !== "function") return () => {};
|
|
34
|
+
|
|
35
|
+
const disposes = TASK_COMMANDS.map((entry) =>
|
|
36
|
+
commands.register({
|
|
37
|
+
name: entry.name,
|
|
38
|
+
description: entry.title.zh + ":" + entry.summary.zh,
|
|
39
|
+
input: { hint: entry.inputHint.zh, attachments: entry.attachments !== false },
|
|
40
|
+
// 正文由 followup 带入模型历史,命令日志里不再重复记一遍。
|
|
41
|
+
recordInput: false,
|
|
42
|
+
handler: ({ agent, rawInput, attachments } = {}) => {
|
|
43
|
+
const requirement = typeof rawInput === "string" ? rawInput.trim() : "";
|
|
44
|
+
if (requirement.length === 0) {
|
|
45
|
+
return { kind: "success", text: usageText(entry, "zh") };
|
|
46
|
+
}
|
|
47
|
+
const limit = entry.limit;
|
|
48
|
+
if (limit && requirement.length > limit.max) {
|
|
49
|
+
return {
|
|
50
|
+
kind: "error",
|
|
51
|
+
text:
|
|
52
|
+
limit.field.zh + "约 " + requirement.length + " 字符,超过 " + limit.max +
|
|
53
|
+
" 字符上限。请精简需求,或把长材料作为附件随命令发送。",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (!agent || typeof agent.followup !== "function") {
|
|
57
|
+
return { kind: "error", text: "当前会话无法接收下一轮消息," + entry.title.zh + "引导没有送出。" };
|
|
58
|
+
}
|
|
59
|
+
const brief = entry.buildBrief({
|
|
60
|
+
requirement,
|
|
61
|
+
sessionId: sessionIdOf(agent),
|
|
62
|
+
now: new Date(),
|
|
63
|
+
});
|
|
64
|
+
const extra = Array.isArray(attachments) ? attachments : [];
|
|
65
|
+
const content = extra.length > 0
|
|
66
|
+
? [...extra, { type: "text", text: brief }]
|
|
67
|
+
: [{ type: "text", text: brief }];
|
|
68
|
+
try {
|
|
69
|
+
agent.followup({ id: newMessageId(), role: "user", content, source: { kind: "user" } });
|
|
70
|
+
} catch (error) {
|
|
71
|
+
const message = error && error.message ? error.message : String(error);
|
|
72
|
+
return { kind: "error", text: entry.title.zh + "引导送入失败:" + message };
|
|
73
|
+
}
|
|
74
|
+
return { kind: "success", text: entry.sentText.zh };
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
return () => {
|
|
79
|
+
for (const dispose of disposes) if (typeof dispose === "function") dispose();
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** 年度任务活动:日期字段保持本地 YYYY-MM-DD,与统计列表一致;只读 SQL 聚合。
|
|
2
|
+
* 事件口径定义在 ./events.js,与难度分布的五维切换共用,避免两处漂移。 */
|
|
3
|
+
import { EVENTS, EVENT_TYPES } from "./events.js";
|
|
4
|
+
import { PLAN, planRange } from "./events.js";
|
|
5
|
+
|
|
6
|
+
const TYPES = EVENT_TYPES;
|
|
7
|
+
// target 是计划口径:与四类事件并列展示,但不计入 total/totals.total(活动总数只数事件)。
|
|
8
|
+
const zero = () => ({ publish: 0, done: 0, shutdown: 0, claimed: 0, target: 0, total: 0 });
|
|
9
|
+
|
|
10
|
+
function validDay(value) {
|
|
11
|
+
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
|
|
12
|
+
const [year, month, day] = value.split('-').map(Number);
|
|
13
|
+
const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
14
|
+
return year >= 1900 && year <= 9999 && month >= 1 && month <= 12 && day >= 1 &&
|
|
15
|
+
day <= [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function activityFilter(activityDate, activityType) {
|
|
19
|
+
if (activityDate === undefined && activityType === undefined) return null;
|
|
20
|
+
if (!validDay(activityDate)) throw new Error('activityDate 必须是合法的 YYYY-MM-DD 日期(1900..9999 年)');
|
|
21
|
+
const type = activityType === undefined ? 'done' : activityType;
|
|
22
|
+
if (!TYPES.includes(type)) throw new Error('activityType 只支持 publish / done / shutdown / claimed');
|
|
23
|
+
const event = EVENTS[type];
|
|
24
|
+
return {
|
|
25
|
+
date: activityDate, type,
|
|
26
|
+
sql: '(' + event.condition + ' AND ' + event.date + '=?)',
|
|
27
|
+
params: [activityDate],
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function activityCalendar(db, args = {}) {
|
|
32
|
+
const year = args.year;
|
|
33
|
+
if (!Number.isInteger(year) || year < 1900 || year > 9999) throw new Error('year 必填,必须是 1900..9999 的整数');
|
|
34
|
+
const from = year + '-01-01', to = year + '-12-31';
|
|
35
|
+
const plan = planRange(from, to);
|
|
36
|
+
const sql = TYPES.map(key => {
|
|
37
|
+
const event = EVENTS[key];
|
|
38
|
+
return "SELECT " + event.date + " AS date, '" + key + "' AS kind, COUNT(*) AS n FROM task t WHERE t.deleted_at IS NULL AND " +
|
|
39
|
+
event.condition + ' AND ' + event.date + ' BETWEEN ? AND ? GROUP BY ' + event.date;
|
|
40
|
+
}).concat([
|
|
41
|
+
"SELECT " + PLAN.date + " AS date, '" + PLAN.key + "' AS kind, COUNT(*) AS n FROM task t WHERE " +
|
|
42
|
+
plan.sql + ' GROUP BY ' + PLAN.date,
|
|
43
|
+
]).join(' UNION ALL ') + ' ORDER BY date';
|
|
44
|
+
const rows = db.prepare(sql).all(...TYPES.flatMap(() => [from, to]), ...plan.params);
|
|
45
|
+
const daily = new Map();
|
|
46
|
+
for (const row of rows) {
|
|
47
|
+
if (!validDay(row.date)) continue;
|
|
48
|
+
if (!daily.has(row.date)) daily.set(row.date, { date: row.date, publish: 0, done: 0, shutdown: 0, claimed: 0, target: 0 });
|
|
49
|
+
daily.get(row.date)[row.kind] = Number(row.n);
|
|
50
|
+
}
|
|
51
|
+
const days = [...daily.values()], totals = zero(), max = zero();
|
|
52
|
+
for (const day of days) {
|
|
53
|
+
let total = 0;
|
|
54
|
+
for (const key of TYPES) {
|
|
55
|
+
totals[key] += day[key]; max[key] = Math.max(max[key], day[key]); total += day[key];
|
|
56
|
+
}
|
|
57
|
+
// 计划口径单列累加:既不进 total,也不参与 max.total(活动总数保持历史口径)。
|
|
58
|
+
totals.target += day.target; max.target = Math.max(max.target, day.target);
|
|
59
|
+
totals.total += total; max.total = Math.max(max.total, total);
|
|
60
|
+
}
|
|
61
|
+
return { year, days, totals, max };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function registerActivityCalendarAction(api) {
|
|
65
|
+
api.action('tasks.activityCalendar', {
|
|
66
|
+
description: '只读年度任务活动日历,独立于 statsReport 时间范围。排除已删除任务;publish=publish_date,done=当前 status=2 且 done_date,shutdown=当前 status=1 且 shutdown_date,claimed=当前 status IN (2,4) 且按 COALESCE(claimed_at 转本地日期, publish_date) 计——已完成任务因终态清空接取列而不写库回填,历史任务的接取日按发布日期近似,前端图例注明;target=计划口径,取未删除任务的目标计划日期 target_date,按日期计数,与事件口径分开;日期均为本地日期,不按 UTC 转换。返回 year、按日期升序且仅含有事件或有目标计划日的 days:[{date,publish,done,shutdown,claimed,target}]、totals 与 max(均含四类事件、target 及 total);totals.total 只累加四类事件,不含 target,同一任务可贡献多类事件;max.total 是单日四类事件数之和的最大值,max.target 是单日目标计划数的最大值;空年份各值为0。',
|
|
67
|
+
input: { type: 'object', required: ['year'], properties: {
|
|
68
|
+
year: { type: 'integer', minimum: 1900, maximum: 9999, description: '必填公历年份,1900..9999 整数;无默认值,不接受字符串。' },
|
|
69
|
+
} },
|
|
70
|
+
examples: [{ year: 2024 }],
|
|
71
|
+
execute: args => activityCalendar(api.db, args || {}),
|
|
72
|
+
render: result => JSON.stringify(result),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// 难度分布的维度归一与分桶:统计页把「当前存量」与「按事件区间」两种口径统一成一张柱图,
|
|
2
|
+
// 分桶规则(0.5 步进、0 与空归「未设置」)与事件口径的区间接入都放这里,stats.js 只负责取数编排。
|
|
3
|
+
import { eventRange } from "./events.js";
|
|
4
|
+
|
|
5
|
+
export const DIFFICULTY_DIMENSIONS = ["all", "done", "publish", "claimed", "shutdown"];
|
|
6
|
+
|
|
7
|
+
export function normalizeDimension(value) {
|
|
8
|
+
const dimension = value === undefined || value === null || value === "" ? "all" : String(value);
|
|
9
|
+
if (DIFFICULTY_DIMENSIONS.indexOf(dimension) < 0) {
|
|
10
|
+
throw new Error("dimension 只支持 all / done / publish / claimed / shutdown");
|
|
11
|
+
}
|
|
12
|
+
return dimension;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function buildDifficultyBins(rows) {
|
|
16
|
+
// 难度是 0.5 步进的单值(0.5–5),一值一桶;0/空值单独归到「未设置」。
|
|
17
|
+
const bins = [];
|
|
18
|
+
for (let i = 1; i <= 10; i += 1) {
|
|
19
|
+
const v = i / 2;
|
|
20
|
+
bins.push({ key: String(v), label: String(v), count: 0 });
|
|
21
|
+
}
|
|
22
|
+
let unset = 0;
|
|
23
|
+
for (const row of rows) {
|
|
24
|
+
const n = Number(row.n || 0);
|
|
25
|
+
const d = Number(row.difficulty);
|
|
26
|
+
if (row.difficulty === null || row.difficulty === undefined || !(d > 0)) {
|
|
27
|
+
unset += n;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const idx = Math.min(10, Math.max(1, Math.round(d * 2)));
|
|
31
|
+
bins[idx - 1].count += n;
|
|
32
|
+
}
|
|
33
|
+
// 未填写必须可见,便于从统计巡检并补齐难度。
|
|
34
|
+
bins.push({ key: "unset", label: "未设置", count: unset });
|
|
35
|
+
return bins;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** dimension=all 走当前存量;其余四维与年度活动日历同口径、只统计区间内的任务。 */
|
|
39
|
+
export function difficultyBinsFor(db, dimension, range) {
|
|
40
|
+
if (dimension === "all") {
|
|
41
|
+
return buildDifficultyBins(db.prepare(
|
|
42
|
+
"SELECT difficulty, COUNT(*) AS n FROM task WHERE deleted_at IS NULL GROUP BY difficulty",
|
|
43
|
+
).all());
|
|
44
|
+
}
|
|
45
|
+
const slice = eventRange(dimension, range.from, range.to);
|
|
46
|
+
return buildDifficultyBins(db.prepare(
|
|
47
|
+
"SELECT difficulty, COUNT(*) AS n FROM task t WHERE " + slice.sql + " GROUP BY difficulty",
|
|
48
|
+
).all(slice.params[0], slice.params[1]));
|
|
49
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// 任务事件口径的唯一来源:年度活动日历与难度分布的五维切换共用同一份日期定义,
|
|
2
|
+
// 避免「完成」在两张图上出现两种算法。日期字段保持本地 YYYY-MM-DD。
|
|
3
|
+
//
|
|
4
|
+
// claimed 用 COALESCE(claimed_at 本地日, publish_date) 兜底:终态会清空接取三列(spec 40),
|
|
5
|
+
// 已完成任务不写库、不回填,历史任务的接取日按发布日期近似,图例里注明。
|
|
6
|
+
|
|
7
|
+
const CLAIMED_DAY =
|
|
8
|
+
"COALESCE(strftime('%Y-%m-%d', t.claimed_at/1000, 'unixepoch', 'localtime'), t.publish_date)";
|
|
9
|
+
|
|
10
|
+
export const EVENTS = {
|
|
11
|
+
publish: { date: "t.publish_date", condition: "1=1" },
|
|
12
|
+
done: { date: "t.done_date", condition: "t.status='2'" },
|
|
13
|
+
shutdown: { date: "t.shutdown_date", condition: "t.status='1'" },
|
|
14
|
+
claimed: { date: CLAIMED_DAY, condition: "t.status IN ('2','4')" },
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const EVENT_TYPES = Object.keys(EVENTS);
|
|
18
|
+
|
|
19
|
+
// 计划口径:目标计划日期(target_date)落在区间内的未删除任务数。
|
|
20
|
+
// 计划不是活动事件——故意不进 EVENTS:那会让难度分布凭空多一维,
|
|
21
|
+
// 也会把「活动总数」这个用户看惯的数字静默改写。年度活动日历与范围趋势图都从这里取,
|
|
22
|
+
// 避免两处各写一份日期条件后缓慢漂移。
|
|
23
|
+
export const PLAN = { key: "target", date: "t.target_date", condition: "1=1" };
|
|
24
|
+
|
|
25
|
+
/** 事件在 [from, to] 区间内的 SQL 片段与参数(含已删除任务排除)。 */
|
|
26
|
+
export function eventRange(type, from, to) {
|
|
27
|
+
const event = EVENTS[type];
|
|
28
|
+
return {
|
|
29
|
+
sql: "t.deleted_at IS NULL AND (" + event.condition + ") AND " + event.date + " BETWEEN ? AND ?",
|
|
30
|
+
params: [from, to],
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 计划在 [from, to] 区间内的 SQL 片段与参数(排除已删除与空目标日)。 */
|
|
35
|
+
export function planRange(from, to) {
|
|
36
|
+
return {
|
|
37
|
+
sql: "t.deleted_at IS NULL AND " + PLAN.date + " IS NOT NULL AND " + PLAN.date + " BETWEEN ? AND ?",
|
|
38
|
+
params: [from, to],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/** Read-only overview; independent of legacy tree/children/path contracts. */
|
|
2
|
+
// id=0 is a virtual parent, never a node. UNION bounds recursion on corrupt imports.
|
|
3
|
+
const VISIBLE = `WITH RECURSIVE visible AS (
|
|
4
|
+
SELECT * FROM task_node WHERE parent_id='0' AND id<>'0' AND deleted_at IS NULL
|
|
5
|
+
UNION
|
|
6
|
+
SELECT n.* FROM task_node n JOIN visible p ON n.parent_id=p.id
|
|
7
|
+
WHERE n.id<>'0' AND n.deleted_at IS NULL
|
|
8
|
+
)`;
|
|
9
|
+
|
|
10
|
+
function descriptionLimit(args) {
|
|
11
|
+
const value = args.maxDescriptionLength;
|
|
12
|
+
if (value === undefined) return null;
|
|
13
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
|
|
14
|
+
throw new Error("maxDescriptionLength 必须是非负安全整数");
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function overview(db, args = {}) {
|
|
20
|
+
const limit = descriptionLimit(args);
|
|
21
|
+
const rows = db.prepare(VISIBLE + " SELECT * FROM visible ORDER BY sort ASC, id ASC").all();
|
|
22
|
+
const stats = new Map(db.prepare(VISIBLE + `, closure(ancestor, id) AS (
|
|
23
|
+
SELECT id, id FROM visible
|
|
24
|
+
UNION
|
|
25
|
+
SELECT c.ancestor, n.id FROM closure c JOIN visible n ON n.parent_id=c.id
|
|
26
|
+
)
|
|
27
|
+
SELECT c.ancestor AS id, COUNT(t.id) AS count,
|
|
28
|
+
SUM(CASE WHEN c.ancestor=c.id AND t.id IS NOT NULL THEN 1 ELSE 0 END) AS directCount,
|
|
29
|
+
MAX(MAX(n.updated_at, COALESCE(t.updated_at, 0))) AS lastUpdatedAt
|
|
30
|
+
FROM closure c JOIN visible n ON n.id=c.id
|
|
31
|
+
LEFT JOIN task t ON t.table_id=c.id AND t.deleted_at IS NULL
|
|
32
|
+
GROUP BY c.ancestor`).all().map(row => [String(row.id), row]));
|
|
33
|
+
const nodes = [];
|
|
34
|
+
const byId = new Map();
|
|
35
|
+
let descriptionTruncated = false;
|
|
36
|
+
for (const row of rows) {
|
|
37
|
+
const stat = stats.get(String(row.id));
|
|
38
|
+
const full = String(row.description || "");
|
|
39
|
+
const chars = Array.from(full);
|
|
40
|
+
const shortened = limit !== null && chars.length > limit;
|
|
41
|
+
descriptionTruncated ||= shortened;
|
|
42
|
+
byId.set(String(row.id), {
|
|
43
|
+
id: String(row.id), name: String(row.name), type: String(row.type),
|
|
44
|
+
typeName: ({ "0": "group", "1": "table", "2": "root_group" })[String(row.type)] || "unknown",
|
|
45
|
+
description: shortened ? chars.slice(0, limit).join("") : full,
|
|
46
|
+
descriptionSupported: true, descriptionTruncated: shortened,
|
|
47
|
+
path: [], count: Number(stat?.count || 0), directCount: Number(stat?.directCount || 0),
|
|
48
|
+
lastUpdatedAt: Number(stat?.lastUpdatedAt || row.updated_at),
|
|
49
|
+
canContainRecords: String(row.type) === "1", children: [],
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
for (const row of rows) {
|
|
53
|
+
const node = byId.get(String(row.id));
|
|
54
|
+
const parent = byId.get(String(row.parent_id));
|
|
55
|
+
if (parent) parent.children.push(node);
|
|
56
|
+
else nodes.push(node);
|
|
57
|
+
}
|
|
58
|
+
const pending = nodes.slice().reverse();
|
|
59
|
+
while (pending.length) {
|
|
60
|
+
const node = pending.pop();
|
|
61
|
+
node.path = [...node.path, { id: node.id, name: node.name, type: node.type }];
|
|
62
|
+
for (const child of node.children) child.path = node.path;
|
|
63
|
+
pending.push(...node.children.slice().reverse());
|
|
64
|
+
}
|
|
65
|
+
return { nodes, total: rows.length, returned: rows.length, truncated: false, descriptionTruncated };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function renderOverview(result) {
|
|
69
|
+
const out = ["任务树概览(节点 " + result.returned + "/" + result.total + ",truncated=" + result.truncated + ")"];
|
|
70
|
+
const pending = result.nodes.slice().reverse().map(node => ({ node, depth: 0 }));
|
|
71
|
+
while (pending.length) {
|
|
72
|
+
const { node, depth } = pending.pop();
|
|
73
|
+
const prefix = " ".repeat(depth);
|
|
74
|
+
const label = (node.canContainRecords ? "表,可直接放任务" : "组,只能装子节点,不能直接放任务") + ";子树 " + node.count + " 个任务";
|
|
75
|
+
out.push(prefix + "- " + node.name + " [" + label + "] id=" + node.id);
|
|
76
|
+
out.push(prefix + " 路径:" + node.path.map(part => part.name).join(" / "));
|
|
77
|
+
out.push(prefix + " 最近更新(Unix毫秒):" + node.lastUpdatedAt);
|
|
78
|
+
out.push(prefix + " 描述:" + (node.description || "(空)") + (node.descriptionTruncated ? "…(已截断)" : ""));
|
|
79
|
+
for (const child of node.children.slice().reverse()) pending.push({ node: child, depth: depth + 1 });
|
|
80
|
+
}
|
|
81
|
+
return out.join("\n");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function registerOverviewAction(api) {
|
|
85
|
+
api.action("taskNodes.overview", {
|
|
86
|
+
description: "只读全量任务树概览:全文描述、group/table/root_group、面包屑、子树count与directCount、lastUpdatedAt、children;先overview再create选tableId,组不可直接放任务。返回nodes/total/returned/truncated/descriptionTruncated。",
|
|
87
|
+
input: {
|
|
88
|
+
type: "object",
|
|
89
|
+
properties: {
|
|
90
|
+
maxDescriptionLength: {
|
|
91
|
+
type: "integer", minimum: 0, maximum: Number.MAX_SAFE_INTEGER,
|
|
92
|
+
description: "可选描述字符数上限(Unicode码点);默认全文,0省略描述正文。结构化结果与文本同时生效,descriptionTruncated 标识描述截断;节点始终全量返回。",
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
execute: args => overview(api.db, args || {}),
|
|
97
|
+
render: renderOverview,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// 今日活动独立于报表 range;历史日期允许附带时间,只比较前十位。
|
|
2
|
+
// claimed 只对已接取/已完成生效,并用 COALESCE(claimed_at 本地日, publish_date) 兜底:
|
|
3
|
+
// 终态清空接取列,历史任务不回填,接取日按发布日期近似。
|
|
4
|
+
export const TODAY_STATS_DESCRIPTION = "服务端本地日期 date(YYYY-MM-DD);published=publish_date 前十位为今日的全部状态任务,done=当前 status=2 且 done_date 前十位为今日,shutdown=当前 status=1 且 shutdown_date 前十位为今日,claimed=当前 status IN (2,4) 且 COALESCE(claimed_at 转本地日期, publish_date) 为今日(已完成任务不回填接取时间,按发布日期近似);均排除软删除。avgDifficultyDone 仅统计上述今日完成且 difficulty>0 的任务,四舍五入一位小数,无样本为 null;avgSample 为有效样本数。今日指标独立于 range/from/to";
|
|
5
|
+
|
|
6
|
+
export function todayActivity(db, date) {
|
|
7
|
+
const row = db.prepare(
|
|
8
|
+
"SELECT " +
|
|
9
|
+
"COUNT(CASE WHEN substr(publish_date,1,10)=? THEN 1 END) AS published, " +
|
|
10
|
+
"COUNT(CASE WHEN status='2' AND substr(done_date,1,10)=? THEN 1 END) AS done, " +
|
|
11
|
+
"COUNT(CASE WHEN status='1' AND substr(shutdown_date,1,10)=? THEN 1 END) AS shutdown, " +
|
|
12
|
+
"COUNT(CASE WHEN status IN ('2','4') AND COALESCE(strftime('%Y-%m-%d', claimed_at/1000, 'unixepoch', 'localtime'), substr(publish_date,1,10))=? THEN 1 END) AS claimed, " +
|
|
13
|
+
"ROUND(AVG(CASE WHEN status='2' AND substr(done_date,1,10)=? AND difficulty>0 THEN difficulty END),1) AS avgDifficultyDone, " +
|
|
14
|
+
"COUNT(CASE WHEN status='2' AND substr(done_date,1,10)=? AND difficulty>0 THEN 1 END) AS avgSample " +
|
|
15
|
+
"FROM task WHERE deleted_at IS NULL",
|
|
16
|
+
).get(date, date, date, date, date, date);
|
|
17
|
+
return {
|
|
18
|
+
date,
|
|
19
|
+
published: Number(row.published),
|
|
20
|
+
done: Number(row.done),
|
|
21
|
+
shutdown: Number(row.shutdown),
|
|
22
|
+
claimed: Number(row.claimed),
|
|
23
|
+
avgDifficultyDone: row.avgDifficultyDone === null ? null : Number(row.avgDifficultyDone),
|
|
24
|
+
avgSample: Number(row.avgSample),
|
|
25
|
+
};
|
|
26
|
+
}
|