agents 0.21.0 → 0.23.0
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/README.md +21 -1
- package/dist/{agent-tool-types-CzGGB-20.d.ts → agent-routing-DE5zmCQ8.d.ts} +1844 -1336
- package/dist/agent-routing.d.ts +14 -0
- package/dist/agent-routing.js +187 -0
- package/dist/agent-routing.js.map +1 -0
- package/dist/agent-tool-types.d.ts +26 -26
- package/dist/{agent-tools-zR2d5uij.d.ts → agent-tools-DtXMTDGM.d.ts} +7 -7
- package/dist/agent-tools.d.ts +21 -21
- package/dist/agent-tools.js +2 -1
- package/dist/agent-tools.js.map +1 -1
- package/dist/browser/ai.js +6 -2
- package/dist/browser/ai.js.map +1 -1
- package/dist/browser/tanstack-ai.js.map +1 -1
- package/dist/callable-decorator-DP__HhBA.d.ts +72 -0
- package/dist/callable-decorator.d.ts +18 -0
- package/dist/callable-decorator.js +71 -0
- package/dist/callable-decorator.js.map +1 -0
- package/dist/capability-BjSKYpzg.js +42 -0
- package/dist/capability-BjSKYpzg.js.map +1 -0
- package/dist/capability-runner-Be_-PLR1.d.ts +459 -0
- package/dist/channel-Bnm4S7T2.d.ts +491 -0
- package/dist/channels/ai-sdk.d.ts +46 -0
- package/dist/channels/ai-sdk.js +120 -0
- package/dist/channels/ai-sdk.js.map +1 -0
- package/dist/channels/email.d.ts +95 -0
- package/dist/channels/email.js +323 -0
- package/dist/channels/email.js.map +1 -0
- package/dist/channels/index.d.ts +233 -0
- package/dist/channels/index.js +608 -0
- package/dist/channels/index.js.map +1 -0
- package/dist/channels/slack.d.ts +140 -0
- package/dist/channels/slack.js +614 -0
- package/dist/channels/slack.js.map +1 -0
- package/dist/channels/tanstack-ai.d.ts +39 -0
- package/dist/channels/tanstack-ai.js +17 -0
- package/dist/channels/tanstack-ai.js.map +1 -0
- package/dist/channels/telegram.d.ts +106 -0
- package/dist/channels/telegram.js +427 -0
- package/dist/channels/telegram.js.map +1 -0
- package/dist/channels/voice.d.ts +45 -0
- package/dist/channels/voice.js +122 -0
- package/dist/channels/voice.js.map +1 -0
- package/dist/chat/index.d.ts +2328 -2015
- package/dist/chat/index.js +891 -521
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/react.d.ts +14 -1
- package/dist/chat/react.js +82 -52
- package/dist/chat/react.js.map +1 -1
- package/dist/chat/transport.js +1 -1
- package/dist/chat-sdk/index.d.ts +7 -7
- package/dist/chat-sdk/index.js +1 -1
- package/dist/{client-zqKcsyFa.js → client-jagG8a9_.js} +129 -37
- package/dist/client-jagG8a9_.js.map +1 -0
- package/dist/client.d.ts +1 -1
- package/dist/client.js +1 -1
- package/dist/{cloudflare-BduZwmYK.js → cloudflare-Dzvc7V2N.js} +10 -3
- package/dist/{cloudflare-BduZwmYK.js.map → cloudflare-Dzvc7V2N.js.map} +1 -1
- package/dist/context/index.d.ts +216 -0
- package/dist/context/index.js +454 -0
- package/dist/context/index.js.map +1 -0
- package/dist/current-agent-Da_C9a3b.d.ts +266 -0
- package/dist/current-agent-DhoDkSnH.js +51 -0
- package/dist/current-agent-DhoDkSnH.js.map +1 -0
- package/dist/diagnostics-BzvaX2UT.js +45 -0
- package/dist/diagnostics-BzvaX2UT.js.map +1 -0
- package/dist/diagnostics-C4jcz3VK.js +360 -0
- package/dist/diagnostics-C4jcz3VK.js.map +1 -0
- package/dist/{do-oauth-client-provider-VTZj2VtM.d.ts → do-oauth-client-provider-Tmf1vgKz.d.ts} +2 -2
- package/dist/{email-CL27preh.d.ts → email-7TatiTnl.d.ts} +38 -9
- package/dist/email-send.d.ts +15 -0
- package/dist/email-send.js +32 -0
- package/dist/email-send.js.map +1 -0
- package/dist/email.d.ts +14 -10
- package/dist/email.js.map +1 -1
- package/dist/{handler-stateless-C_bo-Ytq.d.ts → handler-stateless-DxYpJ_XF.d.ts} +3 -3
- package/dist/{handler-stateless-CIkKPETH.js → handler-stateless-VvrWSAVA.js} +5 -5
- package/dist/handler-stateless-VvrWSAVA.js.map +1 -0
- package/dist/index-BB0kqhIz.d.ts +101 -0
- package/dist/index-XDkuQ7zm.d.ts +89 -0
- package/dist/{index-BRnybD6X.d.ts → index-YSKgfgg9.d.ts} +21 -31
- package/dist/index.d.ts +113 -101
- package/dist/index.js +11 -7234
- package/dist/ingress-BfetZbMO.js +83 -0
- package/dist/ingress-BfetZbMO.js.map +1 -0
- package/dist/internal-CYlgHl1l.js +59 -0
- package/dist/internal-CYlgHl1l.js.map +1 -0
- package/dist/internal_context-BlxFEWfn.d.ts +19 -0
- package/dist/internal_context.d.ts +10 -4
- package/dist/internal_context.js +1 -10
- package/dist/{client-invoker-BNSZxAkv.d.ts → invoker-CG0_p_Wq.d.ts} +2 -2
- package/dist/{client-invoker-VNZ7X0nn.js → invoker-CHMnoxIA.js} +2 -2
- package/dist/invoker-CHMnoxIA.js.map +1 -0
- package/dist/lifecycle/index.d.ts +66 -0
- package/dist/lifecycle/index.js +4 -0
- package/dist/lifecycle-CMRGjZdw.js +1299 -0
- package/dist/lifecycle-CMRGjZdw.js.map +1 -0
- package/dist/mcp/{do-oauth-client-provider.d.ts → client/do-oauth-client-provider.d.ts} +1 -1
- package/dist/mcp/{do-oauth-client-provider.js → client/do-oauth-client-provider.js} +1 -1
- package/dist/mcp/client/do-oauth-client-provider.js.map +1 -0
- package/dist/mcp/client/index.d.ts +42 -0
- package/dist/mcp/{client.js → client/index.js} +1 -1
- package/dist/mcp/{x402.d.ts → client/x402.d.ts} +2 -2
- package/dist/mcp/{x402.js → client/x402.js} +2 -2
- package/dist/mcp/client/x402.js.map +1 -0
- package/dist/mcp/index.d.ts +36 -36
- package/dist/mcp/index.js +14 -16
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/{server.d.ts → server/index.d.ts} +1 -1
- package/dist/mcp/{server.js → server/index.js} +1 -1
- package/dist/observability/ai/index.js +50 -35
- package/dist/observability/ai/index.js.map +1 -1
- package/dist/observability/index.d.ts +4 -4
- package/dist/observability/index.js +3 -50
- package/dist/observability/index.js.map +1 -1
- package/dist/{protocol-Dqc2MQxo.js → protocol-B0nh6KNf.js} +19 -21
- package/dist/protocol-B0nh6KNf.js.map +1 -0
- package/dist/react.d.ts +4 -4
- package/dist/react.js +1 -1
- package/dist/{retries-CAvxtG9d.d.ts → retries-D9Ds-1lz.d.ts} +17 -6
- package/dist/retries.d.ts +8 -6
- package/dist/retries.js +13 -1
- package/dist/retries.js.map +1 -1
- package/dist/routing/index.d.ts +137 -0
- package/dist/routing/index.js +244 -0
- package/dist/routing/index.js.map +1 -0
- package/dist/sanitize-D9TujEK8.js +79 -0
- package/dist/sanitize-D9TujEK8.js.map +1 -0
- package/dist/schedule.d.ts +25 -94
- package/dist/schedule.js +1 -98
- package/dist/schedule.js.map +1 -1
- package/dist/scheduler-DD9NdYbF.js +665 -0
- package/dist/scheduler-DD9NdYbF.js.map +1 -0
- package/dist/scheduler-Dwh85ZGl.d.ts +223 -0
- package/dist/schedules/index.d.ts +22 -0
- package/dist/schedules/index.js +2 -0
- package/dist/schedules/parser.d.ts +79 -0
- package/dist/schedules/parser.js +103 -0
- package/dist/schedules/parser.js.map +1 -0
- package/dist/sentence-chunker-BAidJ4DA.d.ts +68 -0
- package/dist/serializable.d.ts +1 -1
- package/dist/sessions/index.d.ts +441 -0
- package/dist/sessions/index.js +2063 -0
- package/dist/sessions/index.js.map +1 -0
- package/dist/skills/index.d.ts +99 -0
- package/dist/skills/index.js +254 -5
- package/dist/skills/index.js.map +1 -1
- package/dist/sql-error-CPY-GXyI.d.ts +12 -0
- package/dist/sql-error.d.ts +2 -0
- package/dist/sql-error.js +16 -0
- package/dist/sql-error.js.map +1 -0
- package/dist/src-DlSHshb2.js +6963 -0
- package/dist/src-DlSHshb2.js.map +1 -0
- package/dist/streams/index.d.ts +120 -0
- package/dist/streams/index.js +107 -0
- package/dist/streams/index.js.map +1 -0
- package/dist/streams-D6tJ0NN9.d.ts +370 -0
- package/dist/streams-DZKgAj9b.js +709 -0
- package/dist/streams-DZKgAj9b.js.map +1 -0
- package/dist/sub-routing.d.ts +12 -12
- package/dist/surface-bZZJqBka.js +17 -0
- package/dist/surface-bZZJqBka.js.map +1 -0
- package/dist/tasks/index.d.ts +64 -0
- package/dist/tasks/index.js +2 -0
- package/dist/tasks-BRJ5zgya.d.ts +517 -0
- package/dist/tasks-ylZgBjhj.js +1656 -0
- package/dist/tasks-ylZgBjhj.js.map +1 -0
- package/dist/text-segment-joiner-BtAFQSA_.js +57 -0
- package/dist/text-segment-joiner-BtAFQSA_.js.map +1 -0
- package/dist/text-stream-CpdiKrJB.js +272 -0
- package/dist/text-stream-CpdiKrJB.js.map +1 -0
- package/dist/tokens-nHAKcN6M.js +52 -0
- package/dist/tokens-nHAKcN6M.js.map +1 -0
- package/dist/tool-schema-CBjGPrsQ.js +31 -0
- package/dist/tool-schema-CBjGPrsQ.js.map +1 -0
- package/dist/types-B7LojTe4.d.ts +202 -0
- package/dist/types-_Faxb570.d.ts +439 -0
- package/dist/voice/client.d.ts +226 -0
- package/dist/voice/client.js +932 -0
- package/dist/voice/client.js.map +1 -0
- package/dist/voice/errors.d.ts +43 -0
- package/dist/voice/errors.js +41 -0
- package/dist/voice/errors.js.map +1 -0
- package/dist/voice/index.d.ts +271 -0
- package/dist/voice/index.js +1812 -0
- package/dist/voice/index.js.map +1 -0
- package/dist/voice/react.d.ts +167 -0
- package/dist/voice/react.js +234 -0
- package/dist/voice/react.js.map +1 -0
- package/dist/voice/sfu.d.ts +71 -0
- package/dist/voice/sfu.js +157 -0
- package/dist/voice/sfu.js.map +1 -0
- package/dist/voice/text.d.ts +6 -0
- package/dist/voice/text.js +2 -0
- package/dist/voice/types.d.ts +58 -0
- package/dist/voice/types.js +18 -0
- package/dist/voice/types.js.map +1 -0
- package/dist/voice/workers-ai.d.ts +136 -0
- package/dist/voice/workers-ai.js +568 -0
- package/dist/voice/workers-ai.js.map +1 -0
- package/dist/websockets/index.d.ts +192 -0
- package/dist/websockets/index.js +2 -0
- package/dist/websockets-DUfRHPRq.js +502 -0
- package/dist/websockets-DUfRHPRq.js.map +1 -0
- package/dist/workflow-types.d.ts +25 -25
- package/dist/workflows.d.ts +22 -22
- package/dist/workflows.js +2 -1
- package/dist/workflows.js.map +1 -1
- package/dist/{ws-chat-transport-CIoOBbO7.js → ws-chat-transport-rWwta645.js} +152 -15
- package/dist/ws-chat-transport-rWwta645.js.map +1 -0
- package/docs/agent-class.md +29 -87
- package/docs/agent-tools.md +2 -1
- package/docs/channels.md +323 -0
- package/docs/chat-agents.md +19 -25
- package/docs/context.md +131 -0
- package/docs/durable-execution.md +1 -1
- package/docs/http-websockets.md +1 -11
- package/docs/human-in-the-loop.md +1 -1
- package/docs/index.md +16 -12
- package/docs/lifecycle.md +370 -0
- package/docs/long-running-agents.md +4 -6
- package/docs/mcp-client.md +55 -0
- package/docs/mcp-servers.md +5 -1
- package/docs/observability.md +11 -11
- package/docs/resumable-streaming.md +2 -2
- package/docs/routing.md +105 -0
- package/docs/scheduling.md +175 -15
- package/docs/server-driven-messages.md +1 -1
- package/docs/sessions.md +237 -871
- package/docs/streams.md +213 -0
- package/docs/sub-agents.md +185 -125
- package/docs/tasks.md +246 -0
- package/docs/voice.md +745 -0
- package/package.json +144 -33
- package/dist/cli/index.js +0 -26
- package/dist/cli/index.js.map +0 -1
- package/dist/client-invoker-VNZ7X0nn.js.map +0 -1
- package/dist/client-zqKcsyFa.js.map +0 -1
- package/dist/compaction-helpers-iiKMr2TQ.js +0 -340
- package/dist/compaction-helpers-iiKMr2TQ.js.map +0 -1
- package/dist/compaction-helpers-wUz6M3us.d.ts +0 -621
- package/dist/experimental/memory/session/index.d.ts +0 -670
- package/dist/experimental/memory/session/index.js +0 -2374
- package/dist/experimental/memory/session/index.js.map +0 -1
- package/dist/experimental/memory/utils/index.d.ts +0 -96
- package/dist/experimental/memory/utils/index.js +0 -79
- package/dist/experimental/memory/utils/index.js.map +0 -1
- package/dist/handler-stateless-CIkKPETH.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/internal_context-Dg4Cgjcu.d.ts +0 -37
- package/dist/internal_context.js.map +0 -1
- package/dist/mcp/client.d.ts +0 -42
- package/dist/mcp/do-oauth-client-provider.js.map +0 -1
- package/dist/mcp/x402.js.map +0 -1
- package/dist/protocol-Dqc2MQxo.js.map +0 -1
- package/dist/tool-output-truncation-CNnnGZQ3.js +0 -98
- package/dist/tool-output-truncation-CNnnGZQ3.js.map +0 -1
- package/dist/ws-chat-transport-CIoOBbO7.js.map +0 -1
- /package/dist/{cli/index.d.ts → index-BVVgDSdq.d.ts} +0 -0
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
o as getMcpAuthContext,
|
|
6
6
|
r as StatelessMcpServerInput,
|
|
7
7
|
t as CreateStatelessMcpHandlerOptions
|
|
8
|
-
} from "
|
|
8
|
+
} from "../../handler-stateless-DxYpJ_XF.js";
|
|
9
9
|
export {
|
|
10
10
|
type CreateStatelessMcpHandlerOptions as CreateMcpHandlerOptions,
|
|
11
11
|
type CreateStatelessMcpHandlerOptions,
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as getMcpAuthContext, t as createStatelessMcpHandler } from "
|
|
1
|
+
import { n as getMcpAuthContext, t as createStatelessMcpHandler } from "../../handler-stateless-VvrWSAVA.js";
|
|
2
2
|
export { createStatelessMcpHandler as createMcpHandler, getMcpAuthContext };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { __DO_NOT_USE_WILL_BREAK__agentContext } from "../../
|
|
2
|
-
import
|
|
1
|
+
import { t as __DO_NOT_USE_WILL_BREAK__agentContext } from "../../current-agent-DhoDkSnH.js";
|
|
2
|
+
import "../../internal_context.js";
|
|
3
|
+
import { r as writeSpanAttributes, t as tracer } from "../../cloudflare-Dzvc7V2N.js";
|
|
3
4
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
5
|
//#region src/observability/ai/read.ts
|
|
5
6
|
/** Narrows an unknown value to a string. */
|
|
@@ -189,6 +190,14 @@ function metadataAttributes(metadata, passthroughPrefix = TraceAttribute.Cloudfl
|
|
|
189
190
|
function operationSpanName(agentName) {
|
|
190
191
|
return spanName(TraceAttribute.GenAI.OperationNameValueInvokeAgent, agentName);
|
|
191
192
|
}
|
|
193
|
+
function semanticContextAttributes(context) {
|
|
194
|
+
return {
|
|
195
|
+
[TraceAttribute.GenAI.AgentID]: context?.agentId,
|
|
196
|
+
[TraceAttribute.GenAI.AgentName]: context?.agentName,
|
|
197
|
+
[TraceAttribute.GenAI.AgentVersion]: context?.agentVersion,
|
|
198
|
+
[TraceAttribute.GenAI.ConversationID]: context?.conversationId
|
|
199
|
+
};
|
|
200
|
+
}
|
|
192
201
|
/** Builds the root span for an SDK operation such as generateText or streamText. */
|
|
193
202
|
function operationSpan(input) {
|
|
194
203
|
return {
|
|
@@ -196,10 +205,7 @@ function operationSpan(input) {
|
|
|
196
205
|
...input.attributes,
|
|
197
206
|
[TraceAttribute.Cloudflare.IntegrationName]: "ai-sdk",
|
|
198
207
|
[TraceAttribute.Cloudflare.OperationName]: input.operation,
|
|
199
|
-
|
|
200
|
-
[TraceAttribute.GenAI.AgentName]: input.context?.agentName,
|
|
201
|
-
[TraceAttribute.GenAI.AgentVersion]: input.context?.agentVersion,
|
|
202
|
-
[TraceAttribute.GenAI.ConversationID]: input.context?.conversationId,
|
|
208
|
+
...semanticContextAttributes(input.context),
|
|
203
209
|
[TraceAttribute.GenAI.OperationName]: TraceAttribute.GenAI.OperationNameValueInvokeAgent,
|
|
204
210
|
[TraceAttribute.GenAI.ProviderName]: normalizeProviderName(input.provider),
|
|
205
211
|
...requestAttributes(input.request, input.model)
|
|
@@ -214,6 +220,7 @@ function modelCallSpan(input) {
|
|
|
214
220
|
...input.attributes,
|
|
215
221
|
[TraceAttribute.Cloudflare.IntegrationName]: "ai-sdk",
|
|
216
222
|
[TraceAttribute.Cloudflare.OperationName]: input.operation,
|
|
223
|
+
...semanticContextAttributes(input.context),
|
|
217
224
|
[TraceAttribute.GenAI.OperationName]: TraceAttribute.GenAI.OperationNameValueChat,
|
|
218
225
|
[TraceAttribute.GenAI.ProviderName]: normalizeProviderName(input.provider),
|
|
219
226
|
...requestAttributes(input.request, input.model)
|
|
@@ -241,6 +248,7 @@ function toolCallSpan(input) {
|
|
|
241
248
|
attributes: {
|
|
242
249
|
[TraceAttribute.Cloudflare.IntegrationName]: "ai-sdk",
|
|
243
250
|
[TraceAttribute.Cloudflare.OperationName]: input.operation,
|
|
251
|
+
...semanticContextAttributes(input.context),
|
|
244
252
|
[TraceAttribute.GenAI.OperationName]: TraceAttribute.GenAI.OperationNameValueExecuteTool,
|
|
245
253
|
[TraceAttribute.GenAI.ToolCallID]: input.toolCallId,
|
|
246
254
|
[TraceAttribute.GenAI.ToolName]: input.toolName,
|
|
@@ -256,6 +264,7 @@ function toolApprovalSpan(input) {
|
|
|
256
264
|
[TraceAttribute.Cloudflare.IntegrationName]: "ai-sdk",
|
|
257
265
|
[TraceAttribute.Cloudflare.OperationName]: "tool.approval",
|
|
258
266
|
[TraceAttribute.Cloudflare.ToolApprovalState]: input.state,
|
|
267
|
+
...semanticContextAttributes(input.context),
|
|
259
268
|
[TraceAttribute.GenAI.OperationName]: TraceAttribute.GenAI.OperationNameValueExecuteTool,
|
|
260
269
|
[TraceAttribute.GenAI.ToolCallID]: input.toolCallId,
|
|
261
270
|
[TraceAttribute.GenAI.ToolName]: input.toolName,
|
|
@@ -1085,7 +1094,7 @@ function isAsyncIterable$1(value) {
|
|
|
1085
1094
|
}
|
|
1086
1095
|
//#endregion
|
|
1087
1096
|
//#region src/observability/ai/wrapper/model.ts
|
|
1088
|
-
function wrapModel(tracer, wrapLanguageModel, model, parentOperation, storeMessages, boundToInvocation = false) {
|
|
1097
|
+
function wrapModel(tracer, wrapLanguageModel, model, parentOperation, storeMessages, context, boundToInvocation = false) {
|
|
1089
1098
|
if (!wrapLanguageModel) return model;
|
|
1090
1099
|
if (typeof model !== "object" || model === null) return model;
|
|
1091
1100
|
const modelInfo = extractModelInfo(model);
|
|
@@ -1094,7 +1103,7 @@ function wrapModel(tracer, wrapLanguageModel, model, parentOperation, storeMessa
|
|
|
1094
1103
|
model: aiGatewayLog.model,
|
|
1095
1104
|
middleware: {
|
|
1096
1105
|
wrapGenerate: async ({ doGenerate, params }) => {
|
|
1097
|
-
const span = modelCallSpanForModel("doGenerate", modelInfo, params, parentOperation, storeMessages);
|
|
1106
|
+
const span = modelCallSpanForModel("doGenerate", modelInfo, params, parentOperation, storeMessages, context);
|
|
1098
1107
|
return tracer.withSpan(span.name, span.attributes, async (modelCall) => {
|
|
1099
1108
|
aiGatewayLog.reset();
|
|
1100
1109
|
try {
|
|
@@ -1114,7 +1123,7 @@ function wrapModel(tracer, wrapLanguageModel, model, parentOperation, storeMessa
|
|
|
1114
1123
|
}, boundToInvocation ? { boundToInvocation: true } : void 0);
|
|
1115
1124
|
},
|
|
1116
1125
|
wrapStream: async ({ doStream, params }) => {
|
|
1117
|
-
const span = modelCallSpanForModel("doStream", modelInfo, params, parentOperation, storeMessages);
|
|
1126
|
+
const span = modelCallSpanForModel("doStream", modelInfo, params, parentOperation, storeMessages, context);
|
|
1118
1127
|
return tracer.openSpan(span.name, span.attributes, async (modelCall) => {
|
|
1119
1128
|
aiGatewayLog.reset();
|
|
1120
1129
|
try {
|
|
@@ -1140,9 +1149,10 @@ function wrapModel(tracer, wrapLanguageModel, model, parentOperation, storeMessa
|
|
|
1140
1149
|
function recordAIGatewayLogOnError(span, cause, capturedLogId) {
|
|
1141
1150
|
writeSpanAttributes(span, aiGatewayLogAttributes(extractAIGatewayLogId(cause) ?? capturedLogId));
|
|
1142
1151
|
}
|
|
1143
|
-
function modelCallSpanForModel(operation, model, params, parentOperation, storeMessages) {
|
|
1152
|
+
function modelCallSpanForModel(operation, model, params, parentOperation, storeMessages, context) {
|
|
1144
1153
|
const record = typeof params === "object" && params !== null ? params : {};
|
|
1145
1154
|
const span = modelCallSpan({
|
|
1155
|
+
context,
|
|
1146
1156
|
model: model?.modelId,
|
|
1147
1157
|
operation,
|
|
1148
1158
|
provider: model?.provider,
|
|
@@ -1158,27 +1168,28 @@ function modelCallSpanForModel(operation, model, params, parentOperation, storeM
|
|
|
1158
1168
|
}
|
|
1159
1169
|
//#endregion
|
|
1160
1170
|
//#region src/observability/ai/wrapper/tools.ts
|
|
1161
|
-
function wrapTools(tracer, tools, storeTools, boundToInvocation = false, approvedToolCalls) {
|
|
1171
|
+
function wrapTools(tracer, tools, storeTools, context, boundToInvocation = false, approvedToolCalls) {
|
|
1162
1172
|
if (typeof tools !== "object" || tools === null) return tools;
|
|
1163
1173
|
const toolRecord = tools;
|
|
1164
1174
|
const wrappedTools = {};
|
|
1165
|
-
for (const [toolName, tool] of Object.entries(toolRecord)) wrappedTools[toolName] = wrapTool(tracer, toolName, tool, storeTools, boundToInvocation, approvedToolCalls);
|
|
1175
|
+
for (const [toolName, tool] of Object.entries(toolRecord)) wrappedTools[toolName] = wrapTool(tracer, toolName, tool, storeTools, context, boundToInvocation, approvedToolCalls);
|
|
1166
1176
|
return wrappedTools;
|
|
1167
1177
|
}
|
|
1168
|
-
function wrapTool(tracer, toolName, tool, storeTools, boundToInvocation, approvedToolCalls) {
|
|
1178
|
+
function wrapTool(tracer, toolName, tool, storeTools, context, boundToInvocation, approvedToolCalls) {
|
|
1169
1179
|
if (typeof tool !== "object" || tool === null) return tool;
|
|
1170
1180
|
const toolRecord = tool;
|
|
1171
1181
|
const hasExecute = typeof toolRecord.execute === "function";
|
|
1172
1182
|
const hasApproval = typeof toolRecord.needsApproval === "boolean" || typeof toolRecord.needsApproval === "function";
|
|
1173
1183
|
if (!hasExecute && !hasApproval) return tool;
|
|
1174
1184
|
const wrappedTool = Object.assign(Object.create(Object.getPrototypeOf(tool)), tool);
|
|
1175
|
-
if (hasApproval) wrapApprovalCheck(tracer, wrappedTool, toolRecord, tool, toolName, boundToInvocation);
|
|
1185
|
+
if (hasApproval) wrapApprovalCheck(tracer, wrappedTool, toolRecord, tool, toolName, context, boundToInvocation);
|
|
1176
1186
|
if (!hasExecute) return wrappedTool;
|
|
1177
1187
|
const execute = toolRecord.execute;
|
|
1178
1188
|
if (typeof execute !== "function") return wrappedTool;
|
|
1179
1189
|
const originalExecute = execute.bind(tool);
|
|
1180
1190
|
wrappedTool.execute = (...args) => {
|
|
1181
1191
|
const span = toolCallSpan({
|
|
1192
|
+
context,
|
|
1182
1193
|
operation: "tool.execute",
|
|
1183
1194
|
toolCallId: extractToolCallId(args[1]),
|
|
1184
1195
|
toolName
|
|
@@ -1191,7 +1202,7 @@ function wrapTool(tracer, toolName, tool, storeTools, boundToInvocation, approve
|
|
|
1191
1202
|
const inSpanContext = AsyncLocalStorage.snapshot();
|
|
1192
1203
|
const toolCallId = extractToolCallId(args[1]);
|
|
1193
1204
|
if (approvalResponseForOptions(args[1], toolCallId)?.approved === true || toolCallId !== void 0 && approvedToolCalls?.get(toolCallId) === toolName) {
|
|
1194
|
-
recordApprovalChild(tracer, toolName, toolCallId, "approved", boundToInvocation);
|
|
1205
|
+
recordApprovalChild(tracer, toolName, toolCallId, "approved", context, boundToInvocation);
|
|
1195
1206
|
if (toolCallId !== void 0) approvedToolCalls?.delete(toolCallId);
|
|
1196
1207
|
}
|
|
1197
1208
|
const result = originalExecute(...args);
|
|
@@ -1204,54 +1215,56 @@ function wrapTool(tracer, toolName, tool, storeTools, boundToInvocation, approve
|
|
|
1204
1215
|
};
|
|
1205
1216
|
return wrappedTool;
|
|
1206
1217
|
}
|
|
1207
|
-
function wrapApprovalCheck(tracer, wrappedTool, toolRecord, tool, toolName, boundToInvocation) {
|
|
1218
|
+
function wrapApprovalCheck(tracer, wrappedTool, toolRecord, tool, toolName, context, boundToInvocation) {
|
|
1208
1219
|
const approval = toolRecord.needsApproval;
|
|
1209
1220
|
const original = typeof approval === "function" ? approval.bind(tool) : void 0;
|
|
1210
1221
|
wrappedTool.needsApproval = (...args) => {
|
|
1211
1222
|
const result = original ? original(...args) : approval;
|
|
1212
1223
|
const recordRequested = (needed) => {
|
|
1213
1224
|
const toolCallId = extractToolCallId(args[1]);
|
|
1214
|
-
if (needed === true && !hasApprovalResponse(args[1], toolCallId)) recordApprovalSegment(tracer, toolName, toolCallId, "requested", boundToInvocation);
|
|
1225
|
+
if (needed === true && !hasApprovalResponse(args[1], toolCallId)) recordApprovalSegment(tracer, toolName, toolCallId, "requested", context, boundToInvocation);
|
|
1215
1226
|
return needed;
|
|
1216
1227
|
};
|
|
1217
1228
|
return isPromiseLike(result) ? Promise.resolve(result).then(recordRequested) : recordRequested(result);
|
|
1218
1229
|
};
|
|
1219
1230
|
}
|
|
1220
1231
|
/** Instruments AI SDK v7's top-level tool approval policy. */
|
|
1221
|
-
function wrapToolApprovalPolicy(tracer, policy, approvedToolCalls, boundToInvocation = false) {
|
|
1232
|
+
function wrapToolApprovalPolicy(tracer, policy, approvedToolCalls, context, boundToInvocation = false) {
|
|
1222
1233
|
if (typeof policy === "function") return (...args) => {
|
|
1223
1234
|
const toolCall = recordValue(recordValue(args[0])?.toolCall);
|
|
1224
|
-
return observePolicyResult(policy(...args), readString(toolCall?.toolName) ?? "tool", readString(toolCall?.toolCallId), tracer, approvedToolCalls, boundToInvocation);
|
|
1235
|
+
return observePolicyResult(policy(...args), readString(toolCall?.toolName) ?? "tool", readString(toolCall?.toolCallId), tracer, approvedToolCalls, context, boundToInvocation);
|
|
1225
1236
|
};
|
|
1226
1237
|
if (typeof policy !== "object" || policy === null) return policy;
|
|
1227
|
-
return Object.fromEntries(Object.entries(policy).map(([toolName, setting]) => [toolName, (...args) => observePolicyResult(typeof setting === "function" ? setting(...args) : setting, toolName, extractToolCallId(args[1]), tracer, approvedToolCalls, boundToInvocation)]));
|
|
1238
|
+
return Object.fromEntries(Object.entries(policy).map(([toolName, setting]) => [toolName, (...args) => observePolicyResult(typeof setting === "function" ? setting(...args) : setting, toolName, extractToolCallId(args[1]), tracer, approvedToolCalls, context, boundToInvocation)]));
|
|
1228
1239
|
}
|
|
1229
|
-
function observePolicyResult(result, toolName, toolCallId, tracer, approvedToolCalls, boundToInvocation = false) {
|
|
1240
|
+
function observePolicyResult(result, toolName, toolCallId, tracer, approvedToolCalls, context, boundToInvocation = false) {
|
|
1230
1241
|
const observe = (status) => {
|
|
1231
1242
|
if (toolCallId === void 0) return status;
|
|
1232
1243
|
const type = typeof status === "string" ? status : readString(recordValue(status)?.type);
|
|
1233
1244
|
if (type === "approved") approvedToolCalls.set(toolCallId, toolName);
|
|
1234
|
-
else if (type === "user-approval" || type === "denied") recordApprovalSegment(tracer, toolName, toolCallId, type === "denied" ? "denied" : "requested", boundToInvocation);
|
|
1245
|
+
else if (type === "user-approval" || type === "denied") recordApprovalSegment(tracer, toolName, toolCallId, type === "denied" ? "denied" : "requested", context, boundToInvocation);
|
|
1235
1246
|
return status;
|
|
1236
1247
|
};
|
|
1237
1248
|
return isPromiseLike(result) ? Promise.resolve(result).then(observe) : observe(result);
|
|
1238
1249
|
}
|
|
1239
1250
|
/** Records denied responses, whose tool never reaches execute(). */
|
|
1240
|
-
function recordDeniedApprovalResponses(tracer, messages) {
|
|
1241
|
-
for (const response of approvalResponses(messages)) if (!response.approved) recordApprovalSegment(tracer, response.toolName, response.toolCallId, "denied");
|
|
1251
|
+
function recordDeniedApprovalResponses(tracer, messages, context) {
|
|
1252
|
+
for (const response of approvalResponses(messages)) if (!response.approved) recordApprovalSegment(tracer, response.toolName, response.toolCallId, "denied", context);
|
|
1242
1253
|
}
|
|
1243
|
-
function recordApprovalSegment(tracer, toolName, toolCallId, state, boundToInvocation = false) {
|
|
1254
|
+
function recordApprovalSegment(tracer, toolName, toolCallId, state, context, boundToInvocation = false) {
|
|
1244
1255
|
const tool = toolCallSpan({
|
|
1256
|
+
context,
|
|
1245
1257
|
operation: "tool.approval",
|
|
1246
1258
|
toolCallId,
|
|
1247
1259
|
toolName
|
|
1248
1260
|
});
|
|
1249
1261
|
tracer.withSpan(tool.name, tool.attributes, () => {
|
|
1250
|
-
recordApprovalChild(tracer, toolName, toolCallId, state, boundToInvocation);
|
|
1262
|
+
recordApprovalChild(tracer, toolName, toolCallId, state, context, boundToInvocation);
|
|
1251
1263
|
}, boundToInvocation ? { boundToInvocation: true } : void 0);
|
|
1252
1264
|
}
|
|
1253
|
-
function recordApprovalChild(tracer, toolName, toolCallId, state, boundToInvocation = false) {
|
|
1265
|
+
function recordApprovalChild(tracer, toolName, toolCallId, state, context, boundToInvocation = false) {
|
|
1254
1266
|
const approval = toolApprovalSpan({
|
|
1267
|
+
context,
|
|
1255
1268
|
state,
|
|
1256
1269
|
toolCallId,
|
|
1257
1270
|
toolName
|
|
@@ -1415,10 +1428,11 @@ function createOperationWrapper(operationName, operation, wrapLanguageModel, ins
|
|
|
1415
1428
|
operationSpan.finish();
|
|
1416
1429
|
return operation(params, ...args);
|
|
1417
1430
|
}
|
|
1431
|
+
const context = semanticContext(params);
|
|
1418
1432
|
writeSpanAttributes(operationSpan, operationSpanForCall(operationName, extractModelInfo(params.model), params, instrumentation.options).attributes);
|
|
1419
|
-
recordDeniedApprovalResponses(instrumentation.tracer, params.messages);
|
|
1433
|
+
recordDeniedApprovalResponses(instrumentation.tracer, params.messages, context);
|
|
1420
1434
|
const startedAtMs = Date.now();
|
|
1421
|
-
const result = operation(operationParamsForCall(params, operationName, wrapLanguageModel, instrumentation.tracer, storage, boundToInvocation), ...args);
|
|
1435
|
+
const result = operation(operationParamsForCall(params, operationName, wrapLanguageModel, instrumentation.tracer, storage, context, boundToInvocation), ...args);
|
|
1422
1436
|
const hasModelSpan = canWrapModel(wrapLanguageModel, params.model);
|
|
1423
1437
|
return finishWhenStreamCompletes(result, operationSpan, {
|
|
1424
1438
|
includeResponse: !hasModelSpan,
|
|
@@ -1430,9 +1444,10 @@ function createOperationWrapper(operationName, operation, wrapLanguageModel, ins
|
|
|
1430
1444
|
const boundToInvocation = isAISDKInvocationBounded(params);
|
|
1431
1445
|
return instrumentation.tracer.withSpan(operationSpanName(agentNameForCall(params)), {}, async (operationSpan) => {
|
|
1432
1446
|
if (!operationSpan.isTraced) return operation(params, ...args);
|
|
1447
|
+
const context = semanticContext(params);
|
|
1433
1448
|
writeSpanAttributes(operationSpan, operationSpanForCall(operationName, extractModelInfo(params.model), params, instrumentation.options).attributes);
|
|
1434
|
-
recordDeniedApprovalResponses(instrumentation.tracer, params.messages);
|
|
1435
|
-
const result = await operation(operationParamsForCall(params, operationName, wrapLanguageModel, instrumentation.tracer, storage, boundToInvocation), ...args);
|
|
1449
|
+
recordDeniedApprovalResponses(instrumentation.tracer, params.messages, context);
|
|
1450
|
+
const result = await operation(operationParamsForCall(params, operationName, wrapLanguageModel, instrumentation.tracer, storage, context, boundToInvocation), ...args);
|
|
1436
1451
|
operationSpan.finish(finishAttributesFromResult(result, { includeResponse: !canWrapModel(wrapLanguageModel, params.model) }));
|
|
1437
1452
|
return result;
|
|
1438
1453
|
}, boundToInvocation ? { boundToInvocation: true } : void 0);
|
|
@@ -1450,13 +1465,13 @@ function agentNameForCall(params) {
|
|
|
1450
1465
|
const runtimeContext = runtimeContextRecord(params);
|
|
1451
1466
|
return metadataValue(metadata, "agentName", "gen_ai.agent.name") ?? metadataValue(runtimeContext, "agentName", "gen_ai.agent.name") ?? readString(telemetry?.functionId);
|
|
1452
1467
|
}
|
|
1453
|
-
function operationParamsForCall(params, operationName, wrapLanguageModel, tracer, storage, boundToInvocation = false) {
|
|
1468
|
+
function operationParamsForCall(params, operationName, wrapLanguageModel, tracer, storage, context, boundToInvocation = false) {
|
|
1454
1469
|
const approvedToolCalls = /* @__PURE__ */ new Map();
|
|
1455
1470
|
return {
|
|
1456
1471
|
...params,
|
|
1457
|
-
...shouldWrapTools(operationName) && params.tools !== void 0 ? { tools: wrapTools(tracer, params.tools, storage.storeTools, boundToInvocation, approvedToolCalls) } : {},
|
|
1458
|
-
...params.toolApproval !== void 0 ? { toolApproval: wrapToolApprovalPolicy(tracer, params.toolApproval, approvedToolCalls, boundToInvocation) } : {},
|
|
1459
|
-
...params.model !== void 0 ? { model: wrapModel(tracer, wrapLanguageModel, params.model, operationName, storage.storeMessages, boundToInvocation) } : {}
|
|
1472
|
+
...shouldWrapTools(operationName) && params.tools !== void 0 ? { tools: wrapTools(tracer, params.tools, storage.storeTools, context, boundToInvocation, approvedToolCalls) } : {},
|
|
1473
|
+
...params.toolApproval !== void 0 ? { toolApproval: wrapToolApprovalPolicy(tracer, params.toolApproval, approvedToolCalls, context, boundToInvocation) } : {},
|
|
1474
|
+
...params.model !== void 0 ? { model: wrapModel(tracer, wrapLanguageModel, params.model, operationName, storage.storeMessages, context, boundToInvocation) } : {}
|
|
1460
1475
|
};
|
|
1461
1476
|
}
|
|
1462
1477
|
function canWrapModel(wrapLanguageModel, model) {
|