tracia 0.1.1 → 0.2.2

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,5 +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
5
  }
4
6
  interface RunVariables {
5
7
  [key: string]: string;
@@ -33,7 +35,16 @@ declare enum TraciaErrorCode {
33
35
  INVALID_REQUEST = "INVALID_REQUEST",
34
36
  NETWORK_ERROR = "NETWORK_ERROR",
35
37
  TIMEOUT = "TIMEOUT",
36
- UNKNOWN = "UNKNOWN"
38
+ ABORTED = "ABORTED",
39
+ UNKNOWN = "UNKNOWN",
40
+ MISSING_PROVIDER_SDK = "MISSING_PROVIDER_SDK",
41
+ MISSING_PROVIDER_API_KEY = "MISSING_PROVIDER_API_KEY",
42
+ UNSUPPORTED_MODEL = "UNSUPPORTED_MODEL"
43
+ }
44
+ declare enum LLMProvider {
45
+ OPENAI = "openai",
46
+ ANTHROPIC = "anthropic",
47
+ GOOGLE = "google"
37
48
  }
38
49
  interface ApiSuccessResponse {
39
50
  text: string;
@@ -43,7 +54,7 @@ interface ApiSuccessResponse {
43
54
  usage: TokenUsage;
44
55
  cost: number;
45
56
  }
46
- type MessageRole = 'system' | 'user' | 'assistant';
57
+ type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
47
58
  interface PromptMessage {
48
59
  id: string;
49
60
  role: MessageRole;
@@ -137,6 +148,356 @@ interface ListTracesResult {
137
148
  traces: TraceListItem[];
138
149
  nextCursor?: string;
139
150
  }
151
+ interface EvaluateOptions {
152
+ evaluator: string;
153
+ value: number;
154
+ note?: string;
155
+ }
156
+ interface EvaluateResult {
157
+ id: string;
158
+ evaluatorKey: string;
159
+ evaluatorName: string;
160
+ value: number;
161
+ source: string;
162
+ note: string | null;
163
+ createdAt: string;
164
+ }
165
+ interface ToolDefinition {
166
+ name: string;
167
+ description: string;
168
+ parameters: ToolParameters;
169
+ }
170
+ interface ToolParameters {
171
+ type: 'object';
172
+ properties: Record<string, JsonSchemaProperty>;
173
+ required?: string[];
174
+ }
175
+ interface JsonSchemaProperty {
176
+ type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object';
177
+ description?: string;
178
+ enum?: (string | number)[];
179
+ items?: JsonSchemaProperty;
180
+ properties?: Record<string, JsonSchemaProperty>;
181
+ required?: string[];
182
+ }
183
+ /**
184
+ * Tool call returned in results - user-friendly format.
185
+ */
186
+ interface ToolCall {
187
+ id: string;
188
+ name: string;
189
+ arguments: Record<string, unknown>;
190
+ }
191
+ type ToolChoice = 'auto' | 'none' | 'required' | {
192
+ tool: string;
193
+ };
194
+ type FinishReason = 'stop' | 'tool_calls' | 'max_tokens';
195
+ /**
196
+ * Text content part for messages.
197
+ */
198
+ interface TextPart {
199
+ type: 'text';
200
+ text: string;
201
+ }
202
+ /**
203
+ * Tool call part in assistant messages.
204
+ */
205
+ interface ToolCallPart {
206
+ type: 'tool_call';
207
+ id: string;
208
+ name: string;
209
+ arguments: Record<string, unknown>;
210
+ }
211
+ type ContentPart = TextPart | ToolCallPart;
212
+ /**
213
+ * Message format for LLM conversations.
214
+ *
215
+ * @example System message
216
+ * ```typescript
217
+ * { role: 'system', content: 'You are a helpful assistant.' }
218
+ * ```
219
+ *
220
+ * @example User message
221
+ * ```typescript
222
+ * { role: 'user', content: 'What is the weather?' }
223
+ * ```
224
+ *
225
+ * @example Assistant message with tool calls
226
+ * ```typescript
227
+ * {
228
+ * role: 'assistant',
229
+ * content: [
230
+ * { type: 'text', text: 'Let me check the weather.' },
231
+ * { type: 'tool_call', id: 'call_123', name: 'get_weather', arguments: { location: 'Paris' } }
232
+ * ]
233
+ * }
234
+ * ```
235
+ *
236
+ * @example Tool result message (simple format)
237
+ * ```typescript
238
+ * { role: 'tool', toolCallId: 'call_123', content: '{"temp": 22, "unit": "celsius"}' }
239
+ * ```
240
+ */
241
+ interface LocalPromptMessage {
242
+ role: MessageRole;
243
+ content: string | ContentPart[];
244
+ /** Required when role is 'tool' - the ID of the tool call this is responding to */
245
+ toolCallId?: string;
246
+ }
247
+ interface RunLocalInput {
248
+ messages: LocalPromptMessage[];
249
+ model: string;
250
+ /** Enable streaming. When true, returns LocalStream. When false/undefined, returns Promise<RunLocalResult>. */
251
+ stream?: boolean;
252
+ /** Explicitly specify the provider. Use for new/custom models not in the built-in list. */
253
+ provider?: LLMProvider;
254
+ temperature?: number;
255
+ maxOutputTokens?: number;
256
+ topP?: number;
257
+ stopSequences?: string[];
258
+ /** Timeout in milliseconds for the LLM call */
259
+ timeoutMs?: number;
260
+ /** Provider-specific options passed directly to the SDK */
261
+ customOptions?: Record<string, unknown>;
262
+ variables?: Record<string, string>;
263
+ providerApiKey?: string;
264
+ tags?: string[];
265
+ userId?: string;
266
+ sessionId?: string;
267
+ sendTrace?: boolean;
268
+ /** Custom trace ID. Must match format: tr_ + 16 hex characters */
269
+ traceId?: string;
270
+ /** Tool definitions for function calling */
271
+ tools?: ToolDefinition[];
272
+ /** Control which tools the model can use */
273
+ toolChoice?: ToolChoice;
274
+ /** AbortSignal to cancel the request (only used when stream: true) */
275
+ signal?: AbortSignal;
276
+ }
277
+ interface RunLocalResult {
278
+ text: string;
279
+ traceId: string;
280
+ latencyMs: number;
281
+ usage: TokenUsage;
282
+ cost: number | null;
283
+ provider: LLMProvider;
284
+ model: string;
285
+ /** Tool calls made by the model, empty array if none */
286
+ toolCalls: ToolCall[];
287
+ /** Reason the model stopped generating */
288
+ finishReason: FinishReason;
289
+ /** Full assistant message for round-tripping in multi-turn conversations */
290
+ message: LocalPromptMessage;
291
+ }
292
+ interface CreateTracePayload {
293
+ traceId: string;
294
+ model: string;
295
+ provider: LLMProvider;
296
+ input: {
297
+ messages: LocalPromptMessage[];
298
+ };
299
+ variables: Record<string, string> | null;
300
+ output: string | null;
301
+ status: TraceStatus;
302
+ error: string | null;
303
+ latencyMs: number;
304
+ inputTokens: number;
305
+ outputTokens: number;
306
+ totalTokens: number;
307
+ tags?: string[];
308
+ userId?: string;
309
+ sessionId?: string;
310
+ temperature?: number;
311
+ maxOutputTokens?: number;
312
+ topP?: number;
313
+ tools?: ToolDefinition[];
314
+ toolCalls?: ToolCall[];
315
+ }
316
+ interface CreateTraceResult {
317
+ traceId: string;
318
+ cost: number | null;
319
+ }
320
+ /**
321
+ * Final result returned after a stream completes.
322
+ * Includes all fields from RunLocalResult plus abort status.
323
+ */
324
+ interface StreamResult extends RunLocalResult {
325
+ /** Whether the stream was aborted before completion */
326
+ aborted: boolean;
327
+ }
328
+ /**
329
+ * A streaming response from runLocal({ stream: true }).
330
+ *
331
+ * @example
332
+ * ```typescript
333
+ * const stream = tracia.runLocal({
334
+ * model: 'gpt-4o',
335
+ * messages: [{ role: 'user', content: 'Write a haiku' }],
336
+ * stream: true,
337
+ * })
338
+ *
339
+ * // traceId is available immediately
340
+ * console.log('Trace:', stream.traceId)
341
+ *
342
+ * // Iterate over text chunks as they arrive
343
+ * for await (const chunk of stream) {
344
+ * process.stdout.write(chunk)
345
+ * }
346
+ *
347
+ * // Get final result with usage stats after iteration completes
348
+ * const result = await stream.result
349
+ * console.log(result.usage)
350
+ * ```
351
+ *
352
+ * @remarks
353
+ * - You must iterate over the stream for the result promise to resolve
354
+ * - Calling abort() will stop the stream and resolve result with aborted: true
355
+ * - The stream can only be iterated once
356
+ */
357
+ interface LocalStream {
358
+ /** Trace ID for this request, available immediately */
359
+ readonly traceId: string;
360
+ /** Async iterator yielding text chunks */
361
+ [Symbol.asyncIterator](): AsyncIterator<string>;
362
+ /**
363
+ * Promise that resolves to the final result after stream completes.
364
+ * Only resolves after the stream has been fully iterated or aborted.
365
+ */
366
+ readonly result: Promise<StreamResult>;
367
+ /** Abort the stream. The result promise will resolve with aborted: true */
368
+ abort(): void;
369
+ }
370
+ /**
371
+ * Input item for the Responses API.
372
+ * Can be a message (developer/user) or a function call output.
373
+ */
374
+ type ResponsesInputItem = {
375
+ role: 'developer' | 'user';
376
+ content: string;
377
+ } | {
378
+ type: 'function_call_output';
379
+ call_id: string;
380
+ output: string;
381
+ } | ResponsesOutputItem;
382
+ /**
383
+ * Output item from a Responses API call.
384
+ * These can be added back to input for multi-turn conversations.
385
+ */
386
+ interface ResponsesOutputItem {
387
+ type: 'message' | 'function_call' | 'reasoning';
388
+ [key: string]: unknown;
389
+ }
390
+ /**
391
+ * Event yielded during Responses API streaming.
392
+ */
393
+ type ResponsesEvent = {
394
+ type: 'text_delta';
395
+ data: string;
396
+ } | {
397
+ type: 'text';
398
+ data: string;
399
+ } | {
400
+ type: 'reasoning';
401
+ content: string;
402
+ } | {
403
+ type: 'tool_call';
404
+ id: string;
405
+ callId: string;
406
+ name: string;
407
+ arguments: Record<string, unknown>;
408
+ } | {
409
+ type: 'done';
410
+ usage: TokenUsage;
411
+ };
412
+ /**
413
+ * Input options for runResponses().
414
+ */
415
+ interface RunResponsesInput {
416
+ /** Model to use (e.g., 'gpt-4o', 'o1', 'o3-mini') */
417
+ model: string;
418
+ /** Input items for the conversation */
419
+ input: ResponsesInputItem[];
420
+ /** Enable streaming. When true, returns ResponsesStream. When false/undefined, returns Promise<RunResponsesResult>. */
421
+ stream?: boolean;
422
+ /** Tool definitions for function calling */
423
+ tools?: ToolDefinition[];
424
+ /** Maximum output tokens */
425
+ maxOutputTokens?: number;
426
+ /** Provider API key override */
427
+ providerApiKey?: string;
428
+ /** AbortSignal to cancel the request (only used when stream: true) */
429
+ signal?: AbortSignal;
430
+ /** Timeout in milliseconds */
431
+ timeoutMs?: number;
432
+ /** Whether to send trace to Tracia (default: true) */
433
+ sendTrace?: boolean;
434
+ /** Custom trace ID */
435
+ traceId?: string;
436
+ /** Tags for the trace */
437
+ tags?: string[];
438
+ /** User ID for the trace */
439
+ userId?: string;
440
+ /** Session ID for the trace */
441
+ sessionId?: string;
442
+ }
443
+ /**
444
+ * Final result from a Responses API call.
445
+ */
446
+ interface RunResponsesResult {
447
+ /** Final text output */
448
+ text: string;
449
+ /** Trace ID for this request */
450
+ traceId: string;
451
+ /** Latency in milliseconds */
452
+ latencyMs: number;
453
+ /** Token usage */
454
+ usage: TokenUsage;
455
+ /** Output items that can be added back to input for multi-turn */
456
+ outputItems: ResponsesOutputItem[];
457
+ /** Tool calls made by the model */
458
+ toolCalls: Array<{
459
+ id: string;
460
+ callId: string;
461
+ name: string;
462
+ arguments: Record<string, unknown>;
463
+ }>;
464
+ /** Whether the stream was aborted */
465
+ aborted: boolean;
466
+ }
467
+ /**
468
+ * A streaming response from runResponses({ stream: true }).
469
+ *
470
+ * @example
471
+ * ```typescript
472
+ * const stream = tracia.runResponses({
473
+ * model: 'o3-mini',
474
+ * input: [
475
+ * { role: 'developer', content: 'You are a helpful assistant.' },
476
+ * { role: 'user', content: 'What is 2+2?' },
477
+ * ],
478
+ * stream: true,
479
+ * })
480
+ *
481
+ * for await (const event of stream) {
482
+ * if (event.type === 'text_delta') process.stdout.write(event.data)
483
+ * if (event.type === 'reasoning') console.log('Thinking:', event.content)
484
+ * if (event.type === 'tool_call') console.log('Tool:', event.name)
485
+ * }
486
+ *
487
+ * const result = await stream.result
488
+ * console.log('Output items:', result.outputItems)
489
+ * ```
490
+ */
491
+ interface ResponsesStream {
492
+ /** Trace ID for this request, available immediately */
493
+ readonly traceId: string;
494
+ /** Async iterator yielding events */
495
+ [Symbol.asyncIterator](): AsyncIterator<ResponsesEvent>;
496
+ /** Promise that resolves to the final result after stream completes */
497
+ readonly result: Promise<RunResponsesResult>;
498
+ /** Abort the stream */
499
+ abort(): void;
500
+ }
140
501
 
141
502
  interface HttpClientOptions {
142
503
  apiKey: string;
@@ -164,11 +525,18 @@ declare class Prompts {
164
525
  run(slug: string, variables?: RunVariables, options?: RunOptions): Promise<RunResult>;
165
526
  }
166
527
 
528
+ /** @internal Symbol for setting pending traces map - not part of public API */
529
+ declare const INTERNAL_SET_PENDING_TRACES: unique symbol;
167
530
  declare class Traces {
168
531
  private readonly client;
532
+ private pendingTraces;
169
533
  constructor(client: HttpClient);
534
+ /** @internal */
535
+ [INTERNAL_SET_PENDING_TRACES](map: Map<string, Promise<void>>): void;
536
+ create(payload: CreateTracePayload): Promise<CreateTraceResult>;
170
537
  get(traceId: string): Promise<Trace>;
171
538
  list(options?: ListTracesOptions): Promise<ListTracesResult>;
539
+ evaluate(traceId: string, options: EvaluateOptions): Promise<EvaluateResult>;
172
540
  }
173
541
 
174
542
  declare class TraciaError extends Error {
@@ -177,11 +545,114 @@ declare class TraciaError extends Error {
177
545
  constructor(code: TraciaErrorCode, message: string, statusCode?: number);
178
546
  }
179
547
 
548
+ declare const Eval: {
549
+ readonly POSITIVE: 1;
550
+ readonly NEGATIVE: 0;
551
+ };
180
552
  declare class Tracia {
181
553
  private readonly client;
554
+ private readonly pendingTraces;
555
+ private readonly onTraceError?;
182
556
  readonly prompts: Prompts;
183
557
  readonly traces: Traces;
184
558
  constructor(options: TraciaOptions);
559
+ /**
560
+ * Execute an LLM call locally using the Vercel AI SDK.
561
+ *
562
+ * @example Non-streaming (default)
563
+ * ```typescript
564
+ * const result = await tracia.runLocal({
565
+ * model: 'gpt-4o',
566
+ * messages: [{ role: 'user', content: 'Hello' }],
567
+ * })
568
+ * console.log(result.text)
569
+ * ```
570
+ *
571
+ * @example Streaming
572
+ * ```typescript
573
+ * const stream = tracia.runLocal({
574
+ * model: 'gpt-4o',
575
+ * messages: [{ role: 'user', content: 'Write a poem' }],
576
+ * stream: true,
577
+ * })
578
+ *
579
+ * for await (const chunk of stream) {
580
+ * process.stdout.write(chunk)
581
+ * }
582
+ *
583
+ * const result = await stream.result
584
+ * console.log('Tokens used:', result.usage.totalTokens)
585
+ * ```
586
+ */
587
+ runLocal(input: RunLocalInput & {
588
+ stream: true;
589
+ }): LocalStream;
590
+ runLocal(input: RunLocalInput & {
591
+ stream?: false;
592
+ }): Promise<RunLocalResult>;
593
+ private runLocalNonStreaming;
594
+ private runLocalStreaming;
595
+ /**
596
+ * Execute an LLM call using OpenAI's Responses API.
597
+ *
598
+ * The Responses API is OpenAI-specific and supports:
599
+ * - Reasoning models (o1, o3-mini) with reasoning summaries
600
+ * - Multi-turn conversations with output items
601
+ * - Different input format (developer/user roles, function_call_output)
602
+ *
603
+ * @example Non-streaming (default)
604
+ * ```typescript
605
+ * const result = await tracia.runResponses({
606
+ * model: 'o3-mini',
607
+ * input: [
608
+ * { role: 'developer', content: 'You are helpful.' },
609
+ * { role: 'user', content: 'What is 2+2?' },
610
+ * ],
611
+ * })
612
+ * console.log(result.text)
613
+ * ```
614
+ *
615
+ * @example Streaming
616
+ * ```typescript
617
+ * const stream = tracia.runResponses({
618
+ * model: 'o3-mini',
619
+ * input: [
620
+ * { role: 'developer', content: 'You are helpful.' },
621
+ * { role: 'user', content: 'What is 2+2?' },
622
+ * ],
623
+ * stream: true,
624
+ * })
625
+ *
626
+ * for await (const event of stream) {
627
+ * if (event.type === 'text_delta') process.stdout.write(event.data)
628
+ * if (event.type === 'reasoning') console.log('Reasoning:', event.content)
629
+ * if (event.type === 'tool_call') console.log('Tool:', event.name, event.arguments)
630
+ * }
631
+ *
632
+ * const result = await stream.result
633
+ * console.log('Output items:', result.outputItems)
634
+ * ```
635
+ */
636
+ runResponses(input: RunResponsesInput & {
637
+ stream: true;
638
+ }): ResponsesStream;
639
+ runResponses(input: RunResponsesInput & {
640
+ stream?: false;
641
+ }): Promise<RunResponsesResult>;
642
+ private runResponsesNonStreaming;
643
+ private runResponsesStreaming;
644
+ private validateResponsesInput;
645
+ private createResponsesStream;
646
+ private createLocalStream;
647
+ private combineAbortSignals;
648
+ flush(): Promise<void>;
649
+ private validateRunLocalInput;
650
+ private scheduleTraceCreation;
651
+ private createTraceWithRetry;
652
+ private delay;
653
+ private interpolateMessages;
654
+ private buildAssistantMessage;
655
+ private getProviderApiKey;
185
656
  }
186
657
 
187
- export { type CreatePromptOptions, type ListTracesOptions, type ListTracesResult, type MessageRole, type Prompt, type PromptListItem, type PromptMessage, type RunOptions, type RunResult, type RunVariables, type TokenUsage, type Trace, type TraceListItem, type TraceStatus, Tracia, TraciaError, TraciaErrorCode, type TraciaOptions, type UpdatePromptOptions };
658
+ 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 };