@fallom/trace 0.1.1 → 0.1.4

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
@@ -32,11 +32,12 @@ interface SessionContext {
32
32
  * await agent.run(message); // Automatically traced
33
33
  * ```
34
34
  */
35
- declare function init$2(options?: {
35
+ declare function init$3(options?: {
36
36
  apiKey?: string;
37
37
  baseUrl?: string;
38
38
  captureContent?: boolean;
39
- }): void;
39
+ debug?: boolean;
40
+ }): Promise<void>;
40
41
  /**
41
42
  * Set the current session context.
42
43
  *
@@ -111,6 +112,74 @@ declare function span(data: Record<string, unknown>, options?: {
111
112
  * Shutdown the tracing SDK gracefully.
112
113
  */
113
114
  declare function shutdown(): Promise<void>;
115
+ /**
116
+ * Wrap an OpenAI client to automatically trace all chat completions.
117
+ * Works with OpenAI, OpenRouter, Azure OpenAI, LiteLLM, and any OpenAI-compatible API.
118
+ *
119
+ * @param client - The OpenAI client instance
120
+ * @returns The same client with tracing enabled
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * import OpenAI from "openai";
125
+ * import { trace } from "@fallom/trace";
126
+ *
127
+ * const openai = trace.wrapOpenAI(new OpenAI());
128
+ *
129
+ * trace.setSession("my-config", sessionId);
130
+ * const response = await openai.chat.completions.create({...}); // Automatically traced!
131
+ * ```
132
+ */
133
+ declare function wrapOpenAI<T extends {
134
+ chat: {
135
+ completions: {
136
+ create: (...args: any[]) => Promise<any>;
137
+ };
138
+ };
139
+ }>(client: T): T;
140
+ /**
141
+ * Wrap an Anthropic client to automatically trace all message creations.
142
+ *
143
+ * @param client - The Anthropic client instance
144
+ * @returns The same client with tracing enabled
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * import Anthropic from "@anthropic-ai/sdk";
149
+ * import { trace } from "@fallom/trace";
150
+ *
151
+ * const anthropic = trace.wrapAnthropic(new Anthropic());
152
+ *
153
+ * trace.setSession("my-config", sessionId);
154
+ * const response = await anthropic.messages.create({...}); // Automatically traced!
155
+ * ```
156
+ */
157
+ declare function wrapAnthropic<T extends {
158
+ messages: {
159
+ create: (...args: any[]) => Promise<any>;
160
+ };
161
+ }>(client: T): T;
162
+ /**
163
+ * Wrap a Google Generative AI client to automatically trace all content generations.
164
+ *
165
+ * @param client - The GoogleGenerativeAI client instance
166
+ * @returns The same client with tracing enabled
167
+ *
168
+ * @example
169
+ * ```typescript
170
+ * import { GoogleGenerativeAI } from "@google/generative-ai";
171
+ * import { trace } from "@fallom/trace";
172
+ *
173
+ * const genAI = new GoogleGenerativeAI(apiKey);
174
+ * const model = trace.wrapGoogleAI(genAI.getGenerativeModel({ model: "gemini-pro" }));
175
+ *
176
+ * trace.setSession("my-config", sessionId);
177
+ * const response = await model.generateContent("Hello!"); // Automatically traced!
178
+ * ```
179
+ */
180
+ declare function wrapGoogleAI<T extends {
181
+ generateContent: (...args: any[]) => Promise<any>;
182
+ }>(model: T): T;
114
183
 
115
184
  declare const trace_clearSession: typeof clearSession;
116
185
  declare const trace_getSession: typeof getSession;
@@ -118,8 +187,11 @@ declare const trace_runWithSession: typeof runWithSession;
118
187
  declare const trace_setSession: typeof setSession;
119
188
  declare const trace_shutdown: typeof shutdown;
120
189
  declare const trace_span: typeof span;
190
+ declare const trace_wrapAnthropic: typeof wrapAnthropic;
191
+ declare const trace_wrapGoogleAI: typeof wrapGoogleAI;
192
+ declare const trace_wrapOpenAI: typeof wrapOpenAI;
121
193
  declare namespace trace {
122
- export { trace_clearSession as clearSession, trace_getSession as getSession, init$2 as init, trace_runWithSession as runWithSession, trace_setSession as setSession, trace_shutdown as shutdown, trace_span as span };
194
+ 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 };
123
195
  }
