TensorFusion Docs

定时任务

配置智能体定时执行 — 支持 cron 表达式、固定间隔和定点执行。

定时任务

定时任务允许你配置智能体按计划自动执行,支持一次性定点执行、固定间隔重复和标准 cron 表达式三种调度方式。执行结果可以通过飞书消息或 Webhook 投递到指定渠道。

调度方式

定点执行(at)

在指定时间执行一次:

{
  "kind": "at",
  "at": "2026-04-15T09:00:00+08:00"
}

at 使用 ISO 8601 格式,支持时区偏移。配合 deleteAfterRun: true 可实现一次性任务。

固定间隔(every)

按固定时间间隔重复执行:

{
  "kind": "every",
  "everyMs": 3600000
}

everyMs 为毫秒数,最小值 60000(1 分钟)。

常用值:

间隔毫秒值
1 分钟60000
5 分钟300000
1 小时3600000
1 天86400000

Cron 表达式(cron)

使用标准 cron 表达式,支持时区:

{
  "kind": "cron",
  "expr": "0 9 * * 1-5",
  "tz": "Asia/Shanghai"
}

常用表达式:

表达式说明
0 9 * * *每天 9:00
0 9 * * 1-5工作日 9:00
*/30 * * * *每 30 分钟
0 0 1 * *每月 1 号 0:00
0 8,18 * * *每天 8:00 和 18:00

tz 为可选时区标识(IANA 格式),不传时使用服务器默认时区。

执行流水线(Pipeline)

推荐使用 Pipeline 模式配置任务的执行逻辑,包含三个阶段:

Action(执行动作)

File Collection(文件收集,可选)

Deliveries(结果投递)

Action 类型

智能体调用(agent_invoke):

向绑定的智能体发送 Prompt 并获取回复:

{
  "type": "agent_invoke",
  "prompt": "请生成本周的销售周报"
}

需要指定 agentId

Webhook 调用(webhook):

直接调用外部 HTTP 接口:

{
  "type": "webhook",
  "url": "https://api.example.com/report",
  "method": "POST",
  "headers": { "Authorization": "Bearer xxx" },
  "body": { "type": "weekly" }
}

静态文本(static):

直接输出固定文本:

{
  "type": "static",
  "text": "这是一条定时提醒"
}

文件收集

当 Action 在沙箱中生成了文件时,可以通过 glob 模式收集文件并附带在投递中:

{
  "pattern": "*.pdf"
}

投递目标

支持将执行结果投递到以下渠道:

飞书消息(lark):

{
  "type": "lark",
  "appId": "cli_xxx",
  "appSecret": "xxx",
  "chatId": "oc_xxx",
  "domain": "https://open.feishu.cn",
  "sendText": true,
  "sendFiles": true
}

sendText 控制是否发送文本回复,sendFiles 控制是否发送收集到的文件。

Webhook 投递(webhook):

{
  "type": "webhook",
  "url": "https://api.example.com/callback",
  "method": "POST",
  "headers": { "X-Source": "tensor-agent" }
}

可以配置多个投递目标,结果会并行发送到所有目标。

完整示例

工作日早晨生成销售周报并发送到飞书

{
  "name": "每日销售周报",
  "description": "工作日 9:00 自动生成销售数据分析",
  "agentId": "agent-sales-analyst",
  "schedule": {
    "kind": "cron",
    "expr": "0 9 * * 1-5",
    "tz": "Asia/Shanghai"
  },
  "pipeline": {
    "action": {
      "type": "agent_invoke",
      "prompt": "请根据昨日的销售数据生成分析报告,包含总成交额、同比环比变化和 Top 5 产品"
    },
    "fileCollection": {
      "pattern": "*.pdf"
    },
    "deliveries": [
      {
        "type": "lark",
        "appId": "cli_xxx",
        "appSecret": "xxx",
        "chatId": "oc_sales_team",
        "sendText": true,
        "sendFiles": true
      }
    ]
  },
  "enabled": true
}

每小时检查系统健康

{
  "name": "系统健康检查",
  "schedule": {
    "kind": "every",
    "everyMs": 3600000
  },
  "pipeline": {
    "action": {
      "type": "webhook",
      "url": "https://monitoring.example.com/health",
      "method": "GET"
    },
    "deliveries": [
      {
        "type": "webhook",
        "url": "https://hooks.example.com/alert"
      }
    ]
  },
  "enabled": true
}

REST API

所有接口需认证,创建/更新/删除操作需 owneradmin 角色。

列出定时任务

GET /api/cron-jobs

获取单个任务

GET /api/cron-jobs/:id

创建定时任务

POST /api/cron-jobs

请求体:

字段类型必填说明
namestring任务名称
descriptionstring任务描述
agentIdstring条件必填使用 agent_invoke 时必填
scheduleCronSchedule调度配置
pipelineCronPipeline条件必填Pipeline 配置(推荐)
enabledboolean是否启用,默认 true
deleteAfterRunboolean执行后自动删除,默认 false

更新定时任务

PATCH /api/cron-jobs/:id

所有字段可选。更新 schedule 时会自动重新计算 nextRunAt

删除定时任务

DELETE /api/cron-jobs/:id

立即执行

POST /api/cron-jobs/:id/run

将任务的 nextRunAt 设为当前时间,调度器会尽快拾取并执行。

返回 { "ok": true, "message": "Job queued for immediate execution" }

执行历史

GET /api/cron-jobs/:id/runs?limit=50

返回该任务的历史执行记录,默认最近 50 条。

数据模型

interface CronJob {
  id: string;
  orgId: string;
  name: string;
  description?: string;
  agentId?: string;
  schedule: CronSchedule;
  pipeline?: CronPipelineConfig;
  enabled: boolean;
  deleteAfterRun: boolean;
  lastRunAt?: Date;
  nextRunAt?: Date;
  runCount: number;
  createdAt: Date;
  updatedAt: Date;
}

目录