@fallom/trace 0.1.6 → 0.1.11

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,3 +1,6 @@
1
+ import { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace-base';
2
+ import { ExportResult } from '@opentelemetry/core';
3
+
1
4
  /**
2
5
  * Fallom tracing module.
3
6
  *
@@ -183,6 +186,70 @@ declare function wrapAnthropic<T extends {
183
186
  declare function wrapGoogleAI<T extends {
184
187
  generateContent: (...args: any[]) => Promise<any>;
185
188
  }>(model: T): T;
189
+ /**
190
+ * Wrap the Vercel AI SDK to automatically trace all LLM calls.
191
+ * Works with generateText, streamText, generateObject, streamObject.
192
+ *
193
+ * @param ai - The ai module (import * as ai from "ai")
194
+ * @returns Object with wrapped generateText, streamText, generateObject, streamObject
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * import * as ai from "ai";
199
+ * import { createOpenAI } from "@ai-sdk/openai";
200
+ * import { trace } from "@fallom/trace";
201
+ *
202
+ * await trace.init({ apiKey: process.env.FALLOM_API_KEY });
203
+ * const { generateText, streamText } = trace.wrapAISDK(ai);
204
+ *
205
+ * const openrouter = createOpenAI({
206
+ * apiKey: process.env.OPENROUTER_API_KEY,
207
+ * baseURL: "https://openrouter.ai/api/v1",
208
+ * });
209
+ *
210
+ * trace.setSession("my-config", sessionId);
211
+ * const { text } = await generateText({
212
+ * model: openrouter("openai/gpt-4o-mini"),
213
+ * prompt: "Hello!",
214
+ * }); // Automatically traced!
215
+ * ```
216
+ */
217
+ declare function wrapAISDK<T extends {
218
+ generateText: (...args: any[]) => Promise<any>;
219
+ streamText: (...args: any[]) => any;
220
+ generateObject?: (...args: any[]) => Promise<any>;
221
+ streamObject?: (...args: any[]) => any;
222
+ }>(ai: T): {
223
+ generateText: T["generateText"];
224
+ streamText: T["streamText"];
225
+ generateObject: T["generateObject"];
226
+ streamObject: T["streamObject"];
227
+ };
228
+ /**
229
+ * Wrap a Mastra agent to automatically trace all generate() calls.
230
+ *
231
+ * @param agent - The Mastra Agent instance
232
+ * @returns The same agent with tracing enabled
233
+ *
234
+ * @example
235
+ * ```typescript
236
+ * import { trace } from "@fallom/trace";
237
+ * import { Agent } from "@mastra/core";
238
+ *
239
+ * await trace.init({ apiKey: "your-key" });
240
+ *
241
+ * const agent = new Agent({ ... });
242
+ * const tracedAgent = trace.wrapMastraAgent(agent);
243
+ *
244
+ * trace.setSession("my-app", "session-123", "user-456");
245
+ * const result = await tracedAgent.generate([{ role: "user", content: "Hello" }]);
246
+ * // ^ Automatically traced!
247
+ * ```
248
+ */
249
+ declare function wrapMastraAgent<T extends {
250
+ generate: (...args: any[]) => Promise<any>;
251
+ name?: string;
252
+ }>(agent: T): T;
186
253
 
187
254
  declare const trace_clearSession: typeof clearSession;
188
255
  declare const trace_getSession: typeof getSession;
@@ -190,11 +257,13 @@ declare const trace_runWithSession: typeof runWithSession;
190
257
  declare const trace_setSession: typeof setSession;
191
258
  declare const trace_shutdown: typeof shutdown;
192
259
  declare const trace_span: typeof span;
260
+ declare const trace_wrapAISDK: typeof wrapAISDK;
193
261
  declare const trace_wrapAnthropic: typeof wrapAnthropic;
194
262
  declare const trace_wrapGoogleAI: typeof wrapGoogleAI;
263
+ declare const trace_wrapMastraAgent: typeof wrapMastraAgent;
195
264
  declare const trace_wrapOpenAI: typeof wrapOpenAI;
196
265
  declare namespace trace {
197
- export { trace_clearSession as clearSession, trace_getSession as getSession, init$3 as init, trace_runWithSession as runWithSession, trace_setSession as setSession, trace_shutdown as shutdown, trace_span as span, trace_wrapAnthropic as wrapAnthropic, trace_wrapGoogleAI as wrapGoogleAI, trace_wrapOpenAI as wrapOpenAI };
266
+ export { trace_clearSession as clearSession, trace_getSession as getSession, init$3 as init, trace_runWithSession as runWithSession, trace_setSession as setSession, trace_shutdown as shutdown, trace_span as span, trace_wrapAISDK as wrapAISDK, trace_wrapAnthropic as wrapAnthropic, trace_wrapGoogleAI as wrapGoogleAI, trace_wrapMastraAgent as wrapMastraAgent, trace_wrapOpenAI as wrapOpenAI };
198
267
  }
199
268
 
200
269
  /**
@@ -368,16 +437,20 @@ declare namespace prompts {
368
437
  */
369
438
  interface InitOptions {
370
439
  apiKey?: string;
371
- baseUrl?: string;
440
+ tracesUrl?: string;
441
+ configsUrl?: string;
442
+ promptsUrl?: string;
372
443
  captureContent?: boolean;
373
444
  debug?: boolean;
374
445
  }
375
446
  /**
376
- * Initialize both trace and models at once.
447
+ * Initialize trace, models, and prompts at once.
377
448
  *
378
449
  * @param options - Configuration options
379
450
  * @param options.apiKey - Your Fallom API key. Defaults to FALLOM_API_KEY env var.
380
- * @param options.baseUrl - API base URL. Defaults to FALLOM_BASE_URL or https://spans.fallom.com
451
+ * @param options.tracesUrl - Traces API URL. Defaults to FALLOM_TRACES_URL or https://traces.fallom.com
452
+ * @param options.configsUrl - Configs API URL. Defaults to FALLOM_CONFIGS_URL or https://configs.fallom.com
453
+ * @param options.promptsUrl - Prompts API URL. Defaults to FALLOM_PROMPTS_URL or https://prompts.fallom.com
381
454
  * @param options.captureContent - Whether to capture prompt/completion content (default: true)
382
455
  *
383
456
  * @example
@@ -388,7 +461,11 @@ interface InitOptions {
388
461
  * fallom.init({ apiKey: "your-api-key" });
389
462
  *
390
463
  * // Local development
391
- * fallom.init({ baseUrl: "http://localhost:8001" });
464
+ * fallom.init({
465
+ * tracesUrl: "http://localhost:3002",
466
+ * configsUrl: "http://localhost:3003",
467
+ * promptsUrl: "http://localhost:3004"
468
+ * });
392
469
  *
393
470
  * // Privacy mode
394
471
  * fallom.init({ captureContent: false });
@@ -396,6 +473,112 @@ interface InitOptions {
396
473
  */
397
474
  declare function init(options?: InitOptions): Promise<void>;
398
475
 
476
+ /**
477
+ * Fallom Exporter for Mastra
478
+ *
479
+ * Custom OpenTelemetry exporter that sends traces from Mastra agents to Fallom.
480
+ * Reads session context from the shared trace module (set via trace.setSession()).
481
+ *
482
+ * Usage with Mastra:
483
+ * ```typescript
484
+ * import { trace, FallomExporter } from "@fallom/trace";
485
+ * import { Mastra } from "@mastra/core/mastra";
486
+ *
487
+ * // Initialize trace module
488
+ * await trace.init({ apiKey: process.env.FALLOM_API_KEY });
489
+ *
490
+ * // Create Mastra with Fallom exporter
491
+ * const mastra = new Mastra({
492
+ * agents: { myAgent },
493
+ * telemetry: {
494
+ * serviceName: "my-agent",
495
+ * enabled: true,
496
+ * export: {
497
+ * type: "custom",
498
+ * exporter: new FallomExporter(),
499
+ * },
500
+ * },
501
+ * });
502
+ *
503
+ * // In your request handler:
504
+ * trace.setSession("my-app", "session-123", "user-456");
505
+ * const result = await mastra.getAgent("myAgent").generate("Hello!");
506
+ * ```
507
+ */
508
+
509
+ interface FallomExporterOptions {
510
+ /** Fallom API key. Defaults to FALLOM_API_KEY env var. */
511
+ apiKey?: string;
512
+ /** Base URL for traces endpoint (defaults to https://traces.fallom.com) */
513
+ baseUrl?: string;
514
+ /** Enable debug logging */
515
+ debug?: boolean;
516
+ }
517
+ /**
518
+ * Set prompt tracking info.
519
+ * Call this after prompts.get() to track which prompt was used.
520
+ */
521
+ declare function setMastraPrompt(promptKey: string, version?: number): void;
522
+ /**
523
+ * Set A/B test prompt tracking info.
524
+ * Call this after prompts.getAB() to track which variant was used.
525
+ */
526
+ declare function setMastraPromptAB(abTestKey: string, variantIndex: number): void;
527
+ /**
528
+ * Clear prompt tracking info.
529
+ */
530
+ declare function clearMastraPrompt(): void;
531
+ /**
532
+ * OpenTelemetry SpanExporter that sends traces to Fallom.
533
+ *
534
+ * Reads session context from trace.setSession() automatically.
535
+ * Compatible with Mastra's custom exporter interface.
536
+ */
537
+ declare class FallomExporter implements SpanExporter {
538
+ private apiKey;
539
+ private baseUrl;
540
+ private debug;
541
+ private pendingExports;
542
+ constructor(options?: FallomExporterOptions);
543
+ private log;
544
+ /**
545
+ * Export spans to Fallom.
546
+ */
547
+ export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void): void;
548
+ /**
549
+ * Shutdown the exporter, waiting for pending exports.
550
+ */
551
+ shutdown(): Promise<void>;
552
+ /**
553
+ * Force flush pending exports.
554
+ */
555
+ forceFlush(): Promise<void>;
556
+ /**
557
+ * Send spans to Fallom's OTLP endpoint.
558
+ */
559
+ private sendSpans;
560
+ /**
561
+ * Convert OpenTelemetry spans to OTLP JSON format.
562
+ */
563
+ private spansToOtlpJson;
564
+ /**
565
+ * Convert a single span to OTLP format.
566
+ */
567
+ private spanToOtlp;
568
+ /**
569
+ * Convert attributes to OTLP format.
570
+ */
571
+ private attributesToOtlp;
572
+ /**
573
+ * Convert a value to OTLP AnyValue format.
574
+ */
575
+ private valueToOtlp;
576
+ /**
577
+ * Convert HrTime to nanoseconds string.
578
+ */
579
+ private hrTimeToNanos;
580
+ }
581
+
399
582
  /**
400
583
  * Fallom - Model A/B testing, prompt management, and tracing for LLM applications.
401
584
  *
@@ -440,4 +623,4 @@ declare const _default: {
440
623
  prompts: typeof prompts;
441
624
  };
442
625
 
443
- export { type InitOptions, type PromptResult, _default as default, init, models, prompts, trace };
626
+ export { FallomExporter, type FallomExporterOptions, type InitOptions, type PromptResult, clearMastraPrompt, _default as default, init, models, prompts, setMastraPrompt, setMastraPromptAB, trace };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace-base';
2
+ import { ExportResult } from '@opentelemetry/core';
3
+
1
4
  /**
2
5
  * Fallom tracing module.
3
6
  *
@@ -183,6 +186,70 @@ declare function wrapAnthropic<T extends {
183
186
  declare function wrapGoogleAI<T extends {
184
187
  generateContent: (...args: any[]) => Promise<any>;
185
188
  }>(model: T): T;
189
+ /**
190
+ * Wrap the Vercel AI SDK to automatically trace all LLM calls.
191
+ * Works with generateText, streamText, generateObject, streamObject.
192
+ *
193
+ * @param ai - The ai module (import * as ai from "ai")
194
+ * @returns Object with wrapped generateText, streamText, generateObject, streamObject
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * import * as ai from "ai";
199
+ * import { createOpenAI } from "@ai-sdk/openai";
200
+ * import { trace } from "@fallom/trace";
201
+ *
202
+ * await trace.init({ apiKey: process.env.FALLOM_API_KEY });
203
+ * const { generateText, streamText } = trace.wrapAISDK(ai);
204
+ *
205
+ * const openrouter = createOpenAI({
206
+ * apiKey: process.env.OPENROUTER_API_KEY,
207
+ * baseURL: "https://openrouter.ai/api/v1",
208
+ * });
209
+ *
210
+ * trace.setSession("my-config", sessionId);
211
+ * const { text } = await generateText({
212
+ * model: openrouter("openai/gpt-4o-mini"),
213
+ * prompt: "Hello!",
214
+ * }); // Automatically traced!
215
+ * ```
216
+ */
217
+ declare function wrapAISDK<T extends {
218
+ generateText: (...args: any[]) => Promise<any>;
219
+ streamText: (...args: any[]) => any;
220
+ generateObject?: (...args: any[]) => Promise<any>;
221
+ streamObject?: (...args: any[]) => any;
222
+ }>(ai: T): {
223
+ generateText: T["generateText"];
224
+ streamText: T["streamText"];
225
+ generateObject: T["generateObject"];
226
+ streamObject: T["streamObject"];
227
+ };
228
+ /**
229
+ * Wrap a Mastra agent to automatically trace all generate() calls.
230
+ *
231
+ * @param agent - The Mastra Agent instance
232
+ * @returns The same agent with tracing enabled
233
+ *
234
+ * @example
235
+ * ```typescript
236
+ * import { trace } from "@fallom/trace";
237
+ * import { Agent } from "@mastra/core";
238
+ *
239
+ * await trace.init({ apiKey: "your-key" });
240
+ *
241
+ * const agent = new Agent({ ... });
242
+ * const tracedAgent = trace.wrapMastraAgent(agent);
243
+ *
244
+ * trace.setSession("my-app", "session-123", "user-456");
245
+ * const result = await tracedAgent.generate([{ role: "user", content: "Hello" }]);
246
+ * // ^ Automatically traced!
247
+ * ```
248
+ */
249
+ declare function wrapMastraAgent<T extends {
250
+ generate: (...args: any[]) => Promise<any>;
251
+ name?: string;
252
+ }>(agent: T): T;
186
253
 
