braintrust 3.31.0 → 3.32.0
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/dev/dist/index.d.mts +736 -0
- package/dev/dist/index.d.ts +736 -0
- package/dev/dist/index.js +1280 -428
- package/dev/dist/index.mjs +859 -7
- package/dist/apply-auto-instrumentation.js +281 -216
- package/dist/apply-auto-instrumentation.mjs +66 -1
- package/dist/auto-instrumentations/bundler/esbuild.cjs +104 -2
- package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
- package/dist/auto-instrumentations/bundler/next.cjs +104 -2
- package/dist/auto-instrumentations/bundler/next.mjs +3 -3
- package/dist/auto-instrumentations/bundler/rollup.cjs +104 -2
- package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
- package/dist/auto-instrumentations/bundler/vite.cjs +104 -2
- package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +104 -2
- package/dist/auto-instrumentations/bundler/webpack.cjs +104 -2
- package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
- package/dist/auto-instrumentations/{chunk-U64OYU4Q.mjs → chunk-AFND2U7E.mjs} +38 -1
- package/dist/auto-instrumentations/{chunk-OXINGIEZ.mjs → chunk-QEEPRBIS.mjs} +1 -1
- package/dist/auto-instrumentations/{chunk-LW4HDHXT.mjs → chunk-RI4Y55ZF.mjs} +69 -2
- package/dist/auto-instrumentations/hook.mjs +124 -3
- package/dist/auto-instrumentations/index.cjs +39 -1
- package/dist/auto-instrumentations/index.d.mts +3 -1
- package/dist/auto-instrumentations/index.d.ts +3 -1
- package/dist/auto-instrumentations/index.mjs +3 -1
- package/dist/browser.d.mts +2124 -2
- package/dist/browser.d.ts +2124 -2
- package/dist/browser.js +994 -10
- package/dist/browser.mjs +994 -10
- package/dist/{chunk-AJT4FR25.mjs → chunk-4QSAHP7D.mjs} +818 -5
- package/dist/{chunk-4AQGZS6X.js → chunk-AW4QECG5.js} +47 -4
- package/dist/{chunk-V32LW47Z.js → chunk-BTRLPQG5.js} +1663 -850
- package/dist/{chunk-CE3BLB2L.mjs → chunk-WV6QZI2W.mjs} +46 -3
- package/dist/cli.js +923 -8
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +994 -10
- package/dist/edge-light.mjs +994 -10
- package/dist/index.d.mts +2124 -2
- package/dist/index.d.ts +2124 -2
- package/dist/index.js +581 -447
- package/dist/index.mjs +136 -2
- package/dist/instrumentation/index.d.mts +434 -0
- package/dist/instrumentation/index.d.ts +434 -0
- package/dist/instrumentation/index.js +859 -7
- package/dist/instrumentation/index.mjs +859 -7
- package/dist/vitest-evals-reporter.js +16 -16
- package/dist/vitest-evals-reporter.mjs +2 -2
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +994 -10
- package/dist/workerd.mjs +994 -10
- package/package.json +1 -1
- package/util/dist/index.d.mts +326 -0
- package/util/dist/index.d.ts +326 -0
package/dist/edge-light.mjs
CHANGED
|
@@ -2815,6 +2815,14 @@ var ChatCompletionContentPartImageWithTitle = z6.object({
|
|
|
2815
2815
|
ttl: z6.enum(["5m", "1h"]).optional()
|
|
2816
2816
|
}).optional()
|
|
2817
2817
|
});
|
|
2818
|
+
var ChatCompletionContentPartInputAudioWithTitle = z6.object({
|
|
2819
|
+
input_audio: z6.object({ data: z6.string(), format: z6.enum(["wav", "mp3"]) }),
|
|
2820
|
+
type: z6.literal("input_audio"),
|
|
2821
|
+
cache_control: z6.object({
|
|
2822
|
+
type: z6.literal("ephemeral"),
|
|
2823
|
+
ttl: z6.enum(["5m", "1h"]).optional()
|
|
2824
|
+
}).optional()
|
|
2825
|
+
});
|
|
2818
2826
|
var ChatCompletionContentPartFileFile = z6.object({ file_data: z6.string(), filename: z6.string(), file_id: z6.string() }).partial();
|
|
2819
2827
|
var ChatCompletionContentPartFileWithTitle = z6.object({
|
|
2820
2828
|
file: ChatCompletionContentPartFileFile,
|
|
@@ -2827,6 +2835,7 @@ var ChatCompletionContentPartFileWithTitle = z6.object({
|
|
|
2827
2835
|
var ChatCompletionContentPart = z6.union([
|
|
2828
2836
|
ChatCompletionContentPartTextWithTitle,
|
|
2829
2837
|
ChatCompletionContentPartImageWithTitle,
|
|
2838
|
+
ChatCompletionContentPartInputAudioWithTitle,
|
|
2830
2839
|
ChatCompletionContentPartFileWithTitle
|
|
2831
2840
|
]);
|
|
2832
2841
|
var ChatCompletionContentPartText = z6.object({
|
|
@@ -5654,6 +5663,7 @@ var INSTRUMENTATION_NAMES = {
|
|
|
5654
5663
|
GENKIT: "genkit",
|
|
5655
5664
|
GITHUB_COPILOT: "github-copilot",
|
|
5656
5665
|
GOOGLE_ADK: "google-adk",
|
|
5666
|
+
GOOGLE_GENERATIVE_AI: "google-generative-ai",
|
|
5657
5667
|
GOOGLE_GENAI: "google-genai",
|
|
5658
5668
|
GROQ: "groq",
|
|
5659
5669
|
HUGGINGFACE: "huggingface",
|
|
@@ -5669,12 +5679,13 @@ var INSTRUMENTATION_NAMES = {
|
|
|
5669
5679
|
OPENROUTER_AGENT: "openrouter-agent",
|
|
5670
5680
|
PI_CODING_AGENT: "pi-coding-agent",
|
|
5671
5681
|
STRANDS_AGENT_SDK: "strands-agent-sdk",
|
|
5672
|
-
VOYAGEAI: "voyageai"
|
|
5682
|
+
VOYAGEAI: "voyageai",
|
|
5683
|
+
ELEVENLABS: "elevenlabs"
|
|
5673
5684
|
};
|
|
5674
5685
|
var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
|
|
5675
5686
|
"braintrust.spanInstrumentationName"
|
|
5676
5687
|
);
|
|
5677
|
-
var SDK_VERSION = true ? "3.
|
|
5688
|
+
var SDK_VERSION = true ? "3.32.0" : "0.0.0";
|
|
5678
5689
|
function withSpanInstrumentationName(args, instrumentationName) {
|
|
5679
5690
|
return {
|
|
5680
5691
|
...args,
|
|
@@ -11632,6 +11643,16 @@ function renderMessageImpl(render, message, variables) {
|
|
|
11632
11643
|
}
|
|
11633
11644
|
}
|
|
11634
11645
|
];
|
|
11646
|
+
case "input_audio":
|
|
11647
|
+
return [
|
|
11648
|
+
{
|
|
11649
|
+
...c,
|
|
11650
|
+
input_audio: {
|
|
11651
|
+
...c.input_audio,
|
|
11652
|
+
data: render(c.input_audio.data)
|
|
11653
|
+
}
|
|
11654
|
+
}
|
|
11655
|
+
];
|
|
11635
11656
|
case "file":
|
|
11636
11657
|
return [
|
|
11637
11658
|
{
|
|
@@ -24266,6 +24287,342 @@ var OpenAIAgentsPlugin = class extends BasePlugin {
|
|
|
24266
24287
|
}
|
|
24267
24288
|
};
|
|
24268
24289
|
|
|
24290
|
+
// src/instrumentation/plugins/google-generative-ai-channels.ts
|
|
24291
|
+
var googleGenerativeAIChannels = defineChannels(
|
|
24292
|
+
"@google/generative-ai",
|
|
24293
|
+
{
|
|
24294
|
+
generateContent: channel({ channelName: "GenerativeModel.generateContent", kind: "async" }),
|
|
24295
|
+
generateContentStream: channel({ channelName: "GenerativeModel.generateContentStream", kind: "async" }),
|
|
24296
|
+
embedContent: channel({ channelName: "GenerativeModel.embedContent", kind: "async" }),
|
|
24297
|
+
batchEmbedContents: channel({ channelName: "GenerativeModel.batchEmbedContents", kind: "async" }),
|
|
24298
|
+
sendMessage: channel({ channelName: "ChatSession.sendMessage", kind: "async" }),
|
|
24299
|
+
sendMessageStream: channel({ channelName: "ChatSession.sendMessageStream", kind: "async" })
|
|
24300
|
+
},
|
|
24301
|
+
{ instrumentationName: INSTRUMENTATION_NAMES.GOOGLE_GENERATIVE_AI }
|
|
24302
|
+
);
|
|
24303
|
+
|
|
24304
|
+
// src/instrumentation/plugins/google-generative-ai-plugin.ts
|
|
24305
|
+
var GENERATION_CONFIG_KEYS = [
|
|
24306
|
+
"temperature",
|
|
24307
|
+
"topP",
|
|
24308
|
+
"topK",
|
|
24309
|
+
"candidateCount",
|
|
24310
|
+
"maxOutputTokens",
|
|
24311
|
+
"stopSequences",
|
|
24312
|
+
"responseMimeType",
|
|
24313
|
+
"responseSchema",
|
|
24314
|
+
"presencePenalty",
|
|
24315
|
+
"frequencyPenalty",
|
|
24316
|
+
"responseLogprobs",
|
|
24317
|
+
"logprobs",
|
|
24318
|
+
"thinkingConfig"
|
|
24319
|
+
];
|
|
24320
|
+
var GoogleGenerativeAIPlugin = class extends BasePlugin {
|
|
24321
|
+
onEnable() {
|
|
24322
|
+
this.unsubscribers.push(
|
|
24323
|
+
interceptCall(
|
|
24324
|
+
googleGenerativeAIChannels.generateContent,
|
|
24325
|
+
"generateContent"
|
|
24326
|
+
),
|
|
24327
|
+
interceptCall(
|
|
24328
|
+
googleGenerativeAIChannels.generateContentStream,
|
|
24329
|
+
"generateContentStream"
|
|
24330
|
+
),
|
|
24331
|
+
interceptCall(googleGenerativeAIChannels.sendMessage, "sendMessage"),
|
|
24332
|
+
interceptCall(
|
|
24333
|
+
googleGenerativeAIChannels.sendMessageStream,
|
|
24334
|
+
"sendMessageStream"
|
|
24335
|
+
),
|
|
24336
|
+
interceptCall(googleGenerativeAIChannels.embedContent, "embedContent"),
|
|
24337
|
+
interceptCall(
|
|
24338
|
+
googleGenerativeAIChannels.batchEmbedContents,
|
|
24339
|
+
"batchEmbedContents"
|
|
24340
|
+
)
|
|
24341
|
+
);
|
|
24342
|
+
}
|
|
24343
|
+
onDisable() {
|
|
24344
|
+
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
24345
|
+
}
|
|
24346
|
+
};
|
|
24347
|
+
function interceptCall(channel2, operation) {
|
|
24348
|
+
return channel2.intercept((target, thisArg, args) => {
|
|
24349
|
+
const invokeTarget = () => Reflect.apply(target, thisArg, args);
|
|
24350
|
+
if (isAutoInstrumentationSuppressed()) return invokeTarget();
|
|
24351
|
+
const self = thisArg;
|
|
24352
|
+
const chat = operation.startsWith("sendMessage");
|
|
24353
|
+
const embedding = operation === "embedContent" || operation === "batchEmbedContents";
|
|
24354
|
+
const start = getCurrentUnixTimestamp();
|
|
24355
|
+
let span;
|
|
24356
|
+
try {
|
|
24357
|
+
span = startSpan(
|
|
24358
|
+
withSpanInstrumentationName(
|
|
24359
|
+
{
|
|
24360
|
+
name: embedding ? operation === "embedContent" ? "embed_content" : "batch_embed_contents" : "generate_content",
|
|
24361
|
+
spanAttributes: { type: "llm" /* LLM */ },
|
|
24362
|
+
event: extractInput(self, args[0], operation)
|
|
24363
|
+
},
|
|
24364
|
+
INSTRUMENTATION_NAMES.GOOGLE_GENERATIVE_AI
|
|
24365
|
+
)
|
|
24366
|
+
);
|
|
24367
|
+
} catch (error) {
|
|
24368
|
+
debugLogger.error("Error starting Google Generative AI span:", error);
|
|
24369
|
+
return invokeTarget();
|
|
24370
|
+
}
|
|
24371
|
+
let ended = false;
|
|
24372
|
+
const finish = (log3) => {
|
|
24373
|
+
if (ended) return;
|
|
24374
|
+
ended = true;
|
|
24375
|
+
try {
|
|
24376
|
+
log3();
|
|
24377
|
+
} catch (error) {
|
|
24378
|
+
debugLogger.error("Error logging Google Generative AI span:", error);
|
|
24379
|
+
}
|
|
24380
|
+
try {
|
|
24381
|
+
span.end();
|
|
24382
|
+
} catch (error) {
|
|
24383
|
+
debugLogger.error("Error ending Google Generative AI span:", error);
|
|
24384
|
+
}
|
|
24385
|
+
};
|
|
24386
|
+
if (chat) {
|
|
24387
|
+
try {
|
|
24388
|
+
void self._sendPromise.then(
|
|
24389
|
+
() => {
|
|
24390
|
+
try {
|
|
24391
|
+
span.log(extractInput(self, args[0], operation));
|
|
24392
|
+
} catch (error) {
|
|
24393
|
+
debugLogger.error("Error capturing Google chat history:", error);
|
|
24394
|
+
}
|
|
24395
|
+
},
|
|
24396
|
+
() => {
|
|
24397
|
+
}
|
|
24398
|
+
);
|
|
24399
|
+
} catch (error) {
|
|
24400
|
+
debugLogger.error("Error observing Google chat history:", error);
|
|
24401
|
+
}
|
|
24402
|
+
}
|
|
24403
|
+
let result;
|
|
24404
|
+
try {
|
|
24405
|
+
result = withCurrent(
|
|
24406
|
+
span,
|
|
24407
|
+
() => runWithAutoInstrumentationSuppressed(invokeTarget)
|
|
24408
|
+
);
|
|
24409
|
+
} catch (error) {
|
|
24410
|
+
finish(() => span.log({ error }));
|
|
24411
|
+
throw error;
|
|
24412
|
+
}
|
|
24413
|
+
void Promise.resolve(result).then(
|
|
24414
|
+
(value) => {
|
|
24415
|
+
try {
|
|
24416
|
+
if ("stream" in value) {
|
|
24417
|
+
let firstToken = false;
|
|
24418
|
+
const partial = { candidates: [] };
|
|
24419
|
+
const candidates = /* @__PURE__ */ new Map();
|
|
24420
|
+
patchStreamIfNeeded(value.stream, {
|
|
24421
|
+
aroundNext: (callback) => withCurrent(span, callback),
|
|
24422
|
+
onChunk: (chunk) => {
|
|
24423
|
+
for (const candidate of chunk.candidates ?? []) {
|
|
24424
|
+
const index = candidate.index ?? 0;
|
|
24425
|
+
const previous = candidates.get(index);
|
|
24426
|
+
const parts = [...previous?.content?.parts ?? []];
|
|
24427
|
+
for (const part of candidate.content?.parts ?? []) {
|
|
24428
|
+
const last = parts[parts.length - 1];
|
|
24429
|
+
if (part.text !== void 0 && last?.text !== void 0) {
|
|
24430
|
+
parts[parts.length - 1] = {
|
|
24431
|
+
...last,
|
|
24432
|
+
text: last.text + part.text
|
|
24433
|
+
};
|
|
24434
|
+
} else parts.push(part);
|
|
24435
|
+
}
|
|
24436
|
+
candidates.set(index, {
|
|
24437
|
+
...previous,
|
|
24438
|
+
...candidate,
|
|
24439
|
+
content: {
|
|
24440
|
+
role: candidate.content?.role ?? previous?.content?.role ?? "model",
|
|
24441
|
+
parts
|
|
24442
|
+
}
|
|
24443
|
+
});
|
|
24444
|
+
}
|
|
24445
|
+
partial.candidates = Array.from(candidates.values());
|
|
24446
|
+
if (chunk.usageMetadata)
|
|
24447
|
+
partial.usageMetadata = chunk.usageMetadata;
|
|
24448
|
+
if (!firstToken && chunk.candidates?.some(
|
|
24449
|
+
(candidate) => candidate.content?.parts.some(
|
|
24450
|
+
(part) => part.text !== void 0 ? part.text.length > 0 : Object.keys(part).length > 0
|
|
24451
|
+
)
|
|
24452
|
+
)) {
|
|
24453
|
+
firstToken = true;
|
|
24454
|
+
span.log({
|
|
24455
|
+
metrics: {
|
|
24456
|
+
time_to_first_token: getCurrentUnixTimestamp() - start
|
|
24457
|
+
}
|
|
24458
|
+
});
|
|
24459
|
+
}
|
|
24460
|
+
},
|
|
24461
|
+
onComplete: () => {
|
|
24462
|
+
},
|
|
24463
|
+
onError: (error) => finish(() => {
|
|
24464
|
+
logResponse(span, partial);
|
|
24465
|
+
span.log({ error });
|
|
24466
|
+
}),
|
|
24467
|
+
onCancel: () => finish(() => logResponse(span, partial))
|
|
24468
|
+
});
|
|
24469
|
+
void value.response.then(
|
|
24470
|
+
(response) => finish(() => logResponse(span, response)),
|
|
24471
|
+
(error) => finish(() => {
|
|
24472
|
+
logResponse(span, partial);
|
|
24473
|
+
span.log({ error });
|
|
24474
|
+
})
|
|
24475
|
+
);
|
|
24476
|
+
} else if ("response" in value) {
|
|
24477
|
+
finish(() => logResponse(span, value.response));
|
|
24478
|
+
} else {
|
|
24479
|
+
const metrics = {};
|
|
24480
|
+
const promptTokens = value.usageMetadata?.promptTokenCount;
|
|
24481
|
+
if (typeof promptTokens === "number" && Number.isFinite(promptTokens) && promptTokens >= 0) {
|
|
24482
|
+
metrics.prompt_tokens = promptTokens;
|
|
24483
|
+
metrics.tokens = promptTokens;
|
|
24484
|
+
}
|
|
24485
|
+
for (const detail of value.usageMetadata?.promptTokenDetails ?? []) {
|
|
24486
|
+
if (detail.modality === "AUDIO" && typeof detail.tokenCount === "number" && Number.isFinite(detail.tokenCount) && detail.tokenCount >= 0) {
|
|
24487
|
+
metrics.prompt_audio_tokens = (metrics.prompt_audio_tokens ?? 0) + detail.tokenCount;
|
|
24488
|
+
}
|
|
24489
|
+
}
|
|
24490
|
+
finish(
|
|
24491
|
+
() => span.log({
|
|
24492
|
+
metrics,
|
|
24493
|
+
output: {
|
|
24494
|
+
count: value.embeddings?.length ?? (value.embedding ? 1 : 0)
|
|
24495
|
+
}
|
|
24496
|
+
})
|
|
24497
|
+
);
|
|
24498
|
+
}
|
|
24499
|
+
} catch (error) {
|
|
24500
|
+
debugLogger.error(
|
|
24501
|
+
"Error observing Google Generative AI result:",
|
|
24502
|
+
error
|
|
24503
|
+
);
|
|
24504
|
+
finish(() => {
|
|
24505
|
+
});
|
|
24506
|
+
}
|
|
24507
|
+
},
|
|
24508
|
+
(error) => finish(() => span.log({ error }))
|
|
24509
|
+
);
|
|
24510
|
+
return result;
|
|
24511
|
+
});
|
|
24512
|
+
}
|
|
24513
|
+
function normalizeContent(message) {
|
|
24514
|
+
const parts = (typeof message === "string" ? [message] : message).map(
|
|
24515
|
+
(part) => typeof part === "string" ? { text: part } : part
|
|
24516
|
+
);
|
|
24517
|
+
return {
|
|
24518
|
+
role: parts.some((part) => part.functionResponse) ? "function" : "user",
|
|
24519
|
+
parts
|
|
24520
|
+
};
|
|
24521
|
+
}
|
|
24522
|
+
function extractInput(self, request, operation) {
|
|
24523
|
+
const model = self.model.replace(/^models\//, "");
|
|
24524
|
+
if (operation === "embedContent" || operation === "batchEmbedContents") {
|
|
24525
|
+
const requests = operation === "batchEmbedContents" ? request.requests : [
|
|
24526
|
+
typeof request === "string" || Array.isArray(request) ? { content: normalizeContent(request) } : request
|
|
24527
|
+
];
|
|
24528
|
+
const dimensions = requests[0]?.outputDimensionality;
|
|
24529
|
+
return {
|
|
24530
|
+
input: {
|
|
24531
|
+
inputs: convertAttachments(requests).map((item) => ({
|
|
24532
|
+
content: item.content.parts.length === 1 && item.content.parts[0].text !== void 0 ? item.content.parts[0].text : item.content.parts.map((part) => {
|
|
24533
|
+
if (part.text !== void 0)
|
|
24534
|
+
return { type: "text", text: part.text };
|
|
24535
|
+
const mimeType = part.inlineData?.mimeType ?? part.fileData?.mimeType;
|
|
24536
|
+
const data = part.inlineData ? typeof part.inlineData.data === "string" ? `data:${mimeType};base64,${part.inlineData.data}` : part.inlineData.data : part.fileData?.fileUri;
|
|
24537
|
+
return mimeType?.startsWith("image/") ? { type: "image_url", image_url: { url: data } } : { type: "file", file: { file_data: data } };
|
|
24538
|
+
})
|
|
24539
|
+
})),
|
|
24540
|
+
...dimensions !== void 0 && requests.every((item) => item.outputDimensionality === dimensions) ? { output_dimensions: dimensions } : {}
|
|
24541
|
+
},
|
|
24542
|
+
metadata: { model, provider: "google" }
|
|
24543
|
+
};
|
|
24544
|
+
}
|
|
24545
|
+
const chat = operation.startsWith("sendMessage");
|
|
24546
|
+
const config = chat ? self.params ?? {} : self;
|
|
24547
|
+
const params = typeof request === "string" || Array.isArray(request) ? { contents: [normalizeContent(request)] } : request;
|
|
24548
|
+
const system = params.systemInstruction ?? config.systemInstruction;
|
|
24549
|
+
const systemContent = typeof system === "string" ? { role: "system", parts: [{ text: system }] } : system && "parts" in system ? { ...system, role: "system" } : system ? { role: "system", parts: [system] } : void 0;
|
|
24550
|
+
const contents = [
|
|
24551
|
+
...systemContent ? [systemContent] : [],
|
|
24552
|
+
...chat ? self._history : [],
|
|
24553
|
+
...params.contents
|
|
24554
|
+
];
|
|
24555
|
+
const metadata = { model, provider: "google" };
|
|
24556
|
+
const generation = params.generationConfig ?? config.generationConfig;
|
|
24557
|
+
for (const key of GENERATION_CONFIG_KEYS) {
|
|
24558
|
+
if (generation && Object.hasOwn(generation, key))
|
|
24559
|
+
metadata[key] = generation[key];
|
|
24560
|
+
}
|
|
24561
|
+
for (const key of ["tools", "toolConfig", "safetySettings"]) {
|
|
24562
|
+
const value = params[key] ?? config[key];
|
|
24563
|
+
if (value !== void 0) metadata[key] = value;
|
|
24564
|
+
}
|
|
24565
|
+
const cached = params.cachedContent ?? config.cachedContent;
|
|
24566
|
+
if (cached)
|
|
24567
|
+
metadata.cachedContent = typeof cached === "string" ? cached : cached.name;
|
|
24568
|
+
return { input: convertAttachments({ model, contents }), metadata };
|
|
24569
|
+
}
|
|
24570
|
+
function convertAttachments(value) {
|
|
24571
|
+
const convert = (node) => {
|
|
24572
|
+
if (Array.isArray(node)) return node.map(convert);
|
|
24573
|
+
if (!isObject(node)) return node;
|
|
24574
|
+
if (isObject(node.inlineData)) {
|
|
24575
|
+
const { data, mimeType } = node.inlineData;
|
|
24576
|
+
const processed = processInputAttachments({
|
|
24577
|
+
type: "file",
|
|
24578
|
+
file: { file_data: `data:${mimeType};base64,${data}` }
|
|
24579
|
+
});
|
|
24580
|
+
if (typeof processed.file.file_data === "string")
|
|
24581
|
+
throw new Error("Unable to convert Google inline data");
|
|
24582
|
+
return {
|
|
24583
|
+
...node,
|
|
24584
|
+
inlineData: { ...node.inlineData, data: processed.file.file_data }
|
|
24585
|
+
};
|
|
24586
|
+
}
|
|
24587
|
+
return Object.fromEntries(
|
|
24588
|
+
Object.entries(node).map(([key, item]) => [key, convert(item)])
|
|
24589
|
+
);
|
|
24590
|
+
};
|
|
24591
|
+
try {
|
|
24592
|
+
return convert(value);
|
|
24593
|
+
} catch (error) {
|
|
24594
|
+
debugLogger.error(
|
|
24595
|
+
"Error converting Google Generative AI attachments:",
|
|
24596
|
+
error
|
|
24597
|
+
);
|
|
24598
|
+
return value;
|
|
24599
|
+
}
|
|
24600
|
+
}
|
|
24601
|
+
function logResponse(span, response) {
|
|
24602
|
+
const usage = response.usageMetadata;
|
|
24603
|
+
const metrics = {};
|
|
24604
|
+
if (usage) {
|
|
24605
|
+
if (usage.promptTokenCount !== void 0)
|
|
24606
|
+
metrics.prompt_tokens = usage.promptTokenCount;
|
|
24607
|
+
if (usage.candidatesTokenCount !== void 0 || usage.thoughtsTokenCount !== void 0)
|
|
24608
|
+
metrics.completion_tokens = (usage.candidatesTokenCount ?? 0) + (usage.thoughtsTokenCount ?? 0);
|
|
24609
|
+
if (usage.totalTokenCount !== void 0)
|
|
24610
|
+
metrics.tokens = usage.totalTokenCount;
|
|
24611
|
+
if (usage.cachedContentTokenCount !== void 0)
|
|
24612
|
+
metrics.prompt_cached_tokens = usage.cachedContentTokenCount;
|
|
24613
|
+
if (usage.thoughtsTokenCount !== void 0)
|
|
24614
|
+
metrics.completion_reasoning_tokens = usage.thoughtsTokenCount;
|
|
24615
|
+
}
|
|
24616
|
+
span.log({
|
|
24617
|
+
output: convertAttachments({
|
|
24618
|
+
candidates: response.candidates,
|
|
24619
|
+
promptFeedback: response.promptFeedback
|
|
24620
|
+
}),
|
|
24621
|
+
metrics,
|
|
24622
|
+
...response.modelVersion ? { metadata: { model: response.modelVersion } } : {}
|
|
24623
|
+
});
|
|
24624
|
+
}
|
|
24625
|
+
|
|
24269
24626
|
// src/instrumentation/plugins/google-genai-channels.ts
|
|
24270
24627
|
var googleGenAIChannels = defineChannels(
|
|
24271
24628
|
"@google/genai",
|
|
@@ -25909,7 +26266,7 @@ var HuggingFaceTransformersPlugin = class extends BasePlugin {
|
|
|
25909
26266
|
getTask(event)
|
|
25910
26267
|
),
|
|
25911
26268
|
extractInput: (args, event) => ({
|
|
25912
|
-
input:
|
|
26269
|
+
input: extractInput2(
|
|
25913
26270
|
getTask(event),
|
|
25914
26271
|
args
|
|
25915
26272
|
),
|
|
@@ -25999,7 +26356,7 @@ function modelIdentifier(pipeline) {
|
|
|
25999
26356
|
}
|
|
26000
26357
|
return void 0;
|
|
26001
26358
|
}
|
|
26002
|
-
function
|
|
26359
|
+
function extractInput2(task, args) {
|
|
26003
26360
|
switch (task) {
|
|
26004
26361
|
case "feature-extraction":
|
|
26005
26362
|
return args[0];
|
|
@@ -36353,6 +36710,477 @@ function logInstrumentationError5(context, error) {
|
|
|
36353
36710
|
debugLogger.debug(`${context}:`, error);
|
|
36354
36711
|
}
|
|
36355
36712
|
|
|
36713
|
+
// src/instrumentation/plugins/elevenlabs-channels.ts
|
|
36714
|
+
var elevenLabsChannels = defineChannels(
|
|
36715
|
+
"@elevenlabs/elevenlabs-js",
|
|
36716
|
+
{
|
|
36717
|
+
convert: channel({
|
|
36718
|
+
channelName: "textToSpeech.convert",
|
|
36719
|
+
kind: "async"
|
|
36720
|
+
}),
|
|
36721
|
+
stream: channel({
|
|
36722
|
+
channelName: "textToSpeech.stream",
|
|
36723
|
+
kind: "async"
|
|
36724
|
+
}),
|
|
36725
|
+
convertWithTimestamps: channel({ channelName: "textToSpeech.convertWithTimestamps", kind: "async" }),
|
|
36726
|
+
streamWithTimestamps: channel({ channelName: "textToSpeech.streamWithTimestamps", kind: "async" }),
|
|
36727
|
+
transcribe: channel({ channelName: "speechToText.convert", kind: "async" })
|
|
36728
|
+
},
|
|
36729
|
+
{ instrumentationName: INSTRUMENTATION_NAMES.ELEVENLABS }
|
|
36730
|
+
);
|
|
36731
|
+
|
|
36732
|
+
// src/instrumentation/plugins/elevenlabs-plugin.ts
|
|
36733
|
+
var ElevenLabsPlugin = class extends BasePlugin {
|
|
36734
|
+
onEnable() {
|
|
36735
|
+
for (const method of [
|
|
36736
|
+
"convert",
|
|
36737
|
+
"stream",
|
|
36738
|
+
"convertWithTimestamps",
|
|
36739
|
+
"streamWithTimestamps"
|
|
36740
|
+
]) {
|
|
36741
|
+
this.unsubscribers.push(
|
|
36742
|
+
interceptCall2(
|
|
36743
|
+
elevenLabsChannels[method],
|
|
36744
|
+
`elevenlabs.textToSpeech.${method}`,
|
|
36745
|
+
([voice, request]) => ({
|
|
36746
|
+
input: {
|
|
36747
|
+
operation: "speech",
|
|
36748
|
+
prompt: request.text,
|
|
36749
|
+
parameters: {
|
|
36750
|
+
voice,
|
|
36751
|
+
format: request.outputFormat,
|
|
36752
|
+
language: request.languageCode,
|
|
36753
|
+
speed: request.voiceSettings?.speed
|
|
36754
|
+
}
|
|
36755
|
+
},
|
|
36756
|
+
metadata: {
|
|
36757
|
+
provider: "elevenlabs",
|
|
36758
|
+
model: request.modelId ?? "eleven_multilingual_v2"
|
|
36759
|
+
}
|
|
36760
|
+
}),
|
|
36761
|
+
(value, args, span, finish, headers, started) => captureSpeech(
|
|
36762
|
+
value,
|
|
36763
|
+
args[1],
|
|
36764
|
+
method,
|
|
36765
|
+
span,
|
|
36766
|
+
finish,
|
|
36767
|
+
started,
|
|
36768
|
+
headers
|
|
36769
|
+
)
|
|
36770
|
+
)
|
|
36771
|
+
);
|
|
36772
|
+
}
|
|
36773
|
+
this.unsubscribers.push(
|
|
36774
|
+
interceptCall2(
|
|
36775
|
+
elevenLabsChannels.transcribe,
|
|
36776
|
+
"elevenlabs.speechToText.convert",
|
|
36777
|
+
([request]) => {
|
|
36778
|
+
if (request.webhook) return void 0;
|
|
36779
|
+
const file = request.file;
|
|
36780
|
+
const filename = typeof File !== "undefined" && file instanceof File ? file.name : "audio";
|
|
36781
|
+
const contentType = file instanceof Blob ? file.type || "application/octet-stream" : "application/octet-stream";
|
|
36782
|
+
const blob = file instanceof Blob ? file : file instanceof Uint8Array ? new Blob([new Uint8Array(file)], { type: contentType }) : file instanceof ArrayBuffer ? new Blob([file.slice(0)], { type: contentType }) : void 0;
|
|
36783
|
+
const fileData = blob ? new Attachment({ data: blob, filename, contentType }) : request.cloudStorageUrl;
|
|
36784
|
+
return {
|
|
36785
|
+
input: {
|
|
36786
|
+
operation: "transcribe",
|
|
36787
|
+
content: fileData ? [{ type: "file", file: { filename, file_data: fileData } }] : [],
|
|
36788
|
+
parameters: {
|
|
36789
|
+
language: request.languageCode,
|
|
36790
|
+
timestamp_granularities: request.timestampsGranularity
|
|
36791
|
+
}
|
|
36792
|
+
},
|
|
36793
|
+
metadata: { provider: "elevenlabs", model: request.modelId }
|
|
36794
|
+
};
|
|
36795
|
+
},
|
|
36796
|
+
(value, _args, span, finish) => {
|
|
36797
|
+
const result = value;
|
|
36798
|
+
const transcripts = result.transcripts ?? [result];
|
|
36799
|
+
span.log({
|
|
36800
|
+
output: {
|
|
36801
|
+
content: transcripts.flatMap(
|
|
36802
|
+
(transcript) => typeof transcript.text === "string" ? [{ type: "text", text: transcript.text }] : []
|
|
36803
|
+
),
|
|
36804
|
+
annotations: {
|
|
36805
|
+
language: result.languageCode,
|
|
36806
|
+
words: result.words ?? (result.transcripts ? result.transcripts.flatMap(
|
|
36807
|
+
(transcript) => transcript.words ?? []
|
|
36808
|
+
) : void 0)
|
|
36809
|
+
}
|
|
36810
|
+
}
|
|
36811
|
+
});
|
|
36812
|
+
finish();
|
|
36813
|
+
},
|
|
36814
|
+
([request], span) => {
|
|
36815
|
+
const file = request.file;
|
|
36816
|
+
if (!isAsyncIterable(file)) return;
|
|
36817
|
+
const chunks = [];
|
|
36818
|
+
const filename = isObject(file) && typeof file.path === "string" ? file.path.split(/[\\/]/).pop() || "audio" : "audio";
|
|
36819
|
+
observeAudioStream(
|
|
36820
|
+
file,
|
|
36821
|
+
(chunk) => chunks.push(new Uint8Array(chunk)),
|
|
36822
|
+
() => {
|
|
36823
|
+
const contentType = "application/octet-stream";
|
|
36824
|
+
const data = new Blob(chunks, {
|
|
36825
|
+
type: contentType
|
|
36826
|
+
});
|
|
36827
|
+
chunks.length = 0;
|
|
36828
|
+
span.log({
|
|
36829
|
+
input: {
|
|
36830
|
+
content: [
|
|
36831
|
+
{
|
|
36832
|
+
type: "file",
|
|
36833
|
+
file: {
|
|
36834
|
+
filename,
|
|
36835
|
+
file_data: new Attachment({
|
|
36836
|
+
data,
|
|
36837
|
+
filename,
|
|
36838
|
+
contentType
|
|
36839
|
+
})
|
|
36840
|
+
}
|
|
36841
|
+
}
|
|
36842
|
+
]
|
|
36843
|
+
}
|
|
36844
|
+
});
|
|
36845
|
+
},
|
|
36846
|
+
() => {
|
|
36847
|
+
chunks.length = 0;
|
|
36848
|
+
},
|
|
36849
|
+
span
|
|
36850
|
+
);
|
|
36851
|
+
}
|
|
36852
|
+
)
|
|
36853
|
+
);
|
|
36854
|
+
}
|
|
36855
|
+
onDisable() {
|
|
36856
|
+
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
36857
|
+
}
|
|
36858
|
+
};
|
|
36859
|
+
function interceptCall2(channel2, name, input, output, beforeInvoke) {
|
|
36860
|
+
return channel2.intercept((target, self, args) => {
|
|
36861
|
+
const invoke2 = () => Reflect.apply(target, self, args);
|
|
36862
|
+
if (isAutoInstrumentationSuppressed()) return invoke2();
|
|
36863
|
+
const started = Date.now() / 1e3;
|
|
36864
|
+
let span;
|
|
36865
|
+
try {
|
|
36866
|
+
const event = input(args);
|
|
36867
|
+
if (event !== void 0)
|
|
36868
|
+
span = startSpan(
|
|
36869
|
+
withSpanInstrumentationName(
|
|
36870
|
+
{
|
|
36871
|
+
name,
|
|
36872
|
+
spanAttributes: { type: "llm" /* LLM */ },
|
|
36873
|
+
event
|
|
36874
|
+
},
|
|
36875
|
+
INSTRUMENTATION_NAMES.ELEVENLABS
|
|
36876
|
+
)
|
|
36877
|
+
);
|
|
36878
|
+
} catch (error) {
|
|
36879
|
+
debugLogger.error("Error starting ElevenLabs span", error);
|
|
36880
|
+
return invoke2();
|
|
36881
|
+
}
|
|
36882
|
+
if (!span) return invoke2();
|
|
36883
|
+
const activeSpan = span;
|
|
36884
|
+
let ended = false;
|
|
36885
|
+
const finish = (error) => {
|
|
36886
|
+
if (ended) return;
|
|
36887
|
+
ended = true;
|
|
36888
|
+
try {
|
|
36889
|
+
if (error !== void 0) activeSpan.log({ error });
|
|
36890
|
+
activeSpan.end();
|
|
36891
|
+
} catch (loggingError) {
|
|
36892
|
+
debugLogger.error("Error ending ElevenLabs span", loggingError);
|
|
36893
|
+
}
|
|
36894
|
+
};
|
|
36895
|
+
try {
|
|
36896
|
+
beforeInvoke?.(args, span);
|
|
36897
|
+
} catch (error) {
|
|
36898
|
+
debugLogger.error("Error observing ElevenLabs input", error);
|
|
36899
|
+
}
|
|
36900
|
+
let result;
|
|
36901
|
+
try {
|
|
36902
|
+
result = withCurrent(
|
|
36903
|
+
span,
|
|
36904
|
+
() => runWithAutoInstrumentationSuppressed(invoke2)
|
|
36905
|
+
);
|
|
36906
|
+
} catch (error) {
|
|
36907
|
+
finish(error);
|
|
36908
|
+
throw error;
|
|
36909
|
+
}
|
|
36910
|
+
const capture = (value, headers) => {
|
|
36911
|
+
try {
|
|
36912
|
+
output(value, args, span, finish, headers, started);
|
|
36913
|
+
} catch (error) {
|
|
36914
|
+
debugLogger.error("Error capturing ElevenLabs output", error);
|
|
36915
|
+
finish();
|
|
36916
|
+
}
|
|
36917
|
+
};
|
|
36918
|
+
try {
|
|
36919
|
+
if (isObject(result) && typeof result.withRawResponse === "function") {
|
|
36920
|
+
void result.withRawResponse().then(
|
|
36921
|
+
({
|
|
36922
|
+
data,
|
|
36923
|
+
rawResponse
|
|
36924
|
+
}) => capture(data, rawResponse?.headers),
|
|
36925
|
+
finish
|
|
36926
|
+
);
|
|
36927
|
+
} else {
|
|
36928
|
+
void Promise.resolve(result).then((value) => capture(value), finish);
|
|
36929
|
+
}
|
|
36930
|
+
} catch (error) {
|
|
36931
|
+
debugLogger.error("Error observing ElevenLabs result", error);
|
|
36932
|
+
finish();
|
|
36933
|
+
}
|
|
36934
|
+
return result;
|
|
36935
|
+
});
|
|
36936
|
+
}
|
|
36937
|
+
function captureSpeech(value, request, method, span, finish, started, headers) {
|
|
36938
|
+
const format = request.outputFormat ?? "mp3_44100_128";
|
|
36939
|
+
const formatType = format.startsWith("mp3_") ? "audio/mpeg" : format.startsWith("pcm_") ? "audio/pcm" : format.startsWith("opus_") ? "audio/ogg" : format.startsWith("ulaw_") ? "audio/basic" : format.startsWith("alaw_") ? "audio/x-alaw" : void 0;
|
|
36940
|
+
const headerType = headers?.get("content-type")?.split(";")[0];
|
|
36941
|
+
const contentType = headerType?.startsWith("audio/") ? headerType : formatType;
|
|
36942
|
+
const disposition = headers?.get("content-disposition");
|
|
36943
|
+
const headerFilename = disposition?.match(
|
|
36944
|
+
/filename="([^"]+)"|filename=([^;]+)/i
|
|
36945
|
+
);
|
|
36946
|
+
const filename = headerFilename?.[1] ?? headerFilename?.[2]?.trim() ?? `speech.${contentType ? getExtensionFromMediaType(contentType) : "bin"}`;
|
|
36947
|
+
const chunks = [];
|
|
36948
|
+
const alignments = [];
|
|
36949
|
+
let bytes = 0;
|
|
36950
|
+
let first = true;
|
|
36951
|
+
let stopped = false;
|
|
36952
|
+
const observe = (chunk) => {
|
|
36953
|
+
if (stopped || chunk.byteLength === 0) return;
|
|
36954
|
+
if (first && method.startsWith("stream")) {
|
|
36955
|
+
span.log({
|
|
36956
|
+
metrics: { time_to_first_token: Date.now() / 1e3 - started }
|
|
36957
|
+
});
|
|
36958
|
+
}
|
|
36959
|
+
first = false;
|
|
36960
|
+
chunks.push(new Uint8Array(chunk));
|
|
36961
|
+
bytes += chunk.byteLength;
|
|
36962
|
+
};
|
|
36963
|
+
const complete = () => {
|
|
36964
|
+
if (stopped) return;
|
|
36965
|
+
stopped = true;
|
|
36966
|
+
try {
|
|
36967
|
+
const content = [];
|
|
36968
|
+
if (contentType && bytes) {
|
|
36969
|
+
const data = new Uint8Array(bytes);
|
|
36970
|
+
let offset = 0;
|
|
36971
|
+
for (const chunk of chunks) {
|
|
36972
|
+
data.set(chunk, offset);
|
|
36973
|
+
offset += chunk.length;
|
|
36974
|
+
}
|
|
36975
|
+
content.push({
|
|
36976
|
+
type: "file",
|
|
36977
|
+
file: {
|
|
36978
|
+
filename,
|
|
36979
|
+
byte_size: bytes,
|
|
36980
|
+
file_data: new Attachment({
|
|
36981
|
+
data: new Blob([data], { type: contentType }),
|
|
36982
|
+
filename,
|
|
36983
|
+
contentType
|
|
36984
|
+
})
|
|
36985
|
+
}
|
|
36986
|
+
});
|
|
36987
|
+
}
|
|
36988
|
+
span.log({
|
|
36989
|
+
output: {
|
|
36990
|
+
content,
|
|
36991
|
+
...alignments.length ? { annotations: alignments } : {}
|
|
36992
|
+
}
|
|
36993
|
+
});
|
|
36994
|
+
} finally {
|
|
36995
|
+
chunks.length = 0;
|
|
36996
|
+
finish();
|
|
36997
|
+
}
|
|
36998
|
+
};
|
|
36999
|
+
const cancel = (error) => {
|
|
37000
|
+
stopped = true;
|
|
37001
|
+
chunks.length = 0;
|
|
37002
|
+
finish(error);
|
|
37003
|
+
};
|
|
37004
|
+
const timestampChunk = (chunk) => {
|
|
37005
|
+
const binary = atob(chunk.audioBase64);
|
|
37006
|
+
observe(Uint8Array.from(binary, (character) => character.charCodeAt(0)));
|
|
37007
|
+
if (chunk.alignment || chunk.normalizedAlignment)
|
|
37008
|
+
alignments.push({
|
|
37009
|
+
alignment: chunk.alignment,
|
|
37010
|
+
normalized_alignment: chunk.normalizedAlignment
|
|
37011
|
+
});
|
|
37012
|
+
};
|
|
37013
|
+
if (method === "convertWithTimestamps") {
|
|
37014
|
+
timestampChunk(value);
|
|
37015
|
+
complete();
|
|
37016
|
+
} else if (method === "streamWithTimestamps") {
|
|
37017
|
+
patchStreamIfNeeded(value, {
|
|
37018
|
+
shouldCollect: (chunk) => {
|
|
37019
|
+
try {
|
|
37020
|
+
timestampChunk(chunk);
|
|
37021
|
+
} catch (error) {
|
|
37022
|
+
debugLogger.error("Error collecting ElevenLabs timestamps", error);
|
|
37023
|
+
cancel();
|
|
37024
|
+
}
|
|
37025
|
+
return false;
|
|
37026
|
+
},
|
|
37027
|
+
onComplete: complete,
|
|
37028
|
+
onCancel: () => cancel(),
|
|
37029
|
+
onError: cancel,
|
|
37030
|
+
aroundNext: (next) => withCurrent(span, next)
|
|
37031
|
+
});
|
|
37032
|
+
} else {
|
|
37033
|
+
observeAudioStream(value, observe, complete, cancel, span);
|
|
37034
|
+
}
|
|
37035
|
+
}
|
|
37036
|
+
function observeAudioStream(value, observe, complete, cancel, span) {
|
|
37037
|
+
let ended = false;
|
|
37038
|
+
const safeObserve = (chunk) => {
|
|
37039
|
+
if (ended) return;
|
|
37040
|
+
try {
|
|
37041
|
+
observe(chunk);
|
|
37042
|
+
} catch (error) {
|
|
37043
|
+
debugLogger.error("Error collecting ElevenLabs audio", error);
|
|
37044
|
+
end(false);
|
|
37045
|
+
}
|
|
37046
|
+
};
|
|
37047
|
+
const end = (success, error) => {
|
|
37048
|
+
if (ended) return;
|
|
37049
|
+
ended = true;
|
|
37050
|
+
try {
|
|
37051
|
+
if (success) complete();
|
|
37052
|
+
else cancel(error);
|
|
37053
|
+
} catch (loggingError) {
|
|
37054
|
+
debugLogger.error("Error logging ElevenLabs audio", loggingError);
|
|
37055
|
+
cancel();
|
|
37056
|
+
}
|
|
37057
|
+
};
|
|
37058
|
+
if (!isObject(value) || !Object.isExtensible(value)) {
|
|
37059
|
+
end(false);
|
|
37060
|
+
return;
|
|
37061
|
+
}
|
|
37062
|
+
if (typeof value.read === "function" && typeof value.on === "function") {
|
|
37063
|
+
value.on("end", () => end(true));
|
|
37064
|
+
value.on("close", () => end(false));
|
|
37065
|
+
const emit2 = value.emit;
|
|
37066
|
+
if (typeof emit2 === "function")
|
|
37067
|
+
value.emit = function(event, ...args) {
|
|
37068
|
+
if (event === "data" && args[0] instanceof Uint8Array)
|
|
37069
|
+
safeObserve(args[0]);
|
|
37070
|
+
if (event === "error") end(false, args[0]);
|
|
37071
|
+
return Reflect.apply(emit2, this, [event, ...args]);
|
|
37072
|
+
};
|
|
37073
|
+
return;
|
|
37074
|
+
}
|
|
37075
|
+
if (typeof value.getReader === "function") {
|
|
37076
|
+
const webStream = value;
|
|
37077
|
+
const pipeTo = webStream.pipeTo;
|
|
37078
|
+
const pipeThrough = webStream.pipeThrough;
|
|
37079
|
+
webStream.pipeTo = function(destination, options) {
|
|
37080
|
+
if (!isObject(destination) || this.locked || destination.locked)
|
|
37081
|
+
return pipeTo.call(this, destination, options);
|
|
37082
|
+
const tap = new TransformStream({
|
|
37083
|
+
transform(chunk, controller) {
|
|
37084
|
+
safeObserve(chunk);
|
|
37085
|
+
controller.enqueue(chunk);
|
|
37086
|
+
},
|
|
37087
|
+
flush() {
|
|
37088
|
+
end(true);
|
|
37089
|
+
}
|
|
37090
|
+
});
|
|
37091
|
+
const observed = pipeThrough.call(
|
|
37092
|
+
this,
|
|
37093
|
+
tap,
|
|
37094
|
+
options
|
|
37095
|
+
);
|
|
37096
|
+
return observed.pipeTo(destination, options).catch((error) => {
|
|
37097
|
+
end(false, error);
|
|
37098
|
+
throw error;
|
|
37099
|
+
});
|
|
37100
|
+
};
|
|
37101
|
+
webStream.pipeThrough = function(transform, options) {
|
|
37102
|
+
if (this.locked || transform.writable.locked || transform.readable.locked)
|
|
37103
|
+
return pipeThrough.call(this, transform, options);
|
|
37104
|
+
const result = webStream.pipeTo.call(this, transform.writable, options);
|
|
37105
|
+
void result.catch(() => {
|
|
37106
|
+
});
|
|
37107
|
+
return transform.readable;
|
|
37108
|
+
};
|
|
37109
|
+
const getReader = value.getReader;
|
|
37110
|
+
value.getReader = function(...args) {
|
|
37111
|
+
const reader = Reflect.apply(getReader, this, args);
|
|
37112
|
+
const read3 = reader.read;
|
|
37113
|
+
reader.read = function(...readArgs) {
|
|
37114
|
+
return Promise.resolve(
|
|
37115
|
+
withCurrent(span, () => Reflect.apply(read3, this, readArgs))
|
|
37116
|
+
).then(
|
|
37117
|
+
(result) => {
|
|
37118
|
+
if (result.value) safeObserve(result.value);
|
|
37119
|
+
if (result.done) end(true);
|
|
37120
|
+
return result;
|
|
37121
|
+
},
|
|
37122
|
+
(error) => {
|
|
37123
|
+
end(false, error);
|
|
37124
|
+
throw error;
|
|
37125
|
+
}
|
|
37126
|
+
);
|
|
37127
|
+
};
|
|
37128
|
+
const readerCancel = reader.cancel;
|
|
37129
|
+
reader.cancel = function(...cancelArgs) {
|
|
37130
|
+
const result = Reflect.apply(readerCancel, this, cancelArgs);
|
|
37131
|
+
end(false);
|
|
37132
|
+
return result;
|
|
37133
|
+
};
|
|
37134
|
+
return reader;
|
|
37135
|
+
};
|
|
37136
|
+
const streamCancel = value.cancel;
|
|
37137
|
+
if (typeof streamCancel === "function")
|
|
37138
|
+
value.cancel = function(...args) {
|
|
37139
|
+
const result = Reflect.apply(streamCancel, this, args);
|
|
37140
|
+
void Promise.resolve(result).then(
|
|
37141
|
+
() => end(false),
|
|
37142
|
+
() => {
|
|
37143
|
+
}
|
|
37144
|
+
);
|
|
37145
|
+
return result;
|
|
37146
|
+
};
|
|
37147
|
+
}
|
|
37148
|
+
if (typeof value.getReader === "function" && typeof value.values === "function") {
|
|
37149
|
+
const values = value.values;
|
|
37150
|
+
const iterate = function(...args) {
|
|
37151
|
+
const iterator = Reflect.apply(values, this, args);
|
|
37152
|
+
patchStreamIfNeeded(iterator, {
|
|
37153
|
+
shouldCollect(chunk) {
|
|
37154
|
+
safeObserve(chunk);
|
|
37155
|
+
return false;
|
|
37156
|
+
},
|
|
37157
|
+
onComplete: () => end(true),
|
|
37158
|
+
onCancel: () => end(false),
|
|
37159
|
+
onError: (error) => end(false, error),
|
|
37160
|
+
aroundNext: (next) => withCurrent(span, next)
|
|
37161
|
+
});
|
|
37162
|
+
return iterator;
|
|
37163
|
+
};
|
|
37164
|
+
value.values = iterate;
|
|
37165
|
+
Object.defineProperty(value, Symbol.asyncIterator, {
|
|
37166
|
+
configurable: true,
|
|
37167
|
+
writable: true,
|
|
37168
|
+
value: iterate
|
|
37169
|
+
});
|
|
37170
|
+
} else if (isAsyncIterable(value)) {
|
|
37171
|
+
patchStreamIfNeeded(value, {
|
|
37172
|
+
shouldCollect: (chunk) => {
|
|
37173
|
+
safeObserve(chunk);
|
|
37174
|
+
return false;
|
|
37175
|
+
},
|
|
37176
|
+
onComplete: () => end(true),
|
|
37177
|
+
onCancel: () => end(false),
|
|
37178
|
+
onError: (error) => end(false, error),
|
|
37179
|
+
aroundNext: (next) => withCurrent(span, next)
|
|
37180
|
+
});
|
|
37181
|
+
} else if (typeof value.getReader !== "function") end(false);
|
|
37182
|
+
}
|
|
37183
|
+
|
|
36356
37184
|
// src/instrumentation/plugins/voyageai-channels.ts
|
|
36357
37185
|
var voyageAIChannels = defineChannels(
|
|
36358
37186
|
"voyageai",
|
|
@@ -36420,7 +37248,7 @@ var VoyageAIPlugin = class extends BasePlugin {
|
|
|
36420
37248
|
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
36421
37249
|
}
|
|
36422
37250
|
};
|
|
36423
|
-
function interceptVoyageAICall(channel2, name,
|
|
37251
|
+
function interceptVoyageAICall(channel2, name, extractInput4, extractOutput2, extractMetrics2 = extractUsageMetrics3) {
|
|
36424
37252
|
return channel2.intercept((target, thisArg, args) => {
|
|
36425
37253
|
const invokeTarget = () => Reflect.apply(target, thisArg, args);
|
|
36426
37254
|
if (isAutoInstrumentationSuppressed()) {
|
|
@@ -36428,7 +37256,7 @@ function interceptVoyageAICall(channel2, name, extractInput3, extractOutput2, ex
|
|
|
36428
37256
|
}
|
|
36429
37257
|
let span;
|
|
36430
37258
|
try {
|
|
36431
|
-
const { input, metadata } =
|
|
37259
|
+
const { input, metadata } = extractInput4(args);
|
|
36432
37260
|
span = startSpan(
|
|
36433
37261
|
withSpanInstrumentationName(
|
|
36434
37262
|
{
|
|
@@ -37227,6 +38055,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
37227
38055
|
cloudflareThinkPlugin = null;
|
|
37228
38056
|
cursorSDKPlugin = null;
|
|
37229
38057
|
openAIAgentsPlugin = null;
|
|
38058
|
+
googleGenerativeAIPlugin = null;
|
|
37230
38059
|
googleGenAIPlugin = null;
|
|
37231
38060
|
huggingFacePlugin = null;
|
|
37232
38061
|
huggingFaceTransformersPlugin = null;
|
|
@@ -37245,6 +38074,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
37245
38074
|
langSmithPlugin = null;
|
|
37246
38075
|
piCodingAgentPlugin = null;
|
|
37247
38076
|
strandsAgentSDKPlugin = null;
|
|
38077
|
+
elevenLabsPlugin = null;
|
|
37248
38078
|
voyageAIPlugin = null;
|
|
37249
38079
|
cloudflareAIChatPlugin = null;
|
|
37250
38080
|
cloudflareAgentsPlugin = null;
|
|
@@ -37286,6 +38116,10 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
37286
38116
|
this.openAIAgentsPlugin = new OpenAIAgentsPlugin();
|
|
37287
38117
|
this.openAIAgentsPlugin.enable();
|
|
37288
38118
|
}
|
|
38119
|
+
if (integrations.googleGenerativeAI !== false) {
|
|
38120
|
+
this.googleGenerativeAIPlugin = new GoogleGenerativeAIPlugin();
|
|
38121
|
+
this.googleGenerativeAIPlugin.enable();
|
|
38122
|
+
}
|
|
37289
38123
|
if (integrations.googleGenAI !== false && integrations.google !== false) {
|
|
37290
38124
|
this.googleGenAIPlugin = new GoogleGenAIPlugin();
|
|
37291
38125
|
this.googleGenAIPlugin.enable();
|
|
@@ -37320,6 +38154,10 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
37320
38154
|
this.coherePlugin = new CoherePlugin();
|
|
37321
38155
|
this.coherePlugin.enable();
|
|
37322
38156
|
}
|
|
38157
|
+
if (integrations.elevenlabs !== false) {
|
|
38158
|
+
this.elevenLabsPlugin = new ElevenLabsPlugin();
|
|
38159
|
+
this.elevenLabsPlugin.enable();
|
|
38160
|
+
}
|
|
37323
38161
|
if (integrations.voyageai !== false) {
|
|
37324
38162
|
this.voyageAIPlugin = new VoyageAIPlugin();
|
|
37325
38163
|
this.voyageAIPlugin.enable();
|
|
@@ -37404,6 +38242,10 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
37404
38242
|
this.openAIAgentsPlugin.disable();
|
|
37405
38243
|
this.openAIAgentsPlugin = null;
|
|
37406
38244
|
}
|
|
38245
|
+
if (this.googleGenerativeAIPlugin) {
|
|
38246
|
+
this.googleGenerativeAIPlugin.disable();
|
|
38247
|
+
this.googleGenerativeAIPlugin = null;
|
|
38248
|
+
}
|
|
37407
38249
|
if (this.googleGenAIPlugin) {
|
|
37408
38250
|
this.googleGenAIPlugin.disable();
|
|
37409
38251
|
this.googleGenAIPlugin = null;
|
|
@@ -37440,6 +38282,10 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
37440
38282
|
this.coherePlugin.disable();
|
|
37441
38283
|
this.coherePlugin = null;
|
|
37442
38284
|
}
|
|
38285
|
+
if (this.elevenLabsPlugin) {
|
|
38286
|
+
this.elevenLabsPlugin.disable();
|
|
38287
|
+
this.elevenLabsPlugin = null;
|
|
38288
|
+
}
|
|
37443
38289
|
if (this.voyageAIPlugin) {
|
|
37444
38290
|
this.voyageAIPlugin.disable();
|
|
37445
38291
|
this.voyageAIPlugin = null;
|
|
@@ -37533,6 +38379,8 @@ var envIntegrationAliases = {
|
|
|
37533
38379
|
"openai-agents-core": "openAIAgents",
|
|
37534
38380
|
openaiagentscore: "openAIAgents",
|
|
37535
38381
|
google: "google",
|
|
38382
|
+
"google-generative-ai": "googleGenerativeAI",
|
|
38383
|
+
googlegenerativeai: "googleGenerativeAI",
|
|
37536
38384
|
"google-genai": "googleGenAI",
|
|
37537
38385
|
googlegenai: "googleGenAI",
|
|
37538
38386
|
huggingface: "huggingface",
|
|
@@ -37566,7 +38414,9 @@ var envIntegrationAliases = {
|
|
|
37566
38414
|
langsmith: "langsmith",
|
|
37567
38415
|
voyage: "voyageai",
|
|
37568
38416
|
"voyage-ai": "voyageai",
|
|
37569
|
-
voyageai: "voyageai"
|
|
38417
|
+
voyageai: "voyageai",
|
|
38418
|
+
elevenlabs: "elevenlabs",
|
|
38419
|
+
"@elevenlabs/elevenlabs-js": "elevenlabs"
|
|
37570
38420
|
};
|
|
37571
38421
|
function getDefaultInstrumentationIntegrations() {
|
|
37572
38422
|
return {
|
|
@@ -37577,6 +38427,7 @@ function getDefaultInstrumentationIntegrations() {
|
|
|
37577
38427
|
aisdk: true,
|
|
37578
38428
|
google: true,
|
|
37579
38429
|
googleGenAI: true,
|
|
38430
|
+
googleGenerativeAI: true,
|
|
37580
38431
|
googleADK: true,
|
|
37581
38432
|
huggingface: true,
|
|
37582
38433
|
claudeAgentSDK: true,
|
|
@@ -37602,6 +38453,7 @@ function getDefaultInstrumentationIntegrations() {
|
|
|
37602
38453
|
langgraph: true,
|
|
37603
38454
|
langsmith: true,
|
|
37604
38455
|
voyageai: true,
|
|
38456
|
+
elevenlabs: true,
|
|
37605
38457
|
piCodingAgent: true,
|
|
37606
38458
|
strandsAgentSDK: true,
|
|
37607
38459
|
cloudflareAgents: true
|
|
@@ -37918,9 +38770,11 @@ __export(exports_exports, {
|
|
|
37918
38770
|
wrapCohere: () => wrapCohere,
|
|
37919
38771
|
wrapCopilotClient: () => wrapCopilotClient,
|
|
37920
38772
|
wrapCursorSDK: () => wrapCursorSDK,
|
|
38773
|
+
wrapElevenLabs: () => wrapElevenLabs,
|
|
37921
38774
|
wrapGenkit: () => wrapGenkit,
|
|
37922
38775
|
wrapGoogleADK: () => wrapGoogleADK,
|
|
37923
38776
|
wrapGoogleGenAI: () => wrapGoogleGenAI,
|
|
38777
|
+
wrapGoogleGenerativeAI: () => wrapGoogleGenerativeAI,
|
|
37924
38778
|
wrapGroq: () => wrapGroq,
|
|
37925
38779
|
wrapHuggingFace: () => wrapHuggingFace,
|
|
37926
38780
|
wrapHuggingFaceTransformers: () => wrapHuggingFaceTransformers,
|
|
@@ -39530,7 +40384,7 @@ function extractToolCallsFromBlocks(blocks) {
|
|
|
39530
40384
|
if (!Array.isArray(blocks)) return [];
|
|
39531
40385
|
return extractToolCallsFromSteps([{ content: blocks }]);
|
|
39532
40386
|
}
|
|
39533
|
-
function
|
|
40387
|
+
function extractInput3(params) {
|
|
39534
40388
|
return params?.prompt ?? params?.messages ?? params?.system;
|
|
39535
40389
|
}
|
|
39536
40390
|
var V2_EXCLUDE_KEYS = /* @__PURE__ */ new Set([
|
|
@@ -39552,7 +40406,7 @@ function BraintrustMiddleware(config = {}) {
|
|
|
39552
40406
|
params,
|
|
39553
40407
|
model: modelFromWrapGenerate
|
|
39554
40408
|
}) => {
|
|
39555
|
-
const rawInput =
|
|
40409
|
+
const rawInput = extractInput3(params);
|
|
39556
40410
|
const processedInput = processInputAttachments(rawInput);
|
|
39557
40411
|
const spanArgs = {
|
|
39558
40412
|
name: config.spanInfo?.name || "ai-sdk.doGenerate",
|
|
@@ -39616,7 +40470,7 @@ function BraintrustMiddleware(config = {}) {
|
|
|
39616
40470
|
}
|
|
39617
40471
|
},
|
|
39618
40472
|
wrapStream: async ({ doStream, params }) => {
|
|
39619
|
-
const rawInput =
|
|
40473
|
+
const rawInput = extractInput3(params);
|
|
39620
40474
|
const processedInput = processInputAttachments(rawInput);
|
|
39621
40475
|
const spanArgs = {
|
|
39622
40476
|
name: config.spanInfo?.name || "ai-sdk.doStream",
|
|
@@ -43009,6 +43863,75 @@ function isModuleNamespace5(value) {
|
|
|
43009
43863
|
}
|
|
43010
43864
|
}
|
|
43011
43865
|
|
|
43866
|
+
// src/wrappers/google-generative-ai.ts
|
|
43867
|
+
function wrapGoogleGenerativeAI(client) {
|
|
43868
|
+
if (!isObject(client) || typeof client.getGenerativeModel !== "function")
|
|
43869
|
+
return client;
|
|
43870
|
+
return wrapClient(client);
|
|
43871
|
+
}
|
|
43872
|
+
var proxies = /* @__PURE__ */ new WeakMap();
|
|
43873
|
+
function wrapClient(client) {
|
|
43874
|
+
const cached = proxies.get(client);
|
|
43875
|
+
if (cached) return cached;
|
|
43876
|
+
const proxy = new Proxy(client, {
|
|
43877
|
+
get(target, prop, receiver) {
|
|
43878
|
+
const value = Reflect.get(target, prop, receiver);
|
|
43879
|
+
if (typeof value !== "function") return value;
|
|
43880
|
+
if (prop === "getGenerativeModel" || prop === "getGenerativeModelFromCachedContent" || prop === "startChat") {
|
|
43881
|
+
return (...args) => wrapClient(Reflect.apply(value, target, args));
|
|
43882
|
+
}
|
|
43883
|
+
switch (prop) {
|
|
43884
|
+
case "generateContent":
|
|
43885
|
+
return (...args) => googleGenerativeAIChannels.generateContent.invoke(
|
|
43886
|
+
value,
|
|
43887
|
+
target,
|
|
43888
|
+
args,
|
|
43889
|
+
{}
|
|
43890
|
+
);
|
|
43891
|
+
case "generateContentStream":
|
|
43892
|
+
return (...args) => googleGenerativeAIChannels.generateContentStream.invoke(
|
|
43893
|
+
value,
|
|
43894
|
+
target,
|
|
43895
|
+
args,
|
|
43896
|
+
{}
|
|
43897
|
+
);
|
|
43898
|
+
case "embedContent":
|
|
43899
|
+
return (...args) => googleGenerativeAIChannels.embedContent.invoke(
|
|
43900
|
+
value,
|
|
43901
|
+
target,
|
|
43902
|
+
args,
|
|
43903
|
+
{}
|
|
43904
|
+
);
|
|
43905
|
+
case "batchEmbedContents":
|
|
43906
|
+
return (...args) => googleGenerativeAIChannels.batchEmbedContents.invoke(
|
|
43907
|
+
value,
|
|
43908
|
+
target,
|
|
43909
|
+
args,
|
|
43910
|
+
{}
|
|
43911
|
+
);
|
|
43912
|
+
case "sendMessage":
|
|
43913
|
+
return (...args) => googleGenerativeAIChannels.sendMessage.invoke(
|
|
43914
|
+
value,
|
|
43915
|
+
target,
|
|
43916
|
+
args,
|
|
43917
|
+
{}
|
|
43918
|
+
);
|
|
43919
|
+
case "sendMessageStream":
|
|
43920
|
+
return (...args) => googleGenerativeAIChannels.sendMessageStream.invoke(
|
|
43921
|
+
value,
|
|
43922
|
+
target,
|
|
43923
|
+
args,
|
|
43924
|
+
{}
|
|
43925
|
+
);
|
|
43926
|
+
}
|
|
43927
|
+
return value.bind(target);
|
|
43928
|
+
}
|
|
43929
|
+
});
|
|
43930
|
+
proxies.set(client, proxy);
|
|
43931
|
+
proxies.set(proxy, proxy);
|
|
43932
|
+
return proxy;
|
|
43933
|
+
}
|
|
43934
|
+
|
|
43012
43935
|
// src/wrappers/google-genai.ts
|
|
43013
43936
|
function wrapGoogleGenAI(googleGenAI) {
|
|
43014
43937
|
if (!googleGenAI || typeof googleGenAI !== "object") {
|
|
@@ -50370,6 +51293,65 @@ var evaluatorDefinitionsSchema = z13.record(
|
|
|
50370
51293
|
evaluatorDefinitionSchema
|
|
50371
51294
|
);
|
|
50372
51295
|
|
|
51296
|
+
// src/wrappers/elevenlabs.ts
|
|
51297
|
+
var clients = /* @__PURE__ */ new WeakMap();
|
|
51298
|
+
function wrapElevenLabs(client) {
|
|
51299
|
+
if (!isObject(client) || !isObject(client.textToSpeech) || typeof client.textToSpeech.convert !== "function") {
|
|
51300
|
+
debugLogger.warn("Unsupported ElevenLabs client. Not wrapping.");
|
|
51301
|
+
return client;
|
|
51302
|
+
}
|
|
51303
|
+
const cached = clients.get(client);
|
|
51304
|
+
if (cached) return cached;
|
|
51305
|
+
const sdk = client;
|
|
51306
|
+
const speech = new Proxy(sdk.textToSpeech, {
|
|
51307
|
+
get(target, prop, receiver) {
|
|
51308
|
+
switch (prop) {
|
|
51309
|
+
case "convert":
|
|
51310
|
+
case "stream":
|
|
51311
|
+
return (...args) => elevenLabsChannels[prop].invoke(target[prop], target, args, {});
|
|
51312
|
+
case "convertWithTimestamps":
|
|
51313
|
+
return (...args) => elevenLabsChannels.convertWithTimestamps.invoke(
|
|
51314
|
+
target.convertWithTimestamps,
|
|
51315
|
+
target,
|
|
51316
|
+
args,
|
|
51317
|
+
{}
|
|
51318
|
+
);
|
|
51319
|
+
case "streamWithTimestamps":
|
|
51320
|
+
return (...args) => elevenLabsChannels.streamWithTimestamps.invoke(
|
|
51321
|
+
target.streamWithTimestamps,
|
|
51322
|
+
target,
|
|
51323
|
+
args,
|
|
51324
|
+
{}
|
|
51325
|
+
);
|
|
51326
|
+
default:
|
|
51327
|
+
return Reflect.get(target, prop, receiver);
|
|
51328
|
+
}
|
|
51329
|
+
}
|
|
51330
|
+
});
|
|
51331
|
+
const transcription = new Proxy(sdk.speechToText, {
|
|
51332
|
+
get(target, prop, receiver) {
|
|
51333
|
+
if (prop === "convert")
|
|
51334
|
+
return (request, options) => elevenLabsChannels.transcribe.invoke(
|
|
51335
|
+
target.convert,
|
|
51336
|
+
target,
|
|
51337
|
+
[request, options],
|
|
51338
|
+
{}
|
|
51339
|
+
);
|
|
51340
|
+
return Reflect.get(target, prop, receiver);
|
|
51341
|
+
}
|
|
51342
|
+
});
|
|
51343
|
+
const proxy = new Proxy(sdk, {
|
|
51344
|
+
get(target, prop, receiver) {
|
|
51345
|
+
if (prop === "textToSpeech") return speech;
|
|
51346
|
+
if (prop === "speechToText") return transcription;
|
|
51347
|
+
return Reflect.get(target, prop, receiver);
|
|
51348
|
+
}
|
|
51349
|
+
});
|
|
51350
|
+
clients.set(client, proxy);
|
|
51351
|
+
clients.set(proxy, proxy);
|
|
51352
|
+
return proxy;
|
|
51353
|
+
}
|
|
51354
|
+
|
|
50373
51355
|
// src/edge-light/index.ts
|
|
50374
51356
|
configureEdgeLight();
|
|
50375
51357
|
export {
|
|
@@ -50535,9 +51517,11 @@ export {
|
|
|
50535
51517
|
wrapCohere,
|
|
50536
51518
|
wrapCopilotClient,
|
|
50537
51519
|
wrapCursorSDK,
|
|
51520
|
+
wrapElevenLabs,
|
|
50538
51521
|
wrapGenkit,
|
|
50539
51522
|
wrapGoogleADK,
|
|
50540
51523
|
wrapGoogleGenAI,
|
|
51524
|
+
wrapGoogleGenerativeAI,
|
|
50541
51525
|
wrapGroq,
|
|
50542
51526
|
wrapHuggingFace,
|
|
50543
51527
|
wrapHuggingFaceTransformers,
|