@trigger.dev/sdk 0.0.0-prerelease-20260908122921 → 0.0.0-prerelease-20260909071038

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.
Files changed (76) hide show
  1. package/dist/commonjs/v3/ai-shared.d.ts +15 -0
  2. package/dist/commonjs/v3/ai-shared.js +35 -1
  3. package/dist/commonjs/v3/ai-shared.js.map +1 -1
  4. package/dist/commonjs/v3/ai.d.ts +76 -9
  5. package/dist/commonjs/v3/ai.js +316 -222
  6. package/dist/commonjs/v3/ai.js.map +1 -1
  7. package/dist/commonjs/v3/chat-client.d.ts +8 -0
  8. package/dist/commonjs/v3/chat-client.js +199 -109
  9. package/dist/commonjs/v3/chat-client.js.map +1 -1
  10. package/dist/commonjs/v3/chat-react.d.ts +54 -0
  11. package/dist/commonjs/v3/chat-react.js +83 -0
  12. package/dist/commonjs/v3/chat-react.js.map +1 -1
  13. package/dist/commonjs/v3/chat.d.ts +11 -0
  14. package/dist/commonjs/v3/chat.js +36 -27
  15. package/dist/commonjs/v3/chat.js.map +1 -1
  16. package/dist/commonjs/v3/chatSnapshotIo.d.ts +2 -0
  17. package/dist/commonjs/v3/chatSnapshotIo.js +174 -0
  18. package/dist/commonjs/v3/chatSnapshotIo.js.map +1 -0
  19. package/dist/commonjs/v3/test/index.d.ts +1 -0
  20. package/dist/commonjs/v3/test/index.js +3 -1
  21. package/dist/commonjs/v3/test/index.js.map +1 -1
  22. package/dist/commonjs/v3/test/mock-chat-agent.d.ts +4 -4
  23. package/dist/commonjs/v3/test/mock-chat-agent.js +25 -4
  24. package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
  25. package/dist/commonjs/v3/test/transcript-storage-tests.d.ts +40 -0
  26. package/dist/commonjs/v3/test/transcript-storage-tests.js +258 -0
  27. package/dist/commonjs/v3/test/transcript-storage-tests.js.map +1 -0
  28. package/dist/commonjs/v3/transcriptStorage.d.ts +229 -0
  29. package/dist/commonjs/v3/transcriptStorage.js +321 -0
  30. package/dist/commonjs/v3/transcriptStorage.js.map +1 -0
  31. package/dist/commonjs/version.js +1 -1
  32. package/dist/esm/v3/ai-shared.d.ts +15 -0
  33. package/dist/esm/v3/ai-shared.js +33 -0
  34. package/dist/esm/v3/ai-shared.js.map +1 -1
  35. package/dist/esm/v3/ai.d.ts +76 -9
  36. package/dist/esm/v3/ai.js +306 -217
  37. package/dist/esm/v3/ai.js.map +1 -1
  38. package/dist/esm/v3/chat-client.d.ts +8 -0
  39. package/dist/esm/v3/chat-client.js +200 -110
  40. package/dist/esm/v3/chat-client.js.map +1 -1
  41. package/dist/esm/v3/chat-react.d.ts +54 -0
  42. package/dist/esm/v3/chat-react.js +81 -0
  43. package/dist/esm/v3/chat-react.js.map +1 -1
  44. package/dist/esm/v3/chat.d.ts +11 -0
  45. package/dist/esm/v3/chat.js +36 -27
  46. package/dist/esm/v3/chat.js.map +1 -1
  47. package/dist/esm/v3/chatSnapshotIo.d.ts +2 -0
  48. package/dist/esm/v3/chatSnapshotIo.js +166 -0
  49. package/dist/esm/v3/chatSnapshotIo.js.map +1 -0
  50. package/dist/esm/v3/test/index.d.ts +1 -0
  51. package/dist/esm/v3/test/index.js +1 -0
  52. package/dist/esm/v3/test/index.js.map +1 -1
  53. package/dist/esm/v3/test/mock-chat-agent.d.ts +4 -4
  54. package/dist/esm/v3/test/mock-chat-agent.js +25 -4
  55. package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
  56. package/dist/esm/v3/test/transcript-storage-tests.d.ts +40 -0
  57. package/dist/esm/v3/test/transcript-storage-tests.js +255 -0
  58. package/dist/esm/v3/test/transcript-storage-tests.js.map +1 -0
  59. package/dist/esm/v3/transcriptStorage.d.ts +229 -0
  60. package/dist/esm/v3/transcriptStorage.js +309 -0
  61. package/dist/esm/v3/transcriptStorage.js.map +1 -0
  62. package/dist/esm/version.js +1 -1
  63. package/docs/ai-chat/actions.mdx +2 -2
  64. package/docs/ai-chat/background-injection.mdx +2 -0
  65. package/docs/ai-chat/compaction.mdx +2 -0
  66. package/docs/ai-chat/frontend.mdx +2 -0
  67. package/docs/ai-chat/lifecycle-hooks.mdx +6 -2
  68. package/docs/ai-chat/patterns/database-persistence.mdx +6 -2
  69. package/docs/ai-chat/patterns/persistence-and-replay.mdx +34 -23
  70. package/docs/ai-chat/reference.mdx +56 -5
  71. package/docs/ai-chat/transcript-storage.mdx +233 -0
  72. package/docs/config/extensions/syncEnvVars.mdx +6 -0
  73. package/docs/deploy-environment-variables.mdx +23 -2
  74. package/package.json +2 -2
  75. package/skills/trigger-authoring-chat-agent/SKILL.md +3 -2
  76. package/skills/trigger-chat-agent-advanced/SKILL.md +5 -5
