braintrust 3.9.0 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dev/dist/index.d.mts +107 -1
- package/dev/dist/index.d.ts +107 -1
- package/dev/dist/index.js +2126 -443
- package/dev/dist/index.mjs +1814 -131
- package/dist/auto-instrumentations/bundler/esbuild.cjs +166 -0
- package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
- package/dist/auto-instrumentations/bundler/rollup.cjs +166 -0
- package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
- package/dist/auto-instrumentations/bundler/vite.cjs +166 -0
- package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +163 -0
- package/dist/auto-instrumentations/bundler/webpack.cjs +166 -0
- package/dist/auto-instrumentations/bundler/webpack.mjs +2 -2
- package/dist/auto-instrumentations/{chunk-KIMMUFAK.mjs → chunk-GZNXBBPU.mjs} +164 -1
- package/dist/auto-instrumentations/{chunk-G7F6HZGE.mjs → chunk-XWEQQOQH.mjs} +8 -1
- package/dist/auto-instrumentations/hook.mjs +255 -8
- package/dist/auto-instrumentations/index.cjs +256 -7
- package/dist/auto-instrumentations/index.d.mts +5 -1
- package/dist/auto-instrumentations/index.d.ts +5 -1
- package/dist/auto-instrumentations/index.mjs +96 -8
- package/dist/browser.d.mts +141 -7
- package/dist/browser.d.ts +141 -7
- package/dist/browser.js +2035 -140
- package/dist/browser.mjs +2035 -140
- package/dist/cli.js +1822 -139
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +2035 -140
- package/dist/edge-light.mjs +2035 -140
- package/dist/index.d.mts +141 -7
- package/dist/index.d.ts +141 -7
- package/dist/index.js +2397 -502
- package/dist/index.mjs +2035 -140
- package/dist/instrumentation/index.d.mts +7 -0
- package/dist/instrumentation/index.d.ts +7 -0
- package/dist/instrumentation/index.js +1662 -167
- package/dist/instrumentation/index.mjs +1662 -167
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +2035 -140
- package/dist/workerd.mjs +2035 -140
- package/package.json +6 -6
package/dev/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } async function _asyncOptionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = await fn(value); } else if (op === 'call' || op === 'optionalCall') { value = await fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25; var _class26;// src/node/config.ts
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } async function _asyncOptionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = await fn(value); } else if (op === 'call' || op === 'optionalCall') { value = await fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25; var _class26; var _class27;// src/node/config.ts
|
|
2
2
|
var _nodeasync_hooks = require('node:async_hooks');
|
|
3
3
|
var _nodediagnostics_channel = require('node:diagnostics_channel'); var diagnostics_channel = _interopRequireWildcard(_nodediagnostics_channel);
|
|
4
4
|
var _nodepath = require('node:path'); var path = _interopRequireWildcard(_nodepath);
|
|
@@ -2123,6 +2123,7 @@ var TopicMapData = _v3.z.object({
|
|
|
2123
2123
|
report_key: _v3.z.string().optional(),
|
|
2124
2124
|
topic_names: _v3.z.record(_v3.z.string()).optional(),
|
|
2125
2125
|
generation_settings: TopicMapGenerationSettings.optional(),
|
|
2126
|
+
disable_reconciliation: _v3.z.boolean().optional(),
|
|
2126
2127
|
distance_threshold: _v3.z.number().optional()
|
|
2127
2128
|
});
|
|
2128
2129
|
var BatchedFacetData = _v3.z.object({
|
|
@@ -2453,28 +2454,6 @@ var EnvVar = _v3.z.object({
|
|
|
2453
2454
|
secret_type: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2454
2455
|
secret_category: _v3.z.enum(["env_var", "ai_provider", "sandbox_provider"]).optional().default("env_var")
|
|
2455
2456
|
});
|
|
2456
|
-
var EvalStatusPageTheme = _v3.z.enum(["light", "dark"]);
|
|
2457
|
-
var EvalStatusPageConfig = _v3.z.object({
|
|
2458
|
-
score_columns: _v3.z.union([_v3.z.array(_v3.z.string()), _v3.z.null()]),
|
|
2459
|
-
metric_columns: _v3.z.union([_v3.z.array(_v3.z.string()), _v3.z.null()]),
|
|
2460
|
-
grouping_field: _v3.z.union([_v3.z.string(), _v3.z.null()]),
|
|
2461
|
-
filter: _v3.z.union([_v3.z.string(), _v3.z.null()]),
|
|
2462
|
-
sort_by: _v3.z.union([_v3.z.string(), _v3.z.null()]),
|
|
2463
|
-
sort_order: _v3.z.union([_v3.z.enum(["asc", "desc"]), _v3.z.null()]),
|
|
2464
|
-
api_key: _v3.z.union([_v3.z.string(), _v3.z.null()])
|
|
2465
|
-
}).partial();
|
|
2466
|
-
var EvalStatusPage = _v3.z.object({
|
|
2467
|
-
id: _v3.z.string().uuid(),
|
|
2468
|
-
project_id: _v3.z.string().uuid(),
|
|
2469
|
-
user_id: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2470
|
-
created: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2471
|
-
deleted_at: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2472
|
-
name: _v3.z.string(),
|
|
2473
|
-
description: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2474
|
-
logo_url: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2475
|
-
theme: EvalStatusPageTheme,
|
|
2476
|
-
config: EvalStatusPageConfig
|
|
2477
|
-
});
|
|
2478
2457
|
var RepoInfo = _v3.z.union([
|
|
2479
2458
|
_v3.z.object({
|
|
2480
2459
|
commit: _v3.z.union([_v3.z.string(), _v3.z.null()]),
|
|
@@ -2501,6 +2480,15 @@ var Experiment = _v3.z.object({
|
|
|
2501
2480
|
deleted_at: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2502
2481
|
dataset_id: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2503
2482
|
dataset_version: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2483
|
+
internal_metadata: _v3.z.union([
|
|
2484
|
+
_v3.z.object({
|
|
2485
|
+
dataset_filter: _v3.z.union([
|
|
2486
|
+
_v3.z.object({}).partial().passthrough(),
|
|
2487
|
+
_v3.z.null()
|
|
2488
|
+
])
|
|
2489
|
+
}).partial().passthrough(),
|
|
2490
|
+
_v3.z.null()
|
|
2491
|
+
]).optional(),
|
|
2504
2492
|
parameters_id: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2505
2493
|
parameters_version: _v3.z.union([_v3.z.string(), _v3.z.null()]).optional(),
|
|
2506
2494
|
public: _v3.z.boolean(),
|
|
@@ -3237,14 +3225,21 @@ var ProjectAutomation = _v3.z.object({
|
|
|
3237
3225
|
_v3.z.object({ type: _v3.z.literal("log_spans") }),
|
|
3238
3226
|
_v3.z.object({ type: _v3.z.literal("btql_query"), btql_query: _v3.z.string() })
|
|
3239
3227
|
]),
|
|
3228
|
+
scope: _v3.z.union([SpanScope, TraceScope, GroupScope, _v3.z.null()]).optional(),
|
|
3240
3229
|
export_path: _v3.z.string(),
|
|
3241
3230
|
format: _v3.z.enum(["jsonl", "parquet"]),
|
|
3242
3231
|
interval_seconds: _v3.z.number().gte(1).lte(2592e3),
|
|
3243
|
-
credentials: _v3.z.
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3232
|
+
credentials: _v3.z.union([
|
|
3233
|
+
_v3.z.object({
|
|
3234
|
+
type: _v3.z.literal("aws_iam"),
|
|
3235
|
+
role_arn: _v3.z.string(),
|
|
3236
|
+
external_id: _v3.z.string()
|
|
3237
|
+
}),
|
|
3238
|
+
_v3.z.object({
|
|
3239
|
+
type: _v3.z.literal("gcp_service_account"),
|
|
3240
|
+
service_account_email: _v3.z.string()
|
|
3241
|
+
})
|
|
3242
|
+
]),
|
|
3248
3243
|
batch_size: _v3.z.union([_v3.z.number(), _v3.z.null()]).optional()
|
|
3249
3244
|
}),
|
|
3250
3245
|
_v3.z.object({
|
|
@@ -3624,7 +3619,7 @@ var ViewOptions = _v3.z.union([
|
|
|
3624
3619
|
_v3.z.object({ from: _v3.z.string(), to: _v3.z.string() }),
|
|
3625
3620
|
_v3.z.null()
|
|
3626
3621
|
]),
|
|
3627
|
-
queryShape: _v3.z.union([_v3.z.enum(["traces", "spans"]), _v3.z.null()]),
|
|
3622
|
+
queryShape: _v3.z.union([_v3.z.enum(["traces", "spans", "topics"]), _v3.z.null()]),
|
|
3628
3623
|
cluster: _v3.z.union([_v3.z.string(), _v3.z.null()]),
|
|
3629
3624
|
freezeColumns: _v3.z.union([_v3.z.boolean(), _v3.z.null()])
|
|
3630
3625
|
}).partial(),
|
|
@@ -4640,6 +4635,19 @@ var RESET_CONTEXT_MANAGER_STATE = Symbol.for(
|
|
|
4640
4635
|
"braintrust.resetContextManagerState"
|
|
4641
4636
|
);
|
|
4642
4637
|
var DEFAULT_MAX_REQUEST_SIZE = 6 * 1024 * 1024;
|
|
4638
|
+
var datasetSnapshotRegisterResponseSchema = _v3.z.object({
|
|
4639
|
+
dataset_snapshot: DatasetSnapshot,
|
|
4640
|
+
found_existing: _v3.z.boolean().optional()
|
|
4641
|
+
});
|
|
4642
|
+
var datasetRestorePreviewResultSchema = _v3.z.object({
|
|
4643
|
+
rows_to_restore: _v3.z.number(),
|
|
4644
|
+
rows_to_delete: _v3.z.number()
|
|
4645
|
+
});
|
|
4646
|
+
var datasetRestoreResultSchema = _v3.z.object({
|
|
4647
|
+
xact_id: _v3.z.string().nullable(),
|
|
4648
|
+
rows_restored: _v3.z.number(),
|
|
4649
|
+
rows_deleted: _v3.z.number()
|
|
4650
|
+
});
|
|
4643
4651
|
var parametersRowSchema = _v3.z.object({
|
|
4644
4652
|
id: _v3.z.string().uuid(),
|
|
4645
4653
|
_xact_id: _v3.z.string(),
|
|
@@ -6649,6 +6657,33 @@ Error: ${errorText}`;
|
|
|
6649
6657
|
this.queue.enforceQueueSizeLimit(enforce);
|
|
6650
6658
|
}
|
|
6651
6659
|
}, _class10);
|
|
6660
|
+
function isDatasetSnapshotNameLookup(lookup) {
|
|
6661
|
+
return "snapshotName" in lookup;
|
|
6662
|
+
}
|
|
6663
|
+
function assertDatasetSnapshotLookup(lookup) {
|
|
6664
|
+
const hasSnapshotName = lookup.snapshotName !== void 0;
|
|
6665
|
+
const hasXactId = lookup.xactId !== void 0;
|
|
6666
|
+
if (hasSnapshotName === hasXactId) {
|
|
6667
|
+
throw new Error("Exactly one of snapshotName or xactId must be provided");
|
|
6668
|
+
}
|
|
6669
|
+
}
|
|
6670
|
+
function getExperimentDatasetFilter({
|
|
6671
|
+
dataset,
|
|
6672
|
+
_internal_btql
|
|
6673
|
+
}) {
|
|
6674
|
+
if (_internal_btql !== void 0) {
|
|
6675
|
+
return _internal_btql;
|
|
6676
|
+
}
|
|
6677
|
+
if (!(dataset instanceof Dataset2)) {
|
|
6678
|
+
return void 0;
|
|
6679
|
+
}
|
|
6680
|
+
const datasetFilter = Reflect.get(dataset, "_internal_btql");
|
|
6681
|
+
return isObject(datasetFilter) ? datasetFilter : void 0;
|
|
6682
|
+
}
|
|
6683
|
+
function getInternalBtqlLimit(internalBtql) {
|
|
6684
|
+
const limit = _optionalChain([internalBtql, 'optionalAccess', _85 => _85["limit"]]);
|
|
6685
|
+
return typeof limit === "number" ? limit : void 0;
|
|
6686
|
+
}
|
|
6652
6687
|
function init(projectOrOptions, optionalOptions) {
|
|
6653
6688
|
const options = (() => {
|
|
6654
6689
|
if (typeof projectOrOptions === "string") {
|
|
@@ -6667,6 +6702,7 @@ function init(projectOrOptions, optionalOptions) {
|
|
|
6667
6702
|
experiment,
|
|
6668
6703
|
description,
|
|
6669
6704
|
dataset,
|
|
6705
|
+
_internal_btql,
|
|
6670
6706
|
parameters,
|
|
6671
6707
|
baseExperiment,
|
|
6672
6708
|
isPublic,
|
|
@@ -6775,16 +6811,24 @@ function init(projectOrOptions, optionalOptions) {
|
|
|
6775
6811
|
args["ancestor_commits"] = await isomorph_default.getPastNAncestors();
|
|
6776
6812
|
}
|
|
6777
6813
|
if (dataset !== void 0) {
|
|
6778
|
-
|
|
6779
|
-
|
|
6780
|
-
|
|
6781
|
-
|
|
6782
|
-
|
|
6783
|
-
|
|
6784
|
-
args["
|
|
6785
|
-
args["dataset_version"] = await dataset.version();
|
|
6814
|
+
const datasetSelection = await serializeDatasetForExperiment({
|
|
6815
|
+
dataset,
|
|
6816
|
+
state
|
|
6817
|
+
});
|
|
6818
|
+
args["dataset_id"] = datasetSelection.datasetId;
|
|
6819
|
+
if (datasetSelection.datasetVersion !== void 0) {
|
|
6820
|
+
args["dataset_version"] = datasetSelection.datasetVersion;
|
|
6786
6821
|
}
|
|
6787
6822
|
}
|
|
6823
|
+
const datasetFilter = getExperimentDatasetFilter({
|
|
6824
|
+
dataset,
|
|
6825
|
+
_internal_btql
|
|
6826
|
+
});
|
|
6827
|
+
if (datasetFilter !== void 0) {
|
|
6828
|
+
args["internal_metadata"] = {
|
|
6829
|
+
dataset_filter: datasetFilter
|
|
6830
|
+
};
|
|
6831
|
+
}
|
|
6788
6832
|
if (parameters !== void 0) {
|
|
6789
6833
|
if (RemoteEvalParameters.isParameters(parameters)) {
|
|
6790
6834
|
args["parameters_id"] = parameters.id;
|
|
@@ -6838,13 +6882,160 @@ function init(projectOrOptions, optionalOptions) {
|
|
|
6838
6882
|
const ret = new Experiment2(
|
|
6839
6883
|
state,
|
|
6840
6884
|
lazyMetadata,
|
|
6841
|
-
dataset !== void 0
|
|
6885
|
+
dataset !== void 0 ? dataset : void 0
|
|
6842
6886
|
);
|
|
6843
6887
|
if (_nullishCoalesce(options.setCurrent, () => ( true))) {
|
|
6844
6888
|
state.currentExperiment = ret;
|
|
6845
6889
|
}
|
|
6846
6890
|
return ret;
|
|
6847
6891
|
}
|
|
6892
|
+
async function getDatasetSnapshots(params) {
|
|
6893
|
+
const { state, datasetId } = params;
|
|
6894
|
+
return DatasetSnapshot.array().parse(
|
|
6895
|
+
await state.appConn().post_json("api/dataset_snapshot/get", {
|
|
6896
|
+
dataset_id: datasetId,
|
|
6897
|
+
..."snapshotName" in params ? { name: params.snapshotName } : {},
|
|
6898
|
+
..."xactId" in params ? { xact_id: params.xactId } : {}
|
|
6899
|
+
})
|
|
6900
|
+
);
|
|
6901
|
+
}
|
|
6902
|
+
async function getDatasetSnapshot(params) {
|
|
6903
|
+
assertDatasetSnapshotLookup(params);
|
|
6904
|
+
const snapshots = await getDatasetSnapshots(params);
|
|
6905
|
+
if (snapshots.length > 1) {
|
|
6906
|
+
throw new Error(
|
|
6907
|
+
isDatasetSnapshotNameLookup(params) ? `Expected a unique dataset snapshot named "${params.snapshotName}" for ${params.datasetId}` : `Expected a unique dataset snapshot for xact_id "${params.xactId}" in ${params.datasetId}`
|
|
6908
|
+
);
|
|
6909
|
+
}
|
|
6910
|
+
return snapshots[0];
|
|
6911
|
+
}
|
|
6912
|
+
function normalizeDatasetSelection({
|
|
6913
|
+
version,
|
|
6914
|
+
environment,
|
|
6915
|
+
snapshotName
|
|
6916
|
+
}) {
|
|
6917
|
+
if (version !== void 0) {
|
|
6918
|
+
return { version };
|
|
6919
|
+
}
|
|
6920
|
+
if (snapshotName !== void 0) {
|
|
6921
|
+
return { snapshotName };
|
|
6922
|
+
}
|
|
6923
|
+
if (environment !== void 0) {
|
|
6924
|
+
return { environment };
|
|
6925
|
+
}
|
|
6926
|
+
return {};
|
|
6927
|
+
}
|
|
6928
|
+
async function resolveDatasetSnapshotName({
|
|
6929
|
+
state,
|
|
6930
|
+
datasetId,
|
|
6931
|
+
snapshotName
|
|
6932
|
+
}) {
|
|
6933
|
+
const match = await getDatasetSnapshot({
|
|
6934
|
+
state,
|
|
6935
|
+
datasetId,
|
|
6936
|
+
snapshotName
|
|
6937
|
+
});
|
|
6938
|
+
if (match === void 0) {
|
|
6939
|
+
throw new Error(
|
|
6940
|
+
`Dataset snapshot "${snapshotName}" not found for ${datasetId}`
|
|
6941
|
+
);
|
|
6942
|
+
}
|
|
6943
|
+
return match.xact_id;
|
|
6944
|
+
}
|
|
6945
|
+
async function resolveDatasetSnapshotNameForMetadata({
|
|
6946
|
+
state,
|
|
6947
|
+
lazyMetadata,
|
|
6948
|
+
snapshotName
|
|
6949
|
+
}) {
|
|
6950
|
+
const metadata = await lazyMetadata.get();
|
|
6951
|
+
return await resolveDatasetSnapshotName({
|
|
6952
|
+
state,
|
|
6953
|
+
datasetId: metadata.dataset.id,
|
|
6954
|
+
snapshotName
|
|
6955
|
+
});
|
|
6956
|
+
}
|
|
6957
|
+
async function resolveDatasetEnvironment({
|
|
6958
|
+
state,
|
|
6959
|
+
datasetId,
|
|
6960
|
+
environment
|
|
6961
|
+
}) {
|
|
6962
|
+
const environmentObjectPath = `environment-object/dataset/${datasetId}/${encodeURIComponent(environment)}`;
|
|
6963
|
+
const response = state.orgName == null ? await state.apiConn().get_json(environmentObjectPath) : await state.apiConn().get_json(environmentObjectPath, {
|
|
6964
|
+
org_name: state.orgName
|
|
6965
|
+
});
|
|
6966
|
+
return _v3.z.object({ object_version: _v3.z.string() }).parse(response).object_version;
|
|
6967
|
+
}
|
|
6968
|
+
async function resolveDatasetEnvironmentForMetadata({
|
|
6969
|
+
state,
|
|
6970
|
+
lazyMetadata,
|
|
6971
|
+
environment
|
|
6972
|
+
}) {
|
|
6973
|
+
const metadata = await lazyMetadata.get();
|
|
6974
|
+
return await resolveDatasetEnvironment({
|
|
6975
|
+
state,
|
|
6976
|
+
datasetId: metadata.dataset.id,
|
|
6977
|
+
environment
|
|
6978
|
+
});
|
|
6979
|
+
}
|
|
6980
|
+
async function serializeDatasetForExperiment({
|
|
6981
|
+
dataset,
|
|
6982
|
+
state
|
|
6983
|
+
}) {
|
|
6984
|
+
if (!Dataset2.isDataset(dataset)) {
|
|
6985
|
+
const selection2 = normalizeDatasetSelection(dataset);
|
|
6986
|
+
if (selection2.version !== void 0) {
|
|
6987
|
+
return {
|
|
6988
|
+
datasetId: dataset.id,
|
|
6989
|
+
datasetVersion: selection2.version
|
|
6990
|
+
};
|
|
6991
|
+
}
|
|
6992
|
+
if (selection2.snapshotName !== void 0) {
|
|
6993
|
+
return {
|
|
6994
|
+
datasetId: dataset.id,
|
|
6995
|
+
datasetVersion: await resolveDatasetSnapshotName({
|
|
6996
|
+
state,
|
|
6997
|
+
datasetId: dataset.id,
|
|
6998
|
+
snapshotName: selection2.snapshotName
|
|
6999
|
+
})
|
|
7000
|
+
};
|
|
7001
|
+
}
|
|
7002
|
+
if (selection2.environment !== void 0) {
|
|
7003
|
+
return {
|
|
7004
|
+
datasetId: dataset.id,
|
|
7005
|
+
datasetVersion: await resolveDatasetEnvironment({
|
|
7006
|
+
state,
|
|
7007
|
+
datasetId: dataset.id,
|
|
7008
|
+
environment: selection2.environment
|
|
7009
|
+
})
|
|
7010
|
+
};
|
|
7011
|
+
}
|
|
7012
|
+
return {
|
|
7013
|
+
datasetId: dataset.id
|
|
7014
|
+
};
|
|
7015
|
+
}
|
|
7016
|
+
const evalData = await dataset.toEvalData();
|
|
7017
|
+
const selection = normalizeDatasetSelection({
|
|
7018
|
+
version: evalData.dataset_version,
|
|
7019
|
+
environment: evalData.dataset_environment,
|
|
7020
|
+
snapshotName: evalData.dataset_snapshot_name
|
|
7021
|
+
});
|
|
7022
|
+
if (selection.version !== void 0) {
|
|
7023
|
+
return {
|
|
7024
|
+
datasetId: evalData.dataset_id,
|
|
7025
|
+
datasetVersion: selection.version
|
|
7026
|
+
};
|
|
7027
|
+
}
|
|
7028
|
+
const datasetVersion = await dataset.version();
|
|
7029
|
+
if (datasetVersion !== void 0) {
|
|
7030
|
+
return {
|
|
7031
|
+
datasetId: evalData.dataset_id,
|
|
7032
|
+
datasetVersion
|
|
7033
|
+
};
|
|
7034
|
+
}
|
|
7035
|
+
return {
|
|
7036
|
+
datasetId: evalData.dataset_id
|
|
7037
|
+
};
|
|
7038
|
+
}
|
|
6848
7039
|
function initDataset(projectOrOptions, optionalOptions) {
|
|
6849
7040
|
const options = (() => {
|
|
6850
7041
|
if (typeof projectOrOptions === "string") {
|
|
@@ -6863,6 +7054,8 @@ function initDataset(projectOrOptions, optionalOptions) {
|
|
|
6863
7054
|
dataset,
|
|
6864
7055
|
description,
|
|
6865
7056
|
version,
|
|
7057
|
+
snapshotName,
|
|
7058
|
+
environment,
|
|
6866
7059
|
appUrl,
|
|
6867
7060
|
apiKey,
|
|
6868
7061
|
orgName,
|
|
@@ -6874,6 +7067,14 @@ function initDataset(projectOrOptions, optionalOptions) {
|
|
|
6874
7067
|
state: stateArg,
|
|
6875
7068
|
_internal_btql
|
|
6876
7069
|
} = options;
|
|
7070
|
+
const selection = normalizeDatasetSelection({
|
|
7071
|
+
version,
|
|
7072
|
+
environment,
|
|
7073
|
+
snapshotName
|
|
7074
|
+
});
|
|
7075
|
+
const normalizedVersion = selection.version;
|
|
7076
|
+
const normalizedEnvironment = selection.environment;
|
|
7077
|
+
const normalizedSnapshotName = selection.snapshotName;
|
|
6877
7078
|
const state = _nullishCoalesce(stateArg, () => ( _globalState));
|
|
6878
7079
|
const lazyMetadata = new LazyValue(
|
|
6879
7080
|
async () => {
|
|
@@ -6907,13 +7108,38 @@ function initDataset(projectOrOptions, optionalOptions) {
|
|
|
6907
7108
|
};
|
|
6908
7109
|
}
|
|
6909
7110
|
);
|
|
6910
|
-
|
|
7111
|
+
const resolvedVersion = normalizedVersion !== void 0 ? normalizedVersion : normalizedSnapshotName !== void 0 ? new LazyValue(async () => {
|
|
7112
|
+
return await resolveDatasetSnapshotNameForMetadata({
|
|
7113
|
+
state,
|
|
7114
|
+
lazyMetadata,
|
|
7115
|
+
snapshotName: normalizedSnapshotName
|
|
7116
|
+
});
|
|
7117
|
+
}) : normalizedEnvironment !== void 0 ? new LazyValue(async () => {
|
|
7118
|
+
return await resolveDatasetEnvironmentForMetadata({
|
|
7119
|
+
state,
|
|
7120
|
+
lazyMetadata,
|
|
7121
|
+
environment: normalizedEnvironment
|
|
7122
|
+
});
|
|
7123
|
+
}) : void 0;
|
|
7124
|
+
const datasetObject = new Dataset2(
|
|
6911
7125
|
_nullishCoalesce(stateArg, () => ( _globalState)),
|
|
6912
7126
|
lazyMetadata,
|
|
6913
|
-
|
|
7127
|
+
typeof resolvedVersion === "string" ? resolvedVersion : void 0,
|
|
6914
7128
|
legacy,
|
|
6915
|
-
_internal_btql
|
|
7129
|
+
_internal_btql,
|
|
7130
|
+
resolvedVersion instanceof LazyValue || normalizedEnvironment !== void 0 || normalizedSnapshotName !== void 0 ? {
|
|
7131
|
+
...resolvedVersion instanceof LazyValue ? {
|
|
7132
|
+
lazyPinnedVersion: resolvedVersion
|
|
7133
|
+
} : {},
|
|
7134
|
+
...normalizedEnvironment !== void 0 ? {
|
|
7135
|
+
pinnedEnvironment: normalizedEnvironment
|
|
7136
|
+
} : {},
|
|
7137
|
+
...normalizedSnapshotName !== void 0 ? {
|
|
7138
|
+
pinnedSnapshotName: normalizedSnapshotName
|
|
7139
|
+
} : {}
|
|
7140
|
+
} : void 0
|
|
6916
7141
|
);
|
|
7142
|
+
return datasetObject;
|
|
6917
7143
|
}
|
|
6918
7144
|
async function computeLoggerMetadata(state, {
|
|
6919
7145
|
project_name,
|
|
@@ -7050,24 +7276,24 @@ async function loginToState(options = {}) {
|
|
|
7050
7276
|
return state;
|
|
7051
7277
|
}
|
|
7052
7278
|
function currentExperiment(options) {
|
|
7053
|
-
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
7279
|
+
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _86 => _86.state]), () => ( _globalState));
|
|
7054
7280
|
return state.currentExperiment;
|
|
7055
7281
|
}
|
|
7056
7282
|
function currentLogger(options) {
|
|
7057
|
-
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
7058
|
-
return castLogger(state.currentLogger, _optionalChain([options, 'optionalAccess',
|
|
7283
|
+
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _87 => _87.state]), () => ( _globalState));
|
|
7284
|
+
return castLogger(state.currentLogger, _optionalChain([options, 'optionalAccess', _88 => _88.asyncFlush]));
|
|
7059
7285
|
}
|
|
7060
7286
|
function currentSpan(options) {
|
|
7061
|
-
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
7287
|
+
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _89 => _89.state]), () => ( _globalState));
|
|
7062
7288
|
return _nullishCoalesce(state.contextManager.getCurrentSpan(), () => ( NOOP_SPAN));
|
|
7063
7289
|
}
|
|
7064
7290
|
function getSpanParentObject(options) {
|
|
7065
|
-
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
7291
|
+
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _90 => _90.state]), () => ( _globalState));
|
|
7066
7292
|
const parentSpan = currentSpan({ state });
|
|
7067
7293
|
if (!Object.is(parentSpan, NOOP_SPAN)) {
|
|
7068
7294
|
return parentSpan;
|
|
7069
7295
|
}
|
|
7070
|
-
const parentStr = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
7296
|
+
const parentStr = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _91 => _91.parent]), () => ( state.currentParent.getStore()));
|
|
7071
7297
|
if (parentStr) return getSpanComponentsClass().fromStr(parentStr);
|
|
7072
7298
|
const experiment = currentExperiment();
|
|
7073
7299
|
if (experiment) {
|
|
@@ -7096,7 +7322,7 @@ function traced(callback, args) {
|
|
|
7096
7322
|
const { span, isSyncFlushLogger } = startSpanAndIsLogger(args);
|
|
7097
7323
|
const ret = runCatchFinally(
|
|
7098
7324
|
() => {
|
|
7099
|
-
if (_nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
7325
|
+
if (_nullishCoalesce(_optionalChain([args, 'optionalAccess', _92 => _92.setCurrent]), () => ( true))) {
|
|
7100
7326
|
return withCurrent(span, callback);
|
|
7101
7327
|
} else {
|
|
7102
7328
|
return callback(span);
|
|
@@ -7108,7 +7334,7 @@ function traced(callback, args) {
|
|
|
7108
7334
|
},
|
|
7109
7335
|
() => span.end()
|
|
7110
7336
|
);
|
|
7111
|
-
if (_optionalChain([args, 'optionalAccess',
|
|
7337
|
+
if (_optionalChain([args, 'optionalAccess', _93 => _93.asyncFlush]) === void 0 || _optionalChain([args, 'optionalAccess', _94 => _94.asyncFlush])) {
|
|
7112
7338
|
return ret;
|
|
7113
7339
|
} else {
|
|
7114
7340
|
return (async () => {
|
|
@@ -7124,14 +7350,14 @@ function startSpan(args) {
|
|
|
7124
7350
|
return startSpanAndIsLogger(args).span;
|
|
7125
7351
|
}
|
|
7126
7352
|
async function flush(options) {
|
|
7127
|
-
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
7353
|
+
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _95 => _95.state]), () => ( _globalState));
|
|
7128
7354
|
return await state.bgLogger().flush();
|
|
7129
7355
|
}
|
|
7130
7356
|
function startSpanAndIsLogger(args) {
|
|
7131
|
-
const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
7357
|
+
const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _96 => _96.state]), () => ( _globalState));
|
|
7132
7358
|
const parentObject = getSpanParentObject({
|
|
7133
|
-
asyncFlush: _optionalChain([args, 'optionalAccess',
|
|
7134
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
7359
|
+
asyncFlush: _optionalChain([args, 'optionalAccess', _97 => _97.asyncFlush]),
|
|
7360
|
+
parent: _optionalChain([args, 'optionalAccess', _98 => _98.parent]),
|
|
7135
7361
|
state
|
|
7136
7362
|
});
|
|
7137
7363
|
if (parentObject instanceof SpanComponentsV3 || parentObject instanceof SpanComponentsV4) {
|
|
@@ -7148,14 +7374,14 @@ function startSpanAndIsLogger(args) {
|
|
|
7148
7374
|
),
|
|
7149
7375
|
parentComputeObjectMetadataArgs: _nullishCoalesce(parentObject.data.compute_object_metadata_args, () => ( void 0)),
|
|
7150
7376
|
parentSpanIds,
|
|
7151
|
-
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
7377
|
+
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _99 => _99.propagatedEvent]), () => ( // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
7152
7378
|
(_nullishCoalesce(parentObject.data.propagated_event, () => ( void 0)))))
|
|
7153
7379
|
});
|
|
7154
7380
|
return {
|
|
7155
7381
|
span,
|
|
7156
7382
|
isSyncFlushLogger: parentObject.data.object_type === 2 /* PROJECT_LOGS */ && // Since there's no parent logger here, we're free to choose the async flush
|
|
7157
7383
|
// behavior, and therefore propagate along whatever we get from the arguments
|
|
7158
|
-
_optionalChain([args, 'optionalAccess',
|
|
7384
|
+
_optionalChain([args, 'optionalAccess', _100 => _100.asyncFlush]) === false
|
|
7159
7385
|
};
|
|
7160
7386
|
} else {
|
|
7161
7387
|
const span = parentObject.startSpan(args);
|
|
@@ -7322,10 +7548,10 @@ function extractAttachments(event, attachments) {
|
|
|
7322
7548
|
event[key] = value.reference;
|
|
7323
7549
|
continue;
|
|
7324
7550
|
}
|
|
7325
|
-
if (_optionalChain([value, 'optionalAccess',
|
|
7551
|
+
if (_optionalChain([value, 'optionalAccess', _101 => _101.type]) === BRAINTRUST_ATTACHMENT && value.key && !value.uploader) {
|
|
7326
7552
|
continue;
|
|
7327
7553
|
}
|
|
7328
|
-
if (_optionalChain([value, 'optionalAccess',
|
|
7554
|
+
if (_optionalChain([value, 'optionalAccess', _102 => _102.reference, 'optionalAccess', _103 => _103.type]) === BRAINTRUST_ATTACHMENT && _optionalChain([value, 'optionalAccess', _104 => _104.uploader])) {
|
|
7329
7555
|
const attachment = new Attachment({
|
|
7330
7556
|
data: value.dataDebugString,
|
|
7331
7557
|
filename: value.reference.filename,
|
|
@@ -7405,11 +7631,20 @@ var ObjectFetcher = (_class11 = class {
|
|
|
7405
7631
|
async getState() {
|
|
7406
7632
|
throw new Error("ObjectFetcher subclasses must have a 'getState' method");
|
|
7407
7633
|
}
|
|
7634
|
+
getPinnedVersion() {
|
|
7635
|
+
return this.pinnedVersion;
|
|
7636
|
+
}
|
|
7637
|
+
setPinnedVersion(pinnedVersion) {
|
|
7638
|
+
this.pinnedVersion = pinnedVersion;
|
|
7639
|
+
}
|
|
7640
|
+
getInternalBtql() {
|
|
7641
|
+
return this._internal_btql;
|
|
7642
|
+
}
|
|
7408
7643
|
async *fetchRecordsFromApi(batchSize) {
|
|
7409
7644
|
const state = await this.getState();
|
|
7410
7645
|
const objectId = await this.id;
|
|
7411
7646
|
const batchLimit = _nullishCoalesce(batchSize, () => ( DEFAULT_FETCH_BATCH_SIZE));
|
|
7412
|
-
const internalLimit =
|
|
7647
|
+
const internalLimit = getInternalBtqlLimit(this._internal_btql);
|
|
7413
7648
|
const limit = batchSize !== void 0 ? batchSize : _nullishCoalesce(internalLimit, () => ( batchLimit));
|
|
7414
7649
|
const internalBtqlWithoutReservedQueryKeys = Object.fromEntries(
|
|
7415
7650
|
Object.entries(_nullishCoalesce(this._internal_btql, () => ( {}))).filter(
|
|
@@ -7483,7 +7718,7 @@ var ObjectFetcher = (_class11 = class {
|
|
|
7483
7718
|
}
|
|
7484
7719
|
return;
|
|
7485
7720
|
}
|
|
7486
|
-
for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess',
|
|
7721
|
+
for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _105 => _105.batchSize]))) {
|
|
7487
7722
|
yield record;
|
|
7488
7723
|
}
|
|
7489
7724
|
}
|
|
@@ -7493,7 +7728,7 @@ var ObjectFetcher = (_class11 = class {
|
|
|
7493
7728
|
async fetchedData(options) {
|
|
7494
7729
|
if (this._fetchedData === void 0) {
|
|
7495
7730
|
const data = [];
|
|
7496
|
-
for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess',
|
|
7731
|
+
for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _106 => _106.batchSize]))) {
|
|
7497
7732
|
data.push(record);
|
|
7498
7733
|
}
|
|
7499
7734
|
this._fetchedData = data;
|
|
@@ -7588,7 +7823,7 @@ var Experiment2 = (_class12 = class extends ObjectFetcher {
|
|
|
7588
7823
|
* @returns The `id` of the logged event.
|
|
7589
7824
|
*/
|
|
7590
7825
|
log(event, options) {
|
|
7591
|
-
if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess',
|
|
7826
|
+
if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _107 => _107.allowConcurrentWithSpans])) {
|
|
7592
7827
|
throw new Error(
|
|
7593
7828
|
"Cannot run toplevel `log` method while using spans. To log to the span, call `experiment.traced` and then log with `span.log`"
|
|
7594
7829
|
);
|
|
@@ -7641,12 +7876,12 @@ var Experiment2 = (_class12 = class extends ObjectFetcher {
|
|
|
7641
7876
|
state: this.state,
|
|
7642
7877
|
...startSpanParentArgs({
|
|
7643
7878
|
state: this.state,
|
|
7644
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
7879
|
+
parent: _optionalChain([args, 'optionalAccess', _108 => _108.parent]),
|
|
7645
7880
|
parentObjectType: this.parentObjectType(),
|
|
7646
7881
|
parentObjectId: this.lazyId,
|
|
7647
7882
|
parentComputeObjectMetadataArgs: void 0,
|
|
7648
7883
|
parentSpanIds: void 0,
|
|
7649
|
-
propagatedEvent: _optionalChain([args, 'optionalAccess',
|
|
7884
|
+
propagatedEvent: _optionalChain([args, 'optionalAccess', _109 => _109.propagatedEvent])
|
|
7650
7885
|
}),
|
|
7651
7886
|
defaultRootType: "eval" /* EVAL */
|
|
7652
7887
|
});
|
|
@@ -7990,7 +8225,7 @@ var SpanImpl = (_class13 = class _SpanImpl {
|
|
|
7990
8225
|
...serializableInternalData,
|
|
7991
8226
|
[IS_MERGE_FIELD]: this.isMerge
|
|
7992
8227
|
});
|
|
7993
|
-
if (typeof _optionalChain([partialRecord, 'access',
|
|
8228
|
+
if (typeof _optionalChain([partialRecord, 'access', _110 => _110.metrics, 'optionalAccess', _111 => _111.end]) === "number") {
|
|
7994
8229
|
this.loggedEndTime = partialRecord.metrics.end;
|
|
7995
8230
|
}
|
|
7996
8231
|
if (this.parentObjectType === 1 /* EXPERIMENT */) {
|
|
@@ -8050,18 +8285,18 @@ var SpanImpl = (_class13 = class _SpanImpl {
|
|
|
8050
8285
|
);
|
|
8051
8286
|
}
|
|
8052
8287
|
startSpan(args) {
|
|
8053
|
-
const parentSpanIds = _optionalChain([args, 'optionalAccess',
|
|
8288
|
+
const parentSpanIds = _optionalChain([args, 'optionalAccess', _112 => _112.parent]) ? void 0 : { spanId: this._spanId, rootSpanId: this._rootSpanId };
|
|
8054
8289
|
return new _SpanImpl({
|
|
8055
8290
|
state: this._state,
|
|
8056
8291
|
...args,
|
|
8057
8292
|
...startSpanParentArgs({
|
|
8058
8293
|
state: this._state,
|
|
8059
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
8294
|
+
parent: _optionalChain([args, 'optionalAccess', _113 => _113.parent]),
|
|
8060
8295
|
parentObjectType: this.parentObjectType,
|
|
8061
8296
|
parentObjectId: this.parentObjectId,
|
|
8062
8297
|
parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
|
|
8063
8298
|
parentSpanIds,
|
|
8064
|
-
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
8299
|
+
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _114 => _114.propagatedEvent]), () => ( this.propagatedEvent))
|
|
8065
8300
|
})
|
|
8066
8301
|
});
|
|
8067
8302
|
}
|
|
@@ -8075,12 +8310,12 @@ var SpanImpl = (_class13 = class _SpanImpl {
|
|
|
8075
8310
|
...args,
|
|
8076
8311
|
...startSpanParentArgs({
|
|
8077
8312
|
state: this._state,
|
|
8078
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
8313
|
+
parent: _optionalChain([args, 'optionalAccess', _115 => _115.parent]),
|
|
8079
8314
|
parentObjectType: this.parentObjectType,
|
|
8080
8315
|
parentObjectId: this.parentObjectId,
|
|
8081
8316
|
parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
|
|
8082
8317
|
parentSpanIds,
|
|
8083
|
-
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
8318
|
+
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _116 => _116.propagatedEvent]), () => ( this.propagatedEvent))
|
|
8084
8319
|
}),
|
|
8085
8320
|
spanId
|
|
8086
8321
|
});
|
|
@@ -8089,7 +8324,7 @@ var SpanImpl = (_class13 = class _SpanImpl {
|
|
|
8089
8324
|
let endTime;
|
|
8090
8325
|
let internalData = {};
|
|
8091
8326
|
if (!this.loggedEndTime) {
|
|
8092
|
-
endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
8327
|
+
endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _117 => _117.endTime]), () => ( getCurrentUnixTimestamp()));
|
|
8093
8328
|
internalData = { metrics: { end: endTime } };
|
|
8094
8329
|
} else {
|
|
8095
8330
|
endTime = this.loggedEndTime;
|
|
@@ -8140,8 +8375,8 @@ var SpanImpl = (_class13 = class _SpanImpl {
|
|
|
8140
8375
|
const args = this.parentComputeObjectMetadataArgs;
|
|
8141
8376
|
switch (this.parentObjectType) {
|
|
8142
8377
|
case 2 /* PROJECT_LOGS */: {
|
|
8143
|
-
const projectID = _optionalChain([args, 'optionalAccess',
|
|
8144
|
-
const projectName = _optionalChain([args, 'optionalAccess',
|
|
8378
|
+
const projectID = _optionalChain([args, 'optionalAccess', _118 => _118.project_id]) || this.parentObjectId.getSync().value;
|
|
8379
|
+
const projectName = _optionalChain([args, 'optionalAccess', _119 => _119.project_name]);
|
|
8145
8380
|
if (projectID) {
|
|
8146
8381
|
return `${baseUrl}/object?object_type=project_logs&object_id=${projectID}&id=${this._id}`;
|
|
8147
8382
|
} else if (projectName) {
|
|
@@ -8151,7 +8386,7 @@ var SpanImpl = (_class13 = class _SpanImpl {
|
|
|
8151
8386
|
}
|
|
8152
8387
|
}
|
|
8153
8388
|
case 1 /* EXPERIMENT */: {
|
|
8154
|
-
const expID = _optionalChain([args, 'optionalAccess',
|
|
8389
|
+
const expID = _optionalChain([args, 'optionalAccess', _120 => _120.experiment_id]) || _optionalChain([this, 'access', _121 => _121.parentObjectId, 'optionalAccess', _122 => _122.getSync, 'call', _123 => _123(), 'optionalAccess', _124 => _124.value]);
|
|
8155
8390
|
if (!expID) {
|
|
8156
8391
|
return getErrPermlink("provide-experiment-id");
|
|
8157
8392
|
} else {
|
|
@@ -8223,7 +8458,7 @@ function splitLoggingData({
|
|
|
8223
8458
|
return [serializableInternalData, lazyInternalData];
|
|
8224
8459
|
}
|
|
8225
8460
|
var Dataset2 = (_class14 = class extends ObjectFetcher {
|
|
8226
|
-
constructor(state, lazyMetadata, pinnedVersion, legacy, _internal_btql) {
|
|
8461
|
+
constructor(state, lazyMetadata, pinnedVersion, legacy, _internal_btql, pinState) {
|
|
8227
8462
|
const isLegacyDataset = _nullishCoalesce(legacy, () => ( DEFAULT_IS_LEGACY_DATASET));
|
|
8228
8463
|
if (isLegacyDataset) {
|
|
8229
8464
|
debugLogger.forState(state).warn(
|
|
@@ -8244,10 +8479,16 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
|
|
|
8244
8479
|
);_class14.prototype.__init57.call(this);_class14.prototype.__init58.call(this);;
|
|
8245
8480
|
this.state = state;
|
|
8246
8481
|
this.lazyMetadata = lazyMetadata;
|
|
8482
|
+
this.lazyPinnedVersion = _optionalChain([pinState, 'optionalAccess', _125 => _125.lazyPinnedVersion]);
|
|
8483
|
+
this.pinnedEnvironment = _optionalChain([pinState, 'optionalAccess', _126 => _126.pinnedEnvironment]);
|
|
8484
|
+
this.pinnedSnapshotName = _optionalChain([pinState, 'optionalAccess', _127 => _127.pinnedSnapshotName]);
|
|
8247
8485
|
}
|
|
8248
8486
|
|
|
8249
8487
|
__init57() {this.__braintrust_dataset_marker = true}
|
|
8250
8488
|
__init58() {this.newRecords = 0}
|
|
8489
|
+
|
|
8490
|
+
|
|
8491
|
+
|
|
8251
8492
|
get id() {
|
|
8252
8493
|
return (async () => {
|
|
8253
8494
|
return (await this.lazyMetadata.get()).dataset.id;
|
|
@@ -8266,10 +8507,40 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
|
|
|
8266
8507
|
get loggingState() {
|
|
8267
8508
|
return this.state;
|
|
8268
8509
|
}
|
|
8510
|
+
async toEvalData() {
|
|
8511
|
+
await this.getState();
|
|
8512
|
+
const metadata = await this.lazyMetadata.get();
|
|
8513
|
+
const pinnedVersion = this.getPinnedVersion();
|
|
8514
|
+
const internalBtql = this.getInternalBtql();
|
|
8515
|
+
return {
|
|
8516
|
+
dataset_id: metadata.dataset.id,
|
|
8517
|
+
...this.pinnedEnvironment !== void 0 ? {
|
|
8518
|
+
dataset_environment: this.pinnedEnvironment
|
|
8519
|
+
} : {},
|
|
8520
|
+
...this.pinnedEnvironment === void 0 && this.pinnedSnapshotName !== void 0 ? {
|
|
8521
|
+
dataset_snapshot_name: this.pinnedSnapshotName
|
|
8522
|
+
} : {},
|
|
8523
|
+
...this.pinnedEnvironment === void 0 && this.pinnedSnapshotName === void 0 && pinnedVersion !== void 0 ? {
|
|
8524
|
+
dataset_version: pinnedVersion
|
|
8525
|
+
} : {},
|
|
8526
|
+
...internalBtql !== void 0 ? { _internal_btql: internalBtql } : {}
|
|
8527
|
+
};
|
|
8528
|
+
}
|
|
8269
8529
|
async getState() {
|
|
8270
8530
|
await this.lazyMetadata.get();
|
|
8531
|
+
if (this.lazyPinnedVersion !== void 0 && this.getPinnedVersion() === void 0) {
|
|
8532
|
+
this.setPinnedVersion(await this.lazyPinnedVersion.get());
|
|
8533
|
+
}
|
|
8271
8534
|
return this.state;
|
|
8272
8535
|
}
|
|
8536
|
+
async version(options) {
|
|
8537
|
+
const pinnedVersion = this.getPinnedVersion();
|
|
8538
|
+
if (pinnedVersion !== void 0) {
|
|
8539
|
+
return pinnedVersion;
|
|
8540
|
+
}
|
|
8541
|
+
await this.getState();
|
|
8542
|
+
return await super.version(options);
|
|
8543
|
+
}
|
|
8273
8544
|
validateEvent({
|
|
8274
8545
|
metadata,
|
|
8275
8546
|
expected,
|
|
@@ -8405,6 +8676,88 @@ var Dataset2 = (_class14 = class extends ObjectFetcher {
|
|
|
8405
8676
|
this.state.bgLogger().log([args]);
|
|
8406
8677
|
return id;
|
|
8407
8678
|
}
|
|
8679
|
+
async createSnapshot({
|
|
8680
|
+
name,
|
|
8681
|
+
description,
|
|
8682
|
+
update
|
|
8683
|
+
}) {
|
|
8684
|
+
await this.flush();
|
|
8685
|
+
const state = await this.getState();
|
|
8686
|
+
const datasetId = await this.id;
|
|
8687
|
+
const currentVersion = await this.version();
|
|
8688
|
+
if (currentVersion === void 0) {
|
|
8689
|
+
throw new Error("Cannot create snapshot: dataset has no version");
|
|
8690
|
+
}
|
|
8691
|
+
const response = await state.appConn().post_json("api/dataset_snapshot/register", {
|
|
8692
|
+
dataset_id: datasetId,
|
|
8693
|
+
dataset_snapshot_name: name,
|
|
8694
|
+
description,
|
|
8695
|
+
xact_id: currentVersion,
|
|
8696
|
+
update
|
|
8697
|
+
});
|
|
8698
|
+
return datasetSnapshotRegisterResponseSchema.parse(response).dataset_snapshot;
|
|
8699
|
+
}
|
|
8700
|
+
async listSnapshots() {
|
|
8701
|
+
const state = await this.getState();
|
|
8702
|
+
return await getDatasetSnapshots({
|
|
8703
|
+
state,
|
|
8704
|
+
datasetId: await this.id
|
|
8705
|
+
});
|
|
8706
|
+
}
|
|
8707
|
+
async getSnapshot(lookup) {
|
|
8708
|
+
const state = await this.getState();
|
|
8709
|
+
const datasetId = await this.id;
|
|
8710
|
+
return await getDatasetSnapshot({
|
|
8711
|
+
state,
|
|
8712
|
+
datasetId,
|
|
8713
|
+
...lookup
|
|
8714
|
+
});
|
|
8715
|
+
}
|
|
8716
|
+
async updateSnapshot(snapshotId, {
|
|
8717
|
+
name,
|
|
8718
|
+
description
|
|
8719
|
+
}) {
|
|
8720
|
+
const state = await this.getState();
|
|
8721
|
+
return DatasetSnapshot.parse(
|
|
8722
|
+
await state.appConn().post_json("api/dataset_snapshot/patch_id", {
|
|
8723
|
+
id: snapshotId,
|
|
8724
|
+
name,
|
|
8725
|
+
description
|
|
8726
|
+
})
|
|
8727
|
+
);
|
|
8728
|
+
}
|
|
8729
|
+
async deleteSnapshot(snapshotId) {
|
|
8730
|
+
const state = await this.getState();
|
|
8731
|
+
return DatasetSnapshot.parse(
|
|
8732
|
+
await state.appConn().post_json("api/dataset_snapshot/delete_id", {
|
|
8733
|
+
id: snapshotId
|
|
8734
|
+
})
|
|
8735
|
+
);
|
|
8736
|
+
}
|
|
8737
|
+
async restorePreview({
|
|
8738
|
+
version
|
|
8739
|
+
}) {
|
|
8740
|
+
await this.flush();
|
|
8741
|
+
const state = await this.getState();
|
|
8742
|
+
const datasetId = await this.id;
|
|
8743
|
+
return datasetRestorePreviewResultSchema.parse(
|
|
8744
|
+
await state.apiConn().post_json(`v1/dataset/${datasetId}/restore/preview`, {
|
|
8745
|
+
version
|
|
8746
|
+
})
|
|
8747
|
+
);
|
|
8748
|
+
}
|
|
8749
|
+
async restore({
|
|
8750
|
+
version
|
|
8751
|
+
}) {
|
|
8752
|
+
await this.flush();
|
|
8753
|
+
const state = await this.getState();
|
|
8754
|
+
const datasetId = await this.id;
|
|
8755
|
+
return datasetRestoreResultSchema.parse(
|
|
8756
|
+
await state.apiConn().post_json(`v1/dataset/${datasetId}/restore`, {
|
|
8757
|
+
version
|
|
8758
|
+
})
|
|
8759
|
+
);
|
|
8760
|
+
}
|
|
8408
8761
|
/**
|
|
8409
8762
|
* Summarize the dataset, including high level metrics about its size and other metadata.
|
|
8410
8763
|
* @param summarizeData Whether to summarize the data. If false, only the metadata will be returned.
|
|
@@ -8651,16 +9004,16 @@ var Prompt2 = (_class15 = class _Prompt {
|
|
|
8651
9004
|
return "slug" in this.metadata ? this.metadata.slug : this.metadata.id;
|
|
8652
9005
|
}
|
|
8653
9006
|
get prompt() {
|
|
8654
|
-
return _optionalChain([this, 'access',
|
|
9007
|
+
return _optionalChain([this, 'access', _128 => _128.getParsedPromptData, 'call', _129 => _129(), 'optionalAccess', _130 => _130.prompt]);
|
|
8655
9008
|
}
|
|
8656
9009
|
get version() {
|
|
8657
9010
|
return this.metadata[TRANSACTION_ID_FIELD];
|
|
8658
9011
|
}
|
|
8659
9012
|
get options() {
|
|
8660
|
-
return _optionalChain([this, 'access',
|
|
9013
|
+
return _optionalChain([this, 'access', _131 => _131.getParsedPromptData, 'call', _132 => _132(), 'optionalAccess', _133 => _133.options]) || {};
|
|
8661
9014
|
}
|
|
8662
9015
|
get templateFormat() {
|
|
8663
|
-
return _optionalChain([this, 'access',
|
|
9016
|
+
return _optionalChain([this, 'access', _134 => _134.getParsedPromptData, 'call', _135 => _135(), 'optionalAccess', _136 => _136.template_format]);
|
|
8664
9017
|
}
|
|
8665
9018
|
get promptData() {
|
|
8666
9019
|
return this.getParsedPromptData();
|
|
@@ -8822,7 +9175,7 @@ var Prompt2 = (_class15 = class _Prompt {
|
|
|
8822
9175
|
return {
|
|
8823
9176
|
type: "chat",
|
|
8824
9177
|
messages,
|
|
8825
|
-
..._optionalChain([prompt, 'access',
|
|
9178
|
+
..._optionalChain([prompt, 'access', _137 => _137.tools, 'optionalAccess', _138 => _138.trim, 'call', _139 => _139()]) ? {
|
|
8826
9179
|
tools: render(prompt.tools)
|
|
8827
9180
|
} : void 0
|
|
8828
9181
|
};
|
|
@@ -9140,7 +9493,7 @@ function getChannelSpanInfo(event) {
|
|
|
9140
9493
|
if (isObject(event.span_info)) {
|
|
9141
9494
|
return event.span_info;
|
|
9142
9495
|
}
|
|
9143
|
-
const firstArg = _optionalChain([event, 'access',
|
|
9496
|
+
const firstArg = _optionalChain([event, 'access', _140 => _140.arguments, 'optionalAccess', _141 => _141[0]]);
|
|
9144
9497
|
if (hasChannelSpanInfo(firstArg)) {
|
|
9145
9498
|
return firstArg.span_info;
|
|
9146
9499
|
}
|
|
@@ -9151,13 +9504,13 @@ function buildStartSpanArgs(config, event) {
|
|
|
9151
9504
|
const spanAttributes = {
|
|
9152
9505
|
type: config.type
|
|
9153
9506
|
};
|
|
9154
|
-
if (isObject(_optionalChain([spanInfo, 'optionalAccess',
|
|
9507
|
+
if (isObject(_optionalChain([spanInfo, 'optionalAccess', _142 => _142.spanAttributes]))) {
|
|
9155
9508
|
mergeDicts(spanAttributes, spanInfo.spanAttributes);
|
|
9156
9509
|
}
|
|
9157
9510
|
return {
|
|
9158
|
-
name: typeof _optionalChain([spanInfo, 'optionalAccess',
|
|
9511
|
+
name: typeof _optionalChain([spanInfo, 'optionalAccess', _143 => _143.name]) === "string" && spanInfo.name ? spanInfo.name : config.name,
|
|
9159
9512
|
spanAttributes,
|
|
9160
|
-
spanInfoMetadata: isObject(_optionalChain([spanInfo, 'optionalAccess',
|
|
9513
|
+
spanInfoMetadata: isObject(_optionalChain([spanInfo, 'optionalAccess', _144 => _144.metadata])) ? spanInfo.metadata : void 0
|
|
9161
9514
|
};
|
|
9162
9515
|
}
|
|
9163
9516
|
function mergeInputMetadata(metadata, spanInfoMetadata) {
|
|
@@ -9247,7 +9600,7 @@ var BasePlugin = (_class17 = class {constructor() { _class17.prototype.__init62.
|
|
|
9247
9600
|
try {
|
|
9248
9601
|
const output = config.extractOutput(event.result, event);
|
|
9249
9602
|
const metrics = config.extractMetrics(event.result, startTime, event);
|
|
9250
|
-
const metadata = _optionalChain([config, 'access',
|
|
9603
|
+
const metadata = _optionalChain([config, 'access', _145 => _145.extractMetadata, 'optionalCall', _146 => _146(event.result, event)]);
|
|
9251
9604
|
span.log({
|
|
9252
9605
|
output,
|
|
9253
9606
|
...metadata !== void 0 ? { metadata } : {},
|
|
@@ -9567,7 +9920,7 @@ function ensureSpanStateForEvent(states, config, event, channelName) {
|
|
|
9567
9920
|
function bindCurrentSpanStoreToStart(tracingChannel2, states, config, channelName) {
|
|
9568
9921
|
const state = _internalGetGlobalState();
|
|
9569
9922
|
const startChannel = tracingChannel2.start;
|
|
9570
|
-
const contextManager = _optionalChain([state, 'optionalAccess',
|
|
9923
|
+
const contextManager = _optionalChain([state, 'optionalAccess', _147 => _147.contextManager]);
|
|
9571
9924
|
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
9572
9925
|
if (!currentSpanStore || !startChannel) {
|
|
9573
9926
|
return void 0;
|
|
@@ -9655,7 +10008,7 @@ function traceAsyncChannel(channel2, config) {
|
|
|
9655
10008
|
startTime,
|
|
9656
10009
|
asyncEndEvent
|
|
9657
10010
|
);
|
|
9658
|
-
const metadata = _optionalChain([config, 'access',
|
|
10011
|
+
const metadata = _optionalChain([config, 'access', _148 => _148.extractMetadata, 'optionalCall', _149 => _149(
|
|
9659
10012
|
asyncEndEvent.result,
|
|
9660
10013
|
asyncEndEvent
|
|
9661
10014
|
)]);
|
|
@@ -9677,7 +10030,7 @@ function traceAsyncChannel(channel2, config) {
|
|
|
9677
10030
|
};
|
|
9678
10031
|
tracingChannel2.subscribe(handlers);
|
|
9679
10032
|
return () => {
|
|
9680
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
10033
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _150 => _150()]);
|
|
9681
10034
|
tracingChannel2.unsubscribe(handlers);
|
|
9682
10035
|
};
|
|
9683
10036
|
}
|
|
@@ -9783,7 +10136,7 @@ function traceStreamingChannel(channel2, config) {
|
|
|
9783
10136
|
});
|
|
9784
10137
|
return;
|
|
9785
10138
|
}
|
|
9786
|
-
if (_optionalChain([config, 'access',
|
|
10139
|
+
if (_optionalChain([config, 'access', _151 => _151.patchResult, 'optionalCall', _152 => _152({
|
|
9787
10140
|
channelName,
|
|
9788
10141
|
endEvent: asyncEndEvent,
|
|
9789
10142
|
result: asyncEndEvent.result,
|
|
@@ -9803,7 +10156,7 @@ function traceStreamingChannel(channel2, config) {
|
|
|
9803
10156
|
startTime,
|
|
9804
10157
|
asyncEndEvent
|
|
9805
10158
|
);
|
|
9806
|
-
const metadata = _optionalChain([config, 'access',
|
|
10159
|
+
const metadata = _optionalChain([config, 'access', _153 => _153.extractMetadata, 'optionalCall', _154 => _154(
|
|
9807
10160
|
asyncEndEvent.result,
|
|
9808
10161
|
asyncEndEvent
|
|
9809
10162
|
)]);
|
|
@@ -9836,7 +10189,7 @@ function traceStreamingChannel(channel2, config) {
|
|
|
9836
10189
|
};
|
|
9837
10190
|
tracingChannel2.subscribe(handlers);
|
|
9838
10191
|
return () => {
|
|
9839
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
10192
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _155 => _155()]);
|
|
9840
10193
|
tracingChannel2.unsubscribe(handlers);
|
|
9841
10194
|
};
|
|
9842
10195
|
}
|
|
@@ -9871,7 +10224,7 @@ function traceSyncStreamChannel(channel2, config) {
|
|
|
9871
10224
|
...endEvent,
|
|
9872
10225
|
result
|
|
9873
10226
|
};
|
|
9874
|
-
if (_optionalChain([config, 'access',
|
|
10227
|
+
if (_optionalChain([config, 'access', _156 => _156.patchResult, 'optionalCall', _157 => _157({
|
|
9875
10228
|
channelName,
|
|
9876
10229
|
endEvent: resolvedEndEvent,
|
|
9877
10230
|
result,
|
|
@@ -9952,7 +10305,7 @@ function traceSyncStreamChannel(channel2, config) {
|
|
|
9952
10305
|
};
|
|
9953
10306
|
tracingChannel2.subscribe(handlers);
|
|
9954
10307
|
return () => {
|
|
9955
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
10308
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _158 => _158()]);
|
|
9956
10309
|
tracingChannel2.unsubscribe(handlers);
|
|
9957
10310
|
};
|
|
9958
10311
|
}
|
|
@@ -10027,7 +10380,7 @@ function processInputAttachments(input) {
|
|
|
10027
10380
|
let attachmentIndex = 0;
|
|
10028
10381
|
const inferMediaTypeFromDataUrl = (value, fallback2) => {
|
|
10029
10382
|
const mediaTypeMatch = value.match(/^data:([^;]+);/);
|
|
10030
|
-
return _optionalChain([mediaTypeMatch, 'optionalAccess',
|
|
10383
|
+
return _optionalChain([mediaTypeMatch, 'optionalAccess', _159 => _159[1]]) || fallback2;
|
|
10031
10384
|
};
|
|
10032
10385
|
const toAttachment = (value, mediaType, filename) => {
|
|
10033
10386
|
const blob = convertDataToBlob(value, mediaType);
|
|
@@ -10281,11 +10634,11 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10281
10634
|
};
|
|
10282
10635
|
},
|
|
10283
10636
|
extractOutput: (result) => {
|
|
10284
|
-
return _optionalChain([result, 'optionalAccess',
|
|
10637
|
+
return _optionalChain([result, 'optionalAccess', _160 => _160.choices]);
|
|
10285
10638
|
},
|
|
10286
10639
|
extractMetrics: (result, startTime, endEvent) => {
|
|
10287
10640
|
const metrics = withCachedMetric(
|
|
10288
|
-
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
10641
|
+
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _161 => _161.usage])),
|
|
10289
10642
|
result,
|
|
10290
10643
|
endEvent
|
|
10291
10644
|
);
|
|
@@ -10309,12 +10662,12 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10309
10662
|
};
|
|
10310
10663
|
},
|
|
10311
10664
|
extractOutput: (result) => {
|
|
10312
|
-
const embedding = _optionalChain([result, 'optionalAccess',
|
|
10665
|
+
const embedding = _optionalChain([result, 'optionalAccess', _162 => _162.data, 'optionalAccess', _163 => _163[0], 'optionalAccess', _164 => _164.embedding]);
|
|
10313
10666
|
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
10314
10667
|
},
|
|
10315
10668
|
extractMetrics: (result, _startTime, endEvent) => {
|
|
10316
10669
|
return withCachedMetric(
|
|
10317
|
-
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
10670
|
+
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _165 => _165.usage])),
|
|
10318
10671
|
result,
|
|
10319
10672
|
endEvent
|
|
10320
10673
|
);
|
|
@@ -10333,11 +10686,11 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10333
10686
|
};
|
|
10334
10687
|
},
|
|
10335
10688
|
extractOutput: (result) => {
|
|
10336
|
-
return _optionalChain([result, 'optionalAccess',
|
|
10689
|
+
return _optionalChain([result, 'optionalAccess', _166 => _166.choices]);
|
|
10337
10690
|
},
|
|
10338
10691
|
extractMetrics: (result, startTime, endEvent) => {
|
|
10339
10692
|
const metrics = withCachedMetric(
|
|
10340
|
-
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
10693
|
+
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _167 => _167.usage])),
|
|
10341
10694
|
result,
|
|
10342
10695
|
endEvent
|
|
10343
10696
|
);
|
|
@@ -10374,11 +10727,11 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10374
10727
|
};
|
|
10375
10728
|
},
|
|
10376
10729
|
extractOutput: (result) => {
|
|
10377
|
-
return _optionalChain([result, 'optionalAccess',
|
|
10730
|
+
return _optionalChain([result, 'optionalAccess', _168 => _168.results]);
|
|
10378
10731
|
},
|
|
10379
10732
|
extractMetrics: (result, _startTime, endEvent) => {
|
|
10380
10733
|
return withCachedMetric(
|
|
10381
|
-
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
10734
|
+
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _169 => _169.usage])),
|
|
10382
10735
|
result,
|
|
10383
10736
|
endEvent
|
|
10384
10737
|
);
|
|
@@ -10397,7 +10750,7 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10397
10750
|
};
|
|
10398
10751
|
},
|
|
10399
10752
|
extractOutput: (result) => {
|
|
10400
|
-
return processImagesInOutput(_optionalChain([result, 'optionalAccess',
|
|
10753
|
+
return processImagesInOutput(_optionalChain([result, 'optionalAccess', _170 => _170.output]));
|
|
10401
10754
|
},
|
|
10402
10755
|
extractMetadata: (result) => {
|
|
10403
10756
|
if (!result) {
|
|
@@ -10408,7 +10761,7 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10408
10761
|
},
|
|
10409
10762
|
extractMetrics: (result, startTime, endEvent) => {
|
|
10410
10763
|
const metrics = withCachedMetric(
|
|
10411
|
-
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
10764
|
+
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _171 => _171.usage])),
|
|
10412
10765
|
result,
|
|
10413
10766
|
endEvent
|
|
10414
10767
|
);
|
|
@@ -10461,7 +10814,7 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10461
10814
|
};
|
|
10462
10815
|
},
|
|
10463
10816
|
extractOutput: (result) => {
|
|
10464
|
-
return processImagesInOutput(_optionalChain([result, 'optionalAccess',
|
|
10817
|
+
return processImagesInOutput(_optionalChain([result, 'optionalAccess', _172 => _172.output]));
|
|
10465
10818
|
},
|
|
10466
10819
|
extractMetadata: (result) => {
|
|
10467
10820
|
if (!result) {
|
|
@@ -10472,7 +10825,7 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10472
10825
|
},
|
|
10473
10826
|
extractMetrics: (result, startTime, endEvent) => {
|
|
10474
10827
|
const metrics = withCachedMetric(
|
|
10475
|
-
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
10828
|
+
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _173 => _173.usage])),
|
|
10476
10829
|
result,
|
|
10477
10830
|
endEvent
|
|
10478
10831
|
);
|
|
@@ -10496,7 +10849,7 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10496
10849
|
};
|
|
10497
10850
|
},
|
|
10498
10851
|
extractOutput: (result) => {
|
|
10499
|
-
return processImagesInOutput(_optionalChain([result, 'optionalAccess',
|
|
10852
|
+
return processImagesInOutput(_optionalChain([result, 'optionalAccess', _174 => _174.output]));
|
|
10500
10853
|
},
|
|
10501
10854
|
extractMetadata: (result) => {
|
|
10502
10855
|
if (!result) {
|
|
@@ -10507,7 +10860,7 @@ var OpenAIPlugin = class extends BasePlugin {
|
|
|
10507
10860
|
},
|
|
10508
10861
|
extractMetrics: (result, startTime, endEvent) => {
|
|
10509
10862
|
const metrics = withCachedMetric(
|
|
10510
|
-
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
10863
|
+
parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _175 => _175.usage])),
|
|
10511
10864
|
result,
|
|
10512
10865
|
endEvent
|
|
10513
10866
|
);
|
|
@@ -10609,11 +10962,11 @@ function aggregateChatLogprobs(existing, incoming) {
|
|
|
10609
10962
|
return _nullishCoalesce(existing, () => ( null));
|
|
10610
10963
|
}
|
|
10611
10964
|
const aggregated = existing && existing !== null ? { ...existing, ...incoming } : { ...incoming };
|
|
10612
|
-
const content = mergeLogprobTokens(_optionalChain([existing, 'optionalAccess',
|
|
10965
|
+
const content = mergeLogprobTokens(_optionalChain([existing, 'optionalAccess', _176 => _176.content]), incoming.content);
|
|
10613
10966
|
if (content !== void 0) {
|
|
10614
10967
|
aggregated.content = content;
|
|
10615
10968
|
}
|
|
10616
|
-
const refusal = mergeLogprobTokens(_optionalChain([existing, 'optionalAccess',
|
|
10969
|
+
const refusal = mergeLogprobTokens(_optionalChain([existing, 'optionalAccess', _177 => _177.refusal]), incoming.refusal);
|
|
10617
10970
|
if (refusal !== void 0) {
|
|
10618
10971
|
aggregated.refusal = refusal;
|
|
10619
10972
|
}
|
|
@@ -10634,7 +10987,7 @@ function aggregateChatCompletionChunks(chunks, streamResult, endEvent) {
|
|
|
10634
10987
|
...parseMetricsFromUsage(chunk.usage)
|
|
10635
10988
|
};
|
|
10636
10989
|
}
|
|
10637
|
-
const choice = _optionalChain([chunk, 'access',
|
|
10990
|
+
const choice = _optionalChain([chunk, 'access', _178 => _178.choices, 'optionalAccess', _179 => _179[0]]);
|
|
10638
10991
|
if (!choice) {
|
|
10639
10992
|
continue;
|
|
10640
10993
|
}
|
|
@@ -10704,14 +11057,14 @@ function aggregateResponseStreamEvents(chunks, _streamResult, endEvent) {
|
|
|
10704
11057
|
continue;
|
|
10705
11058
|
}
|
|
10706
11059
|
const response = chunk.response;
|
|
10707
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
11060
|
+
if (_optionalChain([response, 'optionalAccess', _180 => _180.output]) !== void 0) {
|
|
10708
11061
|
output = processImagesInOutput(response.output);
|
|
10709
11062
|
}
|
|
10710
11063
|
const { usage: _usage, output: _output, ...rest } = response || {};
|
|
10711
11064
|
if (Object.keys(rest).length > 0) {
|
|
10712
11065
|
metadata = rest;
|
|
10713
11066
|
}
|
|
10714
|
-
metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess',
|
|
11067
|
+
metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _181 => _181.usage]));
|
|
10715
11068
|
}
|
|
10716
11069
|
return {
|
|
10717
11070
|
output,
|
|
@@ -10785,7 +11138,7 @@ var AnthropicPlugin = class extends BasePlugin {
|
|
|
10785
11138
|
return message ? { role: message.role, content: message.content } : null;
|
|
10786
11139
|
},
|
|
10787
11140
|
extractMetrics: (message, startTime) => {
|
|
10788
|
-
const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess',
|
|
11141
|
+
const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _182 => _182.usage]));
|
|
10789
11142
|
if (startTime) {
|
|
10790
11143
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
10791
11144
|
}
|
|
@@ -10800,7 +11153,7 @@ var AnthropicPlugin = class extends BasePlugin {
|
|
|
10800
11153
|
const metadata = {};
|
|
10801
11154
|
const metas = ["stop_reason", "stop_sequence"];
|
|
10802
11155
|
for (const m of metas) {
|
|
10803
|
-
if (_optionalChain([message, 'optionalAccess',
|
|
11156
|
+
if (_optionalChain([message, 'optionalAccess', _183 => _183[m]]) !== void 0) {
|
|
10804
11157
|
metadata[m] = message[m];
|
|
10805
11158
|
}
|
|
10806
11159
|
}
|
|
@@ -10950,8 +11303,8 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
|
|
|
10950
11303
|
Object.getOwnPropertyDescriptors(tool)
|
|
10951
11304
|
);
|
|
10952
11305
|
Object.defineProperty(wrappedTool, "run", {
|
|
10953
|
-
configurable: _nullishCoalesce(_optionalChain([runDescriptor, 'optionalAccess',
|
|
10954
|
-
enumerable: _nullishCoalesce(_optionalChain([runDescriptor, 'optionalAccess',
|
|
11306
|
+
configurable: _nullishCoalesce(_optionalChain([runDescriptor, 'optionalAccess', _184 => _184.configurable]), () => ( true)),
|
|
11307
|
+
enumerable: _nullishCoalesce(_optionalChain([runDescriptor, 'optionalAccess', _185 => _185.enumerable]), () => ( true)),
|
|
10955
11308
|
value: function braintrustAnthropicToolRunnerRun(...args) {
|
|
10956
11309
|
return state.span.traced(
|
|
10957
11310
|
(span) => {
|
|
@@ -10988,7 +11341,7 @@ function wrapAnthropicToolRunnerTool(tool, index, state) {
|
|
|
10988
11341
|
}
|
|
10989
11342
|
);
|
|
10990
11343
|
},
|
|
10991
|
-
writable: _nullishCoalesce(_optionalChain([runDescriptor, 'optionalAccess',
|
|
11344
|
+
writable: _nullishCoalesce(_optionalChain([runDescriptor, 'optionalAccess', _186 => _186.writable]), () => ( true))
|
|
10992
11345
|
});
|
|
10993
11346
|
Object.defineProperty(wrappedTool, ANTHROPIC_TOOL_RUNNER_TOOL_WRAPPED, {
|
|
10994
11347
|
configurable: false,
|
|
@@ -11093,10 +11446,10 @@ async function finalizeAnthropicToolRunner(state, finalMessage) {
|
|
|
11093
11446
|
const metadata = {
|
|
11094
11447
|
anthropic_tool_runner_iterations: state.iterationCount
|
|
11095
11448
|
};
|
|
11096
|
-
if (_optionalChain([message, 'optionalAccess',
|
|
11449
|
+
if (_optionalChain([message, 'optionalAccess', _187 => _187.stop_reason]) !== void 0) {
|
|
11097
11450
|
metadata.stop_reason = message.stop_reason;
|
|
11098
11451
|
}
|
|
11099
|
-
if (_optionalChain([message, 'optionalAccess',
|
|
11452
|
+
if (_optionalChain([message, 'optionalAccess', _188 => _188.stop_sequence]) !== void 0) {
|
|
11100
11453
|
metadata.stop_sequence = message.stop_sequence;
|
|
11101
11454
|
}
|
|
11102
11455
|
state.span.log({
|
|
@@ -11238,13 +11591,13 @@ function aggregateAnthropicStreamChunks(chunks) {
|
|
|
11238
11591
|
let metadata = {};
|
|
11239
11592
|
let role;
|
|
11240
11593
|
for (const event of chunks) {
|
|
11241
|
-
switch (_optionalChain([event, 'optionalAccess',
|
|
11594
|
+
switch (_optionalChain([event, 'optionalAccess', _189 => _189.type])) {
|
|
11242
11595
|
case "message_start":
|
|
11243
|
-
if (_optionalChain([event, 'access',
|
|
11596
|
+
if (_optionalChain([event, 'access', _190 => _190.message, 'optionalAccess', _191 => _191.usage])) {
|
|
11244
11597
|
const initialMetrics = parseMetricsFromUsage2(event.message.usage);
|
|
11245
11598
|
metrics = { ...metrics, ...initialMetrics };
|
|
11246
11599
|
}
|
|
11247
|
-
if (typeof _optionalChain([event, 'access',
|
|
11600
|
+
if (typeof _optionalChain([event, 'access', _192 => _192.message, 'optionalAccess', _193 => _193.role]) === "string") {
|
|
11248
11601
|
role = event.message.role;
|
|
11249
11602
|
}
|
|
11250
11603
|
break;
|
|
@@ -11310,7 +11663,7 @@ function aggregateAnthropicStreamChunks(chunks) {
|
|
|
11310
11663
|
})).filter(({ block }) => block !== void 0).sort((left, right) => left.index - right.index).map(({ block }) => block);
|
|
11311
11664
|
let output = fallbackTextDeltas.join("");
|
|
11312
11665
|
if (orderedContent.length > 0) {
|
|
11313
|
-
if (orderedContent.every(isTextContentBlock) && orderedContent.every((block) => !_optionalChain([block, 'access',
|
|
11666
|
+
if (orderedContent.every(isTextContentBlock) && orderedContent.every((block) => !_optionalChain([block, 'access', _194 => _194.citations, 'optionalAccess', _195 => _195.length]))) {
|
|
11314
11667
|
output = orderedContent.map((block) => block.text).join("");
|
|
11315
11668
|
} else {
|
|
11316
11669
|
output = {
|
|
@@ -11337,7 +11690,7 @@ function finalizeContentBlock(index, contentBlocks, contentBlockDeltas, fallback
|
|
|
11337
11690
|
return;
|
|
11338
11691
|
}
|
|
11339
11692
|
const acc = contentBlockDeltas[index];
|
|
11340
|
-
const text = _nullishCoalesce(_optionalChain([acc, 'optionalAccess',
|
|
11693
|
+
const text = _nullishCoalesce(_optionalChain([acc, 'optionalAccess', _196 => _196.textDeltas, 'access', _197 => _197.join, 'call', _198 => _198("")]), () => ( ""));
|
|
11341
11694
|
if (isToolUseLikeContentBlock(contentBlock)) {
|
|
11342
11695
|
if (!text) {
|
|
11343
11696
|
return;
|
|
@@ -11368,7 +11721,7 @@ function finalizeContentBlock(index, contentBlocks, contentBlockDeltas, fallback
|
|
|
11368
11721
|
return;
|
|
11369
11722
|
}
|
|
11370
11723
|
const updated = { ...contentBlock, text };
|
|
11371
|
-
if (_optionalChain([acc, 'optionalAccess',
|
|
11724
|
+
if (_optionalChain([acc, 'optionalAccess', _199 => _199.citations, 'access', _200 => _200.length])) {
|
|
11372
11725
|
updated.citations = acc.citations;
|
|
11373
11726
|
}
|
|
11374
11727
|
contentBlocks[index] = updated;
|
|
@@ -11873,10 +12226,10 @@ var AISDKPlugin = class extends BasePlugin {
|
|
|
11873
12226
|
}
|
|
11874
12227
|
};
|
|
11875
12228
|
function resolveDenyOutputPaths(event, defaultDenyOutputPaths) {
|
|
11876
|
-
if (Array.isArray(_optionalChain([event, 'optionalAccess',
|
|
12229
|
+
if (Array.isArray(_optionalChain([event, 'optionalAccess', _201 => _201.denyOutputPaths]))) {
|
|
11877
12230
|
return event.denyOutputPaths;
|
|
11878
12231
|
}
|
|
11879
|
-
const firstArgument = _optionalChain([event, 'optionalAccess',
|
|
12232
|
+
const firstArgument = _optionalChain([event, 'optionalAccess', _202 => _202.arguments]) && event.arguments.length > 0 ? event.arguments[0] : void 0;
|
|
11880
12233
|
if (!firstArgument || typeof firstArgument !== "object") {
|
|
11881
12234
|
return defaultDenyOutputPaths;
|
|
11882
12235
|
}
|
|
@@ -12091,7 +12444,7 @@ var convertImageToAttachment = (image, explicitMimeType) => {
|
|
|
12091
12444
|
try {
|
|
12092
12445
|
if (typeof image === "string" && image.startsWith("data:")) {
|
|
12093
12446
|
const [mimeTypeSection, base64Data] = image.split(",");
|
|
12094
|
-
const mimeType = _optionalChain([mimeTypeSection, 'access',
|
|
12447
|
+
const mimeType = _optionalChain([mimeTypeSection, 'access', _203 => _203.match, 'call', _204 => _204(/data:(.*?);/), 'optionalAccess', _205 => _205[1]]);
|
|
12095
12448
|
if (mimeType && base64Data) {
|
|
12096
12449
|
const blob = convertDataToBlob(base64Data, mimeType);
|
|
12097
12450
|
if (blob) {
|
|
@@ -12221,7 +12574,7 @@ function extractTopLevelAISDKMetrics(result, event, startTime) {
|
|
|
12221
12574
|
return metrics;
|
|
12222
12575
|
}
|
|
12223
12576
|
function hasModelChildTracing(event) {
|
|
12224
|
-
return _optionalChain([event, 'optionalAccess',
|
|
12577
|
+
return _optionalChain([event, 'optionalAccess', _206 => _206.modelWrapped]) === true || _optionalChain([event, 'optionalAccess', _207 => _207.__braintrust_ai_sdk_model_wrapped]) === true;
|
|
12225
12578
|
}
|
|
12226
12579
|
function createAISDKIntegrationMetadata() {
|
|
12227
12580
|
return {
|
|
@@ -12232,7 +12585,7 @@ function createAISDKIntegrationMetadata() {
|
|
|
12232
12585
|
};
|
|
12233
12586
|
}
|
|
12234
12587
|
function resolveModelFromSelf(self) {
|
|
12235
|
-
return self && typeof self === "object" && "model" in self && self.model ? self.model : self && typeof self === "object" && "settings" in self && _optionalChain([self, 'access',
|
|
12588
|
+
return self && typeof self === "object" && "model" in self && self.model ? self.model : self && typeof self === "object" && "settings" in self && _optionalChain([self, 'access', _208 => _208.settings, 'optionalAccess', _209 => _209.model]) ? _optionalChain([self, 'access', _210 => _210.settings, 'optionalAccess', _211 => _211.model]) : void 0;
|
|
12236
12589
|
}
|
|
12237
12590
|
function extractBaseMetadata(model, self) {
|
|
12238
12591
|
const metadata = createAISDKIntegrationMetadata();
|
|
@@ -12359,9 +12712,9 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
12359
12712
|
case "raw":
|
|
12360
12713
|
if (chunk.rawValue) {
|
|
12361
12714
|
const rawVal = chunk.rawValue;
|
|
12362
|
-
if (_optionalChain([rawVal, 'access',
|
|
12715
|
+
if (_optionalChain([rawVal, 'access', _212 => _212.delta, 'optionalAccess', _213 => _213.content])) {
|
|
12363
12716
|
text += rawVal.delta.content;
|
|
12364
|
-
} else if (_optionalChain([rawVal, 'access',
|
|
12717
|
+
} else if (_optionalChain([rawVal, 'access', _214 => _214.choices, 'optionalAccess', _215 => _215[0], 'optionalAccess', _216 => _216.delta, 'optionalAccess', _217 => _217.content])) {
|
|
12365
12718
|
text += rawVal.choices[0].delta.content;
|
|
12366
12719
|
} else if (typeof rawVal.text === "string") {
|
|
12367
12720
|
text += rawVal.text;
|
|
@@ -12515,14 +12868,14 @@ function prepareAISDKChildTracing(params, self, parentSpan, denyOutputPaths, aiS
|
|
|
12515
12868
|
return {
|
|
12516
12869
|
cleanup: cleanup.length > 0 ? () => {
|
|
12517
12870
|
while (cleanup.length > 0) {
|
|
12518
|
-
_optionalChain([cleanup, 'access',
|
|
12871
|
+
_optionalChain([cleanup, 'access', _218 => _218.pop, 'call', _219 => _219(), 'optionalCall', _220 => _220()]);
|
|
12519
12872
|
}
|
|
12520
12873
|
} : void 0,
|
|
12521
12874
|
modelWrapped
|
|
12522
12875
|
};
|
|
12523
12876
|
}
|
|
12524
12877
|
function finalizeAISDKChildTracing(event) {
|
|
12525
|
-
const cleanup = _optionalChain([event, 'optionalAccess',
|
|
12878
|
+
const cleanup = _optionalChain([event, 'optionalAccess', _221 => _221.__braintrust_ai_sdk_cleanup]);
|
|
12526
12879
|
if (event && typeof cleanup === "function") {
|
|
12527
12880
|
cleanup();
|
|
12528
12881
|
delete event.__braintrust_ai_sdk_cleanup;
|
|
@@ -12738,10 +13091,10 @@ function buildAISDKChildMetadata(model) {
|
|
|
12738
13091
|
function buildResolvedMetadataPayload(result) {
|
|
12739
13092
|
const gatewayInfo = extractGatewayRoutingInfo(result);
|
|
12740
13093
|
const metadata = {};
|
|
12741
|
-
if (_optionalChain([gatewayInfo, 'optionalAccess',
|
|
13094
|
+
if (_optionalChain([gatewayInfo, 'optionalAccess', _222 => _222.provider])) {
|
|
12742
13095
|
metadata.provider = gatewayInfo.provider;
|
|
12743
13096
|
}
|
|
12744
|
-
if (_optionalChain([gatewayInfo, 'optionalAccess',
|
|
13097
|
+
if (_optionalChain([gatewayInfo, 'optionalAccess', _223 => _223.model])) {
|
|
12745
13098
|
metadata.model = gatewayInfo.model;
|
|
12746
13099
|
}
|
|
12747
13100
|
let finishReason;
|
|
@@ -12762,7 +13115,7 @@ function resolveAISDKModel(model, aiSDK) {
|
|
|
12762
13115
|
if (typeof model !== "string") {
|
|
12763
13116
|
return model;
|
|
12764
13117
|
}
|
|
12765
|
-
const provider = _nullishCoalesce(_nullishCoalesce(globalThis.AI_SDK_DEFAULT_PROVIDER, () => ( _optionalChain([aiSDK, 'optionalAccess',
|
|
13118
|
+
const provider = _nullishCoalesce(_nullishCoalesce(globalThis.AI_SDK_DEFAULT_PROVIDER, () => ( _optionalChain([aiSDK, 'optionalAccess', _224 => _224.gateway]))), () => ( null));
|
|
12766
13119
|
if (provider && typeof provider.languageModel === "function") {
|
|
12767
13120
|
return provider.languageModel(model);
|
|
12768
13121
|
}
|
|
@@ -12824,8 +13177,8 @@ function processAISDKRerankOutput(output, _denyOutputPaths) {
|
|
|
12824
13177
|
return ranking.slice(0, 100).map((item) => {
|
|
12825
13178
|
const entry = item && typeof item === "object" ? item : void 0;
|
|
12826
13179
|
return {
|
|
12827
|
-
index: typeof _optionalChain([entry, 'optionalAccess',
|
|
12828
|
-
relevance_score: typeof _optionalChain([entry, 'optionalAccess',
|
|
13180
|
+
index: typeof _optionalChain([entry, 'optionalAccess', _225 => _225.originalIndex]) === "number" ? entry.originalIndex : void 0,
|
|
13181
|
+
relevance_score: typeof _optionalChain([entry, 'optionalAccess', _226 => _226.score]) === "number" ? entry.score : void 0
|
|
12829
13182
|
};
|
|
12830
13183
|
});
|
|
12831
13184
|
}
|
|
@@ -12848,7 +13201,7 @@ function extractTokenMetrics(result) {
|
|
|
12848
13201
|
return metrics;
|
|
12849
13202
|
}
|
|
12850
13203
|
const promptTokens = firstNumber(
|
|
12851
|
-
_optionalChain([usage, 'access',
|
|
13204
|
+
_optionalChain([usage, 'access', _227 => _227.inputTokens, 'optionalAccess', _228 => _228.total]),
|
|
12852
13205
|
usage.inputTokens,
|
|
12853
13206
|
usage.promptTokens,
|
|
12854
13207
|
usage.prompt_tokens
|
|
@@ -12857,7 +13210,7 @@ function extractTokenMetrics(result) {
|
|
|
12857
13210
|
metrics.prompt_tokens = promptTokens;
|
|
12858
13211
|
}
|
|
12859
13212
|
const completionTokens = firstNumber(
|
|
12860
|
-
_optionalChain([usage, 'access',
|
|
13213
|
+
_optionalChain([usage, 'access', _229 => _229.outputTokens, 'optionalAccess', _230 => _230.total]),
|
|
12861
13214
|
usage.outputTokens,
|
|
12862
13215
|
usage.completionTokens,
|
|
12863
13216
|
usage.completion_tokens
|
|
@@ -12874,8 +13227,8 @@ function extractTokenMetrics(result) {
|
|
|
12874
13227
|
metrics.tokens = totalTokens;
|
|
12875
13228
|
}
|
|
12876
13229
|
const promptCachedTokens = firstNumber(
|
|
12877
|
-
_optionalChain([usage, 'access',
|
|
12878
|
-
_optionalChain([usage, 'access',
|
|
13230
|
+
_optionalChain([usage, 'access', _231 => _231.inputTokens, 'optionalAccess', _232 => _232.cacheRead]),
|
|
13231
|
+
_optionalChain([usage, 'access', _233 => _233.inputTokenDetails, 'optionalAccess', _234 => _234.cacheReadTokens]),
|
|
12879
13232
|
usage.cachedInputTokens,
|
|
12880
13233
|
usage.promptCachedTokens,
|
|
12881
13234
|
usage.prompt_cached_tokens
|
|
@@ -12884,8 +13237,8 @@ function extractTokenMetrics(result) {
|
|
|
12884
13237
|
metrics.prompt_cached_tokens = promptCachedTokens;
|
|
12885
13238
|
}
|
|
12886
13239
|
const promptCacheCreationTokens = firstNumber(
|
|
12887
|
-
_optionalChain([usage, 'access',
|
|
12888
|
-
_optionalChain([usage, 'access',
|
|
13240
|
+
_optionalChain([usage, 'access', _235 => _235.inputTokens, 'optionalAccess', _236 => _236.cacheWrite]),
|
|
13241
|
+
_optionalChain([usage, 'access', _237 => _237.inputTokenDetails, 'optionalAccess', _238 => _238.cacheWriteTokens]),
|
|
12889
13242
|
usage.promptCacheCreationTokens,
|
|
12890
13243
|
usage.prompt_cache_creation_tokens,
|
|
12891
13244
|
extractAnthropicCacheCreationTokens(result)
|
|
@@ -12908,7 +13261,7 @@ function extractTokenMetrics(result) {
|
|
|
12908
13261
|
metrics.completion_cached_tokens = completionCachedTokens;
|
|
12909
13262
|
}
|
|
12910
13263
|
const reasoningTokenCount = firstNumber(
|
|
12911
|
-
_optionalChain([usage, 'access',
|
|
13264
|
+
_optionalChain([usage, 'access', _239 => _239.outputTokens, 'optionalAccess', _240 => _240.reasoning]),
|
|
12912
13265
|
usage.reasoningTokens,
|
|
12913
13266
|
usage.completionReasoningTokens,
|
|
12914
13267
|
usage.completion_reasoning_tokens,
|
|
@@ -12938,13 +13291,13 @@ function extractAnthropicCacheCreationTokens(result) {
|
|
|
12938
13291
|
result,
|
|
12939
13292
|
"providerMetadata"
|
|
12940
13293
|
);
|
|
12941
|
-
const anthropicMetadata = _optionalChain([providerMetadata, 'optionalAccess',
|
|
13294
|
+
const anthropicMetadata = _optionalChain([providerMetadata, 'optionalAccess', _241 => _241.anthropic]);
|
|
12942
13295
|
if (!anthropicMetadata) {
|
|
12943
13296
|
return void 0;
|
|
12944
13297
|
}
|
|
12945
13298
|
return firstNumber(
|
|
12946
13299
|
anthropicMetadata.cacheCreationInputTokens,
|
|
12947
|
-
_optionalChain([anthropicMetadata, 'access',
|
|
13300
|
+
_optionalChain([anthropicMetadata, 'access', _242 => _242.usage, 'optionalAccess', _243 => _243.cache_creation_input_tokens])
|
|
12948
13301
|
);
|
|
12949
13302
|
}
|
|
12950
13303
|
function safeResultFieldRead(result, field) {
|
|
@@ -12952,7 +13305,7 @@ function safeResultFieldRead(result, field) {
|
|
|
12952
13305
|
}
|
|
12953
13306
|
function safeSerializableFieldRead(obj, field) {
|
|
12954
13307
|
try {
|
|
12955
|
-
const value = _optionalChain([obj, 'optionalAccess',
|
|
13308
|
+
const value = _optionalChain([obj, 'optionalAccess', _244 => _244[field]]);
|
|
12956
13309
|
if (isPromiseLike(value)) {
|
|
12957
13310
|
void Promise.resolve(value).catch(() => {
|
|
12958
13311
|
});
|
|
@@ -13046,7 +13399,7 @@ function extractSerializableOutputFields(output) {
|
|
|
13046
13399
|
];
|
|
13047
13400
|
for (const name of directFieldNames) {
|
|
13048
13401
|
try {
|
|
13049
|
-
const value = _optionalChain([output, 'optionalAccess',
|
|
13402
|
+
const value = _optionalChain([output, 'optionalAccess', _245 => _245[name]]);
|
|
13050
13403
|
if (isPromiseLike(value)) {
|
|
13051
13404
|
void Promise.resolve(value).catch(() => {
|
|
13052
13405
|
});
|
|
@@ -13079,8 +13432,8 @@ function isSerializableOutputValue(value) {
|
|
|
13079
13432
|
return true;
|
|
13080
13433
|
}
|
|
13081
13434
|
function serializeModelWithProvider(model) {
|
|
13082
|
-
const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess',
|
|
13083
|
-
const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess',
|
|
13435
|
+
const modelId = typeof model === "string" ? model : _optionalChain([model, 'optionalAccess', _246 => _246.modelId]);
|
|
13436
|
+
const explicitProvider = typeof model === "object" ? _optionalChain([model, 'optionalAccess', _247 => _247.provider]) : void 0;
|
|
13084
13437
|
if (!modelId) {
|
|
13085
13438
|
return { model: modelId, provider: explicitProvider };
|
|
13086
13439
|
}
|
|
@@ -13106,7 +13459,7 @@ function parseGatewayModelString(modelString) {
|
|
|
13106
13459
|
function extractGatewayRoutingInfo(result) {
|
|
13107
13460
|
const steps = safeSerializableFieldRead(result, "steps");
|
|
13108
13461
|
if (Array.isArray(steps) && steps.length > 0) {
|
|
13109
|
-
const routing2 = _optionalChain([steps, 'access',
|
|
13462
|
+
const routing2 = _optionalChain([steps, 'access', _248 => _248[0], 'optionalAccess', _249 => _249.providerMetadata, 'optionalAccess', _250 => _250.gateway, 'optionalAccess', _251 => _251.routing]);
|
|
13110
13463
|
if (routing2) {
|
|
13111
13464
|
return {
|
|
13112
13465
|
provider: routing2.resolvedProvider || routing2.finalProvider,
|
|
@@ -13118,7 +13471,7 @@ function extractGatewayRoutingInfo(result) {
|
|
|
13118
13471
|
result,
|
|
13119
13472
|
"providerMetadata"
|
|
13120
13473
|
);
|
|
13121
|
-
const routing = _optionalChain([providerMetadata, 'optionalAccess',
|
|
13474
|
+
const routing = _optionalChain([providerMetadata, 'optionalAccess', _252 => _252.gateway, 'optionalAccess', _253 => _253.routing]);
|
|
13122
13475
|
if (routing) {
|
|
13123
13476
|
return {
|
|
13124
13477
|
provider: routing.resolvedProvider || routing.finalProvider,
|
|
@@ -13133,8 +13486,8 @@ function extractCostFromResult(result) {
|
|
|
13133
13486
|
let totalCost = 0;
|
|
13134
13487
|
let foundCost = false;
|
|
13135
13488
|
for (const step of steps) {
|
|
13136
|
-
const gateway2 = _optionalChain([step, 'optionalAccess',
|
|
13137
|
-
const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess',
|
|
13489
|
+
const gateway2 = _optionalChain([step, 'optionalAccess', _254 => _254.providerMetadata, 'optionalAccess', _255 => _255.gateway]);
|
|
13490
|
+
const stepCost = parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _256 => _256.cost])) || parseGatewayCost(_optionalChain([gateway2, 'optionalAccess', _257 => _257.marketCost]));
|
|
13138
13491
|
if (stepCost !== void 0 && stepCost > 0) {
|
|
13139
13492
|
totalCost += stepCost;
|
|
13140
13493
|
foundCost = true;
|
|
@@ -13148,8 +13501,8 @@ function extractCostFromResult(result) {
|
|
|
13148
13501
|
result,
|
|
13149
13502
|
"providerMetadata"
|
|
13150
13503
|
);
|
|
13151
|
-
const gateway = _optionalChain([providerMetadata, 'optionalAccess',
|
|
13152
|
-
const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess',
|
|
13504
|
+
const gateway = _optionalChain([providerMetadata, 'optionalAccess', _258 => _258.gateway]);
|
|
13505
|
+
const directCost = parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _259 => _259.cost])) || parseGatewayCost(_optionalChain([gateway, 'optionalAccess', _260 => _260.marketCost]));
|
|
13153
13506
|
if (directCost !== void 0 && directCost > 0) {
|
|
13154
13507
|
return directCost;
|
|
13155
13508
|
}
|
|
@@ -13325,7 +13678,7 @@ function setClaudeLocalToolParentResolver(resolver) {
|
|
|
13325
13678
|
context.resolveLocalToolParent = resolver;
|
|
13326
13679
|
}
|
|
13327
13680
|
function getClaudeLocalToolParentResolver() {
|
|
13328
|
-
return _nullishCoalesce(_optionalChain([localToolContextStore, 'access',
|
|
13681
|
+
return _nullishCoalesce(_optionalChain([localToolContextStore, 'access', _261 => _261.getStore, 'call', _262 => _262(), 'optionalAccess', _263 => _263.resolveLocalToolParent]), () => ( fallbackLocalToolParentResolver));
|
|
13329
13682
|
}
|
|
13330
13683
|
function isAsyncIterable2(value) {
|
|
13331
13684
|
return value !== null && typeof value === "object" && Symbol.asyncIterator in value && typeof value[Symbol.asyncIterator] === "function";
|
|
@@ -13566,6 +13919,9 @@ function llmParentKey(parentToolUseId) {
|
|
|
13566
13919
|
function isSubAgentDelegationToolName(toolName) {
|
|
13567
13920
|
return toolName === "Agent" || toolName === "Task";
|
|
13568
13921
|
}
|
|
13922
|
+
function shouldParentToolAsTaskSibling(toolName) {
|
|
13923
|
+
return toolName === "Agent" || toolName === "Task" || toolName === "Bash";
|
|
13924
|
+
}
|
|
13569
13925
|
function filterSerializableOptions(options) {
|
|
13570
13926
|
const allowedKeys = [
|
|
13571
13927
|
"model",
|
|
@@ -13616,10 +13972,10 @@ function upsertSubAgentDetails(detailsByToolUseId, toolUseId, update) {
|
|
|
13616
13972
|
return merged;
|
|
13617
13973
|
}
|
|
13618
13974
|
function formatSubAgentSpanName(details) {
|
|
13619
|
-
if (_optionalChain([details, 'optionalAccess',
|
|
13975
|
+
if (_optionalChain([details, 'optionalAccess', _264 => _264.description])) {
|
|
13620
13976
|
return `Agent: ${details.description}`;
|
|
13621
13977
|
}
|
|
13622
|
-
if (_optionalChain([details, 'optionalAccess',
|
|
13978
|
+
if (_optionalChain([details, 'optionalAccess', _265 => _265.agentType])) {
|
|
13623
13979
|
return `Agent: ${details.agentType}`;
|
|
13624
13980
|
}
|
|
13625
13981
|
return "Agent: sub-agent";
|
|
@@ -13665,11 +14021,16 @@ function resolveTaskToolUseId(taskIdToToolUseId, message) {
|
|
|
13665
14021
|
}
|
|
13666
14022
|
return void 0;
|
|
13667
14023
|
}
|
|
14024
|
+
function seedTaskToolUseIdMapping(taskIdToToolUseId, message) {
|
|
14025
|
+
if (typeof message.task_id === "string" && typeof message.tool_use_id === "string") {
|
|
14026
|
+
taskIdToToolUseId.set(message.task_id, message.tool_use_id);
|
|
14027
|
+
}
|
|
14028
|
+
}
|
|
13668
14029
|
function extractUsageFromMessage(message) {
|
|
13669
14030
|
const metrics = {};
|
|
13670
14031
|
let usage;
|
|
13671
14032
|
if (message.type === "assistant") {
|
|
13672
|
-
usage = _optionalChain([message, 'access',
|
|
14033
|
+
usage = _optionalChain([message, 'access', _266 => _266.message, 'optionalAccess', _267 => _267.usage]);
|
|
13673
14034
|
} else if (message.type === "result") {
|
|
13674
14035
|
usage = message.usage;
|
|
13675
14036
|
}
|
|
@@ -13703,8 +14064,8 @@ function buildLLMInput(prompt, conversationHistory, capturedPromptMessages) {
|
|
|
13703
14064
|
promptMessages.push({ content: prompt, role: "user" });
|
|
13704
14065
|
} else if (capturedPromptMessages && capturedPromptMessages.length > 0) {
|
|
13705
14066
|
for (const msg of capturedPromptMessages) {
|
|
13706
|
-
const role = _optionalChain([msg, 'access',
|
|
13707
|
-
const content = _optionalChain([msg, 'access',
|
|
14067
|
+
const role = _optionalChain([msg, 'access', _268 => _268.message, 'optionalAccess', _269 => _269.role]);
|
|
14068
|
+
const content = _optionalChain([msg, 'access', _270 => _270.message, 'optionalAccess', _271 => _271.content]);
|
|
13708
14069
|
if (role && content !== void 0) {
|
|
13709
14070
|
promptMessages.push({ content, role });
|
|
13710
14071
|
}
|
|
@@ -13721,7 +14082,7 @@ async function createLLMSpanForMessages(messages, prompt, conversationHistory, o
|
|
|
13721
14082
|
return void 0;
|
|
13722
14083
|
}
|
|
13723
14084
|
const lastMessage = messages[messages.length - 1];
|
|
13724
|
-
if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access',
|
|
14085
|
+
if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _272 => _272.message, 'optionalAccess', _273 => _273.usage])) {
|
|
13725
14086
|
return void 0;
|
|
13726
14087
|
}
|
|
13727
14088
|
const model = lastMessage.message.model || options.model;
|
|
@@ -13732,7 +14093,7 @@ async function createLLMSpanForMessages(messages, prompt, conversationHistory, o
|
|
|
13732
14093
|
capturedPromptMessages
|
|
13733
14094
|
);
|
|
13734
14095
|
const outputs = messages.map(
|
|
13735
|
-
(m) => _optionalChain([m, 'access',
|
|
14096
|
+
(m) => _optionalChain([m, 'access', _274 => _274.message, 'optionalAccess', _275 => _275.content]) && _optionalChain([m, 'access', _276 => _276.message, 'optionalAccess', _277 => _277.role]) ? { content: m.message.content, role: m.message.role } : void 0
|
|
13736
14097
|
).filter(
|
|
13737
14098
|
(c) => c !== void 0
|
|
13738
14099
|
);
|
|
@@ -13752,7 +14113,7 @@ async function createLLMSpanForMessages(messages, prompt, conversationHistory, o
|
|
|
13752
14113
|
});
|
|
13753
14114
|
const spanExport = await span.export();
|
|
13754
14115
|
await span.end();
|
|
13755
|
-
const finalMessage = _optionalChain([lastMessage, 'access',
|
|
14116
|
+
const finalMessage = _optionalChain([lastMessage, 'access', _278 => _278.message, 'optionalAccess', _279 => _279.content]) && _optionalChain([lastMessage, 'access', _280 => _280.message, 'optionalAccess', _281 => _281.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
|
|
13756
14117
|
return {
|
|
13757
14118
|
finalMessage,
|
|
13758
14119
|
spanExport
|
|
@@ -13860,7 +14221,10 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
|
|
|
13860
14221
|
}
|
|
13861
14222
|
},
|
|
13862
14223
|
name: parsed.displayName,
|
|
13863
|
-
parent: await resolveParentSpan(toolUseID
|
|
14224
|
+
parent: await resolveParentSpan(toolUseID, {
|
|
14225
|
+
agentId: input.agent_id,
|
|
14226
|
+
preferTaskSiblingParent: shouldParentToolAsTaskSibling(parsed.toolName)
|
|
14227
|
+
}),
|
|
13864
14228
|
spanAttributes: { type: "tool" /* TOOL */ }
|
|
13865
14229
|
});
|
|
13866
14230
|
activeToolSpans.set(toolUseID, toolSpan);
|
|
@@ -13886,18 +14250,18 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
|
|
|
13886
14250
|
subAgentDetailsByToolUseId.get(toolUseID)
|
|
13887
14251
|
)
|
|
13888
14252
|
};
|
|
13889
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
14253
|
+
if (_optionalChain([response, 'optionalAccess', _282 => _282.status])) {
|
|
13890
14254
|
metadata["claude_agent_sdk.status"] = response.status;
|
|
13891
14255
|
}
|
|
13892
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
14256
|
+
if (_optionalChain([response, 'optionalAccess', _283 => _283.totalDurationMs])) {
|
|
13893
14257
|
metadata["claude_agent_sdk.duration_ms"] = response.totalDurationMs;
|
|
13894
14258
|
}
|
|
13895
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
14259
|
+
if (_optionalChain([response, 'optionalAccess', _284 => _284.totalToolUseCount]) !== void 0) {
|
|
13896
14260
|
metadata["claude_agent_sdk.tool_use_count"] = response.totalToolUseCount;
|
|
13897
14261
|
}
|
|
13898
14262
|
subAgentSpan.log({
|
|
13899
14263
|
metadata,
|
|
13900
|
-
output: _optionalChain([response, 'optionalAccess',
|
|
14264
|
+
output: _optionalChain([response, 'optionalAccess', _285 => _285.content])
|
|
13901
14265
|
});
|
|
13902
14266
|
} finally {
|
|
13903
14267
|
subAgentSpan.end();
|
|
@@ -14109,7 +14473,7 @@ async function finalizeCurrentMessageGroup(state) {
|
|
|
14109
14473
|
if (state.currentMessages.length === 0) {
|
|
14110
14474
|
return;
|
|
14111
14475
|
}
|
|
14112
|
-
const parentToolUseId = _nullishCoalesce(_optionalChain([state, 'access',
|
|
14476
|
+
const parentToolUseId = _nullishCoalesce(_optionalChain([state, 'access', _286 => _286.currentMessages, 'access', _287 => _287[0], 'optionalAccess', _288 => _288.parent_tool_use_id]), () => ( null));
|
|
14113
14477
|
const parentKey = llmParentKey(parentToolUseId);
|
|
14114
14478
|
let parentSpan = await state.span.export();
|
|
14115
14479
|
if (parentToolUseId) {
|
|
@@ -14144,13 +14508,14 @@ async function finalizeCurrentMessageGroup(state) {
|
|
|
14144
14508
|
}
|
|
14145
14509
|
state.activeLlmSpansByParentToolUse.delete(parentKey);
|
|
14146
14510
|
const lastMessage = state.currentMessages[state.currentMessages.length - 1];
|
|
14147
|
-
if (_optionalChain([lastMessage, 'optionalAccess',
|
|
14511
|
+
if (_optionalChain([lastMessage, 'optionalAccess', _289 => _289.message, 'optionalAccess', _290 => _290.usage])) {
|
|
14148
14512
|
state.accumulatedOutputTokens += getNumberProperty(lastMessage.message.usage, "output_tokens") || 0;
|
|
14149
14513
|
}
|
|
14150
14514
|
state.currentMessages.length = 0;
|
|
14151
14515
|
}
|
|
14152
14516
|
function maybeTrackToolUseContext(state, message) {
|
|
14153
|
-
|
|
14517
|
+
seedTaskToolUseIdMapping(state.taskIdToToolUseId, message);
|
|
14518
|
+
if (message.type !== "assistant" || !Array.isArray(_optionalChain([message, 'access', _291 => _291.message, 'optionalAccess', _292 => _292.content]))) {
|
|
14154
14519
|
return;
|
|
14155
14520
|
}
|
|
14156
14521
|
const parentToolUseId = _nullishCoalesce(message.parent_tool_use_id, () => ( null));
|
|
@@ -14204,6 +14569,34 @@ async function ensureSubAgentSpan(subAgentDetailsByToolUseId, rootSpan, activeTo
|
|
|
14204
14569
|
subAgentSpans.set(parentToolUseId, subAgentSpan);
|
|
14205
14570
|
return subAgentSpan;
|
|
14206
14571
|
}
|
|
14572
|
+
async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByParentToolUse, subAgentDetailsByToolUseId, activeToolSpans, subAgentSpans, parentToolUseId, startTime) {
|
|
14573
|
+
const parentKey = llmParentKey(parentToolUseId);
|
|
14574
|
+
const existingLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
|
|
14575
|
+
if (existingLlmSpan) {
|
|
14576
|
+
return existingLlmSpan;
|
|
14577
|
+
}
|
|
14578
|
+
let llmParentSpan = await rootSpan.export();
|
|
14579
|
+
if (parentToolUseId) {
|
|
14580
|
+
const subAgentSpan = await ensureSubAgentSpan(
|
|
14581
|
+
subAgentDetailsByToolUseId,
|
|
14582
|
+
rootSpan,
|
|
14583
|
+
activeToolSpans,
|
|
14584
|
+
subAgentSpans,
|
|
14585
|
+
parentToolUseId
|
|
14586
|
+
);
|
|
14587
|
+
llmParentSpan = await subAgentSpan.export();
|
|
14588
|
+
}
|
|
14589
|
+
const llmSpan = startSpan({
|
|
14590
|
+
name: "anthropic.messages.create",
|
|
14591
|
+
parent: llmParentSpan,
|
|
14592
|
+
spanAttributes: {
|
|
14593
|
+
type: "llm" /* LLM */
|
|
14594
|
+
},
|
|
14595
|
+
startTime
|
|
14596
|
+
});
|
|
14597
|
+
activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
|
|
14598
|
+
return llmSpan;
|
|
14599
|
+
}
|
|
14207
14600
|
async function maybeHandleTaskLifecycleMessage(state, message) {
|
|
14208
14601
|
if (message.type !== "system") {
|
|
14209
14602
|
return false;
|
|
@@ -14305,37 +14698,23 @@ async function handleStreamMessage(state, message) {
|
|
|
14305
14698
|
return;
|
|
14306
14699
|
}
|
|
14307
14700
|
await maybeStartSubAgentSpan(state, message);
|
|
14308
|
-
const messageId = _optionalChain([message, 'access',
|
|
14701
|
+
const messageId = _optionalChain([message, 'access', _293 => _293.message, 'optionalAccess', _294 => _294.id]);
|
|
14309
14702
|
if (messageId && messageId !== state.currentMessageId) {
|
|
14310
14703
|
await finalizeCurrentMessageGroup(state);
|
|
14311
14704
|
state.currentMessageId = messageId;
|
|
14312
14705
|
state.currentMessageStartTime = getCurrentUnixTimestamp();
|
|
14313
14706
|
}
|
|
14314
|
-
if (message.type === "assistant" && _optionalChain([message, 'access',
|
|
14707
|
+
if (message.type === "assistant" && _optionalChain([message, 'access', _295 => _295.message, 'optionalAccess', _296 => _296.usage])) {
|
|
14315
14708
|
const parentToolUseId = _nullishCoalesce(message.parent_tool_use_id, () => ( null));
|
|
14316
|
-
|
|
14317
|
-
|
|
14318
|
-
|
|
14319
|
-
|
|
14320
|
-
|
|
14321
|
-
|
|
14322
|
-
|
|
14323
|
-
|
|
14324
|
-
|
|
14325
|
-
parentToolUseId
|
|
14326
|
-
);
|
|
14327
|
-
llmParentSpan = await subAgentSpan.export();
|
|
14328
|
-
}
|
|
14329
|
-
const llmSpan = startSpan({
|
|
14330
|
-
name: "anthropic.messages.create",
|
|
14331
|
-
parent: llmParentSpan,
|
|
14332
|
-
spanAttributes: {
|
|
14333
|
-
type: "llm" /* LLM */
|
|
14334
|
-
},
|
|
14335
|
-
startTime: state.currentMessageStartTime
|
|
14336
|
-
});
|
|
14337
|
-
state.activeLlmSpansByParentToolUse.set(parentKey, llmSpan);
|
|
14338
|
-
}
|
|
14709
|
+
await ensureActiveLlmSpanForParentToolUse(
|
|
14710
|
+
state.span,
|
|
14711
|
+
state.activeLlmSpansByParentToolUse,
|
|
14712
|
+
state.subAgentDetailsByToolUseId,
|
|
14713
|
+
state.activeToolSpans,
|
|
14714
|
+
state.subAgentSpans,
|
|
14715
|
+
parentToolUseId,
|
|
14716
|
+
state.currentMessageStartTime
|
|
14717
|
+
);
|
|
14339
14718
|
state.currentMessages.push(message);
|
|
14340
14719
|
}
|
|
14341
14720
|
if (message.type !== "result" || !message.usage) {
|
|
@@ -14344,7 +14723,7 @@ async function handleStreamMessage(state, message) {
|
|
|
14344
14723
|
const finalUsageMetrics = extractUsageFromMessage(message);
|
|
14345
14724
|
if (state.currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
|
|
14346
14725
|
const lastMessage = state.currentMessages[state.currentMessages.length - 1];
|
|
14347
|
-
if (_optionalChain([lastMessage, 'optionalAccess',
|
|
14726
|
+
if (_optionalChain([lastMessage, 'optionalAccess', _297 => _297.message, 'optionalAccess', _298 => _298.usage])) {
|
|
14348
14727
|
const adjustedTokens = finalUsageMetrics.completion_tokens - state.accumulatedOutputTokens;
|
|
14349
14728
|
if (adjustedTokens >= 0) {
|
|
14350
14729
|
lastMessage.message.usage.output_tokens = adjustedTokens;
|
|
@@ -14449,7 +14828,7 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
14449
14828
|
yield message;
|
|
14450
14829
|
}
|
|
14451
14830
|
} finally {
|
|
14452
|
-
_optionalChain([resolvePromptDone, 'optionalCall',
|
|
14831
|
+
_optionalChain([resolvePromptDone, 'optionalCall', _299 => _299()]);
|
|
14453
14832
|
}
|
|
14454
14833
|
})();
|
|
14455
14834
|
}
|
|
@@ -14482,10 +14861,35 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
14482
14861
|
const localToolContext = createClaudeLocalToolContext();
|
|
14483
14862
|
const { hasLocalToolHandlers, localToolHookNames } = prepareLocalToolHandlersInMcpServers(options.mcpServers);
|
|
14484
14863
|
const skipLocalToolHooks = options[CLAUDE_AGENT_SDK_SKIP_LOCAL_TOOL_HOOKS_OPTION] === true || hasLocalToolHandlers;
|
|
14485
|
-
const resolveToolUseParentSpan = async (toolUseID) => {
|
|
14486
|
-
const
|
|
14864
|
+
const resolveToolUseParentSpan = async (toolUseID, context) => {
|
|
14865
|
+
const trackedParentToolUseId = toolUseToParent.get(toolUseID);
|
|
14866
|
+
const parentToolUseId = _nullishCoalesce(trackedParentToolUseId, () => ( (_optionalChain([context, 'optionalAccess', _300 => _300.agentId]) ? _nullishCoalesce(taskIdToToolUseId.get(context.agentId), () => ( null)) : null)));
|
|
14487
14867
|
const parentKey = llmParentKey(parentToolUseId);
|
|
14488
14868
|
const activeLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
|
|
14869
|
+
if (_optionalChain([context, 'optionalAccess', _301 => _301.preferTaskSiblingParent])) {
|
|
14870
|
+
if (!activeLlmSpan) {
|
|
14871
|
+
await ensureActiveLlmSpanForParentToolUse(
|
|
14872
|
+
span,
|
|
14873
|
+
activeLlmSpansByParentToolUse,
|
|
14874
|
+
subAgentDetailsByToolUseId,
|
|
14875
|
+
activeToolSpans,
|
|
14876
|
+
subAgentSpans,
|
|
14877
|
+
parentToolUseId,
|
|
14878
|
+
getCurrentUnixTimestamp()
|
|
14879
|
+
);
|
|
14880
|
+
}
|
|
14881
|
+
if (parentToolUseId) {
|
|
14882
|
+
const subAgentSpan = await ensureSubAgentSpan(
|
|
14883
|
+
subAgentDetailsByToolUseId,
|
|
14884
|
+
span,
|
|
14885
|
+
activeToolSpans,
|
|
14886
|
+
subAgentSpans,
|
|
14887
|
+
parentToolUseId
|
|
14888
|
+
);
|
|
14889
|
+
return subAgentSpan.export();
|
|
14890
|
+
}
|
|
14891
|
+
return span.export();
|
|
14892
|
+
}
|
|
14489
14893
|
if (activeLlmSpan) {
|
|
14490
14894
|
return activeLlmSpan.export();
|
|
14491
14895
|
}
|
|
@@ -14613,56 +15017,934 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
14613
15017
|
}
|
|
14614
15018
|
};
|
|
14615
15019
|
|
|
14616
|
-
// src/instrumentation/plugins/
|
|
14617
|
-
var
|
|
14618
|
-
|
|
14619
|
-
channelName: "
|
|
15020
|
+
// src/instrumentation/plugins/cursor-sdk-channels.ts
|
|
15021
|
+
var cursorSDKChannels = defineChannels("@cursor/sdk", {
|
|
15022
|
+
create: channel({
|
|
15023
|
+
channelName: "Agent.create",
|
|
14620
15024
|
kind: "async"
|
|
14621
15025
|
}),
|
|
14622
|
-
|
|
14623
|
-
channelName: "
|
|
15026
|
+
resume: channel({
|
|
15027
|
+
channelName: "Agent.resume",
|
|
14624
15028
|
kind: "async"
|
|
14625
15029
|
}),
|
|
14626
|
-
|
|
14627
|
-
channelName: "
|
|
15030
|
+
prompt: channel({
|
|
15031
|
+
channelName: "Agent.prompt",
|
|
15032
|
+
kind: "async"
|
|
15033
|
+
}),
|
|
15034
|
+
send: channel({
|
|
15035
|
+
channelName: "agent.send",
|
|
14628
15036
|
kind: "async"
|
|
14629
15037
|
})
|
|
14630
15038
|
});
|
|
14631
15039
|
|
|
14632
|
-
// src/instrumentation/plugins/
|
|
14633
|
-
var
|
|
14634
|
-
|
|
14635
|
-
|
|
14636
|
-
|
|
14637
|
-
}
|
|
14638
|
-
function createWrapperParityEvent(args) {
|
|
14639
|
-
return {
|
|
14640
|
-
context: GOOGLE_GENAI_INTERNAL_CONTEXT,
|
|
14641
|
-
input: args.input,
|
|
14642
|
-
metadata: args.metadata
|
|
14643
|
-
};
|
|
14644
|
-
}
|
|
14645
|
-
var GoogleGenAIPlugin = class extends BasePlugin {
|
|
15040
|
+
// src/instrumentation/plugins/cursor-sdk-plugin.ts
|
|
15041
|
+
var PATCHED_AGENT = Symbol.for("braintrust.cursor-sdk.auto-patched-agent");
|
|
15042
|
+
var PATCHED_RUN = Symbol.for("braintrust.cursor-sdk.patched-run");
|
|
15043
|
+
var WRAPPED_AGENT = Symbol.for("braintrust.cursor-sdk.wrapped-agent");
|
|
15044
|
+
var CursorSDKPlugin = (_class18 = class extends BasePlugin {constructor(...args3) { super(...args3); _class18.prototype.__init64.call(this); }
|
|
15045
|
+
__init64() {this.promptDepth = 0}
|
|
14646
15046
|
onEnable() {
|
|
14647
|
-
this.
|
|
15047
|
+
this.subscribeToAgentFactories();
|
|
15048
|
+
this.subscribeToPrompt();
|
|
15049
|
+
this.subscribeToSend();
|
|
14648
15050
|
}
|
|
14649
15051
|
onDisable() {
|
|
14650
|
-
|
|
15052
|
+
for (const unsubscribe of this.unsubscribers) {
|
|
15053
|
+
unsubscribe();
|
|
15054
|
+
}
|
|
15055
|
+
this.unsubscribers = [];
|
|
15056
|
+
this.promptDepth = 0;
|
|
14651
15057
|
}
|
|
14652
|
-
|
|
14653
|
-
this.
|
|
14654
|
-
this.
|
|
14655
|
-
this.subscribeToEmbedContentChannel();
|
|
15058
|
+
subscribeToAgentFactories() {
|
|
15059
|
+
this.subscribeToAgentFactory(cursorSDKChannels.create);
|
|
15060
|
+
this.subscribeToAgentFactory(cursorSDKChannels.resume);
|
|
14656
15061
|
}
|
|
14657
|
-
|
|
14658
|
-
const tracingChannel2 =
|
|
14659
|
-
const
|
|
14660
|
-
|
|
14661
|
-
|
|
14662
|
-
|
|
14663
|
-
(
|
|
14664
|
-
|
|
14665
|
-
|
|
15062
|
+
subscribeToAgentFactory(channel2) {
|
|
15063
|
+
const tracingChannel2 = channel2.tracingChannel();
|
|
15064
|
+
const handlers = {
|
|
15065
|
+
asyncEnd: (event) => {
|
|
15066
|
+
patchCursorAgentInPlace(event.result);
|
|
15067
|
+
},
|
|
15068
|
+
error: () => {
|
|
15069
|
+
}
|
|
15070
|
+
};
|
|
15071
|
+
tracingChannel2.subscribe(handlers);
|
|
15072
|
+
this.unsubscribers.push(() => {
|
|
15073
|
+
tracingChannel2.unsubscribe(handlers);
|
|
15074
|
+
});
|
|
15075
|
+
}
|
|
15076
|
+
subscribeToPrompt() {
|
|
15077
|
+
const channel2 = cursorSDKChannels.prompt.tracingChannel();
|
|
15078
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
15079
|
+
const handlers = {
|
|
15080
|
+
start: (event) => {
|
|
15081
|
+
this.promptDepth += 1;
|
|
15082
|
+
const message = event.arguments[0];
|
|
15083
|
+
const options = event.arguments[1];
|
|
15084
|
+
const metadata = {
|
|
15085
|
+
...extractAgentOptionsMetadata(options),
|
|
15086
|
+
"cursor_sdk.operation": "Agent.prompt",
|
|
15087
|
+
provider: "cursor",
|
|
15088
|
+
...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
|
|
15089
|
+
};
|
|
15090
|
+
const span = startSpan({
|
|
15091
|
+
name: "Cursor Agent",
|
|
15092
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
15093
|
+
});
|
|
15094
|
+
const startTime = getCurrentUnixTimestamp();
|
|
15095
|
+
safeLog(span, {
|
|
15096
|
+
input: sanitizeUserMessage(message),
|
|
15097
|
+
metadata
|
|
15098
|
+
});
|
|
15099
|
+
states.set(event, { metadata, span, startTime });
|
|
15100
|
+
},
|
|
15101
|
+
asyncEnd: (event) => {
|
|
15102
|
+
this.promptDepth = Math.max(0, this.promptDepth - 1);
|
|
15103
|
+
const state = states.get(event);
|
|
15104
|
+
if (!state) {
|
|
15105
|
+
return;
|
|
15106
|
+
}
|
|
15107
|
+
try {
|
|
15108
|
+
safeLog(state.span, {
|
|
15109
|
+
metadata: {
|
|
15110
|
+
...state.metadata,
|
|
15111
|
+
...extractRunResultMetadata(event.result)
|
|
15112
|
+
},
|
|
15113
|
+
metrics: buildDurationMetrics(state.startTime),
|
|
15114
|
+
output: _nullishCoalesce(_optionalChain([event, 'access', _302 => _302.result, 'optionalAccess', _303 => _303.result]), () => ( event.result))
|
|
15115
|
+
});
|
|
15116
|
+
} finally {
|
|
15117
|
+
state.span.end();
|
|
15118
|
+
states.delete(event);
|
|
15119
|
+
}
|
|
15120
|
+
},
|
|
15121
|
+
error: (event) => {
|
|
15122
|
+
this.promptDepth = Math.max(0, this.promptDepth - 1);
|
|
15123
|
+
const state = states.get(event);
|
|
15124
|
+
if (!state || !event.error) {
|
|
15125
|
+
return;
|
|
15126
|
+
}
|
|
15127
|
+
safeLog(state.span, { error: event.error.message });
|
|
15128
|
+
state.span.end();
|
|
15129
|
+
states.delete(event);
|
|
15130
|
+
}
|
|
15131
|
+
};
|
|
15132
|
+
channel2.subscribe(handlers);
|
|
15133
|
+
this.unsubscribers.push(() => {
|
|
15134
|
+
channel2.unsubscribe(handlers);
|
|
15135
|
+
});
|
|
15136
|
+
}
|
|
15137
|
+
subscribeToSend() {
|
|
15138
|
+
const channel2 = cursorSDKChannels.send.tracingChannel();
|
|
15139
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
15140
|
+
const handlers = {
|
|
15141
|
+
start: (event) => {
|
|
15142
|
+
if (this.promptDepth > 0) {
|
|
15143
|
+
return;
|
|
15144
|
+
}
|
|
15145
|
+
const message = event.arguments[0];
|
|
15146
|
+
const sendOptions = event.arguments[1];
|
|
15147
|
+
const agent = event.agent;
|
|
15148
|
+
const metadata = {
|
|
15149
|
+
...extractSendMetadata(sendOptions),
|
|
15150
|
+
...agent ? extractAgentMetadata(agent) : {},
|
|
15151
|
+
"cursor_sdk.operation": "agent.send",
|
|
15152
|
+
provider: "cursor",
|
|
15153
|
+
...event.moduleVersion ? { "cursor_sdk.version": event.moduleVersion } : {}
|
|
15154
|
+
};
|
|
15155
|
+
const span = startSpan({
|
|
15156
|
+
name: "Cursor Agent",
|
|
15157
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
15158
|
+
});
|
|
15159
|
+
const startTime = getCurrentUnixTimestamp();
|
|
15160
|
+
safeLog(span, {
|
|
15161
|
+
input: sanitizeUserMessage(message),
|
|
15162
|
+
metadata
|
|
15163
|
+
});
|
|
15164
|
+
const state = {
|
|
15165
|
+
activeToolSpans: /* @__PURE__ */ new Map(),
|
|
15166
|
+
agent,
|
|
15167
|
+
conversationText: [],
|
|
15168
|
+
deltaText: [],
|
|
15169
|
+
finalized: false,
|
|
15170
|
+
input: message,
|
|
15171
|
+
metadata,
|
|
15172
|
+
metrics: {},
|
|
15173
|
+
span,
|
|
15174
|
+
startTime,
|
|
15175
|
+
streamMessages: [],
|
|
15176
|
+
streamText: [],
|
|
15177
|
+
stepText: [],
|
|
15178
|
+
taskText: []
|
|
15179
|
+
};
|
|
15180
|
+
if (hasCursorCallbacks(sendOptions)) {
|
|
15181
|
+
event.arguments[1] = wrapSendOptionsCallbacks(sendOptions, state);
|
|
15182
|
+
}
|
|
15183
|
+
states.set(event, state);
|
|
15184
|
+
},
|
|
15185
|
+
asyncEnd: (event) => {
|
|
15186
|
+
const state = states.get(event);
|
|
15187
|
+
if (!state) {
|
|
15188
|
+
return;
|
|
15189
|
+
}
|
|
15190
|
+
if (!event.result) {
|
|
15191
|
+
return;
|
|
15192
|
+
}
|
|
15193
|
+
state.run = event.result;
|
|
15194
|
+
state.metadata = {
|
|
15195
|
+
...state.metadata,
|
|
15196
|
+
...extractRunMetadata(event.result)
|
|
15197
|
+
};
|
|
15198
|
+
patchCursorRun(event.result, state);
|
|
15199
|
+
},
|
|
15200
|
+
error: (event) => {
|
|
15201
|
+
const state = states.get(event);
|
|
15202
|
+
if (!state || !event.error) {
|
|
15203
|
+
return;
|
|
15204
|
+
}
|
|
15205
|
+
safeLog(state.span, { error: event.error.message });
|
|
15206
|
+
endOpenToolSpans(state, event.error.message);
|
|
15207
|
+
state.span.end();
|
|
15208
|
+
state.finalized = true;
|
|
15209
|
+
states.delete(event);
|
|
15210
|
+
}
|
|
15211
|
+
};
|
|
15212
|
+
channel2.subscribe(handlers);
|
|
15213
|
+
this.unsubscribers.push(() => {
|
|
15214
|
+
channel2.unsubscribe(handlers);
|
|
15215
|
+
});
|
|
15216
|
+
}
|
|
15217
|
+
}, _class18);
|
|
15218
|
+
function patchCursorAgentInPlace(agent) {
|
|
15219
|
+
if (!agent || typeof agent !== "object") {
|
|
15220
|
+
return;
|
|
15221
|
+
}
|
|
15222
|
+
const agentRecord = agent;
|
|
15223
|
+
if (agentRecord[PATCHED_AGENT] || agentRecord[WRAPPED_AGENT] || typeof agentRecord.send !== "function") {
|
|
15224
|
+
return;
|
|
15225
|
+
}
|
|
15226
|
+
const originalSend = agentRecord.send.bind(agentRecord);
|
|
15227
|
+
try {
|
|
15228
|
+
Object.defineProperty(agentRecord, PATCHED_AGENT, {
|
|
15229
|
+
configurable: false,
|
|
15230
|
+
enumerable: false,
|
|
15231
|
+
value: true
|
|
15232
|
+
});
|
|
15233
|
+
Object.defineProperty(agentRecord, "send", {
|
|
15234
|
+
configurable: true,
|
|
15235
|
+
value(message, options) {
|
|
15236
|
+
const args = [message, options];
|
|
15237
|
+
return cursorSDKChannels.send.tracePromise(
|
|
15238
|
+
() => originalSend(...args),
|
|
15239
|
+
{
|
|
15240
|
+
agent: agentRecord,
|
|
15241
|
+
arguments: args,
|
|
15242
|
+
operation: "send"
|
|
15243
|
+
}
|
|
15244
|
+
);
|
|
15245
|
+
},
|
|
15246
|
+
writable: true
|
|
15247
|
+
});
|
|
15248
|
+
} catch (e36) {
|
|
15249
|
+
}
|
|
15250
|
+
}
|
|
15251
|
+
function wrapSendOptionsCallbacks(options, state) {
|
|
15252
|
+
const originalOnDelta = options.onDelta;
|
|
15253
|
+
const originalOnStep = options.onStep;
|
|
15254
|
+
return {
|
|
15255
|
+
...options,
|
|
15256
|
+
async onDelta(args) {
|
|
15257
|
+
try {
|
|
15258
|
+
await handleInteractionUpdate(state, args.update);
|
|
15259
|
+
} catch (error) {
|
|
15260
|
+
logInstrumentationError("Cursor SDK onDelta", error);
|
|
15261
|
+
}
|
|
15262
|
+
if (originalOnDelta) {
|
|
15263
|
+
return originalOnDelta(args);
|
|
15264
|
+
}
|
|
15265
|
+
},
|
|
15266
|
+
async onStep(args) {
|
|
15267
|
+
try {
|
|
15268
|
+
handleStepUpdate(state, args.step);
|
|
15269
|
+
} catch (error) {
|
|
15270
|
+
logInstrumentationError("Cursor SDK onStep", error);
|
|
15271
|
+
}
|
|
15272
|
+
if (originalOnStep) {
|
|
15273
|
+
return originalOnStep(args);
|
|
15274
|
+
}
|
|
15275
|
+
}
|
|
15276
|
+
};
|
|
15277
|
+
}
|
|
15278
|
+
function hasCursorCallbacks(options) {
|
|
15279
|
+
return !!options && (typeof options.onDelta === "function" || typeof options.onStep === "function");
|
|
15280
|
+
}
|
|
15281
|
+
function patchCursorRun(run, state) {
|
|
15282
|
+
if (!run || typeof run !== "object") {
|
|
15283
|
+
return;
|
|
15284
|
+
}
|
|
15285
|
+
const runRecord = run;
|
|
15286
|
+
if (runRecord[PATCHED_RUN]) {
|
|
15287
|
+
return;
|
|
15288
|
+
}
|
|
15289
|
+
try {
|
|
15290
|
+
Object.defineProperty(runRecord, PATCHED_RUN, {
|
|
15291
|
+
configurable: false,
|
|
15292
|
+
enumerable: false,
|
|
15293
|
+
value: true
|
|
15294
|
+
});
|
|
15295
|
+
if (typeof runRecord.stream === "function") {
|
|
15296
|
+
const originalStream = runRecord.stream.bind(runRecord);
|
|
15297
|
+
Object.defineProperty(runRecord, "stream", {
|
|
15298
|
+
configurable: true,
|
|
15299
|
+
value() {
|
|
15300
|
+
const stream = originalStream();
|
|
15301
|
+
return patchCursorStream(stream, state);
|
|
15302
|
+
},
|
|
15303
|
+
writable: true
|
|
15304
|
+
});
|
|
15305
|
+
}
|
|
15306
|
+
if (typeof runRecord.wait === "function") {
|
|
15307
|
+
const originalWait = runRecord.wait.bind(runRecord);
|
|
15308
|
+
Object.defineProperty(runRecord, "wait", {
|
|
15309
|
+
configurable: true,
|
|
15310
|
+
async value() {
|
|
15311
|
+
try {
|
|
15312
|
+
const result = await originalWait();
|
|
15313
|
+
state.lastResult = result;
|
|
15314
|
+
await finalizeCursorRun(state, { result });
|
|
15315
|
+
return result;
|
|
15316
|
+
} catch (error) {
|
|
15317
|
+
await finalizeCursorRun(state, { error });
|
|
15318
|
+
throw error;
|
|
15319
|
+
}
|
|
15320
|
+
},
|
|
15321
|
+
writable: true
|
|
15322
|
+
});
|
|
15323
|
+
}
|
|
15324
|
+
if (typeof runRecord.conversation === "function") {
|
|
15325
|
+
const originalConversation = runRecord.conversation.bind(runRecord);
|
|
15326
|
+
Object.defineProperty(runRecord, "conversation", {
|
|
15327
|
+
configurable: true,
|
|
15328
|
+
async value() {
|
|
15329
|
+
try {
|
|
15330
|
+
const conversation = await originalConversation();
|
|
15331
|
+
await handleConversation(state, conversation);
|
|
15332
|
+
await finalizeCursorRun(state);
|
|
15333
|
+
return conversation;
|
|
15334
|
+
} catch (error) {
|
|
15335
|
+
await finalizeCursorRun(state, { error });
|
|
15336
|
+
throw error;
|
|
15337
|
+
}
|
|
15338
|
+
},
|
|
15339
|
+
writable: true
|
|
15340
|
+
});
|
|
15341
|
+
}
|
|
15342
|
+
} catch (e37) {
|
|
15343
|
+
void finalizeCursorRun(state, { output: run });
|
|
15344
|
+
}
|
|
15345
|
+
}
|
|
15346
|
+
async function* patchCursorStream(stream, state) {
|
|
15347
|
+
try {
|
|
15348
|
+
for await (const message of stream) {
|
|
15349
|
+
try {
|
|
15350
|
+
await handleStreamMessage2(state, message);
|
|
15351
|
+
} catch (error) {
|
|
15352
|
+
logInstrumentationError("Cursor SDK stream", error);
|
|
15353
|
+
}
|
|
15354
|
+
yield message;
|
|
15355
|
+
}
|
|
15356
|
+
await finalizeCursorRun(state);
|
|
15357
|
+
} catch (error) {
|
|
15358
|
+
await finalizeCursorRun(state, { error });
|
|
15359
|
+
throw error;
|
|
15360
|
+
}
|
|
15361
|
+
}
|
|
15362
|
+
async function handleInteractionUpdate(state, update) {
|
|
15363
|
+
switch (update.type) {
|
|
15364
|
+
case "text-delta":
|
|
15365
|
+
if (typeof update.text === "string") {
|
|
15366
|
+
state.deltaText.push(update.text);
|
|
15367
|
+
}
|
|
15368
|
+
return;
|
|
15369
|
+
case "token-delta":
|
|
15370
|
+
if (typeof update.tokens === "number") {
|
|
15371
|
+
state.metrics["cursor_sdk.delta_tokens"] = (_nullishCoalesce(state.metrics["cursor_sdk.delta_tokens"], () => ( 0))) + update.tokens;
|
|
15372
|
+
}
|
|
15373
|
+
return;
|
|
15374
|
+
case "tool-call-started":
|
|
15375
|
+
case "partial-tool-call":
|
|
15376
|
+
case "tool-call-completed":
|
|
15377
|
+
await handleToolUpdate(
|
|
15378
|
+
state,
|
|
15379
|
+
update
|
|
15380
|
+
);
|
|
15381
|
+
return;
|
|
15382
|
+
case "turn-ended":
|
|
15383
|
+
addUsageMetrics(
|
|
15384
|
+
state.metrics,
|
|
15385
|
+
update.usage
|
|
15386
|
+
);
|
|
15387
|
+
return;
|
|
15388
|
+
case "summary":
|
|
15389
|
+
if (typeof update.summary === "string") {
|
|
15390
|
+
state.taskText.push(update.summary);
|
|
15391
|
+
}
|
|
15392
|
+
return;
|
|
15393
|
+
case "step-completed":
|
|
15394
|
+
if (typeof update.stepDurationMs === "number") {
|
|
15395
|
+
state.metrics["cursor_sdk.step_duration_ms"] = (_nullishCoalesce(state.metrics["cursor_sdk.step_duration_ms"], () => ( 0))) + update.stepDurationMs;
|
|
15396
|
+
}
|
|
15397
|
+
state.metrics["cursor_sdk.steps"] = (_nullishCoalesce(state.metrics["cursor_sdk.steps"], () => ( 0))) + 1;
|
|
15398
|
+
return;
|
|
15399
|
+
default:
|
|
15400
|
+
return;
|
|
15401
|
+
}
|
|
15402
|
+
}
|
|
15403
|
+
async function handleToolUpdate(state, update) {
|
|
15404
|
+
const callId = update.callId;
|
|
15405
|
+
if (!callId) {
|
|
15406
|
+
return;
|
|
15407
|
+
}
|
|
15408
|
+
const toolCall = update.toolCall;
|
|
15409
|
+
const name = extractToolName(toolCall);
|
|
15410
|
+
const args = extractToolArgs(toolCall);
|
|
15411
|
+
const result = extractToolResult(toolCall);
|
|
15412
|
+
if (update.type === "tool-call-started" || update.type === "partial-tool-call") {
|
|
15413
|
+
if (!state.activeToolSpans.has(callId)) {
|
|
15414
|
+
state.activeToolSpans.set(
|
|
15415
|
+
callId,
|
|
15416
|
+
await startToolSpan(state, {
|
|
15417
|
+
args,
|
|
15418
|
+
callId,
|
|
15419
|
+
name,
|
|
15420
|
+
status: "running",
|
|
15421
|
+
toolCall
|
|
15422
|
+
})
|
|
15423
|
+
);
|
|
15424
|
+
}
|
|
15425
|
+
return;
|
|
15426
|
+
}
|
|
15427
|
+
const toolState = await _asyncNullishCoalesce(state.activeToolSpans.get(callId), async () => ( await startToolSpan(state, {
|
|
15428
|
+
args,
|
|
15429
|
+
callId,
|
|
15430
|
+
name,
|
|
15431
|
+
status: "completed",
|
|
15432
|
+
toolCall
|
|
15433
|
+
})));
|
|
15434
|
+
finishToolSpan(toolState, {
|
|
15435
|
+
error: _optionalChain([toolCall, 'optionalAccess', _304 => _304.status]) === "error" ? stringifyUnknown(result) : void 0,
|
|
15436
|
+
metadata: {
|
|
15437
|
+
"cursor_sdk.tool.status": _nullishCoalesce(_optionalChain([toolCall, 'optionalAccess', _305 => _305.status]), () => ( "completed"))
|
|
15438
|
+
},
|
|
15439
|
+
output: result
|
|
15440
|
+
});
|
|
15441
|
+
state.activeToolSpans.delete(callId);
|
|
15442
|
+
}
|
|
15443
|
+
async function handleStreamMessage2(state, message) {
|
|
15444
|
+
state.streamMessages.push(message);
|
|
15445
|
+
if (message.type === "system") {
|
|
15446
|
+
const systemMessage = message;
|
|
15447
|
+
state.metadata = {
|
|
15448
|
+
...state.metadata,
|
|
15449
|
+
...extractModelMetadata(systemMessage.model),
|
|
15450
|
+
...systemMessage.agent_id ? { "cursor_sdk.agent_id": systemMessage.agent_id } : {},
|
|
15451
|
+
...systemMessage.run_id ? { "cursor_sdk.run_id": systemMessage.run_id } : {},
|
|
15452
|
+
...systemMessage.tools ? { "cursor_sdk.tools": systemMessage.tools } : {}
|
|
15453
|
+
};
|
|
15454
|
+
return;
|
|
15455
|
+
}
|
|
15456
|
+
if (message.type === "assistant") {
|
|
15457
|
+
const assistantMessage = message;
|
|
15458
|
+
for (const block of _nullishCoalesce(_optionalChain([assistantMessage, 'access', _306 => _306.message, 'optionalAccess', _307 => _307.content]), () => ( []))) {
|
|
15459
|
+
if (_optionalChain([block, 'optionalAccess', _308 => _308.type]) === "text" && typeof block.text === "string") {
|
|
15460
|
+
state.streamText.push(block.text);
|
|
15461
|
+
} else if (_optionalChain([block, 'optionalAccess', _309 => _309.type]) === "tool_use" && block.id) {
|
|
15462
|
+
state.activeToolSpans.set(
|
|
15463
|
+
block.id,
|
|
15464
|
+
await startToolSpan(state, {
|
|
15465
|
+
args: block.input,
|
|
15466
|
+
callId: block.id,
|
|
15467
|
+
name: block.name,
|
|
15468
|
+
status: "running"
|
|
15469
|
+
})
|
|
15470
|
+
);
|
|
15471
|
+
}
|
|
15472
|
+
}
|
|
15473
|
+
return;
|
|
15474
|
+
}
|
|
15475
|
+
if (message.type === "tool_call") {
|
|
15476
|
+
await handleToolMessage(
|
|
15477
|
+
state,
|
|
15478
|
+
message
|
|
15479
|
+
);
|
|
15480
|
+
return;
|
|
15481
|
+
}
|
|
15482
|
+
if (message.type === "task" && typeof message.text === "string") {
|
|
15483
|
+
state.taskText.push(message.text);
|
|
15484
|
+
return;
|
|
15485
|
+
}
|
|
15486
|
+
if (message.type === "status" && message.status) {
|
|
15487
|
+
state.metadata["cursor_sdk.status"] = message.status;
|
|
15488
|
+
}
|
|
15489
|
+
}
|
|
15490
|
+
async function handleToolMessage(state, message) {
|
|
15491
|
+
const callId = message.call_id;
|
|
15492
|
+
if (!callId) {
|
|
15493
|
+
return;
|
|
15494
|
+
}
|
|
15495
|
+
if (message.status === "running") {
|
|
15496
|
+
if (!state.activeToolSpans.has(callId)) {
|
|
15497
|
+
state.activeToolSpans.set(
|
|
15498
|
+
callId,
|
|
15499
|
+
await startToolSpan(state, {
|
|
15500
|
+
args: message.args,
|
|
15501
|
+
callId,
|
|
15502
|
+
name: message.name,
|
|
15503
|
+
status: message.status,
|
|
15504
|
+
truncated: message.truncated
|
|
15505
|
+
})
|
|
15506
|
+
);
|
|
15507
|
+
}
|
|
15508
|
+
return;
|
|
15509
|
+
}
|
|
15510
|
+
const toolState = await _asyncNullishCoalesce(state.activeToolSpans.get(callId), async () => ( await startToolSpan(state, {
|
|
15511
|
+
args: message.args,
|
|
15512
|
+
callId,
|
|
15513
|
+
name: message.name,
|
|
15514
|
+
status: message.status,
|
|
15515
|
+
truncated: message.truncated
|
|
15516
|
+
})));
|
|
15517
|
+
finishToolSpan(toolState, {
|
|
15518
|
+
error: message.status === "error" ? stringifyUnknown(message.result) : void 0,
|
|
15519
|
+
metadata: {
|
|
15520
|
+
"cursor_sdk.tool.status": message.status
|
|
15521
|
+
},
|
|
15522
|
+
output: message.result
|
|
15523
|
+
});
|
|
15524
|
+
state.activeToolSpans.delete(callId);
|
|
15525
|
+
}
|
|
15526
|
+
async function handleConversation(state, turns) {
|
|
15527
|
+
state.conversationOutput = turns;
|
|
15528
|
+
for (const turn of turns) {
|
|
15529
|
+
if (turn.type === "agentConversationTurn") {
|
|
15530
|
+
for (const step of _nullishCoalesce(_optionalChain([turn, 'access', _310 => _310.turn, 'optionalAccess', _311 => _311.steps]), () => ( []))) {
|
|
15531
|
+
await handleConversationStep(state, step);
|
|
15532
|
+
}
|
|
15533
|
+
} else if (turn.type === "shellConversationTurn") {
|
|
15534
|
+
const command = _optionalChain([turn, 'access', _312 => _312.turn, 'optionalAccess', _313 => _313.shellCommand, 'optionalAccess', _314 => _314.command]);
|
|
15535
|
+
if (command) {
|
|
15536
|
+
const callId = `shell:${state.activeToolSpans.size}:${command}`;
|
|
15537
|
+
const toolState = await startToolSpan(state, {
|
|
15538
|
+
args: _optionalChain([turn, 'access', _315 => _315.turn, 'optionalAccess', _316 => _316.shellCommand]),
|
|
15539
|
+
callId,
|
|
15540
|
+
name: "shell",
|
|
15541
|
+
status: "completed"
|
|
15542
|
+
});
|
|
15543
|
+
finishToolSpan(toolState, {
|
|
15544
|
+
metadata: { "cursor_sdk.tool.status": "completed" },
|
|
15545
|
+
output: _optionalChain([turn, 'access', _317 => _317.turn, 'optionalAccess', _318 => _318.shellOutput])
|
|
15546
|
+
});
|
|
15547
|
+
}
|
|
15548
|
+
}
|
|
15549
|
+
}
|
|
15550
|
+
}
|
|
15551
|
+
async function handleConversationStep(state, step) {
|
|
15552
|
+
if (step.type === "assistantMessage" && typeof _optionalChain([step, 'access', _319 => _319.message, 'optionalAccess', _320 => _320.text]) === "string") {
|
|
15553
|
+
state.conversationText.push(step.message.text);
|
|
15554
|
+
return;
|
|
15555
|
+
}
|
|
15556
|
+
if (step.type !== "toolCall") {
|
|
15557
|
+
return;
|
|
15558
|
+
}
|
|
15559
|
+
const toolCall = step.message;
|
|
15560
|
+
const callId = typeof _optionalChain([toolCall, 'optionalAccess', _321 => _321.callId]) === "string" ? toolCall.callId : `conversation-tool:${state.activeToolSpans.size}`;
|
|
15561
|
+
const toolState = await startToolSpan(state, {
|
|
15562
|
+
args: extractToolArgs(toolCall),
|
|
15563
|
+
callId,
|
|
15564
|
+
name: extractToolName(toolCall),
|
|
15565
|
+
status: _optionalChain([toolCall, 'optionalAccess', _322 => _322.status]),
|
|
15566
|
+
toolCall
|
|
15567
|
+
});
|
|
15568
|
+
finishToolSpan(toolState, {
|
|
15569
|
+
error: _optionalChain([toolCall, 'optionalAccess', _323 => _323.status]) === "error" ? stringifyUnknown(toolCall.result) : void 0,
|
|
15570
|
+
metadata: {
|
|
15571
|
+
"cursor_sdk.tool.status": _nullishCoalesce(_optionalChain([toolCall, 'optionalAccess', _324 => _324.status]), () => ( "completed"))
|
|
15572
|
+
},
|
|
15573
|
+
output: extractToolResult(toolCall)
|
|
15574
|
+
});
|
|
15575
|
+
}
|
|
15576
|
+
function handleStepUpdate(state, step) {
|
|
15577
|
+
state.metrics["cursor_sdk.steps"] = (_nullishCoalesce(state.metrics["cursor_sdk.steps"], () => ( 0))) + 1;
|
|
15578
|
+
if (step.type) {
|
|
15579
|
+
const stepTypes = state.metadata["cursor_sdk.step_types"];
|
|
15580
|
+
if (Array.isArray(stepTypes)) {
|
|
15581
|
+
if (!stepTypes.includes(step.type)) {
|
|
15582
|
+
stepTypes.push(step.type);
|
|
15583
|
+
}
|
|
15584
|
+
} else {
|
|
15585
|
+
state.metadata["cursor_sdk.step_types"] = [step.type];
|
|
15586
|
+
}
|
|
15587
|
+
}
|
|
15588
|
+
if (step.type === "assistantMessage" && typeof _optionalChain([step, 'access', _325 => _325.message, 'optionalAccess', _326 => _326.text]) === "string") {
|
|
15589
|
+
state.stepText.push(step.message.text);
|
|
15590
|
+
}
|
|
15591
|
+
}
|
|
15592
|
+
async function startToolSpan(state, args) {
|
|
15593
|
+
const name = args.name || "unknown";
|
|
15594
|
+
const metadata = {
|
|
15595
|
+
"cursor_sdk.tool.status": args.status,
|
|
15596
|
+
"gen_ai.tool.call.id": args.callId,
|
|
15597
|
+
"gen_ai.tool.name": name
|
|
15598
|
+
};
|
|
15599
|
+
if (_optionalChain([args, 'access', _327 => _327.truncated, 'optionalAccess', _328 => _328.args]) !== void 0) {
|
|
15600
|
+
metadata["cursor_sdk.tool.args_truncated"] = args.truncated.args;
|
|
15601
|
+
}
|
|
15602
|
+
if (_optionalChain([args, 'access', _329 => _329.truncated, 'optionalAccess', _330 => _330.result]) !== void 0) {
|
|
15603
|
+
metadata["cursor_sdk.tool.result_truncated"] = args.truncated.result;
|
|
15604
|
+
}
|
|
15605
|
+
const span = startSpan({
|
|
15606
|
+
event: {
|
|
15607
|
+
input: args.args,
|
|
15608
|
+
metadata
|
|
15609
|
+
},
|
|
15610
|
+
name: `tool: ${name}`,
|
|
15611
|
+
parent: await state.span.export(),
|
|
15612
|
+
spanAttributes: { type: "tool" /* TOOL */ }
|
|
15613
|
+
});
|
|
15614
|
+
let subAgentSpan;
|
|
15615
|
+
if (isSubAgentToolName(name)) {
|
|
15616
|
+
subAgentSpan = startSpan({
|
|
15617
|
+
event: {
|
|
15618
|
+
input: args.args,
|
|
15619
|
+
metadata: {
|
|
15620
|
+
"cursor_sdk.subagent.tool_call_id": args.callId,
|
|
15621
|
+
"gen_ai.tool.name": name
|
|
15622
|
+
}
|
|
15623
|
+
},
|
|
15624
|
+
name: formatSubAgentSpanName2(args.toolCall, args.args),
|
|
15625
|
+
parent: await span.export(),
|
|
15626
|
+
spanAttributes: { type: "task" /* TASK */ }
|
|
15627
|
+
});
|
|
15628
|
+
}
|
|
15629
|
+
return { span, subAgentSpan };
|
|
15630
|
+
}
|
|
15631
|
+
function finishToolSpan(toolState, result) {
|
|
15632
|
+
try {
|
|
15633
|
+
if (result.error) {
|
|
15634
|
+
safeLog(toolState.span, {
|
|
15635
|
+
error: result.error,
|
|
15636
|
+
metadata: result.metadata,
|
|
15637
|
+
output: result.output
|
|
15638
|
+
});
|
|
15639
|
+
if (toolState.subAgentSpan) {
|
|
15640
|
+
safeLog(toolState.subAgentSpan, {
|
|
15641
|
+
error: result.error,
|
|
15642
|
+
metadata: result.metadata,
|
|
15643
|
+
output: result.output
|
|
15644
|
+
});
|
|
15645
|
+
}
|
|
15646
|
+
} else {
|
|
15647
|
+
safeLog(toolState.span, {
|
|
15648
|
+
metadata: result.metadata,
|
|
15649
|
+
output: result.output
|
|
15650
|
+
});
|
|
15651
|
+
if (toolState.subAgentSpan) {
|
|
15652
|
+
safeLog(toolState.subAgentSpan, {
|
|
15653
|
+
metadata: result.metadata,
|
|
15654
|
+
output: result.output
|
|
15655
|
+
});
|
|
15656
|
+
}
|
|
15657
|
+
}
|
|
15658
|
+
} finally {
|
|
15659
|
+
_optionalChain([toolState, 'access', _331 => _331.subAgentSpan, 'optionalAccess', _332 => _332.end, 'call', _333 => _333()]);
|
|
15660
|
+
toolState.span.end();
|
|
15661
|
+
}
|
|
15662
|
+
}
|
|
15663
|
+
async function finalizeCursorRun(state, params = {}) {
|
|
15664
|
+
if (state.finalized) {
|
|
15665
|
+
return;
|
|
15666
|
+
}
|
|
15667
|
+
state.finalized = true;
|
|
15668
|
+
const error = params.error;
|
|
15669
|
+
const result = _nullishCoalesce(params.result, () => ( state.lastResult));
|
|
15670
|
+
const output = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(params.output, () => ( _optionalChain([result, 'optionalAccess', _334 => _334.result]))), () => ( _optionalChain([state, 'access', _335 => _335.run, 'optionalAccess', _336 => _336.result]))), () => ( (state.conversationText.length > 0 ? state.conversationText.join("\n") : void 0))), () => ( state.conversationOutput)), () => ( (state.streamText.length > 0 ? state.streamText.join("") : void 0))), () => ( (state.deltaText.length > 0 ? state.deltaText.join("") : void 0))), () => ( (state.stepText.length > 0 ? state.stepText.join("\n") : void 0))), () => ( (state.taskText.length > 0 ? state.taskText.join("\n") : void 0)));
|
|
15671
|
+
try {
|
|
15672
|
+
const metadata = {
|
|
15673
|
+
...state.metadata,
|
|
15674
|
+
...state.run ? extractRunMetadata(state.run) : {},
|
|
15675
|
+
...result ? extractRunResultMetadata(result) : {}
|
|
15676
|
+
};
|
|
15677
|
+
if (error) {
|
|
15678
|
+
safeLog(state.span, {
|
|
15679
|
+
error: error instanceof Error ? error.message : String(error),
|
|
15680
|
+
metadata,
|
|
15681
|
+
metrics: {
|
|
15682
|
+
...cleanMetrics(state.metrics),
|
|
15683
|
+
...buildDurationMetrics(state.startTime)
|
|
15684
|
+
},
|
|
15685
|
+
output
|
|
15686
|
+
});
|
|
15687
|
+
} else {
|
|
15688
|
+
safeLog(state.span, {
|
|
15689
|
+
metadata,
|
|
15690
|
+
metrics: {
|
|
15691
|
+
...cleanMetrics(state.metrics),
|
|
15692
|
+
...buildDurationMetrics(state.startTime)
|
|
15693
|
+
},
|
|
15694
|
+
output
|
|
15695
|
+
});
|
|
15696
|
+
}
|
|
15697
|
+
} finally {
|
|
15698
|
+
endOpenToolSpans(state);
|
|
15699
|
+
state.span.end();
|
|
15700
|
+
}
|
|
15701
|
+
}
|
|
15702
|
+
function endOpenToolSpans(state, error) {
|
|
15703
|
+
for (const [, toolState] of state.activeToolSpans) {
|
|
15704
|
+
finishToolSpan(toolState, { error });
|
|
15705
|
+
}
|
|
15706
|
+
state.activeToolSpans.clear();
|
|
15707
|
+
}
|
|
15708
|
+
function sanitizeUserMessage(message) {
|
|
15709
|
+
if (typeof message === "string" || message === void 0) {
|
|
15710
|
+
return message;
|
|
15711
|
+
}
|
|
15712
|
+
return {
|
|
15713
|
+
...message,
|
|
15714
|
+
images: _optionalChain([message, 'access', _337 => _337.images, 'optionalAccess', _338 => _338.map, 'call', _339 => _339((image) => {
|
|
15715
|
+
const imageRecord = image;
|
|
15716
|
+
return {
|
|
15717
|
+
...typeof imageRecord.url === "string" ? { url: imageRecord.url } : {},
|
|
15718
|
+
...typeof imageRecord.mimeType === "string" ? { mimeType: imageRecord.mimeType } : {},
|
|
15719
|
+
...image.dimension ? { dimension: image.dimension } : {},
|
|
15720
|
+
hasData: typeof imageRecord.data === "string"
|
|
15721
|
+
};
|
|
15722
|
+
})])
|
|
15723
|
+
};
|
|
15724
|
+
}
|
|
15725
|
+
function extractAgentOptionsMetadata(options) {
|
|
15726
|
+
if (!options) {
|
|
15727
|
+
return {};
|
|
15728
|
+
}
|
|
15729
|
+
return {
|
|
15730
|
+
...extractModelMetadata(options.model),
|
|
15731
|
+
...options.agentId ? { "cursor_sdk.agent_id": options.agentId } : {},
|
|
15732
|
+
...options.name ? { "cursor_sdk.agent_name": options.name } : {},
|
|
15733
|
+
...options.local ? {
|
|
15734
|
+
"cursor_sdk.runtime": "local",
|
|
15735
|
+
"cursor_sdk.local.cwd": Array.isArray(options.local.cwd) ? options.local.cwd.join(",") : options.local.cwd
|
|
15736
|
+
} : {},
|
|
15737
|
+
...options.cloud ? {
|
|
15738
|
+
"cursor_sdk.runtime": "cloud",
|
|
15739
|
+
"cursor_sdk.cloud.auto_create_pr": options.cloud.autoCreatePR,
|
|
15740
|
+
"cursor_sdk.cloud.env_type": _optionalChain([options, 'access', _340 => _340.cloud, 'access', _341 => _341.env, 'optionalAccess', _342 => _342.type]),
|
|
15741
|
+
"cursor_sdk.cloud.env_name": _optionalChain([options, 'access', _343 => _343.cloud, 'access', _344 => _344.env, 'optionalAccess', _345 => _345.name]),
|
|
15742
|
+
"cursor_sdk.cloud.repos": _optionalChain([options, 'access', _346 => _346.cloud, 'access', _347 => _347.repos, 'optionalAccess', _348 => _348.map, 'call', _349 => _349((repo) => repo.url), 'access', _350 => _350.filter, 'call', _351 => _351((url) => typeof url === "string")])
|
|
15743
|
+
} : {}
|
|
15744
|
+
};
|
|
15745
|
+
}
|
|
15746
|
+
function extractSendMetadata(options) {
|
|
15747
|
+
if (!options) {
|
|
15748
|
+
return {};
|
|
15749
|
+
}
|
|
15750
|
+
return {
|
|
15751
|
+
...extractModelMetadata(options.model),
|
|
15752
|
+
..._optionalChain([options, 'access', _352 => _352.local, 'optionalAccess', _353 => _353.force]) !== void 0 ? { "cursor_sdk.local.force": options.local.force } : {}
|
|
15753
|
+
};
|
|
15754
|
+
}
|
|
15755
|
+
function extractAgentMetadata(agent) {
|
|
15756
|
+
return {
|
|
15757
|
+
...agent.agentId ? { "cursor_sdk.agent_id": agent.agentId } : {},
|
|
15758
|
+
...extractModelMetadata(agent.model)
|
|
15759
|
+
};
|
|
15760
|
+
}
|
|
15761
|
+
function extractRunMetadata(run) {
|
|
15762
|
+
if (!run) {
|
|
15763
|
+
return {};
|
|
15764
|
+
}
|
|
15765
|
+
return {
|
|
15766
|
+
...run.id ? { "cursor_sdk.run_id": run.id } : {},
|
|
15767
|
+
...run.agentId ? { "cursor_sdk.agent_id": run.agentId } : {},
|
|
15768
|
+
...run.status ? { "cursor_sdk.status": run.status } : {},
|
|
15769
|
+
...run.durationMs !== void 0 ? { "cursor_sdk.duration_ms": run.durationMs } : {},
|
|
15770
|
+
...extractModelMetadata(run.model),
|
|
15771
|
+
...extractGitMetadata(run.git)
|
|
15772
|
+
};
|
|
15773
|
+
}
|
|
15774
|
+
function extractRunResultMetadata(result) {
|
|
15775
|
+
if (!result) {
|
|
15776
|
+
return {};
|
|
15777
|
+
}
|
|
15778
|
+
return {
|
|
15779
|
+
...result.id ? { "cursor_sdk.run_id": result.id } : {},
|
|
15780
|
+
...result.status ? { "cursor_sdk.status": result.status } : {},
|
|
15781
|
+
...result.durationMs !== void 0 ? { "cursor_sdk.duration_ms": result.durationMs } : {},
|
|
15782
|
+
...extractModelMetadata(result.model),
|
|
15783
|
+
...extractGitMetadata(result.git)
|
|
15784
|
+
};
|
|
15785
|
+
}
|
|
15786
|
+
function extractGitMetadata(git) {
|
|
15787
|
+
const branches = _optionalChain([git, 'optionalAccess', _354 => _354.branches]);
|
|
15788
|
+
if (!branches || branches.length === 0) {
|
|
15789
|
+
return {};
|
|
15790
|
+
}
|
|
15791
|
+
return {
|
|
15792
|
+
"cursor_sdk.git.branches": branches.map((branch) => ({
|
|
15793
|
+
branch: branch.branch,
|
|
15794
|
+
prUrl: branch.prUrl,
|
|
15795
|
+
repoUrl: branch.repoUrl
|
|
15796
|
+
}))
|
|
15797
|
+
};
|
|
15798
|
+
}
|
|
15799
|
+
function extractModelMetadata(model) {
|
|
15800
|
+
if (!_optionalChain([model, 'optionalAccess', _355 => _355.id])) {
|
|
15801
|
+
return {};
|
|
15802
|
+
}
|
|
15803
|
+
return {
|
|
15804
|
+
model: model.id,
|
|
15805
|
+
"cursor_sdk.model": model.id,
|
|
15806
|
+
...model.params ? { "cursor_sdk.model.params": model.params } : {}
|
|
15807
|
+
};
|
|
15808
|
+
}
|
|
15809
|
+
function addUsageMetrics(metrics, usage) {
|
|
15810
|
+
if (!usage) {
|
|
15811
|
+
return;
|
|
15812
|
+
}
|
|
15813
|
+
if (usage.inputTokens !== void 0) {
|
|
15814
|
+
metrics.prompt_tokens = (_nullishCoalesce(metrics.prompt_tokens, () => ( 0))) + usage.inputTokens;
|
|
15815
|
+
}
|
|
15816
|
+
if (usage.outputTokens !== void 0) {
|
|
15817
|
+
metrics.completion_tokens = (_nullishCoalesce(metrics.completion_tokens, () => ( 0))) + usage.outputTokens;
|
|
15818
|
+
}
|
|
15819
|
+
if (usage.cacheReadTokens !== void 0) {
|
|
15820
|
+
metrics.prompt_cached_tokens = (_nullishCoalesce(metrics.prompt_cached_tokens, () => ( 0))) + usage.cacheReadTokens;
|
|
15821
|
+
}
|
|
15822
|
+
if (usage.cacheWriteTokens !== void 0) {
|
|
15823
|
+
metrics.prompt_cache_creation_tokens = (_nullishCoalesce(metrics.prompt_cache_creation_tokens, () => ( 0))) + usage.cacheWriteTokens;
|
|
15824
|
+
}
|
|
15825
|
+
metrics.tokens = (_nullishCoalesce(metrics.prompt_tokens, () => ( 0))) + (_nullishCoalesce(metrics.completion_tokens, () => ( 0))) + (_nullishCoalesce(metrics.prompt_cached_tokens, () => ( 0))) + (_nullishCoalesce(metrics.prompt_cache_creation_tokens, () => ( 0)));
|
|
15826
|
+
}
|
|
15827
|
+
function buildDurationMetrics(startTime) {
|
|
15828
|
+
const end = getCurrentUnixTimestamp();
|
|
15829
|
+
return {
|
|
15830
|
+
duration: end - startTime,
|
|
15831
|
+
end,
|
|
15832
|
+
start: startTime
|
|
15833
|
+
};
|
|
15834
|
+
}
|
|
15835
|
+
function extractToolName(toolCall) {
|
|
15836
|
+
if (!toolCall) {
|
|
15837
|
+
return "unknown";
|
|
15838
|
+
}
|
|
15839
|
+
if (typeof toolCall.name === "string") {
|
|
15840
|
+
return toolCall.name;
|
|
15841
|
+
}
|
|
15842
|
+
if (typeof toolCall.type === "string") {
|
|
15843
|
+
return toolCall.type;
|
|
15844
|
+
}
|
|
15845
|
+
return "unknown";
|
|
15846
|
+
}
|
|
15847
|
+
function extractToolArgs(toolCall) {
|
|
15848
|
+
return toolCall && "args" in toolCall ? toolCall.args : void 0;
|
|
15849
|
+
}
|
|
15850
|
+
function extractToolResult(toolCall) {
|
|
15851
|
+
return toolCall && "result" in toolCall ? toolCall.result : void 0;
|
|
15852
|
+
}
|
|
15853
|
+
function isSubAgentToolName(name) {
|
|
15854
|
+
return name === "Agent" || name === "Task" || name === "task";
|
|
15855
|
+
}
|
|
15856
|
+
function formatSubAgentSpanName2(toolCall, args) {
|
|
15857
|
+
const details = _nullishCoalesce(toolCall, () => ( args));
|
|
15858
|
+
const description = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(getString(details, "description"), () => ( getString(details, "subagent_type"))), () => ( getString(details, "type"))), () => ( getString(details, "name")));
|
|
15859
|
+
return description ? `Agent: ${description}` : "Agent: sub-agent";
|
|
15860
|
+
}
|
|
15861
|
+
function getString(obj, key) {
|
|
15862
|
+
const value = _optionalChain([obj, 'optionalAccess', _356 => _356[key]]);
|
|
15863
|
+
return typeof value === "string" ? value : void 0;
|
|
15864
|
+
}
|
|
15865
|
+
function stringifyUnknown(value) {
|
|
15866
|
+
if (value instanceof Error) {
|
|
15867
|
+
return value.message;
|
|
15868
|
+
}
|
|
15869
|
+
if (typeof value === "string") {
|
|
15870
|
+
return value;
|
|
15871
|
+
}
|
|
15872
|
+
try {
|
|
15873
|
+
return JSON.stringify(value);
|
|
15874
|
+
} catch (e38) {
|
|
15875
|
+
return String(value);
|
|
15876
|
+
}
|
|
15877
|
+
}
|
|
15878
|
+
function safeLog(span, event) {
|
|
15879
|
+
try {
|
|
15880
|
+
span.log(event);
|
|
15881
|
+
} catch (error) {
|
|
15882
|
+
logInstrumentationError("Cursor SDK span log", error);
|
|
15883
|
+
}
|
|
15884
|
+
}
|
|
15885
|
+
function logInstrumentationError(context, error) {
|
|
15886
|
+
debugLogger.error(`Error processing ${context}:`, error);
|
|
15887
|
+
}
|
|
15888
|
+
function cleanMetrics(metrics) {
|
|
15889
|
+
const cleaned = {};
|
|
15890
|
+
for (const [key, value] of Object.entries(metrics)) {
|
|
15891
|
+
if (value !== void 0 && Number.isFinite(value)) {
|
|
15892
|
+
cleaned[key] = value;
|
|
15893
|
+
}
|
|
15894
|
+
}
|
|
15895
|
+
return cleaned;
|
|
15896
|
+
}
|
|
15897
|
+
|
|
15898
|
+
// src/instrumentation/plugins/google-genai-channels.ts
|
|
15899
|
+
var googleGenAIChannels = defineChannels("@google/genai", {
|
|
15900
|
+
generateContent: channel({
|
|
15901
|
+
channelName: "models.generateContent",
|
|
15902
|
+
kind: "async"
|
|
15903
|
+
}),
|
|
15904
|
+
generateContentStream: channel({
|
|
15905
|
+
channelName: "models.generateContentStream",
|
|
15906
|
+
kind: "async"
|
|
15907
|
+
}),
|
|
15908
|
+
embedContent: channel({
|
|
15909
|
+
channelName: "models.embedContent",
|
|
15910
|
+
kind: "async"
|
|
15911
|
+
})
|
|
15912
|
+
});
|
|
15913
|
+
|
|
15914
|
+
// src/instrumentation/plugins/google-genai-plugin.ts
|
|
15915
|
+
var GOOGLE_GENAI_INTERNAL_CONTEXT = {
|
|
15916
|
+
caller_filename: "<node-internal>",
|
|
15917
|
+
caller_functionname: "<node-internal>",
|
|
15918
|
+
caller_lineno: 0
|
|
15919
|
+
};
|
|
15920
|
+
function createWrapperParityEvent(args) {
|
|
15921
|
+
return {
|
|
15922
|
+
context: GOOGLE_GENAI_INTERNAL_CONTEXT,
|
|
15923
|
+
input: args.input,
|
|
15924
|
+
metadata: args.metadata
|
|
15925
|
+
};
|
|
15926
|
+
}
|
|
15927
|
+
var GoogleGenAIPlugin = class extends BasePlugin {
|
|
15928
|
+
onEnable() {
|
|
15929
|
+
this.subscribeToGoogleGenAIChannels();
|
|
15930
|
+
}
|
|
15931
|
+
onDisable() {
|
|
15932
|
+
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
15933
|
+
}
|
|
15934
|
+
subscribeToGoogleGenAIChannels() {
|
|
15935
|
+
this.subscribeToGenerateContentChannel();
|
|
15936
|
+
this.subscribeToGenerateContentStreamChannel();
|
|
15937
|
+
this.subscribeToEmbedContentChannel();
|
|
15938
|
+
}
|
|
15939
|
+
subscribeToGenerateContentChannel() {
|
|
15940
|
+
const tracingChannel2 = googleGenAIChannels.generateContent.tracingChannel();
|
|
15941
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
15942
|
+
const unbindCurrentSpanStore = bindCurrentSpanStoreToStart2(
|
|
15943
|
+
tracingChannel2,
|
|
15944
|
+
states,
|
|
15945
|
+
(event) => {
|
|
15946
|
+
const params = event.arguments[0];
|
|
15947
|
+
const input = serializeGenerateContentInput(params);
|
|
14666
15948
|
const metadata = extractGenerateContentMetadata(params);
|
|
14667
15949
|
const span = startSpan({
|
|
14668
15950
|
name: "generate_content",
|
|
@@ -14705,7 +15987,7 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
14705
15987
|
const responseMetadata = extractResponseMetadata(event.result);
|
|
14706
15988
|
spanState.span.log({
|
|
14707
15989
|
...responseMetadata ? { metadata: responseMetadata } : {},
|
|
14708
|
-
metrics:
|
|
15990
|
+
metrics: cleanMetrics2(
|
|
14709
15991
|
extractGenerateContentMetrics(
|
|
14710
15992
|
event.result,
|
|
14711
15993
|
spanState.startTime
|
|
@@ -14724,7 +16006,7 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
14724
16006
|
};
|
|
14725
16007
|
tracingChannel2.subscribe(handlers);
|
|
14726
16008
|
this.unsubscribers.push(() => {
|
|
14727
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
16009
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _357 => _357()]);
|
|
14728
16010
|
tracingChannel2.unsubscribe(handlers);
|
|
14729
16011
|
});
|
|
14730
16012
|
}
|
|
@@ -14806,7 +16088,7 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
14806
16088
|
const output = summarizeEmbedContentOutput(event.result);
|
|
14807
16089
|
spanState.span.log({
|
|
14808
16090
|
...output ? { output } : {},
|
|
14809
|
-
metrics:
|
|
16091
|
+
metrics: cleanMetrics2(
|
|
14810
16092
|
extractEmbedContentMetrics(event.result, spanState.startTime)
|
|
14811
16093
|
)
|
|
14812
16094
|
});
|
|
@@ -14821,7 +16103,7 @@ var GoogleGenAIPlugin = class extends BasePlugin {
|
|
|
14821
16103
|
};
|
|
14822
16104
|
tracingChannel2.subscribe(handlers);
|
|
14823
16105
|
this.unsubscribers.push(() => {
|
|
14824
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
16106
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _358 => _358()]);
|
|
14825
16107
|
tracingChannel2.unsubscribe(handlers);
|
|
14826
16108
|
});
|
|
14827
16109
|
}
|
|
@@ -14837,10 +16119,10 @@ function ensureSpanState(states, event, create) {
|
|
|
14837
16119
|
}
|
|
14838
16120
|
function bindCurrentSpanStoreToStart2(tracingChannel2, states, create) {
|
|
14839
16121
|
const state = _internalGetGlobalState();
|
|
14840
|
-
const contextManager = _optionalChain([state, 'optionalAccess',
|
|
16122
|
+
const contextManager = _optionalChain([state, 'optionalAccess', _359 => _359.contextManager]);
|
|
14841
16123
|
const startChannel = tracingChannel2.start;
|
|
14842
16124
|
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
14843
|
-
if (!_optionalChain([startChannel, 'optionalAccess',
|
|
16125
|
+
if (!_optionalChain([startChannel, 'optionalAccess', _360 => _360.bindStore]) || !currentSpanStore) {
|
|
14844
16126
|
return void 0;
|
|
14845
16127
|
}
|
|
14846
16128
|
startChannel.bindStore(currentSpanStore, (event) => {
|
|
@@ -14852,7 +16134,7 @@ function bindCurrentSpanStoreToStart2(tracingChannel2, states, create) {
|
|
|
14852
16134
|
return contextManager.wrapSpanForStore(span);
|
|
14853
16135
|
});
|
|
14854
16136
|
return () => {
|
|
14855
|
-
_optionalChain([startChannel, 'access',
|
|
16137
|
+
_optionalChain([startChannel, 'access', _361 => _361.unbindStore, 'optionalCall', _362 => _362(currentSpanStore)]);
|
|
14856
16138
|
};
|
|
14857
16139
|
}
|
|
14858
16140
|
function logErrorAndEndSpan(states, event) {
|
|
@@ -14903,7 +16185,7 @@ function patchGoogleGenAIStreamingResult(args) {
|
|
|
14903
16185
|
);
|
|
14904
16186
|
span.log({
|
|
14905
16187
|
...responseMetadata ? { metadata: responseMetadata } : {},
|
|
14906
|
-
metrics:
|
|
16188
|
+
metrics: cleanMetrics2(metricsWithoutEnd),
|
|
14907
16189
|
output: options.result.aggregated
|
|
14908
16190
|
});
|
|
14909
16191
|
span.end(typeof end === "number" ? { endTime: end } : void 0);
|
|
@@ -15087,7 +16369,7 @@ function serializePart(part) {
|
|
|
15087
16369
|
}
|
|
15088
16370
|
function serializeGenerateContentTools(params) {
|
|
15089
16371
|
const config = params.config ? tryToDict(params.config) : null;
|
|
15090
|
-
const tools = _optionalChain([config, 'optionalAccess',
|
|
16372
|
+
const tools = _optionalChain([config, 'optionalAccess', _363 => _363.tools]);
|
|
15091
16373
|
if (!Array.isArray(tools)) {
|
|
15092
16374
|
return null;
|
|
15093
16375
|
}
|
|
@@ -15100,7 +16382,7 @@ function serializeGenerateContentTools(params) {
|
|
|
15100
16382
|
}
|
|
15101
16383
|
}
|
|
15102
16384
|
return serializedTools.length > 0 ? serializedTools : null;
|
|
15103
|
-
} catch (
|
|
16385
|
+
} catch (e39) {
|
|
15104
16386
|
return null;
|
|
15105
16387
|
}
|
|
15106
16388
|
}
|
|
@@ -15146,7 +16428,7 @@ function extractGenerateContentMetrics(response, startTime) {
|
|
|
15146
16428
|
metrics.end = end;
|
|
15147
16429
|
metrics.duration = end - startTime;
|
|
15148
16430
|
}
|
|
15149
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
16431
|
+
if (_optionalChain([response, 'optionalAccess', _364 => _364.usageMetadata])) {
|
|
15150
16432
|
populateUsageMetrics(metrics, response.usageMetadata);
|
|
15151
16433
|
}
|
|
15152
16434
|
return metrics;
|
|
@@ -15159,7 +16441,7 @@ function extractEmbedContentMetrics(response, startTime) {
|
|
|
15159
16441
|
metrics.end = end;
|
|
15160
16442
|
metrics.duration = end - startTime;
|
|
15161
16443
|
}
|
|
15162
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
16444
|
+
if (_optionalChain([response, 'optionalAccess', _365 => _365.usageMetadata])) {
|
|
15163
16445
|
populateUsageMetrics(metrics, response.usageMetadata);
|
|
15164
16446
|
}
|
|
15165
16447
|
const embeddingTokenCount = extractEmbedPromptTokenCount(response);
|
|
@@ -15173,11 +16455,11 @@ function extractEmbedPromptTokenCount(response) {
|
|
|
15173
16455
|
if (!response) {
|
|
15174
16456
|
return void 0;
|
|
15175
16457
|
}
|
|
15176
|
-
const usagePromptTokens = _optionalChain([response, 'access',
|
|
16458
|
+
const usagePromptTokens = _optionalChain([response, 'access', _366 => _366.usageMetadata, 'optionalAccess', _367 => _367.promptTokenCount]);
|
|
15177
16459
|
if (typeof usagePromptTokens === "number" && Number.isFinite(usagePromptTokens)) {
|
|
15178
16460
|
return usagePromptTokens;
|
|
15179
16461
|
}
|
|
15180
|
-
const usageTotalTokens = _optionalChain([response, 'access',
|
|
16462
|
+
const usageTotalTokens = _optionalChain([response, 'access', _368 => _368.usageMetadata, 'optionalAccess', _369 => _369.totalTokenCount]);
|
|
15181
16463
|
if (typeof usageTotalTokens === "number" && Number.isFinite(usageTotalTokens)) {
|
|
15182
16464
|
return usageTotalTokens;
|
|
15183
16465
|
}
|
|
@@ -15188,8 +16470,8 @@ function extractEmbedPromptTokenCount(response) {
|
|
|
15188
16470
|
let total = 0;
|
|
15189
16471
|
let sawAny = false;
|
|
15190
16472
|
for (const embedding of embeddings) {
|
|
15191
|
-
const embeddingStats = tryToDict(_optionalChain([tryToDict, 'call',
|
|
15192
|
-
const tokenCount = _optionalChain([embeddingStats, 'optionalAccess',
|
|
16473
|
+
const embeddingStats = tryToDict(_optionalChain([tryToDict, 'call', _370 => _370(embedding), 'optionalAccess', _371 => _371.statistics]));
|
|
16474
|
+
const tokenCount = _optionalChain([embeddingStats, 'optionalAccess', _372 => _372.tokenCount]);
|
|
15193
16475
|
if (typeof tokenCount === "number" && Number.isFinite(tokenCount)) {
|
|
15194
16476
|
total += tokenCount;
|
|
15195
16477
|
sawAny = true;
|
|
@@ -15205,7 +16487,7 @@ function summarizeEmbedContentOutput(response) {
|
|
|
15205
16487
|
if (embeddings.length === 0) {
|
|
15206
16488
|
return void 0;
|
|
15207
16489
|
}
|
|
15208
|
-
const firstValues = _optionalChain([embeddings, 'access',
|
|
16490
|
+
const firstValues = _optionalChain([embeddings, 'access', _373 => _373[0], 'optionalAccess', _374 => _374.values]);
|
|
15209
16491
|
if (!Array.isArray(firstValues)) {
|
|
15210
16492
|
return void 0;
|
|
15211
16493
|
}
|
|
@@ -15260,7 +16542,7 @@ function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
|
|
|
15260
16542
|
}
|
|
15261
16543
|
if (chunk.candidates && Array.isArray(chunk.candidates)) {
|
|
15262
16544
|
for (const candidate of chunk.candidates) {
|
|
15263
|
-
if (_optionalChain([candidate, 'access',
|
|
16545
|
+
if (_optionalChain([candidate, 'access', _375 => _375.content, 'optionalAccess', _376 => _376.parts])) {
|
|
15264
16546
|
for (const part of candidate.content.parts) {
|
|
15265
16547
|
if (part.text !== void 0) {
|
|
15266
16548
|
if (part.thought) {
|
|
@@ -15291,7 +16573,7 @@ function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
|
|
|
15291
16573
|
parts.push({ text });
|
|
15292
16574
|
}
|
|
15293
16575
|
parts.push(...otherParts);
|
|
15294
|
-
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess',
|
|
16576
|
+
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _377 => _377.candidates])) {
|
|
15295
16577
|
const candidates = [];
|
|
15296
16578
|
for (const candidate of lastResponse.candidates) {
|
|
15297
16579
|
const candidateDict = {
|
|
@@ -15328,7 +16610,7 @@ function aggregateGenerateContentChunks(chunks, startTime, firstTokenTime) {
|
|
|
15328
16610
|
}
|
|
15329
16611
|
return { aggregated, metrics };
|
|
15330
16612
|
}
|
|
15331
|
-
function
|
|
16613
|
+
function cleanMetrics2(metrics) {
|
|
15332
16614
|
const cleaned = {};
|
|
15333
16615
|
for (const [key, value] of Object.entries(metrics)) {
|
|
15334
16616
|
if (value !== null && value !== void 0) {
|
|
@@ -15348,7 +16630,7 @@ function extractResponseMetadata(response) {
|
|
|
15348
16630
|
if (Array.isArray(responseDict.candidates)) {
|
|
15349
16631
|
for (const candidate of responseDict.candidates) {
|
|
15350
16632
|
const candidateDict = tryToDict(candidate);
|
|
15351
|
-
if (_optionalChain([candidateDict, 'optionalAccess',
|
|
16633
|
+
if (_optionalChain([candidateDict, 'optionalAccess', _378 => _378.groundingMetadata]) !== void 0) {
|
|
15352
16634
|
candidateGroundingMetadata.push(candidateDict.groundingMetadata);
|
|
15353
16635
|
}
|
|
15354
16636
|
}
|
|
@@ -15412,6 +16694,8 @@ var REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
|
15412
16694
|
"stop",
|
|
15413
16695
|
"stream",
|
|
15414
16696
|
"temperature",
|
|
16697
|
+
"tool_choice",
|
|
16698
|
+
"tools",
|
|
15415
16699
|
"top_p"
|
|
15416
16700
|
]);
|
|
15417
16701
|
var RESPONSE_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
@@ -15427,9 +16711,9 @@ var HuggingFacePlugin = class extends BasePlugin {
|
|
|
15427
16711
|
name: "huggingface.chat_completion",
|
|
15428
16712
|
type: "llm" /* LLM */,
|
|
15429
16713
|
extractInput: extractChatInputWithMetadata,
|
|
15430
|
-
extractOutput: (result) => _optionalChain([result, 'optionalAccess',
|
|
16714
|
+
extractOutput: (result) => _optionalChain([result, 'optionalAccess', _379 => _379.choices]),
|
|
15431
16715
|
extractMetadata: (result) => extractResponseMetadata2(result),
|
|
15432
|
-
extractMetrics: (result) => parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
16716
|
+
extractMetrics: (result) => parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _380 => _380.usage]))
|
|
15433
16717
|
}),
|
|
15434
16718
|
traceSyncStreamChannel(huggingFaceChannels.chatCompletionStream, {
|
|
15435
16719
|
name: "huggingface.chat_completion_stream",
|
|
@@ -15447,7 +16731,7 @@ var HuggingFacePlugin = class extends BasePlugin {
|
|
|
15447
16731
|
extractInput: extractTextGenerationInputWithMetadata,
|
|
15448
16732
|
extractOutput: (result) => isObject(result) ? { generated_text: result.generated_text } : result,
|
|
15449
16733
|
extractMetadata: extractTextGenerationMetadata,
|
|
15450
|
-
extractMetrics: (result) => extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([result, 'optionalAccess',
|
|
16734
|
+
extractMetrics: (result) => extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([result, 'optionalAccess', _381 => _381.details]), () => ( null)))
|
|
15451
16735
|
}),
|
|
15452
16736
|
traceSyncStreamChannel(huggingFaceChannels.textGenerationStream, {
|
|
15453
16737
|
name: "huggingface.text_generation_stream",
|
|
@@ -15566,7 +16850,7 @@ function extractTextGenerationMetrics(details) {
|
|
|
15566
16850
|
return metrics;
|
|
15567
16851
|
}
|
|
15568
16852
|
function extractTextGenerationMetadata(result) {
|
|
15569
|
-
if (!isObject(_optionalChain([result, 'optionalAccess',
|
|
16853
|
+
if (!isObject(_optionalChain([result, 'optionalAccess', _382 => _382.details]))) {
|
|
15570
16854
|
return void 0;
|
|
15571
16855
|
}
|
|
15572
16856
|
return typeof result.details.finish_reason === "string" ? {
|
|
@@ -15612,7 +16896,7 @@ function patchChatCompletionStream(args) {
|
|
|
15612
16896
|
const lastChunk = chunks.at(-1);
|
|
15613
16897
|
const responseMetadata = extractResponseMetadata2(lastChunk);
|
|
15614
16898
|
const metrics = {
|
|
15615
|
-
...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess',
|
|
16899
|
+
...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess', _383 => _383.usage])),
|
|
15616
16900
|
...firstChunkTime !== void 0 ? { time_to_first_token: firstChunkTime - startTime } : {}
|
|
15617
16901
|
};
|
|
15618
16902
|
span.log({
|
|
@@ -15650,8 +16934,8 @@ function patchTextGenerationStream(args) {
|
|
|
15650
16934
|
output: aggregateTextGenerationStreamChunks(chunks),
|
|
15651
16935
|
...streamMetadata ? { metadata: streamMetadata } : {},
|
|
15652
16936
|
metrics: {
|
|
15653
|
-
...extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([lastChunk, 'optionalAccess',
|
|
15654
|
-
...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess',
|
|
16937
|
+
...extractTextGenerationMetrics(_nullishCoalesce(_optionalChain([lastChunk, 'optionalAccess', _384 => _384.details]), () => ( null))),
|
|
16938
|
+
...parseMetricsFromUsage(_optionalChain([lastChunk, 'optionalAccess', _385 => _385.usage])),
|
|
15655
16939
|
...firstChunkTime !== void 0 ? { time_to_first_token: firstChunkTime - startTime } : {}
|
|
15656
16940
|
}
|
|
15657
16941
|
});
|
|
@@ -15674,22 +16958,29 @@ function aggregateChatCompletionChunks2(chunks) {
|
|
|
15674
16958
|
for (const chunk of chunks) {
|
|
15675
16959
|
for (const choice of _nullishCoalesce(chunk.choices, () => ( []))) {
|
|
15676
16960
|
const index = typeof choice.index === "number" ? choice.index : 0;
|
|
15677
|
-
const existing = _nullishCoalesce(aggregatedChoices.get(index), () => ( {
|
|
16961
|
+
const existing = _nullishCoalesce(aggregatedChoices.get(index), () => ( {
|
|
16962
|
+
content: "",
|
|
16963
|
+
toolCallsByIndex: /* @__PURE__ */ new Map()
|
|
16964
|
+
}));
|
|
15678
16965
|
const delta = isObject(choice.delta) ? choice.delta : void 0;
|
|
15679
16966
|
const message = isObject(choice.message) ? choice.message : void 0;
|
|
15680
|
-
if (typeof _optionalChain([delta, 'optionalAccess',
|
|
16967
|
+
if (typeof _optionalChain([delta, 'optionalAccess', _386 => _386.content]) === "string") {
|
|
15681
16968
|
existing.content += delta.content;
|
|
15682
|
-
} else if (typeof _optionalChain([message, 'optionalAccess',
|
|
16969
|
+
} else if (typeof _optionalChain([message, 'optionalAccess', _387 => _387.content]) === "string") {
|
|
15683
16970
|
existing.content = message.content;
|
|
15684
16971
|
}
|
|
15685
|
-
if (typeof _optionalChain([delta, 'optionalAccess',
|
|
16972
|
+
if (typeof _optionalChain([delta, 'optionalAccess', _388 => _388.role]) === "string") {
|
|
15686
16973
|
existing.role = delta.role;
|
|
15687
|
-
} else if (typeof _optionalChain([message, 'optionalAccess',
|
|
16974
|
+
} else if (typeof _optionalChain([message, 'optionalAccess', _389 => _389.role]) === "string") {
|
|
15688
16975
|
existing.role = message.role;
|
|
15689
16976
|
}
|
|
15690
16977
|
if (choice.finish_reason !== void 0) {
|
|
15691
16978
|
existing.finish_reason = choice.finish_reason;
|
|
15692
16979
|
}
|
|
16980
|
+
const toolCallDeltas = _nullishCoalesce(getChatToolCallDeltas(delta), () => ( getChatToolCallDeltas(message)));
|
|
16981
|
+
if (toolCallDeltas) {
|
|
16982
|
+
mergeChatToolCallDeltas(existing.toolCallsByIndex, toolCallDeltas);
|
|
16983
|
+
}
|
|
15693
16984
|
aggregatedChoices.set(index, existing);
|
|
15694
16985
|
}
|
|
15695
16986
|
}
|
|
@@ -15698,12 +16989,72 @@ function aggregateChatCompletionChunks2(chunks) {
|
|
|
15698
16989
|
index,
|
|
15699
16990
|
message: {
|
|
15700
16991
|
content: choice.content,
|
|
15701
|
-
role: _nullishCoalesce(choice.role, () => ( "assistant"))
|
|
16992
|
+
role: _nullishCoalesce(choice.role, () => ( "assistant")),
|
|
16993
|
+
...choice.toolCallsByIndex.size > 0 ? {
|
|
16994
|
+
tool_calls: [...choice.toolCallsByIndex.entries()].sort(([leftIndex], [rightIndex]) => leftIndex - rightIndex).map(([, toolCall]) => toolCall)
|
|
16995
|
+
} : {}
|
|
15702
16996
|
},
|
|
15703
16997
|
...choice.finish_reason !== void 0 ? { finish_reason: choice.finish_reason } : {}
|
|
15704
16998
|
}))
|
|
15705
16999
|
};
|
|
15706
17000
|
}
|
|
17001
|
+
function getChatToolCallDeltas(value) {
|
|
17002
|
+
if (!Array.isArray(_optionalChain([value, 'optionalAccess', _390 => _390.tool_calls]))) {
|
|
17003
|
+
return void 0;
|
|
17004
|
+
}
|
|
17005
|
+
const toolCalls = value.tool_calls.filter((toolCall) => isObject(toolCall));
|
|
17006
|
+
return toolCalls.length > 0 ? toolCalls : void 0;
|
|
17007
|
+
}
|
|
17008
|
+
function mergeChatToolCallDeltas(toolCallsByIndex, toolCallDeltas) {
|
|
17009
|
+
for (const toolDelta of toolCallDeltas) {
|
|
17010
|
+
const toolIndex = typeof toolDelta.index === "number" && toolDelta.index >= 0 ? toolDelta.index : 0;
|
|
17011
|
+
const existing = toolCallsByIndex.get(toolIndex);
|
|
17012
|
+
if (!existing) {
|
|
17013
|
+
toolCallsByIndex.set(toolIndex, createChatToolCall(toolDelta));
|
|
17014
|
+
continue;
|
|
17015
|
+
}
|
|
17016
|
+
mergeChatToolCall(existing, toolDelta);
|
|
17017
|
+
}
|
|
17018
|
+
}
|
|
17019
|
+
function createChatToolCall(toolDelta) {
|
|
17020
|
+
const toolFunction = isObject(toolDelta.function) ? toolDelta.function : {};
|
|
17021
|
+
const toolCallFunction = {
|
|
17022
|
+
arguments: typeof toolFunction.arguments === "string" ? toolFunction.arguments : ""
|
|
17023
|
+
};
|
|
17024
|
+
if (typeof toolFunction.name === "string") {
|
|
17025
|
+
toolCallFunction.name = toolFunction.name;
|
|
17026
|
+
}
|
|
17027
|
+
const toolCall = {
|
|
17028
|
+
function: toolCallFunction
|
|
17029
|
+
};
|
|
17030
|
+
if (typeof toolDelta.id === "string") {
|
|
17031
|
+
toolCall.id = toolDelta.id;
|
|
17032
|
+
}
|
|
17033
|
+
if (typeof toolDelta.type === "string") {
|
|
17034
|
+
toolCall.type = toolDelta.type;
|
|
17035
|
+
}
|
|
17036
|
+
return toolCall;
|
|
17037
|
+
}
|
|
17038
|
+
function mergeChatToolCall(existing, toolDelta) {
|
|
17039
|
+
const currentFunction = isObject(existing.function) ? existing.function : {};
|
|
17040
|
+
const deltaFunction = isObject(toolDelta.function) ? toolDelta.function : {};
|
|
17041
|
+
const currentArguments = typeof currentFunction.arguments === "string" ? currentFunction.arguments : "";
|
|
17042
|
+
const deltaArguments = typeof deltaFunction.arguments === "string" ? deltaFunction.arguments : "";
|
|
17043
|
+
if (typeof toolDelta.id === "string" && typeof existing.id !== "string") {
|
|
17044
|
+
existing.id = toolDelta.id;
|
|
17045
|
+
}
|
|
17046
|
+
if (typeof toolDelta.type === "string" && typeof existing.type !== "string") {
|
|
17047
|
+
existing.type = toolDelta.type;
|
|
17048
|
+
}
|
|
17049
|
+
const nextFunction = {
|
|
17050
|
+
...currentFunction,
|
|
17051
|
+
arguments: `${currentArguments}${deltaArguments}`
|
|
17052
|
+
};
|
|
17053
|
+
if (typeof deltaFunction.name === "string" && typeof currentFunction.name !== "string") {
|
|
17054
|
+
nextFunction.name = deltaFunction.name;
|
|
17055
|
+
}
|
|
17056
|
+
existing.function = nextFunction;
|
|
17057
|
+
}
|
|
15707
17058
|
function aggregateTextGenerationStreamChunks(chunks) {
|
|
15708
17059
|
if (chunks.length === 0) {
|
|
15709
17060
|
return void 0;
|
|
@@ -15713,7 +17064,7 @@ function aggregateTextGenerationStreamChunks(chunks) {
|
|
|
15713
17064
|
for (const chunk of chunks) {
|
|
15714
17065
|
if (typeof chunk.generated_text === "string") {
|
|
15715
17066
|
generatedText = chunk.generated_text;
|
|
15716
|
-
} else if (typeof _optionalChain([chunk, 'access',
|
|
17067
|
+
} else if (typeof _optionalChain([chunk, 'access', _391 => _391.token, 'optionalAccess', _392 => _392.text]) === "string" && !chunk.token.special) {
|
|
15717
17068
|
generatedText += chunk.token.text;
|
|
15718
17069
|
} else if (Array.isArray(chunk.choices)) {
|
|
15719
17070
|
for (const choice of chunk.choices) {
|
|
@@ -15737,17 +17088,17 @@ function aggregateTextGenerationStreamChunks(chunks) {
|
|
|
15737
17088
|
function extractTextGenerationStreamMetadata(chunks) {
|
|
15738
17089
|
for (let index = chunks.length - 1; index >= 0; index--) {
|
|
15739
17090
|
const chunk = chunks[index];
|
|
15740
|
-
if (isObject(_optionalChain([chunk, 'optionalAccess',
|
|
17091
|
+
if (isObject(_optionalChain([chunk, 'optionalAccess', _393 => _393.details])) && typeof chunk.details.finish_reason === "string") {
|
|
15741
17092
|
return {
|
|
15742
17093
|
finish_reason: chunk.details.finish_reason
|
|
15743
17094
|
};
|
|
15744
17095
|
}
|
|
15745
|
-
if (!Array.isArray(_optionalChain([chunk, 'optionalAccess',
|
|
17096
|
+
if (!Array.isArray(_optionalChain([chunk, 'optionalAccess', _394 => _394.choices]))) {
|
|
15746
17097
|
continue;
|
|
15747
17098
|
}
|
|
15748
17099
|
for (let choiceIndex = chunk.choices.length - 1; choiceIndex >= 0; choiceIndex--) {
|
|
15749
17100
|
const choice = chunk.choices[choiceIndex];
|
|
15750
|
-
if (_optionalChain([choice, 'optionalAccess',
|
|
17101
|
+
if (_optionalChain([choice, 'optionalAccess', _395 => _395.finish_reason]) !== void 0) {
|
|
15751
17102
|
return { finish_reason: choice.finish_reason };
|
|
15752
17103
|
}
|
|
15753
17104
|
}
|
|
@@ -15823,14 +17174,14 @@ var OpenRouterAgentPlugin = class extends BasePlugin {
|
|
|
15823
17174
|
extractMetadata: (result, event) => {
|
|
15824
17175
|
if (!isObject(result)) {
|
|
15825
17176
|
return {
|
|
15826
|
-
step: _optionalChain([event, 'optionalAccess',
|
|
15827
|
-
step_type: _optionalChain([event, 'optionalAccess',
|
|
17177
|
+
step: _optionalChain([event, 'optionalAccess', _396 => _396.step]),
|
|
17178
|
+
step_type: _optionalChain([event, 'optionalAccess', _397 => _397.stepType])
|
|
15828
17179
|
};
|
|
15829
17180
|
}
|
|
15830
17181
|
return {
|
|
15831
17182
|
...extractOpenRouterResponseMetadata(result) || {},
|
|
15832
|
-
..._optionalChain([event, 'optionalAccess',
|
|
15833
|
-
..._optionalChain([event, 'optionalAccess',
|
|
17183
|
+
..._optionalChain([event, 'optionalAccess', _398 => _398.step]) !== void 0 ? { step: event.step } : {},
|
|
17184
|
+
..._optionalChain([event, 'optionalAccess', _399 => _399.stepType]) ? { step_type: event.stepType } : {}
|
|
15834
17185
|
};
|
|
15835
17186
|
},
|
|
15836
17187
|
extractMetrics: (result) => isObject(result) ? parseOpenRouterMetricsFromUsage(result.usage) : {}
|
|
@@ -15985,7 +17336,7 @@ function isZodSchema3(value) {
|
|
|
15985
17336
|
function serializeZodSchema3(schema) {
|
|
15986
17337
|
try {
|
|
15987
17338
|
return zodToJsonSchema(schema);
|
|
15988
|
-
} catch (
|
|
17339
|
+
} catch (e40) {
|
|
15989
17340
|
return {
|
|
15990
17341
|
type: "object",
|
|
15991
17342
|
description: "Zod schema (conversion failed)"
|
|
@@ -16191,7 +17542,7 @@ function publishToolResult(tracingChannel2, event, result) {
|
|
|
16191
17542
|
}
|
|
16192
17543
|
function getToolCallId(context) {
|
|
16193
17544
|
const toolContext = context;
|
|
16194
|
-
return typeof _optionalChain([toolContext, 'optionalAccess',
|
|
17545
|
+
return typeof _optionalChain([toolContext, 'optionalAccess', _400 => _400.toolCall, 'optionalAccess', _401 => _401.id]) === "string" ? toolContext.toolCall.id : void 0;
|
|
16195
17546
|
}
|
|
16196
17547
|
var OPENROUTER_WRAPPED_CALL_MODEL_RESULT = Symbol(
|
|
16197
17548
|
"braintrust.openrouter.wrappedCallModelResult"
|
|
@@ -16281,7 +17632,7 @@ function patchOpenRouterCallModelResult(args) {
|
|
|
16281
17632
|
}
|
|
16282
17633
|
try {
|
|
16283
17634
|
await endSpanWithResult(await originalGetResponse(), fallbackOutput);
|
|
16284
|
-
} catch (
|
|
17635
|
+
} catch (e41) {
|
|
16285
17636
|
await endSpanWithResult(void 0, fallbackOutput);
|
|
16286
17637
|
}
|
|
16287
17638
|
};
|
|
@@ -16586,9 +17937,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
16586
17937
|
type: "llm" /* LLM */,
|
|
16587
17938
|
extractInput: (args) => {
|
|
16588
17939
|
const request = getOpenRouterRequestArg(args);
|
|
16589
|
-
const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess',
|
|
16590
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
16591
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
17940
|
+
const chatGenerationParams = isObject(_optionalChain([request, 'optionalAccess', _402 => _402.chatGenerationParams])) ? request.chatGenerationParams : {};
|
|
17941
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _403 => _403.httpReferer]);
|
|
17942
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _404 => _404.xTitle]);
|
|
16592
17943
|
const { messages, ...metadata } = chatGenerationParams;
|
|
16593
17944
|
return {
|
|
16594
17945
|
input: messages,
|
|
@@ -16599,7 +17950,7 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
16599
17950
|
return isObject(result) ? result.choices : void 0;
|
|
16600
17951
|
},
|
|
16601
17952
|
extractMetrics: (result, startTime) => {
|
|
16602
|
-
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess',
|
|
17953
|
+
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _405 => _405.usage]));
|
|
16603
17954
|
if (startTime) {
|
|
16604
17955
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
16605
17956
|
}
|
|
@@ -16614,9 +17965,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
16614
17965
|
type: "llm" /* LLM */,
|
|
16615
17966
|
extractInput: (args) => {
|
|
16616
17967
|
const request = getOpenRouterRequestArg(args);
|
|
16617
|
-
const requestBody = isObject(_optionalChain([request, 'optionalAccess',
|
|
16618
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
16619
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
17968
|
+
const requestBody = isObject(_optionalChain([request, 'optionalAccess', _406 => _406.requestBody])) ? request.requestBody : {};
|
|
17969
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _407 => _407.httpReferer]);
|
|
17970
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _408 => _408.xTitle]);
|
|
16620
17971
|
const { input, ...metadata } = requestBody;
|
|
16621
17972
|
return {
|
|
16622
17973
|
input,
|
|
@@ -16631,7 +17982,7 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
16631
17982
|
if (!isObject(result)) {
|
|
16632
17983
|
return void 0;
|
|
16633
17984
|
}
|
|
16634
|
-
const embedding = _optionalChain([result, 'access',
|
|
17985
|
+
const embedding = _optionalChain([result, 'access', _409 => _409.data, 'optionalAccess', _410 => _410[0], 'optionalAccess', _411 => _411.embedding]);
|
|
16635
17986
|
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
16636
17987
|
},
|
|
16637
17988
|
extractMetadata: (result) => {
|
|
@@ -16651,9 +18002,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
16651
18002
|
type: "llm" /* LLM */,
|
|
16652
18003
|
extractInput: (args) => {
|
|
16653
18004
|
const request = getOpenRouterRequestArg(args);
|
|
16654
|
-
const requestBody = isObject(_optionalChain([request, 'optionalAccess',
|
|
16655
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
16656
|
-
const xTitle = _nullishCoalesce(_optionalChain([request, 'optionalAccess',
|
|
18005
|
+
const requestBody = isObject(_optionalChain([request, 'optionalAccess', _412 => _412.requestBody])) ? request.requestBody : {};
|
|
18006
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _413 => _413.httpReferer]);
|
|
18007
|
+
const xTitle = _nullishCoalesce(_optionalChain([request, 'optionalAccess', _414 => _414.xTitle]), () => ( _optionalChain([request, 'optionalAccess', _415 => _415.appTitle])));
|
|
16657
18008
|
const { documents, query, ...metadata } = requestBody;
|
|
16658
18009
|
return {
|
|
16659
18010
|
input: {
|
|
@@ -16679,9 +18030,9 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
16679
18030
|
type: "llm" /* LLM */,
|
|
16680
18031
|
extractInput: (args) => {
|
|
16681
18032
|
const request = getOpenRouterRequestArg(args);
|
|
16682
|
-
const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess',
|
|
16683
|
-
const httpReferer = _optionalChain([request, 'optionalAccess',
|
|
16684
|
-
const xTitle = _optionalChain([request, 'optionalAccess',
|
|
18033
|
+
const openResponsesRequest = isObject(_optionalChain([request, 'optionalAccess', _416 => _416.openResponsesRequest])) ? request.openResponsesRequest : {};
|
|
18034
|
+
const httpReferer = _optionalChain([request, 'optionalAccess', _417 => _417.httpReferer]);
|
|
18035
|
+
const xTitle = _optionalChain([request, 'optionalAccess', _418 => _418.xTitle]);
|
|
16685
18036
|
const { input, ...metadata } = openResponsesRequest;
|
|
16686
18037
|
return {
|
|
16687
18038
|
input,
|
|
@@ -16691,7 +18042,7 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
16691
18042
|
extractOutput: (result) => extractOpenRouterResponseOutput2(result),
|
|
16692
18043
|
extractMetadata: (result) => extractOpenRouterResponseMetadata2(result),
|
|
16693
18044
|
extractMetrics: (result, startTime) => {
|
|
16694
|
-
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess',
|
|
18045
|
+
const metrics = parseOpenRouterMetricsFromUsage2(_optionalChain([result, 'optionalAccess', _419 => _419.usage]));
|
|
16695
18046
|
if (startTime) {
|
|
16696
18047
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
16697
18048
|
}
|
|
@@ -16743,14 +18094,14 @@ var OpenRouterPlugin = class extends BasePlugin {
|
|
|
16743
18094
|
extractMetadata: (result, event) => {
|
|
16744
18095
|
if (!isObject(result)) {
|
|
16745
18096
|
return {
|
|
16746
|
-
step: _optionalChain([event, 'optionalAccess',
|
|
16747
|
-
step_type: _optionalChain([event, 'optionalAccess',
|
|
18097
|
+
step: _optionalChain([event, 'optionalAccess', _420 => _420.step]),
|
|
18098
|
+
step_type: _optionalChain([event, 'optionalAccess', _421 => _421.stepType])
|
|
16748
18099
|
};
|
|
16749
18100
|
}
|
|
16750
18101
|
return {
|
|
16751
18102
|
...extractOpenRouterResponseMetadata2(result) || {},
|
|
16752
|
-
..._optionalChain([event, 'optionalAccess',
|
|
16753
|
-
..._optionalChain([event, 'optionalAccess',
|
|
18103
|
+
..._optionalChain([event, 'optionalAccess', _422 => _422.step]) !== void 0 ? { step: event.step } : {},
|
|
18104
|
+
..._optionalChain([event, 'optionalAccess', _423 => _423.stepType]) ? { step_type: event.stepType } : {}
|
|
16754
18105
|
};
|
|
16755
18106
|
},
|
|
16756
18107
|
extractMetrics: (result) => isObject(result) ? parseOpenRouterMetricsFromUsage2(result.usage) : {}
|
|
@@ -16909,7 +18260,7 @@ function isZodSchema4(value) {
|
|
|
16909
18260
|
function serializeZodSchema4(schema) {
|
|
16910
18261
|
try {
|
|
16911
18262
|
return zodToJsonSchema(schema);
|
|
16912
|
-
} catch (
|
|
18263
|
+
} catch (e42) {
|
|
16913
18264
|
return {
|
|
16914
18265
|
type: "object",
|
|
16915
18266
|
description: "Zod schema (conversion failed)"
|
|
@@ -17130,7 +18481,7 @@ function publishToolResult2(tracingChannel2, event, result) {
|
|
|
17130
18481
|
}
|
|
17131
18482
|
function getToolCallId2(context) {
|
|
17132
18483
|
const toolContext = context;
|
|
17133
|
-
return typeof _optionalChain([toolContext, 'optionalAccess',
|
|
18484
|
+
return typeof _optionalChain([toolContext, 'optionalAccess', _424 => _424.toolCall, 'optionalAccess', _425 => _425.id]) === "string" ? toolContext.toolCall.id : void 0;
|
|
17134
18485
|
}
|
|
17135
18486
|
function aggregateOpenRouterChatChunks(chunks) {
|
|
17136
18487
|
let role;
|
|
@@ -17141,12 +18492,12 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
17141
18492
|
for (const chunk of chunks) {
|
|
17142
18493
|
metrics = {
|
|
17143
18494
|
...metrics,
|
|
17144
|
-
...parseOpenRouterMetricsFromUsage2(_optionalChain([chunk, 'optionalAccess',
|
|
18495
|
+
...parseOpenRouterMetricsFromUsage2(_optionalChain([chunk, 'optionalAccess', _426 => _426.usage]))
|
|
17145
18496
|
};
|
|
17146
|
-
const choice = _optionalChain([chunk, 'optionalAccess',
|
|
17147
|
-
const delta = _optionalChain([choice, 'optionalAccess',
|
|
18497
|
+
const choice = _optionalChain([chunk, 'optionalAccess', _427 => _427.choices, 'optionalAccess', _428 => _428[0]]);
|
|
18498
|
+
const delta = _optionalChain([choice, 'optionalAccess', _429 => _429.delta]);
|
|
17148
18499
|
if (!delta) {
|
|
17149
|
-
if (_optionalChain([choice, 'optionalAccess',
|
|
18500
|
+
if (_optionalChain([choice, 'optionalAccess', _430 => _430.finish_reason]) !== void 0) {
|
|
17150
18501
|
finishReason = choice.finish_reason;
|
|
17151
18502
|
}
|
|
17152
18503
|
continue;
|
|
@@ -17157,7 +18508,7 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
17157
18508
|
if (typeof delta.content === "string") {
|
|
17158
18509
|
content += delta.content;
|
|
17159
18510
|
}
|
|
17160
|
-
const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess',
|
|
18511
|
+
const choiceFinishReason = _nullishCoalesce(_nullishCoalesce(_optionalChain([choice, 'optionalAccess', _431 => _431.finishReason]), () => ( _optionalChain([choice, 'optionalAccess', _432 => _432.finish_reason]))), () => ( void 0));
|
|
17161
18512
|
const deltaFinishReason = _nullishCoalesce(_nullishCoalesce(delta.finishReason, () => ( delta.finish_reason)), () => ( void 0));
|
|
17162
18513
|
if (choiceFinishReason !== void 0) {
|
|
17163
18514
|
finishReason = choiceFinishReason;
|
|
@@ -17169,11 +18520,11 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
17169
18520
|
continue;
|
|
17170
18521
|
}
|
|
17171
18522
|
for (const toolDelta of toolCallDeltas) {
|
|
17172
|
-
if (!_optionalChain([toolDelta, 'optionalAccess',
|
|
18523
|
+
if (!_optionalChain([toolDelta, 'optionalAccess', _433 => _433.function])) {
|
|
17173
18524
|
continue;
|
|
17174
18525
|
}
|
|
17175
18526
|
const toolIndex = _nullishCoalesce(toolDelta.index, () => ( 0));
|
|
17176
|
-
const existingToolCall = _optionalChain([toolCalls, 'optionalAccess',
|
|
18527
|
+
const existingToolCall = _optionalChain([toolCalls, 'optionalAccess', _434 => _434[toolIndex]]);
|
|
17177
18528
|
if (!existingToolCall || toolDelta.id && existingToolCall.id !== void 0 && existingToolCall.id !== toolDelta.id) {
|
|
17178
18529
|
const nextToolCalls = [...toolCalls || []];
|
|
17179
18530
|
nextToolCalls[toolIndex] = {
|
|
@@ -17220,7 +18571,7 @@ function aggregateOpenRouterChatChunks(chunks) {
|
|
|
17220
18571
|
function aggregateOpenRouterResponseStreamEvents(chunks) {
|
|
17221
18572
|
let finalResponse;
|
|
17222
18573
|
for (const chunk of chunks) {
|
|
17223
|
-
const response = _optionalChain([chunk, 'optionalAccess',
|
|
18574
|
+
const response = _optionalChain([chunk, 'optionalAccess', _435 => _435.response]);
|
|
17224
18575
|
if (!response) {
|
|
17225
18576
|
continue;
|
|
17226
18577
|
}
|
|
@@ -17328,7 +18679,7 @@ function patchOpenRouterCallModelResult2(args) {
|
|
|
17328
18679
|
}
|
|
17329
18680
|
try {
|
|
17330
18681
|
await endSpanWithResult(await originalGetResponse(), fallbackOutput);
|
|
17331
|
-
} catch (
|
|
18682
|
+
} catch (e43) {
|
|
17332
18683
|
await endSpanWithResult(void 0, fallbackOutput);
|
|
17333
18684
|
}
|
|
17334
18685
|
};
|
|
@@ -17631,10 +18982,10 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
17631
18982
|
type: "llm" /* LLM */,
|
|
17632
18983
|
extractInput: extractMessagesInputWithMetadata,
|
|
17633
18984
|
extractOutput: (result) => {
|
|
17634
|
-
return _optionalChain([result, 'optionalAccess',
|
|
18985
|
+
return _optionalChain([result, 'optionalAccess', _436 => _436.choices]);
|
|
17635
18986
|
},
|
|
17636
18987
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
17637
|
-
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess',
|
|
18988
|
+
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _437 => _437.usage]), startTime)
|
|
17638
18989
|
})
|
|
17639
18990
|
);
|
|
17640
18991
|
this.unsubscribers.push(
|
|
@@ -17654,11 +19005,11 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
17654
19005
|
type: "llm" /* LLM */,
|
|
17655
19006
|
extractInput: extractEmbeddingInputWithMetadata,
|
|
17656
19007
|
extractOutput: (result) => {
|
|
17657
|
-
const embedding = _optionalChain([result, 'optionalAccess',
|
|
19008
|
+
const embedding = _optionalChain([result, 'optionalAccess', _438 => _438.data, 'optionalAccess', _439 => _439[0], 'optionalAccess', _440 => _440.embedding]);
|
|
17658
19009
|
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
17659
19010
|
},
|
|
17660
19011
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
17661
|
-
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
19012
|
+
extractMetrics: (result) => parseMistralMetricsFromUsage(_optionalChain([result, 'optionalAccess', _441 => _441.usage]))
|
|
17662
19013
|
})
|
|
17663
19014
|
);
|
|
17664
19015
|
this.unsubscribers.push(
|
|
@@ -17667,10 +19018,10 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
17667
19018
|
type: "llm" /* LLM */,
|
|
17668
19019
|
extractInput: extractPromptInputWithMetadata,
|
|
17669
19020
|
extractOutput: (result) => {
|
|
17670
|
-
return _optionalChain([result, 'optionalAccess',
|
|
19021
|
+
return _optionalChain([result, 'optionalAccess', _442 => _442.choices]);
|
|
17671
19022
|
},
|
|
17672
19023
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
17673
|
-
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess',
|
|
19024
|
+
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _443 => _443.usage]), startTime)
|
|
17674
19025
|
})
|
|
17675
19026
|
);
|
|
17676
19027
|
this.unsubscribers.push(
|
|
@@ -17690,10 +19041,10 @@ var MistralPlugin = class extends BasePlugin {
|
|
|
17690
19041
|
type: "llm" /* LLM */,
|
|
17691
19042
|
extractInput: extractMessagesInputWithMetadata,
|
|
17692
19043
|
extractOutput: (result) => {
|
|
17693
|
-
return _optionalChain([result, 'optionalAccess',
|
|
19044
|
+
return _optionalChain([result, 'optionalAccess', _444 => _444.choices]);
|
|
17694
19045
|
},
|
|
17695
19046
|
extractMetadata: (result) => extractMistralResponseMetadata(result),
|
|
17696
|
-
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess',
|
|
19047
|
+
extractMetrics: (result, startTime) => extractMistralMetrics(_optionalChain([result, 'optionalAccess', _445 => _445.usage]), startTime)
|
|
17697
19048
|
})
|
|
17698
19049
|
);
|
|
17699
19050
|
this.unsubscribers.push(
|
|
@@ -17748,6 +19099,8 @@ var MISTRAL_REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
|
17748
19099
|
"presence_penalty",
|
|
17749
19100
|
"randomSeed",
|
|
17750
19101
|
"random_seed",
|
|
19102
|
+
"reasoningEffort",
|
|
19103
|
+
"reasoning_effort",
|
|
17751
19104
|
"responseFormat",
|
|
17752
19105
|
"response_format",
|
|
17753
19106
|
"safePrompt",
|
|
@@ -17888,6 +19241,98 @@ function extractDeltaText(content) {
|
|
|
17888
19241
|
}).filter((part) => part.length > 0);
|
|
17889
19242
|
return textParts.length > 0 ? textParts.join("") : void 0;
|
|
17890
19243
|
}
|
|
19244
|
+
function normalizeMistralTextContentPart(part) {
|
|
19245
|
+
if (!isObject(part) || part.type !== "text" || typeof part.text !== "string") {
|
|
19246
|
+
return void 0;
|
|
19247
|
+
}
|
|
19248
|
+
return {
|
|
19249
|
+
type: "text",
|
|
19250
|
+
text: part.text
|
|
19251
|
+
};
|
|
19252
|
+
}
|
|
19253
|
+
function normalizeMistralThinkingContentPart(part) {
|
|
19254
|
+
if (!isObject(part) || part.type !== "thinking") {
|
|
19255
|
+
return void 0;
|
|
19256
|
+
}
|
|
19257
|
+
const thinking = Array.isArray(part.thinking) ? part.thinking.map((thinkingPart) => normalizeMistralTextContentPart(thinkingPart)).filter(
|
|
19258
|
+
(thinkingPart) => thinkingPart !== void 0 && typeof thinkingPart.text === "string"
|
|
19259
|
+
) : [];
|
|
19260
|
+
return {
|
|
19261
|
+
type: "thinking",
|
|
19262
|
+
thinking
|
|
19263
|
+
};
|
|
19264
|
+
}
|
|
19265
|
+
function normalizeMistralContentParts(content) {
|
|
19266
|
+
if (!Array.isArray(content)) {
|
|
19267
|
+
return [];
|
|
19268
|
+
}
|
|
19269
|
+
return content.map((part) => {
|
|
19270
|
+
return normalizeMistralTextContentPart(part) || normalizeMistralThinkingContentPart(part);
|
|
19271
|
+
}).filter((part) => part !== void 0);
|
|
19272
|
+
}
|
|
19273
|
+
function mergeMistralTextSegments(left, right) {
|
|
19274
|
+
const merged = left.map((part) => ({ ...part }));
|
|
19275
|
+
for (const part of right) {
|
|
19276
|
+
const lastPart = merged[merged.length - 1];
|
|
19277
|
+
if (lastPart && lastPart.type === "text" && typeof lastPart.text === "string" && typeof part.text === "string") {
|
|
19278
|
+
lastPart.text += part.text;
|
|
19279
|
+
continue;
|
|
19280
|
+
}
|
|
19281
|
+
merged.push({ ...part });
|
|
19282
|
+
}
|
|
19283
|
+
return merged;
|
|
19284
|
+
}
|
|
19285
|
+
function mergeMistralContentParts(left, right) {
|
|
19286
|
+
const merged = [...(left || []).map((part) => structuredClone(part))];
|
|
19287
|
+
for (const part of right) {
|
|
19288
|
+
const lastPart = merged[merged.length - 1];
|
|
19289
|
+
if (part.type === "text" && _optionalChain([lastPart, 'optionalAccess', _446 => _446.type]) === "text" && typeof lastPart.text === "string" && typeof part.text === "string") {
|
|
19290
|
+
lastPart.text += part.text;
|
|
19291
|
+
continue;
|
|
19292
|
+
}
|
|
19293
|
+
if (part.type === "thinking" && _optionalChain([lastPart, 'optionalAccess', _447 => _447.type]) === "thinking" && Array.isArray(lastPart.thinking) && Array.isArray(part.thinking)) {
|
|
19294
|
+
lastPart.thinking = mergeMistralTextSegments(
|
|
19295
|
+
lastPart.thinking,
|
|
19296
|
+
part.thinking
|
|
19297
|
+
);
|
|
19298
|
+
continue;
|
|
19299
|
+
}
|
|
19300
|
+
merged.push(structuredClone(part));
|
|
19301
|
+
}
|
|
19302
|
+
return merged;
|
|
19303
|
+
}
|
|
19304
|
+
function appendMistralContent(accumulator, content) {
|
|
19305
|
+
if (typeof content === "string") {
|
|
19306
|
+
if (accumulator.contentParts) {
|
|
19307
|
+
accumulator.contentParts = mergeMistralContentParts(
|
|
19308
|
+
accumulator.contentParts,
|
|
19309
|
+
[{ type: "text", text: content }]
|
|
19310
|
+
);
|
|
19311
|
+
return;
|
|
19312
|
+
}
|
|
19313
|
+
accumulator.content = `${accumulator.content || ""}${content}`;
|
|
19314
|
+
return;
|
|
19315
|
+
}
|
|
19316
|
+
const normalizedContentParts = normalizeMistralContentParts(content);
|
|
19317
|
+
if (normalizedContentParts.length === 0) {
|
|
19318
|
+
return;
|
|
19319
|
+
}
|
|
19320
|
+
const hasStructuredContent = normalizedContentParts.some(
|
|
19321
|
+
(part) => part.type !== "text"
|
|
19322
|
+
);
|
|
19323
|
+
if (!accumulator.contentParts && !hasStructuredContent) {
|
|
19324
|
+
const text = extractDeltaText(content);
|
|
19325
|
+
if (text) {
|
|
19326
|
+
accumulator.content = `${accumulator.content || ""}${text}`;
|
|
19327
|
+
}
|
|
19328
|
+
return;
|
|
19329
|
+
}
|
|
19330
|
+
accumulator.contentParts = mergeMistralContentParts(
|
|
19331
|
+
accumulator.contentParts || (accumulator.content ? [{ type: "text", text: accumulator.content }] : []),
|
|
19332
|
+
normalizedContentParts
|
|
19333
|
+
);
|
|
19334
|
+
delete accumulator.content;
|
|
19335
|
+
}
|
|
17891
19336
|
function getDeltaToolCalls(delta) {
|
|
17892
19337
|
const toolCalls = Array.isArray(delta.toolCalls) && delta.toolCalls || Array.isArray(delta.tool_calls) && delta.tool_calls || [];
|
|
17893
19338
|
return toolCalls.filter((toolCall) => isObject(toolCall));
|
|
@@ -17900,13 +19345,13 @@ function createMergedToolCallDelta(delta) {
|
|
|
17900
19345
|
...delta,
|
|
17901
19346
|
function: {
|
|
17902
19347
|
...delta.function,
|
|
17903
|
-
arguments: typeof _optionalChain([delta, 'access',
|
|
19348
|
+
arguments: typeof _optionalChain([delta, 'access', _448 => _448.function, 'optionalAccess', _449 => _449.arguments]) === "string" ? delta.function.arguments : ""
|
|
17904
19349
|
}
|
|
17905
19350
|
};
|
|
17906
19351
|
}
|
|
17907
19352
|
function mergeToolCallDeltaPair(current, delta) {
|
|
17908
|
-
const currentArguments = typeof _optionalChain([current, 'access',
|
|
17909
|
-
const deltaArguments = typeof _optionalChain([delta, 'access',
|
|
19353
|
+
const currentArguments = typeof _optionalChain([current, 'access', _450 => _450.function, 'optionalAccess', _451 => _451.arguments]) === "string" ? current.function.arguments : "";
|
|
19354
|
+
const deltaArguments = typeof _optionalChain([delta, 'access', _452 => _452.function, 'optionalAccess', _453 => _453.arguments]) === "string" ? delta.function.arguments : "";
|
|
17910
19355
|
return {
|
|
17911
19356
|
...current,
|
|
17912
19357
|
...delta,
|
|
@@ -18009,7 +19454,7 @@ function aggregateMistralStreamChunks(chunks) {
|
|
|
18009
19454
|
let metrics = {};
|
|
18010
19455
|
let metadata;
|
|
18011
19456
|
for (const event of chunks) {
|
|
18012
|
-
const chunk = isMistralChatCompletionChunk(_optionalChain([event, 'optionalAccess',
|
|
19457
|
+
const chunk = isMistralChatCompletionChunk(_optionalChain([event, 'optionalAccess', _454 => _454.data])) ? event.data : void 0;
|
|
18013
19458
|
if (!chunk) {
|
|
18014
19459
|
continue;
|
|
18015
19460
|
}
|
|
@@ -18056,10 +19501,7 @@ function aggregateMistralStreamChunks(chunks) {
|
|
|
18056
19501
|
if (!accumulator.role && typeof delta.role === "string") {
|
|
18057
19502
|
accumulator.role = delta.role;
|
|
18058
19503
|
}
|
|
18059
|
-
|
|
18060
|
-
if (deltaText) {
|
|
18061
|
-
accumulator.content = `${accumulator.content || ""}${deltaText}`;
|
|
18062
|
-
}
|
|
19504
|
+
appendMistralContent(accumulator, delta.content);
|
|
18063
19505
|
accumulator.toolCalls = mergeToolCallDeltas(
|
|
18064
19506
|
accumulator.toolCalls,
|
|
18065
19507
|
getDeltaToolCalls(delta)
|
|
@@ -18077,7 +19519,7 @@ function aggregateMistralStreamChunks(chunks) {
|
|
|
18077
19519
|
index: choice.index,
|
|
18078
19520
|
message: {
|
|
18079
19521
|
...choice.role ? { role: choice.role } : {},
|
|
18080
|
-
content: _nullishCoalesce(choice.content, () => ( null)),
|
|
19522
|
+
content: _nullishCoalesce(_nullishCoalesce(choice.contentParts, () => ( choice.content)), () => ( null)),
|
|
18081
19523
|
...choice.toolCalls ? { toolCalls: choice.toolCalls } : {}
|
|
18082
19524
|
},
|
|
18083
19525
|
...choice.finishReason !== void 0 ? { finishReason: choice.finishReason } : {}
|
|
@@ -18106,9 +19548,9 @@ var googleADKChannels = defineChannels("@google/adk", {
|
|
|
18106
19548
|
});
|
|
18107
19549
|
|
|
18108
19550
|
// src/instrumentation/plugins/google-adk-plugin.ts
|
|
18109
|
-
var GoogleADKPlugin = (
|
|
18110
|
-
|
|
18111
|
-
|
|
19551
|
+
var GoogleADKPlugin = (_class19 = class extends BasePlugin {constructor(...args4) { super(...args4); _class19.prototype.__init65.call(this);_class19.prototype.__init66.call(this); }
|
|
19552
|
+
__init65() {this.activeRunnerSpans = /* @__PURE__ */ new Map()}
|
|
19553
|
+
__init66() {this.activeAgentSpans = /* @__PURE__ */ new Map()}
|
|
18112
19554
|
onEnable() {
|
|
18113
19555
|
this.subscribeToRunnerRunAsync();
|
|
18114
19556
|
this.subscribeToAgentRunAsync();
|
|
@@ -18141,7 +19583,7 @@ var GoogleADKPlugin = (_class18 = class extends BasePlugin {constructor(...args3
|
|
|
18141
19583
|
input: extractRunnerInput(params),
|
|
18142
19584
|
metadata
|
|
18143
19585
|
});
|
|
18144
|
-
} catch (
|
|
19586
|
+
} catch (e44) {
|
|
18145
19587
|
}
|
|
18146
19588
|
if (contextKey) {
|
|
18147
19589
|
this.activeRunnerSpans.set(contextKey, span);
|
|
@@ -18203,7 +19645,7 @@ var GoogleADKPlugin = (_class18 = class extends BasePlugin {constructor(...args3
|
|
|
18203
19645
|
};
|
|
18204
19646
|
tracingChannel2.subscribe(handlers);
|
|
18205
19647
|
this.unsubscribers.push(() => {
|
|
18206
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
19648
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _455 => _455()]);
|
|
18207
19649
|
tracingChannel2.unsubscribe(handlers);
|
|
18208
19650
|
});
|
|
18209
19651
|
}
|
|
@@ -18243,7 +19685,7 @@ var GoogleADKPlugin = (_class18 = class extends BasePlugin {constructor(...args3
|
|
|
18243
19685
|
metadata.model = modelName;
|
|
18244
19686
|
}
|
|
18245
19687
|
span.log({ metadata });
|
|
18246
|
-
} catch (
|
|
19688
|
+
} catch (e45) {
|
|
18247
19689
|
}
|
|
18248
19690
|
if (contextKey && agentName) {
|
|
18249
19691
|
this.activeAgentSpans.set(agentContextKey(contextKey, agentName), span);
|
|
@@ -18305,7 +19747,7 @@ var GoogleADKPlugin = (_class18 = class extends BasePlugin {constructor(...args3
|
|
|
18305
19747
|
};
|
|
18306
19748
|
tracingChannel2.subscribe(handlers);
|
|
18307
19749
|
this.unsubscribers.push(() => {
|
|
18308
|
-
_optionalChain([unbindCurrentSpanStore, 'optionalCall',
|
|
19750
|
+
_optionalChain([unbindCurrentSpanStore, 'optionalCall', _456 => _456()]);
|
|
18309
19751
|
tracingChannel2.unsubscribe(handlers);
|
|
18310
19752
|
});
|
|
18311
19753
|
}
|
|
@@ -18316,7 +19758,7 @@ var GoogleADKPlugin = (_class18 = class extends BasePlugin {constructor(...args3
|
|
|
18316
19758
|
start: (event) => {
|
|
18317
19759
|
const req = _nullishCoalesce(event.arguments[0], () => ( {}));
|
|
18318
19760
|
const tool = event.self;
|
|
18319
|
-
const toolName =
|
|
19761
|
+
const toolName = extractToolName2(req, tool);
|
|
18320
19762
|
const parentSpan = findToolParentSpan(
|
|
18321
19763
|
req,
|
|
18322
19764
|
this.activeAgentSpans,
|
|
@@ -18355,7 +19797,7 @@ var GoogleADKPlugin = (_class18 = class extends BasePlugin {constructor(...args3
|
|
|
18355
19797
|
metrics.duration = end - state.startTime;
|
|
18356
19798
|
state.span.log({
|
|
18357
19799
|
output: event.result,
|
|
18358
|
-
metrics:
|
|
19800
|
+
metrics: cleanMetrics3(metrics)
|
|
18359
19801
|
});
|
|
18360
19802
|
} finally {
|
|
18361
19803
|
state.span.end();
|
|
@@ -18377,7 +19819,7 @@ var GoogleADKPlugin = (_class18 = class extends BasePlugin {constructor(...args3
|
|
|
18377
19819
|
tracingChannel2.unsubscribe(handlers);
|
|
18378
19820
|
});
|
|
18379
19821
|
}
|
|
18380
|
-
},
|
|
19822
|
+
}, _class19);
|
|
18381
19823
|
function ensureState(states, event, create) {
|
|
18382
19824
|
const existing = states.get(event);
|
|
18383
19825
|
if (existing) {
|
|
@@ -18414,7 +19856,7 @@ function bindAsyncIterableToCurrentSpan(stream, span) {
|
|
|
18414
19856
|
value: true
|
|
18415
19857
|
});
|
|
18416
19858
|
return stream;
|
|
18417
|
-
} catch (
|
|
19859
|
+
} catch (e46) {
|
|
18418
19860
|
return stream;
|
|
18419
19861
|
}
|
|
18420
19862
|
}
|
|
@@ -18445,17 +19887,17 @@ function bindAsyncIterableToCurrentSpan(stream, span) {
|
|
|
18445
19887
|
}
|
|
18446
19888
|
);
|
|
18447
19889
|
stream[Symbol.asyncIterator] = patchedIteratorFn;
|
|
18448
|
-
} catch (
|
|
19890
|
+
} catch (e47) {
|
|
18449
19891
|
return stream;
|
|
18450
19892
|
}
|
|
18451
19893
|
return stream;
|
|
18452
19894
|
}
|
|
18453
19895
|
function bindCurrentSpanStoreToStart3(tracingChannel2, states, create) {
|
|
18454
19896
|
const state = _internalGetGlobalState();
|
|
18455
|
-
const contextManager = _optionalChain([state, 'optionalAccess',
|
|
19897
|
+
const contextManager = _optionalChain([state, 'optionalAccess', _457 => _457.contextManager]);
|
|
18456
19898
|
const startChannel = tracingChannel2.start;
|
|
18457
19899
|
const currentSpanStore = contextManager ? contextManager[BRAINTRUST_CURRENT_SPAN_STORE] : void 0;
|
|
18458
|
-
if (!_optionalChain([startChannel, 'optionalAccess',
|
|
19900
|
+
if (!_optionalChain([startChannel, 'optionalAccess', _458 => _458.bindStore]) || !currentSpanStore) {
|
|
18459
19901
|
return void 0;
|
|
18460
19902
|
}
|
|
18461
19903
|
startChannel.bindStore(currentSpanStore, (event) => {
|
|
@@ -18467,7 +19909,7 @@ function bindCurrentSpanStoreToStart3(tracingChannel2, states, create) {
|
|
|
18467
19909
|
return contextManager.wrapSpanForStore(span);
|
|
18468
19910
|
});
|
|
18469
19911
|
return () => {
|
|
18470
|
-
_optionalChain([startChannel, 'access',
|
|
19912
|
+
_optionalChain([startChannel, 'access', _459 => _459.unbindStore, 'optionalCall', _460 => _460(currentSpanStore)]);
|
|
18471
19913
|
};
|
|
18472
19914
|
}
|
|
18473
19915
|
function extractRunnerContextKey(paramsOrContext) {
|
|
@@ -18480,9 +19922,9 @@ function extractRunnerContextKey(paramsOrContext) {
|
|
|
18480
19922
|
return extractInvocationContextKey(invocationContext);
|
|
18481
19923
|
}
|
|
18482
19924
|
function extractInvocationContextKey(parentContext) {
|
|
18483
|
-
const session = _optionalChain([parentContext, 'optionalAccess',
|
|
18484
|
-
const userId = _optionalChain([session, 'optionalAccess',
|
|
18485
|
-
const sessionId = _optionalChain([session, 'optionalAccess',
|
|
19925
|
+
const session = _optionalChain([parentContext, 'optionalAccess', _461 => _461.session]);
|
|
19926
|
+
const userId = _optionalChain([session, 'optionalAccess', _462 => _462.userId]);
|
|
19927
|
+
const sessionId = _optionalChain([session, 'optionalAccess', _463 => _463.id]);
|
|
18486
19928
|
if (typeof userId !== "string" || typeof sessionId !== "string") {
|
|
18487
19929
|
return void 0;
|
|
18488
19930
|
}
|
|
@@ -18539,10 +19981,10 @@ function extractRunnerMetadata(paramsOrContext) {
|
|
|
18539
19981
|
metadata["google_adk.session_id"] = directSessionId;
|
|
18540
19982
|
}
|
|
18541
19983
|
const session = "session" in paramsOrContext ? paramsOrContext.session : void 0;
|
|
18542
|
-
if (metadata["google_adk.user_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess',
|
|
19984
|
+
if (metadata["google_adk.user_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess', _464 => _464.userId]) === "string") {
|
|
18543
19985
|
metadata["google_adk.user_id"] = session.userId;
|
|
18544
19986
|
}
|
|
18545
|
-
if (metadata["google_adk.session_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess',
|
|
19987
|
+
if (metadata["google_adk.session_id"] === void 0 && typeof _optionalChain([session, 'optionalAccess', _465 => _465.id]) === "string") {
|
|
18546
19988
|
metadata["google_adk.session_id"] = session.id;
|
|
18547
19989
|
}
|
|
18548
19990
|
return metadata;
|
|
@@ -18552,14 +19994,14 @@ function extractAgentName(parentContext) {
|
|
|
18552
19994
|
return void 0;
|
|
18553
19995
|
}
|
|
18554
19996
|
const agent = parentContext.agent;
|
|
18555
|
-
return _optionalChain([agent, 'optionalAccess',
|
|
19997
|
+
return _optionalChain([agent, 'optionalAccess', _466 => _466.name]);
|
|
18556
19998
|
}
|
|
18557
19999
|
function extractModelName(parentContext) {
|
|
18558
20000
|
if (!parentContext) {
|
|
18559
20001
|
return void 0;
|
|
18560
20002
|
}
|
|
18561
20003
|
const agent = parentContext.agent;
|
|
18562
|
-
if (!_optionalChain([agent, 'optionalAccess',
|
|
20004
|
+
if (!_optionalChain([agent, 'optionalAccess', _467 => _467.model])) {
|
|
18563
20005
|
return void 0;
|
|
18564
20006
|
}
|
|
18565
20007
|
if (typeof agent.model === "string") {
|
|
@@ -18572,30 +20014,30 @@ function extractModelName(parentContext) {
|
|
|
18572
20014
|
}
|
|
18573
20015
|
function extractToolCallId(req) {
|
|
18574
20016
|
const toolContext = req.toolContext;
|
|
18575
|
-
return _optionalChain([toolContext, 'optionalAccess',
|
|
20017
|
+
return _optionalChain([toolContext, 'optionalAccess', _468 => _468.functionCallId]);
|
|
18576
20018
|
}
|
|
18577
|
-
function
|
|
18578
|
-
if (typeof _optionalChain([tool, 'optionalAccess',
|
|
20019
|
+
function extractToolName2(req, tool) {
|
|
20020
|
+
if (typeof _optionalChain([tool, 'optionalAccess', _469 => _469.name]) === "string" && tool.name.length > 0) {
|
|
18579
20021
|
return tool.name;
|
|
18580
20022
|
}
|
|
18581
20023
|
const toolContext = req.toolContext;
|
|
18582
|
-
const invocationContext = _optionalChain([toolContext, 'optionalAccess',
|
|
18583
|
-
const invocationTool = _optionalChain([invocationContext, 'optionalAccess',
|
|
18584
|
-
const toolName = _optionalChain([invocationTool, 'optionalAccess',
|
|
20024
|
+
const invocationContext = _optionalChain([toolContext, 'optionalAccess', _470 => _470.invocationContext]);
|
|
20025
|
+
const invocationTool = _optionalChain([invocationContext, 'optionalAccess', _471 => _471.tool]);
|
|
20026
|
+
const toolName = _optionalChain([invocationTool, 'optionalAccess', _472 => _472.name]);
|
|
18585
20027
|
return typeof toolName === "string" && toolName.length > 0 ? toolName : void 0;
|
|
18586
20028
|
}
|
|
18587
20029
|
function extractToolAgentName(req) {
|
|
18588
20030
|
const toolContext = req.toolContext;
|
|
18589
|
-
const directName = _optionalChain([toolContext, 'optionalAccess',
|
|
20031
|
+
const directName = _optionalChain([toolContext, 'optionalAccess', _473 => _473.agentName]);
|
|
18590
20032
|
if (typeof directName === "string" && directName.length > 0) {
|
|
18591
20033
|
return directName;
|
|
18592
20034
|
}
|
|
18593
|
-
const invocationContext = _optionalChain([toolContext, 'optionalAccess',
|
|
20035
|
+
const invocationContext = _optionalChain([toolContext, 'optionalAccess', _474 => _474.invocationContext]);
|
|
18594
20036
|
return extractAgentName(invocationContext);
|
|
18595
20037
|
}
|
|
18596
20038
|
function findToolParentSpan(req, activeAgentSpans, activeRunnerSpans) {
|
|
18597
20039
|
const toolContext = req.toolContext;
|
|
18598
|
-
const invocationContext = _optionalChain([toolContext, 'optionalAccess',
|
|
20040
|
+
const invocationContext = _optionalChain([toolContext, 'optionalAccess', _475 => _475.invocationContext]);
|
|
18599
20041
|
const contextKey = extractInvocationContextKey(invocationContext);
|
|
18600
20042
|
const agentName = extractToolAgentName(req);
|
|
18601
20043
|
if (contextKey && agentName) {
|
|
@@ -18622,7 +20064,7 @@ function finalizeRunnerSpan(state, activeRunnerSpans) {
|
|
|
18622
20064
|
}
|
|
18623
20065
|
state.span.log({
|
|
18624
20066
|
output: lastEvent ? extractEventOutput(lastEvent) : void 0,
|
|
18625
|
-
metrics:
|
|
20067
|
+
metrics: cleanMetrics3(metrics)
|
|
18626
20068
|
});
|
|
18627
20069
|
} finally {
|
|
18628
20070
|
cleanupActiveRunnerSpan(state, activeRunnerSpans);
|
|
@@ -18639,7 +20081,7 @@ function finalizeAgentSpan(state, activeAgentSpans) {
|
|
|
18639
20081
|
metrics.duration = end - state.startTime;
|
|
18640
20082
|
state.span.log({
|
|
18641
20083
|
output: lastEvent ? extractEventOutput(lastEvent) : void 0,
|
|
18642
|
-
metrics:
|
|
20084
|
+
metrics: cleanMetrics3(metrics)
|
|
18643
20085
|
});
|
|
18644
20086
|
} finally {
|
|
18645
20087
|
cleanupActiveAgentSpan(state, activeAgentSpans);
|
|
@@ -18725,7 +20167,7 @@ function populateUsageMetrics2(metrics, usage) {
|
|
|
18725
20167
|
metrics.completion_reasoning_tokens = usage.thoughtsTokenCount;
|
|
18726
20168
|
}
|
|
18727
20169
|
}
|
|
18728
|
-
function
|
|
20170
|
+
function cleanMetrics3(metrics) {
|
|
18729
20171
|
const cleaned = {};
|
|
18730
20172
|
for (const [key, value] of Object.entries(metrics)) {
|
|
18731
20173
|
if (value !== null && value !== void 0) {
|
|
@@ -18853,6 +20295,11 @@ var CHAT_REQUEST_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
|
18853
20295
|
"strictTools",
|
|
18854
20296
|
"strict_tools",
|
|
18855
20297
|
"temperature",
|
|
20298
|
+
"thinking",
|
|
20299
|
+
"thinkingTokenBudget",
|
|
20300
|
+
"thinkingType",
|
|
20301
|
+
"thinking_token_budget",
|
|
20302
|
+
"thinking_type",
|
|
18856
20303
|
"toolChoice",
|
|
18857
20304
|
"tool_choice"
|
|
18858
20305
|
]);
|
|
@@ -18972,7 +20419,7 @@ function extractCohereResponseMetadata(result) {
|
|
|
18972
20419
|
RESPONSE_METADATA_ALLOWLIST2
|
|
18973
20420
|
);
|
|
18974
20421
|
const meta = isObject(result.meta) ? result.meta : void 0;
|
|
18975
|
-
const apiVersion = isObject(_optionalChain([meta, 'optionalAccess',
|
|
20422
|
+
const apiVersion = isObject(_optionalChain([meta, 'optionalAccess', _476 => _476.apiVersion])) && typeof meta.apiVersion.version === "string" && meta.apiVersion.version || isObject(_optionalChain([meta, 'optionalAccess', _477 => _477.api_version])) && typeof meta.api_version.version === "string" && meta.api_version.version;
|
|
18976
20423
|
const metadata = {
|
|
18977
20424
|
...responseMetadata,
|
|
18978
20425
|
...apiVersion ? { api_version: apiVersion } : {}
|
|
@@ -19073,6 +20520,11 @@ function mergeUsageMetrics(metrics, usage) {
|
|
|
19073
20520
|
"tokens",
|
|
19074
20521
|
_nullishCoalesce(tokenContainer.totalTokens, () => ( tokenContainer.total_tokens))
|
|
19075
20522
|
);
|
|
20523
|
+
setMetricIfNumber(
|
|
20524
|
+
metrics,
|
|
20525
|
+
"reasoning_tokens",
|
|
20526
|
+
_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(tokenContainer.reasoningTokens, () => ( tokenContainer.reasoning_tokens)), () => ( tokenContainer.thinkingTokens)), () => ( tokenContainer.thinking_tokens))
|
|
20527
|
+
);
|
|
19076
20528
|
}
|
|
19077
20529
|
const billedUnits = (isObject(source.billedUnits) ? source.billedUnits : void 0) || (isObject(source.billed_units) ? source.billed_units : void 0);
|
|
19078
20530
|
if (billedUnits) {
|
|
@@ -19128,13 +20580,13 @@ function getToolCallIndex2(toolCall, fallbackIndex) {
|
|
|
19128
20580
|
return typeof toolCall.index === "number" && Number.isInteger(toolCall.index) ? toolCall.index : fallbackIndex;
|
|
19129
20581
|
}
|
|
19130
20582
|
function appendToolCallDelta(existing, incoming) {
|
|
19131
|
-
const currentArguments = isObject(_optionalChain([existing, 'optionalAccess',
|
|
20583
|
+
const currentArguments = isObject(_optionalChain([existing, 'optionalAccess', _478 => _478.function])) && typeof existing.function.arguments === "string" ? existing.function.arguments : "";
|
|
19132
20584
|
const incomingArguments = isObject(incoming.function) && typeof incoming.function.arguments === "string" ? incoming.function.arguments : "";
|
|
19133
20585
|
return {
|
|
19134
20586
|
...existing,
|
|
19135
20587
|
...incoming,
|
|
19136
20588
|
function: {
|
|
19137
|
-
...isObject(_optionalChain([existing, 'optionalAccess',
|
|
20589
|
+
...isObject(_optionalChain([existing, 'optionalAccess', _479 => _479.function])) ? existing.function : {},
|
|
19138
20590
|
...isObject(incoming.function) ? incoming.function : {},
|
|
19139
20591
|
...incomingArguments ? { arguments: `${currentArguments}${incomingArguments}` } : {}
|
|
19140
20592
|
}
|
|
@@ -19153,13 +20605,87 @@ function extractV8DeltaText(chunk) {
|
|
|
19153
20605
|
}
|
|
19154
20606
|
return void 0;
|
|
19155
20607
|
}
|
|
20608
|
+
function getV8ContentIndex(chunk) {
|
|
20609
|
+
return typeof chunk.index === "number" ? chunk.index : 0;
|
|
20610
|
+
}
|
|
20611
|
+
function toContentBlockType(value) {
|
|
20612
|
+
return value === "text" || value === "thinking" ? value : void 0;
|
|
20613
|
+
}
|
|
20614
|
+
function getOrCreateContentBlock(contentBlocksByIndex, contentBlockOrder, index) {
|
|
20615
|
+
if (!contentBlockOrder.includes(index)) {
|
|
20616
|
+
contentBlockOrder.push(index);
|
|
20617
|
+
}
|
|
20618
|
+
if (!(index in contentBlocksByIndex)) {
|
|
20619
|
+
contentBlocksByIndex[index] = {
|
|
20620
|
+
text: "",
|
|
20621
|
+
thinking: ""
|
|
20622
|
+
};
|
|
20623
|
+
}
|
|
20624
|
+
return contentBlocksByIndex[index];
|
|
20625
|
+
}
|
|
20626
|
+
function appendV8ContentBlock(contentBlocksByIndex, contentBlockOrder, index, content) {
|
|
20627
|
+
if (typeof content === "string") {
|
|
20628
|
+
const block2 = getOrCreateContentBlock(
|
|
20629
|
+
contentBlocksByIndex,
|
|
20630
|
+
contentBlockOrder,
|
|
20631
|
+
index
|
|
20632
|
+
);
|
|
20633
|
+
block2.type ??= "text";
|
|
20634
|
+
block2.text += content;
|
|
20635
|
+
return;
|
|
20636
|
+
}
|
|
20637
|
+
if (!isObject(content)) {
|
|
20638
|
+
return;
|
|
20639
|
+
}
|
|
20640
|
+
const block = getOrCreateContentBlock(
|
|
20641
|
+
contentBlocksByIndex,
|
|
20642
|
+
contentBlockOrder,
|
|
20643
|
+
index
|
|
20644
|
+
);
|
|
20645
|
+
const contentType = toContentBlockType(content.type);
|
|
20646
|
+
if (contentType) {
|
|
20647
|
+
block.type = contentType;
|
|
20648
|
+
}
|
|
20649
|
+
if (typeof content.text === "string") {
|
|
20650
|
+
block.type ??= "text";
|
|
20651
|
+
block.text += content.text;
|
|
20652
|
+
}
|
|
20653
|
+
if (typeof content.thinking === "string") {
|
|
20654
|
+
block.type ??= "thinking";
|
|
20655
|
+
block.thinking += content.thinking;
|
|
20656
|
+
}
|
|
20657
|
+
}
|
|
20658
|
+
function serializeAggregatedContentBlocks(contentBlocksByIndex, contentBlockOrder) {
|
|
20659
|
+
return contentBlockOrder.sort((left, right) => left - right).flatMap((index) => {
|
|
20660
|
+
const block = contentBlocksByIndex[index];
|
|
20661
|
+
if (!block) {
|
|
20662
|
+
return [];
|
|
20663
|
+
}
|
|
20664
|
+
if (block.type === "thinking" && block.thinking.length > 0) {
|
|
20665
|
+
return [{ type: "thinking", thinking: block.thinking }];
|
|
20666
|
+
}
|
|
20667
|
+
if (block.text.length > 0) {
|
|
20668
|
+
return [{ type: "text", text: block.text }];
|
|
20669
|
+
}
|
|
20670
|
+
if (block.thinking.length > 0) {
|
|
20671
|
+
return [{ type: "thinking", thinking: block.thinking }];
|
|
20672
|
+
}
|
|
20673
|
+
return [];
|
|
20674
|
+
});
|
|
20675
|
+
}
|
|
20676
|
+
function hasThinkingContent(contentBlocks) {
|
|
20677
|
+
return contentBlocks.some((block) => block.type === "thinking");
|
|
20678
|
+
}
|
|
19156
20679
|
function aggregateCohereChatStreamChunks(chunks) {
|
|
19157
20680
|
const textDeltas = [];
|
|
20681
|
+
const contentBlocksByIndex = {};
|
|
20682
|
+
const contentBlockOrder = [];
|
|
19158
20683
|
const toolCallsByIndex = {};
|
|
19159
20684
|
const toolCallOrder = [];
|
|
19160
20685
|
let terminalResponse;
|
|
19161
20686
|
let role;
|
|
19162
20687
|
let finishReason;
|
|
20688
|
+
let toolPlan = "";
|
|
19163
20689
|
let metadata = {};
|
|
19164
20690
|
let metrics = {};
|
|
19165
20691
|
for (const chunk of chunks) {
|
|
@@ -19217,12 +20743,36 @@ function aggregateCohereChatStreamChunks(chunks) {
|
|
|
19217
20743
|
continue;
|
|
19218
20744
|
}
|
|
19219
20745
|
if (eventType === "content-delta") {
|
|
20746
|
+
appendV8ContentBlock(
|
|
20747
|
+
contentBlocksByIndex,
|
|
20748
|
+
contentBlockOrder,
|
|
20749
|
+
getV8ContentIndex(chunk),
|
|
20750
|
+
isObject(chunk.delta) && isObject(chunk.delta.message) ? chunk.delta.message.content : void 0
|
|
20751
|
+
);
|
|
19220
20752
|
const text = extractV8DeltaText(chunk);
|
|
19221
20753
|
if (text) {
|
|
19222
20754
|
textDeltas.push(text);
|
|
19223
20755
|
}
|
|
19224
20756
|
continue;
|
|
19225
20757
|
}
|
|
20758
|
+
if (eventType === "content-start") {
|
|
20759
|
+
appendV8ContentBlock(
|
|
20760
|
+
contentBlocksByIndex,
|
|
20761
|
+
contentBlockOrder,
|
|
20762
|
+
getV8ContentIndex(chunk),
|
|
20763
|
+
isObject(chunk.delta) && isObject(chunk.delta.message) ? chunk.delta.message.content : void 0
|
|
20764
|
+
);
|
|
20765
|
+
continue;
|
|
20766
|
+
}
|
|
20767
|
+
if (eventType === "tool-plan-delta") {
|
|
20768
|
+
if (isObject(chunk.delta) && isObject(chunk.delta.message)) {
|
|
20769
|
+
const deltaToolPlan = typeof chunk.delta.message.toolPlan === "string" ? chunk.delta.message.toolPlan : typeof chunk.delta.message.tool_plan === "string" ? chunk.delta.message.tool_plan : void 0;
|
|
20770
|
+
if (deltaToolPlan) {
|
|
20771
|
+
toolPlan += deltaToolPlan;
|
|
20772
|
+
}
|
|
20773
|
+
}
|
|
20774
|
+
continue;
|
|
20775
|
+
}
|
|
19226
20776
|
if (eventType === "tool-call-start") {
|
|
19227
20777
|
const toolCalls = isObject(chunk.delta) && isObject(chunk.delta.message) ? toToolCallArray(
|
|
19228
20778
|
Array.isArray(chunk.delta.message.toolCalls) ? chunk.delta.message.toolCalls : _nullishCoalesce(chunk.delta.message.toolCalls, () => ( chunk.delta.message.tool_calls))
|
|
@@ -19276,13 +20826,26 @@ function aggregateCohereChatStreamChunks(chunks) {
|
|
|
19276
20826
|
}
|
|
19277
20827
|
}
|
|
19278
20828
|
const mergedToolCalls = toolCallOrder.sort((left, right) => left - right).map((index) => toolCallsByIndex[index]).filter((toolCall) => isObject(toolCall));
|
|
20829
|
+
const aggregatedContentBlocks = serializeAggregatedContentBlocks(
|
|
20830
|
+
contentBlocksByIndex,
|
|
20831
|
+
contentBlockOrder
|
|
20832
|
+
);
|
|
19279
20833
|
let output = extractCohereChatOutput(terminalResponse);
|
|
19280
20834
|
if (output === void 0) {
|
|
19281
20835
|
const mergedText = textDeltas.join("");
|
|
19282
|
-
|
|
20836
|
+
const shouldUseStructuredContent = hasThinkingContent(aggregatedContentBlocks) || toolPlan.length > 0;
|
|
20837
|
+
if (shouldUseStructuredContent) {
|
|
20838
|
+
output = {
|
|
20839
|
+
...role ? { role } : {},
|
|
20840
|
+
...aggregatedContentBlocks.length > 0 ? { content: aggregatedContentBlocks } : {},
|
|
20841
|
+
...toolPlan.length > 0 ? { toolPlan } : {},
|
|
20842
|
+
...mergedToolCalls.length > 0 ? { toolCalls: mergedToolCalls } : {}
|
|
20843
|
+
};
|
|
20844
|
+
} else if (mergedToolCalls.length > 0 || role || mergedText.length > 0 || aggregatedContentBlocks.length > 0) {
|
|
20845
|
+
const textContent = mergedText.length > 0 ? mergedText : _optionalChain([aggregatedContentBlocks, 'access', _480 => _480[0], 'optionalAccess', _481 => _481.type]) === "text" ? aggregatedContentBlocks[0].text : void 0;
|
|
19283
20846
|
output = {
|
|
19284
20847
|
...role ? { role } : {},
|
|
19285
|
-
...
|
|
20848
|
+
...textContent ? { content: textContent } : {},
|
|
19286
20849
|
...mergedToolCalls.length > 0 ? { toolCalls: mergedToolCalls } : {}
|
|
19287
20850
|
};
|
|
19288
20851
|
}
|
|
@@ -19300,22 +20863,116 @@ function aggregateCohereChatStreamChunks(chunks) {
|
|
|
19300
20863
|
};
|
|
19301
20864
|
}
|
|
19302
20865
|
|
|
20866
|
+
// src/instrumentation/plugins/groq-channels.ts
|
|
20867
|
+
var groqChannels = defineChannels("groq-sdk", {
|
|
20868
|
+
chatCompletionsCreate: channel({
|
|
20869
|
+
channelName: "chat.completions.create",
|
|
20870
|
+
kind: "async"
|
|
20871
|
+
}),
|
|
20872
|
+
embeddingsCreate: channel(
|
|
20873
|
+
{
|
|
20874
|
+
channelName: "embeddings.create",
|
|
20875
|
+
kind: "async"
|
|
20876
|
+
}
|
|
20877
|
+
)
|
|
20878
|
+
});
|
|
20879
|
+
|
|
20880
|
+
// src/instrumentation/plugins/groq-plugin.ts
|
|
20881
|
+
var GroqPlugin = class extends BasePlugin {
|
|
20882
|
+
onEnable() {
|
|
20883
|
+
this.unsubscribers.push(
|
|
20884
|
+
traceStreamingChannel(groqChannels.chatCompletionsCreate, {
|
|
20885
|
+
name: "groq.chat.completions.create",
|
|
20886
|
+
type: "llm" /* LLM */,
|
|
20887
|
+
extractInput: ([params]) => {
|
|
20888
|
+
const { messages, ...metadata } = params;
|
|
20889
|
+
return {
|
|
20890
|
+
input: processInputAttachments(messages),
|
|
20891
|
+
metadata: { ...metadata, provider: "groq" }
|
|
20892
|
+
};
|
|
20893
|
+
},
|
|
20894
|
+
extractOutput: (result) => _optionalChain([result, 'optionalAccess', _482 => _482.choices]),
|
|
20895
|
+
extractMetrics: (result, startTime) => {
|
|
20896
|
+
const metrics = parseGroqMetrics(result);
|
|
20897
|
+
if (startTime) {
|
|
20898
|
+
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
20899
|
+
}
|
|
20900
|
+
return metrics;
|
|
20901
|
+
},
|
|
20902
|
+
aggregateChunks: aggregateGroqChatCompletionChunks
|
|
20903
|
+
})
|
|
20904
|
+
);
|
|
20905
|
+
this.unsubscribers.push(
|
|
20906
|
+
traceAsyncChannel(groqChannels.embeddingsCreate, {
|
|
20907
|
+
name: "groq.embeddings.create",
|
|
20908
|
+
type: "llm" /* LLM */,
|
|
20909
|
+
extractInput: ([params]) => {
|
|
20910
|
+
const { input, ...metadata } = params;
|
|
20911
|
+
return {
|
|
20912
|
+
input,
|
|
20913
|
+
metadata: { ...metadata, provider: "groq" }
|
|
20914
|
+
};
|
|
20915
|
+
},
|
|
20916
|
+
extractOutput: (result) => {
|
|
20917
|
+
const embedding = _optionalChain([result, 'optionalAccess', _483 => _483.data, 'optionalAccess', _484 => _484[0], 'optionalAccess', _485 => _485.embedding]);
|
|
20918
|
+
return Array.isArray(embedding) ? { embedding_length: embedding.length } : void 0;
|
|
20919
|
+
},
|
|
20920
|
+
extractMetrics: (result) => parseGroqMetrics(result)
|
|
20921
|
+
})
|
|
20922
|
+
);
|
|
20923
|
+
}
|
|
20924
|
+
onDisable() {
|
|
20925
|
+
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
20926
|
+
}
|
|
20927
|
+
};
|
|
20928
|
+
function parseGroqMetrics(result) {
|
|
20929
|
+
const metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _486 => _486.usage]));
|
|
20930
|
+
const xGroq = _optionalChain([result, 'optionalAccess', _487 => _487.x_groq]);
|
|
20931
|
+
if (!xGroq || typeof xGroq !== "object") {
|
|
20932
|
+
return metrics;
|
|
20933
|
+
}
|
|
20934
|
+
const extraUsage = "usage" in xGroq ? xGroq.usage : void 0;
|
|
20935
|
+
if (!extraUsage || typeof extraUsage !== "object") {
|
|
20936
|
+
return metrics;
|
|
20937
|
+
}
|
|
20938
|
+
const dramCachedTokens = extraUsage["dram_cached_tokens"];
|
|
20939
|
+
const sramCachedTokens = extraUsage["sram_cached_tokens"];
|
|
20940
|
+
return {
|
|
20941
|
+
...metrics,
|
|
20942
|
+
...typeof dramCachedTokens === "number" ? { dram_cached_tokens: dramCachedTokens } : {},
|
|
20943
|
+
...typeof sramCachedTokens === "number" ? { sram_cached_tokens: sramCachedTokens } : {}
|
|
20944
|
+
};
|
|
20945
|
+
}
|
|
20946
|
+
function aggregateGroqChatCompletionChunks(chunks, streamResult, endEvent) {
|
|
20947
|
+
const aggregated = aggregateChatCompletionChunks(
|
|
20948
|
+
chunks,
|
|
20949
|
+
streamResult,
|
|
20950
|
+
endEvent
|
|
20951
|
+
);
|
|
20952
|
+
return {
|
|
20953
|
+
metrics: aggregated.metrics,
|
|
20954
|
+
output: aggregated.output
|
|
20955
|
+
};
|
|
20956
|
+
}
|
|
20957
|
+
|
|
19303
20958
|
// src/instrumentation/braintrust-plugin.ts
|
|
19304
|
-
var BraintrustPlugin = (
|
|
20959
|
+
var BraintrustPlugin = (_class20 = class extends BasePlugin {
|
|
19305
20960
|
|
|
19306
|
-
|
|
19307
|
-
|
|
19308
|
-
|
|
19309
|
-
|
|
19310
|
-
|
|
19311
|
-
|
|
19312
|
-
|
|
19313
|
-
|
|
19314
|
-
|
|
19315
|
-
|
|
19316
|
-
|
|
20961
|
+
__init67() {this.openaiPlugin = null}
|
|
20962
|
+
__init68() {this.anthropicPlugin = null}
|
|
20963
|
+
__init69() {this.aiSDKPlugin = null}
|
|
20964
|
+
__init70() {this.claudeAgentSDKPlugin = null}
|
|
20965
|
+
__init71() {this.cursorSDKPlugin = null}
|
|
20966
|
+
__init72() {this.googleGenAIPlugin = null}
|
|
20967
|
+
__init73() {this.huggingFacePlugin = null}
|
|
20968
|
+
__init74() {this.openRouterPlugin = null}
|
|
20969
|
+
__init75() {this.openRouterAgentPlugin = null}
|
|
20970
|
+
__init76() {this.mistralPlugin = null}
|
|
20971
|
+
__init77() {this.googleADKPlugin = null}
|
|
20972
|
+
__init78() {this.coherePlugin = null}
|
|
20973
|
+
__init79() {this.groqPlugin = null}
|
|
19317
20974
|
constructor(config = {}) {
|
|
19318
|
-
super();
|
|
20975
|
+
super();_class20.prototype.__init67.call(this);_class20.prototype.__init68.call(this);_class20.prototype.__init69.call(this);_class20.prototype.__init70.call(this);_class20.prototype.__init71.call(this);_class20.prototype.__init72.call(this);_class20.prototype.__init73.call(this);_class20.prototype.__init74.call(this);_class20.prototype.__init75.call(this);_class20.prototype.__init76.call(this);_class20.prototype.__init77.call(this);_class20.prototype.__init78.call(this);_class20.prototype.__init79.call(this);;
|
|
19319
20976
|
this.config = config;
|
|
19320
20977
|
}
|
|
19321
20978
|
onEnable() {
|
|
@@ -19336,6 +20993,10 @@ var BraintrustPlugin = (_class19 = class extends BasePlugin {
|
|
|
19336
20993
|
this.claudeAgentSDKPlugin = new ClaudeAgentSDKPlugin();
|
|
19337
20994
|
this.claudeAgentSDKPlugin.enable();
|
|
19338
20995
|
}
|
|
20996
|
+
if (integrations.cursorSDK !== false && integrations.cursor !== false) {
|
|
20997
|
+
this.cursorSDKPlugin = new CursorSDKPlugin();
|
|
20998
|
+
this.cursorSDKPlugin.enable();
|
|
20999
|
+
}
|
|
19339
21000
|
if (integrations.googleGenAI !== false && integrations.google !== false) {
|
|
19340
21001
|
this.googleGenAIPlugin = new GoogleGenAIPlugin();
|
|
19341
21002
|
this.googleGenAIPlugin.enable();
|
|
@@ -19364,6 +21025,10 @@ var BraintrustPlugin = (_class19 = class extends BasePlugin {
|
|
|
19364
21025
|
this.coherePlugin = new CoherePlugin();
|
|
19365
21026
|
this.coherePlugin.enable();
|
|
19366
21027
|
}
|
|
21028
|
+
if (integrations.groq !== false) {
|
|
21029
|
+
this.groqPlugin = new GroqPlugin();
|
|
21030
|
+
this.groqPlugin.enable();
|
|
21031
|
+
}
|
|
19367
21032
|
}
|
|
19368
21033
|
onDisable() {
|
|
19369
21034
|
if (this.openaiPlugin) {
|
|
@@ -19382,6 +21047,10 @@ var BraintrustPlugin = (_class19 = class extends BasePlugin {
|
|
|
19382
21047
|
this.claudeAgentSDKPlugin.disable();
|
|
19383
21048
|
this.claudeAgentSDKPlugin = null;
|
|
19384
21049
|
}
|
|
21050
|
+
if (this.cursorSDKPlugin) {
|
|
21051
|
+
this.cursorSDKPlugin.disable();
|
|
21052
|
+
this.cursorSDKPlugin = null;
|
|
21053
|
+
}
|
|
19385
21054
|
if (this.googleGenAIPlugin) {
|
|
19386
21055
|
this.googleGenAIPlugin.disable();
|
|
19387
21056
|
this.googleGenAIPlugin = null;
|
|
@@ -19410,14 +21079,18 @@ var BraintrustPlugin = (_class19 = class extends BasePlugin {
|
|
|
19410
21079
|
this.coherePlugin.disable();
|
|
19411
21080
|
this.coherePlugin = null;
|
|
19412
21081
|
}
|
|
21082
|
+
if (this.groqPlugin) {
|
|
21083
|
+
this.groqPlugin.disable();
|
|
21084
|
+
this.groqPlugin = null;
|
|
21085
|
+
}
|
|
19413
21086
|
}
|
|
19414
|
-
},
|
|
21087
|
+
}, _class20);
|
|
19415
21088
|
|
|
19416
21089
|
// src/instrumentation/registry.ts
|
|
19417
|
-
var PluginRegistry = (
|
|
19418
|
-
|
|
19419
|
-
|
|
19420
|
-
|
|
21090
|
+
var PluginRegistry = (_class21 = class {constructor() { _class21.prototype.__init80.call(this);_class21.prototype.__init81.call(this);_class21.prototype.__init82.call(this); }
|
|
21091
|
+
__init80() {this.braintrustPlugin = null}
|
|
21092
|
+
__init81() {this.config = {}}
|
|
21093
|
+
__init82() {this.enabled = false}
|
|
19421
21094
|
/**
|
|
19422
21095
|
* Configure which integrations should be enabled.
|
|
19423
21096
|
* This must be called before any SDK imports to take effect.
|
|
@@ -19483,6 +21156,8 @@ var PluginRegistry = (_class20 = class {constructor() { _class20.prototype.__ini
|
|
|
19483
21156
|
google: true,
|
|
19484
21157
|
huggingface: true,
|
|
19485
21158
|
claudeAgentSDK: true,
|
|
21159
|
+
cursor: true,
|
|
21160
|
+
cursorSDK: true,
|
|
19486
21161
|
openrouter: true,
|
|
19487
21162
|
openrouterAgent: true,
|
|
19488
21163
|
mistral: true,
|
|
@@ -19499,12 +21174,16 @@ var PluginRegistry = (_class20 = class {constructor() { _class20.prototype.__ini
|
|
|
19499
21174
|
if (disabledList) {
|
|
19500
21175
|
const disabled = disabledList.split(",").map((s) => s.trim().toLowerCase()).filter((s) => s.length > 0);
|
|
19501
21176
|
for (const sdk of disabled) {
|
|
19502
|
-
|
|
21177
|
+
if (sdk === "cursor-sdk") {
|
|
21178
|
+
integrations.cursorSDK = false;
|
|
21179
|
+
} else {
|
|
21180
|
+
integrations[sdk] = false;
|
|
21181
|
+
}
|
|
19503
21182
|
}
|
|
19504
21183
|
}
|
|
19505
21184
|
return { integrations };
|
|
19506
21185
|
}
|
|
19507
|
-
},
|
|
21186
|
+
}, _class21);
|
|
19508
21187
|
var registry = new PluginRegistry();
|
|
19509
21188
|
|
|
19510
21189
|
// src/node/config.ts
|
|
@@ -20721,11 +22400,11 @@ var SpanFetcher = class _SpanFetcher extends ObjectFetcher {
|
|
|
20721
22400
|
return this._state;
|
|
20722
22401
|
}
|
|
20723
22402
|
};
|
|
20724
|
-
var CachedSpanFetcher = (
|
|
20725
|
-
|
|
20726
|
-
|
|
22403
|
+
var CachedSpanFetcher = (_class22 = class {
|
|
22404
|
+
__init83() {this.spanCache = /* @__PURE__ */ new Map()}
|
|
22405
|
+
__init84() {this.allFetched = false}
|
|
20727
22406
|
|
|
20728
|
-
constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;
|
|
22407
|
+
constructor(objectTypeOrFetchFn, objectId, rootSpanId, getState) {;_class22.prototype.__init83.call(this);_class22.prototype.__init84.call(this);
|
|
20729
22408
|
if (typeof objectTypeOrFetchFn === "function") {
|
|
20730
22409
|
this.fetchFn = objectTypeOrFetchFn;
|
|
20731
22410
|
} else {
|
|
@@ -20740,7 +22419,7 @@ var CachedSpanFetcher = (_class21 = class {
|
|
|
20740
22419
|
spanType
|
|
20741
22420
|
);
|
|
20742
22421
|
const rows = await fetcher.fetchedData();
|
|
20743
|
-
return rows.filter((row) => _optionalChain([row, 'access',
|
|
22422
|
+
return rows.filter((row) => _optionalChain([row, 'access', _488 => _488.span_attributes, 'optionalAccess', _489 => _489.purpose]) !== "scorer").map((row) => ({
|
|
20744
22423
|
input: row.input,
|
|
20745
22424
|
output: row.output,
|
|
20746
22425
|
metadata: row.metadata,
|
|
@@ -20774,7 +22453,7 @@ var CachedSpanFetcher = (_class21 = class {
|
|
|
20774
22453
|
async fetchSpans(spanType) {
|
|
20775
22454
|
const spans = await this.fetchFn(spanType);
|
|
20776
22455
|
for (const span of spans) {
|
|
20777
|
-
const type = _nullishCoalesce(_optionalChain([span, 'access',
|
|
22456
|
+
const type = _nullishCoalesce(_optionalChain([span, 'access', _490 => _490.span_attributes, 'optionalAccess', _491 => _491.type]), () => ( ""));
|
|
20778
22457
|
const existing = _nullishCoalesce(this.spanCache.get(type), () => ( []));
|
|
20779
22458
|
existing.push(span);
|
|
20780
22459
|
this.spanCache.set(type, existing);
|
|
@@ -20791,24 +22470,24 @@ var CachedSpanFetcher = (_class21 = class {
|
|
|
20791
22470
|
}
|
|
20792
22471
|
return result;
|
|
20793
22472
|
}
|
|
20794
|
-
},
|
|
20795
|
-
var LocalTrace = (
|
|
22473
|
+
}, _class22);
|
|
22474
|
+
var LocalTrace = (_class23 = class {
|
|
20796
22475
|
|
|
20797
22476
|
|
|
20798
22477
|
|
|
20799
22478
|
|
|
20800
22479
|
|
|
20801
|
-
|
|
20802
|
-
|
|
22480
|
+
__init85() {this.spansFlushed = false}
|
|
22481
|
+
__init86() {this.spansFlushPromise = null}
|
|
20803
22482
|
|
|
20804
|
-
|
|
22483
|
+
__init87() {this.threadCache = /* @__PURE__ */ new Map()}
|
|
20805
22484
|
constructor({
|
|
20806
22485
|
objectType,
|
|
20807
22486
|
objectId,
|
|
20808
22487
|
rootSpanId,
|
|
20809
22488
|
ensureSpansFlushed,
|
|
20810
22489
|
state
|
|
20811
|
-
}) {;
|
|
22490
|
+
}) {;_class23.prototype.__init85.call(this);_class23.prototype.__init86.call(this);_class23.prototype.__init87.call(this);
|
|
20812
22491
|
this.objectType = objectType;
|
|
20813
22492
|
this.objectId = objectId;
|
|
20814
22493
|
this.rootSpanId = rootSpanId;
|
|
@@ -20854,11 +22533,11 @@ var LocalTrace = (_class22 = class {
|
|
|
20854
22533
|
const cachedSpans = this.state.spanCache.getByRootSpanId(this.rootSpanId);
|
|
20855
22534
|
if (cachedSpans && cachedSpans.length > 0) {
|
|
20856
22535
|
let spans = cachedSpans.filter(
|
|
20857
|
-
(span) => _optionalChain([span, 'access',
|
|
22536
|
+
(span) => _optionalChain([span, 'access', _492 => _492.span_attributes, 'optionalAccess', _493 => _493.purpose]) !== "scorer"
|
|
20858
22537
|
);
|
|
20859
22538
|
if (spanType && spanType.length > 0) {
|
|
20860
22539
|
spans = spans.filter(
|
|
20861
|
-
(span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access',
|
|
22540
|
+
(span) => spanType.includes(_nullishCoalesce(_optionalChain([span, 'access', _494 => _494.span_attributes, 'optionalAccess', _495 => _495.type]), () => ( "")))
|
|
20862
22541
|
);
|
|
20863
22542
|
}
|
|
20864
22543
|
return spans.map((span) => ({
|
|
@@ -20877,7 +22556,7 @@ var LocalTrace = (_class22 = class {
|
|
|
20877
22556
|
* Calls the API with the project_default preprocessor (which falls back to "thread").
|
|
20878
22557
|
*/
|
|
20879
22558
|
async getThread(options) {
|
|
20880
|
-
const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
22559
|
+
const cacheKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _496 => _496.preprocessor]), () => ( "project_default"));
|
|
20881
22560
|
if (!this.threadCache.has(cacheKey)) {
|
|
20882
22561
|
const promise = this.fetchThread(options);
|
|
20883
22562
|
this.threadCache.set(cacheKey, promise);
|
|
@@ -20888,7 +22567,7 @@ var LocalTrace = (_class22 = class {
|
|
|
20888
22567
|
await this.ensureSpansReady();
|
|
20889
22568
|
await this.state.login({});
|
|
20890
22569
|
const result = await invoke({
|
|
20891
|
-
globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
22570
|
+
globalFunction: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _497 => _497.preprocessor]), () => ( "project_default")),
|
|
20892
22571
|
functionType: "preprocessor",
|
|
20893
22572
|
input: {
|
|
20894
22573
|
trace_ref: {
|
|
@@ -20919,7 +22598,7 @@ var LocalTrace = (_class22 = class {
|
|
|
20919
22598
|
}
|
|
20920
22599
|
await this.spansFlushPromise;
|
|
20921
22600
|
}
|
|
20922
|
-
},
|
|
22601
|
+
}, _class23);
|
|
20923
22602
|
|
|
20924
22603
|
// src/reporters/progress.ts
|
|
20925
22604
|
var SimpleProgressReporter = class {
|
|
@@ -21064,10 +22743,10 @@ function validateParametersWithJsonSchema(parameters, schema) {
|
|
|
21064
22743
|
const ajv = new (0, _ajv2.default)({ coerceTypes: true, useDefaults: true, strict: false });
|
|
21065
22744
|
const validate = ajv.compile(schema);
|
|
21066
22745
|
if (!validate(parameters)) {
|
|
21067
|
-
const errorMessages = _optionalChain([validate, 'access',
|
|
22746
|
+
const errorMessages = _optionalChain([validate, 'access', _498 => _498.errors, 'optionalAccess', _499 => _499.map, 'call', _500 => _500((err) => {
|
|
21068
22747
|
const path2 = err.instancePath || "root";
|
|
21069
22748
|
return `${path2}: ${err.message}`;
|
|
21070
|
-
}), 'access',
|
|
22749
|
+
}), 'access', _501 => _501.join, 'call', _502 => _502(", ")]);
|
|
21071
22750
|
throw Error(`Invalid parameters: ${errorMessages}`);
|
|
21072
22751
|
}
|
|
21073
22752
|
return rehydrateRemoteParameters(parameters, schema);
|
|
@@ -21407,7 +23086,7 @@ async function runEvaluator(experiment, evaluator, progressReporter, filters, st
|
|
|
21407
23086
|
}
|
|
21408
23087
|
async function runEvaluatorInternal(experiment, evaluator, progressReporter, filters, stream, parameters, collectResults, enableCache) {
|
|
21409
23088
|
if (enableCache) {
|
|
21410
|
-
_optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess',
|
|
23089
|
+
_optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _503 => _503.spanCache, 'optionalAccess', _504 => _504.start, 'call', _505 => _505()]);
|
|
21411
23090
|
}
|
|
21412
23091
|
try {
|
|
21413
23092
|
if (typeof evaluator.data === "string") {
|
|
@@ -21462,7 +23141,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
21462
23141
|
const experimentIdPromise = experiment ? (async () => {
|
|
21463
23142
|
try {
|
|
21464
23143
|
return await experiment.id;
|
|
21465
|
-
} catch (
|
|
23144
|
+
} catch (e48) {
|
|
21466
23145
|
return void 0;
|
|
21467
23146
|
}
|
|
21468
23147
|
})() : void 0;
|
|
@@ -21518,7 +23197,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
21518
23197
|
objectType: parentComponents ? spanObjectTypeV3ToTypedString(
|
|
21519
23198
|
parentComponents.data.object_type
|
|
21520
23199
|
) : "experiment",
|
|
21521
|
-
objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async
|
|
23200
|
+
objectId: await _asyncNullishCoalesce(await _asyncOptionalChain([parentComponents, 'optionalAccess', async _506 => _506.data, 'access', async _507 => _507.object_id]), async () => ( (experimentIdPromise ? await _asyncNullishCoalesce(await experimentIdPromise, async () => ( "")) : ""))),
|
|
21522
23201
|
rootSpanId: rootSpan.rootSpanId,
|
|
21523
23202
|
ensureSpansFlushed,
|
|
21524
23203
|
state
|
|
@@ -21548,10 +23227,10 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
21548
23227
|
span,
|
|
21549
23228
|
parameters: _nullishCoalesce(parameters, () => ( {})),
|
|
21550
23229
|
reportProgress: (event) => {
|
|
21551
|
-
_optionalChain([stream, 'optionalCall',
|
|
23230
|
+
_optionalChain([stream, 'optionalCall', _508 => _508({
|
|
21552
23231
|
...event,
|
|
21553
23232
|
id: rootSpan.id,
|
|
21554
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
23233
|
+
origin: _optionalChain([baseEvent, 'access', _509 => _509.event, 'optionalAccess', _510 => _510.origin]),
|
|
21555
23234
|
name: evaluator.evalName,
|
|
21556
23235
|
object_type: "task"
|
|
21557
23236
|
})]);
|
|
@@ -21739,7 +23418,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
21739
23418
|
tags: tags.length ? tags : void 0,
|
|
21740
23419
|
metadata,
|
|
21741
23420
|
error,
|
|
21742
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
23421
|
+
origin: _optionalChain([baseEvent, 'access', _511 => _511.event, 'optionalAccess', _512 => _512.origin])
|
|
21743
23422
|
};
|
|
21744
23423
|
collectedResults.push({
|
|
21745
23424
|
...baseResult,
|
|
@@ -21779,7 +23458,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
21779
23458
|
break;
|
|
21780
23459
|
}
|
|
21781
23460
|
scheduledTrials++;
|
|
21782
|
-
_optionalChain([progressReporter, 'access',
|
|
23461
|
+
_optionalChain([progressReporter, 'access', _513 => _513.setTotal, 'optionalCall', _514 => _514(evaluator.evalName, scheduledTrials)]);
|
|
21783
23462
|
q.pushAsync({ datum, trialIndex }).catch((e) => {
|
|
21784
23463
|
if (queueErrors.length < 5) {
|
|
21785
23464
|
queueErrors.push(e);
|
|
@@ -21864,9 +23543,9 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
21864
23543
|
);
|
|
21865
23544
|
} finally {
|
|
21866
23545
|
if (enableCache) {
|
|
21867
|
-
const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess',
|
|
21868
|
-
_optionalChain([spanCache, 'optionalAccess',
|
|
21869
|
-
_optionalChain([spanCache, 'optionalAccess',
|
|
23546
|
+
const spanCache = _optionalChain([(_nullishCoalesce(evaluator.state, () => ( _internalGetGlobalState()))), 'optionalAccess', _515 => _515.spanCache]);
|
|
23547
|
+
_optionalChain([spanCache, 'optionalAccess', _516 => _516.dispose, 'call', _517 => _517()]);
|
|
23548
|
+
_optionalChain([spanCache, 'optionalAccess', _518 => _518.stop, 'call', _519 => _519()]);
|
|
21870
23549
|
}
|
|
21871
23550
|
}
|
|
21872
23551
|
}
|
|
@@ -22106,7 +23785,7 @@ async function cachedLogin(options) {
|
|
|
22106
23785
|
}
|
|
22107
23786
|
function makeCheckAuthorized(allowedOrgName) {
|
|
22108
23787
|
return async (req, _res, next) => {
|
|
22109
|
-
if (!_optionalChain([req, 'access',
|
|
23788
|
+
if (!_optionalChain([req, 'access', _520 => _520.ctx, 'optionalAccess', _521 => _521.token])) {
|
|
22110
23789
|
return next(_httperrors2.default.call(void 0, 401, "Unauthorized"));
|
|
22111
23790
|
}
|
|
22112
23791
|
try {
|
|
@@ -22119,7 +23798,7 @@ function makeCheckAuthorized(allowedOrgName) {
|
|
|
22119
23798
|
return next(_httperrors2.default.call(void 0, 403, errorMessage));
|
|
22120
23799
|
}
|
|
22121
23800
|
const state = await cachedLogin({
|
|
22122
|
-
apiKey: _optionalChain([req, 'access',
|
|
23801
|
+
apiKey: _optionalChain([req, 'access', _522 => _522.ctx, 'optionalAccess', _523 => _523.token]),
|
|
22123
23802
|
orgName
|
|
22124
23803
|
});
|
|
22125
23804
|
req.ctx.state = state;
|
|
@@ -22311,17 +23990,17 @@ var ProjectBuilder = class {
|
|
|
22311
23990
|
}
|
|
22312
23991
|
};
|
|
22313
23992
|
var projects = new ProjectBuilder();
|
|
22314
|
-
var Project2 = (
|
|
23993
|
+
var Project2 = (_class24 = class {
|
|
22315
23994
|
|
|
22316
23995
|
|
|
22317
23996
|
|
|
22318
23997
|
|
|
22319
23998
|
|
|
22320
23999
|
|
|
22321
|
-
|
|
22322
|
-
|
|
22323
|
-
|
|
22324
|
-
constructor(args) {;
|
|
24000
|
+
__init88() {this._publishableCodeFunctions = []}
|
|
24001
|
+
__init89() {this._publishablePrompts = []}
|
|
24002
|
+
__init90() {this._publishableParameters = []}
|
|
24003
|
+
constructor(args) {;_class24.prototype.__init88.call(this);_class24.prototype.__init89.call(this);_class24.prototype.__init90.call(this);
|
|
22325
24004
|
_initializeSpanContext();
|
|
22326
24005
|
this.name = "name" in args ? args.name : void 0;
|
|
22327
24006
|
this.id = "id" in args ? args.id : void 0;
|
|
@@ -22373,12 +24052,12 @@ var Project2 = (_class23 = class {
|
|
|
22373
24052
|
functions: functionDefinitions
|
|
22374
24053
|
});
|
|
22375
24054
|
}
|
|
22376
|
-
},
|
|
22377
|
-
var ToolBuilder = (
|
|
22378
|
-
constructor(project) {;
|
|
24055
|
+
}, _class24);
|
|
24056
|
+
var ToolBuilder = (_class25 = class {
|
|
24057
|
+
constructor(project) {;_class25.prototype.__init91.call(this);
|
|
22379
24058
|
this.project = project;
|
|
22380
24059
|
}
|
|
22381
|
-
|
|
24060
|
+
__init91() {this.taskCounter = 0}
|
|
22382
24061
|
// This type definition is just a catch all so that the implementation can be
|
|
22383
24062
|
// less specific than the two more specific declarations above.
|
|
22384
24063
|
create(opts) {
|
|
@@ -22403,12 +24082,12 @@ var ToolBuilder = (_class24 = class {
|
|
|
22403
24082
|
this.project.addCodeFunction(tool);
|
|
22404
24083
|
return tool;
|
|
22405
24084
|
}
|
|
22406
|
-
},
|
|
22407
|
-
var ScorerBuilder = (
|
|
22408
|
-
constructor(project) {;
|
|
24085
|
+
}, _class25);
|
|
24086
|
+
var ScorerBuilder = (_class26 = class {
|
|
24087
|
+
constructor(project) {;_class26.prototype.__init92.call(this);
|
|
22409
24088
|
this.project = project;
|
|
22410
24089
|
}
|
|
22411
|
-
|
|
24090
|
+
__init92() {this.taskCounter = 0}
|
|
22412
24091
|
create(opts) {
|
|
22413
24092
|
this.taskCounter++;
|
|
22414
24093
|
let resolvedName = opts.name;
|
|
@@ -22462,7 +24141,7 @@ var ScorerBuilder = (_class25 = class {
|
|
|
22462
24141
|
this.project.addPrompt(codePrompt);
|
|
22463
24142
|
}
|
|
22464
24143
|
}
|
|
22465
|
-
},
|
|
24144
|
+
}, _class26);
|
|
22466
24145
|
var CodeFunction = class {
|
|
22467
24146
|
constructor(project, opts) {
|
|
22468
24147
|
this.project = project;
|
|
@@ -22764,9 +24443,9 @@ function serializeRemoteEvalParametersContainer(parameters) {
|
|
|
22764
24443
|
source: null
|
|
22765
24444
|
};
|
|
22766
24445
|
}
|
|
22767
|
-
var ProjectNameIdMap = (
|
|
22768
|
-
|
|
22769
|
-
|
|
24446
|
+
var ProjectNameIdMap = (_class27 = class {constructor() { _class27.prototype.__init93.call(this);_class27.prototype.__init94.call(this); }
|
|
24447
|
+
__init93() {this.nameToId = {}}
|
|
24448
|
+
__init94() {this.idToName = {}}
|
|
22770
24449
|
async getId(projectName) {
|
|
22771
24450
|
if (!(projectName in this.nameToId)) {
|
|
22772
24451
|
const response = await _internalGetGlobalState().appConn().post_json("api/project/register", {
|
|
@@ -22799,7 +24478,7 @@ var ProjectNameIdMap = (_class26 = class {constructor() { _class26.prototype.__i
|
|
|
22799
24478
|
}
|
|
22800
24479
|
return this.getId(project.name);
|
|
22801
24480
|
}
|
|
22802
|
-
},
|
|
24481
|
+
}, _class27);
|
|
22803
24482
|
|
|
22804
24483
|
// dev/server.ts
|
|
22805
24484
|
function runDevServer(evaluators, opts) {
|
|
@@ -22874,7 +24553,7 @@ function runDevServer(evaluators, opts) {
|
|
|
22874
24553
|
scores,
|
|
22875
24554
|
stream
|
|
22876
24555
|
} = evalBodySchema.parse(req.body);
|
|
22877
|
-
if (!_optionalChain([req, 'access',
|
|
24556
|
+
if (!_optionalChain([req, 'access', _524 => _524.ctx, 'optionalAccess', _525 => _525.state])) {
|
|
22878
24557
|
res.status(500).json({ error: "Braintrust state not initialized in request" });
|
|
22879
24558
|
return;
|
|
22880
24559
|
}
|
|
@@ -22925,12 +24604,12 @@ function runDevServer(evaluators, opts) {
|
|
|
22925
24604
|
...evaluator,
|
|
22926
24605
|
data: evalData.data,
|
|
22927
24606
|
scores: (_nullishCoalesce(evaluator.scores, () => ( []))).concat(
|
|
22928
|
-
_nullishCoalesce(_optionalChain([scores, 'optionalAccess',
|
|
24607
|
+
_nullishCoalesce(_optionalChain([scores, 'optionalAccess', _526 => _526.map, 'call', _527 => _527(
|
|
22929
24608
|
(score) => makeScorer(
|
|
22930
24609
|
state,
|
|
22931
24610
|
score.name,
|
|
22932
24611
|
score.function_id,
|
|
22933
|
-
_optionalChain([req, 'access',
|
|
24612
|
+
_optionalChain([req, 'access', _528 => _528.ctx, 'optionalAccess', _529 => _529.projectId])
|
|
22934
24613
|
)
|
|
22935
24614
|
)]), () => ( []))
|
|
22936
24615
|
),
|
|
@@ -23021,6 +24700,8 @@ async function getDataset(state, data) {
|
|
|
23021
24700
|
state,
|
|
23022
24701
|
project: data.project_name,
|
|
23023
24702
|
dataset: data.dataset_name,
|
|
24703
|
+
version: _nullishCoalesce(data.dataset_version, () => ( void 0)),
|
|
24704
|
+
environment: _nullishCoalesce(data.dataset_environment, () => ( void 0)),
|
|
23024
24705
|
_internal_btql: _nullishCoalesce(data._internal_btql, () => ( void 0))
|
|
23025
24706
|
});
|
|
23026
24707
|
} else if ("dataset_id" in data) {
|
|
@@ -23032,6 +24713,8 @@ async function getDataset(state, data) {
|
|
|
23032
24713
|
state,
|
|
23033
24714
|
projectId: datasetInfo.projectId,
|
|
23034
24715
|
dataset: datasetInfo.dataset,
|
|
24716
|
+
version: _nullishCoalesce(data.dataset_version, () => ( void 0)),
|
|
24717
|
+
environment: _nullishCoalesce(data.dataset_environment, () => ( void 0)),
|
|
23035
24718
|
_internal_btql: _nullishCoalesce(data._internal_btql, () => ( void 0))
|
|
23036
24719
|
});
|
|
23037
24720
|
} else {
|