@introspection-sdk/introspection-node 0.4.0 → 0.4.1

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 (41) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/LICENSE +191 -0
  3. package/dist/converters/ai-sdk.d.ts +1 -1
  4. package/dist/converters/ai-sdk.d.ts.map +1 -1
  5. package/dist/converters/ai-sdk.js +24 -1
  6. package/dist/converters/claude.d.ts +1 -1
  7. package/dist/converters/claude.d.ts.map +1 -1
  8. package/dist/converters/claude.js +1 -1
  9. package/dist/converters/openai.d.ts +1 -1
  10. package/dist/converters/openai.d.ts.map +1 -1
  11. package/dist/converters/openinference.d.ts +1 -1
  12. package/dist/converters/openinference.d.ts.map +1 -1
  13. package/dist/converters/openinference.js +1 -1
  14. package/dist/index.d.ts +2 -2
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +1 -1
  17. package/dist/langchain-handler.d.ts +7 -5
  18. package/dist/langchain-handler.d.ts.map +1 -1
  19. package/dist/langchain-handler.js +55 -26
  20. package/dist/span-processor.d.ts.map +1 -1
  21. package/dist/span-processor.js +3 -0
  22. package/package.json +4 -2
  23. package/src/ai-sdk-integration.ts +1 -1
  24. package/src/anthropic.ts +1 -1
  25. package/src/converters/ai-sdk.ts +27 -2
  26. package/src/converters/claude.ts +2 -2
  27. package/src/converters/openai.ts +1 -1
  28. package/src/converters/openinference.ts +2 -2
  29. package/src/index.ts +2 -2
  30. package/src/langchain-handler.ts +64 -33
  31. package/src/mastra-exporter.ts +1 -1
  32. package/src/span-processor.ts +3 -0
  33. package/.turbo/turbo-check-types.log +0 -5
  34. package/.turbo/turbo-lint.log +0 -5
  35. package/dist/converters/openclaw.d.ts +0 -16
  36. package/dist/converters/openclaw.d.ts.map +0 -1
  37. package/dist/converters/openclaw.js +0 -49
  38. package/dist/types/genai.d.ts +0 -165
  39. package/dist/types/genai.d.ts.map +0 -1
  40. package/dist/types/genai.js +0 -82
  41. package/src/types/genai.ts +0 -264
package/package.json CHANGED
@@ -1,6 +1,8 @@
1
1
  {
2
2
  "name": "@introspection-sdk/introspection-node",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
+ "license": "Apache-2.0",
5
+ "description": "Introspection observability SDK for Node.js",
4
6
  "type": "module",
5
7
  "main": "./dist/index.js",
6
8
  "types": "./dist/index.d.ts",
@@ -68,7 +70,7 @@
68
70
  "@opentelemetry/sdk-logs": "^0.213.0",
69
71
  "@opentelemetry/sdk-trace-base": "^2.6.0",
70
72
  "@opentelemetry/semantic-conventions": "^1.40.0",
71
- "@introspection-sdk/types": "0.4.0"
73
+ "@introspection-sdk/types": "0.4.1"
72
74
  },
73
75
  "scripts": {
74
76
  "lint": "eslint . --max-warnings 0",
@@ -55,7 +55,7 @@ import {
55
55
  buildOutputMessages,
56
56
  convertToolsToToolDefinitions,
57
57
  } from "./converters/ai-sdk.js";
58
- import type { InputMessage } from "./types/genai.js";
58
+ import type { InputMessage } from "@introspection-sdk/types";
59
59
  import { logger } from "./utils.js";
60
60
  import { VERSION } from "./version.js";
61
61
 
package/src/anthropic.ts CHANGED
@@ -22,7 +22,7 @@ import type {
22
22
  OutputMessage,
23
23
  MessagePart,
24
24
  ReasoningPart,
25
- } from "./types/genai.js";
25
+ } from "@introspection-sdk/types";
26
26
 
27
27
  import { trace, SpanKind, SpanStatusCode } from "@opentelemetry/api";
28
28
  import type { Tracer, Span } from "@opentelemetry/api";
@@ -14,7 +14,7 @@ import type {
14
14
  SystemInstruction,
15
15
  ToolDefinition,
16
16
  MessagePart,
17
- } from "../types/genai.js";
17
+ } from "@introspection-sdk/types";
18
18
 
19
19
  // ---------------------------------------------------------------------------
