@trigger.dev/sdk 4.5.0-rc.3 → 4.5.0-rc.5
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/imports/ai-runtime-cjs.cjs.map +1 -0
- package/dist/commonjs/imports/ai-runtime.d.ts +1 -0
- package/dist/commonjs/imports/ai-runtime.js +27 -0
- 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 +81 -8
- package/dist/commonjs/v3/ai.js +138 -34
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/aiAutoTelemetry.d.ts +2 -0
- package/dist/commonjs/v3/aiAutoTelemetry.js +81 -0
- package/dist/commonjs/v3/aiAutoTelemetry.js.map +1 -0
- package/dist/commonjs/v3/chat-client.js +5 -3
- package/dist/commonjs/v3/chat-client.js.map +1 -1
- package/dist/commonjs/v3/chat-server.d.ts +29 -6
- package/dist/commonjs/v3/chat-server.js +6 -4
- package/dist/commonjs/v3/chat-server.js.map +1 -1
- package/dist/commonjs/v3/chat.d.ts +11 -0
- package/dist/commonjs/v3/chat.js +61 -1
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/v3/shared.js +17 -9
- package/dist/commonjs/v3/shared.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/imports/ai-runtime.d.ts +2 -0
- package/dist/esm/imports/ai-runtime.js +16 -0
- package/dist/esm/imports/ai-runtime.js.map +1 -0
- 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 +81 -8
- package/dist/esm/v3/ai.js +109 -5
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/aiAutoTelemetry.d.ts +2 -0
- package/dist/esm/v3/aiAutoTelemetry.js +78 -0
- package/dist/esm/v3/aiAutoTelemetry.js.map +1 -0
- package/dist/esm/v3/chat-client.js +3 -1
- package/dist/esm/v3/chat-client.js.map +1 -1
- package/dist/esm/v3/chat-server.d.ts +29 -6
- package/dist/esm/v3/chat-server.js +3 -1
- package/dist/esm/v3/chat-server.js.map +1 -1
- package/dist/esm/v3/chat.d.ts +11 -0
- package/dist/esm/v3/chat.js +61 -1
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/v3/shared.js +18 -10
- package/dist/esm/v3/shared.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +11 -6
package/dist/esm/v3/ai.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { accessoryAttributes, apiClientManager, getSchemaParseFn, headerValue, InputStreamOncePromise, isSchemaZodEsque, logger, ManualWaitpointPromise, OutOfMemoryError, sessionStreams, SemanticInternalAttributes, taskContext, SESSION_IN_EVENT_ID_HEADER, TRIGGER_CONTROL_SUBTYPE, generateJWT, } from "@trigger.dev/core/v3";
|
|
2
|
-
|
|
2
|
+
// Runtime VALUES go through the ESM/CJS shim so the CJS build can `require`
|
|
3
|
+
// ESM-only `ai@7` (see ../imports/ai-runtime.ts).
|
|
4
|
+
import { convertToModelMessages, dynamicTool, generateId as generateMessageId, getToolName, isToolUIPart, jsonSchema, readUIMessageStream, tool as aiTool, zodSchema, } from "../imports/ai-runtime.js";
|
|
3
5
|
import { trace } from "@opentelemetry/api";
|
|
4
6
|
import { auth } from "./auth.js";
|
|
5
7
|
import { locals } from "./locals.js";
|
|
@@ -16,6 +18,7 @@ import { runBashInSkill, readFileInSkill } from "./agentSkillsRuntime.js";
|
|
|
16
18
|
import { markChatAgentRunForStreamsWarning } from "./streams.js";
|
|
17
19
|
import { sessions, } from "./sessions.js";
|
|
18
20
|
import { createTask } from "./shared.js";
|
|
21
|
+
import { ensureAiSdkTelemetry } from "./aiAutoTelemetry.js";
|
|
19
22
|
import { resourceCatalog } from "@trigger.dev/core/v3";
|
|
20
23
|
import { tracer } from "./tracer.js";
|
|
21
24
|
const METADATA_KEY = "tool.execute.options";
|
|
@@ -25,7 +28,16 @@ const METADATA_KEY = "tool.execute.options";
|
|
|
25
28
|
* stopped/aborted conversations with partial tool parts.
|
|
26
29
|
*/
|
|
27
30
|
function toModelMessages(messages) {
|
|
28
|
-
|
|
31
|
+
// Pass the resolved per-turn `tools` (if any) so the AI SDK can look up each
|
|
32
|
+
// tool's `toModelOutput` and re-apply it to prior-turn tool results. Without
|
|
33
|
+
// `tools` it falls back to JSON-stringifying the raw output (TRI-10149). The
|
|
34
|
+
// conditional spread keeps the options object byte-identical to the no-tools
|
|
35
|
+
// path when nothing was declared.
|
|
36
|
+
const tools = locals.get(chatResolvedToolsKey);
|
|
37
|
+
return convertToModelMessages(messages, {
|
|
38
|
+
ignoreIncompleteToolCalls: true,
|
|
39
|
+
...(tools ? { tools } : {}),
|
|
40
|
+
});
|
|
29
41
|
}
|
|
30
42
|
const chatTurnContextKey = locals.create("chat.turnContext");
|
|
31
43
|
/**
|
|
@@ -632,9 +644,14 @@ function createTaskToolExecuteHandler(task) {
|
|
|
632
644
|
const toolMeta = {
|
|
633
645
|
toolCallId: toolOpts?.toolCallId ?? "",
|
|
634
646
|
};
|
|
635
|
-
|
|
647
|
+
// v6 passes user context as `experimental_context`, v7 as `context`. Read
|
|
648
|
+
// whichever is set and stamp both so subtasks reading either name work.
|
|
649
|
+
const toolContext = toolOpts?.context ?? toolOpts?.experimental_context;
|
|
650
|
+
if (toolContext !== undefined) {
|
|
636
651
|
try {
|
|
637
|
-
|
|
652
|
+
const serialized = JSON.parse(JSON.stringify(toolContext));
|
|
653
|
+
toolMeta.experimental_context = serialized;
|
|
654
|
+
toolMeta.context = serialized;
|
|
638
655
|
}
|
|
639
656
|
catch {
|
|
640
657
|
/* non-serializable */
|
|
@@ -1730,6 +1747,18 @@ const chatOnCompactedKey = locals.create("chat.onCompacted");
|
|
|
1730
1747
|
/** @internal Full task `ctx` for the active `chat.agent` run (for hooks invoked from nested compaction). */
|
|
1731
1748
|
const chatAgentRunContextKey = locals.create("chat.agentRunContext");
|
|
1732
1749
|
const chatPrepareMessagesKey = locals.create("chat.prepareMessages");
|
|
1750
|
+
/**
|
|
1751
|
+
* @internal The raw `tools` option from `chat.agent({ tools })`, either a
|
|
1752
|
+
* static `ToolSet` or a per-turn function. Set once at boot.
|
|
1753
|
+
*/
|
|
1754
|
+
const chatToolsOptionKey = locals.create("chat.toolsOption");
|
|
1755
|
+
/**
|
|
1756
|
+
* @internal The concrete `ToolSet` resolved for the current turn. Read by
|
|
1757
|
+
* `toModelMessages` so `convertToModelMessages` can re-run `toModelOutput` on
|
|
1758
|
+
* prior-turn tool results. Unset when no `tools` were declared (preserves the
|
|
1759
|
+
* exact pre-feature conversion behavior).
|
|
1760
|
+
*/
|
|
1761
|
+
const chatResolvedToolsKey = locals.create("chat.resolvedTools");
|
|
1733
1762
|
/** @internal Flag set by `chat.requestUpgrade()` to exit the loop after the current turn. */
|
|
1734
1763
|
const chatUpgradeRequestedKey = locals.create("chat.upgradeRequested");
|
|
1735
1764
|
/**
|
|
@@ -1821,6 +1850,37 @@ async function applyPrepareMessages(messages, reason) {
|
|
|
1821
1850
|
},
|
|
1822
1851
|
});
|
|
1823
1852
|
}
|
|
1853
|
+
/**
|
|
1854
|
+
* Resolve the `tools` option into a concrete `ToolSet` and cache it in locals so
|
|
1855
|
+
* `toModelMessages` can pass it to `convertToModelMessages`. For the function
|
|
1856
|
+
* form, invokes the user function with the given context (or the current turn
|
|
1857
|
+
* context when no override is passed). Pass an `override` for the boot-time
|
|
1858
|
+
* history conversion, which runs before the per-turn context exists and uses
|
|
1859
|
+
* the run/continuation payload's `clientData`.
|
|
1860
|
+
*
|
|
1861
|
+
* Fails closed: a throwing resolver propagates rather than carrying a prior
|
|
1862
|
+
* turn's set forward. The function form can gate capabilities by user or flag,
|
|
1863
|
+
* so reusing stale tools would leak capabilities. No-op when no `tools` were
|
|
1864
|
+
* declared.
|
|
1865
|
+
* @internal
|
|
1866
|
+
*/
|
|
1867
|
+
async function resolveTurnTools(override) {
|
|
1868
|
+
const option = locals.get(chatToolsOptionKey);
|
|
1869
|
+
if (!option)
|
|
1870
|
+
return;
|
|
1871
|
+
if (typeof option !== "function") {
|
|
1872
|
+
locals.set(chatResolvedToolsKey, option);
|
|
1873
|
+
return;
|
|
1874
|
+
}
|
|
1875
|
+
const ctx = override ?? locals.get(chatTurnContextKey);
|
|
1876
|
+
const resolved = await option({
|
|
1877
|
+
chatId: ctx?.chatId ?? "",
|
|
1878
|
+
turn: ctx?.turn ?? 0,
|
|
1879
|
+
continuation: ctx?.continuation ?? false,
|
|
1880
|
+
clientData: ctx?.clientData,
|
|
1881
|
+
});
|
|
1882
|
+
locals.set(chatResolvedToolsKey, resolved);
|
|
1883
|
+
}
|
|
1824
1884
|
/**
|
|
1825
1885
|
* Read the current compaction state. Returns the summary and base message count
|
|
1826
1886
|
* if compaction has occurred in this turn, or `undefined` if not.
|
|
@@ -2546,7 +2606,7 @@ function chatCustomAgent(options) {
|
|
|
2546
2606
|
return task;
|
|
2547
2607
|
}
|
|
2548
2608
|
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;
|
|
2609
|
+
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
2610
|
const parseClientData = clientDataSchema ? getSchemaParseFn(clientDataSchema) : undefined;
|
|
2551
2611
|
const parseAction = actionSchema ? getSchemaParseFn(actionSchema) : undefined;
|
|
2552
2612
|
// chat.agent does not expose generic retry options (see docstring on
|
|
@@ -2563,6 +2623,11 @@ function chatAgent(options) {
|
|
|
2563
2623
|
agentConfig: { type: "ai-sdk-chat" },
|
|
2564
2624
|
run: async (payload, { signal: runSignal, ctx }) => {
|
|
2565
2625
|
locals.set(chatAgentRunContextKey, ctx);
|
|
2626
|
+
// On AI SDK 7, register the `@ai-sdk/otel` integration (once per process)
|
|
2627
|
+
// so `experimental_telemetry` spans flow into the run trace. Awaited here
|
|
2628
|
+
// at run boot — before any `streamText` — and a no-op on v5/v6 or when the
|
|
2629
|
+
// optional `@ai-sdk/otel` peer isn't installed. See ./aiAutoTelemetry.ts.
|
|
2630
|
+
await ensureAiSdkTelemetry();
|
|
2566
2631
|
// Bind the run to its backing Session so every module-level helper
|
|
2567
2632
|
// (chat.stream, chat.messages, chat.stopSignal) resolves to this
|
|
2568
2633
|
// chat's `.in` / `.out` channels.
|
|
@@ -2595,6 +2660,19 @@ function chatAgent(options) {
|
|
|
2595
2660
|
if (prepareMessages) {
|
|
2596
2661
|
locals.set(chatPrepareMessagesKey, prepareMessages);
|
|
2597
2662
|
}
|
|
2663
|
+
if (toolsOption) {
|
|
2664
|
+
// Cast: the option's function form is typed against the parsed
|
|
2665
|
+
// `clientData` (`ResolveToolsEvent<inferSchemaOut<...>>`), but the
|
|
2666
|
+
// locals key uses the erased `ResolveToolsEvent<unknown>`. The runtime
|
|
2667
|
+
// value is identical; this mirrors how `prepareMessages` is stored.
|
|
2668
|
+
locals.set(chatToolsOptionKey, toolsOption);
|
|
2669
|
+
// Static tools are usable immediately. The function form is resolved
|
|
2670
|
+
// just before the boot history conversion (with the payload's
|
|
2671
|
+
// clientData) and again per-turn (see resolveTurnTools).
|
|
2672
|
+
if (typeof toolsOption !== "function") {
|
|
2673
|
+
locals.set(chatResolvedToolsKey, toolsOption);
|
|
2674
|
+
}
|
|
2675
|
+
}
|
|
2598
2676
|
if (compaction) {
|
|
2599
2677
|
locals.set(chatAgentCompactionKey, compaction);
|
|
2600
2678
|
}
|
|
@@ -2925,6 +3003,27 @@ function chatAgent(options) {
|
|
|
2925
3003
|
accumulatedUIMessages = [...payload.headStartMessages];
|
|
2926
3004
|
}
|
|
2927
3005
|
if (accumulatedUIMessages.length > 0) {
|
|
3006
|
+
// Resolve a function-form `tools` with the run/continuation payload's
|
|
3007
|
+
// clientData so this conversion of the restored history applies each
|
|
3008
|
+
// tool's toModelOutput (static tools were already seeded above). This
|
|
3009
|
+
// only re-renders saved history, so it fails open: a resolver hiccup
|
|
3010
|
+
// logs and converts without tools rather than blocking the resume.
|
|
3011
|
+
// Per-turn resolveTurnTools still fails closed for live turns.
|
|
3012
|
+
if (typeof toolsOption === "function") {
|
|
3013
|
+
try {
|
|
3014
|
+
await resolveTurnTools({
|
|
3015
|
+
chatId: payload.chatId,
|
|
3016
|
+
turn: 0,
|
|
3017
|
+
continuation: payload.continuation ?? false,
|
|
3018
|
+
clientData: parseClientData
|
|
3019
|
+
? await parseClientData(payload.metadata)
|
|
3020
|
+
: payload.metadata,
|
|
3021
|
+
});
|
|
3022
|
+
}
|
|
3023
|
+
catch (error) {
|
|
3024
|
+
logger.warn("chat.agent: tools() resolver threw at boot; restored history converted without toModelOutput", { error: error instanceof Error ? error.message : String(error) });
|
|
3025
|
+
}
|
|
3026
|
+
}
|
|
2928
3027
|
try {
|
|
2929
3028
|
accumulatedMessages = await toModelMessages(accumulatedUIMessages);
|
|
2930
3029
|
}
|
|
@@ -3346,6 +3445,10 @@ function chatAgent(options) {
|
|
|
3346
3445
|
continuation,
|
|
3347
3446
|
clientData,
|
|
3348
3447
|
});
|
|
3448
|
+
// Resolve the per-turn `tools` set now that turn context
|
|
3449
|
+
// (incl. parsed clientData) exists, so every toModelMessages
|
|
3450
|
+
// call this turn can re-apply tool `toModelOutput`.
|
|
3451
|
+
await resolveTurnTools();
|
|
3349
3452
|
// Per-turn stop controller (reset each turn)
|
|
3350
3453
|
const stopController = new AbortController();
|
|
3351
3454
|
currentStopController = stopController;
|
|
@@ -3915,6 +4018,7 @@ function chatAgent(options) {
|
|
|
3915
4018
|
previousTurnUsage,
|
|
3916
4019
|
totalUsage: cumulativeUsage,
|
|
3917
4020
|
ctx,
|
|
4021
|
+
tools: locals.get(chatResolvedToolsKey) ?? {},
|
|
3918
4022
|
signal: combinedSignal,
|
|
3919
4023
|
cancelSignal,
|
|
3920
4024
|
stopSignal,
|