187
254
  declare const trace_clearSession: typeof clearSession;
188
255
  declare const trace_getSession: typeof getSession;
@@ -190,11 +257,13 @@ declare const trace_runWithSession: typeof runWithSession;
190
257
  declare const trace_setSession: typeof setSession;
191
258
  declare const trace_shutdown: typeof shutdown;
192
259
  declare const trace_span: typeof span;
260
+ declare const trace_wrapAISDK: typeof wrapAISDK;
193
261
  declare const trace_wrapAnthropic: typeof wrapAnthropic;
194
262
  declare const trace_wrapGoogleAI: typeof wrapGoogleAI;
263
+ declare const trace_wrapMastraAgent: typeof wrapMastraAgent;
195
264
  declare const trace_wrapOpenAI: typeof wrapOpenAI;
196
265
  declare namespace trace {
197
- export { trace_clearSession as clearSession, trace_getSession as getSession, init$3 as init, trace_runWithSession as runWithSession, trace_setSession as setSession, trace_shutdown as shutdown, trace_span as span, trace_wrapAnthropic as wrapAnthropic, trace_wrapGoogleAI as wrapGoogleAI, trace_wrapOpenAI as wrapOpenAI };
266
+ export { trace_clearSession as clearSession, trace_getSession as getSession, init$3 as init, trace_runWithSession as runWithSession, trace_setSession as setSession, trace_shutdown as shutdown, trace_span as span, trace_wrapAISDK as wrapAISDK, trace_wrapAnthropic as wrapAnthropic, trace_wrapGoogleAI as wrapGoogleAI, trace_wrapMastraAgent as wrapMastraAgent, trace_wrapOpenAI as wrapOpenAI };
198
267
  }
