| @trigger.dev/sdk | minor |
|---|---|
| @trigger.dev/core | patch |
AI Agents — run AI SDK chat completions as durable Trigger.dev agents instead of fragile API routes. Define an agent in one function, point useChat at it from React, and the conversation survives page refreshes, network blips, and process restarts.
import { chat } from "@trigger.dev/sdk/ai";
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
export const myChat = chat.agent({
id: "my-chat",
run: async ({ messages, signal }) =>
streamText({ model: openai("gpt-4o"), messages, abortSignal: signal }),
});import { useChat } from "@ai-sdk/react";
import { useTriggerChatTransport } from "@trigger.dev/sdk/chat/react";
const transport = useTriggerChatTransport({ task: "my-chat", accessToken, startSession });
const { messages, sendMessage } = useChat({ transport });What you get:
- AI SDK
useChatintegration — a customChatTransport(useTriggerChatTransport) plugs straight into Vercel AI SDK'suseChathook. Text streaming, tool calls, reasoning, anddata-*parts all work natively over Trigger.dev's realtime streams. No custom API routes needed. - First-turn fast path (
chat.headStart) — opt-in handler that runs the first turn'sstreamTextstep in your warm server process while the agent run boots in parallel, cutting cold-start TTFC by roughly half (measured 2801ms → 1218ms onclaude-sonnet-4-6). The agent owns step 2+ (tool execution, persistence, hooks) so heavy deps stay where they belong. Web Fetch handler works natively in Next.js, Hono, SvelteKit, Remix, Workers, etc.; bridge to Express/Fastify/Koa viachat.toNodeListener. New@trigger.dev/sdk/chat-serversubpath. - Multi-turn durability via Sessions — every chat is backed by a durable Session that outlives any individual run. Conversations resume across page refreshes, idle timeout, crashes, and deploys;
resume: truereconnects vialastEventIdso clients only see new chunks.sessions.listenumerates chats for inbox-style UIs. - Auto-accumulated history, delta-only wire — the backend accumulates the full conversation across turns; clients only ship the new message each turn. Long chats never hit the 512 KiB body cap. Register
hydrateMessagesto be the source of truth yourself. - Lifecycle hooks —
onPreload,onChatStart,onValidateMessages,hydrateMessages,onTurnStart,onBeforeTurnComplete,onTurnComplete,onChatSuspend,onChatResume— for persistence, validation, and post-turn work. - Stop generation — client-driven
transport.stopGeneration(chatId)aborts mid-stream; the run stays alive for the next message, partial response is captured, and aborted parts (stuckpartial-calltools, in-progress reasoning) are auto-cleaned. - Tool approvals (HITL) — tools with
needsApproval: truepause until the user approves or denies viaaddToolApprovalResponse. The runtime reconciles the updated assistant message by ID and continuesstreamText. - Steering and background injection —
pendingMessagesinjects user messages between tool-call steps so users can steer the agent mid-execution;chat.inject()+chat.defer()adds context from background work (self-review, RAG, safety checks) between turns. - Actions — non-turn frontend commands (undo, rollback, regenerate, edit) sent via
transport.sendAction. FirehydrateMessages+onActiononly — no turn hooks, norun().onActioncan return aStreamTextResultfor a model response, orvoidfor side-effect-only. - Typed state primitives —
chat.local<T>for per-run state accessible from hooks,run(), tools, and subtasks (auto-serialized throughai.toolExecute);chat.storefor typed shared data between agent and client;chat.historyfor reading and mutating the message chain;clientDataSchemafor typedclientDatain every hook. chat.toStreamTextOptions()— one spread intostreamTextwires up versioned system Prompts, model resolution, telemetry metadata, compaction, steering, and background injection.- Multi-tab coordination —
multiTab: true+useMultiTabChatprevents duplicate sends and syncs state across browser tabs viaBroadcastChannel. Non-active tabs go read-only with live updates. - Network resilience — built-in indefinite retry with bounded backoff, reconnect on
online/ tab refocus / bfcache restore,Last-Event-IDmid-stream resume. No app code needed.
See /docs/ai-chat for the full surface — quick start, three backend approaches (chat.agent, chat.createSession, raw task), persistence and code-sandbox patterns, type-level guides, and API reference.