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.
Files changed (258) hide show
  1. package/README.md +21 -1
  2. package/dist/{agent-tool-types-CzGGB-20.d.ts → agent-routing-DE5zmCQ8.d.ts} +1844 -1336
  3. package/dist/agent-routing.d.ts +14 -0
  4. package/dist/agent-routing.js +187 -0
  5. package/dist/agent-routing.js.map +1 -0
  6. package/dist/agent-tool-types.d.ts +26 -26
  7. package/dist/{agent-tools-zR2d5uij.d.ts → agent-tools-DtXMTDGM.d.ts} +7 -7
  8. package/dist/agent-tools.d.ts +21 -21
  9. package/dist/agent-tools.js +2 -1
  10. package/dist/agent-tools.js.map +1 -1
  11. package/dist/browser/ai.js +6 -2
  12. package/dist/browser/ai.js.map +1 -1
  13. package/dist/browser/tanstack-ai.js.map +1 -1
  14. package/dist/callable-decorator-DP__HhBA.d.ts +72 -0
  15. package/dist/callable-decorator.d.ts +18 -0
  16. package/dist/callable-decorator.js +71 -0
  17. package/dist/callable-decorator.js.map +1 -0
  18. package/dist/capability-BjSKYpzg.js +42 -0
  19. package/dist/capability-BjSKYpzg.js.map +1 -0
  20. package/dist/capability-runner-Be_-PLR1.d.ts +459 -0
  21. package/dist/channel-Bnm4S7T2.d.ts +491 -0
  22. package/dist/channels/ai-sdk.d.ts +46 -0
  23. package/dist/channels/ai-sdk.js +120 -0
  24. package/dist/channels/ai-sdk.js.map +1 -0
  25. package/dist/channels/email.d.ts +95 -0
  26. package/dist/channels/email.js +323 -0
  27. package/dist/channels/email.js.map +1 -0
  28. package/dist/channels/index.d.ts +233 -0
  29. package/dist/channels/index.js +608 -0
  30. package/dist/channels/index.js.map +1 -0
  31. package/dist/channels/slack.d.ts +140 -0
  32. package/dist/channels/slack.js +614 -0
  33. package/dist/channels/slack.js.map +1 -0
  34. package/dist/channels/tanstack-ai.d.ts +39 -0
  35. package/dist/channels/tanstack-ai.js +17 -0
  36. package/dist/channels/tanstack-ai.js.map +1 -0
  37. package/dist/channels/telegram.d.ts +106 -0
  38. package/dist/channels/telegram.js +427 -0
  39. package/dist/channels/telegram.js.map +1 -0
  40. package/dist/channels/voice.d.ts +45 -0
  41. package/dist/channels/voice.js +122 -0
  42. package/dist/channels/voice.js.map +1 -0
  43. package/dist/chat/index.d.ts +2328 -2015
  44. package/dist/chat/index.js +891 -521
  45. package/dist/chat/index.js.map +1 -1
  46. package/dist/chat/react.d.ts +14 -1
  47. package/dist/chat/react.js +82 -52
  48. package/dist/chat/react.js.map +1 -1
  49. package/dist/chat/transport.js +1 -1
  50. package/dist/chat-sdk/index.d.ts +7 -7
  51. package/dist/chat-sdk/index.js +1 -1
  52. package/dist/{client-zqKcsyFa.js → client-jagG8a9_.js} +129 -37
  53. package/dist/client-jagG8a9_.js.map +1 -0
  54. package/dist/client.d.ts +1 -1
  55. package/dist/client.js +1 -1
  56. package/dist/{cloudflare-BduZwmYK.js → cloudflare-Dzvc7V2N.js} +10 -3
  57. package/dist/{cloudflare-BduZwmYK.js.map → cloudflare-Dzvc7V2N.js.map} +1 -1
  58. package/dist/context/index.d.ts +216 -0
  59. package/dist/context/index.js +454 -0
  60. package/dist/context/index.js.map +1 -0
  61. package/dist/current-agent-Da_C9a3b.d.ts +266 -0
  62. package/dist/current-agent-DhoDkSnH.js +51 -0
  63. package/dist/current-agent-DhoDkSnH.js.map +1 -0
  64. package/dist/diagnostics-BzvaX2UT.js +45 -0
  65. package/dist/diagnostics-BzvaX2UT.js.map +1 -0
  66. package/dist/diagnostics-C4jcz3VK.js +360 -0
  67. package/dist/diagnostics-C4jcz3VK.js.map +1 -0
  68. package/dist/{do-oauth-client-provider-VTZj2VtM.d.ts → do-oauth-client-provider-Tmf1vgKz.d.ts} +2 -2
  69. package/dist/{email-CL27preh.d.ts → email-7TatiTnl.d.ts} +38 -9
  70. package/dist/email-send.d.ts +15 -0
  71. package/dist/email-send.js +32 -0
  72. package/dist/email-send.js.map +1 -0
  73. package/dist/email.d.ts +14 -10
  74. package/dist/email.js.map +1 -1
  75. package/dist/{handler-stateless-C_bo-Ytq.d.ts → handler-stateless-DxYpJ_XF.d.ts} +3 -3
  76. package/dist/{handler-stateless-CIkKPETH.js → handler-stateless-VvrWSAVA.js} +5 -5
  77. package/dist/handler-stateless-VvrWSAVA.js.map +1 -0
  78. package/dist/index-BB0kqhIz.d.ts +101 -0
  79. package/dist/index-XDkuQ7zm.d.ts +89 -0
  80. package/dist/{index-BRnybD6X.d.ts → index-YSKgfgg9.d.ts} +21 -31
  81. package/dist/index.d.ts +113 -101
  82. package/dist/index.js +11 -7234
  83. package/dist/ingress-BfetZbMO.js +83 -0
  84. package/dist/ingress-BfetZbMO.js.map +1 -0
  85. package/dist/internal-CYlgHl1l.js +59 -0
  86. package/dist/internal-CYlgHl1l.js.map +1 -0
  87. package/dist/internal_context-BlxFEWfn.d.ts +19 -0
  88. package/dist/internal_context.d.ts +10 -4
  89. package/dist/internal_context.js +1 -10
  90. package/dist/{client-invoker-BNSZxAkv.d.ts → invoker-CG0_p_Wq.d.ts} +2 -2
  91. package/dist/{client-invoker-VNZ7X0nn.js → invoker-CHMnoxIA.js} +2 -2
  92. package/dist/invoker-CHMnoxIA.js.map +1 -0
  93. package/dist/lifecycle/index.d.ts +66 -0
  94. package/dist/lifecycle/index.js +4 -0
  95. package/dist/lifecycle-CMRGjZdw.js +1299 -0
  96. package/dist/lifecycle-CMRGjZdw.js.map +1 -0
  97. package/dist/mcp/{do-oauth-client-provider.d.ts → client/do-oauth-client-provider.d.ts} +1 -1
  98. package/dist/mcp/{do-oauth-client-provider.js → client/do-oauth-client-provider.js} +1 -1
  99. package/dist/mcp/client/do-oauth-client-provider.js.map +1 -0
  100. package/dist/mcp/client/index.d.ts +42 -0
  101. package/dist/mcp/{client.js → client/index.js} +1 -1
  102. package/dist/mcp/{x402.d.ts → client/x402.d.ts} +2 -2
  103. package/dist/mcp/{x402.js → client/x402.js} +2 -2
  104. package/dist/mcp/client/x402.js.map +1 -0
  105. package/dist/mcp/index.d.ts +36 -36
  106. package/dist/mcp/index.js +14 -16
  107. package/dist/mcp/index.js.map +1 -1
  108. package/dist/mcp/{server.d.ts → server/index.d.ts} +1 -1
  109. package/dist/mcp/{server.js → server/index.js} +1 -1
  110. package/dist/observability/ai/index.js +50 -35
  111. package/dist/observability/ai/index.js.map +1 -1
  112. package/dist/observability/index.d.ts +4 -4
  113. package/dist/observability/index.js +3 -50
  114. package/dist/observability/index.js.map +1 -1
  115. package/dist/{protocol-Dqc2MQxo.js → protocol-B0nh6KNf.js} +19 -21
  116. package/dist/protocol-B0nh6KNf.js.map +1 -0
  117. package/dist/react.d.ts +4 -4
  118. package/dist/react.js +1 -1
  119. package/dist/{retries-CAvxtG9d.d.ts → retries-D9Ds-1lz.d.ts} +17 -6
  120. package/dist/retries.d.ts +8 -6
  121. package/dist/retries.js +13 -1
  122. package/dist/retries.js.map +1 -1
  123. package/dist/routing/index.d.ts +137 -0
  124. package/dist/routing/index.js +244 -0
  125. package/dist/routing/index.js.map +1 -0
  126. package/dist/sanitize-D9TujEK8.js +79 -0
  127. package/dist/sanitize-D9TujEK8.js.map +1 -0
  128. package/dist/schedule.d.ts +25 -94
  129. package/dist/schedule.js +1 -98
  130. package/dist/schedule.js.map +1 -1
  131. package/dist/scheduler-DD9NdYbF.js +665 -0
  132. package/dist/scheduler-DD9NdYbF.js.map +1 -0
  133. package/dist/scheduler-Dwh85ZGl.d.ts +223 -0
  134. package/dist/schedules/index.d.ts +22 -0
  135. package/dist/schedules/index.js +2 -0
  136. package/dist/schedules/parser.d.ts +79 -0
  137. package/dist/schedules/parser.js +103 -0
  138. package/dist/schedules/parser.js.map +1 -0
  139. package/dist/sentence-chunker-BAidJ4DA.d.ts +68 -0
  140. package/dist/serializable.d.ts +1 -1
  141. package/dist/sessions/index.d.ts +441 -0
  142. package/dist/sessions/index.js +2063 -0
  143. package/dist/sessions/index.js.map +1 -0
  144. package/dist/skills/index.d.ts +99 -0
  145. package/dist/skills/index.js +254 -5
  146. package/dist/skills/index.js.map +1 -1
  147. package/dist/sql-error-CPY-GXyI.d.ts +12 -0
  148. package/dist/sql-error.d.ts +2 -0
  149. package/dist/sql-error.js +16 -0
  150. package/dist/sql-error.js.map +1 -0
  151. package/dist/src-DlSHshb2.js +6963 -0
  152. package/dist/src-DlSHshb2.js.map +1 -0
  153. package/dist/streams/index.d.ts +120 -0
  154. package/dist/streams/index.js +107 -0
  155. package/dist/streams/index.js.map +1 -0
  156. package/dist/streams-D6tJ0NN9.d.ts +370 -0
  157. package/dist/streams-DZKgAj9b.js +709 -0
  158. package/dist/streams-DZKgAj9b.js.map +1 -0
  159. package/dist/sub-routing.d.ts +12 -12
  160. package/dist/surface-bZZJqBka.js +17 -0
  161. package/dist/surface-bZZJqBka.js.map +1 -0
  162. package/dist/tasks/index.d.ts +64 -0
  163. package/dist/tasks/index.js +2 -0
  164. package/dist/tasks-BRJ5zgya.d.ts +517 -0
  165. package/dist/tasks-ylZgBjhj.js +1656 -0
  166. package/dist/tasks-ylZgBjhj.js.map +1 -0
  167. package/dist/text-segment-joiner-BtAFQSA_.js +57 -0
  168. package/dist/text-segment-joiner-BtAFQSA_.js.map +1 -0
  169. package/dist/text-stream-CpdiKrJB.js +272 -0
  170. package/dist/text-stream-CpdiKrJB.js.map +1 -0
  171. package/dist/tokens-nHAKcN6M.js +52 -0
  172. package/dist/tokens-nHAKcN6M.js.map +1 -0
  173. package/dist/tool-schema-CBjGPrsQ.js +31 -0
  174. package/dist/tool-schema-CBjGPrsQ.js.map +1 -0
  175. package/dist/types-B7LojTe4.d.ts +202 -0
  176. package/dist/types-_Faxb570.d.ts +439 -0
  177. package/dist/voice/client.d.ts +226 -0
  178. package/dist/voice/client.js +932 -0
  179. package/dist/voice/client.js.map +1 -0
  180. package/dist/voice/errors.d.ts +43 -0
  181. package/dist/voice/errors.js +41 -0
  182. package/dist/voice/errors.js.map +1 -0
  183. package/dist/voice/index.d.ts +271 -0
  184. package/dist/voice/index.js +1812 -0
  185. package/dist/voice/index.js.map +1 -0
  186. package/dist/voice/react.d.ts +167 -0
  187. package/dist/voice/react.js +234 -0
  188. package/dist/voice/react.js.map +1 -0
  189. package/dist/voice/sfu.d.ts +71 -0
  190. package/dist/voice/sfu.js +157 -0
  191. package/dist/voice/sfu.js.map +1 -0
  192. package/dist/voice/text.d.ts +6 -0
  193. package/dist/voice/text.js +2 -0
  194. package/dist/voice/types.d.ts +58 -0
  195. package/dist/voice/types.js +18 -0
  196. package/dist/voice/types.js.map +1 -0
  197. package/dist/voice/workers-ai.d.ts +136 -0
  198. package/dist/voice/workers-ai.js +568 -0
  199. package/dist/voice/workers-ai.js.map +1 -0
  200. package/dist/websockets/index.d.ts +192 -0
  201. package/dist/websockets/index.js +2 -0
  202. package/dist/websockets-DUfRHPRq.js +502 -0
  203. package/dist/websockets-DUfRHPRq.js.map +1 -0
  204. package/dist/workflow-types.d.ts +25 -25
  205. package/dist/workflows.d.ts +22 -22
  206. package/dist/workflows.js +2 -1
  207. package/dist/workflows.js.map +1 -1
  208. package/dist/{ws-chat-transport-CIoOBbO7.js → ws-chat-transport-rWwta645.js} +152 -15
  209. package/dist/ws-chat-transport-rWwta645.js.map +1 -0
  210. package/docs/agent-class.md +29 -87
  211. package/docs/agent-tools.md +2 -1
  212. package/docs/channels.md +323 -0
  213. package/docs/chat-agents.md +19 -25
  214. package/docs/context.md +131 -0
  215. package/docs/durable-execution.md +1 -1
  216. package/docs/http-websockets.md +1 -11
  217. package/docs/human-in-the-loop.md +1 -1
  218. package/docs/index.md +16 -12
  219. package/docs/lifecycle.md +370 -0
  220. package/docs/long-running-agents.md +4 -6
  221. package/docs/mcp-client.md +55 -0
  222. package/docs/mcp-servers.md +5 -1
  223. package/docs/observability.md +11 -11
  224. package/docs/resumable-streaming.md +2 -2
  225. package/docs/routing.md +105 -0
  226. package/docs/scheduling.md +175 -15
  227. package/docs/server-driven-messages.md +1 -1
  228. package/docs/sessions.md +237 -871
  229. package/docs/streams.md +213 -0
  230. package/docs/sub-agents.md +185 -125
  231. package/docs/tasks.md +246 -0
  232. package/docs/voice.md +745 -0
  233. package/package.json +144 -33
  234. package/dist/cli/index.js +0 -26
  235. package/dist/cli/index.js.map +0 -1
  236. package/dist/client-invoker-VNZ7X0nn.js.map +0 -1
  237. package/dist/client-zqKcsyFa.js.map +0 -1
  238. package/dist/compaction-helpers-iiKMr2TQ.js +0 -340
  239. package/dist/compaction-helpers-iiKMr2TQ.js.map +0 -1
  240. package/dist/compaction-helpers-wUz6M3us.d.ts +0 -621
  241. package/dist/experimental/memory/session/index.d.ts +0 -670
  242. package/dist/experimental/memory/session/index.js +0 -2374
  243. package/dist/experimental/memory/session/index.js.map +0 -1
  244. package/dist/experimental/memory/utils/index.d.ts +0 -96
  245. package/dist/experimental/memory/utils/index.js +0 -79
  246. package/dist/experimental/memory/utils/index.js.map +0 -1
  247. package/dist/handler-stateless-CIkKPETH.js.map +0 -1
  248. package/dist/index.js.map +0 -1
  249. package/dist/internal_context-Dg4Cgjcu.d.ts +0 -37
  250. package/dist/internal_context.js.map +0 -1
  251. package/dist/mcp/client.d.ts +0 -42
  252. package/dist/mcp/do-oauth-client-provider.js.map +0 -1
  253. package/dist/mcp/x402.js.map +0 -1
  254. package/dist/protocol-Dqc2MQxo.js.map +0 -1
  255. package/dist/tool-output-truncation-CNnnGZQ3.js +0 -98
  256. package/dist/tool-output-truncation-CNnnGZQ3.js.map +0 -1
  257. package/dist/ws-chat-transport-CIoOBbO7.js.map +0 -1
  258. /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 "../handler-stateless-C_bo-Ytq.js";
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 "../handler-stateless-CIkKPETH.js";
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 "../../internal_context.js";
2
- import { r as writeSpanAttributes, t as tracer } from "../../cloudflare-BduZwmYK.js";
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
- [TraceAttribute.GenAI.AgentID]: input.context?.agentId,
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) {