@sentry/server-utils 11.22.0 → 11.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 (241) hide show
  1. package/build/THIRD-PARTY-LICENSES.txt +10 -10
  2. package/build/cjs/chunks/config.js +2 -2
  3. package/build/cjs/chunks/config.js.map +1 -1
  4. package/build/cjs/chunks/core.js +0 -1
  5. package/build/cjs/chunks/exports.js +72 -27
  6. package/build/cjs/chunks/exports.js.map +1 -1
  7. package/build/cjs/chunks/module-details-from-path.js +427 -428
  8. package/build/cjs/chunks/module-details-from-path.js.map +1 -1
  9. package/build/cjs/chunks/moduleInjectedTransform.js +419 -421
  10. package/build/cjs/chunks/moduleInjectedTransform.js.map +1 -1
  11. package/build/cjs/chunks/rolldown-runtime.js +8 -6
  12. package/build/cjs/index.js +42 -25
  13. package/build/cjs/index.js.map +1 -1
  14. package/build/cjs/index.request-handler.js +2 -2
  15. package/build/cjs/orchestrion/bundler/bun.js +1 -1
  16. package/build/cjs/orchestrion/bundler/esbuild.js +1 -1
  17. package/build/cjs/orchestrion/bundler/rollup.js +1 -1
  18. package/build/cjs/orchestrion/bundler/vite.js +2 -2
  19. package/build/cjs/orchestrion/bundler/webpack-loader.js +2 -2
  20. package/build/cjs/orchestrion/bundler/webpack.js +4 -4
  21. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/defineProperty.js +1 -1
  22. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/assertClassBrand.js +1 -1
  23. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/checkPrivateRedeclaration.js +1 -1
  24. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/classPrivateFieldGet2.js +1 -1
  25. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/classPrivateFieldInitSpec.js +1 -1
  26. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/classPrivateFieldSet2.js +1 -1
  27. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/classPrivateMethodInitSpec.js +1 -1
  28. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/defineProperty.js +1 -1
  29. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/toPrimitive.js +1 -1
  30. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/toPropertyKey.js +1 -1
  31. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/esm/typeof.js +1 -1
  32. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/toPrimitive.js +1 -1
  33. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/toPropertyKey.js +1 -1
  34. package/build/esm/_virtual/{_@oxc-project_runtime@0.158.0 → _@oxc-project_runtime@0.160.0}/helpers/typeof.js +1 -1
  35. package/build/esm/_virtual/_rolldown/runtime.js +8 -6
  36. package/build/esm/ai/anthropic-ai/index.js +2 -2
  37. package/build/esm/ai/anthropic-ai/streaming.js +11 -2
  38. package/build/esm/ai/anthropic-ai/streaming.js.map +1 -1
  39. package/build/esm/ai/anthropic-ai/tool-runner.js +3 -3
  40. package/build/esm/ai/anthropic-ai/utils.js +2 -2
  41. package/build/esm/ai/core/span-hooks.js +1 -1
  42. package/build/esm/ai/core/time-to-first-chunk.js +1 -1
  43. package/build/esm/ai/core/utils.js +2 -2
  44. package/build/esm/ai/flue/index.js +2 -2
  45. package/build/esm/ai/flue/utils.js +2 -2
  46. package/build/esm/ai/google-genai/decisions.js +1 -1
  47. package/build/esm/ai/google-genai/index.js +2 -2
  48. package/build/esm/ai/google-genai/streaming.js +9 -2
  49. package/build/esm/ai/google-genai/streaming.js.map +1 -1
  50. package/build/esm/ai/google-genai/utils.js +1 -1
  51. package/build/esm/ai/langchain/embeddings.js +3 -3
  52. package/build/esm/ai/langchain/index.js +15 -9
  53. package/build/esm/ai/langchain/index.js.map +1 -1
  54. package/build/esm/ai/langchain/typesafe-classifier.js +2 -2
  55. package/build/esm/ai/langchain/utils.js +3 -4
  56. package/build/esm/ai/langchain/utils.js.map +1 -1
  57. package/build/esm/ai/langgraph/agent-span.js +3 -3
  58. package/build/esm/ai/langgraph/index.js +1 -1
  59. package/build/esm/ai/langgraph/stream.js +2 -2
  60. package/build/esm/ai/langgraph/utils.js +3 -3
  61. package/build/esm/ai/mastra/index.js +2 -2
  62. package/build/esm/ai/mastra/span-registry.js +1 -1
  63. package/build/esm/ai/mastra/utils.js +2 -2
  64. package/build/esm/ai/mistral/index.js +2 -2
  65. package/build/esm/ai/mistral/streaming.js +18 -9
  66. package/build/esm/ai/mistral/streaming.js.map +1 -1
  67. package/build/esm/ai/openai/index.js +2 -2
  68. package/build/esm/ai/openai/mapping.js +1 -1
  69. package/build/esm/ai/openai/streaming.js +15 -2
  70. package/build/esm/ai/openai/streaming.js.map +1 -1
  71. package/build/esm/ai/openai/utils.js +2 -2
  72. package/build/esm/ai/pi-ai/chat.js +12 -4
  73. package/build/esm/ai/pi-ai/chat.js.map +1 -1
  74. package/build/esm/ai/pi-ai/providers.js +1 -1
  75. package/build/esm/ai/pi-ai/recording.js +1 -1
  76. package/build/esm/ai/pi-durable/index.js +2 -2
  77. package/build/esm/ai/pi-durable/runs.js +3 -3
  78. package/build/esm/ai/pi-durable/tools.js +3 -3
  79. package/build/esm/ai/tanstack-ai/index.js +2 -2
  80. package/build/esm/ai/tanstack-ai/utils.js +2 -2
  81. package/build/esm/ai/typesafe/index.js +3 -3
  82. package/build/esm/ai/workers-ai/index.js +2 -2
  83. package/build/esm/ai/workers-ai/streaming.js +7 -5
  84. package/build/esm/ai/workers-ai/streaming.js.map +1 -1
  85. package/build/esm/ai/workers-ai/utils.js +3 -3
  86. package/build/esm/async-context.js +1 -1
  87. package/build/esm/cron/common.js +1 -1
  88. package/build/esm/cron/cron.js +1 -1
  89. package/build/esm/cron/node-cron.js +1 -1
  90. package/build/esm/cron/node-schedule.js +1 -1
  91. package/build/esm/eve.js +1 -1
  92. package/build/esm/integrations/amqplib.js +3 -3
  93. package/build/esm/integrations/anthropic.js +3 -4
  94. package/build/esm/integrations/anthropic.js.map +1 -1
  95. package/build/esm/integrations/aws-sdk/index.js +3 -3
  96. package/build/esm/integrations/aws-sdk/services/MessageAttributes.js +1 -1
  97. package/build/esm/integrations/aws-sdk/services/bedrock-runtime-content.js +2 -2
  98. package/build/esm/integrations/aws-sdk/services/bedrock-runtime.js +3 -3
  99. package/build/esm/integrations/aws-sdk/services/dynamodb.js +1 -1
  100. package/build/esm/integrations/aws-sdk/services/lambda.js +2 -2
  101. package/build/esm/integrations/aws-sdk/services/sns.js +3 -3
  102. package/build/esm/integrations/aws-sdk/services/sqs.js +2 -2
  103. package/build/esm/integrations/bullmq/meter.js +2 -2
  104. package/build/esm/integrations/bullmq/tracer.js +2 -2
  105. package/build/esm/integrations/clickhouse.js +3 -3
  106. package/build/esm/integrations/cron.js +1 -1
  107. package/build/esm/integrations/dataloader.js +3 -3
  108. package/build/esm/integrations/express/error-handler.js +1 -1
  109. package/build/esm/integrations/express/index.js +1 -1
  110. package/build/esm/integrations/express/instrumentation.js +3 -3
  111. package/build/esm/integrations/fastify/errors.js +1 -1
  112. package/build/esm/integrations/fastify/index.js +1 -1
  113. package/build/esm/integrations/fastify/instrumentation.js +3 -3
  114. package/build/esm/integrations/firebase/firestore.js +2 -2
  115. package/build/esm/integrations/firebase/functions.js +1 -1
  116. package/build/esm/integrations/firebase/index.js +1 -1
  117. package/build/esm/integrations/flue.js +1 -1
  118. package/build/esm/integrations/generic-pool.js +3 -3
  119. package/build/esm/integrations/google-genai.js +2 -2
  120. package/build/esm/integrations/graphql/graphql-dc-subscriber.js +3 -3
  121. package/build/esm/integrations/graphql/index.js +1 -1
  122. package/build/esm/integrations/graphql/resolvers.js +2 -2
  123. package/build/esm/integrations/graphql/spans.js +3 -3
  124. package/build/esm/integrations/groq.js +1 -1
  125. package/build/esm/integrations/hapi/hapi-error-handler.js +1 -1
  126. package/build/esm/integrations/hapi/hapi-utils.js +4 -5
  127. package/build/esm/integrations/hapi/hapi-utils.js.map +1 -1
  128. package/build/esm/integrations/hapi/index.js +1 -1
  129. package/build/esm/integrations/hono/applyPatches.js +1 -1
  130. package/build/esm/integrations/hono/createHonoMiddleware.js +1 -1
  131. package/build/esm/integrations/hono/honoIntegration.js +3 -3
  132. package/build/esm/integrations/hono/matchResult.js +1 -1
  133. package/build/esm/integrations/hono/middlewareHandlers.js +2 -2
  134. package/build/esm/integrations/hono/patchAppRequest.js +3 -3
  135. package/build/esm/integrations/hono/patchAppUse.js +1 -1
  136. package/build/esm/integrations/hono/patchRoute.js +1 -1
  137. package/build/esm/integrations/hono/wrapMiddlewareSpan.js +3 -3
  138. package/build/esm/integrations/kafkajs/consumer.js +2 -2
  139. package/build/esm/integrations/kafkajs/index.js +1 -1
  140. package/build/esm/integrations/kafkajs/spans.js +2 -2
  141. package/build/esm/integrations/knex.js +3 -3
  142. package/build/esm/integrations/koa/index.js +3 -3
  143. package/build/esm/integrations/langchain.js +1 -1
  144. package/build/esm/integrations/langgraph.js +1 -1
  145. package/build/esm/integrations/lru-memoizer.js +1 -1
  146. package/build/esm/integrations/mastra-classifier.js +2 -2
  147. package/build/esm/integrations/mastra.js +4 -4
  148. package/build/esm/integrations/mcp-server.js +2 -2
  149. package/build/esm/integrations/mistral.js +2 -2
  150. package/build/esm/integrations/mongodb/index.js +1 -1
  151. package/build/esm/integrations/mongodb/mongodb-span.js +1 -1
  152. package/build/esm/integrations/mongoose/index.js +1 -1
  153. package/build/esm/integrations/mongoose/mongoose-dc-subscriber.js +3 -3
  154. package/build/esm/integrations/mongoose/mongoose-legacy-span.js +1 -1
  155. package/build/esm/integrations/mysql.js +3 -3
  156. package/build/esm/integrations/mysql2/index.js +1 -1
  157. package/build/esm/integrations/mysql2/mysql2-dc-subscriber.js +3 -3
  158. package/build/esm/integrations/neon.js +3 -3
  159. package/build/esm/integrations/nitro/captureStorageEvents.js +3 -3
  160. package/build/esm/integrations/nitro/captureTracingEvents.js +3 -3
  161. package/build/esm/integrations/nitro/index.js +1 -1
  162. package/build/esm/integrations/nitro/setServerTimingHeaders.js +1 -1
  163. package/build/esm/integrations/node-cron.js +1 -1
  164. package/build/esm/integrations/node-schedule.js +1 -1
  165. package/build/esm/integrations/openai-compatible.js +2 -2
  166. package/build/esm/integrations/openai.js +2 -2
  167. package/build/esm/integrations/pg.js +3 -3
  168. package/build/esm/integrations/pi-ai.js +1 -1
  169. package/build/esm/integrations/pi-durable.js +1 -1
  170. package/build/esm/integrations/pino.js +1 -1
  171. package/build/esm/integrations/postgresjs/index.js +3 -3
  172. package/build/esm/integrations/postgresjs/postgresjs-dc-subscriber.js +3 -3
  173. package/build/esm/integrations/postgresjs-wrapper.js +1 -1
  174. package/build/esm/integrations/prisma/index.js +1 -1
  175. package/build/esm/integrations/prisma/orchestrion.js +8 -12
  176. package/build/esm/integrations/prisma/orchestrion.js.map +1 -1
  177. package/build/esm/integrations/prisma/tracing-helper.js +2 -2
  178. package/build/esm/integrations/redis/index.js +3 -3
  179. package/build/esm/integrations/redis/ioredis-channel-subscriber.js +3 -3
  180. package/build/esm/integrations/redis/redis-cache.js +1 -1
  181. package/build/esm/integrations/redis/redis-dc-subscriber.js +3 -3
  182. package/build/esm/integrations/serveTunnel.js +2 -2
  183. package/build/esm/integrations/tanstack-ai.js +1 -1
  184. package/build/esm/integrations/tedious.js +4 -4
  185. package/build/esm/integrations/together-ai.js +1 -1
  186. package/build/esm/integrations/typesafe.js +1 -1
  187. package/build/esm/integrations/vercel-ai/extract.js +1 -1
  188. package/build/esm/integrations/vercel-ai/index.js +1 -1
  189. package/build/esm/integrations/vercel-ai/util.js +21 -2
  190. package/build/esm/integrations/vercel-ai/util.js.map +1 -1
  191. package/build/esm/integrations/vercel-ai/vercel-ai-dc-subscriber.js +3 -3
  192. package/build/esm/integrations/vercel-ai/vercel-ai-orchestrion-subscriber.js +1 -1
  193. package/build/esm/integrations/vercel-ai/vercel-ai-tool-telemetry.js +3 -3
  194. package/build/esm/integrations/winston.js +1 -1
  195. package/build/esm/opentelemetry.js +1 -1
  196. package/build/esm/orchestrion/bundler/esbuild.js +1 -1
  197. package/build/esm/orchestrion/bundler/externalRequire.js +1 -1
  198. package/build/esm/orchestrion/bundler/rollup.js +1 -1
  199. package/build/esm/orchestrion/bundler/webpack-loader.js +1 -1
  200. package/build/esm/orchestrion/bundler/webpack.js +1 -1
  201. package/build/esm/orchestrion/config/index.js +1 -1
  202. package/build/esm/orchestrion/config/mistral.js +1 -1
  203. package/build/esm/orchestrion/config/mistral.js.map +1 -1
  204. package/build/esm/orchestrion/detect.js +1 -1
  205. package/build/esm/orchestrion/instrumentation.js +1 -1
  206. package/build/esm/package.json +1 -1
  207. package/build/esm/tracing-channel.js +1 -2
  208. package/build/esm/tracing-channel.js.map +1 -1
  209. package/build/esm/utils/cronMonitoring.js +1 -1
  210. package/build/esm/utils/diagnosticsChannel.js +1 -1
  211. package/build/esm/utils/module.js +2 -2
  212. package/build/esm/utils/module.js.map +1 -1
  213. package/build/esm/utils/monitorStream.js +1 -1
  214. package/build/esm/utils/serverTimingTraceHeader.js +1 -1
  215. package/build/esm/utils/tunnelServer.js +1 -1
  216. package/build/esm/utils/wrapServerRequestHandler.js +4 -4
  217. package/build/esm/vendored/@apm-js-collab/code-transformer/lib/matcher.js +3 -3
  218. package/build/esm/vendored/@apm-js-collab/code-transformer/lib/transformer.js +5 -5
  219. package/build/esm/vendored/@apm-js-collab/code-transformer-bundler-plugins/dist/esm/core-BiWIBBjf.js +1 -1
  220. package/build/esm/vendored/@apm-js-collab/code-transformer-bundler-plugins/dist/esm/webpack-loader-factory.js +1 -1
  221. package/build/esm/vendored/esquery/dist/esquery.min.js +1 -0
  222. package/build/esm/vendored/esquery/dist/esquery.min.js.map +1 -1
  223. package/build/esm/vendored/meriyah/dist/meriyah.js +416 -418
  224. package/build/esm/vendored/meriyah/dist/meriyah.js.map +1 -1
  225. package/build/esm/vendored/meriyah/dist/meriyah2.js +416 -418
  226. package/build/esm/vendored/meriyah/dist/meriyah2.js.map +1 -1
  227. package/build/esm/vendored/source-map/lib/base64-vlq.js +1 -1
  228. package/build/esm/vendored/source-map/lib/base64-vlq.js.map +1 -1
  229. package/build/types/ai/anthropic-ai/streaming.d.ts.map +1 -1
  230. package/build/types/ai/anthropic-ai/types.d.ts +1 -0
  231. package/build/types/ai/anthropic-ai/types.d.ts.map +1 -1
  232. package/build/types/ai/google-genai/streaming.d.ts.map +1 -1
  233. package/build/types/ai/langchain/index.d.ts.map +1 -1
  234. package/build/types/ai/mistral/streaming.d.ts.map +1 -1
  235. package/build/types/ai/openai/streaming.d.ts.map +1 -1
  236. package/build/types/ai/pi-ai/chat.d.ts.map +1 -1
  237. package/build/types/ai/workers-ai/streaming.d.ts.map +1 -1
  238. package/build/types/integrations/vercel-ai/util.d.ts.map +1 -1
  239. package/build/types/orchestrion/config/mistral.d.ts +3 -3
  240. package/build/types/tracing-channel.d.ts.map +1 -1
  241. package/package.json +3 -3
