为什么需要
独立开发者做海外产品,后台任务是刚需:发邮件、调第三方 API、跑 AI Agent、处理 webhook 回调。这些任务可能跑几秒到几十分钟,传统方案要自己搭 Redis + BullMQ + Worker,运维成本高。Trigger.dev 把这些都托管了——写 TypeScript 代码,部署到云端,自动扩缩容,失败自动重试。
怎么用
- 安装:
npm install @trigger.dev/sdk - 定义任务:
typescript
import { task } from "@trigger.dev/sdk/v3";
export const processOrder = task({
id: "process-order",
maxDuration: 300,
run: async (payload) => {
await chargePayment(payload.orderId);
await sendConfirmationEmail(payload.email);
return { success: true };
},
});
- 触发任务:
await processOrder.trigger({ orderId: "123", email: "user@example.com" }) - 部署:
npx trigger deploy - 在 Dashboard 查看运行状态、日志、错误
AI Agent 场景
Trigger.dev 对 AI Agent 支持很好:
- 工具调用:在任务里调用 LLM,处理工具调用结果,自动重试
- 流式输出:通过 Realtime API 把 LLM 流式响应推送到前端
- 长时间运行:Agent 可以跑几十分钟,不受 serverless 超时限制
- Human-in-the-loop:任务可以暂停等待人工审批
注意事项
- 开源(Apache 2.0),可自托管
- Cloud 版有免费额度,按用量付费
- 只支持 TypeScript/JavaScript
- 需要 Node.js 运行时