Files
Agent-Workbench/docs/m2-live-model.diff

111 lines
4.1 KiB
Diff
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
diff --git a/src/cli.mjs b/src/cli.mjs
index 090ad4d..b2a2acd 100644
--- a/src/cli.mjs
+++ b/src/cli.mjs
@@ -2,7 +2,20 @@ import fs from 'node:fs';
import { normalizeTasks } from './normalize.mjs';
import { summarize } from './summary.mjs';
import { renderText } from './render.mjs';
+
try {
- const tasks = normalizeTasks(JSON.parse(fs.readFileSync(process.argv[2], 'utf8')));
- console.log(renderText(summarize(tasks)));
-} catch (error) { console.error(error.message); }
+ const args = process.argv.slice(2);
+ if (args.length < 1 || args.length > 2 || !args[0] || args[0].startsWith('-') ||
+ (args.length === 2 && args[1] !== '--json')) {
+ throw new TypeError('用法:node src/cli.mjs <JSON文件> [--json]');
+ }
+
+ const tasks = normalizeTasks(JSON.parse(fs.readFileSync(args[0], 'utf8')));
+ const summary = summarize(tasks);
+ const output = args[1] === '--json' ? JSON.stringify(summary) : renderText(summary);
+ process.stdout.write(output + '\n');
+ process.exitCode = 0;
+} catch (error) {
+ process.stderr.write(`输入错误:${error.message}\n`);
+ process.exitCode = 2;
+}
diff --git a/src/normalize.mjs b/src/normalize.mjs
index 70d376d..1a08d4d 100644
--- a/src/normalize.mjs
+++ b/src/normalize.mjs
@@ -1,4 +1,36 @@
+const statuses = new Set(['todo', 'doing', 'done', 'blocked']);
+
export function normalizeTasks(input) {
- if (!Array.isArray(input)) throw new TypeError('tasks must be an array');
- return input;
+ if (!Array.isArray(input)) throw new TypeError('任务列表必须是数组');
+ if (input.length > 10000) throw new RangeError('任务列表最多包含10000项');
+
+ const ids = new Set();
+ const tasks = [];
+ for (const [index, task] of input.entries()) {
+ const label = `第${index + 1}项任务`;
+ if (task === null || typeof task !== 'object' || Array.isArray(task)) {
+ throw new TypeError(`${label}必须是对象`);
+ }
+ if (typeof task.id !== 'string' || !task.id.trim()) {
+ throw new TypeError(`${label}的id必须是非空字符串`);
+ }
+ if (typeof task.title !== 'string' || !task.title.trim()) {
+ throw new TypeError(`${label}的title必须是非空字符串`);
+ }
+
+ const id = task.id.trim();
+ const title = task.title.trim();
+ const { status, minutes } = task;
+ if (ids.has(id)) throw new TypeError(`${label}的id重复:${id}`);
+ if (!statuses.has(status)) {
+ throw new TypeError(`${label}的status必须是todo、doing、done或blocked`);
+ }
+ if (!Number.isSafeInteger(minutes) || minutes < 0 || minutes > 1000000) {
+ throw new RangeError(`${label}的minutes必须是0到1000000之间的安全整数`);
+ }
+
+ ids.add(id);
+ tasks.push({ id, title, status, minutes });
+ }
+ return tasks;
}
diff --git a/src/render.mjs b/src/render.mjs
index f8ab62f..7f923c1 100644
--- a/src/render.mjs
+++ b/src/render.mjs
@@ -1,3 +1,8 @@
export function renderText(summary) {
- return `Done: ${summary.counts.done}/${summary.total}`;
+ const { total, counts, completedPercent, remainingMinutes } = summary;
+ return [
+ `交付进度:${counts.done}/${total}(${completedPercent}%)`,
+ `待办 ${counts.todo} · 进行中 ${counts.doing} · 已完成 ${counts.done} · 阻塞 ${counts.blocked}`,
+ `剩余预计:${remainingMinutes} 分钟`,
+ ].join('\n');
}
diff --git a/src/summary.mjs b/src/summary.mjs
index 2d615a3..3db2263 100644
--- a/src/summary.mjs
+++ b/src/summary.mjs
@@ -1,5 +1,17 @@
export function summarize(tasks) {
- const done = tasks.filter(task => task.status === 'done').length;
- return { total: tasks.length, counts: { done }, completedPercent: tasks.length ? 100 : 0,
- remainingMinutes: tasks.reduce((sum, task) => sum + task.minutes, 0) };
+ const counts = { todo: 0, doing: 0, done: 0, blocked: 0 };
+ let remainingMinutes = 0;
+
+ for (const task of tasks) {
+ counts[task.status] += 1;
+ if (task.status !== 'done') remainingMinutes += task.minutes;
+ }
+
+ const total = tasks.length;
+ return {
+ total,
+ counts,
+ completedPercent: total === 0 ? 0 : Math.round(counts.done / total * 100),
+ remainingMinutes,
+ };
}