Skip to content

Commit afd9df5

Browse files
Cover non-Error onTriggeredRun failures in onError reporting
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent b76c8b5 commit afd9df5

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,76 @@ describe("TriggerChatTransport", function () {
11421142
expect(errors[0]?.error.message).toBe("callback failed");
11431143
});
11441144

1145+
it("normalizes non-Error onTriggeredRun failures before reporting onError", async function () {
1146+
const errors: TriggerChatTransportError[] = [];
1147+
1148+
const server = await startServer(function (req, res) {
1149+
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
1150+
res.writeHead(200, {
1151+
"content-type": "application/json",
1152+
"x-trigger-jwt": "pk_run_callback_string",
1153+
});
1154+
res.end(JSON.stringify({ id: "run_callback_string" }));
1155+
return;
1156+
}
1157+
1158+
if (
1159+
req.method === "GET" &&
1160+
req.url === "/realtime/v1/streams/run_callback_string/chat-stream"
1161+
) {
1162+
res.writeHead(200, {
1163+
"content-type": "text/event-stream",
1164+
});
1165+
writeSSE(
1166+
res,
1167+
"1-0",
1168+
JSON.stringify({ type: "text-start", id: "callback_string_1" })
1169+
);
1170+
writeSSE(
1171+
res,
1172+
"2-0",
1173+
JSON.stringify({ type: "text-end", id: "callback_string_1" })
1174+
);
1175+
res.end();
1176+
return;
1177+
}
1178+
1179+
res.writeHead(404);
1180+
res.end();
1181+
});
1182+
1183+
const transport = new TriggerChatTransport({
1184+
task: "chat-task",
1185+
stream: "chat-stream",
1186+
accessToken: "pk_trigger",
1187+
baseURL: server.url,
1188+
onTriggeredRun: async function onTriggeredRun() {
1189+
throw "callback string failure";
1190+
},
1191+
onError: function onError(error) {
1192+
errors.push(error);
1193+
},
1194+
});
1195+
1196+
const stream = await transport.sendMessages({
1197+
trigger: "submit-message",
1198+
chatId: "chat-callback-string",
1199+
messageId: undefined,
1200+
messages: [],
1201+
abortSignal: undefined,
1202+
});
1203+
1204+
const chunks = await readChunks(stream);
1205+
expect(chunks).toHaveLength(2);
1206+
expect(errors).toHaveLength(1);
1207+
expect(errors[0]).toMatchObject({
1208+
phase: "onTriggeredRun",
1209+
chatId: "chat-callback-string",
1210+
runId: "run_callback_string",
1211+
});
1212+
expect(errors[0]?.error.message).toBe("callback string failure");
1213+
});
1214+
11451215
it("ignores failures from onError callback", async function () {
11461216
const server = await startServer(function (req, res) {
11471217
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {

0 commit comments

Comments
 (0)