dispatch-tasks.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // ============================================================
  2. // Dispatch Tasks tool - workspace-level task batch dispatch
  3. // ============================================================
  4. // 设计合同见根目录 docs/specs/workspace-task-dispatch.md。
  5. // 关键约束:
  6. // - 子会话不继承本对话历史,prompt 必须自包含;schema description 里显式告知模型。
  7. // - execute 可被用户在统一确认窗改写(仅创建 / 创建并执行),host 端以确认结果为准。
  8. // - 失败隔离:单项失败不阻塞其余,输出为逐项结果的有界列表,不回灌子会话正文。
  9. import { z } from "zod";
  10. import { toToolJsonSchema } from "./json-schema.js";
  11. const nonEmptyString = z.string().trim().min(1);
  12. export const DISPATCH_TASKS_MAX_TASKS = 50;
  13. export const DISPATCH_TASKS_TITLE_MAX_CHARS = 120;
  14. export const DispatchTaskItemSchema = z
  15. .object({
  16. id: nonEmptyString
  17. .max(64)
  18. .describe(
  19. "Short stable slug for this task, unique within this dispatch (for example 'fix-login-crash'). Used by dependsOn references and the result summary.",
  20. ),
  21. title: nonEmptyString
  22. .max(DISPATCH_TASKS_TITLE_MAX_CHARS)
  23. .describe(
  24. "Concise task title shown in the workspace task list, for example '修复登录页崩溃'. No file paths.",
  25. ),
  26. prompt: nonEmptyString.describe(
  27. "Self-contained instructions for this task. The new session does NOT inherit this conversation's history, so restate all needed context (files, symptoms, acceptance criteria) inside the prompt.",
  28. ),
  29. dependsOn: z
  30. .array(nonEmptyString.max(64))
  31. .max(DISPATCH_TASKS_MAX_TASKS)
  32. .optional()
  33. .describe(
  34. "Ids of tasks this one logically depends on. Display-only in v1 (all tasks run concurrently); kept for future ordered execution.",
  35. ),
  36. parallelGroup: z
  37. .number()
  38. .int()
  39. .min(0)
  40. .max(99)
  41. .optional()
  42. .describe(
  43. "Execution plan group (M-spec): tasks with the same parallelGroup run concurrently; groups run in ascending order. Omit for a single default group.",
  44. ),
  45. })
  46. .strict();
  47. export type DispatchTaskItem = z.infer<typeof DispatchTaskItemSchema>;
  48. export const DispatchTasksInputSchema = z
  49. .object({
  50. tasks: z.array(DispatchTaskItemSchema).min(1).max(DISPATCH_TASKS_MAX_TASKS).describe(
  51. "The organized task list to create under the current workspace. Deduplicate overlapping items first; each item must be independently actionable.",
  52. ),
  53. execute: z
  54. .boolean()
  55. .default(true)
  56. .describe(
  57. "Whether to also start executing each task after creation. The user can override this in the confirmation dialog (create-only mode leaves tasks as drafts).",
  58. ),
  59. })
  60. .strict();
  61. export type DispatchTasksInput = z.infer<typeof DispatchTasksInputSchema>;
  62. export const DispatchTasksInputJsonSchema = toToolJsonSchema(DispatchTasksInputSchema);
  63. /** 单项终态;failure 仅在 status 为 failed 时出现一行摘要,不带堆栈。 */
  64. export const DispatchTaskResultSchema = z
  65. .object({
  66. id: nonEmptyString.max(64),
  67. title: z.string(),
  68. status: z.enum(["created", "started", "failed"]),
  69. sessionId: nonEmptyString.optional(),
  70. failure: z.string().max(400).optional(),
  71. })
  72. .strict();
  73. export type DispatchTaskResult = z.infer<typeof DispatchTaskResultSchema>;
  74. export const DispatchTasksOutputSchema = z
  75. .object({
  76. executed: z.boolean(),
  77. createdCount: z.number().int().nonnegative(),
  78. failedCount: z.number().int().nonnegative(),
  79. results: z.array(DispatchTaskResultSchema).max(DISPATCH_TASKS_MAX_TASKS),
  80. message: nonEmptyString,
  81. })
  82. .strict();
  83. export type DispatchTasksOutput = z.infer<typeof DispatchTasksOutputSchema>;
  84. export const DispatchTasksOutputJsonSchema = toToolJsonSchema(DispatchTasksOutputSchema);