@sentry/core 10.45.0 → 10.46.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/build/cjs/asyncContext/stackStrategy.js +6 -11
- package/build/cjs/asyncContext/stackStrategy.js.map +1 -1
- package/build/cjs/tracing/ai/messageTruncation.js +57 -39
- package/build/cjs/tracing/ai/messageTruncation.js.map +1 -1
- package/build/cjs/tracing/ai/utils.js +81 -0
- package/build/cjs/tracing/ai/utils.js.map +1 -1
- package/build/cjs/tracing/anthropic-ai/index.js +35 -19
- package/build/cjs/tracing/anthropic-ai/index.js.map +1 -1
- package/build/cjs/tracing/openai/index.js +3 -73
- package/build/cjs/tracing/openai/index.js.map +1 -1
- package/build/cjs/tracing/openai/utils.js +0 -8
- package/build/cjs/tracing/openai/utils.js.map +1 -1
- package/build/cjs/tracing/vercel-ai/index.js +9 -1
- package/build/cjs/tracing/vercel-ai/index.js.map +1 -1
- package/build/cjs/utils/chain-and-copy-promiselike.js +57 -0
- package/build/cjs/utils/chain-and-copy-promiselike.js.map +1 -0
- package/build/cjs/utils/handleCallbackErrors.js +15 -11
- package/build/cjs/utils/handleCallbackErrors.js.map +1 -1
- package/build/cjs/utils/version.js +1 -1
- package/build/esm/asyncContext/stackStrategy.js +6 -11
- package/build/esm/asyncContext/stackStrategy.js.map +1 -1
- package/build/esm/package.json +1 -1
- package/build/esm/tracing/ai/messageTruncation.js +57 -39
- package/build/esm/tracing/ai/messageTruncation.js.map +1 -1
- package/build/esm/tracing/ai/utils.js +81 -1
- package/build/esm/tracing/ai/utils.js.map +1 -1
- package/build/esm/tracing/anthropic-ai/index.js +36 -20
- package/build/esm/tracing/anthropic-ai/index.js.map +1 -1
- package/build/esm/tracing/openai/index.js +4 -74
- package/build/esm/tracing/openai/index.js.map +1 -1
- package/build/esm/tracing/openai/utils.js +1 -8
- package/build/esm/tracing/openai/utils.js.map +1 -1
- package/build/esm/tracing/vercel-ai/index.js +9 -1
- package/build/esm/tracing/vercel-ai/index.js.map +1 -1
- package/build/esm/utils/chain-and-copy-promiselike.js +55 -0
- package/build/esm/utils/chain-and-copy-promiselike.js.map +1 -0
- package/build/esm/utils/handleCallbackErrors.js +15 -11
- package/build/esm/utils/handleCallbackErrors.js.map +1 -1
- package/build/esm/utils/version.js +1 -1
- package/build/types/asyncContext/stackStrategy.d.ts.map +1 -1
- package/build/types/tracing/ai/messageTruncation.d.ts.map +1 -1
- package/build/types/tracing/ai/utils.d.ts +9 -0
- package/build/types/tracing/ai/utils.d.ts.map +1 -1
- package/build/types/tracing/anthropic-ai/index.d.ts.map +1 -1
- package/build/types/tracing/openai/index.d.ts.map +1 -1
- package/build/types/tracing/openai/utils.d.ts +0 -4
- package/build/types/tracing/openai/utils.d.ts.map +1 -1
- package/build/types/tracing/vercel-ai/index.d.ts.map +1 -1
- package/build/types/utils/chain-and-copy-promiselike.d.ts +6 -0
- package/build/types/utils/chain-and-copy-promiselike.d.ts.map +1 -0
- package/build/types/utils/handleCallbackErrors.d.ts.map +1 -1
- package/build/types-ts3.8/tracing/ai/utils.d.ts +9 -0
- package/build/types-ts3.8/tracing/openai/utils.d.ts +0 -4
- package/build/types-ts3.8/utils/chain-and-copy-promiselike.d.ts +6 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/tracing/vercel-ai/index.ts"],"sourcesContent":["/* eslint-disable max-lines */\nimport type { Client } from '../../client';\nimport { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../../semanticAttributes';\nimport type { Event } from '../../types-hoist/event';\nimport type { Span, SpanAttributes, SpanAttributeValue, SpanJSON } from '../../types-hoist/span';\nimport { spanToJSON } from '../../utils/spanUtils';\nimport {\n GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE,\n GEN_AI_INPUT_MESSAGES_ATTRIBUTE,\n GEN_AI_OPERATION_NAME_ATTRIBUTE,\n GEN_AI_OUTPUT_MESSAGES_ATTRIBUTE,\n GEN_AI_REQUEST_MODEL_ATTRIBUTE,\n GEN_AI_RESPONSE_MODEL_ATTRIBUTE,\n GEN_AI_TOOL_CALL_ID_ATTRIBUTE,\n GEN_AI_TOOL_INPUT_ATTRIBUTE,\n GEN_AI_TOOL_NAME_ATTRIBUTE,\n GEN_AI_TOOL_OUTPUT_ATTRIBUTE,\n GEN_AI_TOOL_TYPE_ATTRIBUTE,\n GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE,\n GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE,\n GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE,\n GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE,\n GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE,\n} from '../ai/gen-ai-attributes';\nimport {\n DO_SPAN_NAME_PREFIX,\n EMBEDDINGS_OPS,\n GENERATE_CONTENT_OPS,\n INVOKE_AGENT_OPS,\n RERANK_OPS,\n toolCallSpanContextMap,\n} from './constants';\nimport type { TokenSummary } from './types';\nimport {\n accumulateTokensForParent,\n applyAccumulatedTokens,\n applyToolDescriptionsAndTokens,\n convertAvailableToolsToJsonString,\n getSpanOpFromName,\n requestMessagesFromPrompt,\n} from './utils';\nimport type { OpenAiProviderMetadata, ProviderMetadata } from './vercel-ai-attributes';\nimport {\n AI_MODEL_ID_ATTRIBUTE,\n AI_OPERATION_ID_ATTRIBUTE,\n AI_PROMPT_MESSAGES_ATTRIBUTE,\n AI_PROMPT_TOOLS_ATTRIBUTE,\n AI_RESPONSE_FINISH_REASON_ATTRIBUTE,\n AI_RESPONSE_OBJECT_ATTRIBUTE,\n AI_RESPONSE_PROVIDER_METADATA_ATTRIBUTE,\n AI_RESPONSE_TEXT_ATTRIBUTE,\n AI_RESPONSE_TOOL_CALLS_ATTRIBUTE,\n AI_SCHEMA_ATTRIBUTE,\n AI_TELEMETRY_FUNCTION_ID_ATTRIBUTE,\n AI_TOOL_CALL_ARGS_ATTRIBUTE,\n AI_TOOL_CALL_ID_ATTRIBUTE,\n AI_TOOL_CALL_NAME_ATTRIBUTE,\n AI_TOOL_CALL_RESULT_ATTRIBUTE,\n AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE,\n AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE,\n AI_USAGE_PROMPT_TOKENS_ATTRIBUTE,\n AI_USAGE_TOKENS_ATTRIBUTE,\n AI_VALUES_ATTRIBUTE,\n OPERATION_NAME_ATTRIBUTE,\n} from './vercel-ai-attributes';\n\n/**\n * Maps Vercel AI SDK operation names to OpenTelemetry semantic convention values\n * @see https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/#llm-request-spans\n */\nfunction mapVercelAiOperationName(operationName: string): string {\n // Top-level pipeline operations map to invoke_agent\n if (INVOKE_AGENT_OPS.has(operationName)) {\n return 'invoke_agent';\n }\n // .do* operations are the actual LLM calls\n if (GENERATE_CONTENT_OPS.has(operationName)) {\n return 'generate_content';\n }\n if (EMBEDDINGS_OPS.has(operationName)) {\n return 'embeddings';\n }\n if (RERANK_OPS.has(operationName)) {\n return 'rerank';\n }\n if (operationName === 'ai.toolCall') {\n return 'execute_tool';\n }\n // Return the original value for unknown operations\n return operationName;\n}\n\n/**\n * Post-process spans emitted by the Vercel AI SDK.\n * This is supposed to be used in `client.on('spanStart', ...)\n */\nfunction onVercelAiSpanStart(span: Span): void {\n const { data: attributes, description: name } = spanToJSON(span);\n\n if (!name) {\n return;\n }\n\n // Tool call spans\n // https://ai-sdk.dev/docs/ai-sdk-core/telemetry#tool-call-spans\n if (attributes[AI_TOOL_CALL_NAME_ATTRIBUTE] && attributes[AI_TOOL_CALL_ID_ATTRIBUTE] && name === 'ai.toolCall') {\n processToolCallSpan(span, attributes);\n return;\n }\n\n // V6+ Check if this is a Vercel AI span by checking if the operation ID attribute is present.\n // V5+ Check if this is a Vercel AI span by name pattern.\n if (!attributes[AI_OPERATION_ID_ATTRIBUTE] && !name.startsWith('ai.')) {\n return;\n }\n\n processGenerateSpan(span, name, attributes);\n}\n\nfunction vercelAiEventProcessor(event: Event): Event {\n if (event.type === 'transaction' && event.spans) {\n // Map to accumulate token data by parent span ID\n const tokenAccumulator: Map<string, TokenSummary> = new Map();\n\n // First pass: process all spans and accumulate token data\n for (const span of event.spans) {\n processEndedVercelAiSpan(span);\n\n // Accumulate token data for parent spans\n accumulateTokensForParent(span, tokenAccumulator);\n }\n\n // Second pass: apply tool descriptions and accumulated tokens\n applyToolDescriptionsAndTokens(event.spans, tokenAccumulator);\n\n // Also apply to root when it is the invoke_agent pipeline\n const trace = event.contexts?.trace;\n if (trace?.op === 'gen_ai.invoke_agent') {\n applyAccumulatedTokens(trace, tokenAccumulator);\n }\n }\n\n return event;\n}\n\n/**\n * Tool call structure from Vercel AI SDK\n * Note: V5/V6 use 'input' for arguments, V4 and earlier use 'args'\n */\ninterface VercelToolCall {\n toolCallId: string;\n toolName: string;\n input?: Record<string, unknown> | string; // V5/V6\n args?: string; // V4 and earlier\n}\n\n/**\n * Normalize finish reason to match OpenTelemetry semantic conventions.\n * Valid values: \"stop\", \"length\", \"content_filter\", \"tool_call\", \"error\"\n *\n * Vercel AI SDK uses \"tool-calls\" (plural, with hyphen) which we map to \"tool_call\".\n */\nfunction normalizeFinishReason(finishReason: unknown): string {\n if (typeof finishReason !== 'string') {\n return 'stop';\n }\n\n // Map Vercel AI SDK finish reasons to OpenTelemetry semantic convention values\n switch (finishReason) {\n case 'tool-calls':\n return 'tool_call';\n case 'stop':\n case 'length':\n case 'content_filter':\n case 'error':\n return finishReason;\n default:\n // For unknown values, return as-is (schema allows arbitrary strings)\n return finishReason;\n }\n}\n\n/**\n * Build gen_ai.output.messages from ai.response.text and/or ai.response.toolCalls\n *\n * Format follows OpenTelemetry semantic conventions:\n * [{\"role\": \"assistant\", \"parts\": [...], \"finish_reason\": \"stop\"}]\n *\n * Parts can be:\n * - {\"type\": \"text\", \"content\": \"...\"}\n * - {\"type\": \"tool_call\", \"id\": \"...\", \"name\": \"...\", \"arguments\": \"...\"}\n */\nfunction buildOutputMessages(attributes: Record<string, unknown>): void {\n const responseText = attributes[AI_RESPONSE_TEXT_ATTRIBUTE];\n const responseToolCalls = attributes[AI_RESPONSE_TOOL_CALLS_ATTRIBUTE];\n const finishReason = attributes[AI_RESPONSE_FINISH_REASON_ATTRIBUTE];\n\n // Skip if neither text nor tool calls are present\n if (responseText == null && responseToolCalls == null) {\n return;\n }\n\n const parts: Array<Record<string, unknown>> = [];\n\n // Add text part if present\n if (typeof responseText === 'string' && responseText.length > 0) {\n parts.push({\n type: 'text',\n content: responseText,\n });\n }\n\n // Add tool call parts if present\n if (responseToolCalls != null) {\n try {\n // Tool calls can be a string (JSON) or already parsed array\n const toolCalls: VercelToolCall[] =\n typeof responseToolCalls === 'string' ? JSON.parse(responseToolCalls) : responseToolCalls;\n\n if (Array.isArray(toolCalls)) {\n for (const toolCall of toolCalls) {\n // V5/V6 use 'input', V4 and earlier use 'args'\n const args = toolCall.input ?? toolCall.args;\n parts.push({\n type: 'tool_call',\n id: toolCall.toolCallId,\n name: toolCall.toolName,\n // Handle undefined args: JSON.stringify(undefined) returns undefined, not a string,\n // which would cause the property to be omitted from the final JSON output\n arguments: typeof args === 'string' ? args : JSON.stringify(args ?? {}),\n });\n }\n // Only delete tool calls attribute if we successfully processed them\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete attributes[AI_RESPONSE_TOOL_CALLS_ATTRIBUTE];\n }\n } catch {\n // Ignore parsing errors - tool calls attribute is preserved\n }\n }\n\n // Only set output messages and delete text attribute if we have parts\n if (parts.length > 0) {\n const outputMessage = {\n role: 'assistant',\n parts,\n finish_reason: normalizeFinishReason(finishReason),\n };\n\n attributes[GEN_AI_OUTPUT_MESSAGES_ATTRIBUTE] = JSON.stringify([outputMessage]);\n\n // Remove the text attribute since it's now captured in gen_ai.output.messages\n // Note: tool calls attribute is deleted above only if successfully parsed\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete attributes[AI_RESPONSE_TEXT_ATTRIBUTE];\n }\n}\n\n/**\n * Post-process spans emitted by the Vercel AI SDK.\n */\nfunction processEndedVercelAiSpan(span: SpanJSON): void {\n const { data: attributes, origin } = span;\n\n if (origin !== 'auto.vercelai.otel') {\n return;\n }\n\n renameAttributeKey(attributes, AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE, GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE);\n renameAttributeKey(attributes, AI_USAGE_PROMPT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE);\n renameAttributeKey(attributes, AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE);\n\n // Parent spans (ai.streamText, ai.streamObject, etc.) use inputTokens/outputTokens instead of promptTokens/completionTokens\n renameAttributeKey(attributes, 'ai.usage.inputTokens', GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE);\n renameAttributeKey(attributes, 'ai.usage.outputTokens', GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE);\n\n // Embedding spans use ai.usage.tokens instead of promptTokens/completionTokens\n renameAttributeKey(attributes, AI_USAGE_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE);\n\n // AI SDK uses avgOutputTokensPerSecond, map to our expected attribute name\n renameAttributeKey(attributes, 'ai.response.avgOutputTokensPerSecond', 'ai.response.avgCompletionTokensPerSecond');\n\n // Input tokens is the sum of prompt tokens and cached input tokens\n if (\n typeof attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] === 'number' &&\n typeof attributes[GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE] === 'number'\n ) {\n attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] =\n attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] + attributes[GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE];\n }\n\n // Compute total tokens from input + output (embeddings may only have input tokens)\n if (typeof attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] === 'number') {\n const outputTokens =\n typeof attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] === 'number'\n ? attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE]\n : 0;\n attributes[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE] = outputTokens + attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE];\n }\n\n // Convert the available tools array to a JSON string\n if (attributes[AI_PROMPT_TOOLS_ATTRIBUTE] && Array.isArray(attributes[AI_PROMPT_TOOLS_ATTRIBUTE])) {\n attributes[AI_PROMPT_TOOLS_ATTRIBUTE] = convertAvailableToolsToJsonString(\n attributes[AI_PROMPT_TOOLS_ATTRIBUTE] as unknown[],\n );\n }\n\n // Rename AI SDK attributes to standardized gen_ai attributes\n // Map operation.name to OpenTelemetry semantic convention values\n if (attributes[OPERATION_NAME_ATTRIBUTE]) {\n const operationName = mapVercelAiOperationName(attributes[OPERATION_NAME_ATTRIBUTE] as string);\n attributes[GEN_AI_OPERATION_NAME_ATTRIBUTE] = operationName;\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete attributes[OPERATION_NAME_ATTRIBUTE];\n }\n renameAttributeKey(attributes, AI_PROMPT_MESSAGES_ATTRIBUTE, GEN_AI_INPUT_MESSAGES_ATTRIBUTE);\n\n // Build gen_ai.output.messages from response text and/or tool calls\n // Note: buildOutputMessages also removes the source attributes when output is successfully generated\n buildOutputMessages(attributes);\n\n renameAttributeKey(attributes, AI_RESPONSE_OBJECT_ATTRIBUTE, 'gen_ai.response.object');\n renameAttributeKey(attributes, AI_PROMPT_TOOLS_ATTRIBUTE, 'gen_ai.request.available_tools');\n\n renameAttributeKey(attributes, AI_TOOL_CALL_ARGS_ATTRIBUTE, GEN_AI_TOOL_INPUT_ATTRIBUTE);\n renameAttributeKey(attributes, AI_TOOL_CALL_RESULT_ATTRIBUTE, GEN_AI_TOOL_OUTPUT_ATTRIBUTE);\n\n renameAttributeKey(attributes, AI_SCHEMA_ATTRIBUTE, 'gen_ai.request.schema');\n renameAttributeKey(attributes, AI_MODEL_ID_ATTRIBUTE, GEN_AI_REQUEST_MODEL_ATTRIBUTE);\n\n // Map embedding input: ai.values → gen_ai.embeddings.input\n // Vercel AI SDK JSON-stringifies each value individually, so we parse each element back.\n // Single embed gets unwrapped to a plain value; batch embedMany stays as a JSON array.\n if (Array.isArray(attributes[AI_VALUES_ATTRIBUTE])) {\n const parsed = (attributes[AI_VALUES_ATTRIBUTE] as string[]).map(v => {\n try {\n return JSON.parse(v);\n } catch {\n return v;\n }\n });\n attributes[GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE] = parsed.length === 1 ? parsed[0] : JSON.stringify(parsed);\n }\n\n addProviderMetadataToAttributes(attributes);\n\n // Change attributes namespaced with `ai.X` to `vercel.ai.X`\n for (const key of Object.keys(attributes)) {\n if (key.startsWith('ai.')) {\n renameAttributeKey(attributes, key, `vercel.${key}`);\n }\n }\n}\n\n/**\n * Renames an attribute key in the provided attributes object if the old key exists.\n * This function safely handles null and undefined values.\n */\nfunction renameAttributeKey(attributes: Record<string, unknown>, oldKey: string, newKey: string): void {\n if (attributes[oldKey] != null) {\n attributes[newKey] = attributes[oldKey];\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete attributes[oldKey];\n }\n}\n\nfunction processToolCallSpan(span: Span, attributes: SpanAttributes): void {\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.vercelai.otel');\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.execute_tool');\n span.setAttribute(GEN_AI_OPERATION_NAME_ATTRIBUTE, 'execute_tool');\n renameAttributeKey(attributes, AI_TOOL_CALL_NAME_ATTRIBUTE, GEN_AI_TOOL_NAME_ATTRIBUTE);\n renameAttributeKey(attributes, AI_TOOL_CALL_ID_ATTRIBUTE, GEN_AI_TOOL_CALL_ID_ATTRIBUTE);\n\n // Store the span context in our global map using the tool call ID.\n // This allows us to capture tool errors and link them to the correct span\n // without retaining the full Span object in memory.\n const toolCallId = attributes[GEN_AI_TOOL_CALL_ID_ATTRIBUTE];\n\n if (typeof toolCallId === 'string') {\n toolCallSpanContextMap.set(toolCallId, span.spanContext());\n }\n\n // https://opentelemetry.io/docs/specs/semconv/registry/attributes/gen-ai/#gen-ai-tool-type\n if (!attributes[GEN_AI_TOOL_TYPE_ATTRIBUTE]) {\n span.setAttribute(GEN_AI_TOOL_TYPE_ATTRIBUTE, 'function');\n }\n const toolName = attributes[GEN_AI_TOOL_NAME_ATTRIBUTE];\n if (toolName) {\n span.updateName(`execute_tool ${toolName}`);\n }\n}\n\nfunction processGenerateSpan(span: Span, name: string, attributes: SpanAttributes): void {\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.vercelai.otel');\n\n const nameWthoutAi = name.replace('ai.', '');\n span.setAttribute('ai.pipeline.name', nameWthoutAi);\n span.updateName(nameWthoutAi);\n\n const functionId = attributes[AI_TELEMETRY_FUNCTION_ID_ATTRIBUTE];\n if (functionId && typeof functionId === 'string') {\n span.setAttribute('gen_ai.function_id', functionId);\n }\n\n requestMessagesFromPrompt(span, attributes);\n\n if (attributes[AI_MODEL_ID_ATTRIBUTE] && !attributes[GEN_AI_RESPONSE_MODEL_ATTRIBUTE]) {\n span.setAttribute(GEN_AI_RESPONSE_MODEL_ATTRIBUTE, attributes[AI_MODEL_ID_ATTRIBUTE]);\n }\n span.setAttribute('ai.streaming', name.includes('stream'));\n\n // Set the op based on the span name\n const op = getSpanOpFromName(name);\n if (op) {\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, op);\n }\n\n // For invoke_agent pipeline spans, use 'invoke_agent' as the description\n // to be consistent with other AI integrations (e.g. LangGraph)\n if (INVOKE_AGENT_OPS.has(name)) {\n if (functionId && typeof functionId === 'string') {\n span.updateName(`invoke_agent ${functionId}`);\n } else {\n span.updateName('invoke_agent');\n }\n return;\n }\n\n const modelId = attributes[AI_MODEL_ID_ATTRIBUTE];\n if (modelId) {\n const doSpanPrefix = GENERATE_CONTENT_OPS.has(name) ? 'generate_content' : DO_SPAN_NAME_PREFIX[name];\n if (doSpanPrefix) {\n span.updateName(`${doSpanPrefix} ${modelId}`);\n }\n }\n}\n\n/**\n * Add event processors to the given client to process Vercel AI spans.\n */\nexport function addVercelAiProcessors(client: Client): void {\n client.on('spanStart', onVercelAiSpanStart);\n // Note: We cannot do this on `spanEnd`, because the span cannot be mutated anymore at this point\n client.addEventProcessor(Object.assign(vercelAiEventProcessor, { id: 'VercelAiEventProcessor' }));\n}\n\nfunction addProviderMetadataToAttributes(attributes: SpanAttributes): void {\n const providerMetadata = attributes[AI_RESPONSE_PROVIDER_METADATA_ATTRIBUTE] as string | undefined;\n if (providerMetadata) {\n try {\n const providerMetadataObject = JSON.parse(providerMetadata) as ProviderMetadata;\n\n // Handle OpenAI metadata (v5 uses 'openai', v6 Azure Responses API uses 'azure')\n const openaiMetadata: OpenAiProviderMetadata | undefined =\n providerMetadataObject.openai ?? providerMetadataObject.azure;\n if (openaiMetadata) {\n setAttributeIfDefined(\n attributes,\n GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE,\n openaiMetadata.cachedPromptTokens,\n );\n setAttributeIfDefined(attributes, 'gen_ai.usage.output_tokens.reasoning', openaiMetadata.reasoningTokens);\n setAttributeIfDefined(\n attributes,\n 'gen_ai.usage.output_tokens.prediction_accepted',\n openaiMetadata.acceptedPredictionTokens,\n );\n setAttributeIfDefined(\n attributes,\n 'gen_ai.usage.output_tokens.prediction_rejected',\n openaiMetadata.rejectedPredictionTokens,\n );\n setAttributeIfDefined(attributes, 'gen_ai.conversation.id', openaiMetadata.responseId);\n }\n\n if (providerMetadataObject.anthropic) {\n const cachedInputTokens =\n providerMetadataObject.anthropic.usage?.cache_read_input_tokens ??\n providerMetadataObject.anthropic.cacheReadInputTokens;\n setAttributeIfDefined(attributes, GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE, cachedInputTokens);\n\n const cacheWriteInputTokens =\n providerMetadataObject.anthropic.usage?.cache_creation_input_tokens ??\n providerMetadataObject.anthropic.cacheCreationInputTokens;\n setAttributeIfDefined(attributes, GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE, cacheWriteInputTokens);\n }\n\n if (providerMetadataObject.bedrock?.usage) {\n setAttributeIfDefined(\n attributes,\n GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE,\n providerMetadataObject.bedrock.usage.cacheReadInputTokens,\n );\n setAttributeIfDefined(\n attributes,\n GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE,\n providerMetadataObject.bedrock.usage.cacheWriteInputTokens,\n );\n }\n\n if (providerMetadataObject.deepseek) {\n setAttributeIfDefined(\n attributes,\n GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE,\n providerMetadataObject.deepseek.promptCacheHitTokens,\n );\n setAttributeIfDefined(\n attributes,\n 'gen_ai.usage.input_tokens.cache_miss',\n providerMetadataObject.deepseek.promptCacheMissTokens,\n );\n }\n } catch {\n // Ignore\n }\n }\n}\n\n/**\n * Sets an attribute only if the value is not null or undefined.\n */\nfunction setAttributeIfDefined(attributes: SpanAttributes, key: string, value: SpanAttributeValue | undefined): void {\n if (value != null) {\n attributes[key] = value;\n }\n}\n"],"names":["INVOKE_AGENT_OPS","GENERATE_CONTENT_OPS","EMBEDDINGS_OPS","RERANK_OPS","spanToJSON","AI_TOOL_CALL_NAME_ATTRIBUTE","AI_TOOL_CALL_ID_ATTRIBUTE","AI_OPERATION_ID_ATTRIBUTE","accumulateTokensForParent","applyToolDescriptionsAndTokens","applyAccumulatedTokens","AI_RESPONSE_TEXT_ATTRIBUTE","AI_RESPONSE_TOOL_CALLS_ATTRIBUTE","AI_RESPONSE_FINISH_REASON_ATTRIBUTE","GEN_AI_OUTPUT_MESSAGES_ATTRIBUTE","AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE","GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE","AI_USAGE_PROMPT_TOKENS_ATTRIBUTE","GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE","AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE","GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE","AI_USAGE_TOKENS_ATTRIBUTE","GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE","AI_PROMPT_TOOLS_ATTRIBUTE","convertAvailableToolsToJsonString","OPERATION_NAME_ATTRIBUTE","GEN_AI_OPERATION_NAME_ATTRIBUTE","AI_PROMPT_MESSAGES_ATTRIBUTE","GEN_AI_INPUT_MESSAGES_ATTRIBUTE","AI_RESPONSE_OBJECT_ATTRIBUTE","AI_TOOL_CALL_ARGS_ATTRIBUTE","GEN_AI_TOOL_INPUT_ATTRIBUTE","AI_TOOL_CALL_RESULT_ATTRIBUTE","GEN_AI_TOOL_OUTPUT_ATTRIBUTE","AI_SCHEMA_ATTRIBUTE","AI_MODEL_ID_ATTRIBUTE","GEN_AI_REQUEST_MODEL_ATTRIBUTE","AI_VALUES_ATTRIBUTE","GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE","SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN","SEMANTIC_ATTRIBUTE_SENTRY_OP","GEN_AI_TOOL_NAME_ATTRIBUTE","GEN_AI_TOOL_CALL_ID_ATTRIBUTE","toolCallSpanContextMap","GEN_AI_TOOL_TYPE_ATTRIBUTE","AI_TELEMETRY_FUNCTION_ID_ATTRIBUTE","requestMessagesFromPrompt","GEN_AI_RESPONSE_MODEL_ATTRIBUTE","getSpanOpFromName","DO_SPAN_NAME_PREFIX","AI_RESPONSE_PROVI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EAAA;AACA;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/tracing/vercel-ai/index.ts"],"sourcesContent":["/* eslint-disable max-lines */\nimport type { Client } from '../../client';\nimport { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../../semanticAttributes';\nimport type { Event } from '../../types-hoist/event';\nimport type { Span, SpanAttributes, SpanAttributeValue, SpanJSON } from '../../types-hoist/span';\nimport { spanToJSON } from '../../utils/spanUtils';\nimport {\n GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE,\n GEN_AI_INPUT_MESSAGES_ATTRIBUTE,\n GEN_AI_OPERATION_NAME_ATTRIBUTE,\n GEN_AI_OUTPUT_MESSAGES_ATTRIBUTE,\n GEN_AI_REQUEST_MODEL_ATTRIBUTE,\n GEN_AI_RESPONSE_MODEL_ATTRIBUTE,\n GEN_AI_TOOL_CALL_ID_ATTRIBUTE,\n GEN_AI_TOOL_INPUT_ATTRIBUTE,\n GEN_AI_TOOL_NAME_ATTRIBUTE,\n GEN_AI_TOOL_OUTPUT_ATTRIBUTE,\n GEN_AI_TOOL_TYPE_ATTRIBUTE,\n GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE,\n GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE,\n GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE,\n GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE,\n GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE,\n} from '../ai/gen-ai-attributes';\nimport {\n DO_SPAN_NAME_PREFIX,\n EMBEDDINGS_OPS,\n GENERATE_CONTENT_OPS,\n INVOKE_AGENT_OPS,\n RERANK_OPS,\n toolCallSpanContextMap,\n} from './constants';\nimport type { TokenSummary } from './types';\nimport {\n accumulateTokensForParent,\n applyAccumulatedTokens,\n applyToolDescriptionsAndTokens,\n convertAvailableToolsToJsonString,\n getSpanOpFromName,\n requestMessagesFromPrompt,\n} from './utils';\nimport type { OpenAiProviderMetadata, ProviderMetadata } from './vercel-ai-attributes';\nimport {\n AI_MODEL_ID_ATTRIBUTE,\n AI_OPERATION_ID_ATTRIBUTE,\n AI_PROMPT_MESSAGES_ATTRIBUTE,\n AI_PROMPT_TOOLS_ATTRIBUTE,\n AI_RESPONSE_FINISH_REASON_ATTRIBUTE,\n AI_RESPONSE_OBJECT_ATTRIBUTE,\n AI_RESPONSE_PROVIDER_METADATA_ATTRIBUTE,\n AI_RESPONSE_TEXT_ATTRIBUTE,\n AI_RESPONSE_TOOL_CALLS_ATTRIBUTE,\n AI_SCHEMA_ATTRIBUTE,\n AI_TELEMETRY_FUNCTION_ID_ATTRIBUTE,\n AI_TOOL_CALL_ARGS_ATTRIBUTE,\n AI_TOOL_CALL_ID_ATTRIBUTE,\n AI_TOOL_CALL_NAME_ATTRIBUTE,\n AI_TOOL_CALL_RESULT_ATTRIBUTE,\n AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE,\n AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE,\n AI_USAGE_PROMPT_TOKENS_ATTRIBUTE,\n AI_USAGE_TOKENS_ATTRIBUTE,\n AI_VALUES_ATTRIBUTE,\n OPERATION_NAME_ATTRIBUTE,\n} from './vercel-ai-attributes';\n\n/**\n * Maps Vercel AI SDK operation names to OpenTelemetry semantic convention values\n * @see https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/#llm-request-spans\n */\nfunction mapVercelAiOperationName(operationName: string): string {\n // Top-level pipeline operations map to invoke_agent\n if (INVOKE_AGENT_OPS.has(operationName)) {\n return 'invoke_agent';\n }\n // .do* operations are the actual LLM calls\n if (GENERATE_CONTENT_OPS.has(operationName)) {\n return 'generate_content';\n }\n if (EMBEDDINGS_OPS.has(operationName)) {\n return 'embeddings';\n }\n if (RERANK_OPS.has(operationName)) {\n return 'rerank';\n }\n if (operationName === 'ai.toolCall') {\n return 'execute_tool';\n }\n // Return the original value for unknown operations\n return operationName;\n}\n\n/**\n * Post-process spans emitted by the Vercel AI SDK.\n * This is supposed to be used in `client.on('spanStart', ...)\n */\nfunction onVercelAiSpanStart(span: Span): void {\n const { data: attributes, description: name } = spanToJSON(span);\n\n if (!name) {\n return;\n }\n\n // Tool call spans\n // https://ai-sdk.dev/docs/ai-sdk-core/telemetry#tool-call-spans\n if (attributes[AI_TOOL_CALL_NAME_ATTRIBUTE] && attributes[AI_TOOL_CALL_ID_ATTRIBUTE] && name === 'ai.toolCall') {\n processToolCallSpan(span, attributes);\n return;\n }\n\n // V6+ Check if this is a Vercel AI span by checking if the operation ID attribute is present.\n // V5+ Check if this is a Vercel AI span by name pattern.\n if (!attributes[AI_OPERATION_ID_ATTRIBUTE] && !name.startsWith('ai.')) {\n return;\n }\n\n processGenerateSpan(span, name, attributes);\n}\n\nfunction vercelAiEventProcessor(event: Event): Event {\n if (event.type === 'transaction' && event.spans) {\n // Map to accumulate token data by parent span ID\n const tokenAccumulator: Map<string, TokenSummary> = new Map();\n\n // First pass: process all spans and accumulate token data\n for (const span of event.spans) {\n processEndedVercelAiSpan(span);\n\n // Accumulate token data for parent spans\n accumulateTokensForParent(span, tokenAccumulator);\n }\n\n // Second pass: apply tool descriptions and accumulated tokens\n applyToolDescriptionsAndTokens(event.spans, tokenAccumulator);\n\n // Also apply to root when it is the invoke_agent pipeline\n const trace = event.contexts?.trace;\n if (trace?.op === 'gen_ai.invoke_agent') {\n applyAccumulatedTokens(trace, tokenAccumulator);\n }\n }\n\n return event;\n}\n\n/**\n * Tool call structure from Vercel AI SDK\n * Note: V5/V6 use 'input' for arguments, V4 and earlier use 'args'\n */\ninterface VercelToolCall {\n toolCallId: string;\n toolName: string;\n input?: Record<string, unknown> | string; // V5/V6\n args?: string; // V4 and earlier\n}\n\n/**\n * Normalize finish reason to match OpenTelemetry semantic conventions.\n * Valid values: \"stop\", \"length\", \"content_filter\", \"tool_call\", \"error\"\n *\n * Vercel AI SDK uses \"tool-calls\" (plural, with hyphen) which we map to \"tool_call\".\n */\nfunction normalizeFinishReason(finishReason: unknown): string {\n if (typeof finishReason !== 'string') {\n return 'stop';\n }\n\n // Map Vercel AI SDK finish reasons to OpenTelemetry semantic convention values\n switch (finishReason) {\n case 'tool-calls':\n return 'tool_call';\n case 'stop':\n case 'length':\n case 'content_filter':\n case 'error':\n return finishReason;\n default:\n // For unknown values, return as-is (schema allows arbitrary strings)\n return finishReason;\n }\n}\n\n/**\n * Build gen_ai.output.messages from ai.response.text and/or ai.response.toolCalls\n *\n * Format follows OpenTelemetry semantic conventions:\n * [{\"role\": \"assistant\", \"parts\": [...], \"finish_reason\": \"stop\"}]\n *\n * Parts can be:\n * - {\"type\": \"text\", \"content\": \"...\"}\n * - {\"type\": \"tool_call\", \"id\": \"...\", \"name\": \"...\", \"arguments\": \"...\"}\n */\nfunction buildOutputMessages(attributes: Record<string, unknown>): void {\n const responseText = attributes[AI_RESPONSE_TEXT_ATTRIBUTE];\n const responseToolCalls = attributes[AI_RESPONSE_TOOL_CALLS_ATTRIBUTE];\n const finishReason = attributes[AI_RESPONSE_FINISH_REASON_ATTRIBUTE];\n\n // Skip if neither text nor tool calls are present\n if (responseText == null && responseToolCalls == null) {\n return;\n }\n\n const parts: Array<Record<string, unknown>> = [];\n\n // Add text part if present\n if (typeof responseText === 'string' && responseText.length > 0) {\n parts.push({\n type: 'text',\n content: responseText,\n });\n }\n\n // Add tool call parts if present\n if (responseToolCalls != null) {\n try {\n // Tool calls can be a string (JSON) or already parsed array\n const toolCalls: VercelToolCall[] =\n typeof responseToolCalls === 'string' ? JSON.parse(responseToolCalls) : responseToolCalls;\n\n if (Array.isArray(toolCalls)) {\n for (const toolCall of toolCalls) {\n // V5/V6 use 'input', V4 and earlier use 'args'\n const args = toolCall.input ?? toolCall.args;\n parts.push({\n type: 'tool_call',\n id: toolCall.toolCallId,\n name: toolCall.toolName,\n // Handle undefined args: JSON.stringify(undefined) returns undefined, not a string,\n // which would cause the property to be omitted from the final JSON output\n arguments: typeof args === 'string' ? args : JSON.stringify(args ?? {}),\n });\n }\n // Only delete tool calls attribute if we successfully processed them\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete attributes[AI_RESPONSE_TOOL_CALLS_ATTRIBUTE];\n }\n } catch {\n // Ignore parsing errors - tool calls attribute is preserved\n }\n }\n\n // Only set output messages and delete text attribute if we have parts\n if (parts.length > 0) {\n const outputMessage = {\n role: 'assistant',\n parts,\n finish_reason: normalizeFinishReason(finishReason),\n };\n\n attributes[GEN_AI_OUTPUT_MESSAGES_ATTRIBUTE] = JSON.stringify([outputMessage]);\n\n // Remove the text attribute since it's now captured in gen_ai.output.messages\n // Note: tool calls attribute is deleted above only if successfully parsed\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete attributes[AI_RESPONSE_TEXT_ATTRIBUTE];\n }\n}\n\n/**\n * Post-process spans emitted by the Vercel AI SDK.\n */\nfunction processEndedVercelAiSpan(span: SpanJSON): void {\n const { data: attributes, origin } = span;\n\n if (origin !== 'auto.vercelai.otel') {\n return;\n }\n\n // The Vercel AI SDK sets span status to raw error message strings.\n // Any such value should be normalized to a SpanStatusType value. We pick internal_error as it is the most generic.\n if (span.status && span.status !== 'ok') {\n span.status = 'internal_error';\n }\n\n renameAttributeKey(attributes, AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE, GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE);\n renameAttributeKey(attributes, AI_USAGE_PROMPT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE);\n renameAttributeKey(attributes, AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE);\n\n // Parent spans (ai.streamText, ai.streamObject, etc.) use inputTokens/outputTokens instead of promptTokens/completionTokens\n renameAttributeKey(attributes, 'ai.usage.inputTokens', GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE);\n renameAttributeKey(attributes, 'ai.usage.outputTokens', GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE);\n\n // Embedding spans use ai.usage.tokens instead of promptTokens/completionTokens\n renameAttributeKey(attributes, AI_USAGE_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE);\n\n // AI SDK uses avgOutputTokensPerSecond, map to our expected attribute name\n renameAttributeKey(attributes, 'ai.response.avgOutputTokensPerSecond', 'ai.response.avgCompletionTokensPerSecond');\n\n // Input tokens is the sum of prompt tokens and cached input tokens\n if (\n typeof attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] === 'number' &&\n typeof attributes[GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE] === 'number'\n ) {\n attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] =\n attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] + attributes[GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE];\n }\n\n // Compute total tokens from input + output (embeddings may only have input tokens)\n if (typeof attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] === 'number') {\n const outputTokens =\n typeof attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] === 'number'\n ? attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE]\n : 0;\n attributes[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE] = outputTokens + attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE];\n }\n\n // Convert the available tools array to a JSON string\n if (attributes[AI_PROMPT_TOOLS_ATTRIBUTE] && Array.isArray(attributes[AI_PROMPT_TOOLS_ATTRIBUTE])) {\n attributes[AI_PROMPT_TOOLS_ATTRIBUTE] = convertAvailableToolsToJsonString(\n attributes[AI_PROMPT_TOOLS_ATTRIBUTE] as unknown[],\n );\n }\n\n // Rename AI SDK attributes to standardized gen_ai attributes\n // Map operation.name to OpenTelemetry semantic convention values\n if (attributes[OPERATION_NAME_ATTRIBUTE]) {\n const operationName = mapVercelAiOperationName(attributes[OPERATION_NAME_ATTRIBUTE] as string);\n attributes[GEN_AI_OPERATION_NAME_ATTRIBUTE] = operationName;\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete attributes[OPERATION_NAME_ATTRIBUTE];\n }\n renameAttributeKey(attributes, AI_PROMPT_MESSAGES_ATTRIBUTE, GEN_AI_INPUT_MESSAGES_ATTRIBUTE);\n\n // Build gen_ai.output.messages from response text and/or tool calls\n // Note: buildOutputMessages also removes the source attributes when output is successfully generated\n buildOutputMessages(attributes);\n\n renameAttributeKey(attributes, AI_RESPONSE_OBJECT_ATTRIBUTE, 'gen_ai.response.object');\n renameAttributeKey(attributes, AI_PROMPT_TOOLS_ATTRIBUTE, 'gen_ai.request.available_tools');\n\n renameAttributeKey(attributes, AI_TOOL_CALL_ARGS_ATTRIBUTE, GEN_AI_TOOL_INPUT_ATTRIBUTE);\n renameAttributeKey(attributes, AI_TOOL_CALL_RESULT_ATTRIBUTE, GEN_AI_TOOL_OUTPUT_ATTRIBUTE);\n\n renameAttributeKey(attributes, AI_SCHEMA_ATTRIBUTE, 'gen_ai.request.schema');\n renameAttributeKey(attributes, AI_MODEL_ID_ATTRIBUTE, GEN_AI_REQUEST_MODEL_ATTRIBUTE);\n\n // Map embedding input: ai.values → gen_ai.embeddings.input\n // Vercel AI SDK JSON-stringifies each value individually, so we parse each element back.\n // Single embed gets unwrapped to a plain value; batch embedMany stays as a JSON array.\n if (Array.isArray(attributes[AI_VALUES_ATTRIBUTE])) {\n const parsed = (attributes[AI_VALUES_ATTRIBUTE] as string[]).map(v => {\n try {\n return JSON.parse(v);\n } catch {\n return v;\n }\n });\n attributes[GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE] = parsed.length === 1 ? parsed[0] : JSON.stringify(parsed);\n }\n\n addProviderMetadataToAttributes(attributes);\n\n // Change attributes namespaced with `ai.X` to `vercel.ai.X`\n for (const key of Object.keys(attributes)) {\n if (key.startsWith('ai.')) {\n renameAttributeKey(attributes, key, `vercel.${key}`);\n }\n }\n}\n\n/**\n * Renames an attribute key in the provided attributes object if the old key exists.\n * This function safely handles null and undefined values.\n */\nfunction renameAttributeKey(attributes: Record<string, unknown>, oldKey: string, newKey: string): void {\n if (attributes[oldKey] != null) {\n attributes[newKey] = attributes[oldKey];\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete attributes[oldKey];\n }\n}\n\nfunction processToolCallSpan(span: Span, attributes: SpanAttributes): void {\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.vercelai.otel');\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.execute_tool');\n span.setAttribute(GEN_AI_OPERATION_NAME_ATTRIBUTE, 'execute_tool');\n renameAttributeKey(attributes, AI_TOOL_CALL_NAME_ATTRIBUTE, GEN_AI_TOOL_NAME_ATTRIBUTE);\n renameAttributeKey(attributes, AI_TOOL_CALL_ID_ATTRIBUTE, GEN_AI_TOOL_CALL_ID_ATTRIBUTE);\n\n // Store the span context in our global map using the tool call ID.\n // This allows us to capture tool errors and link them to the correct span\n // without retaining the full Span object in memory.\n const toolCallId = attributes[GEN_AI_TOOL_CALL_ID_ATTRIBUTE];\n\n if (typeof toolCallId === 'string') {\n toolCallSpanContextMap.set(toolCallId, span.spanContext());\n }\n\n // https://opentelemetry.io/docs/specs/semconv/registry/attributes/gen-ai/#gen-ai-tool-type\n if (!attributes[GEN_AI_TOOL_TYPE_ATTRIBUTE]) {\n span.setAttribute(GEN_AI_TOOL_TYPE_ATTRIBUTE, 'function');\n }\n const toolName = attributes[GEN_AI_TOOL_NAME_ATTRIBUTE];\n if (toolName) {\n span.updateName(`execute_tool ${toolName}`);\n }\n}\n\nfunction processGenerateSpan(span: Span, name: string, attributes: SpanAttributes): void {\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.vercelai.otel');\n\n const nameWthoutAi = name.replace('ai.', '');\n span.setAttribute('ai.pipeline.name', nameWthoutAi);\n span.updateName(nameWthoutAi);\n\n const functionId = attributes[AI_TELEMETRY_FUNCTION_ID_ATTRIBUTE];\n if (functionId && typeof functionId === 'string') {\n span.setAttribute('gen_ai.function_id', functionId);\n }\n\n requestMessagesFromPrompt(span, attributes);\n\n if (attributes[AI_MODEL_ID_ATTRIBUTE] && !attributes[GEN_AI_RESPONSE_MODEL_ATTRIBUTE]) {\n span.setAttribute(GEN_AI_RESPONSE_MODEL_ATTRIBUTE, attributes[AI_MODEL_ID_ATTRIBUTE]);\n }\n span.setAttribute('ai.streaming', name.includes('stream'));\n\n // Set the op based on the span name\n const op = getSpanOpFromName(name);\n if (op) {\n span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, op);\n }\n\n // For invoke_agent pipeline spans, use 'invoke_agent' as the description\n // to be consistent with other AI integrations (e.g. LangGraph)\n if (INVOKE_AGENT_OPS.has(name)) {\n if (functionId && typeof functionId === 'string') {\n span.updateName(`invoke_agent ${functionId}`);\n } else {\n span.updateName('invoke_agent');\n }\n return;\n }\n\n const modelId = attributes[AI_MODEL_ID_ATTRIBUTE];\n if (modelId) {\n const doSpanPrefix = GENERATE_CONTENT_OPS.has(name) ? 'generate_content' : DO_SPAN_NAME_PREFIX[name];\n if (doSpanPrefix) {\n span.updateName(`${doSpanPrefix} ${modelId}`);\n }\n }\n}\n\n/**\n * Add event processors to the given client to process Vercel AI spans.\n */\nexport function addVercelAiProcessors(client: Client): void {\n client.on('spanStart', onVercelAiSpanStart);\n // Note: We cannot do this on `spanEnd`, because the span cannot be mutated anymore at this point\n client.addEventProcessor(Object.assign(vercelAiEventProcessor, { id: 'VercelAiEventProcessor' }));\n}\n\nfunction addProviderMetadataToAttributes(attributes: SpanAttributes): void {\n const providerMetadata = attributes[AI_RESPONSE_PROVIDER_METADATA_ATTRIBUTE] as string | undefined;\n if (providerMetadata) {\n try {\n const providerMetadataObject = JSON.parse(providerMetadata) as ProviderMetadata;\n\n // Handle OpenAI metadata (v5 uses 'openai', v6 Azure Responses API uses 'azure')\n const openaiMetadata: OpenAiProviderMetadata | undefined =\n providerMetadataObject.openai ?? providerMetadataObject.azure;\n if (openaiMetadata) {\n setAttributeIfDefined(\n attributes,\n GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE,\n openaiMetadata.cachedPromptTokens,\n );\n setAttributeIfDefined(attributes, 'gen_ai.usage.output_tokens.reasoning', openaiMetadata.reasoningTokens);\n setAttributeIfDefined(\n attributes,\n 'gen_ai.usage.output_tokens.prediction_accepted',\n openaiMetadata.acceptedPredictionTokens,\n );\n setAttributeIfDefined(\n attributes,\n 'gen_ai.usage.output_tokens.prediction_rejected',\n openaiMetadata.rejectedPredictionTokens,\n );\n if (!attributes['gen_ai.conversation.id']) {\n setAttributeIfDefined(attributes, 'gen_ai.conversation.id', openaiMetadata.responseId);\n }\n }\n\n if (providerMetadataObject.anthropic) {\n const cachedInputTokens =\n providerMetadataObject.anthropic.usage?.cache_read_input_tokens ??\n providerMetadataObject.anthropic.cacheReadInputTokens;\n setAttributeIfDefined(attributes, GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE, cachedInputTokens);\n\n const cacheWriteInputTokens =\n providerMetadataObject.anthropic.usage?.cache_creation_input_tokens ??\n providerMetadataObject.anthropic.cacheCreationInputTokens;\n setAttributeIfDefined(attributes, GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE, cacheWriteInputTokens);\n }\n\n if (providerMetadataObject.bedrock?.usage) {\n setAttributeIfDefined(\n attributes,\n GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE,\n providerMetadataObject.bedrock.usage.cacheReadInputTokens,\n );\n setAttributeIfDefined(\n attributes,\n GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE,\n providerMetadataObject.bedrock.usage.cacheWriteInputTokens,\n );\n }\n\n if (providerMetadataObject.deepseek) {\n setAttributeIfDefined(\n attributes,\n GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE,\n providerMetadataObject.deepseek.promptCacheHitTokens,\n );\n setAttributeIfDefined(\n attributes,\n 'gen_ai.usage.input_tokens.cache_miss',\n providerMetadataObject.deepseek.promptCacheMissTokens,\n );\n }\n } catch {\n // Ignore\n }\n }\n}\n\n/**\n * Sets an attribute only if the value is not null or undefined.\n */\nfunction setAttributeIfDefined(attributes: SpanAttributes, key: string, value: SpanAttributeValue | undefined): void {\n if (value != null) {\n attributes[key] = value;\n }\n}\n"],"names":["INVOKE_AGENT_OPS","GENERATE_CONTENT_OPS","EMBEDDINGS_OPS","RERANK_OPS","spanToJSON","AI_TOOL_CALL_NAME_ATTRIBUTE","AI_TOOL_CALL_ID_ATTRIBUTE","AI_OPERATION_ID_ATTRIBUTE","accumulateTokensForParent","applyToolDescriptionsAndTokens","applyAccumulatedTokens","AI_RESPONSE_TEXT_ATTRIBUTE","AI_RESPONSE_TOOL_CALLS_ATTRIBUTE","AI_RESPONSE_FINISH_REASON_ATTRIBUTE","GEN_AI_OUTPUT_MESSAGES_ATTRIBUTE","AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE","GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE","AI_USAGE_PROMPT_TOKENS_ATTRIBUTE","GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE","AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE","GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE","AI_USAGE_TOKENS_ATTRIBUTE","GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE","AI_PROMPT_TOOLS_ATTRIBUTE","convertAvailableToolsToJsonString","OPERATION_NAME_ATTRIBUTE","GEN_AI_OPERATION_NAME_ATTRIBUTE","AI_PROMPT_MESSAGES_ATTRIBUTE","GEN_AI_INPUT_MESSAGES_ATTRIBUTE","AI_RESPONSE_OBJECT_ATTRIBUTE","AI_TOOL_CALL_ARGS_ATTRIBUTE","GEN_AI_TOOL_INPUT_ATTRIBUTE","AI_TOOL_CALL_RESULT_ATTRIBUTE","GEN_AI_TOOL_OUTPUT_ATTRIBUTE","AI_SCHEMA_ATTRIBUTE","AI_MODEL_ID_ATTRIBUTE","GEN_AI_REQUEST_MODEL_ATTRIBUTE","AI_VALUES_ATTRIBUTE","GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE","SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN","SEMANTIC_ATTRIBUTE_SENTRY_OP","GEN_AI_TOOL_NAME_ATTRIBUTE","GEN_AI_TOOL_CALL_ID_ATTRIBUTE","toolCallSpanContextMap","GEN_AI_TOOL_TYPE_ATTRIBUTE","AI_TELEMETRY_FUNCTION_ID_ATTRIBUTE","requestMessagesFromPrompt","GEN_AI_RESPONSE_MODEL_ATTRIBUTE","getSpanOpFromName","DO_SPAN_NAME_PREFIX","AI_RESPONSE_PROVIDER_METADATA_ATTRIBUTE","GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE_ATTRIBUTE"],"mappings":";;;;;;;;;AAkEA;AACA;AACA;AACA;AACA,SAAS,wBAAwB,CAAC,aAAa,EAAkB;AACjE;AACA,EAAE,IAAIA,0BAAgB,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE;AAC3C,IAAI,OAAO,cAAc;AACzB,EAAE;AACF;AACA,EAAE,IAAIC,8BAAoB,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE;AAC/C,IAAI,OAAO,kBAAkB;AAC7B,EAAE;AACF,EAAE,IAAIC,wBAAc,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE;AACzC,IAAI,OAAO,YAAY;AACvB,EAAE;AACF,EAAE,IAAIC,oBAAU,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE;AACrC,IAAI,OAAO,QAAQ;AACnB,EAAE;AACF,EAAE,IAAI,aAAA,KAAkB,aAAa,EAAE;AACvC,IAAI,OAAO,cAAc;AACzB,E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EA,MAAA,IAAA,sBAAA,CAAA,OAAA,EAAA,KAAA,EAAA;AACA,QAAA,qBAAA;AACA,UAAA,UAAA;AACA,UAAA/B,0DAAA;AACA,UAAA,sBAAA,CAAA,OAAA,CAAA,KAAA,CAAA,oBAAA;AACA,SAAA;AACA,QAAA,qBAAA;AACA,UAAA,UAAA;AACA,UAAA+B,+DAAA;AACA,UAAA,sBAAA,CAAA,OAAA,CAAA,KAAA,CAAA,qBAAA;AACA,SAAA;AACA,MAAA;;AAEA,MAAA,IAAA,sBAAA,CAAA,QAAA,EAAA;AACA,QAAA,qBAAA;AACA,UAAA,UAAA;AACA,UAAA/B,0DAAA;AACA,UAAA,sBAAA,CAAA,QAAA,CAAA,oBAAA;AACA,SAAA;AACA,QAAA,qBAAA;AACA,UAAA,UAAA;AACA,UAAA,sCAAA;AACA,UAAA,sBAAA,CAAA,QAAA,CAAA,qBAAA;AACA,SAAA;AACA,MAAA;AACA,IAAA,CAAA,CAAA,MAAA;AACA;AACA,IAAA;AACA,EAAA;AACA;;AAEA;AACA;AACA;AACA,SAAA,qBAAA,CAAA,UAAA,EAAA,GAAA,EAAA,KAAA,EAAA;AACA,EAAA,IAAA,KAAA,IAAA,IAAA,EAAA;AACA,IAAA,UAAA,CAAA,GAAA,CAAA,GAAA,KAAA;AACA,EAAA;AACA;;;;"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
const isActualPromise = (p) =>
|
|
4
|
+
p instanceof Promise && !(p )[kChainedCopy];
|
|
5
|
+
|
|
6
|
+
const kChainedCopy = Symbol('chained PromiseLike');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Copy the properties from a decorated promiselike object onto its chained
|
|
10
|
+
* actual promise.
|
|
11
|
+
*/
|
|
12
|
+
const chainAndCopyPromiseLike = (
|
|
13
|
+
original,
|
|
14
|
+
onSuccess,
|
|
15
|
+
onError,
|
|
16
|
+
) => {
|
|
17
|
+
const chained = original.then(
|
|
18
|
+
value => {
|
|
19
|
+
onSuccess(value);
|
|
20
|
+
return value;
|
|
21
|
+
},
|
|
22
|
+
err => {
|
|
23
|
+
onError(err);
|
|
24
|
+
throw err;
|
|
25
|
+
},
|
|
26
|
+
) ;
|
|
27
|
+
|
|
28
|
+
// if we're just dealing with "normal" Promise objects, return the chain
|
|
29
|
+
return isActualPromise(chained) && isActualPromise(original) ? chained : copyProps(original, chained);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
const copyProps = (original, chained) => {
|
|
34
|
+
let mutated = false;
|
|
35
|
+
//oxlint-disable-next-line guard-for-in
|
|
36
|
+
for (const key in original) {
|
|
37
|
+
if (key in chained) continue;
|
|
38
|
+
mutated = true;
|
|
39
|
+
const value = original[key];
|
|
40
|
+
if (typeof value === 'function') {
|
|
41
|
+
Object.defineProperty(chained, key, {
|
|
42
|
+
value: (...args) => value.apply(original, args),
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
});
|
|
47
|
+
} else {
|
|
48
|
+
(chained )[key] = value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (mutated) Object.assign(chained, { [kChainedCopy]: true });
|
|
53
|
+
return chained;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
exports.chainAndCopyPromiseLike = chainAndCopyPromiseLike;
|
|
57
|
+
//# sourceMappingURL=chain-and-copy-promiselike.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chain-and-copy-promiselike.js","sources":["../../../src/utils/chain-and-copy-promiselike.ts"],"sourcesContent":["const isActualPromise = (p: unknown) =>\n p instanceof Promise && !(p as unknown as ChainedPromiseLike<unknown>)[kChainedCopy];\n\ntype ChainedPromiseLike<T> = PromiseLike<T> & {\n [kChainedCopy]: true;\n};\nconst kChainedCopy = Symbol('chained PromiseLike');\n\n/**\n * Copy the properties from a decorated promiselike object onto its chained\n * actual promise.\n */\nexport const chainAndCopyPromiseLike = <V, T extends PromiseLike<V>>(\n original: T,\n onSuccess: (value: V) => void,\n onError: (e: unknown) => void,\n): T => {\n const chained = original.then(\n value => {\n onSuccess(value);\n return value;\n },\n err => {\n onError(err);\n throw err;\n },\n ) as T;\n\n // if we're just dealing with \"normal\" Promise objects, return the chain\n return isActualPromise(chained) && isActualPromise(original) ? chained : copyProps(original, chained);\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst copyProps = <T extends Record<string, any>>(original: T, chained: T): T => {\n let mutated = false;\n //oxlint-disable-next-line guard-for-in\n for (const key in original) {\n if (key in chained) continue;\n mutated = true;\n const value = original[key];\n if (typeof value === 'function') {\n Object.defineProperty(chained, key, {\n value: (...args: unknown[]) => value.apply(original, args),\n enumerable: true,\n configurable: true,\n writable: true,\n });\n } else {\n (chained as Record<string, unknown>)[key] = value;\n }\n }\n\n if (mutated) Object.assign(chained, { [kChainedCopy]: true });\n return chained;\n};\n"],"names":[],"mappings":";;AAAA,MAAM,eAAA,GAAkB,CAAC,CAAC;AAC1B,EAAE,CAAA,YAAa,OAAA,IAAW,CAAC,CAAC,CAAA,GAA6C,YAAY,CAAC;;AAKtF,MAAM,YAAA,GAAe,MAAM,CAAC,qBAAqB,CAAC;;AAElD;AACA;AACA;AACA;AACO,MAAM,0BAA0B;AACvC,EAAE,QAAQ;AACV,EAAE,SAAS;AACX,EAAE,OAAO;AACT,KAAQ;AACR,EAAE,MAAM,OAAA,GAAU,QAAQ,CAAC,IAAI;AAC/B,IAAI,SAAS;AACb,MAAM,SAAS,CAAC,KAAK,CAAC;AACtB,MAAM,OAAO,KAAK;AAClB,IAAI,CAAC;AACL,IAAI,OAAO;AACX,MAAM,OAAO,CAAC,GAAG,CAAC;AAClB,MAAM,MAAM,GAAG;AACf,IAAI,CAAC;AACL,GAAE;;AAEF;AACA,EAAE,OAAO,eAAe,CAAC,OAAO,CAAA,IAAK,eAAe,CAAC,QAAQ,CAAA,GAAI,UAAU,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAC;AACvG;;AAEA;AACA,MAAM,YAAY,CAAgC,QAAQ,EAAK,OAAO,KAAW;AACjF,EAAE,IAAI,OAAA,GAAU,KAAK;AACrB;AACA,EAAE,KAAK,MAAM,GAAA,IAAO,QAAQ,EAAE;AAC9B,IAAI,IAAI,GAAA,IAAO,OAAO,EAAE;AACxB,IAAI,OAAA,GAAU,IAAI;AAClB,IAAI,MAAM,KAAA,GAAQ,QAAQ,CAAC,GAAG,CAAC;AAC/B,IAAI,IAAI,OAAO,KAAA,KAAU,UAAU,EAAE;AACrC,MAAM,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,GAAG,EAAE;AAC1C,QAAQ,KAAK,EAAE,CAAC,GAAG,IAAI,KAAgB,KAAK,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC;AAClE,QAAQ,UAAU,EAAE,IAAI;AACxB,QAAQ,YAAY,EAAE,IAAI;AAC1B,QAAQ,QAAQ,EAAE,IAAI;AACtB,OAAO,CAAC;AACR,IAAI,OAAO;AACX,MAAM,CAAC,OAAA,GAAoC,GAAG,CAAA,GAAI,KAAK;AACvD,IAAI;AACJ,EAAE;;AAEF,EAAE,IAAI,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC,YAAY,GAAG,IAAA,EAAM,CAAC;AAC/D,EAAE,OAAO,OAAO;AAChB,CAAC;;;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
|
|
3
|
+
const chainAndCopyPromiselike = require('./chain-and-copy-promiselike.js');
|
|
3
4
|
const is = require('./is.js');
|
|
4
5
|
|
|
5
6
|
/* eslint-disable */
|
|
@@ -40,7 +41,12 @@ function handleCallbackErrors
|
|
|
40
41
|
* Maybe handle a promise rejection.
|
|
41
42
|
* This expects to be given a value that _may_ be a promise, or any other value.
|
|
42
43
|
* If it is a promise, and it rejects, it will call the `onError` callback.
|
|
43
|
-
*
|
|
44
|
+
*
|
|
45
|
+
* For thenable objects with extra methods (like jQuery's jqXHR),
|
|
46
|
+
* this function preserves those methods by wrapping the original thenable in a Proxy
|
|
47
|
+
* that intercepts .then() calls to apply error handling while forwarding all other
|
|
48
|
+
* properties to the original object.
|
|
49
|
+
* This allows code like `startSpan(() => $.ajax(...)).abort()` to work correctly.
|
|
44
50
|
*/
|
|
45
51
|
function maybeHandlePromiseRejection(
|
|
46
52
|
value,
|
|
@@ -49,21 +55,19 @@ function maybeHandlePromiseRejection(
|
|
|
49
55
|
onSuccess,
|
|
50
56
|
) {
|
|
51
57
|
if (is.isThenable(value)) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
58
|
+
return chainAndCopyPromiselike.chainAndCopyPromiseLike(
|
|
59
|
+
value ,
|
|
60
|
+
result => {
|
|
55
61
|
onFinally();
|
|
56
|
-
onSuccess(
|
|
57
|
-
return res;
|
|
62
|
+
onSuccess(result );
|
|
58
63
|
},
|
|
59
|
-
|
|
60
|
-
onError(
|
|
64
|
+
err => {
|
|
65
|
+
onError(err);
|
|
61
66
|
onFinally();
|
|
62
|
-
throw e;
|
|
63
67
|
},
|
|
64
|
-
);
|
|
68
|
+
) ;
|
|
65
69
|
}
|
|
66
|
-
|
|
70
|
+
// Non-thenable value - call callbacks immediately and return as-is
|
|
67
71
|
onFinally();
|
|
68
72
|
onSuccess(value);
|
|
69
73
|
return value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleCallbackErrors.js","sources":["../../../src/utils/handleCallbackErrors.ts"],"sourcesContent":["import { isThenable } from '../utils/is';\n\n/* eslint-disable */\n// Vendor \"Awaited\" in to be TS 3.8 compatible\ntype AwaitedPromise<T> = T extends null | undefined\n ? T // special case for `null | undefined` when not in `--strictNullChecks` mode\n : T extends object & { then(onfulfilled: infer F, ...args: infer _): any } // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped\n ? F extends (value: infer V, ...args: infer _) => any // if the argument to `then` is callable, extracts the first argument\n ? V // normally this would recursively unwrap, but this is not possible in TS3.8\n : never // the argument to `then` was not callable\n : T; // non-object or non-thenable\n/* eslint-enable */\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function handleCallbackErrors<Fn extends () => Promise<any>, PromiseValue = AwaitedPromise<ReturnType<Fn>>>(\n fn: Fn,\n onError: (error: unknown) => void,\n onFinally?: () => void,\n onSuccess?: (result: PromiseValue) => void,\n): ReturnType<Fn>;\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function handleCallbackErrors<Fn extends () => any>(\n fn: Fn,\n onError: (error: unknown) => void,\n onFinally?: () => void,\n onSuccess?: (result: ReturnType<Fn>) => void,\n): ReturnType<Fn>;\n/**\n * Wrap a callback function with error handling.\n * If an error is thrown, it will be passed to the `onError` callback and re-thrown.\n *\n * If the return value of the function is a promise, it will be handled with `maybeHandlePromiseRejection`.\n *\n * If an `onFinally` callback is provided, this will be called when the callback has finished\n * - so if it returns a promise, once the promise resolved/rejected,\n * else once the callback has finished executing.\n * The `onFinally` callback will _always_ be called, no matter if an error was thrown or not.\n */\nexport function handleCallbackErrors<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Fn extends () => any,\n ValueType = ReturnType<Fn>,\n>(\n fn: Fn,\n onError: (error: unknown) => void,\n onFinally: () => void = () => {},\n onSuccess: (result: ValueType | AwaitedPromise<ValueType>) => void = () => {},\n): ValueType {\n let maybePromiseResult: ReturnType<Fn>;\n try {\n maybePromiseResult = fn();\n } catch (e) {\n onError(e);\n onFinally();\n throw e;\n }\n\n return maybeHandlePromiseRejection(maybePromiseResult, onError, onFinally, onSuccess);\n}\n\n/**\n * Maybe handle a promise rejection.\n * This expects to be given a value that _may_ be a promise, or any other value.\n * If it is a promise, and it rejects, it will call the `onError` callback.\n *
|
|
1
|
+
{"version":3,"file":"handleCallbackErrors.js","sources":["../../../src/utils/handleCallbackErrors.ts"],"sourcesContent":["import { chainAndCopyPromiseLike } from '../utils/chain-and-copy-promiselike';\nimport { isThenable } from '../utils/is';\n\n/* eslint-disable */\n// Vendor \"Awaited\" in to be TS 3.8 compatible\ntype AwaitedPromise<T> = T extends null | undefined\n ? T // special case for `null | undefined` when not in `--strictNullChecks` mode\n : T extends object & { then(onfulfilled: infer F, ...args: infer _): any } // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped\n ? F extends (value: infer V, ...args: infer _) => any // if the argument to `then` is callable, extracts the first argument\n ? V // normally this would recursively unwrap, but this is not possible in TS3.8\n : never // the argument to `then` was not callable\n : T; // non-object or non-thenable\n/* eslint-enable */\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function handleCallbackErrors<Fn extends () => Promise<any>, PromiseValue = AwaitedPromise<ReturnType<Fn>>>(\n fn: Fn,\n onError: (error: unknown) => void,\n onFinally?: () => void,\n onSuccess?: (result: PromiseValue) => void,\n): ReturnType<Fn>;\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function handleCallbackErrors<Fn extends () => any>(\n fn: Fn,\n onError: (error: unknown) => void,\n onFinally?: () => void,\n onSuccess?: (result: ReturnType<Fn>) => void,\n): ReturnType<Fn>;\n/**\n * Wrap a callback function with error handling.\n * If an error is thrown, it will be passed to the `onError` callback and re-thrown.\n *\n * If the return value of the function is a promise, it will be handled with `maybeHandlePromiseRejection`.\n *\n * If an `onFinally` callback is provided, this will be called when the callback has finished\n * - so if it returns a promise, once the promise resolved/rejected,\n * else once the callback has finished executing.\n * The `onFinally` callback will _always_ be called, no matter if an error was thrown or not.\n */\nexport function handleCallbackErrors<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Fn extends () => any,\n ValueType = ReturnType<Fn>,\n>(\n fn: Fn,\n onError: (error: unknown) => void,\n onFinally: () => void = () => {},\n onSuccess: (result: ValueType | AwaitedPromise<ValueType>) => void = () => {},\n): ValueType {\n let maybePromiseResult: ReturnType<Fn>;\n try {\n maybePromiseResult = fn();\n } catch (e) {\n onError(e);\n onFinally();\n throw e;\n }\n\n return maybeHandlePromiseRejection(maybePromiseResult, onError, onFinally, onSuccess);\n}\n\n/**\n * Maybe handle a promise rejection.\n * This expects to be given a value that _may_ be a promise, or any other value.\n * If it is a promise, and it rejects, it will call the `onError` callback.\n *\n * For thenable objects with extra methods (like jQuery's jqXHR),\n * this function preserves those methods by wrapping the original thenable in a Proxy\n * that intercepts .then() calls to apply error handling while forwarding all other\n * properties to the original object.\n * This allows code like `startSpan(() => $.ajax(...)).abort()` to work correctly.\n */\nfunction maybeHandlePromiseRejection<MaybePromise>(\n value: MaybePromise,\n onError: (error: unknown) => void,\n onFinally: () => void,\n onSuccess: (result: MaybePromise | AwaitedPromise<MaybePromise>) => void,\n): MaybePromise {\n if (isThenable(value)) {\n return chainAndCopyPromiseLike(\n value as MaybePromise & PromiseLike<Awaited<typeof value>> & Record<string, unknown>,\n result => {\n onFinally();\n onSuccess(result as Awaited<MaybePromise>);\n },\n err => {\n onError(err);\n onFinally();\n },\n ) as MaybePromise;\n }\n // Non-thenable value - call callbacks immediately and return as-is\n onFinally();\n onSuccess(value);\n return value;\n}\n"],"names":["isThenable","chainAndCopyPromiseLike"],"mappings":";;;;;AAGA;AACA;;AAwBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS;;AAIhB;AACA,EAAE,EAAE;AACJ,EAAE,OAAO;AACT,EAAE,SAAS,GAAe,MAAM,CAAC,CAAC;AAClC,EAAE,SAAS,GAA4D,MAAM,CAAC,CAAC;AAC/E,EAAa;AACb,EAAE,IAAI,kBAAkB;AACxB,EAAE,IAAI;AACN,IAAI,kBAAA,GAAqB,EAAE,EAAE;AAC7B,EAAE,CAAA,CAAE,OAAO,CAAC,EAAE;AACd,IAAI,OAAO,CAAC,CAAC,CAAC;AACd,IAAI,SAAS,EAAE;AACf,IAAI,MAAM,CAAC;AACX,EAAE;;AAEF,EAAE,OAAO,2BAA2B,CAAC,kBAAkB,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,CAAC;AACvF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS,2BAA2B;AACpC,EAAE,KAAK;AACP,EAAE,OAAO;AACT,EAAE,SAAS;AACX,EAAE,SAAS;AACX,EAAgB;AAChB,EAAE,IAAIA,aAAU,CAAC,KAAK,CAAC,EAAE;AACzB,IAAI,OAAOC,+CAAuB;AAClC,MAAM,KAAA;AACN,MAAM,UAAU;AAChB,QAAQ,SAAS,EAAE;AACnB,QAAQ,SAAS,CAAC,MAAA,EAAgC;AAClD,MAAM,CAAC;AACP,MAAM,OAAO;AACb,QAAQ,OAAO,CAAC,GAAG,CAAC;AACpB,QAAQ,SAAS,EAAE;AACnB,MAAM,CAAC;AACP,KAAI;AACJ,EAAE;AACF;AACA,EAAE,SAAS,EAAE;AACb,EAAE,SAAS,CAAC,KAAK,CAAC;AAClB,EAAE,OAAO,KAAK;AACd;;;;"}
|
|
@@ -2,7 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2
2
|
|
|
3
3
|
// This is a magic string replaced by rollup
|
|
4
4
|
|
|
5
|
-
const SDK_VERSION = "10.
|
|
5
|
+
const SDK_VERSION = "10.46.0" ;
|
|
6
6
|
|
|
7
7
|
exports.SDK_VERSION = SDK_VERSION;
|
|
8
8
|
//# sourceMappingURL=version.js.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getDefaultCurrentScope, getDefaultIsolationScope } from '../defaultScopes.js';
|
|
2
2
|
import { Scope } from '../scope.js';
|
|
3
|
+
import { chainAndCopyPromiseLike } from '../utils/chain-and-copy-promiselike.js';
|
|
3
4
|
import { isThenable } from '../utils/is.js';
|
|
4
5
|
import { getMainCarrier, getSentryCarrier } from '../carrier.js';
|
|
5
6
|
|
|
@@ -43,17 +44,11 @@ class AsyncContextStack {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
if (isThenable(maybePromiseResult)) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
},
|
|
52
|
-
e => {
|
|
53
|
-
this._popScope();
|
|
54
|
-
throw e;
|
|
55
|
-
},
|
|
56
|
-
);
|
|
47
|
+
return chainAndCopyPromiseLike(
|
|
48
|
+
maybePromiseResult ,
|
|
49
|
+
() => this._popScope(),
|
|
50
|
+
() => this._popScope(),
|
|
51
|
+
) ;
|
|
57
52
|
}
|
|
58
53
|
|
|
59
54
|
this._popScope();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stackStrategy.js","sources":["../../../src/asyncContext/stackStrategy.ts"],"sourcesContent":["import type { Client } from '../client';\nimport { getDefaultCurrentScope, getDefaultIsolationScope } from '../defaultScopes';\nimport { Scope } from '../scope';\nimport { isThenable } from '../utils/is';\nimport { getMainCarrier, getSentryCarrier } from './../carrier';\nimport type { AsyncContextStrategy } from './types';\n\ninterface Layer {\n client?: Client;\n scope: Scope;\n}\n\n/**\n * This is an object that holds a stack of scopes.\n */\nexport class AsyncContextStack {\n private readonly _stack: [Layer, ...Layer[]];\n private _isolationScope: Scope;\n\n public constructor(scope?: Scope, isolationScope?: Scope) {\n let assignedScope;\n if (!scope) {\n assignedScope = new Scope();\n } else {\n assignedScope = scope;\n }\n\n let assignedIsolationScope;\n if (!isolationScope) {\n assignedIsolationScope = new Scope();\n } else {\n assignedIsolationScope = isolationScope;\n }\n\n // scope stack for domains or the process\n this._stack = [{ scope: assignedScope }];\n this._isolationScope = assignedIsolationScope;\n }\n\n /**\n * Fork a scope for the stack.\n */\n public withScope<T>(callback: (scope: Scope) => T): T {\n const scope = this._pushScope();\n\n let maybePromiseResult: T;\n try {\n maybePromiseResult = callback(scope);\n } catch (e) {\n this._popScope();\n throw e;\n }\n\n if (isThenable(maybePromiseResult)) {\n
|
|
1
|
+
{"version":3,"file":"stackStrategy.js","sources":["../../../src/asyncContext/stackStrategy.ts"],"sourcesContent":["import type { Client } from '../client';\nimport { getDefaultCurrentScope, getDefaultIsolationScope } from '../defaultScopes';\nimport { Scope } from '../scope';\nimport { chainAndCopyPromiseLike } from '../utils/chain-and-copy-promiselike';\nimport { isThenable } from '../utils/is';\nimport { getMainCarrier, getSentryCarrier } from './../carrier';\nimport type { AsyncContextStrategy } from './types';\n\ninterface Layer {\n client?: Client;\n scope: Scope;\n}\n\n/**\n * This is an object that holds a stack of scopes.\n */\nexport class AsyncContextStack {\n private readonly _stack: [Layer, ...Layer[]];\n private _isolationScope: Scope;\n\n public constructor(scope?: Scope, isolationScope?: Scope) {\n let assignedScope;\n if (!scope) {\n assignedScope = new Scope();\n } else {\n assignedScope = scope;\n }\n\n let assignedIsolationScope;\n if (!isolationScope) {\n assignedIsolationScope = new Scope();\n } else {\n assignedIsolationScope = isolationScope;\n }\n\n // scope stack for domains or the process\n this._stack = [{ scope: assignedScope }];\n this._isolationScope = assignedIsolationScope;\n }\n\n /**\n * Fork a scope for the stack.\n */\n public withScope<T>(callback: (scope: Scope) => T): T {\n const scope = this._pushScope();\n\n let maybePromiseResult: T;\n try {\n maybePromiseResult = callback(scope);\n } catch (e) {\n this._popScope();\n throw e;\n }\n\n if (isThenable(maybePromiseResult)) {\n return chainAndCopyPromiseLike(\n maybePromiseResult as PromiseLike<Awaited<typeof maybePromiseResult>> & Record<string, unknown>,\n () => this._popScope(),\n () => this._popScope(),\n ) as T;\n }\n\n this._popScope();\n return maybePromiseResult;\n }\n\n /**\n * Get the client of the stack.\n */\n public getClient<C extends Client>(): C | undefined {\n return this.getStackTop().client as C;\n }\n\n /**\n * Returns the scope of the top stack.\n */\n public getScope(): Scope {\n return this.getStackTop().scope;\n }\n\n /**\n * Get the isolation scope for the stack.\n */\n public getIsolationScope(): Scope {\n return this._isolationScope;\n }\n\n /**\n * Returns the topmost scope layer in the order domain > local > process.\n */\n public getStackTop(): Layer {\n return this._stack[this._stack.length - 1] as Layer;\n }\n\n /**\n * Push a scope to the stack.\n */\n private _pushScope(): Scope {\n // We want to clone the content of prev scope\n const scope = this.getScope().clone();\n this._stack.push({\n client: this.getClient(),\n scope,\n });\n return scope;\n }\n\n /**\n * Pop a scope from the stack.\n */\n private _popScope(): boolean {\n if (this._stack.length <= 1) return false;\n return !!this._stack.pop();\n }\n}\n\n/**\n * Get the global async context stack.\n * This will be removed during the v8 cycle and is only here to make migration easier.\n */\nfunction getAsyncContextStack(): AsyncContextStack {\n const registry = getMainCarrier();\n const sentry = getSentryCarrier(registry);\n\n return (sentry.stack = sentry.stack || new AsyncContextStack(getDefaultCurrentScope(), getDefaultIsolationScope()));\n}\n\nfunction withScope<T>(callback: (scope: Scope) => T): T {\n return getAsyncContextStack().withScope(callback);\n}\n\nfunction withSetScope<T>(scope: Scope, callback: (scope: Scope) => T): T {\n const stack = getAsyncContextStack();\n return stack.withScope(() => {\n stack.getStackTop().scope = scope;\n return callback(scope);\n });\n}\n\nfunction withIsolationScope<T>(callback: (isolationScope: Scope) => T): T {\n return getAsyncContextStack().withScope(() => {\n return callback(getAsyncContextStack().getIsolationScope());\n });\n}\n\n/**\n * Get the stack-based async context strategy.\n */\nexport function getStackAsyncContextStrategy(): AsyncContextStrategy {\n return {\n withIsolationScope,\n withScope,\n withSetScope,\n withSetIsolationScope: <T>(_isolationScope: Scope, callback: (isolationScope: Scope) => T) => {\n return withIsolationScope(callback);\n },\n getCurrentScope: () => getAsyncContextStack().getScope(),\n getIsolationScope: () => getAsyncContextStack().getIsolationScope(),\n };\n}\n"],"names":[],"mappings":";;;;;;AAaA;AACA;AACA;AACO,MAAM,iBAAA,CAAkB;;AAI/B,GAAS,WAAW,CAAC,KAAK,EAAU,cAAc,EAAU;AAC5D,IAAI,IAAI,aAAa;AACrB,IAAI,IAAI,CAAC,KAAK,EAAE;AAChB,MAAM,aAAA,GAAgB,IAAI,KAAK,EAAE;AACjC,IAAI,OAA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AAI,KAAK,CAAC,WAAW,EAAE,CAAC,KAAA,GAAQ,KAAK;AACrC,IAAI,OAAO,QAAQ,CAAC,KAAK,CAAC;AAC1B,EAAE,CAAC,CAAC;AACJ;;AAEA,SAAS,kBAAkB,CAAI,QAAQ,EAAmC;AAC1E,EAAE,OAAO,oBAAoB,EAAE,CAAC,SAAS,CAAC,MAAM;AAChD,IAAI,OAAO,QAAQ,CAAC,oBAAoB,EAAE,CAAC,iBAAiB,EAAE,CAAC;AAC/D,EAAE,CAAC,CAAC;AACJ;;AAEA;AACA;AACA;AACO,SAAS,4BAA4B,GAAyB;AACrE,EAAE,OAAO;AACT,IAAI,kBAAkB;AACtB,IAAI,SAAS;AACb,IAAI,YAAY;AAChB,IAAI,qBAAqB,EAAE,CAAI,eAAe,EAAS,QAAQ,KAAmC;AAClG,MAAM,OAAO,kBAAkB,CAAC,QAAQ,CAAC;AACzC,IAAI,CAAC;AACL,IAAI,eAAe,EAAE,MAAM,oBAAoB,EAAE,CAAC,QAAQ,EAAE;AAC5D,IAAI,iBAAiB,EAAE,MAAM,oBAAoB,EAAE,CAAC,iBAAiB,EAAE;AACvE,GAAG;AACH;;;;"}
|
package/build/esm/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"type":"module","version":"10.
|
|
1
|
+
{"type":"module","version":"10.46.0","sideEffects":false}
|
|
@@ -58,31 +58,33 @@ function truncateTextByBytes(text, maxBytes) {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
|
-
* Extract text content from a
|
|
62
|
-
*
|
|
61
|
+
* Extract text content from a message item.
|
|
62
|
+
* Handles plain strings and objects with a text property.
|
|
63
63
|
*
|
|
64
64
|
* @returns The text content
|
|
65
65
|
*/
|
|
66
|
-
function
|
|
67
|
-
if (typeof
|
|
68
|
-
return
|
|
66
|
+
function getItemText(item) {
|
|
67
|
+
if (typeof item === 'string') {
|
|
68
|
+
return item;
|
|
69
|
+
}
|
|
70
|
+
if ('text' in item && typeof item.text === 'string') {
|
|
71
|
+
return item.text;
|
|
69
72
|
}
|
|
70
|
-
if ('text' in part) return part.text;
|
|
71
73
|
return '';
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
/**
|
|
75
|
-
* Create a new
|
|
77
|
+
* Create a new item with updated text content while preserving the original structure.
|
|
76
78
|
*
|
|
77
|
-
* @param
|
|
79
|
+
* @param item - Original item (string or object)
|
|
78
80
|
* @param text - New text content
|
|
79
|
-
* @returns New
|
|
81
|
+
* @returns New item with updated text
|
|
80
82
|
*/
|
|
81
|
-
function
|
|
82
|
-
if (typeof
|
|
83
|
+
function withItemText(item, text) {
|
|
84
|
+
if (typeof item === 'string') {
|
|
83
85
|
return text;
|
|
84
86
|
}
|
|
85
|
-
return { ...
|
|
87
|
+
return { ...item, text };
|
|
86
88
|
}
|
|
87
89
|
|
|
88
90
|
/**
|
|
@@ -139,56 +141,77 @@ function truncateContentMessage(message, maxBytes) {
|
|
|
139
141
|
}
|
|
140
142
|
|
|
141
143
|
/**
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
+
* Extracts the array items and their key from an array-based message.
|
|
145
|
+
* Returns `null` key if neither `parts` nor `content` is a valid array.
|
|
146
|
+
*/
|
|
147
|
+
function getArrayItems(message)
|
|
148
|
+
|
|
149
|
+
{
|
|
150
|
+
if ('parts' in message && Array.isArray(message.parts)) {
|
|
151
|
+
return { key: 'parts', items: message.parts };
|
|
152
|
+
}
|
|
153
|
+
if ('content' in message && Array.isArray(message.content)) {
|
|
154
|
+
return { key: 'content', items: message.content };
|
|
155
|
+
}
|
|
156
|
+
return { key: null, items: [] };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Truncate a message with an array-based format.
|
|
161
|
+
* Handles both `parts: [...]` (Google GenAI) and `content: [...]` (OpenAI/Anthropic multimodal).
|
|
162
|
+
* Keeps as many complete items as possible, only truncating the first item if needed.
|
|
144
163
|
*
|
|
145
|
-
* @param message - Message with parts array
|
|
164
|
+
* @param message - Message with parts or content array
|
|
146
165
|
* @param maxBytes - Maximum byte limit
|
|
147
166
|
* @returns Array with truncated message, or empty array if it doesn't fit
|
|
148
167
|
*/
|
|
149
|
-
function
|
|
150
|
-
const {
|
|
168
|
+
function truncateArrayMessage(message, maxBytes) {
|
|
169
|
+
const { key, items } = getArrayItems(message);
|
|
170
|
+
|
|
171
|
+
if (key === null || items.length === 0) {
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
151
174
|
|
|
152
|
-
// Calculate overhead by creating empty text
|
|
153
|
-
const
|
|
154
|
-
const overhead = jsonBytes({ ...message,
|
|
175
|
+
// Calculate overhead by creating empty text items
|
|
176
|
+
const emptyItems = items.map(item => withItemText(item, ''));
|
|
177
|
+
const overhead = jsonBytes({ ...message, [key]: emptyItems });
|
|
155
178
|
let remainingBytes = maxBytes - overhead;
|
|
156
179
|
|
|
157
180
|
if (remainingBytes <= 0) {
|
|
158
181
|
return [];
|
|
159
182
|
}
|
|
160
183
|
|
|
161
|
-
// Include
|
|
162
|
-
const
|
|
184
|
+
// Include items until we run out of space
|
|
185
|
+
const includedItems = [];
|
|
163
186
|
|
|
164
|
-
for (const
|
|
165
|
-
const text =
|
|
187
|
+
for (const item of items) {
|
|
188
|
+
const text = getItemText(item);
|
|
166
189
|
const textSize = utf8Bytes(text);
|
|
167
190
|
|
|
168
191
|
if (textSize <= remainingBytes) {
|
|
169
|
-
//
|
|
170
|
-
|
|
192
|
+
// Item fits: include it as-is
|
|
193
|
+
includedItems.push(item);
|
|
171
194
|
remainingBytes -= textSize;
|
|
172
|
-
} else if (
|
|
173
|
-
// First
|
|
195
|
+
} else if (includedItems.length === 0) {
|
|
196
|
+
// First item doesn't fit: truncate it
|
|
174
197
|
const truncated = truncateTextByBytes(text, remainingBytes);
|
|
175
198
|
if (truncated) {
|
|
176
|
-
|
|
199
|
+
includedItems.push(withItemText(item, truncated));
|
|
177
200
|
}
|
|
178
201
|
break;
|
|
179
202
|
} else {
|
|
180
|
-
// Subsequent
|
|
203
|
+
// Subsequent item doesn't fit: stop here
|
|
181
204
|
break;
|
|
182
205
|
}
|
|
183
206
|
}
|
|
184
207
|
|
|
185
208
|
/* c8 ignore start
|
|
186
209
|
* for type safety only, algorithm guarantees SOME text included */
|
|
187
|
-
if (
|
|
210
|
+
if (includedItems.length <= 0) {
|
|
188
211
|
return [];
|
|
189
212
|
} else {
|
|
190
213
|
/* c8 ignore stop */
|
|
191
|
-
return [{ ...message,
|
|
214
|
+
return [{ ...message, [key]: includedItems }];
|
|
192
215
|
}
|
|
193
216
|
}
|
|
194
217
|
|
|
@@ -221,13 +244,8 @@ function truncateSingleMessage(message, maxBytes) {
|
|
|
221
244
|
return truncateContentMessage(message, maxBytes);
|
|
222
245
|
}
|
|
223
246
|
|
|
224
|
-
if (isContentArrayMessage(message)) {
|
|
225
|
-
|
|
226
|
-
return [message];
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if (isPartsMessage(message)) {
|
|
230
|
-
return truncatePartsMessage(message, maxBytes);
|
|
247
|
+
if (isContentArrayMessage(message) || isPartsMessage(message)) {
|
|
248
|
+
return truncateArrayMessage(message, maxBytes);
|
|
231
249
|
}
|
|
232
250
|
|
|
233
251
|
// Unknown message format: cannot truncate safely
|