20
20
  // Local types — minimal shapes matching AI SDK data structures.
@@ -192,7 +192,7 @@ export function convertToolsToToolDefinitions(
192
192
  name,
193
193
  description:
194
194
  typeof t.description === "string" ? t.description : undefined,
195
- parameters: extractToolParameters(t.parameters),
195
+ parameters: extractToolParameters(t.inputSchema ?? t.parameters),
196
196
  });
197
197
  }
198
198
 
@@ -284,6 +284,31 @@ function extractToolParameters(
284
284
  if (!parameters || typeof parameters !== "object") return undefined;
285
285
  const p = parameters as Record<string, unknown>;
286
286
 
287
+ // Zod 4 schema — exposes a toJSONSchema() method.
288
+ if (typeof p.toJSONSchema === "function") {
289
+ const jsonSchema = p.toJSONSchema();
290
+ if (jsonSchema && typeof jsonSchema === "object") {
291
+ return jsonSchema as Record<string, unknown>;
292
+ }
293
+ }
294
+
295
+ // AI SDK v6 tools expose inputSchema instead of parameters. Zod-backed
296
+ // schemas usually carry a JSON Schema factory on this property.
297
+ if (p["~standard"] && typeof p["~standard"] === "object") {
298
+ const standard = p["~standard"] as Record<string, unknown>;
299
+ const standardJsonSchema = standard.jsonSchema;
300
+ if (standardJsonSchema && typeof standardJsonSchema === "object") {
301
+ const input = (standardJsonSchema as Record<string, unknown>).input;
302
+ if (typeof input === "function") {
303
+ const jsonSchema = input();
304
+ if (jsonSchema && typeof jsonSchema === "object") {
305
+ return jsonSchema as Record<string, unknown>;
306
+ }
307
+ }
308
+ return standardJsonSchema as Record<string, unknown>;
309
+ }
310
+ }
311
+
287
312
  // jsonSchema() wrapper — has a jsonSchema property
288
313
  if (p.jsonSchema && typeof p.jsonSchema === "object") {
289
314
  return p.jsonSchema as Record<string, unknown>;
@@ -12,8 +12,8 @@ import type {
12
12
  ReasoningPart,
13
13
  GenAiAttributes,
14
14
  ToolDefinition,
15
- } from "../types/genai.js";
16
- import { toAttributes } from "../types/genai.js";
15
+ } from "@introspection-sdk/types";
16
+ import { toAttributes } from "@introspection-sdk/types";
17
17
 
18
18
  /** A plain-text content block from the Claude API. */
19
19
  export interface ClaudeTextBlock {
@@ -14,7 +14,7 @@ import type {
14
14
  ReasoningPart,
15
15
  ToolDefinition,
16
16
  SystemInstruction,
17
- } from "../types/genai.js";
17
+ } from "@introspection-sdk/types";
18
18
 
19
19
  /** Re-export OpenAI types used by callers (e.g. tracing-processor). */
20
20
  export type ResponseInputItem = Responses.ResponseInputItem;
@@ -7,8 +7,8 @@ import type {
7
7
  ReasoningPart,
8
8
  SystemInstruction,
9
9
  ToolDefinition,
10
- } from "../types/genai.js";
11
- import { toAttributes } from "../types/genai.js";
10
+ } from "@introspection-sdk/types";
11
+ import { toAttributes } from "@introspection-sdk/types";
12
12
 
13
13
  const OI = {
14
14
  MODEL_NAME: "llm.model_name",
package/src/index.ts CHANGED
@@ -82,8 +82,8 @@ export type {
82
82
  ToolCallRequestPart,
83
83
  ToolCallResponsePart,
84
84
  MessagePart,
85
- } from "./types/genai.js";
86
- export { toAttributes } from "./types/genai.js";
85
+ } from "@introspection-sdk/types";
86
+ export { toAttributes } from "@introspection-sdk/types";
87
87
 
88
88
  // OpenAI converter exports
89
89
  export {
@@ -34,6 +34,8 @@ import type { ChainValues } from "@langchain/core/utils/types";
34
34
 
35
35
  import {
36
36
  context as otelContext,
37
+ ROOT_CONTEXT,
38
+ SpanStatusCode,
37
39
  trace as otelTrace,
38
40
  type Span as OtelSpan,
39
41
  } from "@opentelemetry/api";
@@ -55,7 +57,7 @@ import type {
55
57
  InputMessage,
56
58
  OutputMessage,
57
59
  MessagePart,
58
- } from "./types/genai.js";
60
+ } from "@introspection-sdk/types";
59
61
 
