Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 0 additions & 7 deletions packages/server-utils/src/ai/vercel-ai/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import type { ToolCallSpanContext } from './types';

// Global map to track tool call IDs to their corresponding span contexts.
// This allows us to capture tool errors and link them to the correct span
// without keeping full Span objects (and their potentially large attributes) alive.
export const toolCallSpanContextMap = new Map<string, ToolCallSpanContext>();

// Used to make tool descriptions available to execute_tool spans in the span streaming path.
// Streamed spans are processed individually, so execute_tool spans cannot look up descriptions
// from their sibling doGenerate span on span end (as we do for transactions).
Expand Down
11 changes: 1 addition & 10 deletions packages/server-utils/src/ai/vercel-ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from '@sentry/conventions/attributes';
import { GENERAL_FUNCTION_SPAN_OP } from '@sentry/conventions/op';
import { GEN_AI_TOOL_CALL_ID_ATTRIBUTE } from '../core/gen-ai-attributes';
import { SPAN_TO_OPERATION_NAME, toolCallSpanContextMap, toolDescriptionMap } from './constants';
import { SPAN_TO_OPERATION_NAME, toolDescriptionMap } from './constants';
import type { TokenSummary } from './types';
import {
accumulateTokensForParent,
Expand Down Expand Up @@ -411,15 +411,6 @@ function processToolCallSpan(span: Span, attributes: SpanAttributes): void {
renameAttributeKey(attributes, AI_TOOL_CALL_NAME_ATTRIBUTE, GEN_AI_TOOL_NAME);
renameAttributeKey(attributes, AI_TOOL_CALL_ID_ATTRIBUTE, GEN_AI_TOOL_CALL_ID_ATTRIBUTE);

// Store the span context in our global map using the tool call ID.
// This allows us to capture tool errors and link them to the correct span
// without retaining the full Span object in memory.
const toolCallId = attributes[GEN_AI_TOOL_CALL_ID_ATTRIBUTE];

if (typeof toolCallId === 'string') {
toolCallSpanContextMap.set(toolCallId, span.spanContext());
}

const toolName = attributes[GEN_AI_TOOL_NAME];
if (toolName) {
span.updateName(`execute_tool ${toolName}`);
Expand Down
5 changes: 0 additions & 5 deletions packages/server-utils/src/ai/vercel-ai/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@ export interface TokenSummary {
inputTokens: number;
outputTokens: number;
}

export interface ToolCallSpanContext {
traceId: string;
spanId: string;
}
17 changes: 1 addition & 16 deletions packages/server-utils/src/ai/vercel-ai/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
GEN_AI_USAGE_OUTPUT_TOKENS,
} from '@sentry/conventions/attributes';
import { extractSystemInstructions } from '../core/utils';
import { toolCallSpanContextMap } from './constants';
import type { TokenSummary, ToolCallSpanContext } from './types';
import type { TokenSummary } from './types';
import { AI_PROMPT_ATTRIBUTE, AI_PROMPT_MESSAGES_ATTRIBUTE } from './vercel-ai-attributes';

/**
Expand Down Expand Up @@ -123,20 +122,6 @@ export function applyToolDescriptionsAndTokens(spans: SpanJSON[], tokenAccumulat
}
}

/**
* Get the span context associated with a tool call ID.
*/
export function _INTERNAL_getSpanContextForToolCallId(toolCallId: string): ToolCallSpanContext | undefined {
return toolCallSpanContextMap.get(toolCallId);
}

/**
* Clean up the span mapping for a tool call ID
*/
export function _INTERNAL_cleanupToolCallSpanContext(toolCallId: string): void {
toolCallSpanContextMap.delete(toolCallId);
}

/**
* Convert an array of tool strings to a JSON string
*/
Expand Down
Loading