braintrust 3.28.0 → 3.29.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/README.md +1 -1
- package/dev/dist/index.d.mts +356 -7
- package/dev/dist/index.d.ts +356 -7
- package/dev/dist/index.js +1815 -1548
- package/dev/dist/index.mjs +1054 -787
- package/dist/apply-auto-instrumentation.js +297 -264
- package/dist/apply-auto-instrumentation.mjs +84 -51
- package/dist/auto-instrumentations/bundler/esbuild.cjs +43 -5
- package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
- package/dist/auto-instrumentations/bundler/next.cjs +43 -5
- package/dist/auto-instrumentations/bundler/next.mjs +3 -3
- package/dist/auto-instrumentations/bundler/rollup.cjs +43 -5
- package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
- package/dist/auto-instrumentations/bundler/vite.cjs +43 -5
- package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +43 -5
- package/dist/auto-instrumentations/bundler/webpack.cjs +43 -5
- package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
- package/dist/auto-instrumentations/{chunk-NP7V4XB2.mjs → chunk-AOIYCVEL.mjs} +32 -2
- package/dist/auto-instrumentations/{chunk-26PKVUKB.mjs → chunk-DKTGDNA7.mjs} +12 -4
- package/dist/auto-instrumentations/{chunk-HD35AM3M.mjs → chunk-OMCZ3MV2.mjs} +1 -1
- package/dist/auto-instrumentations/hook.mjs +952 -133
- package/dist/auto-instrumentations/index.cjs +32 -2
- package/dist/auto-instrumentations/index.mjs +1 -1
- package/dist/browser.d.mts +760 -22
- package/dist/browser.d.ts +760 -22
- package/dist/browser.js +1661 -848
- package/dist/browser.mjs +1661 -848
- package/dist/{chunk-BBE7SNRV.js → chunk-6Z5S7VOU.js} +143 -23
- package/dist/{chunk-ZHUHZWFY.mjs → chunk-7FA6VP2S.mjs} +140 -20
- package/dist/{chunk-UPFNQCGB.mjs → chunk-M6XPNJC4.mjs} +1144 -931
- package/dist/{chunk-OBBWQW6K.js → chunk-XLLGRXGR.js} +2335 -2122
- package/dist/cli.js +5134 -1090
- package/dist/edge-light.js +1661 -848
- package/dist/edge-light.mjs +1661 -848
- package/dist/index.d.mts +760 -22
- package/dist/index.d.ts +760 -22
- package/dist/index.js +1054 -567
- package/dist/index.mjs +514 -27
- package/dist/instrumentation/index.d.mts +548 -9
- package/dist/instrumentation/index.d.ts +548 -9
- package/dist/instrumentation/index.js +1975 -765
- package/dist/instrumentation/index.mjs +1975 -765
- package/dist/vitest-evals-reporter.js +16 -16
- package/dist/vitest-evals-reporter.mjs +2 -2
- package/dist/workerd.js +1661 -848
- package/dist/workerd.mjs +1661 -848
- package/package.json +1 -1
- package/util/dist/index.d.mts +42 -0
- package/util/dist/index.d.ts +42 -0
package/dist/edge-light.js
CHANGED
|
@@ -869,8 +869,6 @@ function newGlobalTracingChannel(nameOrChannels) {
|
|
|
869
869
|
var DefaultAsyncLocalStorage = class {
|
|
870
870
|
constructor() {
|
|
871
871
|
}
|
|
872
|
-
enterWith(_) {
|
|
873
|
-
}
|
|
874
872
|
run(_, callback) {
|
|
875
873
|
return callback();
|
|
876
874
|
}
|
|
@@ -2687,7 +2685,9 @@ var AclObjectType = import_v36.z.union([
|
|
|
2687
2685
|
"project_log",
|
|
2688
2686
|
"org_project",
|
|
2689
2687
|
"org_audit_logs",
|
|
2690
|
-
"project_group"
|
|
2688
|
+
"project_group",
|
|
2689
|
+
"ai_secret",
|
|
2690
|
+
"org_ai_secret"
|
|
2691
2691
|
]),
|
|
2692
2692
|
import_v36.z.null()
|
|
2693
2693
|
]);
|
|
@@ -3520,7 +3520,7 @@ var PromptParserNullish = import_v36.z.union([
|
|
|
3520
3520
|
}),
|
|
3521
3521
|
import_v36.z.null()
|
|
3522
3522
|
]);
|
|
3523
|
-
var
|
|
3523
|
+
var PreprocessorId = import_v36.z.union([
|
|
3524
3524
|
import_v36.z.object({
|
|
3525
3525
|
type: import_v36.z.literal("function"),
|
|
3526
3526
|
id: import_v36.z.string(),
|
|
@@ -3531,6 +3531,7 @@ var PreprocessorSavedFunctionId = import_v36.z.union([
|
|
|
3531
3531
|
name: import_v36.z.string(),
|
|
3532
3532
|
function_type: import_v36.z.literal("preprocessor").optional().default("preprocessor")
|
|
3533
3533
|
}),
|
|
3534
|
+
import_v36.z.object({ type: import_v36.z.literal("inline"), code: import_v36.z.string().min(1) }),
|
|
3534
3535
|
import_v36.z.null()
|
|
3535
3536
|
]);
|
|
3536
3537
|
var PromptDataNullish = import_v36.z.union([
|
|
@@ -3538,7 +3539,7 @@ var PromptDataNullish = import_v36.z.union([
|
|
|
3538
3539
|
prompt: PromptBlockDataNullish,
|
|
3539
3540
|
options: PromptOptionsNullish,
|
|
3540
3541
|
parser: PromptParserNullish,
|
|
3541
|
-
preprocessor:
|
|
3542
|
+
preprocessor: PreprocessorId,
|
|
3542
3543
|
tool_functions: import_v36.z.union([import_v36.z.array(SavedFunctionId), import_v36.z.null()]),
|
|
3543
3544
|
template_format: import_v36.z.union([
|
|
3544
3545
|
import_v36.z.enum(["mustache", "nunjucks", "none"]),
|
|
@@ -3740,7 +3741,7 @@ var PromptData = import_v36.z.object({
|
|
|
3740
3741
|
prompt: PromptBlockDataNullish,
|
|
3741
3742
|
options: PromptOptionsNullish,
|
|
3742
3743
|
parser: PromptParserNullish,
|
|
3743
|
-
preprocessor:
|
|
3744
|
+
preprocessor: PreprocessorId,
|
|
3744
3745
|
tool_functions: import_v36.z.union([import_v36.z.array(SavedFunctionId), import_v36.z.null()]),
|
|
3745
3746
|
template_format: import_v36.z.union([
|
|
3746
3747
|
import_v36.z.enum(["mustache", "nunjucks", "none"]),
|
|
@@ -4684,6 +4685,7 @@ var View = import_v36.z.object({
|
|
|
4684
4685
|
]),
|
|
4685
4686
|
name: import_v36.z.string(),
|
|
4686
4687
|
description: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
|
|
4688
|
+
starred: import_v36.z.boolean().optional(),
|
|
4687
4689
|
created: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
|
|
4688
4690
|
updated_at: import_v36.z.union([import_v36.z.string(), import_v36.z.null()]).optional(),
|
|
4689
4691
|
view_data: ViewData.optional(),
|
|
@@ -5385,44 +5387,72 @@ function createCacheLayers({
|
|
|
5385
5387
|
}
|
|
5386
5388
|
|
|
5387
5389
|
// src/prompt-cache/prompt-cache.ts
|
|
5388
|
-
function createCacheKey(key) {
|
|
5390
|
+
function createCacheKey(key, namespace) {
|
|
5391
|
+
let cacheKey;
|
|
5389
5392
|
if (key.id) {
|
|
5390
|
-
|
|
5391
|
-
}
|
|
5392
|
-
|
|
5393
|
-
|
|
5394
|
-
|
|
5395
|
-
|
|
5396
|
-
|
|
5397
|
-
|
|
5393
|
+
cacheKey = `id:${key.id}`;
|
|
5394
|
+
} else {
|
|
5395
|
+
const prefix = key.projectId ?? key.projectName;
|
|
5396
|
+
if (!prefix) {
|
|
5397
|
+
throw new Error("Either projectId or projectName must be provided");
|
|
5398
|
+
}
|
|
5399
|
+
if (!key.slug) {
|
|
5400
|
+
throw new Error("Slug must be provided when not using ID");
|
|
5401
|
+
}
|
|
5402
|
+
cacheKey = `${prefix}:${key.slug}:${key.version ?? "latest"}`;
|
|
5398
5403
|
}
|
|
5399
|
-
return `${
|
|
5404
|
+
return namespace === void 0 ? cacheKey : `${namespace.length}:${namespace}:${cacheKey}`;
|
|
5400
5405
|
}
|
|
5401
|
-
var PromptCache = class {
|
|
5406
|
+
var PromptCache = class _PromptCache {
|
|
5402
5407
|
memoryCache;
|
|
5403
5408
|
diskCache;
|
|
5409
|
+
namespace;
|
|
5410
|
+
expectedResolvedOrgIdentity;
|
|
5404
5411
|
constructor(options) {
|
|
5405
5412
|
this.memoryCache = options.memoryCache;
|
|
5406
5413
|
this.diskCache = options.diskCache;
|
|
5414
|
+
this.namespace = options.namespace;
|
|
5415
|
+
this.expectedResolvedOrgIdentity = options.expectedResolvedOrgIdentity;
|
|
5416
|
+
}
|
|
5417
|
+
/**
|
|
5418
|
+
* Returns a cache view that shares the same storage layers but isolates all
|
|
5419
|
+
* entries under the provided namespace.
|
|
5420
|
+
*/
|
|
5421
|
+
withNamespace(namespace, expectedResolvedOrgIdentity) {
|
|
5422
|
+
return new _PromptCache({
|
|
5423
|
+
memoryCache: this.memoryCache,
|
|
5424
|
+
diskCache: this.diskCache,
|
|
5425
|
+
namespace,
|
|
5426
|
+
expectedResolvedOrgIdentity
|
|
5427
|
+
});
|
|
5407
5428
|
}
|
|
5408
5429
|
/**
|
|
5409
5430
|
* Retrieves a prompt from the cache.
|
|
5410
5431
|
* First checks the in-memory LRU cache, then falls back to checking the disk cache if available.
|
|
5411
5432
|
*/
|
|
5412
5433
|
async get(key) {
|
|
5413
|
-
const cacheKey = createCacheKey(key);
|
|
5434
|
+
const cacheKey = createCacheKey(key, this.namespace);
|
|
5414
5435
|
if (this.memoryCache) {
|
|
5415
|
-
const
|
|
5416
|
-
if (
|
|
5417
|
-
return
|
|
5436
|
+
const memoryEntry = this.memoryCache.get(cacheKey);
|
|
5437
|
+
if (memoryEntry !== void 0 && (this.expectedResolvedOrgIdentity === void 0 || memoryEntry.resolvedOrgIdentity === this.expectedResolvedOrgIdentity)) {
|
|
5438
|
+
return memoryEntry.value;
|
|
5418
5439
|
}
|
|
5419
5440
|
}
|
|
5420
5441
|
if (this.diskCache) {
|
|
5421
|
-
const
|
|
5422
|
-
if (!
|
|
5442
|
+
const diskEntry = await this.diskCache.get(cacheKey);
|
|
5443
|
+
if (!diskEntry || this.expectedResolvedOrgIdentity !== void 0 && diskEntry.resolvedOrgIdentity !== this.expectedResolvedOrgIdentity) {
|
|
5423
5444
|
return void 0;
|
|
5424
5445
|
}
|
|
5425
|
-
|
|
5446
|
+
const serializedPrompt = diskEntry.value;
|
|
5447
|
+
const diskPrompt = new Prompt2(
|
|
5448
|
+
serializedPrompt.metadata,
|
|
5449
|
+
serializedPrompt.defaults,
|
|
5450
|
+
serializedPrompt.noTrace
|
|
5451
|
+
);
|
|
5452
|
+
this.memoryCache?.set(cacheKey, {
|
|
5453
|
+
value: diskPrompt,
|
|
5454
|
+
resolvedOrgIdentity: diskEntry.resolvedOrgIdentity
|
|
5455
|
+
});
|
|
5426
5456
|
return diskPrompt;
|
|
5427
5457
|
}
|
|
5428
5458
|
return void 0;
|
|
@@ -5436,58 +5466,91 @@ var PromptCache = class {
|
|
|
5436
5466
|
* @throws If there is an error writing to the disk cache.
|
|
5437
5467
|
*/
|
|
5438
5468
|
async set(key, value) {
|
|
5439
|
-
const cacheKey = createCacheKey(key);
|
|
5440
|
-
|
|
5469
|
+
const cacheKey = createCacheKey(key, this.namespace);
|
|
5470
|
+
const memoryEntry = {
|
|
5471
|
+
value,
|
|
5472
|
+
resolvedOrgIdentity: this.expectedResolvedOrgIdentity
|
|
5473
|
+
};
|
|
5474
|
+
this.memoryCache?.set(cacheKey, memoryEntry);
|
|
5441
5475
|
if (this.diskCache) {
|
|
5442
|
-
await this.diskCache.set(cacheKey,
|
|
5476
|
+
await this.diskCache.set(cacheKey, {
|
|
5477
|
+
value: value._internalSerializeForCache(),
|
|
5478
|
+
resolvedOrgIdentity: this.expectedResolvedOrgIdentity
|
|
5479
|
+
});
|
|
5443
5480
|
}
|
|
5444
5481
|
}
|
|
5445
5482
|
};
|
|
5446
5483
|
|
|
5447
5484
|
// src/prompt-cache/parameters-cache.ts
|
|
5448
|
-
function createCacheKey2(key) {
|
|
5485
|
+
function createCacheKey2(key, namespace) {
|
|
5486
|
+
let cacheKey;
|
|
5449
5487
|
if (key.id) {
|
|
5450
|
-
|
|
5451
|
-
}
|
|
5452
|
-
|
|
5453
|
-
|
|
5454
|
-
|
|
5455
|
-
|
|
5456
|
-
|
|
5457
|
-
|
|
5488
|
+
cacheKey = `parameters:id:${key.id}`;
|
|
5489
|
+
} else {
|
|
5490
|
+
const prefix = key.projectId ?? key.projectName;
|
|
5491
|
+
if (!prefix) {
|
|
5492
|
+
throw new Error("Either projectId or projectName must be provided");
|
|
5493
|
+
}
|
|
5494
|
+
if (!key.slug) {
|
|
5495
|
+
throw new Error("Slug must be provided when not using ID");
|
|
5496
|
+
}
|
|
5497
|
+
cacheKey = `parameters:${prefix}:${key.slug}:${key.version ?? "latest"}`;
|
|
5458
5498
|
}
|
|
5459
|
-
return
|
|
5499
|
+
return namespace === void 0 ? cacheKey : `${namespace.length}:${namespace}:${cacheKey}`;
|
|
5460
5500
|
}
|
|
5461
|
-
var ParametersCache = class {
|
|
5501
|
+
var ParametersCache = class _ParametersCache {
|
|
5462
5502
|
memoryCache;
|
|
5463
5503
|
diskCache;
|
|
5504
|
+
namespace;
|
|
5505
|
+
expectedResolvedOrgIdentity;
|
|
5464
5506
|
constructor(options) {
|
|
5465
5507
|
this.memoryCache = options.memoryCache;
|
|
5466
5508
|
this.diskCache = options.diskCache;
|
|
5509
|
+
this.namespace = options.namespace;
|
|
5510
|
+
this.expectedResolvedOrgIdentity = options.expectedResolvedOrgIdentity;
|
|
5511
|
+
}
|
|
5512
|
+
withNamespace(namespace, expectedResolvedOrgIdentity) {
|
|
5513
|
+
return new _ParametersCache({
|
|
5514
|
+
memoryCache: this.memoryCache,
|
|
5515
|
+
diskCache: this.diskCache,
|
|
5516
|
+
namespace,
|
|
5517
|
+
expectedResolvedOrgIdentity
|
|
5518
|
+
});
|
|
5467
5519
|
}
|
|
5468
5520
|
async get(key) {
|
|
5469
|
-
const cacheKey = createCacheKey2(key);
|
|
5521
|
+
const cacheKey = createCacheKey2(key, this.namespace);
|
|
5470
5522
|
if (this.memoryCache) {
|
|
5471
|
-
const
|
|
5472
|
-
if (
|
|
5473
|
-
return
|
|
5523
|
+
const memoryEntry = this.memoryCache.get(cacheKey);
|
|
5524
|
+
if (memoryEntry !== void 0 && (this.expectedResolvedOrgIdentity === void 0 || memoryEntry.resolvedOrgIdentity === this.expectedResolvedOrgIdentity)) {
|
|
5525
|
+
return memoryEntry.value;
|
|
5474
5526
|
}
|
|
5475
5527
|
}
|
|
5476
5528
|
if (this.diskCache) {
|
|
5477
|
-
const
|
|
5478
|
-
if (!
|
|
5529
|
+
const diskEntry = await this.diskCache.get(cacheKey);
|
|
5530
|
+
if (!diskEntry || this.expectedResolvedOrgIdentity !== void 0 && diskEntry.resolvedOrgIdentity !== this.expectedResolvedOrgIdentity) {
|
|
5479
5531
|
return void 0;
|
|
5480
5532
|
}
|
|
5481
|
-
|
|
5482
|
-
|
|
5533
|
+
const diskParameters = new RemoteEvalParameters(diskEntry.value.metadata);
|
|
5534
|
+
this.memoryCache?.set(cacheKey, {
|
|
5535
|
+
value: diskParameters,
|
|
5536
|
+
resolvedOrgIdentity: diskEntry.resolvedOrgIdentity
|
|
5537
|
+
});
|
|
5538
|
+
return diskParameters;
|
|
5483
5539
|
}
|
|
5484
5540
|
return void 0;
|
|
5485
5541
|
}
|
|
5486
5542
|
async set(key, value) {
|
|
5487
|
-
const cacheKey = createCacheKey2(key);
|
|
5488
|
-
|
|
5543
|
+
const cacheKey = createCacheKey2(key, this.namespace);
|
|
5544
|
+
const memoryEntry = {
|
|
5545
|
+
value,
|
|
5546
|
+
resolvedOrgIdentity: this.expectedResolvedOrgIdentity
|
|
5547
|
+
};
|
|
5548
|
+
this.memoryCache?.set(cacheKey, memoryEntry);
|
|
5489
5549
|
if (this.diskCache) {
|
|
5490
|
-
await this.diskCache.set(cacheKey,
|
|
5550
|
+
await this.diskCache.set(cacheKey, {
|
|
5551
|
+
value: value._internalSerializeForCache(),
|
|
5552
|
+
resolvedOrgIdentity: this.expectedResolvedOrgIdentity
|
|
5553
|
+
});
|
|
5491
5554
|
}
|
|
5492
5555
|
}
|
|
5493
5556
|
};
|
|
@@ -5791,6 +5854,7 @@ var INSTRUMENTATION_NAMES = {
|
|
|
5791
5854
|
CLOUDFLARE_THINK: "cloudflare-think",
|
|
5792
5855
|
COHERE: "cohere",
|
|
5793
5856
|
CURSOR_SDK: "cursor-sdk",
|
|
5857
|
+
DEEPSEEK_HARNESS: "deepseek-harness",
|
|
5794
5858
|
EVE: "eve",
|
|
5795
5859
|
FLUE: "flue",
|
|
5796
5860
|
GENKIT: "genkit",
|
|
@@ -5816,7 +5880,7 @@ var INSTRUMENTATION_NAMES = {
|
|
|
5816
5880
|
var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
|
|
5817
5881
|
"braintrust.spanInstrumentationName"
|
|
5818
5882
|
);
|
|
5819
|
-
var SDK_VERSION = true ? "3.
|
|
5883
|
+
var SDK_VERSION = true ? "3.29.0" : "0.0.0";
|
|
5820
5884
|
function withSpanInstrumentationName(args, instrumentationName) {
|
|
5821
5885
|
return {
|
|
5822
5886
|
...args,
|
|
@@ -5935,6 +5999,16 @@ var datasetSnapshotRegisterResponseSchema = import_v38.z.object({
|
|
|
5935
5999
|
dataset_snapshot: DatasetSnapshot,
|
|
5936
6000
|
found_existing: import_v38.z.boolean().optional()
|
|
5937
6001
|
});
|
|
6002
|
+
var datasetObjectInfoSchema = import_v38.z.object({
|
|
6003
|
+
object_id: import_v38.z.string(),
|
|
6004
|
+
object_name: import_v38.z.string(),
|
|
6005
|
+
parent_cols: import_v38.z.object({
|
|
6006
|
+
project: import_v38.z.object({
|
|
6007
|
+
id: import_v38.z.string(),
|
|
6008
|
+
name: import_v38.z.string()
|
|
6009
|
+
})
|
|
6010
|
+
})
|
|
6011
|
+
});
|
|
5938
6012
|
var datasetRestorePreviewResultSchema = import_v38.z.object({
|
|
5939
6013
|
rows_to_restore: import_v38.z.number(),
|
|
5940
6014
|
rows_to_delete: import_v38.z.number()
|
|
@@ -6148,12 +6222,53 @@ var loginSchema = import_v38.z.strictObject({
|
|
|
6148
6222
|
});
|
|
6149
6223
|
var stateNonce = 0;
|
|
6150
6224
|
var V1_PROXY_SUFFIX = "/v1/proxy";
|
|
6225
|
+
var LOADER_LOGIN_CACHE_MAX = 16;
|
|
6151
6226
|
function normalizeProxyConnUrl(proxyUrl) {
|
|
6152
6227
|
return proxyUrl.endsWith(V1_PROXY_SUFFIX) ? proxyUrl.slice(0, proxyUrl.length - V1_PROXY_SUFFIX.length) : proxyUrl;
|
|
6153
6228
|
}
|
|
6154
6229
|
var BraintrustState = class _BraintrustState {
|
|
6230
|
+
id;
|
|
6231
|
+
currentExperiment;
|
|
6232
|
+
// Note: the value of IsAsyncFlush doesn't really matter here, since we
|
|
6233
|
+
// (safely) dynamically cast it whenever retrieving the logger.
|
|
6234
|
+
currentLogger;
|
|
6235
|
+
currentParent;
|
|
6236
|
+
currentSpan;
|
|
6237
|
+
// Any time we re-log in, we directly update the apiConn inside the logger.
|
|
6238
|
+
// This is preferable to replacing the whole logger, which would create the
|
|
6239
|
+
// possibility of multiple loggers floating around, which may not log in a
|
|
6240
|
+
// deterministic order.
|
|
6241
|
+
_bgLogger;
|
|
6242
|
+
_overrideBgLogger = null;
|
|
6243
|
+
appUrl = null;
|
|
6244
|
+
appPublicUrl = null;
|
|
6245
|
+
loginToken = null;
|
|
6246
|
+
orgId = null;
|
|
6247
|
+
orgName = null;
|
|
6248
|
+
apiUrl = null;
|
|
6249
|
+
proxyUrl = null;
|
|
6250
|
+
loggedIn = false;
|
|
6251
|
+
gitMetadataSettings;
|
|
6252
|
+
debugLogLevel;
|
|
6253
|
+
debugLogLevelConfigured = false;
|
|
6254
|
+
fetch = globalThis.fetch;
|
|
6255
|
+
_appConn = null;
|
|
6256
|
+
_apiConn = null;
|
|
6257
|
+
_proxyConn = null;
|
|
6258
|
+
promptCache;
|
|
6259
|
+
parametersCache;
|
|
6260
|
+
spanCache;
|
|
6261
|
+
_idGenerator = null;
|
|
6262
|
+
_contextManager = null;
|
|
6263
|
+
_otelFlushCallback = null;
|
|
6264
|
+
spanOriginEnvironment;
|
|
6265
|
+
traceContextSigningSecret;
|
|
6266
|
+
loaderLoginCache = /* @__PURE__ */ new WeakMap();
|
|
6267
|
+
loginParams;
|
|
6268
|
+
activeLoginOrgNameSelector;
|
|
6155
6269
|
constructor(loginParams) {
|
|
6156
|
-
this.loginParams = loginParams;
|
|
6270
|
+
this.loginParams = { ...loginParams };
|
|
6271
|
+
this.activeLoginOrgNameSelector = loginParams.orgName ?? isomorph_default.getEnv("BRAINTRUST_ORG_NAME");
|
|
6157
6272
|
this.id = `${(/* @__PURE__ */ new Date()).toLocaleString()}-${stateNonce++}`;
|
|
6158
6273
|
this.currentExperiment = void 0;
|
|
6159
6274
|
this.currentLogger = void 0;
|
|
@@ -6190,12 +6305,14 @@ var BraintrustState = class _BraintrustState {
|
|
|
6190
6305
|
const {
|
|
6191
6306
|
memoryCache: parametersMemoryCache,
|
|
6192
6307
|
diskCache: parametersDiskCache
|
|
6193
|
-
} = createCacheLayers(
|
|
6194
|
-
|
|
6195
|
-
|
|
6196
|
-
|
|
6197
|
-
|
|
6198
|
-
|
|
6308
|
+
} = createCacheLayers(
|
|
6309
|
+
{
|
|
6310
|
+
memoryMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX",
|
|
6311
|
+
diskCacheDirEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DIR",
|
|
6312
|
+
diskMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DISK_MAX",
|
|
6313
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`
|
|
6314
|
+
}
|
|
6315
|
+
);
|
|
6199
6316
|
this.parametersCache = new ParametersCache({
|
|
6200
6317
|
memoryCache: parametersMemoryCache,
|
|
6201
6318
|
diskCache: parametersDiskCache
|
|
@@ -6204,43 +6321,6 @@ var BraintrustState = class _BraintrustState {
|
|
|
6204
6321
|
this.spanOriginEnvironment = detectSpanOriginEnvironment();
|
|
6205
6322
|
this._internalSetTraceContextSigningSecret(loginParams.apiKey);
|
|
6206
6323
|
}
|
|
6207
|
-
loginParams;
|
|
6208
|
-
id;
|
|
6209
|
-
currentExperiment;
|
|
6210
|
-
// Note: the value of IsAsyncFlush doesn't really matter here, since we
|
|
6211
|
-
// (safely) dynamically cast it whenever retrieving the logger.
|
|
6212
|
-
currentLogger;
|
|
6213
|
-
currentParent;
|
|
6214
|
-
currentSpan;
|
|
6215
|
-
// Any time we re-log in, we directly update the apiConn inside the logger.
|
|
6216
|
-
// This is preferable to replacing the whole logger, which would create the
|
|
6217
|
-
// possibility of multiple loggers floating around, which may not log in a
|
|
6218
|
-
// deterministic order.
|
|
6219
|
-
_bgLogger;
|
|
6220
|
-
_overrideBgLogger = null;
|
|
6221
|
-
appUrl = null;
|
|
6222
|
-
appPublicUrl = null;
|
|
6223
|
-
loginToken = null;
|
|
6224
|
-
orgId = null;
|
|
6225
|
-
orgName = null;
|
|
6226
|
-
apiUrl = null;
|
|
6227
|
-
proxyUrl = null;
|
|
6228
|
-
loggedIn = false;
|
|
6229
|
-
gitMetadataSettings;
|
|
6230
|
-
debugLogLevel;
|
|
6231
|
-
debugLogLevelConfigured = false;
|
|
6232
|
-
fetch = globalThis.fetch;
|
|
6233
|
-
_appConn = null;
|
|
6234
|
-
_apiConn = null;
|
|
6235
|
-
_proxyConn = null;
|
|
6236
|
-
promptCache;
|
|
6237
|
-
parametersCache;
|
|
6238
|
-
spanCache;
|
|
6239
|
-
_idGenerator = null;
|
|
6240
|
-
_contextManager = null;
|
|
6241
|
-
_otelFlushCallback = null;
|
|
6242
|
-
spanOriginEnvironment;
|
|
6243
|
-
traceContextSigningSecret;
|
|
6244
6324
|
/** @internal */
|
|
6245
6325
|
_internalSetTraceContextSigningSecret(secret) {
|
|
6246
6326
|
const normalizedSecret = secret?.trim();
|
|
@@ -6265,6 +6345,101 @@ var BraintrustState = class _BraintrustState {
|
|
|
6265
6345
|
this._appConn = null;
|
|
6266
6346
|
this._apiConn = null;
|
|
6267
6347
|
this._proxyConn = null;
|
|
6348
|
+
this.loaderLoginCache = /* @__PURE__ */ new WeakMap();
|
|
6349
|
+
}
|
|
6350
|
+
/** @internal */
|
|
6351
|
+
async _internalResolveLoaderLoginOptions({
|
|
6352
|
+
apiKey,
|
|
6353
|
+
appUrl,
|
|
6354
|
+
orgName,
|
|
6355
|
+
fetch: fetch2,
|
|
6356
|
+
forceLogin
|
|
6357
|
+
}) {
|
|
6358
|
+
const resolvedAppUrl = appUrl ?? (this.loggedIn ? this.appUrl ?? void 0 : void 0) ?? this.loginParams.appUrl ?? isomorph_default.getEnv("BRAINTRUST_APP_URL") ?? "https://www.braintrust.dev";
|
|
6359
|
+
const resolvedApiKey = apiKey ?? (this.loggedIn ? this.loginToken ?? void 0 : void 0) ?? this.loginParams.apiKey ?? await isomorph_default.getBraintrustApiKey();
|
|
6360
|
+
if (!resolvedApiKey) {
|
|
6361
|
+
throw new Error(
|
|
6362
|
+
"Please specify an api key (e.g. by setting BRAINTRUST_API_KEY)."
|
|
6363
|
+
);
|
|
6364
|
+
}
|
|
6365
|
+
const normalizedApiKey = HTTPConnection.sanitize_token(resolvedApiKey);
|
|
6366
|
+
const usesActiveCredential = this.loggedIn && normalizedApiKey === this.loginToken;
|
|
6367
|
+
const requestedOrgName = orgName ?? (usesActiveCredential ? this.activeLoginOrgNameSelector : void 0) ?? this.loginParams.orgName ?? isomorph_default.getEnv("BRAINTRUST_ORG_NAME");
|
|
6368
|
+
const resolvedOrgName = orgName ?? (usesActiveCredential ? this.orgName ?? void 0 : void 0) ?? requestedOrgName;
|
|
6369
|
+
const resolvedFetch = fetch2 ?? (this.loggedIn ? this.fetch : void 0) ?? this.loginParams.fetch ?? globalThis.fetch;
|
|
6370
|
+
const credentialCacheNamespace = JSON.stringify([
|
|
6371
|
+
"loader-credential",
|
|
6372
|
+
resolvedAppUrl,
|
|
6373
|
+
requestedOrgName,
|
|
6374
|
+
normalizedApiKey
|
|
6375
|
+
]);
|
|
6376
|
+
return {
|
|
6377
|
+
apiKey: normalizedApiKey,
|
|
6378
|
+
appUrl: resolvedAppUrl,
|
|
6379
|
+
orgName: resolvedOrgName,
|
|
6380
|
+
fetch: resolvedFetch,
|
|
6381
|
+
forceLogin,
|
|
6382
|
+
credentialCacheNamespace,
|
|
6383
|
+
existingState: !forceLogin && usesActiveCredential && resolvedAppUrl === this.appUrl && resolvedOrgName === this.orgName && resolvedFetch === this.fetch ? this : void 0
|
|
6384
|
+
};
|
|
6385
|
+
}
|
|
6386
|
+
/** @internal */
|
|
6387
|
+
_internalGetLoaderCacheViews(loginOptions, requestState) {
|
|
6388
|
+
const expectedResolvedOrgIdentity = requestState?.orgId && requestState.appUrl ? JSON.stringify([
|
|
6389
|
+
"loader-org",
|
|
6390
|
+
requestState.appUrl,
|
|
6391
|
+
requestState.orgId
|
|
6392
|
+
]) : void 0;
|
|
6393
|
+
return {
|
|
6394
|
+
promptCache: this.promptCache.withNamespace(
|
|
6395
|
+
loginOptions.credentialCacheNamespace,
|
|
6396
|
+
expectedResolvedOrgIdentity
|
|
6397
|
+
),
|
|
6398
|
+
parametersCache: this.parametersCache.withNamespace(
|
|
6399
|
+
loginOptions.credentialCacheNamespace,
|
|
6400
|
+
expectedResolvedOrgIdentity
|
|
6401
|
+
)
|
|
6402
|
+
};
|
|
6403
|
+
}
|
|
6404
|
+
/** @internal */
|
|
6405
|
+
async _internalGetLoaderState({
|
|
6406
|
+
apiKey,
|
|
6407
|
+
appUrl,
|
|
6408
|
+
orgName,
|
|
6409
|
+
fetch: fetch2,
|
|
6410
|
+
forceLogin,
|
|
6411
|
+
existingState
|
|
6412
|
+
}) {
|
|
6413
|
+
if (existingState) {
|
|
6414
|
+
return existingState;
|
|
6415
|
+
}
|
|
6416
|
+
let cache = this.loaderLoginCache.get(fetch2);
|
|
6417
|
+
if (!cache) {
|
|
6418
|
+
cache = new LRUCache({ max: LOADER_LOGIN_CACHE_MAX });
|
|
6419
|
+
this.loaderLoginCache.set(fetch2, cache);
|
|
6420
|
+
}
|
|
6421
|
+
const cacheKey = JSON.stringify([appUrl, orgName, apiKey]);
|
|
6422
|
+
if (!forceLogin) {
|
|
6423
|
+
const cachedState = cache.get(cacheKey);
|
|
6424
|
+
if (cachedState) {
|
|
6425
|
+
return cachedState;
|
|
6426
|
+
}
|
|
6427
|
+
}
|
|
6428
|
+
const statePromise = loginToLoaderRequestState({
|
|
6429
|
+
orgName,
|
|
6430
|
+
apiKey,
|
|
6431
|
+
appUrl,
|
|
6432
|
+
fetch: fetch2
|
|
6433
|
+
});
|
|
6434
|
+
cache.set(cacheKey, statePromise);
|
|
6435
|
+
try {
|
|
6436
|
+
return await statePromise;
|
|
6437
|
+
} catch (error) {
|
|
6438
|
+
if (cache.get(cacheKey) === statePromise) {
|
|
6439
|
+
cache.delete(cacheKey);
|
|
6440
|
+
}
|
|
6441
|
+
throw error;
|
|
6442
|
+
}
|
|
6268
6443
|
}
|
|
6269
6444
|
resetIdGenState() {
|
|
6270
6445
|
this._idGenerator = null;
|
|
@@ -6313,6 +6488,8 @@ var BraintrustState = class _BraintrustState {
|
|
|
6313
6488
|
this.debugLogLevel = other.debugLogLevel;
|
|
6314
6489
|
this.debugLogLevelConfigured = other.debugLogLevelConfigured;
|
|
6315
6490
|
this.traceContextSigningSecret = other.traceContextSigningSecret;
|
|
6491
|
+
this.fetch = other.fetch;
|
|
6492
|
+
this.activeLoginOrgNameSelector = other.activeLoginOrgNameSelector;
|
|
6316
6493
|
setGlobalDebugLogLevel(
|
|
6317
6494
|
this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
|
|
6318
6495
|
);
|
|
@@ -6552,36 +6729,85 @@ var FailedHTTPResponse = class extends Error {
|
|
|
6552
6729
|
status;
|
|
6553
6730
|
text;
|
|
6554
6731
|
data;
|
|
6555
|
-
|
|
6732
|
+
cause;
|
|
6733
|
+
constructor(status, text, data, cause) {
|
|
6556
6734
|
super(`${status}: ${text} (${data})`);
|
|
6557
6735
|
this.status = status;
|
|
6558
6736
|
this.text = text;
|
|
6559
6737
|
this.data = data;
|
|
6738
|
+
this.cause = cause;
|
|
6739
|
+
}
|
|
6740
|
+
};
|
|
6741
|
+
var HTTPTransportError = class extends Error {
|
|
6742
|
+
cause;
|
|
6743
|
+
constructor(cause) {
|
|
6744
|
+
super(cause instanceof Error ? cause.message : String(cause));
|
|
6745
|
+
this.name = "HTTPTransportError";
|
|
6746
|
+
this.cause = cause;
|
|
6560
6747
|
}
|
|
6561
6748
|
};
|
|
6749
|
+
var httpTransportErrorCauses = /* @__PURE__ */ new WeakSet();
|
|
6750
|
+
function recordHTTPTransportError(error) {
|
|
6751
|
+
if (typeof error === "object" && error !== null || typeof error === "function") {
|
|
6752
|
+
httpTransportErrorCauses.add(error);
|
|
6753
|
+
}
|
|
6754
|
+
}
|
|
6755
|
+
function rethrowHTTPTransportError(error, classifyTransportErrors) {
|
|
6756
|
+
if (classifyTransportErrors) {
|
|
6757
|
+
throw new HTTPTransportError(error);
|
|
6758
|
+
}
|
|
6759
|
+
recordHTTPTransportError(error);
|
|
6760
|
+
throw error;
|
|
6761
|
+
}
|
|
6762
|
+
function isLoaderCacheFallbackError(error) {
|
|
6763
|
+
if (error instanceof FailedHTTPResponse) {
|
|
6764
|
+
return error.status === 408 || error.status === 429 || error.status >= 500;
|
|
6765
|
+
}
|
|
6766
|
+
if (error instanceof HTTPTransportError) {
|
|
6767
|
+
return true;
|
|
6768
|
+
}
|
|
6769
|
+
return (typeof error === "object" && error !== null || typeof error === "function") && httpTransportErrorCauses.has(error);
|
|
6770
|
+
}
|
|
6771
|
+
async function readJSONResponse(response, classifyTransportErrors = false) {
|
|
6772
|
+
let data;
|
|
6773
|
+
try {
|
|
6774
|
+
data = await response.text();
|
|
6775
|
+
} catch (error) {
|
|
6776
|
+
rethrowHTTPTransportError(error, classifyTransportErrors);
|
|
6777
|
+
}
|
|
6778
|
+
return JSON.parse(data);
|
|
6779
|
+
}
|
|
6562
6780
|
async function checkResponse(resp) {
|
|
6563
6781
|
if (resp.ok) {
|
|
6564
6782
|
return resp;
|
|
6565
|
-
}
|
|
6783
|
+
}
|
|
6784
|
+
let data;
|
|
6785
|
+
try {
|
|
6786
|
+
data = await resp.text();
|
|
6787
|
+
} catch (error) {
|
|
6566
6788
|
throw new FailedHTTPResponse(
|
|
6567
6789
|
resp.status,
|
|
6568
6790
|
resp.statusText,
|
|
6569
|
-
|
|
6791
|
+
"Unable to read response body",
|
|
6792
|
+
error
|
|
6570
6793
|
);
|
|
6571
6794
|
}
|
|
6795
|
+
throw new FailedHTTPResponse(resp.status, resp.statusText, data);
|
|
6572
6796
|
}
|
|
6573
6797
|
var HTTPConnection = class _HTTPConnection {
|
|
6574
|
-
base_url
|
|
6575
|
-
|
|
6576
|
-
headers;
|
|
6577
|
-
fetch;
|
|
6578
|
-
constructor(base_url, fetch2) {
|
|
6798
|
+
constructor(base_url, fetch2, classifyTransportErrors = false) {
|
|
6799
|
+
this.classifyTransportErrors = classifyTransportErrors;
|
|
6579
6800
|
this.base_url = base_url;
|
|
6580
6801
|
this.token = null;
|
|
6581
6802
|
this.headers = {};
|
|
6582
6803
|
this._reset();
|
|
6583
6804
|
this.fetch = fetch2;
|
|
6584
6805
|
}
|
|
6806
|
+
classifyTransportErrors;
|
|
6807
|
+
base_url;
|
|
6808
|
+
token;
|
|
6809
|
+
headers;
|
|
6810
|
+
fetch;
|
|
6585
6811
|
setFetch(fetch2) {
|
|
6586
6812
|
this.fetch = fetch2;
|
|
6587
6813
|
}
|
|
@@ -6621,9 +6847,9 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6621
6847
|
).toString();
|
|
6622
6848
|
const this_fetch = this.fetch;
|
|
6623
6849
|
const this_headers = this.headers;
|
|
6624
|
-
|
|
6625
|
-
|
|
6626
|
-
await this_fetch(url.toString(), {
|
|
6850
|
+
let response;
|
|
6851
|
+
try {
|
|
6852
|
+
response = await this_fetch(url.toString(), {
|
|
6627
6853
|
headers: {
|
|
6628
6854
|
Accept: "application/json",
|
|
6629
6855
|
...this_headers,
|
|
@@ -6631,8 +6857,14 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6631
6857
|
},
|
|
6632
6858
|
keepalive: true,
|
|
6633
6859
|
...rest
|
|
6634
|
-
})
|
|
6635
|
-
)
|
|
6860
|
+
});
|
|
6861
|
+
} catch (error) {
|
|
6862
|
+
if (config?.signal?.aborted) {
|
|
6863
|
+
throw getAbortReason(config.signal);
|
|
6864
|
+
}
|
|
6865
|
+
rethrowHTTPTransportError(error, this.classifyTransportErrors);
|
|
6866
|
+
}
|
|
6867
|
+
return await checkResponse(response);
|
|
6636
6868
|
}
|
|
6637
6869
|
async post(path, params, config, retries = 0) {
|
|
6638
6870
|
const { headers, ...rest } = config || {};
|
|
@@ -6642,8 +6874,9 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6642
6874
|
const tries = retries + 1;
|
|
6643
6875
|
for (let i = 0; i < tries; i++) {
|
|
6644
6876
|
try {
|
|
6645
|
-
|
|
6646
|
-
|
|
6877
|
+
let response;
|
|
6878
|
+
try {
|
|
6879
|
+
response = await this_fetch(_urljoin(this_base_url, path), {
|
|
6647
6880
|
method: "POST",
|
|
6648
6881
|
headers: {
|
|
6649
6882
|
Accept: "application/json",
|
|
@@ -6654,8 +6887,14 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6654
6887
|
body: typeof params === "string" ? params : params ? JSON.stringify(params) : void 0,
|
|
6655
6888
|
keepalive: true,
|
|
6656
6889
|
...rest
|
|
6657
|
-
})
|
|
6658
|
-
)
|
|
6890
|
+
});
|
|
6891
|
+
} catch (error) {
|
|
6892
|
+
if (config?.signal?.aborted) {
|
|
6893
|
+
throw getAbortReason(config.signal);
|
|
6894
|
+
}
|
|
6895
|
+
rethrowHTTPTransportError(error, this.classifyTransportErrors);
|
|
6896
|
+
}
|
|
6897
|
+
return await checkResponse(response);
|
|
6659
6898
|
} catch (error) {
|
|
6660
6899
|
if (config?.signal?.aborted) {
|
|
6661
6900
|
throw getAbortReason(config.signal);
|
|
@@ -6680,7 +6919,7 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6680
6919
|
for (let i = 0; i < tries; i++) {
|
|
6681
6920
|
try {
|
|
6682
6921
|
const resp = await this.get(`${object_type}`, args);
|
|
6683
|
-
return await resp.
|
|
6922
|
+
return await readJSONResponse(resp, this.classifyTransportErrors);
|
|
6684
6923
|
} catch (e) {
|
|
6685
6924
|
if (i < tries - 1) {
|
|
6686
6925
|
debugLogger.debug(
|
|
@@ -6703,7 +6942,7 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6703
6942
|
const resp = await this.post(`${object_type}`, args, {
|
|
6704
6943
|
headers: { "Content-Type": "application/json" }
|
|
6705
6944
|
});
|
|
6706
|
-
return await resp.
|
|
6945
|
+
return await readJSONResponse(resp, this.classifyTransportErrors);
|
|
6707
6946
|
}
|
|
6708
6947
|
// Custom inspect for Node.js console.log
|
|
6709
6948
|
[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
|
|
@@ -8751,6 +8990,7 @@ function initDataset(projectOrOptions, optionalOptions) {
|
|
|
8751
8990
|
const {
|
|
8752
8991
|
project,
|
|
8753
8992
|
dataset,
|
|
8993
|
+
datasetId,
|
|
8754
8994
|
description,
|
|
8755
8995
|
version,
|
|
8756
8996
|
snapshotName,
|
|
@@ -8766,6 +9006,11 @@ function initDataset(projectOrOptions, optionalOptions) {
|
|
|
8766
9006
|
state: stateArg,
|
|
8767
9007
|
_internal_btql
|
|
8768
9008
|
} = options;
|
|
9009
|
+
if (datasetId !== void 0 && (description !== void 0 || metadata !== void 0)) {
|
|
9010
|
+
throw new Error(
|
|
9011
|
+
"Cannot specify description or metadata when datasetId is provided"
|
|
9012
|
+
);
|
|
9013
|
+
}
|
|
8769
9014
|
const selection = normalizeDatasetSelection({
|
|
8770
9015
|
version,
|
|
8771
9016
|
environment,
|
|
@@ -8786,6 +9031,35 @@ function initDataset(projectOrOptions, optionalOptions) {
|
|
|
8786
9031
|
fetch: fetch2,
|
|
8787
9032
|
forceLogin
|
|
8788
9033
|
});
|
|
9034
|
+
if (datasetId !== void 0) {
|
|
9035
|
+
const objectInfo = datasetObjectInfoSchema.array().parse(
|
|
9036
|
+
await state.appConn().post_json("api/self/get_object_info", {
|
|
9037
|
+
object_type: "dataset",
|
|
9038
|
+
object_ids: [datasetId]
|
|
9039
|
+
})
|
|
9040
|
+
);
|
|
9041
|
+
if (objectInfo.length === 0) {
|
|
9042
|
+
throw new Error(`Dataset with ID ${datasetId} not found`);
|
|
9043
|
+
}
|
|
9044
|
+
if (objectInfo.length !== 1) {
|
|
9045
|
+
throw new Error(
|
|
9046
|
+
`Expected exactly one dataset with ID ${datasetId}, but found ${objectInfo.length}`
|
|
9047
|
+
);
|
|
9048
|
+
}
|
|
9049
|
+
const datasetInfo = objectInfo[0];
|
|
9050
|
+
return {
|
|
9051
|
+
project: {
|
|
9052
|
+
id: datasetInfo.parent_cols.project.id,
|
|
9053
|
+
name: datasetInfo.parent_cols.project.name,
|
|
9054
|
+
fullInfo: datasetInfo.parent_cols.project
|
|
9055
|
+
},
|
|
9056
|
+
dataset: {
|
|
9057
|
+
id: datasetInfo.object_id,
|
|
9058
|
+
name: datasetInfo.object_name,
|
|
9059
|
+
fullInfo: datasetInfo
|
|
9060
|
+
}
|
|
9061
|
+
};
|
|
9062
|
+
}
|
|
8789
9063
|
const args = {
|
|
8790
9064
|
org_id: state.orgId,
|
|
8791
9065
|
project_name: project,
|
|
@@ -8939,6 +9213,24 @@ function initLogger(options = {}) {
|
|
|
8939
9213
|
}
|
|
8940
9214
|
return ret;
|
|
8941
9215
|
}
|
|
9216
|
+
async function runCredentialScopedLoaderRequest(state, loginOptions, request) {
|
|
9217
|
+
const resolvedLoginOptions = await state._internalResolveLoaderLoginOptions(loginOptions);
|
|
9218
|
+
let cacheViews = state._internalGetLoaderCacheViews(resolvedLoginOptions);
|
|
9219
|
+
try {
|
|
9220
|
+
const requestState = await state._internalGetLoaderState(resolvedLoginOptions);
|
|
9221
|
+
cacheViews = state._internalGetLoaderCacheViews(
|
|
9222
|
+
resolvedLoginOptions,
|
|
9223
|
+
requestState
|
|
9224
|
+
);
|
|
9225
|
+
return {
|
|
9226
|
+
ok: true,
|
|
9227
|
+
response: await request(requestState),
|
|
9228
|
+
...cacheViews
|
|
9229
|
+
};
|
|
9230
|
+
} catch (error) {
|
|
9231
|
+
return { ok: false, error, ...cacheViews };
|
|
9232
|
+
}
|
|
9233
|
+
}
|
|
8942
9234
|
async function loadPrompt({
|
|
8943
9235
|
projectName,
|
|
8944
9236
|
projectId,
|
|
@@ -8963,43 +9255,47 @@ async function loadPrompt({
|
|
|
8963
9255
|
throw new Error("Must specify slug");
|
|
8964
9256
|
}
|
|
8965
9257
|
const state = stateArg ?? _globalState;
|
|
8966
|
-
|
|
8967
|
-
|
|
8968
|
-
|
|
8969
|
-
|
|
8970
|
-
|
|
8971
|
-
|
|
8972
|
-
|
|
8973
|
-
|
|
8974
|
-
|
|
8975
|
-
|
|
8976
|
-
|
|
8977
|
-
|
|
8978
|
-
|
|
9258
|
+
const result = await runCredentialScopedLoaderRequest(
|
|
9259
|
+
state,
|
|
9260
|
+
{ orgName, apiKey, appUrl, fetch: fetch2, forceLogin },
|
|
9261
|
+
async (requestState) => {
|
|
9262
|
+
let response2;
|
|
9263
|
+
if (id) {
|
|
9264
|
+
response2 = await requestState.apiConn().get_json(`v1/prompt/${id}`, versionOrEnvironment);
|
|
9265
|
+
if (response2) {
|
|
9266
|
+
response2 = { objects: [response2] };
|
|
9267
|
+
}
|
|
9268
|
+
} else {
|
|
9269
|
+
response2 = await requestState.apiConn().get_json("v1/prompt", {
|
|
9270
|
+
project_name: projectName,
|
|
9271
|
+
project_id: projectId,
|
|
9272
|
+
slug,
|
|
9273
|
+
...versionOrEnvironment
|
|
9274
|
+
});
|
|
8979
9275
|
}
|
|
8980
|
-
|
|
8981
|
-
|
|
8982
|
-
|
|
8983
|
-
|
|
8984
|
-
|
|
8985
|
-
|
|
8986
|
-
|
|
9276
|
+
return response2;
|
|
9277
|
+
}
|
|
9278
|
+
);
|
|
9279
|
+
const { promptCache } = result;
|
|
9280
|
+
if (!result.ok) {
|
|
9281
|
+
const e = result.error;
|
|
9282
|
+
if (!isLoaderCacheFallbackError(e)) {
|
|
9283
|
+
throw e;
|
|
8987
9284
|
}
|
|
8988
|
-
} catch (e) {
|
|
8989
9285
|
if (version || environment) {
|
|
8990
9286
|
throw new Error(`Prompt not found with specified parameters: ${e}`);
|
|
8991
9287
|
}
|
|
8992
9288
|
debugLogger.forState(state).warn("Failed to load prompt, attempting to fall back to cache:", e);
|
|
8993
9289
|
let prompt2;
|
|
8994
9290
|
if (id) {
|
|
8995
|
-
prompt2 = await
|
|
9291
|
+
prompt2 = await promptCache.get({ id });
|
|
8996
9292
|
if (!prompt2) {
|
|
8997
9293
|
throw new Error(
|
|
8998
9294
|
`Prompt with id ${id} not found (not found on server or in local cache): ${e}`
|
|
8999
9295
|
);
|
|
9000
9296
|
}
|
|
9001
9297
|
} else {
|
|
9002
|
-
prompt2 = await
|
|
9298
|
+
prompt2 = await promptCache.get({
|
|
9003
9299
|
slug,
|
|
9004
9300
|
projectId,
|
|
9005
9301
|
projectName,
|
|
@@ -9015,6 +9311,7 @@ async function loadPrompt({
|
|
|
9015
9311
|
}
|
|
9016
9312
|
return prompt2;
|
|
9017
9313
|
}
|
|
9314
|
+
const { response } = result;
|
|
9018
9315
|
if (!("objects" in response) || response.objects.length === 0) {
|
|
9019
9316
|
if (id) {
|
|
9020
9317
|
throw new Error(`Prompt with id ${id} not found.`);
|
|
@@ -9038,9 +9335,9 @@ async function loadPrompt({
|
|
|
9038
9335
|
const prompt = new Prompt2(metadata, defaults || {}, noTrace);
|
|
9039
9336
|
try {
|
|
9040
9337
|
if (id) {
|
|
9041
|
-
await
|
|
9338
|
+
await promptCache.set({ id }, prompt);
|
|
9042
9339
|
} else if (slug) {
|
|
9043
|
-
await
|
|
9340
|
+
await promptCache.set(
|
|
9044
9341
|
{ slug, projectId, projectName, version: version ?? "latest" },
|
|
9045
9342
|
prompt
|
|
9046
9343
|
);
|
|
@@ -9072,46 +9369,50 @@ async function loadParameters({
|
|
|
9072
9369
|
throw new Error("Must specify slug");
|
|
9073
9370
|
}
|
|
9074
9371
|
const state = stateArg ?? _globalState;
|
|
9075
|
-
|
|
9076
|
-
|
|
9077
|
-
|
|
9078
|
-
|
|
9079
|
-
|
|
9080
|
-
|
|
9081
|
-
|
|
9082
|
-
|
|
9083
|
-
|
|
9084
|
-
|
|
9085
|
-
|
|
9086
|
-
|
|
9087
|
-
}
|
|
9088
|
-
|
|
9089
|
-
|
|
9372
|
+
const result = await runCredentialScopedLoaderRequest(
|
|
9373
|
+
state,
|
|
9374
|
+
{ orgName, apiKey, appUrl, fetch: fetch2, forceLogin },
|
|
9375
|
+
async (requestState) => {
|
|
9376
|
+
let response2;
|
|
9377
|
+
if (id) {
|
|
9378
|
+
response2 = await requestState.apiConn().get_json(`v1/function/${id}`, {
|
|
9379
|
+
...versionOrEnvironment
|
|
9380
|
+
});
|
|
9381
|
+
if (response2) {
|
|
9382
|
+
response2 = { objects: [response2] };
|
|
9383
|
+
}
|
|
9384
|
+
} else {
|
|
9385
|
+
response2 = await requestState.apiConn().get_json("v1/function", {
|
|
9386
|
+
project_name: projectName,
|
|
9387
|
+
project_id: projectId,
|
|
9388
|
+
slug,
|
|
9389
|
+
function_type: "parameters",
|
|
9390
|
+
...versionOrEnvironment
|
|
9391
|
+
});
|
|
9090
9392
|
}
|
|
9091
|
-
|
|
9092
|
-
|
|
9093
|
-
|
|
9094
|
-
|
|
9095
|
-
|
|
9096
|
-
|
|
9097
|
-
|
|
9098
|
-
|
|
9393
|
+
return response2;
|
|
9394
|
+
}
|
|
9395
|
+
);
|
|
9396
|
+
const { parametersCache } = result;
|
|
9397
|
+
if (!result.ok) {
|
|
9398
|
+
const e = result.error;
|
|
9399
|
+
if (!isLoaderCacheFallbackError(e)) {
|
|
9400
|
+
throw e;
|
|
9099
9401
|
}
|
|
9100
|
-
} catch (e) {
|
|
9101
9402
|
if (version || environment) {
|
|
9102
9403
|
throw new Error(`Parameters not found with specified parameters: ${e}`);
|
|
9103
9404
|
}
|
|
9104
9405
|
debugLogger.forState(state).warn("Failed to load parameters, attempting to fall back to cache:", e);
|
|
9105
9406
|
let parameters2;
|
|
9106
9407
|
if (id) {
|
|
9107
|
-
parameters2 = await
|
|
9408
|
+
parameters2 = await parametersCache.get({ id });
|
|
9108
9409
|
if (!parameters2) {
|
|
9109
9410
|
throw new Error(
|
|
9110
9411
|
`Parameters with id ${id} not found (not found on server or in local cache): ${e}`
|
|
9111
9412
|
);
|
|
9112
9413
|
}
|
|
9113
9414
|
} else {
|
|
9114
|
-
parameters2 = await
|
|
9415
|
+
parameters2 = await parametersCache.get({
|
|
9115
9416
|
slug,
|
|
9116
9417
|
projectId,
|
|
9117
9418
|
projectName,
|
|
@@ -9127,6 +9428,7 @@ async function loadParameters({
|
|
|
9127
9428
|
}
|
|
9128
9429
|
return parameters2;
|
|
9129
9430
|
}
|
|
9431
|
+
const { response } = result;
|
|
9130
9432
|
if (!("objects" in response) || response.objects.length === 0) {
|
|
9131
9433
|
if (id) {
|
|
9132
9434
|
throw new Error(`Parameters with id ${id} not found.`);
|
|
@@ -9150,9 +9452,9 @@ async function loadParameters({
|
|
|
9150
9452
|
const parameters = new RemoteEvalParameters(metadata);
|
|
9151
9453
|
try {
|
|
9152
9454
|
if (id) {
|
|
9153
|
-
await
|
|
9455
|
+
await parametersCache.set({ id }, parameters);
|
|
9154
9456
|
} else if (slug) {
|
|
9155
|
-
await
|
|
9457
|
+
await parametersCache.set(
|
|
9156
9458
|
{ slug, projectId, projectName, version: version ?? "latest" },
|
|
9157
9459
|
parameters
|
|
9158
9460
|
);
|
|
@@ -9193,6 +9495,52 @@ async function login(options = {}) {
|
|
|
9193
9495
|
await state.login(options);
|
|
9194
9496
|
return state;
|
|
9195
9497
|
}
|
|
9498
|
+
async function loginToLoaderRequestState({
|
|
9499
|
+
appUrl,
|
|
9500
|
+
apiKey,
|
|
9501
|
+
orgName,
|
|
9502
|
+
fetch: fetch2
|
|
9503
|
+
}) {
|
|
9504
|
+
let orgId;
|
|
9505
|
+
let apiUrl;
|
|
9506
|
+
if (apiKey === TEST_API_KEY) {
|
|
9507
|
+
orgId = "test-org-id";
|
|
9508
|
+
apiUrl = "https://braintrust.dev/fake-api-url";
|
|
9509
|
+
} else {
|
|
9510
|
+
let loginResponse;
|
|
9511
|
+
try {
|
|
9512
|
+
loginResponse = await fetch2(_urljoin(appUrl, `/api/apikey/login`), {
|
|
9513
|
+
method: "POST",
|
|
9514
|
+
headers: {
|
|
9515
|
+
"Content-Type": "application/json",
|
|
9516
|
+
Authorization: `Bearer ${apiKey}`
|
|
9517
|
+
}
|
|
9518
|
+
});
|
|
9519
|
+
} catch (error) {
|
|
9520
|
+
throw new HTTPTransportError(error);
|
|
9521
|
+
}
|
|
9522
|
+
const info = await readJSONResponse(
|
|
9523
|
+
await checkResponse(loginResponse),
|
|
9524
|
+
true
|
|
9525
|
+
);
|
|
9526
|
+
const org = selectLoginOrg(info.org_info, orgName);
|
|
9527
|
+
orgId = org.id;
|
|
9528
|
+
apiUrl = isomorph_default.getEnv("BRAINTRUST_API_URL") ?? org.api_url;
|
|
9529
|
+
if (!apiUrl) {
|
|
9530
|
+
throw new Error(
|
|
9531
|
+
orgName ? `Unable to log into organization '${orgName}'. Are you sure this credential is scoped to the organization?` : "Unable to log into any organization with the provided credential."
|
|
9532
|
+
);
|
|
9533
|
+
}
|
|
9534
|
+
}
|
|
9535
|
+
const apiConnection = new HTTPConnection(apiUrl, fetch2, true);
|
|
9536
|
+
apiConnection.set_token(apiKey);
|
|
9537
|
+
apiConnection.make_long_lived();
|
|
9538
|
+
return {
|
|
9539
|
+
appUrl,
|
|
9540
|
+
orgId,
|
|
9541
|
+
apiConn: () => apiConnection
|
|
9542
|
+
};
|
|
9543
|
+
}
|
|
9196
9544
|
async function loginToState(options = {}) {
|
|
9197
9545
|
const {
|
|
9198
9546
|
appUrl = isomorph_default.getEnv("BRAINTRUST_APP_URL") || "https://www.braintrust.dev",
|
|
@@ -9224,16 +9572,18 @@ async function loginToState(options = {}) {
|
|
|
9224
9572
|
_saveOrgInfo(state, testOrgInfo, testOrgInfo[0].name);
|
|
9225
9573
|
return state;
|
|
9226
9574
|
} else {
|
|
9227
|
-
const
|
|
9228
|
-
|
|
9575
|
+
const loginResponse = await fetch2(
|
|
9576
|
+
_urljoin(state.appUrl, `/api/apikey/login`),
|
|
9577
|
+
{
|
|
9229
9578
|
method: "POST",
|
|
9230
9579
|
headers: {
|
|
9231
9580
|
"Content-Type": "application/json",
|
|
9232
9581
|
Authorization: `Bearer ${apiKey}`
|
|
9233
9582
|
}
|
|
9234
|
-
}
|
|
9583
|
+
}
|
|
9235
9584
|
);
|
|
9236
|
-
const
|
|
9585
|
+
const resp = await checkResponse(loginResponse);
|
|
9586
|
+
const info = await readJSONResponse(resp);
|
|
9237
9587
|
_saveOrgInfo(state, info.org_info, orgName);
|
|
9238
9588
|
if (!state.apiUrl) {
|
|
9239
9589
|
if (orgName) {
|
|
@@ -9736,11 +10086,11 @@ function wrapTraced(fn, args) {
|
|
|
9736
10086
|
}
|
|
9737
10087
|
if (args?.asyncFlush) {
|
|
9738
10088
|
return ((...fnArgs) => traced((span) => {
|
|
9739
|
-
if (!hasExplicitInput) {
|
|
10089
|
+
if (!args?.noTraceIO && !hasExplicitInput) {
|
|
9740
10090
|
span.log({ input: fnArgs });
|
|
9741
10091
|
}
|
|
9742
10092
|
const output = fn(...fnArgs);
|
|
9743
|
-
if (!hasExplicitOutput) {
|
|
10093
|
+
if (!args?.noTraceIO && !hasExplicitOutput) {
|
|
9744
10094
|
if (output instanceof Promise) {
|
|
9745
10095
|
return (async () => {
|
|
9746
10096
|
const result = await output;
|
|
@@ -9755,12 +10105,12 @@ function wrapTraced(fn, args) {
|
|
|
9755
10105
|
}, spanArgs));
|
|
9756
10106
|
} else {
|
|
9757
10107
|
return ((...fnArgs) => traced(async (span) => {
|
|
9758
|
-
if (!hasExplicitInput) {
|
|
10108
|
+
if (!args?.noTraceIO && !hasExplicitInput) {
|
|
9759
10109
|
span.log({ input: fnArgs });
|
|
9760
10110
|
}
|
|
9761
10111
|
const outputResult = fn(...fnArgs);
|
|
9762
10112
|
const output = await outputResult;
|
|
9763
|
-
if (!hasExplicitOutput) {
|
|
10113
|
+
if (!args?.noTraceIO && !hasExplicitOutput) {
|
|
9764
10114
|
span.log({ output });
|
|
9765
10115
|
}
|
|
9766
10116
|
return output;
|
|
@@ -9886,27 +10236,28 @@ async function* asyncGeneratorWithCurrent(span, gen, state = void 0) {
|
|
|
9886
10236
|
function withParent(parent, callback, state = void 0) {
|
|
9887
10237
|
return (state ?? _globalState).currentParent.run(parent, () => callback());
|
|
9888
10238
|
}
|
|
9889
|
-
function _saveOrgInfo(state,
|
|
9890
|
-
|
|
10239
|
+
function _saveOrgInfo(state, orgInfo, orgName) {
|
|
10240
|
+
const org = selectLoginOrg(orgInfo, orgName);
|
|
10241
|
+
state.orgId = org.id;
|
|
10242
|
+
state.orgName = org.name;
|
|
10243
|
+
state.apiUrl = isomorph_default.getEnv("BRAINTRUST_API_URL") ?? org.api_url;
|
|
10244
|
+
state.proxyUrl = isomorph_default.getEnv("BRAINTRUST_PROXY_URL") ?? org.proxy_url;
|
|
10245
|
+
state.gitMetadataSettings = org.git_metadata || void 0;
|
|
10246
|
+
}
|
|
10247
|
+
function selectLoginOrg(orgInfo, orgName) {
|
|
10248
|
+
if (orgInfo.length === 0) {
|
|
9891
10249
|
throw new LoginInvalidOrgError(
|
|
9892
10250
|
"This user is not part of any organizations."
|
|
9893
10251
|
);
|
|
9894
10252
|
}
|
|
9895
|
-
for (const org of
|
|
9896
|
-
if (
|
|
9897
|
-
|
|
9898
|
-
state.orgName = org.name;
|
|
9899
|
-
state.apiUrl = isomorph_default.getEnv("BRAINTRUST_API_URL") ?? org.api_url;
|
|
9900
|
-
state.proxyUrl = isomorph_default.getEnv("BRAINTRUST_PROXY_URL") ?? org.proxy_url;
|
|
9901
|
-
state.gitMetadataSettings = org.git_metadata || void 0;
|
|
9902
|
-
break;
|
|
10253
|
+
for (const org of orgInfo) {
|
|
10254
|
+
if (orgName === void 0 || org.name === orgName) {
|
|
10255
|
+
return org;
|
|
9903
10256
|
}
|
|
9904
10257
|
}
|
|
9905
|
-
|
|
9906
|
-
|
|
9907
|
-
|
|
9908
|
-
);
|
|
9909
|
-
}
|
|
10258
|
+
throw new LoginInvalidOrgError(
|
|
10259
|
+
`Organization ${orgName} not found. Must be one of ${orgInfo.map((org) => org.name).join(", ")}`
|
|
10260
|
+
);
|
|
9910
10261
|
}
|
|
9911
10262
|
function validateTags(tags) {
|
|
9912
10263
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -11812,6 +12163,14 @@ var Prompt2 = class _Prompt {
|
|
|
11812
12163
|
static isPrompt(data) {
|
|
11813
12164
|
return typeof data === "object" && data !== null && "__braintrust_prompt_marker" in data;
|
|
11814
12165
|
}
|
|
12166
|
+
/** @internal */
|
|
12167
|
+
_internalSerializeForCache() {
|
|
12168
|
+
return {
|
|
12169
|
+
metadata: this.metadata,
|
|
12170
|
+
defaults: this.defaults,
|
|
12171
|
+
noTrace: this.noTrace
|
|
12172
|
+
};
|
|
12173
|
+
}
|
|
11815
12174
|
static fromPromptData(name, promptData) {
|
|
11816
12175
|
return new _Prompt(
|
|
11817
12176
|
{
|
|
@@ -11852,6 +12211,10 @@ var RemoteEvalParameters = class {
|
|
|
11852
12211
|
get data() {
|
|
11853
12212
|
return this.metadata.function_data.data ?? {};
|
|
11854
12213
|
}
|
|
12214
|
+
/** @internal */
|
|
12215
|
+
_internalSerializeForCache() {
|
|
12216
|
+
return { metadata: this.metadata };
|
|
12217
|
+
}
|
|
11855
12218
|
validate(data) {
|
|
11856
12219
|
if (typeof data !== "object" || data === null) {
|
|
11857
12220
|
return false;
|
|
@@ -12672,56 +13035,14 @@ function suppressionStore() {
|
|
|
12672
13035
|
autoInstrumentationSuppressionStore ??= isomorph_default.newAsyncLocalStorage();
|
|
12673
13036
|
return autoInstrumentationSuppressionStore;
|
|
12674
13037
|
}
|
|
12675
|
-
function currentFrames() {
|
|
12676
|
-
return suppressionStore().getStore()?.frames ?? [];
|
|
12677
|
-
}
|
|
12678
13038
|
function isAutoInstrumentationSuppressed() {
|
|
12679
|
-
|
|
12680
|
-
return frames[frames.length - 1]?.mode === "suppress";
|
|
13039
|
+
return suppressionStore().getStore() === true;
|
|
12681
13040
|
}
|
|
12682
13041
|
function runWithAutoInstrumentationSuppressed(callback) {
|
|
12683
|
-
|
|
12684
|
-
id: /* @__PURE__ */ Symbol("braintrust.auto-instrumentation-suppress"),
|
|
12685
|
-
mode: "suppress"
|
|
12686
|
-
};
|
|
12687
|
-
return suppressionStore().run(
|
|
12688
|
-
{ frames: [...currentFrames(), frame] },
|
|
12689
|
-
callback
|
|
12690
|
-
);
|
|
13042
|
+
return suppressionStore().run(true, callback);
|
|
12691
13043
|
}
|
|
12692
|
-
function
|
|
12693
|
-
|
|
12694
|
-
if (!startChannel) {
|
|
12695
|
-
return void 0;
|
|
12696
|
-
}
|
|
12697
|
-
const store = suppressionStore();
|
|
12698
|
-
startChannel.bindStore(store, () => ({
|
|
12699
|
-
frames: [
|
|
12700
|
-
...currentFrames(),
|
|
12701
|
-
{
|
|
12702
|
-
id: /* @__PURE__ */ Symbol("braintrust.auto-instrumentation-suppress"),
|
|
12703
|
-
mode: "suppress"
|
|
12704
|
-
}
|
|
12705
|
-
]
|
|
12706
|
-
}));
|
|
12707
|
-
return () => {
|
|
12708
|
-
startChannel.unbindStore(store);
|
|
12709
|
-
};
|
|
12710
|
-
}
|
|
12711
|
-
function enterAutoInstrumentationAllowed() {
|
|
12712
|
-
const frame = {
|
|
12713
|
-
id: /* @__PURE__ */ Symbol("braintrust.auto-instrumentation-allow"),
|
|
12714
|
-
mode: "allow"
|
|
12715
|
-
};
|
|
12716
|
-
suppressionStore().enterWith({
|
|
12717
|
-
frames: [...currentFrames(), frame]
|
|
12718
|
-
});
|
|
12719
|
-
return () => {
|
|
12720
|
-
const frames = currentFrames().filter(
|
|
12721
|
-
(candidate) => candidate.id !== frame.id
|
|
12722
|
-
);
|
|
12723
|
-
suppressionStore().enterWith(frames.length > 0 ? { frames } : void 0);
|
|
12724
|
-
};
|
|
13044
|
+
function runWithAutoInstrumentationAllowed(callback) {
|
|
13045
|
+
return suppressionStore().run(void 0, callback);
|
|
12725
13046
|
}
|
|
12726
13047
|
|
|
12727
13048
|
// src/instrumentation/core/channel-tracing.ts
|
|
@@ -14010,13 +14331,33 @@ function aggregateChatLogprobs(existing, incoming) {
|
|
|
14010
14331
|
}
|
|
14011
14332
|
return aggregated;
|
|
14012
14333
|
}
|
|
14334
|
+
function createAggregatedChatChoice(index) {
|
|
14335
|
+
return {
|
|
14336
|
+
index,
|
|
14337
|
+
role: void 0,
|
|
14338
|
+
content: void 0,
|
|
14339
|
+
refusal: void 0,
|
|
14340
|
+
toolCallsByIndex: /* @__PURE__ */ new Map(),
|
|
14341
|
+
logprobs: void 0,
|
|
14342
|
+
finish_reason: void 0
|
|
14343
|
+
};
|
|
14344
|
+
}
|
|
14345
|
+
function toChatChoice(choice) {
|
|
14346
|
+
const toolCalls2 = Array.from(choice.toolCallsByIndex.entries()).sort(([left], [right]) => left - right).map(([, toolCall]) => toolCall);
|
|
14347
|
+
return {
|
|
14348
|
+
index: choice.index,
|
|
14349
|
+
message: {
|
|
14350
|
+
role: choice.role,
|
|
14351
|
+
content: choice.content,
|
|
14352
|
+
...choice.refusal !== void 0 ? { refusal: choice.refusal } : {},
|
|
14353
|
+
tool_calls: toolCalls2.length > 0 ? toolCalls2 : void 0
|
|
14354
|
+
},
|
|
14355
|
+
logprobs: choice.logprobs ?? null,
|
|
14356
|
+
finish_reason: choice.finish_reason
|
|
14357
|
+
};
|
|
14358
|
+
}
|
|
14013
14359
|
function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
|
|
14014
|
-
|
|
14015
|
-
let content = void 0;
|
|
14016
|
-
let refusal = void 0;
|
|
14017
|
-
let tool_calls = void 0;
|
|
14018
|
-
let logprobs = void 0;
|
|
14019
|
-
let finish_reason = void 0;
|
|
14360
|
+
const choicesByIndex = /* @__PURE__ */ new Map();
|
|
14020
14361
|
let metrics = {};
|
|
14021
14362
|
for (const chunk of chunks) {
|
|
14022
14363
|
if (chunk.usage) {
|
|
@@ -14025,62 +14366,75 @@ function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
|
|
|
14025
14366
|
...parseMetricsFromUsage(chunk.usage)
|
|
14026
14367
|
};
|
|
14027
14368
|
}
|
|
14028
|
-
const
|
|
14029
|
-
if (!
|
|
14030
|
-
continue;
|
|
14031
|
-
}
|
|
14032
|
-
if (choice.finish_reason) {
|
|
14033
|
-
finish_reason = choice.finish_reason;
|
|
14034
|
-
}
|
|
14035
|
-
logprobs = aggregateChatLogprobs(logprobs, choice.logprobs);
|
|
14036
|
-
const delta = choice.delta;
|
|
14037
|
-
if (!delta) {
|
|
14369
|
+
const choices = chunk.choices;
|
|
14370
|
+
if (!choices?.length) {
|
|
14038
14371
|
continue;
|
|
14039
14372
|
}
|
|
14040
|
-
|
|
14041
|
-
|
|
14042
|
-
|
|
14043
|
-
|
|
14044
|
-
|
|
14045
|
-
|
|
14046
|
-
|
|
14047
|
-
|
|
14048
|
-
|
|
14049
|
-
|
|
14050
|
-
|
|
14051
|
-
|
|
14052
|
-
|
|
14053
|
-
|
|
14054
|
-
|
|
14055
|
-
|
|
14056
|
-
|
|
14057
|
-
|
|
14058
|
-
|
|
14059
|
-
|
|
14060
|
-
|
|
14373
|
+
for (const choice of choices) {
|
|
14374
|
+
const choiceIndex = choice.index;
|
|
14375
|
+
let aggregatedChoice = choicesByIndex.get(choiceIndex);
|
|
14376
|
+
if (!aggregatedChoice) {
|
|
14377
|
+
aggregatedChoice = createAggregatedChatChoice(choiceIndex);
|
|
14378
|
+
choicesByIndex.set(choiceIndex, aggregatedChoice);
|
|
14379
|
+
}
|
|
14380
|
+
if (choice.finish_reason) {
|
|
14381
|
+
aggregatedChoice.finish_reason = choice.finish_reason;
|
|
14382
|
+
}
|
|
14383
|
+
aggregatedChoice.logprobs = aggregateChatLogprobs(
|
|
14384
|
+
aggregatedChoice.logprobs,
|
|
14385
|
+
choice.logprobs
|
|
14386
|
+
);
|
|
14387
|
+
const delta = choice.delta;
|
|
14388
|
+
if (!delta) {
|
|
14389
|
+
continue;
|
|
14390
|
+
}
|
|
14391
|
+
if (delta.finish_reason) {
|
|
14392
|
+
aggregatedChoice.finish_reason = delta.finish_reason;
|
|
14393
|
+
}
|
|
14394
|
+
if (!aggregatedChoice.role && delta.role) {
|
|
14395
|
+
aggregatedChoice.role = delta.role;
|
|
14396
|
+
}
|
|
14397
|
+
if (delta.content) {
|
|
14398
|
+
aggregatedChoice.content = (aggregatedChoice.content || "") + delta.content;
|
|
14399
|
+
}
|
|
14400
|
+
if (delta.refusal) {
|
|
14401
|
+
aggregatedChoice.refusal = (aggregatedChoice.refusal || "") + delta.refusal;
|
|
14402
|
+
}
|
|
14403
|
+
if (delta.tool_calls) {
|
|
14404
|
+
for (const toolDelta of delta.tool_calls) {
|
|
14405
|
+
let aggregatedToolCall = aggregatedChoice.toolCallsByIndex.get(
|
|
14406
|
+
toolDelta.index
|
|
14407
|
+
);
|
|
14408
|
+
if (!aggregatedToolCall) {
|
|
14409
|
+
aggregatedToolCall = {
|
|
14410
|
+
function: { arguments: "" }
|
|
14411
|
+
};
|
|
14412
|
+
aggregatedChoice.toolCallsByIndex.set(
|
|
14413
|
+
toolDelta.index,
|
|
14414
|
+
aggregatedToolCall
|
|
14415
|
+
);
|
|
14061
14416
|
}
|
|
14062
|
-
|
|
14063
|
-
|
|
14064
|
-
|
|
14417
|
+
if (toolDelta.id !== void 0) {
|
|
14418
|
+
aggregatedToolCall.id = toolDelta.id;
|
|
14419
|
+
}
|
|
14420
|
+
if (toolDelta.type !== void 0) {
|
|
14421
|
+
aggregatedToolCall.type = toolDelta.type;
|
|
14422
|
+
}
|
|
14423
|
+
if (toolDelta.function?.name !== void 0) {
|
|
14424
|
+
aggregatedToolCall.function.name = toolDelta.function.name;
|
|
14425
|
+
}
|
|
14426
|
+
if (toolDelta.function?.arguments !== void 0) {
|
|
14427
|
+
aggregatedToolCall.function.arguments += toolDelta.function.arguments;
|
|
14428
|
+
}
|
|
14429
|
+
}
|
|
14065
14430
|
}
|
|
14066
14431
|
}
|
|
14067
14432
|
}
|
|
14068
14433
|
metrics = withCachedMetric(metrics, streamResult, endEvent);
|
|
14434
|
+
const output = Array.from(choicesByIndex.values()).sort((left, right) => left.index - right.index).map(toChatChoice);
|
|
14069
14435
|
return {
|
|
14070
14436
|
metrics,
|
|
14071
|
-
output: [
|
|
14072
|
-
{
|
|
14073
|
-
index: 0,
|
|
14074
|
-
message: {
|
|
14075
|
-
role,
|
|
14076
|
-
content,
|
|
14077
|
-
...refusal !== void 0 ? { refusal } : {},
|
|
14078
|
-
tool_calls
|
|
14079
|
-
},
|
|
14080
|
-
logprobs: logprobs ?? null,
|
|
14081
|
-
finish_reason
|
|
14082
|
-
}
|
|
14083
|
-
]
|
|
14437
|
+
output: output.length > 0 ? output : [toChatChoice(createAggregatedChatChoice(0))]
|
|
14084
14438
|
};
|
|
14085
14439
|
}
|
|
14086
14440
|
function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
|
|
@@ -15416,6 +15770,12 @@ function parseMetricsFromUsage2(usage) {
|
|
|
15416
15770
|
}
|
|
15417
15771
|
}
|
|
15418
15772
|
}
|
|
15773
|
+
if (isObject(usage.output_tokens_details)) {
|
|
15774
|
+
const thinkingTokens = usage.output_tokens_details.thinking_tokens;
|
|
15775
|
+
if (typeof thinkingTokens === "number") {
|
|
15776
|
+
metrics.completion_reasoning_tokens = thinkingTokens;
|
|
15777
|
+
}
|
|
15778
|
+
}
|
|
15419
15779
|
if (isObject(usage.server_tool_use)) {
|
|
15420
15780
|
for (const [name, value] of Object.entries(usage.server_tool_use)) {
|
|
15421
15781
|
if (typeof value === "number") {
|
|
@@ -16372,7 +16732,6 @@ function endHarnessTurn(parent) {
|
|
|
16372
16732
|
function braintrustAISDKTelemetry() {
|
|
16373
16733
|
const operations = /* @__PURE__ */ new Map();
|
|
16374
16734
|
const operationKeysByCallId = /* @__PURE__ */ new Map();
|
|
16375
|
-
const workflowOperationKeyStore = isomorph_default.newAsyncLocalStorage();
|
|
16376
16735
|
const modelSpans = /* @__PURE__ */ new Map();
|
|
16377
16736
|
const objectSpans = /* @__PURE__ */ new Map();
|
|
16378
16737
|
const embedSpans = /* @__PURE__ */ new Map();
|
|
@@ -16415,9 +16774,6 @@ function braintrustAISDKTelemetry() {
|
|
|
16415
16774
|
return;
|
|
16416
16775
|
}
|
|
16417
16776
|
operations.delete(operationKey);
|
|
16418
|
-
if (workflowOperationKeyStore.getStore() === operationKey) {
|
|
16419
|
-
workflowOperationKeyStore.enterWith(void 0);
|
|
16420
|
-
}
|
|
16421
16777
|
const keys = operationKeysByCallId.get(state.callId);
|
|
16422
16778
|
if (!keys) {
|
|
16423
16779
|
return;
|
|
@@ -16465,14 +16821,7 @@ function braintrustAISDKTelemetry() {
|
|
|
16465
16821
|
return key;
|
|
16466
16822
|
}
|
|
16467
16823
|
}
|
|
16468
|
-
|
|
16469
|
-
if (workflowOperationKey && keys.includes(workflowOperationKey)) {
|
|
16470
|
-
return workflowOperationKey;
|
|
16471
|
-
}
|
|
16472
|
-
if (callId === "workflow-agent") {
|
|
16473
|
-
return void 0;
|
|
16474
|
-
}
|
|
16475
|
-
return mode === "finish" ? keys[0] : keys[keys.length - 1];
|
|
16824
|
+
return callId === "workflow-agent" || mode === "active" ? keys[keys.length - 1] : keys[0];
|
|
16476
16825
|
};
|
|
16477
16826
|
const operationKeyFromEvent = (event, mode = "active") => {
|
|
16478
16827
|
const explicit = explicitOperationKey(event);
|
|
@@ -16486,17 +16835,13 @@ function braintrustAISDKTelemetry() {
|
|
|
16486
16835
|
if (operationKey) {
|
|
16487
16836
|
return operationKey;
|
|
16488
16837
|
}
|
|
16489
|
-
const
|
|
16490
|
-
if (
|
|
16491
|
-
return
|
|
16838
|
+
const workflowAgentKeys2 = operationKeysByCallId.get("workflow-agent");
|
|
16839
|
+
if (workflowAgentKeys2?.length) {
|
|
16840
|
+
return workflowAgentKeys2[workflowAgentKeys2.length - 1];
|
|
16492
16841
|
}
|
|
16493
16842
|
return callId === "workflow-agent" ? void 0 : callId;
|
|
16494
16843
|
}
|
|
16495
16844
|
}
|
|
16496
|
-
const workflowOperationKey = workflowOperationKeyStore.getStore();
|
|
16497
|
-
if (workflowOperationKey && operations.has(workflowOperationKey)) {
|
|
16498
|
-
return workflowOperationKey;
|
|
16499
|
-
}
|
|
16500
16845
|
const wrapperSpan = currentWorkflowAgentWrapperSpan();
|
|
16501
16846
|
if (wrapperSpan?.spanId) {
|
|
16502
16847
|
for (const [operationKey, state] of operations) {
|
|
@@ -16506,8 +16851,8 @@ function braintrustAISDKTelemetry() {
|
|
|
16506
16851
|
}
|
|
16507
16852
|
}
|
|
16508
16853
|
const workflowAgentKeys = operationKeysByCallId.get("workflow-agent");
|
|
16509
|
-
if (workflowAgentKeys?.length
|
|
16510
|
-
return workflowAgentKeys[
|
|
16854
|
+
if (workflowAgentKeys?.length) {
|
|
16855
|
+
return workflowAgentKeys[workflowAgentKeys.length - 1];
|
|
16511
16856
|
}
|
|
16512
16857
|
if (operations.size === 1) {
|
|
16513
16858
|
return operations.keys().next().value;
|
|
@@ -16710,9 +17055,6 @@ function braintrustAISDKTelemetry() {
|
|
|
16710
17055
|
if (!ownsSpan) {
|
|
16711
17056
|
return;
|
|
16712
17057
|
}
|
|
16713
|
-
if (workflowAgent) {
|
|
16714
|
-
workflowOperationKeyStore.enterWith(operationKey);
|
|
16715
|
-
}
|
|
16716
17058
|
let metadata = metadataFromEvent(event);
|
|
16717
17059
|
const logPayload = { metadata };
|
|
16718
17060
|
const workflowAgentCallInput = workflowAgent ? operationInput(event, operationName) : void 0;
|
|
@@ -17184,6 +17526,10 @@ var aiSDKChannels = defineChannels(
|
|
|
17184
17526
|
channelName: "generateText",
|
|
17185
17527
|
kind: "async"
|
|
17186
17528
|
}),
|
|
17529
|
+
generateImage: channel({
|
|
17530
|
+
channelName: "generateImage",
|
|
17531
|
+
kind: "async"
|
|
17532
|
+
}),
|
|
17187
17533
|
streamText: channel({
|
|
17188
17534
|
channelName: "streamText",
|
|
17189
17535
|
kind: "async"
|
|
@@ -17371,7 +17717,7 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
17371
17717
|
}
|
|
17372
17718
|
subscribeToAISDK() {
|
|
17373
17719
|
const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
|
|
17374
|
-
this.unsubscribers.push(
|
|
17720
|
+
this.unsubscribers.push(interceptAISDKV7TelemetryDispatcher());
|
|
17375
17721
|
this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
|
|
17376
17722
|
this.unsubscribers.push(
|
|
17377
17723
|
subscribeToHarnessContinuation(
|
|
@@ -17399,6 +17745,18 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
17399
17745
|
aggregateChunks: aggregateAISDKChunks
|
|
17400
17746
|
})
|
|
17401
17747
|
);
|
|
17748
|
+
this.unsubscribers.push(
|
|
17749
|
+
traceAsyncChannel(aiSDKChannels.generateImage, {
|
|
17750
|
+
name: "generateImage",
|
|
17751
|
+
type: "llm" /* LLM */,
|
|
17752
|
+
extractInput: ([params], event) => prepareAISDKGenerateImageInput(params, event.self),
|
|
17753
|
+
extractOutput: (result, endEvent) => processAISDKGenerateImageOutput(
|
|
17754
|
+
result,
|
|
17755
|
+
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
17756
|
+
),
|
|
17757
|
+
extractMetrics: (result) => extractTokenMetrics(result)
|
|
17758
|
+
})
|
|
17759
|
+
);
|
|
17402
17760
|
this.unsubscribers.push(
|
|
17403
17761
|
traceStreamingChannel(aiSDKChannels.streamText, {
|
|
17404
17762
|
name: "streamText",
|
|
@@ -17888,26 +18246,29 @@ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPa
|
|
|
17888
18246
|
channel2.unsubscribe(handlers);
|
|
17889
18247
|
};
|
|
17890
18248
|
}
|
|
17891
|
-
function
|
|
17892
|
-
const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
|
|
18249
|
+
function interceptAISDKV7TelemetryDispatcher() {
|
|
17893
18250
|
const telemetry = braintrustAISDKTelemetry();
|
|
17894
|
-
|
|
17895
|
-
|
|
17896
|
-
const
|
|
17897
|
-
|
|
17898
|
-
|
|
18251
|
+
return aiSDKChannels.v7CreateTelemetryDispatcher.intercept(
|
|
18252
|
+
(target, thisArg, args) => {
|
|
18253
|
+
const dispatcher = Reflect.apply(target, thisArg, args);
|
|
18254
|
+
const telemetryOptions = args[0]?.telemetry;
|
|
18255
|
+
if (telemetryOptions?.isEnabled !== false) {
|
|
18256
|
+
try {
|
|
18257
|
+
patchAISDKV7TelemetryDispatcher(
|
|
18258
|
+
dispatcher,
|
|
18259
|
+
telemetry,
|
|
18260
|
+
telemetryOptions
|
|
18261
|
+
);
|
|
18262
|
+
} catch (error) {
|
|
18263
|
+
debugLogger.error(
|
|
18264
|
+
"Error instrumenting AI SDK v7 telemetry dispatcher:",
|
|
18265
|
+
error
|
|
18266
|
+
);
|
|
18267
|
+
}
|
|
17899
18268
|
}
|
|
17900
|
-
|
|
17901
|
-
event.result,
|
|
17902
|
-
telemetry,
|
|
17903
|
-
telemetryOptions
|
|
17904
|
-
);
|
|
18269
|
+
return dispatcher;
|
|
17905
18270
|
}
|
|
17906
|
-
|
|
17907
|
-
channel2.subscribe(handlers);
|
|
17908
|
-
return () => {
|
|
17909
|
-
channel2.unsubscribe(handlers);
|
|
17910
|
-
};
|
|
18271
|
+
);
|
|
17911
18272
|
}
|
|
17912
18273
|
function patchAISDKV7TelemetryDispatcher(dispatcher, telemetry, telemetryOptions) {
|
|
17913
18274
|
if (!isObject(dispatcher)) {
|
|
@@ -18232,16 +18593,10 @@ var convertImageToAttachment = (image, explicitMimeType) => {
|
|
|
18232
18593
|
}
|
|
18233
18594
|
}
|
|
18234
18595
|
if (explicitMimeType) {
|
|
18235
|
-
|
|
18236
|
-
|
|
18237
|
-
data: new Blob([image], { type: explicitMimeType }),
|
|
18238
|
-
filename: `image.${getExtensionFromMediaType(explicitMimeType)}`,
|
|
18239
|
-
contentType: explicitMimeType
|
|
18240
|
-
});
|
|
18241
|
-
}
|
|
18242
|
-
if (typeof Buffer !== "undefined" && Buffer.isBuffer(image)) {
|
|
18596
|
+
const blob = convertDataToBlob(image, explicitMimeType);
|
|
18597
|
+
if (blob) {
|
|
18243
18598
|
return new Attachment({
|
|
18244
|
-
data:
|
|
18599
|
+
data: blob,
|
|
18245
18600
|
filename: `image.${getExtensionFromMediaType(explicitMimeType)}`,
|
|
18246
18601
|
contentType: explicitMimeType
|
|
18247
18602
|
});
|
|
@@ -18295,6 +18650,25 @@ var convertDataToAttachment = (data, mimeType, filename) => {
|
|
|
18295
18650
|
function processAISDKCallInput(params) {
|
|
18296
18651
|
return processInputAttachmentsSync(params);
|
|
18297
18652
|
}
|
|
18653
|
+
function processAISDKGenerateImageInput(params) {
|
|
18654
|
+
const prompt = params.prompt;
|
|
18655
|
+
if (!isObject(prompt) || Array.isArray(prompt)) {
|
|
18656
|
+
return processAISDKCallInput(params);
|
|
18657
|
+
}
|
|
18658
|
+
const processedPrompt = { ...prompt };
|
|
18659
|
+
if (Array.isArray(prompt.images)) {
|
|
18660
|
+
processedPrompt.images = prompt.images.map(
|
|
18661
|
+
(image) => convertImageToAttachment(image, "image/png") ?? image
|
|
18662
|
+
);
|
|
18663
|
+
}
|
|
18664
|
+
if (prompt.mask !== void 0) {
|
|
18665
|
+
processedPrompt.mask = convertImageToAttachment(prompt.mask, "image/png") ?? prompt.mask;
|
|
18666
|
+
}
|
|
18667
|
+
return processAISDKCallInput({
|
|
18668
|
+
...params,
|
|
18669
|
+
prompt: processedPrompt
|
|
18670
|
+
});
|
|
18671
|
+
}
|
|
18298
18672
|
function processAISDKWorkflowAgentCallInput(params) {
|
|
18299
18673
|
const processed = processAISDKCallInput(params);
|
|
18300
18674
|
return {
|
|
@@ -18433,6 +18807,12 @@ function prepareAISDKEmbedInput(params, self) {
|
|
|
18433
18807
|
metadata: extractMetadataFromEmbedParams(params, self)
|
|
18434
18808
|
};
|
|
18435
18809
|
}
|
|
18810
|
+
function prepareAISDKGenerateImageInput(params, self) {
|
|
18811
|
+
return {
|
|
18812
|
+
input: processAISDKGenerateImageInput(params).input,
|
|
18813
|
+
metadata: extractMetadataFromCallParams(params, self)
|
|
18814
|
+
};
|
|
18815
|
+
}
|
|
18436
18816
|
function prepareAISDKRerankInput(params, self) {
|
|
18437
18817
|
const { documents, query } = params;
|
|
18438
18818
|
return {
|
|
@@ -19808,6 +20188,57 @@ function processAISDKOutput(output, denyOutputPaths) {
|
|
|
19808
20188
|
}
|
|
19809
20189
|
return normalizeAISDKLoggedOutput(sanitized);
|
|
19810
20190
|
}
|
|
20191
|
+
function processAISDKGenerateImageOutput(output, denyOutputPaths) {
|
|
20192
|
+
if (!output || typeof output !== "object") {
|
|
20193
|
+
return output;
|
|
20194
|
+
}
|
|
20195
|
+
const summarized = {};
|
|
20196
|
+
for (const field of [
|
|
20197
|
+
"usage",
|
|
20198
|
+
"warnings",
|
|
20199
|
+
"providerMetadata",
|
|
20200
|
+
"experimental_providerMetadata",
|
|
20201
|
+
"responses"
|
|
20202
|
+
]) {
|
|
20203
|
+
const value = safeSerializableFieldRead(output, field);
|
|
20204
|
+
if (value !== void 0 && isSerializableOutputValue(value)) {
|
|
20205
|
+
summarized[field] = value;
|
|
20206
|
+
}
|
|
20207
|
+
}
|
|
20208
|
+
const images = safeSerializableFieldRead(output, "images");
|
|
20209
|
+
const image = safeSerializableFieldRead(output, "image");
|
|
20210
|
+
const generatedFiles = Array.isArray(images) && images.length > 0 ? images : image !== void 0 ? [image] : [];
|
|
20211
|
+
const loggedOutput = normalizeAISDKLoggedOutput(
|
|
20212
|
+
omit(summarized, denyOutputPaths)
|
|
20213
|
+
);
|
|
20214
|
+
if (generatedFiles.length > 0) {
|
|
20215
|
+
loggedOutput.images = generatedFiles.map(
|
|
20216
|
+
(file, index) => convertAISDKGeneratedFileToAttachment(file, index)
|
|
20217
|
+
);
|
|
20218
|
+
}
|
|
20219
|
+
return loggedOutput;
|
|
20220
|
+
}
|
|
20221
|
+
function convertAISDKGeneratedFileToAttachment(file, index) {
|
|
20222
|
+
if (!file || typeof file !== "object") {
|
|
20223
|
+
return file;
|
|
20224
|
+
}
|
|
20225
|
+
const generatedFile = file;
|
|
20226
|
+
const generatedMediaType = safeSerializableFieldRead(
|
|
20227
|
+
generatedFile,
|
|
20228
|
+
"mediaType"
|
|
20229
|
+
);
|
|
20230
|
+
const mediaType = typeof generatedMediaType === "string" ? generatedMediaType : "application/octet-stream";
|
|
20231
|
+
const data = safeSerializableFieldRead(generatedFile, "base64") ?? safeSerializableFieldRead(generatedFile, "uint8Array");
|
|
20232
|
+
const blob = convertDataToBlob(data, mediaType);
|
|
20233
|
+
if (blob) {
|
|
20234
|
+
return new Attachment({
|
|
20235
|
+
data: blob,
|
|
20236
|
+
filename: `generated_image_${index}.${getExtensionFromMediaType(mediaType)}`,
|
|
20237
|
+
contentType: mediaType
|
|
20238
|
+
});
|
|
20239
|
+
}
|
|
20240
|
+
return file;
|
|
20241
|
+
}
|
|
19811
20242
|
function processAISDKEmbeddingOutput(output, denyOutputPaths) {
|
|
19812
20243
|
if (!output || typeof output !== "object") {
|
|
19813
20244
|
return output;
|
|
@@ -20308,118 +20739,22 @@ var claudeAgentSDKChannels = defineChannels(
|
|
|
20308
20739
|
var CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION = "__braintrust_skip_local_tool_hooks";
|
|
20309
20740
|
|
|
20310
20741
|
// src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts
|
|
20311
|
-
var
|
|
20312
|
-
|
|
20313
|
-
)
|
|
20314
|
-
|
|
20315
|
-
const maybeIsoWithAsyncLocalStorage = isomorph_default;
|
|
20316
|
-
if (typeof maybeIsoWithAsyncLocalStorage.newAsyncLocalStorage === "function") {
|
|
20317
|
-
return maybeIsoWithAsyncLocalStorage.newAsyncLocalStorage();
|
|
20318
|
-
}
|
|
20319
|
-
let currentStore;
|
|
20320
|
-
return {
|
|
20321
|
-
enterWith(store) {
|
|
20322
|
-
currentStore = store;
|
|
20323
|
-
},
|
|
20324
|
-
getStore() {
|
|
20325
|
-
return currentStore;
|
|
20326
|
-
},
|
|
20327
|
-
run(store, callback) {
|
|
20328
|
-
const previousStore = currentStore;
|
|
20329
|
-
currentStore = store;
|
|
20330
|
-
try {
|
|
20331
|
-
return callback();
|
|
20332
|
-
} finally {
|
|
20333
|
-
currentStore = previousStore;
|
|
20334
|
-
}
|
|
20335
|
-
}
|
|
20336
|
-
};
|
|
20337
|
-
}
|
|
20338
|
-
var localToolContextStore = createLocalToolContextStore();
|
|
20339
|
-
var fallbackLocalToolParentResolver;
|
|
20340
|
-
function createClaudeLocalToolContext() {
|
|
20341
|
-
return {};
|
|
20342
|
-
}
|
|
20343
|
-
function runWithClaudeLocalToolContext(callback, context) {
|
|
20344
|
-
return localToolContextStore.run(
|
|
20345
|
-
context ?? createClaudeLocalToolContext(),
|
|
20346
|
-
callback
|
|
20347
|
-
);
|
|
20348
|
-
}
|
|
20349
|
-
function ensureClaudeLocalToolContext() {
|
|
20350
|
-
const existing = localToolContextStore.getStore();
|
|
20351
|
-
if (existing) {
|
|
20352
|
-
return existing;
|
|
20353
|
-
}
|
|
20354
|
-
const created = {};
|
|
20355
|
-
localToolContextStore.enterWith(created);
|
|
20356
|
-
return created;
|
|
20357
|
-
}
|
|
20358
|
-
function setClaudeLocalToolParentResolver(resolver) {
|
|
20359
|
-
fallbackLocalToolParentResolver = resolver;
|
|
20360
|
-
const context = ensureClaudeLocalToolContext();
|
|
20361
|
-
if (!context) {
|
|
20362
|
-
return;
|
|
20363
|
-
}
|
|
20364
|
-
context.resolveLocalToolParent = resolver;
|
|
20365
|
-
}
|
|
20366
|
-
function getClaudeLocalToolParentResolver() {
|
|
20367
|
-
return localToolContextStore.getStore()?.resolveLocalToolParent ?? fallbackLocalToolParentResolver;
|
|
20742
|
+
var localToolContextStore = isomorph_default.newAsyncLocalStorage();
|
|
20743
|
+
var localToolParentResolversByToolUseId = /* @__PURE__ */ new Map();
|
|
20744
|
+
function runWithClaudeLocalToolContext(callback, resolver) {
|
|
20745
|
+
return localToolContextStore.run(resolver, callback);
|
|
20368
20746
|
}
|
|
20369
|
-
function
|
|
20370
|
-
|
|
20747
|
+
function registerClaudeLocalToolParentResolver(toolUseId, resolver) {
|
|
20748
|
+
localToolParentResolversByToolUseId.set(toolUseId, resolver);
|
|
20371
20749
|
}
|
|
20372
|
-
function
|
|
20373
|
-
|
|
20374
|
-
|
|
20375
|
-
|
|
20376
|
-
const stream = result;
|
|
20377
|
-
const originalAsyncIterator = stream[Symbol.asyncIterator];
|
|
20378
|
-
if (originalAsyncIterator[LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED]) {
|
|
20379
|
-
return result;
|
|
20750
|
+
function getClaudeLocalToolParentResolver(toolUseId) {
|
|
20751
|
+
const currentResolver = localToolContextStore.getStore();
|
|
20752
|
+
if (!toolUseId) {
|
|
20753
|
+
return currentResolver;
|
|
20380
20754
|
}
|
|
20381
|
-
const
|
|
20382
|
-
|
|
20383
|
-
|
|
20384
|
-
if (!iterator || typeof iterator !== "object") {
|
|
20385
|
-
return iterator;
|
|
20386
|
-
}
|
|
20387
|
-
const patchMethod = (methodName) => {
|
|
20388
|
-
const originalMethod = Reflect.get(iterator, methodName);
|
|
20389
|
-
if (typeof originalMethod !== "function") {
|
|
20390
|
-
return;
|
|
20391
|
-
}
|
|
20392
|
-
Reflect.set(
|
|
20393
|
-
iterator,
|
|
20394
|
-
methodName,
|
|
20395
|
-
(...args) => runWithClaudeLocalToolContext(
|
|
20396
|
-
() => Reflect.apply(
|
|
20397
|
-
originalMethod,
|
|
20398
|
-
iterator,
|
|
20399
|
-
args
|
|
20400
|
-
),
|
|
20401
|
-
localToolContext
|
|
20402
|
-
)
|
|
20403
|
-
);
|
|
20404
|
-
};
|
|
20405
|
-
patchMethod("next");
|
|
20406
|
-
patchMethod("return");
|
|
20407
|
-
patchMethod("throw");
|
|
20408
|
-
return iterator;
|
|
20409
|
-
}, localToolContext);
|
|
20410
|
-
};
|
|
20411
|
-
Object.defineProperty(
|
|
20412
|
-
patchedAsyncIterator,
|
|
20413
|
-
LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED,
|
|
20414
|
-
{
|
|
20415
|
-
configurable: false,
|
|
20416
|
-
enumerable: false,
|
|
20417
|
-
value: true,
|
|
20418
|
-
writable: false
|
|
20419
|
-
}
|
|
20420
|
-
);
|
|
20421
|
-
Reflect.set(stream, Symbol.asyncIterator, patchedAsyncIterator);
|
|
20422
|
-
return result;
|
|
20755
|
+
const registeredResolver = localToolParentResolversByToolUseId.get(toolUseId);
|
|
20756
|
+
localToolParentResolversByToolUseId.delete(toolUseId);
|
|
20757
|
+
return currentResolver ?? registeredResolver;
|
|
20423
20758
|
}
|
|
20424
20759
|
|
|
20425
20760
|
// src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts
|
|
@@ -20445,7 +20780,7 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
|
|
|
20445
20780
|
const metadata = getMetadata();
|
|
20446
20781
|
const rawToolName = metadata.serverName ? `mcp__${metadata.serverName}__${metadata.toolName}` : metadata.toolName;
|
|
20447
20782
|
const toolUseId = getToolUseIdFromExtra(handlerArgs[1]);
|
|
20448
|
-
const localToolParentResolver = getClaudeLocalToolParentResolver();
|
|
20783
|
+
const localToolParentResolver = getClaudeLocalToolParentResolver(toolUseId);
|
|
20449
20784
|
const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
|
|
20450
20785
|
const runWithResolvedParent = async () => {
|
|
20451
20786
|
const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
|
|
@@ -20972,6 +21307,7 @@ function createToolTracingHooks(resolveParentSpan, taskIdToToolUseId, toolUseToP
|
|
|
20972
21307
|
}
|
|
20973
21308
|
}
|
|
20974
21309
|
if (skipLocalToolHooks && (isLocalToolUse(input.tool_name, mcpServers) || localToolHookNames.has(input.tool_name))) {
|
|
21310
|
+
registerClaudeLocalToolParentResolver(toolUseID, resolveParentSpan);
|
|
20975
21311
|
return {};
|
|
20976
21312
|
}
|
|
20977
21313
|
const parsed = parseToolName(input.tool_name);
|
|
@@ -21682,7 +22018,7 @@ async function finalizeQuerySpan(state) {
|
|
|
21682
22018
|
}
|
|
21683
22019
|
var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
21684
22020
|
onEnable() {
|
|
21685
|
-
this.
|
|
22021
|
+
this.interceptQuery();
|
|
21686
22022
|
}
|
|
21687
22023
|
onDisable() {
|
|
21688
22024
|
for (const unsubscribe of this.unsubscribers) {
|
|
@@ -21690,211 +22026,218 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
21690
22026
|
}
|
|
21691
22027
|
this.unsubscribers = [];
|
|
21692
22028
|
}
|
|
21693
|
-
|
|
21694
|
-
const
|
|
21695
|
-
|
|
21696
|
-
|
|
21697
|
-
|
|
21698
|
-
|
|
21699
|
-
|
|
21700
|
-
|
|
21701
|
-
|
|
21702
|
-
|
|
21703
|
-
|
|
21704
|
-
|
|
21705
|
-
|
|
21706
|
-
|
|
21707
|
-
|
|
21708
|
-
|
|
21709
|
-
|
|
21710
|
-
|
|
21711
|
-
|
|
21712
|
-
|
|
21713
|
-
try {
|
|
21714
|
-
for await (const message of promptStream) {
|
|
21715
|
-
capturedPromptMessages.push(message);
|
|
21716
|
-
yield message;
|
|
21717
|
-
}
|
|
21718
|
-
} finally {
|
|
21719
|
-
resolvePromptDone?.();
|
|
22029
|
+
interceptQuery() {
|
|
22030
|
+
const startQuery = (params) => {
|
|
22031
|
+
const originalPrompt = params.prompt;
|
|
22032
|
+
const options = params.options ?? {};
|
|
22033
|
+
const promptIsAsyncIterable = isAsyncIterable(originalPrompt);
|
|
22034
|
+
let promptStarted = false;
|
|
22035
|
+
let capturedPromptMessages;
|
|
22036
|
+
let resolvePromptDone;
|
|
22037
|
+
const promptDone = new Promise((resolve) => {
|
|
22038
|
+
resolvePromptDone = resolve;
|
|
22039
|
+
});
|
|
22040
|
+
if (promptIsAsyncIterable) {
|
|
22041
|
+
capturedPromptMessages = [];
|
|
22042
|
+
const promptStream = originalPrompt;
|
|
22043
|
+
params.prompt = (async function* () {
|
|
22044
|
+
promptStarted = true;
|
|
22045
|
+
try {
|
|
22046
|
+
for await (const message of promptStream) {
|
|
22047
|
+
capturedPromptMessages.push(message);
|
|
22048
|
+
yield message;
|
|
21720
22049
|
}
|
|
21721
|
-
}
|
|
21722
|
-
|
|
21723
|
-
const span = startSpan(
|
|
21724
|
-
withSpanInstrumentationName(
|
|
21725
|
-
{
|
|
21726
|
-
name: "Claude Agent",
|
|
21727
|
-
spanAttributes: {
|
|
21728
|
-
type: "task" /* TASK */
|
|
21729
|
-
}
|
|
21730
|
-
},
|
|
21731
|
-
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
21732
|
-
)
|
|
21733
|
-
);
|
|
21734
|
-
const startTime = getCurrentUnixTimestamp();
|
|
21735
|
-
try {
|
|
21736
|
-
span.log({
|
|
21737
|
-
input: typeof originalPrompt === "string" ? originalPrompt : promptIsAsyncIterable ? void 0 : originalPrompt !== void 0 ? String(originalPrompt) : void 0,
|
|
21738
|
-
metadata: filterSerializableOptions(options)
|
|
21739
|
-
});
|
|
21740
|
-
} catch (error) {
|
|
21741
|
-
console.error("Error extracting input for Claude Agent SDK:", error);
|
|
21742
|
-
}
|
|
21743
|
-
const activeToolSpans = /* @__PURE__ */ new Map();
|
|
21744
|
-
const activeLlmSpansByParentToolUse = /* @__PURE__ */ new Map();
|
|
21745
|
-
const conversationHistoryByParentKey = /* @__PURE__ */ new Map();
|
|
21746
|
-
const subAgentSpans = /* @__PURE__ */ new Map();
|
|
21747
|
-
const endedSubAgentSpans = /* @__PURE__ */ new Set();
|
|
21748
|
-
const toolUseToParent = /* @__PURE__ */ new Map();
|
|
21749
|
-
const latestLlmParentBySubAgentToolUse = /* @__PURE__ */ new Map();
|
|
21750
|
-
const latestRootLlmParentRef = {
|
|
21751
|
-
value: void 0
|
|
21752
|
-
};
|
|
21753
|
-
const subAgentDetailsByToolUseId = /* @__PURE__ */ new Map();
|
|
21754
|
-
const taskIdToToolUseId = /* @__PURE__ */ new Map();
|
|
21755
|
-
const promptMessagesByParentKey = /* @__PURE__ */ new Map();
|
|
21756
|
-
const promptSourcePriorityByParentKey = /* @__PURE__ */ new Map();
|
|
21757
|
-
const localToolContext = createClaudeLocalToolContext();
|
|
21758
|
-
const { hasLocalToolHandlers, localToolHookNames } = prepareLocalToolHandlersInMcpServers(options.mcpServers);
|
|
21759
|
-
const skipLocalToolHooks = options[CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION] === true || hasLocalToolHandlers;
|
|
21760
|
-
const resolveToolUseParentSpan = async (toolUseID, context) => {
|
|
21761
|
-
const trackedParentToolUseId = toolUseToParent.get(toolUseID);
|
|
21762
|
-
const parentToolUseId = trackedParentToolUseId ?? (context?.agentId ? taskIdToToolUseId.get(context.agentId) ?? null : null);
|
|
21763
|
-
const parentKey = llmParentKey(parentToolUseId);
|
|
21764
|
-
const activeLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
|
|
21765
|
-
const latestLlmParent = parentToolUseId ? latestLlmParentBySubAgentToolUse.get(parentToolUseId) : latestRootLlmParentRef.value;
|
|
21766
|
-
if (!activeLlmSpan && !latestLlmParent) {
|
|
21767
|
-
await ensureActiveLlmSpanForParentToolUse(
|
|
21768
|
-
span,
|
|
21769
|
-
activeLlmSpansByParentToolUse,
|
|
21770
|
-
subAgentDetailsByToolUseId,
|
|
21771
|
-
activeToolSpans,
|
|
21772
|
-
subAgentSpans,
|
|
21773
|
-
parentToolUseId,
|
|
21774
|
-
getCurrentUnixTimestamp()
|
|
21775
|
-
);
|
|
21776
|
-
}
|
|
21777
|
-
if (parentToolUseId) {
|
|
21778
|
-
const subAgentSpan = await ensureSubAgentSpan(
|
|
21779
|
-
subAgentDetailsByToolUseId,
|
|
21780
|
-
span,
|
|
21781
|
-
activeToolSpans,
|
|
21782
|
-
subAgentSpans,
|
|
21783
|
-
parentToolUseId
|
|
21784
|
-
);
|
|
21785
|
-
return subAgentSpan.export();
|
|
22050
|
+
} finally {
|
|
22051
|
+
resolvePromptDone?.();
|
|
21786
22052
|
}
|
|
21787
|
-
|
|
21788
|
-
|
|
21789
|
-
|
|
21790
|
-
|
|
21791
|
-
|
|
21792
|
-
|
|
21793
|
-
|
|
21794
|
-
|
|
21795
|
-
|
|
21796
|
-
|
|
21797
|
-
|
|
21798
|
-
|
|
21799
|
-
|
|
21800
|
-
|
|
21801
|
-
|
|
21802
|
-
|
|
21803
|
-
|
|
21804
|
-
|
|
21805
|
-
spans.set(event, {
|
|
21806
|
-
activeLlmSpansByParentToolUse,
|
|
21807
|
-
activePartialMessageIdByParentKey: /* @__PURE__ */ new Map(),
|
|
21808
|
-
activeToolSpans,
|
|
21809
|
-
conversationHistoryByParentKey,
|
|
21810
|
-
capturedPromptMessages,
|
|
21811
|
-
currentMessageId: void 0,
|
|
21812
|
-
currentMessageStartTime: startTime,
|
|
21813
|
-
currentMessages: [],
|
|
21814
|
-
endedSubAgentSpans,
|
|
21815
|
-
finalOutputUsageMessageIds: /* @__PURE__ */ new Set(),
|
|
21816
|
-
finalResults: [],
|
|
21817
|
-
options: optionsWithHooks,
|
|
21818
|
-
originalPrompt,
|
|
21819
|
-
processing: Promise.resolve(),
|
|
21820
|
-
promptDone,
|
|
21821
|
-
promptMessagesByParentKey,
|
|
21822
|
-
promptStarted: () => promptStarted,
|
|
21823
|
-
promptSourcePriorityByParentKey,
|
|
21824
|
-
span,
|
|
21825
|
-
subAgentDetailsByToolUseId,
|
|
21826
|
-
subAgentSpans,
|
|
21827
|
-
taskIdToToolUseId,
|
|
21828
|
-
latestLlmParentBySubAgentToolUse,
|
|
21829
|
-
latestRootLlmParentRef,
|
|
21830
|
-
toolUseToParent,
|
|
21831
|
-
usageByMessageId: /* @__PURE__ */ new Map(),
|
|
21832
|
-
localToolContext
|
|
22053
|
+
})();
|
|
22054
|
+
}
|
|
22055
|
+
const span = startSpan(
|
|
22056
|
+
withSpanInstrumentationName(
|
|
22057
|
+
{
|
|
22058
|
+
name: "Claude Agent",
|
|
22059
|
+
spanAttributes: {
|
|
22060
|
+
type: "task" /* TASK */
|
|
22061
|
+
}
|
|
22062
|
+
},
|
|
22063
|
+
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
22064
|
+
)
|
|
22065
|
+
);
|
|
22066
|
+
const startTime = getCurrentUnixTimestamp();
|
|
22067
|
+
try {
|
|
22068
|
+
span.log({
|
|
22069
|
+
input: typeof originalPrompt === "string" ? originalPrompt : promptIsAsyncIterable ? void 0 : originalPrompt !== void 0 ? String(originalPrompt) : void 0,
|
|
22070
|
+
metadata: filterSerializableOptions(options)
|
|
21833
22071
|
});
|
|
21834
|
-
}
|
|
21835
|
-
|
|
21836
|
-
|
|
21837
|
-
|
|
21838
|
-
|
|
21839
|
-
|
|
21840
|
-
|
|
21841
|
-
|
|
21842
|
-
|
|
21843
|
-
|
|
21844
|
-
|
|
21845
|
-
|
|
21846
|
-
|
|
21847
|
-
|
|
21848
|
-
|
|
21849
|
-
|
|
21850
|
-
|
|
21851
|
-
|
|
21852
|
-
|
|
21853
|
-
|
|
21854
|
-
|
|
21855
|
-
|
|
21856
|
-
|
|
21857
|
-
|
|
21858
|
-
|
|
21859
|
-
|
|
21860
|
-
|
|
21861
|
-
|
|
21862
|
-
|
|
21863
|
-
|
|
21864
|
-
|
|
21865
|
-
|
|
21866
|
-
|
|
21867
|
-
|
|
21868
|
-
|
|
21869
|
-
})
|
|
21870
|
-
});
|
|
21871
|
-
return;
|
|
21872
|
-
}
|
|
21873
|
-
try {
|
|
21874
|
-
state.span.log({ output: eventResult });
|
|
21875
|
-
} catch (error) {
|
|
21876
|
-
console.error("Error extracting output for Claude Agent SDK:", error);
|
|
21877
|
-
} finally {
|
|
21878
|
-
state.span.end();
|
|
21879
|
-
spans.delete(event);
|
|
22072
|
+
} catch (error) {
|
|
22073
|
+
console.error("Error extracting input for Claude Agent SDK:", error);
|
|
22074
|
+
}
|
|
22075
|
+
const activeToolSpans = /* @__PURE__ */ new Map();
|
|
22076
|
+
const activeLlmSpansByParentToolUse = /* @__PURE__ */ new Map();
|
|
22077
|
+
const conversationHistoryByParentKey = /* @__PURE__ */ new Map();
|
|
22078
|
+
const subAgentSpans = /* @__PURE__ */ new Map();
|
|
22079
|
+
const endedSubAgentSpans = /* @__PURE__ */ new Set();
|
|
22080
|
+
const toolUseToParent = /* @__PURE__ */ new Map();
|
|
22081
|
+
const latestLlmParentBySubAgentToolUse = /* @__PURE__ */ new Map();
|
|
22082
|
+
const latestRootLlmParentRef = {
|
|
22083
|
+
value: void 0
|
|
22084
|
+
};
|
|
22085
|
+
const subAgentDetailsByToolUseId = /* @__PURE__ */ new Map();
|
|
22086
|
+
const taskIdToToolUseId = /* @__PURE__ */ new Map();
|
|
22087
|
+
const promptMessagesByParentKey = /* @__PURE__ */ new Map();
|
|
22088
|
+
const promptSourcePriorityByParentKey = /* @__PURE__ */ new Map();
|
|
22089
|
+
const { hasLocalToolHandlers, localToolHookNames } = prepareLocalToolHandlersInMcpServers(options.mcpServers);
|
|
22090
|
+
const skipLocalToolHooks = options[CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION] === true || hasLocalToolHandlers;
|
|
22091
|
+
const resolveToolUseParentSpan = async (toolUseID, context) => {
|
|
22092
|
+
const trackedParentToolUseId = toolUseToParent.get(toolUseID);
|
|
22093
|
+
const parentToolUseId = trackedParentToolUseId ?? (context?.agentId ? taskIdToToolUseId.get(context.agentId) ?? null : null);
|
|
22094
|
+
const parentKey = llmParentKey(parentToolUseId);
|
|
22095
|
+
const activeLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
|
|
22096
|
+
const latestLlmParent = parentToolUseId ? latestLlmParentBySubAgentToolUse.get(parentToolUseId) : latestRootLlmParentRef.value;
|
|
22097
|
+
if (!activeLlmSpan && !latestLlmParent) {
|
|
22098
|
+
await ensureActiveLlmSpanForParentToolUse(
|
|
22099
|
+
span,
|
|
22100
|
+
activeLlmSpansByParentToolUse,
|
|
22101
|
+
subAgentDetailsByToolUseId,
|
|
22102
|
+
activeToolSpans,
|
|
22103
|
+
subAgentSpans,
|
|
22104
|
+
parentToolUseId,
|
|
22105
|
+
getCurrentUnixTimestamp()
|
|
22106
|
+
);
|
|
21880
22107
|
}
|
|
21881
|
-
|
|
21882
|
-
|
|
21883
|
-
|
|
21884
|
-
|
|
21885
|
-
|
|
22108
|
+
if (parentToolUseId) {
|
|
22109
|
+
const subAgentSpan = await ensureSubAgentSpan(
|
|
22110
|
+
subAgentDetailsByToolUseId,
|
|
22111
|
+
span,
|
|
22112
|
+
activeToolSpans,
|
|
22113
|
+
subAgentSpans,
|
|
22114
|
+
parentToolUseId
|
|
22115
|
+
);
|
|
22116
|
+
return subAgentSpan.export();
|
|
21886
22117
|
}
|
|
21887
|
-
|
|
21888
|
-
|
|
22118
|
+
return span.export();
|
|
22119
|
+
};
|
|
22120
|
+
const optionsWithHooks = injectTracingHooks(
|
|
22121
|
+
options,
|
|
22122
|
+
resolveToolUseParentSpan,
|
|
22123
|
+
taskIdToToolUseId,
|
|
22124
|
+
toolUseToParent,
|
|
22125
|
+
activeToolSpans,
|
|
22126
|
+
localToolHookNames,
|
|
22127
|
+
skipLocalToolHooks,
|
|
22128
|
+
subAgentDetailsByToolUseId,
|
|
22129
|
+
subAgentSpans,
|
|
22130
|
+
endedSubAgentSpans
|
|
22131
|
+
);
|
|
22132
|
+
params.options = optionsWithHooks;
|
|
22133
|
+
return {
|
|
22134
|
+
activeLlmSpansByParentToolUse,
|
|
22135
|
+
activePartialMessageIdByParentKey: /* @__PURE__ */ new Map(),
|
|
22136
|
+
activeToolSpans,
|
|
22137
|
+
conversationHistoryByParentKey,
|
|
22138
|
+
capturedPromptMessages,
|
|
22139
|
+
currentMessageId: void 0,
|
|
22140
|
+
currentMessageStartTime: startTime,
|
|
22141
|
+
currentMessages: [],
|
|
22142
|
+
endedSubAgentSpans,
|
|
22143
|
+
finalOutputUsageMessageIds: /* @__PURE__ */ new Set(),
|
|
22144
|
+
finalResults: [],
|
|
22145
|
+
options: optionsWithHooks,
|
|
22146
|
+
originalPrompt,
|
|
22147
|
+
processing: Promise.resolve(),
|
|
22148
|
+
promptDone,
|
|
22149
|
+
promptMessagesByParentKey,
|
|
22150
|
+
promptStarted: () => promptStarted,
|
|
22151
|
+
promptSourcePriorityByParentKey,
|
|
22152
|
+
span,
|
|
22153
|
+
subAgentDetailsByToolUseId,
|
|
22154
|
+
subAgentSpans,
|
|
22155
|
+
taskIdToToolUseId,
|
|
22156
|
+
latestLlmParentBySubAgentToolUse,
|
|
22157
|
+
latestRootLlmParentRef,
|
|
22158
|
+
toolUseToParent,
|
|
22159
|
+
usageByMessageId: /* @__PURE__ */ new Map(),
|
|
22160
|
+
localToolParentResolver: resolveToolUseParentSpan
|
|
22161
|
+
};
|
|
22162
|
+
};
|
|
22163
|
+
const finishQuery = (state, result) => {
|
|
22164
|
+
if (isAsyncIterable(result)) {
|
|
22165
|
+
patchStreamIfNeeded(result, {
|
|
22166
|
+
aroundNext: (callback) => runWithClaudeLocalToolContext(
|
|
22167
|
+
callback,
|
|
22168
|
+
state.localToolParentResolver
|
|
22169
|
+
),
|
|
22170
|
+
onChunk: (message) => {
|
|
22171
|
+
maybeTrackToolUseContext(state, message);
|
|
22172
|
+
state.processing = state.processing.then(() => handleStreamMessage(state, message)).catch((error) => {
|
|
22173
|
+
console.error(
|
|
22174
|
+
"Error processing Claude Agent SDK stream chunk:",
|
|
22175
|
+
error
|
|
22176
|
+
);
|
|
22177
|
+
});
|
|
22178
|
+
},
|
|
22179
|
+
onComplete: () => state.processing.then(() => finalizeQuerySpan(state)),
|
|
22180
|
+
onError: (error) => state.processing.then(() => {
|
|
22181
|
+
state.span.log({ error: error.message });
|
|
22182
|
+
}).then(() => finalizeQuerySpan(state))
|
|
21889
22183
|
});
|
|
22184
|
+
return;
|
|
22185
|
+
}
|
|
22186
|
+
try {
|
|
22187
|
+
state.span.log({ output: result });
|
|
22188
|
+
} catch (error) {
|
|
22189
|
+
console.error("Error extracting output for Claude Agent SDK:", error);
|
|
22190
|
+
} finally {
|
|
21890
22191
|
state.span.end();
|
|
21891
|
-
spans.delete(event);
|
|
21892
22192
|
}
|
|
21893
22193
|
};
|
|
21894
|
-
|
|
21895
|
-
|
|
21896
|
-
|
|
21897
|
-
|
|
22194
|
+
this.unsubscribers.push(
|
|
22195
|
+
claudeAgentSDKChannels.query.intercept((target, thisArg, args) => {
|
|
22196
|
+
let state;
|
|
22197
|
+
try {
|
|
22198
|
+
args[0] ??= {};
|
|
22199
|
+
state = startQuery(args[0]);
|
|
22200
|
+
} catch (error) {
|
|
22201
|
+
debugLogger.error(
|
|
22202
|
+
"Error starting Claude Agent SDK instrumentation:",
|
|
22203
|
+
error
|
|
22204
|
+
);
|
|
22205
|
+
}
|
|
22206
|
+
const invokeTarget = () => Reflect.apply(target, thisArg, args);
|
|
22207
|
+
try {
|
|
22208
|
+
const result = state ? runWithClaudeLocalToolContext(
|
|
22209
|
+
invokeTarget,
|
|
22210
|
+
state.localToolParentResolver
|
|
22211
|
+
) : invokeTarget();
|
|
22212
|
+
if (state) {
|
|
22213
|
+
try {
|
|
22214
|
+
finishQuery(state, result);
|
|
22215
|
+
} catch (error) {
|
|
22216
|
+
debugLogger.error(
|
|
22217
|
+
"Error finalizing Claude Agent SDK instrumentation:",
|
|
22218
|
+
error
|
|
22219
|
+
);
|
|
22220
|
+
}
|
|
22221
|
+
}
|
|
22222
|
+
return result;
|
|
22223
|
+
} catch (error) {
|
|
22224
|
+
if (state) {
|
|
22225
|
+
try {
|
|
22226
|
+
state.span.log({
|
|
22227
|
+
error: error instanceof Error ? error.message : String(error)
|
|
22228
|
+
});
|
|
22229
|
+
state.span.end();
|
|
22230
|
+
} catch (instrumentationError) {
|
|
22231
|
+
debugLogger.error(
|
|
22232
|
+
"Error handling Claude Agent SDK instrumentation failure:",
|
|
22233
|
+
instrumentationError
|
|
22234
|
+
);
|
|
22235
|
+
}
|
|
22236
|
+
}
|
|
22237
|
+
throw error;
|
|
22238
|
+
}
|
|
22239
|
+
})
|
|
22240
|
+
);
|
|
21898
22241
|
}
|
|
21899
22242
|
};
|
|
21900
22243
|
|
|
@@ -26008,7 +26351,7 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
26008
26351
|
span,
|
|
26009
26352
|
() => originalMethod.apply(resultLike, args2)
|
|
26010
26353
|
);
|
|
26011
|
-
if (!
|
|
26354
|
+
if (!isAsyncIterable3(stream)) {
|
|
26012
26355
|
return stream;
|
|
26013
26356
|
}
|
|
26014
26357
|
return wrapAsyncIterableWithSpan({
|
|
@@ -26203,7 +26546,7 @@ function wrapAsyncIterableWithSpan(args) {
|
|
|
26203
26546
|
}
|
|
26204
26547
|
};
|
|
26205
26548
|
}
|
|
26206
|
-
function
|
|
26549
|
+
function isAsyncIterable3(value) {
|
|
26207
26550
|
return !!value && (typeof value === "object" || typeof value === "function") && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
26208
26551
|
}
|
|
26209
26552
|
function normalizeError(error) {
|
|
@@ -27081,7 +27424,7 @@ function patchOpenRouterCallModelResult2(args) {
|
|
|
27081
27424
|
span,
|
|
27082
27425
|
() => originalMethod.apply(resultLike, args2)
|
|
27083
27426
|
);
|
|
27084
|
-
if (!
|
|
27427
|
+
if (!isAsyncIterable4(stream)) {
|
|
27085
27428
|
return stream;
|
|
27086
27429
|
}
|
|
27087
27430
|
return wrapAsyncIterableWithSpan2({
|
|
@@ -27276,7 +27619,7 @@ function wrapAsyncIterableWithSpan2(args) {
|
|
|
27276
27619
|
}
|
|
27277
27620
|
};
|
|
27278
27621
|
}
|
|
27279
|
-
function
|
|
27622
|
+
function isAsyncIterable4(value) {
|
|
27280
27623
|
return !!value && (typeof value === "object" || typeof value === "function") && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
27281
27624
|
}
|
|
27282
27625
|
function normalizeError2(error) {
|
|
@@ -33268,12 +33611,14 @@ function getMetricsFromResponse(response) {
|
|
|
33268
33611
|
continue;
|
|
33269
33612
|
}
|
|
33270
33613
|
const inputTokenDetails = usageMetadata.input_token_details;
|
|
33614
|
+
const outputTokenDetails = usageMetadata.output_token_details;
|
|
33271
33615
|
return normalizeTokenMetrics({
|
|
33272
33616
|
total_tokens: usageMetadata.total_tokens,
|
|
33273
33617
|
prompt_tokens: usageMetadata.input_tokens,
|
|
33274
33618
|
completion_tokens: usageMetadata.output_tokens,
|
|
33275
33619
|
prompt_cache_creation_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_creation : void 0,
|
|
33276
|
-
prompt_cached_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_read : void 0
|
|
33620
|
+
prompt_cached_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_read : void 0,
|
|
33621
|
+
completion_reasoning_tokens: isRecord(outputTokenDetails) ? outputTokenDetails.reasoning : void 0
|
|
33277
33622
|
});
|
|
33278
33623
|
}
|
|
33279
33624
|
const llmOutput = response.llmOutput || {};
|
|
@@ -33860,6 +34205,9 @@ var piCodingAgentChannels = defineChannels(
|
|
|
33860
34205
|
// src/instrumentation/plugins/pi-coding-agent-plugin.ts
|
|
33861
34206
|
var piAgentPatchStates = /* @__PURE__ */ new WeakMap();
|
|
33862
34207
|
var piAgentEventSubscriptions = /* @__PURE__ */ new WeakSet();
|
|
34208
|
+
var PI_TOOL_EXECUTE_WRAPPED = /* @__PURE__ */ Symbol.for(
|
|
34209
|
+
"braintrust.pi_coding_agent.tool_execute_wrapped"
|
|
34210
|
+
);
|
|
33863
34211
|
var piPromptContextStore;
|
|
33864
34212
|
var PiCodingAgentPlugin = class extends BasePlugin {
|
|
33865
34213
|
activePromptStates = /* @__PURE__ */ new Set();
|
|
@@ -33940,6 +34288,7 @@ function startPiPromptRun(event, onFinalize) {
|
|
|
33940
34288
|
return void 0;
|
|
33941
34289
|
}
|
|
33942
34290
|
installPiAgentInstrumentation(agent);
|
|
34291
|
+
wrapPiToolExecutors(agent.state?.tools);
|
|
33943
34292
|
const metadata = {
|
|
33944
34293
|
...extractSessionMetadata(session),
|
|
33945
34294
|
...extractPromptOptionsMetadata(event.arguments[1]),
|
|
@@ -34036,6 +34385,7 @@ function makeInstrumentedStreamFn(agent, originalStreamFn) {
|
|
|
34036
34385
|
if (!state || state.agent !== agent || state.finalized) {
|
|
34037
34386
|
return invokeOriginal();
|
|
34038
34387
|
}
|
|
34388
|
+
wrapPiToolExecutors(context.tools);
|
|
34039
34389
|
const llmState = await startPiLlmSpan(state, model, context, options);
|
|
34040
34390
|
try {
|
|
34041
34391
|
const stream = await runWithAutoInstrumentationSuppressed(invokeOriginal);
|
|
@@ -34046,6 +34396,33 @@ function makeInstrumentedStreamFn(agent, originalStreamFn) {
|
|
|
34046
34396
|
}
|
|
34047
34397
|
};
|
|
34048
34398
|
}
|
|
34399
|
+
function wrapPiToolExecutors(tools) {
|
|
34400
|
+
if (!tools) {
|
|
34401
|
+
return;
|
|
34402
|
+
}
|
|
34403
|
+
for (const tool of tools) {
|
|
34404
|
+
try {
|
|
34405
|
+
const execute = tool.execute;
|
|
34406
|
+
if (typeof execute !== "function" || execute[PI_TOOL_EXECUTE_WRAPPED]) {
|
|
34407
|
+
continue;
|
|
34408
|
+
}
|
|
34409
|
+
const wrappedExecute = function(...args) {
|
|
34410
|
+
return runWithAutoInstrumentationAllowed(
|
|
34411
|
+
() => Reflect.apply(execute, this, args)
|
|
34412
|
+
);
|
|
34413
|
+
};
|
|
34414
|
+
Object.defineProperty(wrappedExecute, PI_TOOL_EXECUTE_WRAPPED, {
|
|
34415
|
+
configurable: false,
|
|
34416
|
+
enumerable: false,
|
|
34417
|
+
value: true,
|
|
34418
|
+
writable: false
|
|
34419
|
+
});
|
|
34420
|
+
tool.execute = wrappedExecute;
|
|
34421
|
+
} catch (error) {
|
|
34422
|
+
logInstrumentationError4("Pi Coding Agent tool wrapping", error);
|
|
34423
|
+
}
|
|
34424
|
+
}
|
|
34425
|
+
}
|
|
34049
34426
|
async function startPiLlmSpan(state, model, context, options) {
|
|
34050
34427
|
const metadata = {
|
|
34051
34428
|
...extractModelMetadata2(model),
|
|
@@ -34217,35 +34594,26 @@ async function startPiToolSpan(state, event) {
|
|
|
34217
34594
|
if (!event.toolCallId || state.activeToolSpans.has(event.toolCallId)) {
|
|
34218
34595
|
return;
|
|
34219
34596
|
}
|
|
34220
|
-
const restoreAutoInstrumentation = enterAutoInstrumentationAllowed();
|
|
34221
34597
|
const metadata = {
|
|
34222
34598
|
"gen_ai.tool.call.id": event.toolCallId,
|
|
34223
34599
|
"gen_ai.tool.name": event.toolName,
|
|
34224
34600
|
"pi_coding_agent.tool.name": event.toolName
|
|
34225
34601
|
};
|
|
34226
|
-
|
|
34227
|
-
|
|
34228
|
-
|
|
34229
|
-
{
|
|
34230
|
-
|
|
34231
|
-
|
|
34232
|
-
metadata
|
|
34233
|
-
},
|
|
34234
|
-
name: event.toolName || "tool",
|
|
34235
|
-
parent: await state.span.export(),
|
|
34236
|
-
spanAttributes: { type: "tool" /* TOOL */ }
|
|
34602
|
+
const span = startSpan(
|
|
34603
|
+
withSpanInstrumentationName(
|
|
34604
|
+
{
|
|
34605
|
+
event: {
|
|
34606
|
+
input: event.args,
|
|
34607
|
+
metadata
|
|
34237
34608
|
},
|
|
34238
|
-
|
|
34239
|
-
|
|
34240
|
-
|
|
34241
|
-
|
|
34242
|
-
|
|
34243
|
-
|
|
34244
|
-
|
|
34245
|
-
|
|
34246
|
-
restoreAutoInstrumentation();
|
|
34247
|
-
throw error;
|
|
34248
|
-
}
|
|
34609
|
+
name: event.toolName || "tool",
|
|
34610
|
+
parent: await state.span.export(),
|
|
34611
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
34612
|
+
},
|
|
34613
|
+
INSTRUMENTATION_NAMES.PI_CODING_AGENT
|
|
34614
|
+
)
|
|
34615
|
+
);
|
|
34616
|
+
state.activeToolSpans.set(event.toolCallId, { span });
|
|
34249
34617
|
}
|
|
34250
34618
|
function finishPiToolSpan(state, event) {
|
|
34251
34619
|
const toolState = state.activeToolSpans.get(event.toolCallId);
|
|
@@ -34266,11 +34634,7 @@ function finishPiToolSpan(state, event) {
|
|
|
34266
34634
|
output: event.result
|
|
34267
34635
|
});
|
|
34268
34636
|
} finally {
|
|
34269
|
-
|
|
34270
|
-
toolState.span.end();
|
|
34271
|
-
} finally {
|
|
34272
|
-
toolState.restoreAutoInstrumentation?.();
|
|
34273
|
-
}
|
|
34637
|
+
toolState.span.end();
|
|
34274
34638
|
}
|
|
34275
34639
|
}
|
|
34276
34640
|
function finishPiPromptRun(state, error) {
|
|
@@ -34320,10 +34684,10 @@ function finishPiLlmSpan(promptState, llmState, message, error) {
|
|
|
34320
34684
|
...cleanMetrics5(llmState.metrics),
|
|
34321
34685
|
...buildDurationMetrics3(llmState.startTime)
|
|
34322
34686
|
};
|
|
34323
|
-
const
|
|
34324
|
-
if (Object.keys(
|
|
34687
|
+
const usageMetrics2 = extractUsageMetrics2(message?.usage);
|
|
34688
|
+
if (Object.keys(usageMetrics2).length > 0) {
|
|
34325
34689
|
promptState.collectedLlmUsageMetrics = true;
|
|
34326
|
-
addMetrics(promptState.metrics,
|
|
34690
|
+
addMetrics(promptState.metrics, usageMetrics2);
|
|
34327
34691
|
}
|
|
34328
34692
|
try {
|
|
34329
34693
|
safeLog4(llmState.span, {
|
|
@@ -34341,14 +34705,10 @@ function finishPiLlmSpan(promptState, llmState, message, error) {
|
|
|
34341
34705
|
}
|
|
34342
34706
|
function finishOpenToolSpans(state, error) {
|
|
34343
34707
|
for (const [, toolState] of state.activeToolSpans) {
|
|
34344
|
-
|
|
34345
|
-
|
|
34346
|
-
|
|
34347
|
-
|
|
34348
|
-
toolState.span.end();
|
|
34349
|
-
} finally {
|
|
34350
|
-
toolState.restoreAutoInstrumentation?.();
|
|
34351
|
-
}
|
|
34708
|
+
safeLog4(toolState.span, {
|
|
34709
|
+
error: error ? toLoggedError(error) : "Pi tool did not complete"
|
|
34710
|
+
});
|
|
34711
|
+
toolState.span.end();
|
|
34352
34712
|
}
|
|
34353
34713
|
state.activeToolSpans.clear();
|
|
34354
34714
|
}
|
|
@@ -34667,12 +35027,12 @@ var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
|
|
|
34667
35027
|
var StrandsAgentSDKPlugin = class extends BasePlugin {
|
|
34668
35028
|
activeChildParents = /* @__PURE__ */ new WeakMap();
|
|
34669
35029
|
onEnable() {
|
|
34670
|
-
this.
|
|
34671
|
-
this.
|
|
35030
|
+
this.interceptAgentStream();
|
|
35031
|
+
this.interceptMultiAgentStream(
|
|
34672
35032
|
strandsAgentSDKChannels.graphStream,
|
|
34673
35033
|
"Graph.stream"
|
|
34674
35034
|
);
|
|
34675
|
-
this.
|
|
35035
|
+
this.interceptMultiAgentStream(
|
|
34676
35036
|
strandsAgentSDKChannels.swarmStream,
|
|
34677
35037
|
"Swarm.stream"
|
|
34678
35038
|
);
|
|
@@ -34683,122 +35043,92 @@ var StrandsAgentSDKPlugin = class extends BasePlugin {
|
|
|
34683
35043
|
}
|
|
34684
35044
|
this.unsubscribers = [];
|
|
34685
35045
|
}
|
|
34686
|
-
|
|
34687
|
-
|
|
34688
|
-
|
|
34689
|
-
|
|
34690
|
-
|
|
34691
|
-
|
|
34692
|
-
|
|
34693
|
-
|
|
34694
|
-
|
|
34695
|
-
|
|
34696
|
-
|
|
34697
|
-
|
|
34698
|
-
|
|
34699
|
-
|
|
34700
|
-
|
|
34701
|
-
|
|
34702
|
-
const result = event.result;
|
|
34703
|
-
if (isAsyncIterable(result)) {
|
|
34704
|
-
patchStreamIfNeeded(result, {
|
|
34705
|
-
aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
|
|
34706
|
-
onChunk: (chunk) => handleAgentStreamEvent(state, chunk),
|
|
34707
|
-
onComplete: () => {
|
|
34708
|
-
finalizeAgentStream(state);
|
|
34709
|
-
states.delete(event);
|
|
34710
|
-
},
|
|
34711
|
-
onError: (error) => {
|
|
34712
|
-
finalizeAgentStream(state, error);
|
|
34713
|
-
states.delete(event);
|
|
34714
|
-
}
|
|
34715
|
-
});
|
|
34716
|
-
return;
|
|
34717
|
-
}
|
|
34718
|
-
finalizeAgentStream(state, void 0, result);
|
|
34719
|
-
states.delete(event);
|
|
34720
|
-
},
|
|
34721
|
-
error: (event) => {
|
|
34722
|
-
const state = states.get(event);
|
|
34723
|
-
if (!state || !event.error) {
|
|
34724
|
-
return;
|
|
34725
|
-
}
|
|
34726
|
-
finalizeAgentStream(state, event.error);
|
|
34727
|
-
states.delete(event);
|
|
34728
|
-
}
|
|
34729
|
-
};
|
|
34730
|
-
channel2.subscribe(handlers);
|
|
34731
|
-
this.unsubscribers.push(() => {
|
|
34732
|
-
unbindAutoInstrumentationSuppression?.();
|
|
34733
|
-
channel2.unsubscribe(handlers);
|
|
34734
|
-
});
|
|
35046
|
+
interceptAgentStream() {
|
|
35047
|
+
this.unsubscribers.push(
|
|
35048
|
+
strandsAgentSDKChannels.agentStream.intercept(
|
|
35049
|
+
(target, thisArg, args, additional) => instrumentStrandsStreamInvocation({
|
|
35050
|
+
finalize: finalizeAgentStream,
|
|
35051
|
+
handleChunk: handleAgentStreamEvent,
|
|
35052
|
+
invoke: () => Reflect.apply(target, thisArg, args),
|
|
35053
|
+
name: "Strands Agent SDK",
|
|
35054
|
+
start: () => startAgentStream(
|
|
35055
|
+
args[0],
|
|
35056
|
+
extractAgent(additional.agent, thisArg),
|
|
35057
|
+
this.activeChildParents
|
|
35058
|
+
)
|
|
35059
|
+
})
|
|
35060
|
+
)
|
|
35061
|
+
);
|
|
34735
35062
|
}
|
|
34736
|
-
|
|
34737
|
-
|
|
34738
|
-
|
|
34739
|
-
|
|
34740
|
-
|
|
34741
|
-
|
|
34742
|
-
|
|
34743
|
-
|
|
34744
|
-
|
|
34745
|
-
|
|
34746
|
-
|
|
34747
|
-
|
|
34748
|
-
|
|
34749
|
-
|
|
34750
|
-
|
|
34751
|
-
|
|
34752
|
-
|
|
34753
|
-
|
|
34754
|
-
|
|
34755
|
-
}
|
|
34756
|
-
|
|
34757
|
-
|
|
34758
|
-
|
|
34759
|
-
|
|
34760
|
-
|
|
34761
|
-
|
|
34762
|
-
|
|
34763
|
-
|
|
34764
|
-
|
|
34765
|
-
|
|
34766
|
-
|
|
34767
|
-
|
|
34768
|
-
|
|
34769
|
-
|
|
34770
|
-
|
|
34771
|
-
|
|
34772
|
-
|
|
34773
|
-
|
|
34774
|
-
|
|
34775
|
-
|
|
34776
|
-
|
|
34777
|
-
|
|
34778
|
-
this.activeChildParents,
|
|
34779
|
-
void 0,
|
|
34780
|
-
result
|
|
35063
|
+
interceptMultiAgentStream(channel2, operation) {
|
|
35064
|
+
this.unsubscribers.push(
|
|
35065
|
+
channel2.intercept(
|
|
35066
|
+
(target, thisArg, args, additional) => instrumentStrandsStreamInvocation({
|
|
35067
|
+
finalize: (state, error, output) => finalizeMultiAgentStream(
|
|
35068
|
+
state,
|
|
35069
|
+
this.activeChildParents,
|
|
35070
|
+
error,
|
|
35071
|
+
output
|
|
35072
|
+
),
|
|
35073
|
+
handleChunk: (state, chunk) => handleMultiAgentStreamEvent(state, chunk, this.activeChildParents),
|
|
35074
|
+
invoke: () => Reflect.apply(target, thisArg, args),
|
|
35075
|
+
name: "Strands multi-agent",
|
|
35076
|
+
start: () => startMultiAgentStream(
|
|
35077
|
+
args[0],
|
|
35078
|
+
extractOrchestrator(additional.orchestrator, thisArg),
|
|
35079
|
+
operation,
|
|
35080
|
+
this.activeChildParents
|
|
35081
|
+
)
|
|
35082
|
+
})
|
|
35083
|
+
)
|
|
35084
|
+
);
|
|
35085
|
+
}
|
|
35086
|
+
};
|
|
35087
|
+
function instrumentStrandsStreamInvocation(options) {
|
|
35088
|
+
let state;
|
|
35089
|
+
try {
|
|
35090
|
+
state = options.start();
|
|
35091
|
+
} catch (error) {
|
|
35092
|
+
debugLogger.error(`Error starting ${options.name} instrumentation:`, error);
|
|
35093
|
+
}
|
|
35094
|
+
let result;
|
|
35095
|
+
try {
|
|
35096
|
+
result = runWithAutoInstrumentationSuppressed(options.invoke);
|
|
35097
|
+
} catch (error) {
|
|
35098
|
+
if (state) {
|
|
35099
|
+
try {
|
|
35100
|
+
options.finalize(state, error);
|
|
35101
|
+
} catch (instrumentationError) {
|
|
35102
|
+
debugLogger.error(
|
|
35103
|
+
`Error handling ${options.name} instrumentation failure:`,
|
|
35104
|
+
instrumentationError
|
|
34781
35105
|
);
|
|
34782
|
-
states.delete(event);
|
|
34783
|
-
},
|
|
34784
|
-
error: (event) => {
|
|
34785
|
-
const state = states.get(event);
|
|
34786
|
-
if (!state || !event.error) {
|
|
34787
|
-
return;
|
|
34788
|
-
}
|
|
34789
|
-
finalizeMultiAgentStream(state, this.activeChildParents, event.error);
|
|
34790
|
-
states.delete(event);
|
|
34791
35106
|
}
|
|
34792
|
-
}
|
|
34793
|
-
|
|
34794
|
-
this.unsubscribers.push(() => {
|
|
34795
|
-
unbindAutoInstrumentationSuppression?.();
|
|
34796
|
-
tracingChannel.unsubscribe(handlers);
|
|
34797
|
-
});
|
|
35107
|
+
}
|
|
35108
|
+
throw error;
|
|
34798
35109
|
}
|
|
34799
|
-
|
|
34800
|
-
|
|
34801
|
-
|
|
35110
|
+
if (state) {
|
|
35111
|
+
try {
|
|
35112
|
+
if (isAsyncIterable(result)) {
|
|
35113
|
+
patchStreamIfNeeded(result, {
|
|
35114
|
+
aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
|
|
35115
|
+
onChunk: (chunk) => options.handleChunk(state, chunk),
|
|
35116
|
+
onComplete: () => options.finalize(state),
|
|
35117
|
+
onError: (error) => options.finalize(state, error)
|
|
35118
|
+
});
|
|
35119
|
+
} else {
|
|
35120
|
+
options.finalize(state, void 0, result);
|
|
35121
|
+
}
|
|
35122
|
+
} catch (error) {
|
|
35123
|
+
debugLogger.error(
|
|
35124
|
+
`Error finalizing ${options.name} instrumentation:`,
|
|
35125
|
+
error
|
|
35126
|
+
);
|
|
35127
|
+
}
|
|
35128
|
+
}
|
|
35129
|
+
return result;
|
|
35130
|
+
}
|
|
35131
|
+
function startAgentStream(input, agent, activeChildParents) {
|
|
34802
35132
|
const model = agent?.model;
|
|
34803
35133
|
const metadata = {
|
|
34804
35134
|
...extractAgentMetadata2(agent),
|
|
@@ -34808,17 +35138,14 @@ function startAgentStream(event, activeChildParents) {
|
|
|
34808
35138
|
};
|
|
34809
35139
|
const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
|
|
34810
35140
|
const attachmentCache = createStrandsAttachmentCache();
|
|
34811
|
-
const
|
|
34812
|
-
event.arguments[0],
|
|
34813
|
-
attachmentCache
|
|
34814
|
-
);
|
|
35141
|
+
const processedInput = processStrandsInputAttachments(input, attachmentCache);
|
|
34815
35142
|
const span = parentSpan ? withCurrent(
|
|
34816
35143
|
parentSpan,
|
|
34817
35144
|
() => startSpan(
|
|
34818
35145
|
withSpanInstrumentationName(
|
|
34819
35146
|
{
|
|
34820
35147
|
event: {
|
|
34821
|
-
input,
|
|
35148
|
+
input: processedInput,
|
|
34822
35149
|
metadata
|
|
34823
35150
|
},
|
|
34824
35151
|
name: formatAgentSpanName(agent),
|
|
@@ -34831,7 +35158,7 @@ function startAgentStream(event, activeChildParents) {
|
|
|
34831
35158
|
withSpanInstrumentationName(
|
|
34832
35159
|
{
|
|
34833
35160
|
event: {
|
|
34834
|
-
input,
|
|
35161
|
+
input: processedInput,
|
|
34835
35162
|
metadata
|
|
34836
35163
|
},
|
|
34837
35164
|
name: formatAgentSpanName(agent),
|
|
@@ -34849,22 +35176,21 @@ function startAgentStream(event, activeChildParents) {
|
|
|
34849
35176
|
startTime: getCurrentUnixTimestamp()
|
|
34850
35177
|
};
|
|
34851
35178
|
}
|
|
34852
|
-
function startMultiAgentStream(
|
|
34853
|
-
const orchestrator = extractOrchestrator(event);
|
|
35179
|
+
function startMultiAgentStream(input, orchestrator, operation, activeChildParents) {
|
|
34854
35180
|
const metadata = {
|
|
34855
35181
|
"strands.operation": operation,
|
|
34856
35182
|
provider: "strands",
|
|
34857
35183
|
...orchestrator?.id ? { "strands.orchestrator.id": orchestrator.id } : {}
|
|
34858
35184
|
};
|
|
34859
35185
|
const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
|
|
34860
|
-
const
|
|
35186
|
+
const processedInput = processStrandsInputAttachments(input);
|
|
34861
35187
|
const span = parentSpan ? withCurrent(
|
|
34862
35188
|
parentSpan,
|
|
34863
35189
|
() => startSpan(
|
|
34864
35190
|
withSpanInstrumentationName(
|
|
34865
35191
|
{
|
|
34866
35192
|
event: {
|
|
34867
|
-
input,
|
|
35193
|
+
input: processedInput,
|
|
34868
35194
|
metadata
|
|
34869
35195
|
},
|
|
34870
35196
|
name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
|
|
@@ -34877,7 +35203,7 @@ function startMultiAgentStream(event, operation, activeChildParents) {
|
|
|
34877
35203
|
withSpanInstrumentationName(
|
|
34878
35204
|
{
|
|
34879
35205
|
event: {
|
|
34880
|
-
input,
|
|
35206
|
+
input: processedInput,
|
|
34881
35207
|
metadata
|
|
34882
35208
|
},
|
|
34883
35209
|
name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
|
|
@@ -35248,12 +35574,12 @@ function finalizeMultiAgentStream(state, activeChildParents, error, output) {
|
|
|
35248
35574
|
});
|
|
35249
35575
|
state.span.end();
|
|
35250
35576
|
}
|
|
35251
|
-
function extractAgent(
|
|
35252
|
-
const candidate =
|
|
35577
|
+
function extractAgent(agent, self) {
|
|
35578
|
+
const candidate = agent ?? self;
|
|
35253
35579
|
return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
|
|
35254
35580
|
}
|
|
35255
|
-
function extractOrchestrator(
|
|
35256
|
-
const candidate =
|
|
35581
|
+
function extractOrchestrator(orchestrator, self) {
|
|
35582
|
+
const candidate = orchestrator ?? self;
|
|
35257
35583
|
return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
|
|
35258
35584
|
}
|
|
35259
35585
|
function extractAgentMetadata2(agent) {
|
|
@@ -37767,6 +38093,13 @@ function wrapAISDK(aiSDK, options = {}) {
|
|
|
37767
38093
|
switch (prop) {
|
|
37768
38094
|
case "generateText":
|
|
37769
38095
|
return wrapGenerateText(typedAISDK.generateText, options, typedAISDK);
|
|
38096
|
+
case "generateImage":
|
|
38097
|
+
case "experimental_generateImage":
|
|
38098
|
+
return typeof original === "function" ? wrapGenerateImage(
|
|
38099
|
+
original,
|
|
38100
|
+
options,
|
|
38101
|
+
typedAISDK
|
|
38102
|
+
) : original;
|
|
37770
38103
|
case "streamText":
|
|
37771
38104
|
return wrapStreamText(typedAISDK.streamText, options, typedAISDK);
|
|
37772
38105
|
case "generateObject":
|
|
@@ -38003,6 +38336,32 @@ var wrapGenerateObject = (generateObject, options = {}, aiSDK) => {
|
|
|
38003
38336
|
options
|
|
38004
38337
|
);
|
|
38005
38338
|
};
|
|
38339
|
+
var wrapGenerateImage = (generateImage, options = {}, aiSDK) => {
|
|
38340
|
+
return makeGenerateImageWrapper(generateImage, { aiSDK }, options);
|
|
38341
|
+
};
|
|
38342
|
+
var makeGenerateImageWrapper = (generateImage, contextOptions = {}, options = {}) => {
|
|
38343
|
+
const wrapper = async function(allParams) {
|
|
38344
|
+
const { span_info, ...params } = allParams;
|
|
38345
|
+
const tracedParams = { ...params };
|
|
38346
|
+
return aiSDKChannels.generateImage.tracePromise(
|
|
38347
|
+
() => generateImage(tracedParams),
|
|
38348
|
+
createAISDKChannelContext(tracedParams, {
|
|
38349
|
+
aiSDK: contextOptions.aiSDK,
|
|
38350
|
+
denyOutputPaths: options.denyOutputPaths,
|
|
38351
|
+
self: contextOptions.self,
|
|
38352
|
+
span_info: mergeSpanInfo(span_info, {
|
|
38353
|
+
name: "generateImage",
|
|
38354
|
+
spanType: contextOptions.spanType
|
|
38355
|
+
})
|
|
38356
|
+
})
|
|
38357
|
+
);
|
|
38358
|
+
};
|
|
38359
|
+
Object.defineProperty(wrapper, "name", {
|
|
38360
|
+
value: "generateImage",
|
|
38361
|
+
writable: false
|
|
38362
|
+
});
|
|
38363
|
+
return wrapper;
|
|
38364
|
+
};
|
|
38006
38365
|
var makeEmbedWrapper = (channel2, name, embed, contextOptions = {}, options = {}) => {
|
|
38007
38366
|
const wrapper = async function(allParams) {
|
|
38008
38367
|
const { span_info, ...params } = allParams;
|
|
@@ -38753,7 +39112,7 @@ function braintrustEveHook(options) {
|
|
|
38753
39112
|
}
|
|
38754
39113
|
};
|
|
38755
39114
|
}
|
|
38756
|
-
function
|
|
39115
|
+
function createLegacyEveInstrumentation(options) {
|
|
38757
39116
|
const state = options.defineState(EVE_TRACE_STATE_KEY, emptyEveTraceState);
|
|
38758
39117
|
return {
|
|
38759
39118
|
events: {
|
|
@@ -40141,8 +40500,10 @@ function capturedModelInput(modelInput) {
|
|
|
40141
40500
|
const value = [];
|
|
40142
40501
|
if (typeof instructions === "string") {
|
|
40143
40502
|
value.push({ content: instructions, role: "system" });
|
|
40144
|
-
} else if (instructions) {
|
|
40503
|
+
} else if (Array.isArray(instructions)) {
|
|
40145
40504
|
value.push(...instructions.map(capturedEveModelMessage));
|
|
40505
|
+
} else if (instructions) {
|
|
40506
|
+
value.push(capturedEveModelMessage(instructions));
|
|
40146
40507
|
}
|
|
40147
40508
|
value.push(...messages.map(capturedEveModelMessage));
|
|
40148
40509
|
try {
|
|
@@ -40386,6 +40747,464 @@ async function deterministicEveId(...parts) {
|
|
|
40386
40747
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
40387
40748
|
}
|
|
40388
40749
|
|
|
40750
|
+
// src/instrumentation/plugins/eve-provider.ts
|
|
40751
|
+
var MAX_EVE_PROVIDER_CACHE_ENTRIES = 1e4;
|
|
40752
|
+
function createEveInstrumentationProvider(options = {}) {
|
|
40753
|
+
const bridge = new EveProviderBridge(options.metadata);
|
|
40754
|
+
return {
|
|
40755
|
+
capture: "content",
|
|
40756
|
+
events: {
|
|
40757
|
+
"action.completed": (event, context) => bridge.handleActionTerminal(event, context),
|
|
40758
|
+
"action.failed": (event, context) => bridge.handleActionTerminal(event, context),
|
|
40759
|
+
"action.started": (event, context) => bridge.handleActionStarted(event, context),
|
|
40760
|
+
"model.call.completed": (event, context) => bridge.handleModelTerminal(event, context),
|
|
40761
|
+
"model.call.failed": (event, context) => bridge.handleModelTerminal(event, context),
|
|
40762
|
+
"model.call.started": (event, context) => bridge.handleModelStarted(event, context),
|
|
40763
|
+
"step.attempt.completed": (event) => bridge.handleStepAttemptTerminal(event),
|
|
40764
|
+
"step.attempt.failed": (event) => bridge.handleStepAttemptTerminal(event),
|
|
40765
|
+
"turn.cancelled": (event, context) => bridge.handleTurnTerminal(event, context),
|
|
40766
|
+
"turn.completed": (event, context) => bridge.handleTurnTerminal(event, context),
|
|
40767
|
+
"turn.failed": (event, context) => bridge.handleTurnTerminal(event, context),
|
|
40768
|
+
"turn.started": (event, context) => bridge.handleTurnStarted(event, context)
|
|
40769
|
+
},
|
|
40770
|
+
flush,
|
|
40771
|
+
setup: options.setup
|
|
40772
|
+
};
|
|
40773
|
+
}
|
|
40774
|
+
var EveProviderBridge = class {
|
|
40775
|
+
constructor(metadata) {
|
|
40776
|
+
this.metadata = metadata;
|
|
40777
|
+
}
|
|
40778
|
+
metadata;
|
|
40779
|
+
activeActions = /* @__PURE__ */ new Map();
|
|
40780
|
+
activeModels = /* @__PURE__ */ new Map();
|
|
40781
|
+
modelsByAttempt = /* @__PURE__ */ new Map();
|
|
40782
|
+
settledOperations = new LRUCache({
|
|
40783
|
+
max: MAX_EVE_PROVIDER_CACHE_ENTRIES
|
|
40784
|
+
});
|
|
40785
|
+
turns = new LRUCache({
|
|
40786
|
+
max: MAX_EVE_PROVIDER_CACHE_ENTRIES
|
|
40787
|
+
});
|
|
40788
|
+
async handleTurnStarted(event, context) {
|
|
40789
|
+
await this.contain("turn start", async () => {
|
|
40790
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40791
|
+
context.state.set({ skip: true });
|
|
40792
|
+
return;
|
|
40793
|
+
}
|
|
40794
|
+
const { rowId, spanId } = await generateEveIds(
|
|
40795
|
+
"turn",
|
|
40796
|
+
event.idempotencyKey
|
|
40797
|
+
);
|
|
40798
|
+
const key = turnKey3(event.sessionId, event.turnId);
|
|
40799
|
+
const stored = storedSpan(context);
|
|
40800
|
+
if (stored && "exported" in stored && stored.rootSpanId) {
|
|
40801
|
+
this.turns.set(key, { rootSpanId: stored.rootSpanId, spanId });
|
|
40802
|
+
return;
|
|
40803
|
+
}
|
|
40804
|
+
let rootSpanId = spanId;
|
|
40805
|
+
let parentSpanId;
|
|
40806
|
+
if (event.parentLineage) {
|
|
40807
|
+
const parentTurnKey = turnKey3(
|
|
40808
|
+
event.parentLineage.sessionId,
|
|
40809
|
+
event.parentLineage.turnId
|
|
40810
|
+
);
|
|
40811
|
+
const parentTurn = this.turns.get(parentTurnKey);
|
|
40812
|
+
rootSpanId = parentTurn?.rootSpanId ?? // Eve propagates the root session through every nested subagent. Use
|
|
40813
|
+
// it instead of the immediate parent session when no ancestor ran
|
|
40814
|
+
// in-process.
|
|
40815
|
+
(await generateEveIds(
|
|
40816
|
+
"turn",
|
|
40817
|
+
turnIdempotencyKey(
|
|
40818
|
+
event.rootSessionId,
|
|
40819
|
+
event.parentLineage.turnId
|
|
40820
|
+
)
|
|
40821
|
+
)).spanId;
|
|
40822
|
+
parentSpanId = (await generateEveIds(
|
|
40823
|
+
"subagent",
|
|
40824
|
+
actionIdempotencyKey(
|
|
40825
|
+
event.parentLineage.sessionId,
|
|
40826
|
+
event.parentLineage.turnId,
|
|
40827
|
+
event.parentLineage.callId
|
|
40828
|
+
)
|
|
40829
|
+
)).spanId;
|
|
40830
|
+
}
|
|
40831
|
+
const metadata = this.spanMetadata(event.sessionId);
|
|
40832
|
+
const span = await this.startSpan(
|
|
40833
|
+
context,
|
|
40834
|
+
{
|
|
40835
|
+
event: { id: rowId, metadata },
|
|
40836
|
+
name: "eve.turn",
|
|
40837
|
+
parentSpanIds: parentSpanId ? { rootSpanId, spanId: parentSpanId } : { parentSpanIds: [], rootSpanId },
|
|
40838
|
+
spanAttributes: { type: "task" /* TASK */ },
|
|
40839
|
+
spanId
|
|
40840
|
+
},
|
|
40841
|
+
rootSpanId
|
|
40842
|
+
);
|
|
40843
|
+
span?.log({ metadata });
|
|
40844
|
+
this.turns.set(key, {
|
|
40845
|
+
rootSpanId,
|
|
40846
|
+
spanId
|
|
40847
|
+
});
|
|
40848
|
+
});
|
|
40849
|
+
}
|
|
40850
|
+
async handleTurnTerminal(event, context) {
|
|
40851
|
+
await this.contain("turn terminal", async () => {
|
|
40852
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40853
|
+
context.state.set(void 0);
|
|
40854
|
+
return;
|
|
40855
|
+
}
|
|
40856
|
+
const error = event.type === "turn.failed" ? event.error : void 0;
|
|
40857
|
+
this.drainActionsForTurn(event.sessionId, event.turnId, error);
|
|
40858
|
+
const stored = storedSpan(context);
|
|
40859
|
+
if (stored && "exported" in stored) {
|
|
40860
|
+
updateSpan({
|
|
40861
|
+
exported: stored.exported,
|
|
40862
|
+
...error !== void 0 ? { error } : {},
|
|
40863
|
+
metrics: { end: Date.now() / 1e3 }
|
|
40864
|
+
});
|
|
40865
|
+
}
|
|
40866
|
+
this.settledOperations.set(event.idempotencyKey, true);
|
|
40867
|
+
context.state.set(void 0);
|
|
40868
|
+
this.turns.delete(turnKey3(event.sessionId, event.turnId));
|
|
40869
|
+
});
|
|
40870
|
+
}
|
|
40871
|
+
async handleModelStarted(event, context) {
|
|
40872
|
+
await this.contain("model start", async () => {
|
|
40873
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40874
|
+
context.state.set({ skip: true });
|
|
40875
|
+
return;
|
|
40876
|
+
}
|
|
40877
|
+
const parent = await this.parentForScope(event.scope);
|
|
40878
|
+
const { rowId, spanId } = await generateEveIds(
|
|
40879
|
+
"step",
|
|
40880
|
+
event.idempotencyKey
|
|
40881
|
+
);
|
|
40882
|
+
const input = event.input ? capturedModelInput(event.input) : void 0;
|
|
40883
|
+
const metadata = {
|
|
40884
|
+
...this.spanMetadata(event.scope.sessionId),
|
|
40885
|
+
model: event.model.modelId,
|
|
40886
|
+
provider: event.model.provider
|
|
40887
|
+
};
|
|
40888
|
+
const span = await this.startSpan(context, {
|
|
40889
|
+
event: {
|
|
40890
|
+
id: rowId,
|
|
40891
|
+
...input !== void 0 ? { input } : {},
|
|
40892
|
+
metadata
|
|
40893
|
+
},
|
|
40894
|
+
name: "eve.step",
|
|
40895
|
+
parentSpanIds: parent,
|
|
40896
|
+
spanAttributes: { type: "llm" /* LLM */ },
|
|
40897
|
+
spanId
|
|
40898
|
+
});
|
|
40899
|
+
if (!span) return;
|
|
40900
|
+
span.log({ ...input !== void 0 ? { input } : {}, metadata });
|
|
40901
|
+
this.activeModels.set(event.idempotencyKey, {
|
|
40902
|
+
span,
|
|
40903
|
+
turnKey: turnKey3(event.scope.sessionId, event.scope.turnId)
|
|
40904
|
+
});
|
|
40905
|
+
const keys = this.modelsByAttempt.get(event.scope.attemptId) ?? /* @__PURE__ */ new Set();
|
|
40906
|
+
keys.add(event.idempotencyKey);
|
|
40907
|
+
this.modelsByAttempt.set(event.scope.attemptId, keys);
|
|
40908
|
+
});
|
|
40909
|
+
}
|
|
40910
|
+
async handleModelTerminal(event, context) {
|
|
40911
|
+
await this.contain("model terminal", async () => {
|
|
40912
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40913
|
+
context.state.set(void 0);
|
|
40914
|
+
return;
|
|
40915
|
+
}
|
|
40916
|
+
const active = this.activeModels.get(event.idempotencyKey);
|
|
40917
|
+
if (active) {
|
|
40918
|
+
const span = active.span;
|
|
40919
|
+
if (event.type === "model.call.failed") {
|
|
40920
|
+
if (event.error !== void 0) span.log({ error: event.error });
|
|
40921
|
+
} else {
|
|
40922
|
+
span.log({
|
|
40923
|
+
metrics: usageMetrics(event.usage),
|
|
40924
|
+
output: modelOutput(event)
|
|
40925
|
+
});
|
|
40926
|
+
}
|
|
40927
|
+
span.end();
|
|
40928
|
+
} else {
|
|
40929
|
+
const stored = storedSpan(context);
|
|
40930
|
+
if (stored && "exported" in stored) {
|
|
40931
|
+
updateSpan({
|
|
40932
|
+
exported: stored.exported,
|
|
40933
|
+
...event.type === "model.call.failed" ? event.error !== void 0 ? { error: event.error } : {} : { output: modelOutput(event) },
|
|
40934
|
+
metrics: {
|
|
40935
|
+
...event.type === "model.call.completed" ? usageMetrics(event.usage) : {},
|
|
40936
|
+
end: Date.now() / 1e3
|
|
40937
|
+
}
|
|
40938
|
+
});
|
|
40939
|
+
}
|
|
40940
|
+
}
|
|
40941
|
+
this.settledOperations.set(event.idempotencyKey, true);
|
|
40942
|
+
this.forgetModel(event.scope.attemptId, event.idempotencyKey);
|
|
40943
|
+
context.state.set(void 0);
|
|
40944
|
+
});
|
|
40945
|
+
}
|
|
40946
|
+
async handleActionStarted(event, context) {
|
|
40947
|
+
await this.contain("action start", async () => {
|
|
40948
|
+
if (event.kind === "load-skill" || this.settledOperations.has(event.idempotencyKey)) {
|
|
40949
|
+
context.state.set({ skip: true });
|
|
40950
|
+
return;
|
|
40951
|
+
}
|
|
40952
|
+
const parent = await this.parentForScope(event.scope);
|
|
40953
|
+
const { rowId, spanId } = await generateEveIds(
|
|
40954
|
+
event.kind === "subagent-call" ? "subagent" : "tool",
|
|
40955
|
+
event.idempotencyKey
|
|
40956
|
+
);
|
|
40957
|
+
const metadata = {
|
|
40958
|
+
...this.spanMetadata(event.scope.sessionId),
|
|
40959
|
+
"eve.action_kind": event.kind
|
|
40960
|
+
};
|
|
40961
|
+
const span = await this.startSpan(context, {
|
|
40962
|
+
event: {
|
|
40963
|
+
id: rowId,
|
|
40964
|
+
...event.input !== void 0 ? { input: event.input } : {},
|
|
40965
|
+
metadata
|
|
40966
|
+
},
|
|
40967
|
+
name: event.name,
|
|
40968
|
+
parentSpanIds: parent,
|
|
40969
|
+
spanAttributes: { type: "tool" /* TOOL */ },
|
|
40970
|
+
spanId
|
|
40971
|
+
});
|
|
40972
|
+
if (!span) return;
|
|
40973
|
+
span.log({
|
|
40974
|
+
...event.input !== void 0 ? { input: event.input } : {},
|
|
40975
|
+
metadata
|
|
40976
|
+
});
|
|
40977
|
+
this.activeActions.set(event.idempotencyKey, {
|
|
40978
|
+
span,
|
|
40979
|
+
turnKey: turnKey3(event.scope.sessionId, event.scope.turnId)
|
|
40980
|
+
});
|
|
40981
|
+
});
|
|
40982
|
+
}
|
|
40983
|
+
async handleActionTerminal(event, context) {
|
|
40984
|
+
await this.contain("action terminal", async () => {
|
|
40985
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40986
|
+
context.state.set(void 0);
|
|
40987
|
+
return;
|
|
40988
|
+
}
|
|
40989
|
+
const stored = storedSpan(context);
|
|
40990
|
+
if (stored?.skip) {
|
|
40991
|
+
context.state.set(void 0);
|
|
40992
|
+
return;
|
|
40993
|
+
}
|
|
40994
|
+
const active = this.activeActions.get(event.idempotencyKey);
|
|
40995
|
+
const end = finiteTimestamp(event.acceptedAtMs) ?? Date.now();
|
|
40996
|
+
if (active) {
|
|
40997
|
+
const span = active.span;
|
|
40998
|
+
if (event.type === "action.failed") {
|
|
40999
|
+
span.log({
|
|
41000
|
+
error: event.error ?? new Error(event.errorCode ?? `Eve action ${event.outcome}`)
|
|
41001
|
+
});
|
|
41002
|
+
} else if (event.output.type === "error") {
|
|
41003
|
+
span.log({
|
|
41004
|
+
error: event.output.error ?? new Error("Eve action returned an error")
|
|
41005
|
+
});
|
|
41006
|
+
} else {
|
|
41007
|
+
span.log({ output: event.output.output });
|
|
41008
|
+
}
|
|
41009
|
+
span.end({ endTime: end / 1e3 });
|
|
41010
|
+
} else {
|
|
41011
|
+
const stored2 = storedSpan(context);
|
|
41012
|
+
if (stored2 && "exported" in stored2) {
|
|
41013
|
+
updateSpan({
|
|
41014
|
+
exported: stored2.exported,
|
|
41015
|
+
...event.type === "action.failed" ? {
|
|
41016
|
+
error: event.error ?? new Error(event.errorCode ?? `Eve action ${event.outcome}`)
|
|
41017
|
+
} : event.output.type === "error" ? {
|
|
41018
|
+
error: event.output.error ?? new Error("Eve action returned an error")
|
|
41019
|
+
} : { output: event.output.output },
|
|
41020
|
+
metrics: { end: end / 1e3 }
|
|
41021
|
+
});
|
|
41022
|
+
}
|
|
41023
|
+
}
|
|
41024
|
+
this.settledOperations.set(event.idempotencyKey, true);
|
|
41025
|
+
this.activeActions.delete(event.idempotencyKey);
|
|
41026
|
+
context.state.set(void 0);
|
|
41027
|
+
});
|
|
41028
|
+
}
|
|
41029
|
+
handleStepAttemptTerminal(event) {
|
|
41030
|
+
void this.contain("step attempt terminal", () => {
|
|
41031
|
+
const keys = this.modelsByAttempt.get(event.scope.attemptId);
|
|
41032
|
+
if (!keys) return;
|
|
41033
|
+
for (const key of keys) {
|
|
41034
|
+
const active = this.activeModels.get(key);
|
|
41035
|
+
if (!active) continue;
|
|
41036
|
+
if (event.type === "step.attempt.failed" && event.error !== void 0) {
|
|
41037
|
+
active.span.log({ error: event.error });
|
|
41038
|
+
}
|
|
41039
|
+
active.span.end();
|
|
41040
|
+
this.activeModels.delete(key);
|
|
41041
|
+
}
|
|
41042
|
+
this.modelsByAttempt.delete(event.scope.attemptId);
|
|
41043
|
+
});
|
|
41044
|
+
}
|
|
41045
|
+
async parentForScope(scope) {
|
|
41046
|
+
const key = turnKey3(scope.sessionId, scope.turnId);
|
|
41047
|
+
const known = this.turns.get(key);
|
|
41048
|
+
if (known) {
|
|
41049
|
+
return { rootSpanId: known.rootSpanId, spanId: known.spanId };
|
|
41050
|
+
}
|
|
41051
|
+
const [{ spanId }, { spanId: rootSpanId }] = await Promise.all([
|
|
41052
|
+
generateEveIds("turn", turnIdempotencyKey(scope.sessionId, scope.turnId)),
|
|
41053
|
+
generateEveIds(
|
|
41054
|
+
"turn",
|
|
41055
|
+
turnIdempotencyKey(
|
|
41056
|
+
scope.rootSessionId ?? scope.sessionId,
|
|
41057
|
+
scope.turnId
|
|
41058
|
+
)
|
|
41059
|
+
)
|
|
41060
|
+
]);
|
|
41061
|
+
return { rootSpanId, spanId };
|
|
41062
|
+
}
|
|
41063
|
+
async startSpan(context, args, rootSpanId) {
|
|
41064
|
+
const span = withCurrent(
|
|
41065
|
+
NOOP_SPAN,
|
|
41066
|
+
() => _internalStartSpanWithInitialMerge(
|
|
41067
|
+
withSpanInstrumentationName(args ?? {}, INSTRUMENTATION_NAMES.EVE)
|
|
41068
|
+
)
|
|
41069
|
+
);
|
|
41070
|
+
try {
|
|
41071
|
+
context.state.set({
|
|
41072
|
+
exported: await span.export(),
|
|
41073
|
+
...rootSpanId ? { rootSpanId } : {}
|
|
41074
|
+
});
|
|
41075
|
+
} catch (error) {
|
|
41076
|
+
debugLogger.warn("Error exporting Eve provider span:", error);
|
|
41077
|
+
}
|
|
41078
|
+
return span;
|
|
41079
|
+
}
|
|
41080
|
+
drainActionsForTurn(sessionId, turnId, error) {
|
|
41081
|
+
const key = turnKey3(sessionId, turnId);
|
|
41082
|
+
for (const [idempotencyKey, active] of this.activeActions) {
|
|
41083
|
+
if (active.turnKey !== key) continue;
|
|
41084
|
+
if (error !== void 0) active.span.log({ error });
|
|
41085
|
+
active.span.end();
|
|
41086
|
+
this.activeActions.delete(idempotencyKey);
|
|
41087
|
+
}
|
|
41088
|
+
}
|
|
41089
|
+
forgetModel(attemptId, idempotencyKey) {
|
|
41090
|
+
this.activeModels.delete(idempotencyKey);
|
|
41091
|
+
const keys = this.modelsByAttempt.get(attemptId);
|
|
41092
|
+
keys?.delete(idempotencyKey);
|
|
41093
|
+
if (keys?.size === 0) this.modelsByAttempt.delete(attemptId);
|
|
41094
|
+
}
|
|
41095
|
+
spanMetadata(sessionId) {
|
|
41096
|
+
return {
|
|
41097
|
+
...this.metadata ?? {},
|
|
41098
|
+
"eve.session_id": sessionId
|
|
41099
|
+
};
|
|
41100
|
+
}
|
|
41101
|
+
async contain(operation, fn) {
|
|
41102
|
+
try {
|
|
41103
|
+
await fn();
|
|
41104
|
+
} catch (error) {
|
|
41105
|
+
debugLogger.warn(`Error in Eve provider ${operation}:`, error);
|
|
41106
|
+
}
|
|
41107
|
+
}
|
|
41108
|
+
};
|
|
41109
|
+
function storedSpan(context) {
|
|
41110
|
+
const value = context.state.get();
|
|
41111
|
+
if (!isObject(value)) return void 0;
|
|
41112
|
+
if (value["skip"] === true) return { skip: true };
|
|
41113
|
+
return typeof value["exported"] === "string" ? {
|
|
41114
|
+
exported: value["exported"],
|
|
41115
|
+
...typeof value["rootSpanId"] === "string" && value["rootSpanId"].length > 0 ? { rootSpanId: value["rootSpanId"] } : {}
|
|
41116
|
+
} : void 0;
|
|
41117
|
+
}
|
|
41118
|
+
function modelOutput(event) {
|
|
41119
|
+
const content = event.content ?? [];
|
|
41120
|
+
let text = "";
|
|
41121
|
+
const reasoning = [];
|
|
41122
|
+
const toolCalls2 = [];
|
|
41123
|
+
for (const part of content) {
|
|
41124
|
+
if (part.type === "text") {
|
|
41125
|
+
text += part.text;
|
|
41126
|
+
} else if (part.type === "reasoning" && part.text.trim().length > 0) {
|
|
41127
|
+
reasoning.push({ content: part.text });
|
|
41128
|
+
} else if (part.type === "tool-call") {
|
|
41129
|
+
toolCalls2.push({
|
|
41130
|
+
function: {
|
|
41131
|
+
arguments: safeJsonStringify(part.input),
|
|
41132
|
+
name: part.toolName
|
|
41133
|
+
},
|
|
41134
|
+
id: part.callId,
|
|
41135
|
+
type: "function"
|
|
41136
|
+
});
|
|
41137
|
+
}
|
|
41138
|
+
}
|
|
41139
|
+
return [
|
|
41140
|
+
{
|
|
41141
|
+
finish_reason: normalizeFinishReason3(event.finishReason),
|
|
41142
|
+
index: 0,
|
|
41143
|
+
message: {
|
|
41144
|
+
content: text || null,
|
|
41145
|
+
...reasoning.length > 0 ? { reasoning } : {},
|
|
41146
|
+
role: "assistant",
|
|
41147
|
+
...toolCalls2.length > 0 ? { tool_calls: toolCalls2 } : {}
|
|
41148
|
+
}
|
|
41149
|
+
}
|
|
41150
|
+
];
|
|
41151
|
+
}
|
|
41152
|
+
function usageMetrics(usage) {
|
|
41153
|
+
const promptTokens = nonNegativeNumber(usage.inputTokens);
|
|
41154
|
+
const completionTokens = nonNegativeNumber(usage.outputTokens);
|
|
41155
|
+
const cachedTokens = nonNegativeNumber(
|
|
41156
|
+
usage.inputTokenDetails?.cacheReadTokens
|
|
41157
|
+
);
|
|
41158
|
+
const cacheCreationTokens = nonNegativeNumber(
|
|
41159
|
+
usage.inputTokenDetails?.cacheWriteTokens
|
|
41160
|
+
);
|
|
41161
|
+
return {
|
|
41162
|
+
...promptTokens !== void 0 ? { prompt_tokens: promptTokens } : {},
|
|
41163
|
+
...completionTokens !== void 0 ? { completion_tokens: completionTokens } : {},
|
|
41164
|
+
...promptTokens !== void 0 && completionTokens !== void 0 ? { tokens: promptTokens + completionTokens } : {},
|
|
41165
|
+
...cachedTokens !== void 0 ? { prompt_cached_tokens: cachedTokens } : {},
|
|
41166
|
+
...cacheCreationTokens !== void 0 ? { prompt_cache_creation_tokens: cacheCreationTokens } : {}
|
|
41167
|
+
};
|
|
41168
|
+
}
|
|
41169
|
+
function nonNegativeNumber(value) {
|
|
41170
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
41171
|
+
}
|
|
41172
|
+
function finiteTimestamp(value) {
|
|
41173
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
41174
|
+
}
|
|
41175
|
+
function normalizeFinishReason3(value) {
|
|
41176
|
+
if (value === "content-filter") return "content_filter";
|
|
41177
|
+
if (value === "tool-calls") return "tool_calls";
|
|
41178
|
+
return value;
|
|
41179
|
+
}
|
|
41180
|
+
function safeJsonStringify(value) {
|
|
41181
|
+
try {
|
|
41182
|
+
return JSON.stringify(value) ?? "null";
|
|
41183
|
+
} catch {
|
|
41184
|
+
return "null";
|
|
41185
|
+
}
|
|
41186
|
+
}
|
|
41187
|
+
function turnKey3(sessionId, turnId) {
|
|
41188
|
+
return `${sessionId}:${turnId}`;
|
|
41189
|
+
}
|
|
41190
|
+
function turnIdempotencyKey(sessionId, turnId) {
|
|
41191
|
+
return `turn:${sessionId}:${turnId}`;
|
|
41192
|
+
}
|
|
41193
|
+
function actionIdempotencyKey(sessionId, turnId, callId) {
|
|
41194
|
+
return `action:${sessionId}:${turnId}:${callId}`;
|
|
41195
|
+
}
|
|
41196
|
+
|
|
41197
|
+
// src/instrumentation/plugins/eve-instrumentation.ts
|
|
41198
|
+
var EVE_INSTRUMENTATION_PROVIDER = /* @__PURE__ */ Symbol.for("eve.instrumentation.provider");
|
|
41199
|
+
function braintrustEveInstrumentation(options) {
|
|
41200
|
+
const definition = "defineState" in options ? createLegacyEveInstrumentation(options) : createEveInstrumentationProvider(options);
|
|
41201
|
+
const declaration = {
|
|
41202
|
+
...definition,
|
|
41203
|
+
[EVE_INSTRUMENTATION_PROVIDER]: true
|
|
41204
|
+
};
|
|
41205
|
+
return declaration;
|
|
41206
|
+
}
|
|
41207
|
+
|
|
40389
41208
|
// src/typed-instrumentation-helpers.ts
|
|
40390
41209
|
var TypedApplyProxy = Proxy;
|
|
40391
41210
|
|
|
@@ -40787,11 +41606,11 @@ function wrapClaudeAgentQuery(queryFn, defaultThis) {
|
|
|
40787
41606
|
}
|
|
40788
41607
|
};
|
|
40789
41608
|
const invocationTarget = thisArg === proxy || thisArg === void 0 ? defaultThis ?? thisArg : thisArg;
|
|
40790
|
-
return claudeAgentSDKChannels.query.
|
|
40791
|
-
|
|
40792
|
-
|
|
40793
|
-
|
|
40794
|
-
{
|
|
41609
|
+
return claudeAgentSDKChannels.query.invoke(
|
|
41610
|
+
target,
|
|
41611
|
+
invocationTarget,
|
|
41612
|
+
[wrappedParams],
|
|
41613
|
+
{}
|
|
40795
41614
|
);
|
|
40796
41615
|
}
|
|
40797
41616
|
});
|
|
@@ -41406,13 +42225,11 @@ function wrapAgentInstance(agent) {
|
|
|
41406
42225
|
if (prop === "stream" && typeof value === "function") {
|
|
41407
42226
|
return function(args, options) {
|
|
41408
42227
|
const callArgs = [args, options];
|
|
41409
|
-
return strandsAgentSDKChannels.agentStream.
|
|
41410
|
-
|
|
41411
|
-
|
|
41412
|
-
|
|
41413
|
-
|
|
41414
|
-
self: proxy
|
|
41415
|
-
}
|
|
42228
|
+
return strandsAgentSDKChannels.agentStream.invoke(
|
|
42229
|
+
value,
|
|
42230
|
+
target,
|
|
42231
|
+
callArgs,
|
|
42232
|
+
{ agent: proxy }
|
|
41416
42233
|
);
|
|
41417
42234
|
};
|
|
41418
42235
|
}
|
|
@@ -41443,12 +42260,12 @@ function wrapMultiAgentInstance(orchestrator, kind) {
|
|
|
41443
42260
|
return function(input, options) {
|
|
41444
42261
|
const callArgs = [input, options];
|
|
41445
42262
|
const channel2 = kind === "graph" ? strandsAgentSDKChannels.graphStream : strandsAgentSDKChannels.swarmStream;
|
|
41446
|
-
return channel2.
|
|
41447
|
-
|
|
42263
|
+
return channel2.invoke(
|
|
42264
|
+
value,
|
|
42265
|
+
target,
|
|
42266
|
+
callArgs,
|
|
41448
42267
|
{
|
|
41449
|
-
|
|
41450
|
-
orchestrator: proxy,
|
|
41451
|
-
self: proxy
|
|
42268
|
+
orchestrator: proxy
|
|
41452
42269
|
}
|
|
41453
42270
|
);
|
|
41454
42271
|
};
|
|
@@ -43400,9 +44217,6 @@ var VitestContextManager = class {
|
|
|
43400
44217
|
getCurrentContext() {
|
|
43401
44218
|
return this.contextStorage.getStore();
|
|
43402
44219
|
}
|
|
43403
|
-
setContext(context) {
|
|
43404
|
-
this.contextStorage.enterWith(context);
|
|
43405
|
-
}
|
|
43406
44220
|
runInContext(context, callback) {
|
|
43407
44221
|
return this.contextStorage.run(context, callback);
|
|
43408
44222
|
}
|
|
@@ -43788,8 +44602,7 @@ function wrapDescribe(originalDescribe, config, afterAll) {
|
|
|
43788
44602
|
if (config.onProgress) {
|
|
43789
44603
|
config.onProgress({ type: "suite_start", suiteName });
|
|
43790
44604
|
}
|
|
43791
|
-
contextManager.
|
|
43792
|
-
factory();
|
|
44605
|
+
contextManager.runInContext(lazyContext, factory);
|
|
43793
44606
|
if (afterAll) {
|
|
43794
44607
|
afterAll(async () => {
|
|
43795
44608
|
await flushExperimentWithSync(context, config);
|
|
@@ -44368,7 +45181,7 @@ function isAsync(fn) {
|
|
|
44368
45181
|
function isAsyncGenerator2(fn) {
|
|
44369
45182
|
return fn[Symbol.toStringTag] === "AsyncGenerator";
|
|
44370
45183
|
}
|
|
44371
|
-
function
|
|
45184
|
+
function isAsyncIterable5(obj) {
|
|
44372
45185
|
return typeof obj[Symbol.asyncIterator] === "function";
|
|
44373
45186
|
}
|
|
44374
45187
|
function wrapAsync(asyncFn) {
|
|
@@ -44540,7 +45353,7 @@ var eachOfLimit$2 = (limit) => {
|
|
|
44540
45353
|
if (isAsyncGenerator2(obj)) {
|
|
44541
45354
|
return asyncEachOfLimit(obj, limit, iteratee, callback);
|
|
44542
45355
|
}
|
|
44543
|
-
if (
|
|
45356
|
+
if (isAsyncIterable5(obj)) {
|
|
44544
45357
|
return asyncEachOfLimit(obj[Symbol.asyncIterator](), limit, iteratee, callback);
|
|
44545
45358
|
}
|
|
44546
45359
|
var nextElem = createIterator(obj);
|
|
@@ -45895,7 +46708,7 @@ function callEvaluatorData(data) {
|
|
|
45895
46708
|
baseExperiment
|
|
45896
46709
|
};
|
|
45897
46710
|
}
|
|
45898
|
-
function
|
|
46711
|
+
function isAsyncIterable6(value) {
|
|
45899
46712
|
return typeof value === "object" && value !== null && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
45900
46713
|
}
|
|
45901
46714
|
function isIterable(value) {
|
|
@@ -45930,7 +46743,7 @@ async function _internalResolveEvaluatorData(evaluator, experiment) {
|
|
|
45930
46743
|
}).asDataset();
|
|
45931
46744
|
}
|
|
45932
46745
|
const resolvedDataResult = dataResult instanceof Promise ? await dataResult : dataResult;
|
|
45933
|
-
if (
|
|
46746
|
+
if (isAsyncIterable6(resolvedDataResult)) {
|
|
45934
46747
|
return resolvedDataResult;
|
|
45935
46748
|
}
|
|
45936
46749
|
if (Array.isArray(resolvedDataResult) || isIterable(resolvedDataResult)) {
|