@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.
Files changed (89) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/.turbo/turbo-check-types.log +5 -0
  3. package/.turbo/turbo-lint.log +5 -0
  4. package/dist/ai-sdk-integration.d.ts +123 -0
  5. package/dist/ai-sdk-integration.d.ts.map +1 -0
  6. package/dist/ai-sdk-integration.js +475 -0
  7. package/dist/anthropic.d.ts +54 -0
  8. package/dist/anthropic.d.ts.map +1 -0
  9. package/dist/anthropic.js +375 -0
  10. package/dist/claude-hooks.d.ts +328 -0
  11. package/dist/claude-hooks.d.ts.map +1 -0
  12. package/dist/claude-hooks.js +571 -0
  13. package/dist/claude-wrapper.d.ts +93 -0
  14. package/dist/claude-wrapper.d.ts.map +1 -0
  15. package/dist/claude-wrapper.js +154 -0
  16. package/dist/client.d.ts +157 -0
  17. package/dist/client.d.ts.map +1 -0
  18. package/dist/client.js +394 -0
  19. package/dist/converters/ai-sdk.d.ts +65 -0
  20. package/dist/converters/ai-sdk.d.ts.map +1 -0
  21. package/dist/converters/ai-sdk.js +238 -0
  22. package/dist/converters/claude.d.ts +146 -0
  23. package/dist/converters/claude.d.ts.map +1 -0
  24. package/dist/converters/claude.js +202 -0
  25. package/dist/converters/index.d.ts +9 -0
  26. package/dist/converters/index.d.ts.map +1 -0
  27. package/dist/converters/index.js +8 -0
  28. package/dist/converters/openai.d.ts +51 -0
  29. package/dist/converters/openai.d.ts.map +1 -0
  30. package/dist/converters/openai.js +268 -0
  31. package/dist/converters/openclaw.d.ts +16 -0
  32. package/dist/converters/openclaw.d.ts.map +1 -0
  33. package/dist/converters/openclaw.js +49 -0
  34. package/dist/converters/openinference.d.ts +107 -0
  35. package/dist/converters/openinference.d.ts.map +1 -0
  36. package/dist/converters/openinference.js +552 -0
  37. package/dist/converters/vercel.d.ts +21 -0
  38. package/dist/converters/vercel.d.ts.map +1 -0
  39. package/dist/converters/vercel.js +208 -0
  40. package/dist/index.d.ts +38 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +39 -0
  43. package/dist/langchain-handler.d.ts +101 -0
  44. package/dist/langchain-handler.d.ts.map +1 -0
  45. package/dist/langchain-handler.js +489 -0
  46. package/dist/mastra-exporter.d.ts +85 -0
  47. package/dist/mastra-exporter.d.ts.map +1 -0
  48. package/dist/mastra-exporter.js +471 -0
  49. package/dist/span-processor.d.ts +62 -0
  50. package/dist/span-processor.d.ts.map +1 -0
  51. package/dist/span-processor.js +310 -0
  52. package/dist/tracing-processor.d.ts +136 -0
  53. package/dist/tracing-processor.d.ts.map +1 -0
  54. package/dist/tracing-processor.js +368 -0
  55. package/dist/types/genai.d.ts +165 -0
  56. package/dist/types/genai.d.ts.map +1 -0
  57. package/dist/types/genai.js +82 -0
  58. package/dist/types.d.ts +7 -0
  59. package/dist/types.d.ts.map +1 -0
  60. package/dist/types.js +1 -0
  61. package/dist/utils.d.ts +19 -0
  62. package/dist/utils.d.ts.map +1 -0
  63. package/dist/utils.js +57 -0
  64. package/dist/version.d.ts +3 -0
  65. package/dist/version.d.ts.map +1 -0
  66. package/dist/version.js +2 -0
  67. package/eslint.config.mjs +4 -0
  68. package/package.json +78 -0
  69. package/src/ai-sdk-integration.ts +689 -0
  70. package/src/anthropic.ts +465 -0
  71. package/src/claude-hooks.ts +1010 -0
  72. package/src/claude-wrapper.ts +240 -0
  73. package/src/client.ts +513 -0
  74. package/src/converters/ai-sdk.ts +298 -0
  75. package/src/converters/claude.ts +334 -0
  76. package/src/converters/index.ts +44 -0
  77. package/src/converters/openai.ts +302 -0
  78. package/src/converters/openinference.ts +673 -0
  79. package/src/converters/vercel.ts +264 -0
  80. package/src/index.ts +121 -0
  81. package/src/langchain-handler.ts +722 -0
  82. package/src/mastra-exporter.ts +650 -0
  83. package/src/span-processor.ts +418 -0
  84. package/src/tracing-processor.ts +548 -0
  85. package/src/types/genai.ts +264 -0
  86. package/src/types.ts +13 -0
  87. package/src/utils.ts +69 -0
  88. package/src/version.ts +2 -0
  89. package/tsconfig.json +8 -0
