@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.
@@ -10,7 +10,7 @@ import {
10
10
  } from "./chunk-EQI6ZJC3.js";
11
11
 
12
12
  // src/version.generated.ts
13
- var __version__ = "0.23.1";
13
+ var __version__ = "0.23.3";
14
14
 
15
15
  // src/constants.ts
16
16
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -946,6 +946,16 @@ var BitfabClaudeAgentHandler = class {
946
946
  }
947
947
  };
948
948
 
949
+ // src/optionalPeer.ts
950
+ function importOptionalPeer(specifierParts) {
951
+ const specifier = specifierParts.join("/");
952
+ return import(
953
+ /* webpackIgnore: true */
954
+ /* @vite-ignore */
955
+ specifier
956
+ );
957
+ }
958
+
949
959
  // src/baml.ts
950
960
  var cachedBaml = null;
951
961
  async function loadBaml() {
@@ -953,7 +963,7 @@ async function loadBaml() {
953
963
  return cachedBaml;
954
964
  }
955
965
  try {
956
- cachedBaml = await import("@boundaryml/baml");
966
+ cachedBaml = await importOptionalPeer(["@boundaryml", "baml"]);
957
967
  return cachedBaml;
958
968
  } catch {
959
969
  throw new Error(
@@ -1794,7 +1804,10 @@ var BitfabOpenAIAgentHandler = class {
1794
1804
  this.getActiveSpanContext = config.getActiveSpanContext;
1795
1805
  }
1796
1806
  async wrapRun(agent, input, options) {
1797
- const { run } = await import("@openai/agents");
1807
+ const { run } = await importOptionalPeer([
1808
+ "@openai",
1809
+ "agents"
1810
+ ]);
1798
1811
  if (this.getActiveSpanContext?.() != null) {
1799
1812
  return run(
1800
1813
  agent,
@@ -2110,6 +2123,135 @@ var BitfabOpenAITracingProcessor = class {
2110
2123
  }
2111
2124
  };
2112
2125
 
2126
+ // src/vercelAiSdk.ts
2127
+ function modelLabel(model) {
2128
+ if (!model || typeof model !== "object") {
2129
+ return void 0;
2130
+ }
2131
+ const m = model;
2132
+ const provider = typeof m.provider === "string" ? m.provider : void 0;
2133
+ const modelId = typeof m.modelId === "string" ? m.modelId : void 0;
2134
+ if (provider == null && modelId == null) {
2135
+ return void 0;
2136
+ }
2137
+ return { provider, modelId };
2138
+ }
2139
+ function summarizeGenerate(result, model) {
2140
+ const content = Array.isArray(result.content) ? result.content : [];
2141
+ const text = typeof result.text === "string" ? result.text : content.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
2142
+ const toolCalls = content.filter((p) => p.type === "tool-call").map((p) => ({
2143
+ toolCallId: p.toolCallId,
2144
+ toolName: p.toolName,
2145
+ input: p.input ?? p.args
2146
+ }));
2147
+ const summary = {
2148
+ text,
2149
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
2150
+ usage: result.usage,
2151
+ finishReason: result.finishReason
2152
+ };
2153
+ if (model) {
2154
+ summary.model = model;
2155
+ }
2156
+ return summary;
2157
+ }
2158
+ function accumulateStream(onComplete, model) {
2159
+ let text = "";
2160
+ const toolCalls = [];
2161
+ let usage;
2162
+ let finishReason;
2163
+ let completed = false;
2164
+ const complete = () => {
2165
+ if (completed) {
2166
+ return;
2167
+ }
2168
+ completed = true;
2169
+ const summary = {
2170
+ text,
2171
+ toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
2172
+ usage,
2173
+ finishReason
2174
+ };
2175
+ if (model) {
2176
+ summary.model = model;
2177
+ }
2178
+ onComplete(summary);
2179
+ };
2180
+ return new TransformStream({
2181
+ transform(part, controller) {
2182
+ try {
2183
+ if (part?.type === "text-delta") {
2184
+ text += part.delta ?? part.textDelta ?? "";
2185
+ } else if (part?.type === "tool-call") {
2186
+ toolCalls.push({
2187
+ toolCallId: part.toolCallId,
2188
+ toolName: part.toolName,
2189
+ input: part.input ?? part.args
2190
+ });
2191
+ } else if (part?.type === "finish") {
2192
+ usage = part.usage;
2193
+ finishReason = part.finishReason;
2194
+ complete();
2195
+ }
2196
+ } catch {
2197
+ }
2198
+ controller.enqueue(part);
2199
+ },
2200
+ flush() {
2201
+ complete();
2202
+ }
2203
+ });
2204
+ }
2205
+ var BitfabVercelAiHandler = class {
2206
+ constructor(config) {
2207
+ this.traceFunctionKey = config.traceFunctionKey;
2208
+ this.withSpanFn = config.withSpan;
2209
+ }
2210
+ /** The `wrapLanguageModel` middleware object for this trace function key. */
2211
+ get middleware() {
2212
+ const key = this.traceFunctionKey;
2213
+ const withSpan = this.withSpanFn;
2214
+ return {
2215
+ specificationVersion: "v3",
2216
+ wrapGenerate: async ({ doGenerate, params, model }) => {
2217
+ const label = modelLabel(model);
2218
+ const traced = withSpan(
2219
+ key,
2220
+ {
2221
+ type: "llm",
2222
+ finalize: (result) => summarizeGenerate(result ?? {}, label)
2223
+ },
2224
+ () => doGenerate()
2225
+ );
2226
+ return traced(params);
2227
+ },
2228
+ wrapStream: async ({ doStream, params, model }) => {
2229
+ const label = modelLabel(model);
2230
+ let resolveSummary = () => {
2231
+ };
2232
+ const summary = new Promise((resolve) => {
2233
+ resolveSummary = resolve;
2234
+ });
2235
+ const traced = withSpan(
2236
+ key,
2237
+ // The wrapped fn returns immediately with the live stream, so the span
2238
+ // output cannot be read from the return value. `finalize` instead
2239
+ // awaits the summary the accumulator resolves once the stream drains.
2240
+ { type: "llm", finalize: () => summary },
2241
+ async () => {
2242
+ const result = await doStream();
2243
+ const stream = result.stream.pipeThrough(
2244
+ accumulateStream(resolveSummary, label)
2245
+ );
2246
+ return { ...result, stream };
2247
+ }
2248
+ );
2249
+ return traced(params);
2250
+ }
2251
+ };
2252
+ }
2253
+ };
2254
+
2113
2255
  // src/client.ts
2114
2256
  var activeTraceStates = /* @__PURE__ */ new Map();
2115
2257
  var pendingSpanPromises = /* @__PURE__ */ new Map();
@@ -2209,7 +2351,10 @@ async function loadCollectorClass() {
2209
2351
  return cachedCollectorClass;
2210
2352
  }
2211
2353
  try {
2212
- const baml = await import("@boundaryml/baml");
2354
+ const baml = await importOptionalPeer([
2355
+ "@boundaryml",
2356
+ "baml"
2357
+ ]);
2213
2358
  cachedCollectorClass = baml.Collector;
2214
2359
  return cachedCollectorClass;
2215
2360
  } catch {
@@ -2625,6 +2770,36 @@ var Bitfab = class {
2625
2770
  }
2626
2771
  });
2627
2772
  }
2773
+ /**
2774
+ * Get a Vercel AI SDK language-model middleware for tracing.
2775
+ *
2776
+ * Pass it to the AI SDK's `wrapLanguageModel` and use the wrapped model with
2777
+ * `generateText` / `streamText` / `generateObject` / `streamObject`. Every
2778
+ * call through that model is captured as a keyed `llm` span carrying the call
2779
+ * parameters (the prompt) as input and a serializable summary
2780
+ * (`{ text, toolCalls, usage, finishReason }`) as output. Streaming is
2781
+ * captured without disturbing the caller's live stream.
2782
+ *
2783
+ * ```typescript
2784
+ * import { wrapLanguageModel, streamText } from "ai";
2785
+ * import { openai } from "@ai-sdk/openai";
2786
+ *
2787
+ * const model = wrapLanguageModel({
2788
+ * model: openai("gpt-4o"),
2789
+ * middleware: client.getVercelAiMiddleware("chat-turn"),
2790
+ * });
2791
+ * const result = streamText({ model, messages });
2792
+ * ```
2793
+ *
2794
+ * @param traceFunctionKey - Groups traces under this key in Bitfab
2795
+ * @returns A Vercel AI SDK middleware configured for this client
2796
+ */
2797
+ getVercelAiMiddleware(traceFunctionKey) {
2798
+ return new BitfabVercelAiHandler({
2799
+ traceFunctionKey,
2800
+ withSpan: this.withSpan.bind(this)
2801
+ }).middleware;
2802
+ }
2628
2803
  /**
2629
2804
  * Wrap a BAML client method to automatically capture prompt and LLM metadata.
2630
2805
  *
@@ -3298,10 +3473,11 @@ export {
3298
3473
  BitfabOpenAIAgentHandler,
3299
3474
  ReplayEnvironment,
3300
3475
  BitfabOpenAITracingProcessor,
3476
+ BitfabVercelAiHandler,
3301
3477
  getCurrentSpan,
3302
3478
  getCurrentTrace,
3303
3479
  Bitfab,
3304
3480
  BitfabFunction,
3305
3481
  finalizers
3306
3482
  };
3307
- //# sourceMappingURL=chunk-2EDITKO2.js.map
3483
+ //# sourceMappingURL=chunk-CG6LVLBK.js.map