@trigger.dev/sdk 4.5.0-rc.2 → 4.5.0-rc.4

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.
@@ -15,7 +15,7 @@
15
15
  * import from `ai.ts`.
16
16
  */
17
17
  import type { Task, AnyTask } from "@trigger.dev/core/v3";
18
- import type { ModelMessage, UIMessage } from "ai";
18
+ import type { InferUITools, ModelMessage, ToolSet, UIDataTypes, UIMessage } from "ai";
19
19
  /**
20
20
  * Message-part `type` value for the pending-message data part the agent
21
21
  * injects when a follow-up message arrives mid-turn.
@@ -171,6 +171,21 @@ export type InferChatClientData<TTask extends AnyTask> = TTask extends Task<stri
171
171
  * ```
172
172
  */
173
173
  export type InferChatUIMessage<TTask extends AnyTask> = TTask extends Task<string, ChatTaskWirePayload<infer TUIM extends UIMessage, any>, any> ? TUIM : UIMessage;
174
+ /**
175
+ * Derive the chat `UIMessage` type for a given tool set. The tool-part types
176
+ * (`tool-${name}` with typed input/output) are inferred from the tools. Use
177
+ * this to declare the message type from your tools (e.g. to pass to
178
+ * `chat.withUIMessage<...>()` or to type the frontend) without hand-writing
179
+ * the `UIMessage<unknown, UIDataTypes, InferUITools<...>>` triple.
180
+ *
181
+ * @example
182
+ * ```ts
183
+ * import type { InferChatUIMessageFromTools } from "@trigger.dev/sdk/ai";
184
+ * const tools = { search, readFile };
185
+ * type ChatUiMessage = InferChatUIMessageFromTools<typeof tools>;
186
+ * ```
187
+ */
188
+ export type InferChatUIMessageFromTools<TTools extends ToolSet> = UIMessage<unknown, UIDataTypes, InferUITools<TTools>>;
174
189
  /**
175
190
  * Upsert an incoming wire message into the customer's DB-backed chain
176
191
  * inside a `hydrateMessages` hook. Returns `true` iff the chain was
@@ -1 +1 @@
1
- {"version":3,"file":"ai-shared.js","sourceRoot":"","sources":["../../../src/v3/ai-shared.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAgOH,sDAiBC;AAoDD,4DAuCC;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ire payload) with abort signals.
227
227
  */
228
- export type ChatTaskRunPayload<TClientData = unknown> = ChatTaskPayload<TClientData> & ChatTaskSignals & {
228
+ export type ChatTaskRunPayload<TClientData = unknown, TTools extends ToolSet = ToolSet> = ChatTaskPayload<TClientData> & ChatTaskSignals & {
229
229
  /**
230
230
  * Task run context — same object as the `ctx` passed to a standard `task({ run })` handler’s second argument.
231
231
  * Use for tags, metadata, parent run links, or any API that needs the full run record.
@@ -235,6 +235,21 @@ export type ChatTaskRunPayload<TClientData = unknown> = ChatTaskPayload<TClientD
235
235
  previousTurnUsage?: LanguageModelUsage;
236
236
  /** Cumulative token usage across all completed turns so far. */
237
237
  totalUsage: LanguageModelUsage;
238
+ /**
239
+ * The resolved tool set for this turn, the same `tools` you declared on
240
+ * `chat.agent({ tools })` (or the result of the per-turn `tools` function).
241
+ * Pass straight to `streamText({ tools })` so you don't redeclare them:
242
+ *
243
+ * ```ts
244
+ * run: ({ messages, tools, signal }) =>
245
+ * streamText({ model, messages, tools, abortSignal: signal })
246
+ * ```
247
+ *
248
+ * Declaring `tools` on the config is also what lets the SDK re-run each
249
+ * tool's `toModelOutput` when it re-converts prior-turn history (see the
250
+ * `tools` option on `chat.agent`). Empty object when no `tools` were declared.
251
+ */
252
+ tools: TTools;
238
253
  };
239
254
  /** Convenience re-export of the AI SDK's `LanguageModelUsage` type. */
240
255
  export type ChatTurnUsage = LanguageModelUsage;
@@ -462,6 +477,24 @@ export type PrepareMessagesEvent<TClientData = unknown> = {
462
477
  /** Custom data from the frontend. */
463
478
  clientData?: TClientData;
464
479
  };
480
+ /**
481
+ * Event passed to the per-turn `tools` function form on `chat.agent`.
482
+ *
483
+ * Use this when the active tool set depends on per-turn context (the user, a
484
+ * feature flag, etc.). Return the `ToolSet` to use for converting this turn's
485
+ * history. Only `inputSchema` and `toModelOutput` are read during conversion,
486
+ * so a lightweight map (no `execute`) is fine.
487
+ */
488
+ export type ResolveToolsEvent<TClientData = unknown> = {
489
+ /** The chat session ID. */
490
+ chatId: string;
491
+ /** The current turn number (0-indexed). */
492
+ turn: number;
493
+ /** Whether this run is continuing an existing chat. */
494
+ continuation: boolean;
495
+ /** Custom data from the frontend. */
496
+ clientData?: TClientData;
497
+ };
465
498
  /**
466
499
  * Data shape for `data-compaction` stream chunks emitted during compaction.
467
500
  * Use to type the `data` field when rendering compaction parts in the frontend.
@@ -1330,7 +1363,7 @@ export type ChatResumeEvent<TClientData = unknown, TUIM extends UIMessage = UIMe
1330
1363
  /** Custom data from the frontend. */
1331
1364
  clientData?: TClientData;
1332
1365
  };