@@ -0,0 +1,264 @@
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
+ /**
11
+ * A text content part in a message.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const part: TextPart = { type: "text", content: "Hello" };
16
+ * ```
17
+ */
18
+ export interface TextPart {
19
+ /** Discriminator — always `"text"`. */
20
+ type: "text";
21
+ /** The text body of this part. */
22
+ content: string;
23
+ }
24
+
25
+ /**
26
+ * A tool / function-call request part in a message.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const part: ToolCallRequestPart = { type: "tool_call", name: "get_weather", arguments: { city: "SF" } };
31
+ * ```
32
+ */
33
+ export interface ToolCallRequestPart {
34
+ /** Discriminator — always `"tool_call"`. */
35
+ type: "tool_call";
36
+ /** Function / tool name. */
37
+ name: string;
38
+ /** Correlation ID linking request to response. */
39
+ id?: string;
40
+ /** Arguments passed to the tool. */
41
+ arguments?: unknown;
42
+ }
43
+
44
+ /**
45
+ * A tool / function-call response part in a message.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const part: ToolCallResponsePart = { type: "tool_call_response", response: "72°F" };
50
+ * ```
51
+ */
52
+ export interface ToolCallResponsePart {
53
+ /** Discriminator — always `"tool_call_response"`. */
54
+ type: "tool_call_response";
55
+ /** The value returned by the tool. */
56
+ response: unknown;
57
+ /** Correlation ID linking response to request. */
58
+ id?: string;
59
+ }
60
+
61
+ /**
62
+ * A reasoning / thinking content part in a message.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * const part: ReasoningPart = { type: "thinking", content: "Let me think through this..." };
67
+ * ```
68
+ */
69
+ export interface ReasoningPart {
70
+ /** Discriminator — always `"thinking"`. */
71
+ type: "thinking";
72
+ /** The reasoning / thinking summary content. */
73
+ content?: string;
74
+ /** Encrypted reasoning signature (maps to OpenAI encrypted_content, Anthropic signature/redacted_thinking data). */
75
+ signature?: string;
76
+ /** Provider that produced this thinking block (e.g. `"anthropic"`, `"openai"`). Used to reconstruct the correct wire format on replay. */
77
+ provider_name?: string;
78
+ }
79
+
80
+ /** Union of all possible message-part shapes. */
81
+ export type MessagePart =
82
+ | TextPart
83
+ | ToolCallRequestPart
84
+ | ToolCallResponsePart
85
+ | ReasoningPart;
86
+
87
+ /**
88
+ * A system instruction entry for `gen_ai.system_instructions`.
89
+ */
90
+ export interface SystemInstruction {
91
+ /** Part type — always `"text"`. */
92
+ type: "text";
93
+ /** The instruction text content. */
94
+ content: string;
95
+ }
96
+
97
+ /**
98
+ * An input message in OTel Gen AI semantic convention format.
99
+ *
100
+ * Used for the `gen_ai.input.messages` span attribute.
101
+ */
102
+ export interface InputMessage {
103
+ /** The role of the message sender. */
104
+ role: "system" | "user" | "assistant" | "tool";
105
+ /** Ordered content parts that compose this message. */
106
+ parts: MessagePart[];
107
+ /** Optional tool name when `role` is `"tool"`. */
108
+ name?: string;
109
+ }
110
+
111
+ /**
112
+ * An output message in OTel Gen AI semantic convention format.
113
+ *
114
+ * Used for the `gen_ai.output.messages` span attribute.
115
+ */
116
+ export interface OutputMessage {
117
+ /** The role of the message sender. */
118
+ role: "system" | "user" | "assistant" | "tool";
119
+ /** Ordered content parts that compose this message. */
120
+ parts: MessagePart[];
121
+ /** Model-reported finish reason (e.g. `"stop"`, `"end_turn"`). */
122
+ finish_reason?: string;
123
+ /** Optional tool name when `role` is `"tool"`. */
124
+ name?: string;
125
+ }
126
+
127
+ /**
128
+ * A tool definition for the `gen_ai.tool.definitions` attribute.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * const def: ToolDefinition = { name: "get_weather", description: "Get current weather" };
133
+ * ```
134
+ */
135
+ export interface ToolDefinition {
136
+ /** Tool / function name. */
137
+ name: string;
138
+ /** Human-readable description of what the tool does. */
139
+ description?: string;
140
+ /** JSON Schema describing the tool's parameters. */
141
+ parameters?: Record<string, unknown>;
142
+ }
143
+
144
+ /**
145
+ * GenAI semantic convention attributes.
146
+ *
147
+ * This interface represents the standardized GenAI attributes that can be
148
+ * converted to OpenTelemetry semantic convention format (gen_ai.* with dots).
149
+ *
150
+ * All properties use camelCase following TypeScript conventions.
151
+ */
152
+ export interface GenAiAttributes {
153
+ /** Model name (gen_ai.request.model) */
154
+ requestModel?: string;
155
+ /** System prompt (gen_ai.system) */
156
+ system?: string;
157
+ /** Operation name (gen_ai.operation.name) */
158
+ operationName?: string;
159
+ /** Tool definitions (gen_ai.tool.definitions) — serialized to JSON by {@link toAttributes}. */
160
+ toolDefinitions?: ToolDefinition[];
161
+ /** Input messages (gen_ai.input.messages) — serialized to JSON by {@link toAttributes}. */
162
+ inputMessages?: InputMessage[];
163
+ /** Output messages (gen_ai.output.messages) — serialized to JSON by {@link toAttributes}. */
164
+ outputMessages?: OutputMessage[];
165
+ /** System instructions (gen_ai.system_instructions) — serialized to JSON by {@link toAttributes}. */
166
+ systemInstructions?: SystemInstruction[];
167
+ /** Response ID (gen_ai.response.id) */
168
+ responseId?: string;
169
+ /** Input token count (gen_ai.usage.input_tokens) */
170
+ inputTokens?: number;
171
+ /** Output token count (gen_ai.usage.output_tokens) */
172
+ outputTokens?: number;
173
+ /** Cache creation input tokens (gen_ai.usage.cache_creation_input_tokens) */
174
+ cacheCreationInputTokens?: number;
175
+ /** Cache read input tokens (gen_ai.usage.cache_read_input_tokens) */
176
+ cacheReadInputTokens?: number;
177
+ }
178
+
179
+ /**
180
+ * Convert a {@link GenAiAttributes} object to an OTel-compatible attributes
181
+ * dictionary with `gen_ai.*` dot-notation keys.
182
+ *
183
+ * Properties that are `undefined` are omitted from the result.
184
+ *
185
+ * @param attrs - The camelCase {@link GenAiAttributes} to convert.
186
+ * @returns A flat `Record<string, string | number>` with OTel semconv keys.
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * const otel = toAttributes({ requestModel: "gpt-4", inputTokens: 42 });
191
+ * // { "gen_ai.request.model": "gpt-4", "gen_ai.usage.input_tokens": 42 }
192
+ * ```
193
+ */
194
+ /**
195
+ * Recursively strip `undefined` and `null` values from an object before
196
+ * JSON serialization. Equivalent to Python's `model_dump(exclude_none=True)`.
197
+ */
198
+ function stripNullish(obj: unknown): unknown {
199
+ if (Array.isArray(obj)) return obj.map(stripNullish);
200
+ if (obj !== null && typeof obj === "object") {
201
+ const out: Record<string, unknown> = {};
202
+ for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {
203
+ if (value !== undefined && value !== null) {
204
+ out[key] = stripNullish(value);
205
+ }
206
+ }
207
+ return out;
208
+ }
209
+ return obj;
210
+ }
211
+
212
+ export function toAttributes(
213
+ attrs: GenAiAttributes,
214
+ ): Record<string, string | number> {
215
+ const result: Record<string, string | number> = {};
216
+
217
+ if (attrs.requestModel !== undefined) {
218
+ result["gen_ai.request.model"] = attrs.requestModel;
219
+ }
220
+ if (attrs.system !== undefined) {
221
+ result["gen_ai.system"] = attrs.system;
222
+ }
223
+ if (attrs.operationName !== undefined) {
224
+ result["gen_ai.operation.name"] = attrs.operationName;
225
+ }
226
+ if (attrs.toolDefinitions !== undefined) {
227
+ result["gen_ai.tool.definitions"] = JSON.stringify(
228
+ stripNullish(attrs.toolDefinitions),
229
+ );
230
+ }
231
+ if (attrs.inputMessages !== undefined) {
232
+ result["gen_ai.input.messages"] = JSON.stringify(
233
+ stripNullish(attrs.inputMessages),
234
+ );
235
+ }
236
+ if (attrs.outputMessages !== undefined) {
237
+ result["gen_ai.output.messages"] = JSON.stringify(
238
+ stripNullish(attrs.outputMessages),
239
+ );
240
+ }
241
+ if (attrs.systemInstructions !== undefined) {
242
+ result["gen_ai.system_instructions"] = JSON.stringify(
243
+ stripNullish(attrs.systemInstructions),
244
+ );
245
+ }
246
+ if (attrs.responseId !== undefined) {
247
+ result["gen_ai.response.id"] = attrs.responseId;
248
+ }
249
+ if (attrs.inputTokens !== undefined) {
250
+ result["gen_ai.usage.input_tokens"] = attrs.inputTokens;
251
+ }
252
+ if (attrs.outputTokens !== undefined) {
253
+ result["gen_ai.usage.output_tokens"] = attrs.outputTokens;
254
+ }
255
+ if (attrs.cacheCreationInputTokens !== undefined) {
256
+ result["gen_ai.usage.cache_creation.input_tokens"] =
257
+ attrs.cacheCreationInputTokens;
258
+ }
259
+ if (attrs.cacheReadInputTokens !== undefined) {
260
+ result["gen_ai.usage.cache_read.input_tokens"] = attrs.cacheReadInputTokens;
261
+ }
262
+
263
+ return result;
264
+ }
package/src/types.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Re-export shared types and functions from @introspection-sdk/types
3
+ */
4
+ export type {
5
+ AdvancedOptions,
6
+ IntrospectionClientOptions,
7
+ FeedbackOptions,
8
+ UserTraits,
9
+ } from "@introspection-sdk/types";
10
+
11
+ export type { GenAiContext, IdentityContext } from "@introspection-sdk/types";
12
+
13
+ export { generateEventId } from "@introspection-sdk/types";
package/src/utils.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { DiagLogLevel, DiagLogger } from "@opentelemetry/api";
2
+
3
+ /**
4
+ * Logger for introspection-sdk package.
5
+ * Uses OpenTelemetry's diagnostic logger.
6
+ */
7
+ class IntrospectionLogger implements DiagLogger {
8
+ private logLevel: DiagLogLevel;
9
+
10
+ constructor() {
11
+ const logLevelStr = (
12
+ process.env.INTROSPECTION_LOG_LEVEL || "INFO"
13
+ ).toUpperCase();
14
+ this.logLevel = this.parseLogLevel(logLevelStr);
15
+ }
16
+
17
+ private parseLogLevel(level: string): DiagLogLevel {
18
+ switch (level) {
19
+ case "ERROR":
20
+ return DiagLogLevel.ERROR;
21
+ case "WARN":
22
+ return DiagLogLevel.WARN;
23
+ case "INFO":
24
+ return DiagLogLevel.INFO;
25
+ case "DEBUG":
26
+ return DiagLogLevel.DEBUG;
27
+ case "VERBOSE":
28
+ return DiagLogLevel.VERBOSE;
29
+ default:
30
+ return DiagLogLevel.INFO;
31
+ }
32
+ }
33
+
34
+ private shouldLog(level: DiagLogLevel): boolean {
35
+ return level <= this.logLevel;
36
+ }
37
+
38
+ error(...args: unknown[]): void {
39
+ if (this.shouldLog(DiagLogLevel.ERROR)) {
40
+ console.error("[introspection-sdk]", ...args);
41
+ }
42
+ }
43
+
44
+ warn(...args: unknown[]): void {
45
+ if (this.shouldLog(DiagLogLevel.WARN)) {
46
+ console.warn("[introspection-sdk]", ...args);
47
+ }
48
+ }
49
+
50
+ info(...args: unknown[]): void {
51
+ if (this.shouldLog(DiagLogLevel.INFO)) {
52
+ console.info("[introspection-sdk]", ...args);
53
+ }
54
+ }
55
+
56
+ debug(...args: unknown[]): void {
57
+ if (this.shouldLog(DiagLogLevel.DEBUG)) {
58
+ console.debug("[introspection-sdk]", ...args);
59
+ }
60
+ }
61
+
62
+ verbose(...args: unknown[]): void {
63
+ if (this.shouldLog(DiagLogLevel.VERBOSE)) {
64
+ console.debug("[introspection-sdk]", ...args);
65
+ }
66
+ }
67
+ }
68
+
69
+ export const logger = new IntrospectionLogger();
package/src/version.ts ADDED
@@ -0,0 +1,2 @@
1
+ /** Version information for introspection-sdk package. */
2
+ export const VERSION = "0.1.0";
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "@repo/typescript-config/react-library.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist"
5
+ },
6
+ "include": ["src"],
7
+ "exclude": ["node_modules", "dist"]
8
+ }