@@ -1,5 +1,6 @@
1
1
  import { type AnyTask, type Task, type ApiClientConfiguration, type inferSchemaIn, type inferSchemaOut, type MachinePresetName, type RealtimeDefinedInputStream, type RealtimeDefinedStream, type TaskIdentifier, type TaskOptions, type TaskRunContext, type TaskSchema, type TaskWithSchema, type AnySessionChannel, type SessionChannelName } from "@trigger.dev/core/v3";
2
2
  import type { streamText as aiStreamTextSignature, FinishReason, LanguageModelUsage, ModelMessage, SystemModelMessage, ProviderMetadata, Tool, ToolSet, UIMessage, UIMessageChunk, UIMessageStreamOptions } from "ai";
3
+ import { type TranscriptLoadResult, type TranscriptStorage } from "./transcriptStorage.js";
3
4
  import { type ChatInputChunk, type ChatTaskWirePayload, type InferChatClientData, type InferChatUIMessage, type InferChatUIMessageFromTools, PENDING_MESSAGE_INJECTED_TYPE, upsertIncomingMessage } from "./ai-shared.js";
4
5
  import { locals } from "./locals.js";
5
6
  import type { ResolvedPrompt } from "./prompt.js";
@@ -33,8 +34,8 @@ type ChatTurnContext<TClientData = unknown> = {
33
34
  continuation: boolean;
34
35
  clientData?: TClientData;
35
36
  };
36
- export declare function __setReadChatSnapshotImplForTests(impl: ReadChatSnapshotImpl | undefined): void;
37
- export declare function __setWriteChatSnapshotImplForTests(impl: WriteChatSnapshotImpl | undefined): void;
37
+ export { __readChatSnapshotProductionPathForTests, __setReadChatSnapshotImplForTests, __setWriteChatSnapshotImplForTests, __writeChatSnapshotProductionPathForTests, } from "./chatSnapshotIo.js";
38
+ export { defaultStorage, memoryTranscriptStorage, reduceTranscriptChanges, snapshotTranscriptStorage, type LoadContextEvent, type MemoryTranscriptStorage, type TranscriptChange, type TranscriptChangeReason, type TranscriptChangeset, type TranscriptCursors, type TranscriptLoadOptions, type TranscriptLoadResult, type TranscriptScope, type TranscriptState, type TranscriptStorage, type TranscriptStorageContext, } from "./transcriptStorage.js";
38
39
  type ReplaySessionOutTailImpl = <TUIMessage extends UIMessage>(sessionId: string, options?: {
39
40
  lastEventId?: string;
40
41
  }) => Promise<ReplaySessionOutTailResult<TUIMessage>>;
