@introspection-sdk/introspection-node 0.4.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.
- package/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-check-types.log +5 -0
- package/.turbo/turbo-lint.log +5 -0
- package/dist/ai-sdk-integration.d.ts +123 -0
- package/dist/ai-sdk-integration.d.ts.map +1 -0
- package/dist/ai-sdk-integration.js +475 -0
- package/dist/anthropic.d.ts +54 -0
- package/dist/anthropic.d.ts.map +1 -0
- package/dist/anthropic.js +375 -0
- package/dist/claude-hooks.d.ts +328 -0
- package/dist/claude-hooks.d.ts.map +1 -0
- package/dist/claude-hooks.js +571 -0
- package/dist/claude-wrapper.d.ts +93 -0
- package/dist/claude-wrapper.d.ts.map +1 -0
- package/dist/claude-wrapper.js +154 -0
- package/dist/client.d.ts +157 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +394 -0
- package/dist/converters/ai-sdk.d.ts +65 -0
- package/dist/converters/ai-sdk.d.ts.map +1 -0
- package/dist/converters/ai-sdk.js +238 -0
- package/dist/converters/claude.d.ts +146 -0
- package/dist/converters/claude.d.ts.map +1 -0
- package/dist/converters/claude.js +202 -0
- package/dist/converters/index.d.ts +9 -0
- package/dist/converters/index.d.ts.map +1 -0
- package/dist/converters/index.js +8 -0
- package/dist/converters/openai.d.ts +51 -0
- package/dist/converters/openai.d.ts.map +1 -0
- package/dist/converters/openai.js +268 -0
- package/dist/converters/openclaw.d.ts +16 -0
- package/dist/converters/openclaw.d.ts.map +1 -0
- package/dist/converters/openclaw.js +49 -0
- package/dist/converters/openinference.d.ts +107 -0
- package/dist/converters/openinference.d.ts.map +1 -0
- package/dist/converters/openinference.js +552 -0
- package/dist/converters/vercel.d.ts +21 -0
- package/dist/converters/vercel.d.ts.map +1 -0
- package/dist/converters/vercel.js +208 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/langchain-handler.d.ts +101 -0
- package/dist/langchain-handler.d.ts.map +1 -0
- package/dist/langchain-handler.js +489 -0
- package/dist/mastra-exporter.d.ts +85 -0
- package/dist/mastra-exporter.d.ts.map +1 -0
- package/dist/mastra-exporter.js +471 -0
- package/dist/span-processor.d.ts +62 -0
- package/dist/span-processor.d.ts.map +1 -0
- package/dist/span-processor.js +310 -0
- package/dist/tracing-processor.d.ts +136 -0
- package/dist/tracing-processor.d.ts.map +1 -0
- package/dist/tracing-processor.js +368 -0
- package/dist/types/genai.d.ts +165 -0
- package/dist/types/genai.d.ts.map +1 -0
- package/dist/types/genai.js +82 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +19 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +57 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +2 -0
- package/eslint.config.mjs +4 -0
- package/package.json +78 -0
- package/src/ai-sdk-integration.ts +689 -0
- package/src/anthropic.ts +465 -0
- package/src/claude-hooks.ts +1010 -0
- package/src/claude-wrapper.ts +240 -0
- package/src/client.ts +513 -0
- package/src/converters/ai-sdk.ts +298 -0
- package/src/converters/claude.ts +334 -0
- package/src/converters/index.ts +44 -0
- package/src/converters/openai.ts +302 -0
- package/src/converters/openinference.ts +673 -0
- package/src/converters/vercel.ts +264 -0
- package/src/index.ts +121 -0
- package/src/langchain-handler.ts +722 -0
- package/src/mastra-exporter.ts +650 -0
- package/src/span-processor.ts +418 -0
- package/src/tracing-processor.ts +548 -0
- package/src/types/genai.ts +264 -0
- package/src/types.ts +13 -0
- package/src/utils.ts +69 -0
- package/src/version.ts +2 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Agents TracingProcessor for Introspection SDK.
|
|
3
|
+
*
|
|
4
|
+
* Forwards OpenAI agent traces to the backend via OTLP with OTel Gen AI semantic
|
|
5
|
+
* convention attributes.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
context as otelContext,
|
|
10
|
+
trace as otelTrace,
|
|
11
|
+
Span as OtelSpan,
|
|
12
|
+
} from "@opentelemetry/api";
|
|
13
|
+
import {
|
|
14
|
+
BasicTracerProvider,
|
|
15
|
+
BatchSpanProcessor,
|
|
16
|
+
SimpleSpanProcessor,
|
|
17
|
+
type SpanExporter,
|
|
18
|
+
type IdGenerator,
|
|
19
|
+
} from "@opentelemetry/sdk-trace-base";
|
|
20
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
21
|
+
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
|
|
22
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
|
|
23
|
+
import { randomUUID } from "crypto";
|
|
24
|
+
|
|
25
|
+
import {
|
|
26
|
+
convertResponsesInputsToSemconv,
|
|
27
|
+
convertResponsesOutputsToSemconv,
|
|
28
|
+
convertResponsesToolsToSemconv,
|
|
29
|
+
convertResponsesInstructionsToSemconv,
|
|
30
|
+
} from "./converters/openai.js";
|
|
31
|
+
import type {
|
|
32
|
+
Response as OpenAIResponse,
|
|
33
|
+
ResponseInputItem,
|
|
34
|
+
} from "./converters/openai.js";
|
|
35
|
+
import { logger } from "./utils.js";
|
|
36
|
+
import { VERSION } from "./version.js";
|
|
37
|
+
|
|
38
|
+
// Type imports from @openai/agents
|
|
39
|
+
import type {
|
|
40
|
+
TracingProcessor,
|
|
41
|
+
Trace,
|
|
42
|
+
Span,
|
|
43
|
+
SpanData,
|
|
44
|
+
AgentSpanData,
|
|
45
|
+
FunctionSpanData,
|
|
46
|
+
GenerationSpanData,
|
|
47
|
+
ResponseSpanData,
|
|
48
|
+
HandoffSpanData,
|
|
49
|
+
} from "@openai/agents";
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Advanced options for testing and customization of
|
|
53
|
+
* {@link IntrospectionTracingProcessor}.
|
|
54
|
+
*/
|
|
55
|
+
export interface TracingProcessorAdvancedOptions {
|
|
56
|
+
/** Custom span exporter (for testing - use InMemorySpanExporter) */
|
|
57
|
+
spanExporter?: SpanExporter;
|
|
58
|
+
/** Custom ID generator (for testing - use IncrementalIdGenerator) */
|
|
59
|
+
idGenerator?: IdGenerator;
|
|
60
|
+
/** Use SimpleSpanProcessor instead of BatchSpanProcessor (for testing) */
|
|
61
|
+
useSimpleSpanProcessor?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Maximum number of spans to export in a single batch.
|
|
64
|
+
* Set to 1 to export each span individually on end, ensuring sequential
|
|
65
|
+
* processing by the backend (useful for multi-turn conversations where
|
|
66
|
+
* each turn must be ingested before the next arrives).
|
|
67
|
+
* Defaults to the OTel SDK default (512).
|
|
68
|
+
*/
|
|
69
|
+
maxExportBatchSize?: number;
|
|
70
|
+
/**
|
|
71
|
+
* Delay interval in milliseconds between batch exports.
|
|
72
|
+
* Lower values reduce latency but increase network requests.
|
|
73
|
+
* Defaults to 1000.
|
|
74
|
+
*/
|
|
75
|
+
scheduledDelayMillis?: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Configuration for {@link IntrospectionTracingProcessor}. */
|
|
79
|
+
export interface IntrospectionTracingProcessorOptions {
|
|
80
|
+
/** Authentication token (env: INTROSPECTION_TOKEN) */
|
|
81
|
+
token?: string;
|
|
82
|
+
/** Base URL for the API (env: INTROSPECTION_BASE_URL, default: "https://otel.introspection.dev") */
|
|
83
|
+
baseUrl?: string;
|
|
84
|
+
/** Service name for telemetry (env: INTROSPECTION_SERVICE_NAME) */
|
|
85
|
+
serviceName?: string;
|
|
86
|
+
/** Additional headers to include in requests */
|
|
87
|
+
additionalHeaders?: Record<string, string>;
|
|
88
|
+
/** Advanced options for testing and customization */
|
|
89
|
+
advanced?: TracingProcessorAdvancedOptions;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* {@link TracingProcessor} that forwards OpenAI agent traces to the
|
|
94
|
+
* Introspection backend via OTLP.
|
|
95
|
+
*
|
|
96
|
+
* Extracts OTel Gen AI semantic convention attributes from span data:
|
|
97
|
+
* - Agent spans: `gen_ai.agent.name`, `gen_ai.tool.definitions`, `gen_ai.agent.handoffs`
|
|
98
|
+
* - Function spans: `gen_ai.tool.name`, `gen_ai.tool.input`, `gen_ai.tool.output`
|
|
99
|
+
* - Response spans: `gen_ai.input/output.messages`, `gen_ai.usage.*`, `gen_ai.request.model`
|
|
100
|
+
* - Generation spans: `gen_ai.request.model`, `gen_ai.usage.*`
|
|
101
|
+
* - Handoff spans: `gen_ai.handoff.from_agent`, `gen_ai.handoff.to_agent`
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* import { IntrospectionTracingProcessor } from "@introspection-sdk/introspection-node";
|
|
106
|
+
*
|
|
107
|
+
* const processor = new IntrospectionTracingProcessor({ token: "sk-intro-…" });
|
|
108
|
+
* // pass processor to the OpenAI Agents SDK withTrace() or registerProcessor()
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export class IntrospectionTracingProcessor implements TracingProcessor {
|
|
112
|
+
private _tracerProvider: BasicTracerProvider;
|
|
113
|
+
private _tracer: ReturnType<BasicTracerProvider["getTracer"]>;
|
|
114
|
+
private _spans: Map<string, OtelSpan> = new Map();
|
|
115
|
+
private _conversationIds: Map<string, string> = new Map();
|
|
116
|
+
|
|
117
|
+
constructor(options: IntrospectionTracingProcessorOptions = {}) {
|
|
118
|
+
const advanced = options.advanced;
|
|
119
|
+
const serviceName =
|
|
120
|
+
options.serviceName || process.env.INTROSPECTION_SERVICE_NAME;
|
|
121
|
+
const resource = serviceName
|
|
122
|
+
? resourceFromAttributes({ [ATTR_SERVICE_NAME]: serviceName })
|
|
123
|
+
: undefined;
|
|
124
|
+
|
|
125
|
+
// If custom exporter is provided (testing mode), skip token validation
|
|
126
|
+
if (!advanced?.spanExporter) {
|
|
127
|
+
const token = options.token || process.env.INTROSPECTION_TOKEN;
|
|
128
|
+
if (!token) {
|
|
129
|
+
throw new Error("INTROSPECTION_TOKEN is required");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const baseUrl =
|
|
133
|
+
options.baseUrl ||
|
|
134
|
+
process.env.INTROSPECTION_BASE_URL ||
|
|
135
|
+
"https://otel.introspection.dev";
|
|
136
|
+
|
|
137
|
+
const headers: Record<string, string> = {
|
|
138
|
+
"User-Agent": `introspection-sdk/${VERSION}`,
|
|
139
|
+
Authorization: `Bearer ${token}`,
|
|
140
|
+
...options.additionalHeaders,
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// Construct endpoint URL
|
|
144
|
+
const endpoint = baseUrl.endsWith("/v1/traces")
|
|
145
|
+
? baseUrl
|
|
146
|
+
: `${baseUrl.replace(/\/$/, "")}/v1/traces`;
|
|
147
|
+
|
|
148
|
+
logger.info(
|
|
149
|
+
`IntrospectionTracingProcessor initialized: endpoint=${endpoint}`,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
// Setup OTLP exporter
|
|
153
|
+
const spanExporter = new OTLPTraceExporter({
|
|
154
|
+
url: endpoint,
|
|
155
|
+
headers,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Use SimpleSpanProcessor for maxExportBatchSize=1 (sequential export),
|
|
159
|
+
// otherwise BatchSpanProcessor with configurable batch size.
|
|
160
|
+
// Default to sequential export for dev/staging tokens.
|
|
161
|
+
const effectiveBatchSize =
|
|
162
|
+
advanced?.maxExportBatchSize ??
|
|
163
|
+
(token.startsWith("intro_dev") || token.startsWith("intro_staging")
|
|
164
|
+
? 1
|
|
165
|
+
: undefined);
|
|
166
|
+
const useSimple =
|
|
167
|
+
advanced?.useSimpleSpanProcessor || effectiveBatchSize === 1;
|
|
168
|
+
const scheduledDelayMillis = advanced?.scheduledDelayMillis ?? 1000;
|
|
169
|
+
const spanProcessor = useSimple
|
|
170
|
+
? new SimpleSpanProcessor(spanExporter)
|
|
171
|
+
: new BatchSpanProcessor(spanExporter, {
|
|
172
|
+
scheduledDelayMillis,
|
|
173
|
+
...(effectiveBatchSize
|
|
174
|
+
? { maxExportBatchSize: effectiveBatchSize }
|
|
175
|
+
: {}),
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
this._tracerProvider = new BasicTracerProvider({
|
|
179
|
+
resource,
|
|
180
|
+
idGenerator: advanced?.idGenerator,
|
|
181
|
+
spanProcessors: [spanProcessor],
|
|
182
|
+
});
|
|
183
|
+
} else {
|
|
184
|
+
// Testing mode: use provided exporter
|
|
185
|
+
logger.info(
|
|
186
|
+
`IntrospectionTracingProcessor initialized in test mode with custom exporter`,
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
// Choose span processor based on options
|
|
190
|
+
const spanProcessor = advanced.useSimpleSpanProcessor
|
|
191
|
+
? new SimpleSpanProcessor(advanced.spanExporter)
|
|
192
|
+
: new BatchSpanProcessor(advanced.spanExporter, {
|
|
193
|
+
scheduledDelayMillis: 100,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
this._tracerProvider = new BasicTracerProvider({
|
|
197
|
+
resource,
|
|
198
|
+
idGenerator: advanced.idGenerator,
|
|
199
|
+
spanProcessors: [spanProcessor],
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
this._tracer = this._tracerProvider.getTracer("openai-agents", VERSION);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Called when the processor is started. Optional lifecycle hook.
|
|
208
|
+
*/
|
|
209
|
+
start(): void {
|
|
210
|
+
// No-op - processor is ready after construction
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Called when a trace starts. Creates a root OTel span.
|
|
215
|
+
*
|
|
216
|
+
* @param trace - The OpenAI Agents SDK trace object.
|
|
217
|
+
*/
|
|
218
|
+
async onTraceStart(trace: Trace): Promise<void> {
|
|
219
|
+
logger.debug(`onTraceStart called: ${trace?.name} (${trace?.traceId})`);
|
|
220
|
+
if (trace) {
|
|
221
|
+
const conversationId = `intro_conv_${randomUUID().replace(/-/g, "")}`;
|
|
222
|
+
this._conversationIds.set(trace.traceId, conversationId);
|
|
223
|
+
const otelSpan = this._tracer.startSpan(trace.name);
|
|
224
|
+
this._spans.set(trace.traceId, otelSpan);
|
|
225
|
+
logger.debug(`Created root span for trace: ${trace.traceId}`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Called when a trace ends. Closes the root OTel span.
|
|
231
|
+
*
|
|
232
|
+
* @param trace - The OpenAI Agents SDK trace object.
|
|
233
|
+
*/
|
|
234
|
+
async onTraceEnd(trace: Trace): Promise<void> {
|
|
235
|
+
logger.debug(`onTraceEnd called: ${trace?.name} (${trace?.traceId})`);
|
|
236
|
+
if (trace) {
|
|
237
|
+
const otelSpan = this._spans.get(trace.traceId);
|
|
238
|
+
if (otelSpan) {
|
|
239
|
+
this._spans.delete(trace.traceId);
|
|
240
|
+
this._conversationIds.delete(trace.traceId);
|
|
241
|
+
otelSpan.end();
|
|
242
|
+
logger.debug(`Ended root span for trace: ${trace.traceId}`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Called when a span starts. Creates a child OTel span with parent context.
|
|
249
|
+
*
|
|
250
|
+
* @param span - The OpenAI Agents SDK span.
|
|
251
|
+
*/
|
|
252
|
+
async onSpanStart(span: Span<SpanData>): Promise<void> {
|
|
253
|
+
logger.debug(
|
|
254
|
+
`onSpanStart called: ${span?.spanId} (type: ${(span?.spanData as SpanData)?.type})`,
|
|
255
|
+
);
|
|
256
|
+
if (span) {
|
|
257
|
+
const parentId = span.parentId || span.traceId;
|
|
258
|
+
const parent = this._spans.get(parentId);
|
|
259
|
+
const ctx = parent
|
|
260
|
+
? otelTrace.setSpan(otelContext.active(), parent)
|
|
261
|
+
: undefined;
|
|
262
|
+
|
|
263
|
+
const spanData = span.spanData as SpanData;
|
|
264
|
+
let name: string;
|
|
265
|
+
|
|
266
|
+
// Use name for agent and function spans, type for others
|
|
267
|
+
if (spanData.type === "agent") {
|
|
268
|
+
name = (spanData as AgentSpanData).name;
|
|
269
|
+
} else if (spanData.type === "function") {
|
|
270
|
+
name = (spanData as FunctionSpanData).name;
|
|
271
|
+
} else {
|
|
272
|
+
name = spanData.type;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const otelSpan = this._tracer.startSpan(name, {}, ctx);
|
|
276
|
+
this._spans.set(span.spanId, otelSpan);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Called when a span ends. Extracts `gen_ai.*` attributes from the span
|
|
282
|
+
* data and sets them on the corresponding OTel span before ending it.
|
|
283
|
+
*
|
|
284
|
+
* @param span - The OpenAI Agents SDK span.
|
|
285
|
+
*/
|
|
286
|
+
async onSpanEnd(span: Span<SpanData>): Promise<void> {
|
|
287
|
+
logger.debug(
|
|
288
|
+
`onSpanEnd called: ${span?.spanId} (type: ${(span?.spanData as SpanData)?.type})`,
|
|
289
|
+
);
|
|
290
|
+
if (!span) return;
|
|
291
|
+
|
|
292
|
+
const otelSpan = this._spans.get(span.spanId);
|
|
293
|
+
if (!otelSpan) {
|
|
294
|
+
logger.debug(`No OTel span found for spanId: ${span.spanId}`);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
this._spans.delete(span.spanId);
|
|
299
|
+
const spanData = span.spanData as SpanData;
|
|
300
|
+
|
|
301
|
+
// Extract gen_ai.* attributes based on span type
|
|
302
|
+
switch (spanData.type) {
|
|
303
|
+
case "agent":
|
|
304
|
+
this._processAgentSpan(otelSpan, spanData as AgentSpanData);
|
|
305
|
+
break;
|
|
306
|
+
case "function":
|
|
307
|
+
this._processFunctionSpan(otelSpan, spanData as FunctionSpanData);
|
|
308
|
+
break;
|
|
309
|
+
case "response":
|
|
310
|
+
this._processResponseSpan(otelSpan, spanData as ResponseSpanData);
|
|
311
|
+
break;
|
|
312
|
+
case "generation":
|
|
313
|
+
this._processGenerationSpan(otelSpan, spanData as GenerationSpanData);
|
|
314
|
+
break;
|
|
315
|
+
case "handoff":
|
|
316
|
+
this._processHandoffSpan(otelSpan, spanData as HandoffSpanData);
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Propagate conversation ID from trace
|
|
321
|
+
const conversationId = this._conversationIds.get(span.traceId);
|
|
322
|
+
if (conversationId) {
|
|
323
|
+
otelSpan.setAttribute("gen_ai.conversation.id", conversationId);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Keep raw span data for debugging (matches Python attribute name)
|
|
327
|
+
otelSpan.setAttribute("openai_agents.span_data", JSON.stringify(spanData));
|
|
328
|
+
otelSpan.end();
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Extract attributes from agent spans.
|
|
333
|
+
*/
|
|
334
|
+
private _processAgentSpan(otelSpan: OtelSpan, spanData: AgentSpanData): void {
|
|
335
|
+
otelSpan.setAttribute("gen_ai.agent.name", spanData.name);
|
|
336
|
+
otelSpan.setAttribute("gen_ai.system", "openai");
|
|
337
|
+
otelSpan.setAttribute("openinference.span.kind", "AGENT");
|
|
338
|
+
|
|
339
|
+
if (spanData.tools) {
|
|
340
|
+
// Wrap tool names as objects for ClickHouse Array(JSON) compatibility
|
|
341
|
+
const toolDefs = spanData.tools.map((t: string) => ({ name: t }));
|
|
342
|
+
otelSpan.setAttribute(
|
|
343
|
+
"gen_ai.tool.definitions",
|
|
344
|
+
JSON.stringify(toolDefs),
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (spanData.handoffs) {
|
|
349
|
+
otelSpan.setAttribute(
|
|
350
|
+
"gen_ai.agent.handoffs",
|
|
351
|
+
JSON.stringify(spanData.handoffs),
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (spanData.output_type) {
|
|
356
|
+
otelSpan.setAttribute("gen_ai.agent.output_type", spanData.output_type);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Extract attributes from function/tool spans.
|
|
362
|
+
*/
|
|
363
|
+
private _processFunctionSpan(
|
|
364
|
+
otelSpan: OtelSpan,
|
|
365
|
+
spanData: FunctionSpanData,
|
|
366
|
+
): void {
|
|
367
|
+
otelSpan.setAttribute("gen_ai.tool.name", spanData.name);
|
|
368
|
+
otelSpan.setAttribute("openinference.span.kind", "TOOL");
|
|
369
|
+
|
|
370
|
+
if (spanData.input) {
|
|
371
|
+
otelSpan.setAttribute("gen_ai.tool.input", spanData.input);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (spanData.output) {
|
|
375
|
+
otelSpan.setAttribute("gen_ai.tool.output", String(spanData.output));
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Extract attributes from response spans.
|
|
381
|
+
* Matches Python implementation - extracts from both spanData and _response object.
|
|
382
|
+
*/
|
|
383
|
+
private _processResponseSpan(
|
|
384
|
+
otelSpan: OtelSpan,
|
|
385
|
+
spanData: ResponseSpanData,
|
|
386
|
+
): void {
|
|
387
|
+
otelSpan.setAttribute("gen_ai.operation.name", "chat");
|
|
388
|
+
otelSpan.setAttribute("gen_ai.system", "openai");
|
|
389
|
+
otelSpan.setAttribute("openinference.span.kind", "LLM");
|
|
390
|
+
|
|
391
|
+
// Response ID (from span_data directly)
|
|
392
|
+
if (spanData.response_id) {
|
|
393
|
+
otelSpan.setAttribute("gen_ai.response.id", spanData.response_id);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Input messages (from spanData._input)
|
|
397
|
+
if (spanData._input) {
|
|
398
|
+
const inputs = Array.isArray(spanData._input)
|
|
399
|
+
? spanData._input
|
|
400
|
+
: [spanData._input];
|
|
401
|
+
const [inputMessages] = convertResponsesInputsToSemconv(
|
|
402
|
+
inputs as ResponseInputItem[],
|
|
403
|
+
undefined,
|
|
404
|
+
);
|
|
405
|
+
if (inputMessages.length > 0) {
|
|
406
|
+
otelSpan.setAttribute(
|
|
407
|
+
"gen_ai.input.messages",
|
|
408
|
+
JSON.stringify(inputMessages),
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Extract from _response if available (full Response object)
|
|
414
|
+
const response = spanData._response as OpenAIResponse | undefined;
|
|
415
|
+
if (!response) return;
|
|
416
|
+
|
|
417
|
+
// System instructions
|
|
418
|
+
const sysInstructions = convertResponsesInstructionsToSemconv(
|
|
419
|
+
response.instructions,
|
|
420
|
+
);
|
|
421
|
+
if (sysInstructions) {
|
|
422
|
+
otelSpan.setAttribute(
|
|
423
|
+
"gen_ai.system_instructions",
|
|
424
|
+
JSON.stringify(sysInstructions),
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// Tool definitions (with full details from Response object)
|
|
429
|
+
if (response.tools && response.tools.length > 0) {
|
|
430
|
+
const toolDefs = convertResponsesToolsToSemconv(response.tools);
|
|
431
|
+
otelSpan.setAttribute(
|
|
432
|
+
"gen_ai.tool.definitions",
|
|
433
|
+
JSON.stringify(toolDefs),
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Token usage
|
|
438
|
+
const usage = response.usage;
|
|
439
|
+
if (usage) {
|
|
440
|
+
if (usage.input_tokens) {
|
|
441
|
+
otelSpan.setAttribute("gen_ai.usage.input_tokens", usage.input_tokens);
|
|
442
|
+
}
|
|
443
|
+
if (usage.output_tokens) {
|
|
444
|
+
otelSpan.setAttribute(
|
|
445
|
+
"gen_ai.usage.output_tokens",
|
|
446
|
+
usage.output_tokens,
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Model info
|
|
452
|
+
if (response.model) {
|
|
453
|
+
otelSpan.setAttribute("gen_ai.request.model", response.model);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Response ID from response object (fallback if not in span_data)
|
|
457
|
+
if (!spanData.response_id && response.id) {
|
|
458
|
+
otelSpan.setAttribute("gen_ai.response.id", response.id);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// Output messages (from response.output)
|
|
462
|
+
if (response.output && response.output.length > 0) {
|
|
463
|
+
const outputMessages = convertResponsesOutputsToSemconv(response.output);
|
|
464
|
+
if (outputMessages.length > 0) {
|
|
465
|
+
otelSpan.setAttribute(
|
|
466
|
+
"gen_ai.output.messages",
|
|
467
|
+
JSON.stringify(outputMessages),
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Extract attributes from generation spans.
|
|
475
|
+
*/
|
|
476
|
+
private _processGenerationSpan(
|
|
477
|
+
otelSpan: OtelSpan,
|
|
478
|
+
spanData: GenerationSpanData,
|
|
479
|
+
): void {
|
|
480
|
+
otelSpan.setAttribute("gen_ai.operation.name", "chat");
|
|
481
|
+
otelSpan.setAttribute("gen_ai.system", "openai");
|
|
482
|
+
otelSpan.setAttribute("openinference.span.kind", "LLM");
|
|
483
|
+
|
|
484
|
+
if (spanData.model) {
|
|
485
|
+
otelSpan.setAttribute("gen_ai.request.model", spanData.model);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (spanData.usage) {
|
|
489
|
+
const usage = spanData.usage as Record<string, number>;
|
|
490
|
+
if (usage.input_tokens) {
|
|
491
|
+
otelSpan.setAttribute("gen_ai.usage.input_tokens", usage.input_tokens);
|
|
492
|
+
}
|
|
493
|
+
if (usage.output_tokens) {
|
|
494
|
+
otelSpan.setAttribute(
|
|
495
|
+
"gen_ai.usage.output_tokens",
|
|
496
|
+
usage.output_tokens,
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (spanData.input) {
|
|
502
|
+
otelSpan.setAttribute(
|
|
503
|
+
"gen_ai.input.messages",
|
|
504
|
+
JSON.stringify(spanData.input),
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
if (spanData.output) {
|
|
509
|
+
otelSpan.setAttribute(
|
|
510
|
+
"gen_ai.output.messages",
|
|
511
|
+
JSON.stringify(spanData.output),
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Extract attributes from handoff spans.
|
|
518
|
+
*/
|
|
519
|
+
private _processHandoffSpan(
|
|
520
|
+
otelSpan: OtelSpan,
|
|
521
|
+
spanData: HandoffSpanData,
|
|
522
|
+
): void {
|
|
523
|
+
if (spanData.from_agent) {
|
|
524
|
+
otelSpan.setAttribute("gen_ai.handoff.from_agent", spanData.from_agent);
|
|
525
|
+
}
|
|
526
|
+
if (spanData.to_agent) {
|
|
527
|
+
otelSpan.setAttribute("gen_ai.handoff.to_agent", spanData.to_agent);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Shut down the tracer provider, flushing all pending spans.
|
|
533
|
+
*
|
|
534
|
+
* @returns A promise that resolves once shutdown is complete.
|
|
535
|
+
*/
|
|
536
|
+
async shutdown(): Promise<void> {
|
|
537
|
+
await this._tracerProvider.shutdown();
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Force-flush any buffered spans to the Introspection backend.
|
|
542
|
+
*
|
|
543
|
+
* @returns A promise that resolves once the flush completes.
|
|
544
|
+
*/
|
|
545
|
+
async forceFlush(): Promise<void> {
|
|
546
|
+
await this._tracerProvider.forceFlush();
|
|
547
|
+
}
|
|
548
|
+
}
|