@trigger.dev/sdk 0.0.0-prerelease-20260309160514 → 0.0.0-prerelease-20260310150337
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 +246 -1
- package/dist/commonjs/v3/ai.js +345 -41
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat.d.ts +3 -1
- package/dist/commonjs/v3/chat.js +4 -1
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/v3/runs.d.ts +1 -1
- package/dist/commonjs/v3/streams.js +33 -1
- package/dist/commonjs/v3/streams.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +246 -1
- package/dist/esm/v3/ai.js +345 -41
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat.d.ts +3 -1
- package/dist/esm/v3/chat.js +4 -1
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/v3/streams.js +33 -1
- package/dist/esm/v3/streams.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +2 -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 TaskWithSchema } from "@trigger.dev/core/v3";
|
|
2
|
-
import type { ModelMessage, UIMessage, UIMessageChunk } from "ai";
|
|
2
|
+
import type { ModelMessage, UIMessage, UIMessageChunk, UIMessageStreamOptions } from "ai";
|
|
3
3
|
import { Tool } from "ai";
|
|
4
4
|
import { locals } from "./locals.js";
|
|
5
5
|
import { CHAT_MESSAGES_STREAM_ID, CHAT_STOP_STREAM_ID } from "./chat-constants.js";
|
|
@@ -110,6 +110,8 @@ export type ChatTaskWirePayload<TMessage extends UIMessage = UIMessage, TMetadat
|
|
|
110
110
|
continuation?: boolean;
|
|
111
111
|
/** The run ID of the previous run (only set when `continuation` is true). */
|
|
112
112
|
previousRunId?: string;
|
|
113
|
+
/** Override warm timeout for this run (seconds). Set by transport.preload(). */
|
|
114
|
+
warmTimeoutInSeconds?: number;
|
|
113
115
|
};
|
|
114
116
|
/**
|
|
115
117
|
* The payload shape passed to the `chatTask` run function.
|
|
@@ -179,6 +181,19 @@ export type PipeChatOptions = {
|
|
|
179
181
|
/** Override the default span name for this operation. */
|
|
180
182
|
spanName?: string;
|
|
181
183
|
};
|
|
184
|
+
/**
|
|
185
|
+
* Options for customizing the `toUIMessageStream()` call used when piping
|
|
186
|
+
* `streamText` results to the frontend.
|
|
187
|
+
*
|
|
188
|
+
* Set static defaults via `uiMessageStreamOptions` on `chat.task()`, or
|
|
189
|
+
* override per-turn via `chat.setUIMessageStreamOptions()`.
|
|
190
|
+
*
|
|
191
|
+
* `onFinish`, `originalMessages`, and `generateMessageId` are omitted because
|
|
192
|
+
* they are managed internally for response capture and message accumulation.
|
|
193
|
+
* Use `streamText`'s `onFinish` for custom finish handling, or drop down to
|
|
194
|
+
* raw task mode with `chat.pipe()` for full control.
|
|
195
|
+
*/
|
|
196
|
+
export type ChatUIMessageStreamOptions = Omit<UIMessageStreamOptions<UIMessage>, "onFinish" | "originalMessages" | "generateMessageId">;
|
|
182
197
|
/**
|
|
183
198
|
* An object with a `toUIMessageStream()` method (e.g. `StreamTextResult` from `streamText()`).
|
|
184
199
|
*/
|
|
@@ -489,6 +504,34 @@ export type ChatTaskOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
489
504
|
* @default Same as `turnTimeout`
|
|
490
505
|
*/
|
|
491
506
|
preloadTimeout?: string;
|
|
507
|
+
/**
|
|
508
|
+
* Default options for `toUIMessageStream()` when auto-piping or using
|
|
509
|
+
* `turn.complete()` / `chat.pipeAndCapture()`.
|
|
510
|
+
*
|
|
511
|
+
* Controls how the `StreamTextResult` is converted to a `UIMessageChunk`
|
|
512
|
+
* stream — error handling, reasoning/source visibility, metadata, etc.
|
|
513
|
+
*
|
|
514
|
+
* Can be overridden per-turn by calling `chat.setUIMessageStreamOptions()`
|
|
515
|
+
* inside `run()` or lifecycle hooks. Per-turn values are merged on top
|
|
516
|
+
* of these defaults (per-turn wins on conflicts).
|
|
517
|
+
*
|
|
518
|
+
* `onFinish`, `originalMessages`, and `generateMessageId` are managed
|
|
519
|
+
* internally and cannot be overridden here. Use `streamText`'s `onFinish`
|
|
520
|
+
* for custom finish handling, or drop to raw task mode for full control.
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```ts
|
|
524
|
+
* chat.task({
|
|
525
|
+
* id: "my-chat",
|
|
526
|
+
* uiMessageStreamOptions: {
|
|
527
|
+
* sendReasoning: true,
|
|
528
|
+
* onError: (error) => error instanceof Error ? error.message : "An error occurred.",
|
|
529
|
+
* },
|
|
530
|
+
* run: async ({ messages, signal }) => { ... },
|
|
531
|
+
* });
|
|
532
|
+
* ```
|
|
533
|
+
*/
|
|
534
|
+
uiMessageStreamOptions?: ChatUIMessageStreamOptions;
|
|
492
535
|
};
|
|
493
536
|
/**
|
|
494
537
|
* Creates a Trigger.dev task pre-configured for AI SDK chat.
|
|
@@ -570,6 +613,28 @@ declare function setTurnTimeoutInSeconds(seconds: number): void;
|
|
|
570
613
|
* ```
|
|
571
614
|
*/
|
|
572
615
|
declare function setWarmTimeoutInSeconds(seconds: number): void;
|
|
616
|
+
/**
|
|
617
|
+
* Override the `toUIMessageStream()` options for the current turn.
|
|
618
|
+
*
|
|
619
|
+
* These options control how the `StreamTextResult` is converted to a
|
|
620
|
+
* `UIMessageChunk` stream — error handling, reasoning/source visibility,
|
|
621
|
+
* message metadata, etc.
|
|
622
|
+
*
|
|
623
|
+
* Per-turn options are merged on top of the static `uiMessageStreamOptions`
|
|
624
|
+
* set on `chat.task()`. Per-turn values win on conflicts.
|
|
625
|
+
*
|
|
626
|
+
* @example
|
|
627
|
+
* ```ts
|
|
628
|
+
* run: async ({ messages, signal }) => {
|
|
629
|
+
* chat.setUIMessageStreamOptions({
|
|
630
|
+
* sendReasoning: true,
|
|
631
|
+
* onError: (error) => error instanceof Error ? error.message : "An error occurred.",
|
|
632
|
+
* });
|
|
633
|
+
* return streamText({ model, messages, abortSignal: signal });
|
|
634
|
+
* }
|
|
635
|
+
* ```
|
|
636
|
+
*/
|
|
637
|
+
declare function setUIMessageStreamOptions(options: ChatUIMessageStreamOptions): void;
|
|
573
638
|
/**
|
|
574
639
|
* Check whether the user stopped generation during the current turn.
|
|
575
640
|
*
|
|
@@ -635,6 +700,172 @@ declare function chatDefer(promise: Promise<unknown>): void;
|
|
|
635
700
|
* ```
|
|
636
701
|
*/
|
|
637
702
|
declare function cleanupAbortedParts(message: UIMessage): UIMessage;
|
|
703
|
+
/**
|
|
704
|
+
* Create a managed stop signal wired to the chat stop input stream.
|
|
705
|
+
*
|
|
706
|
+
* Call once at the start of your run. Use `signal` as the abort signal for
|
|
707
|
+
* `streamText`. Call `reset()` at the start of each turn to get a fresh
|
|
708
|
+
* per-turn signal. Call `cleanup()` when the run ends.
|
|
709
|
+
*
|
|
710
|
+
* @example
|
|
711
|
+
* ```ts
|
|
712
|
+
* const stop = chat.createStopSignal();
|
|
713
|
+
* for (let turn = 0; turn < 100; turn++) {
|
|
714
|
+
* stop.reset();
|
|
715
|
+
* const result = streamText({ model, messages, abortSignal: stop.signal });
|
|
716
|
+
* await chat.pipe(result);
|
|
717
|
+
* // ...
|
|
718
|
+
* }
|
|
719
|
+
* stop.cleanup();
|
|
720
|
+
* ```
|
|
721
|
+
*/
|
|
722
|
+
declare function createStopSignal(): {
|
|
723
|
+
readonly signal: AbortSignal;
|
|
724
|
+
reset: () => void;
|
|
725
|
+
cleanup: () => void;
|
|
726
|
+
};
|
|
727
|
+
/**
|
|
728
|
+
* Signal the frontend that the current turn is complete.
|
|
729
|
+
*
|
|
730
|
+
* The `TriggerChatTransport` intercepts this to close the ReadableStream
|
|
731
|
+
* for the current turn. Call after piping the response stream.
|
|
732
|
+
*
|
|
733
|
+
* @example
|
|
734
|
+
* ```ts
|
|
735
|
+
* await chat.pipe(result);
|
|
736
|
+
* await chat.writeTurnComplete();
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
739
|
+
declare function chatWriteTurnComplete(options?: {
|
|
740
|
+
publicAccessToken?: string;
|
|
741
|
+
}): Promise<void>;
|
|
742
|
+
/**
|
|
743
|
+
* Pipe a `StreamTextResult` (or similar) to the chat stream and capture
|
|
744
|
+
* the assistant's response message via `onFinish`.
|
|
745
|
+
*
|
|
746
|
+
* Combines `toUIMessageStream()` + `onFinish` callback + `chat.pipe()`.
|
|
747
|
+
* Returns the captured `UIMessage`, or `undefined` if capture failed.
|
|
748
|
+
*
|
|
749
|
+
* @example
|
|
750
|
+
* ```ts
|
|
751
|
+
* const result = streamText({ model, messages, abortSignal: signal });
|
|
752
|
+
* const response = await chat.pipeAndCapture(result, { signal });
|
|
753
|
+
* if (response) conversation.addResponse(response);
|
|
754
|
+
* ```
|
|
755
|
+
*/
|
|
756
|
+
declare function pipeChatAndCapture(source: UIMessageStreamable, options?: {
|
|
757
|
+
signal?: AbortSignal;
|
|
758
|
+
spanName?: string;
|
|
759
|
+
}): Promise<UIMessage | undefined>;
|
|
760
|
+
/**
|
|
761
|
+
* Accumulates conversation messages across turns.
|
|
762
|
+
*
|
|
763
|
+
* Handles the transport protocol: turn 0 sends full history (replace),
|
|
764
|
+
* subsequent turns send only new messages (append), regenerate sends
|
|
765
|
+
* full history minus last assistant message (replace).
|
|
766
|
+
*
|
|
767
|
+
* @example
|
|
768
|
+
* ```ts
|
|
769
|
+
* const conversation = new chat.MessageAccumulator();
|
|
770
|
+
* for (let turn = 0; turn < 100; turn++) {
|
|
771
|
+
* const messages = await conversation.addIncoming(payload.messages, payload.trigger, turn);
|
|
772
|
+
* const result = streamText({ model, messages });
|
|
773
|
+
* const response = await chat.pipeAndCapture(result);
|
|
774
|
+
* if (response) await conversation.addResponse(response);
|
|
775
|
+
* }
|
|
776
|
+
* ```
|
|
777
|
+
*/
|
|
778
|
+
declare class ChatMessageAccumulator {
|
|
779
|
+
modelMessages: ModelMessage[];
|
|
780
|
+
uiMessages: UIMessage[];
|
|
781
|
+
/**
|
|
782
|
+
* Add incoming messages from the transport payload.
|
|
783
|
+
* Returns the full accumulated model messages for `streamText`.
|
|
784
|
+
*/
|
|
785
|
+
addIncoming(messages: UIMessage[], trigger: string, turn: number): Promise<ModelMessage[]>;
|
|
786
|
+
/**
|
|
787
|
+
* Add the assistant's response to the accumulator.
|
|
788
|
+
* Call after `pipeAndCapture` with the captured response.
|
|
789
|
+
*/
|
|
790
|
+
addResponse(response: UIMessage): Promise<void>;
|
|
791
|
+
}
|
|
792
|
+
export type ChatSessionOptions = {
|
|
793
|
+
/** Run-level cancel signal (from task context). */
|
|
794
|
+
signal: AbortSignal;
|
|
795
|
+
/** Seconds to stay warm between turns before suspending. @default 30 */
|
|
796
|
+
warmTimeoutInSeconds?: number;
|
|
797
|
+
/** Duration string for suspend timeout. @default "1h" */
|
|
798
|
+
timeout?: string;
|
|
799
|
+
/** Max turns before ending. @default 100 */
|
|
800
|
+
maxTurns?: number;
|
|
801
|
+
};
|
|
802
|
+
export type ChatTurn = {
|
|
803
|
+
/** Turn number (0-indexed). */
|
|
804
|
+
number: number;
|
|
805
|
+
/** Chat session ID. */
|
|
806
|
+
chatId: string;
|
|
807
|
+
/** What triggered this turn. */
|
|
808
|
+
trigger: string;
|
|
809
|
+
/** Client data from the transport (`metadata` field on the wire payload). */
|
|
810
|
+
clientData: unknown;
|
|
811
|
+
/** Full accumulated model messages — pass directly to `streamText`. */
|
|
812
|
+
messages: ModelMessage[];
|
|
813
|
+
/** Full accumulated UI messages — use for persistence. */
|
|
814
|
+
uiMessages: UIMessage[];
|
|
815
|
+
/** Combined stop+cancel AbortSignal (fresh each turn). */
|
|
816
|
+
signal: AbortSignal;
|
|
817
|
+
/** Whether the user stopped generation this turn. */
|
|
818
|
+
readonly stopped: boolean;
|
|
819
|
+
/** Whether this is a continuation run. */
|
|
820
|
+
continuation: boolean;
|
|
821
|
+
/**
|
|
822
|
+
* Easy path: pipe stream, capture response, accumulate it,
|
|
823
|
+
* clean up aborted parts if stopped, and write turn-complete chunk.
|
|
824
|
+
*/
|
|
825
|
+
complete(source: UIMessageStreamable): Promise<UIMessage | undefined>;
|
|
826
|
+
/**
|
|
827
|
+
* Manual path: just write turn-complete chunk.
|
|
828
|
+
* Use when you've already piped and accumulated manually.
|
|
829
|
+
*/
|
|
830
|
+
done(): Promise<void>;
|
|
831
|
+
/**
|
|
832
|
+
* Add the response to the accumulator manually.
|
|
833
|
+
* Use with `chat.pipeAndCapture` when you need control between pipe and done.
|
|
834
|
+
*/
|
|
835
|
+
addResponse(response: UIMessage): Promise<void>;
|
|
836
|
+
};
|
|
837
|
+
/**
|
|
838
|
+
* Create a chat session that yields turns as an async iterator.
|
|
839
|
+
*
|
|
840
|
+
* Handles: preload wait, stop signals, message accumulation, turn-complete
|
|
841
|
+
* signaling, and warm/suspend between turns. You control: initialization,
|
|
842
|
+
* model/tool selection, persistence, and any custom per-turn logic.
|
|
843
|
+
*
|
|
844
|
+
* @example
|
|
845
|
+
* ```ts
|
|
846
|
+
* import { task } from "@trigger.dev/sdk";
|
|
847
|
+
* import { chat, type ChatTaskWirePayload } from "@trigger.dev/sdk/ai";
|
|
848
|
+
* import { streamText } from "ai";
|
|
849
|
+
* import { openai } from "@ai-sdk/openai";
|
|
850
|
+
*
|
|
851
|
+
* export const myChat = task({
|
|
852
|
+
* id: "my-chat",
|
|
853
|
+
* run: async (payload: ChatTaskWirePayload, { signal }) => {
|
|
854
|
+
* const session = chat.createSession(payload, { signal });
|
|
855
|
+
*
|
|
856
|
+
* for await (const turn of session) {
|
|
857
|
+
* const result = streamText({
|
|
858
|
+
* model: openai("gpt-4o"),
|
|
859
|
+
* messages: turn.messages,
|
|
860
|
+
* abortSignal: turn.signal,
|
|
861
|
+
* });
|
|
862
|
+
* await turn.complete(result);
|
|
863
|
+
* }
|
|
864
|
+
* },
|
|
865
|
+
* });
|
|
866
|
+
* ```
|
|
867
|
+
*/
|
|
868
|
+
declare function createChatSession(payload: ChatTaskWirePayload, options: ChatSessionOptions): AsyncIterable<ChatTurn>;
|
|
638
869
|
/**
|
|
639
870
|
* A Proxy-backed, run-scoped data object that appears as `T` to users.
|
|
640
871
|
* Includes helper methods for initialization, dirty tracking, and serialization.
|
|
@@ -724,6 +955,8 @@ export declare const chat: {
|
|
|
724
955
|
setTurnTimeoutInSeconds: typeof setTurnTimeoutInSeconds;
|
|
725
956
|
/** Override the warm timeout at runtime. See {@link setWarmTimeoutInSeconds}. */
|
|
726
957
|
setWarmTimeoutInSeconds: typeof setWarmTimeoutInSeconds;
|
|
958
|
+
/** Override toUIMessageStream() options for the current turn. See {@link setUIMessageStreamOptions}. */
|
|
959
|
+
setUIMessageStreamOptions: typeof setUIMessageStreamOptions;
|
|
727
960
|
/** Check if the current turn was stopped by the user. See {@link isStopped}. */
|
|
728
961
|
isStopped: typeof isStopped;
|
|
729
962
|
/** Clean up aborted parts from a UIMessage. See {@link cleanupAbortedParts}. */
|
|
@@ -732,4 +965,16 @@ export declare const chat: {
|
|
|
732
965
|
defer: typeof chatDefer;
|
|
733
966
|
/** Typed chat output stream for writing custom chunks or piping from subtasks. */
|
|
734
967
|
stream: import("@trigger.dev/core/v3").RealtimeDefinedStream<UIMessageChunk>;
|
|
968
|
+
/** Pre-built input stream for receiving messages from the transport. */
|
|
969
|
+
messages: import("@trigger.dev/core/v3").RealtimeDefinedInputStream<ChatTaskWirePayload<UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>, unknown>>;
|
|
970
|
+
/** Create a managed stop signal wired to the stop input stream. See {@link createStopSignal}. */
|
|
971
|
+
createStopSignal: typeof createStopSignal;
|
|
972
|
+
/** Signal the frontend that the current turn is complete. See {@link chatWriteTurnComplete}. */
|
|
973
|
+
writeTurnComplete: typeof chatWriteTurnComplete;
|
|
974
|
+
/** Pipe a stream and capture the response message. See {@link pipeChatAndCapture}. */
|
|
975
|
+
pipeAndCapture: typeof pipeChatAndCapture;
|
|
976
|
+
/** Message accumulator class for raw task chat. See {@link ChatMessageAccumulator}. */
|
|
977
|
+
MessageAccumulator: typeof ChatMessageAccumulator;
|
|
978
|
+
/** Create a chat session (async iterator). See {@link createChatSession}. */
|
|
979
|
+
createSession: typeof createChatSession;
|
|
735
980
|
};
|