124
196
 
125
197
  /**
@@ -140,7 +212,7 @@ declare namespace trace {
140
212
  * This is optional - get() will auto-init if needed.
141
213
  * Non-blocking: starts background config fetch immediately.
142
214
  */
143
- declare function init$1(options?: {
215
+ declare function init$2(options?: {
144
216
  apiKey?: string;
145
217
  baseUrl?: string;
146
218
  }): void;
@@ -164,24 +236,138 @@ declare function init$1(options?: {
164
236
  * @returns Model string (e.g., "claude-opus", "gpt-4o")
165
237
  * @throws Error if config not found AND no fallback provided
166
238
  */
167
- declare function get(configKey: string, sessionId: string, options?: {
239
+ declare function get$1(configKey: string, sessionId: string, options?: {
168
240
  version?: number;
169
241
  fallback?: string;
170
242
  debug?: boolean;
171
243
  }): Promise<string>;
172
244
 
173
- declare const models_get: typeof get;
174
245
  declare namespace models {
175
- export { models_get as get, init$1 as init };
246
+ export { get$1 as get, init$2 as init };
247
+ }
248
+
249
+ /**
250
+ * Fallom prompts module.
251
+ *
252
+ * Provides prompt management and A/B testing.
253
+ * Zero latency on get() - uses local cache + template interpolation.
254
+ *
255
+ * Design principles:
256
+ * - Never block user's app if Fallom is down
257
+ * - Very short timeouts (1-2 seconds max)
258
+ * - Always return usable prompt (or throw if not found)
259
+ * - Background sync keeps prompts fresh
260
+ * - Auto-tag next trace with prompt context
261
+ */
262
+ interface PromptContext {
263
+ promptKey: string;
264
+ promptVersion: number;
265
+ abTestKey?: string;
266
+ variantIndex?: number;
267
+ }
268
+ /**
269
+ * Result from prompts.get() or prompts.getAB()
270
+ */
271
+ interface PromptResult {
272
+ key: string;
273
+ version: number;
274
+ system: string;
275
+ user: string;
276
+ abTestKey?: string;
277
+ variantIndex?: number;
278
+ }
279
+ /**
280
+ * Initialize Fallom prompts.
281
+ * This is called automatically by fallom.init().
282
+ */
283
+ declare function init$1(options?: {
284
+ apiKey?: string;
285
+ baseUrl?: string;
286
+ }): void;
287
+ /**
288
+ * Get and clear prompt context (one-shot).
289
+ */
290
+ declare function getPromptContext(): PromptContext | null;
291
+ /**
292
+ * Get a prompt (non-A/B).
293
+ *
294
+ * Zero latency - uses local cache + string interpolation.
295
+ * Also sets prompt context for next trace auto-tagging.
296
+ *
297
+ * @param promptKey - Your prompt key (e.g., "onboarding")
298
+ * @param options - Optional settings
299
+ * @param options.variables - Template variables (e.g., { userName: "John" })
300
+ * @param options.version - Pin to specific version. undefined = current
301
+ * @param options.debug - Enable debug logging
302
+ *
303
+ * @example
304
+ * ```typescript
305
+ * const prompt = await prompts.get("onboarding", {
306
+ * variables: { userName: "John" }
307
+ * });
308
+ *
309
+ * // Use with OpenAI
310
+ * const response = await openai.chat.completions.create({
311
+ * model,
312
+ * messages: [
313
+ * { role: "system", content: prompt.system },
314
+ * { role: "user", content: prompt.user }
315
+ * ]
316
+ * });
317
+ * ```
318
+ */
319
+ declare function get(promptKey: string, options?: {
320
+ variables?: Record<string, unknown>;
321
+ version?: number;
322
+ debug?: boolean;
323
+ }): Promise<PromptResult>;
324
+ /**
325
+ * Get a prompt from an A/B test.
326
+ *
327
+ * Uses sessionId hash for deterministic, sticky assignment.
328
+ * Same session always gets same variant.
329
+ *
330
+ * Also sets prompt context for next trace auto-tagging.
331
+ *
332
+ * @param abTestKey - Your A/B test key (e.g., "onboarding-experiment")
333
+ * @param sessionId - Your session/conversation ID (for sticky assignment)
334
+ * @param options - Optional settings
335
+ * @param options.variables - Template variables
336
+ * @param options.debug - Enable debug logging
337
+ *
338
+ * @example
339
+ * ```typescript
340
+ * const prompt = await prompts.getAB("onboarding-test", sessionId, {
341
+ * variables: { userName: "John" }
342
+ * });
343
+ * ```
344
+ */
345
+ declare function getAB(abTestKey: string, sessionId: string, options?: {
346
+ variables?: Record<string, unknown>;
347
+ debug?: boolean;
348
+ }): Promise<PromptResult>;
349
+ /**
350
+ * Manually clear prompt context.
351
+ */
352
+ declare function clearPromptContext(): void;
353
+
354
+ type prompts_PromptResult = PromptResult;
355
+ declare const prompts_clearPromptContext: typeof clearPromptContext;
356
+ declare const prompts_get: typeof get;
357
+ declare const prompts_getAB: typeof getAB;
358
+ declare const prompts_getPromptContext: typeof getPromptContext;
359
+ declare namespace prompts {
360
+ export { type prompts_PromptResult as PromptResult, prompts_clearPromptContext as clearPromptContext, prompts_get as get, prompts_getAB as getAB, prompts_getPromptContext as getPromptContext, init$1 as init };
176
361
  }
177
362
 
178
363
  /**
179
- * Combined initialization for both trace and models.
364
+ * Combined initialization for trace, models, and prompts.
180
365
  */
181
366
  interface InitOptions {
182
367
  apiKey?: string;
183
368
  baseUrl?: string;
184
369
  captureContent?: boolean;
370
+ debug?: boolean;
185
371
  }
186
372
  /**
187
373
  * Initialize both trace and models at once.
@@ -205,10 +391,10 @@ interface InitOptions {
205
391
  * fallom.init({ captureContent: false });
206
392
  * ```
207
393
  */
208
- declare function init(options?: InitOptions): void;
394
+ declare function init(options?: InitOptions): Promise<void>;
209
395
 
210
396
  /**
211
- * Fallom - Model A/B testing and tracing for LLM applications.
397
+ * Fallom - Model A/B testing, prompt management, and tracing for LLM applications.
212
398
  *
213
399
  * @example
214
400
  * ```typescript
@@ -225,10 +411,18 @@ declare function init(options?: InitOptions): void;
225
411
  * fallback: "gpt-4o-mini"
226
412
  * });
227
413
  *
414
+ * // Get managed prompts (with optional A/B testing)
415
+ * const prompt = await fallom.prompts.get("onboarding", {
416
+ * variables: { userName: "John" }
417
+ * });
418
+ *
228
419
  * // Use with OpenAI
229
420
  * const response = await openai.chat.completions.create({
230
421
  * model,
231
- * messages: [...]
422
+ * messages: [
423
+ * { role: "system", content: prompt.system },
424
+ * { role: "user", content: prompt.user }
425
+ * ]
232
426
  * });
233
427
  *
234
428
  * // Record custom metrics
@@ -240,6 +434,7 @@ declare const _default: {
240
434
  init: typeof init;
241
435
  trace: typeof trace;
242
436
  models: typeof models;
437
+ prompts: typeof prompts;
243
438
  };
244
439
 
245
- export { type InitOptions, _default as default, init, models, trace };
440
+ export { type InitOptions, type PromptResult, _default as default, init, models, prompts, trace };
package/dist/index.d.ts CHANGED
@@ -32,11 +32,12 @@ interface SessionContext {
32
32
  * await agent.run(message); // Automatically traced
33
33
  * ```
34
34
  */
35
- declare function init$2(options?: {
35
+ declare function init$3(options?: {
36
36
  apiKey?: string;
37
37
  baseUrl?: string;
38
38
  captureContent?: boolean;
39
- }): void;
39
+ debug?: boolean;
40
+ }): Promise<void>;
40
41
  /**
41
42
  * Set the current session context.
42
43
  *
@@ -111,6 +112,74 @@ declare function span(data: Record<string, unknown>, options?: {
111
112
  * Shutdown the tracing SDK gracefully.
112
113
  */
113
114
  declare function shutdown(): Promise<void>;
115
+ /**
116
+ * Wrap an OpenAI client to automatically trace all chat completions.
117
+ * Works with OpenAI, OpenRouter, Azure OpenAI, LiteLLM, and any OpenAI-compatible API.
118
+ *
119
+ * @param client - The OpenAI client instance
120
+ * @returns The same client with tracing enabled
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * import OpenAI from "openai";
125
+ * import { trace } from "@fallom/trace";
126
+ *
127
+ * const openai = trace.wrapOpenAI(new OpenAI());
128
+ *
129
+ * trace.setSession("my-config", sessionId);
130
+ * const response = await openai.chat.completions.create({...}); // Automatically traced!
131
+ * ```
132
+ */
133
+ declare function wrapOpenAI<T extends {
134
+ chat: {
135
+ completions: {
136
+ create: (...args: any[]) => Promise<any>;
137
+ };
138
+ };
139
+ }>(client: T): T;
140
+ /**
141
+ * Wrap an Anthropic client to automatically trace all message creations.
142
+ *
143
+ * @param client - The Anthropic client instance
144
+ * @returns The same client with tracing enabled
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * import Anthropic from "@anthropic-ai/sdk";
149
+ * import { trace } from "@fallom/trace";
150
+ *
151
+ * const anthropic = trace.wrapAnthropic(new Anthropic());
152
+ *
153
+ * trace.setSession("my-config", sessionId);
154
+ * const response = await anthropic.messages.create({...}); // Automatically traced!
155
+ * ```
156
+ */
157
+ declare function wrapAnthropic<T extends {
158
+ messages: {
159
+ create: (...args: any[]) => Promise<any>;
160
+ };
161
+ }>(client: T): T;
162
+ /**
163
+ * Wrap a Google Generative AI client to automatically trace all content generations.
164
+ *
165
+ * @param client - The GoogleGenerativeAI client instance
166
+ * @returns The same client with tracing enabled
167
+ *
168
+ * @example
169
+ * ```typescript
170
+ * import { GoogleGenerativeAI } from "@google/generative-ai";
171
+ * import { trace } from "@fallom/trace";
172
+ *
173
+ * const genAI = new GoogleGenerativeAI(apiKey);
174
+ * const model = trace.wrapGoogleAI(genAI.getGenerativeModel({ model: "gemini-pro" }));
175
+ *
176
+ * trace.setSession("my-config", sessionId);
177
+ * const response = await model.generateContent("Hello!"); // Automatically traced!
178
+ * ```
179
+ */
180
+ declare function wrapGoogleAI<T extends {
181
+ generateContent: (...args: any[]) => Promise<any>;
182
+ }>(model: T): T;
114
183
 
115
184
  declare const trace_clearSession: typeof clearSession;
116
185
  declare const trace_getSession: typeof getSession;
@@ -118,8 +187,11 @@ declare const trace_runWithSession: typeof runWithSession;
118
187
  declare const trace_setSession: typeof setSession;
119
188
  declare const trace_shutdown: typeof shutdown;
120
189
  declare const trace_span: typeof span;
190
+ declare const trace_wrapAnthropic: typeof wrapAnthropic;
191
+ declare const trace_wrapGoogleAI: typeof wrapGoogleAI;
192
+ declare const trace_wrapOpenAI: typeof wrapOpenAI;
121
193
  declare namespace trace {
122
- export { trace_clearSession as clearSession, trace_getSession as getSession, init$2 as init, trace_runWithSession as runWithSession, trace_setSession as setSession, trace_shutdown as shutdown, trace_span as span };
194
+ 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 };
123
195
  }
124
196
 
125
197
  /**
@@ -140,7 +212,7 @@ declare namespace trace {
140
212
  * This is optional - get() will auto-init if needed.
141
213
  * Non-blocking: starts background config fetch immediately.
142
214
  */
143
- declare function init$1(options?: {
215
+ declare function init$2(options?: {
144
216
  apiKey?: string;
145
217
  baseUrl?: string;
146
218
  }): void;
@@ -164,24 +236,138 @@ declare function init$1(options?: {
164
236
  * @returns Model string (e.g., "claude-opus", "gpt-4o")
165
237
  * @throws Error if config not found AND no fallback provided
166
238
  */
167
- declare function get(configKey: string, sessionId: string, options?: {
239
+ declare function get$1(configKey: string, sessionId: string, options?: {
168
240
  version?: number;
169
241
  fallback?: string;
170
242
  debug?: boolean;
171
243
  }): Promise<string>;
172
244
 
173
- declare const models_get: typeof get;
174
245
  declare namespace models {
175
- export { models_get as get, init$1 as init };
246
+ export { get$1 as get, init$2 as init };
247
+ }
248
+
249
+ /**
250
+ * Fallom prompts module.
251
+ *
252
+ * Provides prompt management and A/B testing.
253
+ * Zero latency on get() - uses local cache + template interpolation.
254
+ *
255
+ * Design principles:
256
+ * - Never block user's app if Fallom is down
257
+ * - Very short timeouts (1-2 seconds max)
258
+ * - Always return usable prompt (or throw if not found)
259
+ * - Background sync keeps prompts fresh
260
+ * - Auto-tag next trace with prompt context
261
+ */
262
+ interface PromptContext {
263
+ promptKey: string;
264
+ promptVersion: number;
265
+ abTestKey?: string;
266
+ variantIndex?: number;
267
+ }
268
+ /**
269
+ * Result from prompts.get() or prompts.getAB()
270
+ */
271
+ interface PromptResult {
272
+ key: string;
273
+ version: number;
274
+ system: string;
275
+ user: string;
276
+ abTestKey?: string;
277
+ variantIndex?: number;
278
+ }
279
+ /**
280
+ * Initialize Fallom prompts.
281
+ * This is called automatically by fallom.init().
282
+ */
283
+ declare function init$1(options?: {
284
+ apiKey?: string;
285
+ baseUrl?: string;
286
+ }): void;
287
+ /**
288
+ * Get and clear prompt context (one-shot).
289
+ */
290
+ declare function getPromptContext(): PromptContext | null;
291
+ /**
292
+ * Get a prompt (non-A/B).
293
+ *
294
+ * Zero latency - uses local cache + string interpolation.
295
+ * Also sets prompt context for next trace auto-tagging.
296
+ *
297
+ * @param promptKey - Your prompt key (e.g., "onboarding")
298
+ * @param options - Optional settings
299
+ * @param options.variables - Template variables (e.g., { userName: "John" })
300
+ * @param options.version - Pin to specific version. undefined = current
301
+ * @param options.debug - Enable debug logging
302
+ *
303
+ * @example
304
+ * ```typescript
305
+ * const prompt = await prompts.get("onboarding", {
306
+ * variables: { userName: "John" }
307
+ * });
308
+ *
309
+ * // Use with OpenAI
310
+ * const response = await openai.chat.completions.create({
311
+ * model,
312
+ * messages: [
313
+ * { role: "system", content: prompt.system },
314
+ * { role: "user", content: prompt.user }
315
+ * ]
316
+ * });
317
+ * ```
318
+ */
319
+ declare function get(promptKey: string, options?: {
320
+ variables?: Record<string, unknown>;
321
+ version?: number;
322
+ debug?: boolean;
323
+ }): Promise<PromptResult>;
324
+ /**
325
+ * Get a prompt from an A/B test.
326
+ *
327
+ * Uses sessionId hash for deterministic, sticky assignment.
328
+ * Same session always gets same variant.
329
+ *
330
+ * Also sets prompt context for next trace auto-tagging.
331
+ *
332
+ * @param abTestKey - Your A/B test key (e.g., "onboarding-experiment")
333
+ * @param sessionId - Your session/conversation ID (for sticky assignment)
334
+ * @param options - Optional settings
335
+ * @param options.variables - Template variables
336
+ * @param options.debug - Enable debug logging
337
+ *
338
+ * @example
339
+ * ```typescript
340
+ * const prompt = await prompts.getAB("onboarding-test", sessionId, {
341
+ * variables: { userName: "John" }
342
+ * });
343
+ * ```
344
+ */
345
+ declare function getAB(abTestKey: string, sessionId: string, options?: {
346
+ variables?: Record<string, unknown>;
347
+ debug?: boolean;
348
+ }): Promise<PromptResult>;
349
+ /**
350
+ * Manually clear prompt context.
351
+ */
352
+ declare function clearPromptContext(): void;
353
+
354
+ type prompts_PromptResult = PromptResult;
355
+ declare const prompts_clearPromptContext: typeof clearPromptContext;
356
+ declare const prompts_get: typeof get;
357
+ declare const prompts_getAB: typeof getAB;
358
+ declare const prompts_getPromptContext: typeof getPromptContext;
359
+ declare namespace prompts {
360
+ export { type prompts_PromptResult as PromptResult, prompts_clearPromptContext as clearPromptContext, prompts_get as get, prompts_getAB as getAB, prompts_getPromptContext as getPromptContext, init$1 as init };
176
361
  }
177
362
 
178
363
  /**
179
- * Combined initialization for both trace and models.
364
+ * Combined initialization for trace, models, and prompts.
180
365
  */
181
366
  interface InitOptions {
182
367
  apiKey?: string;
183
368
  baseUrl?: string;
184
369
  captureContent?: boolean;
370
+ debug?: boolean;
185
371
  }
186
372
  /**
187
373
  * Initialize both trace and models at once.
@@ -205,10 +391,10 @@ interface InitOptions {
205
391
  * fallom.init({ captureContent: false });
206
392
  * ```
207
393
  */
208
- declare function init(options?: InitOptions): void;
394
+ declare function init(options?: InitOptions): Promise<void>;
209
395
 
210
396
  /**
211
- * Fallom - Model A/B testing and tracing for LLM applications.
397
+ * Fallom - Model A/B testing, prompt management, and tracing for LLM applications.
212
398
  *
213
399
  * @example
214
400
  * ```typescript
@@ -225,10 +411,18 @@ declare function init(options?: InitOptions): void;
225
411
  * fallback: "gpt-4o-mini"
226
412
  * });
227
413
  *
414
+ * // Get managed prompts (with optional A/B testing)
415
+ * const prompt = await fallom.prompts.get("onboarding", {
416
+ * variables: { userName: "John" }
417
+ * });
418
+ *
228
419
  * // Use with OpenAI
229
420
  * const response = await openai.chat.completions.create({
230
421
  * model,
231
- * messages: [...]
422
+ * messages: [
423
+ * { role: "system", content: prompt.system },
424
+ * { role: "user", content: prompt.user }
425
+ * ]
232
426
  * });
233
427
  *
234
428
  * // Record custom metrics
@@ -240,6 +434,7 @@ declare const _default: {
240
434
  init: typeof init;
241
435
  trace: typeof trace;
242
436
  models: typeof models;
437
+ prompts: typeof prompts;
243
438
  };
244
439
 
245
- export { type InitOptions, _default as default, init, models, trace };
440
+ export { type InitOptions, type PromptResult, _default as default, init, models, prompts, trace };