定义于 src/cron/types.ts 和 src/cron/types-shared.ts。

CronSchedule — 三种调度模式

type CronSchedule =
  | { kind: "at"; at: string }
  | { kind: "every"; everyMs: number; anchorMs?: number }
  | { kind: "cron"; expr: string; tz?: string; staggerMs?: number }
模式说明示例
at一次性执行,ISO 时间戳{ kind: "at", at: "2026-05-22T09:00:00Z" }
every固定间隔,支持锚点对齐{ kind: "every", everyMs: 3600000 }
cronCron 表达式,支持时区和错开{ kind: "cron", expr: "0 9 * * *", tz: "Asia/Shanghai" }

CronJob — 任务定义

type CronJob = {
  id: string
  agentId?: string
  sessionKey?: string
  name: string
  description?: string
  enabled: boolean
  deleteAfterRun?: boolean          // 执行后自动删除
  createdAtMs: number
  updatedAtMs: number
  schedule: CronSchedule
  sessionTarget: CronSessionTarget
  wakeMode: "next-heartbeat" | "now"
  payload: CronPayload
  delivery?: CronDelivery
  failureAlert?: CronFailureAlert | false
  state: CronJobState
}

sessionTarget 取值

值说明
"main"主会话,发送 systemEvent
"isolated"隔离会话,发送 agentTurn
"current"当前会话(运行时解析)
"session:xxx"指定会话 ID

wakeMode 取值

值说明
"now"立即执行
"next-heartbeat"等待下一个心跳周期

CronPayload — 任务负载

type CronPayload =
  | { kind: "systemEvent"; text: string }
  | {
      kind: "agentTurn"
      message: string
      model?: string
      fallbacks?: string[]
      toolsAllow?: string[]
      lightContext?: boolean
      thinking?: boolean
      externalContentSource?: string
    }
  • systemEvent:向主会话注入系统事件文本
  • agentTurn:在隔离会话中运行 Agent,支持模型覆盖、工具过滤、轻量上下文

CronDelivery — 交付配置

type CronDelivery = {
  mode: "none" | "announce" | "webhook"
  channel?: CronMessageChannel
  to?: string
  threadId?: string | number
  accountId?: string
  bestEffort?: boolean
  failureDestination?: CronFailureDestination
}
mode说明
none不交付
announce通过消息渠道发送结果
webhookHTTP POST 到指定 URL

CronJobState — 运行时状态

type CronJobState = {
  nextRunAtMs?: number
  runningAtMs?: number
  lastRunAtMs?: number
  lastRunStatus?: "ok" | "error" | "skipped"
  lastError?: string
  lastDiagnostics?: CronRunDiagnostics
  consecutiveErrors?: number
  consecutiveSkipped?: number
  lastFailureAlertAtMs?: number
  scheduleErrorCount?: number
  lastDeliveryStatus?: "delivered" | "not-delivered" | "unknown" | "not-requested"
  lastDelivered?: boolean
}

CronFailureAlert — 失败告警

type CronFailureAlert = {
  after?: number                    // 连续失败次数阈值(默认 2)
  cooldownMs?: number               // 告警冷却时间(默认 1h)
  includeSkipped?: boolean          // 是否计入跳过的运行
  destination?: CronFailureDestination
}

存储格式

jobs.json(任务配置)

{
  "version": 1,
  "jobs": [
    {
      "id": "job-123",
      "name": "Daily Report",
      "enabled": true,
      "schedule": { "kind": "cron", "expr": "0 9 * * *", "tz": "Asia/Shanghai" },
      "sessionTarget": "isolated",
      "wakeMode": "now",
      "payload": { "kind": "agentTurn", "message": "Generate daily report" },
      "delivery": { "mode": "announce", "channel": "slack", "to": "#reports" },
      "state": {}
    }
  ]
}

jobs-state.json(运行时状态,分离存储)

{
  "version": 1,
  "jobs": {
    "job-123": {
      "updatedAtMs": 1234567890000,
      "scheduleIdentity": "hash-of-schedule",
      "state": {
        "nextRunAtMs": 1234654290000,
        "lastRunAtMs": 1234567890000,
        "lastRunStatus": "ok",
        "consecutiveErrors": 0,
        "lastDeliveryStatus": "delivered"
      }
    }
  }
}

配置与状态分离存储,减少写入冲突,支持原子写入。

相关页面