@introspection-sdk/introspection-node 0.4.3 → 0.4.5
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 +1 -1
- package/dist/otel/anthropic.d.ts +14 -12
- package/dist/otel/anthropic.d.ts.map +1 -1
- package/dist/otel/anthropic.js +89 -47
- package/dist/otel/anthropic.js.map +1 -1
- package/dist/otel/gemini.d.ts +15 -7
- package/dist/otel/gemini.d.ts.map +1 -1
- package/dist/otel/gemini.js +88 -37
- package/dist/otel/gemini.js.map +1 -1
- package/dist/otel/index.d.ts +5 -0
- package/dist/otel/index.d.ts.map +1 -1
- package/dist/otel/index.js +8 -0
- package/dist/otel/index.js.map +1 -1
- package/dist/otel/init.d.ts +126 -0
- package/dist/otel/init.d.ts.map +1 -0
- package/dist/otel/init.js +259 -0
- package/dist/otel/init.js.map +1 -0
- package/dist/otel/integrations/anthropic.d.ts +10 -0
- package/dist/otel/integrations/anthropic.d.ts.map +1 -0
- package/dist/otel/integrations/anthropic.js +22 -0
- package/dist/otel/integrations/anthropic.js.map +1 -0
- package/dist/otel/integrations/base.d.ts +83 -0
- package/dist/otel/integrations/base.d.ts.map +1 -0
- package/dist/otel/integrations/base.js +23 -0
- package/dist/otel/integrations/base.js.map +1 -0
- package/dist/otel/integrations/claude-agent.d.ts +13 -0
- package/dist/otel/integrations/claude-agent.d.ts.map +1 -0
- package/dist/otel/integrations/claude-agent.js +27 -0
- package/dist/otel/integrations/claude-agent.js.map +1 -0
- package/dist/otel/integrations/gemini.d.ts +11 -0
- package/dist/otel/integrations/gemini.d.ts.map +1 -0
- package/dist/otel/integrations/gemini.js +21 -0
- package/dist/otel/integrations/gemini.js.map +1 -0
- package/dist/otel/integrations/index.d.ts +39 -0
- package/dist/otel/integrations/index.d.ts.map +1 -0
- package/dist/otel/integrations/index.js +131 -0
- package/dist/otel/integrations/index.js.map +1 -0
- package/dist/otel/integrations/langchain.d.ts +16 -0
- package/dist/otel/integrations/langchain.d.ts.map +1 -0
- package/dist/otel/integrations/langchain.js +41 -0
- package/dist/otel/integrations/langchain.js.map +1 -0
- package/dist/otel/integrations/mastra.d.ts +14 -0
- package/dist/otel/integrations/mastra.d.ts.map +1 -0
- package/dist/otel/integrations/mastra.js +27 -0
- package/dist/otel/integrations/mastra.js.map +1 -0
- package/dist/otel/integrations/openai-agents.d.ts +11 -0
- package/dist/otel/integrations/openai-agents.d.ts.map +1 -0
- package/dist/otel/integrations/openai-agents.js +25 -0
- package/dist/otel/integrations/openai-agents.js.map +1 -0
- package/dist/otel/integrations/pi.d.ts +12 -0
- package/dist/otel/integrations/pi.d.ts.map +1 -0
- package/dist/otel/integrations/pi.js +20 -0
- package/dist/otel/integrations/pi.js.map +1 -0
- package/dist/otel/integrations/vercel.d.ts +15 -0
- package/dist/otel/integrations/vercel.d.ts.map +1 -0
- package/dist/otel/integrations/vercel.js +20 -0
- package/dist/otel/integrations/vercel.js.map +1 -0
- package/dist/otel/register.d.ts +8 -0
- package/dist/otel/register.d.ts.map +1 -0
- package/dist/otel/register.js +48 -0
- package/dist/otel/register.js.map +1 -0
- package/dist/otel/setup.d.ts +29 -0
- package/dist/otel/setup.d.ts.map +1 -1
- package/dist/otel/setup.js +22 -3
- package/dist/otel/setup.js.map +1 -1
- package/dist/utils.d.ts +10 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +12 -4
- package/dist/utils.js.map +1 -1
- package/package.json +17 -5
- package/src/otel/anthropic.ts +128 -63
- package/src/otel/gemini.ts +139 -45
- package/src/otel/index.ts +41 -0
- package/src/otel/init.ts +411 -0
- package/src/otel/integrations/anthropic.ts +26 -0
- package/src/otel/integrations/base.ts +95 -0
- package/src/otel/integrations/claude-agent.ts +32 -0
- package/src/otel/integrations/gemini.ts +25 -0
- package/src/otel/integrations/index.ts +151 -0
- package/src/otel/integrations/langchain.ts +45 -0
- package/src/otel/integrations/mastra.ts +30 -0
- package/src/otel/integrations/openai-agents.ts +31 -0
- package/src/otel/integrations/pi.ts +24 -0
- package/src/otel/integrations/vercel.ts +24 -0
- package/src/otel/register.ts +50 -0
- package/src/otel/setup.ts +44 -3
- package/src/utils.ts +12 -5
- package/.turbo/turbo-check-types.log +0 -4
- package/.turbo/turbo-lint.log +0 -5
- package/dist/ai-sdk-integration.d.ts +0 -123
- package/dist/ai-sdk-integration.d.ts.map +0 -1
- package/dist/ai-sdk-integration.js +0 -475
- package/dist/anthropic.d.ts +0 -54
- package/dist/anthropic.d.ts.map +0 -1
- package/dist/anthropic.js +0 -375
- package/dist/claude-hooks.d.ts +0 -328
- package/dist/claude-hooks.d.ts.map +0 -1
- package/dist/claude-hooks.js +0 -571
- package/dist/claude-wrapper.d.ts +0 -93
- package/dist/claude-wrapper.d.ts.map +0 -1
- package/dist/claude-wrapper.js +0 -154
- package/dist/converters/ai-sdk.d.ts +0 -65
- package/dist/converters/ai-sdk.d.ts.map +0 -1
- package/dist/converters/ai-sdk.js +0 -261
- package/dist/converters/openclaw.d.ts +0 -16
- package/dist/converters/openclaw.d.ts.map +0 -1
- package/dist/converters/openclaw.js +0 -49
- package/dist/langchain-handler.d.ts +0 -103
- package/dist/langchain-handler.d.ts.map +0 -1
- package/dist/langchain-handler.js +0 -518
- package/dist/mastra-exporter.d.ts +0 -85
- package/dist/mastra-exporter.d.ts.map +0 -1
- package/dist/mastra-exporter.js +0 -471
- package/dist/span-processor.d.ts +0 -62
- package/dist/span-processor.d.ts.map +0 -1
- package/dist/span-processor.js +0 -313
- package/dist/tracing-processor.d.ts +0 -136
- package/dist/tracing-processor.d.ts.map +0 -1
- package/dist/tracing-processor.js +0 -368
- package/dist/types/genai.d.ts +0 -165
- package/dist/types/genai.d.ts.map +0 -1
- package/dist/types/genai.js +0 -82
|
@@ -1,368 +0,0 @@
|
|
|
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
|
-
import { context as otelContext, trace as otelTrace, } from "@opentelemetry/api";
|
|
8
|
-
import { BasicTracerProvider, BatchSpanProcessor, SimpleSpanProcessor, } from "@opentelemetry/sdk-trace-base";
|
|
9
|
-
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
10
|
-
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
|
|
11
|
-
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
|
|
12
|
-
import { randomUUID } from "crypto";
|
|
13
|
-
import { convertResponsesInputsToSemconv, convertResponsesOutputsToSemconv, convertResponsesToolsToSemconv, convertResponsesInstructionsToSemconv, } from "./converters/openai.js";
|
|
14
|
-
import { logger, withOtlpHttpsProxy } from "./utils.js";
|
|
15
|
-
import { VERSION } from "./version.js";
|
|
16
|
-
/**
|
|
17
|
-
* {@link TracingProcessor} that forwards OpenAI agent traces to the
|
|
18
|
-
* Introspection backend via OTLP.
|
|
19
|
-
*
|
|
20
|
-
* Extracts OTel Gen AI semantic convention attributes from span data:
|
|
21
|
-
* - Agent spans: `gen_ai.agent.name`, `gen_ai.tool.definitions`, `gen_ai.agent.handoffs`
|
|
22
|
-
* - Function spans: `gen_ai.tool.name`, `gen_ai.tool.input`, `gen_ai.tool.output`
|
|
23
|
-
* - Response spans: `gen_ai.input/output.messages`, `gen_ai.usage.*`, `gen_ai.request.model`
|
|
24
|
-
* - Generation spans: `gen_ai.request.model`, `gen_ai.usage.*`
|
|
25
|
-
* - Handoff spans: `gen_ai.handoff.from_agent`, `gen_ai.handoff.to_agent`
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```ts
|
|
29
|
-
* import { IntrospectionTracingProcessor } from "@introspection-sdk/introspection-node";
|
|
30
|
-
*
|
|
31
|
-
* const processor = new IntrospectionTracingProcessor({ token: "sk-intro-…" });
|
|
32
|
-
* // pass processor to the OpenAI Agents SDK withTrace() or registerProcessor()
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export class IntrospectionTracingProcessor {
|
|
36
|
-
_tracerProvider;
|
|
37
|
-
_tracer;
|
|
38
|
-
_spans = new Map();
|
|
39
|
-
_conversationIds = new Map();
|
|
40
|
-
constructor(options = {}) {
|
|
41
|
-
const advanced = options.advanced;
|
|
42
|
-
const serviceName = options.serviceName || process.env.INTROSPECTION_SERVICE_NAME;
|
|
43
|
-
const resource = serviceName
|
|
44
|
-
? resourceFromAttributes({ [ATTR_SERVICE_NAME]: serviceName })
|
|
45
|
-
: undefined;
|
|
46
|
-
// If custom exporter is provided (testing mode), skip token validation
|
|
47
|
-
if (!advanced?.spanExporter) {
|
|
48
|
-
const token = options.token || process.env.INTROSPECTION_TOKEN;
|
|
49
|
-
if (!token) {
|
|
50
|
-
throw new Error("INTROSPECTION_TOKEN is required");
|
|
51
|
-
}
|
|
52
|
-
const baseUrl = options.baseUrl ||
|
|
53
|
-
process.env.INTROSPECTION_BASE_URL ||
|
|
54
|
-
"https://otel.introspection.dev";
|
|
55
|
-
const headers = {
|
|
56
|
-
"User-Agent": `introspection-sdk/${VERSION}`,
|
|
57
|
-
Authorization: `Bearer ${token}`,
|
|
58
|
-
...options.additionalHeaders,
|
|
59
|
-
};
|
|
60
|
-
// Construct endpoint URL
|
|
61
|
-
const endpoint = baseUrl.endsWith("/v1/traces")
|
|
62
|
-
? baseUrl
|
|
63
|
-
: `${baseUrl.replace(/\/$/, "")}/v1/traces`;
|
|
64
|
-
logger.info(`IntrospectionTracingProcessor initialized: endpoint=${endpoint}`);
|
|
65
|
-
// Setup OTLP exporter
|
|
66
|
-
const spanExporter = new OTLPTraceExporter(withOtlpHttpsProxy({
|
|
67
|
-
url: endpoint,
|
|
68
|
-
headers,
|
|
69
|
-
}));
|
|
70
|
-
// Use SimpleSpanProcessor for maxExportBatchSize=1 (sequential export),
|
|
71
|
-
// otherwise BatchSpanProcessor with configurable batch size.
|
|
72
|
-
// Default to sequential export for dev/staging tokens.
|
|
73
|
-
const effectiveBatchSize = advanced?.maxExportBatchSize ??
|
|
74
|
-
(token.startsWith("intro_dev") || token.startsWith("intro_staging")
|
|
75
|
-
? 1
|
|
76
|
-
: undefined);
|
|
77
|
-
const useSimple = advanced?.useSimpleSpanProcessor || effectiveBatchSize === 1;
|
|
78
|
-
const scheduledDelayMillis = advanced?.scheduledDelayMillis ?? 1000;
|
|
79
|
-
const spanProcessor = useSimple
|
|
80
|
-
? new SimpleSpanProcessor(spanExporter)
|
|
81
|
-
: new BatchSpanProcessor(spanExporter, {
|
|
82
|
-
scheduledDelayMillis,
|
|
83
|
-
...(effectiveBatchSize
|
|
84
|
-
? { maxExportBatchSize: effectiveBatchSize }
|
|
85
|
-
: {}),
|
|
86
|
-
});
|
|
87
|
-
this._tracerProvider = new BasicTracerProvider({
|
|
88
|
-
resource,
|
|
89
|
-
idGenerator: advanced?.idGenerator,
|
|
90
|
-
spanProcessors: [spanProcessor],
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
// Testing mode: use provided exporter
|
|
95
|
-
logger.info(`IntrospectionTracingProcessor initialized in test mode with custom exporter`);
|
|
96
|
-
// Choose span processor based on options
|
|
97
|
-
const spanProcessor = advanced.useSimpleSpanProcessor
|
|
98
|
-
? new SimpleSpanProcessor(advanced.spanExporter)
|
|
99
|
-
: new BatchSpanProcessor(advanced.spanExporter, {
|
|
100
|
-
scheduledDelayMillis: 100,
|
|
101
|
-
});
|
|
102
|
-
this._tracerProvider = new BasicTracerProvider({
|
|
103
|
-
resource,
|
|
104
|
-
idGenerator: advanced.idGenerator,
|
|
105
|
-
spanProcessors: [spanProcessor],
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
this._tracer = this._tracerProvider.getTracer("openai-agents", VERSION);
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Called when the processor is started. Optional lifecycle hook.
|
|
112
|
-
*/
|
|
113
|
-
start() {
|
|
114
|
-
// No-op - processor is ready after construction
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Called when a trace starts. Creates a root OTel span.
|
|
118
|
-
*
|
|
119
|
-
* @param trace - The OpenAI Agents SDK trace object.
|
|
120
|
-
*/
|
|
121
|
-
async onTraceStart(trace) {
|
|
122
|
-
logger.debug(`onTraceStart called: ${trace?.name} (${trace?.traceId})`);
|
|
123
|
-
if (trace) {
|
|
124
|
-
const conversationId = `intro_conv_${randomUUID().replace(/-/g, "")}`;
|
|
125
|
-
this._conversationIds.set(trace.traceId, conversationId);
|
|
126
|
-
const otelSpan = this._tracer.startSpan(trace.name);
|
|
127
|
-
this._spans.set(trace.traceId, otelSpan);
|
|
128
|
-
logger.debug(`Created root span for trace: ${trace.traceId}`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Called when a trace ends. Closes the root OTel span.
|
|
133
|
-
*
|
|
134
|
-
* @param trace - The OpenAI Agents SDK trace object.
|
|
135
|
-
*/
|
|
136
|
-
async onTraceEnd(trace) {
|
|
137
|
-
logger.debug(`onTraceEnd called: ${trace?.name} (${trace?.traceId})`);
|
|
138
|
-
if (trace) {
|
|
139
|
-
const otelSpan = this._spans.get(trace.traceId);
|
|
140
|
-
if (otelSpan) {
|
|
141
|
-
this._spans.delete(trace.traceId);
|
|
142
|
-
this._conversationIds.delete(trace.traceId);
|
|
143
|
-
otelSpan.end();
|
|
144
|
-
logger.debug(`Ended root span for trace: ${trace.traceId}`);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Called when a span starts. Creates a child OTel span with parent context.
|
|
150
|
-
*
|
|
151
|
-
* @param span - The OpenAI Agents SDK span.
|
|
152
|
-
*/
|
|
153
|
-
async onSpanStart(span) {
|
|
154
|
-
logger.debug(`onSpanStart called: ${span?.spanId} (type: ${span?.spanData?.type})`);
|
|
155
|
-
if (span) {
|
|
156
|
-
const parentId = span.parentId || span.traceId;
|
|
157
|
-
const parent = this._spans.get(parentId);
|
|
158
|
-
const ctx = parent
|
|
159
|
-
? otelTrace.setSpan(otelContext.active(), parent)
|
|
160
|
-
: undefined;
|
|
161
|
-
const spanData = span.spanData;
|
|
162
|
-
let name;
|
|
163
|
-
// Use name for agent and function spans, type for others
|
|
164
|
-
if (spanData.type === "agent") {
|
|
165
|
-
name = spanData.name;
|
|
166
|
-
}
|
|
167
|
-
else if (spanData.type === "function") {
|
|
168
|
-
name = spanData.name;
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
name = spanData.type;
|
|
172
|
-
}
|
|
173
|
-
const otelSpan = this._tracer.startSpan(name, {}, ctx);
|
|
174
|
-
this._spans.set(span.spanId, otelSpan);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Called when a span ends. Extracts `gen_ai.*` attributes from the span
|
|
179
|
-
* data and sets them on the corresponding OTel span before ending it.
|
|
180
|
-
*
|
|
181
|
-
* @param span - The OpenAI Agents SDK span.
|
|
182
|
-
*/
|
|
183
|
-
async onSpanEnd(span) {
|
|
184
|
-
logger.debug(`onSpanEnd called: ${span?.spanId} (type: ${span?.spanData?.type})`);
|
|
185
|
-
if (!span)
|
|
186
|
-
return;
|
|
187
|
-
const otelSpan = this._spans.get(span.spanId);
|
|
188
|
-
if (!otelSpan) {
|
|
189
|
-
logger.debug(`No OTel span found for spanId: ${span.spanId}`);
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
this._spans.delete(span.spanId);
|
|
193
|
-
const spanData = span.spanData;
|
|
194
|
-
// Extract gen_ai.* attributes based on span type
|
|
195
|
-
switch (spanData.type) {
|
|
196
|
-
case "agent":
|
|
197
|
-
this._processAgentSpan(otelSpan, spanData);
|
|
198
|
-
break;
|
|
199
|
-
case "function":
|
|
200
|
-
this._processFunctionSpan(otelSpan, spanData);
|
|
201
|
-
break;
|
|
202
|
-
case "response":
|
|
203
|
-
this._processResponseSpan(otelSpan, spanData);
|
|
204
|
-
break;
|
|
205
|
-
case "generation":
|
|
206
|
-
this._processGenerationSpan(otelSpan, spanData);
|
|
207
|
-
break;
|
|
208
|
-
case "handoff":
|
|
209
|
-
this._processHandoffSpan(otelSpan, spanData);
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
// Propagate conversation ID from trace
|
|
213
|
-
const conversationId = this._conversationIds.get(span.traceId);
|
|
214
|
-
if (conversationId) {
|
|
215
|
-
otelSpan.setAttribute("gen_ai.conversation.id", conversationId);
|
|
216
|
-
}
|
|
217
|
-
// Keep raw span data for debugging (matches Python attribute name)
|
|
218
|
-
otelSpan.setAttribute("openai_agents.span_data", JSON.stringify(spanData));
|
|
219
|
-
otelSpan.end();
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Extract attributes from agent spans.
|
|
223
|
-
*/
|
|
224
|
-
_processAgentSpan(otelSpan, spanData) {
|
|
225
|
-
otelSpan.setAttribute("gen_ai.agent.name", spanData.name);
|
|
226
|
-
otelSpan.setAttribute("gen_ai.system", "openai");
|
|
227
|
-
otelSpan.setAttribute("openinference.span.kind", "AGENT");
|
|
228
|
-
if (spanData.tools) {
|
|
229
|
-
// Wrap tool names as objects for ClickHouse Array(JSON) compatibility
|
|
230
|
-
const toolDefs = spanData.tools.map((t) => ({ name: t }));
|
|
231
|
-
otelSpan.setAttribute("gen_ai.tool.definitions", JSON.stringify(toolDefs));
|
|
232
|
-
}
|
|
233
|
-
if (spanData.handoffs) {
|
|
234
|
-
otelSpan.setAttribute("gen_ai.agent.handoffs", JSON.stringify(spanData.handoffs));
|
|
235
|
-
}
|
|
236
|
-
if (spanData.output_type) {
|
|
237
|
-
otelSpan.setAttribute("gen_ai.agent.output_type", spanData.output_type);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Extract attributes from function/tool spans.
|
|
242
|
-
*/
|
|
243
|
-
_processFunctionSpan(otelSpan, spanData) {
|
|
244
|
-
otelSpan.setAttribute("gen_ai.tool.name", spanData.name);
|
|
245
|
-
otelSpan.setAttribute("openinference.span.kind", "TOOL");
|
|
246
|
-
if (spanData.input) {
|
|
247
|
-
otelSpan.setAttribute("gen_ai.tool.input", spanData.input);
|
|
248
|
-
}
|
|
249
|
-
if (spanData.output) {
|
|
250
|
-
otelSpan.setAttribute("gen_ai.tool.output", String(spanData.output));
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Extract attributes from response spans.
|
|
255
|
-
* Matches Python implementation - extracts from both spanData and _response object.
|
|
256
|
-
*/
|
|
257
|
-
_processResponseSpan(otelSpan, spanData) {
|
|
258
|
-
otelSpan.setAttribute("gen_ai.operation.name", "chat");
|
|
259
|
-
otelSpan.setAttribute("gen_ai.system", "openai");
|
|
260
|
-
otelSpan.setAttribute("openinference.span.kind", "LLM");
|
|
261
|
-
// Response ID (from span_data directly)
|
|
262
|
-
if (spanData.response_id) {
|
|
263
|
-
otelSpan.setAttribute("gen_ai.response.id", spanData.response_id);
|
|
264
|
-
}
|
|
265
|
-
// Input messages (from spanData._input)
|
|
266
|
-
if (spanData._input) {
|
|
267
|
-
const inputs = Array.isArray(spanData._input)
|
|
268
|
-
? spanData._input
|
|
269
|
-
: [spanData._input];
|
|
270
|
-
const [inputMessages] = convertResponsesInputsToSemconv(inputs, undefined);
|
|
271
|
-
if (inputMessages.length > 0) {
|
|
272
|
-
otelSpan.setAttribute("gen_ai.input.messages", JSON.stringify(inputMessages));
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
// Extract from _response if available (full Response object)
|
|
276
|
-
const response = spanData._response;
|
|
277
|
-
if (!response)
|
|
278
|
-
return;
|
|
279
|
-
// System instructions
|
|
280
|
-
const sysInstructions = convertResponsesInstructionsToSemconv(response.instructions);
|
|
281
|
-
if (sysInstructions) {
|
|
282
|
-
otelSpan.setAttribute("gen_ai.system_instructions", JSON.stringify(sysInstructions));
|
|
283
|
-
}
|
|
284
|
-
// Tool definitions (with full details from Response object)
|
|
285
|
-
if (response.tools && response.tools.length > 0) {
|
|
286
|
-
const toolDefs = convertResponsesToolsToSemconv(response.tools);
|
|
287
|
-
otelSpan.setAttribute("gen_ai.tool.definitions", JSON.stringify(toolDefs));
|
|
288
|
-
}
|
|
289
|
-
// Token usage
|
|
290
|
-
const usage = response.usage;
|
|
291
|
-
if (usage) {
|
|
292
|
-
if (usage.input_tokens) {
|
|
293
|
-
otelSpan.setAttribute("gen_ai.usage.input_tokens", usage.input_tokens);
|
|
294
|
-
}
|
|
295
|
-
if (usage.output_tokens) {
|
|
296
|
-
otelSpan.setAttribute("gen_ai.usage.output_tokens", usage.output_tokens);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
// Model info
|
|
300
|
-
if (response.model) {
|
|
301
|
-
otelSpan.setAttribute("gen_ai.request.model", response.model);
|
|
302
|
-
}
|
|
303
|
-
// Response ID from response object (fallback if not in span_data)
|
|
304
|
-
if (!spanData.response_id && response.id) {
|
|
305
|
-
otelSpan.setAttribute("gen_ai.response.id", response.id);
|
|
306
|
-
}
|
|
307
|
-
// Output messages (from response.output)
|
|
308
|
-
if (response.output && response.output.length > 0) {
|
|
309
|
-
const outputMessages = convertResponsesOutputsToSemconv(response.output);
|
|
310
|
-
if (outputMessages.length > 0) {
|
|
311
|
-
otelSpan.setAttribute("gen_ai.output.messages", JSON.stringify(outputMessages));
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Extract attributes from generation spans.
|
|
317
|
-
*/
|
|
318
|
-
_processGenerationSpan(otelSpan, spanData) {
|
|
319
|
-
otelSpan.setAttribute("gen_ai.operation.name", "chat");
|
|
320
|
-
otelSpan.setAttribute("gen_ai.system", "openai");
|
|
321
|
-
otelSpan.setAttribute("openinference.span.kind", "LLM");
|
|
322
|
-
if (spanData.model) {
|
|
323
|
-
otelSpan.setAttribute("gen_ai.request.model", spanData.model);
|
|
324
|
-
}
|
|
325
|
-
if (spanData.usage) {
|
|
326
|
-
const usage = spanData.usage;
|
|
327
|
-
if (usage.input_tokens) {
|
|
328
|
-
otelSpan.setAttribute("gen_ai.usage.input_tokens", usage.input_tokens);
|
|
329
|
-
}
|
|
330
|
-
if (usage.output_tokens) {
|
|
331
|
-
otelSpan.setAttribute("gen_ai.usage.output_tokens", usage.output_tokens);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
if (spanData.input) {
|
|
335
|
-
otelSpan.setAttribute("gen_ai.input.messages", JSON.stringify(spanData.input));
|
|
336
|
-
}
|
|
337
|
-
if (spanData.output) {
|
|
338
|
-
otelSpan.setAttribute("gen_ai.output.messages", JSON.stringify(spanData.output));
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
/**
|
|
342
|
-
* Extract attributes from handoff spans.
|
|
343
|
-
*/
|
|
344
|
-
_processHandoffSpan(otelSpan, spanData) {
|
|
345
|
-
if (spanData.from_agent) {
|
|
346
|
-
otelSpan.setAttribute("gen_ai.handoff.from_agent", spanData.from_agent);
|
|
347
|
-
}
|
|
348
|
-
if (spanData.to_agent) {
|
|
349
|
-
otelSpan.setAttribute("gen_ai.handoff.to_agent", spanData.to_agent);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Shut down the tracer provider, flushing all pending spans.
|
|
354
|
-
*
|
|
355
|
-
* @returns A promise that resolves once shutdown is complete.
|
|
356
|
-
*/
|
|
357
|
-
async shutdown() {
|
|
358
|
-
await this._tracerProvider.shutdown();
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Force-flush any buffered spans to the Introspection backend.
|
|
362
|
-
*
|
|
363
|
-
* @returns A promise that resolves once the flush completes.
|
|
364
|
-
*/
|
|
365
|
-
async forceFlush() {
|
|
366
|
-
await this._tracerProvider.forceFlush();
|
|
367
|
-
}
|
|
368
|
-
}
|
package/dist/types/genai.d.ts
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OTel Gen AI Semantic Convention types.
|
|
3
|
-
*
|
|
4
|
-
* These types represent the standardized format for gen_ai.input.messages
|
|
5
|
-
* and gen_ai.output.messages attributes as per OpenTelemetry Gen AI semantic conventions.
|
|
6
|
-
*
|
|
7
|
-
* @see https://opentelemetry.io/docs/specs/semconv/gen-ai/
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* A text content part in a message.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const part: TextPart = { type: "text", content: "Hello" };
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
export interface TextPart {
|
|
18
|
-
/** Discriminator — always `"text"`. */
|
|
19
|
-
type: "text";
|
|
20
|
-
/** The text body of this part. */
|
|
21
|
-
content: string;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* A tool / function-call request part in a message.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```ts
|
|
28
|
-
* const part: ToolCallRequestPart = { type: "tool_call", name: "get_weather", arguments: { city: "SF" } };
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
export interface ToolCallRequestPart {
|
|
32
|
-
/** Discriminator — always `"tool_call"`. */
|
|
33
|
-
type: "tool_call";
|
|
34
|
-
/** Function / tool name. */
|
|
35
|
-
name: string;
|
|
36
|
-
/** Correlation ID linking request to response. */
|
|
37
|
-
id?: string;
|
|
38
|
-
/** Arguments passed to the tool. */
|
|
39
|
-
arguments?: unknown;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* A tool / function-call response part in a message.
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```ts
|
|
46
|
-
* const part: ToolCallResponsePart = { type: "tool_call_response", response: "72°F" };
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
export interface ToolCallResponsePart {
|
|
50
|
-
/** Discriminator — always `"tool_call_response"`. */
|
|
51
|
-
type: "tool_call_response";
|
|
52
|
-
/** The value returned by the tool. */
|
|
53
|
-
response: unknown;
|
|
54
|
-
/** Correlation ID linking response to request. */
|
|
55
|
-
id?: string;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* A reasoning / thinking content part in a message.
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```ts
|
|
62
|
-
* const part: ReasoningPart = { type: "thinking", content: "Let me think through this..." };
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
export interface ReasoningPart {
|
|
66
|
-
/** Discriminator — always `"thinking"`. */
|
|
67
|
-
type: "thinking";
|
|
68
|
-
/** The reasoning / thinking summary content. */
|
|
69
|
-
content?: string;
|
|
70
|
-
/** Encrypted reasoning signature (maps to OpenAI encrypted_content, Anthropic signature/redacted_thinking data). */
|
|
71
|
-
signature?: string;
|
|
72
|
-
/** Provider that produced this thinking block (e.g. `"anthropic"`, `"openai"`). Used to reconstruct the correct wire format on replay. */
|
|
73
|
-
provider_name?: string;
|
|
74
|
-
}
|
|
75
|
-
/** Union of all possible message-part shapes. */
|
|
76
|
-
export type MessagePart = TextPart | ToolCallRequestPart | ToolCallResponsePart | ReasoningPart;
|
|
77
|
-
/**
|
|
78
|
-
* A system instruction entry for `gen_ai.system_instructions`.
|
|
79
|
-
*/
|
|
80
|
-
export interface SystemInstruction {
|
|
81
|
-
/** Part type — always `"text"`. */
|
|
82
|
-
type: "text";
|
|
83
|
-
/** The instruction text content. */
|
|
84
|
-
content: string;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* An input message in OTel Gen AI semantic convention format.
|
|
88
|
-
*
|
|
89
|
-
* Used for the `gen_ai.input.messages` span attribute.
|
|
90
|
-
*/
|
|
91
|
-
export interface InputMessage {
|
|
92
|
-
/** The role of the message sender. */
|
|
93
|
-
role: "system" | "user" | "assistant" | "tool";
|
|
94
|
-
/** Ordered content parts that compose this message. */
|
|
95
|
-
parts: MessagePart[];
|
|
96
|
-
/** Optional tool name when `role` is `"tool"`. */
|
|
97
|
-
name?: string;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* An output message in OTel Gen AI semantic convention format.
|
|
101
|
-
*
|
|
102
|
-
* Used for the `gen_ai.output.messages` span attribute.
|
|
103
|
-
*/
|
|
104
|
-
export interface OutputMessage {
|
|
105
|
-
/** The role of the message sender. */
|
|
106
|
-
role: "system" | "user" | "assistant" | "tool";
|
|
107
|
-
/** Ordered content parts that compose this message. */
|
|
108
|
-
parts: MessagePart[];
|
|
109
|
-
/** Model-reported finish reason (e.g. `"stop"`, `"end_turn"`). */
|
|
110
|
-
finish_reason?: string;
|
|
111
|
-
/** Optional tool name when `role` is `"tool"`. */
|
|
112
|
-
name?: string;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* A tool definition for the `gen_ai.tool.definitions` attribute.
|
|
116
|
-
*
|
|
117
|
-
* @example
|
|
118
|
-
* ```ts
|
|
119
|
-
* const def: ToolDefinition = { name: "get_weather", description: "Get current weather" };
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
export interface ToolDefinition {
|
|
123
|
-
/** Tool / function name. */
|
|
124
|
-
name: string;
|
|
125
|
-
/** Human-readable description of what the tool does. */
|
|
126
|
-
description?: string;
|
|
127
|
-
/** JSON Schema describing the tool's parameters. */
|
|
128
|
-
parameters?: Record<string, unknown>;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* GenAI semantic convention attributes.
|
|
132
|
-
*
|
|
133
|
-
* This interface represents the standardized GenAI attributes that can be
|
|
134
|
-
* converted to OpenTelemetry semantic convention format (gen_ai.* with dots).
|
|
135
|
-
*
|
|
136
|
-
* All properties use camelCase following TypeScript conventions.
|
|
137
|
-
*/
|
|
138
|
-
export interface GenAiAttributes {
|
|
139
|
-
/** Model name (gen_ai.request.model) */
|
|
140
|
-
requestModel?: string;
|
|
141
|
-
/** System prompt (gen_ai.system) */
|
|
142
|
-
system?: string;
|
|
143
|
-
/** Operation name (gen_ai.operation.name) */
|
|
144
|
-
operationName?: string;
|
|
145
|
-
/** Tool definitions (gen_ai.tool.definitions) — serialized to JSON by {@link toAttributes}. */
|
|
146
|
-
toolDefinitions?: ToolDefinition[];
|
|
147
|
-
/** Input messages (gen_ai.input.messages) — serialized to JSON by {@link toAttributes}. */
|
|
148
|
-
inputMessages?: InputMessage[];
|
|
149
|
-
/** Output messages (gen_ai.output.messages) — serialized to JSON by {@link toAttributes}. */
|
|
150
|
-
outputMessages?: OutputMessage[];
|
|
151
|
-
/** System instructions (gen_ai.system_instructions) — serialized to JSON by {@link toAttributes}. */
|
|
152
|
-
systemInstructions?: SystemInstruction[];
|
|
153
|
-
/** Response ID (gen_ai.response.id) */
|
|
154
|
-
responseId?: string;
|
|
155
|
-
/** Input token count (gen_ai.usage.input_tokens) */
|
|
156
|
-
inputTokens?: number;
|
|
157
|
-
/** Output token count (gen_ai.usage.output_tokens) */
|
|
158
|
-
outputTokens?: number;
|
|
159
|
-
/** Cache creation input tokens (gen_ai.usage.cache_creation_input_tokens) */
|
|
160
|
-
cacheCreationInputTokens?: number;
|
|
161
|
-
/** Cache read input tokens (gen_ai.usage.cache_read_input_tokens) */
|
|
162
|
-
cacheReadInputTokens?: number;
|
|
163
|
-
}
|
|
164
|
-
export declare function toAttributes(attrs: GenAiAttributes): Record<string, string | number>;
|
|
165
|
-
//# sourceMappingURL=genai.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"genai.d.ts","sourceRoot":"","sources":["../../src/types/genai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;GAOG;AACH,MAAM,WAAW,QAAQ;IACvB,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,kCAAkC;IAClC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,mBAAmB;IAClC,4CAA4C;IAC5C,IAAI,EAAE,WAAW,CAAC;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-
*
|
|
4
|
-
* These types represent the standardized format for gen_ai.input.messages
|
|
5
|
-
* and gen_ai.output.messages attributes as per OpenTelemetry Gen AI semantic conventions.
|
|
6
|
-
*
|
|
7
|
-
* @see https://opentelemetry.io/docs/specs/semconv/gen-ai/
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Convert a {@link GenAiAttributes} object to an OTel-compatible attributes
|
|
11
|
-
* dictionary with `gen_ai.*` dot-notation keys.
|
|
12
|
-
*
|
|
13
|
-
* Properties that are `undefined` are omitted from the result.
|
|
14
|
-
*
|
|
15
|
-
* @param attrs - The camelCase {@link GenAiAttributes} to convert.
|
|
16
|
-
* @returns A flat `Record<string, string | number>` with OTel semconv keys.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* const otel = toAttributes({ requestModel: "gpt-4", inputTokens: 42 });
|
|
21
|
-
* // { "gen_ai.request.model": "gpt-4", "gen_ai.usage.input_tokens": 42 }
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
/**
|
|
25
|
-
* Recursively strip `undefined` and `null` values from an object before
|
|
26
|
-
* JSON serialization. Equivalent to Python's `model_dump(exclude_none=True)`.
|
|
27
|
-
*/
|
|
28
|
-
function stripNullish(obj) {
|
|
29
|
-
if (Array.isArray(obj))
|
|
30
|
-
return obj.map(stripNullish);
|
|
31
|
-
if (obj !== null && typeof obj === "object") {
|
|
32
|
-
const out = {};
|
|
33
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
34
|
-
if (value !== undefined && value !== null) {
|
|
35
|
-
out[key] = stripNullish(value);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return out;
|
|
39
|
-
}
|
|
40
|
-
return obj;
|
|
41
|
-
}
|
|
42
|
-
export function toAttributes(attrs) {
|
|
43
|
-
const result = {};
|
|
44
|
-
if (attrs.requestModel !== undefined) {
|
|
45
|
-
result["gen_ai.request.model"] = attrs.requestModel;
|
|
46
|
-
}
|
|
47
|
-
if (attrs.system !== undefined) {
|
|
48
|
-
result["gen_ai.system"] = attrs.system;
|
|
49
|
-
}
|
|
50
|
-
if (attrs.operationName !== undefined) {
|
|
51
|
-
result["gen_ai.operation.name"] = attrs.operationName;
|
|
52
|
-
}
|
|
53
|
-
if (attrs.toolDefinitions !== undefined) {
|
|
54
|
-
result["gen_ai.tool.definitions"] = JSON.stringify(stripNullish(attrs.toolDefinitions));
|
|
55
|
-
}
|
|
56
|
-
if (attrs.inputMessages !== undefined) {
|
|
57
|
-
result["gen_ai.input.messages"] = JSON.stringify(stripNullish(attrs.inputMessages));
|
|
58
|
-
}
|
|
59
|
-
if (attrs.outputMessages !== undefined) {
|
|
60
|
-
result["gen_ai.output.messages"] = JSON.stringify(stripNullish(attrs.outputMessages));
|
|
61
|
-
}
|
|
62
|
-
if (attrs.systemInstructions !== undefined) {
|
|
63
|
-
result["gen_ai.system_instructions"] = JSON.stringify(stripNullish(attrs.systemInstructions));
|
|
64
|
-
}
|
|
65
|
-
if (attrs.responseId !== undefined) {
|
|
66
|
-
result["gen_ai.response.id"] = attrs.responseId;
|
|
67
|
-
}
|
|
68
|
-
if (attrs.inputTokens !== undefined) {
|
|
69
|
-
result["gen_ai.usage.input_tokens"] = attrs.inputTokens;
|
|
70
|
-
}
|
|
71
|
-
if (attrs.outputTokens !== undefined) {
|
|
72
|
-
result["gen_ai.usage.output_tokens"] = attrs.outputTokens;
|
|
73
|
-
}
|
|
74
|
-
if (attrs.cacheCreationInputTokens !== undefined) {
|
|
75
|
-
result["gen_ai.usage.cache_creation.input_tokens"] =
|
|
76
|
-
attrs.cacheCreationInputTokens;
|
|
77
|
-
}
|
|
78
|
-
if (attrs.cacheReadInputTokens !== undefined) {
|
|
79
|
-
result["gen_ai.usage.cache_read.input_tokens"] = attrs.cacheReadInputTokens;
|
|
80
|
-
}
|
|
81
|
-
return result;
|
|
82
|
-
}
|