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/browser.mjs
CHANGED
|
@@ -659,8 +659,6 @@ function newGlobalTracingChannel(nameOrChannels) {
|
|
|
659
659
|
var DefaultAsyncLocalStorage = class {
|
|
660
660
|
constructor() {
|
|
661
661
|
}
|
|
662
|
-
enterWith(_) {
|
|
663
|
-
}
|
|
664
662
|
run(_, callback) {
|
|
665
663
|
return callback();
|
|
666
664
|
}
|
|
@@ -2477,7 +2475,9 @@ var AclObjectType = z6.union([
|
|
|
2477
2475
|
"project_log",
|
|
2478
2476
|
"org_project",
|
|
2479
2477
|
"org_audit_logs",
|
|
2480
|
-
"project_group"
|
|
2478
|
+
"project_group",
|
|
2479
|
+
"ai_secret",
|
|
2480
|
+
"org_ai_secret"
|
|
2481
2481
|
]),
|
|
2482
2482
|
z6.null()
|
|
2483
2483
|
]);
|
|
@@ -3310,7 +3310,7 @@ var PromptParserNullish = z6.union([
|
|
|
3310
3310
|
}),
|
|
3311
3311
|
z6.null()
|
|
3312
3312
|
]);
|
|
3313
|
-
var
|
|
3313
|
+
var PreprocessorId = z6.union([
|
|
3314
3314
|
z6.object({
|
|
3315
3315
|
type: z6.literal("function"),
|
|
3316
3316
|
id: z6.string(),
|
|
@@ -3321,6 +3321,7 @@ var PreprocessorSavedFunctionId = z6.union([
|
|
|
3321
3321
|
name: z6.string(),
|
|
3322
3322
|
function_type: z6.literal("preprocessor").optional().default("preprocessor")
|
|
3323
3323
|
}),
|
|
3324
|
+
z6.object({ type: z6.literal("inline"), code: z6.string().min(1) }),
|
|
3324
3325
|
z6.null()
|
|
3325
3326
|
]);
|
|
3326
3327
|
var PromptDataNullish = z6.union([
|
|
@@ -3328,7 +3329,7 @@ var PromptDataNullish = z6.union([
|
|
|
3328
3329
|
prompt: PromptBlockDataNullish,
|
|
3329
3330
|
options: PromptOptionsNullish,
|
|
3330
3331
|
parser: PromptParserNullish,
|
|
3331
|
-
preprocessor:
|
|
3332
|
+
preprocessor: PreprocessorId,
|
|
3332
3333
|
tool_functions: z6.union([z6.array(SavedFunctionId), z6.null()]),
|
|
3333
3334
|
template_format: z6.union([
|
|
3334
3335
|
z6.enum(["mustache", "nunjucks", "none"]),
|
|
@@ -3530,7 +3531,7 @@ var PromptData = z6.object({
|
|
|
3530
3531
|
prompt: PromptBlockDataNullish,
|
|
3531
3532
|
options: PromptOptionsNullish,
|
|
3532
3533
|
parser: PromptParserNullish,
|
|
3533
|
-
preprocessor:
|
|
3534
|
+
preprocessor: PreprocessorId,
|
|
3534
3535
|
tool_functions: z6.union([z6.array(SavedFunctionId), z6.null()]),
|
|
3535
3536
|
template_format: z6.union([
|
|
3536
3537
|
z6.enum(["mustache", "nunjucks", "none"]),
|
|
@@ -4474,6 +4475,7 @@ var View = z6.object({
|
|
|
4474
4475
|
]),
|
|
4475
4476
|
name: z6.string(),
|
|
4476
4477
|
description: z6.union([z6.string(), z6.null()]).optional(),
|
|
4478
|
+
starred: z6.boolean().optional(),
|
|
4477
4479
|
created: z6.union([z6.string(), z6.null()]).optional(),
|
|
4478
4480
|
updated_at: z6.union([z6.string(), z6.null()]).optional(),
|
|
4479
4481
|
view_data: ViewData.optional(),
|
|
@@ -5177,44 +5179,72 @@ function createCacheLayers({
|
|
|
5177
5179
|
}
|
|
5178
5180
|
|
|
5179
5181
|
// src/prompt-cache/prompt-cache.ts
|
|
5180
|
-
function createCacheKey(key) {
|
|
5182
|
+
function createCacheKey(key, namespace) {
|
|
5183
|
+
let cacheKey;
|
|
5181
5184
|
if (key.id) {
|
|
5182
|
-
|
|
5183
|
-
}
|
|
5184
|
-
|
|
5185
|
-
|
|
5186
|
-
|
|
5187
|
-
|
|
5188
|
-
|
|
5189
|
-
|
|
5185
|
+
cacheKey = `id:${key.id}`;
|
|
5186
|
+
} else {
|
|
5187
|
+
const prefix = key.projectId ?? key.projectName;
|
|
5188
|
+
if (!prefix) {
|
|
5189
|
+
throw new Error("Either projectId or projectName must be provided");
|
|
5190
|
+
}
|
|
5191
|
+
if (!key.slug) {
|
|
5192
|
+
throw new Error("Slug must be provided when not using ID");
|
|
5193
|
+
}
|
|
5194
|
+
cacheKey = `${prefix}:${key.slug}:${key.version ?? "latest"}`;
|
|
5190
5195
|
}
|
|
5191
|
-
return `${
|
|
5196
|
+
return namespace === void 0 ? cacheKey : `${namespace.length}:${namespace}:${cacheKey}`;
|
|
5192
5197
|
}
|
|
5193
|
-
var PromptCache = class {
|
|
5198
|
+
var PromptCache = class _PromptCache {
|
|
5194
5199
|
memoryCache;
|
|
5195
5200
|
diskCache;
|
|
5201
|
+
namespace;
|
|
5202
|
+
expectedResolvedOrgIdentity;
|
|
5196
5203
|
constructor(options) {
|
|
5197
5204
|
this.memoryCache = options.memoryCache;
|
|
5198
5205
|
this.diskCache = options.diskCache;
|
|
5206
|
+
this.namespace = options.namespace;
|
|
5207
|
+
this.expectedResolvedOrgIdentity = options.expectedResolvedOrgIdentity;
|
|
5208
|
+
}
|
|
5209
|
+
/**
|
|
5210
|
+
* Returns a cache view that shares the same storage layers but isolates all
|
|
5211
|
+
* entries under the provided namespace.
|
|
5212
|
+
*/
|
|
5213
|
+
withNamespace(namespace, expectedResolvedOrgIdentity) {
|
|
5214
|
+
return new _PromptCache({
|
|
5215
|
+
memoryCache: this.memoryCache,
|
|
5216
|
+
diskCache: this.diskCache,
|
|
5217
|
+
namespace,
|
|
5218
|
+
expectedResolvedOrgIdentity
|
|
5219
|
+
});
|
|
5199
5220
|
}
|
|
5200
5221
|
/**
|
|
5201
5222
|
* Retrieves a prompt from the cache.
|
|
5202
5223
|
* First checks the in-memory LRU cache, then falls back to checking the disk cache if available.
|
|
5203
5224
|
*/
|
|
5204
5225
|
async get(key) {
|
|
5205
|
-
const cacheKey = createCacheKey(key);
|
|
5226
|
+
const cacheKey = createCacheKey(key, this.namespace);
|
|
5206
5227
|
if (this.memoryCache) {
|
|
5207
|
-
const
|
|
5208
|
-
if (
|
|
5209
|
-
return
|
|
5228
|
+
const memoryEntry = this.memoryCache.get(cacheKey);
|
|
5229
|
+
if (memoryEntry !== void 0 && (this.expectedResolvedOrgIdentity === void 0 || memoryEntry.resolvedOrgIdentity === this.expectedResolvedOrgIdentity)) {
|
|
5230
|
+
return memoryEntry.value;
|
|
5210
5231
|
}
|
|
5211
5232
|
}
|
|
5212
5233
|
if (this.diskCache) {
|
|
5213
|
-
const
|
|
5214
|
-
if (!
|
|
5234
|
+
const diskEntry = await this.diskCache.get(cacheKey);
|
|
5235
|
+
if (!diskEntry || this.expectedResolvedOrgIdentity !== void 0 && diskEntry.resolvedOrgIdentity !== this.expectedResolvedOrgIdentity) {
|
|
5215
5236
|
return void 0;
|
|
5216
5237
|
}
|
|
5217
|
-
|
|
5238
|
+
const serializedPrompt = diskEntry.value;
|
|
5239
|
+
const diskPrompt = new Prompt2(
|
|
5240
|
+
serializedPrompt.metadata,
|
|
5241
|
+
serializedPrompt.defaults,
|
|
5242
|
+
serializedPrompt.noTrace
|
|
5243
|
+
);
|
|
5244
|
+
this.memoryCache?.set(cacheKey, {
|
|
5245
|
+
value: diskPrompt,
|
|
5246
|
+
resolvedOrgIdentity: diskEntry.resolvedOrgIdentity
|
|
5247
|
+
});
|
|
5218
5248
|
return diskPrompt;
|
|
5219
5249
|
}
|
|
5220
5250
|
return void 0;
|
|
@@ -5228,58 +5258,91 @@ var PromptCache = class {
|
|
|
5228
5258
|
* @throws If there is an error writing to the disk cache.
|
|
5229
5259
|
*/
|
|
5230
5260
|
async set(key, value) {
|
|
5231
|
-
const cacheKey = createCacheKey(key);
|
|
5232
|
-
|
|
5261
|
+
const cacheKey = createCacheKey(key, this.namespace);
|
|
5262
|
+
const memoryEntry = {
|
|
5263
|
+
value,
|
|
5264
|
+
resolvedOrgIdentity: this.expectedResolvedOrgIdentity
|
|
5265
|
+
};
|
|
5266
|
+
this.memoryCache?.set(cacheKey, memoryEntry);
|
|
5233
5267
|
if (this.diskCache) {
|
|
5234
|
-
await this.diskCache.set(cacheKey,
|
|
5268
|
+
await this.diskCache.set(cacheKey, {
|
|
5269
|
+
value: value._internalSerializeForCache(),
|
|
5270
|
+
resolvedOrgIdentity: this.expectedResolvedOrgIdentity
|
|
5271
|
+
});
|
|
5235
5272
|
}
|
|
5236
5273
|
}
|
|
5237
5274
|
};
|
|
5238
5275
|
|
|
5239
5276
|
// src/prompt-cache/parameters-cache.ts
|
|
5240
|
-
function createCacheKey2(key) {
|
|
5277
|
+
function createCacheKey2(key, namespace) {
|
|
5278
|
+
let cacheKey;
|
|
5241
5279
|
if (key.id) {
|
|
5242
|
-
|
|
5243
|
-
}
|
|
5244
|
-
|
|
5245
|
-
|
|
5246
|
-
|
|
5247
|
-
|
|
5248
|
-
|
|
5249
|
-
|
|
5280
|
+
cacheKey = `parameters:id:${key.id}`;
|
|
5281
|
+
} else {
|
|
5282
|
+
const prefix = key.projectId ?? key.projectName;
|
|
5283
|
+
if (!prefix) {
|
|
5284
|
+
throw new Error("Either projectId or projectName must be provided");
|
|
5285
|
+
}
|
|
5286
|
+
if (!key.slug) {
|
|
5287
|
+
throw new Error("Slug must be provided when not using ID");
|
|
5288
|
+
}
|
|
5289
|
+
cacheKey = `parameters:${prefix}:${key.slug}:${key.version ?? "latest"}`;
|
|
5250
5290
|
}
|
|
5251
|
-
return
|
|
5291
|
+
return namespace === void 0 ? cacheKey : `${namespace.length}:${namespace}:${cacheKey}`;
|
|
5252
5292
|
}
|
|
5253
|
-
var ParametersCache = class {
|
|
5293
|
+
var ParametersCache = class _ParametersCache {
|
|
5254
5294
|
memoryCache;
|
|
5255
5295
|
diskCache;
|
|
5296
|
+
namespace;
|
|
5297
|
+
expectedResolvedOrgIdentity;
|
|
5256
5298
|
constructor(options) {
|
|
5257
5299
|
this.memoryCache = options.memoryCache;
|
|
5258
5300
|
this.diskCache = options.diskCache;
|
|
5301
|
+
this.namespace = options.namespace;
|
|
5302
|
+
this.expectedResolvedOrgIdentity = options.expectedResolvedOrgIdentity;
|
|
5303
|
+
}
|
|
5304
|
+
withNamespace(namespace, expectedResolvedOrgIdentity) {
|
|
5305
|
+
return new _ParametersCache({
|
|
5306
|
+
memoryCache: this.memoryCache,
|
|
5307
|
+
diskCache: this.diskCache,
|
|
5308
|
+
namespace,
|
|
5309
|
+
expectedResolvedOrgIdentity
|
|
5310
|
+
});
|
|
5259
5311
|
}
|
|
5260
5312
|
async get(key) {
|
|
5261
|
-
const cacheKey = createCacheKey2(key);
|
|
5313
|
+
const cacheKey = createCacheKey2(key, this.namespace);
|
|
5262
5314
|
if (this.memoryCache) {
|
|
5263
|
-
const
|
|
5264
|
-
if (
|
|
5265
|
-
return
|
|
5315
|
+
const memoryEntry = this.memoryCache.get(cacheKey);
|
|
5316
|
+
if (memoryEntry !== void 0 && (this.expectedResolvedOrgIdentity === void 0 || memoryEntry.resolvedOrgIdentity === this.expectedResolvedOrgIdentity)) {
|
|
5317
|
+
return memoryEntry.value;
|
|
5266
5318
|
}
|
|
5267
5319
|
}
|
|
5268
5320
|
if (this.diskCache) {
|
|
5269
|
-
const
|
|
5270
|
-
if (!
|
|
5321
|
+
const diskEntry = await this.diskCache.get(cacheKey);
|
|
5322
|
+
if (!diskEntry || this.expectedResolvedOrgIdentity !== void 0 && diskEntry.resolvedOrgIdentity !== this.expectedResolvedOrgIdentity) {
|
|
5271
5323
|
return void 0;
|
|
5272
5324
|
}
|
|
5273
|
-
|
|
5274
|
-
|
|
5325
|
+
const diskParameters = new RemoteEvalParameters(diskEntry.value.metadata);
|
|
5326
|
+
this.memoryCache?.set(cacheKey, {
|
|
5327
|
+
value: diskParameters,
|
|
5328
|
+
resolvedOrgIdentity: diskEntry.resolvedOrgIdentity
|
|
5329
|
+
});
|
|
5330
|
+
return diskParameters;
|
|
5275
5331
|
}
|
|
5276
5332
|
return void 0;
|
|
5277
5333
|
}
|
|
5278
5334
|
async set(key, value) {
|
|
5279
|
-
const cacheKey = createCacheKey2(key);
|
|
5280
|
-
|
|
5335
|
+
const cacheKey = createCacheKey2(key, this.namespace);
|
|
5336
|
+
const memoryEntry = {
|
|
5337
|
+
value,
|
|
5338
|
+
resolvedOrgIdentity: this.expectedResolvedOrgIdentity
|
|
5339
|
+
};
|
|
5340
|
+
this.memoryCache?.set(cacheKey, memoryEntry);
|
|
5281
5341
|
if (this.diskCache) {
|
|
5282
|
-
await this.diskCache.set(cacheKey,
|
|
5342
|
+
await this.diskCache.set(cacheKey, {
|
|
5343
|
+
value: value._internalSerializeForCache(),
|
|
5344
|
+
resolvedOrgIdentity: this.expectedResolvedOrgIdentity
|
|
5345
|
+
});
|
|
5283
5346
|
}
|
|
5284
5347
|
}
|
|
5285
5348
|
};
|
|
@@ -5583,6 +5646,7 @@ var INSTRUMENTATION_NAMES = {
|
|
|
5583
5646
|
CLOUDFLARE_THINK: "cloudflare-think",
|
|
5584
5647
|
COHERE: "cohere",
|
|
5585
5648
|
CURSOR_SDK: "cursor-sdk",
|
|
5649
|
+
DEEPSEEK_HARNESS: "deepseek-harness",
|
|
5586
5650
|
EVE: "eve",
|
|
5587
5651
|
FLUE: "flue",
|
|
5588
5652
|
GENKIT: "genkit",
|
|
@@ -5608,7 +5672,7 @@ var INSTRUMENTATION_NAMES = {
|
|
|
5608
5672
|
var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
|
|
5609
5673
|
"braintrust.spanInstrumentationName"
|
|
5610
5674
|
);
|
|
5611
|
-
var SDK_VERSION = true ? "3.
|
|
5675
|
+
var SDK_VERSION = true ? "3.29.0" : "0.0.0";
|
|
5612
5676
|
function withSpanInstrumentationName(args, instrumentationName) {
|
|
5613
5677
|
return {
|
|
5614
5678
|
...args,
|
|
@@ -5727,6 +5791,16 @@ var datasetSnapshotRegisterResponseSchema = z8.object({
|
|
|
5727
5791
|
dataset_snapshot: DatasetSnapshot,
|
|
5728
5792
|
found_existing: z8.boolean().optional()
|
|
5729
5793
|
});
|
|
5794
|
+
var datasetObjectInfoSchema = z8.object({
|
|
5795
|
+
object_id: z8.string(),
|
|
5796
|
+
object_name: z8.string(),
|
|
5797
|
+
parent_cols: z8.object({
|
|
5798
|
+
project: z8.object({
|
|
5799
|
+
id: z8.string(),
|
|
5800
|
+
name: z8.string()
|
|
5801
|
+
})
|
|
5802
|
+
})
|
|
5803
|
+
});
|
|
5730
5804
|
var datasetRestorePreviewResultSchema = z8.object({
|
|
5731
5805
|
rows_to_restore: z8.number(),
|
|
5732
5806
|
rows_to_delete: z8.number()
|
|
@@ -5940,12 +6014,53 @@ var loginSchema = z8.strictObject({
|
|
|
5940
6014
|
});
|
|
5941
6015
|
var stateNonce = 0;
|
|
5942
6016
|
var V1_PROXY_SUFFIX = "/v1/proxy";
|
|
6017
|
+
var LOADER_LOGIN_CACHE_MAX = 16;
|
|
5943
6018
|
function normalizeProxyConnUrl(proxyUrl) {
|
|
5944
6019
|
return proxyUrl.endsWith(V1_PROXY_SUFFIX) ? proxyUrl.slice(0, proxyUrl.length - V1_PROXY_SUFFIX.length) : proxyUrl;
|
|
5945
6020
|
}
|
|
5946
6021
|
var BraintrustState = class _BraintrustState {
|
|
6022
|
+
id;
|
|
6023
|
+
currentExperiment;
|
|
6024
|
+
// Note: the value of IsAsyncFlush doesn't really matter here, since we
|
|
6025
|
+
// (safely) dynamically cast it whenever retrieving the logger.
|
|
6026
|
+
currentLogger;
|
|
6027
|
+
currentParent;
|
|
6028
|
+
currentSpan;
|
|
6029
|
+
// Any time we re-log in, we directly update the apiConn inside the logger.
|
|
6030
|
+
// This is preferable to replacing the whole logger, which would create the
|
|
6031
|
+
// possibility of multiple loggers floating around, which may not log in a
|
|
6032
|
+
// deterministic order.
|
|
6033
|
+
_bgLogger;
|
|
6034
|
+
_overrideBgLogger = null;
|
|
6035
|
+
appUrl = null;
|
|
6036
|
+
appPublicUrl = null;
|
|
6037
|
+
loginToken = null;
|
|
6038
|
+
orgId = null;
|
|
6039
|
+
orgName = null;
|
|
6040
|
+
apiUrl = null;
|
|
6041
|
+
proxyUrl = null;
|
|
6042
|
+
loggedIn = false;
|
|
6043
|
+
gitMetadataSettings;
|
|
6044
|
+
debugLogLevel;
|
|
6045
|
+
debugLogLevelConfigured = false;
|
|
6046
|
+
fetch = globalThis.fetch;
|
|
6047
|
+
_appConn = null;
|
|
6048
|
+
_apiConn = null;
|
|
6049
|
+
_proxyConn = null;
|
|
6050
|
+
promptCache;
|
|
6051
|
+
parametersCache;
|
|
6052
|
+
spanCache;
|
|
6053
|
+
_idGenerator = null;
|
|
6054
|
+
_contextManager = null;
|
|
6055
|
+
_otelFlushCallback = null;
|
|
6056
|
+
spanOriginEnvironment;
|
|
6057
|
+
traceContextSigningSecret;
|
|
6058
|
+
loaderLoginCache = /* @__PURE__ */ new WeakMap();
|
|
6059
|
+
loginParams;
|
|
6060
|
+
activeLoginOrgNameSelector;
|
|
5947
6061
|
constructor(loginParams) {
|
|
5948
|
-
this.loginParams = loginParams;
|
|
6062
|
+
this.loginParams = { ...loginParams };
|
|
6063
|
+
this.activeLoginOrgNameSelector = loginParams.orgName ?? isomorph_default.getEnv("BRAINTRUST_ORG_NAME");
|
|
5949
6064
|
this.id = `${(/* @__PURE__ */ new Date()).toLocaleString()}-${stateNonce++}`;
|
|
5950
6065
|
this.currentExperiment = void 0;
|
|
5951
6066
|
this.currentLogger = void 0;
|
|
@@ -5982,12 +6097,14 @@ var BraintrustState = class _BraintrustState {
|
|
|
5982
6097
|
const {
|
|
5983
6098
|
memoryCache: parametersMemoryCache,
|
|
5984
6099
|
diskCache: parametersDiskCache
|
|
5985
|
-
} = createCacheLayers(
|
|
5986
|
-
|
|
5987
|
-
|
|
5988
|
-
|
|
5989
|
-
|
|
5990
|
-
|
|
6100
|
+
} = createCacheLayers(
|
|
6101
|
+
{
|
|
6102
|
+
memoryMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_MEMORY_MAX",
|
|
6103
|
+
diskCacheDirEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DIR",
|
|
6104
|
+
diskMaxEnvVar: "BRAINTRUST_PARAMETERS_CACHE_DISK_MAX",
|
|
6105
|
+
getDefaultDiskCacheDir: () => `${isomorph_default.getEnv("HOME") ?? isomorph_default.homedir()}/.braintrust/parameters_cache`
|
|
6106
|
+
}
|
|
6107
|
+
);
|
|
5991
6108
|
this.parametersCache = new ParametersCache({
|
|
5992
6109
|
memoryCache: parametersMemoryCache,
|
|
5993
6110
|
diskCache: parametersDiskCache
|
|
@@ -5996,43 +6113,6 @@ var BraintrustState = class _BraintrustState {
|
|
|
5996
6113
|
this.spanOriginEnvironment = detectSpanOriginEnvironment();
|
|
5997
6114
|
this._internalSetTraceContextSigningSecret(loginParams.apiKey);
|
|
5998
6115
|
}
|
|
5999
|
-
loginParams;
|
|
6000
|
-
id;
|
|
6001
|
-
currentExperiment;
|
|
6002
|
-
// Note: the value of IsAsyncFlush doesn't really matter here, since we
|
|
6003
|
-
// (safely) dynamically cast it whenever retrieving the logger.
|
|
6004
|
-
currentLogger;
|
|
6005
|
-
currentParent;
|
|
6006
|
-
currentSpan;
|
|
6007
|
-
// Any time we re-log in, we directly update the apiConn inside the logger.
|
|
6008
|
-
// This is preferable to replacing the whole logger, which would create the
|
|
6009
|
-
// possibility of multiple loggers floating around, which may not log in a
|
|
6010
|
-
// deterministic order.
|
|
6011
|
-
_bgLogger;
|
|
6012
|
-
_overrideBgLogger = null;
|
|
6013
|
-
appUrl = null;
|
|
6014
|
-
appPublicUrl = null;
|
|
6015
|
-
loginToken = null;
|
|
6016
|
-
orgId = null;
|
|
6017
|
-
orgName = null;
|
|
6018
|
-
apiUrl = null;
|
|
6019
|
-
proxyUrl = null;
|
|
6020
|
-
loggedIn = false;
|
|
6021
|
-
gitMetadataSettings;
|
|
6022
|
-
debugLogLevel;
|
|
6023
|
-
debugLogLevelConfigured = false;
|
|
6024
|
-
fetch = globalThis.fetch;
|
|
6025
|
-
_appConn = null;
|
|
6026
|
-
_apiConn = null;
|
|
6027
|
-
_proxyConn = null;
|
|
6028
|
-
promptCache;
|
|
6029
|
-
parametersCache;
|
|
6030
|
-
spanCache;
|
|
6031
|
-
_idGenerator = null;
|
|
6032
|
-
_contextManager = null;
|
|
6033
|
-
_otelFlushCallback = null;
|
|
6034
|
-
spanOriginEnvironment;
|
|
6035
|
-
traceContextSigningSecret;
|
|
6036
6116
|
/** @internal */
|
|
6037
6117
|
_internalSetTraceContextSigningSecret(secret) {
|
|
6038
6118
|
const normalizedSecret = secret?.trim();
|
|
@@ -6057,6 +6137,101 @@ var BraintrustState = class _BraintrustState {
|
|
|
6057
6137
|
this._appConn = null;
|
|
6058
6138
|
this._apiConn = null;
|
|
6059
6139
|
this._proxyConn = null;
|
|
6140
|
+
this.loaderLoginCache = /* @__PURE__ */ new WeakMap();
|
|
6141
|
+
}
|
|
6142
|
+
/** @internal */
|
|
6143
|
+
async _internalResolveLoaderLoginOptions({
|
|
6144
|
+
apiKey,
|
|
6145
|
+
appUrl,
|
|
6146
|
+
orgName,
|
|
6147
|
+
fetch: fetch2,
|
|
6148
|
+
forceLogin
|
|
6149
|
+
}) {
|
|
6150
|
+
const resolvedAppUrl = appUrl ?? (this.loggedIn ? this.appUrl ?? void 0 : void 0) ?? this.loginParams.appUrl ?? isomorph_default.getEnv("BRAINTRUST_APP_URL") ?? "https://www.braintrust.dev";
|
|
6151
|
+
const resolvedApiKey = apiKey ?? (this.loggedIn ? this.loginToken ?? void 0 : void 0) ?? this.loginParams.apiKey ?? await isomorph_default.getBraintrustApiKey();
|
|
6152
|
+
if (!resolvedApiKey) {
|
|
6153
|
+
throw new Error(
|
|
6154
|
+
"Please specify an api key (e.g. by setting BRAINTRUST_API_KEY)."
|
|
6155
|
+
);
|
|
6156
|
+
}
|
|
6157
|
+
const normalizedApiKey = HTTPConnection.sanitize_token(resolvedApiKey);
|
|
6158
|
+
const usesActiveCredential = this.loggedIn && normalizedApiKey === this.loginToken;
|
|
6159
|
+
const requestedOrgName = orgName ?? (usesActiveCredential ? this.activeLoginOrgNameSelector : void 0) ?? this.loginParams.orgName ?? isomorph_default.getEnv("BRAINTRUST_ORG_NAME");
|
|
6160
|
+
const resolvedOrgName = orgName ?? (usesActiveCredential ? this.orgName ?? void 0 : void 0) ?? requestedOrgName;
|
|
6161
|
+
const resolvedFetch = fetch2 ?? (this.loggedIn ? this.fetch : void 0) ?? this.loginParams.fetch ?? globalThis.fetch;
|
|
6162
|
+
const credentialCacheNamespace = JSON.stringify([
|
|
6163
|
+
"loader-credential",
|
|
6164
|
+
resolvedAppUrl,
|
|
6165
|
+
requestedOrgName,
|
|
6166
|
+
normalizedApiKey
|
|
6167
|
+
]);
|
|
6168
|
+
return {
|
|
6169
|
+
apiKey: normalizedApiKey,
|
|
6170
|
+
appUrl: resolvedAppUrl,
|
|
6171
|
+
orgName: resolvedOrgName,
|
|
6172
|
+
fetch: resolvedFetch,
|
|
6173
|
+
forceLogin,
|
|
6174
|
+
credentialCacheNamespace,
|
|
6175
|
+
existingState: !forceLogin && usesActiveCredential && resolvedAppUrl === this.appUrl && resolvedOrgName === this.orgName && resolvedFetch === this.fetch ? this : void 0
|
|
6176
|
+
};
|
|
6177
|
+
}
|
|
6178
|
+
/** @internal */
|
|
6179
|
+
_internalGetLoaderCacheViews(loginOptions, requestState) {
|
|
6180
|
+
const expectedResolvedOrgIdentity = requestState?.orgId && requestState.appUrl ? JSON.stringify([
|
|
6181
|
+
"loader-org",
|
|
6182
|
+
requestState.appUrl,
|
|
6183
|
+
requestState.orgId
|
|
6184
|
+
]) : void 0;
|
|
6185
|
+
return {
|
|
6186
|
+
promptCache: this.promptCache.withNamespace(
|
|
6187
|
+
loginOptions.credentialCacheNamespace,
|
|
6188
|
+
expectedResolvedOrgIdentity
|
|
6189
|
+
),
|
|
6190
|
+
parametersCache: this.parametersCache.withNamespace(
|
|
6191
|
+
loginOptions.credentialCacheNamespace,
|
|
6192
|
+
expectedResolvedOrgIdentity
|
|
6193
|
+
)
|
|
6194
|
+
};
|
|
6195
|
+
}
|
|
6196
|
+
/** @internal */
|
|
6197
|
+
async _internalGetLoaderState({
|
|
6198
|
+
apiKey,
|
|
6199
|
+
appUrl,
|
|
6200
|
+
orgName,
|
|
6201
|
+
fetch: fetch2,
|
|
6202
|
+
forceLogin,
|
|
6203
|
+
existingState
|
|
6204
|
+
}) {
|
|
6205
|
+
if (existingState) {
|
|
6206
|
+
return existingState;
|
|
6207
|
+
}
|
|
6208
|
+
let cache = this.loaderLoginCache.get(fetch2);
|
|
6209
|
+
if (!cache) {
|
|
6210
|
+
cache = new LRUCache({ max: LOADER_LOGIN_CACHE_MAX });
|
|
6211
|
+
this.loaderLoginCache.set(fetch2, cache);
|
|
6212
|
+
}
|
|
6213
|
+
const cacheKey = JSON.stringify([appUrl, orgName, apiKey]);
|
|
6214
|
+
if (!forceLogin) {
|
|
6215
|
+
const cachedState = cache.get(cacheKey);
|
|
6216
|
+
if (cachedState) {
|
|
6217
|
+
return cachedState;
|
|
6218
|
+
}
|
|
6219
|
+
}
|
|
6220
|
+
const statePromise = loginToLoaderRequestState({
|
|
6221
|
+
orgName,
|
|
6222
|
+
apiKey,
|
|
6223
|
+
appUrl,
|
|
6224
|
+
fetch: fetch2
|
|
6225
|
+
});
|
|
6226
|
+
cache.set(cacheKey, statePromise);
|
|
6227
|
+
try {
|
|
6228
|
+
return await statePromise;
|
|
6229
|
+
} catch (error) {
|
|
6230
|
+
if (cache.get(cacheKey) === statePromise) {
|
|
6231
|
+
cache.delete(cacheKey);
|
|
6232
|
+
}
|
|
6233
|
+
throw error;
|
|
6234
|
+
}
|
|
6060
6235
|
}
|
|
6061
6236
|
resetIdGenState() {
|
|
6062
6237
|
this._idGenerator = null;
|
|
@@ -6105,6 +6280,8 @@ var BraintrustState = class _BraintrustState {
|
|
|
6105
6280
|
this.debugLogLevel = other.debugLogLevel;
|
|
6106
6281
|
this.debugLogLevelConfigured = other.debugLogLevelConfigured;
|
|
6107
6282
|
this.traceContextSigningSecret = other.traceContextSigningSecret;
|
|
6283
|
+
this.fetch = other.fetch;
|
|
6284
|
+
this.activeLoginOrgNameSelector = other.activeLoginOrgNameSelector;
|
|
6108
6285
|
setGlobalDebugLogLevel(
|
|
6109
6286
|
this.debugLogLevelConfigured ? this.debugLogLevel ?? false : void 0
|
|
6110
6287
|
);
|
|
@@ -6344,36 +6521,85 @@ var FailedHTTPResponse = class extends Error {
|
|
|
6344
6521
|
status;
|
|
6345
6522
|
text;
|
|
6346
6523
|
data;
|
|
6347
|
-
|
|
6524
|
+
cause;
|
|
6525
|
+
constructor(status, text, data, cause) {
|
|
6348
6526
|
super(`${status}: ${text} (${data})`);
|
|
6349
6527
|
this.status = status;
|
|
6350
6528
|
this.text = text;
|
|
6351
6529
|
this.data = data;
|
|
6530
|
+
this.cause = cause;
|
|
6531
|
+
}
|
|
6532
|
+
};
|
|
6533
|
+
var HTTPTransportError = class extends Error {
|
|
6534
|
+
cause;
|
|
6535
|
+
constructor(cause) {
|
|
6536
|
+
super(cause instanceof Error ? cause.message : String(cause));
|
|
6537
|
+
this.name = "HTTPTransportError";
|
|
6538
|
+
this.cause = cause;
|
|
6352
6539
|
}
|
|
6353
6540
|
};
|
|
6541
|
+
var httpTransportErrorCauses = /* @__PURE__ */ new WeakSet();
|
|
6542
|
+
function recordHTTPTransportError(error) {
|
|
6543
|
+
if (typeof error === "object" && error !== null || typeof error === "function") {
|
|
6544
|
+
httpTransportErrorCauses.add(error);
|
|
6545
|
+
}
|
|
6546
|
+
}
|
|
6547
|
+
function rethrowHTTPTransportError(error, classifyTransportErrors) {
|
|
6548
|
+
if (classifyTransportErrors) {
|
|
6549
|
+
throw new HTTPTransportError(error);
|
|
6550
|
+
}
|
|
6551
|
+
recordHTTPTransportError(error);
|
|
6552
|
+
throw error;
|
|
6553
|
+
}
|
|
6554
|
+
function isLoaderCacheFallbackError(error) {
|
|
6555
|
+
if (error instanceof FailedHTTPResponse) {
|
|
6556
|
+
return error.status === 408 || error.status === 429 || error.status >= 500;
|
|
6557
|
+
}
|
|
6558
|
+
if (error instanceof HTTPTransportError) {
|
|
6559
|
+
return true;
|
|
6560
|
+
}
|
|
6561
|
+
return (typeof error === "object" && error !== null || typeof error === "function") && httpTransportErrorCauses.has(error);
|
|
6562
|
+
}
|
|
6563
|
+
async function readJSONResponse(response, classifyTransportErrors = false) {
|
|
6564
|
+
let data;
|
|
6565
|
+
try {
|
|
6566
|
+
data = await response.text();
|
|
6567
|
+
} catch (error) {
|
|
6568
|
+
rethrowHTTPTransportError(error, classifyTransportErrors);
|
|
6569
|
+
}
|
|
6570
|
+
return JSON.parse(data);
|
|
6571
|
+
}
|
|
6354
6572
|
async function checkResponse(resp) {
|
|
6355
6573
|
if (resp.ok) {
|
|
6356
6574
|
return resp;
|
|
6357
|
-
}
|
|
6575
|
+
}
|
|
6576
|
+
let data;
|
|
6577
|
+
try {
|
|
6578
|
+
data = await resp.text();
|
|
6579
|
+
} catch (error) {
|
|
6358
6580
|
throw new FailedHTTPResponse(
|
|
6359
6581
|
resp.status,
|
|
6360
6582
|
resp.statusText,
|
|
6361
|
-
|
|
6583
|
+
"Unable to read response body",
|
|
6584
|
+
error
|
|
6362
6585
|
);
|
|
6363
6586
|
}
|
|
6587
|
+
throw new FailedHTTPResponse(resp.status, resp.statusText, data);
|
|
6364
6588
|
}
|
|
6365
6589
|
var HTTPConnection = class _HTTPConnection {
|
|
6366
|
-
base_url
|
|
6367
|
-
|
|
6368
|
-
headers;
|
|
6369
|
-
fetch;
|
|
6370
|
-
constructor(base_url, fetch2) {
|
|
6590
|
+
constructor(base_url, fetch2, classifyTransportErrors = false) {
|
|
6591
|
+
this.classifyTransportErrors = classifyTransportErrors;
|
|
6371
6592
|
this.base_url = base_url;
|
|
6372
6593
|
this.token = null;
|
|
6373
6594
|
this.headers = {};
|
|
6374
6595
|
this._reset();
|
|
6375
6596
|
this.fetch = fetch2;
|
|
6376
6597
|
}
|
|
6598
|
+
classifyTransportErrors;
|
|
6599
|
+
base_url;
|
|
6600
|
+
token;
|
|
6601
|
+
headers;
|
|
6602
|
+
fetch;
|
|
6377
6603
|
setFetch(fetch2) {
|
|
6378
6604
|
this.fetch = fetch2;
|
|
6379
6605
|
}
|
|
@@ -6413,9 +6639,9 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6413
6639
|
).toString();
|
|
6414
6640
|
const this_fetch = this.fetch;
|
|
6415
6641
|
const this_headers = this.headers;
|
|
6416
|
-
|
|
6417
|
-
|
|
6418
|
-
await this_fetch(url.toString(), {
|
|
6642
|
+
let response;
|
|
6643
|
+
try {
|
|
6644
|
+
response = await this_fetch(url.toString(), {
|
|
6419
6645
|
headers: {
|
|
6420
6646
|
Accept: "application/json",
|
|
6421
6647
|
...this_headers,
|
|
@@ -6423,8 +6649,14 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6423
6649
|
},
|
|
6424
6650
|
keepalive: true,
|
|
6425
6651
|
...rest
|
|
6426
|
-
})
|
|
6427
|
-
)
|
|
6652
|
+
});
|
|
6653
|
+
} catch (error) {
|
|
6654
|
+
if (config?.signal?.aborted) {
|
|
6655
|
+
throw getAbortReason(config.signal);
|
|
6656
|
+
}
|
|
6657
|
+
rethrowHTTPTransportError(error, this.classifyTransportErrors);
|
|
6658
|
+
}
|
|
6659
|
+
return await checkResponse(response);
|
|
6428
6660
|
}
|
|
6429
6661
|
async post(path, params, config, retries = 0) {
|
|
6430
6662
|
const { headers, ...rest } = config || {};
|
|
@@ -6434,8 +6666,9 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6434
6666
|
const tries = retries + 1;
|
|
6435
6667
|
for (let i = 0; i < tries; i++) {
|
|
6436
6668
|
try {
|
|
6437
|
-
|
|
6438
|
-
|
|
6669
|
+
let response;
|
|
6670
|
+
try {
|
|
6671
|
+
response = await this_fetch(_urljoin(this_base_url, path), {
|
|
6439
6672
|
method: "POST",
|
|
6440
6673
|
headers: {
|
|
6441
6674
|
Accept: "application/json",
|
|
@@ -6446,8 +6679,14 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6446
6679
|
body: typeof params === "string" ? params : params ? JSON.stringify(params) : void 0,
|
|
6447
6680
|
keepalive: true,
|
|
6448
6681
|
...rest
|
|
6449
|
-
})
|
|
6450
|
-
)
|
|
6682
|
+
});
|
|
6683
|
+
} catch (error) {
|
|
6684
|
+
if (config?.signal?.aborted) {
|
|
6685
|
+
throw getAbortReason(config.signal);
|
|
6686
|
+
}
|
|
6687
|
+
rethrowHTTPTransportError(error, this.classifyTransportErrors);
|
|
6688
|
+
}
|
|
6689
|
+
return await checkResponse(response);
|
|
6451
6690
|
} catch (error) {
|
|
6452
6691
|
if (config?.signal?.aborted) {
|
|
6453
6692
|
throw getAbortReason(config.signal);
|
|
@@ -6472,7 +6711,7 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6472
6711
|
for (let i = 0; i < tries; i++) {
|
|
6473
6712
|
try {
|
|
6474
6713
|
const resp = await this.get(`${object_type}`, args);
|
|
6475
|
-
return await resp.
|
|
6714
|
+
return await readJSONResponse(resp, this.classifyTransportErrors);
|
|
6476
6715
|
} catch (e) {
|
|
6477
6716
|
if (i < tries - 1) {
|
|
6478
6717
|
debugLogger.debug(
|
|
@@ -6495,7 +6734,7 @@ var HTTPConnection = class _HTTPConnection {
|
|
|
6495
6734
|
const resp = await this.post(`${object_type}`, args, {
|
|
6496
6735
|
headers: { "Content-Type": "application/json" }
|
|
6497
6736
|
});
|
|
6498
|
-
return await resp.
|
|
6737
|
+
return await readJSONResponse(resp, this.classifyTransportErrors);
|
|
6499
6738
|
}
|
|
6500
6739
|
// Custom inspect for Node.js console.log
|
|
6501
6740
|
[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
|
|
@@ -8543,6 +8782,7 @@ function initDataset(projectOrOptions, optionalOptions) {
|
|
|
8543
8782
|
const {
|
|
8544
8783
|
project,
|
|
8545
8784
|
dataset,
|
|
8785
|
+
datasetId,
|
|
8546
8786
|
description,
|
|
8547
8787
|
version,
|
|
8548
8788
|
snapshotName,
|
|
@@ -8558,6 +8798,11 @@ function initDataset(projectOrOptions, optionalOptions) {
|
|
|
8558
8798
|
state: stateArg,
|
|
8559
8799
|
_internal_btql
|
|
8560
8800
|
} = options;
|
|
8801
|
+
if (datasetId !== void 0 && (description !== void 0 || metadata !== void 0)) {
|
|
8802
|
+
throw new Error(
|
|
8803
|
+
"Cannot specify description or metadata when datasetId is provided"
|
|
8804
|
+
);
|
|
8805
|
+
}
|
|
8561
8806
|
const selection = normalizeDatasetSelection({
|
|
8562
8807
|
version,
|
|
8563
8808
|
environment,
|
|
@@ -8578,6 +8823,35 @@ function initDataset(projectOrOptions, optionalOptions) {
|
|
|
8578
8823
|
fetch: fetch2,
|
|
8579
8824
|
forceLogin
|
|
8580
8825
|
});
|
|
8826
|
+
if (datasetId !== void 0) {
|
|
8827
|
+
const objectInfo = datasetObjectInfoSchema.array().parse(
|
|
8828
|
+
await state.appConn().post_json("api/self/get_object_info", {
|
|
8829
|
+
object_type: "dataset",
|
|
8830
|
+
object_ids: [datasetId]
|
|
8831
|
+
})
|
|
8832
|
+
);
|
|
8833
|
+
if (objectInfo.length === 0) {
|
|
8834
|
+
throw new Error(`Dataset with ID ${datasetId} not found`);
|
|
8835
|
+
}
|
|
8836
|
+
if (objectInfo.length !== 1) {
|
|
8837
|
+
throw new Error(
|
|
8838
|
+
`Expected exactly one dataset with ID ${datasetId}, but found ${objectInfo.length}`
|
|
8839
|
+
);
|
|
8840
|
+
}
|
|
8841
|
+
const datasetInfo = objectInfo[0];
|
|
8842
|
+
return {
|
|
8843
|
+
project: {
|
|
8844
|
+
id: datasetInfo.parent_cols.project.id,
|
|
8845
|
+
name: datasetInfo.parent_cols.project.name,
|
|
8846
|
+
fullInfo: datasetInfo.parent_cols.project
|
|
8847
|
+
},
|
|
8848
|
+
dataset: {
|
|
8849
|
+
id: datasetInfo.object_id,
|
|
8850
|
+
name: datasetInfo.object_name,
|
|
8851
|
+
fullInfo: datasetInfo
|
|
8852
|
+
}
|
|
8853
|
+
};
|
|
8854
|
+
}
|
|
8581
8855
|
const args = {
|
|
8582
8856
|
org_id: state.orgId,
|
|
8583
8857
|
project_name: project,
|
|
@@ -8731,6 +9005,24 @@ function initLogger(options = {}) {
|
|
|
8731
9005
|
}
|
|
8732
9006
|
return ret;
|
|
8733
9007
|
}
|
|
9008
|
+
async function runCredentialScopedLoaderRequest(state, loginOptions, request) {
|
|
9009
|
+
const resolvedLoginOptions = await state._internalResolveLoaderLoginOptions(loginOptions);
|
|
9010
|
+
let cacheViews = state._internalGetLoaderCacheViews(resolvedLoginOptions);
|
|
9011
|
+
try {
|
|
9012
|
+
const requestState = await state._internalGetLoaderState(resolvedLoginOptions);
|
|
9013
|
+
cacheViews = state._internalGetLoaderCacheViews(
|
|
9014
|
+
resolvedLoginOptions,
|
|
9015
|
+
requestState
|
|
9016
|
+
);
|
|
9017
|
+
return {
|
|
9018
|
+
ok: true,
|
|
9019
|
+
response: await request(requestState),
|
|
9020
|
+
...cacheViews
|
|
9021
|
+
};
|
|
9022
|
+
} catch (error) {
|
|
9023
|
+
return { ok: false, error, ...cacheViews };
|
|
9024
|
+
}
|
|
9025
|
+
}
|
|
8734
9026
|
async function loadPrompt({
|
|
8735
9027
|
projectName,
|
|
8736
9028
|
projectId,
|
|
@@ -8755,43 +9047,47 @@ async function loadPrompt({
|
|
|
8755
9047
|
throw new Error("Must specify slug");
|
|
8756
9048
|
}
|
|
8757
9049
|
const state = stateArg ?? _globalState;
|
|
8758
|
-
|
|
8759
|
-
|
|
8760
|
-
|
|
8761
|
-
|
|
8762
|
-
|
|
8763
|
-
|
|
8764
|
-
|
|
8765
|
-
|
|
8766
|
-
|
|
8767
|
-
|
|
8768
|
-
|
|
8769
|
-
|
|
8770
|
-
|
|
9050
|
+
const result = await runCredentialScopedLoaderRequest(
|
|
9051
|
+
state,
|
|
9052
|
+
{ orgName, apiKey, appUrl, fetch: fetch2, forceLogin },
|
|
9053
|
+
async (requestState) => {
|
|
9054
|
+
let response2;
|
|
9055
|
+
if (id) {
|
|
9056
|
+
response2 = await requestState.apiConn().get_json(`v1/prompt/${id}`, versionOrEnvironment);
|
|
9057
|
+
if (response2) {
|
|
9058
|
+
response2 = { objects: [response2] };
|
|
9059
|
+
}
|
|
9060
|
+
} else {
|
|
9061
|
+
response2 = await requestState.apiConn().get_json("v1/prompt", {
|
|
9062
|
+
project_name: projectName,
|
|
9063
|
+
project_id: projectId,
|
|
9064
|
+
slug,
|
|
9065
|
+
...versionOrEnvironment
|
|
9066
|
+
});
|
|
8771
9067
|
}
|
|
8772
|
-
|
|
8773
|
-
|
|
8774
|
-
|
|
8775
|
-
|
|
8776
|
-
|
|
8777
|
-
|
|
8778
|
-
|
|
9068
|
+
return response2;
|
|
9069
|
+
}
|
|
9070
|
+
);
|
|
9071
|
+
const { promptCache } = result;
|
|
9072
|
+
if (!result.ok) {
|
|
9073
|
+
const e = result.error;
|
|
9074
|
+
if (!isLoaderCacheFallbackError(e)) {
|
|
9075
|
+
throw e;
|
|
8779
9076
|
}
|
|
8780
|
-
} catch (e) {
|
|
8781
9077
|
if (version || environment) {
|
|
8782
9078
|
throw new Error(`Prompt not found with specified parameters: ${e}`);
|
|
8783
9079
|
}
|
|
8784
9080
|
debugLogger.forState(state).warn("Failed to load prompt, attempting to fall back to cache:", e);
|
|
8785
9081
|
let prompt2;
|
|
8786
9082
|
if (id) {
|
|
8787
|
-
prompt2 = await
|
|
9083
|
+
prompt2 = await promptCache.get({ id });
|
|
8788
9084
|
if (!prompt2) {
|
|
8789
9085
|
throw new Error(
|
|
8790
9086
|
`Prompt with id ${id} not found (not found on server or in local cache): ${e}`
|
|
8791
9087
|
);
|
|
8792
9088
|
}
|
|
8793
9089
|
} else {
|
|
8794
|
-
prompt2 = await
|
|
9090
|
+
prompt2 = await promptCache.get({
|
|
8795
9091
|
slug,
|
|
8796
9092
|
projectId,
|
|
8797
9093
|
projectName,
|
|
@@ -8807,6 +9103,7 @@ async function loadPrompt({
|
|
|
8807
9103
|
}
|
|
8808
9104
|
return prompt2;
|
|
8809
9105
|
}
|
|
9106
|
+
const { response } = result;
|
|
8810
9107
|
if (!("objects" in response) || response.objects.length === 0) {
|
|
8811
9108
|
if (id) {
|
|
8812
9109
|
throw new Error(`Prompt with id ${id} not found.`);
|
|
@@ -8830,9 +9127,9 @@ async function loadPrompt({
|
|
|
8830
9127
|
const prompt = new Prompt2(metadata, defaults || {}, noTrace);
|
|
8831
9128
|
try {
|
|
8832
9129
|
if (id) {
|
|
8833
|
-
await
|
|
9130
|
+
await promptCache.set({ id }, prompt);
|
|
8834
9131
|
} else if (slug) {
|
|
8835
|
-
await
|
|
9132
|
+
await promptCache.set(
|
|
8836
9133
|
{ slug, projectId, projectName, version: version ?? "latest" },
|
|
8837
9134
|
prompt
|
|
8838
9135
|
);
|
|
@@ -8864,46 +9161,50 @@ async function loadParameters({
|
|
|
8864
9161
|
throw new Error("Must specify slug");
|
|
8865
9162
|
}
|
|
8866
9163
|
const state = stateArg ?? _globalState;
|
|
8867
|
-
|
|
8868
|
-
|
|
8869
|
-
|
|
8870
|
-
|
|
8871
|
-
|
|
8872
|
-
|
|
8873
|
-
|
|
8874
|
-
|
|
8875
|
-
|
|
8876
|
-
|
|
8877
|
-
|
|
8878
|
-
|
|
8879
|
-
}
|
|
8880
|
-
|
|
8881
|
-
|
|
9164
|
+
const result = await runCredentialScopedLoaderRequest(
|
|
9165
|
+
state,
|
|
9166
|
+
{ orgName, apiKey, appUrl, fetch: fetch2, forceLogin },
|
|
9167
|
+
async (requestState) => {
|
|
9168
|
+
let response2;
|
|
9169
|
+
if (id) {
|
|
9170
|
+
response2 = await requestState.apiConn().get_json(`v1/function/${id}`, {
|
|
9171
|
+
...versionOrEnvironment
|
|
9172
|
+
});
|
|
9173
|
+
if (response2) {
|
|
9174
|
+
response2 = { objects: [response2] };
|
|
9175
|
+
}
|
|
9176
|
+
} else {
|
|
9177
|
+
response2 = await requestState.apiConn().get_json("v1/function", {
|
|
9178
|
+
project_name: projectName,
|
|
9179
|
+
project_id: projectId,
|
|
9180
|
+
slug,
|
|
9181
|
+
function_type: "parameters",
|
|
9182
|
+
...versionOrEnvironment
|
|
9183
|
+
});
|
|
8882
9184
|
}
|
|
8883
|
-
|
|
8884
|
-
|
|
8885
|
-
|
|
8886
|
-
|
|
8887
|
-
|
|
8888
|
-
|
|
8889
|
-
|
|
8890
|
-
|
|
9185
|
+
return response2;
|
|
9186
|
+
}
|
|
9187
|
+
);
|
|
9188
|
+
const { parametersCache } = result;
|
|
9189
|
+
if (!result.ok) {
|
|
9190
|
+
const e = result.error;
|
|
9191
|
+
if (!isLoaderCacheFallbackError(e)) {
|
|
9192
|
+
throw e;
|
|
8891
9193
|
}
|
|
8892
|
-
} catch (e) {
|
|
8893
9194
|
if (version || environment) {
|
|
8894
9195
|
throw new Error(`Parameters not found with specified parameters: ${e}`);
|
|
8895
9196
|
}
|
|
8896
9197
|
debugLogger.forState(state).warn("Failed to load parameters, attempting to fall back to cache:", e);
|
|
8897
9198
|
let parameters2;
|
|
8898
9199
|
if (id) {
|
|
8899
|
-
parameters2 = await
|
|
9200
|
+
parameters2 = await parametersCache.get({ id });
|
|
8900
9201
|
if (!parameters2) {
|
|
8901
9202
|
throw new Error(
|
|
8902
9203
|
`Parameters with id ${id} not found (not found on server or in local cache): ${e}`
|
|
8903
9204
|
);
|
|
8904
9205
|
}
|
|
8905
9206
|
} else {
|
|
8906
|
-
parameters2 = await
|
|
9207
|
+
parameters2 = await parametersCache.get({
|
|
8907
9208
|
slug,
|
|
8908
9209
|
projectId,
|
|
8909
9210
|
projectName,
|
|
@@ -8919,6 +9220,7 @@ async function loadParameters({
|
|
|
8919
9220
|
}
|
|
8920
9221
|
return parameters2;
|
|
8921
9222
|
}
|
|
9223
|
+
const { response } = result;
|
|
8922
9224
|
if (!("objects" in response) || response.objects.length === 0) {
|
|
8923
9225
|
if (id) {
|
|
8924
9226
|
throw new Error(`Parameters with id ${id} not found.`);
|
|
@@ -8942,9 +9244,9 @@ async function loadParameters({
|
|
|
8942
9244
|
const parameters = new RemoteEvalParameters(metadata);
|
|
8943
9245
|
try {
|
|
8944
9246
|
if (id) {
|
|
8945
|
-
await
|
|
9247
|
+
await parametersCache.set({ id }, parameters);
|
|
8946
9248
|
} else if (slug) {
|
|
8947
|
-
await
|
|
9249
|
+
await parametersCache.set(
|
|
8948
9250
|
{ slug, projectId, projectName, version: version ?? "latest" },
|
|
8949
9251
|
parameters
|
|
8950
9252
|
);
|
|
@@ -8985,6 +9287,52 @@ async function login(options = {}) {
|
|
|
8985
9287
|
await state.login(options);
|
|
8986
9288
|
return state;
|
|
8987
9289
|
}
|
|
9290
|
+
async function loginToLoaderRequestState({
|
|
9291
|
+
appUrl,
|
|
9292
|
+
apiKey,
|
|
9293
|
+
orgName,
|
|
9294
|
+
fetch: fetch2
|
|
9295
|
+
}) {
|
|
9296
|
+
let orgId;
|
|
9297
|
+
let apiUrl;
|
|
9298
|
+
if (apiKey === TEST_API_KEY) {
|
|
9299
|
+
orgId = "test-org-id";
|
|
9300
|
+
apiUrl = "https://braintrust.dev/fake-api-url";
|
|
9301
|
+
} else {
|
|
9302
|
+
let loginResponse;
|
|
9303
|
+
try {
|
|
9304
|
+
loginResponse = await fetch2(_urljoin(appUrl, `/api/apikey/login`), {
|
|
9305
|
+
method: "POST",
|
|
9306
|
+
headers: {
|
|
9307
|
+
"Content-Type": "application/json",
|
|
9308
|
+
Authorization: `Bearer ${apiKey}`
|
|
9309
|
+
}
|
|
9310
|
+
});
|
|
9311
|
+
} catch (error) {
|
|
9312
|
+
throw new HTTPTransportError(error);
|
|
9313
|
+
}
|
|
9314
|
+
const info = await readJSONResponse(
|
|
9315
|
+
await checkResponse(loginResponse),
|
|
9316
|
+
true
|
|
9317
|
+
);
|
|
9318
|
+
const org = selectLoginOrg(info.org_info, orgName);
|
|
9319
|
+
orgId = org.id;
|
|
9320
|
+
apiUrl = isomorph_default.getEnv("BRAINTRUST_API_URL") ?? org.api_url;
|
|
9321
|
+
if (!apiUrl) {
|
|
9322
|
+
throw new Error(
|
|
9323
|
+
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."
|
|
9324
|
+
);
|
|
9325
|
+
}
|
|
9326
|
+
}
|
|
9327
|
+
const apiConnection = new HTTPConnection(apiUrl, fetch2, true);
|
|
9328
|
+
apiConnection.set_token(apiKey);
|
|
9329
|
+
apiConnection.make_long_lived();
|
|
9330
|
+
return {
|
|
9331
|
+
appUrl,
|
|
9332
|
+
orgId,
|
|
9333
|
+
apiConn: () => apiConnection
|
|
9334
|
+
};
|
|
9335
|
+
}
|
|
8988
9336
|
async function loginToState(options = {}) {
|
|
8989
9337
|
const {
|
|
8990
9338
|
appUrl = isomorph_default.getEnv("BRAINTRUST_APP_URL") || "https://www.braintrust.dev",
|
|
@@ -9016,16 +9364,18 @@ async function loginToState(options = {}) {
|
|
|
9016
9364
|
_saveOrgInfo(state, testOrgInfo, testOrgInfo[0].name);
|
|
9017
9365
|
return state;
|
|
9018
9366
|
} else {
|
|
9019
|
-
const
|
|
9020
|
-
|
|
9367
|
+
const loginResponse = await fetch2(
|
|
9368
|
+
_urljoin(state.appUrl, `/api/apikey/login`),
|
|
9369
|
+
{
|
|
9021
9370
|
method: "POST",
|
|
9022
9371
|
headers: {
|
|
9023
9372
|
"Content-Type": "application/json",
|
|
9024
9373
|
Authorization: `Bearer ${apiKey}`
|
|
9025
9374
|
}
|
|
9026
|
-
}
|
|
9375
|
+
}
|
|
9027
9376
|
);
|
|
9028
|
-
const
|
|
9377
|
+
const resp = await checkResponse(loginResponse);
|
|
9378
|
+
const info = await readJSONResponse(resp);
|
|
9029
9379
|
_saveOrgInfo(state, info.org_info, orgName);
|
|
9030
9380
|
if (!state.apiUrl) {
|
|
9031
9381
|
if (orgName) {
|
|
@@ -9528,11 +9878,11 @@ function wrapTraced(fn, args) {
|
|
|
9528
9878
|
}
|
|
9529
9879
|
if (args?.asyncFlush) {
|
|
9530
9880
|
return ((...fnArgs) => traced((span) => {
|
|
9531
|
-
if (!hasExplicitInput) {
|
|
9881
|
+
if (!args?.noTraceIO && !hasExplicitInput) {
|
|
9532
9882
|
span.log({ input: fnArgs });
|
|
9533
9883
|
}
|
|
9534
9884
|
const output = fn(...fnArgs);
|
|
9535
|
-
if (!hasExplicitOutput) {
|
|
9885
|
+
if (!args?.noTraceIO && !hasExplicitOutput) {
|
|
9536
9886
|
if (output instanceof Promise) {
|
|
9537
9887
|
return (async () => {
|
|
9538
9888
|
const result = await output;
|
|
@@ -9547,12 +9897,12 @@ function wrapTraced(fn, args) {
|
|
|
9547
9897
|
}, spanArgs));
|
|
9548
9898
|
} else {
|
|
9549
9899
|
return ((...fnArgs) => traced(async (span) => {
|
|
9550
|
-
if (!hasExplicitInput) {
|
|
9900
|
+
if (!args?.noTraceIO && !hasExplicitInput) {
|
|
9551
9901
|
span.log({ input: fnArgs });
|
|
9552
9902
|
}
|
|
9553
9903
|
const outputResult = fn(...fnArgs);
|
|
9554
9904
|
const output = await outputResult;
|
|
9555
|
-
if (!hasExplicitOutput) {
|
|
9905
|
+
if (!args?.noTraceIO && !hasExplicitOutput) {
|
|
9556
9906
|
span.log({ output });
|
|
9557
9907
|
}
|
|
9558
9908
|
return output;
|
|
@@ -9678,27 +10028,28 @@ async function* asyncGeneratorWithCurrent(span, gen, state = void 0) {
|
|
|
9678
10028
|
function withParent(parent, callback, state = void 0) {
|
|
9679
10029
|
return (state ?? _globalState).currentParent.run(parent, () => callback());
|
|
9680
10030
|
}
|
|
9681
|
-
function _saveOrgInfo(state,
|
|
9682
|
-
|
|
10031
|
+
function _saveOrgInfo(state, orgInfo, orgName) {
|
|
10032
|
+
const org = selectLoginOrg(orgInfo, orgName);
|
|
10033
|
+
state.orgId = org.id;
|
|
10034
|
+
state.orgName = org.name;
|
|
10035
|
+
state.apiUrl = isomorph_default.getEnv("BRAINTRUST_API_URL") ?? org.api_url;
|
|
10036
|
+
state.proxyUrl = isomorph_default.getEnv("BRAINTRUST_PROXY_URL") ?? org.proxy_url;
|
|
10037
|
+
state.gitMetadataSettings = org.git_metadata || void 0;
|
|
10038
|
+
}
|
|
10039
|
+
function selectLoginOrg(orgInfo, orgName) {
|
|
10040
|
+
if (orgInfo.length === 0) {
|
|
9683
10041
|
throw new LoginInvalidOrgError(
|
|
9684
10042
|
"This user is not part of any organizations."
|
|
9685
10043
|
);
|
|
9686
10044
|
}
|
|
9687
|
-
for (const org of
|
|
9688
|
-
if (
|
|
9689
|
-
|
|
9690
|
-
state.orgName = org.name;
|
|
9691
|
-
state.apiUrl = isomorph_default.getEnv("BRAINTRUST_API_URL") ?? org.api_url;
|
|
9692
|
-
state.proxyUrl = isomorph_default.getEnv("BRAINTRUST_PROXY_URL") ?? org.proxy_url;
|
|
9693
|
-
state.gitMetadataSettings = org.git_metadata || void 0;
|
|
9694
|
-
break;
|
|
10045
|
+
for (const org of orgInfo) {
|
|
10046
|
+
if (orgName === void 0 || org.name === orgName) {
|
|
10047
|
+
return org;
|
|
9695
10048
|
}
|
|
9696
10049
|
}
|
|
9697
|
-
|
|
9698
|
-
|
|
9699
|
-
|
|
9700
|
-
);
|
|
9701
|
-
}
|
|
10050
|
+
throw new LoginInvalidOrgError(
|
|
10051
|
+
`Organization ${orgName} not found. Must be one of ${orgInfo.map((org) => org.name).join(", ")}`
|
|
10052
|
+
);
|
|
9702
10053
|
}
|
|
9703
10054
|
function validateTags(tags) {
|
|
9704
10055
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -11604,6 +11955,14 @@ var Prompt2 = class _Prompt {
|
|
|
11604
11955
|
static isPrompt(data) {
|
|
11605
11956
|
return typeof data === "object" && data !== null && "__braintrust_prompt_marker" in data;
|
|
11606
11957
|
}
|
|
11958
|
+
/** @internal */
|
|
11959
|
+
_internalSerializeForCache() {
|
|
11960
|
+
return {
|
|
11961
|
+
metadata: this.metadata,
|
|
11962
|
+
defaults: this.defaults,
|
|
11963
|
+
noTrace: this.noTrace
|
|
11964
|
+
};
|
|
11965
|
+
}
|
|
11607
11966
|
static fromPromptData(name, promptData) {
|
|
11608
11967
|
return new _Prompt(
|
|
11609
11968
|
{
|
|
@@ -11644,6 +12003,10 @@ var RemoteEvalParameters = class {
|
|
|
11644
12003
|
get data() {
|
|
11645
12004
|
return this.metadata.function_data.data ?? {};
|
|
11646
12005
|
}
|
|
12006
|
+
/** @internal */
|
|
12007
|
+
_internalSerializeForCache() {
|
|
12008
|
+
return { metadata: this.metadata };
|
|
12009
|
+
}
|
|
11647
12010
|
validate(data) {
|
|
11648
12011
|
if (typeof data !== "object" || data === null) {
|
|
11649
12012
|
return false;
|
|
@@ -12430,56 +12793,14 @@ function suppressionStore() {
|
|
|
12430
12793
|
autoInstrumentationSuppressionStore ??= isomorph_default.newAsyncLocalStorage();
|
|
12431
12794
|
return autoInstrumentationSuppressionStore;
|
|
12432
12795
|
}
|
|
12433
|
-
function currentFrames() {
|
|
12434
|
-
return suppressionStore().getStore()?.frames ?? [];
|
|
12435
|
-
}
|
|
12436
12796
|
function isAutoInstrumentationSuppressed() {
|
|
12437
|
-
|
|
12438
|
-
return frames[frames.length - 1]?.mode === "suppress";
|
|
12797
|
+
return suppressionStore().getStore() === true;
|
|
12439
12798
|
}
|
|
12440
12799
|
function runWithAutoInstrumentationSuppressed(callback) {
|
|
12441
|
-
|
|
12442
|
-
id: /* @__PURE__ */ Symbol("braintrust.auto-instrumentation-suppress"),
|
|
12443
|
-
mode: "suppress"
|
|
12444
|
-
};
|
|
12445
|
-
return suppressionStore().run(
|
|
12446
|
-
{ frames: [...currentFrames(), frame] },
|
|
12447
|
-
callback
|
|
12448
|
-
);
|
|
12800
|
+
return suppressionStore().run(true, callback);
|
|
12449
12801
|
}
|
|
12450
|
-
function
|
|
12451
|
-
|
|
12452
|
-
if (!startChannel) {
|
|
12453
|
-
return void 0;
|
|
12454
|
-
}
|
|
12455
|
-
const store = suppressionStore();
|
|
12456
|
-
startChannel.bindStore(store, () => ({
|
|
12457
|
-
frames: [
|
|
12458
|
-
...currentFrames(),
|
|
12459
|
-
{
|
|
12460
|
-
id: /* @__PURE__ */ Symbol("braintrust.auto-instrumentation-suppress"),
|
|
12461
|
-
mode: "suppress"
|
|
12462
|
-
}
|
|
12463
|
-
]
|
|
12464
|
-
}));
|
|
12465
|
-
return () => {
|
|
12466
|
-
startChannel.unbindStore(store);
|
|
12467
|
-
};
|
|
12468
|
-
}
|
|
12469
|
-
function enterAutoInstrumentationAllowed() {
|
|
12470
|
-
const frame = {
|
|
12471
|
-
id: /* @__PURE__ */ Symbol("braintrust.auto-instrumentation-allow"),
|
|
12472
|
-
mode: "allow"
|
|
12473
|
-
};
|
|
12474
|
-
suppressionStore().enterWith({
|
|
12475
|
-
frames: [...currentFrames(), frame]
|
|
12476
|
-
});
|
|
12477
|
-
return () => {
|
|
12478
|
-
const frames = currentFrames().filter(
|
|
12479
|
-
(candidate) => candidate.id !== frame.id
|
|
12480
|
-
);
|
|
12481
|
-
suppressionStore().enterWith(frames.length > 0 ? { frames } : void 0);
|
|
12482
|
-
};
|
|
12802
|
+
function runWithAutoInstrumentationAllowed(callback) {
|
|
12803
|
+
return suppressionStore().run(void 0, callback);
|
|
12483
12804
|
}
|
|
12484
12805
|
|
|
12485
12806
|
// src/instrumentation/core/channel-tracing.ts
|
|
@@ -13768,13 +14089,33 @@ function aggregateChatLogprobs(existing, incoming) {
|
|
|
13768
14089
|
}
|
|
13769
14090
|
return aggregated;
|
|
13770
14091
|
}
|
|
14092
|
+
function createAggregatedChatChoice(index) {
|
|
14093
|
+
return {
|
|
14094
|
+
index,
|
|
14095
|
+
role: void 0,
|
|
14096
|
+
content: void 0,
|
|
14097
|
+
refusal: void 0,
|
|
14098
|
+
toolCallsByIndex: /* @__PURE__ */ new Map(),
|
|
14099
|
+
logprobs: void 0,
|
|
14100
|
+
finish_reason: void 0
|
|
14101
|
+
};
|
|
14102
|
+
}
|
|
14103
|
+
function toChatChoice(choice) {
|
|
14104
|
+
const toolCalls2 = Array.from(choice.toolCallsByIndex.entries()).sort(([left], [right]) => left - right).map(([, toolCall]) => toolCall);
|
|
14105
|
+
return {
|
|
14106
|
+
index: choice.index,
|
|
14107
|
+
message: {
|
|
14108
|
+
role: choice.role,
|
|
14109
|
+
content: choice.content,
|
|
14110
|
+
...choice.refusal !== void 0 ? { refusal: choice.refusal } : {},
|
|
14111
|
+
tool_calls: toolCalls2.length > 0 ? toolCalls2 : void 0
|
|
14112
|
+
},
|
|
14113
|
+
logprobs: choice.logprobs ?? null,
|
|
14114
|
+
finish_reason: choice.finish_reason
|
|
14115
|
+
};
|
|
14116
|
+
}
|
|
13771
14117
|
function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
|
|
13772
|
-
|
|
13773
|
-
let content = void 0;
|
|
13774
|
-
let refusal = void 0;
|
|
13775
|
-
let tool_calls = void 0;
|
|
13776
|
-
let logprobs = void 0;
|
|
13777
|
-
let finish_reason = void 0;
|
|
14118
|
+
const choicesByIndex = /* @__PURE__ */ new Map();
|
|
13778
14119
|
let metrics = {};
|
|
13779
14120
|
for (const chunk of chunks) {
|
|
13780
14121
|
if (chunk.usage) {
|
|
@@ -13783,62 +14124,75 @@ function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
|
|
|
13783
14124
|
...parseMetricsFromUsage(chunk.usage)
|
|
13784
14125
|
};
|
|
13785
14126
|
}
|
|
13786
|
-
const
|
|
13787
|
-
if (!
|
|
13788
|
-
continue;
|
|
13789
|
-
}
|
|
13790
|
-
if (choice.finish_reason) {
|
|
13791
|
-
finish_reason = choice.finish_reason;
|
|
13792
|
-
}
|
|
13793
|
-
logprobs = aggregateChatLogprobs(logprobs, choice.logprobs);
|
|
13794
|
-
const delta = choice.delta;
|
|
13795
|
-
if (!delta) {
|
|
14127
|
+
const choices = chunk.choices;
|
|
14128
|
+
if (!choices?.length) {
|
|
13796
14129
|
continue;
|
|
13797
14130
|
}
|
|
13798
|
-
|
|
13799
|
-
|
|
13800
|
-
|
|
13801
|
-
|
|
13802
|
-
|
|
13803
|
-
|
|
13804
|
-
|
|
13805
|
-
|
|
13806
|
-
|
|
13807
|
-
|
|
13808
|
-
|
|
13809
|
-
|
|
13810
|
-
|
|
13811
|
-
|
|
13812
|
-
|
|
13813
|
-
|
|
13814
|
-
|
|
13815
|
-
|
|
13816
|
-
|
|
13817
|
-
|
|
13818
|
-
|
|
14131
|
+
for (const choice of choices) {
|
|
14132
|
+
const choiceIndex = choice.index;
|
|
14133
|
+
let aggregatedChoice = choicesByIndex.get(choiceIndex);
|
|
14134
|
+
if (!aggregatedChoice) {
|
|
14135
|
+
aggregatedChoice = createAggregatedChatChoice(choiceIndex);
|
|
14136
|
+
choicesByIndex.set(choiceIndex, aggregatedChoice);
|
|
14137
|
+
}
|
|
14138
|
+
if (choice.finish_reason) {
|
|
14139
|
+
aggregatedChoice.finish_reason = choice.finish_reason;
|
|
14140
|
+
}
|
|
14141
|
+
aggregatedChoice.logprobs = aggregateChatLogprobs(
|
|
14142
|
+
aggregatedChoice.logprobs,
|
|
14143
|
+
choice.logprobs
|
|
14144
|
+
);
|
|
14145
|
+
const delta = choice.delta;
|
|
14146
|
+
if (!delta) {
|
|
14147
|
+
continue;
|
|
14148
|
+
}
|
|
14149
|
+
if (delta.finish_reason) {
|
|
14150
|
+
aggregatedChoice.finish_reason = delta.finish_reason;
|
|
14151
|
+
}
|
|
14152
|
+
if (!aggregatedChoice.role && delta.role) {
|
|
14153
|
+
aggregatedChoice.role = delta.role;
|
|
14154
|
+
}
|
|
14155
|
+
if (delta.content) {
|
|
14156
|
+
aggregatedChoice.content = (aggregatedChoice.content || "") + delta.content;
|
|
14157
|
+
}
|
|
14158
|
+
if (delta.refusal) {
|
|
14159
|
+
aggregatedChoice.refusal = (aggregatedChoice.refusal || "") + delta.refusal;
|
|
14160
|
+
}
|
|
14161
|
+
if (delta.tool_calls) {
|
|
14162
|
+
for (const toolDelta of delta.tool_calls) {
|
|
14163
|
+
let aggregatedToolCall = aggregatedChoice.toolCallsByIndex.get(
|
|
14164
|
+
toolDelta.index
|
|
14165
|
+
);
|
|
14166
|
+
if (!aggregatedToolCall) {
|
|
14167
|
+
aggregatedToolCall = {
|
|
14168
|
+
function: { arguments: "" }
|
|
14169
|
+
};
|
|
14170
|
+
aggregatedChoice.toolCallsByIndex.set(
|
|
14171
|
+
toolDelta.index,
|
|
14172
|
+
aggregatedToolCall
|
|
14173
|
+
);
|
|
13819
14174
|
}
|
|
13820
|
-
|
|
13821
|
-
|
|
13822
|
-
|
|
14175
|
+
if (toolDelta.id !== void 0) {
|
|
14176
|
+
aggregatedToolCall.id = toolDelta.id;
|
|
14177
|
+
}
|
|
14178
|
+
if (toolDelta.type !== void 0) {
|
|
14179
|
+
aggregatedToolCall.type = toolDelta.type;
|
|
14180
|
+
}
|
|
14181
|
+
if (toolDelta.function?.name !== void 0) {
|
|
14182
|
+
aggregatedToolCall.function.name = toolDelta.function.name;
|
|
14183
|
+
}
|
|
14184
|
+
if (toolDelta.function?.arguments !== void 0) {
|
|
14185
|
+
aggregatedToolCall.function.arguments += toolDelta.function.arguments;
|
|
14186
|
+
}
|
|
14187
|
+
}
|
|
13823
14188
|
}
|
|
13824
14189
|
}
|
|
13825
14190
|
}
|
|
13826
14191
|
metrics = withCachedMetric(metrics, streamResult, endEvent);
|
|
14192
|
+
const output = Array.from(choicesByIndex.values()).sort((left, right) => left.index - right.index).map(toChatChoice);
|
|
13827
14193
|
return {
|
|
13828
14194
|
metrics,
|
|
13829
|
-
output: [
|
|
13830
|
-
{
|
|
13831
|
-
index: 0,
|
|
13832
|
-
message: {
|
|
13833
|
-
role,
|
|
13834
|
-
content,
|
|
13835
|
-
...refusal !== void 0 ? { refusal } : {},
|
|
13836
|
-
tool_calls
|
|
13837
|
-
},
|
|
13838
|
-
logprobs: logprobs ?? null,
|
|
13839
|
-
finish_reason
|
|
13840
|
-
}
|
|
13841
|
-
]
|
|
14195
|
+
output: output.length > 0 ? output : [toChatChoice(createAggregatedChatChoice(0))]
|
|
13842
14196
|
};
|
|
13843
14197
|
}
|
|
13844
14198
|
function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
|
|
@@ -15174,6 +15528,12 @@ function parseMetricsFromUsage2(usage) {
|
|
|
15174
15528
|
}
|
|
15175
15529
|
}
|
|
15176
15530
|
}
|
|
15531
|
+
if (isObject(usage.output_tokens_details)) {
|
|
15532
|
+
const thinkingTokens = usage.output_tokens_details.thinking_tokens;
|
|
15533
|
+
if (typeof thinkingTokens === "number") {
|
|
15534
|
+
metrics.completion_reasoning_tokens = thinkingTokens;
|
|
15535
|
+
}
|
|
15536
|
+
}
|
|
15177
15537
|
if (isObject(usage.server_tool_use)) {
|
|
15178
15538
|
for (const [name, value] of Object.entries(usage.server_tool_use)) {
|
|
15179
15539
|
if (typeof value === "number") {
|
|
@@ -16130,7 +16490,6 @@ function endHarnessTurn(parent) {
|
|
|
16130
16490
|
function braintrustAISDKTelemetry() {
|
|
16131
16491
|
const operations = /* @__PURE__ */ new Map();
|
|
16132
16492
|
const operationKeysByCallId = /* @__PURE__ */ new Map();
|
|
16133
|
-
const workflowOperationKeyStore = isomorph_default.newAsyncLocalStorage();
|
|
16134
16493
|
const modelSpans = /* @__PURE__ */ new Map();
|
|
16135
16494
|
const objectSpans = /* @__PURE__ */ new Map();
|
|
16136
16495
|
const embedSpans = /* @__PURE__ */ new Map();
|
|
@@ -16173,9 +16532,6 @@ function braintrustAISDKTelemetry() {
|
|
|
16173
16532
|
return;
|
|
16174
16533
|
}
|
|
16175
16534
|
operations.delete(operationKey);
|
|
16176
|
-
if (workflowOperationKeyStore.getStore() === operationKey) {
|
|
16177
|
-
workflowOperationKeyStore.enterWith(void 0);
|
|
16178
|
-
}
|
|
16179
16535
|
const keys = operationKeysByCallId.get(state.callId);
|
|
16180
16536
|
if (!keys) {
|
|
16181
16537
|
return;
|
|
@@ -16223,14 +16579,7 @@ function braintrustAISDKTelemetry() {
|
|
|
16223
16579
|
return key;
|
|
16224
16580
|
}
|
|
16225
16581
|
}
|
|
16226
|
-
|
|
16227
|
-
if (workflowOperationKey && keys.includes(workflowOperationKey)) {
|
|
16228
|
-
return workflowOperationKey;
|
|
16229
|
-
}
|
|
16230
|
-
if (callId === "workflow-agent") {
|
|
16231
|
-
return void 0;
|
|
16232
|
-
}
|
|
16233
|
-
return mode === "finish" ? keys[0] : keys[keys.length - 1];
|
|
16582
|
+
return callId === "workflow-agent" || mode === "active" ? keys[keys.length - 1] : keys[0];
|
|
16234
16583
|
};
|
|
16235
16584
|
const operationKeyFromEvent = (event, mode = "active") => {
|
|
16236
16585
|
const explicit = explicitOperationKey(event);
|
|
@@ -16244,17 +16593,13 @@ function braintrustAISDKTelemetry() {
|
|
|
16244
16593
|
if (operationKey) {
|
|
16245
16594
|
return operationKey;
|
|
16246
16595
|
}
|
|
16247
|
-
const
|
|
16248
|
-
if (
|
|
16249
|
-
return
|
|
16596
|
+
const workflowAgentKeys2 = operationKeysByCallId.get("workflow-agent");
|
|
16597
|
+
if (workflowAgentKeys2?.length) {
|
|
16598
|
+
return workflowAgentKeys2[workflowAgentKeys2.length - 1];
|
|
16250
16599
|
}
|
|
16251
16600
|
return callId === "workflow-agent" ? void 0 : callId;
|
|
16252
16601
|
}
|
|
16253
16602
|
}
|
|
16254
|
-
const workflowOperationKey = workflowOperationKeyStore.getStore();
|
|
16255
|
-
if (workflowOperationKey && operations.has(workflowOperationKey)) {
|
|
16256
|
-
return workflowOperationKey;
|
|
16257
|
-
}
|
|
16258
16603
|
const wrapperSpan = currentWorkflowAgentWrapperSpan();
|
|
16259
16604
|
if (wrapperSpan?.spanId) {
|
|
16260
16605
|
for (const [operationKey, state] of operations) {
|
|
@@ -16264,8 +16609,8 @@ function braintrustAISDKTelemetry() {
|
|
|
16264
16609
|
}
|
|
16265
16610
|
}
|
|
16266
16611
|
const workflowAgentKeys = operationKeysByCallId.get("workflow-agent");
|
|
16267
|
-
if (workflowAgentKeys?.length
|
|
16268
|
-
return workflowAgentKeys[
|
|
16612
|
+
if (workflowAgentKeys?.length) {
|
|
16613
|
+
return workflowAgentKeys[workflowAgentKeys.length - 1];
|
|
16269
16614
|
}
|
|
16270
16615
|
if (operations.size === 1) {
|
|
16271
16616
|
return operations.keys().next().value;
|
|
@@ -16468,9 +16813,6 @@ function braintrustAISDKTelemetry() {
|
|
|
16468
16813
|
if (!ownsSpan) {
|
|
16469
16814
|
return;
|
|
16470
16815
|
}
|
|
16471
|
-
if (workflowAgent) {
|
|
16472
|
-
workflowOperationKeyStore.enterWith(operationKey);
|
|
16473
|
-
}
|
|
16474
16816
|
let metadata = metadataFromEvent(event);
|
|
16475
16817
|
const logPayload = { metadata };
|
|
16476
16818
|
const workflowAgentCallInput = workflowAgent ? operationInput(event, operationName) : void 0;
|
|
@@ -16942,6 +17284,10 @@ var aiSDKChannels = defineChannels(
|
|
|
16942
17284
|
channelName: "generateText",
|
|
16943
17285
|
kind: "async"
|
|
16944
17286
|
}),
|
|
17287
|
+
generateImage: channel({
|
|
17288
|
+
channelName: "generateImage",
|
|
17289
|
+
kind: "async"
|
|
17290
|
+
}),
|
|
16945
17291
|
streamText: channel({
|
|
16946
17292
|
channelName: "streamText",
|
|
16947
17293
|
kind: "async"
|
|
@@ -17129,7 +17475,7 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
17129
17475
|
}
|
|
17130
17476
|
subscribeToAISDK() {
|
|
17131
17477
|
const denyOutputPaths = this.config.denyOutputPaths || DEFAULT_DENY_OUTPUT_PATHS;
|
|
17132
|
-
this.unsubscribers.push(
|
|
17478
|
+
this.unsubscribers.push(interceptAISDKV7TelemetryDispatcher());
|
|
17133
17479
|
this.unsubscribers.push(subscribeToHarnessAgentCreateSession());
|
|
17134
17480
|
this.unsubscribers.push(
|
|
17135
17481
|
subscribeToHarnessContinuation(
|
|
@@ -17157,6 +17503,18 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
17157
17503
|
aggregateChunks: aggregateAISDKChunks
|
|
17158
17504
|
})
|
|
17159
17505
|
);
|
|
17506
|
+
this.unsubscribers.push(
|
|
17507
|
+
traceAsyncChannel(aiSDKChannels.generateImage, {
|
|
17508
|
+
name: "generateImage",
|
|
17509
|
+
type: "llm" /* LLM */,
|
|
17510
|
+
extractInput: ([params], event) => prepareAISDKGenerateImageInput(params, event.self),
|
|
17511
|
+
extractOutput: (result, endEvent) => processAISDKGenerateImageOutput(
|
|
17512
|
+
result,
|
|
17513
|
+
resolveDenyOutputPaths(endEvent, denyOutputPaths)
|
|
17514
|
+
),
|
|
17515
|
+
extractMetrics: (result) => extractTokenMetrics(result)
|
|
17516
|
+
})
|
|
17517
|
+
);
|
|
17160
17518
|
this.unsubscribers.push(
|
|
17161
17519
|
traceStreamingChannel(aiSDKChannels.streamText, {
|
|
17162
17520
|
name: "streamText",
|
|
@@ -17646,26 +18004,29 @@ function subscribeToHarnessContinuation(continuationChannel, defaultDenyOutputPa
|
|
|
17646
18004
|
channel2.unsubscribe(handlers);
|
|
17647
18005
|
};
|
|
17648
18006
|
}
|
|
17649
|
-
function
|
|
17650
|
-
const channel2 = aiSDKChannels.v7CreateTelemetryDispatcher.tracingChannel();
|
|
18007
|
+
function interceptAISDKV7TelemetryDispatcher() {
|
|
17651
18008
|
const telemetry = braintrustAISDKTelemetry();
|
|
17652
|
-
|
|
17653
|
-
|
|
17654
|
-
const
|
|
17655
|
-
|
|
17656
|
-
|
|
18009
|
+
return aiSDKChannels.v7CreateTelemetryDispatcher.intercept(
|
|
18010
|
+
(target, thisArg, args) => {
|
|
18011
|
+
const dispatcher = Reflect.apply(target, thisArg, args);
|
|
18012
|
+
const telemetryOptions = args[0]?.telemetry;
|
|
18013
|
+
if (telemetryOptions?.isEnabled !== false) {
|
|
18014
|
+
try {
|
|
18015
|
+
patchAISDKV7TelemetryDispatcher(
|
|
18016
|
+
dispatcher,
|
|
18017
|
+
telemetry,
|
|
18018
|
+
telemetryOptions
|
|
18019
|
+
);
|
|
18020
|
+
} catch (error) {
|
|
18021
|
+
debugLogger.error(
|
|
18022
|
+
"Error instrumenting AI SDK v7 telemetry dispatcher:",
|
|
18023
|
+
error
|
|
18024
|
+
);
|
|
18025
|
+
}
|
|
17657
18026
|
}
|
|
17658
|
-
|
|
17659
|
-
event.result,
|
|
17660
|
-
telemetry,
|
|
17661
|
-
telemetryOptions
|
|
17662
|
-
);
|
|
18027
|
+
return dispatcher;
|
|
17663
18028
|
}
|
|
17664
|
-
|
|
17665
|
-
channel2.subscribe(handlers);
|
|
17666
|
-
return () => {
|
|
17667
|
-
channel2.unsubscribe(handlers);
|
|
17668
|
-
};
|
|
18029
|
+
);
|
|
17669
18030
|
}
|
|
17670
18031
|
function patchAISDKV7TelemetryDispatcher(dispatcher, telemetry, telemetryOptions) {
|
|
17671
18032
|
if (!isObject(dispatcher)) {
|
|
@@ -17990,16 +18351,10 @@ var convertImageToAttachment = (image, explicitMimeType) => {
|
|
|
17990
18351
|
}
|
|
17991
18352
|
}
|
|
17992
18353
|
if (explicitMimeType) {
|
|
17993
|
-
|
|
17994
|
-
|
|
17995
|
-
data: new Blob([image], { type: explicitMimeType }),
|
|
17996
|
-
filename: `image.${getExtensionFromMediaType(explicitMimeType)}`,
|
|
17997
|
-
contentType: explicitMimeType
|
|
17998
|
-
});
|
|
17999
|
-
}
|
|
18000
|
-
if (typeof Buffer !== "undefined" && Buffer.isBuffer(image)) {
|
|
18354
|
+
const blob = convertDataToBlob(image, explicitMimeType);
|
|
18355
|
+
if (blob) {
|
|
18001
18356
|
return new Attachment({
|
|
18002
|
-
data:
|
|
18357
|
+
data: blob,
|
|
18003
18358
|
filename: `image.${getExtensionFromMediaType(explicitMimeType)}`,
|
|
18004
18359
|
contentType: explicitMimeType
|
|
18005
18360
|
});
|
|
@@ -18053,6 +18408,25 @@ var convertDataToAttachment = (data, mimeType, filename) => {
|
|
|
18053
18408
|
function processAISDKCallInput(params) {
|
|
18054
18409
|
return processInputAttachmentsSync(params);
|
|
18055
18410
|
}
|
|
18411
|
+
function processAISDKGenerateImageInput(params) {
|
|
18412
|
+
const prompt = params.prompt;
|
|
18413
|
+
if (!isObject(prompt) || Array.isArray(prompt)) {
|
|
18414
|
+
return processAISDKCallInput(params);
|
|
18415
|
+
}
|
|
18416
|
+
const processedPrompt = { ...prompt };
|
|
18417
|
+
if (Array.isArray(prompt.images)) {
|
|
18418
|
+
processedPrompt.images = prompt.images.map(
|
|
18419
|
+
(image) => convertImageToAttachment(image, "image/png") ?? image
|
|
18420
|
+
);
|
|
18421
|
+
}
|
|
18422
|
+
if (prompt.mask !== void 0) {
|
|
18423
|
+
processedPrompt.mask = convertImageToAttachment(prompt.mask, "image/png") ?? prompt.mask;
|
|
18424
|
+
}
|
|
18425
|
+
return processAISDKCallInput({
|
|
18426
|
+
...params,
|
|
18427
|
+
prompt: processedPrompt
|
|
18428
|
+
});
|
|
18429
|
+
}
|
|
18056
18430
|
function processAISDKWorkflowAgentCallInput(params) {
|
|
18057
18431
|
const processed = processAISDKCallInput(params);
|
|
18058
18432
|
return {
|
|
@@ -18191,6 +18565,12 @@ function prepareAISDKEmbedInput(params, self) {
|
|
|
18191
18565
|
metadata: extractMetadataFromEmbedParams(params, self)
|
|
18192
18566
|
};
|
|
18193
18567
|
}
|
|
18568
|
+
function prepareAISDKGenerateImageInput(params, self) {
|
|
18569
|
+
return {
|
|
18570
|
+
input: processAISDKGenerateImageInput(params).input,
|
|
18571
|
+
metadata: extractMetadataFromCallParams(params, self)
|
|
18572
|
+
};
|
|
18573
|
+
}
|
|
18194
18574
|
function prepareAISDKRerankInput(params, self) {
|
|
18195
18575
|
const { documents, query } = params;
|
|
18196
18576
|
return {
|
|
@@ -19566,6 +19946,57 @@ function processAISDKOutput(output, denyOutputPaths) {
|
|
|
19566
19946
|
}
|
|
19567
19947
|
return normalizeAISDKLoggedOutput(sanitized);
|
|
19568
19948
|
}
|
|
19949
|
+
function processAISDKGenerateImageOutput(output, denyOutputPaths) {
|
|
19950
|
+
if (!output || typeof output !== "object") {
|
|
19951
|
+
return output;
|
|
19952
|
+
}
|
|
19953
|
+
const summarized = {};
|
|
19954
|
+
for (const field of [
|
|
19955
|
+
"usage",
|
|
19956
|
+
"warnings",
|
|
19957
|
+
"providerMetadata",
|
|
19958
|
+
"experimental_providerMetadata",
|
|
19959
|
+
"responses"
|
|
19960
|
+
]) {
|
|
19961
|
+
const value = safeSerializableFieldRead(output, field);
|
|
19962
|
+
if (value !== void 0 && isSerializableOutputValue(value)) {
|
|
19963
|
+
summarized[field] = value;
|
|
19964
|
+
}
|
|
19965
|
+
}
|
|
19966
|
+
const images = safeSerializableFieldRead(output, "images");
|
|
19967
|
+
const image = safeSerializableFieldRead(output, "image");
|
|
19968
|
+
const generatedFiles = Array.isArray(images) && images.length > 0 ? images : image !== void 0 ? [image] : [];
|
|
19969
|
+
const loggedOutput = normalizeAISDKLoggedOutput(
|
|
19970
|
+
omit(summarized, denyOutputPaths)
|
|
19971
|
+
);
|
|
19972
|
+
if (generatedFiles.length > 0) {
|
|
19973
|
+
loggedOutput.images = generatedFiles.map(
|
|
19974
|
+
(file, index) => convertAISDKGeneratedFileToAttachment(file, index)
|
|
19975
|
+
);
|
|
19976
|
+
}
|
|
19977
|
+
return loggedOutput;
|
|
19978
|
+
}
|
|
19979
|
+
function convertAISDKGeneratedFileToAttachment(file, index) {
|
|
19980
|
+
if (!file || typeof file !== "object") {
|
|
19981
|
+
return file;
|
|
19982
|
+
}
|
|
19983
|
+
const generatedFile = file;
|
|
19984
|
+
const generatedMediaType = safeSerializableFieldRead(
|
|
19985
|
+
generatedFile,
|
|
19986
|
+
"mediaType"
|
|
19987
|
+
);
|
|
19988
|
+
const mediaType = typeof generatedMediaType === "string" ? generatedMediaType : "application/octet-stream";
|
|
19989
|
+
const data = safeSerializableFieldRead(generatedFile, "base64") ?? safeSerializableFieldRead(generatedFile, "uint8Array");
|
|
19990
|
+
const blob = convertDataToBlob(data, mediaType);
|
|
19991
|
+
if (blob) {
|
|
19992
|
+
return new Attachment({
|
|
19993
|
+
data: blob,
|
|
19994
|
+
filename: `generated_image_${index}.${getExtensionFromMediaType(mediaType)}`,
|
|
19995
|
+
contentType: mediaType
|
|
19996
|
+
});
|
|
19997
|
+
}
|
|
19998
|
+
return file;
|
|
19999
|
+
}
|
|
19569
20000
|
function processAISDKEmbeddingOutput(output, denyOutputPaths) {
|
|
19570
20001
|
if (!output || typeof output !== "object") {
|
|
19571
20002
|
return output;
|
|
@@ -20066,118 +20497,22 @@ var claudeAgentSDKChannels = defineChannels(
|
|
|
20066
20497
|
var CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION = "__braintrust_skip_local_tool_hooks";
|
|
20067
20498
|
|
|
20068
20499
|
// src/instrumentation/plugins/claude-agent-sdk-local-tool-context.ts
|
|
20069
|
-
var
|
|
20070
|
-
|
|
20071
|
-
)
|
|
20072
|
-
|
|
20073
|
-
const maybeIsoWithAsyncLocalStorage = isomorph_default;
|
|
20074
|
-
if (typeof maybeIsoWithAsyncLocalStorage.newAsyncLocalStorage === "function") {
|
|
20075
|
-
return maybeIsoWithAsyncLocalStorage.newAsyncLocalStorage();
|
|
20076
|
-
}
|
|
20077
|
-
let currentStore;
|
|
20078
|
-
return {
|
|
20079
|
-
enterWith(store) {
|
|
20080
|
-
currentStore = store;
|
|
20081
|
-
},
|
|
20082
|
-
getStore() {
|
|
20083
|
-
return currentStore;
|
|
20084
|
-
},
|
|
20085
|
-
run(store, callback) {
|
|
20086
|
-
const previousStore = currentStore;
|
|
20087
|
-
currentStore = store;
|
|
20088
|
-
try {
|
|
20089
|
-
return callback();
|
|
20090
|
-
} finally {
|
|
20091
|
-
currentStore = previousStore;
|
|
20092
|
-
}
|
|
20093
|
-
}
|
|
20094
|
-
};
|
|
20095
|
-
}
|
|
20096
|
-
var localToolContextStore = createLocalToolContextStore();
|
|
20097
|
-
var fallbackLocalToolParentResolver;
|
|
20098
|
-
function createClaudeLocalToolContext() {
|
|
20099
|
-
return {};
|
|
20100
|
-
}
|
|
20101
|
-
function runWithClaudeLocalToolContext(callback, context) {
|
|
20102
|
-
return localToolContextStore.run(
|
|
20103
|
-
context ?? createClaudeLocalToolContext(),
|
|
20104
|
-
callback
|
|
20105
|
-
);
|
|
20106
|
-
}
|
|
20107
|
-
function ensureClaudeLocalToolContext() {
|
|
20108
|
-
const existing = localToolContextStore.getStore();
|
|
20109
|
-
if (existing) {
|
|
20110
|
-
return existing;
|
|
20111
|
-
}
|
|
20112
|
-
const created = {};
|
|
20113
|
-
localToolContextStore.enterWith(created);
|
|
20114
|
-
return created;
|
|
20115
|
-
}
|
|
20116
|
-
function setClaudeLocalToolParentResolver(resolver) {
|
|
20117
|
-
fallbackLocalToolParentResolver = resolver;
|
|
20118
|
-
const context = ensureClaudeLocalToolContext();
|
|
20119
|
-
if (!context) {
|
|
20120
|
-
return;
|
|
20121
|
-
}
|
|
20122
|
-
context.resolveLocalToolParent = resolver;
|
|
20123
|
-
}
|
|
20124
|
-
function getClaudeLocalToolParentResolver() {
|
|
20125
|
-
return localToolContextStore.getStore()?.resolveLocalToolParent ?? fallbackLocalToolParentResolver;
|
|
20500
|
+
var localToolContextStore = isomorph_default.newAsyncLocalStorage();
|
|
20501
|
+
var localToolParentResolversByToolUseId = /* @__PURE__ */ new Map();
|
|
20502
|
+
function runWithClaudeLocalToolContext(callback, resolver) {
|
|
20503
|
+
return localToolContextStore.run(resolver, callback);
|
|
20126
20504
|
}
|
|
20127
|
-
function
|
|
20128
|
-
|
|
20505
|
+
function registerClaudeLocalToolParentResolver(toolUseId, resolver) {
|
|
20506
|
+
localToolParentResolversByToolUseId.set(toolUseId, resolver);
|
|
20129
20507
|
}
|
|
20130
|
-
function
|
|
20131
|
-
|
|
20132
|
-
|
|
20133
|
-
|
|
20134
|
-
const stream = result;
|
|
20135
|
-
const originalAsyncIterator = stream[Symbol.asyncIterator];
|
|
20136
|
-
if (originalAsyncIterator[LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED]) {
|
|
20137
|
-
return result;
|
|
20508
|
+
function getClaudeLocalToolParentResolver(toolUseId) {
|
|
20509
|
+
const currentResolver = localToolContextStore.getStore();
|
|
20510
|
+
if (!toolUseId) {
|
|
20511
|
+
return currentResolver;
|
|
20138
20512
|
}
|
|
20139
|
-
const
|
|
20140
|
-
|
|
20141
|
-
|
|
20142
|
-
if (!iterator || typeof iterator !== "object") {
|
|
20143
|
-
return iterator;
|
|
20144
|
-
}
|
|
20145
|
-
const patchMethod = (methodName) => {
|
|
20146
|
-
const originalMethod = Reflect.get(iterator, methodName);
|
|
20147
|
-
if (typeof originalMethod !== "function") {
|
|
20148
|
-
return;
|
|
20149
|
-
}
|
|
20150
|
-
Reflect.set(
|
|
20151
|
-
iterator,
|
|
20152
|
-
methodName,
|
|
20153
|
-
(...args) => runWithClaudeLocalToolContext(
|
|
20154
|
-
() => Reflect.apply(
|
|
20155
|
-
originalMethod,
|
|
20156
|
-
iterator,
|
|
20157
|
-
args
|
|
20158
|
-
),
|
|
20159
|
-
localToolContext
|
|
20160
|
-
)
|
|
20161
|
-
);
|
|
20162
|
-
};
|
|
20163
|
-
patchMethod("next");
|
|
20164
|
-
patchMethod("return");
|
|
20165
|
-
patchMethod("throw");
|
|
20166
|
-
return iterator;
|
|
20167
|
-
}, localToolContext);
|
|
20168
|
-
};
|
|
20169
|
-
Object.defineProperty(
|
|
20170
|
-
patchedAsyncIterator,
|
|
20171
|
-
LOCAL_TOOL_CONTEXT_ASYNC_ITERATOR_PATCHED,
|
|
20172
|
-
{
|
|
20173
|
-
configurable: false,
|
|
20174
|
-
enumerable: false,
|
|
20175
|
-
value: true,
|
|
20176
|
-
writable: false
|
|
20177
|
-
}
|
|
20178
|
-
);
|
|
20179
|
-
Reflect.set(stream, Symbol.asyncIterator, patchedAsyncIterator);
|
|
20180
|
-
return result;
|
|
20513
|
+
const registeredResolver = localToolParentResolversByToolUseId.get(toolUseId);
|
|
20514
|
+
localToolParentResolversByToolUseId.delete(toolUseId);
|
|
20515
|
+
return currentResolver ?? registeredResolver;
|
|
20181
20516
|
}
|
|
20182
20517
|
|
|
20183
20518
|
// src/instrumentation/plugins/claude-agent-sdk-local-tool-spans.ts
|
|
@@ -20203,7 +20538,7 @@ function wrapLocalClaudeToolHandler(handler, getMetadata) {
|
|
|
20203
20538
|
const metadata = getMetadata();
|
|
20204
20539
|
const rawToolName = metadata.serverName ? `mcp__${metadata.serverName}__${metadata.toolName}` : metadata.toolName;
|
|
20205
20540
|
const toolUseId = getToolUseIdFromExtra(handlerArgs[1]);
|
|
20206
|
-
const localToolParentResolver = getClaudeLocalToolParentResolver();
|
|
20541
|
+
const localToolParentResolver = getClaudeLocalToolParentResolver(toolUseId);
|
|
20207
20542
|
const spanName = metadata.serverName ? `tool: ${metadata.serverName}/${metadata.toolName}` : `tool: ${metadata.toolName}`;
|
|
20208
20543
|
const runWithResolvedParent = async () => {
|
|
20209
20544
|
const parent = toolUseId && localToolParentResolver ? await localToolParentResolver(toolUseId).catch(() => void 0) : void 0;
|
|
@@ -20730,6 +21065,7 @@ function createToolTracingHooks(resolveParentSpan, taskIdToToolUseId, toolUseToP
|
|
|
20730
21065
|
}
|
|
20731
21066
|
}
|
|
20732
21067
|
if (skipLocalToolHooks && (isLocalToolUse(input.tool_name, mcpServers) || localToolHookNames.has(input.tool_name))) {
|
|
21068
|
+
registerClaudeLocalToolParentResolver(toolUseID, resolveParentSpan);
|
|
20733
21069
|
return {};
|
|
20734
21070
|
}
|
|
20735
21071
|
const parsed = parseToolName(input.tool_name);
|
|
@@ -21440,7 +21776,7 @@ async function finalizeQuerySpan(state) {
|
|
|
21440
21776
|
}
|
|
21441
21777
|
var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
21442
21778
|
onEnable() {
|
|
21443
|
-
this.
|
|
21779
|
+
this.interceptQuery();
|
|
21444
21780
|
}
|
|
21445
21781
|
onDisable() {
|
|
21446
21782
|
for (const unsubscribe of this.unsubscribers) {
|
|
@@ -21448,211 +21784,218 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
21448
21784
|
}
|
|
21449
21785
|
this.unsubscribers = [];
|
|
21450
21786
|
}
|
|
21451
|
-
|
|
21452
|
-
const
|
|
21453
|
-
|
|
21454
|
-
|
|
21455
|
-
|
|
21456
|
-
|
|
21457
|
-
|
|
21458
|
-
|
|
21459
|
-
|
|
21460
|
-
|
|
21461
|
-
|
|
21462
|
-
|
|
21463
|
-
|
|
21464
|
-
|
|
21465
|
-
|
|
21466
|
-
|
|
21467
|
-
|
|
21468
|
-
|
|
21469
|
-
|
|
21470
|
-
|
|
21471
|
-
try {
|
|
21472
|
-
for await (const message of promptStream) {
|
|
21473
|
-
capturedPromptMessages.push(message);
|
|
21474
|
-
yield message;
|
|
21475
|
-
}
|
|
21476
|
-
} finally {
|
|
21477
|
-
resolvePromptDone?.();
|
|
21787
|
+
interceptQuery() {
|
|
21788
|
+
const startQuery = (params) => {
|
|
21789
|
+
const originalPrompt = params.prompt;
|
|
21790
|
+
const options = params.options ?? {};
|
|
21791
|
+
const promptIsAsyncIterable = isAsyncIterable(originalPrompt);
|
|
21792
|
+
let promptStarted = false;
|
|
21793
|
+
let capturedPromptMessages;
|
|
21794
|
+
let resolvePromptDone;
|
|
21795
|
+
const promptDone = new Promise((resolve) => {
|
|
21796
|
+
resolvePromptDone = resolve;
|
|
21797
|
+
});
|
|
21798
|
+
if (promptIsAsyncIterable) {
|
|
21799
|
+
capturedPromptMessages = [];
|
|
21800
|
+
const promptStream = originalPrompt;
|
|
21801
|
+
params.prompt = (async function* () {
|
|
21802
|
+
promptStarted = true;
|
|
21803
|
+
try {
|
|
21804
|
+
for await (const message of promptStream) {
|
|
21805
|
+
capturedPromptMessages.push(message);
|
|
21806
|
+
yield message;
|
|
21478
21807
|
}
|
|
21479
|
-
}
|
|
21480
|
-
|
|
21481
|
-
const span = startSpan(
|
|
21482
|
-
withSpanInstrumentationName(
|
|
21483
|
-
{
|
|
21484
|
-
name: "Claude Agent",
|
|
21485
|
-
spanAttributes: {
|
|
21486
|
-
type: "task" /* TASK */
|
|
21487
|
-
}
|
|
21488
|
-
},
|
|
21489
|
-
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
21490
|
-
)
|
|
21491
|
-
);
|
|
21492
|
-
const startTime = getCurrentUnixTimestamp();
|
|
21493
|
-
try {
|
|
21494
|
-
span.log({
|
|
21495
|
-
input: typeof originalPrompt === "string" ? originalPrompt : promptIsAsyncIterable ? void 0 : originalPrompt !== void 0 ? String(originalPrompt) : void 0,
|
|
21496
|
-
metadata: filterSerializableOptions(options)
|
|
21497
|
-
});
|
|
21498
|
-
} catch (error) {
|
|
21499
|
-
console.error("Error extracting input for Claude Agent SDK:", error);
|
|
21500
|
-
}
|
|
21501
|
-
const activeToolSpans = /* @__PURE__ */ new Map();
|
|
21502
|
-
const activeLlmSpansByParentToolUse = /* @__PURE__ */ new Map();
|
|
21503
|
-
const conversationHistoryByParentKey = /* @__PURE__ */ new Map();
|
|
21504
|
-
const subAgentSpans = /* @__PURE__ */ new Map();
|
|
21505
|
-
const endedSubAgentSpans = /* @__PURE__ */ new Set();
|
|
21506
|
-
const toolUseToParent = /* @__PURE__ */ new Map();
|
|
21507
|
-
const latestLlmParentBySubAgentToolUse = /* @__PURE__ */ new Map();
|
|
21508
|
-
const latestRootLlmParentRef = {
|
|
21509
|
-
value: void 0
|
|
21510
|
-
};
|
|
21511
|
-
const subAgentDetailsByToolUseId = /* @__PURE__ */ new Map();
|
|
21512
|
-
const taskIdToToolUseId = /* @__PURE__ */ new Map();
|
|
21513
|
-
const promptMessagesByParentKey = /* @__PURE__ */ new Map();
|
|
21514
|
-
const promptSourcePriorityByParentKey = /* @__PURE__ */ new Map();
|
|
21515
|
-
const localToolContext = createClaudeLocalToolContext();
|
|
21516
|
-
const { hasLocalToolHandlers, localToolHookNames } = prepareLocalToolHandlersInMcpServers(options.mcpServers);
|
|
21517
|
-
const skipLocalToolHooks = options[CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION] === true || hasLocalToolHandlers;
|
|
21518
|
-
const resolveToolUseParentSpan = async (toolUseID, context) => {
|
|
21519
|
-
const trackedParentToolUseId = toolUseToParent.get(toolUseID);
|
|
21520
|
-
const parentToolUseId = trackedParentToolUseId ?? (context?.agentId ? taskIdToToolUseId.get(context.agentId) ?? null : null);
|
|
21521
|
-
const parentKey = llmParentKey(parentToolUseId);
|
|
21522
|
-
const activeLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
|
|
21523
|
-
const latestLlmParent = parentToolUseId ? latestLlmParentBySubAgentToolUse.get(parentToolUseId) : latestRootLlmParentRef.value;
|
|
21524
|
-
if (!activeLlmSpan && !latestLlmParent) {
|
|
21525
|
-
await ensureActiveLlmSpanForParentToolUse(
|
|
21526
|
-
span,
|
|
21527
|
-
activeLlmSpansByParentToolUse,
|
|
21528
|
-
subAgentDetailsByToolUseId,
|
|
21529
|
-
activeToolSpans,
|
|
21530
|
-
subAgentSpans,
|
|
21531
|
-
parentToolUseId,
|
|
21532
|
-
getCurrentUnixTimestamp()
|
|
21533
|
-
);
|
|
21534
|
-
}
|
|
21535
|
-
if (parentToolUseId) {
|
|
21536
|
-
const subAgentSpan = await ensureSubAgentSpan(
|
|
21537
|
-
subAgentDetailsByToolUseId,
|
|
21538
|
-
span,
|
|
21539
|
-
activeToolSpans,
|
|
21540
|
-
subAgentSpans,
|
|
21541
|
-
parentToolUseId
|
|
21542
|
-
);
|
|
21543
|
-
return subAgentSpan.export();
|
|
21808
|
+
} finally {
|
|
21809
|
+
resolvePromptDone?.();
|
|
21544
21810
|
}
|
|
21545
|
-
|
|
21546
|
-
|
|
21547
|
-
|
|
21548
|
-
|
|
21549
|
-
|
|
21550
|
-
|
|
21551
|
-
|
|
21552
|
-
|
|
21553
|
-
|
|
21554
|
-
|
|
21555
|
-
|
|
21556
|
-
|
|
21557
|
-
|
|
21558
|
-
|
|
21559
|
-
|
|
21560
|
-
|
|
21561
|
-
|
|
21562
|
-
|
|
21563
|
-
spans.set(event, {
|
|
21564
|
-
activeLlmSpansByParentToolUse,
|
|
21565
|
-
activePartialMessageIdByParentKey: /* @__PURE__ */ new Map(),
|
|
21566
|
-
activeToolSpans,
|
|
21567
|
-
conversationHistoryByParentKey,
|
|
21568
|
-
capturedPromptMessages,
|
|
21569
|
-
currentMessageId: void 0,
|
|
21570
|
-
currentMessageStartTime: startTime,
|
|
21571
|
-
currentMessages: [],
|
|
21572
|
-
endedSubAgentSpans,
|
|
21573
|
-
finalOutputUsageMessageIds: /* @__PURE__ */ new Set(),
|
|
21574
|
-
finalResults: [],
|
|
21575
|
-
options: optionsWithHooks,
|
|
21576
|
-
originalPrompt,
|
|
21577
|
-
processing: Promise.resolve(),
|
|
21578
|
-
promptDone,
|
|
21579
|
-
promptMessagesByParentKey,
|
|
21580
|
-
promptStarted: () => promptStarted,
|
|
21581
|
-
promptSourcePriorityByParentKey,
|
|
21582
|
-
span,
|
|
21583
|
-
subAgentDetailsByToolUseId,
|
|
21584
|
-
subAgentSpans,
|
|
21585
|
-
taskIdToToolUseId,
|
|
21586
|
-
latestLlmParentBySubAgentToolUse,
|
|
21587
|
-
latestRootLlmParentRef,
|
|
21588
|
-
toolUseToParent,
|
|
21589
|
-
usageByMessageId: /* @__PURE__ */ new Map(),
|
|
21590
|
-
localToolContext
|
|
21811
|
+
})();
|
|
21812
|
+
}
|
|
21813
|
+
const span = startSpan(
|
|
21814
|
+
withSpanInstrumentationName(
|
|
21815
|
+
{
|
|
21816
|
+
name: "Claude Agent",
|
|
21817
|
+
spanAttributes: {
|
|
21818
|
+
type: "task" /* TASK */
|
|
21819
|
+
}
|
|
21820
|
+
},
|
|
21821
|
+
INSTRUMENTATION_NAMES.CLAUDE_AGENT_SDK
|
|
21822
|
+
)
|
|
21823
|
+
);
|
|
21824
|
+
const startTime = getCurrentUnixTimestamp();
|
|
21825
|
+
try {
|
|
21826
|
+
span.log({
|
|
21827
|
+
input: typeof originalPrompt === "string" ? originalPrompt : promptIsAsyncIterable ? void 0 : originalPrompt !== void 0 ? String(originalPrompt) : void 0,
|
|
21828
|
+
metadata: filterSerializableOptions(options)
|
|
21591
21829
|
});
|
|
21592
|
-
}
|
|
21593
|
-
|
|
21594
|
-
|
|
21595
|
-
|
|
21596
|
-
|
|
21597
|
-
|
|
21598
|
-
|
|
21599
|
-
|
|
21600
|
-
|
|
21601
|
-
|
|
21602
|
-
|
|
21603
|
-
|
|
21604
|
-
|
|
21605
|
-
|
|
21606
|
-
|
|
21607
|
-
|
|
21608
|
-
|
|
21609
|
-
|
|
21610
|
-
|
|
21611
|
-
|
|
21612
|
-
|
|
21613
|
-
|
|
21614
|
-
|
|
21615
|
-
|
|
21616
|
-
|
|
21617
|
-
|
|
21618
|
-
|
|
21619
|
-
|
|
21620
|
-
|
|
21621
|
-
|
|
21622
|
-
|
|
21623
|
-
|
|
21624
|
-
|
|
21625
|
-
|
|
21626
|
-
|
|
21627
|
-
})
|
|
21628
|
-
});
|
|
21629
|
-
return;
|
|
21630
|
-
}
|
|
21631
|
-
try {
|
|
21632
|
-
state.span.log({ output: eventResult });
|
|
21633
|
-
} catch (error) {
|
|
21634
|
-
console.error("Error extracting output for Claude Agent SDK:", error);
|
|
21635
|
-
} finally {
|
|
21636
|
-
state.span.end();
|
|
21637
|
-
spans.delete(event);
|
|
21830
|
+
} catch (error) {
|
|
21831
|
+
console.error("Error extracting input for Claude Agent SDK:", error);
|
|
21832
|
+
}
|
|
21833
|
+
const activeToolSpans = /* @__PURE__ */ new Map();
|
|
21834
|
+
const activeLlmSpansByParentToolUse = /* @__PURE__ */ new Map();
|
|
21835
|
+
const conversationHistoryByParentKey = /* @__PURE__ */ new Map();
|
|
21836
|
+
const subAgentSpans = /* @__PURE__ */ new Map();
|
|
21837
|
+
const endedSubAgentSpans = /* @__PURE__ */ new Set();
|
|
21838
|
+
const toolUseToParent = /* @__PURE__ */ new Map();
|
|
21839
|
+
const latestLlmParentBySubAgentToolUse = /* @__PURE__ */ new Map();
|
|
21840
|
+
const latestRootLlmParentRef = {
|
|
21841
|
+
value: void 0
|
|
21842
|
+
};
|
|
21843
|
+
const subAgentDetailsByToolUseId = /* @__PURE__ */ new Map();
|
|
21844
|
+
const taskIdToToolUseId = /* @__PURE__ */ new Map();
|
|
21845
|
+
const promptMessagesByParentKey = /* @__PURE__ */ new Map();
|
|
21846
|
+
const promptSourcePriorityByParentKey = /* @__PURE__ */ new Map();
|
|
21847
|
+
const { hasLocalToolHandlers, localToolHookNames } = prepareLocalToolHandlersInMcpServers(options.mcpServers);
|
|
21848
|
+
const skipLocalToolHooks = options[CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION] === true || hasLocalToolHandlers;
|
|
21849
|
+
const resolveToolUseParentSpan = async (toolUseID, context) => {
|
|
21850
|
+
const trackedParentToolUseId = toolUseToParent.get(toolUseID);
|
|
21851
|
+
const parentToolUseId = trackedParentToolUseId ?? (context?.agentId ? taskIdToToolUseId.get(context.agentId) ?? null : null);
|
|
21852
|
+
const parentKey = llmParentKey(parentToolUseId);
|
|
21853
|
+
const activeLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
|
|
21854
|
+
const latestLlmParent = parentToolUseId ? latestLlmParentBySubAgentToolUse.get(parentToolUseId) : latestRootLlmParentRef.value;
|
|
21855
|
+
if (!activeLlmSpan && !latestLlmParent) {
|
|
21856
|
+
await ensureActiveLlmSpanForParentToolUse(
|
|
21857
|
+
span,
|
|
21858
|
+
activeLlmSpansByParentToolUse,
|
|
21859
|
+
subAgentDetailsByToolUseId,
|
|
21860
|
+
activeToolSpans,
|
|
21861
|
+
subAgentSpans,
|
|
21862
|
+
parentToolUseId,
|
|
21863
|
+
getCurrentUnixTimestamp()
|
|
21864
|
+
);
|
|
21638
21865
|
}
|
|
21639
|
-
|
|
21640
|
-
|
|
21641
|
-
|
|
21642
|
-
|
|
21643
|
-
|
|
21866
|
+
if (parentToolUseId) {
|
|
21867
|
+
const subAgentSpan = await ensureSubAgentSpan(
|
|
21868
|
+
subAgentDetailsByToolUseId,
|
|
21869
|
+
span,
|
|
21870
|
+
activeToolSpans,
|
|
21871
|
+
subAgentSpans,
|
|
21872
|
+
parentToolUseId
|
|
21873
|
+
);
|
|
21874
|
+
return subAgentSpan.export();
|
|
21644
21875
|
}
|
|
21645
|
-
|
|
21646
|
-
|
|
21876
|
+
return span.export();
|
|
21877
|
+
};
|
|
21878
|
+
const optionsWithHooks = injectTracingHooks(
|
|
21879
|
+
options,
|
|
21880
|
+
resolveToolUseParentSpan,
|
|
21881
|
+
taskIdToToolUseId,
|
|
21882
|
+
toolUseToParent,
|
|
21883
|
+
activeToolSpans,
|
|
21884
|
+
localToolHookNames,
|
|
21885
|
+
skipLocalToolHooks,
|
|
21886
|
+
subAgentDetailsByToolUseId,
|
|
21887
|
+
subAgentSpans,
|
|
21888
|
+
endedSubAgentSpans
|
|
21889
|
+
);
|
|
21890
|
+
params.options = optionsWithHooks;
|
|
21891
|
+
return {
|
|
21892
|
+
activeLlmSpansByParentToolUse,
|
|
21893
|
+
activePartialMessageIdByParentKey: /* @__PURE__ */ new Map(),
|
|
21894
|
+
activeToolSpans,
|
|
21895
|
+
conversationHistoryByParentKey,
|
|
21896
|
+
capturedPromptMessages,
|
|
21897
|
+
currentMessageId: void 0,
|
|
21898
|
+
currentMessageStartTime: startTime,
|
|
21899
|
+
currentMessages: [],
|
|
21900
|
+
endedSubAgentSpans,
|
|
21901
|
+
finalOutputUsageMessageIds: /* @__PURE__ */ new Set(),
|
|
21902
|
+
finalResults: [],
|
|
21903
|
+
options: optionsWithHooks,
|
|
21904
|
+
originalPrompt,
|
|
21905
|
+
processing: Promise.resolve(),
|
|
21906
|
+
promptDone,
|
|
21907
|
+
promptMessagesByParentKey,
|
|
21908
|
+
promptStarted: () => promptStarted,
|
|
21909
|
+
promptSourcePriorityByParentKey,
|
|
21910
|
+
span,
|
|
21911
|
+
subAgentDetailsByToolUseId,
|
|
21912
|
+
subAgentSpans,
|
|
21913
|
+
taskIdToToolUseId,
|
|
21914
|
+
latestLlmParentBySubAgentToolUse,
|
|
21915
|
+
latestRootLlmParentRef,
|
|
21916
|
+
toolUseToParent,
|
|
21917
|
+
usageByMessageId: /* @__PURE__ */ new Map(),
|
|
21918
|
+
localToolParentResolver: resolveToolUseParentSpan
|
|
21919
|
+
};
|
|
21920
|
+
};
|
|
21921
|
+
const finishQuery = (state, result) => {
|
|
21922
|
+
if (isAsyncIterable(result)) {
|
|
21923
|
+
patchStreamIfNeeded(result, {
|
|
21924
|
+
aroundNext: (callback) => runWithClaudeLocalToolContext(
|
|
21925
|
+
callback,
|
|
21926
|
+
state.localToolParentResolver
|
|
21927
|
+
),
|
|
21928
|
+
onChunk: (message) => {
|
|
21929
|
+
maybeTrackToolUseContext(state, message);
|
|
21930
|
+
state.processing = state.processing.then(() => handleStreamMessage(state, message)).catch((error) => {
|
|
21931
|
+
console.error(
|
|
21932
|
+
"Error processing Claude Agent SDK stream chunk:",
|
|
21933
|
+
error
|
|
21934
|
+
);
|
|
21935
|
+
});
|
|
21936
|
+
},
|
|
21937
|
+
onComplete: () => state.processing.then(() => finalizeQuerySpan(state)),
|
|
21938
|
+
onError: (error) => state.processing.then(() => {
|
|
21939
|
+
state.span.log({ error: error.message });
|
|
21940
|
+
}).then(() => finalizeQuerySpan(state))
|
|
21647
21941
|
});
|
|
21942
|
+
return;
|
|
21943
|
+
}
|
|
21944
|
+
try {
|
|
21945
|
+
state.span.log({ output: result });
|
|
21946
|
+
} catch (error) {
|
|
21947
|
+
console.error("Error extracting output for Claude Agent SDK:", error);
|
|
21948
|
+
} finally {
|
|
21648
21949
|
state.span.end();
|
|
21649
|
-
spans.delete(event);
|
|
21650
21950
|
}
|
|
21651
21951
|
};
|
|
21652
|
-
|
|
21653
|
-
|
|
21654
|
-
|
|
21655
|
-
|
|
21952
|
+
this.unsubscribers.push(
|
|
21953
|
+
claudeAgentSDKChannels.query.intercept((target, thisArg, args) => {
|
|
21954
|
+
let state;
|
|
21955
|
+
try {
|
|
21956
|
+
args[0] ??= {};
|
|
21957
|
+
state = startQuery(args[0]);
|
|
21958
|
+
} catch (error) {
|
|
21959
|
+
debugLogger.error(
|
|
21960
|
+
"Error starting Claude Agent SDK instrumentation:",
|
|
21961
|
+
error
|
|
21962
|
+
);
|
|
21963
|
+
}
|
|
21964
|
+
const invokeTarget = () => Reflect.apply(target, thisArg, args);
|
|
21965
|
+
try {
|
|
21966
|
+
const result = state ? runWithClaudeLocalToolContext(
|
|
21967
|
+
invokeTarget,
|
|
21968
|
+
state.localToolParentResolver
|
|
21969
|
+
) : invokeTarget();
|
|
21970
|
+
if (state) {
|
|
21971
|
+
try {
|
|
21972
|
+
finishQuery(state, result);
|
|
21973
|
+
} catch (error) {
|
|
21974
|
+
debugLogger.error(
|
|
21975
|
+
"Error finalizing Claude Agent SDK instrumentation:",
|
|
21976
|
+
error
|
|
21977
|
+
);
|
|
21978
|
+
}
|
|
21979
|
+
}
|
|
21980
|
+
return result;
|
|
21981
|
+
} catch (error) {
|
|
21982
|
+
if (state) {
|
|
21983
|
+
try {
|
|
21984
|
+
state.span.log({
|
|
21985
|
+
error: error instanceof Error ? error.message : String(error)
|
|
21986
|
+
});
|
|
21987
|
+
state.span.end();
|
|
21988
|
+
} catch (instrumentationError) {
|
|
21989
|
+
debugLogger.error(
|
|
21990
|
+
"Error handling Claude Agent SDK instrumentation failure:",
|
|
21991
|
+
instrumentationError
|
|
21992
|
+
);
|
|
21993
|
+
}
|
|
21994
|
+
}
|
|
21995
|
+
throw error;
|
|
21996
|
+
}
|
|
21997
|
+
})
|
|
21998
|
+
);
|
|
21656
21999
|
}
|
|
21657
22000
|
};
|
|
21658
22001
|
|
|
@@ -25766,7 +26109,7 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
25766
26109
|
span,
|
|
25767
26110
|
() => originalMethod.apply(resultLike, args2)
|
|
25768
26111
|
);
|
|
25769
|
-
if (!
|
|
26112
|
+
if (!isAsyncIterable3(stream)) {
|
|
25770
26113
|
return stream;
|
|
25771
26114
|
}
|
|
25772
26115
|
return wrapAsyncIterableWithSpan({
|
|
@@ -25961,7 +26304,7 @@ function wrapAsyncIterableWithSpan(args) {
|
|
|
25961
26304
|
}
|
|
25962
26305
|
};
|
|
25963
26306
|
}
|
|
25964
|
-
function
|
|
26307
|
+
function isAsyncIterable3(value) {
|
|
25965
26308
|
return !!value && (typeof value === "object" || typeof value === "function") && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
25966
26309
|
}
|
|
25967
26310
|
function normalizeError(error) {
|
|
@@ -26839,7 +27182,7 @@ function patchOpenRouterCallModelResult2(args) {
|
|
|
26839
27182
|
span,
|
|
26840
27183
|
() => originalMethod.apply(resultLike, args2)
|
|
26841
27184
|
);
|
|
26842
|
-
if (!
|
|
27185
|
+
if (!isAsyncIterable4(stream)) {
|
|
26843
27186
|
return stream;
|
|
26844
27187
|
}
|
|
26845
27188
|
return wrapAsyncIterableWithSpan2({
|
|
@@ -27034,7 +27377,7 @@ function wrapAsyncIterableWithSpan2(args) {
|
|
|
27034
27377
|
}
|
|
27035
27378
|
};
|
|
27036
27379
|
}
|
|
27037
|
-
function
|
|
27380
|
+
function isAsyncIterable4(value) {
|
|
27038
27381
|
return !!value && (typeof value === "object" || typeof value === "function") && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
27039
27382
|
}
|
|
27040
27383
|
function normalizeError2(error) {
|
|
@@ -33026,12 +33369,14 @@ function getMetricsFromResponse(response) {
|
|
|
33026
33369
|
continue;
|
|
33027
33370
|
}
|
|
33028
33371
|
const inputTokenDetails = usageMetadata.input_token_details;
|
|
33372
|
+
const outputTokenDetails = usageMetadata.output_token_details;
|
|
33029
33373
|
return normalizeTokenMetrics({
|
|
33030
33374
|
total_tokens: usageMetadata.total_tokens,
|
|
33031
33375
|
prompt_tokens: usageMetadata.input_tokens,
|
|
33032
33376
|
completion_tokens: usageMetadata.output_tokens,
|
|
33033
33377
|
prompt_cache_creation_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_creation : void 0,
|
|
33034
|
-
prompt_cached_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_read : void 0
|
|
33378
|
+
prompt_cached_tokens: isRecord(inputTokenDetails) ? inputTokenDetails.cache_read : void 0,
|
|
33379
|
+
completion_reasoning_tokens: isRecord(outputTokenDetails) ? outputTokenDetails.reasoning : void 0
|
|
33035
33380
|
});
|
|
33036
33381
|
}
|
|
33037
33382
|
const llmOutput = response.llmOutput || {};
|
|
@@ -33618,6 +33963,9 @@ var piCodingAgentChannels = defineChannels(
|
|
|
33618
33963
|
// src/instrumentation/plugins/pi-coding-agent-plugin.ts
|
|
33619
33964
|
var piAgentPatchStates = /* @__PURE__ */ new WeakMap();
|
|
33620
33965
|
var piAgentEventSubscriptions = /* @__PURE__ */ new WeakSet();
|
|
33966
|
+
var PI_TOOL_EXECUTE_WRAPPED = /* @__PURE__ */ Symbol.for(
|
|
33967
|
+
"braintrust.pi_coding_agent.tool_execute_wrapped"
|
|
33968
|
+
);
|
|
33621
33969
|
var piPromptContextStore;
|
|
33622
33970
|
var PiCodingAgentPlugin = class extends BasePlugin {
|
|
33623
33971
|
activePromptStates = /* @__PURE__ */ new Set();
|
|
@@ -33698,6 +34046,7 @@ function startPiPromptRun(event, onFinalize) {
|
|
|
33698
34046
|
return void 0;
|
|
33699
34047
|
}
|
|
33700
34048
|
installPiAgentInstrumentation(agent);
|
|
34049
|
+
wrapPiToolExecutors(agent.state?.tools);
|
|
33701
34050
|
const metadata = {
|
|
33702
34051
|
...extractSessionMetadata(session),
|
|
33703
34052
|
...extractPromptOptionsMetadata(event.arguments[1]),
|
|
@@ -33794,6 +34143,7 @@ function makeInstrumentedStreamFn(agent, originalStreamFn) {
|
|
|
33794
34143
|
if (!state || state.agent !== agent || state.finalized) {
|
|
33795
34144
|
return invokeOriginal();
|
|
33796
34145
|
}
|
|
34146
|
+
wrapPiToolExecutors(context.tools);
|
|
33797
34147
|
const llmState = await startPiLlmSpan(state, model, context, options);
|
|
33798
34148
|
try {
|
|
33799
34149
|
const stream = await runWithAutoInstrumentationSuppressed(invokeOriginal);
|
|
@@ -33804,6 +34154,33 @@ function makeInstrumentedStreamFn(agent, originalStreamFn) {
|
|
|
33804
34154
|
}
|
|
33805
34155
|
};
|
|
33806
34156
|
}
|
|
34157
|
+
function wrapPiToolExecutors(tools) {
|
|
34158
|
+
if (!tools) {
|
|
34159
|
+
return;
|
|
34160
|
+
}
|
|
34161
|
+
for (const tool of tools) {
|
|
34162
|
+
try {
|
|
34163
|
+
const execute = tool.execute;
|
|
34164
|
+
if (typeof execute !== "function" || execute[PI_TOOL_EXECUTE_WRAPPED]) {
|
|
34165
|
+
continue;
|
|
34166
|
+
}
|
|
34167
|
+
const wrappedExecute = function(...args) {
|
|
34168
|
+
return runWithAutoInstrumentationAllowed(
|
|
34169
|
+
() => Reflect.apply(execute, this, args)
|
|
34170
|
+
);
|
|
34171
|
+
};
|
|
34172
|
+
Object.defineProperty(wrappedExecute, PI_TOOL_EXECUTE_WRAPPED, {
|
|
34173
|
+
configurable: false,
|
|
34174
|
+
enumerable: false,
|
|
34175
|
+
value: true,
|
|
34176
|
+
writable: false
|
|
34177
|
+
});
|
|
34178
|
+
tool.execute = wrappedExecute;
|
|
34179
|
+
} catch (error) {
|
|
34180
|
+
logInstrumentationError4("Pi Coding Agent tool wrapping", error);
|
|
34181
|
+
}
|
|
34182
|
+
}
|
|
34183
|
+
}
|
|
33807
34184
|
async function startPiLlmSpan(state, model, context, options) {
|
|
33808
34185
|
const metadata = {
|
|
33809
34186
|
...extractModelMetadata2(model),
|
|
@@ -33975,35 +34352,26 @@ async function startPiToolSpan(state, event) {
|
|
|
33975
34352
|
if (!event.toolCallId || state.activeToolSpans.has(event.toolCallId)) {
|
|
33976
34353
|
return;
|
|
33977
34354
|
}
|
|
33978
|
-
const restoreAutoInstrumentation = enterAutoInstrumentationAllowed();
|
|
33979
34355
|
const metadata = {
|
|
33980
34356
|
"gen_ai.tool.call.id": event.toolCallId,
|
|
33981
34357
|
"gen_ai.tool.name": event.toolName,
|
|
33982
34358
|
"pi_coding_agent.tool.name": event.toolName
|
|
33983
34359
|
};
|
|
33984
|
-
|
|
33985
|
-
|
|
33986
|
-
|
|
33987
|
-
{
|
|
33988
|
-
|
|
33989
|
-
|
|
33990
|
-
metadata
|
|
33991
|
-
},
|
|
33992
|
-
name: event.toolName || "tool",
|
|
33993
|
-
parent: await state.span.export(),
|
|
33994
|
-
spanAttributes: { type: "tool" /* TOOL */ }
|
|
34360
|
+
const span = startSpan(
|
|
34361
|
+
withSpanInstrumentationName(
|
|
34362
|
+
{
|
|
34363
|
+
event: {
|
|
34364
|
+
input: event.args,
|
|
34365
|
+
metadata
|
|
33995
34366
|
},
|
|
33996
|
-
|
|
33997
|
-
|
|
33998
|
-
|
|
33999
|
-
|
|
34000
|
-
|
|
34001
|
-
|
|
34002
|
-
|
|
34003
|
-
|
|
34004
|
-
restoreAutoInstrumentation();
|
|
34005
|
-
throw error;
|
|
34006
|
-
}
|
|
34367
|
+
name: event.toolName || "tool",
|
|
34368
|
+
parent: await state.span.export(),
|
|
34369
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
34370
|
+
},
|
|
34371
|
+
INSTRUMENTATION_NAMES.PI_CODING_AGENT
|
|
34372
|
+
)
|
|
34373
|
+
);
|
|
34374
|
+
state.activeToolSpans.set(event.toolCallId, { span });
|
|
34007
34375
|
}
|
|
34008
34376
|
function finishPiToolSpan(state, event) {
|
|
34009
34377
|
const toolState = state.activeToolSpans.get(event.toolCallId);
|
|
@@ -34024,11 +34392,7 @@ function finishPiToolSpan(state, event) {
|
|
|
34024
34392
|
output: event.result
|
|
34025
34393
|
});
|
|
34026
34394
|
} finally {
|
|
34027
|
-
|
|
34028
|
-
toolState.span.end();
|
|
34029
|
-
} finally {
|
|
34030
|
-
toolState.restoreAutoInstrumentation?.();
|
|
34031
|
-
}
|
|
34395
|
+
toolState.span.end();
|
|
34032
34396
|
}
|
|
34033
34397
|
}
|
|
34034
34398
|
function finishPiPromptRun(state, error) {
|
|
@@ -34078,10 +34442,10 @@ function finishPiLlmSpan(promptState, llmState, message, error) {
|
|
|
34078
34442
|
...cleanMetrics5(llmState.metrics),
|
|
34079
34443
|
...buildDurationMetrics3(llmState.startTime)
|
|
34080
34444
|
};
|
|
34081
|
-
const
|
|
34082
|
-
if (Object.keys(
|
|
34445
|
+
const usageMetrics2 = extractUsageMetrics2(message?.usage);
|
|
34446
|
+
if (Object.keys(usageMetrics2).length > 0) {
|
|
34083
34447
|
promptState.collectedLlmUsageMetrics = true;
|
|
34084
|
-
addMetrics(promptState.metrics,
|
|
34448
|
+
addMetrics(promptState.metrics, usageMetrics2);
|
|
34085
34449
|
}
|
|
34086
34450
|
try {
|
|
34087
34451
|
safeLog4(llmState.span, {
|
|
@@ -34099,14 +34463,10 @@ function finishPiLlmSpan(promptState, llmState, message, error) {
|
|
|
34099
34463
|
}
|
|
34100
34464
|
function finishOpenToolSpans(state, error) {
|
|
34101
34465
|
for (const [, toolState] of state.activeToolSpans) {
|
|
34102
|
-
|
|
34103
|
-
|
|
34104
|
-
|
|
34105
|
-
|
|
34106
|
-
toolState.span.end();
|
|
34107
|
-
} finally {
|
|
34108
|
-
toolState.restoreAutoInstrumentation?.();
|
|
34109
|
-
}
|
|
34466
|
+
safeLog4(toolState.span, {
|
|
34467
|
+
error: error ? toLoggedError(error) : "Pi tool did not complete"
|
|
34468
|
+
});
|
|
34469
|
+
toolState.span.end();
|
|
34110
34470
|
}
|
|
34111
34471
|
state.activeToolSpans.clear();
|
|
34112
34472
|
}
|
|
@@ -34425,12 +34785,12 @@ var MAX_STRANDS_STRING_ATTACHMENT_CACHE_ENTRIES = 32;
|
|
|
34425
34785
|
var StrandsAgentSDKPlugin = class extends BasePlugin {
|
|
34426
34786
|
activeChildParents = /* @__PURE__ */ new WeakMap();
|
|
34427
34787
|
onEnable() {
|
|
34428
|
-
this.
|
|
34429
|
-
this.
|
|
34788
|
+
this.interceptAgentStream();
|
|
34789
|
+
this.interceptMultiAgentStream(
|
|
34430
34790
|
strandsAgentSDKChannels.graphStream,
|
|
34431
34791
|
"Graph.stream"
|
|
34432
34792
|
);
|
|
34433
|
-
this.
|
|
34793
|
+
this.interceptMultiAgentStream(
|
|
34434
34794
|
strandsAgentSDKChannels.swarmStream,
|
|
34435
34795
|
"Swarm.stream"
|
|
34436
34796
|
);
|
|
@@ -34441,122 +34801,92 @@ var StrandsAgentSDKPlugin = class extends BasePlugin {
|
|
|
34441
34801
|
}
|
|
34442
34802
|
this.unsubscribers = [];
|
|
34443
34803
|
}
|
|
34444
|
-
|
|
34445
|
-
|
|
34446
|
-
|
|
34447
|
-
|
|
34448
|
-
|
|
34449
|
-
|
|
34450
|
-
|
|
34451
|
-
|
|
34452
|
-
|
|
34453
|
-
|
|
34454
|
-
|
|
34455
|
-
|
|
34456
|
-
|
|
34457
|
-
|
|
34458
|
-
|
|
34459
|
-
|
|
34460
|
-
const result = event.result;
|
|
34461
|
-
if (isAsyncIterable(result)) {
|
|
34462
|
-
patchStreamIfNeeded(result, {
|
|
34463
|
-
aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
|
|
34464
|
-
onChunk: (chunk) => handleAgentStreamEvent(state, chunk),
|
|
34465
|
-
onComplete: () => {
|
|
34466
|
-
finalizeAgentStream(state);
|
|
34467
|
-
states.delete(event);
|
|
34468
|
-
},
|
|
34469
|
-
onError: (error) => {
|
|
34470
|
-
finalizeAgentStream(state, error);
|
|
34471
|
-
states.delete(event);
|
|
34472
|
-
}
|
|
34473
|
-
});
|
|
34474
|
-
return;
|
|
34475
|
-
}
|
|
34476
|
-
finalizeAgentStream(state, void 0, result);
|
|
34477
|
-
states.delete(event);
|
|
34478
|
-
},
|
|
34479
|
-
error: (event) => {
|
|
34480
|
-
const state = states.get(event);
|
|
34481
|
-
if (!state || !event.error) {
|
|
34482
|
-
return;
|
|
34483
|
-
}
|
|
34484
|
-
finalizeAgentStream(state, event.error);
|
|
34485
|
-
states.delete(event);
|
|
34486
|
-
}
|
|
34487
|
-
};
|
|
34488
|
-
channel2.subscribe(handlers);
|
|
34489
|
-
this.unsubscribers.push(() => {
|
|
34490
|
-
unbindAutoInstrumentationSuppression?.();
|
|
34491
|
-
channel2.unsubscribe(handlers);
|
|
34492
|
-
});
|
|
34804
|
+
interceptAgentStream() {
|
|
34805
|
+
this.unsubscribers.push(
|
|
34806
|
+
strandsAgentSDKChannels.agentStream.intercept(
|
|
34807
|
+
(target, thisArg, args, additional) => instrumentStrandsStreamInvocation({
|
|
34808
|
+
finalize: finalizeAgentStream,
|
|
34809
|
+
handleChunk: handleAgentStreamEvent,
|
|
34810
|
+
invoke: () => Reflect.apply(target, thisArg, args),
|
|
34811
|
+
name: "Strands Agent SDK",
|
|
34812
|
+
start: () => startAgentStream(
|
|
34813
|
+
args[0],
|
|
34814
|
+
extractAgent(additional.agent, thisArg),
|
|
34815
|
+
this.activeChildParents
|
|
34816
|
+
)
|
|
34817
|
+
})
|
|
34818
|
+
)
|
|
34819
|
+
);
|
|
34493
34820
|
}
|
|
34494
|
-
|
|
34495
|
-
|
|
34496
|
-
|
|
34497
|
-
|
|
34498
|
-
|
|
34499
|
-
|
|
34500
|
-
|
|
34501
|
-
|
|
34502
|
-
|
|
34503
|
-
|
|
34504
|
-
|
|
34505
|
-
|
|
34506
|
-
|
|
34507
|
-
|
|
34508
|
-
|
|
34509
|
-
|
|
34510
|
-
|
|
34511
|
-
|
|
34512
|
-
|
|
34513
|
-
}
|
|
34514
|
-
|
|
34515
|
-
|
|
34516
|
-
|
|
34517
|
-
|
|
34518
|
-
|
|
34519
|
-
|
|
34520
|
-
|
|
34521
|
-
|
|
34522
|
-
|
|
34523
|
-
|
|
34524
|
-
|
|
34525
|
-
|
|
34526
|
-
|
|
34527
|
-
|
|
34528
|
-
|
|
34529
|
-
|
|
34530
|
-
|
|
34531
|
-
|
|
34532
|
-
|
|
34533
|
-
|
|
34534
|
-
|
|
34535
|
-
|
|
34536
|
-
this.activeChildParents,
|
|
34537
|
-
void 0,
|
|
34538
|
-
result
|
|
34821
|
+
interceptMultiAgentStream(channel2, operation) {
|
|
34822
|
+
this.unsubscribers.push(
|
|
34823
|
+
channel2.intercept(
|
|
34824
|
+
(target, thisArg, args, additional) => instrumentStrandsStreamInvocation({
|
|
34825
|
+
finalize: (state, error, output) => finalizeMultiAgentStream(
|
|
34826
|
+
state,
|
|
34827
|
+
this.activeChildParents,
|
|
34828
|
+
error,
|
|
34829
|
+
output
|
|
34830
|
+
),
|
|
34831
|
+
handleChunk: (state, chunk) => handleMultiAgentStreamEvent(state, chunk, this.activeChildParents),
|
|
34832
|
+
invoke: () => Reflect.apply(target, thisArg, args),
|
|
34833
|
+
name: "Strands multi-agent",
|
|
34834
|
+
start: () => startMultiAgentStream(
|
|
34835
|
+
args[0],
|
|
34836
|
+
extractOrchestrator(additional.orchestrator, thisArg),
|
|
34837
|
+
operation,
|
|
34838
|
+
this.activeChildParents
|
|
34839
|
+
)
|
|
34840
|
+
})
|
|
34841
|
+
)
|
|
34842
|
+
);
|
|
34843
|
+
}
|
|
34844
|
+
};
|
|
34845
|
+
function instrumentStrandsStreamInvocation(options) {
|
|
34846
|
+
let state;
|
|
34847
|
+
try {
|
|
34848
|
+
state = options.start();
|
|
34849
|
+
} catch (error) {
|
|
34850
|
+
debugLogger.error(`Error starting ${options.name} instrumentation:`, error);
|
|
34851
|
+
}
|
|
34852
|
+
let result;
|
|
34853
|
+
try {
|
|
34854
|
+
result = runWithAutoInstrumentationSuppressed(options.invoke);
|
|
34855
|
+
} catch (error) {
|
|
34856
|
+
if (state) {
|
|
34857
|
+
try {
|
|
34858
|
+
options.finalize(state, error);
|
|
34859
|
+
} catch (instrumentationError) {
|
|
34860
|
+
debugLogger.error(
|
|
34861
|
+
`Error handling ${options.name} instrumentation failure:`,
|
|
34862
|
+
instrumentationError
|
|
34539
34863
|
);
|
|
34540
|
-
states.delete(event);
|
|
34541
|
-
},
|
|
34542
|
-
error: (event) => {
|
|
34543
|
-
const state = states.get(event);
|
|
34544
|
-
if (!state || !event.error) {
|
|
34545
|
-
return;
|
|
34546
|
-
}
|
|
34547
|
-
finalizeMultiAgentStream(state, this.activeChildParents, event.error);
|
|
34548
|
-
states.delete(event);
|
|
34549
34864
|
}
|
|
34550
|
-
}
|
|
34551
|
-
|
|
34552
|
-
this.unsubscribers.push(() => {
|
|
34553
|
-
unbindAutoInstrumentationSuppression?.();
|
|
34554
|
-
tracingChannel.unsubscribe(handlers);
|
|
34555
|
-
});
|
|
34865
|
+
}
|
|
34866
|
+
throw error;
|
|
34556
34867
|
}
|
|
34557
|
-
|
|
34558
|
-
|
|
34559
|
-
|
|
34868
|
+
if (state) {
|
|
34869
|
+
try {
|
|
34870
|
+
if (isAsyncIterable(result)) {
|
|
34871
|
+
patchStreamIfNeeded(result, {
|
|
34872
|
+
aroundNext: (callback) => runWithAutoInstrumentationSuppressed(callback),
|
|
34873
|
+
onChunk: (chunk) => options.handleChunk(state, chunk),
|
|
34874
|
+
onComplete: () => options.finalize(state),
|
|
34875
|
+
onError: (error) => options.finalize(state, error)
|
|
34876
|
+
});
|
|
34877
|
+
} else {
|
|
34878
|
+
options.finalize(state, void 0, result);
|
|
34879
|
+
}
|
|
34880
|
+
} catch (error) {
|
|
34881
|
+
debugLogger.error(
|
|
34882
|
+
`Error finalizing ${options.name} instrumentation:`,
|
|
34883
|
+
error
|
|
34884
|
+
);
|
|
34885
|
+
}
|
|
34886
|
+
}
|
|
34887
|
+
return result;
|
|
34888
|
+
}
|
|
34889
|
+
function startAgentStream(input, agent, activeChildParents) {
|
|
34560
34890
|
const model = agent?.model;
|
|
34561
34891
|
const metadata = {
|
|
34562
34892
|
...extractAgentMetadata2(agent),
|
|
@@ -34566,17 +34896,14 @@ function startAgentStream(event, activeChildParents) {
|
|
|
34566
34896
|
};
|
|
34567
34897
|
const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
|
|
34568
34898
|
const attachmentCache = createStrandsAttachmentCache();
|
|
34569
|
-
const
|
|
34570
|
-
event.arguments[0],
|
|
34571
|
-
attachmentCache
|
|
34572
|
-
);
|
|
34899
|
+
const processedInput = processStrandsInputAttachments(input, attachmentCache);
|
|
34573
34900
|
const span = parentSpan ? withCurrent(
|
|
34574
34901
|
parentSpan,
|
|
34575
34902
|
() => startSpan(
|
|
34576
34903
|
withSpanInstrumentationName(
|
|
34577
34904
|
{
|
|
34578
34905
|
event: {
|
|
34579
|
-
input,
|
|
34906
|
+
input: processedInput,
|
|
34580
34907
|
metadata
|
|
34581
34908
|
},
|
|
34582
34909
|
name: formatAgentSpanName(agent),
|
|
@@ -34589,7 +34916,7 @@ function startAgentStream(event, activeChildParents) {
|
|
|
34589
34916
|
withSpanInstrumentationName(
|
|
34590
34917
|
{
|
|
34591
34918
|
event: {
|
|
34592
|
-
input,
|
|
34919
|
+
input: processedInput,
|
|
34593
34920
|
metadata
|
|
34594
34921
|
},
|
|
34595
34922
|
name: formatAgentSpanName(agent),
|
|
@@ -34607,22 +34934,21 @@ function startAgentStream(event, activeChildParents) {
|
|
|
34607
34934
|
startTime: getCurrentUnixTimestamp()
|
|
34608
34935
|
};
|
|
34609
34936
|
}
|
|
34610
|
-
function startMultiAgentStream(
|
|
34611
|
-
const orchestrator = extractOrchestrator(event);
|
|
34937
|
+
function startMultiAgentStream(input, orchestrator, operation, activeChildParents) {
|
|
34612
34938
|
const metadata = {
|
|
34613
34939
|
"strands.operation": operation,
|
|
34614
34940
|
provider: "strands",
|
|
34615
34941
|
...orchestrator?.id ? { "strands.orchestrator.id": orchestrator.id } : {}
|
|
34616
34942
|
};
|
|
34617
34943
|
const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
|
|
34618
|
-
const
|
|
34944
|
+
const processedInput = processStrandsInputAttachments(input);
|
|
34619
34945
|
const span = parentSpan ? withCurrent(
|
|
34620
34946
|
parentSpan,
|
|
34621
34947
|
() => startSpan(
|
|
34622
34948
|
withSpanInstrumentationName(
|
|
34623
34949
|
{
|
|
34624
34950
|
event: {
|
|
34625
|
-
input,
|
|
34951
|
+
input: processedInput,
|
|
34626
34952
|
metadata
|
|
34627
34953
|
},
|
|
34628
34954
|
name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
|
|
@@ -34635,7 +34961,7 @@ function startMultiAgentStream(event, operation, activeChildParents) {
|
|
|
34635
34961
|
withSpanInstrumentationName(
|
|
34636
34962
|
{
|
|
34637
34963
|
event: {
|
|
34638
|
-
input,
|
|
34964
|
+
input: processedInput,
|
|
34639
34965
|
metadata
|
|
34640
34966
|
},
|
|
34641
34967
|
name: operation === "Graph.stream" ? "Strands Graph" : "Strands Swarm",
|
|
@@ -35006,12 +35332,12 @@ function finalizeMultiAgentStream(state, activeChildParents, error, output) {
|
|
|
35006
35332
|
});
|
|
35007
35333
|
state.span.end();
|
|
35008
35334
|
}
|
|
35009
|
-
function extractAgent(
|
|
35010
|
-
const candidate =
|
|
35335
|
+
function extractAgent(agent, self) {
|
|
35336
|
+
const candidate = agent ?? self;
|
|
35011
35337
|
return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
|
|
35012
35338
|
}
|
|
35013
|
-
function extractOrchestrator(
|
|
35014
|
-
const candidate =
|
|
35339
|
+
function extractOrchestrator(orchestrator, self) {
|
|
35340
|
+
const candidate = orchestrator ?? self;
|
|
35015
35341
|
return isObject(candidate) && typeof candidate.stream === "function" ? candidate : void 0;
|
|
35016
35342
|
}
|
|
35017
35343
|
function extractAgentMetadata2(agent) {
|
|
@@ -37527,6 +37853,13 @@ function wrapAISDK(aiSDK, options = {}) {
|
|
|
37527
37853
|
switch (prop) {
|
|
37528
37854
|
case "generateText":
|
|
37529
37855
|
return wrapGenerateText(typedAISDK.generateText, options, typedAISDK);
|
|
37856
|
+
case "generateImage":
|
|
37857
|
+
case "experimental_generateImage":
|
|
37858
|
+
return typeof original === "function" ? wrapGenerateImage(
|
|
37859
|
+
original,
|
|
37860
|
+
options,
|
|
37861
|
+
typedAISDK
|
|
37862
|
+
) : original;
|
|
37530
37863
|
case "streamText":
|
|
37531
37864
|
return wrapStreamText(typedAISDK.streamText, options, typedAISDK);
|
|
37532
37865
|
case "generateObject":
|
|
@@ -37763,6 +38096,32 @@ var wrapGenerateObject = (generateObject, options = {}, aiSDK) => {
|
|
|
37763
38096
|
options
|
|
37764
38097
|
);
|
|
37765
38098
|
};
|
|
38099
|
+
var wrapGenerateImage = (generateImage, options = {}, aiSDK) => {
|
|
38100
|
+
return makeGenerateImageWrapper(generateImage, { aiSDK }, options);
|
|
38101
|
+
};
|
|
38102
|
+
var makeGenerateImageWrapper = (generateImage, contextOptions = {}, options = {}) => {
|
|
38103
|
+
const wrapper = async function(allParams) {
|
|
38104
|
+
const { span_info, ...params } = allParams;
|
|
38105
|
+
const tracedParams = { ...params };
|
|
38106
|
+
return aiSDKChannels.generateImage.tracePromise(
|
|
38107
|
+
() => generateImage(tracedParams),
|
|
38108
|
+
createAISDKChannelContext(tracedParams, {
|
|
38109
|
+
aiSDK: contextOptions.aiSDK,
|
|
38110
|
+
denyOutputPaths: options.denyOutputPaths,
|
|
38111
|
+
self: contextOptions.self,
|
|
38112
|
+
span_info: mergeSpanInfo(span_info, {
|
|
38113
|
+
name: "generateImage",
|
|
38114
|
+
spanType: contextOptions.spanType
|
|
38115
|
+
})
|
|
38116
|
+
})
|
|
38117
|
+
);
|
|
38118
|
+
};
|
|
38119
|
+
Object.defineProperty(wrapper, "name", {
|
|
38120
|
+
value: "generateImage",
|
|
38121
|
+
writable: false
|
|
38122
|
+
});
|
|
38123
|
+
return wrapper;
|
|
38124
|
+
};
|
|
37766
38125
|
var makeEmbedWrapper = (channel2, name, embed, contextOptions = {}, options = {}) => {
|
|
37767
38126
|
const wrapper = async function(allParams) {
|
|
37768
38127
|
const { span_info, ...params } = allParams;
|
|
@@ -38513,7 +38872,7 @@ function braintrustEveHook(options) {
|
|
|
38513
38872
|
}
|
|
38514
38873
|
};
|
|
38515
38874
|
}
|
|
38516
|
-
function
|
|
38875
|
+
function createLegacyEveInstrumentation(options) {
|
|
38517
38876
|
const state = options.defineState(EVE_TRACE_STATE_KEY, emptyEveTraceState);
|
|
38518
38877
|
return {
|
|
38519
38878
|
events: {
|
|
@@ -39901,8 +40260,10 @@ function capturedModelInput(modelInput) {
|
|
|
39901
40260
|
const value = [];
|
|
39902
40261
|
if (typeof instructions === "string") {
|
|
39903
40262
|
value.push({ content: instructions, role: "system" });
|
|
39904
|
-
} else if (instructions) {
|
|
40263
|
+
} else if (Array.isArray(instructions)) {
|
|
39905
40264
|
value.push(...instructions.map(capturedEveModelMessage));
|
|
40265
|
+
} else if (instructions) {
|
|
40266
|
+
value.push(capturedEveModelMessage(instructions));
|
|
39906
40267
|
}
|
|
39907
40268
|
value.push(...messages.map(capturedEveModelMessage));
|
|
39908
40269
|
try {
|
|
@@ -40146,6 +40507,464 @@ async function deterministicEveId(...parts) {
|
|
|
40146
40507
|
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
40147
40508
|
}
|
|
40148
40509
|
|
|
40510
|
+
// src/instrumentation/plugins/eve-provider.ts
|
|
40511
|
+
var MAX_EVE_PROVIDER_CACHE_ENTRIES = 1e4;
|
|
40512
|
+
function createEveInstrumentationProvider(options = {}) {
|
|
40513
|
+
const bridge = new EveProviderBridge(options.metadata);
|
|
40514
|
+
return {
|
|
40515
|
+
capture: "content",
|
|
40516
|
+
events: {
|
|
40517
|
+
"action.completed": (event, context) => bridge.handleActionTerminal(event, context),
|
|
40518
|
+
"action.failed": (event, context) => bridge.handleActionTerminal(event, context),
|
|
40519
|
+
"action.started": (event, context) => bridge.handleActionStarted(event, context),
|
|
40520
|
+
"model.call.completed": (event, context) => bridge.handleModelTerminal(event, context),
|
|
40521
|
+
"model.call.failed": (event, context) => bridge.handleModelTerminal(event, context),
|
|
40522
|
+
"model.call.started": (event, context) => bridge.handleModelStarted(event, context),
|
|
40523
|
+
"step.attempt.completed": (event) => bridge.handleStepAttemptTerminal(event),
|
|
40524
|
+
"step.attempt.failed": (event) => bridge.handleStepAttemptTerminal(event),
|
|
40525
|
+
"turn.cancelled": (event, context) => bridge.handleTurnTerminal(event, context),
|
|
40526
|
+
"turn.completed": (event, context) => bridge.handleTurnTerminal(event, context),
|
|
40527
|
+
"turn.failed": (event, context) => bridge.handleTurnTerminal(event, context),
|
|
40528
|
+
"turn.started": (event, context) => bridge.handleTurnStarted(event, context)
|
|
40529
|
+
},
|
|
40530
|
+
flush,
|
|
40531
|
+
setup: options.setup
|
|
40532
|
+
};
|
|
40533
|
+
}
|
|
40534
|
+
var EveProviderBridge = class {
|
|
40535
|
+
constructor(metadata) {
|
|
40536
|
+
this.metadata = metadata;
|
|
40537
|
+
}
|
|
40538
|
+
metadata;
|
|
40539
|
+
activeActions = /* @__PURE__ */ new Map();
|
|
40540
|
+
activeModels = /* @__PURE__ */ new Map();
|
|
40541
|
+
modelsByAttempt = /* @__PURE__ */ new Map();
|
|
40542
|
+
settledOperations = new LRUCache({
|
|
40543
|
+
max: MAX_EVE_PROVIDER_CACHE_ENTRIES
|
|
40544
|
+
});
|
|
40545
|
+
turns = new LRUCache({
|
|
40546
|
+
max: MAX_EVE_PROVIDER_CACHE_ENTRIES
|
|
40547
|
+
});
|
|
40548
|
+
async handleTurnStarted(event, context) {
|
|
40549
|
+
await this.contain("turn start", async () => {
|
|
40550
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40551
|
+
context.state.set({ skip: true });
|
|
40552
|
+
return;
|
|
40553
|
+
}
|
|
40554
|
+
const { rowId, spanId } = await generateEveIds(
|
|
40555
|
+
"turn",
|
|
40556
|
+
event.idempotencyKey
|
|
40557
|
+
);
|
|
40558
|
+
const key = turnKey3(event.sessionId, event.turnId);
|
|
40559
|
+
const stored = storedSpan(context);
|
|
40560
|
+
if (stored && "exported" in stored && stored.rootSpanId) {
|
|
40561
|
+
this.turns.set(key, { rootSpanId: stored.rootSpanId, spanId });
|
|
40562
|
+
return;
|
|
40563
|
+
}
|
|
40564
|
+
let rootSpanId = spanId;
|
|
40565
|
+
let parentSpanId;
|
|
40566
|
+
if (event.parentLineage) {
|
|
40567
|
+
const parentTurnKey = turnKey3(
|
|
40568
|
+
event.parentLineage.sessionId,
|
|
40569
|
+
event.parentLineage.turnId
|
|
40570
|
+
);
|
|
40571
|
+
const parentTurn = this.turns.get(parentTurnKey);
|
|
40572
|
+
rootSpanId = parentTurn?.rootSpanId ?? // Eve propagates the root session through every nested subagent. Use
|
|
40573
|
+
// it instead of the immediate parent session when no ancestor ran
|
|
40574
|
+
// in-process.
|
|
40575
|
+
(await generateEveIds(
|
|
40576
|
+
"turn",
|
|
40577
|
+
turnIdempotencyKey(
|
|
40578
|
+
event.rootSessionId,
|
|
40579
|
+
event.parentLineage.turnId
|
|
40580
|
+
)
|
|
40581
|
+
)).spanId;
|
|
40582
|
+
parentSpanId = (await generateEveIds(
|
|
40583
|
+
"subagent",
|
|
40584
|
+
actionIdempotencyKey(
|
|
40585
|
+
event.parentLineage.sessionId,
|
|
40586
|
+
event.parentLineage.turnId,
|
|
40587
|
+
event.parentLineage.callId
|
|
40588
|
+
)
|
|
40589
|
+
)).spanId;
|
|
40590
|
+
}
|
|
40591
|
+
const metadata = this.spanMetadata(event.sessionId);
|
|
40592
|
+
const span = await this.startSpan(
|
|
40593
|
+
context,
|
|
40594
|
+
{
|
|
40595
|
+
event: { id: rowId, metadata },
|
|
40596
|
+
name: "eve.turn",
|
|
40597
|
+
parentSpanIds: parentSpanId ? { rootSpanId, spanId: parentSpanId } : { parentSpanIds: [], rootSpanId },
|
|
40598
|
+
spanAttributes: { type: "task" /* TASK */ },
|
|
40599
|
+
spanId
|
|
40600
|
+
},
|
|
40601
|
+
rootSpanId
|
|
40602
|
+
);
|
|
40603
|
+
span?.log({ metadata });
|
|
40604
|
+
this.turns.set(key, {
|
|
40605
|
+
rootSpanId,
|
|
40606
|
+
spanId
|
|
40607
|
+
});
|
|
40608
|
+
});
|
|
40609
|
+
}
|
|
40610
|
+
async handleTurnTerminal(event, context) {
|
|
40611
|
+
await this.contain("turn terminal", async () => {
|
|
40612
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40613
|
+
context.state.set(void 0);
|
|
40614
|
+
return;
|
|
40615
|
+
}
|
|
40616
|
+
const error = event.type === "turn.failed" ? event.error : void 0;
|
|
40617
|
+
this.drainActionsForTurn(event.sessionId, event.turnId, error);
|
|
40618
|
+
const stored = storedSpan(context);
|
|
40619
|
+
if (stored && "exported" in stored) {
|
|
40620
|
+
updateSpan({
|
|
40621
|
+
exported: stored.exported,
|
|
40622
|
+
...error !== void 0 ? { error } : {},
|
|
40623
|
+
metrics: { end: Date.now() / 1e3 }
|
|
40624
|
+
});
|
|
40625
|
+
}
|
|
40626
|
+
this.settledOperations.set(event.idempotencyKey, true);
|
|
40627
|
+
context.state.set(void 0);
|
|
40628
|
+
this.turns.delete(turnKey3(event.sessionId, event.turnId));
|
|
40629
|
+
});
|
|
40630
|
+
}
|
|
40631
|
+
async handleModelStarted(event, context) {
|
|
40632
|
+
await this.contain("model start", async () => {
|
|
40633
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40634
|
+
context.state.set({ skip: true });
|
|
40635
|
+
return;
|
|
40636
|
+
}
|
|
40637
|
+
const parent = await this.parentForScope(event.scope);
|
|
40638
|
+
const { rowId, spanId } = await generateEveIds(
|
|
40639
|
+
"step",
|
|
40640
|
+
event.idempotencyKey
|
|
40641
|
+
);
|
|
40642
|
+
const input = event.input ? capturedModelInput(event.input) : void 0;
|
|
40643
|
+
const metadata = {
|
|
40644
|
+
...this.spanMetadata(event.scope.sessionId),
|
|
40645
|
+
model: event.model.modelId,
|
|
40646
|
+
provider: event.model.provider
|
|
40647
|
+
};
|
|
40648
|
+
const span = await this.startSpan(context, {
|
|
40649
|
+
event: {
|
|
40650
|
+
id: rowId,
|
|
40651
|
+
...input !== void 0 ? { input } : {},
|
|
40652
|
+
metadata
|
|
40653
|
+
},
|
|
40654
|
+
name: "eve.step",
|
|
40655
|
+
parentSpanIds: parent,
|
|
40656
|
+
spanAttributes: { type: "llm" /* LLM */ },
|
|
40657
|
+
spanId
|
|
40658
|
+
});
|
|
40659
|
+
if (!span) return;
|
|
40660
|
+
span.log({ ...input !== void 0 ? { input } : {}, metadata });
|
|
40661
|
+
this.activeModels.set(event.idempotencyKey, {
|
|
40662
|
+
span,
|
|
40663
|
+
turnKey: turnKey3(event.scope.sessionId, event.scope.turnId)
|
|
40664
|
+
});
|
|
40665
|
+
const keys = this.modelsByAttempt.get(event.scope.attemptId) ?? /* @__PURE__ */ new Set();
|
|
40666
|
+
keys.add(event.idempotencyKey);
|
|
40667
|
+
this.modelsByAttempt.set(event.scope.attemptId, keys);
|
|
40668
|
+
});
|
|
40669
|
+
}
|
|
40670
|
+
async handleModelTerminal(event, context) {
|
|
40671
|
+
await this.contain("model terminal", async () => {
|
|
40672
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40673
|
+
context.state.set(void 0);
|
|
40674
|
+
return;
|
|
40675
|
+
}
|
|
40676
|
+
const active = this.activeModels.get(event.idempotencyKey);
|
|
40677
|
+
if (active) {
|
|
40678
|
+
const span = active.span;
|
|
40679
|
+
if (event.type === "model.call.failed") {
|
|
40680
|
+
if (event.error !== void 0) span.log({ error: event.error });
|
|
40681
|
+
} else {
|
|
40682
|
+
span.log({
|
|
40683
|
+
metrics: usageMetrics(event.usage),
|
|
40684
|
+
output: modelOutput(event)
|
|
40685
|
+
});
|
|
40686
|
+
}
|
|
40687
|
+
span.end();
|
|
40688
|
+
} else {
|
|
40689
|
+
const stored = storedSpan(context);
|
|
40690
|
+
if (stored && "exported" in stored) {
|
|
40691
|
+
updateSpan({
|
|
40692
|
+
exported: stored.exported,
|
|
40693
|
+
...event.type === "model.call.failed" ? event.error !== void 0 ? { error: event.error } : {} : { output: modelOutput(event) },
|
|
40694
|
+
metrics: {
|
|
40695
|
+
...event.type === "model.call.completed" ? usageMetrics(event.usage) : {},
|
|
40696
|
+
end: Date.now() / 1e3
|
|
40697
|
+
}
|
|
40698
|
+
});
|
|
40699
|
+
}
|
|
40700
|
+
}
|
|
40701
|
+
this.settledOperations.set(event.idempotencyKey, true);
|
|
40702
|
+
this.forgetModel(event.scope.attemptId, event.idempotencyKey);
|
|
40703
|
+
context.state.set(void 0);
|
|
40704
|
+
});
|
|
40705
|
+
}
|
|
40706
|
+
async handleActionStarted(event, context) {
|
|
40707
|
+
await this.contain("action start", async () => {
|
|
40708
|
+
if (event.kind === "load-skill" || this.settledOperations.has(event.idempotencyKey)) {
|
|
40709
|
+
context.state.set({ skip: true });
|
|
40710
|
+
return;
|
|
40711
|
+
}
|
|
40712
|
+
const parent = await this.parentForScope(event.scope);
|
|
40713
|
+
const { rowId, spanId } = await generateEveIds(
|
|
40714
|
+
event.kind === "subagent-call" ? "subagent" : "tool",
|
|
40715
|
+
event.idempotencyKey
|
|
40716
|
+
);
|
|
40717
|
+
const metadata = {
|
|
40718
|
+
...this.spanMetadata(event.scope.sessionId),
|
|
40719
|
+
"eve.action_kind": event.kind
|
|
40720
|
+
};
|
|
40721
|
+
const span = await this.startSpan(context, {
|
|
40722
|
+
event: {
|
|
40723
|
+
id: rowId,
|
|
40724
|
+
...event.input !== void 0 ? { input: event.input } : {},
|
|
40725
|
+
metadata
|
|
40726
|
+
},
|
|
40727
|
+
name: event.name,
|
|
40728
|
+
parentSpanIds: parent,
|
|
40729
|
+
spanAttributes: { type: "tool" /* TOOL */ },
|
|
40730
|
+
spanId
|
|
40731
|
+
});
|
|
40732
|
+
if (!span) return;
|
|
40733
|
+
span.log({
|
|
40734
|
+
...event.input !== void 0 ? { input: event.input } : {},
|
|
40735
|
+
metadata
|
|
40736
|
+
});
|
|
40737
|
+
this.activeActions.set(event.idempotencyKey, {
|
|
40738
|
+
span,
|
|
40739
|
+
turnKey: turnKey3(event.scope.sessionId, event.scope.turnId)
|
|
40740
|
+
});
|
|
40741
|
+
});
|
|
40742
|
+
}
|
|
40743
|
+
async handleActionTerminal(event, context) {
|
|
40744
|
+
await this.contain("action terminal", async () => {
|
|
40745
|
+
if (this.settledOperations.has(event.idempotencyKey)) {
|
|
40746
|
+
context.state.set(void 0);
|
|
40747
|
+
return;
|
|
40748
|
+
}
|
|
40749
|
+
const stored = storedSpan(context);
|
|
40750
|
+
if (stored?.skip) {
|
|
40751
|
+
context.state.set(void 0);
|
|
40752
|
+
return;
|
|
40753
|
+
}
|
|
40754
|
+
const active = this.activeActions.get(event.idempotencyKey);
|
|
40755
|
+
const end = finiteTimestamp(event.acceptedAtMs) ?? Date.now();
|
|
40756
|
+
if (active) {
|
|
40757
|
+
const span = active.span;
|
|
40758
|
+
if (event.type === "action.failed") {
|
|
40759
|
+
span.log({
|
|
40760
|
+
error: event.error ?? new Error(event.errorCode ?? `Eve action ${event.outcome}`)
|
|
40761
|
+
});
|
|
40762
|
+
} else if (event.output.type === "error") {
|
|
40763
|
+
span.log({
|
|
40764
|
+
error: event.output.error ?? new Error("Eve action returned an error")
|
|
40765
|
+
});
|
|
40766
|
+
} else {
|
|
40767
|
+
span.log({ output: event.output.output });
|
|
40768
|
+
}
|
|
40769
|
+
span.end({ endTime: end / 1e3 });
|
|
40770
|
+
} else {
|
|
40771
|
+
const stored2 = storedSpan(context);
|
|
40772
|
+
if (stored2 && "exported" in stored2) {
|
|
40773
|
+
updateSpan({
|
|
40774
|
+
exported: stored2.exported,
|
|
40775
|
+
...event.type === "action.failed" ? {
|
|
40776
|
+
error: event.error ?? new Error(event.errorCode ?? `Eve action ${event.outcome}`)
|
|
40777
|
+
} : event.output.type === "error" ? {
|
|
40778
|
+
error: event.output.error ?? new Error("Eve action returned an error")
|
|
40779
|
+
} : { output: event.output.output },
|
|
40780
|
+
metrics: { end: end / 1e3 }
|
|
40781
|
+
});
|
|
40782
|
+
}
|
|
40783
|
+
}
|
|
40784
|
+
this.settledOperations.set(event.idempotencyKey, true);
|
|
40785
|
+
this.activeActions.delete(event.idempotencyKey);
|
|
40786
|
+
context.state.set(void 0);
|
|
40787
|
+
});
|
|
40788
|
+
}
|
|
40789
|
+
handleStepAttemptTerminal(event) {
|
|
40790
|
+
void this.contain("step attempt terminal", () => {
|
|
40791
|
+
const keys = this.modelsByAttempt.get(event.scope.attemptId);
|
|
40792
|
+
if (!keys) return;
|
|
40793
|
+
for (const key of keys) {
|
|
40794
|
+
const active = this.activeModels.get(key);
|
|
40795
|
+
if (!active) continue;
|
|
40796
|
+
if (event.type === "step.attempt.failed" && event.error !== void 0) {
|
|
40797
|
+
active.span.log({ error: event.error });
|
|
40798
|
+
}
|
|
40799
|
+
active.span.end();
|
|
40800
|
+
this.activeModels.delete(key);
|
|
40801
|
+
}
|
|
40802
|
+
this.modelsByAttempt.delete(event.scope.attemptId);
|
|
40803
|
+
});
|
|
40804
|
+
}
|
|
40805
|
+
async parentForScope(scope) {
|
|
40806
|
+
const key = turnKey3(scope.sessionId, scope.turnId);
|
|
40807
|
+
const known = this.turns.get(key);
|
|
40808
|
+
if (known) {
|
|
40809
|
+
return { rootSpanId: known.rootSpanId, spanId: known.spanId };
|
|
40810
|
+
}
|
|
40811
|
+
const [{ spanId }, { spanId: rootSpanId }] = await Promise.all([
|
|
40812
|
+
generateEveIds("turn", turnIdempotencyKey(scope.sessionId, scope.turnId)),
|
|
40813
|
+
generateEveIds(
|
|
40814
|
+
"turn",
|
|
40815
|
+
turnIdempotencyKey(
|
|
40816
|
+
scope.rootSessionId ?? scope.sessionId,
|
|
40817
|
+
scope.turnId
|
|
40818
|
+
)
|
|
40819
|
+
)
|
|
40820
|
+
]);
|
|
40821
|
+
return { rootSpanId, spanId };
|
|
40822
|
+
}
|
|
40823
|
+
async startSpan(context, args, rootSpanId) {
|
|
40824
|
+
const span = withCurrent(
|
|
40825
|
+
NOOP_SPAN,
|
|
40826
|
+
() => _internalStartSpanWithInitialMerge(
|
|
40827
|
+
withSpanInstrumentationName(args ?? {}, INSTRUMENTATION_NAMES.EVE)
|
|
40828
|
+
)
|
|
40829
|
+
);
|
|
40830
|
+
try {
|
|
40831
|
+
context.state.set({
|
|
40832
|
+
exported: await span.export(),
|
|
40833
|
+
...rootSpanId ? { rootSpanId } : {}
|
|
40834
|
+
});
|
|
40835
|
+
} catch (error) {
|
|
40836
|
+
debugLogger.warn("Error exporting Eve provider span:", error);
|
|
40837
|
+
}
|
|
40838
|
+
return span;
|
|
40839
|
+
}
|
|
40840
|
+
drainActionsForTurn(sessionId, turnId, error) {
|
|
40841
|
+
const key = turnKey3(sessionId, turnId);
|
|
40842
|
+
for (const [idempotencyKey, active] of this.activeActions) {
|
|
40843
|
+
if (active.turnKey !== key) continue;
|
|
40844
|
+
if (error !== void 0) active.span.log({ error });
|
|
40845
|
+
active.span.end();
|
|
40846
|
+
this.activeActions.delete(idempotencyKey);
|
|
40847
|
+
}
|
|
40848
|
+
}
|
|
40849
|
+
forgetModel(attemptId, idempotencyKey) {
|
|
40850
|
+
this.activeModels.delete(idempotencyKey);
|
|
40851
|
+
const keys = this.modelsByAttempt.get(attemptId);
|
|
40852
|
+
keys?.delete(idempotencyKey);
|
|
40853
|
+
if (keys?.size === 0) this.modelsByAttempt.delete(attemptId);
|
|
40854
|
+
}
|
|
40855
|
+
spanMetadata(sessionId) {
|
|
40856
|
+
return {
|
|
40857
|
+
...this.metadata ?? {},
|
|
40858
|
+
"eve.session_id": sessionId
|
|
40859
|
+
};
|
|
40860
|
+
}
|
|
40861
|
+
async contain(operation, fn) {
|
|
40862
|
+
try {
|
|
40863
|
+
await fn();
|
|
40864
|
+
} catch (error) {
|
|
40865
|
+
debugLogger.warn(`Error in Eve provider ${operation}:`, error);
|
|
40866
|
+
}
|
|
40867
|
+
}
|
|
40868
|
+
};
|
|
40869
|
+
function storedSpan(context) {
|
|
40870
|
+
const value = context.state.get();
|
|
40871
|
+
if (!isObject(value)) return void 0;
|
|
40872
|
+
if (value["skip"] === true) return { skip: true };
|
|
40873
|
+
return typeof value["exported"] === "string" ? {
|
|
40874
|
+
exported: value["exported"],
|
|
40875
|
+
...typeof value["rootSpanId"] === "string" && value["rootSpanId"].length > 0 ? { rootSpanId: value["rootSpanId"] } : {}
|
|
40876
|
+
} : void 0;
|
|
40877
|
+
}
|
|
40878
|
+
function modelOutput(event) {
|
|
40879
|
+
const content = event.content ?? [];
|
|
40880
|
+
let text = "";
|
|
40881
|
+
const reasoning = [];
|
|
40882
|
+
const toolCalls2 = [];
|
|
40883
|
+
for (const part of content) {
|
|
40884
|
+
if (part.type === "text") {
|
|
40885
|
+
text += part.text;
|
|
40886
|
+
} else if (part.type === "reasoning" && part.text.trim().length > 0) {
|
|
40887
|
+
reasoning.push({ content: part.text });
|
|
40888
|
+
} else if (part.type === "tool-call") {
|
|
40889
|
+
toolCalls2.push({
|
|
40890
|
+
function: {
|
|
40891
|
+
arguments: safeJsonStringify(part.input),
|
|
40892
|
+
name: part.toolName
|
|
40893
|
+
},
|
|
40894
|
+
id: part.callId,
|
|
40895
|
+
type: "function"
|
|
40896
|
+
});
|
|
40897
|
+
}
|
|
40898
|
+
}
|
|
40899
|
+
return [
|
|
40900
|
+
{
|
|
40901
|
+
finish_reason: normalizeFinishReason3(event.finishReason),
|
|
40902
|
+
index: 0,
|
|
40903
|
+
message: {
|
|
40904
|
+
content: text || null,
|
|
40905
|
+
...reasoning.length > 0 ? { reasoning } : {},
|
|
40906
|
+
role: "assistant",
|
|
40907
|
+
...toolCalls2.length > 0 ? { tool_calls: toolCalls2 } : {}
|
|
40908
|
+
}
|
|
40909
|
+
}
|
|
40910
|
+
];
|
|
40911
|
+
}
|
|
40912
|
+
function usageMetrics(usage) {
|
|
40913
|
+
const promptTokens = nonNegativeNumber(usage.inputTokens);
|
|
40914
|
+
const completionTokens = nonNegativeNumber(usage.outputTokens);
|
|
40915
|
+
const cachedTokens = nonNegativeNumber(
|
|
40916
|
+
usage.inputTokenDetails?.cacheReadTokens
|
|
40917
|
+
);
|
|
40918
|
+
const cacheCreationTokens = nonNegativeNumber(
|
|
40919
|
+
usage.inputTokenDetails?.cacheWriteTokens
|
|
40920
|
+
);
|
|
40921
|
+
return {
|
|
40922
|
+
...promptTokens !== void 0 ? { prompt_tokens: promptTokens } : {},
|
|
40923
|
+
...completionTokens !== void 0 ? { completion_tokens: completionTokens } : {},
|
|
40924
|
+
...promptTokens !== void 0 && completionTokens !== void 0 ? { tokens: promptTokens + completionTokens } : {},
|
|
40925
|
+
...cachedTokens !== void 0 ? { prompt_cached_tokens: cachedTokens } : {},
|
|
40926
|
+
...cacheCreationTokens !== void 0 ? { prompt_cache_creation_tokens: cacheCreationTokens } : {}
|
|
40927
|
+
};
|
|
40928
|
+
}
|
|
40929
|
+
function nonNegativeNumber(value) {
|
|
40930
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
40931
|
+
}
|
|
40932
|
+
function finiteTimestamp(value) {
|
|
40933
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
40934
|
+
}
|
|
40935
|
+
function normalizeFinishReason3(value) {
|
|
40936
|
+
if (value === "content-filter") return "content_filter";
|
|
40937
|
+
if (value === "tool-calls") return "tool_calls";
|
|
40938
|
+
return value;
|
|
40939
|
+
}
|
|
40940
|
+
function safeJsonStringify(value) {
|
|
40941
|
+
try {
|
|
40942
|
+
return JSON.stringify(value) ?? "null";
|
|
40943
|
+
} catch {
|
|
40944
|
+
return "null";
|
|
40945
|
+
}
|
|
40946
|
+
}
|
|
40947
|
+
function turnKey3(sessionId, turnId) {
|
|
40948
|
+
return `${sessionId}:${turnId}`;
|
|
40949
|
+
}
|
|
40950
|
+
function turnIdempotencyKey(sessionId, turnId) {
|
|
40951
|
+
return `turn:${sessionId}:${turnId}`;
|
|
40952
|
+
}
|
|
40953
|
+
function actionIdempotencyKey(sessionId, turnId, callId) {
|
|
40954
|
+
return `action:${sessionId}:${turnId}:${callId}`;
|
|
40955
|
+
}
|
|
40956
|
+
|
|
40957
|
+
// src/instrumentation/plugins/eve-instrumentation.ts
|
|
40958
|
+
var EVE_INSTRUMENTATION_PROVIDER = /* @__PURE__ */ Symbol.for("eve.instrumentation.provider");
|
|
40959
|
+
function braintrustEveInstrumentation(options) {
|
|
40960
|
+
const definition = "defineState" in options ? createLegacyEveInstrumentation(options) : createEveInstrumentationProvider(options);
|
|
40961
|
+
const declaration = {
|
|
40962
|
+
...definition,
|
|
40963
|
+
[EVE_INSTRUMENTATION_PROVIDER]: true
|
|
40964
|
+
};
|
|
40965
|
+
return declaration;
|
|
40966
|
+
}
|
|
40967
|
+
|
|
40149
40968
|
// src/typed-instrumentation-helpers.ts
|
|
40150
40969
|
var TypedApplyProxy = Proxy;
|
|
40151
40970
|
|
|
@@ -40547,11 +41366,11 @@ function wrapClaudeAgentQuery(queryFn, defaultThis) {
|
|
|
40547
41366
|
}
|
|
40548
41367
|
};
|
|
40549
41368
|
const invocationTarget = thisArg === proxy || thisArg === void 0 ? defaultThis ?? thisArg : thisArg;
|
|
40550
|
-
return claudeAgentSDKChannels.query.
|
|
40551
|
-
|
|
40552
|
-
|
|
40553
|
-
|
|
40554
|
-
{
|
|
41369
|
+
return claudeAgentSDKChannels.query.invoke(
|
|
41370
|
+
target,
|
|
41371
|
+
invocationTarget,
|
|
41372
|
+
[wrappedParams],
|
|
41373
|
+
{}
|
|
40555
41374
|
);
|
|
40556
41375
|
}
|
|
40557
41376
|
});
|
|
@@ -41166,13 +41985,11 @@ function wrapAgentInstance(agent) {
|
|
|
41166
41985
|
if (prop === "stream" && typeof value === "function") {
|
|
41167
41986
|
return function(args, options) {
|
|
41168
41987
|
const callArgs = [args, options];
|
|
41169
|
-
return strandsAgentSDKChannels.agentStream.
|
|
41170
|
-
|
|
41171
|
-
|
|
41172
|
-
|
|
41173
|
-
|
|
41174
|
-
self: proxy
|
|
41175
|
-
}
|
|
41988
|
+
return strandsAgentSDKChannels.agentStream.invoke(
|
|
41989
|
+
value,
|
|
41990
|
+
target,
|
|
41991
|
+
callArgs,
|
|
41992
|
+
{ agent: proxy }
|
|
41176
41993
|
);
|
|
41177
41994
|
};
|
|
41178
41995
|
}
|
|
@@ -41203,12 +42020,12 @@ function wrapMultiAgentInstance(orchestrator, kind) {
|
|
|
41203
42020
|
return function(input, options) {
|
|
41204
42021
|
const callArgs = [input, options];
|
|
41205
42022
|
const channel2 = kind === "graph" ? strandsAgentSDKChannels.graphStream : strandsAgentSDKChannels.swarmStream;
|
|
41206
|
-
return channel2.
|
|
41207
|
-
|
|
42023
|
+
return channel2.invoke(
|
|
42024
|
+
value,
|
|
42025
|
+
target,
|
|
42026
|
+
callArgs,
|
|
41208
42027
|
{
|
|
41209
|
-
|
|
41210
|
-
orchestrator: proxy,
|
|
41211
|
-
self: proxy
|
|
42028
|
+
orchestrator: proxy
|
|
41212
42029
|
}
|
|
41213
42030
|
);
|
|
41214
42031
|
};
|
|
@@ -43160,9 +43977,6 @@ var VitestContextManager = class {
|
|
|
43160
43977
|
getCurrentContext() {
|
|
43161
43978
|
return this.contextStorage.getStore();
|
|
43162
43979
|
}
|
|
43163
|
-
setContext(context) {
|
|
43164
|
-
this.contextStorage.enterWith(context);
|
|
43165
|
-
}
|
|
43166
43980
|
runInContext(context, callback) {
|
|
43167
43981
|
return this.contextStorage.run(context, callback);
|
|
43168
43982
|
}
|
|
@@ -43548,8 +44362,7 @@ function wrapDescribe(originalDescribe, config, afterAll) {
|
|
|
43548
44362
|
if (config.onProgress) {
|
|
43549
44363
|
config.onProgress({ type: "suite_start", suiteName });
|
|
43550
44364
|
}
|
|
43551
|
-
contextManager.
|
|
43552
|
-
factory();
|
|
44365
|
+
contextManager.runInContext(lazyContext, factory);
|
|
43553
44366
|
if (afterAll) {
|
|
43554
44367
|
afterAll(async () => {
|
|
43555
44368
|
await flushExperimentWithSync(context, config);
|
|
@@ -44128,7 +44941,7 @@ function isAsync(fn) {
|
|
|
44128
44941
|
function isAsyncGenerator2(fn) {
|
|
44129
44942
|
return fn[Symbol.toStringTag] === "AsyncGenerator";
|
|
44130
44943
|
}
|
|
44131
|
-
function
|
|
44944
|
+
function isAsyncIterable5(obj) {
|
|
44132
44945
|
return typeof obj[Symbol.asyncIterator] === "function";
|
|
44133
44946
|
}
|
|
44134
44947
|
function wrapAsync(asyncFn) {
|
|
@@ -44300,7 +45113,7 @@ var eachOfLimit$2 = (limit) => {
|
|
|
44300
45113
|
if (isAsyncGenerator2(obj)) {
|
|
44301
45114
|
return asyncEachOfLimit(obj, limit, iteratee, callback);
|
|
44302
45115
|
}
|
|
44303
|
-
if (
|
|
45116
|
+
if (isAsyncIterable5(obj)) {
|
|
44304
45117
|
return asyncEachOfLimit(obj[Symbol.asyncIterator](), limit, iteratee, callback);
|
|
44305
45118
|
}
|
|
44306
45119
|
var nextElem = createIterator(obj);
|
|
@@ -45655,7 +46468,7 @@ function callEvaluatorData(data) {
|
|
|
45655
46468
|
baseExperiment
|
|
45656
46469
|
};
|
|
45657
46470
|
}
|
|
45658
|
-
function
|
|
46471
|
+
function isAsyncIterable6(value) {
|
|
45659
46472
|
return typeof value === "object" && value !== null && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
45660
46473
|
}
|
|
45661
46474
|
function isIterable(value) {
|
|
@@ -45690,7 +46503,7 @@ async function _internalResolveEvaluatorData(evaluator, experiment) {
|
|
|
45690
46503
|
}).asDataset();
|
|
45691
46504
|
}
|
|
45692
46505
|
const resolvedDataResult = dataResult instanceof Promise ? await dataResult : dataResult;
|
|
45693
|
-
if (
|
|
46506
|
+
if (isAsyncIterable6(resolvedDataResult)) {
|
|
45694
46507
|
return resolvedDataResult;
|
|
45695
46508
|
}
|
|
45696
46509
|
if (Array.isArray(resolvedDataResult) || isIterable(resolvedDataResult)) {
|