automation-types.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import type { ZCodeTaskMode } from "./zcode-task-types-core.js";
  2. import type { ZCodeAutomationBotDeliveryTarget } from "./bots.js";
  3. import type { ModelSelection } from "./model-selection.js";
  4. // ---- 定时任务(Automation)领域类型 ----
  5. // automation / automation_runs 存 tasks-index.sqlite。
  6. // 这里是跨 services / cli / desktop / ui 复用的 camelCase 领域类型;sqlite 列名为 snake_case。
  7. /** 单个本地任务索引允许保留的定时任务定义总数;所有生命周期状态都计入。 */
  8. export const AUTOMATION_CREATE_LIMIT = 20;
  9. export const AUTOMATION_CREATE_LIMIT_ERROR_CODE = "AUTOMATION_CREATE_LIMIT_REACHED";
  10. export function isAutomationCreateLimitError(error: unknown): boolean {
  11. const message = typeof error === "string" ? error : error instanceof Error ? error.message : "";
  12. return message.includes(AUTOMATION_CREATE_LIMIT_ERROR_CODE);
  13. }
  14. /** 整条 automation 的生命周期。 */
  15. export type ZCodeAutomationLifecycleStatus = "active" | "completed" | "failed" | "paused";
  16. /** 单条 automation 当前一轮的派发信息态(供 UI/诊断)。 */
  17. export type ZCodeAutomationDispatchStatus =
  18. | "idle"
  19. | "claimed"
  20. | "dispatched"
  21. | "failed_to_dispatch";
  22. /** workspace 位置;本期恒为 local,远端预留。 */
  23. export type ZCodeAutomationLocationKind = "local" | "remote";
  24. /** 触发来源:定时 or 立即运行。 */
  25. export type ZCodeAutomationTrigger = "schedule" | "manual";
  26. /** 自定义重复规则;cronExpr 保留为兼容展示,调度以本字段为权威。 */
  27. export interface ZCodeAutomationScheduleRule {
  28. unit: "minute" | "hourly" | "daily" | "weekly" | "monthly" | "yearly";
  29. interval: number;
  30. hour: number;
  31. minute: number;
  32. anchorAt: number;
  33. weekdays?: number[];
  34. monthDays?: number[];
  35. /** yearly 用:1-12 人类月份。缺省回退 anchorAt 的月份(兼容未写该字段的旧记录)。 */
  36. months?: number[];
  37. monthlyMode?: "date" | "weekday";
  38. }
  39. /**
  40. * 会话侧长间隔周期 carrier 的 unit 枚举。与 scheduleRule.unit 同集,但 carrier 是受控入参
  41. * (模型不能直接写完整 scheduleRule),由 service 层归一化为权威 scheduleRule。
  42. */
  43. export type ZCodeAutomationIntervalUnit = ZCodeAutomationScheduleRule["unit"];
  44. /** 单次 run 的派发结果(scheduler 权威,驱动重试)。skipped=错过触发窗口。 */
  45. export type ZCodeAutomationRunDispatchStatus =
  46. | "claimed"
  47. | "dispatched"
  48. | "failed_to_dispatch"
  49. | "skipped";
  50. /** 单次 run 产出 session 后的运行结果(由 session runtime 回写,仅用于展示)。 */
  51. export type ZCodeAutomationRunOutcome = "running" | "succeeded" | "failed" | "stopped";
  52. /** 一条定时任务定义 + 调度状态。 */
  53. export interface ZCodeAutomation {
  54. automationId: string;
  55. title: string;
  56. /** 5 段 cron,本地时区。 */
  57. cronExpr: string;
  58. prompt: string;
  59. /** 缺失表示 Select 阶段继续跟随 Workspace 首选;存在时固定结构化模型意图。 */
  60. modelSelection?: ModelSelection;
  61. /** 权限模式;派发时透传给 createTask,缺省走 workspace 默认。 */
  62. mode?: ZCodeTaskMode;
  63. /** workspaceIdentity?.trim() || workspacePath */
  64. workspaceKey: string;
  65. workspacePath: string;
  66. workspaceIdentity?: string;
  67. /** 会话内创建的 cron 绑定到当前 task;后续触发都投递回该 task,不再新建 session。 */
  68. targetTaskId?: string;
  69. locationKind: ZCodeAutomationLocationKind;
  70. /** true=无限循环;false=有限次(配合 maxRuns)。 */
  71. recurring: boolean;
  72. maxRuns?: number;
  73. /** 自定义重复的截止时间(本地所选日期的日末,毫秒时间戳)。 */
  74. endAt?: number;
  75. scheduleRule?: ZCodeAutomationScheduleRule;
  76. /** 会话来源的只读调度是否已被用户在管理页显式删除并重设。 */
  77. scheduleEditedByUser?: boolean;
  78. runCount: number;
  79. enabled: boolean;
  80. lifecycleStatus: ZCodeAutomationLifecycleStatus;
  81. nextRunAt?: number;
  82. lastRunAt?: number;
  83. dispatchStatus: ZCodeAutomationDispatchStatus;
  84. dispatchAttempts: number;
  85. retryAt?: number;
  86. lastError?: string;
  87. createdAt: number;
  88. updatedAt: number;
  89. }
  90. /** parseAutomationRunId 的解析结果;scheduledAt 仅 schedule 触发存在。 */
  91. export interface ZCodeAutomationRunIdParts {
  92. automationId: string;
  93. trigger: ZCodeAutomationTrigger;
  94. /** runId 内嵌的本轮理论触发时间(Unix 毫秒)。 */
  95. scheduledAt?: number;
  96. }
  97. /**
  98. * runId 契约(见 ZCodeAutomationRun.runId)的唯一解析入口;desktop host/scheduler 与
  99. * CLI telemetry 不各自按字符串猜格式。不符合两种声明格式的输入一律返回 null。
  100. */
  101. export function parseAutomationRunId(runId: string): ZCodeAutomationRunIdParts | null {
  102. const separatorIndex = runId.indexOf(":");
  103. if (separatorIndex <= 0) return null;
  104. const automationId = runId.slice(0, separatorIndex);
  105. const rest = runId.slice(separatorIndex + 1);
  106. if (rest.startsWith("manual:")) {
  107. return rest.length > "manual:".length ? { automationId, trigger: "manual" } : null;
  108. }
  109. const scheduledAt = Number(rest);
  110. if (!Number.isSafeInteger(scheduledAt) || scheduledAt <= 0) return null;
  111. return { automationId, trigger: "schedule", scheduledAt };
  112. }
  113. /** 一次触发的运行历史 / runId 幂等台账。 */
  114. export interface ZCodeAutomationRun {
  115. /** automationId:scheduledAt / automationId:manual:uuid */
  116. runId: string;
  117. automationId: string;
  118. workspaceKey: string;
  119. scheduledAt?: number;
  120. trigger: ZCodeAutomationTrigger;
  121. /** Select 转 Submission 后固定;同一 run 的派发重试不得重新读取 Workspace 首选。 */
  122. modelSelection?: ModelSelection;
  123. dispatchStatus: ZCodeAutomationRunDispatchStatus;
  124. outcome?: ZCodeAutomationRunOutcome;
  125. /** dispatched 成功后回填,用于历史跳转。 */
  126. sessionId?: string;
  127. error?: string;
  128. attempts: number;
  129. createdAt: number;
  130. updatedAt: number;
  131. }
  132. /** 创建 automation 的入参(workspace 由调用方从 session 上下文注入)。 */
  133. export interface ZCodeAutomationCreateParams {
  134. title: string;
  135. cronExpr: string;
  136. /** 仅创建期使用:由服务层基于真实当前时间换算为 cronExpr,不写入数据库。 */
  137. relativeDelayMinutes?: number;
  138. prompt: string;
  139. modelSelection?: ModelSelection;
  140. mode?: ZCodeTaskMode;
  141. workspacePath: string;
  142. workspaceIdentity?: string;
  143. /** 当前会话内创建时由 runtime 注入,模型不可控。 */
  144. targetTaskId?: string;
  145. /** Bot 会话创建时由 Host 注入;仅供 scheduler 终态回推,不进入 UI 展示模型。 */
  146. botDeliveryTarget?: ZCodeAutomationBotDeliveryTarget;
  147. recurring: boolean;
  148. maxRuns?: number;
  149. endAt?: number;
  150. scheduleRule?: ZCodeAutomationScheduleRule;
  151. /**
  152. * 会话侧自定义重复 carrier。每 N 分钟/小时/天/周/月/年均配对提交 intervalUnit + interval,
  153. * 真实间隔由 service 层归一化为权威 scheduleRule 承载;cronExpr 仅作合法兼容展示。
  154. * 与 scheduleRule、relativeDelayMinutes 互斥。undefined=不使用 carrier。
  155. */
  156. intervalUnit?: ZCodeAutomationIntervalUnit;
  157. /** 1–200 的整数间隔,必须与 intervalUnit 配对。 */
  158. interval?: number;
  159. }
  160. /** 编辑 automation 的可变字段。 */
  161. export interface ZCodeAutomationUpdateParams {
  162. title?: string;
  163. cronExpr?: string;
  164. prompt?: string;
  165. /** undefined=不修改;null=清空,回退到 Workspace 首选。 */
  166. modelSelection?: ModelSelection | null;
  167. /** undefined=不修改;null=清空,回退到 workspace 默认权限模式。 */
  168. mode?: ZCodeTaskMode | null;
  169. recurring?: boolean;
  170. /** undefined=通常不修改;recurring=true 时领域层会自动清空旧上限;显式 null 必须同时传 recurring=true。 */
  171. maxRuns?: number | null;
  172. /** undefined=不修改;null=永不结束。 */
  173. endAt?: number | null;
  174. /** undefined=不修改;null=恢复为普通 cron。 */
  175. scheduleRule?: ZCodeAutomationScheduleRule | null;
  176. /**
  177. * 会话侧自定义重复 carrier(同 create 侧)。配对提交 intervalUnit + interval,service 层
  178. * 归一化为权威 scheduleRule(anchorAt 重置为本次修改时刻)。undefined=不修改。
  179. */
  180. intervalUnit?: ZCodeAutomationIntervalUnit;
  181. /** 1–200 的整数间隔,必须与 intervalUnit 配对。 */
  182. interval?: number;
  183. /** 仅由管理页在用户实际修改调度时写入;undefined=保留原来源状态。 */
  184. scheduleEditedByUser?: boolean;
  185. }