@introspection-sdk/introspection-node 0.20.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/README.md +28 -8
  2. package/dist/auth.d.ts +1 -1
  3. package/dist/client.d.ts +4 -3
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/client.js +16 -7
  6. package/dist/client.js.map +1 -1
  7. package/dist/dev-target.js +1 -1
  8. package/dist/index.d.ts +3 -7
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +2 -6
  11. package/dist/index.js.map +1 -1
  12. package/dist/otel/index.d.ts +1 -14
  13. package/dist/otel/index.d.ts.map +1 -1
  14. package/dist/otel/index.js +10 -18
  15. package/dist/otel/index.js.map +1 -1
  16. package/dist/otel/init.d.ts +19 -21
  17. package/dist/otel/init.d.ts.map +1 -1
  18. package/dist/otel/init.js +62 -48
  19. package/dist/otel/init.js.map +1 -1
  20. package/dist/otel/integrations/base.d.ts +17 -37
  21. package/dist/otel/integrations/base.d.ts.map +1 -1
  22. package/dist/otel/integrations/base.js +6 -13
  23. package/dist/otel/integrations/base.js.map +1 -1
  24. package/dist/otel/integrations/index.d.ts +3 -4
  25. package/dist/otel/integrations/index.d.ts.map +1 -1
  26. package/dist/otel/integrations/index.js +6 -25
  27. package/dist/otel/integrations/index.js.map +1 -1
  28. package/dist/otel/integrations/pi.d.ts.map +1 -1
  29. package/dist/otel/integrations/pi.js +9 -1
  30. package/dist/otel/integrations/pi.js.map +1 -1
  31. package/dist/otel/logs.d.ts +41 -8
  32. package/dist/otel/logs.d.ts.map +1 -1
  33. package/dist/otel/logs.js +95 -66
  34. package/dist/otel/logs.js.map +1 -1
  35. package/dist/otel/pi.d.ts +18 -5
  36. package/dist/otel/pi.d.ts.map +1 -1
  37. package/dist/otel/pi.js +38 -9
  38. package/dist/otel/pi.js.map +1 -1
  39. package/dist/otel/register.d.ts +2 -2
  40. package/dist/otel/register.d.ts.map +1 -1
  41. package/dist/otel/register.js +7 -9
  42. package/dist/otel/register.js.map +1 -1
  43. package/dist/otel/setup.d.ts +2 -2
  44. package/dist/otel/setup.d.ts.map +1 -1
  45. package/dist/otel/setup.js +16 -6
  46. package/dist/otel/setup.js.map +1 -1
  47. package/dist/otel/span-processor.d.ts +18 -5
  48. package/dist/otel/span-processor.d.ts.map +1 -1
  49. package/dist/otel/span-processor.js +50 -65
  50. package/dist/otel/span-processor.js.map +1 -1
  51. package/dist/resources/experiments.d.ts +10 -5
  52. package/dist/resources/experiments.d.ts.map +1 -1
  53. package/dist/resources/experiments.js +9 -25
  54. package/dist/resources/experiments.js.map +1 -1
  55. package/dist/resources/recipes.d.ts +6 -6
  56. package/dist/resources/recipes.d.ts.map +1 -1
  57. package/dist/resources/recipes.js +5 -23
  58. package/dist/resources/recipes.js.map +1 -1
  59. package/dist/resources/runtimes.d.ts +2 -2
  60. package/dist/resources/runtimes.d.ts.map +1 -1
  61. package/dist/resources/runtimes.js +2 -2
  62. package/dist/resources/runtimes.js.map +1 -1
  63. package/dist/runner.d.ts +8 -4
  64. package/dist/runner.d.ts.map +1 -1
  65. package/dist/runner.js +26 -18
  66. package/dist/runner.js.map +1 -1
  67. package/dist/types.d.ts +1 -1
  68. package/dist/types.d.ts.map +1 -1
  69. package/dist/types.js +1 -1
  70. package/dist/types.js.map +1 -1
  71. package/dist/utils.d.ts +45 -0
  72. package/dist/utils.d.ts.map +1 -1
  73. package/dist/utils.js +61 -2
  74. package/dist/utils.js.map +1 -1
  75. package/dist/version.d.ts +1 -1
  76. package/dist/version.d.ts.map +1 -1
  77. package/dist/version.js +1 -1
  78. package/dist/version.js.map +1 -1
  79. package/package.json +18 -60
  80. package/.turbo/turbo-build.log +0 -4
  81. package/dist/converters/claude.d.ts +0 -150
  82. package/dist/converters/claude.d.ts.map +0 -1
  83. package/dist/converters/claude.js +0 -211
  84. package/dist/converters/claude.js.map +0 -1
  85. package/dist/converters/gemini.d.ts +0 -90
  86. package/dist/converters/gemini.d.ts.map +0 -1
  87. package/dist/converters/gemini.js +0 -171
  88. package/dist/converters/gemini.js.map +0 -1
  89. package/dist/converters/index.d.ts +0 -9
  90. package/dist/converters/index.d.ts.map +0 -1
  91. package/dist/converters/index.js +0 -9
  92. package/dist/converters/index.js.map +0 -1
  93. package/dist/converters/openai.d.ts +0 -51
  94. package/dist/converters/openai.d.ts.map +0 -1
  95. package/dist/converters/openai.js +0 -269
  96. package/dist/converters/openai.js.map +0 -1
  97. package/dist/converters/openinference.d.ts +0 -107
  98. package/dist/converters/openinference.d.ts.map +0 -1
  99. package/dist/converters/openinference.js +0 -553
  100. package/dist/converters/openinference.js.map +0 -1
  101. package/dist/converters/vercel.d.ts +0 -21
  102. package/dist/converters/vercel.d.ts.map +0 -1
  103. package/dist/converters/vercel.js +0 -236
  104. package/dist/converters/vercel.js.map +0 -1
  105. package/dist/otel/anthropic.d.ts +0 -63
  106. package/dist/otel/anthropic.d.ts.map +0 -1
  107. package/dist/otel/anthropic.js +0 -475
  108. package/dist/otel/anthropic.js.map +0 -1
  109. package/dist/otel/claude-hooks.d.ts +0 -346
  110. package/dist/otel/claude-hooks.d.ts.map +0 -1
  111. package/dist/otel/claude-hooks.js +0 -647
  112. package/dist/otel/claude-hooks.js.map +0 -1
  113. package/dist/otel/claude-wrapper.d.ts +0 -93
  114. package/dist/otel/claude-wrapper.d.ts.map +0 -1
  115. package/dist/otel/claude-wrapper.js +0 -155
  116. package/dist/otel/claude-wrapper.js.map +0 -1
  117. package/dist/otel/gemini.d.ts +0 -56
  118. package/dist/otel/gemini.d.ts.map +0 -1
  119. package/dist/otel/gemini.js +0 -350
  120. package/dist/otel/gemini.js.map +0 -1
  121. package/dist/otel/integrations/anthropic.d.ts +0 -10
  122. package/dist/otel/integrations/anthropic.d.ts.map +0 -1
  123. package/dist/otel/integrations/anthropic.js +0 -26
  124. package/dist/otel/integrations/anthropic.js.map +0 -1
  125. package/dist/otel/integrations/claude-agent.d.ts +0 -12
  126. package/dist/otel/integrations/claude-agent.d.ts.map +0 -1
  127. package/dist/otel/integrations/claude-agent.js +0 -28
  128. package/dist/otel/integrations/claude-agent.js.map +0 -1
  129. package/dist/otel/integrations/gemini.d.ts +0 -11
  130. package/dist/otel/integrations/gemini.d.ts.map +0 -1
  131. package/dist/otel/integrations/gemini.js +0 -23
  132. package/dist/otel/integrations/gemini.js.map +0 -1
  133. package/dist/otel/integrations/langchain.d.ts +0 -15
  134. package/dist/otel/integrations/langchain.d.ts.map +0 -1
  135. package/dist/otel/integrations/langchain.js +0 -43
  136. package/dist/otel/integrations/langchain.js.map +0 -1
  137. package/dist/otel/integrations/mastra.d.ts +0 -14
  138. package/dist/otel/integrations/mastra.d.ts.map +0 -1
  139. package/dist/otel/integrations/mastra.js +0 -31
  140. package/dist/otel/integrations/mastra.js.map +0 -1
  141. package/dist/otel/integrations/openai-agents.d.ts +0 -11
  142. package/dist/otel/integrations/openai-agents.d.ts.map +0 -1
  143. package/dist/otel/integrations/openai-agents.js +0 -30
  144. package/dist/otel/integrations/openai-agents.js.map +0 -1
  145. package/dist/otel/integrations/vercel.d.ts +0 -14
  146. package/dist/otel/integrations/vercel.d.ts.map +0 -1
  147. package/dist/otel/integrations/vercel.js +0 -20
  148. package/dist/otel/integrations/vercel.js.map +0 -1
  149. package/dist/otel/langchain-handler.d.ts +0 -112
  150. package/dist/otel/langchain-handler.d.ts.map +0 -1
  151. package/dist/otel/langchain-handler.js +0 -561
  152. package/dist/otel/langchain-handler.js.map +0 -1
  153. package/dist/otel/mastra-exporter.d.ts +0 -87
  154. package/dist/otel/mastra-exporter.d.ts.map +0 -1
  155. package/dist/otel/mastra-exporter.js +0 -530
  156. package/dist/otel/mastra-exporter.js.map +0 -1
  157. package/dist/otel/openai.d.ts +0 -20
  158. package/dist/otel/openai.d.ts.map +0 -1
  159. package/dist/otel/openai.js +0 -55
  160. package/dist/otel/openai.js.map +0 -1
  161. package/dist/otel/tracing-processor.d.ts +0 -139
  162. package/dist/otel/tracing-processor.d.ts.map +0 -1
  163. package/dist/otel/tracing-processor.js +0 -434
  164. package/dist/otel/tracing-processor.js.map +0 -1
  165. package/eslint.config.mjs +0 -4
  166. package/src/auth.ts +0 -236
  167. package/src/client.ts +0 -163
  168. package/src/converters/claude.ts +0 -343
  169. package/src/converters/gemini.ts +0 -243
  170. package/src/converters/index.ts +0 -50
  171. package/src/converters/openai.ts +0 -300
  172. package/src/converters/openinference.ts +0 -665
  173. package/src/converters/vercel.ts +0 -302
  174. package/src/dev-target.ts +0 -79
  175. package/src/http.ts +0 -43
  176. package/src/index.ts +0 -277
  177. package/src/otel/anthropic.ts +0 -619
  178. package/src/otel/claude-hooks.ts +0 -1116
  179. package/src/otel/claude-wrapper.ts +0 -239
  180. package/src/otel/gemini.ts +0 -483
  181. package/src/otel/index.ts +0 -128
  182. package/src/otel/init.ts +0 -411
  183. package/src/otel/integrations/anthropic.ts +0 -36
  184. package/src/otel/integrations/base.ts +0 -158
  185. package/src/otel/integrations/claude-agent.ts +0 -36
  186. package/src/otel/integrations/gemini.ts +0 -30
  187. package/src/otel/integrations/index.ts +0 -152
  188. package/src/otel/integrations/langchain.ts +0 -60
  189. package/src/otel/integrations/mastra.ts +0 -48
  190. package/src/otel/integrations/openai-agents.ts +0 -47
  191. package/src/otel/integrations/pi.ts +0 -28
  192. package/src/otel/integrations/vercel.ts +0 -26
  193. package/src/otel/langchain-handler.ts +0 -804
  194. package/src/otel/logs.ts +0 -395
  195. package/src/otel/mastra-exporter.ts +0 -714
  196. package/src/otel/openai.ts +0 -93
  197. package/src/otel/pi.ts +0 -94
  198. package/src/otel/register.ts +0 -50
  199. package/src/otel/setup.ts +0 -215
  200. package/src/otel/span-processor.ts +0 -512
  201. package/src/otel/tracing-processor.ts +0 -637
  202. package/src/pagination.ts +0 -10
  203. package/src/proxy-from-env.d.ts +0 -7
  204. package/src/resources/experiments.ts +0 -176
  205. package/src/resources/recipes.ts +0 -74
  206. package/src/resources/runtimes.ts +0 -31
  207. package/src/runner.ts +0 -213
  208. package/src/types.ts +0 -13
  209. package/src/utils.ts +0 -95
  210. package/src/version.ts +0 -2
  211. package/tsconfig.json +0 -8