@@ -1,2 +1 @@
1
1
  require("./core-BiWIBBjf.js");
2
- require("./instrumentation-serde-C-Xxv-jj.js");
@@ -1,10 +1,10 @@
1
- const require_debug_build = require("./debug-build.js");
2
- const require_pi_ai = require("./pi-ai.js");
3
1
  let _sentry_conventions_attributes = require("@sentry/conventions/attributes");
4
2
  let _sentry_core = require("@sentry/core");
5
3
  let node_async_hooks = require("node:async_hooks");
6
4
  let _opentelemetry_api = require("@opentelemetry/api");
5
+ const require_debug_build = require("./debug-build.js");
7
6
  let _sentry_conventions_op = require("@sentry/conventions/op");
7
+ const require_pi_ai = require("./pi-ai.js");
8
8
  let _sentry_core_server = require("@sentry/core/server");
9
9
  // src/utils/setHttpServerSpanRouteAttribute.ts
10
10
  /**
@@ -869,6 +869,19 @@ function processResponsesApiEvent(streamEvent, state, recordOutputs, span) {
869
869
  }
870
870
  }
871
871
  /**
872
+ * Whether an event carries new model output, which is what `gen_ai.response.time_to_first_chunk` measures.
873
+ * Role-only, usage-only, finish and lifecycle (`response.created`, ...) events do not.
874
+ */
875
+ function hasOutput$4(event) {
876
+ if (isChatCompletionChunk(event)) return (event.choices ?? []).some(({ delta }) => !!(delta?.content || delta?.refusal || delta?.tool_calls?.length || delta?.function_call));
877
+ if (isResponsesApiStreamEvent(event)) {
878
+ const { type } = event;
879
+ if (type.endsWith(".delta")) return !!event.delta;
880
+ return type === "response.output_item.added" && event.item?.type === "function_call";
881
+ }
882
+ return false;
883
+ }
884
+ /**
872
885
  * Instruments a stream of OpenAI events, updating the provided span with relevant attributes and
873
886
  * optionally recording output text. This function yields each event from the input stream as it is processed.
874
887
  *
@@ -897,7 +910,7 @@ async function* instrumentStream$1(stream, span, recordOutputs, beforeSpanEnd) {
897
910
  const result = {};
898
911
  try {
899
912
  for await (const event of stream) {
900
- recordTimeToFirstChunk(span);
913
+ if (hasOutput$4(event)) recordTimeToFirstChunk(span);
901
914
  if (isChatCompletionChunk(event)) processChatCompletionChunk(event, state, recordOutputs);
902
915
  else if (isResponsesApiStreamEvent(event)) processResponsesApiEvent(event, state, recordOutputs, span);
903
916
  yield event;
@@ -1302,6 +1315,15 @@ function handleContentBlockStop(event, state) {
1302
1315
  delete state.activeToolBlocks[event.index];
1303
1316
  }
1304
1317
  /**
1318
+ * Whether an event carries new model output, which is what `gen_ai.response.time_to_first_chunk` measures.
1319
+ * `message_start`, `ping`, empty block starts, signatures and stop events do not.
1320
+ */
1321
+ function hasOutput$3(event) {
1322
+ if (event.type === "content_block_start") return !!(event.content_block?.name || event.content_block?.type === "redacted_thinking");
1323
+ if (event.type === "content_block_delta") return !!(event.delta?.text || event.delta?.partial_json || event.delta?.thinking);
1324
+ return false;
1325
+ }
1326
+ /**
1305
1327
  * Processes an event
1306
1328
  * @param event - The event to process
1307
1329
  * @param state - The state of the streaming process
@@ -1311,7 +1333,7 @@ function handleContentBlockStop(event, state) {
1311
1333
  function processEvent$1(event, state, recordOutputs, span) {
1312
1334
  if (!(event && typeof event === "object")) return;
1313
1335
  if (isErrorEvent(event, span)) return;
1314
- recordTimeToFirstChunk(span);
1336
+ if (hasOutput$3(event)) recordTimeToFirstChunk(span);
1315
1337
  handleMessageMetadata(event, state);
1316
1338
  handleContentBlockStart(event, state);
1317
1339
  handleContentBlockDelta(event, state, recordOutputs);
@@ -1958,6 +1980,13 @@ function handleCandidateContent(chunk, state, recordOutputs) {
1958
1980
  }
1959
1981
  }
1960
1982
  /**
1983
+ * Whether a chunk carries new model output, which is what `gen_ai.response.time_to_first_chunk` measures.
1984
+ * Chunks with only usage, a finish reason or a thought signature do not.
1985
+ */
1986
+ function hasOutput$2(chunk) {
1987
+ return (chunk.candidates ?? []).some((candidate) => (candidate?.content?.parts ?? []).some((part) => !!(part.text || part.functionCall || part.inlineData)));
1988
+ }
1989
+ /**
1961
1990
  * Processes a single chunk from the Google GenAI stream
1962
1991
  * @param chunk - The chunk to process
1963
1992
  * @param state - The state of the streaming process
@@ -1966,7 +1995,7 @@ function handleCandidateContent(chunk, state, recordOutputs) {
1966
1995
  */
