@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/{chunk-2EDITKO2.js → chunk-CG6LVLBK.js} +181 -5
- package/dist/chunk-CG6LVLBK.js.map +1 -0
- package/dist/index.cjs +181 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +157 -8
- package/dist/index.d.ts +157 -8
- package/dist/index.js +3 -1
- package/dist/node.cjs +181 -4
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.js +3 -1
- package/dist/node.js.map +1 -1
- package/package.json +7 -2
- package/dist/chunk-2EDITKO2.js.map +0 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Agent, AgentInputItem, RunState, NonStreamRunOptions, RunResult, StreamRunOptions, StreamedRunResult
|
|
1
|
+
import { Agent, AgentInputItem, RunState, NonStreamRunOptions, RunResult, StreamRunOptions, StreamedRunResult } from '@openai/agents';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* BAML execution utilities for the Bitfab TypeScript SDK.
|
|
@@ -171,6 +171,122 @@ declare class BitfabClaudeAgentHandler {
|
|
|
171
171
|
private resetState;
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
+
/**
|
|
175
|
+
* Vercel AI SDK integration for Bitfab tracing.
|
|
176
|
+
*
|
|
177
|
+
* The Vercel AI SDK (`ai`) routes every `generateText` / `streamText` /
|
|
178
|
+
* `generateObject` / `streamObject` call through a language model. Bitfab hooks
|
|
179
|
+
* that model with a *language-model middleware* (`wrapLanguageModel`), so each
|
|
180
|
+
* model call is captured as a keyed `llm` span with no hand-written `withSpan`:
|
|
181
|
+
*
|
|
182
|
+
* ```typescript
|
|
183
|
+
* import { Bitfab } from "@bitfab/sdk";
|
|
184
|
+
* import { wrapLanguageModel, streamText } from "ai";
|
|
185
|
+
* import { openai } from "@ai-sdk/openai";
|
|
186
|
+
*
|
|
187
|
+
* const bitfab = new Bitfab({ apiKey: "..." });
|
|
188
|
+
*
|
|
189
|
+
* const model = wrapLanguageModel({
|
|
190
|
+
* model: openai("gpt-4o"),
|
|
191
|
+
* middleware: bitfab.getVercelAiMiddleware("chat-turn"),
|
|
192
|
+
* });
|
|
193
|
+
*
|
|
194
|
+
* const result = streamText({ model, messages });
|
|
195
|
+
* return result.toUIMessageStreamResponse(); // live stream untouched
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* The span records the call parameters (the prompt/messages) as its input and a
|
|
199
|
+
* serializable summary (`{ text, toolCalls, usage, finishReason }`) as its
|
|
200
|
+
* output. Streaming is handled by passing the model's stream through a
|
|
201
|
+
* transform that accumulates the assembled text/usage as the caller consumes
|
|
202
|
+
* it: the live stream is handed back unchanged (first-byte latency untouched)
|
|
203
|
+
* and the span is finalized once the stream completes.
|
|
204
|
+
*
|
|
205
|
+
* The middleware is fully duck-typed (no static or dynamic `ai` import), so it
|
|
206
|
+
* adds no dependency and is browser-safe. It works with `ai` v5 and v6.
|
|
207
|
+
*/
|
|
208
|
+
type LlmSpanOptions = {
|
|
209
|
+
type: "llm";
|
|
210
|
+
finalize: (result: unknown) => unknown | Promise<unknown>;
|
|
211
|
+
};
|
|
212
|
+
type WithSpanFn$1 = <TArgs extends unknown[], TReturn>(traceFunctionKey: string, options: LlmSpanOptions, fn: (...args: TArgs) => TReturn) => (...args: TArgs) => TReturn;
|
|
213
|
+
/**
|
|
214
|
+
* Duck-typed subset of the Vercel AI SDK language-model call parameters. The
|
|
215
|
+
* only field we read for the span input is `prompt` (the messages), but the
|
|
216
|
+
* whole object is recorded so the call is reconstructable on replay.
|
|
217
|
+
*/
|
|
218
|
+
interface VercelCallParams {
|
|
219
|
+
prompt?: unknown;
|
|
220
|
+
[key: string]: unknown;
|
|
221
|
+
}
|
|
222
|
+
/** A content part of a non-streaming `doGenerate` result. */
|
|
223
|
+
interface VercelContentPart {
|
|
224
|
+
type: string;
|
|
225
|
+
text?: string;
|
|
226
|
+
toolCallId?: string;
|
|
227
|
+
toolName?: string;
|
|
228
|
+
input?: unknown;
|
|
229
|
+
args?: unknown;
|
|
230
|
+
}
|
|
231
|
+
/** Duck-typed subset of a non-streaming `doGenerate` result. */
|
|
232
|
+
interface VercelGenerateResult {
|
|
233
|
+
content?: VercelContentPart[];
|
|
234
|
+
text?: string;
|
|
235
|
+
usage?: unknown;
|
|
236
|
+
finishReason?: unknown;
|
|
237
|
+
[key: string]: unknown;
|
|
238
|
+
}
|
|
239
|
+
/** Duck-typed subset of a single streaming part from `doStream`. */
|
|
240
|
+
interface VercelStreamPart {
|
|
241
|
+
type: string;
|
|
242
|
+
delta?: string;
|
|
243
|
+
textDelta?: string;
|
|
244
|
+
toolCallId?: string;
|
|
245
|
+
toolName?: string;
|
|
246
|
+
input?: unknown;
|
|
247
|
+
args?: unknown;
|
|
248
|
+
usage?: unknown;
|
|
249
|
+
finishReason?: unknown;
|
|
250
|
+
}
|
|
251
|
+
/** Duck-typed subset of a streaming `doStream` result. */
|
|
252
|
+
interface VercelStreamResult {
|
|
253
|
+
stream: ReadableStream<VercelStreamPart>;
|
|
254
|
+
[key: string]: unknown;
|
|
255
|
+
}
|
|
256
|
+
interface MiddlewareCall<TResult> {
|
|
257
|
+
doGenerate: () => PromiseLike<VercelGenerateResult>;
|
|
258
|
+
doStream: () => PromiseLike<VercelStreamResult>;
|
|
259
|
+
params: VercelCallParams;
|
|
260
|
+
model: unknown;
|
|
261
|
+
__result?: TResult;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* The structural shape of a Vercel AI SDK language-model middleware. Matches
|
|
265
|
+
* `LanguageModelV3Middleware` from `@ai-sdk/provider` without importing it, so
|
|
266
|
+
* the SDK stays dependency-free. `wrapLanguageModel` only reads the method
|
|
267
|
+
* fields (it ignores `specificationVersion`), so this object drops straight in.
|
|
268
|
+
*/
|
|
269
|
+
interface BitfabLanguageModelMiddleware {
|
|
270
|
+
specificationVersion: "v3";
|
|
271
|
+
wrapGenerate: (options: MiddlewareCall<VercelGenerateResult>) => Promise<any>;
|
|
272
|
+
wrapStream: (options: MiddlewareCall<VercelStreamResult>) => Promise<any>;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Vercel AI SDK middleware that records each language-model call as a keyed
|
|
276
|
+
* `llm` span. Obtain it from {@link BitfabClient.getVercelAiMiddleware} rather
|
|
277
|
+
* than constructing it directly.
|
|
278
|
+
*/
|
|
279
|
+
declare class BitfabVercelAiHandler {
|
|
280
|
+
private readonly traceFunctionKey;
|
|
281
|
+
private readonly withSpanFn;
|
|
282
|
+
constructor(config: {
|
|
283
|
+
traceFunctionKey: string;
|
|
284
|
+
withSpan: WithSpanFn$1;
|
|
285
|
+
});
|
|
286
|
+
/** The `wrapLanguageModel` middleware object for this trace function key. */
|
|
287
|
+
get middleware(): BitfabLanguageModelMiddleware;
|
|
288
|
+
}
|
|
289
|
+
|
|
174
290
|
/**
|
|
175
291
|
* Per-trace database snapshot ref capture.
|
|
176
292
|
*
|
|
@@ -592,7 +708,19 @@ interface ReplayResult<T> {
|
|
|
592
708
|
* This module provides utilities for sending external traces (e.g., from OpenAI API calls)
|
|
593
709
|
* to Bitfab for monitoring and analysis.
|
|
594
710
|
*/
|
|
595
|
-
|
|
711
|
+
interface Trace {
|
|
712
|
+
traceId: string;
|
|
713
|
+
toJSON(): unknown;
|
|
714
|
+
}
|
|
715
|
+
interface Span<_T = any> {
|
|
716
|
+
traceId?: string;
|
|
717
|
+
toJSON(): unknown;
|
|
718
|
+
spanData?: {
|
|
719
|
+
type?: string;
|
|
720
|
+
_input?: unknown;
|
|
721
|
+
_response?: unknown;
|
|
722
|
+
} | null;
|
|
723
|
+
}
|
|
596
724
|
interface TraceResponse {
|
|
597
725
|
traceId: string;
|
|
598
726
|
status: "success";
|
|
@@ -705,10 +833,6 @@ declare class BitfabOpenAITracingProcessor implements TracingProcessor {
|
|
|
705
833
|
private sendSpan;
|
|
706
834
|
}
|
|
707
835
|
|
|
708
|
-
/**
|
|
709
|
-
* Bitfab client for provider-based API calls.
|
|
710
|
-
*/
|
|
711
|
-
|
|
712
836
|
/**
|
|
713
837
|
* Options for wrapBAML.
|
|
714
838
|
*/
|
|
@@ -1025,6 +1149,31 @@ declare class Bitfab {
|
|
|
1025
1149
|
* @returns A BitfabClaudeAgentHandler configured for this client
|
|
1026
1150
|
*/
|
|
1027
1151
|
getClaudeAgentHandler(traceFunctionKey: string): BitfabClaudeAgentHandler;
|
|
1152
|
+
/**
|
|
1153
|
+
* Get a Vercel AI SDK language-model middleware for tracing.
|
|
1154
|
+
*
|
|
1155
|
+
* Pass it to the AI SDK's `wrapLanguageModel` and use the wrapped model with
|
|
1156
|
+
* `generateText` / `streamText` / `generateObject` / `streamObject`. Every
|
|
1157
|
+
* call through that model is captured as a keyed `llm` span carrying the call
|
|
1158
|
+
* parameters (the prompt) as input and a serializable summary
|
|
1159
|
+
* (`{ text, toolCalls, usage, finishReason }`) as output. Streaming is
|
|
1160
|
+
* captured without disturbing the caller's live stream.
|
|
1161
|
+
*
|
|
1162
|
+
* ```typescript
|
|
1163
|
+
* import { wrapLanguageModel, streamText } from "ai";
|
|
1164
|
+
* import { openai } from "@ai-sdk/openai";
|
|
1165
|
+
*
|
|
1166
|
+
* const model = wrapLanguageModel({
|
|
1167
|
+
* model: openai("gpt-4o"),
|
|
1168
|
+
* middleware: client.getVercelAiMiddleware("chat-turn"),
|
|
1169
|
+
* });
|
|
1170
|
+
* const result = streamText({ model, messages });
|
|
1171
|
+
* ```
|
|
1172
|
+
*
|
|
1173
|
+
* @param traceFunctionKey - Groups traces under this key in Bitfab
|
|
1174
|
+
* @returns A Vercel AI SDK middleware configured for this client
|
|
1175
|
+
*/
|
|
1176
|
+
getVercelAiMiddleware(traceFunctionKey: string): BitfabLanguageModelMiddleware;
|
|
1028
1177
|
/**
|
|
1029
1178
|
* Wrap a BAML client method to automatically capture prompt and LLM metadata.
|
|
1030
1179
|
*
|
|
@@ -1221,7 +1370,7 @@ declare class BitfabFunction {
|
|
|
1221
1370
|
/**
|
|
1222
1371
|
* SDK version from package.json (injected at build time)
|
|
1223
1372
|
*/
|
|
1224
|
-
declare const __version__ = "0.23.
|
|
1373
|
+
declare const __version__ = "0.23.3";
|
|
1225
1374
|
|
|
1226
1375
|
/**
|
|
1227
1376
|
* Constants for the Bitfab SDK.
|
|
@@ -1289,4 +1438,4 @@ declare const finalizers: {
|
|
|
1289
1438
|
readableStream: typeof readableStream;
|
|
1290
1439
|
};
|
|
1291
1440
|
|
|
1292
|
-
export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, BitfabOpenAIAgentHandler, BitfabOpenAITracingProcessor, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, type MockStrategy, type ProviderDefinition, ReplayEnvironment, type ReplayEnvironmentSnapshot, type ReplayItem, type ReplayOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type WrapBAMLOptions, type WrappedBamlFn, __version__, finalizers, flushTraces, getCurrentSpan, getCurrentTrace };
|
|
1441
|
+
export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, type BitfabLanguageModelMiddleware, BitfabOpenAIAgentHandler, BitfabOpenAITracingProcessor, BitfabVercelAiHandler, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, type MockStrategy, type ProviderDefinition, ReplayEnvironment, type ReplayEnvironmentSnapshot, type ReplayItem, type ReplayOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type VercelCallParams, type VercelGenerateResult, type VercelStreamResult, type WrapBAMLOptions, type WrappedBamlFn, __version__, finalizers, flushTraces, getCurrentSpan, getCurrentTrace };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Agent, AgentInputItem, RunState, NonStreamRunOptions, RunResult, StreamRunOptions, StreamedRunResult
|
|
1
|
+
import { Agent, AgentInputItem, RunState, NonStreamRunOptions, RunResult, StreamRunOptions, StreamedRunResult } from '@openai/agents';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* BAML execution utilities for the Bitfab TypeScript SDK.
|
|
@@ -171,6 +171,122 @@ declare class BitfabClaudeAgentHandler {
|
|
|
171
171
|
private resetState;
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
+
/**
|
|
175
|
+
* Vercel AI SDK integration for Bitfab tracing.
|
|
176
|
+
*
|
|
177
|
+
* The Vercel AI SDK (`ai`) routes every `generateText` / `streamText` /
|
|
178
|
+
* `generateObject` / `streamObject` call through a language model. Bitfab hooks
|
|
179
|
+
* that model with a *language-model middleware* (`wrapLanguageModel`), so each
|
|
180
|
+
* model call is captured as a keyed `llm` span with no hand-written `withSpan`:
|
|
181
|
+
*
|
|
182
|
+
* ```typescript
|
|
183
|
+
* import { Bitfab } from "@bitfab/sdk";
|
|
184
|
+
* import { wrapLanguageModel, streamText } from "ai";
|
|
185
|
+
* import { openai } from "@ai-sdk/openai";
|
|
186
|
+
*
|
|
187
|
+
* const bitfab = new Bitfab({ apiKey: "..." });
|
|
188
|
+
*
|
|
189
|
+
* const model = wrapLanguageModel({
|
|
190
|
+
* model: openai("gpt-4o"),
|
|
191
|
+
* middleware: bitfab.getVercelAiMiddleware("chat-turn"),
|
|
192
|
+
* });
|
|
193
|
+
*
|
|
194
|
+
* const result = streamText({ model, messages });
|
|
195
|
+
* return result.toUIMessageStreamResponse(); // live stream untouched
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* The span records the call parameters (the prompt/messages) as its input and a
|
|
199
|
+
* serializable summary (`{ text, toolCalls, usage, finishReason }`) as its
|
|
200
|
+
* output. Streaming is handled by passing the model's stream through a
|
|
201
|
+
* transform that accumulates the assembled text/usage as the caller consumes
|
|
202
|
+
* it: the live stream is handed back unchanged (first-byte latency untouched)
|
|
203
|
+
* and the span is finalized once the stream completes.
|
|
204
|
+
*
|
|
205
|
+
* The middleware is fully duck-typed (no static or dynamic `ai` import), so it
|
|
206
|
+
* adds no dependency and is browser-safe. It works with `ai` v5 and v6.
|
|
207
|
+
*/
|
|
208
|
+
type LlmSpanOptions = {
|
|
209
|
+
type: "llm";
|
|
210
|
+
finalize: (result: unknown) => unknown | Promise<unknown>;
|
|
211
|
+
};
|
|
212
|
+
type WithSpanFn$1 = <TArgs extends unknown[], TReturn>(traceFunctionKey: string, options: LlmSpanOptions, fn: (...args: TArgs) => TReturn) => (...args: TArgs) => TReturn;
|
|
213
|
+
/**
|
|
214
|
+
* Duck-typed subset of the Vercel AI SDK language-model call parameters. The
|
|
215
|
+
* only field we read for the span input is `prompt` (the messages), but the
|
|
216
|
+
* whole object is recorded so the call is reconstructable on replay.
|
|
217
|
+
*/
|
|
218
|
+
interface VercelCallParams {
|
|
219
|
+
prompt?: unknown;
|
|
220
|
+
[key: string]: unknown;
|
|
221
|
+
}
|
|
222
|
+
/** A content part of a non-streaming `doGenerate` result. */
|
|
223
|
+
interface VercelContentPart {
|
|
224
|
+
type: string;
|
|
225
|
+
text?: string;
|
|
226
|
+
toolCallId?: string;
|
|
227
|
+
toolName?: string;
|
|
228
|
+
input?: unknown;
|
|
229
|
+
args?: unknown;
|
|
230
|
+
}
|
|
231
|
+
/** Duck-typed subset of a non-streaming `doGenerate` result. */
|
|
232
|
+
interface VercelGenerateResult {
|
|
233
|
+
content?: VercelContentPart[];
|
|
234
|
+
text?: string;
|
|
235
|
+
usage?: unknown;
|
|
236
|
+
finishReason?: unknown;
|
|
237
|
+
[key: string]: unknown;
|
|
238
|
+
}
|
|
239
|
+
/** Duck-typed subset of a single streaming part from `doStream`. */
|
|
240
|
+
interface VercelStreamPart {
|
|
241
|
+
type: string;
|
|
242
|
+
delta?: string;
|
|
243
|
+
textDelta?: string;
|
|
244
|
+
toolCallId?: string;
|
|
245
|
+
toolName?: string;
|
|
246
|
+
input?: unknown;
|
|
247
|
+
args?: unknown;
|
|
248
|
+
usage?: unknown;
|
|
249
|
+
finishReason?: unknown;
|
|
250
|
+
}
|
|
251
|
+
/** Duck-typed subset of a streaming `doStream` result. */
|
|
252
|
+
interface VercelStreamResult {
|
|
253
|
+
stream: ReadableStream<VercelStreamPart>;
|
|
254
|
+
[key: string]: unknown;
|
|
255
|
+
}
|
|
256
|
+
interface MiddlewareCall<TResult> {
|
|
257
|
+
doGenerate: () => PromiseLike<VercelGenerateResult>;
|
|
258
|
+
doStream: () => PromiseLike<VercelStreamResult>;
|
|
259
|
+
params: VercelCallParams;
|
|
260
|
+
model: unknown;
|
|
261
|
+
__result?: TResult;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* The structural shape of a Vercel AI SDK language-model middleware. Matches
|
|
265
|
+
* `LanguageModelV3Middleware` from `@ai-sdk/provider` without importing it, so
|
|
266
|
+
* the SDK stays dependency-free. `wrapLanguageModel` only reads the method
|
|
267
|
+
* fields (it ignores `specificationVersion`), so this object drops straight in.
|
|
268
|
+
*/
|
|
269
|
+
interface BitfabLanguageModelMiddleware {
|
|
270
|
+
specificationVersion: "v3";
|
|
271
|
+
wrapGenerate: (options: MiddlewareCall<VercelGenerateResult>) => Promise<any>;
|
|
272
|
+
wrapStream: (options: MiddlewareCall<VercelStreamResult>) => Promise<any>;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Vercel AI SDK middleware that records each language-model call as a keyed
|
|
276
|
+
* `llm` span. Obtain it from {@link BitfabClient.getVercelAiMiddleware} rather
|
|
277
|
+
* than constructing it directly.
|
|
278
|
+
*/
|
|
279
|
+
declare class BitfabVercelAiHandler {
|
|
280
|
+
private readonly traceFunctionKey;
|
|
281
|
+
private readonly withSpanFn;
|
|
282
|
+
constructor(config: {
|
|
283
|
+
traceFunctionKey: string;
|
|
284
|
+
withSpan: WithSpanFn$1;
|
|
285
|
+
});
|
|
286
|
+
/** The `wrapLanguageModel` middleware object for this trace function key. */
|
|
287
|
+
get middleware(): BitfabLanguageModelMiddleware;
|
|
288
|
+
}
|
|
289
|
+
|
|
174
290
|
/**
|
|
175
291
|
* Per-trace database snapshot ref capture.
|
|
176
292
|
*
|
|
@@ -592,7 +708,19 @@ interface ReplayResult<T> {
|
|
|
592
708
|
* This module provides utilities for sending external traces (e.g., from OpenAI API calls)
|
|
593
709
|
* to Bitfab for monitoring and analysis.
|
|
594
710
|
*/
|
|
595
|
-
|
|
711
|
+
interface Trace {
|
|
712
|
+
traceId: string;
|
|
713
|
+
toJSON(): unknown;
|
|
714
|
+
}
|
|
715
|
+
interface Span<_T = any> {
|
|
716
|
+
traceId?: string;
|
|
717
|
+
toJSON(): unknown;
|
|
718
|
+
spanData?: {
|
|
719
|
+
type?: string;
|
|
720
|
+
_input?: unknown;
|
|
721
|
+
_response?: unknown;
|
|
722
|
+
} | null;
|
|
723
|
+
}
|
|
596
724
|
interface TraceResponse {
|
|
597
725
|
traceId: string;
|
|
598
726
|
status: "success";
|
|
@@ -705,10 +833,6 @@ declare class BitfabOpenAITracingProcessor implements TracingProcessor {
|
|
|
705
833
|
private sendSpan;
|
|
706
834
|
}
|
|
707
835
|
|
|
708
|
-
/**
|
|
709
|
-
* Bitfab client for provider-based API calls.
|
|
710
|
-
*/
|
|
711
|
-
|
|
712
836
|
/**
|
|
713
837
|
* Options for wrapBAML.
|
|
714
838
|
*/
|
|
@@ -1025,6 +1149,31 @@ declare class Bitfab {
|
|
|
1025
1149
|
* @returns A BitfabClaudeAgentHandler configured for this client
|
|
1026
1150
|
*/
|
|
1027
1151
|
getClaudeAgentHandler(traceFunctionKey: string): BitfabClaudeAgentHandler;
|
|
1152
|
+
/**
|
|
1153
|
+
* Get a Vercel AI SDK language-model middleware for tracing.
|
|
1154
|
+
*
|
|
1155
|
+
* Pass it to the AI SDK's `wrapLanguageModel` and use the wrapped model with
|
|
1156
|
+
* `generateText` / `streamText` / `generateObject` / `streamObject`. Every
|
|
1157
|
+
* call through that model is captured as a keyed `llm` span carrying the call
|
|
1158
|
+
* parameters (the prompt) as input and a serializable summary
|
|
1159
|
+
* (`{ text, toolCalls, usage, finishReason }`) as output. Streaming is
|
|
1160
|
+
* captured without disturbing the caller's live stream.
|
|
1161
|
+
*
|
|
1162
|
+
* ```typescript
|
|
1163
|
+
* import { wrapLanguageModel, streamText } from "ai";
|
|
1164
|
+
* import { openai } from "@ai-sdk/openai";
|
|
1165
|
+
*
|
|
1166
|
+
* const model = wrapLanguageModel({
|
|
1167
|
+
* model: openai("gpt-4o"),
|
|
1168
|
+
* middleware: client.getVercelAiMiddleware("chat-turn"),
|
|
1169
|
+
* });
|
|
1170
|
+
* const result = streamText({ model, messages });
|
|
1171
|
+
* ```
|
|
1172
|
+
*
|
|
1173
|
+
* @param traceFunctionKey - Groups traces under this key in Bitfab
|
|
1174
|
+
* @returns A Vercel AI SDK middleware configured for this client
|
|
1175
|
+
*/
|
|
1176
|
+
getVercelAiMiddleware(traceFunctionKey: string): BitfabLanguageModelMiddleware;
|
|
1028
1177
|
/**
|
|
1029
1178
|
* Wrap a BAML client method to automatically capture prompt and LLM metadata.
|
|
1030
1179
|
*
|
|
@@ -1221,7 +1370,7 @@ declare class BitfabFunction {
|
|
|
1221
1370
|
/**
|
|
1222
1371
|
* SDK version from package.json (injected at build time)
|
|
1223
1372
|
*/
|
|
1224
|
-
declare const __version__ = "0.23.
|
|
1373
|
+
declare const __version__ = "0.23.3";
|
|
1225
1374
|
|
|
1226
1375
|
/**
|
|
1227
1376
|
* Constants for the Bitfab SDK.
|
|
@@ -1289,4 +1438,4 @@ declare const finalizers: {
|
|
|
1289
1438
|
readableStream: typeof readableStream;
|
|
1290
1439
|
};
|
|
1291
1440
|
|
|
1292
|
-
export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, BitfabOpenAIAgentHandler, BitfabOpenAITracingProcessor, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, type MockStrategy, type ProviderDefinition, ReplayEnvironment, type ReplayEnvironmentSnapshot, type ReplayItem, type ReplayOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type WrapBAMLOptions, type WrappedBamlFn, __version__, finalizers, flushTraces, getCurrentSpan, getCurrentTrace };
|
|
1441
|
+
export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, type BitfabLanguageModelMiddleware, BitfabOpenAIAgentHandler, BitfabOpenAITracingProcessor, BitfabVercelAiHandler, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, type MockStrategy, type ProviderDefinition, ReplayEnvironment, type ReplayEnvironmentSnapshot, type ReplayItem, type ReplayOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type VercelCallParams, type VercelGenerateResult, type VercelStreamResult, type WrapBAMLOptions, type WrappedBamlFn, __version__, finalizers, flushTraces, getCurrentSpan, getCurrentTrace };
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
BitfabLangGraphCallbackHandler,
|
|
6
6
|
BitfabOpenAIAgentHandler,
|
|
7
7
|
BitfabOpenAITracingProcessor,
|
|
8
|
+
BitfabVercelAiHandler,
|
|
8
9
|
DEFAULT_SERVICE_URL,
|
|
9
10
|
ReplayEnvironment,
|
|
10
11
|
SUPPORTED_PROVIDERS,
|
|
@@ -13,7 +14,7 @@ import {
|
|
|
13
14
|
flushTraces,
|
|
14
15
|
getCurrentSpan,
|
|
15
16
|
getCurrentTrace
|
|
16
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-CG6LVLBK.js";
|
|
17
18
|
import {
|
|
18
19
|
BitfabError
|
|
19
20
|
} from "./chunk-EQI6ZJC3.js";
|
|
@@ -26,6 +27,7 @@ export {
|
|
|
26
27
|
BitfabLangGraphCallbackHandler,
|
|
27
28
|
BitfabOpenAIAgentHandler,
|
|
28
29
|
BitfabOpenAITracingProcessor,
|
|
30
|
+
BitfabVercelAiHandler,
|
|
29
31
|
DEFAULT_SERVICE_URL,
|
|
30
32
|
ReplayEnvironment,
|
|
31
33
|
SUPPORTED_PROVIDERS,
|
package/dist/node.cjs
CHANGED
|
@@ -486,6 +486,7 @@ __export(node_exports, {
|
|
|
486
486
|
BitfabLangGraphCallbackHandler: () => BitfabLangGraphCallbackHandler,
|
|
487
487
|
BitfabOpenAIAgentHandler: () => BitfabOpenAIAgentHandler,
|
|
488
488
|
BitfabOpenAITracingProcessor: () => BitfabOpenAITracingProcessor,
|
|
489
|
+
BitfabVercelAiHandler: () => BitfabVercelAiHandler,
|
|
489
490
|
DEFAULT_SERVICE_URL: () => DEFAULT_SERVICE_URL,
|
|
490
491
|
ReplayEnvironment: () => ReplayEnvironment,
|
|
491
492
|
SUPPORTED_PROVIDERS: () => SUPPORTED_PROVIDERS,
|
|
@@ -505,7 +506,7 @@ registerAsyncLocalStorageClass(
|
|
|
505
506
|
);
|
|
506
507
|
|
|
507
508
|
// src/version.generated.ts
|
|
508
|
-
var __version__ = "0.23.
|
|
509
|
+
var __version__ = "0.23.3";
|
|
509
510
|
|
|
510
511
|
// src/constants.ts
|
|
511
512
|
var DEFAULT_SERVICE_URL = "https://bitfab.ai";
|
|
@@ -1446,6 +1447,16 @@ var BitfabClaudeAgentHandler = class {
|
|
|
1446
1447
|
// src/client.ts
|
|
1447
1448
|
init_asyncStorage();
|
|
1448
1449
|
|
|
1450
|
+
// src/optionalPeer.ts
|
|
1451
|
+
function importOptionalPeer(specifierParts) {
|
|
1452
|
+
const specifier = specifierParts.join("/");
|
|
1453
|
+
return import(
|
|
1454
|
+
/* webpackIgnore: true */
|
|
1455
|
+
/* @vite-ignore */
|
|
1456
|
+
specifier
|
|
1457
|
+
);
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1449
1460
|
// src/baml.ts
|
|
1450
1461
|
var cachedBaml = null;
|
|
1451
1462
|
async function loadBaml() {
|
|
@@ -1453,7 +1464,7 @@ async function loadBaml() {
|
|
|
1453
1464
|
return cachedBaml;
|
|
1454
1465
|
}
|
|
1455
1466
|
try {
|
|
1456
|
-
cachedBaml = await
|
|
1467
|
+
cachedBaml = await importOptionalPeer(["@boundaryml", "baml"]);
|
|
1457
1468
|
return cachedBaml;
|
|
1458
1469
|
} catch {
|
|
1459
1470
|
throw new Error(
|
|
@@ -2296,7 +2307,10 @@ var BitfabOpenAIAgentHandler = class {
|
|
|
2296
2307
|
this.getActiveSpanContext = config.getActiveSpanContext;
|
|
2297
2308
|
}
|
|
2298
2309
|
async wrapRun(agent, input, options) {
|
|
2299
|
-
const { run } = await
|
|
2310
|
+
const { run } = await importOptionalPeer([
|
|
2311
|
+
"@openai",
|
|
2312
|
+
"agents"
|
|
2313
|
+
]);
|
|
2300
2314
|
if (this.getActiveSpanContext?.() != null) {
|
|
2301
2315
|
return run(
|
|
2302
2316
|
agent,
|
|
@@ -2619,6 +2633,135 @@ var BitfabOpenAITracingProcessor = class {
|
|
|
2619
2633
|
}
|
|
2620
2634
|
};
|
|
2621
2635
|
|
|
2636
|
+
// src/vercelAiSdk.ts
|
|
2637
|
+
function modelLabel(model) {
|
|
2638
|
+
if (!model || typeof model !== "object") {
|
|
2639
|
+
return void 0;
|
|
2640
|
+
}
|
|
2641
|
+
const m = model;
|
|
2642
|
+
const provider = typeof m.provider === "string" ? m.provider : void 0;
|
|
2643
|
+
const modelId = typeof m.modelId === "string" ? m.modelId : void 0;
|
|
2644
|
+
if (provider == null && modelId == null) {
|
|
2645
|
+
return void 0;
|
|
2646
|
+
}
|
|
2647
|
+
return { provider, modelId };
|
|
2648
|
+
}
|
|
2649
|
+
function summarizeGenerate(result, model) {
|
|
2650
|
+
const content = Array.isArray(result.content) ? result.content : [];
|
|
2651
|
+
const text = typeof result.text === "string" ? result.text : content.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
|
|
2652
|
+
const toolCalls = content.filter((p) => p.type === "tool-call").map((p) => ({
|
|
2653
|
+
toolCallId: p.toolCallId,
|
|
2654
|
+
toolName: p.toolName,
|
|
2655
|
+
input: p.input ?? p.args
|
|
2656
|
+
}));
|
|
2657
|
+
const summary = {
|
|
2658
|
+
text,
|
|
2659
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
2660
|
+
usage: result.usage,
|
|
2661
|
+
finishReason: result.finishReason
|
|
2662
|
+
};
|
|
2663
|
+
if (model) {
|
|
2664
|
+
summary.model = model;
|
|
2665
|
+
}
|
|
2666
|
+
return summary;
|
|
2667
|
+
}
|
|
2668
|
+
function accumulateStream(onComplete, model) {
|
|
2669
|
+
let text = "";
|
|
2670
|
+
const toolCalls = [];
|
|
2671
|
+
let usage;
|
|
2672
|
+
let finishReason;
|
|
2673
|
+
let completed = false;
|
|
2674
|
+
const complete = () => {
|
|
2675
|
+
if (completed) {
|
|
2676
|
+
return;
|
|
2677
|
+
}
|
|
2678
|
+
completed = true;
|
|
2679
|
+
const summary = {
|
|
2680
|
+
text,
|
|
2681
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
2682
|
+
usage,
|
|
2683
|
+
finishReason
|
|
2684
|
+
};
|
|
2685
|
+
if (model) {
|
|
2686
|
+
summary.model = model;
|
|
2687
|
+
}
|
|
2688
|
+
onComplete(summary);
|
|
2689
|
+
};
|
|
2690
|
+
return new TransformStream({
|
|
2691
|
+
transform(part, controller) {
|
|
2692
|
+
try {
|
|
2693
|
+
if (part?.type === "text-delta") {
|
|
2694
|
+
text += part.delta ?? part.textDelta ?? "";
|
|
2695
|
+
} else if (part?.type === "tool-call") {
|
|
2696
|
+
toolCalls.push({
|
|
2697
|
+
toolCallId: part.toolCallId,
|
|
2698
|
+
toolName: part.toolName,
|
|
2699
|
+
input: part.input ?? part.args
|
|
2700
|
+
});
|
|
2701
|
+
} else if (part?.type === "finish") {
|
|
2702
|
+
usage = part.usage;
|
|
2703
|
+
finishReason = part.finishReason;
|
|
2704
|
+
complete();
|
|
2705
|
+
}
|
|
2706
|
+
} catch {
|
|
2707
|
+
}
|
|
2708
|
+
controller.enqueue(part);
|
|
2709
|
+
},
|
|
2710
|
+
flush() {
|
|
2711
|
+
complete();
|
|
2712
|
+
}
|
|
2713
|
+
});
|
|
2714
|
+
}
|
|
2715
|
+
var BitfabVercelAiHandler = class {
|
|
2716
|
+
constructor(config) {
|
|
2717
|
+
this.traceFunctionKey = config.traceFunctionKey;
|
|
2718
|
+
this.withSpanFn = config.withSpan;
|
|
2719
|
+
}
|
|
2720
|
+
/** The `wrapLanguageModel` middleware object for this trace function key. */
|
|
2721
|
+
get middleware() {
|
|
2722
|
+
const key = this.traceFunctionKey;
|
|
2723
|
+
const withSpan = this.withSpanFn;
|
|
2724
|
+
return {
|
|
2725
|
+
specificationVersion: "v3",
|
|
2726
|
+
wrapGenerate: async ({ doGenerate, params, model }) => {
|
|
2727
|
+
const label = modelLabel(model);
|
|
2728
|
+
const traced = withSpan(
|
|
2729
|
+
key,
|
|
2730
|
+
{
|
|
2731
|
+
type: "llm",
|
|
2732
|
+
finalize: (result) => summarizeGenerate(result ?? {}, label)
|
|
2733
|
+
},
|
|
2734
|
+
() => doGenerate()
|
|
2735
|
+
);
|
|
2736
|
+
return traced(params);
|
|
2737
|
+
},
|
|
2738
|
+
wrapStream: async ({ doStream, params, model }) => {
|
|
2739
|
+
const label = modelLabel(model);
|
|
2740
|
+
let resolveSummary = () => {
|
|
2741
|
+
};
|
|
2742
|
+
const summary = new Promise((resolve) => {
|
|
2743
|
+
resolveSummary = resolve;
|
|
2744
|
+
});
|
|
2745
|
+
const traced = withSpan(
|
|
2746
|
+
key,
|
|
2747
|
+
// The wrapped fn returns immediately with the live stream, so the span
|
|
2748
|
+
// output cannot be read from the return value. `finalize` instead
|
|
2749
|
+
// awaits the summary the accumulator resolves once the stream drains.
|
|
2750
|
+
{ type: "llm", finalize: () => summary },
|
|
2751
|
+
async () => {
|
|
2752
|
+
const result = await doStream();
|
|
2753
|
+
const stream = result.stream.pipeThrough(
|
|
2754
|
+
accumulateStream(resolveSummary, label)
|
|
2755
|
+
);
|
|
2756
|
+
return { ...result, stream };
|
|
2757
|
+
}
|
|
2758
|
+
);
|
|
2759
|
+
return traced(params);
|
|
2760
|
+
}
|
|
2761
|
+
};
|
|
2762
|
+
}
|
|
2763
|
+
};
|
|
2764
|
+
|
|
2622
2765
|
// src/client.ts
|
|
2623
2766
|
var activeTraceStates = /* @__PURE__ */ new Map();
|
|
2624
2767
|
var pendingSpanPromises = /* @__PURE__ */ new Map();
|
|
@@ -2718,7 +2861,10 @@ async function loadCollectorClass() {
|
|
|
2718
2861
|
return cachedCollectorClass;
|
|
2719
2862
|
}
|
|
2720
2863
|
try {
|
|
2721
|
-
const baml = await
|
|
2864
|
+
const baml = await importOptionalPeer([
|
|
2865
|
+
"@boundaryml",
|
|
2866
|
+
"baml"
|
|
2867
|
+
]);
|
|
2722
2868
|
cachedCollectorClass = baml.Collector;
|
|
2723
2869
|
return cachedCollectorClass;
|
|
2724
2870
|
} catch {
|
|
@@ -3134,6 +3280,36 @@ var Bitfab = class {
|
|
|
3134
3280
|
}
|
|
3135
3281
|
});
|
|
3136
3282
|
}
|
|
3283
|
+
/**
|
|
3284
|
+
* Get a Vercel AI SDK language-model middleware for tracing.
|
|
3285
|
+
*
|
|
3286
|
+
* Pass it to the AI SDK's `wrapLanguageModel` and use the wrapped model with
|
|
3287
|
+
* `generateText` / `streamText` / `generateObject` / `streamObject`. Every
|
|
3288
|
+
* call through that model is captured as a keyed `llm` span carrying the call
|
|
3289
|
+
* parameters (the prompt) as input and a serializable summary
|
|
3290
|
+
* (`{ text, toolCalls, usage, finishReason }`) as output. Streaming is
|
|
3291
|
+
* captured without disturbing the caller's live stream.
|
|
3292
|
+
*
|
|
3293
|
+
* ```typescript
|
|
3294
|
+
* import { wrapLanguageModel, streamText } from "ai";
|
|
3295
|
+
* import { openai } from "@ai-sdk/openai";
|
|
3296
|
+
*
|
|
3297
|
+
* const model = wrapLanguageModel({
|
|
3298
|
+
* model: openai("gpt-4o"),
|
|
3299
|
+
* middleware: client.getVercelAiMiddleware("chat-turn"),
|
|
3300
|
+
* });
|
|
3301
|
+
* const result = streamText({ model, messages });
|
|
3302
|
+
* ```
|
|
3303
|
+
*
|
|
3304
|
+
* @param traceFunctionKey - Groups traces under this key in Bitfab
|
|
3305
|
+
* @returns A Vercel AI SDK middleware configured for this client
|
|
3306
|
+
*/
|
|
3307
|
+
getVercelAiMiddleware(traceFunctionKey) {
|
|
3308
|
+
return new BitfabVercelAiHandler({
|
|
3309
|
+
traceFunctionKey,
|
|
3310
|
+
withSpan: this.withSpan.bind(this)
|
|
3311
|
+
}).middleware;
|
|
3312
|
+
}
|
|
3137
3313
|
/**
|
|
3138
3314
|
* Wrap a BAML client method to automatically capture prompt and LLM metadata.
|
|
3139
3315
|
*
|
|
@@ -3810,6 +3986,7 @@ assertAsyncStorageRegistered();
|
|
|
3810
3986
|
BitfabLangGraphCallbackHandler,
|
|
3811
3987
|
BitfabOpenAIAgentHandler,
|
|
3812
3988
|
BitfabOpenAITracingProcessor,
|
|
3989
|
+
BitfabVercelAiHandler,
|
|
3813
3990
|
DEFAULT_SERVICE_URL,
|
|
3814
3991
|
ReplayEnvironment,
|
|
3815
3992
|
SUPPORTED_PROVIDERS,
|