tracia 0.3.12 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -66,6 +66,11 @@ declare enum LLMProvider {
66
66
  GOOGLE = "google",
67
67
  AMAZON_BEDROCK = "amazon_bedrock"
68
68
  }
69
+ declare enum SpanKind {
70
+ ROOT = "ROOT",
71
+ LLM = "LLM",
72
+ EMBEDDING = "EMBEDDING"
73
+ }
69
74
  interface ApiSuccessResponse {
70
75
  text: string;
71
76
  spanId: string;
@@ -341,7 +346,8 @@ interface CreateSpanPayload {
341
346
  model: string;
342
347
  provider: LLMProvider;
343
348
  input: {
344
- messages: LocalPromptMessage[];
349
+ messages?: LocalPromptMessage[];
350
+ text?: string | string[];
345
351
  };
346
352
  variables: Record<string, string> | null;
347
353
  output: string | null;
@@ -363,6 +369,8 @@ interface CreateSpanPayload {
363
369
  traceId?: string;
364
370
  /** Parent span ID for chaining spans in a sequence */
365
371
  parentSpanId?: string;
372
+ /** Span kind override (for embedding spans) */
373
+ spanKind?: SpanKind;
366
374
  }
367
375
  interface CreateSpanResult {
368
376
  spanId: string;
@@ -561,6 +569,65 @@ interface ResponsesStream {
561
569
  /** Abort the stream */
562
570
  abort(): void;
563
571
  }
572
+ interface RunEmbeddingInput {
573
+ /** Text(s) to embed */
574
+ input: string | string[];
575
+ /** Embedding model (e.g., 'text-embedding-3-small') */
576
+ model: string;
577
+ /** Explicitly specify the provider */
578
+ provider?: LLMProvider;
579
+ /** Provider API key override */
580
+ providerApiKey?: string;
581
+ /** Optional dimension override */
582
+ dimensions?: number;
583
+ /** Whether to send span to Tracia (default: true) */
584
+ sendTrace?: boolean;
585
+ /** Custom span ID */
586
+ spanId?: string;
587
+ /** Tags for the span */
588
+ tags?: string[];
589
+ /** User ID for the span */
590
+ userId?: string;
591
+ /** Session ID for the span */
592
+ sessionId?: string;
593
+ /** Trace ID to group related spans together */
594
+ traceId?: string;
595
+ /** Parent span ID for chaining spans in a sequence */
596
+ parentSpanId?: string;
597
+ /** Timeout in milliseconds */
598
+ timeoutMs?: number;
599
+ }
600
+ interface EmbeddingVector {
601
+ /** The embedding values */
602
+ values: number[];
603
+ /** Index of this embedding in the input array */
604
+ index: number;
605
+ }
606
+ interface EmbeddingUsage {
607
+ /** Total tokens used for the embedding request */
608
+ totalTokens: number;
609
+ }
610
+ interface RunEmbeddingResult {
611
+ /** The generated embeddings */
612
+ embeddings: EmbeddingVector[];
613
+ /** Span ID for this request */
614
+ spanId: string;
615
+ /** Trace ID grouping related spans */
616
+ traceId: string;
617
+ /** Latency in milliseconds */
618
+ latencyMs: number;
619
+ /** Token usage */
620
+ usage: EmbeddingUsage;
621
+ /**
622
+ * Estimated cost in USD.
623
+ * Currently always `null` — cost is calculated server-side when the span is ingested.
624
+ */
625
+ cost: number | null;
626
+ /** The provider used */
627
+ provider: LLMProvider;
628
+ /** The model used */
629
+ model: string;
630
+ }
564
631
  /** @deprecated Use SpanStatus instead */
565
632
  type TraceStatus = SpanStatus;
566
633
  /** @deprecated Use SpanListItem instead */
@@ -612,6 +679,11 @@ type SessionRunLocalInput = Omit<RunLocalInput, 'traceId' | 'parentSpanId'>;
612
679
  * as those are managed by the session.
613
680
  */
614
681
  type SessionRunResponsesInput = Omit<RunResponsesInput, 'traceId' | 'parentSpanId'>;
682
+ /**
683
+ * Input for session.runEmbedding() - same as RunEmbeddingInput but without traceId/parentSpanId
684
+ * as those are managed by the session.
685
+ */
686
+ type SessionRunEmbeddingInput = Omit<RunEmbeddingInput, 'traceId' | 'parentSpanId'>;
615
687
  /**
616
688
  * A session for grouping related spans together under a single trace.
617
689
  *
@@ -691,6 +763,11 @@ declare class TraciaSession {
691
763
  }): Promise<RunResponsesResult>;
692
764
  private runResponsesNonStreaming;
693
765
  private runResponsesStreaming;
766
+ /**
767
+ * Generate embeddings, automatically linking the span to this session.
768
+ * See Tracia.runEmbedding() for full documentation.
769
+ */
770
+ runEmbedding(input: SessionRunEmbeddingInput): Promise<RunEmbeddingResult>;
694
771
  private updateSessionState;
695
772
  }
696
773
 
@@ -814,6 +891,29 @@ declare class Tracia {
814
891
  private createResponsesStream;
815
892
  private createLocalStream;
816
893
  private combineAbortSignals;
894
+ /**
895
+ * Generate embeddings for text input(s) using an embedding model.
896
+ *
897
+ * @example Single text
898
+ * ```typescript
899
+ * const result = await tracia.runEmbedding({
900
+ * model: 'text-embedding-3-small',
901
+ * input: 'Hello world',
902
+ * })
903
+ * console.log(result.embeddings[0].values.length) // 1536
904
+ * ```
905
+ *
906
+ * @example Batch
907
+ * ```typescript
908
+ * const result = await tracia.runEmbedding({
909
+ * model: 'text-embedding-3-small',
910
+ * input: ['Hello', 'World'],
911
+ * })
912
+ * console.log(result.embeddings.length) // 2
913
+ * ```
914
+ */
915
+ runEmbedding(input: RunEmbeddingInput): Promise<RunEmbeddingResult>;
916
+ private validateRunEmbeddingInput;
817
917
  flush(): Promise<void>;
818
918
  /**
819
919
  * Create a new session for grouping related spans together under a single trace.
@@ -859,4 +959,4 @@ declare class Tracia {
859
959
  private getProviderApiKey;
860
960
  }
861
961
 
862
- export { type ContentPart, type CreatePromptOptions, type CreateSpanPayload, type CreateSpanResult, type CreateTracePayload, type CreateTraceResult, Eval, type EvaluateOptions, type EvaluateResult, type FinishReason, type JsonSchemaProperty, LLMProvider, type ListSpansOptions, type ListSpansResult, type ListTracesOptions, type ListTracesResult, type LocalPromptMessage, type LocalStream, type MessageRole, type Prompt, type PromptListItem, type PromptMessage, type ResponsesEvent, type ResponsesInputItem, type ResponsesOutputItem, type ResponsesStream, type RunLocalInput, type RunLocalResult, type RunOptions, type RunResponsesInput, type RunResponsesResult, type RunResult, type RunVariables, type SessionRunLocalInput, type SessionRunResponsesInput, type Span, type SpanListItem, type SpanStatus, type StreamResult, type TextPart, type TokenUsage, type ToolCall, type ToolCallPart, type ToolChoice, type ToolDefinition, type ToolParameters, type Trace, type TraceListItem, type TraceStatus, Tracia, TraciaError, TraciaErrorCode, type TraciaOptions, TraciaSession, type UpdatePromptOptions };
962
+ export { type ContentPart, type CreatePromptOptions, type CreateSpanPayload, type CreateSpanResult, type CreateTracePayload, type CreateTraceResult, type EmbeddingUsage, type EmbeddingVector, Eval, type EvaluateOptions, type EvaluateResult, type FinishReason, type JsonSchemaProperty, LLMProvider, type ListSpansOptions, type ListSpansResult, type ListTracesOptions, type ListTracesResult, type LocalPromptMessage, type LocalStream, type MessageRole, type Prompt, type PromptListItem, type PromptMessage, type ResponsesEvent, type ResponsesInputItem, type ResponsesOutputItem, type ResponsesStream, type RunEmbeddingInput, type RunEmbeddingResult, type RunLocalInput, type RunLocalResult, type RunOptions, type RunResponsesInput, type RunResponsesResult, type RunResult, type RunVariables, type SessionRunEmbeddingInput, type SessionRunLocalInput, type SessionRunResponsesInput, type Span, SpanKind, type SpanListItem, type SpanStatus, type StreamResult, type TextPart, type TokenUsage, type ToolCall, type ToolCallPart, type ToolChoice, type ToolDefinition, type ToolParameters, type Trace, type TraceListItem, type TraceStatus, Tracia, TraciaError, TraciaErrorCode, type TraciaOptions, TraciaSession, type UpdatePromptOptions };
package/dist/index.d.ts CHANGED
@@ -66,6 +66,11 @@ declare enum LLMProvider {
66
66
  GOOGLE = "google",
67
67
  AMAZON_BEDROCK = "amazon_bedrock"
68
68
  }
69
+ declare enum SpanKind {
70
+ ROOT = "ROOT",
71
+ LLM = "LLM",
72
+ EMBEDDING = "EMBEDDING"
73
+ }
69
74
  interface ApiSuccessResponse {
70
75
  text: string;
71
76
  spanId: string;
@@ -341,7 +346,8 @@ interface CreateSpanPayload {
341
346
  model: string;
342
347
  provider: LLMProvider;
343
348
  input: {
344
- messages: LocalPromptMessage[];
349
+ messages?: LocalPromptMessage[];
350
+ text?: string | string[];
345
351
  };
346
352
  variables: Record<string, string> | null;
347
353
  output: string | null;
@@ -363,6 +369,8 @@ interface CreateSpanPayload {
363
369
  traceId?: string;
364
370
  /** Parent span ID for chaining spans in a sequence */
365
371
  parentSpanId?: string;
372
+ /** Span kind override (for embedding spans) */
373
+ spanKind?: SpanKind;
366
374
  }
367
375
  interface CreateSpanResult {
368
376
  spanId: string;
@@ -561,6 +569,65 @@ interface ResponsesStream {
561
569
  /** Abort the stream */
562
570
  abort(): void;
563
571
  }
572
+ interface RunEmbeddingInput {
573
+ /** Text(s) to embed */
574
+ input: string | string[];
575
+ /** Embedding model (e.g., 'text-embedding-3-small') */
576
+ model: string;
577
+ /** Explicitly specify the provider */
578
+ provider?: LLMProvider;
579
+ /** Provider API key override */
580
+ providerApiKey?: string;
581
+ /** Optional dimension override */
582
+ dimensions?: number;
583
+ /** Whether to send span to Tracia (default: true) */
584
+ sendTrace?: boolean;
585
+ /** Custom span ID */
586
+ spanId?: string;
587
+ /** Tags for the span */
588
+ tags?: string[];
589
+ /** User ID for the span */
590
+ userId?: string;
591
+ /** Session ID for the span */
592
+ sessionId?: string;
593
+ /** Trace ID to group related spans together */
594
+ traceId?: string;
595
+ /** Parent span ID for chaining spans in a sequence */
596
+ parentSpanId?: string;
597
+ /** Timeout in milliseconds */
598
+ timeoutMs?: number;
599
+ }
600
+ interface EmbeddingVector {
601
+ /** The embedding values */
602
+ values: number[];
603
+ /** Index of this embedding in the input array */
604
+ index: number;
605
+ }
606
+ interface EmbeddingUsage {
607
+ /** Total tokens used for the embedding request */
608
+ totalTokens: number;
609
+ }
610
+ interface RunEmbeddingResult {
611
+ /** The generated embeddings */
612
+ embeddings: EmbeddingVector[];
613
+ /** Span ID for this request */
614
+ spanId: string;
615
+ /** Trace ID grouping related spans */
616
+ traceId: string;
617
+ /** Latency in milliseconds */
618
+ latencyMs: number;
619
+ /** Token usage */
620
+ usage: EmbeddingUsage;
621
+ /**
622
+ * Estimated cost in USD.
623
+ * Currently always `null` — cost is calculated server-side when the span is ingested.
624
+ */
625
+ cost: number | null;
626
+ /** The provider used */
627
+ provider: LLMProvider;
628
+ /** The model used */
629
+ model: string;
630
+ }
564
631
  /** @deprecated Use SpanStatus instead */
565
632
  type TraceStatus = SpanStatus;
566
633
  /** @deprecated Use SpanListItem instead */
@@ -612,6 +679,11 @@ type SessionRunLocalInput = Omit<RunLocalInput, 'traceId' | 'parentSpanId'>;
612
679
  * as those are managed by the session.
613
680
  */
614
681
  type SessionRunResponsesInput = Omit<RunResponsesInput, 'traceId' | 'parentSpanId'>;
682
+ /**
683
+ * Input for session.runEmbedding() - same as RunEmbeddingInput but without traceId/parentSpanId
684
+ * as those are managed by the session.
685
+ */
686
+ type SessionRunEmbeddingInput = Omit<RunEmbeddingInput, 'traceId' | 'parentSpanId'>;
615
687
  /**
616
688
  * A session for grouping related spans together under a single trace.
617
689
  *
@@ -691,6 +763,11 @@ declare class TraciaSession {
691
763
  }): Promise<RunResponsesResult>;
692
764
  private runResponsesNonStreaming;
693
765
  private runResponsesStreaming;
766
+ /**
767
+ * Generate embeddings, automatically linking the span to this session.
768
+ * See Tracia.runEmbedding() for full documentation.
769
+ */
770
+ runEmbedding(input: SessionRunEmbeddingInput): Promise<RunEmbeddingResult>;
694
771
  private updateSessionState;
695
772
  }
696
773
 
@@ -814,6 +891,29 @@ declare class Tracia {
814
891
  private createResponsesStream;
815
892
  private createLocalStream;
816
893
  private combineAbortSignals;
894
+ /**
895
+ * Generate embeddings for text input(s) using an embedding model.
896
+ *
897
+ * @example Single text
898
+ * ```typescript
899
+ * const result = await tracia.runEmbedding({
900
+ * model: 'text-embedding-3-small',
901
+ * input: 'Hello world',
902
+ * })
903
+ * console.log(result.embeddings[0].values.length) // 1536
904
+ * ```
905
+ *
906
+ * @example Batch
907
+ * ```typescript
908
+ * const result = await tracia.runEmbedding({
909
+ * model: 'text-embedding-3-small',
910
+ * input: ['Hello', 'World'],
911
+ * })
912
+ * console.log(result.embeddings.length) // 2
913
+ * ```
914
+ */
915
+ runEmbedding(input: RunEmbeddingInput): Promise<RunEmbeddingResult>;
916
+ private validateRunEmbeddingInput;
817
917
  flush(): Promise<void>;
818
918
  /**
819
919
  * Create a new session for grouping related spans together under a single trace.
@@ -859,4 +959,4 @@ declare class Tracia {
859
959
  private getProviderApiKey;
860
960
  }
861
961
 
862
- export { type ContentPart, type CreatePromptOptions, type CreateSpanPayload, type CreateSpanResult, type CreateTracePayload, type CreateTraceResult, Eval, type EvaluateOptions, type EvaluateResult, type FinishReason, type JsonSchemaProperty, LLMProvider, type ListSpansOptions, type ListSpansResult, type ListTracesOptions, type ListTracesResult, type LocalPromptMessage, type LocalStream, type MessageRole, type Prompt, type PromptListItem, type PromptMessage, type ResponsesEvent, type ResponsesInputItem, type ResponsesOutputItem, type ResponsesStream, type RunLocalInput, type RunLocalResult, type RunOptions, type RunResponsesInput, type RunResponsesResult, type RunResult, type RunVariables, type SessionRunLocalInput, type SessionRunResponsesInput, type Span, type SpanListItem, type SpanStatus, type StreamResult, type TextPart, type TokenUsage, type ToolCall, type ToolCallPart, type ToolChoice, type ToolDefinition, type ToolParameters, type Trace, type TraceListItem, type TraceStatus, Tracia, TraciaError, TraciaErrorCode, type TraciaOptions, TraciaSession, type UpdatePromptOptions };
962
+ export { type ContentPart, type CreatePromptOptions, type CreateSpanPayload, type CreateSpanResult, type CreateTracePayload, type CreateTraceResult, type EmbeddingUsage, type EmbeddingVector, Eval, type EvaluateOptions, type EvaluateResult, type FinishReason, type JsonSchemaProperty, LLMProvider, type ListSpansOptions, type ListSpansResult, type ListTracesOptions, type ListTracesResult, type LocalPromptMessage, type LocalStream, type MessageRole, type Prompt, type PromptListItem, type PromptMessage, type ResponsesEvent, type ResponsesInputItem, type ResponsesOutputItem, type ResponsesStream, type RunEmbeddingInput, type RunEmbeddingResult, type RunLocalInput, type RunLocalResult, type RunOptions, type RunResponsesInput, type RunResponsesResult, type RunResult, type RunVariables, type SessionRunEmbeddingInput, type SessionRunLocalInput, type SessionRunResponsesInput, type Span, SpanKind, type SpanListItem, type SpanStatus, type StreamResult, type TextPart, type TokenUsage, type ToolCall, type ToolCallPart, type ToolChoice, type ToolDefinition, type ToolParameters, type Trace, type TraceListItem, type TraceStatus, Tracia, TraciaError, TraciaErrorCode, type TraciaOptions, TraciaSession, type UpdatePromptOptions };
package/dist/index.js CHANGED
@@ -32,6 +32,7 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  Eval: () => Eval,
34
34
  LLMProvider: () => LLMProvider,
35
+ SpanKind: () => SpanKind,
35
36
  Tracia: () => Tracia,
36
37
  TraciaError: () => TraciaError,
37
38
  TraciaErrorCode: () => TraciaErrorCode,
@@ -75,9 +76,15 @@ var LLMProvider = /* @__PURE__ */ ((LLMProvider2) => {
75
76
  LLMProvider2["AMAZON_BEDROCK"] = "amazon_bedrock";
76
77
  return LLMProvider2;
77
78
  })(LLMProvider || {});
79
+ var SpanKind = /* @__PURE__ */ ((SpanKind2) => {
80
+ SpanKind2["ROOT"] = "ROOT";
81
+ SpanKind2["LLM"] = "LLM";
82
+ SpanKind2["EMBEDDING"] = "EMBEDDING";
83
+ return SpanKind2;
84
+ })(SpanKind || {});
78
85
 
79
86
  // src/client.ts
80
- var SDK_VERSION = "0.3.12";
87
+ var SDK_VERSION = "0.4.0";
81
88
  var DEFAULT_TIMEOUT_MS = 12e4;
82
89
  function mapApiErrorCodeToTraciaErrorCode(apiCode) {
83
90
  const codeMap = {
@@ -342,7 +349,16 @@ var MODEL_TO_PROVIDER = {
342
349
  "amazon.nova-micro-v1:0": "amazon_bedrock" /* AMAZON_BEDROCK */,
343
350
  "amazon.nova-lite-v1:0": "amazon_bedrock" /* AMAZON_BEDROCK */,
344
351
  "amazon.nova-pro-v1:0": "amazon_bedrock" /* AMAZON_BEDROCK */,
345
- "mistral.pixtral-large-2502-v1:0": "amazon_bedrock" /* AMAZON_BEDROCK */
352
+ "mistral.pixtral-large-2502-v1:0": "amazon_bedrock" /* AMAZON_BEDROCK */,
353
+ // OpenAI - Embedding models
354
+ "text-embedding-3-small": "openai" /* OPENAI */,
355
+ "text-embedding-3-large": "openai" /* OPENAI */,
356
+ "text-embedding-ada-002": "openai" /* OPENAI */,
357
+ // Google - Embedding models
358
+ "text-embedding-004": "google" /* GOOGLE */,
359
+ // Amazon Bedrock - Embedding models
360
+ "amazon.titan-embed-text-v2:0": "amazon_bedrock" /* AMAZON_BEDROCK */,
361
+ "cohere.embed-english-v3": "amazon_bedrock" /* AMAZON_BEDROCK */
346
362
  };
347
363
  function getProviderForModel(modelId) {
348
364
  return MODEL_TO_PROVIDER[modelId];
@@ -502,7 +518,7 @@ async function getLanguageModel(provider, model, apiKey) {
502
518
  case "amazon_bedrock" /* AMAZON_BEDROCK */: {
503
519
  const { createAmazonBedrock } = await loadBedrockProvider();
504
520
  const region = process.env.AWS_REGION ?? "eu-central-1";
505
- const bedrock = apiKey ? createAmazonBedrock({ apiKey, region }) : createAmazonBedrock({ region });
521
+ const bedrock = createAmazonBedrock({ region });
506
522
  return bedrock(applyBedrockRegionPrefix(model, region));
507
523
  }
508
524
  default:
@@ -512,6 +528,74 @@ async function getLanguageModel(provider, model, apiKey) {
512
528
  );
513
529
  }
514
530
  }
531
+ async function getEmbeddingModel(provider, model, apiKey) {
532
+ switch (provider) {
533
+ case "openai" /* OPENAI */: {
534
+ const { createOpenAI } = await loadOpenAIProvider();
535
+ const openai = createOpenAI({ apiKey });
536
+ return openai.textEmbeddingModel(model);
537
+ }
538
+ case "google" /* GOOGLE */: {
539
+ const { createGoogleGenerativeAI } = await loadGoogleProvider();
540
+ const google = createGoogleGenerativeAI({ apiKey });
541
+ return google.textEmbeddingModel(model);
542
+ }
543
+ case "amazon_bedrock" /* AMAZON_BEDROCK */: {
544
+ const { createAmazonBedrock } = await loadBedrockProvider();
545
+ const region = process.env.AWS_REGION ?? "eu-central-1";
546
+ const bedrock = createAmazonBedrock({ region });
547
+ return bedrock.textEmbeddingModel(applyBedrockRegionPrefix(model, region));
548
+ }
549
+ case "anthropic" /* ANTHROPIC */:
550
+ throw new TraciaError(
551
+ "UNSUPPORTED_MODEL" /* UNSUPPORTED_MODEL */,
552
+ "Anthropic does not offer embedding models. Use OpenAI, Google, or Amazon Bedrock instead."
553
+ );
554
+ default:
555
+ throw new TraciaError(
556
+ "UNSUPPORTED_MODEL" /* UNSUPPORTED_MODEL */,
557
+ `Unsupported provider for embeddings: ${provider}`
558
+ );
559
+ }
560
+ }
561
+ async function embedText(options) {
562
+ const provider = resolveProvider(options.model, options.provider);
563
+ const embeddingModel = await getEmbeddingModel(provider, options.model, options.apiKey);
564
+ const inputs = Array.isArray(options.input) ? options.input : [options.input];
565
+ try {
566
+ const { embedMany } = await loadAISdk();
567
+ const providerOptions = {};
568
+ if (options.dimensions) {
569
+ if (provider === "openai" /* OPENAI */) {
570
+ providerOptions.openai = { dimensions: options.dimensions };
571
+ } else if (provider === "google" /* GOOGLE */) {
572
+ providerOptions.google = { outputDimensionality: options.dimensions };
573
+ }
574
+ }
575
+ const result = await embedMany({
576
+ model: embeddingModel,
577
+ values: inputs,
578
+ abortSignal: options.timeoutMs ? AbortSignal.timeout(options.timeoutMs) : void 0,
579
+ ...Object.keys(providerOptions).length > 0 && { providerOptions }
580
+ });
581
+ const embeddings = result.embeddings.map((values, index) => ({
582
+ values,
583
+ index
584
+ }));
585
+ return {
586
+ embeddings,
587
+ totalTokens: result.usage?.tokens ?? 0,
588
+ provider
589
+ };
590
+ } catch (error) {
591
+ if (error instanceof TraciaError) throw error;
592
+ const rawMessage = error instanceof Error ? error.message : String(error);
593
+ throw new TraciaError(
594
+ "PROVIDER_ERROR" /* PROVIDER_ERROR */,
595
+ `${provider} embedding error: ${sanitizeErrorMessage(rawMessage)}`
596
+ );
597
+ }
598
+ }
515
599
  function convertMessages(messages) {
516
600
  return messages.map((msg) => {
517
601
  if (msg.role === "tool") {
@@ -548,9 +632,12 @@ function convertMessages(messages) {
548
632
  };
549
633
  }
550
634
  const role = msg.role === "developer" ? "system" : msg.role;
635
+ if (typeof msg.content === "string") {
636
+ return { role, content: msg.content };
637
+ }
551
638
  return {
552
639
  role,
553
- content: typeof msg.content === "string" ? msg.content : msg.content.map((b) => b.type === "text" ? b.text : "").join("")
640
+ content: msg.content.map((b) => b.type === "text" ? b.text : "").join("")
554
641
  };
555
642
  });
556
643
  }
@@ -957,6 +1044,20 @@ var TraciaSession = class {
957
1044
  abort: () => responsesStream2.abort()
958
1045
  };
959
1046
  }
1047
+ /**
1048
+ * Generate embeddings, automatically linking the span to this session.
1049
+ * See Tracia.runEmbedding() for full documentation.
1050
+ */
1051
+ async runEmbedding(input) {
1052
+ const inputWithSession = {
1053
+ ...input,
1054
+ traceId: this.traceId ?? void 0,
1055
+ parentSpanId: this.lastSpanId ?? void 0
1056
+ };
1057
+ const result = await this.tracia.runEmbedding(inputWithSession);
1058
+ this.updateSessionState(result.spanId, result.traceId);
1059
+ return result;
1060
+ }
960
1061
  updateSessionState(spanId, traceId) {
961
1062
  if (!spanId) return;
962
1063
  if (!this.traceId && traceId) {
@@ -1596,6 +1697,128 @@ var Tracia = class {
1596
1697
  signal2.addEventListener("abort", onAbort, { once: true });
1597
1698
  return controller.signal;
1598
1699
  }
1700
+ /**
1701
+ * Generate embeddings for text input(s) using an embedding model.
1702
+ *
1703
+ * @example Single text
1704
+ * ```typescript
1705
+ * const result = await tracia.runEmbedding({
1706
+ * model: 'text-embedding-3-small',
1707
+ * input: 'Hello world',
1708
+ * })
1709
+ * console.log(result.embeddings[0].values.length) // 1536
1710
+ * ```
1711
+ *
1712
+ * @example Batch
1713
+ * ```typescript
1714
+ * const result = await tracia.runEmbedding({
1715
+ * model: 'text-embedding-3-small',
1716
+ * input: ['Hello', 'World'],
1717
+ * })
1718
+ * console.log(result.embeddings.length) // 2
1719
+ * ```
1720
+ */
1721
+ async runEmbedding(input) {
1722
+ this.validateRunEmbeddingInput(input);
1723
+ let spanId = "";
1724
+ let traceId = "";
1725
+ if (input.sendTrace !== false) {
1726
+ if (input.spanId && !isValidSpanIdFormat(input.spanId)) {
1727
+ throw new TraciaError(
1728
+ "INVALID_REQUEST" /* INVALID_REQUEST */,
1729
+ `Invalid span ID format. Must match: sp_ + 16 hex characters (e.g., sp_1234567890abcdef)`
1730
+ );
1731
+ }
1732
+ spanId = input.spanId || generateSpanId();
1733
+ traceId = input.traceId || generateTraceId();
1734
+ }
1735
+ const provider = resolveProvider(input.model, input.provider);
1736
+ const apiKey = this.getProviderApiKey(provider, input.providerApiKey);
1737
+ const startTime = Date.now();
1738
+ let embeddingResult = null;
1739
+ let errorMessage = null;
1740
+ try {
1741
+ embeddingResult = await embedText({
1742
+ model: input.model,
1743
+ input: input.input,
1744
+ apiKey,
1745
+ provider: input.provider,
1746
+ dimensions: input.dimensions,
1747
+ timeoutMs: input.timeoutMs
1748
+ });
1749
+ } catch (error) {
1750
+ if (error instanceof TraciaError) {
1751
+ errorMessage = error.message;
1752
+ } else {
1753
+ errorMessage = error instanceof Error ? error.message : String(error);
1754
+ }
1755
+ }
1756
+ const latencyMs = Date.now() - startTime;
1757
+ const inputTexts = Array.isArray(input.input) ? input.input : [input.input];
1758
+ if (spanId) {
1759
+ const embeddingCount = embeddingResult?.embeddings.length ?? 0;
1760
+ const embeddingDimensions = embeddingResult?.embeddings[0]?.values.length ?? 0;
1761
+ this.scheduleSpanCreation(spanId, {
1762
+ spanId,
1763
+ model: input.model,
1764
+ provider: embeddingResult?.provider ?? provider,
1765
+ input: { text: inputTexts },
1766
+ variables: null,
1767
+ output: embeddingResult ? JSON.stringify({ dimensions: embeddingDimensions, count: embeddingCount }) : null,
1768
+ status: errorMessage ? SPAN_STATUS_ERROR : SPAN_STATUS_SUCCESS,
1769
+ error: errorMessage,
1770
+ latencyMs,
1771
+ inputTokens: embeddingResult?.totalTokens ?? 0,
1772
+ outputTokens: 0,
1773
+ totalTokens: embeddingResult?.totalTokens ?? 0,
1774
+ tags: input.tags,
1775
+ userId: input.userId,
1776
+ sessionId: input.sessionId,
1777
+ traceId,
1778
+ parentSpanId: input.parentSpanId,
1779
+ spanKind: "EMBEDDING" /* EMBEDDING */
1780
+ });
1781
+ }
1782
+ if (errorMessage || !embeddingResult) {
1783
+ throw new TraciaError("PROVIDER_ERROR" /* PROVIDER_ERROR */, errorMessage ?? "Embedding call returned no result");
1784
+ }
1785
+ return {
1786
+ embeddings: embeddingResult.embeddings,
1787
+ spanId,
1788
+ traceId,
1789
+ latencyMs,
1790
+ usage: { totalTokens: embeddingResult.totalTokens },
1791
+ cost: null,
1792
+ provider: embeddingResult.provider,
1793
+ model: input.model
1794
+ };
1795
+ }
1796
+ validateRunEmbeddingInput(input) {
1797
+ if (!input.model || input.model.trim() === "") {
1798
+ throw new TraciaError(
1799
+ "INVALID_REQUEST" /* INVALID_REQUEST */,
1800
+ "model is required and cannot be empty"
1801
+ );
1802
+ }
1803
+ if (!input.input || Array.isArray(input.input) && input.input.length === 0) {
1804
+ throw new TraciaError(
1805
+ "INVALID_REQUEST" /* INVALID_REQUEST */,
1806
+ "input is required and cannot be empty"
1807
+ );
1808
+ }
1809
+ if (typeof input.input === "string" && input.input.trim() === "") {
1810
+ throw new TraciaError(
1811
+ "INVALID_REQUEST" /* INVALID_REQUEST */,
1812
+ "input text cannot be empty"
1813
+ );
1814
+ }
1815
+ if (Array.isArray(input.input) && input.input.some((text) => text.trim() === "")) {
1816
+ throw new TraciaError(
1817
+ "INVALID_REQUEST" /* INVALID_REQUEST */,
1818
+ "input array cannot contain empty strings"
1819
+ );
1820
+ }
1821
+ }
1599
1822
  async flush() {
1600
1823
  await Promise.all(this.pendingSpans.values());
1601
1824
  }
@@ -1762,6 +1985,7 @@ var Tracia = class {
1762
1985
  0 && (module.exports = {
1763
1986
  Eval,
1764
1987
  LLMProvider,
1988
+ SpanKind,
1765
1989
  Tracia,
1766
1990
  TraciaError,
1767
1991
  TraciaErrorCode,