@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,310 @@
|
|
|
1
|
+
import { propagation, context as otelContext, } from "@opentelemetry/api";
|
|
2
|
+
import { BatchSpanProcessor, } from "@opentelemetry/sdk-trace-base";
|
|
3
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
4
|
+
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
|
|
5
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
|
|
6
|
+
import { ExportResultCode } from "@opentelemetry/core";
|
|
7
|
+
import { randomUUID } from "crypto";
|
|
8
|
+
import { logger } from "./utils.js";
|
|
9
|
+
import { isOpenInferenceSpan, replaceOpenInferenceWithGenAI, isVercelAISpan, convertVercelAIToGenAI, } from "./converters/index.js";
|
|
10
|
+
/**
|
|
11
|
+
* A {@link ReadableSpan} wrapper that substitutes the original attributes with
|
|
12
|
+
* a converted set while delegating every other property to the original span.
|
|
13
|
+
*
|
|
14
|
+
* Used internally by {@link IntrospectionSpanProcessor} to convert
|
|
15
|
+
* OpenInference attributes to Gen AI semantic conventions on-the-fly.
|
|
16
|
+
*/
|
|
17
|
+
class ConvertedReadableSpan {
|
|
18
|
+
_original;
|
|
19
|
+
_convertedAttributes;
|
|
20
|
+
_resource;
|
|
21
|
+
constructor(original, convertedAttributes, resource) {
|
|
22
|
+
this._original = original;
|
|
23
|
+
this._convertedAttributes = convertedAttributes;
|
|
24
|
+
this._resource = resource;
|
|
25
|
+
}
|
|
26
|
+
get name() {
|
|
27
|
+
return this._original.name;
|
|
28
|
+
}
|
|
29
|
+
get kind() {
|
|
30
|
+
return this._original.kind;
|
|
31
|
+
}
|
|
32
|
+
spanContext() {
|
|
33
|
+
return this._original.spanContext();
|
|
34
|
+
}
|
|
35
|
+
get startTime() {
|
|
36
|
+
return this._original.startTime;
|
|
37
|
+
}
|
|
38
|
+
get endTime() {
|
|
39
|
+
return this._original.endTime;
|
|
40
|
+
}
|
|
41
|
+
get status() {
|
|
42
|
+
return this._original.status;
|
|
43
|
+
}
|
|
44
|
+
get links() {
|
|
45
|
+
return this._original.links;
|
|
46
|
+
}
|
|
47
|
+
get events() {
|
|
48
|
+
return this._original.events;
|
|
49
|
+
}
|
|
50
|
+
get duration() {
|
|
51
|
+
return this._original.duration;
|
|
52
|
+
}
|
|
53
|
+
get ended() {
|
|
54
|
+
return this._original.ended;
|
|
55
|
+
}
|
|
56
|
+
get resource() {
|
|
57
|
+
return this._resource ?? this._original.resource;
|
|
58
|
+
}
|
|
59
|
+
get instrumentationScope() {
|
|
60
|
+
return this._original.instrumentationScope;
|
|
61
|
+
}
|
|
62
|
+
get droppedAttributesCount() {
|
|
63
|
+
return this._original.droppedAttributesCount;
|
|
64
|
+
}
|
|
65
|
+
get droppedEventsCount() {
|
|
66
|
+
return this._original.droppedEventsCount;
|
|
67
|
+
}
|
|
68
|
+
get droppedLinksCount() {
|
|
69
|
+
return this._original.droppedLinksCount;
|
|
70
|
+
}
|
|
71
|
+
get attributes() {
|
|
72
|
+
return this._convertedAttributes;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* OTel {@link SpanProcessor} that forwards traces to the Introspection backend
|
|
77
|
+
* via OTLP, automatically converting any OpenInference spans to Gen AI
|
|
78
|
+
* semantic convention attributes.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { IntrospectionSpanProcessor } from "@introspection-sdk/introspection-node";
|
|
83
|
+
*
|
|
84
|
+
* const processor = new IntrospectionSpanProcessor({ token: "sk-intro-…" });
|
|
85
|
+
* tracerProvider.addSpanProcessor(processor);
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export class IntrospectionSpanProcessor {
|
|
89
|
+
_spanProcessor;
|
|
90
|
+
_serviceName;
|
|
91
|
+
_conversationIds = new Map();
|
|
92
|
+
constructor(options = {}) {
|
|
93
|
+
const token = options.token || process.env.INTROSPECTION_TOKEN;
|
|
94
|
+
if (!token) {
|
|
95
|
+
throw new Error("INTROSPECTION_TOKEN is required");
|
|
96
|
+
}
|
|
97
|
+
const serviceName = options.serviceName ||
|
|
98
|
+
process.env.INTROSPECTION_SERVICE_NAME ||
|
|
99
|
+
undefined; // Falls back to TracerProvider's resource
|
|
100
|
+
this._serviceName = serviceName;
|
|
101
|
+
const advanced = options.advanced || {};
|
|
102
|
+
const baseUrl = advanced.baseUrl ||
|
|
103
|
+
process.env.INTROSPECTION_BASE_URL ||
|
|
104
|
+
"https://otel.introspection.dev";
|
|
105
|
+
const headers = {
|
|
106
|
+
Authorization: `Bearer ${token}`,
|
|
107
|
+
...(advanced.additionalHeaders || {}),
|
|
108
|
+
};
|
|
109
|
+
// Construct endpoint URL
|
|
110
|
+
let endpoint;
|
|
111
|
+
if (baseUrl.endsWith("/v1/traces")) {
|
|
112
|
+
endpoint = baseUrl;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
endpoint = `${baseUrl.replace(/\/$/, "")}/v1/traces`;
|
|
116
|
+
}
|
|
117
|
+
logger.info(`IntrospectionSpanProcessor initialized: endpoint=${endpoint}${serviceName ? `, serviceName=${serviceName}` : ""}`);
|
|
118
|
+
// Use custom spanExporter if provided (for testing), otherwise create OTLP exporter
|
|
119
|
+
const baseExporter = advanced.spanExporter ||
|
|
120
|
+
new OTLPTraceExporter({
|
|
121
|
+
url: endpoint,
|
|
122
|
+
headers,
|
|
123
|
+
});
|
|
124
|
+
// Wrap exporter to add v1/v2 compatibility and basic validation
|
|
125
|
+
const exporter = {
|
|
126
|
+
export: (spans, resultCallback) => {
|
|
127
|
+
// Filter and fix spans: ensure they have required properties and add v1 compatibility
|
|
128
|
+
const validSpans = spans
|
|
129
|
+
.filter((span) => {
|
|
130
|
+
// Basic validation: span must have name and resource
|
|
131
|
+
if (!span?.name || !span.resource) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
// Must have either instrumentationLibrary or instrumentationScope
|
|
135
|
+
const compatSpan = span;
|
|
136
|
+
return !!(compatSpan.instrumentationLibrary ||
|
|
137
|
+
compatSpan.instrumentationScope);
|
|
138
|
+
})
|
|
139
|
+
.map((span) => span)
|
|
140
|
+
.map((span) => {
|
|
141
|
+
// Compatibility: add instrumentationLibrary for spans that only have instrumentationScope (v2)
|
|
142
|
+
const compatSpan = span;
|
|
143
|
+
if (compatSpan.instrumentationScope &&
|
|
144
|
+
!compatSpan.instrumentationLibrary) {
|
|
145
|
+
compatSpan.instrumentationLibrary =
|
|
146
|
+
compatSpan.instrumentationScope;
|
|
147
|
+
}
|
|
148
|
+
return span;
|
|
149
|
+
});
|
|
150
|
+
if (validSpans.length === 0) {
|
|
151
|
+
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
// Export with error handling
|
|
155
|
+
try {
|
|
156
|
+
baseExporter.export(validSpans, (result) => {
|
|
157
|
+
if (result.code === ExportResultCode.SUCCESS) {
|
|
158
|
+
logger.debug(`Exported ${validSpans.length} span(s) to ${endpoint}`);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
logger.error(`Failed to export ${validSpans.length} span(s): ${result.error?.message || "Unknown error"}`);
|
|
162
|
+
}
|
|
163
|
+
resultCallback(result);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
logger.error(`Export error: ${error instanceof Error ? error.message : String(error)}`);
|
|
168
|
+
resultCallback({
|
|
169
|
+
code: ExportResultCode.FAILED,
|
|
170
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
shutdown: () => baseExporter.shutdown(),
|
|
175
|
+
forceFlush: () => {
|
|
176
|
+
if (baseExporter.forceFlush) {
|
|
177
|
+
return baseExporter.forceFlush();
|
|
178
|
+
}
|
|
179
|
+
return Promise.resolve();
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
// Use BatchSpanProcessor like logfire does
|
|
183
|
+
this._spanProcessor = new BatchSpanProcessor(exporter);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Called when a new span is started; delegates to the inner batch processor.
|
|
187
|
+
*
|
|
188
|
+
* @param span - The span that was just started.
|
|
189
|
+
* @param parentContext - The parent {@link Context}.
|
|
190
|
+
*/
|
|
191
|
+
onStart(span, parentContext) {
|
|
192
|
+
this._spanProcessor.onStart(span, parentContext);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Called when a span ends. If the span originates from an OpenInference
|
|
196
|
+
* instrumentor, its attributes are converted to `gen_ai.*` semconv keys
|
|
197
|
+
* before being forwarded.
|
|
198
|
+
*
|
|
199
|
+
* @param span - The completed {@link ReadableSpan}.
|
|
200
|
+
*/
|
|
201
|
+
onEnd(span) {
|
|
202
|
+
const compatSpan = span;
|
|
203
|
+
const scopeName = compatSpan.instrumentationScope?.name ||
|
|
204
|
+
compatSpan.instrumentationLibrary?.name;
|
|
205
|
+
const isOI = isOpenInferenceSpan(scopeName) ||
|
|
206
|
+
typeof span.attributes["openinference.span.kind"] === "string";
|
|
207
|
+
const isVercel = isVercelAISpan(span.attributes);
|
|
208
|
+
// Skip Vercel AI SDK wrapper spans (ai.streamText, ai.generateText).
|
|
209
|
+
// They duplicate the child doStream/doGenerate output without proper
|
|
210
|
+
// input message windows, causing empty user messages in conversation view.
|
|
211
|
+
// This applies regardless of whether OI attributes are also present.
|
|
212
|
+
const aiOperationId = span.attributes["ai.operationId"];
|
|
213
|
+
if (typeof aiOperationId === "string" &&
|
|
214
|
+
!String(aiOperationId).includes(".do")) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
// Skip spans that have no LLM-relevant data — they are infrastructure spans
|
|
218
|
+
// (e.g. Next.js route resolution, HTTP spans) that should not be exported.
|
|
219
|
+
const hasGenAi = isOI ||
|
|
220
|
+
isVercel ||
|
|
221
|
+
span.attributes["gen_ai.system"] != null ||
|
|
222
|
+
span.attributes["gen_ai.operation.name"] != null ||
|
|
223
|
+
span.attributes["gen_ai.request.model"] != null ||
|
|
224
|
+
span.attributes["gen_ai.input.messages"] != null ||
|
|
225
|
+
span.attributes["gen_ai.output.messages"] != null;
|
|
226
|
+
if (!hasGenAi)
|
|
227
|
+
return;
|
|
228
|
+
let attrs;
|
|
229
|
+
if (isOI) {
|
|
230
|
+
attrs = {
|
|
231
|
+
...replaceOpenInferenceWithGenAI(span.attributes),
|
|
232
|
+
};
|
|
233
|
+
// When both OI and Vercel attrs present, merge Vercel-specific
|
|
234
|
+
// enrichments (conversation ID from metadata, etc.)
|
|
235
|
+
if (isVercel) {
|
|
236
|
+
const vercelAttrs = convertVercelAIToGenAI(span.attributes);
|
|
237
|
+
for (const [key, value] of Object.entries(vercelAttrs)) {
|
|
238
|
+
// Prefer Vercel output messages when they contain reasoning
|
|
239
|
+
if (key === "gen_ai.output.messages" &&
|
|
240
|
+
typeof value === "string" &&
|
|
241
|
+
value.includes('"thinking"')) {
|
|
242
|
+
attrs[key] = value;
|
|
243
|
+
}
|
|
244
|
+
else if (attrs[key] === undefined) {
|
|
245
|
+
attrs[key] = value;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
logger.debug(`Converting OpenInference span: ${span.name}`);
|
|
250
|
+
}
|
|
251
|
+
else if (isVercel) {
|
|
252
|
+
attrs = {
|
|
253
|
+
...span.attributes,
|
|
254
|
+
...convertVercelAIToGenAI(span.attributes),
|
|
255
|
+
};
|
|
256
|
+
logger.debug(`Converting Vercel AI span: ${span.name}`);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
attrs = { ...span.attributes };
|
|
260
|
+
}
|
|
261
|
+
// Read baggage from active context
|
|
262
|
+
const baggage = propagation.getBaggage(otelContext.active());
|
|
263
|
+
// Conversation ID: baggage > existing attr > auto-generate per trace
|
|
264
|
+
const baggageConvId = baggage?.getEntry("gen_ai.conversation.id")?.value;
|
|
265
|
+
const existingConvId = attrs["gen_ai.conversation.id"];
|
|
266
|
+
if (baggageConvId) {
|
|
267
|
+
attrs["gen_ai.conversation.id"] = baggageConvId;
|
|
268
|
+
}
|
|
269
|
+
else if (!existingConvId) {
|
|
270
|
+
const traceId = span.spanContext().traceId;
|
|
271
|
+
if (!this._conversationIds.has(traceId)) {
|
|
272
|
+
this._conversationIds.set(traceId, `intro_conv_${randomUUID().replace(/-/g, "")}`);
|
|
273
|
+
}
|
|
274
|
+
attrs["gen_ai.conversation.id"] = this._conversationIds.get(traceId);
|
|
275
|
+
}
|
|
276
|
+
// Default gen_ai.operation.name to "chat" for spans with messages but no operation name
|
|
277
|
+
if (!attrs["gen_ai.operation.name"] &&
|
|
278
|
+
(attrs["gen_ai.input.messages"] || attrs["gen_ai.output.messages"])) {
|
|
279
|
+
attrs["gen_ai.operation.name"] = "chat";
|
|
280
|
+
}
|
|
281
|
+
// Agent name from baggage
|
|
282
|
+
const baggageAgentName = baggage?.getEntry("gen_ai.agent.name")?.value;
|
|
283
|
+
if (baggageAgentName && !attrs["gen_ai.agent.name"]) {
|
|
284
|
+
attrs["gen_ai.agent.name"] = baggageAgentName;
|
|
285
|
+
}
|
|
286
|
+
// Override resource with service name if provided
|
|
287
|
+
let resource;
|
|
288
|
+
if (this._serviceName) {
|
|
289
|
+
resource = span.resource.merge(resourceFromAttributes({ [ATTR_SERVICE_NAME]: this._serviceName }));
|
|
290
|
+
}
|
|
291
|
+
const processedSpan = new ConvertedReadableSpan(span, attrs, resource);
|
|
292
|
+
this._spanProcessor.onEnd(processedSpan);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Shut down the inner batch processor and its exporter.
|
|
296
|
+
*
|
|
297
|
+
* @returns A promise that resolves once all pending spans are flushed.
|
|
298
|
+
*/
|
|
299
|
+
async shutdown() {
|
|
300
|
+
await this._spanProcessor.shutdown();
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Force-flush any buffered spans to the Introspection backend.
|
|
304
|
+
*
|
|
305
|
+
* @returns A promise that resolves once the flush completes.
|
|
306
|
+
*/
|
|
307
|
+
async forceFlush() {
|
|
308
|
+
await this._spanProcessor.forceFlush();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
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 { type SpanExporter, type IdGenerator } from "@opentelemetry/sdk-trace-base";
|
|
8
|
+
import type { TracingProcessor, Trace, Span, SpanData } from "@openai/agents";
|
|
9
|
+
/**
|
|
10
|
+
* Advanced options for testing and customization of
|
|
11
|
+
* {@link IntrospectionTracingProcessor}.
|
|
12
|
+
*/
|
|
13
|
+
export interface TracingProcessorAdvancedOptions {
|
|
14
|
+
/** Custom span exporter (for testing - use InMemorySpanExporter) */
|
|
15
|
+
spanExporter?: SpanExporter;
|
|
16
|
+
/** Custom ID generator (for testing - use IncrementalIdGenerator) */
|
|
17
|
+
idGenerator?: IdGenerator;
|
|
18
|
+
/** Use SimpleSpanProcessor instead of BatchSpanProcessor (for testing) */
|
|
19
|
+
useSimpleSpanProcessor?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Maximum number of spans to export in a single batch.
|
|
22
|
+
* Set to 1 to export each span individually on end, ensuring sequential
|
|
23
|
+
* processing by the backend (useful for multi-turn conversations where
|
|
24
|
+
* each turn must be ingested before the next arrives).
|
|
25
|
+
* Defaults to the OTel SDK default (512).
|
|
26
|
+
*/
|
|
27
|
+
maxExportBatchSize?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Delay interval in milliseconds between batch exports.
|
|
30
|
+
* Lower values reduce latency but increase network requests.
|
|
31
|
+
* Defaults to 1000.
|
|
32
|
+
*/
|
|
33
|
+
scheduledDelayMillis?: number;
|
|
34
|
+
}
|
|
35
|
+
/** Configuration for {@link IntrospectionTracingProcessor}. */
|
|
36
|
+
export interface IntrospectionTracingProcessorOptions {
|
|
37
|
+
/** Authentication token (env: INTROSPECTION_TOKEN) */
|
|
38
|
+
token?: string;
|
|
39
|
+
/** Base URL for the API (env: INTROSPECTION_BASE_URL, default: "https://otel.introspection.dev") */
|
|
40
|
+
baseUrl?: string;
|
|
41
|
+
/** Service name for telemetry (env: INTROSPECTION_SERVICE_NAME) */
|
|
42
|
+
serviceName?: string;
|
|
43
|
+
/** Additional headers to include in requests */
|
|
44
|
+
additionalHeaders?: Record<string, string>;
|
|
45
|
+
/** Advanced options for testing and customization */
|
|
46
|
+
advanced?: TracingProcessorAdvancedOptions;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* {@link TracingProcessor} that forwards OpenAI agent traces to the
|
|
50
|
+
* Introspection backend via OTLP.
|
|
51
|
+
*
|
|
52
|
+
* Extracts OTel Gen AI semantic convention attributes from span data:
|
|
53
|
+
* - Agent spans: `gen_ai.agent.name`, `gen_ai.tool.definitions`, `gen_ai.agent.handoffs`
|
|
54
|
+
* - Function spans: `gen_ai.tool.name`, `gen_ai.tool.input`, `gen_ai.tool.output`
|
|
55
|
+
* - Response spans: `gen_ai.input/output.messages`, `gen_ai.usage.*`, `gen_ai.request.model`
|
|
56
|
+
* - Generation spans: `gen_ai.request.model`, `gen_ai.usage.*`
|
|
57
|
+
* - Handoff spans: `gen_ai.handoff.from_agent`, `gen_ai.handoff.to_agent`
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* import { IntrospectionTracingProcessor } from "@introspection-sdk/introspection-node";
|
|
62
|
+
*
|
|
63
|
+
* const processor = new IntrospectionTracingProcessor({ token: "sk-intro-…" });
|
|
64
|
+
* // pass processor to the OpenAI Agents SDK withTrace() or registerProcessor()
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare class IntrospectionTracingProcessor implements TracingProcessor {
|
|
68
|
+
private _tracerProvider;
|
|
69
|
+
private _tracer;
|
|
70
|
+
private _spans;
|
|
71
|
+
private _conversationIds;
|
|
72
|
+
constructor(options?: IntrospectionTracingProcessorOptions);
|
|
73
|
+
/**
|
|
74
|
+
* Called when the processor is started. Optional lifecycle hook.
|
|
75
|
+
*/
|
|
76
|
+
start(): void;
|
|
77
|
+
/**
|
|
78
|
+
* Called when a trace starts. Creates a root OTel span.
|
|
79
|
+
*
|
|
80
|
+
* @param trace - The OpenAI Agents SDK trace object.
|
|
81
|
+
*/
|
|
82
|
+
onTraceStart(trace: Trace): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Called when a trace ends. Closes the root OTel span.
|
|
85
|
+
*
|
|
86
|
+
* @param trace - The OpenAI Agents SDK trace object.
|
|
87
|
+
*/
|
|
88
|
+
onTraceEnd(trace: Trace): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Called when a span starts. Creates a child OTel span with parent context.
|
|
91
|
+
*
|
|
92
|
+
* @param span - The OpenAI Agents SDK span.
|
|
93
|
+
*/
|
|
94
|
+
onSpanStart(span: Span<SpanData>): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Called when a span ends. Extracts `gen_ai.*` attributes from the span
|
|
97
|
+
* data and sets them on the corresponding OTel span before ending it.
|
|
98
|
+
*
|
|
99
|
+
* @param span - The OpenAI Agents SDK span.
|
|
100
|
+
*/
|
|
101
|
+
onSpanEnd(span: Span<SpanData>): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* Extract attributes from agent spans.
|
|
104
|
+
*/
|
|
105
|
+
private _processAgentSpan;
|
|
106
|
+
/**
|
|
107
|
+
* Extract attributes from function/tool spans.
|
|
108
|
+
*/
|
|
109
|
+
private _processFunctionSpan;
|
|
110
|
+
/**
|
|
111
|
+
* Extract attributes from response spans.
|
|
112
|
+
* Matches Python implementation - extracts from both spanData and _response object.
|
|
113
|
+
*/
|
|
114
|
+
private _processResponseSpan;
|
|
115
|
+
/**
|
|
116
|
+
* Extract attributes from generation spans.
|
|
117
|
+
*/
|
|
118
|
+
private _processGenerationSpan;
|
|
119
|
+
/**
|
|
120
|
+
* Extract attributes from handoff spans.
|
|
121
|
+
*/
|
|
122
|
+
private _processHandoffSpan;
|
|
123
|
+
/**
|
|
124
|
+
* Shut down the tracer provider, flushing all pending spans.
|
|
125
|
+
*
|
|
126
|
+
* @returns A promise that resolves once shutdown is complete.
|
|
127
|
+
*/
|
|
128
|
+
shutdown(): Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Force-flush any buffered spans to the Introspection backend.
|
|
131
|
+
*
|
|
132
|
+
* @returns A promise that resolves once the flush completes.
|
|
133
|
+
*/
|
|
134
|
+
forceFlush(): Promise<void>;
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=tracing-processor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracing-processor.d.ts","sourceRoot":"","sources":["../src/tracing-processor.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,OAAO,EAIL,KAAK,YAAY,EACjB,KAAK,WAAW,EACjB,MAAM,+BAA+B,CAAC;AAoBvC,OAAO,KAAK,EACV,gBAAgB,EAChB,KAAK,EACL,IAAI,EACJ,QAAQ,EAMT,MAAM,gBAAgB,CAAC;AAExB;;;GAGG;AACH,MAAM,WAAW,+BAA+B;IAC9C,oEAAoE;IACpE,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,qEAAqE;IACrE,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,0EAA0E;IAC1E,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC;;;;;;OAMG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,+DAA+D;AAC/D,MAAM,WAAW,oCAAoC;IACnD,sDAAsD;IACtD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,oGAAoG;IACpG,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mEAAmE;IACnE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gDAAgD;IAChD,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3C,qDAAqD;IACrD,QAAQ,CAAC,EAAE,+BAA+B,CAAC;CAC5C;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,6BAA8B,YAAW,gBAAgB;IACpE,OAAO,CAAC,eAAe,CAAsB;IAC7C,OAAO,CAAC,OAAO,CAA+C;IAC9D,OAAO,CAAC,MAAM,CAAoC;IAClD,OAAO,CAAC,gBAAgB,CAAkC;gBAE9C,OAAO,GAAE,oCAAyC;IAyF9D;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;;;OAIG;IACG,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;IAW/C;;;;OAIG;IACG,UAAU,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;IAa7C;;;;OAIG;IACG,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IA4BtD;;;;;OAKG;IACG,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IA6CpD;;OAEG;IACH,OAAO,CAAC,iBAAiB;IA0BzB;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAgB5B;;;OAGG;IACH,OAAO,CAAC,oBAAoB;IA0F5B;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAwC9B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAY3B;;;;OAIG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAI/B;;;;OAIG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;CAGlC"}
|