bitfab 0.23.1 → 0.23.3

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.cjs CHANGED
@@ -488,6 +488,7 @@ __export(index_exports, {
488
488
  BitfabLangGraphCallbackHandler: () => BitfabLangGraphCallbackHandler,
489
489
  BitfabOpenAIAgentHandler: () => BitfabOpenAIAgentHandler,
490
490
  BitfabOpenAITracingProcessor: () => BitfabOpenAITracingProcessor,
491
+ BitfabVercelAiHandler: () => BitfabVercelAiHandler,
491
492
  DEFAULT_SERVICE_URL: () => DEFAULT_SERVICE_URL,
492
493
  ReplayEnvironment: () => ReplayEnvironment,
493
494
  SUPPORTED_PROVIDERS: () => SUPPORTED_PROVIDERS,
@@ -500,7 +501,7 @@ __export(index_exports, {
500
501
  module.exports = __toCommonJS(index_exports);
501
502
 
502
503
  // src/version.generated.ts
503
- var __version__ = "0.23.1";
504
+ var __version__ = "0.23.3";
504
505
 
505
506
  // src/constants.ts
506
507
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -1441,6 +1442,16 @@ var BitfabClaudeAgentHandler = class {
1441
1442
  // src/client.ts
1442
1443
  init_asyncStorage();
1443
1444
 
1445
+ // src/optionalPeer.ts
1446
+ function importOptionalPeer(specifierParts) {
1447
+ const specifier = specifierParts.join("/");
1448
+ return import(
1449
+ /* webpackIgnore: true */
1450
+ /* @vite-ignore */
1451
+ specifier
1452
+ );
1453
+ }
1454
+
1444
1455
  // src/baml.ts
1445
1456
  var cachedBaml = null;
1446
1457
  async function loadBaml() {
@@ -1448,7 +1459,7 @@ async function loadBaml() {
1448
1459
  return cachedBaml;
1449
1460
  }
1450
1461
  try {
1451
- cachedBaml = await import("@boundaryml/baml");
1462
+ cachedBaml = await importOptionalPeer(["@boundaryml", "baml"]);
1452
1463
  return cachedBaml;
1453
1464
  } catch {
1454
1465
  throw new Error(
@@ -2291,7 +2302,10 @@ var BitfabOpenAIAgentHandler = class {
2291
2302
  this.getActiveSpanContext = config.getActiveSpanContext;
2292
2303
  }
2293
2304
  async wrapRun(agent, input, options) {
2294
- const { run } = await import("@openai/agents");
2305
+ const { run } = await importOptionalPeer([
2306
+ "@openai",
2307
+ "agents"
2308
+ ]);
2295
2309
  if (this.getActiveSpanContext?.() != null) {
2296
2310
  return run(
2297
2311
  agent,
@@ -2614,6 +2628,135 @@ var BitfabOpenAITracingProcessor = class {
2614
2628
  }
2615
2629
  };
2616
2630
 
2631
+ // src/vercelAiSdk.ts
2632
+ function modelLabel(model) {
2633
+ if (!model || typeof model !== "object") {
2634
+ return void 0;
2635
+ }
2636
+ const m = model;
2637
+ const provider = typeof m.provider === "string" ? m.provider : void 0;
2638
+ const modelId = typeof m.modelId === "string" ? m.modelId : void 0;
2639
+ if (provider == null && modelId == null) {
2640
+ return void 0;
2641
+ }
2642
+ return { provider, modelId };
2643
+ }
2644
+ function summarizeGenerate(result, model) {
2645
+ const content = Array.isArray(result.content) ? result.content : [];
2646
+ const text = typeof result.text === "string" ? result.text : content.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
2647
+ const toolCalls = content.filter((p) => p.type === "tool-call").map((p) => ({
2648
+ toolCallId: p.toolCallId,
2649
+ toolName: p.toolName,
2650
+ input: p.input ?? p.args
2651
+ }));
2652
+ const summary = {
2653
+ text,
2654
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
2655
+ usage: result.usage,
2656
+ finishReason: result.finishReason
2657
+ };
2658
+ if (model) {
2659
+ summary.model = model;
2660
+ }
2661
+ return summary;
2662
+ }
2663
+ function accumulateStream(onComplete, model) {
2664
+ let text = "";
2665
+ const toolCalls = [];
2666
+ let usage;
2667
+ let finishReason;
2668
+ let completed = false;
2669
+ const complete = () => {
2670
+ if (completed) {
2671
+ return;
2672
+ }
2673
+ completed = true;
2674
+ const summary = {
2675
+ text,
2676
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
2677
+ usage,
2678
+ finishReason
2679
+ };
2680
+ if (model) {
2681
+ summary.model = model;
2682
+ }
2683
+ onComplete(summary);
2684
+ };
2685
+ return new TransformStream({
2686
+ transform(part, controller) {
2687
+ try {
2688
+ if (part?.type === "text-delta") {
2689
+ text += part.delta ?? part.textDelta ?? "";
2690
+ } else if (part?.type === "tool-call") {
2691
+ toolCalls.push({
2692
+ toolCallId: part.toolCallId,
2693
+ toolName: part.toolName,
2694
+ input: part.input ?? part.args
2695
+ });
2696
+ } else if (part?.type === "finish") {
2697
+ usage = part.usage;
2698
+ finishReason = part.finishReason;
2699
+ complete();
2700
+ }
2701
+ } catch {
2702
+ }
2703
+ controller.enqueue(part);
2704
+ },
2705
+ flush() {
2706
+ complete();
2707
+ }
2708
+ });
2709
+ }
2710
+ var BitfabVercelAiHandler = class {
2711
+ constructor(config) {
2712
+ this.traceFunctionKey = config.traceFunctionKey;
2713
+ this.withSpanFn = config.withSpan;
2714
+ }
2715
+ /** The `wrapLanguageModel` middleware object for this trace function key. */
2716
+ get middleware() {
2717
+ const key = this.traceFunctionKey;
2718
+ const withSpan = this.withSpanFn;
2719
+ return {
2720
+ specificationVersion: "v3",
2721
+ wrapGenerate: async ({ doGenerate, params, model }) => {
2722
+ const label = modelLabel(model);
2723
+ const traced = withSpan(
2724
+ key,
2725
+ {
2726
+ type: "llm",
2727
+ finalize: (result) => summarizeGenerate(result ?? {}, label)
2728
+ },
2729
+ () => doGenerate()
2730
+ );
2731
+ return traced(params);
2732
+ },
2733
+ wrapStream: async ({ doStream, params, model }) => {
2734
+ const label = modelLabel(model);
2735
+ let resolveSummary = () => {
2736
+ };
2737
+ const summary = new Promise((resolve) => {
2738
+ resolveSummary = resolve;
2739
+ });
2740
+ const traced = withSpan(
2741
+ key,
2742
+ // The wrapped fn returns immediately with the live stream, so the span
2743
+ // output cannot be read from the return value. `finalize` instead
2744
+ // awaits the summary the accumulator resolves once the stream drains.
2745
+ { type: "llm", finalize: () => summary },
2746
+ async () => {
2747
+ const result = await doStream();
2748
+ const stream = result.stream.pipeThrough(
2749
+ accumulateStream(resolveSummary, label)
2750
+ );
2751
+ return { ...result, stream };
2752
+ }
2753
+ );
2754
+ return traced(params);
2755
+ }
2756
+ };
2757
+ }
2758
+ };
2759
+
2617
2760
  // src/client.ts
2618
2761
  var activeTraceStates = /* @__PURE__ */ new Map();
2619
2762
  var pendingSpanPromises = /* @__PURE__ */ new Map();
@@ -2713,7 +2856,10 @@ async function loadCollectorClass() {
2713
2856
  return cachedCollectorClass;
2714
2857
  }
2715
2858
  try {
2716
- const baml = await import("@boundaryml/baml");
2859
+ const baml = await importOptionalPeer([
2860
+ "@boundaryml",
2861
+ "baml"
2862
+ ]);
2717
2863
  cachedCollectorClass = baml.Collector;
2718
2864
  return cachedCollectorClass;
2719
2865
  } catch {
@@ -3129,6 +3275,36 @@ var Bitfab = class {
3129
3275
  }
3130
3276
  });
3131
3277
  }
3278
+ /**
3279
+ * Get a Vercel AI SDK language-model middleware for tracing.
3280
+ *
3281
+ * Pass it to the AI SDK's `wrapLanguageModel` and use the wrapped model with
3282
+ * `generateText` / `streamText` / `generateObject` / `streamObject`. Every
3283
+ * call through that model is captured as a keyed `llm` span carrying the call
3284
+ * parameters (the prompt) as input and a serializable summary
3285
+ * (`{ text, toolCalls, usage, finishReason }`) as output. Streaming is
3286
+ * captured without disturbing the caller's live stream.
3287
+ *
3288
+ * ```typescript
3289
+ * import { wrapLanguageModel, streamText } from "ai";
3290
+ * import { openai } from "@ai-sdk/openai";
3291
+ *
3292
+ * const model = wrapLanguageModel({
3293
+ * model: openai("gpt-4o"),
3294
+ * middleware: client.getVercelAiMiddleware("chat-turn"),
3295
+ * });
3296
+ * const result = streamText({ model, messages });
3297
+ * ```
3298
+ *
3299
+ * @param traceFunctionKey - Groups traces under this key in Bitfab
3300
+ * @returns A Vercel AI SDK middleware configured for this client
3301
+ */
3302
+ getVercelAiMiddleware(traceFunctionKey) {
3303
+ return new BitfabVercelAiHandler({
3304
+ traceFunctionKey,
3305
+ withSpan: this.withSpan.bind(this)
3306
+ }).middleware;
3307
+ }
3132
3308
  /**
3133
3309
  * Wrap a BAML client method to automatically capture prompt and LLM metadata.
3134
3310
  *
@@ -3801,6 +3977,7 @@ var finalizers = {
3801
3977
  BitfabLangGraphCallbackHandler,
3802
3978
  BitfabOpenAIAgentHandler,
3803
3979
  BitfabOpenAITracingProcessor,
3980
+ BitfabVercelAiHandler,
3804
3981
  DEFAULT_SERVICE_URL,
3805
3982
  ReplayEnvironment,
3806
3983
  SUPPORTED_PROVIDERS,