@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.
- package/dist/commonjs/v3/ai-shared.d.ts +16 -1
- package/dist/commonjs/v3/ai-shared.js.map +1 -1
- package/dist/commonjs/v3/ai.d.ts +71 -6
- package/dist/commonjs/v3/ai.js +93 -2
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai-shared.d.ts +16 -1
- package/dist/esm/v3/ai-shared.js.map +1 -1
- package/dist/esm/v3/ai.d.ts +71 -6
- package/dist/esm/v3/ai.js +93 -2
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +2 -2
package/dist/commonjs/version.js
CHANGED
|
@@ -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;AAKH;;;GAGG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,+BAAwC,CAAC;
|
|
1
|
+
{"version":3,"file":"ai-shared.js","sourceRoot":"","sources":["../../../src/v3/ai-shared.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAKH;;;GAGG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,+BAAwC,CAAC;AAqMtF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,UAAU,qBAAqB,CACnC,MAAc,EACd,KAGC;IAED,IAAI,KAAK,CAAC,OAAO,KAAK,gBAAgB;QAAE,OAAO,KAAK,CAAC;IACrD,IAAI,KAAK,CAAC,gBAAgB,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACtD,MAAM,MAAM,GAAG,KAAK,CAAC,gBAAgB,CAAC,KAAK,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACzE,IAAI,CAAC,MAAM;QAAE,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CAAC,IAAI;QAClB,KAAK,EAAE,SAAS;KACE,CAAC;AACvB,CAAC"}
|
package/dist/esm/v3/ai.d.ts
CHANGED
|
@@ -225,7 +225,7 @@ export type ChatTaskSignals = {
|
|
|
225
225
|
* The full payload passed to a `chatAgent` run function.
|
|
226
226
|
* Extends `ChatTaskPayload` (the wire 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
|
|
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
|
*/
|
package/dist/esm/v3/ai.js
CHANGED
|
@@ -25,7 +25,16 @@ const METADATA_KEY = "tool.execute.options";
|
|
|
25
25
|
* stopped/aborted conversations with partial tool parts.
|
|
26
26
|
*/
|
|
27
27
|
function toModelMessages(messages) {
|
|
28
|
-
|
|
28
|
+
// Pass the resolved per-turn `tools` (if any) so the AI SDK can look up each
|
|
29
|
+
// tool's `toModelOutput` and re-apply it to prior-turn tool results. Without
|
|
30
|
+
// `tools` it falls back to JSON-stringifying the raw output (TRI-10149). The
|
|
31
|
+
// conditional spread keeps the options object byte-identical to the no-tools
|
|
32
|
+
// path when nothing was declared.
|
|
33
|
+
const tools = locals.get(chatResolvedToolsKey);
|
|
34
|
+
return convertToModelMessages(messages, {
|
|
35
|
+
ignoreIncompleteToolCalls: true,
|
|
36
|
+
...(tools ? { tools } : {}),
|
|
37
|
+
});
|
|
29
38
|
}
|
|
30
39
|
const chatTurnContextKey = locals.create("chat.turnContext");
|
|
31
40
|
/**
|
|
@@ -1730,6 +1739,18 @@ const chatOnCompactedKey = locals.create("chat.onCompacted");
|
|
|
1730
1739
|
/** @internal Full task `ctx` for the active `chat.agent` run (for hooks invoked from nested compaction). */
|
|
1731
1740
|
const chatAgentRunContextKey = locals.create("chat.agentRunContext");
|
|
1732
1741
|
const chatPrepareMessagesKey = locals.create("chat.prepareMessages");
|
|
1742
|
+
/**
|
|
1743
|
+
* @internal The raw `tools` option from `chat.agent({ tools })`, either a
|
|
1744
|
+
* static `ToolSet` or a per-turn function. Set once at boot.
|
|
1745
|
+
*/
|
|
1746
|
+
const chatToolsOptionKey = locals.create("chat.toolsOption");
|
|
1747
|
+
/**
|
|
1748
|
+
* @internal The concrete `ToolSet` resolved for the current turn. Read by
|
|
1749
|
+
* `toModelMessages` so `convertToModelMessages` can re-run `toModelOutput` on
|
|
1750
|
+
* prior-turn tool results. Unset when no `tools` were declared (preserves the
|
|
1751
|
+
* exact pre-feature conversion behavior).
|
|
1752
|
+
*/
|
|
1753
|
+
const chatResolvedToolsKey = locals.create("chat.resolvedTools");
|
|
1733
1754
|
/** @internal Flag set by `chat.requestUpgrade()` to exit the loop after the current turn. */
|
|
1734
1755
|
const chatUpgradeRequestedKey = locals.create("chat.upgradeRequested");
|
|
1735
1756
|
/**
|
|
@@ -1821,6 +1842,37 @@ async function applyPrepareMessages(messages, reason) {
|
|
|
1821
1842
|
},
|
|
1822
1843
|
});
|
|
1823
1844
|
}
|
|
1845
|
+
/**
|
|
1846
|
+
* Resolve the `tools` option into a concrete `ToolSet` and cache it in locals so
|
|
1847
|
+
* `toModelMessages` can pass it to `convertToModelMessages`. For the function
|
|
1848
|
+
* form, invokes the user function with the given context (or the current turn
|
|
1849
|
+
* context when no override is passed). Pass an `override` for the boot-time
|
|
1850
|
+
* history conversion, which runs before the per-turn context exists and uses
|
|
1851
|
+
* the run/continuation payload's `clientData`.
|
|
1852
|
+
*
|
|
1853
|
+
* Fails closed: a throwing resolver propagates rather than carrying a prior
|
|
1854
|
+
* turn's set forward. The function form can gate capabilities by user or flag,
|
|
1855
|
+
* so reusing stale tools would leak capabilities. No-op when no `tools` were
|
|
1856
|
+
* declared.
|
|
1857
|
+
* @internal
|
|
1858
|
+
*/
|
|
1859
|
+
async function resolveTurnTools(override) {
|
|
1860
|
+
const option = locals.get(chatToolsOptionKey);
|
|
1861
|
+
if (!option)
|
|
1862
|
+
return;
|
|
1863
|
+
if (typeof option !== "function") {
|
|
1864
|
+
locals.set(chatResolvedToolsKey, option);
|
|
1865
|
+
return;
|
|
1866
|
+
}
|
|
1867
|
+
const ctx = override ?? locals.get(chatTurnContextKey);
|
|
1868
|
+
const resolved = await option({
|
|
1869
|
+
chatId: ctx?.chatId ?? "",
|
|
1870
|
+
turn: ctx?.turn ?? 0,
|
|
1871
|
+
continuation: ctx?.continuation ?? false,
|
|
1872
|
+
clientData: ctx?.clientData,
|
|
1873
|
+
});
|
|
1874
|
+
locals.set(chatResolvedToolsKey, resolved);
|
|
1875
|
+
}
|
|
1824
1876
|
/**
|
|
1825
1877
|
* Read the current compaction state. Returns the summary and base message count
|
|
1826
1878
|
* if compaction has occurred in this turn, or `undefined` if not.
|
|
@@ -2546,7 +2598,7 @@ function chatCustomAgent(options) {
|
|
|
2546
2598
|
return task;
|
|
2547
2599
|
}
|
|
2548
2600
|
function chatAgent(options) {
|
|
2549
|
-
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;
|
|
2601
|
+
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;
|
|
2550
2602
|
const parseClientData = clientDataSchema ? getSchemaParseFn(clientDataSchema) : undefined;
|
|
2551
2603
|
const parseAction = actionSchema ? getSchemaParseFn(actionSchema) : undefined;
|
|
2552
2604
|
// chat.agent does not expose generic retry options (see docstring on
|
|
@@ -2595,6 +2647,19 @@ function chatAgent(options) {
|
|
|
2595
2647
|
if (prepareMessages) {
|
|
2596
2648
|
locals.set(chatPrepareMessagesKey, prepareMessages);
|
|
2597
2649
|
}
|
|
2650
|
+
if (toolsOption) {
|
|
2651
|
+
// Cast: the option's function form is typed against the parsed
|
|
2652
|
+
// `clientData` (`ResolveToolsEvent<inferSchemaOut<...>>`), but the
|
|
2653
|
+
// locals key uses the erased `ResolveToolsEvent<unknown>`. The runtime
|
|
2654
|
+
// value is identical; this mirrors how `prepareMessages` is stored.
|
|
2655
|
+
locals.set(chatToolsOptionKey, toolsOption);
|
|
2656
|
+
// Static tools are usable immediately. The function form is resolved
|
|
2657
|
+
// just before the boot history conversion (with the payload's
|
|
2658
|
+
// clientData) and again per-turn (see resolveTurnTools).
|
|
2659
|
+
if (typeof toolsOption !== "function") {
|
|
2660
|
+
locals.set(chatResolvedToolsKey, toolsOption);
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2598
2663
|
if (compaction) {
|
|
2599
2664
|
locals.set(chatAgentCompactionKey, compaction);
|
|
2600
2665
|
}
|
|
@@ -2925,6 +2990,27 @@ function chatAgent(options) {
|
|
|
2925
2990
|
accumulatedUIMessages = [...payload.headStartMessages];
|
|
2926
2991
|
}
|
|
2927
2992
|
if (accumulatedUIMessages.length > 0) {
|
|
2993
|
+
// Resolve a function-form `tools` with the run/continuation payload's
|
|
2994
|
+
// clientData so this conversion of the restored history applies each
|
|
2995
|
+
// tool's toModelOutput (static tools were already seeded above). This
|
|
2996
|
+
// only re-renders saved history, so it fails open: a resolver hiccup
|
|
2997
|
+
// logs and converts without tools rather than blocking the resume.
|
|
2998
|
+
// Per-turn resolveTurnTools still fails closed for live turns.
|
|
2999
|
+
if (typeof toolsOption === "function") {
|
|
3000
|
+
try {
|
|
3001
|
+
await resolveTurnTools({
|
|
3002
|
+
chatId: payload.chatId,
|
|
3003
|
+
turn: 0,
|
|
3004
|
+
continuation: payload.continuation ?? false,
|
|
3005
|
+
clientData: parseClientData
|
|
3006
|
+
? await parseClientData(payload.metadata)
|
|
3007
|
+
: payload.metadata,
|
|
3008
|
+
});
|
|
3009
|
+
}
|
|
3010
|
+
catch (error) {
|
|
3011
|
+
logger.warn("chat.agent: tools() resolver threw at boot; restored history converted without toModelOutput", { error: error instanceof Error ? error.message : String(error) });
|
|
3012
|
+
}
|
|
3013
|
+
}
|
|
2928
3014
|
try {
|
|
2929
3015
|
accumulatedMessages = await toModelMessages(accumulatedUIMessages);
|
|
2930
3016
|
}
|
|
@@ -3346,6 +3432,10 @@ function chatAgent(options) {
|
|
|
3346
3432
|
continuation,
|
|
3347
3433
|
clientData,
|
|
3348
3434
|
});
|
|
3435
|
+
// Resolve the per-turn `tools` set now that turn context
|
|
3436
|
+
// (incl. parsed clientData) exists, so every toModelMessages
|
|
3437
|
+
// call this turn can re-apply tool `toModelOutput`.
|
|
3438
|
+
await resolveTurnTools();
|
|
3349
3439
|
// Per-turn stop controller (reset each turn)
|
|
3350
3440
|
const stopController = new AbortController();
|
|
3351
3441
|
currentStopController = stopController;
|
|
@@ -3915,6 +4005,7 @@ function chatAgent(options) {
|
|
|
3915
4005
|
previousTurnUsage,
|
|
3916
4006
|
totalUsage: cumulativeUsage,
|
|
3917
4007
|
ctx,
|
|
4008
|
+
tools: locals.get(chatResolvedToolsKey) ?? {},
|
|
3918
4009
|
signal: combinedSignal,
|
|
3919
4010
|
cancelSignal,
|
|
3920
4011
|
stopSignal,
|