@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
package/src/client.ts ADDED
@@ -0,0 +1,513 @@
1
+ /**
2
+ * Introspection Client for Node.js
3
+ * Provides an API for tracking events and feedback using OTLP Logs.
4
+ *
5
+ * Identity and gen_ai context are automatically extracted from OpenTelemetry
6
+ * baggage.
7
+ */
8
+
9
+ import { SeverityNumber, type LogAttributes } from "@opentelemetry/api-logs";
10
+ import {
11
+ LoggerProvider,
12
+ BatchLogRecordProcessor,
13
+ } from "@opentelemetry/sdk-logs";
14
+ import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-proto";
15
+ import {
16
+ resourceFromAttributes,
17
+ defaultResource,
18
+ } from "@opentelemetry/resources";
19
+ import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
20
+ import { propagation, context } from "@opentelemetry/api";
21
+ import { logger as sdkLogger } from "./utils.js";
22
+ import { VERSION } from "./version.js";
23
+ import {
24
+ generateEventId,
25
+ type IntrospectionClientOptions,
26
+ type FeedbackOptions,
27
+ type UserTraits,
28
+ type GenAiContext,
29
+ type IdentityContext,
30
+ } from "./types.js";
31
+
32
+ /**
33
+ * Introspection client for tracking events and feedback using OTLP Logs.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * const client = new IntrospectionClient({
38
+ * token: process.env.INTROSPECTION_TOKEN,
39
+ * });
40
+ *
41
+ * client.identify("user_123", { email: "user@example.com" });
42
+ * client.feedback("thumbs_up", { comments: "Great response!" });
43
+ *
44
+ * await client.shutdown();
45
+ * ```
46
+ */
47
+ export class IntrospectionClient {
48
+ private loggerProvider: LoggerProvider;
49
+ private otelLogger: ReturnType<LoggerProvider["getLogger"]>;
50
+ private userId: string | undefined;
51
+ private anonymousId: string | undefined;
52
+ private traits: Record<string, unknown> = {};
53
+
54
+ constructor(options: IntrospectionClientOptions = {}) {
55
+ const token = options.token || process.env.INTROSPECTION_TOKEN || "";
56
+ const serviceName =
57
+ options.serviceName ||
58
+ process.env.INTROSPECTION_SERVICE_NAME ||
59
+ "introspection-client";
60
+ const advanced = options.advanced || {};
61
+ const baseUrl =
62
+ advanced.baseUrl ||
63
+ process.env.INTROSPECTION_BASE_URL ||
64
+ "https://otel.introspection.dev";
65
+
66
+ if (!token) {
67
+ sdkLogger.warn(
68
+ "IntrospectionClient: No token provided. Events will not be sent.",
69
+ );
70
+ }
71
+
72
+ // Construct endpoint URL for logs
73
+ let endpoint: string;
74
+ if (baseUrl.endsWith("/v1/logs")) {
75
+ endpoint = baseUrl;
76
+ } else {
77
+ endpoint = `${baseUrl.replace(/\/$/, "")}/v1/logs`;
78
+ }
79
+
80
+ // Build headers
81
+ const headers: Record<string, string> = {
82
+ Authorization: `Bearer ${token}`,
83
+ };
84
+ if (advanced.additionalHeaders) {
85
+ Object.assign(headers, advanced.additionalHeaders);
86
+ }
87
+
88
+ // Create OTLP log exporter
89
+ const exporter = new OTLPLogExporter({
90
+ url: endpoint,
91
+ headers,
92
+ });
93
+
94
+ // Create batch processor
95
+ const processor = new BatchLogRecordProcessor(exporter, {
96
+ maxQueueSize: advanced.maxBatchSize ?? 100,
97
+ scheduledDelayMillis: advanced.flushInterval ?? 5000,
98
+ });
99
+
100
+ // Get default resource and merge with our service name
101
+ const baseResource = defaultResource();
102
+ const introspectionResource = resourceFromAttributes({
103
+ [ATTR_SERVICE_NAME]: serviceName,
104
+ });
105
+ const resource = baseResource.merge(introspectionResource);
106
+
107
+ // Create logger provider
108
+ this.loggerProvider = new LoggerProvider({
109
+ resource,
110
+ processors: [processor],
111
+ });
112
+
113
+ this.otelLogger = this.loggerProvider.getLogger(
114
+ "@introspection-sdk/introspection-node",
115
+ VERSION,
116
+ );
117
+
118
+ sdkLogger.info(
119
+ `IntrospectionClient initialized: service=${serviceName}, endpoint=${endpoint}`,
120
+ );
121
+ }
122
+
123
+ /** Get gen_ai context from baggage */
124
+ private getGenAiFromContext(): GenAiContext {
125
+ const baggage = propagation.getBaggage(context.active());
126
+ return {
127
+ conversationId: baggage?.getEntry("gen_ai.conversation.id")?.value,
128
+ previousResponseId: baggage?.getEntry(
129
+ "gen_ai.request.previous_response_id",
130
+ )?.value,
131
+ agentName: baggage?.getEntry("gen_ai.agent.name")?.value,
132
+ agentId: baggage?.getEntry("gen_ai.agent.id")?.value,
133
+ };
134
+ }
135
+
136
+ /** Get identity from baggage or client instance */
137
+ private getIdentityFromContext(): IdentityContext {
138
+ const baggage = propagation.getBaggage(context.active());
139
+ return {
140
+ userId: baggage?.getEntry("identity.user_id")?.value || this.userId,
141
+ anonymousId:
142
+ baggage?.getEntry("identity.anonymous_id")?.value || this.anonymousId,
143
+ };
144
+ }
145
+
146
+ /**
147
+ * Get current timestamp as OpenTelemetry HrTime.
148
+ * Uses process.hrtime.bigint() for nanosecond precision in Node.js.
149
+ */
150
+ private getTimestamp(): [number, number] {
151
+ const hrTimeNs = process.hrtime.bigint();
152
+ const epochNs = BigInt(Date.now()) * BigInt(1_000_000);
153
+ const offsetNs = hrTimeNs - process.hrtime.bigint() + epochNs;
154
+ const seconds = Number(offsetNs / BigInt(1_000_000_000));
155
+ const nanos = Number(offsetNs % BigInt(1_000_000_000));
156
+ return [seconds, nanos];
157
+ }
158
+
159
+ /**
160
+ * Build log record attributes.
161
+ */
162
+ private buildAttributes(
163
+ eventName: string,
164
+ options: {
165
+ properties?: Record<string, unknown>;
166
+ traits?: Record<string, unknown>;
167
+ conversationId?: string;
168
+ previousResponseId?: string;
169
+ eventId?: string;
170
+ } = {},
171
+ ): LogAttributes {
172
+ const { properties, traits, conversationId, previousResponseId, eventId } =
173
+ options;
174
+
175
+ // Core fields
176
+ const attributes: LogAttributes = {
177
+ "event.name": eventName,
178
+ "event.id": eventId || generateEventId(),
179
+ };
180
+
181
+ // Identity
182
+ const identity = this.getIdentityFromContext();
183
+ if (identity.userId) {
184
+ attributes["identity.user.id"] = identity.userId;
185
+ }
186
+ if (identity.anonymousId) {
187
+ attributes["identity.anonymous.id"] = identity.anonymousId;
188
+ }
189
+
190
+ // Gen AI context (explicit params override baggage)
191
+ const genAi = this.getGenAiFromContext();
192
+ const finalConversationId = conversationId || genAi.conversationId;
193
+ const finalPreviousResponseId =
194
+ previousResponseId || genAi.previousResponseId;
195
+
196
+ if (finalConversationId) {
197
+ attributes["gen_ai.conversation.id"] = finalConversationId;
198
+ }
199
+ if (finalPreviousResponseId) {
200
+ attributes["gen_ai.request.previous_response_id"] =
201
+ finalPreviousResponseId;
202
+ }
203
+ if (genAi.agentName) {
204
+ attributes["gen_ai.agent.name"] = genAi.agentName;
205
+ }
206
+ if (genAi.agentId) {
207
+ attributes["gen_ai.agent.id"] = genAi.agentId;
208
+ }
209
+
210
+ // Flatten properties with "properties." prefix
211
+ if (properties) {
212
+ for (const [key, value] of Object.entries(properties)) {
213
+ if (value != null) {
214
+ const attrKey = `properties.${key}`;
215
+ if (typeof value === "object") {
216
+ attributes[attrKey] = JSON.stringify(value);
217
+ } else if (
218
+ typeof value === "string" ||
219
+ typeof value === "number" ||
220
+ typeof value === "boolean"
221
+ ) {
222
+ attributes[attrKey] = value;
223
+ }
224
+ }
225
+ }
226
+ }
227
+
228
+ // Flatten traits with "context.traits." prefix
229
+ if (traits) {
230
+ for (const [key, value] of Object.entries(traits)) {
231
+ if (value != null) {
232
+ const attrKey = `context.traits.${key}`;
233
+ if (typeof value === "object") {
234
+ attributes[attrKey] = JSON.stringify(value);
235
+ } else if (
236
+ typeof value === "string" ||
237
+ typeof value === "number" ||
238
+ typeof value === "boolean"
239
+ ) {
240
+ attributes[attrKey] = value;
241
+ }
242
+ }
243
+ }
244
+ }
245
+
246
+ return attributes;
247
+ }
248
+
249
+ /**
250
+ * Track a custom event.
251
+ *
252
+ * @param eventName - A descriptive name for the event (e.g. `"page_view"`).
253
+ * @param properties - Arbitrary key/value pairs attached to the event.
254
+ * @param options - Optional overrides such as a custom `eventId`.
255
+ */
256
+ track(
257
+ eventName: string,
258
+ properties?: Record<string, unknown>,
259
+ options?: { eventId?: string },
260
+ ): void {
261
+ const attributes = this.buildAttributes(eventName, {
262
+ properties,
263
+ eventId: options?.eventId,
264
+ });
265
+
266
+ this.otelLogger.emit({
267
+ timestamp: this.getTimestamp(),
268
+ context: context.active(),
269
+ severityNumber: SeverityNumber.INFO,
270
+ severityText: "INFO",
271
+ attributes,
272
+ });
273
+
274
+ sdkLogger.debug(`Tracked: ${eventName}`);
275
+ }
276
+
277
+ /**
278
+ * Track feedback on a message or response.
279
+ *
280
+ * @param name - Feedback type (e.g. `"thumbs_up"`, `"thumbs_down"`).
281
+ * @param options - Additional feedback context such as comments or conversation ID.
282
+ */
283
+ feedback(name: string, options: FeedbackOptions = {}): void {
284
+ const { comments, conversationId, previousResponseId, eventId, ...extra } =
285
+ options;
286
+
287
+ const properties: Record<string, unknown> = { name, ...extra };
288
+ if (comments) {
289
+ properties.comments = comments;
290
+ }
291
+
292
+ const attributes = this.buildAttributes("introspection.feedback", {
293
+ properties,
294
+ conversationId: conversationId as string | undefined,
295
+ previousResponseId: previousResponseId as string | undefined,
296
+ eventId: eventId as string | undefined,
297
+ });
298
+
299
+ this.otelLogger.emit({
300
+ timestamp: this.getTimestamp(),
301
+ context: context.active(),
302
+ severityNumber: SeverityNumber.INFO,
303
+ severityText: "INFO",
304
+ attributes,
305
+ });
306
+
307
+ sdkLogger.debug(`Feedback: ${name}`);
308
+ }
309
+
310
+ /**
311
+ * Identify a user and associate traits with them.
312
+ *
313
+ * @param userId - Unique identifier for the user.
314
+ * @param traits - Key/value user properties (e.g. `{ email: "…" }`).
315
+ * @param anonymousId - Optional anonymous identifier to link pre-auth sessions.
316
+ * @param eventId - Optional custom event ID override.
317
+ */
318
+ identify(
319
+ userId: string,
320
+ traits?: UserTraits,
321
+ anonymousId?: string,
322
+ eventId?: string,
323
+ ): void {
324
+ this.userId = userId;
325
+ if (anonymousId) {
326
+ this.anonymousId = anonymousId;
327
+ }
328
+ if (traits) {
329
+ this.traits = { ...this.traits, ...traits };
330
+ }
331
+
332
+ const attributes = this.buildAttributes("identify", { traits, eventId });
333
+
334
+ this.otelLogger.emit({
335
+ timestamp: this.getTimestamp(),
336
+ context: context.active(),
337
+ severityNumber: SeverityNumber.INFO,
338
+ severityText: "INFO",
339
+ attributes,
340
+ });
341
+
342
+ sdkLogger.debug(`Identified: ${userId}`);
343
+ }
344
+
345
+ /**
346
+ * Create an OTel context with the given baggage values set.
347
+ *
348
+ * @param values - Key/value pairs to store in OTel baggage.
349
+ * @returns A new context containing the baggage entries.
350
+ */
351
+ createBaggageContext(values: Record<string, string>) {
352
+ const ctx = context.active();
353
+ let bag = propagation.getBaggage(ctx) || propagation.createBaggage();
354
+
355
+ for (const [key, value] of Object.entries(values)) {
356
+ bag = bag.setEntry(key, { value });
357
+ }
358
+
359
+ return propagation.setBaggage(ctx, bag);
360
+ }
361
+
362
+ /**
363
+ * Set multiple baggage values and run a callback within that context.
364
+ *
365
+ * @param values - Key/value pairs to store in OTel baggage.
366
+ * @param callback - Function to execute with the baggage context active.
367
+ * @returns The value returned by `callback`.
368
+ */
369
+ async withBaggage<T>(
370
+ values: Record<string, string>,
371
+ callback: () => T | Promise<T>,
372
+ ): Promise<T> {
373
+ const ctx = this.createBaggageContext(values);
374
+ return await context.with(ctx, callback);
375
+ }
376
+
377
+ /**
378
+ * Set `gen_ai.agent.name` (and optionally `gen_ai.agent.id`) baggage and run
379
+ * a callback within that context.
380
+ *
381
+ * @param agentName - The agent name to propagate.
382
+ * @param agentId - Optional agent identifier.
383
+ * @param callback - Function to execute with the agent baggage active.
384
+ * @returns The value returned by `callback`.
385
+ */
386
+ async withAgent<T>(
387
+ agentName: string,
388
+ agentId: string | undefined,
389
+ callback: () => T | Promise<T>,
390
+ ): Promise<T> {
391
+ const values: Record<string, string> = { "gen_ai.agent.name": agentName };
392
+ if (agentId) {
393
+ values["gen_ai.agent.id"] = agentId;
394
+ }
395
+ return await context.with(this.createBaggageContext(values), callback);
396
+ }
397
+
398
+ /**
399
+ * Set conversation-related baggage (`gen_ai.conversation.id`,
400
+ * `gen_ai.request.previous_response_id`) and run a callback within that
401
+ * context.
402
+ *
403
+ * @param conversationId - The conversation identifier.
404
+ * @param previousResponseId - The ID of the previous response in the conversation.
405
+ * @param callback - Function to execute with the conversation baggage active.
406
+ * @returns The value returned by `callback`.
407
+ */
408
+ async withConversation<T>(
409
+ conversationId: string | undefined,
410
+ previousResponseId: string | undefined,
411
+ callback: () => T | Promise<T>,
412
+ ): Promise<T> {
413
+ const values: Record<string, string> = {};
414
+ if (conversationId) {
415
+ values["gen_ai.conversation.id"] = conversationId;
416
+ }
417
+ if (previousResponseId) {
418
+ values["gen_ai.request.previous_response_id"] = previousResponseId;
419
+ }
420
+ return await context.with(this.createBaggageContext(values), callback);
421
+ }
422
+
423
+ /**
424
+ * Set the user ID on this client instance.
425
+ *
426
+ * @param userId - Unique identifier for the user.
427
+ */
428
+ setUserId(userId: string): void {
429
+ this.userId = userId;
430
+ }
431
+
432
+ /**
433
+ * Set the anonymous ID on this client instance.
434
+ *
435
+ * @param anonymousId - Anonymous identifier used before authentication.
436
+ */
437
+ setAnonymousId(anonymousId: string): void {
438
+ this.anonymousId = anonymousId;
439
+ }
440
+
441
+ /**
442
+ * Set `identity.user_id` baggage and run a callback within that context.
443
+ *
444
+ * @param userId - The user identifier to propagate.
445
+ * @param callback - Function to execute with the user-ID baggage active.
446
+ * @returns The value returned by `callback`.
447
+ */
448
+ async withUserId<T>(
449
+ userId: string,
450
+ callback: () => T | Promise<T>,
451
+ ): Promise<T> {
452
+ return await context.with(
453
+ this.createBaggageContext({ "identity.user_id": userId }),
454
+ callback,
455
+ );
456
+ }
457
+
458
+ /**
459
+ * Set `identity.anonymous_id` baggage and run a callback within that context.
460
+ *
461
+ * @param anonymousId - The anonymous identifier to propagate.
462
+ * @param callback - Function to execute with the anonymous-ID baggage active.
463
+ * @returns The value returned by `callback`.
464
+ */
465
+ async withAnonymousId<T>(
466
+ anonymousId: string,
467
+ callback: () => T | Promise<T>,
468
+ ): Promise<T> {
469
+ return await context.with(
470
+ this.createBaggageContext({ "identity.anonymous_id": anonymousId }),
471
+ callback,
472
+ );
473
+ }
474
+
475
+ /**
476
+ * Get the current anonymous ID stored on this client instance.
477
+ *
478
+ * @returns The anonymous ID, or `undefined` if not set.
479
+ */
480
+ getAnonymousId(): string | undefined {
481
+ return this.anonymousId;
482
+ }
483
+
484
+ /**
485
+ * Reset all client-side state (user ID, anonymous ID, and traits).
486
+ */
487
+ reset(): void {
488
+ this.userId = undefined;
489
+ this.anonymousId = undefined;
490
+ this.traits = {};
491
+ sdkLogger.debug("Client state reset");
492
+ }
493
+
494
+ /**
495
+ * Flush all queued log records to the backend.
496
+ *
497
+ * @returns A promise that resolves once the flush completes.
498
+ */
499
+ async flush(): Promise<void> {
500
+ await this.loggerProvider.forceFlush();
501
+ sdkLogger.debug("Flushed pending log records");
502
+ }
503
+
504
+ /**
505
+ * Shut down the client, flushing all pending log records.
506
+ *
507
+ * @returns A promise that resolves once shutdown is complete.
508
+ */
509
+ async shutdown(): Promise<void> {
510
+ await this.loggerProvider.shutdown();
511
+ sdkLogger.debug("IntrospectionClient shutdown complete");
512
+ }
513
+ }