@trigger.dev/sdk 0.0.0-chat-prerelease-20260414181032 → 0.0.0-chat-prerelease-20260415152704
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 +135 -4
- package/dist/commonjs/v3/ai.js +300 -80
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat-client.d.ts +23 -0
- package/dist/commonjs/v3/chat-client.js +43 -0
- package/dist/commonjs/v3/chat-client.js.map +1 -1
- package/dist/commonjs/v3/chat.d.ts +16 -0
- package/dist/commonjs/v3/chat.js +49 -0
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +135 -4
- package/dist/esm/v3/ai.js +300 -80
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat-client.d.ts +23 -0
- package/dist/esm/v3/chat-client.js +43 -0
- package/dist/esm/v3/chat-client.js.map +1 -1
- package/dist/esm/v3/chat.d.ts +16 -0
- package/dist/esm/v3/chat.js +49 -0
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +2 -2
package/dist/commonjs/v3/ai.d.ts
CHANGED
|
@@ -170,9 +170,11 @@ export type ChatWriter = {
|
|
|
170
170
|
export type ChatTaskWirePayload<TMessage extends UIMessage = UIMessage, TMetadata = unknown> = {
|
|
171
171
|
messages: TMessage[];
|
|
172
172
|
chatId: string;
|
|
173
|
-
trigger: "submit-message" | "regenerate-message" | "preload" | "close";
|
|
173
|
+
trigger: "submit-message" | "regenerate-message" | "preload" | "close" | "action";
|
|
174
174
|
messageId?: string;
|
|
175
175
|
metadata?: TMetadata;
|
|
176
|
+
/** Custom action payload when `trigger` is `"action"`. Validated against `actionSchema` on the backend. */
|
|
177
|
+
action?: unknown;
|
|
176
178
|
/** Whether this run is continuing an existing chat whose previous run ended. */
|
|
177
179
|
continuation?: boolean;
|
|
178
180
|
/** The run ID of the previous run (only set when `continuation` is true). */
|
|
@@ -838,6 +840,27 @@ export type ChatStartEvent<TClientData = unknown> = {
|
|
|
838
840
|
/** Stream writer — write custom `UIMessageChunk` parts to the chat stream. Lazy: no overhead if unused. */
|
|
839
841
|
writer: ChatWriter;
|
|
840
842
|
};
|
|
843
|
+
/**
|
|
844
|
+
* Event passed to the `hydrateMessages` callback.
|
|
845
|
+
*/
|
|
846
|
+
export type HydrateMessagesEvent<TClientData = unknown, TUIM extends UIMessage = UIMessage> = {
|
|
847
|
+
/** The unique identifier for the chat session. */
|
|
848
|
+
chatId: string;
|
|
849
|
+
/** The turn number (0-indexed). */
|
|
850
|
+
turn: number;
|
|
851
|
+
/** The trigger type for this turn. */
|
|
852
|
+
trigger: "submit-message" | "regenerate-message" | "action";
|
|
853
|
+
/** Validated incoming UI messages from the wire payload (what the frontend sent). Empty for actions. */
|
|
854
|
+
incomingMessages: TUIM[];
|
|
855
|
+
/** The accumulated UI messages before this turn (empty on turn 0). */
|
|
856
|
+
previousMessages: TUIM[];
|
|
857
|
+
/** Parsed client data from the transport metadata. */
|
|
858
|
+
clientData?: TClientData;
|
|
859
|
+
/** Whether this run is continuing from a previous run. */
|
|
860
|
+
continuation: boolean;
|
|
861
|
+
/** The ID of the previous run (if continuation). */
|
|
862
|
+
previousRunId?: string;
|
|
863
|
+
};
|
|
841
864
|
/**
|
|
842
865
|
* Event passed to the `onValidateMessages` callback.
|
|
843
866
|
*/
|
|
@@ -851,6 +874,23 @@ export type ValidateMessagesEvent<TUIM extends UIMessage = UIMessage> = {
|
|
|
851
874
|
/** The trigger type for this turn. */
|
|
852
875
|
trigger: "submit-message" | "regenerate-message" | "preload" | "close";
|
|
853
876
|
};
|
|
877
|
+
/**
|
|
878
|
+
* Event passed to the `onAction` callback.
|
|
879
|
+
*/
|
|
880
|
+
export type ActionEvent<TAction = unknown, TClientData = unknown, TUIM extends UIMessage = UIMessage> = {
|
|
881
|
+
/** The parsed and validated action payload. */
|
|
882
|
+
action: TAction;
|
|
883
|
+
/** The unique identifier for the chat session. */
|
|
884
|
+
chatId: string;
|
|
885
|
+
/** The turn number (0-indexed). */
|
|
886
|
+
turn: number;
|
|
887
|
+
/** Parsed client data from the transport metadata. */
|
|
888
|
+
clientData?: TClientData;
|
|
889
|
+
/** The accumulated UI messages (after hydration, if set). */
|
|
890
|
+
uiMessages: TUIM[];
|
|
891
|
+
/** The accumulated model messages (after hydration, if set). */
|
|
892
|
+
messages: ModelMessage[];
|
|
893
|
+
};
|
|
854
894
|
/**
|
|
855
895
|
* Event passed to the `onTurnStart` callback.
|
|
856
896
|
*/
|
|
@@ -1014,7 +1054,7 @@ export type ChatResumeEvent<TClientData = unknown, TUIM extends UIMessage = UIMe
|
|
|
1014
1054
|
/** Custom data from the frontend. */
|
|
1015
1055
|
clientData?: TClientData;
|
|
1016
1056
|
};
|
|
1017
|
-
export type ChatAgentOptions<TIdentifier extends string, TClientDataSchema extends TaskSchema | undefined = undefined, TUIMessage extends UIMessage = UIMessage> = Omit<TaskOptions<TIdentifier, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>, "run"> & {
|
|
1057
|
+
export type ChatAgentOptions<TIdentifier extends string, TClientDataSchema extends TaskSchema | undefined = undefined, TUIMessage extends UIMessage = UIMessage, TActionSchema extends TaskSchema | undefined = undefined> = Omit<TaskOptions<TIdentifier, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>, "run"> & {
|
|
1018
1058
|
/**
|
|
1019
1059
|
* Schema for validating `clientData` from the frontend.
|
|
1020
1060
|
* Accepts Zod, ArkType, Valibot, or any supported schema library.
|
|
@@ -1035,6 +1075,42 @@ export type ChatAgentOptions<TIdentifier extends string, TClientDataSchema exten
|
|
|
1035
1075
|
* ```
|
|
1036
1076
|
*/
|
|
1037
1077
|
clientDataSchema?: TClientDataSchema;
|
|
1078
|
+
/**
|
|
1079
|
+
* Schema for validating custom actions sent via `transport.sendAction()`.
|
|
1080
|
+
*
|
|
1081
|
+
* When the frontend sends `trigger: "action"`, the `action` payload is
|
|
1082
|
+
* parsed against this schema before reaching `onAction`. Invalid actions
|
|
1083
|
+
* throw and abort the turn.
|
|
1084
|
+
*
|
|
1085
|
+
* @example
|
|
1086
|
+
* ```ts
|
|
1087
|
+
* import { z } from "zod";
|
|
1088
|
+
*
|
|
1089
|
+
* chat.agent({
|
|
1090
|
+
* id: "my-chat",
|
|
1091
|
+
* actionSchema: z.discriminatedUnion("type", [
|
|
1092
|
+
* z.object({ type: z.literal("undo") }),
|
|
1093
|
+
* z.object({ type: z.literal("rollback"), targetMessageId: z.string() }),
|
|
1094
|
+
* ]),
|
|
1095
|
+
* onAction: async ({ action }) => {
|
|
1096
|
+
* if (action.type === "undo") chat.history.slice(0, -2);
|
|
1097
|
+
* if (action.type === "rollback") chat.history.rollbackTo(action.targetMessageId);
|
|
1098
|
+
* },
|
|
1099
|
+
* run: async ({ messages, signal }) => { ... },
|
|
1100
|
+
* });
|
|
1101
|
+
* ```
|
|
1102
|
+
*/
|
|
1103
|
+
actionSchema?: TActionSchema;
|
|
1104
|
+
/**
|
|
1105
|
+
* Called when the frontend sends a custom action via `transport.sendAction()`.
|
|
1106
|
+
*
|
|
1107
|
+
* Fires after message hydration (if set) but before `onTurnStart` and `run()`.
|
|
1108
|
+
* Use `chat.history.*` to modify the conversation state — the LLM will respond
|
|
1109
|
+
* to the modified state.
|
|
1110
|
+
*/
|
|
1111
|
+
onAction?: (event: ActionEvent<[
|
|
1112
|
+
TActionSchema
|
|
1113
|
+
] extends [TaskSchema] ? inferSchemaOut<TActionSchema> : unknown, inferSchemaOut<TClientDataSchema>, TUIMessage>) => Promise<void> | void;
|
|
1038
1114
|
/**
|
|
1039
1115
|
* The run function for the chat task.
|
|
1040
1116
|
*
|
|
@@ -1100,6 +1176,42 @@ export type ChatAgentOptions<TIdentifier extends string, TClientDataSchema exten
|
|
|
1100
1176
|
* ```
|
|
1101
1177
|
*/
|
|
1102
1178
|
onValidateMessages?: (event: ValidateMessagesEvent<TUIMessage>) => TUIMessage[] | Promise<TUIMessage[]>;
|
|
1179
|
+
/**
|
|
1180
|
+
* Load the full message history from your backend on every turn,
|
|
1181
|
+
* replacing the built-in linear accumulator.
|
|
1182
|
+
*
|
|
1183
|
+
* When set, the returned messages become the accumulated state for this turn.
|
|
1184
|
+
* The normal accumulation logic (append for submit, replace for regenerate)
|
|
1185
|
+
* is skipped entirely — the hook is the source of truth.
|
|
1186
|
+
*
|
|
1187
|
+
* After the hook returns, any incoming wire messages with matching IDs are
|
|
1188
|
+
* auto-merged (handles tool approval responses transparently).
|
|
1189
|
+
*
|
|
1190
|
+
* Use cases:
|
|
1191
|
+
* - Backend trust: prevent clients from injecting fabricated history
|
|
1192
|
+
* - Branching conversations (DAGs): load only the active branch
|
|
1193
|
+
* - Rollback/undo: exclude undone messages from history
|
|
1194
|
+
*
|
|
1195
|
+
* @example
|
|
1196
|
+
* ```ts
|
|
1197
|
+
* chat.agent({
|
|
1198
|
+
* id: "my-chat",
|
|
1199
|
+
* hydrateMessages: async ({ chatId, trigger, incomingMessages }) => {
|
|
1200
|
+
* // Persist the new message
|
|
1201
|
+
* const newMsg = incomingMessages[incomingMessages.length - 1];
|
|
1202
|
+
* if (newMsg && trigger === "submit-message") {
|
|
1203
|
+
* await db.chatMessages.create({ chatId, message: newMsg });
|
|
1204
|
+
* }
|
|
1205
|
+
* // Return the full authoritative history
|
|
1206
|
+
* return db.chatMessages.findMany({ where: { chatId } });
|
|
1207
|
+
* },
|
|
1208
|
+
* run: async ({ messages, signal }) => {
|
|
1209
|
+
* return streamText({ model: openai("gpt-4o"), messages, abortSignal: signal });
|
|
1210
|
+
* },
|
|
1211
|
+
* });
|
|
1212
|
+
* ```
|
|
1213
|
+
*/
|
|
1214
|
+
hydrateMessages?: (event: HydrateMessagesEvent<inferSchemaOut<TClientDataSchema>, TUIMessage>) => TUIMessage[] | Promise<TUIMessage[]>;
|
|
1103
1215
|
/**
|
|
1104
1216
|
* Called at the start of every turn, after message accumulation and `onChatStart` (turn 0),
|
|
1105
1217
|
* but before the `run` function executes.
|
|
@@ -1394,7 +1506,7 @@ type ChatCustomAgentOptions<TIdentifier extends string, TClientDataSchema extend
|
|
|
1394
1506
|
clientDataSchema?: TClientDataSchema;
|
|
1395
1507
|
};
|
|
1396
1508
|
declare function chatCustomAgent<TIdentifier extends string, TClientDataSchema extends TaskSchema | undefined = undefined, TUIMessage extends UIMessage = UIMessage>(options: ChatCustomAgentOptions<TIdentifier, TClientDataSchema, TUIMessage>): Task<TIdentifier, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>;
|
|
1397
|
-
declare function chatAgent<TIdentifier extends string, TClientDataSchema extends TaskSchema | undefined = undefined, TUIMessage extends UIMessage = UIMessage>(options: ChatAgentOptions<TIdentifier, TClientDataSchema, TUIMessage>): Task<TIdentifier, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>;
|
|
1509
|
+
declare function chatAgent<TIdentifier extends string, TClientDataSchema extends TaskSchema | undefined = undefined, TUIMessage extends UIMessage = UIMessage, TActionSchema extends TaskSchema | undefined = undefined>(options: ChatAgentOptions<TIdentifier, TClientDataSchema, TUIMessage, TActionSchema>): Task<TIdentifier, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>;
|
|
1398
1510
|
/**
|
|
1399
1511
|
* Optional config for {@link chat.withUIMessage}. `streamOptions` become default
|
|
1400
1512
|
* static `toUIMessageStream()` settings; inner `chat.agent({ uiMessageStreamOptions })`
|
|
@@ -1451,7 +1563,7 @@ export interface ChatBuilder<TUIMessage extends UIMessage = UIMessage, TClientDa
|
|
|
1451
1563
|
* and omitted from options. Otherwise, it can still be set directly in options
|
|
1452
1564
|
* (backwards compatible).
|
|
1453
1565
|
*/
|
|
1454
|
-
agent: [TClientDataSchema] extends [undefined] ? <TId extends string, TInfer extends TaskSchema | undefined = undefined>(options: ChatAgentOptions<TId, TInfer, TUIMessage>) => Task<TId, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TInfer>>, unknown> : <TId extends string>(options: Omit<ChatAgentOptions<TId, TClientDataSchema, TUIMessage>, "clientDataSchema">) => Task<TId, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>;
|
|
1566
|
+
agent: [TClientDataSchema] extends [undefined] ? <TId extends string, TInfer extends TaskSchema | undefined = undefined, TAction extends TaskSchema | undefined = undefined>(options: ChatAgentOptions<TId, TInfer, TUIMessage, TAction>) => Task<TId, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TInfer>>, unknown> : <TId extends string, TAction extends TaskSchema | undefined = undefined>(options: Omit<ChatAgentOptions<TId, TClientDataSchema, TUIMessage, TAction>, "clientDataSchema">) => Task<TId, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>;
|
|
1455
1567
|
/**
|
|
1456
1568
|
* Create a custom agent with manual lifecycle control.
|
|
1457
1569
|
*
|
|
@@ -2145,6 +2257,25 @@ export declare const chat: {
|
|
|
2145
2257
|
* converts to `ModelMessage[]` internally.
|
|
2146
2258
|
*/
|
|
2147
2259
|
setMessages: typeof setChatMessages;
|
|
2260
|
+
/**
|
|
2261
|
+
* Imperative API for modifying the accumulated message history.
|
|
2262
|
+
* Supports rollback, remove, replace, slice, and full replacement.
|
|
2263
|
+
* Can be called from any hook or `run()`.
|
|
2264
|
+
*/
|
|
2265
|
+
history: {
|
|
2266
|
+
/** Read the current accumulated UI messages (copy). */
|
|
2267
|
+
all(): UIMessage[];
|
|
2268
|
+
/** Replace all accumulated messages. Same as `chat.setMessages()`. */
|
|
2269
|
+
set(messages: UIMessage[]): void;
|
|
2270
|
+
/** Remove a specific message by ID. */
|
|
2271
|
+
remove(messageId: string): void;
|
|
2272
|
+
/** Keep messages up to and including the given ID (undo/rollback). */
|
|
2273
|
+
rollbackTo(messageId: string): void;
|
|
2274
|
+
/** Replace a specific message by ID (edit). */
|
|
2275
|
+
replace(messageId: string, message: UIMessage): void;
|
|
2276
|
+
/** Keep only messages in the given range. */
|
|
2277
|
+
slice(start: number, end?: number): void;
|
|
2278
|
+
};
|
|
2148
2279
|
/** Check if it's safe to compact messages (no in-flight tool calls). */
|
|
2149
2280
|
isCompactionSafe: typeof isCompactionSafe;
|
|
2150
2281
|
/** Returns a `prepareStep` function that handles context compaction automatically. */
|