-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(sdk): AI SDK chat transport — run useChat as tasks #3065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ericallam
wants to merge
16
commits into
main
Choose a base branch
from
cursor/ai-sdk-chat-transport-5c3a
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
741c983
feat: add @trigger.dev/ai package with TriggerChatTransport
cursoragent 8354e2a
test: add comprehensive unit tests for TriggerChatTransport
cursoragent b6448fb
refactor: polish TriggerChatTransport implementation
cursoragent 68bd584
test: add abort signal, multiple sessions, and body merging tests
cursoragent 4a828fe
chore: add changeset for @trigger.dev/ai package
cursoragent 97811bb
refactor: remove internal ChatSessionState from public exports
cursoragent a17cad9
feat: support dynamic accessToken function for token refresh
cursoragent c3656a5
refactor: avoid double-resolving accessToken in sendMessages
cursoragent 0ca459d
feat: add chat transport and AI chat helpers to @trigger.dev/sdk
cursoragent 6dd87fd
test: move chat transport tests to @trigger.dev/sdk
cursoragent d9ef611
refactor: delete packages/ai/ — moved to @trigger.dev/sdk subpaths
cursoragent e4c30b0
chore: update changeset to target @trigger.dev/sdk
cursoragent eb2ccc0
fix: address CodeRabbit review feedback
cursoragent 3f8bc26
docs(ai): add AI Chat with useChat guide
cursoragent 2329a10
feat(reference): add ai-chat Next.js reference project
cursoragent 7badb14
fix(reference): use compatible @ai-sdk v3 packages, await convertToMo…
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| --- | ||
| "@trigger.dev/ai": minor | ||
| --- | ||
|
|
||
| New package: `@trigger.dev/ai` — AI SDK integration for Trigger.dev | ||
|
|
||
| Provides `TriggerChatTransport`, a custom `ChatTransport` implementation for the Vercel AI SDK that bridges `useChat` with Trigger.dev's durable task execution and realtime streams. | ||
|
|
||
| **Frontend usage:** | ||
| ```tsx | ||
| import { useChat } from "@ai-sdk/react"; | ||
| import { TriggerChatTransport } from "@trigger.dev/ai"; | ||
|
|
||
| const { messages, sendMessage } = useChat({ | ||
| transport: new TriggerChatTransport({ | ||
| accessToken: publicAccessToken, | ||
| taskId: "my-chat-task", | ||
| }), | ||
| }); | ||
| ``` | ||
|
|
||
| **Backend task:** | ||
| ```ts | ||
| import { task, streams } from "@trigger.dev/sdk"; | ||
| import { streamText, convertToModelMessages } from "ai"; | ||
| import type { ChatTaskPayload } from "@trigger.dev/ai"; | ||
|
|
||
| export const myChatTask = task({ | ||
| id: "my-chat-task", | ||
| run: async (payload: ChatTaskPayload) => { | ||
| const result = streamText({ | ||
| model: openai("gpt-4o"), | ||
| messages: convertToModelMessages(payload.messages), | ||
| }); | ||
| const { waitUntilComplete } = streams.pipe("chat", result.toUIMessageStream()); | ||
| await waitUntilComplete(); | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Also exports `createChatTransport()` factory function and `ChatTaskPayload` type for task-side typing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| { | ||
| "name": "@trigger.dev/ai", | ||
| "version": "4.3.3", | ||
| "description": "AI SDK integration for Trigger.dev - Custom ChatTransport for running AI chat as durable tasks", | ||
| "license": "MIT", | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/triggerdotdev/trigger.dev", | ||
| "directory": "packages/ai" | ||
| }, | ||
| "type": "module", | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "tshy": { | ||
| "selfLink": false, | ||
| "main": true, | ||
| "module": true, | ||
| "project": "./tsconfig.json", | ||
| "exports": { | ||
| "./package.json": "./package.json", | ||
| ".": "./src/index.ts" | ||
| }, | ||
| "sourceDialects": [ | ||
| "@triggerdotdev/source" | ||
| ] | ||
| }, | ||
| "scripts": { | ||
| "clean": "rimraf dist .tshy .tshy-build .turbo", | ||
| "build": "tshy && pnpm run update-version", | ||
| "dev": "tshy --watch", | ||
| "typecheck": "tsc --noEmit", | ||
| "test": "vitest", | ||
| "update-version": "tsx ../../scripts/updateVersion.ts", | ||
| "check-exports": "attw --pack ." | ||
| }, | ||
| "dependencies": { | ||
| "@trigger.dev/core": "workspace:4.3.3" | ||
| }, | ||
| "peerDependencies": { | ||
| "ai": "^5.0.0 || ^6.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@arethetypeswrong/cli": "^0.15.4", | ||
| "ai": "^6.0.0", | ||
| "rimraf": "^3.0.2", | ||
| "tshy": "^3.0.2", | ||
| "tsx": "4.17.0", | ||
| "vitest": "^2.1.0" | ||
| }, | ||
| "engines": { | ||
| "node": ">=18.20.0" | ||
| }, | ||
| "exports": { | ||
| "./package.json": "./package.json", | ||
| ".": { | ||
| "import": { | ||
| "@triggerdotdev/source": "./src/index.ts", | ||
| "types": "./dist/esm/index.d.ts", | ||
| "default": "./dist/esm/index.js" | ||
| }, | ||
| "require": { | ||
| "types": "./dist/commonjs/index.d.ts", | ||
| "default": "./dist/commonjs/index.js" | ||
| } | ||
| } | ||
| }, | ||
| "main": "./dist/commonjs/index.js", | ||
| "types": "./dist/commonjs/index.d.ts", | ||
| "module": "./dist/esm/index.js" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { TriggerChatTransport, createChatTransport } from "./transport.js"; | ||
| export type { TriggerChatTransportOptions, ChatTaskPayload } from "./types.js"; | ||
| export { VERSION } from "./version.js"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: triggerdotdev/trigger.dev
Length of output: 97
🏁 Script executed:
Repository: triggerdotdev/trigger.dev
Length of output: 96
🌐 Web query:
When was ChatTransport introduced in Vercel AI SDK release history💡 Result:
ChatTransportwas introduced with Vercel AI SDK v5.0, whenuseChatmoved to a transport-based architecture. The v5 release was announced on July 31, 2025. [1], [2]Sources: AI SDK 5 announcement (Jul 31, 2025) [1];
useChatv5 docs showing thetransport?: ChatTransportAPI and noting the v5 transport architecture [2].🌐 Web query:
ChatTransport ai npm package v5 vs v6 compatibility💡 Result:
ChatTransportis part of Vercel’s AI SDK (ainpm package). AI SDK v5 and v6 are different major versions and are not “drop-in compatible” at the type/API level—you should not mixai@5.xwithai@6.x(or@ai-sdk/react@2.xwith@ai-sdk/react@3.x) in the same app/library; keep the whole AI SDK stack on the same major. The official v5→v6 migration guide explicitly requires upgradingaito^6.0.0and the@ai-sdk/*packages to their v6-compatible majors. [1]What this means for
ChatTransportimplementationsChatTransportinterface (e.g.,sendMessagesuses a{ trigger: 'submit-message' | 'regenerate-message', chatId, messageId, messages, abortSignal, ...ChatRequestOptions }options object, andreconnectToStreamreturnsPromise<ReadableStream<UIMessageChunk> | null>). [2]useChat) exists in both v5 and v6, but treat the transport interface as versioned and pin it viapeerDependenciesif you publish a transport package. (Typical approach: release separate majors likeyour-transport@5forai@^5andyour-transport@6forai@^6.) [3][4]Recommended path
npx@ai-sdk/codemodv6). [1]peerDependenciesto eitherai@^5orai@^6(not both unless you actively maintain compatibility), and provide separate releases if needed. [2]Sources: v6 migration guide [1], v6
ChatTransportinterface [2], v5/v6 transport docs showing the transport system arounduseChat[3][4].🏁 Script executed:
# Read the transport.ts file to see the actual implementation cat -n packages/ai/src/transport.tsRepository: triggerdotdev/trigger.dev
Length of output: 10058
Narrow peer dependency to
"ai": "^6.0.0"only.ChatTransportexists in both AI SDK v5 and v6, but the interface is incompatible between versions. This implementation targets the v6ChatTransportAPI (e.g.,sendMessagessignature with{ trigger, chatId, messageId, messages, abortSignal }andreconnectToStreamreturningPromise<ReadableStream<UIMessageChunk> | null>). The current peer dependency"^5.0.0 || ^6.0.0"allows v5, which has a different interface and will cause runtime errors. Either narrow to"^6.0.0"or maintain separate major versions (e.g.,@trigger.dev/ai@5forai@^5,@trigger.dev/ai@6forai@^6).🤖 Prompt for AI Agents