1333
- 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" | "retry"> & {
1366
+ export type ChatAgentOptions<TIdentifier extends string, TClientDataSchema extends TaskSchema | undefined = undefined, TUIMessage extends UIMessage = UIMessage, TActionSchema extends TaskSchema | undefined = undefined, TTools extends ToolSet = ToolSet> = Omit<TaskOptions<TIdentifier, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>, "run" | "retry"> & {
1334
1367
  /**
1335
1368
  * Fallback machine preset to use when an attempt fails with an
1336
1369
  * out-of-memory (OOM) error. Setting this enables a single OOM retry:
@@ -1425,6 +1458,38 @@ export type ChatAgentOptions<TIdentifier extends string, TClientDataSchema exten
1425
1458
  onAction?: (event: ActionEvent<[
1426
1459
  TActionSchema
1427
1460
  ] extends [TaskSchema] ? inferSchemaOut<TActionSchema> : unknown, inferSchemaOut<TClientDataSchema>, TUIMessage>) => Promise<unknown> | unknown;
1461
+ /**
1462
+ * The tools available to this agent.
1463
+ *
1464
+ * `chat.agent` doesn't call the model for you. Your tools still go to
1465
+ * `streamText({ tools })` inside `run()`. Declaring them here additionally
1466
+ * lets the SDK re-run each tool's
1467
+ * [`toModelOutput`](https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#tomodeloutput)
1468
+ * when it re-converts persisted history on later turns. Without this, the
1469
+ * AI SDK has no `tools` to look up `toModelOutput` against, so a tool's
1470
+ * transformed result (e.g. raw image bytes → an image content part, or a
1471
+ * sub-agent summary) silently degrades to its raw JSON output from turn 2
1472
+ * onward.
1473
+ *
1474
+ * Only `inputSchema` and `toModelOutput` are read during conversion (never
1475
+ * `execute`), so you may pass a lightweight map if you keep heavy execute
1476
+ * deps out of this module.
1477
+ *
1478
+ * Pass either a static `ToolSet` or a function of per-turn context (for
1479
+ * tools that depend on the user, a feature flag, etc.). The resolved set is
1480
+ * available on the `run()` payload as `tools`.
1481
+ *
1482
+ * @example
1483
+ * ```ts
1484
+ * const tools = { read_file, search };
1485
+ * chat.agent({
1486
+ * tools,
1487
+ * run: async ({ messages, tools, signal }) =>
1488
+ * streamText({ model, messages, tools, abortSignal: signal }),
1489
+ * });
1490
+ * ```
1491
+ */
1492
+ tools?: TTools | ((event: ResolveToolsEvent<inferSchemaOut<TClientDataSchema>>) => TTools | Promise<TTools>);
1428
1493
  /**
1429
1494
  * The run function for the chat task.
1430
1495
  *
@@ -1435,7 +1500,7 @@ export type ChatAgentOptions<TIdentifier extends string, TClientDataSchema exten
1435
1500
  * **Auto-piping:** If this function returns a value with `.toUIMessageStream()`,
1436
1501
  * the stream is automatically piped to the frontend.
1437
1502
  */
1438
- run: (payload: ChatTaskRunPayload<inferSchemaOut<TClientDataSchema>>) => Promise<unknown>;
1503
+ run: (payload: ChatTaskRunPayload<inferSchemaOut<TClientDataSchema>, TTools>) => Promise<unknown>;
1439
1504
  /**
1440
1505
  * Called once at the start of every run boot — for the initial run, for
1441
1506
  * preloaded runs, AND for reactive continuation runs (post-cancel /
@@ -1899,7 +1964,7 @@ type ChatCustomAgentOptions<TIdentifier extends string, TClientDataSchema extend
1899
1964
  clientDataSchema?: TClientDataSchema;
1900
1965
  };
1901
1966
  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>;
1902
- 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>;
1967
+ declare function chatAgent<TIdentifier extends string, TClientDataSchema extends TaskSchema | undefined = undefined, TUIMessage extends UIMessage = UIMessage, TActionSchema extends TaskSchema | undefined = undefined, TTools extends ToolSet = ToolSet>(options: ChatAgentOptions<TIdentifier, TClientDataSchema, TUIMessage, TActionSchema, TTools>): Task<TIdentifier, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>;
1903
1968
  /**
1904
1969
  * Optional config for {@link chat.withUIMessage}. `streamOptions` become default
1905
1970
  * static `toUIMessageStream()` settings; inner `chat.agent({ uiMessageStreamOptions })`
@@ -1958,7 +2023,7 @@ export interface ChatBuilder<TUIMessage extends UIMessage = UIMessage, TClientDa
1958
2023
  * and omitted from options. Otherwise, it can still be set directly in options
1959
2024
  * (backwards compatible).
1960
2025
  */
1961
- 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>;
2026
+ agent: [TClientDataSchema] extends [undefined] ? <TId extends string, TInfer extends TaskSchema | undefined = undefined, TAction extends TaskSchema | undefined = undefined, TTools extends ToolSet = ToolSet>(options: ChatAgentOptions<TId, TInfer, TUIMessage, TAction, TTools>) => Task<TId, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TInfer>>, unknown> : <TId extends string, TAction extends TaskSchema | undefined = undefined, TTools extends ToolSet = ToolSet>(options: Omit<ChatAgentOptions<TId, TClientDataSchema, TUIMessage, TAction, TTools>, "clientDataSchema">) => Task<TId, ChatTaskWirePayload<TUIMessage, inferSchemaIn<TClientDataSchema>>, unknown>;
1962
2027
  /**
1963
2028
  * Create a custom agent with manual lifecycle control.
1964
2029
  *
@@ -2560,7 +2625,7 @@ declare function chatLocal<T extends Record<string, unknown>>(options: {
2560
2625
  * ```
2561
2626
  */
2562
2627
  import type { InferChatClientData } from "./ai-shared.js";
2563
- export type { InferChatClientData, InferChatUIMessage } from "./ai-shared.js";
2628
+ export type { InferChatClientData, InferChatUIMessage, InferChatUIMessageFromTools, } from "./ai-shared.js";
2564
2629
  /**
2565
2630
  * Options for {@link createChatStartSessionAction}.
2566
2631
  */
@@ -38,7 +38,16 @@ const METADATA_KEY = "tool.execute.options";
38
38
  * stopped/aborted conversations with partial tool parts.
39
39
  */
40
40
  function toModelMessages(messages) {
41
- return (0, ai_1.convertToModelMessages)(messages, { ignoreIncompleteToolCalls: true });
41
+ // Pass the resolved per-turn `tools` (if any) so the AI SDK can look up each
42
+ // tool's `toModelOutput` and re-apply it to prior-turn tool results. Without
43
+ // `tools` it falls back to JSON-stringifying the raw output (TRI-10149). The
44
+ // conditional spread keeps the options object byte-identical to the no-tools
45
+ // path when nothing was declared.
46
+ const tools = locals_js_1.locals.get(chatResolvedToolsKey);
47
+ return (0, ai_1.convertToModelMessages)(messages, {
48
+ ignoreIncompleteToolCalls: true,
49
+ ...(tools ? { tools } : {}),
50
+ });
42
51
  }
43
52
  const chatTurnContextKey = locals_js_1.locals.create("chat.turnContext");
44
53
  /**
@@ -1743,6 +1752,18 @@ const chatOnCompactedKey = locals_js_1.locals.create("chat.onCompacted");
1743
1752
  /** @internal Full task `ctx` for the active `chat.agent` run (for hooks invoked from nested compaction). */
1744
1753
  const chatAgentRunContextKey = locals_js_1.locals.create("chat.agentRunContext");
1745
1754
  const chatPrepareMessagesKey = locals_js_1.locals.create("chat.prepareMessages");
1755
+ /**
1756
+ * @internal The raw `tools` option from `chat.agent({ tools })`, either a
1757
+ * static `ToolSet` or a per-turn function. Set once at boot.
1758
+ */
1759
+ const chatToolsOptionKey = locals_js_1.locals.create("chat.toolsOption");
1760
+ /**
1761
+ * @internal The concrete `ToolSet` resolved for the current turn. Read by
1762
+ * `toModelMessages` so `convertToModelMessages` can re-run `toModelOutput` on
1763
+ * prior-turn tool results. Unset when no `tools` were declared (preserves the
1764
+ * exact pre-feature conversion behavior).
1765
+ */
1766
+ const chatResolvedToolsKey = locals_js_1.locals.create("chat.resolvedTools");
1746
1767
  /** @internal Flag set by `chat.requestUpgrade()` to exit the loop after the current turn. */
1747
1768
  const chatUpgradeRequestedKey = locals_js_1.locals.create("chat.upgradeRequested");
1748
1769
  /**
@@ -1836,6 +1857,37 @@ async function applyPrepareMessages(messages, reason) {
1836
1857
  },
1837
1858
  });
1838
1859
  }
1860
+ /**
1861
+ * Resolve the `tools` option into a concrete `ToolSet` and cache it in locals so
1862
+ * `toModelMessages` can pass it to `convertToModelMessages`. For the function
1863
+ * form, invokes the user function with the given context (or the current turn
1864
+ * context when no override is passed). Pass an `override` for the boot-time
1865
+ * history conversion, which runs before the per-turn context exists and uses
1866
+ * the run/continuation payload's `clientData`.
1867
+ *
1868
+ * Fails closed: a throwing resolver propagates rather than carrying a prior
1869
+ * turn's set forward. The function form can gate capabilities by user or flag,
1870
+ * so reusing stale tools would leak capabilities. No-op when no `tools` were
1871
+ * declared.
1872
+ * @internal
1873
+ */
1874
+ async function resolveTurnTools(override) {
1875
+ const option = locals_js_1.locals.get(chatToolsOptionKey);
1876
+ if (!option)
1877
+ return;
1878
+ if (typeof option !== "function") {
1879
+ locals_js_1.locals.set(chatResolvedToolsKey, option);
1880
+ return;
1881
+ }
1882
+ const ctx = override ?? locals_js_1.locals.get(chatTurnContextKey);
1883
+ const resolved = await option({
1884
+ chatId: ctx?.chatId ?? "",
1885
+ turn: ctx?.turn ?? 0,
1886
+ continuation: ctx?.continuation ?? false,
1887
+ clientData: ctx?.clientData,
1888
+ });
1889
+ locals_js_1.locals.set(chatResolvedToolsKey, resolved);
1890
+ }
1839
1891
  /**
1840
1892
  * Read the current compaction state. Returns the summary and base message count
1841
1893
  * if compaction has occurred in this turn, or `undefined` if not.
@@ -2561,7 +2613,7 @@ function chatCustomAgent(options) {
2561
2613
  return task;
2562
2614
  }
2563
2615
  function chatAgent(options) {
2564
- const { run: userRun, clientDataSchema, onBoot, onRecoveryBoot, onPreload, onChatStart, onValidateMessages, hydrateMessages, actionSchema, onAction, onTurnStart, onBeforeTurnComplete, onCompacted, compaction, pendingMessages: pendingMessagesConfig, prepareMessages, onTurnComplete, maxTurns = 100, turnTimeout = "1h", idleTimeoutInSeconds = 30, chatAccessTokenTTL = "1h", preloadIdleTimeoutInSeconds, preloadTimeout, uiMessageStreamOptions, onChatSuspend, onChatResume, exitAfterPreloadIdle = false, oomMachine, ...restOptions } = options;
2616
+ const { run: userRun, clientDataSchema, onBoot, onRecoveryBoot, onPreload, onChatStart, onValidateMessages, hydrateMessages, actionSchema, onAction, onTurnStart, onBeforeTurnComplete, onCompacted, compaction, pendingMessages: pendingMessagesConfig, prepareMessages, tools: toolsOption, onTurnComplete, maxTurns = 100, turnTimeout = "1h", idleTimeoutInSeconds = 30, chatAccessTokenTTL = "1h", preloadIdleTimeoutInSeconds, preloadTimeout, uiMessageStreamOptions, onChatSuspend, onChatResume, exitAfterPreloadIdle = false, oomMachine, ...restOptions } = options;
2565
2617
  const parseClientData = clientDataSchema ? (0, v3_1.getSchemaParseFn)(clientDataSchema) : undefined;
2566
2618
  const parseAction = actionSchema ? (0, v3_1.getSchemaParseFn)(actionSchema) : undefined;
2567
2619
  // chat.agent does not expose generic retry options (see docstring on
@@ -2610,6 +2662,19 @@ function chatAgent(options) {
2610
2662
  if (prepareMessages) {
2611
2663
  locals_js_1.locals.set(chatPrepareMessagesKey, prepareMessages);
2612
2664
  }
2665
+ if (toolsOption) {
2666
+ // Cast: the option's function form is typed against the parsed
2667
+ // `clientData` (`ResolveToolsEvent<inferSchemaOut<...>>`), but the
2668
+ // locals key uses the erased `ResolveToolsEvent<unknown>`. The runtime
2669
+ // value is identical; this mirrors how `prepareMessages` is stored.
2670
+ locals_js_1.locals.set(chatToolsOptionKey, toolsOption);
2671
+ // Static tools are usable immediately. The function form is resolved
2672
+ // just before the boot history conversion (with the payload's
2673
+ // clientData) and again per-turn (see resolveTurnTools).
2674
+ if (typeof toolsOption !== "function") {
2675
+ locals_js_1.locals.set(chatResolvedToolsKey, toolsOption);
2676
+ }
2677
+ }
2613
2678
  if (compaction) {
2614
2679
  locals_js_1.locals.set(chatAgentCompactionKey, compaction);
2615
2680
  }
@@ -2940,6 +3005,27 @@ function chatAgent(options) {
2940
3005
  accumulatedUIMessages = [...payload.headStartMessages];
2941
3006
  }
2942
3007
  if (accumulatedUIMessages.length > 0) {
3008
+ // Resolve a function-form `tools` with the run/continuation payload's
3009
+ // clientData so this conversion of the restored history applies each
3010
+ // tool's toModelOutput (static tools were already seeded above). This
3011
+ // only re-renders saved history, so it fails open: a resolver hiccup
3012
+ // logs and converts without tools rather than blocking the resume.
3013
+ // Per-turn resolveTurnTools still fails closed for live turns.
3014
+ if (typeof toolsOption === "function") {
3015
+ try {
3016
+ await resolveTurnTools({
3017
+ chatId: payload.chatId,
3018
+ turn: 0,
3019
+ continuation: payload.continuation ?? false,
3020
+ clientData: parseClientData
3021
+ ? await parseClientData(payload.metadata)
3022
+ : payload.metadata,
3023
+ });
3024
+ }
3025
+ catch (error) {
3026
+ v3_1.logger.warn("chat.agent: tools() resolver threw at boot; restored history converted without toModelOutput", { error: error instanceof Error ? error.message : String(error) });
3027
+ }
3028
+ }
2943
3029
  try {
2944
3030
  accumulatedMessages = await toModelMessages(accumulatedUIMessages);
2945
3031
  }
@@ -3361,6 +3447,10 @@ function chatAgent(options) {
3361
3447
  continuation,
3362
3448
  clientData,
3363
3449
  });
3450
+ // Resolve the per-turn `tools` set now that turn context
3451
+ // (incl. parsed clientData) exists, so every toModelMessages
3452
+ // call this turn can re-apply tool `toModelOutput`.
3453
+ await resolveTurnTools();
3364
3454
  // Per-turn stop controller (reset each turn)
3365
3455
  const stopController = new AbortController();
3366
3456
  currentStopController = stopController;
@@ -3930,6 +4020,7 @@ function chatAgent(options) {
3930
4020
  previousTurnUsage,
3931
4021
  totalUsage: cumulativeUsage,
3932
4022
  ctx,
4023
+ tools: locals_js_1.locals.get(chatResolvedToolsKey) ?? {},
3933
4024
  signal: combinedSignal,
3934
4025
  cancelSignal,
3935
4026
  stopSignal,