braintrust 0.2.1 → 0.2.3
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 +24 -12
- package/dev/dist/index.d.ts +24 -12
- package/dev/dist/index.js +1 -1
- package/dev/dist/index.mjs +1 -1
- package/dist/browser.d.mts +115 -45
- package/dist/browser.d.ts +115 -45
- package/dist/browser.js +259 -45
- package/dist/browser.mjs +217 -3
- package/dist/cli.js +74 -20
- package/dist/index.d.mts +131 -51
- package/dist/index.d.ts +131 -51
- package/dist/index.js +311 -79
- package/dist/index.mjs +246 -14
- package/package.json +2 -2
package/dist/browser.js
CHANGED
|
@@ -2715,6 +2715,7 @@ async function loadPrompt({
|
|
|
2715
2715
|
projectId,
|
|
2716
2716
|
slug,
|
|
2717
2717
|
version,
|
|
2718
|
+
environment,
|
|
2718
2719
|
id,
|
|
2719
2720
|
defaults,
|
|
2720
2721
|
noTrace = false,
|
|
@@ -2725,6 +2726,11 @@ async function loadPrompt({
|
|
|
2725
2726
|
forceLogin,
|
|
2726
2727
|
state: stateArg
|
|
2727
2728
|
}) {
|
|
2729
|
+
if (version && environment) {
|
|
2730
|
+
throw new Error(
|
|
2731
|
+
"Cannot specify both 'version' and 'environment' parameters. Please use only one (remove the other)."
|
|
2732
|
+
);
|
|
2733
|
+
}
|
|
2728
2734
|
if (id) {
|
|
2729
2735
|
} else if (isEmpty(projectName) && isEmpty(projectId)) {
|
|
2730
2736
|
throw new Error("Must specify either projectName or projectId");
|
|
@@ -2742,7 +2748,10 @@ async function loadPrompt({
|
|
|
2742
2748
|
forceLogin
|
|
2743
2749
|
});
|
|
2744
2750
|
if (id) {
|
|
2745
|
-
response = await state.apiConn().get_json(`v1/prompt/${id}`, {
|
|
2751
|
+
response = await state.apiConn().get_json(`v1/prompt/${id}`, {
|
|
2752
|
+
...version && { version },
|
|
2753
|
+
...environment && { environment }
|
|
2754
|
+
});
|
|
2746
2755
|
if (response) {
|
|
2747
2756
|
response = { objects: [response] };
|
|
2748
2757
|
}
|
|
@@ -2751,10 +2760,14 @@ async function loadPrompt({
|
|
|
2751
2760
|
project_name: projectName,
|
|
2752
2761
|
project_id: projectId,
|
|
2753
2762
|
slug,
|
|
2754
|
-
version
|
|
2763
|
+
version,
|
|
2764
|
+
...environment && { environment }
|
|
2755
2765
|
});
|
|
2756
2766
|
}
|
|
2757
2767
|
} catch (e) {
|
|
2768
|
+
if (environment || version) {
|
|
2769
|
+
throw new Error(`Prompt not found with specified parameters: ${e}`);
|
|
2770
|
+
}
|
|
2758
2771
|
console.warn("Failed to load prompt, attempting to fall back to cache:", e);
|
|
2759
2772
|
let prompt2;
|
|
2760
2773
|
if (id) {
|
|
@@ -2997,6 +3010,110 @@ function traced(callback, args) {
|
|
|
2997
3010
|
})();
|
|
2998
3011
|
}
|
|
2999
3012
|
}
|
|
3013
|
+
function isGeneratorFunction(fn) {
|
|
3014
|
+
return Object.prototype.toString.call(fn) === "[object GeneratorFunction]";
|
|
3015
|
+
}
|
|
3016
|
+
function isAsyncGeneratorFunction(fn) {
|
|
3017
|
+
return Object.prototype.toString.call(fn) === "[object AsyncGeneratorFunction]";
|
|
3018
|
+
}
|
|
3019
|
+
function wrapTracedSyncGenerator(fn, spanArgs, noTraceIO) {
|
|
3020
|
+
const wrapper = function* (...fnArgs) {
|
|
3021
|
+
const span = startSpan(spanArgs);
|
|
3022
|
+
try {
|
|
3023
|
+
if (!noTraceIO) {
|
|
3024
|
+
span.log({ input: fnArgs });
|
|
3025
|
+
}
|
|
3026
|
+
const envValue = isomorph_default.getEnv("BRAINTRUST_MAX_GENERATOR_ITEMS");
|
|
3027
|
+
const maxItems = envValue !== void 0 ? Number(envValue) : 1e3;
|
|
3028
|
+
if (!noTraceIO && maxItems !== 0) {
|
|
3029
|
+
let collected = [];
|
|
3030
|
+
let truncated = false;
|
|
3031
|
+
const gen = generatorWithCurrent(span, fn.apply(this, fnArgs));
|
|
3032
|
+
try {
|
|
3033
|
+
for (const value of gen) {
|
|
3034
|
+
if (maxItems === -1 || !truncated && collected.length < maxItems) {
|
|
3035
|
+
collected.push(value);
|
|
3036
|
+
} else {
|
|
3037
|
+
truncated = true;
|
|
3038
|
+
collected = [];
|
|
3039
|
+
console.warn(
|
|
3040
|
+
`Generator output exceeded limit of ${maxItems} items, output not logged. Increase BRAINTRUST_MAX_GENERATOR_ITEMS or set to -1 to disable limit.`
|
|
3041
|
+
);
|
|
3042
|
+
}
|
|
3043
|
+
yield value;
|
|
3044
|
+
}
|
|
3045
|
+
if (!truncated) {
|
|
3046
|
+
span.log({ output: collected });
|
|
3047
|
+
}
|
|
3048
|
+
} catch (error) {
|
|
3049
|
+
logError(span, error);
|
|
3050
|
+
if (!truncated && collected.length > 0) {
|
|
3051
|
+
span.log({ output: collected });
|
|
3052
|
+
}
|
|
3053
|
+
throw error;
|
|
3054
|
+
}
|
|
3055
|
+
} else {
|
|
3056
|
+
const gen = generatorWithCurrent(span, fn.apply(this, fnArgs));
|
|
3057
|
+
for (const value of gen) {
|
|
3058
|
+
yield value;
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
} finally {
|
|
3062
|
+
span.end();
|
|
3063
|
+
}
|
|
3064
|
+
};
|
|
3065
|
+
Object.defineProperty(wrapper, "name", { value: fn.name });
|
|
3066
|
+
return wrapper;
|
|
3067
|
+
}
|
|
3068
|
+
function wrapTracedAsyncGenerator(fn, spanArgs, noTraceIO) {
|
|
3069
|
+
const wrapper = async function* (...fnArgs) {
|
|
3070
|
+
const span = startSpan(spanArgs);
|
|
3071
|
+
try {
|
|
3072
|
+
if (!noTraceIO) {
|
|
3073
|
+
span.log({ input: fnArgs });
|
|
3074
|
+
}
|
|
3075
|
+
const envValue = isomorph_default.getEnv("BRAINTRUST_MAX_GENERATOR_ITEMS");
|
|
3076
|
+
const maxItems = envValue !== void 0 ? Number(envValue) : 1e3;
|
|
3077
|
+
if (!noTraceIO && maxItems !== 0) {
|
|
3078
|
+
let collected = [];
|
|
3079
|
+
let truncated = false;
|
|
3080
|
+
const gen = asyncGeneratorWithCurrent(span, fn.apply(this, fnArgs));
|
|
3081
|
+
try {
|
|
3082
|
+
for await (const value of gen) {
|
|
3083
|
+
if (maxItems === -1 || !truncated && collected.length < maxItems) {
|
|
3084
|
+
collected.push(value);
|
|
3085
|
+
} else {
|
|
3086
|
+
truncated = true;
|
|
3087
|
+
collected = [];
|
|
3088
|
+
console.warn(
|
|
3089
|
+
`Generator output exceeded limit of ${maxItems} items, output not logged. Increase BRAINTRUST_MAX_GENERATOR_ITEMS or set to -1 to disable limit.`
|
|
3090
|
+
);
|
|
3091
|
+
}
|
|
3092
|
+
yield value;
|
|
3093
|
+
}
|
|
3094
|
+
if (!truncated) {
|
|
3095
|
+
span.log({ output: collected });
|
|
3096
|
+
}
|
|
3097
|
+
} catch (error) {
|
|
3098
|
+
logError(span, error);
|
|
3099
|
+
if (!truncated && collected.length > 0) {
|
|
3100
|
+
span.log({ output: collected });
|
|
3101
|
+
}
|
|
3102
|
+
throw error;
|
|
3103
|
+
}
|
|
3104
|
+
} else {
|
|
3105
|
+
const gen = asyncGeneratorWithCurrent(span, fn.apply(this, fnArgs));
|
|
3106
|
+
for await (const value of gen) {
|
|
3107
|
+
yield value;
|
|
3108
|
+
}
|
|
3109
|
+
}
|
|
3110
|
+
} finally {
|
|
3111
|
+
span.end();
|
|
3112
|
+
}
|
|
3113
|
+
};
|
|
3114
|
+
Object.defineProperty(wrapper, "name", { value: fn.name });
|
|
3115
|
+
return wrapper;
|
|
3116
|
+
}
|
|
3000
3117
|
function wrapTraced(fn, args) {
|
|
3001
3118
|
const spanArgs = {
|
|
3002
3119
|
name: fn.name,
|
|
@@ -3005,7 +3122,14 @@ function wrapTraced(fn, args) {
|
|
|
3005
3122
|
};
|
|
3006
3123
|
const hasExplicitInput = args && args.event && "input" in args.event && args.event.input !== void 0;
|
|
3007
3124
|
const hasExplicitOutput = args && args.event && args.event.output !== void 0;
|
|
3008
|
-
|
|
3125
|
+
const noTraceIO = _optionalChain([args, 'optionalAccess', _32 => _32.noTraceIO]) || hasExplicitInput || hasExplicitOutput;
|
|
3126
|
+
if (isGeneratorFunction(fn)) {
|
|
3127
|
+
return wrapTracedSyncGenerator(fn, spanArgs, !!noTraceIO);
|
|
3128
|
+
}
|
|
3129
|
+
if (isAsyncGeneratorFunction(fn)) {
|
|
3130
|
+
return wrapTracedAsyncGenerator(fn, spanArgs, !!noTraceIO);
|
|
3131
|
+
}
|
|
3132
|
+
if (_optionalChain([args, 'optionalAccess', _33 => _33.asyncFlush])) {
|
|
3009
3133
|
return (...fnArgs) => traced((span) => {
|
|
3010
3134
|
if (!hasExplicitInput) {
|
|
3011
3135
|
span.log({ input: fnArgs });
|
|
@@ -3043,15 +3167,15 @@ function startSpan(args) {
|
|
|
3043
3167
|
return startSpanAndIsLogger(args).span;
|
|
3044
3168
|
}
|
|
3045
3169
|
async function flush(options) {
|
|
3046
|
-
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
3170
|
+
const state = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _34 => _34.state]), () => ( _globalState));
|
|
3047
3171
|
return await state.bgLogger().flush();
|
|
3048
3172
|
}
|
|
3049
3173
|
function setFetch(fetch2) {
|
|
3050
3174
|
_globalState.setFetch(fetch2);
|
|
3051
3175
|
}
|
|
3052
3176
|
function startSpanAndIsLogger(args) {
|
|
3053
|
-
const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
3054
|
-
const parentStr = _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
3177
|
+
const state = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _35 => _35.state]), () => ( _globalState));
|
|
3178
|
+
const parentStr = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _36 => _36.parent]), () => ( state.currentParent.getStore()));
|
|
3055
3179
|
const components = parentStr ? _core.SpanComponentsV3.fromStr(parentStr) : void 0;
|
|
3056
3180
|
if (components) {
|
|
3057
3181
|
const parentSpanIds = components.data.row_id ? {
|
|
@@ -3067,18 +3191,18 @@ function startSpanAndIsLogger(args) {
|
|
|
3067
3191
|
),
|
|
3068
3192
|
parentComputeObjectMetadataArgs: _nullishCoalesce(components.data.compute_object_metadata_args, () => ( void 0)),
|
|
3069
3193
|
parentSpanIds,
|
|
3070
|
-
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
3194
|
+
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _37 => _37.propagatedEvent]), () => ( // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
3071
3195
|
(_nullishCoalesce(components.data.propagated_event, () => ( void 0)))))
|
|
3072
3196
|
});
|
|
3073
3197
|
return {
|
|
3074
3198
|
span,
|
|
3075
3199
|
isSyncFlushLogger: components.data.object_type === _core.SpanObjectTypeV3.PROJECT_LOGS && // Since there's no parent logger here, we're free to choose the async flush
|
|
3076
3200
|
// behavior, and therefore propagate along whatever we get from the arguments
|
|
3077
|
-
_optionalChain([args, 'optionalAccess',
|
|
3201
|
+
_optionalChain([args, 'optionalAccess', _38 => _38.asyncFlush]) === false
|
|
3078
3202
|
};
|
|
3079
3203
|
} else {
|
|
3080
3204
|
const parentObject = getSpanParentObject({
|
|
3081
|
-
asyncFlush: _optionalChain([args, 'optionalAccess',
|
|
3205
|
+
asyncFlush: _optionalChain([args, 'optionalAccess', _39 => _39.asyncFlush])
|
|
3082
3206
|
});
|
|
3083
3207
|
const span = parentObject.startSpan(args);
|
|
3084
3208
|
return {
|
|
@@ -3090,6 +3214,52 @@ function startSpanAndIsLogger(args) {
|
|
|
3090
3214
|
function withCurrent(span, callback, state = void 0) {
|
|
3091
3215
|
return (_nullishCoalesce(state, () => ( _globalState))).currentSpan.run(span, () => callback(span));
|
|
3092
3216
|
}
|
|
3217
|
+
function* generatorWithCurrent(span, gen, state = void 0) {
|
|
3218
|
+
let nextValue;
|
|
3219
|
+
while (true) {
|
|
3220
|
+
const result = withCurrent(
|
|
3221
|
+
span,
|
|
3222
|
+
() => {
|
|
3223
|
+
try {
|
|
3224
|
+
return gen.next(nextValue);
|
|
3225
|
+
} catch (e) {
|
|
3226
|
+
return { value: void 0, done: true, error: e };
|
|
3227
|
+
}
|
|
3228
|
+
},
|
|
3229
|
+
state
|
|
3230
|
+
);
|
|
3231
|
+
if ("error" in result) {
|
|
3232
|
+
throw result.error;
|
|
3233
|
+
}
|
|
3234
|
+
if (result.done) {
|
|
3235
|
+
return result.value;
|
|
3236
|
+
}
|
|
3237
|
+
nextValue = yield result.value;
|
|
3238
|
+
}
|
|
3239
|
+
}
|
|
3240
|
+
async function* asyncGeneratorWithCurrent(span, gen, state = void 0) {
|
|
3241
|
+
let nextValue;
|
|
3242
|
+
while (true) {
|
|
3243
|
+
const result = await withCurrent(
|
|
3244
|
+
span,
|
|
3245
|
+
async () => {
|
|
3246
|
+
try {
|
|
3247
|
+
return await gen.next(nextValue);
|
|
3248
|
+
} catch (e) {
|
|
3249
|
+
return { value: void 0, done: true, error: e };
|
|
3250
|
+
}
|
|
3251
|
+
},
|
|
3252
|
+
state
|
|
3253
|
+
);
|
|
3254
|
+
if ("error" in result) {
|
|
3255
|
+
throw result.error;
|
|
3256
|
+
}
|
|
3257
|
+
if (result.done) {
|
|
3258
|
+
return result.value;
|
|
3259
|
+
}
|
|
3260
|
+
nextValue = yield result.value;
|
|
3261
|
+
}
|
|
3262
|
+
}
|
|
3093
3263
|
function withParent(parent, callback, state = void 0) {
|
|
3094
3264
|
return (_nullishCoalesce(state, () => ( _globalState))).currentParent.run(parent, () => callback());
|
|
3095
3265
|
}
|
|
@@ -3220,10 +3390,10 @@ function extractAttachments(event, attachments) {
|
|
|
3220
3390
|
event[key] = value.reference;
|
|
3221
3391
|
continue;
|
|
3222
3392
|
}
|
|
3223
|
-
if (_optionalChain([value, 'optionalAccess',
|
|
3393
|
+
if (_optionalChain([value, 'optionalAccess', _40 => _40.type]) === _typespecs.BRAINTRUST_ATTACHMENT && value.key && !value.uploader) {
|
|
3224
3394
|
continue;
|
|
3225
3395
|
}
|
|
3226
|
-
if (_optionalChain([value, 'optionalAccess',
|
|
3396
|
+
if (_optionalChain([value, 'optionalAccess', _41 => _41.reference, 'optionalAccess', _42 => _42.type]) === _typespecs.BRAINTRUST_ATTACHMENT && _optionalChain([value, 'optionalAccess', _43 => _43.uploader])) {
|
|
3227
3397
|
const attachment = new Attachment({
|
|
3228
3398
|
data: value.dataDebugString,
|
|
3229
3399
|
filename: value.reference.filename,
|
|
@@ -3361,7 +3531,7 @@ var ObjectFetcher = (_class9 = class {
|
|
|
3361
3531
|
throw new Error("Too many BTQL iterations");
|
|
3362
3532
|
}
|
|
3363
3533
|
}
|
|
3364
|
-
this._fetchedData = this.mutateRecord ? _optionalChain([data, 'optionalAccess',
|
|
3534
|
+
this._fetchedData = this.mutateRecord ? _optionalChain([data, 'optionalAccess', _44 => _44.map, 'call', _45 => _45(this.mutateRecord)]) : data;
|
|
3365
3535
|
}
|
|
3366
3536
|
return this._fetchedData || [];
|
|
3367
3537
|
}
|
|
@@ -3442,7 +3612,7 @@ var Experiment = (_class10 = class extends ObjectFetcher {
|
|
|
3442
3612
|
* @returns The `id` of the logged event.
|
|
3443
3613
|
*/
|
|
3444
3614
|
log(event, options) {
|
|
3445
|
-
if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess',
|
|
3615
|
+
if (this.calledStartSpan && !_optionalChain([options, 'optionalAccess', _46 => _46.allowConcurrentWithSpans])) {
|
|
3446
3616
|
throw new Error(
|
|
3447
3617
|
"Cannot run toplevel `log` method while using spans. To log to the span, call `experiment.traced` and then log with `span.log`"
|
|
3448
3618
|
);
|
|
@@ -3495,12 +3665,12 @@ var Experiment = (_class10 = class extends ObjectFetcher {
|
|
|
3495
3665
|
state: this.state,
|
|
3496
3666
|
...startSpanParentArgs({
|
|
3497
3667
|
state: this.state,
|
|
3498
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
3668
|
+
parent: _optionalChain([args, 'optionalAccess', _47 => _47.parent]),
|
|
3499
3669
|
parentObjectType: this.parentObjectType(),
|
|
3500
3670
|
parentObjectId: this.lazyId,
|
|
3501
3671
|
parentComputeObjectMetadataArgs: void 0,
|
|
3502
3672
|
parentSpanIds: void 0,
|
|
3503
|
-
propagatedEvent: _optionalChain([args, 'optionalAccess',
|
|
3673
|
+
propagatedEvent: _optionalChain([args, 'optionalAccess', _48 => _48.propagatedEvent])
|
|
3504
3674
|
}),
|
|
3505
3675
|
defaultRootType: _core.SpanTypeAttribute.EVAL
|
|
3506
3676
|
});
|
|
@@ -3792,10 +3962,10 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
3792
3962
|
...serializableInternalData,
|
|
3793
3963
|
[_core.IS_MERGE_FIELD]: this.isMerge
|
|
3794
3964
|
});
|
|
3795
|
-
if (_optionalChain([partialRecord, 'access',
|
|
3796
|
-
this.loggedEndTime = _optionalChain([partialRecord, 'access',
|
|
3965
|
+
if (_optionalChain([partialRecord, 'access', _49 => _49.metrics, 'optionalAccess', _50 => _50.end])) {
|
|
3966
|
+
this.loggedEndTime = _optionalChain([partialRecord, 'access', _51 => _51.metrics, 'optionalAccess', _52 => _52.end]);
|
|
3797
3967
|
}
|
|
3798
|
-
if ((_nullishCoalesce(partialRecord.tags, () => ( []))).length > 0 && _optionalChain([this, 'access',
|
|
3968
|
+
if ((_nullishCoalesce(partialRecord.tags, () => ( []))).length > 0 && _optionalChain([this, 'access', _53 => _53._spanParents, 'optionalAccess', _54 => _54.length])) {
|
|
3799
3969
|
throw new Error("Tags can only be logged to the root span");
|
|
3800
3970
|
}
|
|
3801
3971
|
const computeRecord = async () => ({
|
|
@@ -3840,18 +4010,18 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
3840
4010
|
);
|
|
3841
4011
|
}
|
|
3842
4012
|
startSpan(args) {
|
|
3843
|
-
const parentSpanIds = _optionalChain([args, 'optionalAccess',
|
|
4013
|
+
const parentSpanIds = _optionalChain([args, 'optionalAccess', _55 => _55.parent]) ? void 0 : { spanId: this._spanId, rootSpanId: this._rootSpanId };
|
|
3844
4014
|
return new _SpanImpl({
|
|
3845
4015
|
state: this._state,
|
|
3846
4016
|
...args,
|
|
3847
4017
|
...startSpanParentArgs({
|
|
3848
4018
|
state: this._state,
|
|
3849
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
4019
|
+
parent: _optionalChain([args, 'optionalAccess', _56 => _56.parent]),
|
|
3850
4020
|
parentObjectType: this.parentObjectType,
|
|
3851
4021
|
parentObjectId: this.parentObjectId,
|
|
3852
4022
|
parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
|
|
3853
4023
|
parentSpanIds,
|
|
3854
|
-
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
4024
|
+
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _57 => _57.propagatedEvent]), () => ( this.propagatedEvent))
|
|
3855
4025
|
})
|
|
3856
4026
|
});
|
|
3857
4027
|
}
|
|
@@ -3865,12 +4035,12 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
3865
4035
|
...args,
|
|
3866
4036
|
...startSpanParentArgs({
|
|
3867
4037
|
state: this._state,
|
|
3868
|
-
parent: _optionalChain([args, 'optionalAccess',
|
|
4038
|
+
parent: _optionalChain([args, 'optionalAccess', _58 => _58.parent]),
|
|
3869
4039
|
parentObjectType: this.parentObjectType,
|
|
3870
4040
|
parentObjectId: this.parentObjectId,
|
|
3871
4041
|
parentComputeObjectMetadataArgs: this.parentComputeObjectMetadataArgs,
|
|
3872
4042
|
parentSpanIds,
|
|
3873
|
-
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
4043
|
+
propagatedEvent: _nullishCoalesce(_optionalChain([args, 'optionalAccess', _59 => _59.propagatedEvent]), () => ( this.propagatedEvent))
|
|
3874
4044
|
}),
|
|
3875
4045
|
spanId
|
|
3876
4046
|
});
|
|
@@ -3879,7 +4049,7 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
3879
4049
|
let endTime;
|
|
3880
4050
|
let internalData = {};
|
|
3881
4051
|
if (!this.loggedEndTime) {
|
|
3882
|
-
endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess',
|
|
4052
|
+
endTime = _nullishCoalesce(_optionalChain([args, 'optionalAccess', _60 => _60.endTime]), () => ( getCurrentUnixTimestamp()));
|
|
3883
4053
|
internalData = { metrics: { end: endTime } };
|
|
3884
4054
|
} else {
|
|
3885
4055
|
endTime = this.loggedEndTime;
|
|
@@ -3922,8 +4092,8 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
3922
4092
|
const args = this.parentComputeObjectMetadataArgs;
|
|
3923
4093
|
switch (this.parentObjectType) {
|
|
3924
4094
|
case _core.SpanObjectTypeV3.PROJECT_LOGS: {
|
|
3925
|
-
const projectID = _optionalChain([args, 'optionalAccess',
|
|
3926
|
-
const projectName = _optionalChain([args, 'optionalAccess',
|
|
4095
|
+
const projectID = _optionalChain([args, 'optionalAccess', _61 => _61.project_id]) || this.parentObjectId.getSync().value;
|
|
4096
|
+
const projectName = _optionalChain([args, 'optionalAccess', _62 => _62.project_name]);
|
|
3927
4097
|
if (projectID) {
|
|
3928
4098
|
return `${baseUrl}/object?object_type=project_logs&object_id=${projectID}&id=${this._id}`;
|
|
3929
4099
|
} else if (projectName) {
|
|
@@ -3933,7 +4103,7 @@ var SpanImpl = (_class11 = class _SpanImpl {
|
|
|
3933
4103
|
}
|
|
3934
4104
|
}
|
|
3935
4105
|
case _core.SpanObjectTypeV3.EXPERIMENT: {
|
|
3936
|
-
const expID = _optionalChain([args, 'optionalAccess',
|
|
4106
|
+
const expID = _optionalChain([args, 'optionalAccess', _63 => _63.experiment_id]) || _optionalChain([this, 'access', _64 => _64.parentObjectId, 'optionalAccess', _65 => _65.getSync, 'call', _66 => _66(), 'optionalAccess', _67 => _67.value]);
|
|
3937
4107
|
if (!expID) {
|
|
3938
4108
|
return getErrPermlink("provide-experiment-id");
|
|
3939
4109
|
} else {
|
|
@@ -4359,13 +4529,13 @@ var Prompt = (_class13 = class _Prompt {
|
|
|
4359
4529
|
return "slug" in this.metadata ? this.metadata.slug : this.metadata.id;
|
|
4360
4530
|
}
|
|
4361
4531
|
get prompt() {
|
|
4362
|
-
return _optionalChain([this, 'access',
|
|
4532
|
+
return _optionalChain([this, 'access', _68 => _68.getParsedPromptData, 'call', _69 => _69(), 'optionalAccess', _70 => _70.prompt]);
|
|
4363
4533
|
}
|
|
4364
4534
|
get version() {
|
|
4365
4535
|
return this.metadata[_core.TRANSACTION_ID_FIELD];
|
|
4366
4536
|
}
|
|
4367
4537
|
get options() {
|
|
4368
|
-
return _optionalChain([this, 'access',
|
|
4538
|
+
return _optionalChain([this, 'access', _71 => _71.getParsedPromptData, 'call', _72 => _72(), 'optionalAccess', _73 => _73.options]) || {};
|
|
4369
4539
|
}
|
|
4370
4540
|
get promptData() {
|
|
4371
4541
|
return this.getParsedPromptData();
|
|
@@ -4516,7 +4686,7 @@ var Prompt = (_class13 = class _Prompt {
|
|
|
4516
4686
|
return {
|
|
4517
4687
|
type: "chat",
|
|
4518
4688
|
messages,
|
|
4519
|
-
..._optionalChain([prompt, 'access',
|
|
4689
|
+
..._optionalChain([prompt, 'access', _74 => _74.tools, 'optionalAccess', _75 => _75.trim, 'call', _76 => _76()]) ? {
|
|
4520
4690
|
tools: render(prompt.tools)
|
|
4521
4691
|
} : void 0
|
|
4522
4692
|
};
|
|
@@ -4586,7 +4756,9 @@ var _exportsForTestingOnly = {
|
|
|
4586
4756
|
clearTestBackgroundLogger,
|
|
4587
4757
|
simulateLoginForTests,
|
|
4588
4758
|
simulateLogoutForTests,
|
|
4589
|
-
setInitialTestState
|
|
4759
|
+
setInitialTestState,
|
|
4760
|
+
isGeneratorFunction,
|
|
4761
|
+
isAsyncGeneratorFunction
|
|
4590
4762
|
};
|
|
4591
4763
|
|
|
4592
4764
|
// src/browser-config.ts
|
|
@@ -4799,6 +4971,8 @@ function responsesProxy(openai) {
|
|
|
4799
4971
|
return responsesCreateProxy(target.create.bind(target));
|
|
4800
4972
|
} else if (name === "stream") {
|
|
4801
4973
|
return responsesStreamProxy(target.stream.bind(target));
|
|
4974
|
+
} else if (name === "parse") {
|
|
4975
|
+
return responsesParseProxy(target.parse.bind(target));
|
|
4802
4976
|
}
|
|
4803
4977
|
return Reflect.get(target, name, receiver);
|
|
4804
4978
|
}
|
|
@@ -4839,8 +5013,38 @@ function parseSpanFromResponseCreateParams(params) {
|
|
|
4839
5013
|
}
|
|
4840
5014
|
function parseEventFromResponseCreateResult(result) {
|
|
4841
5015
|
return {
|
|
4842
|
-
output: _optionalChain([result, 'optionalAccess',
|
|
4843
|
-
metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
5016
|
+
output: _optionalChain([result, 'optionalAccess', _77 => _77.output_text]) || "",
|
|
5017
|
+
metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _78 => _78.usage]))
|
|
5018
|
+
};
|
|
5019
|
+
}
|
|
5020
|
+
function parseSpanFromResponseParseParams(params) {
|
|
5021
|
+
const input = [{ role: "user", content: params.input }];
|
|
5022
|
+
if (params.instructions) {
|
|
5023
|
+
input.push({ role: "system", content: params.instructions });
|
|
5024
|
+
}
|
|
5025
|
+
const spanArgs = {
|
|
5026
|
+
name: "openai.responses.parse",
|
|
5027
|
+
spanAttributes: {
|
|
5028
|
+
type: "llm"
|
|
5029
|
+
},
|
|
5030
|
+
event: {
|
|
5031
|
+
input,
|
|
5032
|
+
metadata: {
|
|
5033
|
+
...filterFrom(params, ["input", "instructions"]),
|
|
5034
|
+
provider: "openai"
|
|
5035
|
+
}
|
|
5036
|
+
},
|
|
5037
|
+
startTime: getCurrentUnixTimestamp()
|
|
5038
|
+
};
|
|
5039
|
+
return {
|
|
5040
|
+
span: startSpan(spanArgs),
|
|
5041
|
+
start: spanArgs.startTime
|
|
5042
|
+
};
|
|
5043
|
+
}
|
|
5044
|
+
function parseEventFromResponseParseResult(result) {
|
|
5045
|
+
return {
|
|
5046
|
+
output: _optionalChain([result, 'optionalAccess', _79 => _79.output_parsed]) || _optionalChain([result, 'optionalAccess', _80 => _80.output_text]) || "",
|
|
5047
|
+
metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _81 => _81.usage]))
|
|
4844
5048
|
};
|
|
4845
5049
|
}
|
|
4846
5050
|
function traceResponseCreateStream(stream, timedSpan) {
|
|
@@ -4857,7 +5061,7 @@ function traceResponseCreateStream(stream, timedSpan) {
|
|
|
4857
5061
|
return result;
|
|
4858
5062
|
}
|
|
4859
5063
|
const item = result.value;
|
|
4860
|
-
if (!item || !_optionalChain([item, 'optionalAccess',
|
|
5064
|
+
if (!item || !_optionalChain([item, 'optionalAccess', _82 => _82.type]) || !_optionalChain([item, 'optionalAccess', _83 => _83.response])) {
|
|
4861
5065
|
return result;
|
|
4862
5066
|
}
|
|
4863
5067
|
const event = parseLogFromItem(item);
|
|
@@ -4868,23 +5072,23 @@ function traceResponseCreateStream(stream, timedSpan) {
|
|
|
4868
5072
|
};
|
|
4869
5073
|
}
|
|
4870
5074
|
function parseLogFromItem(item) {
|
|
4871
|
-
if (!item || !_optionalChain([item, 'optionalAccess',
|
|
5075
|
+
if (!item || !_optionalChain([item, 'optionalAccess', _84 => _84.type]) || !_optionalChain([item, 'optionalAccess', _85 => _85.response])) {
|
|
4872
5076
|
return {};
|
|
4873
5077
|
}
|
|
4874
5078
|
const response = item.response;
|
|
4875
5079
|
switch (item.type) {
|
|
4876
5080
|
case "response.completed":
|
|
4877
5081
|
const texts = [];
|
|
4878
|
-
for (const output of _optionalChain([response, 'optionalAccess',
|
|
4879
|
-
for (const content of _optionalChain([output, 'optionalAccess',
|
|
4880
|
-
if (_optionalChain([content, 'optionalAccess',
|
|
5082
|
+
for (const output of _optionalChain([response, 'optionalAccess', _86 => _86.output]) || []) {
|
|
5083
|
+
for (const content of _optionalChain([output, 'optionalAccess', _87 => _87.content]) || []) {
|
|
5084
|
+
if (_optionalChain([content, 'optionalAccess', _88 => _88.type]) === "output_text") {
|
|
4881
5085
|
texts.push(content.text);
|
|
4882
5086
|
}
|
|
4883
5087
|
}
|
|
4884
5088
|
}
|
|
4885
5089
|
return {
|
|
4886
5090
|
output: texts.join(""),
|
|
4887
|
-
metrics: parseMetricsFromUsage(_optionalChain([response, 'optionalAccess',
|
|
5091
|
+
metrics: parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _89 => _89.usage]))
|
|
4888
5092
|
};
|
|
4889
5093
|
default:
|
|
4890
5094
|
return {};
|
|
@@ -4917,6 +5121,16 @@ function responsesStreamProxy(target) {
|
|
|
4917
5121
|
}
|
|
4918
5122
|
});
|
|
4919
5123
|
}
|
|
5124
|
+
function responsesParseProxy(target) {
|
|
5125
|
+
const hooks = {
|
|
5126
|
+
name: "openai.responses.parse",
|
|
5127
|
+
toSpanFunc: parseSpanFromResponseParseParams,
|
|
5128
|
+
resultToEventFunc: parseEventFromResponseParseResult,
|
|
5129
|
+
traceStreamFunc: traceResponseCreateStream
|
|
5130
|
+
// Reuse the same stream tracing
|
|
5131
|
+
};
|
|
5132
|
+
return proxyCreate(target, hooks);
|
|
5133
|
+
}
|
|
4920
5134
|
var TOKEN_NAME_MAP = {
|
|
4921
5135
|
input_tokens: "prompt_tokens",
|
|
4922
5136
|
output_tokens: "completion_tokens",
|
|
@@ -5058,8 +5272,8 @@ function wrapOpenAIv4(openai) {
|
|
|
5058
5272
|
const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
|
|
5059
5273
|
const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
|
|
5060
5274
|
let betaProxy;
|
|
5061
|
-
if (_optionalChain([openai, 'access',
|
|
5062
|
-
const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess',
|
|
5275
|
+
if (_optionalChain([openai, 'access', _90 => _90.beta, 'optionalAccess', _91 => _91.chat, 'optionalAccess', _92 => _92.completions, 'optionalAccess', _93 => _93.stream])) {
|
|
5276
|
+
const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _94 => _94.beta, 'optionalAccess', _95 => _95.chat, 'access', _96 => _96.completions]), {
|
|
5063
5277
|
get(target, name, receiver) {
|
|
5064
5278
|
const baseVal = Reflect.get(target, name, receiver);
|
|
5065
5279
|
if (name === "parse") {
|
|
@@ -5107,7 +5321,7 @@ function wrapOpenAIv4(openai) {
|
|
|
5107
5321
|
});
|
|
5108
5322
|
}
|
|
5109
5323
|
function logCompletionResponse(startTime, response, span) {
|
|
5110
|
-
const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess',
|
|
5324
|
+
const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _97 => _97.usage]));
|
|
5111
5325
|
metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
|
|
5112
5326
|
span.log({
|
|
5113
5327
|
output: response.choices,
|
|
@@ -5309,7 +5523,7 @@ function parseChatCompletionParams(params) {
|
|
|
5309
5523
|
function processEmbeddingResponse(result, span) {
|
|
5310
5524
|
span.log({
|
|
5311
5525
|
output: { embedding_length: result.data[0].embedding.length },
|
|
5312
|
-
metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
5526
|
+
metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _98 => _98.usage]))
|
|
5313
5527
|
});
|
|
5314
5528
|
}
|
|
5315
5529
|
function processModerationResponse(result, span) {
|
|
@@ -5339,10 +5553,10 @@ function postprocessStreamingResults(allResults) {
|
|
|
5339
5553
|
if (result.usage) {
|
|
5340
5554
|
metrics = {
|
|
5341
5555
|
...metrics,
|
|
5342
|
-
...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess',
|
|
5556
|
+
...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _99 => _99.usage]))
|
|
5343
5557
|
};
|
|
5344
5558
|
}
|
|
5345
|
-
const delta = _optionalChain([result, 'access',
|
|
5559
|
+
const delta = _optionalChain([result, 'access', _100 => _100.choices, 'optionalAccess', _101 => _101[0], 'optionalAccess', _102 => _102.delta]);
|
|
5346
5560
|
if (!delta) {
|
|
5347
5561
|
continue;
|
|
5348
5562
|
}
|