@trigger.dev/sdk 0.0.0-chat-prerelease-20260417152143 → 0.0.0-chat-prerelease-20260418174118
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 +51 -2
- package/dist/commonjs/v3/ai.js +50 -6
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat-react.js +31 -4
- package/dist/commonjs/v3/chat-react.js.map +1 -1
- package/dist/commonjs/v3/test/index.d.ts +3 -0
- package/dist/commonjs/v3/test/index.js +18 -0
- package/dist/commonjs/v3/test/index.js.map +1 -0
- package/dist/commonjs/v3/test/mock-chat-agent.d.ts +121 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js +233 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -0
- package/dist/commonjs/v3/test/setup-catalog.d.ts +1 -0
- package/dist/commonjs/v3/test/setup-catalog.js +18 -0
- package/dist/commonjs/v3/test/setup-catalog.js.map +1 -0
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +51 -2
- package/dist/esm/v3/ai.js +50 -6
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat-react.js +31 -4
- package/dist/esm/v3/chat-react.js.map +1 -1
- package/dist/esm/v3/test/index.d.ts +3 -0
- package/dist/esm/v3/test/index.js +13 -0
- package/dist/esm/v3/test/index.js.map +1 -0
- package/dist/esm/v3/test/mock-chat-agent.d.ts +121 -0
- package/dist/esm/v3/test/mock-chat-agent.js +230 -0
- package/dist/esm/v3/test/mock-chat-agent.js.map +1 -0
- package/dist/esm/v3/test/setup-catalog.d.ts +1 -0
- package/dist/esm/v3/test/setup-catalog.js +16 -0
- package/dist/esm/v3/test/setup-catalog.js.map +1 -0
- package/dist/esm/version.js +1 -1
- package/package.json +18 -2
package/dist/commonjs/v3/ai.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AnyTask, Task, type inferSchemaIn, type inferSchemaOut, type TaskIdentifier, type TaskOptions, type TaskSchema, type TaskRunContext, type TaskWithSchema } from "@trigger.dev/core/v3";
|
|
2
|
-
import type { ModelMessage, ToolSet, UIMessage, UIMessageChunk, UIMessageStreamOptions, LanguageModelUsage } from "ai";
|
|
2
|
+
import type { FinishReason, ModelMessage, ToolSet, UIMessage, UIMessageChunk, UIMessageStreamOptions, LanguageModelUsage } from "ai";
|
|
3
3
|
import { Tool, ToolCallOptions } from "ai";
|
|
4
4
|
import { locals } from "./locals.js";
|
|
5
5
|
import type { ResolvedPrompt } from "./prompt.js";
|
|
@@ -202,8 +202,12 @@ export type ChatTaskPayload<TClientData = unknown> = {
|
|
|
202
202
|
* - `"submit-message"`: A new user message
|
|
203
203
|
* - `"regenerate-message"`: Regenerate the last assistant response
|
|
204
204
|
* - `"preload"`: Run was preloaded before the first message (only on turn 0)
|
|
205
|
+
* - `"action"`: A typed action from the frontend (see `actionSchema` + `onAction`).
|
|
206
|
+
* The action has already been applied before `run()` fires — check `trigger === "action"`
|
|
207
|
+
* to short-circuit the LLM call when an action doesn't need a response.
|
|
208
|
+
* - `"close"`: The chat session is being closed (internal; `run()` is not called).
|
|
205
209
|
*/
|
|
206
|
-
trigger: "submit-message" | "regenerate-message" | "preload" | "close";
|
|
210
|
+
trigger: "submit-message" | "regenerate-message" | "preload" | "action" | "close";
|
|
207
211
|
/** The ID of the message to regenerate (only for `"regenerate-message"`) */
|
|
208
212
|
messageId?: string;
|
|
209
213
|
/** Custom data from the frontend (passed via `metadata` on `sendMessage()` or the transport). */
|
|
@@ -979,6 +983,22 @@ export type TurnCompleteEvent<TClientData = unknown, TUIM extends UIMessage = UI
|
|
|
979
983
|
usage?: LanguageModelUsage;
|
|
980
984
|
/** Cumulative token usage across all turns in this run (including this turn). */
|
|
981
985
|
totalUsage: LanguageModelUsage;
|
|
986
|
+
/**
|
|
987
|
+
* Why the LLM stopped generating this turn:
|
|
988
|
+
* - `"stop"` — model generated a stop sequence (normal completion)
|
|
989
|
+
* - `"tool-calls"` — model stopped on one or more tool calls. If any tool
|
|
990
|
+
* has no `execute` function (e.g. an `ask_user` HITL tool), the turn is
|
|
991
|
+
* paused awaiting user input; inspect `responseMessage.parts` for tool
|
|
992
|
+
* parts in `input-available` state to distinguish.
|
|
993
|
+
* - `"length"` — max tokens reached
|
|
994
|
+
* - `"content-filter"` — content filter stopped the model
|
|
995
|
+
* - `"error"` — model errored
|
|
996
|
+
* - `"other"` — provider-specific reason
|
|
997
|
+
*
|
|
998
|
+
* Undefined if the underlying stream didn't provide a finish reason (e.g.
|
|
999
|
+
* manual `pipeChat()` or an aborted stream).
|
|
1000
|
+
*/
|
|
1001
|
+
finishReason?: FinishReason;
|
|
982
1002
|
};
|
|
983
1003
|
/**
|
|
984
1004
|
* Event passed to the `onBeforeTurnComplete` callback.
|
|
@@ -1744,6 +1764,33 @@ declare function isStopped(): boolean;
|
|
|
1744
1764
|
* ```
|
|
1745
1765
|
*/
|
|
1746
1766
|
declare function requestUpgrade(): void;
|
|
1767
|
+
/**
|
|
1768
|
+
* Exit the run after the current turn completes, without waiting for the
|
|
1769
|
+
* next message. Unlike {@link requestUpgrade}, no upgrade-required signal
|
|
1770
|
+
* is sent to the client — the turn finishes normally, `onTurnComplete`
|
|
1771
|
+
* fires, and the loop exits instead of going idle.
|
|
1772
|
+
*
|
|
1773
|
+
* Call from `run()`, `chat.defer()`, `onBeforeTurnComplete`, or
|
|
1774
|
+
* `onTurnComplete` to end the run on your own terms (budget exhausted,
|
|
1775
|
+
* task complete, goal achieved, etc.).
|
|
1776
|
+
*
|
|
1777
|
+
* The next user message on the same `chatId` starts a fresh run via the
|
|
1778
|
+
* normal continuation mechanism.
|
|
1779
|
+
*
|
|
1780
|
+
* @example
|
|
1781
|
+
* ```ts
|
|
1782
|
+
* chat.agent({
|
|
1783
|
+
* id: "one-shot-agent",
|
|
1784
|
+
* run: async ({ messages, signal }) => {
|
|
1785
|
+
* const result = streamText({ model: openai("gpt-4o"), messages, abortSignal: signal });
|
|
1786
|
+
* // Single-response agent — exit after this turn.
|
|
1787
|
+
* chat.endRun();
|
|
1788
|
+
* return result;
|
|
1789
|
+
* },
|
|
1790
|
+
* });
|
|
1791
|
+
* ```
|
|
1792
|
+
*/
|
|
1793
|
+
declare function endRun(): void;
|
|
1747
1794
|
/**
|
|
1748
1795
|
* Register a promise that runs in the background during the current turn.
|
|
1749
1796
|
*
|
|
@@ -2208,6 +2255,8 @@ export declare const chat: {
|
|
|
2208
2255
|
isStopped: typeof isStopped;
|
|
2209
2256
|
/** Request that the run exits after the current turn so the next message starts on the latest version. See {@link requestUpgrade}. */
|
|
2210
2257
|
requestUpgrade: typeof requestUpgrade;
|
|
2258
|
+
/** Exit the run after the current turn completes, without any upgrade signal. See {@link endRun}. */
|
|
2259
|
+
endRun: typeof endRun;
|
|
2211
2260
|
/** Clean up aborted parts from a UIMessage. See {@link cleanupAbortedParts}. */
|
|
2212
2261
|
cleanupAbortedParts: typeof cleanupAbortedParts;
|
|
2213
2262
|
/** Register background work that runs in parallel with streaming. See {@link chatDefer}. */
|
package/dist/commonjs/v3/ai.js
CHANGED
|
@@ -482,6 +482,12 @@ const chatAgentRunContextKey = locals_js_1.locals.create("chat.agentRunContext")
|
|
|
482
482
|
const chatPrepareMessagesKey = locals_js_1.locals.create("chat.prepareMessages");
|
|
483
483
|
/** @internal Flag set by `chat.requestUpgrade()` to exit the loop after the current turn. */
|
|
484
484
|
const chatUpgradeRequestedKey = locals_js_1.locals.create("chat.upgradeRequested");
|
|
485
|
+
/**
|
|
486
|
+
* @internal Flag set by `chat.endRun()` to exit the loop after the current
|
|
487
|
+
* turn completes, without any upgrade semantics. Checked at the same
|
|
488
|
+
* post-turn / pre-wait sites as `chatUpgradeRequestedKey`.
|
|
489
|
+
*/
|
|
490
|
+
const chatEndRunRequestedKey = locals_js_1.locals.create("chat.endRunRequested");
|
|
485
491
|
/** @internal */
|
|
486
492
|
const chatAgentCompactionKey = locals_js_1.locals.create("chat.agentCompaction");
|
|
487
493
|
/**
|
|
@@ -1666,6 +1672,7 @@ function chatAgent(options) {
|
|
|
1666
1672
|
}
|
|
1667
1673
|
// Captured by the onFinish callback below — works even on abort/stop.
|
|
1668
1674
|
let capturedResponseMessage;
|
|
1675
|
+
let capturedFinishReason;
|
|
1669
1676
|
// Promise that resolves when the AI SDK's onFinish fires.
|
|
1670
1677
|
// On abort, the stream's cancel() handler calls onFinish
|
|
1671
1678
|
// asynchronously AFTER pipeChat resolves, so we must await
|
|
@@ -1727,8 +1734,9 @@ function chatAgent(options) {
|
|
|
1727
1734
|
// messageId. Without this, the frontend and backend generate IDs
|
|
1728
1735
|
// independently and they won't match for ID-based dedup.
|
|
1729
1736
|
generateMessageId: resolvedOptions.generateMessageId ?? ai_1.generateId,
|
|
1730
|
-
onFinish: ({ responseMessage }) => {
|
|
1737
|
+
onFinish: ({ responseMessage, finishReason, }) => {
|
|
1731
1738
|
capturedResponseMessage = responseMessage;
|
|
1739
|
+
capturedFinishReason = finishReason;
|
|
1732
1740
|
resolveOnFinish();
|
|
1733
1741
|
},
|
|
1734
1742
|
});
|
|
@@ -2056,6 +2064,7 @@ function chatAgent(options) {
|
|
|
2056
2064
|
preloaded,
|
|
2057
2065
|
usage: turnUsage,
|
|
2058
2066
|
totalUsage: cumulativeUsage,
|
|
2067
|
+
finishReason: capturedFinishReason,
|
|
2059
2068
|
};
|
|
2060
2069
|
// Fire onBeforeTurnComplete — stream is still open so the hook
|
|
2061
2070
|
// can write custom chunks to the frontend (e.g. compaction progress).
|
|
@@ -2158,7 +2167,9 @@ function chatAgent(options) {
|
|
|
2158
2167
|
}
|
|
2159
2168
|
// chat.requestUpgrade() was called — exit the loop so the
|
|
2160
2169
|
// transport triggers a new run on the latest version.
|
|
2161
|
-
|
|
2170
|
+
// chat.endRun() — same exit, no upgrade semantics.
|
|
2171
|
+
if (locals_js_1.locals.get(chatUpgradeRequestedKey) ||
|
|
2172
|
+
locals_js_1.locals.get(chatEndRunRequestedKey)) {
|
|
2162
2173
|
return "exit";
|
|
2163
2174
|
}
|
|
2164
2175
|
// Wait for the next message — stay idle briefly, then suspend
|
|
@@ -2253,8 +2264,9 @@ function chatAgent(options) {
|
|
|
2253
2264
|
catch {
|
|
2254
2265
|
// Best-effort — if stream write fails, let the run continue anyway
|
|
2255
2266
|
}
|
|
2256
|
-
// chat.requestUpgrade() — exit after error turn too
|
|
2257
|
-
if (locals_js_1.locals.get(chatUpgradeRequestedKey)
|
|
2267
|
+
// chat.requestUpgrade() / chat.endRun() — exit after error turn too
|
|
2268
|
+
if (locals_js_1.locals.get(chatUpgradeRequestedKey) ||
|
|
2269
|
+
locals_js_1.locals.get(chatEndRunRequestedKey)) {
|
|
2258
2270
|
return;
|
|
2259
2271
|
}
|
|
2260
2272
|
// Wait for the next message — same as after a successful turn
|
|
@@ -2624,6 +2636,35 @@ function isStopped() {
|
|
|
2624
2636
|
function requestUpgrade() {
|
|
2625
2637
|
locals_js_1.locals.set(chatUpgradeRequestedKey, true);
|
|
2626
2638
|
}
|
|
2639
|
+
/**
|
|
2640
|
+
* Exit the run after the current turn completes, without waiting for the
|
|
2641
|
+
* next message. Unlike {@link requestUpgrade}, no upgrade-required signal
|
|
2642
|
+
* is sent to the client — the turn finishes normally, `onTurnComplete`
|
|
2643
|
+
* fires, and the loop exits instead of going idle.
|
|
2644
|
+
*
|
|
2645
|
+
* Call from `run()`, `chat.defer()`, `onBeforeTurnComplete`, or
|
|
2646
|
+
* `onTurnComplete` to end the run on your own terms (budget exhausted,
|
|
2647
|
+
* task complete, goal achieved, etc.).
|
|
2648
|
+
*
|
|
2649
|
+
* The next user message on the same `chatId` starts a fresh run via the
|
|
2650
|
+
* normal continuation mechanism.
|
|
2651
|
+
*
|
|
2652
|
+
* @example
|
|
2653
|
+
* ```ts
|
|
2654
|
+
* chat.agent({
|
|
2655
|
+
* id: "one-shot-agent",
|
|
2656
|
+
* run: async ({ messages, signal }) => {
|
|
2657
|
+
* const result = streamText({ model: openai("gpt-4o"), messages, abortSignal: signal });
|
|
2658
|
+
* // Single-response agent — exit after this turn.
|
|
2659
|
+
* chat.endRun();
|
|
2660
|
+
* return result;
|
|
2661
|
+
* },
|
|
2662
|
+
* });
|
|
2663
|
+
* ```
|
|
2664
|
+
*/
|
|
2665
|
+
function endRun() {
|
|
2666
|
+
locals_js_1.locals.set(chatEndRunRequestedKey, true);
|
|
2667
|
+
}
|
|
2627
2668
|
// ---------------------------------------------------------------------------
|
|
2628
2669
|
// Per-turn deferred work
|
|
2629
2670
|
// ---------------------------------------------------------------------------
|
|
@@ -3085,8 +3126,9 @@ function createChatSession(payload, options) {
|
|
|
3085
3126
|
}
|
|
3086
3127
|
// Subsequent turns: wait for the next message
|
|
3087
3128
|
if (turn > 0) {
|
|
3088
|
-
// chat.requestUpgrade() — exit before waiting
|
|
3089
|
-
if (locals_js_1.locals.get(chatUpgradeRequestedKey)
|
|
3129
|
+
// chat.requestUpgrade() / chat.endRun() — exit before waiting
|
|
3130
|
+
if (locals_js_1.locals.get(chatUpgradeRequestedKey) ||
|
|
3131
|
+
locals_js_1.locals.get(chatEndRunRequestedKey)) {
|
|
3090
3132
|
stop.cleanup();
|
|
3091
3133
|
return { done: true, value: undefined };
|
|
3092
3134
|
}
|
|
@@ -3592,6 +3634,8 @@ exports.chat = {
|
|
|
3592
3634
|
isStopped,
|
|
3593
3635
|
/** Request that the run exits after the current turn so the next message starts on the latest version. See {@link requestUpgrade}. */
|
|
3594
3636
|
requestUpgrade,
|
|
3637
|
+
/** Exit the run after the current turn completes, without any upgrade signal. See {@link endRun}. */
|
|
3638
|
+
endRun,
|
|
3595
3639
|
/** Clean up aborted parts from a UIMessage. See {@link cleanupAbortedParts}. */
|
|
3596
3640
|
cleanupAbortedParts,
|
|
3597
3641
|
/** Register background work that runs in parallel with streaming. See {@link chatDefer}. */
|