199
268
 
200
269
  /**
@@ -368,16 +437,20 @@ declare namespace prompts {
368
437
  */
369
438
  interface InitOptions {
370
439
  apiKey?: string;
371
- baseUrl?: string;
440
+ tracesUrl?: string;
441
+ configsUrl?: string;
442
+ promptsUrl?: string;
372
443
  captureContent?: boolean;
373
444
  debug?: boolean;
374
445
  }
375
446
  /**
376
- * Initialize both trace and models at once.
447
+ * Initialize trace, models, and prompts at once.
377
448
  *
378
449
  * @param options - Configuration options
379
450
  * @param options.apiKey - Your Fallom API key. Defaults to FALLOM_API_KEY env var.
380
- * @param options.baseUrl - API base URL. Defaults to FALLOM_BASE_URL or https://spans.fallom.com
451
+ * @param options.tracesUrl - Traces API URL. Defaults to FALLOM_TRACES_URL or https://traces.fallom.com
452
+ * @param options.configsUrl - Configs API URL. Defaults to FALLOM_CONFIGS_URL or https://configs.fallom.com
453
+ * @param options.promptsUrl - Prompts API URL. Defaults to FALLOM_PROMPTS_URL or https://prompts.fallom.com
381
454
  * @param options.captureContent - Whether to capture prompt/completion content (default: true)
382
455
  *
383
456
  * @example
@@ -388,7 +461,11 @@ interface InitOptions {
388
461
  * fallom.init({ apiKey: "your-api-key" });
389
462
  *
390
463
  * // Local development
391
- * fallom.init({ baseUrl: "http://localhost:8001" });
464
+ * fallom.init({
465
+ * tracesUrl: "http://localhost:3002",
466
+ * configsUrl: "http://localhost:3003",
467
+ * promptsUrl: "http://localhost:3004"
468
+ * });
392
469
  *
393
470
  * // Privacy mode
394
471
  * fallom.init({ captureContent: false });
@@ -396,6 +473,112 @@ interface InitOptions {
396
473
  */