60
62
  // ---------------------------------------------------------------------------
61
63
  // Configuration
@@ -102,11 +104,11 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
102
104
  private _tracerProvider: BasicTracerProvider;
103
105
  private _tracer: ReturnType<BasicTracerProvider["getTracer"]>;
104
106
  private _spans: Map<string, OtelSpan> = new Map();
105
- private _rootSpan: OtelSpan | null = null;
106
107
  private _conversationId: string;
107
108
  // Track span names and parents so children can resolve gen_ai.agent.name
108
109
  private _spanNames: Map<string, string> = new Map();
109
110
  private _spanParents: Map<string, string> = new Map();
111
+ private _runRoots: Map<string, string> = new Map();
110
112
  // LangChain wrapper names to skip when resolving agent names
111
113
  private static _wrapperNames = new Set([
112
114
  "RunnableSequence",
@@ -203,29 +205,39 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
203
205
  this._conversationId = `intro_conv_${randomUUID().replace(/-/g, "")}`;
204
206
  }
205
207
 
206
- /** Get or create a root span so all callbacks share the same traceId. */
207
- private _ensureRoot(): OtelSpan {
208
- if (!this._rootSpan) {
209
- this._rootSpan = this._tracer.startSpan("langchain-run");
210
- this._rootSpan.setAttribute(
211
- "gen_ai.conversation.id",
212
- this._conversationId,
213
- );
214
- }
215
- return this._rootSpan;
208
+ private _rootRunId(runId: string, parentRunId?: string): string {
209
+ if (!parentRunId) return runId;
210
+ return this._runRoots.get(parentRunId) || parentRunId;
211
+ }
212
+
213
+ /** Keep the top-level run span aligned with child conversation IDs. */
214
+ private _setRootConversationId(
215
+ conversationId: string,
216
+ runId: string,
217
+ parentRunId?: string,
218
+ ): void {
219
+ const root = this._spans.get(this._rootRunId(runId, parentRunId));
220
+ root?.setAttribute("gen_ai.conversation.id", conversationId);
221
+ }
222
+
223
+ private _endRootIfComplete(runId: string): void {
224
+ this._runRoots.delete(runId);
225
+ this._spanNames.delete(runId);
226
+ this._spanParents.delete(runId);
216
227
  }
217
228
 
218
- /** Create a child span under the root (or under a parent if provided).
229
+ /** Create a span under a LangChain parent when one exists.
219
230
  * Sets gen_ai.agent.name to the parent span's name for hierarchy. */
220
231
  private _createChildSpan(
221
232
  name: string,
222
233
  runId: string,
223
234
  parentRunId?: string,
224
235
  ): OtelSpan {
225
- const parent =
226
- (parentRunId ? this._spans.get(parentRunId) : undefined) ||
227
- this._ensureRoot();
228
- const ctx = otelTrace.setSpan(otelContext.active(), parent);
236
+ const parent = parentRunId ? this._spans.get(parentRunId) : undefined;
237
+ this._runRoots.set(runId, this._rootRunId(runId, parentRunId));
238
+ const ctx = parent
239
+ ? otelTrace.setSpan(otelContext.active(), parent)
240
+ : ROOT_CONTEXT;
229
241
  const span = this._tracer.startSpan(name, {}, ctx);
230
242
  this._spanNames.set(runId, name);
231
243
  if (parentRunId) {
@@ -262,6 +274,7 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
262
274
  this._spans.set(runId, otelSpan);
263
275
 
264
276
  const conversationId = this._getConversationId(metadata);
277
+ this._setRootConversationId(conversationId, runId, parentRunId);
265
278
 
266
279
  otelSpan.setAttribute("gen_ai.operation.name", "chat");
267
280
  otelSpan.setAttribute("gen_ai.conversation.id", conversationId);
@@ -346,6 +359,7 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
346
359
  this._spans.set(runId, otelSpan);
347
360
 
348
361
  const conversationId = this._getConversationId(metadata);
362
+ this._setRootConversationId(conversationId, runId, parentRunId);
349
363
 
350
364
  otelSpan.setAttribute("gen_ai.operation.name", "chat");
351
365
  otelSpan.setAttribute("gen_ai.conversation.id", conversationId);
@@ -375,6 +389,7 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
375
389
  this._spans.delete(runId);
376
390
  this._spanNames.delete(runId);
377
391
  this._llmInputs.delete(runId);
392
+ this._endRootIfComplete(runId);
378
393
 
379
394
  const generations = output.generations?.[0];
380
395
  if (generations && generations.length > 0) {
@@ -458,8 +473,10 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
458
473
  this._spans.delete(runId);
459
474
  this._spanNames.delete(runId);
460
475
  this._llmInputs.delete(runId);
461
- otelSpan.setAttribute("error", true);
462
- otelSpan.setAttribute("error.message", err.message);
476
+ this._endRootIfComplete(runId);
477
+ otelSpan.recordException(err);
478
+ otelSpan.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
479
+ otelSpan.setAttribute("exception.message", err.message);
463
480
  otelSpan.end();
464
481
  }
465
482
 
@@ -484,6 +501,7 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
484
501
  this._spans.set(runId, otelSpan);
485
502
 
486
503
  const conversationId = this._getConversationId(metadata);
504
+ this._setRootConversationId(conversationId, runId, parentRunId);
487
505
  otelSpan.setAttribute("gen_ai.conversation.id", conversationId);
488
506
  }
489
507
 
@@ -491,6 +509,7 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
491
509
  const otelSpan = this._spans.get(runId);
492
510
  if (!otelSpan) return;
493
511
  this._spans.delete(runId);
512
+ this._endRootIfComplete(runId);
494
513
  otelSpan.end();
495
514
  }
496
515
 
@@ -498,8 +517,10 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
498
517
  const otelSpan = this._spans.get(runId);
499
518
  if (!otelSpan) return;
500
519
  this._spans.delete(runId);
501
- otelSpan.setAttribute("error", true);
502
- otelSpan.setAttribute("error.message", err.message);
520
+ this._endRootIfComplete(runId);
521
+ otelSpan.recordException(err);
522
+ otelSpan.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
523
+ otelSpan.setAttribute("exception.message", err.message);
503
524
  otelSpan.end();
504
525
  }
505
526
 
@@ -513,7 +534,7 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
513
534
  runId: string,
514
535
  parentRunId?: string,
515
536
  _tags?: string[],
516
- _metadata?: Record<string, unknown>,
537
+ metadata?: Record<string, unknown>,
517
538
  runName?: string,
518
539
  ): void {
519
540
  const toolName =
@@ -522,16 +543,20 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
522
543
  const otelSpan = this._createChildSpan(toolName, runId, parentRunId);
523
544
  this._spans.set(runId, otelSpan);
524
545
 
546
+ const conversationId = this._getConversationId(metadata);
547
+ this._setRootConversationId(conversationId, runId, parentRunId);
548
+
525
549
  otelSpan.setAttribute("gen_ai.tool.name", toolName);
526
550
  otelSpan.setAttribute("openinference.span.kind", "TOOL");
527
551
  otelSpan.setAttribute("gen_ai.tool.input", input);
528
- otelSpan.setAttribute("gen_ai.conversation.id", this._conversationId);
552
+ otelSpan.setAttribute("gen_ai.conversation.id", conversationId);
529
553
  }
