task-tree.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import type { TaskNode } from "@/lib/types";
  2. export interface TaskBranch {
  3. task: TaskNode;
  4. children: TaskBranch[];
  5. }
  6. function pathParts(path: string) {
  7. return path.split(".").map((part) => Number.parseInt(part, 10));
  8. }
  9. export function compareTaskPath(left: TaskNode, right: TaskNode) {
  10. const a = pathParts(left.display_path);
  11. const b = pathParts(right.display_path);
  12. const length = Math.max(a.length, b.length);
  13. for (let index = 0; index < length; index += 1) {
  14. const delta = (a[index] ?? -1) - (b[index] ?? -1);
  15. if (delta !== 0) return delta;
  16. }
  17. return 0;
  18. }
  19. export function buildTaskForest(tasks: TaskNode[], visibleIds?: Set<string>): TaskBranch[] {
  20. const visible = tasks.filter((task) => !visibleIds || visibleIds.has(task.task_id));
  21. const byParent = new Map<string | null, TaskNode[]>();
  22. for (const task of visible) {
  23. const siblings = byParent.get(task.parent_task_id) ?? [];
  24. siblings.push(task);
  25. byParent.set(task.parent_task_id, siblings);
  26. }
  27. for (const siblings of byParent.values()) siblings.sort(compareTaskPath);
  28. const walk = (task: TaskNode): TaskBranch => ({
  29. task,
  30. children: (byParent.get(task.task_id) ?? []).map(walk),
  31. });
  32. return (byParent.get(null) ?? []).map(walk);
  33. }
  34. export function taskKindLabel(kind: string) {
  35. const labels: Record<string, string> = {
  36. root: "创作总目标",
  37. direction: "创作方向",
  38. "pattern-retrieval": "模式检索",
  39. "external-retrieval": "外部检索",
  40. "candidate-portfolio": "脚本候选集",
  41. compose: "整稿组合",
  42. structure: "脚本结构",
  43. paragraph: "脚本段落",
  44. "element-set": "内容元素",
  45. compare: "候选比较",
  46. };
  47. return labels[kind] ?? kind;
  48. }
  49. export function statusLabel(status: string) {
  50. const labels: Record<string, string> = {
  51. completed: "已验收",
  52. waiting_children: "等待子任务",
  53. needs_replan: "等待 Planner",
  54. in_progress: "执行中",
  55. pending: "待执行",
  56. awaiting_validation: "等待验收",
  57. awaiting_decision: "等待决策",
  58. blocked: "阶段边界",
  59. cancelled: "已取消",
  60. failed: "失败",
  61. stopped: "已停止",
  62. submitted: "已提交",
  63. passed: "通过",
  64. accept: "采用",
  65. };
  66. return labels[status] ?? status;
  67. }