397
474
  declare function init(options?: InitOptions): Promise<void>;
398
475
 
476
+ /**
477
+ * Fallom Exporter for Mastra
478
+ *
479
+ * Custom OpenTelemetry exporter that sends traces from Mastra agents to Fallom.
480
+ * Reads session context from the shared trace module (set via trace.setSession()).
481
+ *
482
+ * Usage with Mastra:
483
+ * ```typescript
484
+ * import { trace, FallomExporter } from "@fallom/trace";
485
+ * import { Mastra } from "@mastra/core/mastra";
486
+ *
487
+ * // Initialize trace module
488
+ * await trace.init({ apiKey: process.env.FALLOM_API_KEY });
489
+ *
490
+ * // Create Mastra with Fallom exporter
491
+ * const mastra = new Mastra({
492
+ * agents: { myAgent },
493
+ * telemetry: {
494
+ * serviceName: "my-agent",
495
+ * enabled: true,
496
+ * export: {
497
+ * type: "custom",
498
+ * exporter: new FallomExporter(),
499
+ * },
500
+ * },
501
+ * });
502
+ *
503
+ * // In your request handler:
504
+ * trace.setSession("my-app", "session-123", "user-456");
505
+ * const result = await mastra.getAgent("myAgent").generate("Hello!");
506
+ * ```
507
+ */
508
+
509
+ interface FallomExporterOptions {
510
+ /** Fallom API key. Defaults to FALLOM_API_KEY env var. */
511
+ apiKey?: string;
512
+ /** Base URL for traces endpoint (defaults to https://traces.fallom.com) */
513
+ baseUrl?: string;
514
+ /** Enable debug logging */
515
+ debug?: boolean;
516
+ }
517
+ /**
518
+ * Set prompt tracking info.
519
+ * Call this after prompts.get() to track which prompt was used.
520
+ */
521
+ declare function setMastraPrompt(promptKey: string, version?: number): void;
522
+ /**
523
+ * Set A/B test prompt tracking info.
524
+ * Call this after prompts.getAB() to track which variant was used.
525
+ */
526
+ declare function setMastraPromptAB(abTestKey: string, variantIndex: number): void;
527
+ /**
528
+ * Clear prompt tracking info.
529
+ */
530
+ declare function clearMastraPrompt(): void;
531
+ /**
532
+ * OpenTelemetry SpanExporter that sends traces to Fallom.
533
+ *
534
+ * Reads session context from trace.setSession() automatically.
535
+ * Compatible with Mastra's custom exporter interface.
536
+ */
537
+ declare class FallomExporter implements SpanExporter {
538
+ private apiKey;
539
+ private baseUrl;
540
+ private debug;
541
+ private pendingExports;
542
+ constructor(options?: FallomExporterOptions);
543
+ private log;
544
+ /**
545
+ * Export spans to Fallom.
546
+ */
547
+ export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void): void;
548
+ /**
549
+ * Shutdown the exporter, waiting for pending exports.
550
+ */
551
+ shutdown(): Promise<void>;
552
+ /**
553
+ * Force flush pending exports.
554
+ */
555
+ forceFlush(): Promise<void>;
556
+ /**
557
+ * Send spans to Fallom's OTLP endpoint.
558
+ */
559
+ private sendSpans;
560
+ /**
561
+ * Convert OpenTelemetry spans to OTLP JSON format.
562
+ */
563
+ private spansToOtlpJson;
564
+ /**
565
+ * Convert a single span to OTLP format.
566
+ */
567
+ private spanToOtlp;
568
+ /**
569
+ * Convert attributes to OTLP format.
570
+ */
571
+ private attributesToOtlp;
572
+ /**
573
+ * Convert a value to OTLP AnyValue format.
574
+ */
575
+ private valueToOtlp;
576
+ /**
577
+ * Convert HrTime to nanoseconds string.
578
+ */
579
+ private hrTimeToNanos;
580
+ }
581
+
399
582
  /**
400
583
  * Fallom - Model A/B testing, prompt management, and tracing for LLM applications.
401
584
  *
@@ -440,4 +623,4 @@ declare const _default: {
440
623
  prompts: typeof prompts;
441
624
  };
442
625
 
443
- export { type InitOptions, type PromptResult, _default as default, init, models, prompts, trace };
626
+ export { FallomExporter, type FallomExporterOptions, type InitOptions, type PromptResult, clearMastraPrompt, _default as default, init, models, prompts, setMastraPrompt, setMastraPromptAB, trace };