@bitfab/sdk 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
@@ -479,6 +479,7 @@ __export(index_exports, {
479
479
  BitfabLangGraphCallbackHandler: () => BitfabLangGraphCallbackHandler,
480
480
  BitfabOpenAIAgentHandler: () => BitfabOpenAIAgentHandler,
481
481
  BitfabOpenAITracingProcessor: () => BitfabOpenAITracingProcessor,
482
+ BitfabVercelAiHandler: () => BitfabVercelAiHandler,
482
483
  DEFAULT_SERVICE_URL: () => DEFAULT_SERVICE_URL,
483
484
  ReplayEnvironment: () => ReplayEnvironment,
484
485
  SUPPORTED_PROVIDERS: () => SUPPORTED_PROVIDERS,
@@ -491,7 +492,7 @@ __export(index_exports, {
491
492
  module.exports = __toCommonJS(index_exports);
492
493
 
493
494
  // src/version.generated.ts
494
- var __version__ = "0.23.1";
495
+ var __version__ = "0.23.3";
495
496
 
496
497
  // src/constants.ts
497
498
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -1432,6 +1433,16 @@ var BitfabClaudeAgentHandler = class {
1432
1433
  // src/client.ts
1433
1434
  init_asyncStorage();
1434
1435
 
1436
+ // src/optionalPeer.ts
1437
+ function importOptionalPeer(specifierParts) {
1438
+ const specifier = specifierParts.join("/");
1439
+ return import(
1440
+ /* webpackIgnore: true */
1441
+ /* @vite-ignore */
1442
+ specifier
1443
+ );
1444
+ }
1445
+
1435
1446
  // src/baml.ts
1436
1447
  var cachedBaml = null;
1437
1448
  async function loadBaml() {
@@ -1439,7 +1450,7 @@ async function loadBaml() {
1439
1450
  return cachedBaml;
1440
1451
  }
1441
1452
  try {
1442
- cachedBaml = await import("@boundaryml/baml");
1453
+ cachedBaml = await importOptionalPeer(["@boundaryml", "baml"]);
1443
1454
  return cachedBaml;
1444
1455
  } catch {
1445
1456
  throw new Error(
@@ -2282,7 +2293,10 @@ var BitfabOpenAIAgentHandler = class {
2282
2293
  this.getActiveSpanContext = config.getActiveSpanContext;
2283
2294
  }
2284
2295
  async wrapRun(agent, input, options) {
2285
- const { run } = await import("@openai/agents");
2296
+ const { run } = await importOptionalPeer([
2297
+ "@openai",
2298
+ "agents"
2299
+ ]);
2286
2300
  if (this.getActiveSpanContext?.() != null) {
2287
2301
  return run(
2288
2302
  agent,
@@ -2605,6 +2619,135 @@ var BitfabOpenAITracingProcessor = class {
2605
2619
  }
2606
2620
  };
2607
2621
 
2622
+ // src/vercelAiSdk.ts
2623
+ function modelLabel(model) {
2624
+ if (!model || typeof model !== "object") {
2625
+ return void 0;
2626
+ }
2627
+ const m = model;
2628
+ const provider = typeof m.provider === "string" ? m.provider : void 0;
2629
+ const modelId = typeof m.modelId === "string" ? m.modelId : void 0;
2630
+ if (provider == null && modelId == null) {
2631
+ return void 0;
2632
+ }
2633
+ return { provider, modelId };
2634
+ }
2635
+ function summarizeGenerate(result, model) {
2636
+ const content = Array.isArray(result.content) ? result.content : [];
2637
+ const text = typeof result.text === "string" ? result.text : content.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
2638
+ const toolCalls = content.filter((p) => p.type === "tool-call").map((p) => ({
2639
+ toolCallId: p.toolCallId,
2640
+ toolName: p.toolName,
2641
+ input: p.input ?? p.args
2642
+ }));
2643
+ const summary = {
2644
+ text,
2645
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
2646
+ usage: result.usage,
2647
+ finishReason: result.finishReason
2648
+ };
2649
+ if (model) {
2650
+ summary.model = model;
2651
+ }
2652
+ return summary;
2653
+ }
2654
+ function accumulateStream(onComplete, model) {
2655
+ let text = "";
2656
+ const toolCalls = [];
2657
+ let usage;
2658
+ let finishReason;
2659
+ let completed = false;
2660
+ const complete = () => {
2661
+ if (completed) {
2662
+ return;
2663
+ }
2664
+ completed = true;
2665
+ const summary = {
2666
+ text,
2667
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
2668
+ usage,
2669
+ finishReason
2670
+ };
2671
+ if (model) {
2672
+ summary.model = model;
2673
+ }
2674
+ onComplete(summary);
2675
+ };
2676
+ return new TransformStream({
2677
+ transform(part, controller) {
2678
+ try {
2679
+ if (part?.type === "text-delta") {
2680
+ text += part.delta ?? part.textDelta ?? "";
2681
+ } else if (part?.type === "tool-call") {
2682
+ toolCalls.push({
2683
+ toolCallId: part.toolCallId,
2684
+ toolName: part.toolName,
2685
+ input: part.input ?? part.args
2686
+ });
2687
+ } else if (part?.type === "finish") {
2688
+ usage = part.usage;
2689
+ finishReason = part.finishReason;
2690
+ complete();
2691
+ }
2692
+ } catch {
2693
+ }
2694
+ controller.enqueue(part);
2695
+ },
2696
+ flush() {
2697
+ complete();
2698
+ }
2699
+ });
2700
+ }
2701
+ var BitfabVercelAiHandler = class {
2702
+ constructor(config) {
2703
+ this.traceFunctionKey = config.traceFunctionKey;
2704
+ this.withSpanFn = config.withSpan;
2705
+ }
2706
+ /** The `wrapLanguageModel` middleware object for this trace function key. */
2707
+ get middleware() {
2708
+ const key = this.traceFunctionKey;
2709
+ const withSpan = this.withSpanFn;
2710
+ return {
2711
+ specificationVersion: "v3",
2712
+ wrapGenerate: async ({ doGenerate, params, model }) => {
2713
+ const label = modelLabel(model);
2714
+ const traced = withSpan(
2715
+ key,
2716
+ {
2717
+ type: "llm",
2718
+ finalize: (result) => summarizeGenerate(result ?? {}, label)
2719
+ },
2720
+ () => doGenerate()
2721
+ );
2722
+ return traced(params);
2723
+ },
2724
+ wrapStream: async ({ doStream, params, model }) => {
2725
+ const label = modelLabel(model);
2726
+ let resolveSummary = () => {
2727
+ };
2728
+ const summary = new Promise((resolve) => {
2729
+ resolveSummary = resolve;
2730
+ });
2731
+ const traced = withSpan(
2732
+ key,
2733
+ // The wrapped fn returns immediately with the live stream, so the span
2734
+ // output cannot be read from the return value. `finalize` instead
2735
+ // awaits the summary the accumulator resolves once the stream drains.
2736
+ { type: "llm", finalize: () => summary },
2737
+ async () => {
2738
+ const result = await doStream();
2739
+ const stream = result.stream.pipeThrough(
2740
+ accumulateStream(resolveSummary, label)
2741
+ );
2742
+ return { ...result, stream };
2743
+ }
2744
+ );
2745
+ return traced(params);
2746
+ }
2747
+ };
2748
+ }
2749
+ };
2750
+
2608
2751
  // src/client.ts
2609
2752
  var activeTraceStates = /* @__PURE__ */ new Map();
2610
2753
  var pendingSpanPromises = /* @__PURE__ */ new Map();
@@ -2704,7 +2847,10 @@ async function loadCollectorClass() {
2704
2847
  return cachedCollectorClass;
2705
2848
  }
2706
2849
  try {
2707
- const baml = await import("@boundaryml/baml");
2850
+ const baml = await importOptionalPeer([
2851
+ "@boundaryml",
2852
+ "baml"
2853
+ ]);
2708
2854
  cachedCollectorClass = baml.Collector;
2709
2855
  return cachedCollectorClass;
2710
2856
  } catch {
@@ -3120,6 +3266,36 @@ var Bitfab = class {
3120
3266
  }
3121
3267
  });
3122
3268
  }
3269
+ /**
3270
+ * Get a Vercel AI SDK language-model middleware for tracing.
3271
+ *
3272
+ * Pass it to the AI SDK's `wrapLanguageModel` and use the wrapped model with
3273
+ * `generateText` / `streamText` / `generateObject` / `streamObject`. Every
3274
+ * call through that model is captured as a keyed `llm` span carrying the call
3275
+ * parameters (the prompt) as input and a serializable summary
3276
+ * (`{ text, toolCalls, usage, finishReason }`) as output. Streaming is
3277
+ * captured without disturbing the caller's live stream.
3278
+ *
3279
+ * ```typescript
3280
+ * import { wrapLanguageModel, streamText } from "ai";
3281
+ * import { openai } from "@ai-sdk/openai";
3282
+ *
3283
+ * const model = wrapLanguageModel({
3284
+ * model: openai("gpt-4o"),
3285
+ * middleware: client.getVercelAiMiddleware("chat-turn"),
3286
+ * });
3287
+ * const result = streamText({ model, messages });
3288
+ * ```
3289
+ *
3290
+ * @param traceFunctionKey - Groups traces under this key in Bitfab
3291
+ * @returns A Vercel AI SDK middleware configured for this client
3292
+ */
3293
+ getVercelAiMiddleware(traceFunctionKey) {
3294
+ return new BitfabVercelAiHandler({
3295
+ traceFunctionKey,
3296
+ withSpan: this.withSpan.bind(this)
3297
+ }).middleware;
3298
+ }
3123
3299
  /**
3124
3300
  * Wrap a BAML client method to automatically capture prompt and LLM metadata.
3125
3301
  *
@@ -3792,6 +3968,7 @@ var finalizers = {
3792
3968
  BitfabLangGraphCallbackHandler,
3793
3969
  BitfabOpenAIAgentHandler,
3794
3970
  BitfabOpenAITracingProcessor,
3971
+ BitfabVercelAiHandler,
3795
3972
  DEFAULT_SERVICE_URL,
3796
3973
  ReplayEnvironment,
3797
3974
  SUPPORTED_PROVIDERS,