braintrust 1.0.0 → 1.0.2
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 +16 -3
- package/dev/dist/index.d.ts +16 -3
- package/dev/dist/index.js +69 -44
- package/dev/dist/index.mjs +43 -18
- package/dist/browser.d.mts +29 -11
- package/dist/browser.d.ts +29 -11
- package/dist/browser.js +338 -108
- package/dist/browser.mjs +258 -28
- package/dist/cli.js +49 -20
- package/dist/index.d.mts +29 -11
- package/dist/index.d.ts +29 -11
- package/dist/index.js +356 -121
- package/dist/index.mjs +264 -29
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -6476,7 +6476,7 @@ function validateAndSanitizeExperimentLogFullArgs(event, hasDataset) {
|
|
|
6476
6476
|
}
|
|
6477
6477
|
return event;
|
|
6478
6478
|
}
|
|
6479
|
-
var
|
|
6479
|
+
var DEFAULT_FETCH_BATCH_SIZE = 1e3;
|
|
6480
6480
|
var MAX_BTQL_ITERATIONS = 1e4;
|
|
6481
6481
|
var ObjectFetcher = (_class9 = class {
|
|
6482
6482
|
constructor(objectType, pinnedVersion, mutateRecord, _internal_btql) {;_class9.prototype.__init40.call(this);
|
|
@@ -6492,9 +6492,10 @@ var ObjectFetcher = (_class9 = class {
|
|
|
6492
6492
|
async getState() {
|
|
6493
6493
|
throw new Error("ObjectFetcher subclasses must have a 'getState' method");
|
|
6494
6494
|
}
|
|
6495
|
-
async *fetchRecordsFromApi() {
|
|
6495
|
+
async *fetchRecordsFromApi(batchSize) {
|
|
6496
6496
|
const state = await this.getState();
|
|
6497
6497
|
const objectId = await this.id;
|
|
6498
|
+
const limit = _nullishCoalesce(batchSize, () => ( DEFAULT_FETCH_BATCH_SIZE));
|
|
6498
6499
|
let cursor = void 0;
|
|
6499
6500
|
let iterations = 0;
|
|
6500
6501
|
while (true) {
|
|
@@ -6522,7 +6523,7 @@ var ObjectFetcher = (_class9 = class {
|
|
|
6522
6523
|
]
|
|
6523
6524
|
},
|
|
6524
6525
|
cursor,
|
|
6525
|
-
limit
|
|
6526
|
+
limit
|
|
6526
6527
|
},
|
|
6527
6528
|
use_columnstore: false,
|
|
6528
6529
|
brainstore_realtime: true,
|
|
@@ -6548,24 +6549,31 @@ var ObjectFetcher = (_class9 = class {
|
|
|
6548
6549
|
}
|
|
6549
6550
|
}
|
|
6550
6551
|
}
|
|
6551
|
-
|
|
6552
|
+
/**
|
|
6553
|
+
* Fetch all records from the object.
|
|
6554
|
+
*
|
|
6555
|
+
* @param options Optional parameters for fetching.
|
|
6556
|
+
* @param options.batchSize The number of records to fetch per request. Defaults to 1000.
|
|
6557
|
+
* @returns An async generator of records.
|
|
6558
|
+
*/
|
|
6559
|
+
async *fetch(options) {
|
|
6552
6560
|
if (this._fetchedData !== void 0) {
|
|
6553
6561
|
for (const record of this._fetchedData) {
|
|
6554
6562
|
yield record;
|
|
6555
6563
|
}
|
|
6556
6564
|
return;
|
|
6557
6565
|
}
|
|
6558
|
-
for await (const record of this.fetchRecordsFromApi()) {
|
|
6566
|
+
for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _64 => _64.batchSize]))) {
|
|
6559
6567
|
yield record;
|
|
6560
6568
|
}
|
|
6561
6569
|
}
|
|
6562
6570
|
[Symbol.asyncIterator]() {
|
|
6563
6571
|
return this.fetch();
|
|
6564
6572
|
}
|
|
6565
|
-
async fetchedData() {
|
|
6573
|
+
async fetchedData(options) {
|
|
6566
6574
|
if (this._fetchedData === void 0) {
|
|
6567
6575
|
const data = [];
|
|
6568
|
-
for await (const record of this.fetchRecordsFromApi()) {
|
|
6576
|
+
for await (const record of this.fetchRecordsFromApi(_optionalChain([options, 'optionalAccess', _65 => _65.batchSize]))) {
|
|
6569
6577
|
data.push(record);
|
|
6570
6578
|
}
|
|
6571
6579
|
this._fetchedData = data;
|
|
@@ -6575,12 +6583,12 @@ var ObjectFetcher = (_class9 = class {
|
|
|
6575
6583
|
clearCache() {
|
|
6576
6584
|
this._fetchedData = void 0;
|
|
6577
6585
|
}
|
|
6578
|
-
async version() {
|
|
6586
|
+
async version(options) {
|
|
6579
6587
|
if (this.pinnedVersion !== void 0) {
|
|
6580
6588
|
return this.pinnedVersion;
|
|
6581
6589
|
} else {
|
|
6582
6590
|
let maxVersion = void 0;
|
|
6583
|
-
for await (const record of this.fetch()) {
|
|
6591
|
+
for await (const record of this.fetch(options)) {
|
|
6584
6592
|
const xactId = String(_nullishCoalesce(record[TRANSACTION_ID_FIELD], () => ( "0")));
|
|
6585
6593
|
if (maxVersion === void 0 || xactId > maxVersion) {
|
|
6586
6594
|
maxVersion = xactId;
|
|
@@ -6660,7 +6668,7 @@ var Experiment2 = (_class10 = class extends ObjectFetcher {
|
|
|
6660
6668
|
* @returns The `id` of the logged event.
|
|
6661
6669
|
*/
|
|
6662
6670
|
log(event, options) {
|
|
6663
|
-
if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess',
|
|
6671
|
+
if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _66 => _66.allowConcurrentWithSpans])) {
|
|
6664
6672
|
throw new Error(
|
|
6665
6673
|
"Cannot run toplevel `log` method while using spans. To log to the span, call `experiment.traced` and then log with `span.log`"
|
|
6666
6674
|
);
|
|
@@ -6713,12 +6721,12 @@ var Experiment2 = (_class10 = class extends ObjectFetcher {
|
|
|
6713
6721
|
state: this.state,
|
|
6714
6722
|
...startSpanParentArgs({
|
|
6715
6723
|
state: this.state,
|
|
6716
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
6724
|
+
parent: _optionalChain([args, 'optionalAccess', _67 => _67.parent]),
|
|
6717
6725
|
parentObjectType: this.parentObjectType(),
|
|
6718
6726
|
parentObjectId: this.lazyId,
|
|
6719
6727
|
parentComputeObjectMetadataArgs: void 0,
|
|
6720
6728
|
parentSpanIds: void 0,
|
|
6721
|
-
propagatedEvent: _optionalChain([args, 'optionalAccess',
|
|
6729
|
+
propagatedEvent: _optionalChain([args, 'optionalAccess', _68 => _68.propagatedEvent])
|
|
6722
6730
|
}),
|
|
6723
6731
|
defaultRootType: "eval" /* EVAL */
|
|
6724
6732
|
});
|
|
@@ -6888,8 +6896,8 @@ var ReadonlyExperiment = class extends ObjectFetcher {
|
|
|
6888
6896
|
await this.lazyMetadata.get();
|
|
6889
6897
|
return this.state;
|
|
6890
6898
|
}
|
|
6891
|
-
async *asDataset() {
|
|
6892
|
-
const records = this.fetch();
|
|
6899
|
+
async *asDataset(options) {
|
|
6900
|
+
const records = this.fetch(options);
|
|
6893
6901
|
for await (const record of records) {
|
|
6894
6902
|
if (record.root_span_id !== record.span_id) {
|
|
6895
6903
|
continue;
|
|
@@ -7056,10 +7064,10 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
7056
7064
|
...serializableInternalData,
|
|
7057
7065
|
[IS_MERGE_FIELD]: this.isMerge
|
|
7058
7066
|
});
|
|
7059
|
-
if (_optionalChain([partialRecord, 'access',
|
|
7060
|
-
this.loggedEndTime = _optionalChain([partialRecord, 'access',
|
|
7067
|
+
if (_optionalChain([partialRecord, 'access', _69 => _69.metrics, 'optionalAccess', _70 => _70.end])) {
|
|
7068
|
+
this.loggedEndTime = _optionalChain([partialRecord, 'access', _71 => _71.metrics, 'optionalAccess', _72 => _72.end]);
|
|
7061
7069
|
}
|
|
7062
|
-
if ((_nullishCoalesce(partialRecord.tags, () => ( []))).length > 0 && _optionalChain([this, 'access',
|
|
7070
|
+
if ((_nullishCoalesce(partialRecord.tags, () => ( []))).length > 0 && _optionalChain([this, 'access', _73 => _73._spanParents, 'optionalAccess', _74 => _74.length])) {
|
|
7063
7071
|
throw new Error("Tags can only be logged to the root span");
|
|
7064
7072
|
}
|
|
7065
7073
|
const computeRecord = async () => ({
|
|
@@ -7104,18 +7112,18 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
7104
7112
|
);
|
|
7105
7113
|
}
|
|
7106
7114
|
startSpan(args) {
|
|
7107
|
-
const parentSpanIds = _optionalChain([args, 'optionalAccess',
|
|
7115
|
+
const parentSpanIds = _optionalChain([args, 'optionalAccess', _75 => _75.parent]) ? void 0 : { spanId: this._spanId, rootSpanId: this._rootSpanId };
|
|
7108
7116
|
return new _SpanImpl({
|
|
7109
7117
|
state: this._state,
|
|
7110
7118
|
...args,
|
|
7111
7119
|
...startSpanParentArgs({
|
|
7112
7120
|
state: this._state,
|
|
7113
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
7121
|
+
parent: _optionalChain([args, 'optionalAccess', _76 => _76.parent]),
|
|
7114
7122
|
parentObjectType: this.parentObjectType,
|
|
7115
7123
|
parentObjectId: this.parentObjectId,
|
|
7116
7124
|
parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
|
|
7117
7125
|
parentSpanIds,
|
|
7118
|
-
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
7126
|
+
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _77 => _77.propagatedEvent]), () => ( this.propagatedEvent))
|
|
7119
7127
|
})
|
|
7120
7128
|
});
|
|
7121
7129
|
}
|
|
@@ -7129,12 +7137,12 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
7129
7137
|
...args,
|
|
7130
7138
|
...startSpanParentArgs({
|
|
7131
7139
|
state: this._state,
|
|
7132
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
7140
|
+
parent: _optionalChain([args, 'optionalAccess', _78 => _78.parent]),
|
|
7133
7141
|
parentObjectType: this.parentObjectType,
|
|
7134
7142
|
parentObjectId: this.parentObjectId,
|
|
7135
7143
|
parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
|
|
7136
7144
|
parentSpanIds,
|
|
7137
|
-
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
7145
|
+
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _79 => _79.propagatedEvent]), () => ( this.propagatedEvent))
|
|
7138
7146
|
}),
|
|
7139
7147
|
spanId
|
|
7140
7148
|
});
|
|
@@ -7143,7 +7151,7 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
7143
7151
|
let endTime;
|
|
7144
7152
|
let internalData = {};
|
|
7145
7153
|
if (!this.loggedEndTime) {
|
|
7146
|
-
endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
7154
|
+
endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _80 => _80.endTime]), () => ( getCurrentUnixTimestamp()));
|
|
7147
7155
|
internalData = { metrics: { end: endTime } };
|
|
7148
7156
|
} else {
|
|
7149
7157
|
endTime = this.loggedEndTime;
|
|
@@ -7186,8 +7194,8 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
7186
7194
|
const args = this.parentComputeObjectMetadataArgs;
|
|
7187
7195
|
switch (this.parentObjectType) {
|
|
7188
7196
|
case 2 /* PROJECT_LOGS */: {
|
|
7189
|
-
const projectID = _optionalChain([args, 'optionalAccess',
|
|
7190
|
-
const projectName = _optionalChain([args, 'optionalAccess',
|
|
7197
|
+
const projectID = _optionalChain([args, 'optionalAccess', _81 => _81.project_id]) || this.parentObjectId.getSync().value;
|
|
7198
|
+
const projectName = _optionalChain([args, 'optionalAccess', _82 => _82.project_name]);
|
|
7191
7199
|
if (projectID) {
|
|
7192
7200
|
return `${baseUrl}/object?object_type=project_logs&object_id=${projectID}&id=${this._id}`;
|
|
7193
7201
|
} else if (projectName) {
|
|
@@ -7197,7 +7205,7 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
7197
7205
|
}
|
|
7198
7206
|
}
|
|
7199
7207
|
case 1 /* EXPERIMENT */: {
|
|
7200
|
-
const expID = _optionalChain([args, 'optionalAccess',
|
|
7208
|
+
const expID = _optionalChain([args, 'optionalAccess', _83 => _83.experiment_id]) || _optionalChain([this, 'access', _84 => _84.parentObjectId, 'optionalAccess', _85 => _85.getSync, 'call', _86 => _86(), 'optionalAccess', _87 => _87.value]);
|
|
7201
7209
|
if (!expID) {
|
|
7202
7210
|
return getErrPermlink("provide-experiment-id");
|
|
7203
7211
|
} else {
|
|
@@ -7653,13 +7661,13 @@ var Prompt2 = (_class13 = class _Prompt {
|
|
|
7653
7661
|
return "slug" in this.metadata ? this.metadata.slug : this.metadata.id;
|
|
7654
7662
|
}
|
|
7655
7663
|
get prompt() {
|
|
7656
|
-
return _optionalChain([this, 'access',
|
|
7664
|
+
return _optionalChain([this, 'access', _88 => _88.getParsedPromptData, 'call', _89 => _89(), 'optionalAccess', _90 => _90.prompt]);
|
|
7657
7665
|
}
|
|
7658
7666
|
get version() {
|
|
7659
7667
|
return this.metadata[TRANSACTION_ID_FIELD];
|
|
7660
7668
|
}
|
|
7661
7669
|
get options() {
|
|
7662
|
-
return _optionalChain([this, 'access',
|
|
7670
|
+
return _optionalChain([this, 'access', _91 => _91.getParsedPromptData, 'call', _92 => _92(), 'optionalAccess', _93 => _93.options]) || {};
|
|
7663
7671
|
}
|
|
7664
7672
|
get promptData() {
|
|
7665
7673
|
return this.getParsedPromptData();
|
|
@@ -7810,7 +7818,7 @@ var Prompt2 = (_class13 = class _Prompt {
|
|
|
7810
7818
|
return {
|
|
7811
7819
|
type: "chat",
|
|
7812
7820
|
messages,
|
|
7813
|
-
..._optionalChain([prompt, 'access',
|
|
7821
|
+
..._optionalChain([prompt, 'access', _94 => _94.tools, 'optionalAccess', _95 => _95.trim, 'call', _96 => _96()]) ? {
|
|
7814
7822
|
tools: render(prompt.tools)
|
|
7815
7823
|
} : void 0
|
|
7816
7824
|
};
|
|
@@ -7920,9 +7928,9 @@ async function getPromptVersions(projectId, promptId) {
|
|
|
7920
7928
|
);
|
|
7921
7929
|
}
|
|
7922
7930
|
const result = await response.json();
|
|
7923
|
-
return _optionalChain([result, 'access',
|
|
7924
|
-
(entry) => ["upsert", "merge"].includes(_optionalChain([entry, 'access',
|
|
7925
|
-
), 'access',
|
|
7931
|
+
return _optionalChain([result, 'access', _97 => _97.data, 'optionalAccess', _98 => _98.filter, 'call', _99 => _99(
|
|
7932
|
+
(entry) => ["upsert", "merge"].includes(_optionalChain([entry, 'access', _100 => _100.audit_data, 'optionalAccess', _101 => _101.action]))
|
|
7933
|
+
), 'access', _102 => _102.map, 'call', _103 => _103((entry) => prettifyXact(entry._xact_id))]) || [];
|
|
7926
7934
|
}
|
|
7927
7935
|
function resetIdGenStateForTests() {
|
|
7928
7936
|
const state = _internalGetGlobalState();
|
|
@@ -7985,6 +7993,7 @@ __export(exports_node_exports, {
|
|
|
7985
7993
|
CodeFunction: () => CodeFunction,
|
|
7986
7994
|
CodePrompt: () => CodePrompt,
|
|
7987
7995
|
ContextManager: () => ContextManager,
|
|
7996
|
+
DEFAULT_FETCH_BATCH_SIZE: () => DEFAULT_FETCH_BATCH_SIZE,
|
|
7988
7997
|
Dataset: () => Dataset2,
|
|
7989
7998
|
ERR_PERMALINK: () => ERR_PERMALINK,
|
|
7990
7999
|
Eval: () => Eval,
|
|
@@ -7993,7 +8002,6 @@ __export(exports_node_exports, {
|
|
|
7993
8002
|
ExternalAttachment: () => ExternalAttachment,
|
|
7994
8003
|
FailedHTTPResponse: () => FailedHTTPResponse,
|
|
7995
8004
|
IDGenerator: () => IDGenerator,
|
|
7996
|
-
INTERNAL_BTQL_LIMIT: () => INTERNAL_BTQL_LIMIT,
|
|
7997
8005
|
JSONAttachment: () => JSONAttachment,
|
|
7998
8006
|
LEGACY_CACHED_HEADER: () => LEGACY_CACHED_HEADER,
|
|
7999
8007
|
LazyValue: () => LazyValue,
|
|
@@ -8219,7 +8227,7 @@ function parseSpanFromResponseCreateParams(params) {
|
|
|
8219
8227
|
}
|
|
8220
8228
|
function parseEventFromResponseCreateResult(result) {
|
|
8221
8229
|
const data = {};
|
|
8222
|
-
if (_optionalChain([result, 'optionalAccess',
|
|
8230
|
+
if (_optionalChain([result, 'optionalAccess', _104 => _104.output]) !== void 0) {
|
|
8223
8231
|
data.output = processImagesInOutput(result.output);
|
|
8224
8232
|
}
|
|
8225
8233
|
if (result) {
|
|
@@ -8228,7 +8236,7 @@ function parseEventFromResponseCreateResult(result) {
|
|
|
8228
8236
|
data.metadata = metadata;
|
|
8229
8237
|
}
|
|
8230
8238
|
}
|
|
8231
|
-
data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
8239
|
+
data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _105 => _105.usage]));
|
|
8232
8240
|
return data;
|
|
8233
8241
|
}
|
|
8234
8242
|
function processImagesInOutput(output) {
|
|
@@ -8282,7 +8290,7 @@ function parseSpanFromResponseParseParams(params) {
|
|
|
8282
8290
|
}
|
|
8283
8291
|
function parseEventFromResponseParseResult(result) {
|
|
8284
8292
|
const data = {};
|
|
8285
|
-
if (_optionalChain([result, 'optionalAccess',
|
|
8293
|
+
if (_optionalChain([result, 'optionalAccess', _106 => _106.output]) !== void 0) {
|
|
8286
8294
|
data.output = processImagesInOutput(result.output);
|
|
8287
8295
|
}
|
|
8288
8296
|
if (result) {
|
|
@@ -8291,7 +8299,7 @@ function parseEventFromResponseParseResult(result) {
|
|
|
8291
8299
|
data.metadata = metadata;
|
|
8292
8300
|
}
|
|
8293
8301
|
}
|
|
8294
|
-
data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
8302
|
+
data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _107 => _107.usage]));
|
|
8295
8303
|
return data;
|
|
8296
8304
|
}
|
|
8297
8305
|
function traceResponseCreateStream(stream, timedSpan) {
|
|
@@ -8308,7 +8316,7 @@ function traceResponseCreateStream(stream, timedSpan) {
|
|
|
8308
8316
|
return result;
|
|
8309
8317
|
}
|
|
8310
8318
|
const item = result.value;
|
|
8311
|
-
if (!item || !_optionalChain([item, 'optionalAccess',
|
|
8319
|
+
if (!item || !_optionalChain([item, 'optionalAccess', _108 => _108.type]) || !_optionalChain([item, 'optionalAccess', _109 => _109.response])) {
|
|
8312
8320
|
return result;
|
|
8313
8321
|
}
|
|
8314
8322
|
const event = parseLogFromItem(item);
|
|
@@ -8319,14 +8327,14 @@ function traceResponseCreateStream(stream, timedSpan) {
|
|
|
8319
8327
|
};
|
|
8320
8328
|
}
|
|
8321
8329
|
function parseLogFromItem(item) {
|
|
8322
|
-
if (!item || !_optionalChain([item, 'optionalAccess',
|
|
8330
|
+
if (!item || !_optionalChain([item, 'optionalAccess', _110 => _110.type]) || !_optionalChain([item, 'optionalAccess', _111 => _111.response])) {
|
|
8323
8331
|
return {};
|
|
8324
8332
|
}
|
|
8325
8333
|
const response = item.response;
|
|
8326
8334
|
switch (item.type) {
|
|
8327
8335
|
case "response.completed":
|
|
8328
8336
|
const data = {};
|
|
8329
|
-
if (_optionalChain([response, 'optionalAccess',
|
|
8337
|
+
if (_optionalChain([response, 'optionalAccess', _112 => _112.output]) !== void 0) {
|
|
8330
8338
|
data.output = processImagesInOutput(response.output);
|
|
8331
8339
|
}
|
|
8332
8340
|
if (response) {
|
|
@@ -8335,7 +8343,7 @@ function parseLogFromItem(item) {
|
|
|
8335
8343
|
data.metadata = metadata;
|
|
8336
8344
|
}
|
|
8337
8345
|
}
|
|
8338
|
-
data.metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess',
|
|
8346
|
+
data.metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _113 => _113.usage]));
|
|
8339
8347
|
return data;
|
|
8340
8348
|
default:
|
|
8341
8349
|
return {};
|
|
@@ -8464,6 +8472,12 @@ function apiPromiseProxy(apiPromise, span, onThen, traceStreamFunc) {
|
|
|
8464
8472
|
);
|
|
8465
8473
|
};
|
|
8466
8474
|
}
|
|
8475
|
+
if (name === "withResponse") {
|
|
8476
|
+
return async () => {
|
|
8477
|
+
const { data, response } = await target.withResponse();
|
|
8478
|
+
return { data: onThen(data), response };
|
|
8479
|
+
};
|
|
8480
|
+
}
|
|
8467
8481
|
return Reflect.get(target, name, receiver);
|
|
8468
8482
|
}
|
|
8469
8483
|
});
|
|
@@ -8509,6 +8523,8 @@ function wrapOpenAIv4(openai) {
|
|
|
8509
8523
|
return wrapChatCompletion(baseVal.bind(target));
|
|
8510
8524
|
} else if (name === "parse") {
|
|
8511
8525
|
return wrapBetaChatCompletionParse(baseVal.bind(target));
|
|
8526
|
+
} else if (name === "stream") {
|
|
8527
|
+
return wrapBetaChatCompletionStream(baseVal.bind(target));
|
|
8512
8528
|
}
|
|
8513
8529
|
return baseVal;
|
|
8514
8530
|
}
|
|
@@ -8524,8 +8540,8 @@ function wrapOpenAIv4(openai) {
|
|
|
8524
8540
|
const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
|
|
8525
8541
|
const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
|
|
8526
8542
|
let betaProxy2;
|
|
8527
|
-
if (_optionalChain([openai, 'access',
|
|
8528
|
-
const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess',
|
|
8543
|
+
if (_optionalChain([openai, 'access', _114 => _114.beta, 'optionalAccess', _115 => _115.chat, 'optionalAccess', _116 => _116.completions, 'optionalAccess', _117 => _117.stream])) {
|
|
8544
|
+
const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _118 => _118.beta, 'optionalAccess', _119 => _119.chat, 'access', _120 => _120.completions]), {
|
|
8529
8545
|
get(target, name, receiver) {
|
|
8530
8546
|
const baseVal = Reflect.get(target, name, receiver);
|
|
8531
8547
|
if (name === "parse") {
|
|
@@ -8573,7 +8589,7 @@ function wrapOpenAIv4(openai) {
|
|
|
8573
8589
|
});
|
|
8574
8590
|
}
|
|
8575
8591
|
function logCompletionResponse(startTime, response, span) {
|
|
8576
|
-
const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess',
|
|
8592
|
+
const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _121 => _121.usage]));
|
|
8577
8593
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
8578
8594
|
span.log({
|
|
8579
8595
|
output: response.choices,
|
|
@@ -8803,7 +8819,7 @@ function parseChatCompletionParams(params) {
|
|
|
8803
8819
|
function processEmbeddingResponse(result, span) {
|
|
8804
8820
|
span.log({
|
|
8805
8821
|
output: { embedding_length: result.data[0].embedding.length },
|
|
8806
|
-
metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
8822
|
+
metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _122 => _122.usage]))
|
|
8807
8823
|
});
|
|
8808
8824
|
}
|
|
8809
8825
|
function processModerationResponse(result, span) {
|
|
@@ -8833,10 +8849,10 @@ function postprocessStreamingResults(allResults) {
|
|
|
8833
8849
|
if (result.usage) {
|
|
8834
8850
|
metrics = {
|
|
8835
8851
|
...metrics,
|
|
8836
|
-
...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
8852
|
+
...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _123 => _123.usage]))
|
|
8837
8853
|
};
|
|
8838
8854
|
}
|
|
8839
|
-
const delta = _optionalChain([result, 'access',
|
|
8855
|
+
const delta = _optionalChain([result, 'access', _124 => _124.choices, 'optionalAccess', _125 => _125[0], 'optionalAccess', _126 => _126.delta]);
|
|
8840
8856
|
if (!delta) {
|
|
8841
8857
|
continue;
|
|
8842
8858
|
}
|
|
@@ -9068,24 +9084,67 @@ function wrapAISDK(aiSDK, options = {}) {
|
|
|
9068
9084
|
const original = Reflect.get(target, prop, receiver);
|
|
9069
9085
|
switch (prop) {
|
|
9070
9086
|
case "generateText":
|
|
9071
|
-
return wrapGenerateText(original, options);
|
|
9087
|
+
return wrapGenerateText(original, options, aiSDK);
|
|
9072
9088
|
case "streamText":
|
|
9073
|
-
return wrapStreamText(original, options);
|
|
9089
|
+
return wrapStreamText(original, options, aiSDK);
|
|
9074
9090
|
case "generateObject":
|
|
9075
|
-
return wrapGenerateObject(original, options);
|
|
9091
|
+
return wrapGenerateObject(original, options, aiSDK);
|
|
9076
9092
|
case "streamObject":
|
|
9077
|
-
return wrapStreamObject(original, options);
|
|
9093
|
+
return wrapStreamObject(original, options, aiSDK);
|
|
9094
|
+
case "Agent":
|
|
9095
|
+
case "Experimental_Agent":
|
|
9096
|
+
case "ToolLoopAgent":
|
|
9097
|
+
return original ? wrapAgentClass(original, options) : original;
|
|
9078
9098
|
}
|
|
9079
9099
|
return original;
|
|
9080
9100
|
}
|
|
9081
9101
|
});
|
|
9082
9102
|
}
|
|
9083
|
-
var
|
|
9084
|
-
return
|
|
9103
|
+
var wrapAgentClass = (AgentClass, options = {}) => {
|
|
9104
|
+
return new Proxy(AgentClass, {
|
|
9105
|
+
construct(target, args) {
|
|
9106
|
+
const instance = new target(...args);
|
|
9107
|
+
return new Proxy(instance, {
|
|
9108
|
+
get(instanceTarget, prop, instanceReceiver) {
|
|
9109
|
+
const original = Reflect.get(instanceTarget, prop, instanceReceiver);
|
|
9110
|
+
if (prop === "generate") {
|
|
9111
|
+
return wrapAgentGenerate(original, instanceTarget, options);
|
|
9112
|
+
}
|
|
9113
|
+
if (prop === "stream") {
|
|
9114
|
+
return wrapAgentStream(original, instanceTarget, options);
|
|
9115
|
+
}
|
|
9116
|
+
return original;
|
|
9117
|
+
}
|
|
9118
|
+
});
|
|
9119
|
+
}
|
|
9120
|
+
});
|
|
9121
|
+
};
|
|
9122
|
+
var wrapAgentGenerate = (generate, instance, options = {}) => {
|
|
9123
|
+
return async (params) => makeGenerateTextWrapper(
|
|
9124
|
+
"Agent.generate",
|
|
9125
|
+
options,
|
|
9126
|
+
generate.bind(instance)
|
|
9127
|
+
// as of v5 this is just streamText under the hood
|
|
9128
|
+
// Follows what the AI SDK does under the hood when calling generateText
|
|
9129
|
+
)({ ...instance.settings, ...params });
|
|
9130
|
+
};
|
|
9131
|
+
var wrapAgentStream = (stream, instance, options = {}) => {
|
|
9132
|
+
return (params) => makeStreamTextWrapper(
|
|
9133
|
+
"Agent.stream",
|
|
9134
|
+
options,
|
|
9135
|
+
stream.bind(instance),
|
|
9136
|
+
// as of v5 this is just streamText under the hood
|
|
9137
|
+
void 0
|
|
9138
|
+
// aiSDK not needed since model is already on instance
|
|
9139
|
+
)({ ...instance.settings, ...params });
|
|
9140
|
+
};
|
|
9141
|
+
var makeGenerateTextWrapper = (name, options, generateText, aiSDK) => {
|
|
9142
|
+
const wrapper = async function(params) {
|
|
9085
9143
|
return traced(
|
|
9086
9144
|
async (span) => {
|
|
9087
9145
|
const result = await generateText({
|
|
9088
9146
|
...params,
|
|
9147
|
+
model: wrapModel(params.model, aiSDK),
|
|
9089
9148
|
tools: wrapTools(params.tools)
|
|
9090
9149
|
});
|
|
9091
9150
|
span.log({
|
|
@@ -9095,7 +9154,7 @@ var wrapGenerateText = (generateText, options = {}) => {
|
|
|
9095
9154
|
return result;
|
|
9096
9155
|
},
|
|
9097
9156
|
{
|
|
9098
|
-
name
|
|
9157
|
+
name,
|
|
9099
9158
|
spanAttributes: {
|
|
9100
9159
|
type: "llm" /* LLM */
|
|
9101
9160
|
},
|
|
@@ -9112,17 +9171,176 @@ var wrapGenerateText = (generateText, options = {}) => {
|
|
|
9112
9171
|
}
|
|
9113
9172
|
);
|
|
9114
9173
|
};
|
|
9174
|
+
Object.defineProperty(wrapper, "name", { value: name, writable: false });
|
|
9175
|
+
return wrapper;
|
|
9115
9176
|
};
|
|
9116
|
-
var
|
|
9177
|
+
var resolveModel = (model, ai) => {
|
|
9178
|
+
if (typeof model !== "string") {
|
|
9179
|
+
return model;
|
|
9180
|
+
}
|
|
9181
|
+
const provider = _nullishCoalesce(_nullishCoalesce(globalThis.AI_SDK_DEFAULT_PROVIDER, () => ( _optionalChain([ai, 'optionalAccess', _127 => _127.gateway]))), () => ( null));
|
|
9182
|
+
if (provider && typeof provider.languageModel === "function") {
|
|
9183
|
+
return provider.languageModel(model);
|
|
9184
|
+
}
|
|
9185
|
+
return model;
|
|
9186
|
+
};
|
|
9187
|
+
var wrapModel = (model, ai) => {
|
|
9188
|
+
const resolvedModel = resolveModel(model, ai);
|
|
9189
|
+
if (!resolvedModel || typeof resolvedModel !== "object" || typeof resolvedModel.doGenerate !== "function") {
|
|
9190
|
+
return model;
|
|
9191
|
+
}
|
|
9192
|
+
if (resolvedModel._braintrustWrapped) {
|
|
9193
|
+
return resolvedModel;
|
|
9194
|
+
}
|
|
9195
|
+
const originalDoGenerate = resolvedModel.doGenerate.bind(resolvedModel);
|
|
9196
|
+
const originalDoStream = _optionalChain([resolvedModel, 'access', _128 => _128.doStream, 'optionalAccess', _129 => _129.bind, 'call', _130 => _130(resolvedModel)]);
|
|
9197
|
+
const wrappedDoGenerate = async (options) => {
|
|
9198
|
+
return traced(
|
|
9199
|
+
async (span) => {
|
|
9200
|
+
const result = await originalDoGenerate(options);
|
|
9201
|
+
span.log({
|
|
9202
|
+
output: await processOutput(result),
|
|
9203
|
+
metrics: extractTokenMetrics(result)
|
|
9204
|
+
});
|
|
9205
|
+
return result;
|
|
9206
|
+
},
|
|
9207
|
+
{
|
|
9208
|
+
name: "doGenerate",
|
|
9209
|
+
spanAttributes: {
|
|
9210
|
+
type: "llm" /* LLM */
|
|
9211
|
+
},
|
|
9212
|
+
event: {
|
|
9213
|
+
input: processInputAttachments2(options),
|
|
9214
|
+
metadata: {
|
|
9215
|
+
model: resolvedModel.modelId,
|
|
9216
|
+
provider: resolvedModel.provider,
|
|
9217
|
+
braintrust: {
|
|
9218
|
+
integration_name: "ai-sdk",
|
|
9219
|
+
sdk_language: "typescript"
|
|
9220
|
+
}
|
|
9221
|
+
}
|
|
9222
|
+
}
|
|
9223
|
+
}
|
|
9224
|
+
);
|
|
9225
|
+
};
|
|
9226
|
+
const wrappedDoStream = async (options) => {
|
|
9227
|
+
const span = startSpan({
|
|
9228
|
+
name: "doStream",
|
|
9229
|
+
spanAttributes: {
|
|
9230
|
+
type: "llm" /* LLM */
|
|
9231
|
+
},
|
|
9232
|
+
event: {
|
|
9233
|
+
input: processInputAttachments2(options),
|
|
9234
|
+
metadata: {
|
|
9235
|
+
model: resolvedModel.modelId,
|
|
9236
|
+
provider: resolvedModel.provider,
|
|
9237
|
+
braintrust: {
|
|
9238
|
+
integration_name: "ai-sdk",
|
|
9239
|
+
sdk_language: "typescript"
|
|
9240
|
+
}
|
|
9241
|
+
}
|
|
9242
|
+
}
|
|
9243
|
+
});
|
|
9244
|
+
const result = await originalDoStream(options);
|
|
9245
|
+
const output = {};
|
|
9246
|
+
let text = "";
|
|
9247
|
+
let reasoning = "";
|
|
9248
|
+
const toolCalls = [];
|
|
9249
|
+
let object = void 0;
|
|
9250
|
+
const extractTextDelta = (chunk) => {
|
|
9251
|
+
if (typeof chunk.textDelta === "string") return chunk.textDelta;
|
|
9252
|
+
if (typeof chunk.delta === "string") return chunk.delta;
|
|
9253
|
+
if (typeof chunk.text === "string") return chunk.text;
|
|
9254
|
+
if (typeof chunk.content === "string") return chunk.content;
|
|
9255
|
+
return "";
|
|
9256
|
+
};
|
|
9257
|
+
const transformStream = new TransformStream({
|
|
9258
|
+
async transform(chunk, controller) {
|
|
9259
|
+
switch (chunk.type) {
|
|
9260
|
+
case "text-delta":
|
|
9261
|
+
text += extractTextDelta(chunk);
|
|
9262
|
+
break;
|
|
9263
|
+
case "reasoning-delta":
|
|
9264
|
+
if (chunk.delta) {
|
|
9265
|
+
reasoning += chunk.delta;
|
|
9266
|
+
} else if (chunk.text) {
|
|
9267
|
+
reasoning += chunk.text;
|
|
9268
|
+
}
|
|
9269
|
+
break;
|
|
9270
|
+
case "tool-call":
|
|
9271
|
+
toolCalls.push(chunk);
|
|
9272
|
+
break;
|
|
9273
|
+
case "object":
|
|
9274
|
+
object = chunk.object;
|
|
9275
|
+
break;
|
|
9276
|
+
case "raw":
|
|
9277
|
+
if (chunk.rawValue) {
|
|
9278
|
+
const rawVal = chunk.rawValue;
|
|
9279
|
+
if (_optionalChain([rawVal, 'access', _131 => _131.delta, 'optionalAccess', _132 => _132.content])) {
|
|
9280
|
+
text += rawVal.delta.content;
|
|
9281
|
+
} else if (_optionalChain([rawVal, 'access', _133 => _133.choices, 'optionalAccess', _134 => _134[0], 'optionalAccess', _135 => _135.delta, 'optionalAccess', _136 => _136.content])) {
|
|
9282
|
+
text += rawVal.choices[0].delta.content;
|
|
9283
|
+
} else if (typeof rawVal.text === "string") {
|
|
9284
|
+
text += rawVal.text;
|
|
9285
|
+
} else if (typeof rawVal.content === "string") {
|
|
9286
|
+
text += rawVal.content;
|
|
9287
|
+
}
|
|
9288
|
+
}
|
|
9289
|
+
break;
|
|
9290
|
+
case "finish":
|
|
9291
|
+
output.text = text;
|
|
9292
|
+
output.reasoning = reasoning;
|
|
9293
|
+
output.toolCalls = toolCalls;
|
|
9294
|
+
output.finishReason = chunk.finishReason;
|
|
9295
|
+
output.usage = chunk.usage;
|
|
9296
|
+
if (object !== void 0) {
|
|
9297
|
+
output.object = object;
|
|
9298
|
+
}
|
|
9299
|
+
span.log({
|
|
9300
|
+
output: await processOutput(output),
|
|
9301
|
+
metrics: extractTokenMetrics(output)
|
|
9302
|
+
});
|
|
9303
|
+
span.end();
|
|
9304
|
+
break;
|
|
9305
|
+
}
|
|
9306
|
+
controller.enqueue(chunk);
|
|
9307
|
+
}
|
|
9308
|
+
});
|
|
9309
|
+
return {
|
|
9310
|
+
...result,
|
|
9311
|
+
stream: result.stream.pipeThrough(transformStream)
|
|
9312
|
+
};
|
|
9313
|
+
};
|
|
9314
|
+
return new Proxy(resolvedModel, {
|
|
9315
|
+
get(target, prop, receiver) {
|
|
9316
|
+
if (prop === "_braintrustWrapped") {
|
|
9317
|
+
return true;
|
|
9318
|
+
}
|
|
9319
|
+
if (prop === "doGenerate") {
|
|
9320
|
+
return wrappedDoGenerate;
|
|
9321
|
+
}
|
|
9322
|
+
if (prop === "doStream" && originalDoStream) {
|
|
9323
|
+
return wrappedDoStream;
|
|
9324
|
+
}
|
|
9325
|
+
return Reflect.get(target, prop, receiver);
|
|
9326
|
+
}
|
|
9327
|
+
});
|
|
9328
|
+
};
|
|
9329
|
+
var wrapGenerateText = (generateText, options = {}, aiSDK) => {
|
|
9330
|
+
return makeGenerateTextWrapper("generateText", options, generateText, aiSDK);
|
|
9331
|
+
};
|
|
9332
|
+
var wrapGenerateObject = (generateObject, options = {}, aiSDK) => {
|
|
9117
9333
|
return async function wrappedGenerateObject(params) {
|
|
9118
9334
|
return traced(
|
|
9119
9335
|
async (span) => {
|
|
9120
9336
|
const result = await generateObject({
|
|
9121
9337
|
...params,
|
|
9338
|
+
model: wrapModel(params.model, aiSDK),
|
|
9122
9339
|
tools: wrapTools(params.tools)
|
|
9123
9340
|
});
|
|
9341
|
+
const output = await processOutput(result, options.denyOutputPaths);
|
|
9124
9342
|
span.log({
|
|
9125
|
-
output
|
|
9343
|
+
output,
|
|
9126
9344
|
metrics: extractTokenMetrics(result)
|
|
9127
9345
|
});
|
|
9128
9346
|
return result;
|
|
@@ -9146,10 +9364,10 @@ var wrapGenerateObject = (generateObject, options = {}) => {
|
|
|
9146
9364
|
);
|
|
9147
9365
|
};
|
|
9148
9366
|
};
|
|
9149
|
-
var
|
|
9150
|
-
|
|
9367
|
+
var makeStreamTextWrapper = (name, options, streamText, aiSDK) => {
|
|
9368
|
+
const wrapper = function(params) {
|
|
9151
9369
|
const span = startSpan({
|
|
9152
|
-
name
|
|
9370
|
+
name,
|
|
9153
9371
|
spanAttributes: {
|
|
9154
9372
|
type: "llm" /* LLM */
|
|
9155
9373
|
},
|
|
@@ -9171,6 +9389,7 @@ var wrapStreamText = (streamText, options = {}) => {
|
|
|
9171
9389
|
span,
|
|
9172
9390
|
() => streamText({
|
|
9173
9391
|
...params,
|
|
9392
|
+
model: wrapModel(params.model, aiSDK),
|
|
9174
9393
|
tools: wrapTools(params.tools),
|
|
9175
9394
|
onChunk: (chunk) => {
|
|
9176
9395
|
if (!receivedFirst) {
|
|
@@ -9181,10 +9400,10 @@ var wrapStreamText = (streamText, options = {}) => {
|
|
|
9181
9400
|
}
|
|
9182
9401
|
});
|
|
9183
9402
|
}
|
|
9184
|
-
_optionalChain([params, 'access',
|
|
9403
|
+
_optionalChain([params, 'access', _137 => _137.onChunk, 'optionalCall', _138 => _138(chunk)]);
|
|
9185
9404
|
},
|
|
9186
9405
|
onFinish: async (event) => {
|
|
9187
|
-
_optionalChain([params, 'access',
|
|
9406
|
+
_optionalChain([params, 'access', _139 => _139.onFinish, 'optionalCall', _140 => _140(event)]);
|
|
9188
9407
|
span.log({
|
|
9189
9408
|
output: await processOutput(event, options.denyOutputPaths),
|
|
9190
9409
|
metrics: extractTokenMetrics(event)
|
|
@@ -9192,7 +9411,7 @@ var wrapStreamText = (streamText, options = {}) => {
|
|
|
9192
9411
|
span.end();
|
|
9193
9412
|
},
|
|
9194
9413
|
onError: async (err) => {
|
|
9195
|
-
_optionalChain([params, 'access',
|
|
9414
|
+
_optionalChain([params, 'access', _141 => _141.onError, 'optionalCall', _142 => _142(err)]);
|
|
9196
9415
|
span.log({
|
|
9197
9416
|
error: serializeError(err)
|
|
9198
9417
|
});
|
|
@@ -9237,8 +9456,13 @@ var wrapStreamText = (streamText, options = {}) => {
|
|
|
9237
9456
|
throw error2;
|
|
9238
9457
|
}
|
|
9239
9458
|
};
|
|
9459
|
+
Object.defineProperty(wrapper, "name", { value: name, writable: false });
|
|
9460
|
+
return wrapper;
|
|
9240
9461
|
};
|
|
9241
|
-
var
|
|
9462
|
+
var wrapStreamText = (streamText, options = {}, aiSDK) => {
|
|
9463
|
+
return makeStreamTextWrapper("streamText", options, streamText, aiSDK);
|
|
9464
|
+
};
|
|
9465
|
+
var wrapStreamObject = (streamObject, options = {}, aiSDK) => {
|
|
9242
9466
|
return function wrappedStreamObject(params) {
|
|
9243
9467
|
const span = startSpan({
|
|
9244
9468
|
name: "streamObject",
|
|
@@ -9263,6 +9487,7 @@ var wrapStreamObject = (streamObject, options = {}) => {
|
|
|
9263
9487
|
span,
|
|
9264
9488
|
() => streamObject({
|
|
9265
9489
|
...params,
|
|
9490
|
+
model: wrapModel(params.model, aiSDK),
|
|
9266
9491
|
tools: wrapTools(params.tools),
|
|
9267
9492
|
onChunk: (chunk) => {
|
|
9268
9493
|
if (!receivedFirst) {
|
|
@@ -9273,10 +9498,10 @@ var wrapStreamObject = (streamObject, options = {}) => {
|
|
|
9273
9498
|
}
|
|
9274
9499
|
});
|
|
9275
9500
|
}
|
|
9276
|
-
_optionalChain([params, 'access',
|
|
9501
|
+
_optionalChain([params, 'access', _143 => _143.onChunk, 'optionalCall', _144 => _144(chunk)]);
|
|
9277
9502
|
},
|
|
9278
9503
|
onFinish: async (event) => {
|
|
9279
|
-
_optionalChain([params, 'access',
|
|
9504
|
+
_optionalChain([params, 'access', _145 => _145.onFinish, 'optionalCall', _146 => _146(event)]);
|
|
9280
9505
|
span.log({
|
|
9281
9506
|
output: await processOutput(event, options.denyOutputPaths),
|
|
9282
9507
|
metrics: extractTokenMetrics(event)
|
|
@@ -9284,7 +9509,7 @@ var wrapStreamObject = (streamObject, options = {}) => {
|
|
|
9284
9509
|
span.end();
|
|
9285
9510
|
},
|
|
9286
9511
|
onError: async (err) => {
|
|
9287
|
-
_optionalChain([params, 'access',
|
|
9512
|
+
_optionalChain([params, 'access', _147 => _147.onError, 'optionalCall', _148 => _148(err)]);
|
|
9288
9513
|
span.log({
|
|
9289
9514
|
error: serializeError(err)
|
|
9290
9515
|
});
|
|
@@ -9421,7 +9646,7 @@ var serializeError = (error2) => {
|
|
|
9421
9646
|
return String(error2);
|
|
9422
9647
|
};
|
|
9423
9648
|
var serializeModel = (model) => {
|
|
9424
|
-
return typeof model === "string" ? model : _optionalChain([model, 'optionalAccess',
|
|
9649
|
+
return typeof model === "string" ? model : _optionalChain([model, 'optionalAccess', _149 => _149.modelId]);
|
|
9425
9650
|
};
|
|
9426
9651
|
var isZodSchema = (value) => {
|
|
9427
9652
|
return value != null && typeof value === "object" && "_def" in value && typeof value._def === "object";
|
|
@@ -9470,8 +9695,12 @@ var processInputAttachments2 = (input) => {
|
|
|
9470
9695
|
if (input.messages && Array.isArray(input.messages)) {
|
|
9471
9696
|
processed.messages = input.messages.map(processMessage);
|
|
9472
9697
|
}
|
|
9473
|
-
if (input.prompt && typeof input.prompt === "object"
|
|
9474
|
-
|
|
9698
|
+
if (input.prompt && typeof input.prompt === "object") {
|
|
9699
|
+
if (Array.isArray(input.prompt)) {
|
|
9700
|
+
processed.prompt = input.prompt.map(processMessage);
|
|
9701
|
+
} else {
|
|
9702
|
+
processed.prompt = processPromptContent(input.prompt);
|
|
9703
|
+
}
|
|
9475
9704
|
}
|
|
9476
9705
|
if (input.tools) {
|
|
9477
9706
|
processed.tools = processTools(input.tools);
|
|
@@ -9564,7 +9793,7 @@ var convertImageToAttachment = (image, explicitMimeType) => {
|
|
|
9564
9793
|
try {
|
|
9565
9794
|
if (typeof image === "string" && image.startsWith("data:")) {
|
|
9566
9795
|
const [mimeTypeSection, base64Data] = image.split(",");
|
|
9567
|
-
const mimeType = _optionalChain([mimeTypeSection, 'access',
|
|
9796
|
+
const mimeType = _optionalChain([mimeTypeSection, 'access', _150 => _150.match, 'call', _151 => _151(/data:(.*?);/), 'optionalAccess', _152 => _152[1]]);
|
|
9568
9797
|
if (mimeType && base64Data) {
|
|
9569
9798
|
const blob = convertDataToBlob(base64Data, mimeType);
|
|
9570
9799
|
if (blob) {
|
|
@@ -9641,6 +9870,7 @@ var extractGetterValues = (obj) => {
|
|
|
9641
9870
|
const getterValues = {};
|
|
9642
9871
|
const getterNames = [
|
|
9643
9872
|
"text",
|
|
9873
|
+
"object",
|
|
9644
9874
|
"finishReason",
|
|
9645
9875
|
"usage",
|
|
9646
9876
|
"toolCalls",
|
|
@@ -9722,7 +9952,7 @@ var convertFileToAttachment = (file, index) => {
|
|
|
9722
9952
|
};
|
|
9723
9953
|
function extractTokenMetrics(result) {
|
|
9724
9954
|
const metrics = {};
|
|
9725
|
-
const usage = _optionalChain([result, 'optionalAccess',
|
|
9955
|
+
const usage = _optionalChain([result, 'optionalAccess', _153 => _153.usage]);
|
|
9726
9956
|
if (!usage) {
|
|
9727
9957
|
return metrics;
|
|
9728
9958
|
}
|
|
@@ -9842,7 +10072,7 @@ var omit = (obj, paths) => {
|
|
|
9842
10072
|
// src/wrappers/ai-sdk/deprecated/wrapAISDKModel.ts
|
|
9843
10073
|
function wrapAISDKModel(model) {
|
|
9844
10074
|
const m = model;
|
|
9845
|
-
if (_optionalChain([m, 'optionalAccess',
|
|
10075
|
+
if (_optionalChain([m, 'optionalAccess', _154 => _154.specificationVersion]) === "v1" && typeof _optionalChain([m, 'optionalAccess', _155 => _155.provider]) === "string" && typeof _optionalChain([m, 'optionalAccess', _156 => _156.modelId]) === "string") {
|
|
9846
10076
|
return new BraintrustLanguageModelWrapper(m);
|
|
9847
10077
|
} else {
|
|
9848
10078
|
console.warn("Unsupported AI SDK model. Not wrapping.");
|
|
@@ -9899,10 +10129,10 @@ var BraintrustLanguageModelWrapper = class {
|
|
|
9899
10129
|
metrics: {
|
|
9900
10130
|
time_to_first_token: getCurrentUnixTimestamp() - startTime,
|
|
9901
10131
|
tokens: !isEmpty(ret.usage) ? ret.usage.promptTokens + ret.usage.completionTokens : void 0,
|
|
9902
|
-
prompt_tokens: _optionalChain([ret, 'access',
|
|
9903
|
-
completion_tokens: _optionalChain([ret, 'access',
|
|
10132
|
+
prompt_tokens: _optionalChain([ret, 'access', _157 => _157.usage, 'optionalAccess', _158 => _158.promptTokens]),
|
|
10133
|
+
completion_tokens: _optionalChain([ret, 'access', _159 => _159.usage, 'optionalAccess', _160 => _160.completionTokens]),
|
|
9904
10134
|
cached: parseCachedHeader(
|
|
9905
|
-
_nullishCoalesce(_optionalChain([ret, 'access',
|
|
10135
|
+
_nullishCoalesce(_optionalChain([ret, 'access', _161 => _161.rawResponse, 'optionalAccess', _162 => _162.headers, 'optionalAccess', _163 => _163[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _164 => _164.rawResponse, 'optionalAccess', _165 => _165.headers, 'optionalAccess', _166 => _166[LEGACY_CACHED_HEADER]])))
|
|
9906
10136
|
)
|
|
9907
10137
|
}
|
|
9908
10138
|
});
|
|
@@ -9994,10 +10224,10 @@ var BraintrustLanguageModelWrapper = class {
|
|
|
9994
10224
|
metrics: {
|
|
9995
10225
|
time_to_first_token,
|
|
9996
10226
|
tokens: !isEmpty(usage) ? usage.promptTokens + usage.completionTokens : void 0,
|
|
9997
|
-
prompt_tokens: _optionalChain([usage, 'optionalAccess',
|
|
9998
|
-
completion_tokens: _optionalChain([usage, 'optionalAccess',
|
|
10227
|
+
prompt_tokens: _optionalChain([usage, 'optionalAccess', _167 => _167.promptTokens]),
|
|
10228
|
+
completion_tokens: _optionalChain([usage, 'optionalAccess', _168 => _168.completionTokens]),
|
|
9999
10229
|
cached: parseCachedHeader(
|
|
10000
|
-
_nullishCoalesce(_optionalChain([ret, 'access',
|
|
10230
|
+
_nullishCoalesce(_optionalChain([ret, 'access', _169 => _169.rawResponse, 'optionalAccess', _170 => _170.headers, 'optionalAccess', _171 => _171[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _172 => _172.rawResponse, 'optionalAccess', _173 => _173.headers, 'optionalAccess', _174 => _174[LEGACY_CACHED_HEADER]])))
|
|
10001
10231
|
)
|
|
10002
10232
|
}
|
|
10003
10233
|
});
|
|
@@ -10041,7 +10271,7 @@ function postProcessPrompt(prompt) {
|
|
|
10041
10271
|
return [
|
|
10042
10272
|
{
|
|
10043
10273
|
role: "assistant",
|
|
10044
|
-
content: _optionalChain([textPart, 'optionalAccess',
|
|
10274
|
+
content: _optionalChain([textPart, 'optionalAccess', _175 => _175.text]),
|
|
10045
10275
|
...toolCallParts.length > 0 ? {
|
|
10046
10276
|
tool_calls: toolCallParts.map((part) => ({
|
|
10047
10277
|
id: part.toolCallId,
|
|
@@ -10134,23 +10364,23 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
|
|
|
10134
10364
|
|
|
10135
10365
|
// src/wrappers/ai-sdk/deprecated/BraintrustMiddleware.ts
|
|
10136
10366
|
function detectProviderFromResult(result) {
|
|
10137
|
-
if (!_optionalChain([result, 'optionalAccess',
|
|
10367
|
+
if (!_optionalChain([result, 'optionalAccess', _176 => _176.providerMetadata])) {
|
|
10138
10368
|
return void 0;
|
|
10139
10369
|
}
|
|
10140
10370
|
const keys = Object.keys(result.providerMetadata);
|
|
10141
|
-
return _optionalChain([keys, 'optionalAccess',
|
|
10371
|
+
return _optionalChain([keys, 'optionalAccess', _177 => _177.at, 'call', _178 => _178(0)]);
|
|
10142
10372
|
}
|
|
10143
10373
|
function extractModelFromResult(result) {
|
|
10144
|
-
if (_optionalChain([result, 'optionalAccess',
|
|
10374
|
+
if (_optionalChain([result, 'optionalAccess', _179 => _179.response, 'optionalAccess', _180 => _180.modelId])) {
|
|
10145
10375
|
return result.response.modelId;
|
|
10146
10376
|
}
|
|
10147
|
-
if (_optionalChain([result, 'optionalAccess',
|
|
10377
|
+
if (_optionalChain([result, 'optionalAccess', _181 => _181.request, 'optionalAccess', _182 => _182.body, 'optionalAccess', _183 => _183.model])) {
|
|
10148
10378
|
return result.request.body.model;
|
|
10149
10379
|
}
|
|
10150
10380
|
return void 0;
|
|
10151
10381
|
}
|
|
10152
10382
|
function extractModelFromWrapGenerateCallback(model) {
|
|
10153
|
-
return _optionalChain([model, 'optionalAccess',
|
|
10383
|
+
return _optionalChain([model, 'optionalAccess', _184 => _184.modelId]);
|
|
10154
10384
|
}
|
|
10155
10385
|
function camelToSnake(str) {
|
|
10156
10386
|
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
@@ -10195,7 +10425,7 @@ function normalizeUsageMetrics(usage, provider, providerMetadata) {
|
|
|
10195
10425
|
metrics.prompt_cached_tokens = cachedInputTokens;
|
|
10196
10426
|
}
|
|
10197
10427
|
if (provider === "anthropic") {
|
|
10198
|
-
const anthropicMetadata = _optionalChain([providerMetadata, 'optionalAccess',
|
|
10428
|
+
const anthropicMetadata = _optionalChain([providerMetadata, 'optionalAccess', _185 => _185.anthropic]);
|
|
10199
10429
|
if (anthropicMetadata) {
|
|
10200
10430
|
const cacheReadTokens = getNumberProperty(anthropicMetadata.usage, "cache_read_input_tokens") || 0;
|
|
10201
10431
|
const cacheCreationTokens = getNumberProperty(
|
|
@@ -10221,7 +10451,7 @@ function buildAssistantOutputWithToolCalls(result, toolCalls) {
|
|
|
10221
10451
|
{
|
|
10222
10452
|
index: 0,
|
|
10223
10453
|
logprobs: null,
|
|
10224
|
-
finish_reason: _nullishCoalesce(normalizeFinishReason(_optionalChain([result, 'optionalAccess',
|
|
10454
|
+
finish_reason: _nullishCoalesce(normalizeFinishReason(_optionalChain([result, 'optionalAccess', _186 => _186.finishReason])), () => ( (toolCalls.length ? "tool_calls" : void 0))),
|
|
10225
10455
|
message: {
|
|
10226
10456
|
role: "assistant",
|
|
10227
10457
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
@@ -10234,7 +10464,7 @@ function extractToolCallsFromSteps(steps) {
|
|
|
10234
10464
|
if (!Array.isArray(steps)) return toolCalls;
|
|
10235
10465
|
let idx = 0;
|
|
10236
10466
|
for (const step of steps) {
|
|
10237
|
-
const blocks = _optionalChain([step, 'optionalAccess',
|
|
10467
|
+
const blocks = _optionalChain([step, 'optionalAccess', _187 => _187.content]);
|
|
10238
10468
|
if (!Array.isArray(blocks)) continue;
|
|
10239
10469
|
for (const block of blocks) {
|
|
10240
10470
|
if (block && typeof block === "object" && block.type === "tool-call") {
|
|
@@ -10257,7 +10487,7 @@ function extractToolCallsFromBlocks(blocks) {
|
|
|
10257
10487
|
return extractToolCallsFromSteps([{ content: blocks }]);
|
|
10258
10488
|
}
|
|
10259
10489
|
function extractInput(params) {
|
|
10260
|
-
return _nullishCoalesce(_nullishCoalesce(_optionalChain([params, 'optionalAccess',
|
|
10490
|
+
return _nullishCoalesce(_nullishCoalesce(_optionalChain([params, 'optionalAccess', _188 => _188.prompt]), () => ( _optionalChain([params, 'optionalAccess', _189 => _189.messages]))), () => ( _optionalChain([params, 'optionalAccess', _190 => _190.system])));
|
|
10261
10491
|
}
|
|
10262
10492
|
var V2_EXCLUDE_KEYS = /* @__PURE__ */ new Set([
|
|
10263
10493
|
"prompt",
|
|
@@ -10281,16 +10511,16 @@ function BraintrustMiddleware(config = {}) {
|
|
|
10281
10511
|
const rawInput = extractInput(params);
|
|
10282
10512
|
const processedInput = processInputAttachments(rawInput);
|
|
10283
10513
|
const spanArgs = {
|
|
10284
|
-
name: _optionalChain([config, 'access',
|
|
10514
|
+
name: _optionalChain([config, 'access', _191 => _191.spanInfo, 'optionalAccess', _192 => _192.name]) || "ai-sdk.doGenerate",
|
|
10285
10515
|
spanAttributes: {
|
|
10286
10516
|
type: "llm" /* LLM */,
|
|
10287
|
-
..._optionalChain([config, 'access',
|
|
10517
|
+
..._optionalChain([config, 'access', _193 => _193.spanInfo, 'optionalAccess', _194 => _194.spanAttributes]) || {}
|
|
10288
10518
|
},
|
|
10289
10519
|
event: {
|
|
10290
10520
|
input: processedInput,
|
|
10291
10521
|
metadata: {
|
|
10292
10522
|
...extractModelParameters(params, V2_EXCLUDE_KEYS),
|
|
10293
|
-
..._optionalChain([config, 'access',
|
|
10523
|
+
..._optionalChain([config, 'access', _195 => _195.spanInfo, 'optionalAccess', _196 => _196.metadata]) || {}
|
|
10294
10524
|
}
|
|
10295
10525
|
}
|
|
10296
10526
|
};
|
|
@@ -10316,12 +10546,12 @@ function BraintrustMiddleware(config = {}) {
|
|
|
10316
10546
|
metadata.model = modelId;
|
|
10317
10547
|
}
|
|
10318
10548
|
}
|
|
10319
|
-
let toolCalls = extractToolCallsFromSteps(_optionalChain([result, 'optionalAccess',
|
|
10549
|
+
let toolCalls = extractToolCallsFromSteps(_optionalChain([result, 'optionalAccess', _197 => _197.steps]));
|
|
10320
10550
|
if (!toolCalls || toolCalls.length === 0) {
|
|
10321
|
-
toolCalls = extractToolCallsFromBlocks(_optionalChain([result, 'optionalAccess',
|
|
10551
|
+
toolCalls = extractToolCallsFromBlocks(_optionalChain([result, 'optionalAccess', _198 => _198.content]));
|
|
10322
10552
|
}
|
|
10323
10553
|
span.log({
|
|
10324
|
-
output: toolCalls.length > 0 ? buildAssistantOutputWithToolCalls(result, toolCalls) : _optionalChain([result, 'optionalAccess',
|
|
10554
|
+
output: toolCalls.length > 0 ? buildAssistantOutputWithToolCalls(result, toolCalls) : _optionalChain([result, 'optionalAccess', _199 => _199.content]),
|
|
10325
10555
|
metadata,
|
|
10326
10556
|
metrics: normalizeUsageMetrics(
|
|
10327
10557
|
result.usage,
|
|
@@ -10343,16 +10573,16 @@ function BraintrustMiddleware(config = {}) {
|
|
|
10343
10573
|
const rawInput = extractInput(params);
|
|
10344
10574
|
const processedInput = processInputAttachments(rawInput);
|
|
10345
10575
|
const spanArgs = {
|
|
10346
|
-
name: _optionalChain([config, 'access',
|
|
10576
|
+
name: _optionalChain([config, 'access', _200 => _200.spanInfo, 'optionalAccess', _201 => _201.name]) || "ai-sdk.doStream",
|
|
10347
10577
|
spanAttributes: {
|
|
10348
10578
|
type: "llm" /* LLM */,
|
|
10349
|
-
..._optionalChain([config, 'access',
|
|
10579
|
+
..._optionalChain([config, 'access', _202 => _202.spanInfo, 'optionalAccess', _203 => _203.spanAttributes]) || {}
|
|
10350
10580
|
},
|
|
10351
10581
|
event: {
|
|
10352
10582
|
input: processedInput,
|
|
10353
10583
|
metadata: {
|
|
10354
10584
|
...extractModelParameters(params, V2_EXCLUDE_KEYS),
|
|
10355
|
-
..._optionalChain([config, 'access',
|
|
10585
|
+
..._optionalChain([config, 'access', _204 => _204.spanInfo, 'optionalAccess', _205 => _205.metadata]) || {}
|
|
10356
10586
|
}
|
|
10357
10587
|
}
|
|
10358
10588
|
};
|
|
@@ -10624,9 +10854,9 @@ function streamNextProxy(stream, sspan) {
|
|
|
10624
10854
|
return result;
|
|
10625
10855
|
}
|
|
10626
10856
|
const item = result.value;
|
|
10627
|
-
switch (_optionalChain([item, 'optionalAccess',
|
|
10857
|
+
switch (_optionalChain([item, 'optionalAccess', _206 => _206.type])) {
|
|
10628
10858
|
case "message_start":
|
|
10629
|
-
const msg = _optionalChain([item, 'optionalAccess',
|
|
10859
|
+
const msg = _optionalChain([item, 'optionalAccess', _207 => _207.message]);
|
|
10630
10860
|
if (msg) {
|
|
10631
10861
|
const event = parseEventFromMessage(msg);
|
|
10632
10862
|
totals = { ...totals, ...event.metrics };
|
|
@@ -10639,20 +10869,20 @@ function streamNextProxy(stream, sspan) {
|
|
|
10639
10869
|
}
|
|
10640
10870
|
break;
|
|
10641
10871
|
case "content_block_delta":
|
|
10642
|
-
if (_optionalChain([item, 'access',
|
|
10643
|
-
const text = _optionalChain([item, 'optionalAccess',
|
|
10872
|
+
if (_optionalChain([item, 'access', _208 => _208.delta, 'optionalAccess', _209 => _209.type]) === "text_delta") {
|
|
10873
|
+
const text = _optionalChain([item, 'optionalAccess', _210 => _210.delta, 'optionalAccess', _211 => _211.text]);
|
|
10644
10874
|
if (text) {
|
|
10645
10875
|
deltas.push(text);
|
|
10646
10876
|
}
|
|
10647
10877
|
}
|
|
10648
10878
|
break;
|
|
10649
10879
|
case "message_delta":
|
|
10650
|
-
const usage = _optionalChain([item, 'optionalAccess',
|
|
10880
|
+
const usage = _optionalChain([item, 'optionalAccess', _212 => _212.usage]);
|
|
10651
10881
|
if (usage) {
|
|
10652
10882
|
const metrics = parseMetricsFromUsage2(usage);
|
|
10653
10883
|
totals = { ...totals, ...metrics };
|
|
10654
10884
|
}
|
|
10655
|
-
const delta = _optionalChain([item, 'optionalAccess',
|
|
10885
|
+
const delta = _optionalChain([item, 'optionalAccess', _213 => _213.delta]);
|
|
10656
10886
|
if (delta) {
|
|
10657
10887
|
metadata = { ...metadata, ...delta };
|
|
10658
10888
|
}
|
|
@@ -10665,7 +10895,7 @@ function streamNextProxy(stream, sspan) {
|
|
|
10665
10895
|
}
|
|
10666
10896
|
function parseEventFromMessage(message) {
|
|
10667
10897
|
const output = message ? { role: message.role, content: message.content } : null;
|
|
10668
|
-
const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess',
|
|
10898
|
+
const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _214 => _214.usage]));
|
|
10669
10899
|
const metas = ["stop_reason", "stop_sequence"];
|
|
10670
10900
|
const metadata = {};
|
|
10671
10901
|
for (const m of metas) {
|
|
@@ -10812,7 +11042,7 @@ function wrapClaudeAgentQuery(queryFn, defaultThis) {
|
|
|
10812
11042
|
finalResults.push(finalMessageContent);
|
|
10813
11043
|
}
|
|
10814
11044
|
const lastMessage = currentMessages[currentMessages.length - 1];
|
|
10815
|
-
if (_optionalChain([lastMessage, 'optionalAccess',
|
|
11045
|
+
if (_optionalChain([lastMessage, 'optionalAccess', _215 => _215.message, 'optionalAccess', _216 => _216.usage])) {
|
|
10816
11046
|
const outputTokens = getNumberProperty2(lastMessage.message.usage, "output_tokens") || 0;
|
|
10817
11047
|
accumulatedOutputTokens += outputTokens;
|
|
10818
11048
|
}
|
|
@@ -10827,20 +11057,20 @@ function wrapClaudeAgentQuery(queryFn, defaultThis) {
|
|
|
10827
11057
|
);
|
|
10828
11058
|
for await (const message of generator) {
|
|
10829
11059
|
const currentTime = getCurrentUnixTimestamp();
|
|
10830
|
-
const messageId = _optionalChain([message, 'access',
|
|
11060
|
+
const messageId = _optionalChain([message, 'access', _217 => _217.message, 'optionalAccess', _218 => _218.id]);
|
|
10831
11061
|
if (messageId && messageId !== currentMessageId) {
|
|
10832
11062
|
await createLLMSpan();
|
|
10833
11063
|
currentMessageId = messageId;
|
|
10834
11064
|
currentMessageStartTime = currentTime;
|
|
10835
11065
|
}
|
|
10836
|
-
if (message.type === "assistant" && _optionalChain([message, 'access',
|
|
11066
|
+
if (message.type === "assistant" && _optionalChain([message, 'access', _219 => _219.message, 'optionalAccess', _220 => _220.usage])) {
|
|
10837
11067
|
currentMessages.push(message);
|
|
10838
11068
|
}
|
|
10839
11069
|
if (message.type === "result" && message.usage) {
|
|
10840
11070
|
finalUsageMetrics = _extractUsageFromMessage(message);
|
|
10841
11071
|
if (currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
|
|
10842
11072
|
const lastMessage = currentMessages[currentMessages.length - 1];
|
|
10843
|
-
if (_optionalChain([lastMessage, 'optionalAccess',
|
|
11073
|
+
if (_optionalChain([lastMessage, 'optionalAccess', _221 => _221.message, 'optionalAccess', _222 => _222.usage])) {
|
|
10844
11074
|
const adjustedTokens = finalUsageMetrics.completion_tokens - accumulatedOutputTokens;
|
|
10845
11075
|
if (adjustedTokens >= 0) {
|
|
10846
11076
|
lastMessage.message.usage.output_tokens = adjustedTokens;
|
|
@@ -10921,7 +11151,7 @@ function _extractUsageFromMessage(message) {
|
|
|
10921
11151
|
const metrics = {};
|
|
10922
11152
|
let usage;
|
|
10923
11153
|
if (message.type === "assistant") {
|
|
10924
|
-
usage = _optionalChain([message, 'access',
|
|
11154
|
+
usage = _optionalChain([message, 'access', _223 => _223.message, 'optionalAccess', _224 => _224.usage]);
|
|
10925
11155
|
} else if (message.type === "result") {
|
|
10926
11156
|
usage = message.usage;
|
|
10927
11157
|
}
|
|
@@ -10953,14 +11183,14 @@ function _extractUsageFromMessage(message) {
|
|
|
10953
11183
|
async function _createLLMSpanForMessages(messages, prompt, conversationHistory, options, startTime, parentSpan) {
|
|
10954
11184
|
if (messages.length === 0) return void 0;
|
|
10955
11185
|
const lastMessage = messages[messages.length - 1];
|
|
10956
|
-
if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access',
|
|
11186
|
+
if (lastMessage.type !== "assistant" || !_optionalChain([lastMessage, 'access', _225 => _225.message, 'optionalAccess', _226 => _226.usage])) {
|
|
10957
11187
|
return void 0;
|
|
10958
11188
|
}
|
|
10959
11189
|
const model = lastMessage.message.model || options.model;
|
|
10960
11190
|
const usage = _extractUsageFromMessage(lastMessage);
|
|
10961
11191
|
const input = _buildLLMInput(prompt, conversationHistory);
|
|
10962
11192
|
const outputs = messages.map(
|
|
10963
|
-
(m) => _optionalChain([m, 'access',
|
|
11193
|
+
(m) => _optionalChain([m, 'access', _227 => _227.message, 'optionalAccess', _228 => _228.content]) && _optionalChain([m, 'access', _229 => _229.message, 'optionalAccess', _230 => _230.role]) ? { content: m.message.content, role: m.message.role } : void 0
|
|
10964
11194
|
).filter((c) => c !== void 0);
|
|
10965
11195
|
await traced(
|
|
10966
11196
|
(llmSpan) => {
|
|
@@ -10980,7 +11210,7 @@ async function _createLLMSpanForMessages(messages, prompt, conversationHistory,
|
|
|
10980
11210
|
parent: parentSpan
|
|
10981
11211
|
}
|
|
10982
11212
|
);
|
|
10983
|
-
return _optionalChain([lastMessage, 'access',
|
|
11213
|
+
return _optionalChain([lastMessage, 'access', _231 => _231.message, 'optionalAccess', _232 => _232.content]) && _optionalChain([lastMessage, 'access', _233 => _233.message, 'optionalAccess', _234 => _234.role]) ? { content: lastMessage.message.content, role: lastMessage.message.role } : void 0;
|
|
10984
11214
|
}
|
|
10985
11215
|
function wrapClaudeAgentSDK(sdk) {
|
|
10986
11216
|
const cache = /* @__PURE__ */ new Map();
|
|
@@ -11270,7 +11500,7 @@ function serializePart(part) {
|
|
|
11270
11500
|
return part;
|
|
11271
11501
|
}
|
|
11272
11502
|
function serializeTools(params) {
|
|
11273
|
-
if (!_optionalChain([params, 'access',
|
|
11503
|
+
if (!_optionalChain([params, 'access', _235 => _235.config, 'optionalAccess', _236 => _236.tools])) {
|
|
11274
11504
|
return null;
|
|
11275
11505
|
}
|
|
11276
11506
|
try {
|
|
@@ -11353,7 +11583,7 @@ function aggregateGenerateContentChunks(chunks, start, firstTokenTime) {
|
|
|
11353
11583
|
}
|
|
11354
11584
|
if (chunk.candidates && Array.isArray(chunk.candidates)) {
|
|
11355
11585
|
for (const candidate of chunk.candidates) {
|
|
11356
|
-
if (_optionalChain([candidate, 'access',
|
|
11586
|
+
if (_optionalChain([candidate, 'access', _237 => _237.content, 'optionalAccess', _238 => _238.parts])) {
|
|
11357
11587
|
for (const part of candidate.content.parts) {
|
|
11358
11588
|
if (part.text !== void 0) {
|
|
11359
11589
|
if (part.thought) {
|
|
@@ -11384,7 +11614,7 @@ function aggregateGenerateContentChunks(chunks, start, firstTokenTime) {
|
|
|
11384
11614
|
parts.push({ text });
|
|
11385
11615
|
}
|
|
11386
11616
|
parts.push(...otherParts);
|
|
11387
|
-
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess',
|
|
11617
|
+
if (parts.length > 0 && _optionalChain([lastResponse, 'optionalAccess', _239 => _239.candidates])) {
|
|
11388
11618
|
const candidates = [];
|
|
11389
11619
|
for (const candidate of lastResponse.candidates) {
|
|
11390
11620
|
const candidateDict = {
|
|
@@ -12769,6 +12999,7 @@ var waterfall$1 = awaitify(waterfall);
|
|
|
12769
12999
|
|
|
12770
13000
|
// src/framework.ts
|
|
12771
13001
|
var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk);
|
|
13002
|
+
var _termilink = require('termi-link');
|
|
12772
13003
|
var _boxen = require('boxen'); var _boxen2 = _interopRequireDefault(_boxen);
|
|
12773
13004
|
var _pluralize = require('pluralize'); var _pluralize2 = _interopRequireDefault(_pluralize);
|
|
12774
13005
|
var _clitable3 = require('cli-table3'); var _clitable32 = _interopRequireDefault(_clitable3);
|
|
@@ -13541,10 +13772,10 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
13541
13772
|
span,
|
|
13542
13773
|
parameters: _nullishCoalesce(parameters, () => ( {})),
|
|
13543
13774
|
reportProgress: (event) => {
|
|
13544
|
-
_optionalChain([stream, 'optionalCall',
|
|
13775
|
+
_optionalChain([stream, 'optionalCall', _240 => _240({
|
|
13545
13776
|
...event,
|
|
13546
13777
|
id: rootSpan.id,
|
|
13547
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
13778
|
+
origin: _optionalChain([baseEvent, 'access', _241 => _241.event, 'optionalAccess', _242 => _242.origin]),
|
|
13548
13779
|
name: evaluator.evalName,
|
|
13549
13780
|
object_type: "task"
|
|
13550
13781
|
})]);
|
|
@@ -13699,7 +13930,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
13699
13930
|
metadata,
|
|
13700
13931
|
scores: mergedScores,
|
|
13701
13932
|
error: error2,
|
|
13702
|
-
origin: _optionalChain([baseEvent, 'access',
|
|
13933
|
+
origin: _optionalChain([baseEvent, 'access', _243 => _243.event, 'optionalAccess', _244 => _244.origin])
|
|
13703
13934
|
});
|
|
13704
13935
|
}
|
|
13705
13936
|
};
|
|
@@ -13732,7 +13963,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
13732
13963
|
break;
|
|
13733
13964
|
}
|
|
13734
13965
|
scheduledTrials++;
|
|
13735
|
-
_optionalChain([progressReporter, 'access',
|
|
13966
|
+
_optionalChain([progressReporter, 'access', _245 => _245.setTotal, 'optionalCall', _246 => _246(evaluator.evalName, scheduledTrials)]);
|
|
13736
13967
|
q.push({ datum, trialIndex });
|
|
13737
13968
|
}
|
|
13738
13969
|
}
|
|
@@ -13994,7 +14225,11 @@ function formatExperimentSummary(summary) {
|
|
|
13994
14225
|
tableParts.push(combinedTable.toString());
|
|
13995
14226
|
}
|
|
13996
14227
|
const content = [comparisonLine, ...tableParts].filter(Boolean).join("\n");
|
|
13997
|
-
const footer = summary.experimentUrl ?
|
|
14228
|
+
const footer = summary.experimentUrl ? _termilink.terminalLink.call(void 0,
|
|
14229
|
+
`View results for ${summary.experimentName}`,
|
|
14230
|
+
summary.experimentUrl,
|
|
14231
|
+
{ fallback: () => `See results at ${summary.experimentUrl}` }
|
|
14232
|
+
) : "";
|
|
13998
14233
|
const boxContent = [content, footer].filter(Boolean).join("\n\n");
|
|
13999
14234
|
return "\n" + _boxen2.default.call(void 0, boxContent, {
|
|
14000
14235
|
title: _chalk2.default.gray("Experiment summary"),
|
|
@@ -14158,4 +14393,4 @@ var index_default = exports_node_exports;
|
|
|
14158
14393
|
|
|
14159
14394
|
|
|
14160
14395
|
|
|
14161
|
-
exports.Attachment = Attachment; exports.BaseAttachment = BaseAttachment; exports.BaseExperiment = BaseExperiment; exports.BraintrustMiddleware = BraintrustMiddleware; exports.BraintrustState = BraintrustState; exports.BraintrustStream = BraintrustStream; exports.CodeFunction = CodeFunction; exports.CodePrompt = CodePrompt; exports.ContextManager = ContextManager; exports.Dataset = Dataset2; exports.ERR_PERMALINK = ERR_PERMALINK; exports.Eval = Eval; exports.EvalResultWithSummary = EvalResultWithSummary; exports.Experiment = Experiment2; exports.ExternalAttachment = ExternalAttachment; exports.FailedHTTPResponse = FailedHTTPResponse; exports.IDGenerator = IDGenerator; exports.
|
|
14396
|
+
exports.Attachment = Attachment; exports.BaseAttachment = BaseAttachment; exports.BaseExperiment = BaseExperiment; exports.BraintrustMiddleware = BraintrustMiddleware; exports.BraintrustState = BraintrustState; exports.BraintrustStream = BraintrustStream; exports.CodeFunction = CodeFunction; exports.CodePrompt = CodePrompt; exports.ContextManager = ContextManager; exports.DEFAULT_FETCH_BATCH_SIZE = DEFAULT_FETCH_BATCH_SIZE; exports.Dataset = Dataset2; exports.ERR_PERMALINK = ERR_PERMALINK; exports.Eval = Eval; exports.EvalResultWithSummary = EvalResultWithSummary; exports.Experiment = Experiment2; exports.ExternalAttachment = ExternalAttachment; exports.FailedHTTPResponse = FailedHTTPResponse; exports.IDGenerator = IDGenerator; exports.JSONAttachment = JSONAttachment; exports.LEGACY_CACHED_HEADER = LEGACY_CACHED_HEADER; exports.LazyValue = LazyValue; exports.Logger = Logger; exports.LoginInvalidOrgError = LoginInvalidOrgError; exports.NOOP_SPAN = NOOP_SPAN; exports.NOOP_SPAN_PERMALINK = NOOP_SPAN_PERMALINK; exports.NoopSpan = NoopSpan; exports.Project = Project2; exports.ProjectNameIdMap = ProjectNameIdMap; exports.Prompt = Prompt2; exports.PromptBuilder = PromptBuilder; exports.ReadonlyAttachment = ReadonlyAttachment; exports.ReadonlyExperiment = ReadonlyExperiment; exports.Reporter = Reporter; exports.ScorerBuilder = ScorerBuilder; exports.SpanImpl = SpanImpl; exports.TestBackgroundLogger = TestBackgroundLogger; exports.ToolBuilder = ToolBuilder; exports.UUIDGenerator = UUIDGenerator; exports.X_CACHED_HEADER = X_CACHED_HEADER; exports._exportsForTestingOnly = _exportsForTestingOnly; exports._internalGetGlobalState = _internalGetGlobalState; exports._internalSetInitialState = _internalSetInitialState; exports.braintrustStreamChunkSchema = braintrustStreamChunkSchema; exports.buildLocalSummary = buildLocalSummary; exports.createFinalValuePassThroughStream = createFinalValuePassThroughStream; exports.currentExperiment = currentExperiment; exports.currentLogger = currentLogger; exports.currentSpan = currentSpan; exports.deepCopyEvent = deepCopyEvent; exports.default = index_default; exports.defaultErrorScoreHandler = defaultErrorScoreHandler; exports.deserializePlainStringAsJSON = deserializePlainStringAsJSON; exports.devNullWritableStream = devNullWritableStream; exports.evaluatorDefinitionSchema = evaluatorDefinitionSchema; exports.evaluatorDefinitionsSchema = evaluatorDefinitionsSchema; exports.flush = flush; exports.getContextManager = getContextManager; exports.getIdGenerator = getIdGenerator; exports.getPromptVersions = getPromptVersions; exports.getSpanParentObject = getSpanParentObject; exports.graph = graph_framework_exports; exports.init = init; exports.initDataset = initDataset; exports.initExperiment = initExperiment; exports.initFunction = initFunction; exports.initLogger = initLogger; exports.invoke = invoke; exports.loadPrompt = loadPrompt; exports.log = log; exports.logError = logError; exports.login = login; exports.loginToState = loginToState; exports.newId = newId; exports.parseCachedHeader = parseCachedHeader; exports.permalink = permalink; exports.projects = projects; exports.promptContentsSchema = promptContentsSchema; exports.promptDefinitionSchema = promptDefinitionSchema; exports.promptDefinitionToPromptData = promptDefinitionToPromptData; exports.promptDefinitionWithToolsSchema = promptDefinitionWithToolsSchema; exports.renderMessage = renderMessage; exports.renderPromptParams = renderPromptParams; exports.reportFailures = reportFailures; exports.runEvaluator = runEvaluator; exports.setFetch = setFetch; exports.setMaskingFunction = setMaskingFunction; exports.spanComponentsToObjectId = spanComponentsToObjectId; exports.startSpan = startSpan; exports.summarize = summarize; exports.toolFunctionDefinitionSchema = ToolFunctionDefinition; exports.traceable = traceable; exports.traced = traced; exports.updateSpan = updateSpan; exports.withCurrent = withCurrent; exports.withDataset = withDataset; exports.withExperiment = withExperiment; exports.withLogger = withLogger; exports.withParent = withParent; exports.wrapAISDK = wrapAISDK; exports.wrapAISDKModel = wrapAISDKModel; exports.wrapAnthropic = wrapAnthropic; exports.wrapClaudeAgentSDK = wrapClaudeAgentSDK; exports.wrapGoogleGenAI = wrapGoogleGenAI; exports.wrapMastraAgent = wrapMastraAgent; exports.wrapOpenAI = wrapOpenAI; exports.wrapOpenAIv4 = wrapOpenAIv4; exports.wrapTraced = wrapTraced;
|