archivedTaskDeletion.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import type { IZCodeTaskService } from "@zcode/services";
  2. import { buildTaskWorkspaceKey } from "@/lib/taskQueryCache.js";
  3. import { logger } from "@/logger.js";
  4. export interface ArchivedTaskDeletionTarget {
  5. workspacePath: string;
  6. workspaceIdentity?: string;
  7. taskId: string;
  8. }
  9. export interface ArchivedTaskDeletionWorkspace {
  10. workspacePath: string;
  11. workspaceIdentity?: string;
  12. label: string;
  13. service?: Pick<IZCodeTaskService, "listArchivedTasks" | "deleteArchivedTasks">;
  14. }
  15. export async function collectArchivedTaskDeletion(workspaces: ArchivedTaskDeletionWorkspace[]) {
  16. const unique = new Map(
  17. workspaces.map((workspace) => [
  18. buildTaskWorkspaceKey(workspace.workspacePath, workspace.workspaceIdentity),
  19. workspace,
  20. ]),
  21. );
  22. const results = await Promise.all(
  23. [...unique.values()].map(async (workspace) => {
  24. if (!workspace.service) return { workspace, targets: null };
  25. try {
  26. // 直接读取完整归档集合,不能使用 UI 已折叠或过滤的 items。
  27. const tasks = await workspace.service.listArchivedTasks({
  28. workspacePath: workspace.workspacePath,
  29. workspaceIdentity: workspace.workspaceIdentity,
  30. });
  31. const targets = [...new Set(tasks.map((task) => task.taskId))].map((taskId) => ({
  32. taskId,
  33. workspacePath: workspace.workspacePath,
  34. workspaceIdentity: workspace.workspaceIdentity,
  35. }));
  36. return { workspace, targets };
  37. } catch (error) {
  38. logger.warn("[ArchivedTaskDeletion] 读取归档项目失败", {
  39. workspaceKey: buildTaskWorkspaceKey(workspace.workspacePath, workspace.workspaceIdentity),
  40. error,
  41. });
  42. return { workspace, targets: null };
  43. }
  44. }),
  45. );
  46. return {
  47. groups: results.filter((result) => result.targets !== null),
  48. count: results.reduce((count, result) => count + (result.targets?.length ?? 0), 0),
  49. unavailableWorkspaces: results
  50. .filter((result) => result.targets === null)
  51. .map((result) => result.workspace.label),
  52. };
  53. }
  54. export async function deleteArchivedTaskSelection(
  55. selection: Awaited<ReturnType<typeof collectArchivedTaskDeletion>>,
  56. onDeleted: (target: ArchivedTaskDeletionTarget) => void,
  57. ) {
  58. let deleted = 0;
  59. let skipped = 0;
  60. let failed = 0;
  61. // 同一 workspace 一次 RPC,让原 source 在逐项事务后统一发事件;独立 source 可以并行。
  62. await Promise.all(
  63. selection.groups.map(async ({ workspace, targets }) => {
  64. if (!targets?.length) return;
  65. let result: Awaited<ReturnType<IZCodeTaskService["deleteArchivedTasks"]>>;
  66. try {
  67. result = await workspace.service!.deleteArchivedTasks({
  68. workspacePath: workspace.workspacePath,
  69. workspaceIdentity: workspace.workspaceIdentity,
  70. taskIds: targets.map((target) => target.taskId),
  71. });
  72. } catch (error) {
  73. failed += targets.length;
  74. logger.warn("[ArchivedTaskDeletion] 工作区批次删除失败", {
  75. workspacePath: workspace.workspacePath,
  76. workspaceIdentity: workspace.workspaceIdentity,
  77. error,
  78. });
  79. return;
  80. }
  81. const deletedIds = new Set(result.deletedTaskIds);
  82. const skippedIds = new Set(result.skippedTaskIds);
  83. for (const target of targets) {
  84. if (deletedIds.has(target.taskId)) {
  85. deleted += 1;
  86. onDeleted(target);
  87. } else if (skippedIds.has(target.taskId)) {
  88. skipped += 1;
  89. } else {
  90. failed += 1;
  91. }
  92. }
  93. }),
  94. );
  95. return { deleted, skipped, failed, unavailableWorkspaces: selection.unavailableWorkspaces };
  96. }