1967
1996
  function processChunk$1(chunk, state, recordOutputs, span) {
1968
1997
  if (!chunk || isErrorChunk(chunk, span)) return;
1969
- recordTimeToFirstChunk(span);
1998
+ if (hasOutput$2(chunk)) recordTimeToFirstChunk(span);
1970
1999
  handleResponseMetadata(chunk, state);
1971
2000
  handleCandidateContent(chunk, state, recordOutputs);
1972
2001
  }
@@ -2419,15 +2448,25 @@ function processChunk(chunk, state, recordOutputs) {
2419
2448
  if (choice.finishReason) state.finishReasons.push(choice.finishReason);
2420
2449
  }
2421
2450
  }
2451
+ /**
2452
+ * Whether a chunk carries new model output, which is what `gen_ai.response.time_to_first_chunk` measures.
2453
+ * Role-only, usage-only and finish chunks do not.
2454
+ */
2455
+ function hasOutput$1(chunk) {
2456
+ return (chunk.choices ?? []).some(({ delta }) => !!(contentToString(delta?.content) || delta?.toolCalls?.length));
2457
+ }
2422
2458
  /** Mistral yields `CompletionEvent` objects that wrap the chunk under `data`. */
2423
- function processEvent(event, state, recordOutputs) {
2459
+ function processEvent(event, state, recordOutputs, span) {
2424
2460
  const chunk = event?.data;
2425
- if (chunk && typeof chunk === "object") processChunk(chunk, state, recordOutputs);
2461
+ if (chunk && typeof chunk === "object") {
2462
+ if (hasOutput$1(chunk)) recordTimeToFirstChunk(span);
2463
+ processChunk(chunk, state, recordOutputs);
2464
+ }
2426
2465
  }
2427
- async function* instrumentIterator(iterate, state, recordOutputs, claim, settle) {
2466
+ async function* instrumentIterator(iterate, state, recordOutputs, span, claim, settle) {
2428
2467
  try {
2429
2468
  for await (const event of { [Symbol.asyncIterator]: iterate }) {
2430
- if (claim("iterator")) processEvent(event, state, recordOutputs);
2469
+ if (claim("iterator")) processEvent(event, state, recordOutputs, span);
2431
2470
  yield event;
2432
2471
  }
2433
2472
  } catch (error) {
@@ -2437,7 +2476,7 @@ async function* instrumentIterator(iterate, state, recordOutputs, claim, settle)
2437
2476
  settle();
2438
2477
  }
2439
2478
  }
2440
- function wrapReader(reader, state, recordOutputs, claim, settle, markCancelled) {
2479
+ function wrapReader(reader, state, recordOutputs, span, claim, settle, markCancelled) {
2441
2480
  const originalRead = reader.read;
2442
2481
  const originalCancel = reader.cancel;
2443
2482
  const read = () => originalRead.call(reader);
@@ -2447,7 +2486,7 @@ function wrapReader(reader, state, recordOutputs, claim, settle, markCancelled)
2447
2486
  try {
2448
2487
  const result = await read();
2449
2488
  if (result.done) settle();
2450
- else if (claim("reader")) processEvent(result.value, state, recordOutputs);
2489
+ else if (claim("reader")) processEvent(result.value, state, recordOutputs, span);
2451
2490
  return result;
2452
2491
  } catch (error) {
2453
2492
  settle(error);
@@ -2517,7 +2556,6 @@ function instrumentEventStream(stream, span, recordOutputs, abortSignal, beforeS
2517
2556
  let settled = false;
2518
2557
  let cancelled = false;
2519
2558
  const claim = (candidate) => {
2520
- recordTimeToFirstChunk(span);
2521
2559
  consumer ?? (consumer = candidate);
2522
2560
  return consumer === candidate;
2523
2561
  };
@@ -2539,12 +2577,12 @@ function instrumentEventStream(stream, span, recordOutputs, abortSignal, beforeS
2539
2577
  toolCalls
2540
2578
  }, recordOutputs, () => beforeSpanEnd?.(error !== void 0 && !cancelled ? { error } : {}));
2541
2579
  };
2542
- const instrumented = instrumentIterator(stream[Symbol.asyncIterator].bind(stream), state, recordOutputs, claim, settle);
2580
+ const instrumented = instrumentIterator(stream[Symbol.asyncIterator].bind(stream), state, recordOutputs, span, claim, settle);
2543
2581
  stream[Symbol.asyncIterator] = () => instrumented;
2544
2582
  const readable = stream;
2545
2583
  if (typeof readable.getReader === "function") {
2546
2584
  const getReader = readable.getReader.bind(readable);
2547
- readable.getReader = (...args) => wrapReader(getReader(...args), state, recordOutputs, claim, settle, markCancelled);
2585
+ readable.getReader = (...args) => wrapReader(getReader(...args), state, recordOutputs, span, claim, settle, markCancelled);
2548
2586
  }
2549
2587
  if (typeof readable.cancel === "function") {
2550
2588
  const cancel = readable.cancel.bind(readable);
@@ -2998,17 +3036,19 @@ function accumulateStreamingToolCalls(toolCalls, accumulator) {
2998
3036
  * Handles both the native Workers AI shape (top-level `response`/`tool_calls`) and the
2999
3037
  * OpenAI-compatible shape (`choices[].delta.content`/`choices[].delta.tool_calls`), because
3000
3038
  * the same `run()` call transparently yields either format depending on the model.
3039
+ *
3040
+ * Returns whether the line carries new model output, which is what `gen_ai.response.time_to_first_chunk` measures.
3001
3041
  */
3002
3042
  function processLine(line, state, recordOutputs, toolCallAccumulator) {
3003
3043
  const trimmed = line.trim();
3004
- if (!trimmed.startsWith("data:")) return;
3044
+ if (!trimmed.startsWith("data:")) return false;
3005
3045
  const data = trimmed.slice(5).trim();
3006
- if (!data || data === "[DONE]") return;
3046
+ if (!data || data === "[DONE]") return false;
3007
3047
  let parsed;
3008
3048
  try {
3009
3049
  parsed = JSON.parse(data);
3010
3050
  } catch {
3011
- return;
3051
+ return false;
3012
3052
  }
3013
3053
  if (parsed.usage) {
3014
3054
  if (typeof parsed.usage.prompt_tokens === "number") state.promptTokens = parsed.usage.prompt_tokens;
@@ -3026,6 +3066,7 @@ function processLine(line, state, recordOutputs, toolCallAccumulator) {
3026
3066
  if (typeof choice.finish_reason === "string") state.finishReasons.push(choice.finish_reason);
3027
3067
  }
3028
3068
  }
3069
+ return !!(typeof parsed.response === "string" && parsed.response || Array.isArray(parsed.tool_calls) && parsed.tool_calls.length || parsed.choices?.some(({ delta }) => typeof delta?.content === "string" && delta.content || delta?.tool_calls?.length));
3029
3070
  }
3030
3071
  /**
3031
3072
  * Wrap a Workers AI streaming response (a server-sent-events `ReadableStream`) so we can
@@ -3069,7 +3110,7 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal, bef
3069
3110
  const flushBuffer = (isDone) => {
3070
3111
  const lines = buffer.split("\n");
3071
3112
  buffer = isDone ? "" : lines.pop() ?? "";
3072
- for (const line of lines) processLine(line, state, recordOutputs, toolCallAccumulator);
3113
+ for (const line of lines) if (processLine(line, state, recordOutputs, toolCallAccumulator)) recordTimeToFirstChunk(span);
3073
3114
  };
3074
3115
  return new ReadableStream({
3075
3116
  async pull(controller) {
@@ -3082,7 +3123,6 @@ function instrumentWorkersAiStream(stream, span, recordOutputs, abortSignal, bef
3082
3123
  controller.close();
3083
3124
  return;
3084
3125
  }
3085
- recordTimeToFirstChunk(span);
3086
3126
  buffer += decoder.decode(value, { stream: true });
3087
3127
  flushBuffer(false);
3088
3128
  controller.enqueue(value);
@@ -3353,8 +3393,7 @@ function baseRequestAttributes(system, modelName, serialized, invocationParams,
3353
3393
  * messages to align with the chat schema used elsewhere.
3354
3394
  */
3355
3395
  function extractLLMRequestAttributes(llm, prompts, recordInputs, invocationParams, langSmithMetadata) {
3356
- const system = langSmithMetadata?.ls_provider;
3357
- const attrs = baseRequestAttributes(system, invocationParams?.model ?? langSmithMetadata?.ls_model_name ?? "unknown", llm, invocationParams, langSmithMetadata);
3396
+ const attrs = baseRequestAttributes(langSmithMetadata?.ls_provider, invocationParams?.model ?? langSmithMetadata?.ls_model_name ?? "unknown", llm, invocationParams, langSmithMetadata);
3358
3397
  if (recordInputs && Array.isArray(prompts) && prompts.length > 0) {
3359
3398
  const messages = prompts.map((p) => ({
3360
3399
  role: "user",
@@ -3727,6 +3766,14 @@ function instrumentLangChainEmbeddings(instance, options) {
3727
3766
  }
3728
3767
  // src/ai/langchain/index.ts
3729
3768
  /**
3769
+ * Whether a streamed token carries new model output, which is what `gen_ai.response.time_to_first_chunk` measures.
3770
+ * Chat models report tool-call chunks with an empty token.
3771
+ */
3772
+ function hasOutput(token, fields) {
3773
+ const toolCallChunks = fields?.chunk?.message?.tool_call_chunks;
3774
+ return !!(token || toolCallChunks?.length);
3775
+ }
3776
+ /**
3730
3777
  * Creates a Sentry callback handler for LangChain
3731
3778
  * Returns a plain object that LangChain will call via duck-typing
3732
3779
  *
@@ -3764,10 +3811,8 @@ function createLangChainCallbackHandler(options = {}) {
3764
3811
  return ancestors;
3765
3812
  };
3766
3813
  const getParentRunSpan = (ancestors, activeSpan) => {
3767
- for (const run of ancestors) {
3768
- if (run.activeSpan !== activeSpan) return;
3769
- if (!(0, _sentry_core.spanIsIgnored)(run.span) || (0, _sentry_core.getRootSpan)(run.span) === run.span) return run.span;
3770
- }
3814
+ const [run] = ancestors;
3815
+ return run && run.activeSpan === activeSpan ? run.span : void 0;
3771
3816
  };
3772
3817
  const startRunSpan = (call, recordOutputs, start) => {
3773
3818
  const { runId, parentRunId } = call;
@@ -3880,9 +3925,9 @@ function createLangChainCallbackHandler(options = {}) {
3880
3925
  }
3881
3926
  }));
3882
3927
  },
3883
- handleLLMNewToken(_token, _idx, runId) {
3928
+ handleLLMNewToken(token, _idx, runId, _parentRunId, _tags, fields) {
3884
3929
  const span = runs.get(runId)?.span;
3885
- if (span) recordTimeToFirstChunk(span);
3930
+ if (span && hasOutput(token, fields)) recordTimeToFirstChunk(span);
3886
3931
  },
3887
3932
  handleLLMEnd(output, runId, _parentRunId, _tags, _extraParams) {
3888
3933
  const run = runs.get(runId);