tracia 0.2.7 → 0.3.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
@@ -1,7 +1,7 @@
1
1
  interface TraciaOptions {
2
2
  apiKey: string;
3
- /** Called when background trace creation fails */
4
- onTraceError?: (error: Error, traceId: string) => void;
3
+ /** Called when background span creation fails */
4
+ onSpanError?: (error: Error, spanId: string) => void;
5
5
  }
6
6
  interface RunVariables {
7
7
  [key: string]: string;
@@ -19,6 +19,9 @@ interface TokenUsage {
19
19
  }
20
20
  interface RunResult {
21
21
  text: string;
22
+ /** Unique ID for this span (individual LLM call) */
23
+ spanId: string;
24
+ /** Trace ID grouping related spans in a session */
22
25
  traceId: string;
23
26
  promptVersion: number;
24
27
  latencyMs: number;
@@ -48,6 +51,7 @@ declare enum LLMProvider {
48
51
  }
49
52
  interface ApiSuccessResponse {
50
53
  text: string;
54
+ spanId: string;
51
55
  traceId: string;
52
56
  promptVersion: number;
53
57
  latencyMs: number;
@@ -95,13 +99,15 @@ interface UpdatePromptOptions {
95
99
  description?: string;
96
100
  content?: PromptMessage[];
97
101
  }
98
- type TraceStatus = 'SUCCESS' | 'ERROR';
99
- interface TraceListItem {
102
+ type SpanStatus = 'SUCCESS' | 'ERROR';
103
+ interface SpanListItem {
100
104
  id: string;
105
+ spanId: string;
101
106
  traceId: string;
107
+ parentSpanId: string | null;
102
108
  promptSlug: string;
103
109
  model: string;
104
- status: TraceStatus;
110
+ status: SpanStatus;
105
111
  latencyMs: number;
106
112
  inputTokens: number;
107
113
  outputTokens: number;
@@ -109,9 +115,11 @@ interface TraceListItem {
109
115
  cost: number | null;
110
116
  createdAt: string;
111
117
  }
112
- interface Trace {
118
+ interface Span {
113
119
  id: string;
120
+ spanId: string;
114
121
  traceId: string;
122
+ parentSpanId: string | null;
115
123
  promptSlug: string;
116
124
  promptVersion: number;
117
125
  model: string;
@@ -121,7 +129,7 @@ interface Trace {
121
129
  };
122
130
  variables: Record<string, string> | null;
123
131
  output: string | null;
124
- status: TraceStatus;
132
+ status: SpanStatus;
125
133
  error: string | null;
126
134
  latencyMs: number;
127
135
  inputTokens: number;
@@ -133,9 +141,9 @@ interface Trace {
133
141
  sessionId: string | null;
134
142
  createdAt: string;
135
143
  }
136
- interface ListTracesOptions {
144
+ interface ListSpansOptions {
137
145
  promptSlug?: string;
138
- status?: TraceStatus;
146
+ status?: SpanStatus;
139
147
  startDate?: Date;
140
148
  endDate?: Date;
141
149
  userId?: string;
@@ -144,8 +152,8 @@ interface ListTracesOptions {
144
152
  limit?: number;
145
153
  cursor?: string;
146
154
  }
147
- interface ListTracesResult {
148
- traces: TraceListItem[];
155
+ interface ListSpansResult {
156
+ spans: SpanListItem[];
149
157
  nextCursor?: string;
150
158
  }
151
159
  interface EvaluateOptions {
@@ -267,17 +275,24 @@ interface RunLocalInput {
267
275
  userId?: string;
268
276
  sessionId?: string;
269
277
  sendTrace?: boolean;
270
- /** Custom trace ID. Must match format: tr_ + 16 hex characters */
271
- traceId?: string;
278
+ /** Custom span ID. Must match format: sp_ + 16 hex characters (or legacy tr_ format) */
279
+ spanId?: string;
272
280
  /** Tool definitions for function calling */
273
281
  tools?: ToolDefinition[];
274
282
  /** Control which tools the model can use */
275
283
  toolChoice?: ToolChoice;
276
284
  /** AbortSignal to cancel the request (only used when stream: true) */
277
285
  signal?: AbortSignal;
286
+ /** Trace ID to group related spans together (uses first span's ID if not specified in session) */
287
+ traceId?: string;
288
+ /** Parent span ID for chaining spans in a sequence */
289
+ parentSpanId?: string;
278
290
  }
279
291
  interface RunLocalResult {
280
292
  text: string;
293
+ /** Unique ID for this span (individual LLM call) */
294
+ spanId: string;
295
+ /** Trace ID grouping related spans in a session */
281
296
  traceId: string;
282
297
  latencyMs: number;
283
298
  usage: TokenUsage;
@@ -291,8 +306,8 @@ interface RunLocalResult {
291
306
  /** Full assistant message for round-tripping in multi-turn conversations */
292
307
  message: LocalPromptMessage;
293
308
  }
294
- interface CreateTracePayload {
295
- traceId: string;
309
+ interface CreateSpanPayload {
310
+ spanId: string;
296
311
  model: string;
297
312
  provider: LLMProvider;
298
313
  input: {
@@ -300,7 +315,7 @@ interface CreateTracePayload {
300
315
  };
301
316
  variables: Record<string, string> | null;
302
317
  output: string | null;
303
- status: TraceStatus;
318
+ status: SpanStatus;
304
319
  error: string | null;
305
320
  latencyMs: number;
306
321
  inputTokens: number;
@@ -314,9 +329,13 @@ interface CreateTracePayload {
314
329
  topP?: number;
315
330
  tools?: ToolDefinition[];
316
331
  toolCalls?: ToolCall[];
332
+ /** Trace ID to group related spans together */
333
+ traceId?: string;
334
+ /** Parent span ID for chaining spans in a sequence */
335
+ parentSpanId?: string;
317
336
  }
318
- interface CreateTraceResult {
319
- traceId: string;
337
+ interface CreateSpanResult {
338
+ spanId: string;
320
339
  cost: number | null;
321
340
  }
322
341
  /**
@@ -338,8 +357,8 @@ interface StreamResult extends RunLocalResult {
338
357
  * stream: true,
339
358
  * })
340
359
  *
341
- * // traceId is available immediately
342
- * console.log('Trace:', stream.traceId)
360
+ * // spanId is available immediately
361
+ * console.log('Span:', stream.spanId)
343
362
  *
344
363
  * // Iterate over text chunks as they arrive
345
364
  * for await (const chunk of stream) {
@@ -357,7 +376,9 @@ interface StreamResult extends RunLocalResult {
357
376
  * - The stream can only be iterated once
358
377
  */
359
378
  interface LocalStream {
360
- /** Trace ID for this request, available immediately */
379
+ /** Span ID for this request, available immediately */
380
+ readonly spanId: string;
381
+ /** Trace ID grouping related spans, available immediately */
361
382
  readonly traceId: string;
362
383
  /** Async iterator yielding text chunks */
363
384
  [Symbol.asyncIterator](): AsyncIterator<string>;
@@ -431,16 +452,20 @@ interface RunResponsesInput {
431
452
  signal?: AbortSignal;
432
453
  /** Timeout in milliseconds */
433
454
  timeoutMs?: number;
434
- /** Whether to send trace to Tracia (default: true) */
455
+ /** Whether to send span to Tracia (default: true) */
435
456
  sendTrace?: boolean;
436
- /** Custom trace ID */
437
- traceId?: string;
438
- /** Tags for the trace */
457
+ /** Custom span ID */
458
+ spanId?: string;
459
+ /** Tags for the span */
439
460
  tags?: string[];
440
- /** User ID for the trace */
461
+ /** User ID for the span */
441
462
  userId?: string;
442
- /** Session ID for the trace */
463
+ /** Session ID for the span */
443
464
  sessionId?: string;
465
+ /** Trace ID to group related spans together (uses first span's ID if not specified in session) */
466
+ traceId?: string;
467
+ /** Parent span ID for chaining spans in a sequence */
468
+ parentSpanId?: string;
444
469
  }
445
470
  /**
446
471
  * Final result from a Responses API call.
@@ -448,7 +473,9 @@ interface RunResponsesInput {
448
473
  interface RunResponsesResult {
449
474
  /** Final text output */
450
475
  text: string;
451
- /** Trace ID for this request */
476
+ /** Span ID for this request */
477
+ spanId: string;
478
+ /** Trace ID grouping related spans in a session */
452
479
  traceId: string;
453
480
  /** Latency in milliseconds */
454
481
  latencyMs: number;
@@ -491,7 +518,9 @@ interface RunResponsesResult {
491
518
  * ```
492
519
  */
493
520
  interface ResponsesStream {
494
- /** Trace ID for this request, available immediately */
521
+ /** Span ID for this request, available immediately */
522
+ readonly spanId: string;
523
+ /** Trace ID grouping related spans, available immediately */
495
524
  readonly traceId: string;
496
525
  /** Async iterator yielding events */
497
526
  [Symbol.asyncIterator](): AsyncIterator<ResponsesEvent>;
@@ -500,6 +529,20 @@ interface ResponsesStream {
500
529
  /** Abort the stream */
501
530
  abort(): void;
502
531
  }
532
+ /** @deprecated Use SpanStatus instead */
533
+ type TraceStatus = SpanStatus;
534
+ /** @deprecated Use SpanListItem instead */
535
+ type TraceListItem = SpanListItem;
536
+ /** @deprecated Use Span instead */
537
+ type Trace = Span;
538
+ /** @deprecated Use ListSpansOptions instead */
539
+ type ListTracesOptions = ListSpansOptions;
540
+ /** @deprecated Use ListSpansResult instead */
541
+ type ListTracesResult = ListSpansResult;
542
+ /** @deprecated Use CreateSpanPayload instead */
543
+ type CreateTracePayload = CreateSpanPayload;
544
+ /** @deprecated Use CreateSpanResult instead */
545
+ type CreateTraceResult = CreateSpanResult;
503
546
 
504
547
  interface HttpClientOptions {
505
548
  apiKey: string;
@@ -527,18 +570,105 @@ declare class Prompts {
527
570
  run(slug: string, variables?: RunVariables, options?: RunOptions): Promise<RunResult>;
528
571
  }
529
572
 
530
- /** @internal Symbol for setting pending traces map - not part of public API */
531
- declare const INTERNAL_SET_PENDING_TRACES: unique symbol;
532
- declare class Traces {
573
+ /**
574
+ * Input for session.runLocal() - same as RunLocalInput but without traceId/parentSpanId
575
+ * as those are managed by the session.
576
+ */
577
+ type SessionRunLocalInput = Omit<RunLocalInput, 'traceId' | 'parentSpanId'>;
578
+ /**
579
+ * Input for session.runResponses() - same as RunResponsesInput but without traceId/parentSpanId
580
+ * as those are managed by the session.
581
+ */
582
+ type SessionRunResponsesInput = Omit<RunResponsesInput, 'traceId' | 'parentSpanId'>;
583
+ /**
584
+ * A session for grouping related spans together under a single trace.
585
+ *
586
+ * Sessions automatically chain spans by setting traceId and parentSpanId,
587
+ * creating a linked sequence of spans that can be viewed together in the Tracia dashboard.
588
+ *
589
+ * @example
590
+ * ```typescript
591
+ * const session = tracia.createSession()
592
+ *
593
+ * // First call - creates the trace group
594
+ * const result1 = await session.runLocal({
595
+ * model: 'gpt-4o',
596
+ * messages: [{ role: 'user', content: 'What is the weather?' }],
597
+ * })
598
+ *
599
+ * // Second call - automatically linked to the first
600
+ * const result2 = await session.runLocal({
601
+ * model: 'gpt-4o',
602
+ * messages: [
603
+ * { role: 'user', content: 'What is the weather?' },
604
+ * result1.message,
605
+ * { role: 'user', content: 'What about tomorrow?' },
606
+ * ],
607
+ * })
608
+ *
609
+ * // All spans are grouped under the same trace in the dashboard
610
+ * ```
611
+ */
612
+ declare class TraciaSession {
613
+ private readonly tracia;
614
+ private traceId;
615
+ private lastSpanId;
616
+ /** @internal */
617
+ constructor(tracia: Tracia);
618
+ /**
619
+ * Get the current session's trace ID (the ID that groups all spans together).
620
+ * Returns null if no spans have been made yet.
621
+ */
622
+ getTraceId(): string | null;
623
+ /**
624
+ * Get the spanId of the last completed span in the session.
625
+ * Returns null if no spans have been made yet.
626
+ */
627
+ getLastSpanId(): string | null;
628
+ /**
629
+ * Reset the session, clearing the trace and parent span IDs.
630
+ * The next call will start a new trace group.
631
+ */
632
+ reset(): void;
633
+ /**
634
+ * Execute an LLM call locally, automatically linking it to this session.
635
+ * See Tracia.runLocal() for full documentation.
636
+ */
637
+ runLocal(input: SessionRunLocalInput & {
638
+ stream: true;
639
+ }): LocalStream;
640
+ runLocal(input: SessionRunLocalInput & {
641
+ stream?: false;
642
+ }): Promise<RunLocalResult>;
643
+ private runLocalNonStreaming;
644
+ private runLocalStreaming;
645
+ /**
646
+ * Execute an LLM call using OpenAI's Responses API, automatically linking it to this session.
647
+ * See Tracia.runResponses() for full documentation.
648
+ */
649
+ runResponses(input: SessionRunResponsesInput & {
650
+ stream: true;
651
+ }): ResponsesStream;
652
+ runResponses(input: SessionRunResponsesInput & {
653
+ stream?: false;
654
+ }): Promise<RunResponsesResult>;
655
+ private runResponsesNonStreaming;
656
+ private runResponsesStreaming;
657
+ private updateSessionState;
658
+ }
659
+
660
+ /** @internal Symbol for setting pending spans map - not part of public API */
661
+ declare const INTERNAL_SET_PENDING_SPANS: unique symbol;
662
+ declare class Spans {
533
663
  private readonly client;
534
- private pendingTraces;
664
+ private pendingSpans;
535
665
  constructor(client: HttpClient);
536
666
  /** @internal */
537
- [INTERNAL_SET_PENDING_TRACES](map: Map<string, Promise<void>>): void;
538
- create(payload: CreateTracePayload): Promise<CreateTraceResult>;
539
- get(traceId: string): Promise<Trace>;
540
- list(options?: ListTracesOptions): Promise<ListTracesResult>;
541
- evaluate(traceId: string, options: EvaluateOptions): Promise<EvaluateResult>;
667
+ [INTERNAL_SET_PENDING_SPANS](map: Map<string, Promise<void>>): void;
668
+ create(payload: CreateSpanPayload): Promise<CreateSpanResult>;
669
+ get(spanId: string): Promise<Span>;
670
+ list(options?: ListSpansOptions): Promise<ListSpansResult>;
671
+ evaluate(spanId: string, options: EvaluateOptions): Promise<EvaluateResult>;
542
672
  }
543
673
 
544
674
  declare class TraciaError extends Error {
@@ -553,10 +683,10 @@ declare const Eval: {
553
683
  };
554
684
  declare class Tracia {
555
685
  private readonly client;
556
- private readonly pendingTraces;
557
- private readonly onTraceError?;
686
+ private readonly pendingSpans;
687
+ private readonly onSpanError?;
558
688
  readonly prompts: Prompts;
559
- readonly traces: Traces;
689
+ readonly spans: Spans;
560
690
  constructor(options: TraciaOptions);
561
691
  /**
562
692
  * Execute an LLM call locally using the Vercel AI SDK.
@@ -648,13 +778,37 @@ declare class Tracia {
648
778
  private createLocalStream;
649
779
  private combineAbortSignals;
650
780
  flush(): Promise<void>;
781
+ /**
782
+ * Create a new session for grouping related spans together under a single trace.
783
+ *
784
+ * Sessions automatically chain spans by setting traceId and parentSpanId,
785
+ * creating a linked sequence of spans that can be viewed together in the Tracia dashboard.
786
+ *
787
+ * @example
788
+ * ```typescript
789
+ * const session = tracia.createSession()
790
+ *
791
+ * // First call - creates the trace group
792
+ * const result1 = await session.runLocal({
793
+ * model: 'gpt-4o',
794
+ * messages: [{ role: 'user', content: 'What is the weather?' }],
795
+ * })
796
+ *
797
+ * // Second call - automatically linked to the first
798
+ * const result2 = await session.runLocal({
799
+ * model: 'gpt-4o',
800
+ * messages: [...messages, result1.message, { role: 'user', content: 'What about tomorrow?' }],
801
+ * })
802
+ * ```
803
+ */
804
+ createSession(): TraciaSession;
651
805
  private validateRunLocalInput;
652
- private scheduleTraceCreation;
653
- private createTraceWithRetry;
806
+ private scheduleSpanCreation;
807
+ private createSpanWithRetry;
654
808
  private delay;
655
809
  private interpolateMessages;
656
810
  private buildAssistantMessage;
657
811
  private getProviderApiKey;
658
812
  }
659
813
 
660
- export { type ContentPart, type CreatePromptOptions, type CreateTracePayload, type CreateTraceResult, Eval, type EvaluateOptions, type EvaluateResult, type FinishReason, type JsonSchemaProperty, LLMProvider, 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 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, type UpdatePromptOptions };
814
+ 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 };