@@ -1162,8 +1163,9 @@ export type RecoveryPendingToolCall = {
1162
1163
  * `chat.endRun()` with no buffered user messages, fresh chat, OOM retry
1163
1164
  * after a successful turn-complete with no in-flight tail).
1164
1165
  *
1165
- * Does NOT fire when `hydrateMessages` is registered (the customer owns
1166
- * persistence; recovery decisions live in their own DB query).
1166
+ * Fires regardless of who owns the model's context. With `hydrateMessages`
1167
+ * or a storage `loadContext`, the recovered tail reaches that hook in
1168
+ * `previousMessages` on the next turn.
1167
1169
  */
1168
1170
  export type RecoveryBootEvent<TUIM extends UIMessage = UIMessage> = {
1169
1171
  /** Task run context — same as `task({ run })` second-argument `ctx`. */
@@ -1232,8 +1234,9 @@ export type RecoveryBootResult<TUIM extends UIMessage = UIMessage> = {
1232
1234
  * context, mutate its tool parts to inject synthesized results,
1233
1235
  * collapse history, etc.
1234
1236
  *
1235
- * Ignored when `hydrateMessages` is registered (the hydrate hook
1236
- * runs per-turn and overwrites the chain).
1237
+ * With `hydrateMessages` or a storage `loadContext`, this chain is what
1238
+ * the hook receives as `previousMessages` on the next turn; the hook's
1239
+ * return value is the chain the model sees.
1237
1240
  */
1238
1241
  chain?: TUIM[];
1239
1242
  /**
@@ -1768,9 +1771,9 @@ export type ChatAgentOptions<TIdentifier extends string, TClientDataSchema exten
1768
1771
  * continuation after `chat.endRun()` with no buffered user, a fresh
1769
1772
  * chat, or an OOM retry on top of a complete snapshot.
1770
1773
  *
1771
- * Does NOT fire when `hydrateMessages` is registered — that hook owns
1772
- * the per-turn chain and overlapping recovery decisions belong in the
1773
- * customer's DB.
1774
+ * Fires regardless of who owns the model's context; a `hydrateMessages`
1775
+ * hook or a storage `loadContext` receives the recovered tail in
1776
+ * `previousMessages` on the next turn.
1774
1777
  *
1775
1778
  * Defaults (returned when the hook is omitted or returns no field):
1776
1779
  * - With two or more in-flight users, the partial and the user it
@@ -1900,6 +1903,32 @@ export type ChatAgentOptions<TIdentifier extends string, TClientDataSchema exten
1900
1903
  * ```
1901
1904
  */
1902
1905
  hydrateMessages?: (event: HydrateMessagesEvent<inferSchemaOut<TClientDataSchema>, TUIMessage>) => TUIMessage[] | Promise<TUIMessage[]>;
1906
+ /**
1907
+ * Where the conversation is persisted. The runtime calls `save` after
1908
+ * every turn, failed turn and history-changing action with the changes
1909
+ * since the last save, and `load` once when a new run boots to continue
1910
+ * the conversation.
1911
+ *
1912
+ * Defaults to `defaultStorage`, the platform's snapshot in object storage
1913
+ * that the Sessions dashboard renders. Bring your own to write each change
1914
+ * to your database; `memoryTranscriptStorage()` is the reference
1915
+ * implementation and `runTranscriptStorageTests` from
1916
+ * `@trigger.dev/sdk/ai/test` checks yours against the contract.
1917
+ *
1918
+ * A storage with `loadContext` also owns the model's context on every
1919
+ * turn, which is what `hydrateMessages` did. The two cannot be combined.
1920
+ *
1921
+ * @example
1922
+ * ```ts
1923
+ * chat.agent({
1924
+ * id: "my-chat",
1925
+ * storage: myPostgresTranscriptStorage,
1926
+ * run: async ({ messages, signal, streamText }) =>
1927
+ * streamText({ model, messages, abortSignal: signal }),
1928
+ * });
1929
+ * ```
1930
+ */
1931
+ storage?: TranscriptStorage<inferSchemaOut<TClientDataSchema>>;
1903
1932
  /**
1904
1933
  * Called at the start of every turn, after message accumulation and `onChatStart` (turn 0),
1905
1934
  * but before the `run` function executes.
@@ -3274,6 +3303,42 @@ export type ChatStartSessionResult = {
3274
3303
  * ```
3275
3304
  */
3276
3305
  declare function createChatStartSessionAction<TChat extends AnyTask = AnyTask>(taskId: string, options?: CreateChatStartSessionActionOptions): (params: ChatStartSessionParams<TChat>) => Promise<ChatStartSessionResult>;
3306
+ export type CreateChatLoadTranscriptActionOptions = {
3307
+ /**
3308
+ * Scope the action to a specific API client configuration (secret key,
3309
+ * base URL) instead of the process-wide one. The default storage reads
3310
+ * through this client.
3311
+ */
3312
+ apiClient?: ApiClientConfiguration;
3313
+ /** Page size when the caller passes none. */
3314
+ limit?: number;
3315
+ };
3316
+ export type ChatLoadTranscriptParams<TClientData = unknown> = {
3317
+ chatId: string;
3318
+ clientData?: TClientData;
3319
+ limit?: number;
3320
+ before?: string;
3321
+ };
3322
+ /**
3323
+ * Creates a server-side helper that reads a conversation from a transcript
3324
+ * storage, for rendering history before the chat connects. Works the same
3325
+ * for every storage, the platform default included, so the browser never
3326
+ * reads a store directly and the secret key stays on the server.
3327
+ *
3328
+ * Wrap it in a Next.js server action (or any server-side handler), scope it
3329
+ * to the authenticated user through `clientData`, and pass the result to
3330
+ * `useLoadTranscript` in the browser.
3331
+ *
3332
+ * @example
3333
+ * ```ts
3334
+ * // actions.ts
3335
+ * "use server";
3336
+ * import { chat, defaultStorage } from "@trigger.dev/sdk/ai";
3337
+ *
3338
+ * export const loadTranscript = chat.createLoadTranscriptAction(defaultStorage, { limit: 50 });
3339
+ * ```
3340
+ */
3341
+ declare function createChatLoadTranscriptAction<TClientData = unknown>(storage: TranscriptStorage<TClientData>, options?: CreateChatLoadTranscriptActionOptions): (params: ChatLoadTranscriptParams<TClientData>) => Promise<TranscriptLoadResult>;
3277
3342
  export declare const chat: {
3278
3343
  /** Create a chat agent. See {@link chatAgent}. */
3279
3344
  agent: typeof chatAgent;
@@ -3285,6 +3350,8 @@ export declare const chat: {
3285
3350
  withClientData: typeof withClientData;
3286
3351
  /** Create a server-side helper for starting (or resuming) a Session for a chatId. See {@link createChatStartSessionAction}. */
3287
3352
  createStartSessionAction: typeof createChatStartSessionAction;
3353
+ /** Returns a server-side helper that reads a conversation from a transcript storage. */
3354
+ createLoadTranscriptAction: typeof createChatLoadTranscriptAction;
3288
3355
  /** Pipe a stream to the chat transport. See {@link pipeChat}. */
3289
3356
  pipe: typeof pipeChat;
3290
3357
  /** Return from `onAction` to run a turn on the edited history. See {@link chatTurn}. */