Skip to content

Commit 89d085a

Browse files
authored
fix(references): repair ai-chat typecheck against current wire shape (#3685)
## Summary Pre-existing typecheck errors in `references/ai-chat` against the current SDK shape. Unblocks `pnpm exec tsc --noEmit` in the reference project. ## What changed Three categories of fixes inside `references/ai-chat`. No SDK changes. ### 1. `payload.messages` → `payload.message` The wire payload is now delta-only — one new message per trigger, optional. Old code in two raw-task files reads `payload.messages` (plural array) which no longer exists. ```ts // before const messages = await conversation.addIncoming(currentPayload.messages, ...); // after const messages = await conversation.addIncoming( currentPayload.message ? [currentPayload.message] : [], ... ); ``` Same fix to the `chat.messages.on` handler, reading `msg.message` (singular) instead of `msg.messages[length - 1]`. ### 2. `clientData` non-null assertion in `cf-trust-test` `ChatTurnContext.clientData` is typed as `?: TClientData` on `onTurnStart` / `run` event objects even when the agent declares a `clientDataSchema`. The runtime validates against the schema before the hook fires, so it's structurally non-null — but TypeScript can't know that. Non-null assert for now. Follow-up worth filing: narrow `ChatTurnContext.clientData` to non-optional when the agent has a `clientDataSchema`. Same friction the docs friction-test subagent flagged. ### 3. `stress-emit.parseConfig` retyped against `ModelMessage[]` The `run` callback hands `messages: ModelMessage[]`, not `UIMessage[]`. Update `parseConfig` to accept `ModelMessage[]` and pull text from `content` (string or array-of-parts). ## Test plan - [x] `pnpm exec tsc --noEmit` in `references/ai-chat` passes (was 8 errors, now 0)
1 parent 9ff410b commit 89d085a

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export const orchestratorAgent = chat
272272
stop.reset();
273273

274274
const messages = await conversation.addIncoming(
275-
currentPayload.messages,
275+
currentPayload.message ? [currentPayload.message] : [],
276276
currentPayload.trigger,
277277
turn
278278
);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ export const aiChatRaw = chat.customAgent({
659659
stop.reset();
660660

661661
const messages = await conversation.addIncoming(
662-
currentPayload.messages,
662+
currentPayload.message ? [currentPayload.message] : [],
663663
currentPayload.trigger,
664664
turn
665665
);
@@ -678,8 +678,7 @@ export const aiChatRaw = chat.customAgent({
678678
const combinedSignal = AbortSignal.any([runSignal, stop.signal]);
679679

680680
const steeringSub = chat.messages.on(async (msg) => {
681-
const lastMsg = msg.messages?.[msg.messages.length - 1];
682-
if (lastMsg) await conversation.steerAsync(lastMsg);
681+
if (msg.message) await conversation.steerAsync(msg.message);
683682
});
684683

685684
const result = streamText({
@@ -1049,10 +1048,10 @@ export const cfTrustTestAgent = chat
10491048
id: "cf-trust-test",
10501049
idleTimeoutInSeconds: 60,
10511050
onTurnStart: async ({ turn, clientData }) => {
1052-
logger.info("cf-trust-test turn", { turn, cf: clientData.__cf, userId: clientData.userId });
1051+
logger.info("cf-trust-test turn", { turn, cf: clientData!.__cf, userId: clientData!.userId });
10531052
},
10541053
run: async ({ messages, clientData, signal }) => {
1055-
const cf = clientData.__cf;
1054+
const cf = clientData!.__cf;
10561055
return streamText({
10571056
model: openai("gpt-4o-mini"),
10581057
system:

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Defaults: 1000 chunks × 10 chars, single message.
1111

1212
import { chat } from "@trigger.dev/sdk/ai";
13-
import { type UIMessage, simulateReadableStream, streamText } from "ai";
13+
import { type ModelMessage, simulateReadableStream, streamText } from "ai";
1414
import { MockLanguageModelV3 } from "ai/test";
1515
import type { LanguageModelV3StreamPart } from "@ai-sdk/provider";
1616

@@ -20,10 +20,16 @@ type StressConfig = {
2020
manyMessages: boolean;
2121
};
2222

23-
function parseConfig(messages: UIMessage[]): StressConfig {
23+
function parseConfig(messages: ModelMessage[]): StressConfig {
2424
const lastUser = [...messages].reverse().find((m) => m.role === "user");
25-
const text =
26-
lastUser?.parts?.[0]?.type === "text" ? lastUser.parts[0].text.trim() : "";
25+
const content = lastUser?.content;
26+
let text = "";
27+
if (typeof content === "string") {
28+
text = content.trim();
29+
} else if (Array.isArray(content)) {
30+
const textPart = content.find((p) => p.type === "text");
31+
text = textPart && "text" in textPart ? textPart.text.trim() : "";
32+
}
2733
const parts = text.split(/\s+/);
2834
const chunkCount = Number(parts[0]);
2935
const chunkSize = Number(parts[1]);

0 commit comments

Comments
 (0)