@trigger.dev/sdk 0.0.0-prerelease-20260302145933 → 0.0.0-prerelease-20260305142821
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/v3/ai.d.ts +347 -2
- package/dist/commonjs/v3/ai.js +563 -1
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat-constants.d.ts +10 -0
- package/dist/commonjs/v3/chat-constants.js +14 -0
- package/dist/commonjs/v3/chat-constants.js.map +1 -0
- package/dist/commonjs/v3/chat-react.d.ts +45 -0
- package/dist/commonjs/v3/chat-react.js +71 -0
- package/dist/commonjs/v3/chat-react.js.map +1 -0
- package/dist/commonjs/v3/chat.d.ts +241 -0
- package/dist/commonjs/v3/chat.js +343 -0
- package/dist/commonjs/v3/chat.js.map +1 -0
- package/dist/commonjs/v3/chat.test.d.ts +1 -0
- package/dist/commonjs/v3/chat.test.js +1557 -0
- package/dist/commonjs/v3/chat.test.js.map +1 -0
- package/dist/commonjs/v3/runs.d.ts +3 -3
- package/dist/commonjs/v3/streams.js +27 -17
- package/dist/commonjs/v3/streams.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +347 -2
- package/dist/esm/v3/ai.js +564 -2
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat-constants.d.ts +10 -0
- package/dist/esm/v3/chat-constants.js +11 -0
- package/dist/esm/v3/chat-constants.js.map +1 -0
- package/dist/esm/v3/chat-react.d.ts +45 -0
- package/dist/esm/v3/chat-react.js +68 -0
- package/dist/esm/v3/chat-react.js.map +1 -0
- package/dist/esm/v3/chat.d.ts +241 -0
- package/dist/esm/v3/chat.js +338 -0
- package/dist/esm/v3/chat.js.map +1 -0
- package/dist/esm/v3/chat.test.d.ts +1 -0
- package/dist/esm/v3/chat.test.js +1555 -0
- package/dist/esm/v3/chat.test.js.map +1 -0
- package/dist/esm/v3/streams.js +27 -17
- package/dist/esm/v3/streams.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +40 -5
package/dist/commonjs/v3/ai.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ai = void 0;
|
|
3
|
+
exports.chat = exports.CHAT_STOP_STREAM_ID = exports.CHAT_MESSAGES_STREAM_ID = exports.CHAT_STREAM_KEY = exports.ai = void 0;
|
|
4
4
|
const v3_1 = require("@trigger.dev/core/v3");
|
|
5
5
|
const ai_1 = require("ai");
|
|
6
|
+
const api_1 = require("@opentelemetry/api");
|
|
7
|
+
const auth_js_1 = require("./auth.js");
|
|
6
8
|
const metadata_js_1 = require("./metadata.js");
|
|
9
|
+
const streams_js_1 = require("./streams.js");
|
|
10
|
+
const shared_js_1 = require("./shared.js");
|
|
11
|
+
const tracer_js_1 = require("./tracer.js");
|
|
12
|
+
const chat_constants_js_1 = require("./chat-constants.js");
|
|
13
|
+
Object.defineProperty(exports, "CHAT_MESSAGES_STREAM_ID", { enumerable: true, get: function () { return chat_constants_js_1.CHAT_MESSAGES_STREAM_ID; } });
|
|
14
|
+
Object.defineProperty(exports, "CHAT_STOP_STREAM_ID", { enumerable: true, get: function () { return chat_constants_js_1.CHAT_STOP_STREAM_ID; } });
|
|
7
15
|
const METADATA_KEY = "tool.execute.options";
|
|
8
16
|
function toolFromTask(task, options) {
|
|
9
17
|
if (("schema" in task && !task.schema) || ("jsonSchema" in task && !task.jsonSchema)) {
|
|
@@ -53,4 +61,558 @@ exports.ai = {
|
|
|
53
61
|
tool: toolFromTask,
|
|
54
62
|
currentToolOptions: getToolOptionsFromMetadata,
|
|
55
63
|
};
|
|
64
|
+
/**
|
|
65
|
+
* Creates a public access token for a chat task.
|
|
66
|
+
*
|
|
67
|
+
* This is a convenience helper that creates a multi-use trigger public token
|
|
68
|
+
* scoped to the given task. Use it in a server action to provide the frontend
|
|
69
|
+
* `TriggerChatTransport` with an `accessToken`.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* // actions.ts
|
|
74
|
+
* "use server";
|
|
75
|
+
* import { chat } from "@trigger.dev/sdk/ai";
|
|
76
|
+
* import type { myChat } from "@/trigger/chat";
|
|
77
|
+
*
|
|
78
|
+
* export const getChatToken = () => chat.createAccessToken<typeof myChat>("my-chat");
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
function createChatAccessToken(taskId) {
|
|
82
|
+
return auth_js_1.auth.createTriggerPublicToken(taskId, { multipleUse: true });
|
|
83
|
+
}
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
// Chat transport helpers — backend side
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
/**
|
|
88
|
+
* The default stream key used for chat transport communication.
|
|
89
|
+
* Both `TriggerChatTransport` (frontend) and `pipeChat`/`chatTask` (backend)
|
|
90
|
+
* use this key by default.
|
|
91
|
+
*/
|
|
92
|
+
exports.CHAT_STREAM_KEY = chat_constants_js_1.CHAT_STREAM_KEY;
|
|
93
|
+
// Input streams for bidirectional chat communication
|
|
94
|
+
const messagesInput = streams_js_1.streams.input({ id: chat_constants_js_1.CHAT_MESSAGES_STREAM_ID });
|
|
95
|
+
const stopInput = streams_js_1.streams.input({ id: chat_constants_js_1.CHAT_STOP_STREAM_ID });
|
|
96
|
+
/**
|
|
97
|
+
* Tracks how many times `pipeChat` has been called in the current `chatTask` run.
|
|
98
|
+
* Used to prevent double-piping when a user both calls `pipeChat()` manually
|
|
99
|
+
* and returns a streamable from their `run` function.
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
let _chatPipeCount = 0;
|
|
103
|
+
function isUIMessageStreamable(value) {
|
|
104
|
+
return (typeof value === "object" &&
|
|
105
|
+
value !== null &&
|
|
106
|
+
"toUIMessageStream" in value &&
|
|
107
|
+
typeof value.toUIMessageStream === "function");
|
|
108
|
+
}
|
|
109
|
+
function isAsyncIterable(value) {
|
|
110
|
+
return typeof value === "object" && value !== null && Symbol.asyncIterator in value;
|
|
111
|
+
}
|
|
112
|
+
function isReadableStream(value) {
|
|
113
|
+
return typeof value === "object" && value !== null && typeof value.getReader === "function";
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Pipes a chat stream to the realtime stream, making it available to the
|
|
117
|
+
* `TriggerChatTransport` on the frontend.
|
|
118
|
+
*
|
|
119
|
+
* Accepts:
|
|
120
|
+
* - A `StreamTextResult` from `streamText()` (has `.toUIMessageStream()`)
|
|
121
|
+
* - An `AsyncIterable` of `UIMessageChunk`s
|
|
122
|
+
* - A `ReadableStream` of `UIMessageChunk`s
|
|
123
|
+
*
|
|
124
|
+
* Must be called from inside a Trigger.dev task's `run` function.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* import { task } from "@trigger.dev/sdk";
|
|
129
|
+
* import { chat, type ChatTaskPayload } from "@trigger.dev/sdk/ai";
|
|
130
|
+
* import { streamText, convertToModelMessages } from "ai";
|
|
131
|
+
*
|
|
132
|
+
* export const myChatTask = task({
|
|
133
|
+
* id: "my-chat-task",
|
|
134
|
+
* run: async (payload: ChatTaskPayload) => {
|
|
135
|
+
* const result = streamText({
|
|
136
|
+
* model: openai("gpt-4o"),
|
|
137
|
+
* messages: payload.messages,
|
|
138
|
+
* });
|
|
139
|
+
*
|
|
140
|
+
* await chat.pipe(result);
|
|
141
|
+
* },
|
|
142
|
+
* });
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* // Works from anywhere inside a task — even deep in your agent code
|
|
148
|
+
* async function runAgentLoop(messages: CoreMessage[]) {
|
|
149
|
+
* const result = streamText({ model, messages });
|
|
150
|
+
* await chat.pipe(result);
|
|
151
|
+
* }
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
async function pipeChat(source, options) {
|
|
155
|
+
_chatPipeCount++;
|
|
156
|
+
const streamKey = options?.streamKey ?? exports.CHAT_STREAM_KEY;
|
|
157
|
+
let stream;
|
|
158
|
+
if (isUIMessageStreamable(source)) {
|
|
159
|
+
stream = source.toUIMessageStream();
|
|
160
|
+
}
|
|
161
|
+
else if (isAsyncIterable(source) || isReadableStream(source)) {
|
|
162
|
+
stream = source;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
throw new Error("pipeChat: source must be a StreamTextResult (with .toUIMessageStream()), " +
|
|
166
|
+
"an AsyncIterable, or a ReadableStream");
|
|
167
|
+
}
|
|
168
|
+
const pipeOptions = {};
|
|
169
|
+
if (options?.signal) {
|
|
170
|
+
pipeOptions.signal = options.signal;
|
|
171
|
+
}
|
|
172
|
+
if (options?.target) {
|
|
173
|
+
pipeOptions.target = options.target;
|
|
174
|
+
}
|
|
175
|
+
if (options?.spanName) {
|
|
176
|
+
pipeOptions.spanName = options.spanName;
|
|
177
|
+
}
|
|
178
|
+
const { waitUntilComplete } = streams_js_1.streams.pipe(streamKey, stream, pipeOptions);
|
|
179
|
+
await waitUntilComplete();
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Creates a Trigger.dev task pre-configured for AI SDK chat.
|
|
183
|
+
*
|
|
184
|
+
* - **Pre-types the payload** as `ChatTaskRunPayload` — includes abort signals
|
|
185
|
+
* - **Auto-pipes the stream** if `run` returns a `StreamTextResult`
|
|
186
|
+
* - **Multi-turn**: keeps the conversation in a single run using input streams
|
|
187
|
+
* - **Stop support**: frontend can stop generation mid-stream via the stop input stream
|
|
188
|
+
* - For complex flows, use `pipeChat()` from anywhere inside your task code
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* import { chat } from "@trigger.dev/sdk/ai";
|
|
193
|
+
* import { streamText, convertToModelMessages } from "ai";
|
|
194
|
+
* import { openai } from "@ai-sdk/openai";
|
|
195
|
+
*
|
|
196
|
+
* export const myChat = chat.task({
|
|
197
|
+
* id: "my-chat",
|
|
198
|
+
* run: async ({ messages, signal }) => {
|
|
199
|
+
* return streamText({
|
|
200
|
+
* model: openai("gpt-4o"),
|
|
201
|
+
* messages, // already converted via convertToModelMessages
|
|
202
|
+
* abortSignal: signal,
|
|
203
|
+
* });
|
|
204
|
+
* },
|
|
205
|
+
* });
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
function chatTask(options) {
|
|
209
|
+
const { run: userRun, onChatStart, onTurnComplete, maxTurns = 100, turnTimeout = "1h", warmTimeoutInSeconds = 30, ...restOptions } = options;
|
|
210
|
+
return (0, shared_js_1.createTask)({
|
|
211
|
+
...restOptions,
|
|
212
|
+
run: async (payload, { signal: runSignal }) => {
|
|
213
|
+
// Set gen_ai.conversation.id on the run-level span for dashboard context
|
|
214
|
+
const activeSpan = api_1.trace.getActiveSpan();
|
|
215
|
+
if (activeSpan) {
|
|
216
|
+
activeSpan.setAttribute("gen_ai.conversation.id", payload.chatId);
|
|
217
|
+
}
|
|
218
|
+
let currentWirePayload = payload;
|
|
219
|
+
// Accumulated model messages across turns. Turn 1 initialises from the
|
|
220
|
+
// full history the frontend sends; subsequent turns append only the new
|
|
221
|
+
// user message(s) and the captured assistant response.
|
|
222
|
+
let accumulatedMessages = [];
|
|
223
|
+
// Accumulated UI messages for persistence. Mirrors the model accumulator
|
|
224
|
+
// but in frontend-friendly UIMessage format (with parts, id, etc.).
|
|
225
|
+
let accumulatedUIMessages = [];
|
|
226
|
+
// Mutable reference to the current turn's stop controller so the
|
|
227
|
+
// stop input stream listener (registered once) can abort the right turn.
|
|
228
|
+
let currentStopController;
|
|
229
|
+
// Listen for stop signals for the lifetime of the run
|
|
230
|
+
const stopSub = stopInput.on((data) => {
|
|
231
|
+
currentStopController?.abort(data?.message || "stopped");
|
|
232
|
+
});
|
|
233
|
+
try {
|
|
234
|
+
for (let turn = 0; turn < maxTurns; turn++) {
|
|
235
|
+
// Extract turn-level context before entering the span
|
|
236
|
+
const { metadata: wireMetadata, messages: uiMessages, ...restWire } = currentWirePayload;
|
|
237
|
+
const lastUserMessage = extractLastUserMessageText(uiMessages);
|
|
238
|
+
const turnAttributes = {
|
|
239
|
+
"turn.number": turn + 1,
|
|
240
|
+
"gen_ai.conversation.id": currentWirePayload.chatId,
|
|
241
|
+
"gen_ai.operation.name": "chat",
|
|
242
|
+
"chat.trigger": currentWirePayload.trigger,
|
|
243
|
+
[v3_1.SemanticInternalAttributes.STYLE_ICON]: "tabler-message-chatbot",
|
|
244
|
+
[v3_1.SemanticInternalAttributes.ENTITY_TYPE]: "chat-turn",
|
|
245
|
+
};
|
|
246
|
+
if (lastUserMessage) {
|
|
247
|
+
turnAttributes["chat.user_message"] = lastUserMessage;
|
|
248
|
+
// Show a truncated preview of the user message as an accessory
|
|
249
|
+
const preview = lastUserMessage.length > 80
|
|
250
|
+
? lastUserMessage.slice(0, 80) + "..."
|
|
251
|
+
: lastUserMessage;
|
|
252
|
+
Object.assign(turnAttributes, (0, v3_1.accessoryAttributes)({
|
|
253
|
+
items: [{ text: preview, variant: "normal" }],
|
|
254
|
+
style: "codepath",
|
|
255
|
+
}));
|
|
256
|
+
}
|
|
257
|
+
if (wireMetadata !== undefined) {
|
|
258
|
+
turnAttributes["chat.client_data"] =
|
|
259
|
+
typeof wireMetadata === "string" ? wireMetadata : JSON.stringify(wireMetadata);
|
|
260
|
+
}
|
|
261
|
+
const turnResult = await tracer_js_1.tracer.startActiveSpan(`chat turn ${turn + 1}`, async () => {
|
|
262
|
+
_chatPipeCount = 0;
|
|
263
|
+
// Per-turn stop controller (reset each turn)
|
|
264
|
+
const stopController = new AbortController();
|
|
265
|
+
currentStopController = stopController;
|
|
266
|
+
// Three signals for the user's run function
|
|
267
|
+
const stopSignal = stopController.signal;
|
|
268
|
+
const cancelSignal = runSignal;
|
|
269
|
+
const combinedSignal = AbortSignal.any([runSignal, stopController.signal]);
|
|
270
|
+
// Buffer messages that arrive during streaming
|
|
271
|
+
const pendingMessages = [];
|
|
272
|
+
const msgSub = messagesInput.on((msg) => {
|
|
273
|
+
pendingMessages.push(msg);
|
|
274
|
+
});
|
|
275
|
+
// Convert the incoming UIMessages to model messages and update the accumulator.
|
|
276
|
+
// Turn 1: full history from the frontend → replaces the accumulator.
|
|
277
|
+
// Turn 2+: only the new message(s) → appended to the accumulator.
|
|
278
|
+
const incomingModelMessages = await (0, ai_1.convertToModelMessages)(uiMessages);
|
|
279
|
+
// Track new messages for this turn (user input + assistant response).
|
|
280
|
+
const turnNewModelMessages = [];
|
|
281
|
+
const turnNewUIMessages = [];
|
|
282
|
+
if (turn === 0) {
|
|
283
|
+
accumulatedMessages = incomingModelMessages;
|
|
284
|
+
accumulatedUIMessages = [...uiMessages];
|
|
285
|
+
// On first turn, the "new" messages are just the last user message
|
|
286
|
+
// (the rest is history). We'll add the response after streaming.
|
|
287
|
+
if (uiMessages.length > 0) {
|
|
288
|
+
turnNewUIMessages.push(uiMessages[uiMessages.length - 1]);
|
|
289
|
+
const lastModel = incomingModelMessages[incomingModelMessages.length - 1];
|
|
290
|
+
if (lastModel)
|
|
291
|
+
turnNewModelMessages.push(lastModel);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
else if (currentWirePayload.trigger === "regenerate-message") {
|
|
295
|
+
// Regenerate: frontend sent full history with last assistant message
|
|
296
|
+
// removed. Reset the accumulator to match.
|
|
297
|
+
accumulatedMessages = incomingModelMessages;
|
|
298
|
+
accumulatedUIMessages = [...uiMessages];
|
|
299
|
+
// No new user messages for regenerate — just the response (added below)
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
// Submit: frontend sent only the new user message(s). Append to accumulator.
|
|
303
|
+
accumulatedMessages.push(...incomingModelMessages);
|
|
304
|
+
accumulatedUIMessages.push(...uiMessages);
|
|
305
|
+
turnNewModelMessages.push(...incomingModelMessages);
|
|
306
|
+
turnNewUIMessages.push(...uiMessages);
|
|
307
|
+
}
|
|
308
|
+
// Fire onChatStart on the first turn
|
|
309
|
+
if (turn === 0 && onChatStart) {
|
|
310
|
+
await tracer_js_1.tracer.startActiveSpan("onChatStart()", async () => {
|
|
311
|
+
await onChatStart({
|
|
312
|
+
chatId: currentWirePayload.chatId,
|
|
313
|
+
messages: accumulatedMessages,
|
|
314
|
+
clientData: wireMetadata,
|
|
315
|
+
});
|
|
316
|
+
}, {
|
|
317
|
+
attributes: {
|
|
318
|
+
[v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onStart",
|
|
319
|
+
[v3_1.SemanticInternalAttributes.COLLAPSED]: true,
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
// Captured by the onFinish callback below — works even on abort/stop.
|
|
324
|
+
let capturedResponseMessage;
|
|
325
|
+
try {
|
|
326
|
+
const result = await userRun({
|
|
327
|
+
...restWire,
|
|
328
|
+
messages: accumulatedMessages,
|
|
329
|
+
clientData: wireMetadata,
|
|
330
|
+
signal: combinedSignal,
|
|
331
|
+
cancelSignal,
|
|
332
|
+
stopSignal,
|
|
333
|
+
});
|
|
334
|
+
// Auto-pipe if the run function returned a StreamTextResult or similar,
|
|
335
|
+
// but only if pipeChat() wasn't already called manually during this turn.
|
|
336
|
+
// We call toUIMessageStream ourselves to attach onFinish for response capture.
|
|
337
|
+
if (_chatPipeCount === 0 && isUIMessageStreamable(result)) {
|
|
338
|
+
const uiStream = result.toUIMessageStream({
|
|
339
|
+
onFinish: ({ responseMessage }) => {
|
|
340
|
+
capturedResponseMessage = responseMessage;
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
await pipeChat(uiStream, { signal: combinedSignal, spanName: "stream response" });
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
// Handle AbortError from streamText gracefully
|
|
348
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
349
|
+
if (runSignal.aborted) {
|
|
350
|
+
return "exit"; // Full run cancellation — exit
|
|
351
|
+
}
|
|
352
|
+
// Stop generation — fall through to continue the loop
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
throw error;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
finally {
|
|
359
|
+
msgSub.off();
|
|
360
|
+
}
|
|
361
|
+
// Append the assistant's response (partial or complete) to the accumulator.
|
|
362
|
+
// The onFinish callback fires even on abort/stop, so partial responses
|
|
363
|
+
// from stopped generation are captured correctly.
|
|
364
|
+
if (capturedResponseMessage) {
|
|
365
|
+
accumulatedUIMessages.push(capturedResponseMessage);
|
|
366
|
+
turnNewUIMessages.push(capturedResponseMessage);
|
|
367
|
+
try {
|
|
368
|
+
const responseModelMessages = await (0, ai_1.convertToModelMessages)([
|
|
369
|
+
stripProviderMetadata(capturedResponseMessage),
|
|
370
|
+
]);
|
|
371
|
+
accumulatedMessages.push(...responseModelMessages);
|
|
372
|
+
turnNewModelMessages.push(...responseModelMessages);
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
// Conversion failed — skip accumulation for this turn
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// TODO: When the user calls `pipeChat` manually instead of returning a
|
|
379
|
+
// StreamTextResult, we don't have access to onFinish. A future iteration
|
|
380
|
+
// should let manual-mode users report back response messages for
|
|
381
|
+
// accumulation (e.g. via a `chat.addMessages()` helper).
|
|
382
|
+
if (runSignal.aborted)
|
|
383
|
+
return "exit";
|
|
384
|
+
// Fire onTurnComplete after response capture
|
|
385
|
+
if (onTurnComplete) {
|
|
386
|
+
await tracer_js_1.tracer.startActiveSpan("onTurnComplete()", async () => {
|
|
387
|
+
await onTurnComplete({
|
|
388
|
+
chatId: currentWirePayload.chatId,
|
|
389
|
+
messages: accumulatedMessages,
|
|
390
|
+
uiMessages: accumulatedUIMessages,
|
|
391
|
+
newMessages: turnNewModelMessages,
|
|
392
|
+
newUIMessages: turnNewUIMessages,
|
|
393
|
+
responseMessage: capturedResponseMessage,
|
|
394
|
+
turn,
|
|
395
|
+
});
|
|
396
|
+
}, {
|
|
397
|
+
attributes: {
|
|
398
|
+
[v3_1.SemanticInternalAttributes.STYLE_ICON]: "task-hook-onComplete",
|
|
399
|
+
[v3_1.SemanticInternalAttributes.COLLAPSED]: true,
|
|
400
|
+
},
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
// Write turn-complete control chunk so frontend closes its stream
|
|
404
|
+
await writeTurnCompleteChunk(currentWirePayload.chatId);
|
|
405
|
+
// If messages arrived during streaming, use the first one immediately
|
|
406
|
+
if (pendingMessages.length > 0) {
|
|
407
|
+
currentWirePayload = pendingMessages[0];
|
|
408
|
+
return "continue";
|
|
409
|
+
}
|
|
410
|
+
// Phase 1: Keep the run warm for quick response to the next message.
|
|
411
|
+
// The run stays active (using compute) during this window.
|
|
412
|
+
const effectiveWarmTimeout = metadata_js_1.metadata.get(WARM_TIMEOUT_METADATA_KEY) ?? warmTimeoutInSeconds;
|
|
413
|
+
if (effectiveWarmTimeout > 0) {
|
|
414
|
+
const warm = await messagesInput.once({
|
|
415
|
+
timeoutMs: effectiveWarmTimeout * 1000,
|
|
416
|
+
spanName: "waiting (warm)",
|
|
417
|
+
});
|
|
418
|
+
if (warm.ok) {
|
|
419
|
+
// Message arrived while warm — respond instantly
|
|
420
|
+
currentWirePayload = warm.output;
|
|
421
|
+
return "continue";
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
// Phase 2: Suspend the task (frees compute) until the next message arrives
|
|
425
|
+
const effectiveTurnTimeout = metadata_js_1.metadata.get(TURN_TIMEOUT_METADATA_KEY) ?? turnTimeout;
|
|
426
|
+
const next = await messagesInput.wait({
|
|
427
|
+
timeout: effectiveTurnTimeout,
|
|
428
|
+
spanName: "waiting (suspended)",
|
|
429
|
+
});
|
|
430
|
+
if (!next.ok) {
|
|
431
|
+
// Timed out waiting for the next message — end the conversation
|
|
432
|
+
return "exit";
|
|
433
|
+
}
|
|
434
|
+
currentWirePayload = next.output;
|
|
435
|
+
return "continue";
|
|
436
|
+
}, {
|
|
437
|
+
attributes: turnAttributes,
|
|
438
|
+
});
|
|
439
|
+
if (turnResult === "exit")
|
|
440
|
+
return;
|
|
441
|
+
// "continue" means proceed to next iteration
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
finally {
|
|
445
|
+
stopSub.off();
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Namespace for AI SDK chat integration.
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* ```ts
|
|
455
|
+
* import { chat } from "@trigger.dev/sdk/ai";
|
|
456
|
+
*
|
|
457
|
+
* // Define a chat task
|
|
458
|
+
* export const myChat = chat.task({
|
|
459
|
+
* id: "my-chat",
|
|
460
|
+
* run: async ({ messages, signal }) => {
|
|
461
|
+
* return streamText({ model, messages, abortSignal: signal });
|
|
462
|
+
* },
|
|
463
|
+
* });
|
|
464
|
+
*
|
|
465
|
+
* // Pipe a stream manually (from inside a task)
|
|
466
|
+
* await chat.pipe(streamTextResult);
|
|
467
|
+
*
|
|
468
|
+
* // Create an access token (from a server action)
|
|
469
|
+
* const token = await chat.createAccessToken("my-chat");
|
|
470
|
+
* ```
|
|
471
|
+
*/
|
|
472
|
+
// ---------------------------------------------------------------------------
|
|
473
|
+
// Runtime configuration helpers
|
|
474
|
+
// ---------------------------------------------------------------------------
|
|
475
|
+
const TURN_TIMEOUT_METADATA_KEY = "chat.turnTimeout";
|
|
476
|
+
const WARM_TIMEOUT_METADATA_KEY = "chat.warmTimeout";
|
|
477
|
+
/**
|
|
478
|
+
* Override the turn timeout for subsequent turns in the current run.
|
|
479
|
+
*
|
|
480
|
+
* The turn timeout controls how long the run stays suspended (freeing compute)
|
|
481
|
+
* waiting for the next user message. When it expires, the run completes
|
|
482
|
+
* gracefully and the next message starts a fresh run.
|
|
483
|
+
*
|
|
484
|
+
* Call from inside a `chatTask` run function to adjust based on context.
|
|
485
|
+
*
|
|
486
|
+
* @param duration - A duration string (e.g. `"5m"`, `"1h"`, `"30s"`)
|
|
487
|
+
*
|
|
488
|
+
* @example
|
|
489
|
+
* ```ts
|
|
490
|
+
* run: async ({ messages, signal }) => {
|
|
491
|
+
* chat.setTurnTimeout("2h");
|
|
492
|
+
* return streamText({ model, messages, abortSignal: signal });
|
|
493
|
+
* }
|
|
494
|
+
* ```
|
|
495
|
+
*/
|
|
496
|
+
function setTurnTimeout(duration) {
|
|
497
|
+
metadata_js_1.metadata.set(TURN_TIMEOUT_METADATA_KEY, duration);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Override the turn timeout in seconds for subsequent turns in the current run.
|
|
501
|
+
*
|
|
502
|
+
* @param seconds - Number of seconds to wait for the next message before ending the run
|
|
503
|
+
*
|
|
504
|
+
* @example
|
|
505
|
+
* ```ts
|
|
506
|
+
* run: async ({ messages, signal }) => {
|
|
507
|
+
* chat.setTurnTimeoutInSeconds(3600); // 1 hour
|
|
508
|
+
* return streamText({ model, messages, abortSignal: signal });
|
|
509
|
+
* }
|
|
510
|
+
* ```
|
|
511
|
+
*/
|
|
512
|
+
function setTurnTimeoutInSeconds(seconds) {
|
|
513
|
+
metadata_js_1.metadata.set(TURN_TIMEOUT_METADATA_KEY, `${seconds}s`);
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Override the warm timeout for subsequent turns in the current run.
|
|
517
|
+
*
|
|
518
|
+
* The warm timeout controls how long the run stays active (using compute)
|
|
519
|
+
* after each turn, waiting for the next message. During this window,
|
|
520
|
+
* responses are instant. After it expires, the run suspends.
|
|
521
|
+
*
|
|
522
|
+
* @param seconds - Number of seconds to stay warm (0 to suspend immediately)
|
|
523
|
+
*
|
|
524
|
+
* @example
|
|
525
|
+
* ```ts
|
|
526
|
+
* run: async ({ messages, signal }) => {
|
|
527
|
+
* chat.setWarmTimeoutInSeconds(60);
|
|
528
|
+
* return streamText({ model, messages, abortSignal: signal });
|
|
529
|
+
* }
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
function setWarmTimeoutInSeconds(seconds) {
|
|
533
|
+
metadata_js_1.metadata.set(WARM_TIMEOUT_METADATA_KEY, seconds);
|
|
534
|
+
}
|
|
535
|
+
exports.chat = {
|
|
536
|
+
/** Create a chat task. See {@link chatTask}. */
|
|
537
|
+
task: chatTask,
|
|
538
|
+
/** Pipe a stream to the chat transport. See {@link pipeChat}. */
|
|
539
|
+
pipe: pipeChat,
|
|
540
|
+
/** Create a public access token for a chat task. See {@link createChatAccessToken}. */
|
|
541
|
+
createAccessToken: createChatAccessToken,
|
|
542
|
+
/** Override the turn timeout at runtime (duration string). See {@link setTurnTimeout}. */
|
|
543
|
+
setTurnTimeout,
|
|
544
|
+
/** Override the turn timeout at runtime (seconds). See {@link setTurnTimeoutInSeconds}. */
|
|
545
|
+
setTurnTimeoutInSeconds,
|
|
546
|
+
/** Override the warm timeout at runtime. See {@link setWarmTimeoutInSeconds}. */
|
|
547
|
+
setWarmTimeoutInSeconds,
|
|
548
|
+
};
|
|
549
|
+
/**
|
|
550
|
+
* Writes a turn-complete control chunk to the chat output stream.
|
|
551
|
+
* The frontend transport intercepts this to close the ReadableStream for the current turn.
|
|
552
|
+
* @internal
|
|
553
|
+
*/
|
|
554
|
+
async function writeTurnCompleteChunk(chatId) {
|
|
555
|
+
const { waitUntilComplete } = streams_js_1.streams.writer(exports.CHAT_STREAM_KEY, {
|
|
556
|
+
spanName: "turn complete",
|
|
557
|
+
collapsed: true,
|
|
558
|
+
execute: ({ write }) => {
|
|
559
|
+
write({ type: "__trigger_turn_complete" });
|
|
560
|
+
},
|
|
561
|
+
});
|
|
562
|
+
await waitUntilComplete();
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Extracts the text content of the last user message from a UIMessage array.
|
|
566
|
+
* Returns undefined if no user message is found.
|
|
567
|
+
* @internal
|
|
568
|
+
*/
|
|
569
|
+
function extractLastUserMessageText(messages) {
|
|
570
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
571
|
+
const msg = messages[i];
|
|
572
|
+
if (msg.role !== "user")
|
|
573
|
+
continue;
|
|
574
|
+
// UIMessage uses parts array
|
|
575
|
+
if (msg.parts) {
|
|
576
|
+
const textParts = msg.parts
|
|
577
|
+
.filter((p) => p.type === "text" && p.text)
|
|
578
|
+
.map((p) => p.text);
|
|
579
|
+
if (textParts.length > 0) {
|
|
580
|
+
return textParts.join("\n");
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
return undefined;
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Strips ephemeral OpenAI Responses API `itemId` from a UIMessage's parts.
|
|
589
|
+
*
|
|
590
|
+
* The OpenAI Responses provider attaches `itemId` to message parts via
|
|
591
|
+
* `providerMetadata.openai.itemId`. These IDs are ephemeral — sending them
|
|
592
|
+
* back in a subsequent `streamText` call causes 404s because the provider
|
|
593
|
+
* can't find the referenced item (especially for stopped/partial responses).
|
|
594
|
+
*
|
|
595
|
+
* @internal
|
|
596
|
+
*/
|
|
597
|
+
function stripProviderMetadata(message) {
|
|
598
|
+
if (!message.parts)
|
|
599
|
+
return message;
|
|
600
|
+
return {
|
|
601
|
+
...message,
|
|
602
|
+
parts: message.parts.map((part) => {
|
|
603
|
+
const openai = part.providerMetadata?.openai;
|
|
604
|
+
if (!openai?.itemId)
|
|
605
|
+
return part;
|
|
606
|
+
const { itemId, ...restOpenai } = openai;
|
|
607
|
+
const { openai: _, ...restProviders } = part.providerMetadata;
|
|
608
|
+
return {
|
|
609
|
+
...part,
|
|
610
|
+
providerMetadata: {
|
|
611
|
+
...restProviders,
|
|
612
|
+
...(Object.keys(restOpenai).length > 0 ? { openai: restOpenai } : {}),
|
|
613
|
+
},
|
|
614
|
+
};
|
|
615
|
+
}),
|
|
616
|
+
};
|
|
617
|
+
}
|
|
56
618
|
//# sourceMappingURL=ai.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai.js","sourceRoot":"","sources":["../../../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AG,CAAC,EAAE,CAAC;gBACzB,OAAO,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;QAED,MAAM;IACR,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,qBAAqB,CAAC,OAAkB;IAC/C,IAAI,CAAC,OAAO,CAAC,KAAK;QAAE,OAAO,OAAO,CAAC;IACnC,OAAO;QACL,GAAG,OAAO;QACV,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAS,EAAE,EAAE;YACrC,MAAM,MAAM,GAAG,IAAI,CAAC,gBAAgB,EAAE,MAAM,CAAC;YAC7C,IAAI,CAAC,MAAM,EAAE,MAAM;gBAAE,OAAO,IAAI,CAAC;YAEjC,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,EAAE,GAAG,MAAM,CAAC;YACzC,MAAM,EAAE,MAAM,EAAE,CAAC,EAAE,GAAG,aAAa,EAAE,GAAG,IAAI,CAAC,gBAAgB,CAAC;YAC9D,OAAO;gBACL,GAAG,IAAI;gBACP,gBAAgB,EAAE;oBAChB,GAAG,aAAa;oBAChB,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;iBACtE;aACF,CAAC;QACJ,CAAC,CAAC;KACH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stream IDs used for bidirectional chat communication.
|
|
3
|
+
* Shared between backend (ai.ts) and frontend (chat.ts).
|
|
4
|
+
*/
|
|
5
|
+
/** The output stream key where UIMessageChunks are written. */
|
|
6
|
+
export declare const CHAT_STREAM_KEY = "chat";
|
|
7
|
+
/** Input stream ID for sending chat messages to the running task. */
|
|
8
|
+
export declare const CHAT_MESSAGES_STREAM_ID = "chat-messages";
|
|
9
|
+
/** Input stream ID for sending stop signals to abort the current generation. */
|
|
10
|
+
export declare const CHAT_STOP_STREAM_ID = "chat-stop";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Stream IDs used for bidirectional chat communication.
|
|
4
|
+
* Shared between backend (ai.ts) and frontend (chat.ts).
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.CHAT_STOP_STREAM_ID = exports.CHAT_MESSAGES_STREAM_ID = exports.CHAT_STREAM_KEY = void 0;
|
|
8
|
+
/** The output stream key where UIMessageChunks are written. */
|
|
9
|
+
exports.CHAT_STREAM_KEY = "chat";
|
|
10
|
+
/** Input stream ID for sending chat messages to the running task. */
|
|
11
|
+
exports.CHAT_MESSAGES_STREAM_ID = "chat-messages";
|
|
12
|
+
/** Input stream ID for sending stop signals to abort the current generation. */
|
|
13
|
+
exports.CHAT_STOP_STREAM_ID = "chat-stop";
|
|
14
|
+
//# sourceMappingURL=chat-constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-constants.js","sourceRoot":"","sources":["../../../src/v3/chat-constants.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,+DAA+D;AAClD,QAAA,eAAe,GAAG,MAAM,CAAC;AAEtC,qEAAqE;AACxD,QAAA,uBAAuB,GAAG,eAAe,CAAC;AAEvD,gFAAgF;AACnE,QAAA,mBAAmB,GAAG,WAAW,CAAC"}
|