@introspection-sdk/introspection-node 0.19.1 → 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.
- package/README.md +28 -8
- package/dist/auth.d.ts +1 -1
- package/dist/client.d.ts +4 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +16 -7
- package/dist/client.js.map +1 -1
- package/dist/dev-target.js +1 -1
- package/dist/index.d.ts +4 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -6
- package/dist/index.js.map +1 -1
- package/dist/otel/index.d.ts +1 -14
- package/dist/otel/index.d.ts.map +1 -1
- package/dist/otel/index.js +10 -18
- package/dist/otel/index.js.map +1 -1
- package/dist/otel/init.d.ts +19 -21
- package/dist/otel/init.d.ts.map +1 -1
- package/dist/otel/init.js +62 -48
- package/dist/otel/init.js.map +1 -1
- package/dist/otel/integrations/base.d.ts +17 -37
- package/dist/otel/integrations/base.d.ts.map +1 -1
- package/dist/otel/integrations/base.js +6 -13
- package/dist/otel/integrations/base.js.map +1 -1
- package/dist/otel/integrations/index.d.ts +3 -4
- package/dist/otel/integrations/index.d.ts.map +1 -1
- package/dist/otel/integrations/index.js +6 -25
- package/dist/otel/integrations/index.js.map +1 -1
- package/dist/otel/integrations/pi.d.ts.map +1 -1
- package/dist/otel/integrations/pi.js +9 -1
- package/dist/otel/integrations/pi.js.map +1 -1
- package/dist/otel/logs.d.ts +41 -8
- package/dist/otel/logs.d.ts.map +1 -1
- package/dist/otel/logs.js +95 -66
- package/dist/otel/logs.js.map +1 -1
- package/dist/otel/pi.d.ts +18 -5
- package/dist/otel/pi.d.ts.map +1 -1
- package/dist/otel/pi.js +38 -9
- package/dist/otel/pi.js.map +1 -1
- package/dist/otel/register.d.ts +2 -2
- package/dist/otel/register.d.ts.map +1 -1
- package/dist/otel/register.js +7 -9
- package/dist/otel/register.js.map +1 -1
- package/dist/otel/setup.d.ts +2 -2
- package/dist/otel/setup.d.ts.map +1 -1
- package/dist/otel/setup.js +16 -6
- package/dist/otel/setup.js.map +1 -1
- package/dist/otel/span-processor.d.ts +18 -5
- package/dist/otel/span-processor.d.ts.map +1 -1
- package/dist/otel/span-processor.js +50 -65
- package/dist/otel/span-processor.js.map +1 -1
- package/dist/resources/experiments.d.ts +10 -5
- package/dist/resources/experiments.d.ts.map +1 -1
- package/dist/resources/experiments.js +9 -25
- package/dist/resources/experiments.js.map +1 -1
- package/dist/resources/recipes.d.ts +6 -6
- package/dist/resources/recipes.d.ts.map +1 -1
- package/dist/resources/recipes.js +5 -23
- package/dist/resources/recipes.js.map +1 -1
- package/dist/resources/runtimes.d.ts +2 -2
- package/dist/resources/runtimes.d.ts.map +1 -1
- package/dist/resources/runtimes.js +2 -2
- package/dist/resources/runtimes.js.map +1 -1
- package/dist/runner.d.ts +8 -4
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +26 -18
- package/dist/runner.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils.d.ts +45 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +61 -2
- package/dist/utils.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +18 -60
- package/.turbo/turbo-build.log +0 -4
- package/dist/converters/claude.d.ts +0 -150
- package/dist/converters/claude.d.ts.map +0 -1
- package/dist/converters/claude.js +0 -211
- package/dist/converters/claude.js.map +0 -1
- package/dist/converters/gemini.d.ts +0 -90
- package/dist/converters/gemini.d.ts.map +0 -1
- package/dist/converters/gemini.js +0 -171
- package/dist/converters/gemini.js.map +0 -1
- package/dist/converters/index.d.ts +0 -9
- package/dist/converters/index.d.ts.map +0 -1
- package/dist/converters/index.js +0 -9
- package/dist/converters/index.js.map +0 -1
- package/dist/converters/openai.d.ts +0 -51
- package/dist/converters/openai.d.ts.map +0 -1
- package/dist/converters/openai.js +0 -269
- package/dist/converters/openai.js.map +0 -1
- package/dist/converters/openinference.d.ts +0 -107
- package/dist/converters/openinference.d.ts.map +0 -1
- package/dist/converters/openinference.js +0 -553
- package/dist/converters/openinference.js.map +0 -1
- package/dist/converters/vercel.d.ts +0 -21
- package/dist/converters/vercel.d.ts.map +0 -1
- package/dist/converters/vercel.js +0 -236
- package/dist/converters/vercel.js.map +0 -1
- package/dist/otel/anthropic.d.ts +0 -63
- package/dist/otel/anthropic.d.ts.map +0 -1
- package/dist/otel/anthropic.js +0 -475
- package/dist/otel/anthropic.js.map +0 -1
- package/dist/otel/claude-hooks.d.ts +0 -346
- package/dist/otel/claude-hooks.d.ts.map +0 -1
- package/dist/otel/claude-hooks.js +0 -647
- package/dist/otel/claude-hooks.js.map +0 -1
- package/dist/otel/claude-wrapper.d.ts +0 -93
- package/dist/otel/claude-wrapper.d.ts.map +0 -1
- package/dist/otel/claude-wrapper.js +0 -155
- package/dist/otel/claude-wrapper.js.map +0 -1
- package/dist/otel/gemini.d.ts +0 -56
- package/dist/otel/gemini.d.ts.map +0 -1
- package/dist/otel/gemini.js +0 -350
- package/dist/otel/gemini.js.map +0 -1
- package/dist/otel/integrations/anthropic.d.ts +0 -10
- package/dist/otel/integrations/anthropic.d.ts.map +0 -1
- package/dist/otel/integrations/anthropic.js +0 -26
- package/dist/otel/integrations/anthropic.js.map +0 -1
- package/dist/otel/integrations/claude-agent.d.ts +0 -12
- package/dist/otel/integrations/claude-agent.d.ts.map +0 -1
- package/dist/otel/integrations/claude-agent.js +0 -28
- package/dist/otel/integrations/claude-agent.js.map +0 -1
- package/dist/otel/integrations/gemini.d.ts +0 -11
- package/dist/otel/integrations/gemini.d.ts.map +0 -1
- package/dist/otel/integrations/gemini.js +0 -23
- package/dist/otel/integrations/gemini.js.map +0 -1
- package/dist/otel/integrations/langchain.d.ts +0 -15
- package/dist/otel/integrations/langchain.d.ts.map +0 -1
- package/dist/otel/integrations/langchain.js +0 -43
- package/dist/otel/integrations/langchain.js.map +0 -1
- package/dist/otel/integrations/mastra.d.ts +0 -14
- package/dist/otel/integrations/mastra.d.ts.map +0 -1
- package/dist/otel/integrations/mastra.js +0 -31
- package/dist/otel/integrations/mastra.js.map +0 -1
- package/dist/otel/integrations/openai-agents.d.ts +0 -11
- package/dist/otel/integrations/openai-agents.d.ts.map +0 -1
- package/dist/otel/integrations/openai-agents.js +0 -30
- package/dist/otel/integrations/openai-agents.js.map +0 -1
- package/dist/otel/integrations/vercel.d.ts +0 -14
- package/dist/otel/integrations/vercel.d.ts.map +0 -1
- package/dist/otel/integrations/vercel.js +0 -20
- package/dist/otel/integrations/vercel.js.map +0 -1
- package/dist/otel/langchain-handler.d.ts +0 -112
- package/dist/otel/langchain-handler.d.ts.map +0 -1
- package/dist/otel/langchain-handler.js +0 -561
- package/dist/otel/langchain-handler.js.map +0 -1
- package/dist/otel/mastra-exporter.d.ts +0 -87
- package/dist/otel/mastra-exporter.d.ts.map +0 -1
- package/dist/otel/mastra-exporter.js +0 -530
- package/dist/otel/mastra-exporter.js.map +0 -1
- package/dist/otel/openai.d.ts +0 -20
- package/dist/otel/openai.d.ts.map +0 -1
- package/dist/otel/openai.js +0 -55
- package/dist/otel/openai.js.map +0 -1
- package/dist/otel/tracing-processor.d.ts +0 -139
- package/dist/otel/tracing-processor.d.ts.map +0 -1
- package/dist/otel/tracing-processor.js +0 -434
- package/dist/otel/tracing-processor.js.map +0 -1
- package/eslint.config.mjs +0 -4
- package/src/auth.ts +0 -236
- package/src/client.ts +0 -163
- package/src/converters/claude.ts +0 -343
- package/src/converters/gemini.ts +0 -243
- package/src/converters/index.ts +0 -50
- package/src/converters/openai.ts +0 -300
- package/src/converters/openinference.ts +0 -665
- package/src/converters/vercel.ts +0 -302
- package/src/dev-target.ts +0 -79
- package/src/http.ts +0 -43
- package/src/index.ts +0 -266
- package/src/otel/anthropic.ts +0 -619
- package/src/otel/claude-hooks.ts +0 -1116
- package/src/otel/claude-wrapper.ts +0 -239
- package/src/otel/gemini.ts +0 -483
- package/src/otel/index.ts +0 -128
- package/src/otel/init.ts +0 -411
- package/src/otel/integrations/anthropic.ts +0 -36
- package/src/otel/integrations/base.ts +0 -158
- package/src/otel/integrations/claude-agent.ts +0 -36
- package/src/otel/integrations/gemini.ts +0 -30
- package/src/otel/integrations/index.ts +0 -152
- package/src/otel/integrations/langchain.ts +0 -60
- package/src/otel/integrations/mastra.ts +0 -48
- package/src/otel/integrations/openai-agents.ts +0 -47
- package/src/otel/integrations/pi.ts +0 -28
- package/src/otel/integrations/vercel.ts +0 -26
- package/src/otel/langchain-handler.ts +0 -804
- package/src/otel/logs.ts +0 -395
- package/src/otel/mastra-exporter.ts +0 -714
- package/src/otel/openai.ts +0 -93
- package/src/otel/pi.ts +0 -94
- package/src/otel/register.ts +0 -50
- package/src/otel/setup.ts +0 -215
- package/src/otel/span-processor.ts +0 -512
- package/src/otel/tracing-processor.ts +0 -637
- package/src/pagination.ts +0 -10
- package/src/proxy-from-env.d.ts +0 -7
- package/src/resources/experiments.ts +0 -176
- package/src/resources/recipes.ts +0 -74
- package/src/resources/runtimes.ts +0 -31
- package/src/runner.ts +0 -213
- package/src/types.ts +0 -13
- package/src/utils.ts +0 -95
- package/src/version.ts +0 -2
- package/tsconfig.json +0 -8
|
@@ -1,637 +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
|
-
|
|
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 { providerCostAttributes } from "@introspection-sdk/types";
|
|
36
|
-
|
|
37
|
-
import { logger, withOtlpHttpsProxy } from "../utils.js";
|
|
38
|
-
import { VERSION } from "../version.js";
|
|
39
|
-
|
|
40
|
-
// Type imports from @openai/agents
|
|
41
|
-
import type {
|
|
42
|
-
TracingProcessor,
|
|
43
|
-
Trace,
|
|
44
|
-
Span,
|
|
45
|
-
SpanData,
|
|
46
|
-
AgentSpanData,
|
|
47
|
-
FunctionSpanData,
|
|
48
|
-
GenerationSpanData,
|
|
49
|
-
ResponseSpanData,
|
|
50
|
-
HandoffSpanData,
|
|
51
|
-
} from "@openai/agents";
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Advanced options for testing and customization of
|
|
55
|
-
* {@link IntrospectionTracingProcessor}.
|
|
56
|
-
*/
|
|
57
|
-
export interface TracingProcessorAdvancedOptions {
|
|
58
|
-
/** Custom span exporter (for testing - use InMemorySpanExporter) */
|
|
59
|
-
spanExporter?: SpanExporter;
|
|
60
|
-
/** Custom ID generator (for testing - use IncrementalIdGenerator) */
|
|
61
|
-
idGenerator?: IdGenerator;
|
|
62
|
-
/** Use SimpleSpanProcessor instead of BatchSpanProcessor (for testing) */
|
|
63
|
-
useSimpleSpanProcessor?: boolean;
|
|
64
|
-
/**
|
|
65
|
-
* Maximum number of spans to export in a single batch.
|
|
66
|
-
* Set to 1 to export each span individually on end, ensuring sequential
|
|
67
|
-
* processing by the backend (useful for multi-turn conversations where
|
|
68
|
-
* each turn must be ingested before the next arrives).
|
|
69
|
-
* Defaults to the OTel SDK default (512).
|
|
70
|
-
*/
|
|
71
|
-
maxExportBatchSize?: number;
|
|
72
|
-
/**
|
|
73
|
-
* Delay interval in milliseconds between batch exports.
|
|
74
|
-
* Lower values reduce latency but increase network requests.
|
|
75
|
-
* Defaults to 1000.
|
|
76
|
-
*/
|
|
77
|
-
scheduledDelayMillis?: number;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Configuration for {@link IntrospectionTracingProcessor}. */
|
|
81
|
-
export interface IntrospectionTracingProcessorOptions {
|
|
82
|
-
/** Authentication token (env: INTROSPECTION_TOKEN) */
|
|
83
|
-
token?: string;
|
|
84
|
-
/** Base URL for the API (env: INTROSPECTION_BASE_OTEL_URL, default: "https://otel.introspection.dev") */
|
|
85
|
-
baseUrl?: string;
|
|
86
|
-
/** Service name for telemetry (env: INTROSPECTION_SERVICE_NAME) */
|
|
87
|
-
serviceName?: string;
|
|
88
|
-
/** Additional headers to include in requests */
|
|
89
|
-
additionalHeaders?: Record<string, string>;
|
|
90
|
-
/** Advanced options for testing and customization */
|
|
91
|
-
advanced?: TracingProcessorAdvancedOptions;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* {@link TracingProcessor} that forwards OpenAI agent traces to the
|
|
96
|
-
* Introspection backend via OTLP.
|
|
97
|
-
*
|
|
98
|
-
* Extracts OTel Gen AI semantic convention attributes from span data:
|
|
99
|
-
* - Agent spans: `gen_ai.agent.name`, `gen_ai.tool.definitions`, `gen_ai.agent.handoffs`
|
|
100
|
-
* - Function spans: `gen_ai.tool.name`, `gen_ai.tool.call.arguments`, `gen_ai.tool.call.result`
|
|
101
|
-
* - Response spans: `gen_ai.input/output.messages`, `gen_ai.usage.*`, `gen_ai.request.model`
|
|
102
|
-
* - Generation spans: `gen_ai.request.model`, `gen_ai.usage.*`
|
|
103
|
-
* - Handoff spans: `gen_ai.handoff.from_agent`, `gen_ai.handoff.to_agent`
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```ts
|
|
107
|
-
* import { IntrospectionTracingProcessor } from "@introspection-sdk/introspection-node";
|
|
108
|
-
*
|
|
109
|
-
* const processor = new IntrospectionTracingProcessor({ token: "sk-intro-…" });
|
|
110
|
-
* // pass processor to the OpenAI Agents SDK withTrace() or registerProcessor()
|
|
111
|
-
* ```
|
|
112
|
-
*/
|
|
113
|
-
export class IntrospectionTracingProcessor implements TracingProcessor {
|
|
114
|
-
private _tracerProvider: BasicTracerProvider;
|
|
115
|
-
private _tracer: ReturnType<BasicTracerProvider["getTracer"]>;
|
|
116
|
-
private _spans: Map<string, OtelSpan> = new Map();
|
|
117
|
-
private _conversationIds: Map<string, string> = new Map();
|
|
118
|
-
|
|
119
|
-
constructor(options: IntrospectionTracingProcessorOptions = {}) {
|
|
120
|
-
const advanced = options.advanced;
|
|
121
|
-
const serviceName =
|
|
122
|
-
options.serviceName || process.env.INTROSPECTION_SERVICE_NAME;
|
|
123
|
-
const resource = serviceName
|
|
124
|
-
? resourceFromAttributes({ [ATTR_SERVICE_NAME]: serviceName })
|
|
125
|
-
: undefined;
|
|
126
|
-
|
|
127
|
-
// If custom exporter is provided (testing mode), skip token validation
|
|
128
|
-
if (!advanced?.spanExporter) {
|
|
129
|
-
const token = options.token || process.env.INTROSPECTION_TOKEN;
|
|
130
|
-
if (!token) {
|
|
131
|
-
throw new Error("INTROSPECTION_TOKEN is required");
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const baseUrl =
|
|
135
|
-
options.baseUrl ||
|
|
136
|
-
process.env.INTROSPECTION_BASE_OTEL_URL ||
|
|
137
|
-
"https://otel.introspection.dev";
|
|
138
|
-
|
|
139
|
-
const headers: Record<string, string> = {
|
|
140
|
-
"User-Agent": `introspection-sdk/${VERSION}`,
|
|
141
|
-
Authorization: `Bearer ${token}`,
|
|
142
|
-
...options.additionalHeaders,
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
// Construct endpoint URL
|
|
146
|
-
const endpoint = baseUrl.endsWith("/v1/traces")
|
|
147
|
-
? baseUrl
|
|
148
|
-
: `${baseUrl.replace(/\/$/, "")}/v1/traces`;
|
|
149
|
-
|
|
150
|
-
logger.info(
|
|
151
|
-
`IntrospectionTracingProcessor initialized: endpoint=${endpoint}`,
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
// Setup OTLP exporter
|
|
155
|
-
const spanExporter = new OTLPTraceExporter(
|
|
156
|
-
withOtlpHttpsProxy({
|
|
157
|
-
url: endpoint,
|
|
158
|
-
headers,
|
|
159
|
-
}),
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
// Use SimpleSpanProcessor for maxExportBatchSize=1 (sequential export),
|
|
163
|
-
// otherwise BatchSpanProcessor with configurable batch size.
|
|
164
|
-
// Default to sequential export for dev/staging tokens.
|
|
165
|
-
const effectiveBatchSize =
|
|
166
|
-
advanced?.maxExportBatchSize ??
|
|
167
|
-
(token.startsWith("intro_dev") || token.startsWith("intro_staging")
|
|
168
|
-
? 1
|
|
169
|
-
: undefined);
|
|
170
|
-
const useSimple =
|
|
171
|
-
advanced?.useSimpleSpanProcessor || effectiveBatchSize === 1;
|
|
172
|
-
const scheduledDelayMillis = advanced?.scheduledDelayMillis ?? 1000;
|
|
173
|
-
const spanProcessor = useSimple
|
|
174
|
-
? new SimpleSpanProcessor(spanExporter)
|
|
175
|
-
: new BatchSpanProcessor(spanExporter, {
|
|
176
|
-
scheduledDelayMillis,
|
|
177
|
-
...(effectiveBatchSize
|
|
178
|
-
? { maxExportBatchSize: effectiveBatchSize }
|
|
179
|
-
: {}),
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
this._tracerProvider = new BasicTracerProvider({
|
|
183
|
-
resource,
|
|
184
|
-
idGenerator: advanced?.idGenerator,
|
|
185
|
-
spanProcessors: [spanProcessor],
|
|
186
|
-
});
|
|
187
|
-
} else {
|
|
188
|
-
// Testing mode: use provided exporter
|
|
189
|
-
logger.info(
|
|
190
|
-
`IntrospectionTracingProcessor initialized in test mode with custom exporter`,
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
// Choose span processor based on options
|
|
194
|
-
const spanProcessor = advanced.useSimpleSpanProcessor
|
|
195
|
-
? new SimpleSpanProcessor(advanced.spanExporter)
|
|
196
|
-
: new BatchSpanProcessor(advanced.spanExporter, {
|
|
197
|
-
scheduledDelayMillis: 100,
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
this._tracerProvider = new BasicTracerProvider({
|
|
201
|
-
resource,
|
|
202
|
-
idGenerator: advanced.idGenerator,
|
|
203
|
-
spanProcessors: [spanProcessor],
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
this._tracer = this._tracerProvider.getTracer("openai-agents", VERSION);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Called when the processor is started. Optional lifecycle hook.
|
|
212
|
-
*/
|
|
213
|
-
start(): void {
|
|
214
|
-
// No-op - processor is ready after construction
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Called when a trace starts. Creates a root OTel span.
|
|
219
|
-
*
|
|
220
|
-
* @param trace - The OpenAI Agents SDK trace object.
|
|
221
|
-
*/
|
|
222
|
-
async onTraceStart(trace: Trace): Promise<void> {
|
|
223
|
-
logger.debug(`onTraceStart called: ${trace?.name} (${trace?.traceId})`);
|
|
224
|
-
if (trace) {
|
|
225
|
-
// Use groupId as conversationId when provided so callers can link
|
|
226
|
-
// multiple traces (e.g. multi-turn orchestrator runs) into one conversation.
|
|
227
|
-
const conversationId =
|
|
228
|
-
trace.groupId ?? `intro_conv_${randomUUID().replace(/-/g, "")}`;
|
|
229
|
-
this._conversationIds.set(trace.traceId, conversationId);
|
|
230
|
-
const otelSpan = this._tracer.startSpan(trace.name);
|
|
231
|
-
this._spans.set(trace.traceId, otelSpan);
|
|
232
|
-
logger.debug(`Created root span for trace: ${trace.traceId}`);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Called when a trace ends. Closes the root OTel span.
|
|
238
|
-
*
|
|
239
|
-
* @param trace - The OpenAI Agents SDK trace object.
|
|
240
|
-
*/
|
|
241
|
-
async onTraceEnd(trace: Trace): Promise<void> {
|
|
242
|
-
logger.debug(`onTraceEnd called: ${trace?.name} (${trace?.traceId})`);
|
|
243
|
-
if (trace) {
|
|
244
|
-
const otelSpan = this._spans.get(trace.traceId);
|
|
245
|
-
if (otelSpan) {
|
|
246
|
-
this._spans.delete(trace.traceId);
|
|
247
|
-
this._conversationIds.delete(trace.traceId);
|
|
248
|
-
otelSpan.end();
|
|
249
|
-
logger.debug(`Ended root span for trace: ${trace.traceId}`);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Called when a span starts. Creates a child OTel span with parent context.
|
|
256
|
-
*
|
|
257
|
-
* @param span - The OpenAI Agents SDK span.
|
|
258
|
-
*/
|
|
259
|
-
async onSpanStart(span: Span<SpanData>): Promise<void> {
|
|
260
|
-
logger.debug(
|
|
261
|
-
`onSpanStart called: ${span?.spanId} (type: ${(span?.spanData as SpanData)?.type})`,
|
|
262
|
-
);
|
|
263
|
-
if (span) {
|
|
264
|
-
const parentId = span.parentId || span.traceId;
|
|
265
|
-
const parent = this._spans.get(parentId);
|
|
266
|
-
const ctx = parent
|
|
267
|
-
? otelTrace.setSpan(otelContext.active(), parent)
|
|
268
|
-
: undefined;
|
|
269
|
-
|
|
270
|
-
const spanData = span.spanData as SpanData;
|
|
271
|
-
let name: string;
|
|
272
|
-
|
|
273
|
-
// Use name for agent and function spans, type for others
|
|
274
|
-
if (spanData.type === "agent") {
|
|
275
|
-
name = (spanData as AgentSpanData).name;
|
|
276
|
-
} else if (spanData.type === "function") {
|
|
277
|
-
name = (spanData as FunctionSpanData).name;
|
|
278
|
-
} else {
|
|
279
|
-
name = spanData.type;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
const otelSpan = this._tracer.startSpan(name, {}, ctx);
|
|
283
|
-
this._spans.set(span.spanId, otelSpan);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Called when a span ends. Extracts `gen_ai.*` attributes from the span
|
|
289
|
-
* data and sets them on the corresponding OTel span before ending it.
|
|
290
|
-
*
|
|
291
|
-
* @param span - The OpenAI Agents SDK span.
|
|
292
|
-
*/
|
|
293
|
-
async onSpanEnd(span: Span<SpanData>): Promise<void> {
|
|
294
|
-
if (!span) return;
|
|
295
|
-
|
|
296
|
-
const otelSpan = this._spans.get(span.spanId);
|
|
297
|
-
if (!otelSpan) {
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
this._spans.delete(span.spanId);
|
|
302
|
-
const spanData = span.spanData as SpanData;
|
|
303
|
-
|
|
304
|
-
// Extract gen_ai.* attributes based on span type
|
|
305
|
-
switch (spanData.type) {
|
|
306
|
-
case "agent":
|
|
307
|
-
this._processAgentSpan(otelSpan, spanData as AgentSpanData);
|
|
308
|
-
break;
|
|
309
|
-
case "function":
|
|
310
|
-
this._processFunctionSpan(otelSpan, spanData as FunctionSpanData);
|
|
311
|
-
break;
|
|
312
|
-
case "response":
|
|
313
|
-
this._processResponseSpan(otelSpan, spanData as ResponseSpanData);
|
|
314
|
-
break;
|
|
315
|
-
case "generation":
|
|
316
|
-
this._processGenerationSpan(otelSpan, spanData as GenerationSpanData);
|
|
317
|
-
break;
|
|
318
|
-
case "handoff":
|
|
319
|
-
this._processHandoffSpan(otelSpan, spanData as HandoffSpanData);
|
|
320
|
-
break;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// Propagate conversation ID from trace
|
|
324
|
-
const conversationId = this._conversationIds.get(span.traceId);
|
|
325
|
-
if (conversationId) {
|
|
326
|
-
otelSpan.setAttribute("gen_ai.conversation.id", conversationId);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// Keep raw span data for debugging (matches Python attribute name)
|
|
330
|
-
otelSpan.setAttribute("openai_agents.span_data", JSON.stringify(spanData));
|
|
331
|
-
otelSpan.end();
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Extract attributes from agent spans.
|
|
336
|
-
*/
|
|
337
|
-
private _processAgentSpan(otelSpan: OtelSpan, spanData: AgentSpanData): void {
|
|
338
|
-
otelSpan.setAttribute("gen_ai.agent.name", spanData.name);
|
|
339
|
-
otelSpan.setAttribute("gen_ai.provider.name", "openai");
|
|
340
|
-
otelSpan.setAttribute("openinference.span.kind", "AGENT");
|
|
341
|
-
|
|
342
|
-
if (spanData.tools) {
|
|
343
|
-
// Wrap tool names as objects for ClickHouse Array(JSON) compatibility
|
|
344
|
-
const toolDefs = spanData.tools.map((t: string) => ({
|
|
345
|
-
type: "function",
|
|
346
|
-
name: t,
|
|
347
|
-
}));
|
|
348
|
-
otelSpan.setAttribute(
|
|
349
|
-
"gen_ai.tool.definitions",
|
|
350
|
-
JSON.stringify(toolDefs),
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (spanData.handoffs) {
|
|
355
|
-
otelSpan.setAttribute(
|
|
356
|
-
"gen_ai.agent.handoffs",
|
|
357
|
-
JSON.stringify(spanData.handoffs),
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
if (spanData.output_type) {
|
|
362
|
-
otelSpan.setAttribute("gen_ai.agent.output_type", spanData.output_type);
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Extract attributes from function/tool spans.
|
|
368
|
-
*/
|
|
369
|
-
private _processFunctionSpan(
|
|
370
|
-
otelSpan: OtelSpan,
|
|
371
|
-
spanData: FunctionSpanData,
|
|
372
|
-
): void {
|
|
373
|
-
otelSpan.updateName(`execute_tool ${spanData.name}`);
|
|
374
|
-
otelSpan.setAttribute("gen_ai.operation.name", "execute_tool");
|
|
375
|
-
otelSpan.setAttribute("gen_ai.tool.name", spanData.name);
|
|
376
|
-
otelSpan.setAttribute("gen_ai.tool.type", "function");
|
|
377
|
-
otelSpan.setAttribute("openinference.span.kind", "TOOL");
|
|
378
|
-
|
|
379
|
-
if (spanData.input) {
|
|
380
|
-
otelSpan.setAttribute("gen_ai.tool.call.arguments", spanData.input);
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
if (spanData.output) {
|
|
384
|
-
otelSpan.setAttribute("gen_ai.tool.call.result", String(spanData.output));
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* Extract attributes from response spans.
|
|
390
|
-
* Matches Python implementation - extracts from both spanData and _response object.
|
|
391
|
-
*/
|
|
392
|
-
private _processResponseSpan(
|
|
393
|
-
otelSpan: OtelSpan,
|
|
394
|
-
spanData: ResponseSpanData,
|
|
395
|
-
): void {
|
|
396
|
-
otelSpan.setAttribute("gen_ai.operation.name", "chat");
|
|
397
|
-
otelSpan.setAttribute("gen_ai.provider.name", "openai");
|
|
398
|
-
otelSpan.setAttribute("openinference.span.kind", "LLM");
|
|
399
|
-
|
|
400
|
-
// Response ID (from span_data directly)
|
|
401
|
-
if (spanData.response_id) {
|
|
402
|
-
otelSpan.setAttribute("gen_ai.response.id", spanData.response_id);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// Input messages (from spanData._input)
|
|
406
|
-
if (spanData._input) {
|
|
407
|
-
const inputs = Array.isArray(spanData._input)
|
|
408
|
-
? spanData._input
|
|
409
|
-
: [spanData._input];
|
|
410
|
-
const [inputMessages] = convertResponsesInputsToSemconv(
|
|
411
|
-
inputs as ResponseInputItem[],
|
|
412
|
-
undefined,
|
|
413
|
-
);
|
|
414
|
-
if (inputMessages.length > 0) {
|
|
415
|
-
otelSpan.setAttribute(
|
|
416
|
-
"gen_ai.input.messages",
|
|
417
|
-
JSON.stringify(inputMessages),
|
|
418
|
-
);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
// Extract from _response if available (full Response object)
|
|
423
|
-
const response = spanData._response as OpenAIResponse | undefined;
|
|
424
|
-
if (!response) return;
|
|
425
|
-
|
|
426
|
-
// System instructions
|
|
427
|
-
const sysInstructions = convertResponsesInstructionsToSemconv(
|
|
428
|
-
response.instructions,
|
|
429
|
-
);
|
|
430
|
-
if (sysInstructions) {
|
|
431
|
-
otelSpan.setAttribute(
|
|
432
|
-
"gen_ai.system_instructions",
|
|
433
|
-
JSON.stringify(sysInstructions),
|
|
434
|
-
);
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
// Tool definitions (with full details from Response object)
|
|
438
|
-
if (response.tools && response.tools.length > 0) {
|
|
439
|
-
const toolDefs = convertResponsesToolsToSemconv(response.tools);
|
|
440
|
-
otelSpan.setAttribute(
|
|
441
|
-
"gen_ai.tool.definitions",
|
|
442
|
-
JSON.stringify(toolDefs),
|
|
443
|
-
);
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// Token usage
|
|
447
|
-
const usage = response.usage;
|
|
448
|
-
if (usage) {
|
|
449
|
-
if (usage.input_tokens) {
|
|
450
|
-
otelSpan.setAttribute("gen_ai.usage.input_tokens", usage.input_tokens);
|
|
451
|
-
}
|
|
452
|
-
if (usage.output_tokens) {
|
|
453
|
-
otelSpan.setAttribute(
|
|
454
|
-
"gen_ai.usage.output_tokens",
|
|
455
|
-
usage.output_tokens,
|
|
456
|
-
);
|
|
457
|
-
}
|
|
458
|
-
// OpenAI returns cached token count in input_tokens_details.cached_tokens
|
|
459
|
-
const cachedTokens = usage.input_tokens_details?.cached_tokens;
|
|
460
|
-
if (cachedTokens) {
|
|
461
|
-
otelSpan.setAttribute(
|
|
462
|
-
"gen_ai.usage.cache_read.input_tokens",
|
|
463
|
-
cachedTokens,
|
|
464
|
-
);
|
|
465
|
-
}
|
|
466
|
-
// Provider-reported cost (e.g. OpenRouter usage.cost) when present.
|
|
467
|
-
otelSpan.setAttributes(providerCostAttributes(usage));
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
// Model info
|
|
471
|
-
if (response.model) {
|
|
472
|
-
otelSpan.setAttribute("gen_ai.request.model", response.model);
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
// Response ID from response object (fallback if not in span_data)
|
|
476
|
-
if (!spanData.response_id && response.id) {
|
|
477
|
-
otelSpan.setAttribute("gen_ai.response.id", response.id);
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
// Output messages (from response.output)
|
|
481
|
-
if (response.output && response.output.length > 0) {
|
|
482
|
-
const outputMessages = convertResponsesOutputsToSemconv(response.output);
|
|
483
|
-
if (outputMessages.length > 0) {
|
|
484
|
-
otelSpan.setAttribute(
|
|
485
|
-
"gen_ai.output.messages",
|
|
486
|
-
JSON.stringify(outputMessages),
|
|
487
|
-
);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Extract attributes from generation spans.
|
|
494
|
-
* Handles both structured GenerationUsageData (spanData.usage) and raw
|
|
495
|
-
* Chat Completions API responses stored in spanData.output[0] by
|
|
496
|
-
* OpenAIChatCompletionsModel.
|
|
497
|
-
*/
|
|
498
|
-
private _processGenerationSpan(
|
|
499
|
-
otelSpan: OtelSpan,
|
|
500
|
-
spanData: GenerationSpanData,
|
|
501
|
-
): void {
|
|
502
|
-
otelSpan.setAttribute("gen_ai.operation.name", "chat");
|
|
503
|
-
otelSpan.setAttribute("gen_ai.provider.name", "openai");
|
|
504
|
-
otelSpan.setAttribute("openinference.span.kind", "LLM");
|
|
505
|
-
|
|
506
|
-
if (spanData.model) {
|
|
507
|
-
otelSpan.setAttribute("gen_ai.request.model", spanData.model);
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
// GenerationUsageData uses snake_case: input_tokens / output_tokens.
|
|
511
|
-
if (spanData.usage?.input_tokens) {
|
|
512
|
-
otelSpan.setAttribute(
|
|
513
|
-
"gen_ai.usage.input_tokens",
|
|
514
|
-
spanData.usage.input_tokens,
|
|
515
|
-
);
|
|
516
|
-
}
|
|
517
|
-
if (spanData.usage?.output_tokens) {
|
|
518
|
-
otelSpan.setAttribute(
|
|
519
|
-
"gen_ai.usage.output_tokens",
|
|
520
|
-
spanData.usage.output_tokens,
|
|
521
|
-
);
|
|
522
|
-
}
|
|
523
|
-
if (spanData.usage) {
|
|
524
|
-
// Provider-reported cost (e.g. OpenRouter usage.cost) when present.
|
|
525
|
-
otelSpan.setAttributes(providerCostAttributes(spanData.usage));
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
// OpenAIChatCompletionsModel stores the raw ChatCompletion response object
|
|
529
|
-
// in spanData.output[0] (it never sets spanData.usage). Extract usage,
|
|
530
|
-
// response ID, response model, and the actual assistant message from it.
|
|
531
|
-
if (!spanData.usage) {
|
|
532
|
-
const rawResponse = spanData.output?.[0] as
|
|
533
|
-
Record<string, unknown> | undefined;
|
|
534
|
-
|
|
535
|
-
// Response ID — required by the backend processor to register this span
|
|
536
|
-
// as a conversation item (build_request returns None without it).
|
|
537
|
-
const responseId = rawResponse?.id;
|
|
538
|
-
if (typeof responseId === "string" && responseId) {
|
|
539
|
-
otelSpan.setAttribute("gen_ai.response.id", responseId);
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
// Resolved model name (e.g. "gpt-4o-mini-2024-07-18" vs "gpt-4o-mini").
|
|
543
|
-
const responseModel = rawResponse?.model;
|
|
544
|
-
if (typeof responseModel === "string" && responseModel) {
|
|
545
|
-
otelSpan.setAttribute("gen_ai.response.model", responseModel);
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
const rawUsage = rawResponse?.usage as
|
|
549
|
-
Record<string, unknown> | undefined;
|
|
550
|
-
if (rawUsage) {
|
|
551
|
-
const promptTokens = rawUsage.prompt_tokens;
|
|
552
|
-
const completionTokens = rawUsage.completion_tokens;
|
|
553
|
-
if (typeof promptTokens === "number") {
|
|
554
|
-
otelSpan.setAttribute("gen_ai.usage.input_tokens", promptTokens);
|
|
555
|
-
}
|
|
556
|
-
if (typeof completionTokens === "number") {
|
|
557
|
-
otelSpan.setAttribute("gen_ai.usage.output_tokens", completionTokens);
|
|
558
|
-
}
|
|
559
|
-
const promptDetails = rawUsage.prompt_tokens_details as
|
|
560
|
-
Record<string, unknown> | undefined;
|
|
561
|
-
const cachedTokens = promptDetails?.cached_tokens;
|
|
562
|
-
if (typeof cachedTokens === "number" && cachedTokens > 0) {
|
|
563
|
-
otelSpan.setAttribute(
|
|
564
|
-
"gen_ai.usage.cache_read.input_tokens",
|
|
565
|
-
cachedTokens,
|
|
566
|
-
);
|
|
567
|
-
}
|
|
568
|
-
// OpenRouter-style gateways report the charged price and reasoning
|
|
569
|
-
// token split inside the Chat Completions usage block.
|
|
570
|
-
otelSpan.setAttributes(providerCostAttributes(rawUsage));
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
// Extract the actual assistant message from choices[0].message so that
|
|
574
|
-
// gen_ai.output.messages is a proper [{role, content}] array rather than
|
|
575
|
-
// the full ChatCompletion envelope (which the backend cannot parse).
|
|
576
|
-
const choices = rawResponse?.choices as
|
|
577
|
-
Array<Record<string, unknown>> | undefined;
|
|
578
|
-
const assistantMessage = choices?.[0]?.message as
|
|
579
|
-
Record<string, unknown> | undefined;
|
|
580
|
-
if (assistantMessage) {
|
|
581
|
-
otelSpan.setAttribute(
|
|
582
|
-
"gen_ai.output.messages",
|
|
583
|
-
JSON.stringify([assistantMessage]),
|
|
584
|
-
);
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
if (spanData.input) {
|
|
589
|
-
otelSpan.setAttribute(
|
|
590
|
-
"gen_ai.input.messages",
|
|
591
|
-
JSON.stringify(spanData.input),
|
|
592
|
-
);
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
// For non-Chat-Completions generation spans that do have usage set,
|
|
596
|
-
// still export output messages in the standard format if present.
|
|
597
|
-
if (spanData.usage && spanData.output) {
|
|
598
|
-
otelSpan.setAttribute(
|
|
599
|
-
"gen_ai.output.messages",
|
|
600
|
-
JSON.stringify(spanData.output),
|
|
601
|
-
);
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
/**
|
|
606
|
-
* Extract attributes from handoff spans.
|
|
607
|
-
*/
|
|
608
|
-
private _processHandoffSpan(
|
|
609
|
-
otelSpan: OtelSpan,
|
|
610
|
-
spanData: HandoffSpanData,
|
|
611
|
-
): void {
|
|
612
|
-
if (spanData.from_agent) {
|
|
613
|
-
otelSpan.setAttribute("gen_ai.handoff.from_agent", spanData.from_agent);
|
|
614
|
-
}
|
|
615
|
-
if (spanData.to_agent) {
|
|
616
|
-
otelSpan.setAttribute("gen_ai.handoff.to_agent", spanData.to_agent);
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
/**
|
|
621
|
-
* Shut down the tracer provider, flushing all pending spans.
|
|
622
|
-
*
|
|
623
|
-
* @returns A promise that resolves once shutdown is complete.
|
|
624
|
-
*/
|
|
625
|
-
async shutdown(): Promise<void> {
|
|
626
|
-
await this._tracerProvider.shutdown();
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
/**
|
|
630
|
-
* Force-flush any buffered spans to the Introspection backend.
|
|
631
|
-
*
|
|
632
|
-
* @returns A promise that resolves once the flush completes.
|
|
633
|
-
*/
|
|
634
|
-
async forceFlush(): Promise<void> {
|
|
635
|
-
await this._tracerProvider.forceFlush();
|
|
636
|
-
}
|
|
637
|
-
}
|
package/src/pagination.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cursor pagination now lives in the shared `@introspection-sdk/http`
|
|
3
|
-
* core so the browser and Node SDKs share one `Paginator`. Re-exported
|
|
4
|
-
* here to keep the existing `./pagination.js` import path stable.
|
|
5
|
-
*/
|
|
6
|
-
export {
|
|
7
|
-
Paginator,
|
|
8
|
-
cursorPaginate,
|
|
9
|
-
type PageSource,
|
|
10
|
-
} from "@introspection-sdk/http";
|
package/src/proxy-from-env.d.ts
DELETED