Skip to content

Commit 03ba2ba

Browse files
committed
provide already converted UIMessages to the run function for better dx
1 parent 4cdc911 commit 03ba2ba

File tree

2 files changed

+24
-16
lines changed
  • packages/trigger-sdk/src/v3
  • references/ai-chat/src/trigger

2 files changed

+24
-16
lines changed

packages/trigger-sdk/src/v3/ai.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
type TaskSchema,
1010
type TaskWithSchema,
1111
} from "@trigger.dev/core/v3";
12-
import type { UIMessage } from "ai";
13-
import { dynamicTool, jsonSchema, JSONSchema7, Schema, Tool, ToolCallOptions, zodSchema } from "ai";
12+
import type { ModelMessage, UIMessage } from "ai";
13+
import { convertToModelMessages, dynamicTool, jsonSchema, JSONSchema7, Schema, Tool, ToolCallOptions, zodSchema } from "ai";
1414
import { auth } from "./auth.js";
1515
import { metadata } from "./metadata.js";
1616
import { streams } from "./streams.js";
@@ -182,12 +182,17 @@ type ChatTaskWirePayload<TMessage extends UIMessage = UIMessage> = {
182182
/**
183183
* The payload shape passed to the `chatTask` run function.
184184
*
185-
* The `metadata` field from the AI SDK transport is exposed as `clientData`
186-
* to avoid confusion with Trigger.dev's run metadata.
185+
* - `messages` contains model-ready messages (converted via `convertToModelMessages`) —
186+
* pass these directly to `streamText`.
187+
* - `uiMessages` contains the raw `UIMessage[]` from the frontend.
188+
* - `clientData` contains custom data from the frontend (the `metadata` field from `sendMessage()`).
187189
*/
188-
export type ChatTaskPayload<TMessage extends UIMessage = UIMessage> = {
189-
/** The conversation messages */
190-
messages: TMessage[];
190+
export type ChatTaskPayload = {
191+
/** Model-ready messages — pass directly to `streamText({ messages })`. */
192+
messages: ModelMessage[];
193+
194+
/** Raw UI messages from the frontend. */
195+
uiMessages: UIMessage[];
191196

192197
/** The unique identifier for the chat session */
193198
chatId: string;
@@ -324,7 +329,7 @@ function isReadableStream(value: unknown): value is ReadableStream<unknown> {
324329
* run: async (payload: ChatTaskPayload) => {
325330
* const result = streamText({
326331
* model: openai("gpt-4o"),
327-
* messages: convertToModelMessages(payload.messages),
332+
* messages: payload.messages,
328333
* });
329334
*
330335
* await chat.pipe(result);
@@ -388,7 +393,7 @@ async function pipeChat(
388393
* transport resumes the same run by sending the next message via input streams.
389394
*/
390395
export type ChatTaskOptions<TIdentifier extends string> = Omit<
391-
TaskOptions<TIdentifier, ChatTaskPayload, unknown>,
396+
TaskOptions<TIdentifier, ChatTaskWirePayload, unknown>,
392397
"run"
393398
> & {
394399
/**
@@ -452,8 +457,8 @@ export type ChatTaskOptions<TIdentifier extends string> = Omit<
452457
* run: async ({ messages, signal }) => {
453458
* return streamText({
454459
* model: openai("gpt-4o"),
455-
* messages: convertToModelMessages(messages),
456-
* abortSignal: signal, // fires on stop or run cancel
460+
* messages, // already converted via convertToModelMessages
461+
* abortSignal: signal,
457462
* });
458463
* },
459464
* });
@@ -503,14 +508,17 @@ function chatTask<TIdentifier extends string>(
503508
pendingMessages.push(msg);
504509
});
505510

506-
// Remap wire payload to user-facing payload (metadata -> clientData)
507-
const { metadata: wireMetadata, ...restWire } = currentWirePayload;
511+
// Convert wire payload to user-facing payload
512+
const { metadata: wireMetadata, messages: uiMessages, ...restWire } = currentWirePayload;
513+
const sanitized = sanitizeMessages(uiMessages);
514+
const modelMessages = await convertToModelMessages(sanitized);
508515

509516
try {
510517
const result = await userRun({
511518
...restWire,
519+
messages: modelMessages,
520+
uiMessages: sanitized,
512521
clientData: wireMetadata,
513-
messages: sanitizeMessages(currentWirePayload.messages),
514522
signal: combinedSignal,
515523
cancelSignal,
516524
stopSignal,

references/ai-chat/src/trigger/chat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { chat } from "@trigger.dev/sdk/ai";
2-
import { streamText, convertToModelMessages, tool, stepCountIs } from "ai";
2+
import { streamText, tool, stepCountIs } from "ai";
33
import type { LanguageModel } from "ai";
44
import { openai } from "@ai-sdk/openai";
55
import { anthropic } from "@ai-sdk/anthropic";
@@ -91,7 +91,7 @@ export const aiChat = chat.task({
9191
return streamText({
9292
model: getModel(modelId),
9393
system: "You are a helpful assistant. Be concise and friendly.",
94-
messages: await convertToModelMessages(messages),
94+
messages,
9595
tools: { inspectEnvironment },
9696
stopWhen: stepCountIs(10),
9797
abortSignal: stopSignal,

0 commit comments

Comments
 (0)