|
| 1 | +/* |
| 2 | + * Copyright Traceloop |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import type * as genai from "@google/genai"; |
| 18 | + |
| 19 | +// Maps a single @google/genai Part to its OTel-compliant part object. |
| 20 | +// |
| 21 | +// text (thought: true) → ReasoningPart { type: "reasoning", content } |
| 22 | +// text → TextPart |
| 23 | +// inlineData (base64) → BlobPart { modality derived from mimeType, mime_type, content } |
| 24 | +// fileData (URI) → UriPart { modality derived from mimeType, mime_type, uri } |
| 25 | +// functionCall → ToolCallRequestPart |
| 26 | +// functionResponse → ToolCallResponsePart |
| 27 | +// executableCode → GenericPart { type: "executable_code", language, content } |
| 28 | +// codeExecutionResult → GenericPart { type: "code_execution_result", outcome, content } |
| 29 | +// <unknown> → GenericPart |
| 30 | + |
| 31 | +/** OTel gen_ai part type strings used by the GenAI mapper. */ |
| 32 | +export const GenAIOtelPartType = { |
| 33 | + TEXT: "text", |
| 34 | + REASONING: "reasoning", |
| 35 | + BLOB: "blob", |
| 36 | + URI: "uri", |
| 37 | + TOOL_CALL: "tool_call", |
| 38 | + TOOL_CALL_RESPONSE: "tool_call_response", |
| 39 | + EXECUTABLE_CODE: "executable_code", |
| 40 | + CODE_EXECUTION_RESULT: "code_execution_result", |
| 41 | + UNKNOWN: "unknown", |
| 42 | +} as const; |
| 43 | + |
| 44 | +function genaiModalityFromMimeType(mimeType: string): string { |
| 45 | + if (mimeType.startsWith("image/")) return "image"; |
| 46 | + if (mimeType.startsWith("audio/")) return "audio"; |
| 47 | + if (mimeType.startsWith("video/")) return "video"; |
| 48 | + return "document"; |
| 49 | +} |
| 50 | + |
| 51 | +export function mapGenAIContentBlock(block: genai.Part | string): object { |
| 52 | + if (typeof block === "string") { |
| 53 | + return { type: GenAIOtelPartType.TEXT, content: block }; |
| 54 | + } |
| 55 | + |
| 56 | + // thought: true marks a model reasoning/thinking block (Gemini thinking models). |
| 57 | + // text may be undefined on malformed parts — fall back to empty string. |
| 58 | + if (block.thought === true) { |
| 59 | + return { type: GenAIOtelPartType.REASONING, content: block.text ?? "" }; |
| 60 | + } |
| 61 | + |
| 62 | + if (block.text !== undefined) { |
| 63 | + return { type: GenAIOtelPartType.TEXT, content: block.text }; |
| 64 | + } |
| 65 | + |
| 66 | + if (block.inlineData) { |
| 67 | + const mimeType = block.inlineData.mimeType; |
| 68 | + return { |
| 69 | + type: GenAIOtelPartType.BLOB, |
| 70 | + modality: genaiModalityFromMimeType(mimeType ?? ""), |
| 71 | + ...(mimeType ? { mime_type: mimeType } : {}), |
| 72 | + content: block.inlineData.data, |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + if (block.fileData) { |
| 77 | + const mimeType = block.fileData.mimeType; |
| 78 | + return { |
| 79 | + type: GenAIOtelPartType.URI, |
| 80 | + modality: genaiModalityFromMimeType(mimeType ?? ""), |
| 81 | + ...(mimeType ? { mime_type: mimeType } : {}), |
| 82 | + uri: block.fileData.fileUri, |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + if (block.functionCall) { |
| 87 | + return { |
| 88 | + type: GenAIOtelPartType.TOOL_CALL, |
| 89 | + id: block.functionCall.id ?? null, |
| 90 | + name: block.functionCall.name, |
| 91 | + arguments: block.functionCall.args, |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + if (block.functionResponse) { |
| 96 | + const resp = block.functionResponse.response; |
| 97 | + return { |
| 98 | + type: GenAIOtelPartType.TOOL_CALL_RESPONSE, |
| 99 | + id: block.functionResponse.id ?? null, |
| 100 | + // Serialize objects to JSON string so the dashboard can display them. |
| 101 | + // Normalize undefined (missing response) to null so the field is always present. |
| 102 | + response: |
| 103 | + resp === undefined |
| 104 | + ? null |
| 105 | + : resp !== null && typeof resp === "object" |
| 106 | + ? JSON.stringify(resp) |
| 107 | + : resp, |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + if (block.executableCode) { |
| 112 | + return { |
| 113 | + type: GenAIOtelPartType.EXECUTABLE_CODE, |
| 114 | + language: block.executableCode.language, |
| 115 | + content: block.executableCode.code, |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + if (block.codeExecutionResult) { |
| 120 | + return { |
| 121 | + type: GenAIOtelPartType.CODE_EXECUTION_RESULT, |
| 122 | + outcome: block.codeExecutionResult.outcome, |
| 123 | + content: block.codeExecutionResult.output, |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + // Do not spread the block — it may contain circular references or sensitive data. |
| 128 | + return { type: GenAIOtelPartType.UNKNOWN }; |
| 129 | +} |
0 commit comments