@promptmetrics/sdk 1.0.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.
@@ -0,0 +1,1320 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+
3
+ interface HttpClientConfig {
4
+ baseURL: string;
5
+ apiKey: string;
6
+ timeout?: number;
7
+ maxRetries?: number;
8
+ debug?: boolean;
9
+ }
10
+ /**
11
+ * HTTP client with automatic retry logic and error handling
12
+ */
13
+ declare class HttpClient {
14
+ private client;
15
+ private maxRetries;
16
+ private debug;
17
+ constructor(config: HttpClientConfig);
18
+ /**
19
+ * Make HTTP request with retry logic
20
+ */
21
+ request<T>(config: AxiosRequestConfig): Promise<T>;
22
+ /**
23
+ * GET request
24
+ */
25
+ get<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
26
+ /**
27
+ * POST request
28
+ */
29
+ post<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T>;
30
+ /**
31
+ * PUT request
32
+ */
33
+ put<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T>;
34
+ /**
35
+ * PATCH request
36
+ */
37
+ patch<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T>;
38
+ /**
39
+ * DELETE request
40
+ */
41
+ delete<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
42
+ /**
43
+ * Handle axios errors and convert to SDK errors
44
+ * Extracts details from backend HttpException format
45
+ */
46
+ private handleError;
47
+ /**
48
+ * Determine if request should be retried
49
+ */
50
+ private shouldRetry;
51
+ /**
52
+ * Sleep utility for retry backoff
53
+ */
54
+ private sleep;
55
+ }
56
+
57
+ /**
58
+ * Common types used across the SDK
59
+ */
60
+ /**
61
+ * Core configuration types
62
+ */
63
+ interface PromptMetricsConfig {
64
+ /** Workspace API key (starts with pm_) - Required */
65
+ apiKey: string;
66
+ /** Request timeout in milliseconds (default: 30000) */
67
+ timeout?: number;
68
+ /** Maximum number of retries for failed requests (default: 3) */
69
+ maxRetries?: number;
70
+ /** Enable debug logging (default: false) */
71
+ debug?: boolean;
72
+ }
73
+ /**
74
+ * Backend response wrapper
75
+ */
76
+ interface BackendResponse<T> {
77
+ success: boolean;
78
+ message: string;
79
+ responseData: {
80
+ data: T;
81
+ };
82
+ error?: string;
83
+ }
84
+ /**
85
+ * Error types
86
+ */
87
+ interface PromptMetricsErrorResponse {
88
+ success: false;
89
+ message: string;
90
+ error?: string | ErrorObject;
91
+ }
92
+ interface ErrorObject {
93
+ message: string;
94
+ type?: string;
95
+ code?: string;
96
+ param?: string | null;
97
+ [key: string]: unknown;
98
+ }
99
+ /**
100
+ * Pagination metadata
101
+ */
102
+ interface PaginationMeta {
103
+ total: number;
104
+ page: number;
105
+ limit: number;
106
+ pages: number;
107
+ }
108
+
109
+ /**
110
+ * Template-related types
111
+ */
112
+ /**
113
+ * Message types for templates
114
+ */
115
+ interface Message {
116
+ role: "system" | "user" | "assistant" | "function" | "tool";
117
+ content: string;
118
+ name?: string;
119
+ variable?: string[];
120
+ tool_call_id?: string;
121
+ tool_calls?: unknown[];
122
+ }
123
+ interface TemplateMetadata {
124
+ [key: string]: string | number | boolean | null | undefined;
125
+ }
126
+ interface TemplateOptions {
127
+ temperature?: number;
128
+ max_tokens?: number;
129
+ top_p?: number;
130
+ frequency_penalty?: number;
131
+ presence_penalty?: number;
132
+ [key: string]: string | number | boolean | null | undefined;
133
+ }
134
+ interface Template {
135
+ _id: string;
136
+ workspace_id: string;
137
+ parent_id?: string | null;
138
+ name: string;
139
+ description?: string;
140
+ is_folder: boolean;
141
+ created_by: string;
142
+ risk_level?: "LOW" | "MEDIUM" | "HIGH";
143
+ created_at: string;
144
+ updated_at: string;
145
+ versions?: TemplateVersion[];
146
+ tags?: Array<{
147
+ _id: string;
148
+ name: string;
149
+ color?: string;
150
+ }>;
151
+ rating_details?: {
152
+ rating: number;
153
+ rated_by: string;
154
+ rated_by_email: string;
155
+ rated_at: string;
156
+ };
157
+ }
158
+ interface TemplateVersion {
159
+ _id: string;
160
+ template_id: string;
161
+ version: number;
162
+ labels: string[];
163
+ env_label: "development" | "staging" | "production";
164
+ model_id?: string | null;
165
+ created_by: string | {
166
+ _id: string;
167
+ first_name: string;
168
+ last_name: string;
169
+ email: string;
170
+ };
171
+ message_format: "f-string" | "jinja2";
172
+ metadata: TemplateMetadata;
173
+ display_mode: number;
174
+ api_type: string | null;
175
+ messages: Message[];
176
+ options: TemplateOptions;
177
+ created_at: string;
178
+ updated_at: string;
179
+ model?: {
180
+ _id: string;
181
+ name: string;
182
+ provider_id: string;
183
+ prompt_cost_per_million_token: number;
184
+ output_cost_per_million_token: number;
185
+ cached_input_cost_per_million_token: number;
186
+ };
187
+ model_name?: string;
188
+ provider?: {
189
+ _id: string;
190
+ name: string;
191
+ slug: string;
192
+ } | string;
193
+ provider_slug?: string;
194
+ workspace_id?: string;
195
+ }
196
+ /**
197
+ * List prompts options (workspace_id comes from API key)
198
+ */
199
+ interface ListPromptsOptions {
200
+ search?: string;
201
+ parent_id?: string | null;
202
+ risk_level?: "LOW" | "MEDIUM" | "HIGH";
203
+ created_by?: string;
204
+ start_date?: string;
205
+ end_date?: string;
206
+ sort_by?: "created_at" | "updated_at" | "name";
207
+ sort_order?: "asc" | "desc";
208
+ page?: number;
209
+ per_page?: number;
210
+ }
211
+ /**
212
+ * List prompts response
213
+ */
214
+ interface ListPromptsResponse {
215
+ prompts: Template[];
216
+ total: number;
217
+ }
218
+ /**
219
+ * Get template options
220
+ */
221
+ interface GetTemplateOptions {
222
+ /** Specific version number to fetch */
223
+ version?: number;
224
+ /** Environment label to filter versions */
225
+ env_label?: "development" | "staging" | "production";
226
+ }
227
+ /**
228
+ * Run template options (canary-aware)
229
+ * Used by templates.run() method
230
+ */
231
+ interface RunTemplateOptions {
232
+ /** Variables to substitute in template messages */
233
+ variables?: Record<string, unknown>;
234
+ /** Model name override */
235
+ model?: string;
236
+ /** Model ID override */
237
+ model_id?: string;
238
+ /** LLM parameters override */
239
+ parameters?: Record<string, unknown>;
240
+ /** Custom messages override */
241
+ messages?: Message[];
242
+ /** API type override */
243
+ api_type?: "chat.completion" | "response";
244
+ /** Custom tags to associate with this prompt log */
245
+ pm_tags?: string[];
246
+ /** Trace ID for correlation */
247
+ trace_id?: string;
248
+ /** Span ID for correlation */
249
+ span_id?: string;
250
+ /** Group ID for correlation */
251
+ group_id?: string;
252
+ /** Specific version number to run (bypasses canary) */
253
+ version?: number;
254
+ /** Environment label to run (production uses canary if active) */
255
+ env_label?: "development" | "staging" | "production" | "legacy" | "canary";
256
+ /** Version name/description to run (bypasses canary) */
257
+ version_name?: string;
258
+ }
259
+
260
+ /**
261
+ * Provider and model types
262
+ */
263
+ interface LLMModel {
264
+ _id: string;
265
+ name: string;
266
+ provider_id: string;
267
+ prompt_cost_per_million_token: number;
268
+ output_cost_per_million_token: number;
269
+ cached_input_cost_per_million_token: number;
270
+ created_at?: string;
271
+ updated_at?: string;
272
+ }
273
+ interface LLMProvider {
274
+ _id: string;
275
+ name: string;
276
+ slug: string;
277
+ has_key?: boolean;
278
+ created_at?: string;
279
+ updated_at?: string;
280
+ }
281
+ interface ProviderWithModels extends LLMProvider {
282
+ models: LLMModel[];
283
+ parameters?: Record<string, unknown>[];
284
+ }
285
+
286
+ /**
287
+ * Version-related types
288
+ */
289
+
290
+ /**
291
+ * Options for running a specific template version
292
+ */
293
+ interface RunVersionOptions {
294
+ /** Variables to substitute in template messages */
295
+ variables?: Record<string, unknown>;
296
+ model?: string;
297
+ parameters?: Record<string, unknown>;
298
+ messages?: Message[];
299
+ /** Custom tags to associate with this prompt log (stored as string array) */
300
+ pm_tags?: string[];
301
+ }
302
+ /**
303
+ * Options for updating a template version
304
+ */
305
+ interface UpdateVersionOptions {
306
+ /** Environment label */
307
+ env_label?: "development" | "staging" | "production";
308
+ /** Custom metadata */
309
+ metadata?: Record<string, unknown>;
310
+ }
311
+ interface RequestObject {
312
+ model?: string;
313
+ messages?: Message[];
314
+ temperature?: number;
315
+ max_tokens?: number;
316
+ [key: string]: unknown;
317
+ }
318
+ interface ResponseObject {
319
+ id?: string;
320
+ model?: string;
321
+ choices?: Array<{
322
+ message?: Message;
323
+ finish_reason?: string;
324
+ index?: number;
325
+ }>;
326
+ usage?: {
327
+ prompt_tokens?: number;
328
+ completion_tokens?: number;
329
+ total_tokens?: number;
330
+ };
331
+ [key: string]: unknown;
332
+ }
333
+
334
+ /**
335
+ * Log-related types
336
+ */
337
+
338
+ interface PromptLog {
339
+ _id: string;
340
+ request_id: string;
341
+ template_version_id: string;
342
+ template_id: string;
343
+ workspace_id: string;
344
+ created_by: string;
345
+ source: "website" | "sdk";
346
+ llm_prompt_tokens: number;
347
+ llm_completion_tokens: number;
348
+ llm_total_tokens: number;
349
+ prompt_cost: number;
350
+ completion_cost: number;
351
+ total_cost: number;
352
+ request_object: RequestObject;
353
+ response_object: ResponseObject;
354
+ error_object?: ErrorObject | null;
355
+ latency: number;
356
+ status: "SUCCESS" | "ERROR";
357
+ messages: Message[];
358
+ model_id: string | null;
359
+ model_name: string;
360
+ start_time: string;
361
+ end_time: string;
362
+ variables: Record<string, string>;
363
+ options: TemplateOptions;
364
+ note?: string;
365
+ created_at: string;
366
+ updated_at: string;
367
+ trace_id?: string;
368
+ span_id?: string;
369
+ group_id?: string;
370
+ tags?: string[];
371
+ }
372
+ interface ListLogsOptions {
373
+ template_id?: string;
374
+ template_version_id?: string;
375
+ model_id?: string;
376
+ start_date?: string;
377
+ end_date?: string;
378
+ status?: "SUCCESS" | "ERROR" | "all";
379
+ source?: "website" | "sdk";
380
+ limit?: number;
381
+ page?: number;
382
+ sort_by?: "created_at" | "latency" | "cost" | "tokens" | "status";
383
+ sort_order?: "asc" | "desc";
384
+ }
385
+
386
+ /**
387
+ * Trace/Tracking types for @traceable decorator
388
+ */
389
+
390
+ type TraceStatus = "SUCCESS" | "ERROR" | "PENDING";
391
+ interface TraceError {
392
+ message: string;
393
+ stack?: string;
394
+ code?: string;
395
+ }
396
+ interface TraceScore {
397
+ criteria: string;
398
+ value: number;
399
+ timestamp: string;
400
+ }
401
+ interface Trace {
402
+ _id: string;
403
+ workspace_id: string;
404
+ trace_id: string;
405
+ span_id: string;
406
+ parent_span_id?: string;
407
+ function_name: string;
408
+ start_time: string;
409
+ end_time: string;
410
+ duration_ms: number;
411
+ status: TraceStatus;
412
+ error?: TraceError;
413
+ input?: Record<string, unknown>;
414
+ output?: Record<string, unknown>;
415
+ metadata?: Record<string, unknown>;
416
+ scores?: TraceScore[];
417
+ group_id?: string;
418
+ group_type?: string;
419
+ tags?: string[];
420
+ created_at: string;
421
+ updated_at: string;
422
+ }
423
+ interface TraceTreeNode extends Omit<Trace, "_id" | "workspace_id" | "created_at" | "updated_at"> {
424
+ _id: string;
425
+ children: TraceTreeNode[];
426
+ }
427
+ interface CreateTraceOptions {
428
+ trace_id: string;
429
+ span_id: string;
430
+ parent_span_id?: string;
431
+ function_name: string;
432
+ start_time: Date | string;
433
+ end_time: Date | string;
434
+ duration_ms: number;
435
+ status: TraceStatus;
436
+ error?: TraceError;
437
+ input?: Record<string, unknown>;
438
+ output?: Record<string, unknown>;
439
+ metadata?: Record<string, unknown>;
440
+ scores?: Array<{
441
+ criteria: string;
442
+ value: number;
443
+ }>;
444
+ tags?: string[];
445
+ group_id?: string;
446
+ group_type?: string;
447
+ }
448
+ interface AddTraceScoreOptions {
449
+ criteria: string;
450
+ value: number;
451
+ }
452
+ interface UpdateTraceMetadataOptions {
453
+ metadata: Record<string, unknown>;
454
+ }
455
+ interface ListTracesOptions {
456
+ trace_id?: string;
457
+ function_name?: string;
458
+ status?: TraceStatus;
459
+ group_id?: string;
460
+ group_type?: string;
461
+ tags?: string[];
462
+ start_date?: string;
463
+ end_date?: string;
464
+ page?: number;
465
+ limit?: number;
466
+ }
467
+ interface TraceListResponse {
468
+ traces: Trace[];
469
+ pagination: PaginationMeta;
470
+ }
471
+ interface TraceAnalytics {
472
+ total_traces: number;
473
+ success_rate: number;
474
+ average_duration_ms: number;
475
+ error_count: number;
476
+ function_breakdown: Array<{
477
+ function_name: string;
478
+ count: number;
479
+ avg_duration_ms: number;
480
+ success_rate: number;
481
+ }>;
482
+ }
483
+ /**
484
+ * Decorator options for @traceable
485
+ */
486
+ interface TraceableOptions {
487
+ /** Name of the function (for display) */
488
+ name?: string;
489
+ /** Static metadata to attach */
490
+ metadata?: Record<string, unknown>;
491
+ /** Tags for categorization (e.g., ['payment', 'critical', 'production']) */
492
+ tags?: string[];
493
+ /** Disable tracing for this function */
494
+ disabled?: boolean;
495
+ }
496
+
497
+ /**
498
+ * Base resource class with shared functionality
499
+ */
500
+ declare abstract class BaseResource {
501
+ protected http: HttpClient;
502
+ constructor(http: HttpClient);
503
+ /**
504
+ * Transform backend response to SDK format
505
+ * Extracts data from the general response wrapper
506
+ *
507
+ * Backend format: { success: true, message: "...", responseData: { data: {...} } }
508
+ * SDK format: Just the data
509
+ */
510
+ protected transformResponse<T>(backendResponse: BackendResponse<T> | T): T;
511
+ }
512
+
513
+ /**
514
+ * Template resource for managing and executing templates
515
+ */
516
+ declare class TemplateResource extends BaseResource {
517
+ /**
518
+ * Get a template by ID or name
519
+ * Workspace ID is automatically determined from API key
520
+ *
521
+ * @param identifier - Template ID or name
522
+ * @param options - Optional version and env_label filters
523
+ * @param options.version - Specific version number to fetch
524
+ * @param options.env_label - Environment label: 'development' | 'staging' | 'production'
525
+ * @returns Template with versions
526
+ *
527
+ * @example
528
+ * ```typescript
529
+ * // Get template by ID (returns all versions)
530
+ * const template = await client.templates.get('template_id_123');
531
+ *
532
+ * // Get template by name (returns all versions)
533
+ * const template = await client.templates.get('my-template-name');
534
+ *
535
+ * // Get specific version
536
+ * const template = await client.templates.get('my-template', {
537
+ * version: 2
538
+ * });
539
+ *
540
+ * // Get by environment label
541
+ * const template = await client.templates.get('my-template', {
542
+ * env_label: 'production'
543
+ * });
544
+ * ```
545
+ */
546
+ get(identifier: string, options?: GetTemplateOptions): Promise<Template>;
547
+ /**
548
+ * List prompts (templates) with advanced filtering
549
+ * Workspace ID is automatically determined from API key
550
+ *
551
+ * @param options - Filter and pagination options
552
+ * @param options.search - Search in prompt name/description
553
+ * @param options.parent_id - Filter by parent folder ID
554
+ * @param options.risk_level - Filter by risk level: 'LOW' | 'MEDIUM' | 'HIGH'
555
+ * @param options.created_by - Filter by creator user ID
556
+ * @param options.start_date - Filter by creation date (ISO string)
557
+ * @param options.end_date - Filter by creation date (ISO string)
558
+ * @param options.sort_by - Sort field: 'created_at' | 'updated_at' | 'name'
559
+ * @param options.sort_order - Sort order: 'asc' | 'desc'
560
+ * @param options.page - Page number (default: 1)
561
+ * @param options.per_page - Items per page (default: 50)
562
+ * @returns Prompts list with total count
563
+ *
564
+ * @example
565
+ * ```typescript
566
+ * // List all prompts
567
+ * const result = await client.templates.list();
568
+ * console.log(`Found ${result.total} prompts`);
569
+ *
570
+ * // Search and filter prompts
571
+ * const result = await client.templates.list({
572
+ * search: 'customer',
573
+ * risk_level: 'HIGH',
574
+ * sort_by: 'updated_at',
575
+ * sort_order: 'desc'
576
+ * });
577
+ *
578
+ * // Paginate results
579
+ * const result = await client.templates.list({
580
+ * page: 1,
581
+ * per_page: 20
582
+ * });
583
+ * ```
584
+ */
585
+ list(options?: ListPromptsOptions): Promise<ListPromptsResponse>;
586
+ /**
587
+ * Run a template (canary-aware)
588
+ *
589
+ * This method supports canary deployments:
590
+ * - If no version/env_label specified: Uses production with canary support
591
+ * - If specific version/env_label specified: Uses that version directly (no canary)
592
+ *
593
+ * @param identifier - Template ID or name
594
+ * @param options - Run options including variables, model overrides, and version selection
595
+ * @returns PromptLog with execution results
596
+ *
597
+ * @example
598
+ * ```typescript
599
+ * // Run template with production version (canary-aware)
600
+ * const result = await client.templates.run('my-template', {
601
+ * variables: { name: 'John', topic: 'AI' }
602
+ * });
603
+ *
604
+ * // Run specific version (bypasses canary)
605
+ * const result = await client.templates.run('my-template', {
606
+ * version: 3,
607
+ * variables: { name: 'John' }
608
+ * });
609
+ *
610
+ * // Run with environment label
611
+ * const result = await client.templates.run('my-template', {
612
+ * env_label: 'staging',
613
+ * variables: { name: 'John' }
614
+ * });
615
+ *
616
+ * // Run with model override and tags
617
+ * const result = await client.templates.run('my-template', {
618
+ * variables: { name: 'John' },
619
+ * model: 'gpt-4',
620
+ * pm_tags: ['production', 'customer-support']
621
+ * });
622
+ * ```
623
+ */
624
+ run(identifier: string, options?: RunTemplateOptions): Promise<PromptLog>;
625
+ }
626
+
627
+ /**
628
+ * Log resource for accessing prompt execution logs
629
+ */
630
+ declare class LogResource extends BaseResource {
631
+ /**
632
+ * List prompt logs (SDK logs only by default)
633
+ *
634
+ * @param options - List options including filters, pagination
635
+ * @returns Array of prompt logs
636
+ *
637
+ * @example
638
+ * ```typescript
639
+ * // List all SDK logs (workspace_id from API key)
640
+ * const logs = await client.logs.list({});
641
+ *
642
+ * // Filter by template
643
+ * const logs = await client.logs.list({
644
+ * template_id: 'template_123'
645
+ * });
646
+ *
647
+ * // Filter by template version
648
+ * const logs = await client.logs.list({
649
+ * template_version_id: 'version_123'
650
+ * });
651
+ *
652
+ * // Filter by date range
653
+ * const logs = await client.logs.list({
654
+ * start_date: '2024-01-01',
655
+ * end_date: '2024-01-31'
656
+ * });
657
+ *
658
+ * // Filter by status
659
+ * const logs = await client.logs.list({
660
+ * status: 'SUCCESS'
661
+ * });
662
+ *
663
+ * // Paginate and sort results
664
+ * const logs = await client.logs.list({
665
+ * page: 1,
666
+ * limit: 50,
667
+ * sort_by: 'created_at',
668
+ * sort_order: 'desc'
669
+ * });
670
+ * ```
671
+ */
672
+ list(options?: ListLogsOptions): Promise<PromptLog[]>;
673
+ /**
674
+ * Get a single prompt log by ID
675
+ *
676
+ * @param logId - Log ID
677
+ * @returns Prompt log details
678
+ *
679
+ * @example
680
+ * ```typescript
681
+ * const log = await client.logs.get('log_123');
682
+ * ```
683
+ */
684
+ get(logId: string): Promise<PromptLog>;
685
+ }
686
+
687
+ /**
688
+ * Template version resource for managing and executing specific versions
689
+ */
690
+ declare class VersionResource extends BaseResource {
691
+ /**
692
+ * Get a template version
693
+ *
694
+ * @param versionId - Version ID
695
+ * @returns Template version details
696
+ *
697
+ * @example
698
+ * ```typescript
699
+ * // Get by version ID
700
+ * const version = await client.versions.get('version_123');
701
+ *
702
+ * // Get by template name + version number
703
+ * const version = await client.versions.get('my-template', 2);
704
+ *
705
+ * // Get by template ID + version number
706
+ * const version = await client.versions.get('template_123', 1);
707
+ * ```
708
+ */
709
+ get(versionId: string): Promise<TemplateVersion>;
710
+ get(templateIdentifier: string, versionNumber: number): Promise<TemplateVersion>;
711
+ /**
712
+ * Run a template version with variables
713
+ *
714
+ * Automatically correlates with active trace context if called within @traceable function.
715
+ * The prompt_log will be linked to the current trace for end-to-end observability.
716
+ *
717
+ * @param versionId - Version ID to run
718
+ * @param options - Run options including variables and parameters
719
+ * @returns Prompt execution log
720
+ *
721
+ * @example
722
+ * ```typescript
723
+ * // Run a version with variables
724
+ * const result = await client.versions.run('version_123', {
725
+ * variables: {
726
+ * user_name: 'John',
727
+ * topic: 'AI'
728
+ * }
729
+ * });
730
+ *
731
+ * // Run with custom parameters
732
+ * const result = await client.versions.run('version_123', {
733
+ * variables: { topic: 'ML' },
734
+ * parameters: {
735
+ * temperature: 0.7,
736
+ * max_tokens: 500
737
+ * }
738
+ * });
739
+ *
740
+ * // Run with custom tags (stored directly in prompt log)
741
+ * const result = await client.versions.run('version_123', {
742
+ * variables: { topic: 'AI' },
743
+ * pm_tags: ['customer-support', 'high-priority', 'eu-region']
744
+ * });
745
+ *
746
+ * // Run within traced function (auto-correlation)
747
+ * @pm.traceable({ name: 'generate_response' })
748
+ * async generateResponse(input: string) {
749
+ * // This run will be automatically linked to the trace
750
+ * const result = await pm.versions.run('version_123', {
751
+ * variables: { input }
752
+ * });
753
+ * return result;
754
+ * }
755
+ * ```
756
+ */
757
+ run(versionId: string, options?: RunVersionOptions): Promise<PromptLog>;
758
+ /**
759
+ * Update a template version
760
+ *
761
+ * @param versionId - Version ID to update
762
+ * @param options - Update options (env_label, metadata, etc.)
763
+ * @returns Updated template version
764
+ *
765
+ * @example
766
+ * ```typescript
767
+ * // Update environment label
768
+ * const version = await client.versions.update('version_123', {
769
+ * env_label: 'production'
770
+ * });
771
+ *
772
+ * // Update metadata
773
+ * const version = await client.versions.update('version_123', {
774
+ * metadata: {
775
+ * deployed_by: 'John Doe',
776
+ * deployment_date: '2024-01-15'
777
+ * }
778
+ * });
779
+ *
780
+ * // Update both env_label and metadata
781
+ * const version = await client.versions.update('version_123', {
782
+ * env_label: 'staging',
783
+ * metadata: { tested: true },
784
+ * });
785
+ *
786
+ * ```
787
+ */
788
+ update(versionId: string, options: UpdateVersionOptions): Promise<TemplateVersion>;
789
+ }
790
+
791
+ /**
792
+ * Provider resource for accessing LLM providers and models
793
+ */
794
+ declare class ProviderResource extends BaseResource {
795
+ /**
796
+ * List all LLM providers
797
+ *
798
+ * Returns providers with `has_key` flag indicating if user has configured API key
799
+ *
800
+ * @returns Array of LLM providers
801
+ *
802
+ * @example
803
+ * ```typescript
804
+ * // List all providers
805
+ * const providers = await client.providers.list();
806
+ *
807
+ * // Check which providers have keys configured
808
+ * const configuredProviders = providers.filter(p => p.has_key);
809
+ * ```
810
+ */
811
+ list(): Promise<LLMProvider[]>;
812
+ /**
813
+ * Get models for a specific provider by slug
814
+ *
815
+ * Returns provider details with all available models and parameters.
816
+ * Only returns models if user has API key configured for the provider.
817
+ *
818
+ * @param slug - Provider slug (e.g., 'openai', 'anthropic', 'google')
819
+ * @returns Provider with models and parameters
820
+ *
821
+ * @example
822
+ * ```typescript
823
+ * // Get OpenAI models
824
+ * const openai = await client.providers.getModels('openai');
825
+ * console.log(openai.models); // Array of OpenAI models
826
+ *
827
+ * // Get Anthropic models
828
+ * const anthropic = await client.providers.getModels('anthropic');
829
+ * console.log(anthropic.models); // Array of Claude models
830
+ *
831
+ * // Get Google models
832
+ * const google = await client.providers.getModels('google');
833
+ * console.log(google.models); // Array of Gemini models
834
+ * ```
835
+ */
836
+ getModels(slug: string): Promise<ProviderWithModels>;
837
+ }
838
+
839
+ /**
840
+ * Trace resource for tracking function execution and workflows
841
+ *
842
+ * @example
843
+ * ```typescript
844
+ * // Create a trace
845
+ * const trace = await client.traces.create({
846
+ * trace_id: 'trace_123',
847
+ * span_id: 'span_abc',
848
+ * function_name: 'process_data',
849
+ * start_time: new Date(),
850
+ * end_time: new Date(),
851
+ * duration_ms: 150,
852
+ * status: 'SUCCESS'
853
+ * });
854
+ *
855
+ * // Add a score
856
+ * await client.traces.addScore('span_abc', {
857
+ * criteria: 'accuracy',
858
+ * value: 0.95
859
+ * });
860
+ *
861
+ * // Get trace tree
862
+ * const tree = await client.traces.getTrace('trace_123');
863
+ * ```
864
+ */
865
+ declare class TraceResource extends BaseResource {
866
+ /**
867
+ * Create a new trace
868
+ *
869
+ * @param options - Trace creation options
870
+ * @returns Created trace
871
+ *
872
+ * @example
873
+ * ```typescript
874
+ * const trace = await client.traces.create({
875
+ * trace_id: 'trace_123',
876
+ * span_id: 'span_abc',
877
+ * function_name: 'process_customer_data',
878
+ * function_type: 'CUSTOM',
879
+ * start_time: new Date('2025-12-08T10:00:00Z'),
880
+ * end_time: new Date('2025-12-08T10:00:02Z'),
881
+ * duration_ms: 2000,
882
+ * status: 'SUCCESS',
883
+ * metadata: {
884
+ * customer_id: '12345',
885
+ * processing_stage: 'enrichment'
886
+ * }
887
+ * });
888
+ * ```
889
+ */
890
+ create(options: CreateTraceOptions): Promise<Trace>;
891
+ /**
892
+ * Create multiple traces in batch (optimized for high throughput)
893
+ *
894
+ * Reduces HTTP overhead by sending multiple traces in a single request.
895
+ * Supports partial success - some traces may succeed while others fail.
896
+ *
897
+ * @param traces - Array of trace creation options (max 100)
898
+ * @returns Batch result with created traces and errors
899
+ *
900
+ * @example
901
+ * ```typescript
902
+ * const result = await client.traces.createBatch([
903
+ * {
904
+ * trace_id: 'trace_123',
905
+ * span_id: 'span_abc',
906
+ * function_name: 'step_1',
907
+ * start_time: new Date(),
908
+ * end_time: new Date(),
909
+ * duration_ms: 100,
910
+ * status: 'SUCCESS'
911
+ * },
912
+ * {
913
+ * trace_id: 'trace_123',
914
+ * span_id: 'span_def',
915
+ * function_name: 'step_2',
916
+ * start_time: new Date(),
917
+ * end_time: new Date(),
918
+ * duration_ms: 200,
919
+ * status: 'SUCCESS'
920
+ * }
921
+ * ]);
922
+ *
923
+ * console.log(`Created: ${result.summary.successful}, Failed: ${result.summary.failed}`);
924
+ * ```
925
+ */
926
+ createBatch(traces: CreateTraceOptions[]): Promise<{
927
+ created: Trace[];
928
+ errors: Array<{
929
+ index: number;
930
+ error: string;
931
+ }>;
932
+ summary: {
933
+ total: number;
934
+ successful: number;
935
+ failed: number;
936
+ };
937
+ }>;
938
+ /**
939
+ * Add a score to a trace
940
+ *
941
+ * @param span_id - Span ID of the trace
942
+ * @param options - Score options
943
+ * @returns Updated trace with score
944
+ *
945
+ * @example
946
+ * ```typescript
947
+ * const trace = await client.traces.addScore('span_abc', {
948
+ * criteria: 'coherence',
949
+ * value: 0.92
950
+ * });
951
+ * ```
952
+ */
953
+ addScore(span_id: string, options: AddTraceScoreOptions): Promise<Trace>;
954
+ /**
955
+ * Update trace metadata
956
+ *
957
+ * @param span_id - Span ID of the trace
958
+ * @param options - Metadata update options
959
+ * @returns Updated trace
960
+ *
961
+ * @example
962
+ * ```typescript
963
+ * const trace = await client.traces.updateMetadata('span_abc', {
964
+ * metadata: {
965
+ * processing_result: 'completed',
966
+ * items_processed: 150
967
+ * }
968
+ * });
969
+ * ```
970
+ */
971
+ updateMetadata(span_id: string, options: UpdateTraceMetadataOptions): Promise<Trace>;
972
+ /**
973
+ * Get a single trace by span_id
974
+ *
975
+ * @param span_id - Span ID of the trace
976
+ * @returns Trace details
977
+ *
978
+ * @example
979
+ * ```typescript
980
+ * const trace = await client.traces.getBySpanId('span_abc');
981
+ * console.log(trace.function_name, trace.duration_ms);
982
+ * ```
983
+ */
984
+ getBySpanId(span_id: string): Promise<Trace>;
985
+ /**
986
+ * Get entire trace tree by trace_id
987
+ *
988
+ * Returns all spans in the trace organized as a tree structure
989
+ * with parent-child relationships
990
+ *
991
+ * @param trace_id - Trace ID
992
+ * @returns Array of trace tree nodes with nested children
993
+ *
994
+ * @example
995
+ * ```typescript
996
+ * const tree = await client.traces.getTrace('trace_123');
997
+ *
998
+ * // Tree structure with children
999
+ * tree.forEach(root => {
1000
+ * console.log(`Root: ${root.function_name}`);
1001
+ * root.children.forEach(child => {
1002
+ * console.log(` Child: ${child.function_name}`);
1003
+ * });
1004
+ * });
1005
+ * ```
1006
+ */
1007
+ getTrace(trace_id: string): Promise<TraceTreeNode[]>;
1008
+ /**
1009
+ * Get all traces for a group_id
1010
+ *
1011
+ * @param group_id - Group ID (e.g., conversation_id, workflow_id)
1012
+ * @returns Array of traces in the group
1013
+ *
1014
+ * @example
1015
+ * ```typescript
1016
+ * const traces = await client.traces.getGroup('conversation_abc');
1017
+ * console.log(`Found ${traces.length} traces in conversation`);
1018
+ * ```
1019
+ */
1020
+ getGroup(group_id: string): Promise<Trace[]>;
1021
+ /**
1022
+ * List traces with filters and pagination
1023
+ *
1024
+ * @param options - List options with filters
1025
+ * @returns Paginated list of traces
1026
+ *
1027
+ * @example
1028
+ * ```typescript
1029
+ * // List all traces
1030
+ * const result = await client.traces.list();
1031
+ *
1032
+ * // Filter by function name
1033
+ * const filtered = await client.traces.list({
1034
+ * function_name: 'process_customer_data',
1035
+ * status: 'SUCCESS',
1036
+ * page: 1,
1037
+ * limit: 50
1038
+ * });
1039
+ *
1040
+ * // Filter by date range
1041
+ * const recent = await client.traces.list({
1042
+ * start_date: '2025-12-01T00:00:00Z',
1043
+ * end_date: '2025-12-08T23:59:59Z'
1044
+ * });
1045
+ * ```
1046
+ */
1047
+ list(options?: ListTracesOptions): Promise<TraceListResponse>;
1048
+ /**
1049
+ * Get trace analytics for workspace
1050
+ *
1051
+ * Returns aggregated statistics about trace execution including
1052
+ * success rates, average duration, and function-level breakdown
1053
+ *
1054
+ * @param options - Optional date range filter
1055
+ * @returns Analytics data
1056
+ *
1057
+ * @example
1058
+ * ```typescript
1059
+ * // Get all-time analytics
1060
+ * const analytics = await client.traces.getAnalytics();
1061
+ * console.log(`Success rate: ${analytics.success_rate}%`);
1062
+ * console.log(`Avg duration: ${analytics.average_duration_ms}ms`);
1063
+ *
1064
+ * // Get analytics for date range
1065
+ * const weeklyAnalytics = await client.traces.getAnalytics({
1066
+ * start_date: '2025-12-01T00:00:00Z',
1067
+ * end_date: '2025-12-08T23:59:59Z'
1068
+ * });
1069
+ *
1070
+ * // Function breakdown
1071
+ * analytics.function_breakdown.forEach(fn => {
1072
+ * console.log(`${fn.function_name}: ${fn.count} calls, ${fn.avg_duration_ms}ms avg`);
1073
+ * });
1074
+ * ```
1075
+ */
1076
+ getAnalytics(options?: {
1077
+ start_date?: string;
1078
+ end_date?: string;
1079
+ }): Promise<TraceAnalytics>;
1080
+ /**
1081
+ * Delete old traces (cleanup)
1082
+ *
1083
+ * Deletes traces older than the specified date
1084
+ *
1085
+ * @param before_date - Delete traces before this date
1086
+ * @returns Number of traces deleted
1087
+ *
1088
+ * @example
1089
+ * ```typescript
1090
+ * // Delete traces older than 90 days
1091
+ * const ninetyDaysAgo = new Date();
1092
+ * ninetyDaysAgo.setDate(ninetyDaysAgo.getDate() - 90);
1093
+ *
1094
+ * const result = await client.traces.cleanup(ninetyDaysAgo.toISOString());
1095
+ * console.log(`Deleted ${result.deleted_count} old traces`);
1096
+ * ```
1097
+ */
1098
+ cleanup(before_date: string): Promise<{
1099
+ deleted_count: number;
1100
+ }>;
1101
+ }
1102
+
1103
+ /**
1104
+ * Track helper for context-aware metadata, score, and group tracking
1105
+ * Similar to Python's pm.track.metadata(), pm.track.score(), pm.track.group()
1106
+ *
1107
+ * All tracking methods collect data in context, which is sent when the trace is created.
1108
+ */
1109
+ declare class Track {
1110
+ /**
1111
+ * Add metadata to the current span
1112
+ * Metadata is collected in context and sent when the trace is created
1113
+ *
1114
+ * @example
1115
+ * ```typescript
1116
+ * pm.track.metadata({
1117
+ * customer_id: '12345',
1118
+ * processing_stage: 'enrichment'
1119
+ * });
1120
+ * ```
1121
+ */
1122
+ metadata(extraData: Record<string, unknown>): void;
1123
+ /**
1124
+ * Add a score to the current span
1125
+ * Score is collected in context and sent when the trace is created
1126
+ *
1127
+ * @example
1128
+ * ```typescript
1129
+ * pm.track.score({
1130
+ * criteria: 'coherence',
1131
+ * value: 0.92
1132
+ * });
1133
+ * ```
1134
+ */
1135
+ score(options: {
1136
+ criteria: string;
1137
+ value: number;
1138
+ }): void;
1139
+ /**
1140
+ * Set group for current context and all child spans
1141
+ * Uses context to propagate group to nested functions
1142
+ *
1143
+ * @example
1144
+ * ```typescript
1145
+ * pm.track.group({
1146
+ * group_id: 'conversation_abc',
1147
+ * group_type: 'conversation' // Flexible - use any string
1148
+ * });
1149
+ * ```
1150
+ */
1151
+ group(options: {
1152
+ group_id: string;
1153
+ group_type: string;
1154
+ }): void;
1155
+ }
1156
+
1157
+ /**
1158
+ * Main PromptMetrics SDK client
1159
+ *
1160
+ * @example
1161
+ * ```typescript
1162
+ * import { PromptMetrics } from '@promptmetrics/sdk';
1163
+ *
1164
+ * const client = new PromptMetrics({
1165
+ * apiKey: 'pm_xxxxxxxxxxxxx'
1166
+ * });
1167
+ *
1168
+ * // Run a template
1169
+ * const result = await client.templates.run('customer-support', {
1170
+ * workspace_id: 'workspace_123',
1171
+ * label: 'production',
1172
+ * variables: { customer_name: 'John', issue: 'billing' }
1173
+ * });
1174
+ *
1175
+ * // Get logs
1176
+ * const logs = await client.logs.list({
1177
+ * workspace_id: 'workspace_123',
1178
+ * limit: 100
1179
+ * });
1180
+ * ```
1181
+ */
1182
+ declare class PromptMetrics {
1183
+ private httpClient;
1184
+ /** Template operations */
1185
+ templates: TemplateResource;
1186
+ /** Template version operations */
1187
+ versions: VersionResource;
1188
+ /** Log operations */
1189
+ logs: LogResource;
1190
+ /** Provider and model operations */
1191
+ providers: ProviderResource;
1192
+ /** Trace and tracking operations */
1193
+ traces: TraceResource;
1194
+ /** Track helper for context-aware metadata, score, and group tracking */
1195
+ track: Track;
1196
+ /**
1197
+ * Create a new PromptMetrics client
1198
+ *
1199
+ * @param config - Configuration options
1200
+ * @throws {ValidationError} If API key is missing or invalid
1201
+ */
1202
+ constructor(config: PromptMetricsConfig);
1203
+ /**
1204
+ * Create a @traceable decorator for automatic function tracking
1205
+ *
1206
+ * @param options - Decorator options
1207
+ * @returns Decorator function
1208
+ *
1209
+ * @example
1210
+ * ```typescript
1211
+ * class MyService {
1212
+ * @pm.traceable({ name: 'process_data' })
1213
+ * async processData(input: string) {
1214
+ * // Automatically tracked
1215
+ * return result;
1216
+ * }
1217
+ * }
1218
+ * ```
1219
+ */
1220
+ traceable(options?: TraceableOptions): (_target: object, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
1221
+ /**
1222
+ * Get current trace ID from context
1223
+ */
1224
+ getCurrentTraceId(): string | undefined;
1225
+ /**
1226
+ * Get current span ID from context
1227
+ */
1228
+ getCurrentSpanId(): string | undefined;
1229
+ /**
1230
+ * Validate configuration
1231
+ */
1232
+ private validateConfig;
1233
+ }
1234
+
1235
+ /**
1236
+ * Base error class for PromptMetrics SDK
1237
+ */
1238
+ declare class PromptMetricsError extends Error {
1239
+ readonly statusCode?: number;
1240
+ readonly code?: string;
1241
+ readonly details?: unknown;
1242
+ constructor(message: string, statusCode?: number, code?: string, details?: unknown);
1243
+ /**
1244
+ * Custom JSON serialization to include message field
1245
+ */
1246
+ toJSON(): Record<string, unknown>;
1247
+ }
1248
+ /**
1249
+ * Authentication error (401)
1250
+ */
1251
+ declare class AuthenticationError extends PromptMetricsError {
1252
+ constructor(message?: string);
1253
+ }
1254
+ /**
1255
+ * Authorization error (403)
1256
+ */
1257
+ declare class AuthorizationError extends PromptMetricsError {
1258
+ constructor(message?: string);
1259
+ }
1260
+ /**
1261
+ * Not found error (404)
1262
+ */
1263
+ declare class NotFoundError extends PromptMetricsError {
1264
+ constructor(message?: string);
1265
+ }
1266
+ /**
1267
+ * Validation error (400)
1268
+ */
1269
+ declare class ValidationError extends PromptMetricsError {
1270
+ constructor(message?: string, details?: unknown);
1271
+ }
1272
+ /**
1273
+ * Rate limit error (429)
1274
+ */
1275
+ declare class RateLimitError extends PromptMetricsError {
1276
+ constructor(message?: string);
1277
+ }
1278
+ /**
1279
+ * Server error (500)
1280
+ */
1281
+ declare class ServerError extends PromptMetricsError {
1282
+ constructor(message?: string);
1283
+ }
1284
+ /**
1285
+ * Network error
1286
+ */
1287
+ declare class NetworkError extends PromptMetricsError {
1288
+ constructor(message?: string);
1289
+ }
1290
+ /**
1291
+ * Timeout error
1292
+ */
1293
+ declare class TimeoutError extends PromptMetricsError {
1294
+ constructor(message?: string);
1295
+ }
1296
+
1297
+ /**
1298
+ * @promptmetrics/sdk
1299
+ *
1300
+ * Official TypeScript/JavaScript SDK for PromptMetrics API
1301
+ *
1302
+ * @example
1303
+ * ```typescript
1304
+ * import { PromptMetrics } from '@promptmetrics/sdk';
1305
+ *
1306
+ * const client = new PromptMetrics({
1307
+ * apiKey: process.env.PROMPTMETRICS_API_KEY
1308
+ * });
1309
+ *
1310
+ * const result = await client.templates.run('my-template', {
1311
+ * workspace_id: 'workspace_123',
1312
+ * label: 'production',
1313
+ * variables: { name: 'John' }
1314
+ * });
1315
+ * ```
1316
+ */
1317
+
1318
+ declare const VERSION = "1.0.0";
1319
+
1320
+ export { type AddTraceScoreOptions, AuthenticationError, AuthorizationError, type CreateTraceOptions, type GetTemplateOptions, type LLMModel, type LLMProvider, type ListLogsOptions, type ListPromptsOptions, type ListPromptsResponse, type ListTracesOptions, type Message, NetworkError, NotFoundError, type PromptLog, PromptMetrics, type PromptMetricsConfig, PromptMetricsError, type PromptMetricsErrorResponse, type ProviderWithModels, RateLimitError, type RunTemplateOptions, type RunVersionOptions, ServerError, type Template, type TemplateMetadata, type TemplateOptions, type TemplateVersion, TimeoutError, type Trace, type TraceAnalytics, type TraceError, type TraceListResponse, type TraceScore, type TraceStatus, type TraceTreeNode, type TraceableOptions, type UpdateTraceMetadataOptions, type UpdateVersionOptions, VERSION, ValidationError };