@@ -1,343 +0,0 @@
1
- /**
2
- * Claude Agent SDK format conversion functions for OTel Gen AI Semantic Conventions.
3
- *
4
- * These functions convert Claude Agent SDK message formats to the standardized
5
- * OTel Gen AI Semantic Convention format for gen_ai.input.messages and gen_ai.output.messages.
6
- */
7
-
8
- import type {
9
- InputMessage,
10
- OutputMessage,
11
- MessagePart,
12
- ReasoningPart,
13
- GenAiAttributes,
14
- ToolDefinition,
15
- } from "@introspection-sdk/types";
16
- import { toAttributes } from "@introspection-sdk/types";
17
-
18
- /** A plain-text content block from the Claude API. */
19
- export interface ClaudeTextBlock {
20
- type: "text";
21
- text: string;
22
- }
23
-
24
- /** A tool-use (function call) content block from the Claude API. */
25
- export interface ClaudeToolUseBlock {
26
- type: "tool_use";
27
- id: string;
28
- name: string;
29
- input: unknown;
30
- }
31
-
32
- /** A tool-result content block returned to the Claude API. */
33
- export interface ClaudeToolResultBlock {
34
- type: "tool_result";
35
- tool_use_id: string;
36
- content: string | unknown[];
37
- is_error?: boolean;
38
- }
39
-
40
- /** Union of all Claude API content block shapes. */
41
- export type ClaudeContentBlock =
42
- | ClaudeTextBlock
43
- | ClaudeToolUseBlock
44
- | ClaudeToolResultBlock
45
- | { type: string; [key: string]: unknown };
46
-
47
- /** A single message in a Claude multi-turn conversation. */
48
- export interface ClaudeMessage {
49
- role: "user" | "assistant";
50
- content: string | ClaudeContentBlock[];
51
- }
52
-
53
- /** Subset of the Claude Messages API response used for conversion. */
54
- export interface ClaudeResponse {
55
- id?: string;
56
- model?: string;
57
- content?: ClaudeContentBlock[];
58
- stop_reason?: string;
59
- usage?: {
60
- input_tokens?: number;
61
- output_tokens?: number;
62
- };
63
- }
64
-
65
- /**
66
- * Convert a user prompt string to OTel Gen AI input message format.
67
- *
68
- * @param prompt - The user's text prompt.
69
- * @returns An array containing a single user-role {@link InputMessage}.
70
- *
71
- * @example
72
- * ```ts
73
- * const msgs = convertClaudePromptToInputMessages("Hello, Claude!");
74
- * // [{ role: "user", parts: [{ type: "text", content: "Hello, Claude!" }] }]
75
- * ```
76
- */
77
- export function convertClaudePromptToInputMessages(
78
- prompt: string,
79
- ): InputMessage[] {
80
- return [
81
- {
82
- role: "user",
83
- parts: [{ type: "text", content: prompt }],
84
- },
85
- ];
86
- }
87
-
88
- /**
89
- * Convert Claude content blocks to message parts.
90
- */
91
- function convertContentBlocksToParts(
92
- content: string | ClaudeContentBlock[],
93
- ): MessagePart[] {
94
- if (typeof content === "string") {
95
- return [{ type: "text", content }];
96
- }
97
-
98
- const parts: MessagePart[] = [];
99
-
100
- for (const block of content) {
101
- if (block.type === "text" && "text" in block) {
102
- parts.push({ type: "text", content: block.text as string });
103
- } else if (block.type === "tool_use" && "name" in block) {
104
- parts.push({
105
- type: "tool_call",
106
- id: block.id as string,
107
- name: block.name as string,
108
- arguments: block.input,
109
- });
110
- } else if (block.type === "tool_result" && "tool_use_id" in block) {
111
- let response: unknown = block.content;
112
- if (typeof response === "string") {
113
- // Keep as string
114
- } else if (Array.isArray(response)) {
115
- // Extract text from content array
116
- response = response
117
- .filter(
118
- (item): item is { type: "text"; text: string } =>
119
- typeof item === "object" &&
120
- item !== null &&
121
- (item as Record<string, unknown>).type === "text",
122
- )
123
- .map((item) => item.text)
124
- .join("");
125
- }
126
- parts.push({
127
- type: "tool_call_response",
128
- id: block.tool_use_id as string,
129
- response,
130
- });
131
- } else if (block.type === "thinking" && "thinking" in block) {
132
- const thinking = block.thinking as string | undefined;
133
- const signature = (block as Record<string, unknown>).signature as
134
- string | undefined;
135
- const thinkingPart: ReasoningPart = {
136
- type: "reasoning",
137
- content: thinking || "[redacted]",
138
- signature: signature || undefined,
139
- provider_name: "anthropic",
140
- };
141
- parts.push(thinkingPart);
142
- }
143
- }
144
-
145
- return parts;
146
- }
147
-
148
- /**
149
- * Convert Claude assistant response content to OTel Gen AI output message format.
150
- *
151
- * @param content - A plain string or array of {@link ClaudeContentBlock} objects.
152
- * @param stopReason - Optional stop/finish reason (e.g. `"end_turn"`).
153
- * @returns An array containing a single assistant-role {@link OutputMessage}.
154
- *
155
- * @example
156
- * ```ts
157
- * const msgs = convertClaudeResponseToOutputMessages(response.content, response.stop_reason);
158
- * span.setAttribute("gen_ai.output.messages", JSON.stringify(msgs));
159
- * ```
160
- */
161
- export function convertClaudeResponseToOutputMessages(
162
- content: string | ClaudeContentBlock[],
163
- stopReason?: string,
164
- ): OutputMessage[] {
165
- const parts = convertContentBlocksToParts(content);
166
-
167
- const message: OutputMessage = {
168
- role: "assistant",
169
- parts,
170
- };
171
-
172
- if (stopReason) {
173
- message.finish_reason = stopReason;
174
- }
175
-
176
- return [message];
177
- }
178
-
179
- /**
180
- * Convert a Claude multi-turn conversation to OTel Gen AI input message format.
181
- *
182
- * @param messages - Array of {@link ClaudeMessage} objects (user + assistant turns).
183
- * @returns An array of {@link InputMessage} objects preserving turn order.
184
- *
185
- * @example
186
- * ```ts
187
- * const msgs = convertClaudeMessagesToInputMessages(conversation);
188
- * span.setAttribute("gen_ai.input.messages", JSON.stringify(msgs));
189
- * ```
190
- */
191
- export function convertClaudeMessagesToInputMessages(
192
- messages: ClaudeMessage[],
193
- ): InputMessage[] {
194
- return messages.map((msg) => ({
195
- role: msg.role as InputMessage["role"],
196
- parts: convertContentBlocksToParts(msg.content),
197
- }));
198
- }
199
-
200
- /**
201
- * Aggregated session data from the Claude Agent SDK, used as input for
202
- * the {@link convertClaudeSessionToGenAI} and
203
- * {@link convertClaudeSessionToOtelAttributes} converters.
204
- */
205
- export interface ClaudeSessionData {
206
- /** Input prompt */
207
- inputPrompt?: string;
208
- /** Response content (text extracted from assistant messages) */
209
- responseContent?: string;
210
- /** Full response content blocks (if available) */
211
- responseBlocks?: ClaudeContentBlock[];
212
- /** Model name */
213
- model?: string;
214
- /** Session ID */
215
- sessionId?: string;
216
- /** Input token count */
217
- inputTokens?: number;
218
- /** Output token count */
219
- outputTokens?: number;
220
- /** Cost in USD */
221
- costUsd?: number;
222
- /** Stop/finish reason */
223
- stopReason?: string;
224
- /** Response ID */
225
- responseId?: string;
226
- /** Tool names from init message */
227
- toolNames?: string[];
228
- /** System instructions / system prompt */
229
- systemInstructions?: string;
230
- /** Tokens read from the LLM prompt cache */
231
- cacheReadInputTokens?: number;
232
- /** Tokens written to the LLM prompt cache */
233
- cacheCreationInputTokens?: number;
234
- }
235
-
236
- /**
237
- * Convert Claude session data to camelCase {@link GenAiAttributes}.
238
- *
239
- * @param data - Aggregated {@link ClaudeSessionData} for one session.
240
- * @returns A {@link GenAiAttributes} object ready for {@link toAttributes}.
241
- *
242
- * @example
243
- * ```ts
244
- * const genAi = convertClaudeSessionToGenAI({ model: "claude-sonnet-4-20250514", inputTokens: 100 });
245
- * const otel = toAttributes(genAi);
246
- * ```
247
- */
248
- export function convertClaudeSessionToGenAI(
249
- data: ClaudeSessionData,
250
- ): GenAiAttributes {
251
- const result: GenAiAttributes = {};
252
-
253
- result.providerName = "anthropic";
254
-
255
- // Set model
256
- if (data.model) {
257
- result.requestModel = data.model;
258
- }
259
-
260
- // Convert input to gen_ai.input.messages format
261
- if (data.inputPrompt) {
262
- result.inputMessages = convertClaudePromptToInputMessages(data.inputPrompt);
263
- }
264
-
265
- // Convert output to gen_ai.output.messages format
266
- if (data.responseBlocks) {
267
- result.outputMessages = convertClaudeResponseToOutputMessages(
268
- data.responseBlocks,
269
- data.stopReason,
270
- );
271
- } else if (data.responseContent) {
272
- result.outputMessages = convertClaudeResponseToOutputMessages(
273
- data.responseContent,
274
- data.stopReason,
275
- );
276
- }
277
-
278
- // Set token counts
279
- if (data.inputTokens !== undefined) {
280
- result.inputTokens = data.inputTokens;
281
- }
282
- if (data.outputTokens !== undefined) {
283
- result.outputTokens = data.outputTokens;
284
- }
285
- if (data.cacheReadInputTokens !== undefined) {
286
- result.cacheReadInputTokens = data.cacheReadInputTokens;
287
- }
288
- if (data.cacheCreationInputTokens !== undefined) {
289
- result.cacheCreationInputTokens = data.cacheCreationInputTokens;
290
- }
291
-
292
- // Set response ID
293
- if (data.responseId) {
294
- result.responseId = data.responseId;
295
- }
296
-
297
- // Set tool definitions from tool names
298
- if (data.toolNames && data.toolNames.length > 0) {
299
- result.toolDefinitions = data.toolNames.map((name): ToolDefinition => ({
300
- type: "function",
301
- name,
302
- }));
303
- }
304
-
305
- // Set system instructions
306
- if (data.systemInstructions) {
307
- result.systemInstructions = [
308
- { type: "text" as const, content: data.systemInstructions },
309
- ];
310
- }
311
-
312
- return result;
313
- }
314
-
315
- /**
316
- * Convert Claude session data directly to OTel span attributes with `gen_ai.*`
317
- * dot-notation keys, plus Claude-specific keys like `claude.session_id`.
318
- *
319
- * @param data - Aggregated {@link ClaudeSessionData} for one session.
320
- * @returns A flat dictionary suitable for `span.setAttributes()`.
321
- *
322
- * @example
323
- * ```ts
324
- * const attrs = convertClaudeSessionToOtelAttributes(sessionData);
325
- * for (const [k, v] of Object.entries(attrs)) span.setAttribute(k, v);
326
- * ```
327
- */
328
- export function convertClaudeSessionToOtelAttributes(
329
- data: ClaudeSessionData,
330
- ): Record<string, string | number | string[] | number[]> {
331
- const genaiAttrs = convertClaudeSessionToGenAI(data);
332
- const result = toAttributes(genaiAttrs);
333
-
334
- // Add Claude-specific attributes
335
- if (data.sessionId) {
336
- result["claude.session_id"] = data.sessionId;
337
- }
338
- if (data.costUsd !== undefined) {
339
- result["gen_ai.cost.usd"] = data.costUsd;
340
- }
341
-
342
- return result;
343
- }
@@ -1,243 +0,0 @@
1
- /**
2
- * Google Gemini (`@google/genai`) format conversion functions for OTel Gen AI
3
- * Semantic Conventions.
4
- *
5
- * Gemini 3.x / Gemini 3.5+ models emit per-part `thoughtSignature` payloads
6
- * that carry encrypted reasoning context which MUST be passed back on
7
- * subsequent turns (especially around tool calls) for the model to maintain
8
- * its chain of thought.
9
- *
10
- * @see https://ai.google.dev/gemini-api/docs/thought-signatures
11
- *
12
- * Whenever a Gemini part carries a `thoughtSignature`, this converter emits a
13
- * `reasoning` gen_ai part preceding the visible content. If the part is itself a
14
- * thought summary (`thought: true`), the visible thought text is used as the
15
- * thinking content; otherwise the signed-but-redacted sentinel
16
- * `REDACTED_THINKING_CONTENT` (`"[redacted]"`) is used — mirroring the way the
17
- * Anthropic instrumentor handles `redacted_thinking` blocks.
18
- */
19
-
20
- import type {
21
- InputMessage,
22
- OutputMessage,
23
- MessagePart,
24
- ReasoningPart,
25
- SystemInstruction,
26
- ToolDefinition,
27
- } from "@introspection-sdk/types";
28
-
29
- /** Sentinel for parts that carry a thought signature but no visible thought content. */
30
- const REDACTED_THINKING_CONTENT = "[redacted]";
31
-
32
- /** Provider name reported on Gemini-produced thinking parts. */
33
- export const GEMINI_PROVIDER_NAME = "gemini";
34
-
35
- /** A single content part as returned by the `@google/genai` SDK. */
36
- export interface GeminiPart {
37
- text?: string;
38
- thought?: boolean;
39
- thoughtSignature?: string;
40
- functionCall?: {
41
- id?: string;
42
- name?: string;
43
- args?: unknown;
44
- };
45
- functionResponse?: {
46
- id?: string;
47
- name?: string;
48
- response?: unknown;
49
- };
50
- [key: string]: unknown;
51
- }
52
-
53
- /** A Content envelope as used in Gemini `contents` requests and candidate outputs. */
54
- export interface GeminiContent {
55
- role?: string;
56
- parts?: GeminiPart[];
57
- }
58
-
59
- /** A single candidate from a `generateContent` response. */
60
- export interface GeminiCandidate {
61
- content?: GeminiContent;
62
- finishReason?: string;
63
- index?: number;
64
- }
65
-
66
- /** A Gemini tool function declaration. */
67
- export interface GeminiFunctionDeclaration {
68
- name?: string;
69
- description?: string;
70
- parameters?: unknown;
71
- parametersJsonSchema?: unknown;
72
- }
73
-
74
- /** A Gemini tool entry — `{ functionDeclarations: [...] }`. */
75
- export interface GeminiTool {
76
- functionDeclarations?: GeminiFunctionDeclaration[];
77
- }
78
-
79
- /** Normalize roles from Gemini to gen_ai. Gemini uses `model` for assistant turns. */
80
- function normalizeRole(role: string | undefined): InputMessage["role"] {
81
- if (role === "model") return "assistant";
82
- if (role === "user" || role === "system" || role === "tool") return role;
83
- return "user";
84
- }
85
-
86
- /**
87
- * Convert a single Gemini part into zero-or-more gen_ai message parts.
88
- *
89
- * A part carrying a `thoughtSignature` always produces a leading `reasoning`
90
- * part so the signed reasoning payload is preserved alongside whatever visible
91
- * content the part contains.
92
- */
93
- function partToGenAiParts(part: GeminiPart): MessagePart[] {
94
- const out: MessagePart[] = [];
95
- const signature = part.thoughtSignature || undefined;
96
- const isThought = part.thought === true;
97
-
98
- if (isThought) {
99
- // Visible thought summary (Gemini emits these when `includeThoughts: true`).
100
- const reasoning: ReasoningPart = {
101
- type: "reasoning",
102
- content: part.text || REDACTED_THINKING_CONTENT,
103
- signature,
104
- provider_name: GEMINI_PROVIDER_NAME,
105
- };
106
- out.push(reasoning);
107
- return out;
108
- }
109
-
110
- if (signature) {
111
- // Signed but non-thought part — emit a redacted reasoning prefix carrying the signature.
112
- const reasoning: ReasoningPart = {
113
- type: "reasoning",
114
- content: REDACTED_THINKING_CONTENT,
115
- signature,
116
- provider_name: GEMINI_PROVIDER_NAME,
117
- };
118
- out.push(reasoning);
119
- }
120
-
121
- if (typeof part.text === "string" && part.text.length > 0) {
122
- out.push({ type: "text", content: part.text });
123
- }
124
-
125
- if (part.functionCall) {
126
- out.push({
127
- type: "tool_call",
128
- id: part.functionCall.id,
129
- name: part.functionCall.name || "",
130
- arguments: part.functionCall.args,
131
- });
132
- }
133
-
134
- if (part.functionResponse) {
135
- out.push({
136
- type: "tool_call_response",
137
- id: part.functionResponse.id,
138
- name: part.functionResponse.name,
139
- response: part.functionResponse.response,
140
- });
141
- }
142
-
143
- return out;
144
- }
145
-
146
- /**
147
- * Convert a `contents` array from a Gemini request to gen_ai input messages.
148
- *
149
- * Accepts a plain string (the convenience form `generateContent({ contents: "hi" })`)
150
- * or an array of {@link GeminiContent} objects. Roles are normalized: Gemini's
151
- * `model` becomes `assistant`.
152
- *
153
- * @param contents - The Gemini request's `contents` field.
154
- * @returns Array of {@link InputMessage} objects.
155
- */
156
- export function convertGeminiContentsToInputMessages(
157
- contents: string | GeminiContent[] | GeminiContent | undefined,
158
- ): InputMessage[] {
159
- if (!contents) return [];
160
-
161
- if (typeof contents === "string") {
162
- return [{ role: "user", parts: [{ type: "text", content: contents }] }];
163
- }
164
-
165
- const list: GeminiContent[] = Array.isArray(contents) ? contents : [contents];
166
-
167
- const result: InputMessage[] = [];
168
- for (const c of list) {
169
- const parts = (c.parts || []).flatMap(partToGenAiParts);
170
- if (parts.length === 0) continue;
171
- result.push({ role: normalizeRole(c.role), parts });
172
- }
173
- return result;
174
- }
175
-
176
- /**
177
- * Convert Gemini response candidates to gen_ai output messages.
178
- *
179
- * @param candidates - The `candidates` array from a `generateContent` response.
180
- * @returns Array of {@link OutputMessage} objects (one per candidate that has content).
181
- */
182
- export function convertGeminiCandidatesToOutputMessages(
183
- candidates: GeminiCandidate[] | undefined,
184
- ): OutputMessage[] {
185
- if (!candidates || candidates.length === 0) return [];
186
-
187
- const result: OutputMessage[] = [];
188
- for (const cand of candidates) {
189
- const role = normalizeRole(cand.content?.role || "model");
190
- const parts = (cand.content?.parts || []).flatMap(partToGenAiParts);
191
- if (parts.length === 0 && !cand.finishReason) continue;
192
- const msg: OutputMessage = { role, parts };
193
- if (cand.finishReason) msg.finish_reason = cand.finishReason;
194
- result.push(msg);
195
- }
196
- return result;
197
- }
198
-
199
- /**
200
- * Convert a Gemini `systemInstruction` field to gen_ai SystemInstruction[].
201
- *
202
- * Gemini accepts either a plain string or a {@link GeminiContent} object.
203
- */
204
- export function convertGeminiSystemInstructionToSemconv(
205
- systemInstruction: string | GeminiContent | undefined,
206
- ): SystemInstruction[] | undefined {
207
- if (!systemInstruction) return undefined;
208
-
209
- if (typeof systemInstruction === "string") {
210
- return [{ type: "text", content: systemInstruction }];
211
- }
212
-
213
- const result = (systemInstruction.parts || []).flatMap(
214
- (p): SystemInstruction[] =>
215
- typeof p.text === "string" && p.text.length > 0
216
- ? [{ type: "text", content: p.text }]
217
- : [],
218
- );
219
- return result.length > 0 ? result : undefined;
220
- }
221
-
222
- /**
223
- * Convert Gemini `tools` (array of `{functionDeclarations}` entries) to gen_ai
224
- * ToolDefinition[].
225
- */
226
- export function convertGeminiToolsToToolDefinitions(
227
- tools: GeminiTool[] | undefined,
228
- ): ToolDefinition[] | undefined {
229
- if (!tools || tools.length === 0) return undefined;
230
-
231
- const defs: ToolDefinition[] = [];
232
- for (const tool of tools) {
233
- for (const fn of tool.functionDeclarations || []) {
234
- if (!fn.name) continue;
235
- const def: ToolDefinition = { type: "function", name: fn.name };
236
- if (fn.description) def.description = fn.description;
237
- const params = fn.parametersJsonSchema ?? fn.parameters;
238
- if (params) def.parameters = params;
239
- defs.push(def);
240
- }
241
- }
242
- return defs.length > 0 ? defs : undefined;
243
- }
@@ -1,50 +0,0 @@
1
- /**
2
- * Converters for transforming various API formats to OTel Gen AI Semantic Conventions.
3
- */
4
-
5
- export {
6
- convertResponsesInputsToSemconv,
7
- convertResponsesOutputsToSemconv,
8
- convertResponsesToolsToSemconv,
9
- convertResponsesInstructionsToSemconv,
10
- type ResponseInputItem,
11
- type ResponseOutputItem,
12
- type ResponseTool,
13
- type ResponseUsage,
14
- type Response as OpenAIResponse,
15
- } from "./openai.js";
16
-
17
- export {
18
- isOpenInferenceSpan,
19
- convertOpenInferenceToGenAI,
20
- replaceOpenInferenceWithGenAI,
21
- addOpenInferenceAttributes,
22
- OpenInferenceSpanExporter,
23
- } from "./openinference.js";
24
-
25
- export { isVercelAISpan, convertVercelAIToGenAI } from "./vercel.js";
26
-
27
- export {
28
- convertClaudePromptToInputMessages,
29
- convertClaudeResponseToOutputMessages,
30
- convertClaudeMessagesToInputMessages,
31
- convertClaudeSessionToGenAI,
32
- convertClaudeSessionToOtelAttributes,
33
- type ClaudeContentBlock,
34
- type ClaudeMessage,
35
- type ClaudeResponse,
36
- type ClaudeSessionData,
37
- } from "./claude.js";
38
-
39
- export {
40
- convertGeminiContentsToInputMessages,
41
- convertGeminiCandidatesToOutputMessages,
42
- convertGeminiSystemInstructionToSemconv,
43
- convertGeminiToolsToToolDefinitions,
44
- GEMINI_PROVIDER_NAME,
45
- type GeminiCandidate,
46
- type GeminiContent,
47
- type GeminiFunctionDeclaration,
48
- type GeminiPart,
49
- type GeminiTool,
50
- } from "./gemini.js";