|
| 1 | +import { ReadableSpan } from "@opentelemetry/sdk-trace-node"; |
| 2 | +import { SpanAttributes } from "@traceloop/ai-semantic-conventions"; |
| 3 | + |
| 4 | +const AI_GENERATE_TEXT_DO_GENERATE = "ai.generateText.doGenerate"; |
| 5 | +const AI_STREAM_TEXT_DO_STREAM = "ai.streamText.doStream"; |
| 6 | +const AI_RESPONSE_TEXT = "ai.response.text"; |
| 7 | +const AI_PROMPT_MESSAGES = "ai.prompt.messages"; |
| 8 | +const AI_USAGE_PROMPT_TOKENS = "ai.usage.promptTokens"; |
| 9 | +const AI_USAGE_COMPLETION_TOKENS = "ai.usage.completionTokens"; |
| 10 | +const AI_MODEL_PROVIDER = "ai.model.provider"; |
| 11 | + |
| 12 | +export const transformAiSdkSpanName = (span: ReadableSpan): void => { |
| 13 | + const nameMap: Record<string, string> = { |
| 14 | + [AI_GENERATE_TEXT_DO_GENERATE]: "ai.generateText.generate", |
| 15 | + [AI_STREAM_TEXT_DO_STREAM]: "ai.streamText.stream", |
| 16 | + }; |
| 17 | + |
| 18 | + if (span.name in nameMap) { |
| 19 | + // Unfortunately, the span name is not writable as this is not the intended behavior |
| 20 | + // but it is a workaround to set the correct span name |
| 21 | + (span as any).name = nameMap[span.name]; |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +export const transformResponseText = ( |
| 26 | + attributes: Record<string, any>, |
| 27 | +): void => { |
| 28 | + if (AI_RESPONSE_TEXT in attributes) { |
| 29 | + attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.content`] = |
| 30 | + attributes[AI_RESPONSE_TEXT]; |
| 31 | + attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.role`] = "assistant"; |
| 32 | + delete attributes[AI_RESPONSE_TEXT]; |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +export const transformPromptMessages = ( |
| 37 | + attributes: Record<string, any>, |
| 38 | +): void => { |
| 39 | + if (AI_PROMPT_MESSAGES in attributes) { |
| 40 | + try { |
| 41 | + const messages = JSON.parse(attributes[AI_PROMPT_MESSAGES] as string); |
| 42 | + messages.forEach((msg: { role: string; content: any }, index: number) => { |
| 43 | + attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] = |
| 44 | + typeof msg.content === "string" |
| 45 | + ? msg.content |
| 46 | + : JSON.stringify(msg.content); |
| 47 | + attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.role`] = msg.role; |
| 48 | + }); |
| 49 | + delete attributes[AI_PROMPT_MESSAGES]; |
| 50 | + } catch { |
| 51 | + // Skip if JSON parsing fails |
| 52 | + } |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +export const transformPromptTokens = ( |
| 57 | + attributes: Record<string, any>, |
| 58 | +): void => { |
| 59 | + if (AI_USAGE_PROMPT_TOKENS in attributes) { |
| 60 | + attributes[`${SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`] = |
| 61 | + attributes[AI_USAGE_PROMPT_TOKENS]; |
| 62 | + delete attributes[AI_USAGE_PROMPT_TOKENS]; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +export const transformCompletionTokens = ( |
| 67 | + attributes: Record<string, any>, |
| 68 | +): void => { |
| 69 | + if (AI_USAGE_COMPLETION_TOKENS in attributes) { |
| 70 | + attributes[`${SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`] = |
| 71 | + attributes[AI_USAGE_COMPLETION_TOKENS]; |
| 72 | + delete attributes[AI_USAGE_COMPLETION_TOKENS]; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +export const calculateTotalTokens = (attributes: Record<string, any>): void => { |
| 77 | + const promptTokens = attributes[`${SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`]; |
| 78 | + const completionTokens = |
| 79 | + attributes[`${SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`]; |
| 80 | + |
| 81 | + if (promptTokens && completionTokens) { |
| 82 | + attributes[`${SpanAttributes.LLM_USAGE_TOTAL_TOKENS}`] = |
| 83 | + Number(promptTokens) + Number(completionTokens); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +export const transformVendor = (attributes: Record<string, any>): void => { |
| 88 | + if (AI_MODEL_PROVIDER in attributes) { |
| 89 | + const vendor = attributes[AI_MODEL_PROVIDER]; |
| 90 | + if (vendor && vendor.startsWith("openai")) { |
| 91 | + attributes[SpanAttributes.LLM_SYSTEM] = "OpenAI"; |
| 92 | + } else { |
| 93 | + attributes[SpanAttributes.LLM_SYSTEM] = vendor; |
| 94 | + } |
| 95 | + delete attributes[AI_MODEL_PROVIDER]; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +export const transformAiSdkAttributes = ( |
| 100 | + attributes: Record<string, any>, |
| 101 | +): void => { |
| 102 | + transformResponseText(attributes); |
| 103 | + transformPromptMessages(attributes); |
| 104 | + transformPromptTokens(attributes); |
| 105 | + transformCompletionTokens(attributes); |
| 106 | + calculateTotalTokens(attributes); |
| 107 | + transformVendor(attributes); |
| 108 | +}; |
| 109 | + |
| 110 | +export const transformAiSdkSpan = (span: ReadableSpan): void => { |
| 111 | + transformAiSdkSpanName(span); |
| 112 | + transformAiSdkAttributes(span.attributes); |
| 113 | +}; |
0 commit comments