Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ module.exports = [
import: createImport('init'),
ignore: [...builtinModules, ...nodePrefixedBuiltinModules],
gzip: true,
limit: '158 KB',
limit: '159 KB',
},
{
name: '@sentry/node - without tracing',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ describe('Vercel AI integration', () => {
]),
};

const EXPECTED_AVAILABLE_TOOLS_JSON =
'[{"type":"function","name":"getWeather","parameters":{"type":"object","properties":{"location":{"type":"string"}},"required":["location"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}}]';

const EXPECTED_TRANSACTION_DEFAULT_PII_TRUE = {
transaction: 'main',
spans: expect.arrayContaining([
Expand Down Expand Up @@ -358,7 +361,7 @@ describe('Vercel AI integration', () => {
'vercel.ai.prompt.format': expect.any(String),
'gen_ai.request.messages': expect.any(String),
'vercel.ai.prompt.toolChoice': expect.any(String),
'gen_ai.request.available_tools': expect.any(Array),
'gen_ai.request.available_tools': EXPECTED_AVAILABLE_TOOLS_JSON,
'vercel.ai.response.finishReason': 'tool-calls',
'vercel.ai.response.id': expect.any(String),
'vercel.ai.response.model': 'mock-model-id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ describe('Vercel AI integration (V5)', () => {
]),
};

const EXPECTED_AVAILABLE_TOOLS_JSON =
'[{"type":"function","name":"getWeather","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"location":{"type":"string"}},"required":["location"],"additionalProperties":false}}]';

const EXPECTED_TRANSACTION_DEFAULT_PII_TRUE = {
transaction: 'main',
spans: expect.arrayContaining([
Expand Down Expand Up @@ -348,7 +351,7 @@ describe('Vercel AI integration (V5)', () => {
'vercel.ai.pipeline.name': 'generateText.doGenerate',
'gen_ai.request.messages': expect.any(String),
'vercel.ai.prompt.toolChoice': expect.any(String),
'gen_ai.request.available_tools': expect.any(Array),
'gen_ai.request.available_tools': EXPECTED_AVAILABLE_TOOLS_JSON,
'vercel.ai.response.finishReason': 'tool-calls',
'vercel.ai.response.id': expect.any(String),
'vercel.ai.response.model': 'mock-model-id',
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/tracing/vercel-ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { getTruncatedJsonString } from '../ai/utils';
import { toolCallSpanMap } from './constants';
import type { TokenSummary } from './types';
import { accumulateTokensForParent, applyAccumulatedTokens } from './utils';
import { accumulateTokensForParent, applyAccumulatedTokens, convertAvailableToolsToJsonString } from './utils';
import type { ProviderMetadata } from './vercel-ai-attributes';
import {
AI_MODEL_ID_ATTRIBUTE,
Expand Down Expand Up @@ -123,6 +123,13 @@ function processEndedVercelAiSpan(span: SpanJSON): void {
attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] + attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE];
}

// Convert the available tools array to a JSON string
if (attributes[AI_PROMPT_TOOLS_ATTRIBUTE] && Array.isArray(attributes[AI_PROMPT_TOOLS_ATTRIBUTE])) {
attributes[AI_PROMPT_TOOLS_ATTRIBUTE] = convertAvailableToolsToJsonString(
attributes[AI_PROMPT_TOOLS_ATTRIBUTE] as unknown[],
);
}

// Rename AI SDK attributes to standardized gen_ai attributes
renameAttributeKey(attributes, AI_PROMPT_MESSAGES_ATTRIBUTE, 'gen_ai.request.messages');
renameAttributeKey(attributes, AI_RESPONSE_TEXT_ATTRIBUTE, 'gen_ai.response.text');
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/tracing/vercel-ai/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,20 @@ export function _INTERNAL_getSpanForToolCallId(toolCallId: string): Span | undef
export function _INTERNAL_cleanupToolCallSpan(toolCallId: string): void {
toolCallSpanMap.delete(toolCallId);
}

/**
* Convert an array of tool strings to a JSON string
*/
export function convertAvailableToolsToJsonString(tools: unknown[]): string {
const toolObjects = tools.map(tool => {
if (typeof tool === 'string') {
try {
return JSON.parse(tool);
} catch {
return tool;
}
}
return tool;
});
return JSON.stringify(toolObjects);
}
Loading