diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-cache-tokens.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-cache-tokens.mjs index 6e8d12a81fac..9765f8c0820b 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-cache-tokens.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-cache-tokens.mjs @@ -4,7 +4,7 @@ import { MockLanguageModelV3 } from 'ai/test'; // Calls routed through the Vercel AI Gateway carry a `gateway` key in `providerMetadata` instead of a // provider name, so cache counts can only be derived from the SDK's normalized usage object. -async function run() { +export async function run() { await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { await generateText({ experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true }, @@ -27,5 +27,3 @@ async function run() { }); }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-concurrent-stream.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-concurrent-stream.mjs index e384661b3a4a..34a87195ec45 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-concurrent-stream.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-concurrent-stream.mjs @@ -32,7 +32,7 @@ async function consume(result) { } } -async function run() { +export async function run() { // A single model instance shared by two *concurrent* streamText calls. The shared model carries // only a single captured-parent slot, so naive parent tracking could attribute a model call to // whichever operation resolved the model last. Each `generate_content` (doStream) must land under @@ -57,5 +57,3 @@ async function run() { await Promise.all(streams.map(consume)); }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-concurrent.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-concurrent.mjs index c87409a04479..eb17f6cda557 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-concurrent.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-concurrent.mjs @@ -17,7 +17,7 @@ function makeModel(text) { }); } -async function run() { +export async function run() { // A single model instance shared by two *concurrent* generateText calls. This // is the case where naive parent tracking could attribute a model call to the // wrong invoke_agent span; each generate_content must land under its own @@ -39,5 +39,3 @@ async function run() { ]); }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-openai-conversation.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-openai-conversation.mjs index fd724db00276..239c61422825 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-openai-conversation.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-openai-conversation.mjs @@ -29,7 +29,7 @@ const toolCallModel = new MockLanguageModelV3({ }), }); -async function run() { +export async function run() { await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { // A turn of an OpenAI Conversations API conversation. await generateText({ @@ -71,5 +71,3 @@ async function run() { }); }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-provider-metadata.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-provider-metadata.mjs index 790b5a0ee8d7..dc4a50f935a3 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-provider-metadata.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-provider-metadata.mjs @@ -2,7 +2,7 @@ import * as Sentry from '@sentry/node'; import { generateText } from 'ai'; import { MockLanguageModelV3 } from 'ai/test'; -async function run() { +export async function run() { await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { await generateText({ experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true }, @@ -31,5 +31,3 @@ async function run() { }); }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-structured-output.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-structured-output.mjs index d5057d323502..9e3c26d6381e 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-structured-output.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-structured-output.mjs @@ -7,7 +7,7 @@ import { z } from 'zod'; // output now flows through the `streamText` primitive with an `experimental_output`, which does. So a // streamed structured-output call must still produce the usual streamText/doStream spans, with the // streamed JSON captured as the output. -async function run() { +export async function run() { await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { const result = streamText({ experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true }, @@ -44,5 +44,3 @@ async function run() { } }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-text.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-text.mjs index 0d39e66bd6b1..547eb9c90d03 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-text.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-text.mjs @@ -2,7 +2,7 @@ import * as Sentry from '@sentry/node'; import { streamText } from 'ai'; import { MockLanguageModelV3, simulateReadableStream } from 'ai/test'; -async function run() { +export async function run() { await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { const result = streamText({ experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true }, @@ -38,5 +38,3 @@ async function run() { } }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-tools.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-tools.mjs index 54efdac423da..ac7cda45c72d 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-tools.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-stream-tools.mjs @@ -3,7 +3,7 @@ import { stepCountIs, streamText, tool } from 'ai'; import { MockLanguageModelV3, simulateReadableStream } from 'ai/test'; import { z } from 'zod'; -async function run() { +export async function run() { await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { let callCount = 0; @@ -74,5 +74,3 @@ async function run() { } }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-system-message.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-system-message.mjs index beb57a683162..b1009c404adb 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-system-message.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-system-message.mjs @@ -19,7 +19,7 @@ const model = () => const brief = { role: 'system', content: 'Be brief.' }; const kind = { role: 'system', content: 'Be kind.' }; -async function run() { +export async function run() { await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { // v6 reads `system`, v7 reads `instructions`. await generateText({ @@ -38,5 +38,3 @@ async function run() { }); }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-tool-loop-agent.mjs b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-tool-loop-agent.mjs index 6967ec2efe94..d87970e3db48 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-tool-loop-agent.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/scenario-tool-loop-agent.mjs @@ -3,7 +3,7 @@ import { ToolLoopAgent, stepCountIs, tool } from 'ai'; import { MockLanguageModelV3 } from 'ai/test'; import { z } from 'zod'; -async function run() { +export async function run() { await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { let callCount = 0; @@ -57,5 +57,3 @@ async function run() { }); }); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/suite.ts b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/suite.ts index 8f2eac8f34b4..429cd4cd423b 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/suite.ts +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/suite.ts @@ -37,6 +37,18 @@ export function registerVercelAiGenerationTests(version: '6' | '7', vercelAiVers cleanupChildProcesses(); }); const expectedOrigin = 'auto.ai.vercelai'; + const defaultScenarios = [ + 'scenario-stream-text.mjs', + 'scenario-stream-tools.mjs', + 'scenario-stream-structured-output.mjs', + 'scenario-provider-metadata.mjs', + 'scenario-system-message.mjs', + 'scenario-openai-conversation.mjs', + 'scenario-cache-tokens.mjs', + 'scenario-tool-loop-agent.mjs', + 'scenario-concurrent.mjs', + 'scenario-concurrent-stream.mjs', + ]; createEsmAndCjsTests( __dirname, 'scenario.mjs', @@ -289,15 +301,20 @@ export function registerVercelAiGenerationTests(version: '6' | '7', vercelAiVers }, }, ); + createEsmTests( __dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { - test('creates ai spans when dataCollection.genAi has inputs and outputs disabled', async () => { + let sharedScenario: Promise | undefined; + + async function runScenario(): Promise { + let captured: SerializedStreamedSpanContainer | undefined; await createRunner() .expect({ span: container => { + captured = container; const segment = container.items.find(span => span.is_segment && span.name === 'main'); expect(segment).toBeDefined(); const spans = container.items.filter( @@ -496,6 +513,31 @@ export function registerVercelAiGenerationTests(version: '6' | '7', vercelAiVers }) .start() .completed(); + return captured!; + } + + const getScenario = () => (sharedScenario ??= runScenario()); + + test('creates ai spans when dataCollection.genAi has inputs and outputs disabled', async () => { + await getScenario(); + }); + + test('creates ai related spans', async () => { + const container = await getScenario(); + const segment = container.items.find(span => span.is_segment && span.name === 'main'); + expect(segment).toBeDefined(); + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai'); + expect(spans).toHaveLength(7); + const invokeAgentSpans = spans.filter(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent'); + expect(invokeAgentSpans).toHaveLength(3); + + const generateContentSpans = spans.filter( + span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', + ); + expect(generateContentSpans).toHaveLength(3); + + const toolSpan = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.execute_tool'); + expect(toolSpan).toBeDefined(); }); }, { @@ -504,6 +546,7 @@ export function registerVercelAiGenerationTests(version: '6' | '7', vercelAiVers }, }, ); + createEsmTests( __dirname, 'scenario-error-in-tool.mjs', @@ -589,138 +632,189 @@ export function registerVercelAiGenerationTests(version: '6' | '7', vercelAiVers }, }, ); + createEsmTests( __dirname, - 'scenario.mjs', + defaultScenarios, 'instrument.mjs', (createRunner, test) => { - test('creates ai related spans', async () => { - await createRunner() - .expect({ - span: container => { - const segment = container.items.find(span => span.is_segment && span.name === 'main'); - expect(segment).toBeDefined(); - const spans = container.items.filter( - span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai', - ); - expect(spans).toHaveLength(7); - const invokeAgentSpans = spans.filter( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent', - ); - expect(invokeAgentSpans).toHaveLength(3); + let scenarios: ReturnType | undefined; + async function runScenarios() { + const runner = createRunner(); - const generateContentSpans = spans.filter( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', - ); - expect(generateContentSpans).toHaveLength(3); + const collectors = defaultScenarios.map(name => + runner.collectStreamedSpansUntilSegment( + span => span.name === 'main' && span.attributes['test.scenario']?.value === name, + ), + ); + await runner.start().completed(); + return { spans: await Promise.all(collectors) }; + } + const getScenarios = () => (scenarios ??= runScenarios()); - const toolSpan = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.execute_tool'); - expect(toolSpan).toBeDefined(); - }, - }) - .start() - .completed(); + test('derives provider-metadata token breakdown and system instructions', async () => { + const container = { items: (await getScenarios()).spans[3]! }; + + const segment = container.items.find(span => span.is_segment && span.name === 'main'); + expect(segment).toBeDefined(); + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai'); + const generateContent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content')!; + + // Cache/reasoning token breakdown is derived from the model's `providerMetadata` by the + // channel subscriber, which v6 reaches through the orchestrion adapter, so the shape is the + // same on both versions. + expect(generateContent.attributes[GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS]?.value).toBe(5); + expect(generateContent.attributes[GEN_AI_USAGE_REASONING_OUTPUT_TOKENS]?.value).toBe(7); + // The per-response `responseId` is not a conversation id and must not be recorded as one. + expect(generateContent.attributes[GEN_AI_CONVERSATION_ID]).toBeUndefined(); + expect(generateContent.attributes[GEN_AI_COST_TOTAL_TOKENS]?.value).toBe(0.0000074); + + const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; + expect(invokeAgent).toBeDefined(); + // `providerMetadata` holds only the last step's cost, which the model-call span already carries. + expect(invokeAgent.attributes[GEN_AI_COST_TOTAL_TOKENS]).toBeUndefined(); + + // The system prompt is supplied via the v7-only `instructions` option. Only the channel + // instrumentation surfaces it (as `gen_ai.system_instructions`); v6 has no such option. + if (version === '7') { + const expected = '[{"type":"text","content":"You are a helpful assistant."}]'; + expect(invokeAgent.attributes[GEN_AI_SYSTEM_INSTRUCTIONS]?.value).toBe(expected); + expect(generateContent.attributes[GEN_AI_SYSTEM_INSTRUCTIONS]?.value).toBe(expected); + } }); - }, - { - additionalDependencies: { - ai: vercelAiVersion, - }, - }, - ); - createEsmTests( - __dirname, - 'scenario-tool-loop-agent.mjs', - 'instrument.mjs', - (createRunner, test) => { + + test('records system instructions from system message objects', async () => { + const container = { items: (await getScenarios()).spans[4]! }; + + const systemInstructions = container.items + .filter(span => span.attributes[SENTRY_OP]?.value === GEN_AI_INVOKE_AGENT) + .map(span => span.attributes[GEN_AI_SYSTEM_INSTRUCTIONS]?.value); + + expect(systemInstructions).toEqual([ + '[{"type":"text","content":"Be brief."}]', + '[{"type":"text","content":"Be brief."},{"type":"text","content":"Be kind."}]', + ]); + }); + + test('derives gen_ai.conversation.id from the OpenAI `conversation` provider option', async () => { + const container = { items: (await getScenarios()).spans[5]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); + const genAiSpans = container.items.filter(s => + String(s.attributes[SENTRY_OP]?.value ?? '').startsWith('gen_ai.'), + ); + const conversationIdOf = (span: (typeof genAiSpans)[number]) => span.attributes[GEN_AI_CONVERSATION_ID]?.value; + const invokeAgentSpans = genAiSpans.filter(s => s.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent'); + expect(invokeAgentSpans).toHaveLength(4); + const [firstTurn, secondTurn, chainedTurn, apiTurn] = invokeAgentSpans.sort( + (a, b) => a.start_timestamp - b.start_timestamp, + ); + + // `providerOptions.openai.conversation` is the Conversations API id: the same on every turn. + expect(conversationIdOf(firstTurn!)).toBe('conv_abc123'); + // The Azure Responses API uses the `azure` key for the same option. + expect(conversationIdOf(secondTurn!)).toBe('conv_azure'); + // `previousResponseId` names a response rather than a thread, and the response's own + // `responseId` is recorded as `gen_ai.response.id` only. + expect(conversationIdOf(chainedTurn!)).toBeUndefined(); + // `Sentry.setConversationId()` beats the provider option. + expect(conversationIdOf(apiTurn!)).toBe('conv-from-api'); + + // Model-call and tool spans carry their operation's id, even though their start events + // do not carry `providerOptions`. + const modelCallSpans = genAiSpans.filter(s => s.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content'); + expect(modelCallSpans.map(conversationIdOf).sort()).toEqual([ + 'conv-from-api', + 'conv_abc123', + 'conv_azure', + undefined, + ]); + const toolSpan = genAiSpans.find(s => s.attributes[SENTRY_OP]?.value === 'gen_ai.execute_tool')!; + expect(toolSpan).toBeDefined(); + expect(conversationIdOf(toolSpan)).toBe('conv_azure'); + }); + + test('reads cache token counts from the SDK usage object', async () => { + const container = { items: (await getScenarios()).spans[6]! }; + + const segment = container.items.find(span => span.is_segment && span.name === 'main'); + expect(segment).toBeDefined(); + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai'); + const generateContent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content')!; + expect(generateContent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(120); + expect(generateContent.attributes[GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS]?.value).toBe(80); + expect(generateContent.attributes[GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS]?.value).toBe(20); + + const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; + expect(invokeAgent.attributes[GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS]?.value).toBe(80); + expect(invokeAgent.attributes[GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS]?.value).toBe(20); + }); + test('creates spans for ToolLoopAgent with tool calls', async () => { - await createRunner() - .expect({ - span: container => { - const segment = container.items.find(span => span.is_segment && span.name === 'main'); - expect(segment).toBeDefined(); - const spans = container.items.filter( - span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai', - ); - expect(spans).toHaveLength(4); - const invokeAgentSpan = spans.find(span => span.name === 'invoke_agent weather_agent')!; - expect(invokeAgentSpan.status).toBe('ok'); - expect(invokeAgentSpan.attributes[SENTRY_OP]?.value).toBe('gen_ai.invoke_agent'); - expect(invokeAgentSpan.attributes[GEN_AI_REQUEST_MODEL]?.value).toBe('mock-model-id'); - - const toolCallsGenerateContentSpan = spans.find( - span => span.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value === '["tool-calls"]', - )!; - expect(toolCallsGenerateContentSpan.name).toBe('generate_content mock-model-id'); - expect(toolCallsGenerateContentSpan.status).toBe('ok'); - expect(toolCallsGenerateContentSpan.attributes[SENTRY_OP]?.value).toBe('gen_ai.generate_content'); - expect(toolCallsGenerateContentSpan.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10); - expect(toolCallsGenerateContentSpan.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20); - - const toolSpan = spans.find(span => span.name === 'execute_tool getWeather')!; - expect(toolSpan.status).toBe('ok'); - expect(toolSpan.attributes[SENTRY_OP]?.value).toBe('gen_ai.execute_tool'); - expect(toolSpan.attributes[GEN_AI_TOOL_NAME]?.value).toBe('getWeather'); - expect(toolSpan.attributes[GEN_AI_TOOL_CALL_ID_ATTRIBUTE]?.value).toBe('call-1'); - - const finalGenerateContentSpan = spans.find( - span => span.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value === '["stop"]', - )!; - expect(finalGenerateContentSpan.name).toBe('generate_content mock-model-id'); - expect(finalGenerateContentSpan.status).toBe('ok'); - expect(finalGenerateContentSpan.attributes[SENTRY_OP]?.value).toBe('gen_ai.generate_content'); - expect(finalGenerateContentSpan.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(15); - expect(finalGenerateContentSpan.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(25); - }, - }) - .start() - .completed(); + const container = { items: (await getScenarios()).spans[7]! }; + + const segment = container.items.find(span => span.is_segment && span.name === 'main'); + expect(segment).toBeDefined(); + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai'); + expect(spans).toHaveLength(4); + const invokeAgentSpan = spans.find(span => span.name === 'invoke_agent weather_agent')!; + expect(invokeAgentSpan.status).toBe('ok'); + expect(invokeAgentSpan.attributes[SENTRY_OP]?.value).toBe('gen_ai.invoke_agent'); + expect(invokeAgentSpan.attributes[GEN_AI_REQUEST_MODEL]?.value).toBe('mock-model-id'); + + const toolCallsGenerateContentSpan = spans.find( + span => span.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value === '["tool-calls"]', + )!; + expect(toolCallsGenerateContentSpan.name).toBe('generate_content mock-model-id'); + expect(toolCallsGenerateContentSpan.status).toBe('ok'); + expect(toolCallsGenerateContentSpan.attributes[SENTRY_OP]?.value).toBe('gen_ai.generate_content'); + expect(toolCallsGenerateContentSpan.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10); + expect(toolCallsGenerateContentSpan.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20); + + const toolSpan = spans.find(span => span.name === 'execute_tool getWeather')!; + expect(toolSpan.status).toBe('ok'); + expect(toolSpan.attributes[SENTRY_OP]?.value).toBe('gen_ai.execute_tool'); + expect(toolSpan.attributes[GEN_AI_TOOL_NAME]?.value).toBe('getWeather'); + expect(toolSpan.attributes[GEN_AI_TOOL_CALL_ID_ATTRIBUTE]?.value).toBe('call-1'); + + const finalGenerateContentSpan = spans.find( + span => span.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value === '["stop"]', + )!; + expect(finalGenerateContentSpan.name).toBe('generate_content mock-model-id'); + expect(finalGenerateContentSpan.status).toBe('ok'); + expect(finalGenerateContentSpan.attributes[SENTRY_OP]?.value).toBe('gen_ai.generate_content'); + expect(finalGenerateContentSpan.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(15); + expect(finalGenerateContentSpan.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(25); }); - }, - { - additionalDependencies: { - ai: vercelAiVersion, - }, - }, - ); - createEsmTests( - __dirname, - 'scenario-concurrent.mjs', - 'instrument.mjs', - (createRunner, test) => { + test('parents concurrent calls that share one model instance correctly', async () => { - await createRunner() - .expect({ - span: container => { - const segment = container.items.find(span => span.is_segment && span.name === 'main'); - expect(segment).toBeDefined(); - const spans = container.items.filter( - span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai', - ); - const invokeAgents = spans.filter(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent'); - const generateContents = spans.filter( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', - ); + const container = { items: (await getScenarios()).spans[8]! }; - // Two concurrent operations -> two invoke_agent + two generate_content spans. - expect(invokeAgents).toHaveLength(2); - expect(generateContents).toHaveLength(2); + const segment = container.items.find(span => span.is_segment && span.name === 'main'); + expect(segment).toBeDefined(); + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai'); + const invokeAgents = spans.filter(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent'); + const generateContents = spans.filter(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content'); - const agentSpanIds = new Set(invokeAgents.map(span => span.span_id)); + // Two concurrent operations -> two invoke_agent + two generate_content spans. + expect(invokeAgents).toHaveLength(2); + expect(generateContents).toHaveLength(2); - // Each model call lands under an invoke_agent span... - for (const span of generateContents) { - expect(agentSpanIds.has(span.parent_span_id!)).toBe(true); - } - // ...a distinct one each (no cross-attribution despite the shared model instance)... - expect(new Set(generateContents.map(span => span.parent_span_id)).size).toBe(2); - // ...and both operations sit under the same `main` parent. - expect(new Set(invokeAgents.map(span => span.parent_span_id)).size).toBe(1); - }, - }) - .start() - .completed(); + const agentSpanIds = new Set(invokeAgents.map(span => span.span_id)); + + // Each model call lands under an invoke_agent span... + for (const span of generateContents) { + expect(agentSpanIds.has(span.parent_span_id!)).toBe(true); + } + // ...a distinct one each (no cross-attribution despite the shared model instance)... + expect(new Set(generateContents.map(span => span.parent_span_id)).size).toBe(2); + // ...and both operations sit under the same `main` parent. + expect(new Set(invokeAgents.map(span => span.parent_span_id)).size).toBe(1); }); + + // A single model instance shared by two concurrent `streamText` calls carries only one + // captured-parent slot, so both model calls must still land under their own `invoke_agent` — not + // collapse onto whichever operation resolved the shared model last. }, { additionalDependencies: { @@ -728,6 +822,7 @@ export function registerVercelAiGenerationTests(version: '6' | '7', vercelAiVers }, }, ); + createEsmTests( __dirname, 'scenario-rejected-model.mjs', @@ -763,179 +858,7 @@ export function registerVercelAiGenerationTests(version: '6' | '7', vercelAiVers }, }, ); - createEsmTests( - __dirname, - 'scenario-provider-metadata.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('derives provider-metadata token breakdown and system instructions', async () => { - await createRunner() - .expect({ - span: container => { - const segment = container.items.find(span => span.is_segment && span.name === 'main'); - expect(segment).toBeDefined(); - const spans = container.items.filter( - span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai', - ); - const generateContent = spans.find( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', - )!; - - // Cache/reasoning token breakdown is derived from the model's `providerMetadata` by the - // channel subscriber, which v6 reaches through the orchestrion adapter, so the shape is the - // same on both versions. - expect(generateContent.attributes[GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS]?.value).toBe(5); - expect(generateContent.attributes[GEN_AI_USAGE_REASONING_OUTPUT_TOKENS]?.value).toBe(7); - // The per-response `responseId` is not a conversation id and must not be recorded as one. - expect(generateContent.attributes[GEN_AI_CONVERSATION_ID]).toBeUndefined(); - expect(generateContent.attributes[GEN_AI_COST_TOTAL_TOKENS]?.value).toBe(0.0000074); - - const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; - expect(invokeAgent).toBeDefined(); - // `providerMetadata` holds only the last step's cost, which the model-call span already carries. - expect(invokeAgent.attributes[GEN_AI_COST_TOTAL_TOKENS]).toBeUndefined(); - - // The system prompt is supplied via the v7-only `instructions` option. Only the channel - // instrumentation surfaces it (as `gen_ai.system_instructions`); v6 has no such option. - if (version === '7') { - const expected = '[{"type":"text","content":"You are a helpful assistant."}]'; - expect(invokeAgent.attributes[GEN_AI_SYSTEM_INSTRUCTIONS]?.value).toBe(expected); - expect(generateContent.attributes[GEN_AI_SYSTEM_INSTRUCTIONS]?.value).toBe(expected); - } - }, - }) - .start() - .completed(); - }); - }, - { - additionalDependencies: { - ai: vercelAiVersion, - }, - }, - ); - createEsmTests( - __dirname, - 'scenario-system-message.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('records system instructions from system message objects', async () => { - await createRunner() - .expect({ - span: container => { - const systemInstructions = container.items - .filter(span => span.attributes[SENTRY_OP]?.value === GEN_AI_INVOKE_AGENT) - .map(span => span.attributes[GEN_AI_SYSTEM_INSTRUCTIONS]?.value); - - expect(systemInstructions).toEqual([ - '[{"type":"text","content":"Be brief."}]', - '[{"type":"text","content":"Be brief."},{"type":"text","content":"Be kind."}]', - ]); - }, - }) - .start() - .completed(); - }); - }, - { - additionalDependencies: { - ai: vercelAiVersion, - }, - }, - ); - createEsmTests( - __dirname, - 'scenario-openai-conversation.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('derives gen_ai.conversation.id from the OpenAI `conversation` provider option', async () => { - await createRunner() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); - const genAiSpans = container.items.filter(s => - String(s.attributes[SENTRY_OP]?.value ?? '').startsWith('gen_ai.'), - ); - const conversationIdOf = (span: (typeof genAiSpans)[number]) => - span.attributes[GEN_AI_CONVERSATION_ID]?.value; - const invokeAgentSpans = genAiSpans.filter(s => s.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent'); - expect(invokeAgentSpans).toHaveLength(4); - const [firstTurn, secondTurn, chainedTurn, apiTurn] = invokeAgentSpans.sort( - (a, b) => a.start_timestamp - b.start_timestamp, - ); - - // `providerOptions.openai.conversation` is the Conversations API id: the same on every turn. - expect(conversationIdOf(firstTurn!)).toBe('conv_abc123'); - // The Azure Responses API uses the `azure` key for the same option. - expect(conversationIdOf(secondTurn!)).toBe('conv_azure'); - // `previousResponseId` names a response rather than a thread, and the response's own - // `responseId` is recorded as `gen_ai.response.id` only. - expect(conversationIdOf(chainedTurn!)).toBeUndefined(); - // `Sentry.setConversationId()` beats the provider option. - expect(conversationIdOf(apiTurn!)).toBe('conv-from-api'); - - // Model-call and tool spans carry their operation's id, even though their start events - // do not carry `providerOptions`. - const modelCallSpans = genAiSpans.filter( - s => s.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', - ); - expect(modelCallSpans.map(conversationIdOf).sort()).toEqual([ - 'conv-from-api', - 'conv_abc123', - 'conv_azure', - undefined, - ]); - const toolSpan = genAiSpans.find(s => s.attributes[SENTRY_OP]?.value === 'gen_ai.execute_tool')!; - expect(toolSpan).toBeDefined(); - expect(conversationIdOf(toolSpan)).toBe('conv_azure'); - }, - }) - .start() - .completed(); - }); - }, - { - additionalDependencies: { - ai: vercelAiVersion, - }, - }, - ); - createEsmTests( - __dirname, - 'scenario-cache-tokens.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('reads cache token counts from the SDK usage object', async () => { - await createRunner() - .expect({ - span: container => { - const segment = container.items.find(span => span.is_segment && span.name === 'main'); - expect(segment).toBeDefined(); - const spans = container.items.filter( - span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai', - ); - const generateContent = spans.find( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', - )!; - expect(generateContent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(120); - expect(generateContent.attributes[GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS]?.value).toBe(80); - expect(generateContent.attributes[GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS]?.value).toBe(20); - const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; - expect(invokeAgent.attributes[GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS]?.value).toBe(80); - expect(invokeAgent.attributes[GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS]?.value).toBe(20); - }, - }) - .start() - .completed(); - }); - }, - { - additionalDependencies: { - ai: vercelAiVersion, - }, - }, - ); createEsmTests( __dirname, 'scenario-embeddings.mjs', @@ -989,6 +912,7 @@ export function registerVercelAiGenerationTests(version: '6' | '7', vercelAiVers }, }, ); + createEsmTests( __dirname, 'scenario-generate-object.mjs', @@ -1043,46 +967,165 @@ export function registerVercelAiStreamingTests(version: '6' | '7', vercelAiVersi afterAll(() => { cleanupChildProcesses(); }); + const defaultScenarios = [ + 'scenario-stream-text.mjs', + 'scenario-stream-tools.mjs', + 'scenario-stream-structured-output.mjs', + 'scenario-provider-metadata.mjs', + 'scenario-system-message.mjs', + 'scenario-openai-conversation.mjs', + 'scenario-cache-tokens.mjs', + 'scenario-tool-loop-agent.mjs', + 'scenario-concurrent.mjs', + 'scenario-concurrent-stream.mjs', + ]; createEsmTests( __dirname, - 'scenario-concurrent-stream.mjs', + defaultScenarios, 'instrument.mjs', (createRunner, test) => { + let scenarios: ReturnType | undefined; + async function runScenarios() { + const runner = createRunner(); + + const collectors = defaultScenarios.map(name => + runner.collectStreamedSpansUntilSegment( + span => span.name === 'main' && span.attributes['test.scenario']?.value === name, + ), + ); + await runner.start().completed(); + return { spans: await Promise.all(collectors) }; + } + const getScenarios = () => (scenarios ??= runScenarios()); + + test('creates streamText spans with the model call parented to invoke_agent', async () => { + const container = { items: (await getScenarios()).spans[0]! }; + + const segment = container.items.find(span => span.is_segment && span.name === 'main'); + expect(segment).toBeDefined(); + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai'); + const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; + expect(invokeAgent.attributes['vercel.ai.operationId']?.value).toBe('ai.streamText'); + + const generateContent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content')!; + expect(generateContent.parent_span_id).toBe(invokeAgent.span_id); + expect(generateContent.attributes['vercel.ai.operationId']?.value).toBe('ai.streamText.doStream'); + + // The stream's final usage/finish/output arrive only as the stream drains, after the + // channel already resolved the model call. Tapping the stream recovers them onto the + // model-call span on every path (v7 channel, v6 OTel, v6 orchestrion). + expect(generateContent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10); + expect(generateContent.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20); + expect(generateContent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(30); + expect(generateContent.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value).toBe('["stop"]'); + expect(generateContent.attributes[GEN_AI_OUTPUT_MESSAGES]?.value).toBe( + '[{"role":"assistant","parts":[{"type":"text","content":"Stream response!"}],"finish_reason":"stop"}]', + ); + + expect(generateContent.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toEqual(expect.any(Number)); + + // The summed usage and output also land on the parent invoke_agent span, whose own + // channel result is otherwise undefined for a stream. + expect(invokeAgent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10); + expect(invokeAgent.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20); + expect(invokeAgent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(30); + expect(invokeAgent.attributes[GEN_AI_OUTPUT_MESSAGES]?.value).toBe( + '[{"role":"assistant","parts":[{"type":"text","content":"Stream response!"}],"finish_reason":"stop"}]', + ); + }); + + test('captures usage, tool calls and output across a multi-step streamText', async () => { + const container = { items: (await getScenarios()).spans[1]! }; + + const segment = container.items.find(span => span.is_segment && span.name === 'main'); + expect(segment).toBeDefined(); + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai'); + const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; + expect(invokeAgent.status).toBe('ok'); + expect(invokeAgent.attributes['vercel.ai.operationId']?.value).toBe('ai.streamText'); + // Usage is summed across the two streamed model calls (10+15, 20+25, 30+40). + expect(invokeAgent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(25); + expect(invokeAgent.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(45); + expect(invokeAgent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(70); + + const generateContents = spans.filter(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content'); + expect(generateContents).toHaveLength(2); + generateContents.forEach(span => expect(span.parent_span_id).toBe(invokeAgent.span_id)); + + // The step that streamed a tool call: tool-call output part + tool-calls finish reason. + const toolStep = generateContents.find( + span => span.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value === '["tool-calls"]', + )!; + expect(toolStep.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10); + expect(toolStep.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20); + const toolStepOutput = toolStep.attributes[GEN_AI_OUTPUT_MESSAGES]?.value as string; + expect(toolStepOutput).toContain('"type":"tool_call"'); + expect(toolStepOutput).toContain('getWeather'); + + // The step that streamed the final answer text. + const textStep = generateContents.find( + span => span.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value === '["stop"]', + )!; + expect(textStep.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(15); + expect(textStep.attributes[GEN_AI_OUTPUT_MESSAGES]?.value).toContain('Sunny, 72°F.'); + + // A tool span is emitted for the streamed tool call. Its parent and recorded input/output + // vary by path during stream consumption (tool i/o is covered by the non-stream scenario + // and the v7 path), so here we just assert the span exists with the right name/status. + const executeTool = spans.find(span => span.name === 'execute_tool getWeather')!; + expect(executeTool.status).toBe('ok'); + expect(executeTool.attributes[GEN_AI_TOOL_NAME]?.value).toBe('getWeather'); + }); + + test('captures streamed structured output (streamText with experimental_output)', async () => { + const container = { items: (await getScenarios()).spans[2]! }; + + const segment = container.items.find(span => span.is_segment && span.name === 'main'); + expect(segment).toBeDefined(); + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai'); + const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; + expect(invokeAgent.status).toBe('ok'); + expect(invokeAgent.attributes['vercel.ai.operationId']?.value).toBe('ai.streamText'); + expect(invokeAgent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(30); + + const generateContent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content')!; + expect(generateContent.parent_span_id).toBe(invokeAgent.span_id); + expect(generateContent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(12); + expect(generateContent.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(18); + expect(generateContent.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value).toBe('["stop"]'); + // The streamed JSON object is accumulated from the text deltas and captured as the + // model's output text (embedded as an escaped JSON string in the output message). + const output = generateContent.attributes[GEN_AI_OUTPUT_MESSAGES]?.value as string; + expect(output).toContain('San Francisco'); + expect(output).toContain('sunny'); + }); + // A single model instance shared by two concurrent `streamText` calls carries only one // captured-parent slot, so both model calls must still land under their own `invoke_agent` — not // collapse onto whichever operation resolved the shared model last. test('parents concurrent streamText calls that share one model instance correctly', async () => { - await createRunner() - .expect({ - span: container => { - const segment = container.items.find(span => span.is_segment && span.name === 'main'); - expect(segment).toBeDefined(); - const spans = container.items.filter( - span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai', - ); - const invokeAgents = spans.filter(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent'); - const generateContents = spans.filter( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', - ); + const container = { items: (await getScenarios()).spans[9]! }; - // Two concurrent operations -> two invoke_agent + two generate_content spans. - expect(invokeAgents).toHaveLength(2); - expect(generateContents).toHaveLength(2); + const segment = container.items.find(span => span.is_segment && span.name === 'main'); + expect(segment).toBeDefined(); + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai'); + const invokeAgents = spans.filter(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent'); + const generateContents = spans.filter(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content'); - const agentSpanIds = new Set(invokeAgents.map(span => span.span_id)); + // Two concurrent operations -> two invoke_agent + two generate_content spans. + expect(invokeAgents).toHaveLength(2); + expect(generateContents).toHaveLength(2); - // Each model call lands under an invoke_agent span... - for (const span of generateContents) { - expect(agentSpanIds.has(span.parent_span_id!)).toBe(true); - } - // ...a distinct one each (no cross-attribution despite the shared model instance)... - expect(new Set(generateContents.map(span => span.parent_span_id)).size).toBe(2); - // ...and both operations sit under the same `main` parent. - expect(new Set(invokeAgents.map(span => span.parent_span_id)).size).toBe(1); - }, - }) - .start() - .completed(); + const agentSpanIds = new Set(invokeAgents.map(span => span.span_id)); + + // Each model call lands under an invoke_agent span... + for (const span of generateContents) { + expect(agentSpanIds.has(span.parent_span_id!)).toBe(true); + } + // ...a distinct one each (no cross-attribution despite the shared model instance)... + expect(new Set(generateContents.map(span => span.parent_span_id)).size).toBe(2); + // ...and both operations sit under the same `main` parent. + expect(new Set(invokeAgents.map(span => span.parent_span_id)).size).toBe(1); }); }, { @@ -1091,51 +1134,33 @@ export function registerVercelAiStreamingTests(version: '6' | '7', vercelAiVersi }, }, ); + createEsmTests( __dirname, - 'scenario-stream-text.mjs', - 'instrument.mjs', + 'scenario-aborted-stream-text.mjs', + 'instrument-abort.mjs', (createRunner, test) => { - test('creates streamText spans with the model call parented to invoke_agent', async () => { + test('aborting a stream with a non-AbortError reason leaves no unhandled rejection', async () => { + await createRunner().ensureNoErrorOutput().start().completed(); + }); + + test('an aborted stream finishes its spans with an error status and no result attributes', async () => { await createRunner() .expect({ span: container => { - const segment = container.items.find(span => span.is_segment && span.name === 'main'); - expect(segment).toBeDefined(); - const spans = container.items.filter( - span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai', - ); - const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; - expect(invokeAgent.attributes['vercel.ai.operationId']?.value).toBe('ai.streamText'); + const invokeAgent = container.items.find( + span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent', + )!; + expect(invokeAgent.status).toBe('error'); + expect(invokeAgent.attributes[GEN_AI_REQUEST_MODEL]?.value).toBe('mock-model-id'); + expect(invokeAgent.attributes[GEN_AI_RESPONSE_MODEL]).toBeUndefined(); + expect(invokeAgent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toBeUndefined(); + expect(invokeAgent.attributes[GEN_AI_OUTPUT_MESSAGES]).toBeUndefined(); - const generateContent = spans.find( + const generateContent = container.items.find( span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', )!; - expect(generateContent.parent_span_id).toBe(invokeAgent.span_id); - expect(generateContent.attributes['vercel.ai.operationId']?.value).toBe('ai.streamText.doStream'); - - // The stream's final usage/finish/output arrive only as the stream drains, after the - // channel already resolved the model call. Tapping the stream recovers them onto the - // model-call span on every path (v7 channel, v6 OTel, v6 orchestrion). - expect(generateContent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10); - expect(generateContent.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20); - expect(generateContent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(30); - expect(generateContent.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value).toBe('["stop"]'); - expect(generateContent.attributes[GEN_AI_OUTPUT_MESSAGES]?.value).toBe( - '[{"role":"assistant","parts":[{"type":"text","content":"Stream response!"}],"finish_reason":"stop"}]', - ); - expect(generateContent.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toEqual( - expect.any(Number), - ); - - // The summed usage and output also land on the parent invoke_agent span, whose own - // channel result is otherwise undefined for a stream. - expect(invokeAgent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10); - expect(invokeAgent.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20); - expect(invokeAgent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(30); - expect(invokeAgent.attributes[GEN_AI_OUTPUT_MESSAGES]?.value).toBe( - '[{"role":"assistant","parts":[{"type":"text","content":"Stream response!"}],"finish_reason":"stop"}]', - ); + expect(generateContent.status).toBe('error'); }, }) .start() @@ -1148,6 +1173,7 @@ export function registerVercelAiStreamingTests(version: '6' | '7', vercelAiVersi }, }, ); + createEsmTests( __dirname, 'scenario-stream-object.mjs', @@ -1193,112 +1219,7 @@ export function registerVercelAiStreamingTests(version: '6' | '7', vercelAiVersi }, }, ); - createEsmTests( - __dirname, - 'scenario-stream-tools.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('captures usage, tool calls and output across a multi-step streamText', async () => { - await createRunner() - .expect({ - span: container => { - const segment = container.items.find(span => span.is_segment && span.name === 'main'); - expect(segment).toBeDefined(); - const spans = container.items.filter( - span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai', - ); - const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; - expect(invokeAgent.status).toBe('ok'); - expect(invokeAgent.attributes['vercel.ai.operationId']?.value).toBe('ai.streamText'); - // Usage is summed across the two streamed model calls (10+15, 20+25, 30+40). - expect(invokeAgent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(25); - expect(invokeAgent.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(45); - expect(invokeAgent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(70); - - const generateContents = spans.filter( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', - ); - expect(generateContents).toHaveLength(2); - generateContents.forEach(span => expect(span.parent_span_id).toBe(invokeAgent.span_id)); - - // The step that streamed a tool call: tool-call output part + tool-calls finish reason. - const toolStep = generateContents.find( - span => span.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value === '["tool-calls"]', - )!; - expect(toolStep.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10); - expect(toolStep.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(20); - const toolStepOutput = toolStep.attributes[GEN_AI_OUTPUT_MESSAGES]?.value as string; - expect(toolStepOutput).toContain('"type":"tool_call"'); - expect(toolStepOutput).toContain('getWeather'); - - // The step that streamed the final answer text. - const textStep = generateContents.find( - span => span.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value === '["stop"]', - )!; - expect(textStep.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(15); - expect(textStep.attributes[GEN_AI_OUTPUT_MESSAGES]?.value).toContain('Sunny, 72°F.'); - - // A tool span is emitted for the streamed tool call. Its parent and recorded input/output - // vary by path during stream consumption (tool i/o is covered by the non-stream scenario - // and the v7 path), so here we just assert the span exists with the right name/status. - const executeTool = spans.find(span => span.name === 'execute_tool getWeather')!; - expect(executeTool.status).toBe('ok'); - expect(executeTool.attributes[GEN_AI_TOOL_NAME]?.value).toBe('getWeather'); - }, - }) - .start() - .completed(); - }); - }, - { - additionalDependencies: { - ai: vercelAiVersion, - }, - }, - ); - createEsmTests( - __dirname, - 'scenario-stream-structured-output.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('captures streamed structured output (streamText with experimental_output)', async () => { - await createRunner() - .expect({ - span: container => { - const segment = container.items.find(span => span.is_segment && span.name === 'main'); - expect(segment).toBeDefined(); - const spans = container.items.filter( - span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.vercelai', - ); - const invokeAgent = spans.find(span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent')!; - expect(invokeAgent.status).toBe('ok'); - expect(invokeAgent.attributes['vercel.ai.operationId']?.value).toBe('ai.streamText'); - expect(invokeAgent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(30); - const generateContent = spans.find( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', - )!; - expect(generateContent.parent_span_id).toBe(invokeAgent.span_id); - expect(generateContent.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(12); - expect(generateContent.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(18); - expect(generateContent.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value).toBe('["stop"]'); - // The streamed JSON object is accumulated from the text deltas and captured as the - // model's output text (embedded as an escaped JSON string in the output message). - const output = generateContent.attributes[GEN_AI_OUTPUT_MESSAGES]?.value as string; - expect(output).toContain('San Francisco'); - expect(output).toContain('sunny'); - }, - }) - .start() - .completed(); - }); - }, - { - additionalDependencies: { - ai: vercelAiVersion, - }, - }, - ); createEsmTests( __dirname, 'scenario-stream-object-invalid.mjs', @@ -1331,42 +1252,4 @@ export function registerVercelAiStreamingTests(version: '6' | '7', vercelAiVersi }, }, ); - createEsmTests( - __dirname, - 'scenario-aborted-stream-text.mjs', - 'instrument-abort.mjs', - (createRunner, test) => { - test('aborting a stream with a non-AbortError reason leaves no unhandled rejection', async () => { - await createRunner().ensureNoErrorOutput().start().completed(); - }); - - test('an aborted stream finishes its spans with an error status and no result attributes', async () => { - await createRunner() - .expect({ - span: container => { - const invokeAgent = container.items.find( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.invoke_agent', - )!; - expect(invokeAgent.status).toBe('error'); - expect(invokeAgent.attributes[GEN_AI_REQUEST_MODEL]?.value).toBe('mock-model-id'); - expect(invokeAgent.attributes[GEN_AI_RESPONSE_MODEL]).toBeUndefined(); - expect(invokeAgent.attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toBeUndefined(); - expect(invokeAgent.attributes[GEN_AI_OUTPUT_MESSAGES]).toBeUndefined(); - - const generateContent = container.items.find( - span => span.attributes[SENTRY_OP]?.value === 'gen_ai.generate_content', - )!; - expect(generateContent.status).toBe('error'); - }, - }) - .start() - .completed(); - }); - }, - { - additionalDependencies: { - ai: vercelAiVersion, - }, - }, - ); } diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/v6/generation/test.ts b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/v6/generation/test.ts index eee2eec32712..908c5bfb8e53 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/v6/generation/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/v6/generation/test.ts @@ -1,8 +1,8 @@ +import { join } from 'node:path'; import { afterAll, expect, describe } from 'vitest'; import { SENTRY_OP, SENTRY_ORIGIN } from '@sentry/conventions/attributes'; import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../../../../utils/runner'; import { registerVercelAiGenerationTests } from '../../suite'; -import { join } from 'node:path'; describe('Vercel AI integration (version 6)', () => { registerVercelAiGenerationTests('6', '^6.0.0'); @@ -12,7 +12,6 @@ describe('Vercel AI telemetry metadata (v6)', () => { afterAll(() => { cleanupChildProcesses(); }); - createEsmAndCjsTests( join(__dirname, '../..'), 'scenario-telemetry-metadata.mjs', diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/v7/generation/test.ts b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/v7/generation/test.ts index 0342b2727dd6..c3daaa61632a 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/v7/generation/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/v7/generation/test.ts @@ -32,7 +32,6 @@ describe('Vercel AI integration experimental_decide', () => { afterAll(() => { cleanupChildProcesses(); }); - createEsmTests( join(__dirname, '../..'), 'scenario-decide.mjs', @@ -137,7 +136,6 @@ describe('Vercel AI runtime and tool context (v7)', () => { afterAll(() => { cleanupChildProcesses(); }); - createEsmTests( join(__dirname, '../..'), 'scenario-runtime-context.mjs', @@ -196,7 +194,6 @@ describe('Vercel AI calls with recording off (v7)', () => { afterAll(() => { cleanupChildProcesses(); }); - createEsmTests( join(__dirname, '../..'), 'scenario-no-recording.mjs',