offPeakDispatchPlan.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { OffPeakPermanentDispatchError } from "@zcode/services/node";
  2. /**
  3. * 闲时派发三分支:
  4. * - resume:conversation_id 已回填 = 已跑过,续跑同一 session 并发续跑提示词;
  5. * - bound-first-run:会话内创建绑定了 session_id 但尚未跑过,resume 绑定会话并发任务原 prompt
  6. * (对齐 dispatchCronRun 的 targetTaskId 路径);
  7. * - init:表单创建,新建专属 session。
  8. */
  9. export type OffPeakDispatchKind = "resume" | "bound-first-run" | "init";
  10. export function resolveOffPeakDispatchKind(request: {
  11. conversationId?: string;
  12. sessionId?: string;
  13. }): OffPeakDispatchKind {
  14. if (request.conversationId?.trim()) return "resume";
  15. if (request.sessionId?.trim()) return "bound-first-run";
  16. return "init";
  17. }
  18. /** 绑定会话已被用户删除:重试无意义,permanent(否则会一直退避到票过期再重取号)。 */
  19. export class OffPeakBoundSessionDeletedError extends OffPeakPermanentDispatchError {
  20. constructor(readonly sessionId: string) {
  21. super(`off-peak bound session was deleted: ${sessionId}`);
  22. this.name = "OffPeakBoundSessionDeletedError";
  23. }
  24. }
  25. /**
  26. * 绑定会话正在跑用户 turn:transient,交给调度器退避重试。
  27. * 必须在写入会话 mode 之前抛出——CLI 的 session/send 会以 -32010 拒绝,
  28. * 但 setMode 没有活跃 turn 检查,先写配置再撞忙会把用户会话悄悄切成任务的权限模式。
  29. */
  30. export class OffPeakBoundSessionBusyError extends Error {
  31. constructor(readonly sessionId: string) {
  32. super(`off-peak bound session is busy: ${sessionId}`);
  33. this.name = "OffPeakBoundSessionBusyError";
  34. }
  35. }
  36. export function assertBoundSessionDispatchable(params: {
  37. sessionId: string;
  38. deleted: boolean;
  39. running: boolean;
  40. }): void {
  41. if (params.deleted) throw new OffPeakBoundSessionDeletedError(params.sessionId);
  42. if (params.running) throw new OffPeakBoundSessionBusyError(params.sessionId);
  43. }