530
554
 
531
555
  handleToolEnd(output: unknown, runId: string): void {
532
556
  const otelSpan = this._spans.get(runId);
533
557
  if (!otelSpan) return;
534
558
  this._spans.delete(runId);
559
+ this._endRootIfComplete(runId);
535
560
 
536
561
  if (output != null) {
537
562
  otelSpan.setAttribute(
@@ -547,8 +572,10 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
547
572
  const otelSpan = this._spans.get(runId);
548
573
  if (!otelSpan) return;
549
574
  this._spans.delete(runId);
550
- otelSpan.setAttribute("error", true);
551
- otelSpan.setAttribute("error.message", err.message);
575
+ this._endRootIfComplete(runId);
576
+ otelSpan.recordException(err);
577
+ otelSpan.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
578
+ otelSpan.setAttribute("exception.message", err.message);
552
579
  otelSpan.end();
553
580
  }
554
581
 
@@ -559,10 +586,9 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
559
586
  async shutdown(): Promise<void> {
560
587
  for (const span of this._spans.values()) span.end();
561
588
  this._spans.clear();
562
- if (this._rootSpan) {
563
- this._rootSpan.end();
564
- this._rootSpan = null;
565
- }
589
+ this._spanNames.clear();
590
+ this._spanParents.clear();
591
+ this._runRoots.clear();
566
592
  await this._tracerProvider.shutdown();
567
593
  }
568
594
 
@@ -623,10 +649,15 @@ export class IntrospectionCallbackHandler extends BaseCallbackHandler {
623
649
  }
624
650
 
625
651
  private _getConversationId(metadata?: Record<string, unknown>): string {
626
- const metaConvId = metadata?.["gen_ai.conversation.id"] as
627
- | string
628
- | undefined;
629
- return metaConvId || this._conversationId;
652
+ const metaConvId = metadata?.["gen_ai.conversation.id"];
653
+ if (metaConvId !== undefined && metaConvId !== null) {
654
+ return String(metaConvId);
655
+ }
656
+ const threadId = metadata?.["thread_id"];
657
+ if (threadId !== undefined && threadId !== null) {
658
+ return String(threadId);
659
+ }
660
+ return this._conversationId;
630
661
  }
631
662
 
632
663
  private _convertMessages(messages: BaseMessage[]): {
@@ -57,7 +57,7 @@ import type {
57
57
  InputMessage,
58
58
  OutputMessage,
59
59
  MessagePart,
60
- } from "./types/genai.js";
60
+ } from "@introspection-sdk/types";
61
61
 
62
62
  // ---------------------------------------------------------------------------
63
63
  // Configuration
@@ -65,6 +65,9 @@ class ConvertedReadableSpan implements ReadableSpan {
65
65
  spanContext() {
66
66
  return this._original.spanContext();
67
67
  }
68
+ get parentSpanContext() {
69
+ return this._original.parentSpanContext;
70
+ }
68
71
  get startTime() {
69
72
  return this._original.startTime;
70
73
  }
@@ -1,5 +0,0 @@
1
-
2
- 
3
- > @introspection-sdk/introspection-node@0.1.0 check-types /Users/julian/projects/introspection-org/introspection-js-sdk/packages/introspection-node
4
- > tsc --noEmit
5
-
@@ -1,5 +0,0 @@
1
-
2
- > @introspection-sdk/introspection-node@0.1.0 lint /Users/julian/projects/introspection-org/introspection-js-sdk/packages/introspection-node
3
- > eslint . --max-warnings 0
4
-
5
- Warning: React version was set to "detect" in eslint-plugin-react settings, but the "react" package is not installed. Assuming latest React version for linting.
@@ -1,16 +0,0 @@
1
- import type { Attributes } from "@opentelemetry/api";
2
- /**
3
- * Detect openclaw spans by the presence of openclaw.* attributes.
4
- * openclaw does not set a distinguishing instrumentation scope, so we
5
- * use attribute-key presence instead.
6
- */
7
- export declare function isOpenClawSpan(attrs?: Attributes): boolean;
8
- /**
9
- * Replace mapped openclaw.* attributes with gen_ai semantic conventions.
10
- * Unmapped openclaw.* attributes (e.g. openclaw.channel, openclaw.sessionId)
11
- * are preserved unchanged.
12
- */
13
- export declare function replaceOpenClawWithGenAI(
14
- attrs?: Attributes,
15
- ): Attributes;
16
- //# sourceMappingURL=openclaw.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"openclaw.d.ts","sourceRoot":"","sources":["../../src/converters/openclaw.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAiBrD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,CAAC,EAAE,UAAU,GAAG,OAAO,CAG1D;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,CAAC,EAAE,UAAU,GAAG,UAAU,CAyBvE"}
@@ -1,49 +0,0 @@
1
- /**
2
- * OpenClaw attribute keys that have gen_ai semantic convention equivalents.
3
- * Other openclaw.* attributes (channel, sessionId, lane, etc.) are preserved as-is.
4
- *
5
- * @see https://github.com/openclaw/openclaw/tree/main/extensions/diagnostics-otel
6
- */
7
- const OC = {
8
- MODEL: "openclaw.model",
9
- PROVIDER: "openclaw.provider",
10
- TOKENS_INPUT: "openclaw.tokens.input",
11
- TOKENS_OUTPUT: "openclaw.tokens.output",
12
- };
13
- const MAPPED_KEYS = new Set(Object.values(OC));
14
- /**
15
- * Detect openclaw spans by the presence of openclaw.* attributes.
16
- * openclaw does not set a distinguishing instrumentation scope, so we
17
- * use attribute-key presence instead.
18
- */
19
- export function isOpenClawSpan(attrs) {
20
- if (!attrs) return false;
21
- return Object.keys(attrs).some((key) => key.startsWith("openclaw."));
22
- }
23
- /**
24
- * Replace mapped openclaw.* attributes with gen_ai semantic conventions.
25
- * Unmapped openclaw.* attributes (e.g. openclaw.channel, openclaw.sessionId)
26
- * are preserved unchanged.
27
- */
28
- export function replaceOpenClawWithGenAI(attrs) {
29
- if (!attrs) return {};
30
- const result = {};
31
- for (const [key, value] of Object.entries(attrs)) {
32
- if (!MAPPED_KEYS.has(key)) {
33
- result[key] = value;
34
- }
35
- }
36
- if (attrs[OC.MODEL] !== undefined) {
37
- result["gen_ai.request.model"] = attrs[OC.MODEL];
38
- }
39
- if (attrs[OC.PROVIDER] !== undefined) {
40
- result["gen_ai.system"] = attrs[OC.PROVIDER];
41
- }
42
- if (attrs[OC.TOKENS_INPUT] !== undefined) {
43
- result["gen_ai.usage.input_tokens"] = attrs[OC.TOKENS_INPUT];
44
- }
45
- if (attrs[OC.TOKENS_OUTPUT] !== undefined) {
46
- result["gen_ai.usage.output_tokens"] = attrs[OC.TOKENS_OUTPUT];
47
- }
48
- return result;
49
- }
@@ -1,165 +0,0 @@
1
- /**
2
- * OTel Gen AI Semantic Convention types.
3
- *
4
- * These types represent the standardized format for gen_ai.input.messages
5
- * and gen_ai.output.messages attributes as per OpenTelemetry Gen AI semantic conventions.
6
- *
7
- * @see https://opentelemetry.io/docs/specs/semconv/gen-ai/
8
- */
9
- /**
10
- * A text content part in a message.
11
- *
12
- * @example
13
- * ```ts
14
- * const part: TextPart = { type: "text", content: "Hello" };
15
- * ```
16
- */
17
- export interface TextPart {
18
- /** Discriminator — always `"text"`. */
19
- type: "text";
20
- /** The text body of this part. */
21
- content: string;
22
- }
23
- /**
24
- * A tool / function-call request part in a message.
25
- *
26
- * @example
27
- * ```ts
28
- * const part: ToolCallRequestPart = { type: "tool_call", name: "get_weather", arguments: { city: "SF" } };
29
- * ```
30
- */
31
- export interface ToolCallRequestPart {
32
- /** Discriminator — always `"tool_call"`. */
33
- type: "tool_call";
34
- /** Function / tool name. */
35
- name: string;
36
- /** Correlation ID linking request to response. */
37
- id?: string;
38
- /** Arguments passed to the tool. */
39
- arguments?: unknown;
40
- }
41
- /**
42
- * A tool / function-call response part in a message.
43
- *
44
- * @example
45
- * ```ts
46
- * const part: ToolCallResponsePart = { type: "tool_call_response", response: "72°F" };
47
- * ```
48
- */
49
- export interface ToolCallResponsePart {
50
- /** Discriminator — always `"tool_call_response"`. */
51
- type: "tool_call_response";
52
- /** The value returned by the tool. */
53
- response: unknown;
54
- /** Correlation ID linking response to request. */
55
- id?: string;
56
- }
57
- /**
58
- * A reasoning / thinking content part in a message.
59
- *
60
- * @example
61
- * ```ts
62
- * const part: ReasoningPart = { type: "thinking", content: "Let me think through this..." };
63
- * ```
64
- */
65
- export interface ReasoningPart {
66
- /** Discriminator — always `"thinking"`. */
67
- type: "thinking";
68
- /** The reasoning / thinking summary content. */
69
- content?: string;
70
- /** Encrypted reasoning signature (maps to OpenAI encrypted_content, Anthropic signature/redacted_thinking data). */
71
- signature?: string;
72
- /** Provider that produced this thinking block (e.g. `"anthropic"`, `"openai"`). Used to reconstruct the correct wire format on replay. */
73
- provider_name?: string;
74
- }
75
- /** Union of all possible message-part shapes. */
76
- export type MessagePart = TextPart | ToolCallRequestPart | ToolCallResponsePart | ReasoningPart;
77
- /**
78
- * A system instruction entry for `gen_ai.system_instructions`.
79
- */
80
- export interface SystemInstruction {
81
- /** Part type — always `"text"`. */
82
- type: "text";
83
- /** The instruction text content. */
84
- content: string;
85
- }
86
- /**
87
- * An input message in OTel Gen AI semantic convention format.
88
- *
89
- * Used for the `gen_ai.input.messages` span attribute.
90
- */
91
- export interface InputMessage {
92
- /** The role of the message sender. */
93
- role: "system" | "user" | "assistant" | "tool";
94
- /** Ordered content parts that compose this message. */
95
- parts: MessagePart[];
96
- /** Optional tool name when `role` is `"tool"`. */
97
- name?: string;
98
- }
99
- /**
100
- * An output message in OTel Gen AI semantic convention format.
101
- *
102
- * Used for the `gen_ai.output.messages` span attribute.
103
- */
104
- export interface OutputMessage {
105
- /** The role of the message sender. */
106
- role: "system" | "user" | "assistant" | "tool";
107
- /** Ordered content parts that compose this message. */
108
- parts: MessagePart[];
109
- /** Model-reported finish reason (e.g. `"stop"`, `"end_turn"`). */
110
- finish_reason?: string;
111
- /** Optional tool name when `role` is `"tool"`. */
112
- name?: string;
113
- }
114
- /**
115
- * A tool definition for the `gen_ai.tool.definitions` attribute.
116
- *
117
- * @example
118
- * ```ts
119
- * const def: ToolDefinition = { name: "get_weather", description: "Get current weather" };
120
- * ```
121
- */
122
- export interface ToolDefinition {
123
- /** Tool / function name. */
124
- name: string;
125
- /** Human-readable description of what the tool does. */
126
- description?: string;
127
- /** JSON Schema describing the tool's parameters. */
128
- parameters?: Record<string, unknown>;
129
- }
130
- /**
131
- * GenAI semantic convention attributes.
132
- *
133
- * This interface represents the standardized GenAI attributes that can be
134
- * converted to OpenTelemetry semantic convention format (gen_ai.* with dots).
135
- *
136
- * All properties use camelCase following TypeScript conventions.
137
- */
138
- export interface GenAiAttributes {
139
- /** Model name (gen_ai.request.model) */
140
- requestModel?: string;
141
- /** System prompt (gen_ai.system) */
142
- system?: string;
143
- /** Operation name (gen_ai.operation.name) */
144
- operationName?: string;
145
- /** Tool definitions (gen_ai.tool.definitions) — serialized to JSON by {@link toAttributes}. */
146
- toolDefinitions?: ToolDefinition[];
147
- /** Input messages (gen_ai.input.messages) — serialized to JSON by {@link toAttributes}. */
148
- inputMessages?: InputMessage[];
149
- /** Output messages (gen_ai.output.messages) — serialized to JSON by {@link toAttributes}. */
150
- outputMessages?: OutputMessage[];
151
- /** System instructions (gen_ai.system_instructions) — serialized to JSON by {@link toAttributes}. */
152
- systemInstructions?: SystemInstruction[];
153
- /** Response ID (gen_ai.response.id) */
154
- responseId?: string;
155
- /** Input token count (gen_ai.usage.input_tokens) */
156
- inputTokens?: number;
157
- /** Output token count (gen_ai.usage.output_tokens) */
158
- outputTokens?: number;
159
- /** Cache creation input tokens (gen_ai.usage.cache_creation_input_tokens) */
160
- cacheCreationInputTokens?: number;
161
- /** Cache read input tokens (gen_ai.usage.cache_read_input_tokens) */
162
- cacheReadInputTokens?: number;
163
- }
164
- export declare function toAttributes(attrs: GenAiAttributes): Record<string, string | number>;
165
- //# sourceMappingURL=genai.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"genai.d.ts","sourceRoot":"","sources":["../../src/types/genai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;GAOG;AACH,MAAM,WAAW,QAAQ;IACvB,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,kCAAkC;IAClC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,mBAAmB;IAClC,4CAA4C;IAC5C,IAAI,EAAE,WAAW,CAAC;IAClB,4BAA4B;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,kDAAkD;IAClD,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,oCAAoC;IACpC,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,oBAAoB;IACnC,qDAAqD;IACrD,IAAI,EAAE,oBAAoB,CAAC;IAC3B,sCAAsC;IACtC,QAAQ,EAAE,OAAO,CAAC;IAClB,kDA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