braintrust 0.2.5 → 0.3.6

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/dist/index.d.ts CHANGED
@@ -1111,26 +1111,6 @@ declare const ChatCompletionTool: z.ZodObject<{
1111
1111
  type: "function";
1112
1112
  }>;
1113
1113
  type ChatCompletionToolType = z.infer<typeof ChatCompletionTool>;
1114
- declare const ObjectReference: z.ZodUnion<[z.ZodObject<{
1115
- object_type: z.ZodEnum<["project_logs", "experiment", "dataset", "prompt", "function", "prompt_session"]>;
1116
- object_id: z.ZodString;
1117
- id: z.ZodString;
1118
- _xact_id: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
1119
- created: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
1120
- }, "strip", z.ZodTypeAny, {
1121
- id: string;
1122
- object_type: "function" | "experiment" | "dataset" | "prompt" | "prompt_session" | "project_logs";
1123
- object_id: string;
1124
- created?: string | null | undefined;
1125
- _xact_id?: string | null | undefined;
1126
- }, {
1127
- id: string;
1128
- object_type: "function" | "experiment" | "dataset" | "prompt" | "prompt_session" | "project_logs";
1129
- object_id: string;
1130
- created?: string | null | undefined;
1131
- _xact_id?: string | null | undefined;
1132
- }>, z.ZodNull]>;
1133
- type ObjectReferenceType = z.infer<typeof ObjectReference>;
1134
1114
  declare const RepoInfo: z.ZodUnion<[z.ZodObject<{
1135
1115
  commit: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
1136
1116
  branch: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
@@ -11623,6 +11603,26 @@ declare const IfExists: z.ZodEnum<["error", "ignore", "replace"]>;
11623
11603
  type IfExistsType = z.infer<typeof IfExists>;
11624
11604
  declare const StreamingMode: z.ZodUnion<[z.ZodEnum<["auto", "parallel"]>, z.ZodNull]>;
11625
11605
  type StreamingModeType = z.infer<typeof StreamingMode>;
11606
+ declare const ObjectReference: z.ZodObject<{
11607
+ object_type: z.ZodEnum<["project_logs", "experiment", "dataset", "prompt", "function", "prompt_session"]>;
11608
+ object_id: z.ZodString;
11609
+ id: z.ZodString;
11610
+ _xact_id: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
11611
+ created: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNull]>>;
11612
+ }, "strip", z.ZodTypeAny, {
11613
+ id: string;
11614
+ object_type: "function" | "experiment" | "dataset" | "prompt" | "prompt_session" | "project_logs";
11615
+ object_id: string;
11616
+ created?: string | null | undefined;
11617
+ _xact_id?: string | null | undefined;
11618
+ }, {
11619
+ id: string;
11620
+ object_type: "function" | "experiment" | "dataset" | "prompt" | "prompt_session" | "project_logs";
11621
+ object_id: string;
11622
+ created?: string | null | undefined;
11623
+ _xact_id?: string | null | undefined;
11624
+ }>;
11625
+ type ObjectReferenceType = z.infer<typeof ObjectReference>;
11626
11626
  declare const Prompt$1: z.ZodObject<{
11627
11627
  id: z.ZodString;
11628
11628
  _xact_id: z.ZodString;
@@ -20941,6 +20941,7 @@ interface ReadableSpan {
20941
20941
  spanId: string;
20942
20942
  traceId: string;
20943
20943
  };
20944
+ parentSpanId?: string;
20944
20945
  }
20945
20946
  interface Span extends ReadableSpan {
20946
20947
  end(): void;
package/dist/index.js CHANGED
@@ -3577,7 +3577,7 @@ var ResponseFormatJsonSchema = _zod.z.object({
3577
3577
  schema: _zod.z.union([_zod.z.object({}).partial().passthrough(), _zod.z.string()]).optional(),
3578
3578
  strict: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional()
3579
3579
  });
3580
- var ResponseFormat = _zod.z.union([
3580
+ var ResponseFormatNullish = _zod.z.union([
3581
3581
  _zod.z.object({ type: _zod.z.literal("json_object") }),
3582
3582
  _zod.z.object({
3583
3583
  type: _zod.z.literal("json_schema"),
@@ -3593,7 +3593,7 @@ var AnyModelParams = _zod.z.object({
3593
3593
  max_completion_tokens: _zod.z.number().optional(),
3594
3594
  frequency_penalty: _zod.z.number().optional(),
3595
3595
  presence_penalty: _zod.z.number().optional(),
3596
- response_format: ResponseFormat.optional(),
3596
+ response_format: ResponseFormatNullish.optional(),
3597
3597
  tool_choice: _zod.z.union([
3598
3598
  _zod.z.literal("auto"),
3599
3599
  _zod.z.literal("none"),
@@ -3631,6 +3631,23 @@ var ApiKey = _zod.z.object({
3631
3631
  user_family_name: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
3632
3632
  org_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional()
3633
3633
  });
3634
+ var AsyncScoringState = _zod.z.union([
3635
+ _zod.z.object({
3636
+ status: _zod.z.literal("enabled"),
3637
+ token: _zod.z.string(),
3638
+ function_ids: _zod.z.array(_zod.z.unknown()).min(1),
3639
+ skip_logging: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional()
3640
+ }),
3641
+ _zod.z.object({ status: _zod.z.literal("disabled") }),
3642
+ _zod.z.null(),
3643
+ _zod.z.null()
3644
+ ]);
3645
+ var AsyncScoringControl = _zod.z.union([
3646
+ _zod.z.object({ kind: _zod.z.literal("score_update"), token: _zod.z.string() }),
3647
+ _zod.z.object({ kind: _zod.z.literal("state_override"), state: AsyncScoringState }),
3648
+ _zod.z.object({ kind: _zod.z.literal("state_force_reselect") }),
3649
+ _zod.z.object({ kind: _zod.z.literal("state_enabled_force_rescore") })
3650
+ ]);
3634
3651
  var BraintrustAttachmentReference = _zod.z.object({
3635
3652
  type: _zod.z.literal("braintrust_attachment"),
3636
3653
  filename: _zod.z.string().min(1),
@@ -3833,7 +3850,7 @@ var Dataset = _zod.z.object({
3833
3850
  user_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
3834
3851
  metadata: _zod.z.union([_zod.z.object({}).partial().passthrough(), _zod.z.null()]).optional()
3835
3852
  });
3836
- var ObjectReference = _zod.z.union([
3853
+ var ObjectReferenceNullish = _zod.z.union([
3837
3854
  _zod.z.object({
3838
3855
  object_type: _zod.z.enum([
3839
3856
  "project_logs",
@@ -3867,7 +3884,7 @@ var DatasetEvent = _zod.z.object({
3867
3884
  span_id: _zod.z.string(),
3868
3885
  root_span_id: _zod.z.string(),
3869
3886
  is_root: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional(),
3870
- origin: ObjectReference.optional()
3887
+ origin: ObjectReferenceNullish.optional()
3871
3888
  });
3872
3889
  var EnvVar = _zod.z.object({
3873
3890
  id: _zod.z.string().uuid(),
@@ -3947,7 +3964,7 @@ var ExperimentEvent = _zod.z.object({
3947
3964
  root_span_id: _zod.z.string(),
3948
3965
  span_attributes: SpanAttributes.optional(),
3949
3966
  is_root: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional(),
3950
- origin: ObjectReference.optional()
3967
+ origin: ObjectReferenceNullish.optional()
3951
3968
  });
3952
3969
  var ExtendedSavedFunctionId = _zod.z.union([
3953
3970
  _zod.z.object({ type: _zod.z.literal("function"), id: _zod.z.string() }),
@@ -3976,7 +3993,7 @@ var ModelParams = _zod.z.union([
3976
3993
  max_completion_tokens: _zod.z.number(),
3977
3994
  frequency_penalty: _zod.z.number(),
3978
3995
  presence_penalty: _zod.z.number(),
3979
- response_format: ResponseFormat,
3996
+ response_format: ResponseFormatNullish,
3980
3997
  tool_choice: _zod.z.union([
3981
3998
  _zod.z.literal("auto"),
3982
3999
  _zod.z.literal("none"),
@@ -4299,6 +4316,20 @@ var MessageRole = _zod.z.enum([
4299
4316
  "model",
4300
4317
  "developer"
4301
4318
  ]);
4319
+ var ObjectReference = _zod.z.object({
4320
+ object_type: _zod.z.enum([
4321
+ "project_logs",
4322
+ "experiment",
4323
+ "dataset",
4324
+ "prompt",
4325
+ "function",
4326
+ "prompt_session"
4327
+ ]),
4328
+ object_id: _zod.z.string().uuid(),
4329
+ id: _zod.z.string(),
4330
+ _xact_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
4331
+ created: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional()
4332
+ });
4302
4333
  var OnlineScoreConfig = _zod.z.union([
4303
4334
  _zod.z.object({
4304
4335
  sampling_rate: _zod.z.number().gte(0).lte(1),
@@ -4431,7 +4462,7 @@ var ProjectLogsEvent = _zod.z.object({
4431
4462
  root_span_id: _zod.z.string(),
4432
4463
  is_root: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional(),
4433
4464
  span_attributes: SpanAttributes.optional(),
4434
- origin: ObjectReference.optional()
4465
+ origin: ObjectReferenceNullish.optional()
4435
4466
  });
4436
4467
  var ProjectScoreType = _zod.z.enum([
4437
4468
  "slider",
@@ -4513,6 +4544,14 @@ var PromptSessionEvent = _zod.z.object({
4513
4544
  completion: _zod.z.unknown().optional(),
4514
4545
  tags: _zod.z.union([_zod.z.array(_zod.z.string()), _zod.z.null()]).optional()
4515
4546
  });
4547
+ var ResponseFormat = _zod.z.union([
4548
+ _zod.z.object({ type: _zod.z.literal("json_object") }),
4549
+ _zod.z.object({
4550
+ type: _zod.z.literal("json_schema"),
4551
+ json_schema: ResponseFormatJsonSchema
4552
+ }),
4553
+ _zod.z.object({ type: _zod.z.literal("text") })
4554
+ ]);
4516
4555
  var Role = _zod.z.object({
4517
4556
  id: _zod.z.string().uuid(),
4518
4557
  org_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
@@ -4572,10 +4611,9 @@ var ServiceToken = _zod.z.object({
4572
4611
  created: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
4573
4612
  name: _zod.z.string(),
4574
4613
  preview_name: _zod.z.string(),
4575
- user_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
4576
- user_email: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
4577
- user_given_name: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
4578
- user_family_name: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
4614
+ service_account_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
4615
+ service_account_email: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
4616
+ service_account_name: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
4579
4617
  org_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional()
4580
4618
  });
4581
4619
  var SpanIFrame = _zod.z.object({
@@ -4596,7 +4634,7 @@ var SSEConsoleEventData = _zod.z.object({
4596
4634
  var SSEProgressEventData = _zod.z.object({
4597
4635
  id: _zod.z.string(),
4598
4636
  object_type: FunctionObjectType,
4599
- origin: ObjectReference.and(_zod.z.unknown()).optional(),
4637
+ origin: ObjectReferenceNullish.and(_zod.z.unknown()).optional(),
4600
4638
  format: FunctionFormat,
4601
4639
  output_type: FunctionOutputType,
4602
4640
  name: _zod.z.string(),
@@ -12190,19 +12228,15 @@ function responsesCreateProxy(target) {
12190
12228
  return proxyCreate(target, hooks);
12191
12229
  }
12192
12230
  function parseSpanFromResponseCreateParams(params) {
12193
- const input = [{ role: "user", content: params.input }];
12194
- if (params.instructions) {
12195
- input.push({ role: "system", content: params.instructions });
12196
- }
12197
12231
  const spanArgs = {
12198
12232
  name: "openai.responses.create",
12199
12233
  spanAttributes: {
12200
12234
  type: "llm"
12201
12235
  },
12202
12236
  event: {
12203
- input,
12237
+ input: params.input,
12204
12238
  metadata: {
12205
- ...filterFrom(params, ["input", "instructions"]),
12239
+ ...filterFrom(params, ["input"]),
12206
12240
  provider: "openai"
12207
12241
  }
12208
12242
  },
@@ -12214,25 +12248,29 @@ function parseSpanFromResponseCreateParams(params) {
12214
12248
  };
12215
12249
  }
12216
12250
  function parseEventFromResponseCreateResult(result) {
12217
- return {
12218
- output: _optionalChain([result, 'optionalAccess', _161 => _161.output_text]) || "",
12219
- metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _162 => _162.usage]))
12220
- };
12251
+ const data = {};
12252
+ if (_optionalChain([result, 'optionalAccess', _161 => _161.output]) !== void 0) {
12253
+ data.output = result.output;
12254
+ }
12255
+ if (result) {
12256
+ const { output, usage, ...metadata } = result;
12257
+ if (Object.keys(metadata).length > 0) {
12258
+ data.metadata = metadata;
12259
+ }
12260
+ }
12261
+ data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _162 => _162.usage]));
12262
+ return data;
12221
12263
  }
12222
12264
  function parseSpanFromResponseParseParams(params) {
12223
- const input = [{ role: "user", content: params.input }];
12224
- if (params.instructions) {
12225
- input.push({ role: "system", content: params.instructions });
12226
- }
12227
12265
  const spanArgs = {
12228
12266
  name: "openai.responses.parse",
12229
12267
  spanAttributes: {
12230
12268
  type: "llm"
12231
12269
  },
12232
12270
  event: {
12233
- input,
12271
+ input: params.input,
12234
12272
  metadata: {
12235
- ...filterFrom(params, ["input", "instructions"]),
12273
+ ...filterFrom(params, ["input"]),
12236
12274
  provider: "openai"
12237
12275
  }
12238
12276
  },
@@ -12244,10 +12282,18 @@ function parseSpanFromResponseParseParams(params) {
12244
12282
  };
12245
12283
  }
12246
12284
  function parseEventFromResponseParseResult(result) {
12247
- return {
12248
- output: _optionalChain([result, 'optionalAccess', _163 => _163.output_parsed]) || _optionalChain([result, 'optionalAccess', _164 => _164.output_text]) || "",
12249
- metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _165 => _165.usage]))
12250
- };
12285
+ const data = {};
12286
+ if (_optionalChain([result, 'optionalAccess', _163 => _163.output]) !== void 0) {
12287
+ data.output = result.output;
12288
+ }
12289
+ if (result) {
12290
+ const { output, usage, ...metadata } = result;
12291
+ if (Object.keys(metadata).length > 0) {
12292
+ data.metadata = metadata;
12293
+ }
12294
+ }
12295
+ data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _164 => _164.usage]));
12296
+ return data;
12251
12297
  }
12252
12298
  function traceResponseCreateStream(stream, timedSpan) {
12253
12299
  const span = timedSpan.span;
@@ -12263,7 +12309,7 @@ function traceResponseCreateStream(stream, timedSpan) {
12263
12309
  return result;
12264
12310
  }
12265
12311
  const item = result.value;
12266
- if (!item || !_optionalChain([item, 'optionalAccess', _166 => _166.type]) || !_optionalChain([item, 'optionalAccess', _167 => _167.response])) {
12312
+ if (!item || !_optionalChain([item, 'optionalAccess', _165 => _165.type]) || !_optionalChain([item, 'optionalAccess', _166 => _166.response])) {
12267
12313
  return result;
12268
12314
  }
12269
12315
  const event = parseLogFromItem(item);
@@ -12274,24 +12320,24 @@ function traceResponseCreateStream(stream, timedSpan) {
12274
12320
  };
12275
12321
  }
12276
12322
  function parseLogFromItem(item) {
12277
- if (!item || !_optionalChain([item, 'optionalAccess', _168 => _168.type]) || !_optionalChain([item, 'optionalAccess', _169 => _169.response])) {
12323
+ if (!item || !_optionalChain([item, 'optionalAccess', _167 => _167.type]) || !_optionalChain([item, 'optionalAccess', _168 => _168.response])) {
12278
12324
  return {};
12279
12325
  }
12280
12326
  const response = item.response;
12281
12327
  switch (item.type) {
12282
12328
  case "response.completed":
12283
- const texts = [];
12284
- for (const output of _optionalChain([response, 'optionalAccess', _170 => _170.output]) || []) {
12285
- for (const content of _optionalChain([output, 'optionalAccess', _171 => _171.content]) || []) {
12286
- if (_optionalChain([content, 'optionalAccess', _172 => _172.type]) === "output_text") {
12287
- texts.push(content.text);
12288
- }
12329
+ const data = {};
12330
+ if (_optionalChain([response, 'optionalAccess', _169 => _169.output]) !== void 0) {
12331
+ data.output = response.output;
12332
+ }
12333
+ if (response) {
12334
+ const { usage, output, ...metadata } = response;
12335
+ if (Object.keys(metadata).length > 0) {
12336
+ data.metadata = metadata;
12289
12337
  }
12290
12338
  }
12291
- return {
12292
- output: texts.join(""),
12293
- metrics: parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _173 => _173.usage]))
12294
- };
12339
+ data.metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _170 => _170.usage]));
12340
+ return data;
12295
12341
  default:
12296
12342
  return {};
12297
12343
  }
@@ -12474,8 +12520,8 @@ function wrapOpenAIv4(openai) {
12474
12520
  const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
12475
12521
  const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
12476
12522
  let betaProxy2;
12477
- if (_optionalChain([openai, 'access', _174 => _174.beta, 'optionalAccess', _175 => _175.chat, 'optionalAccess', _176 => _176.completions, 'optionalAccess', _177 => _177.stream])) {
12478
- const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _178 => _178.beta, 'optionalAccess', _179 => _179.chat, 'access', _180 => _180.completions]), {
12523
+ if (_optionalChain([openai, 'access', _171 => _171.beta, 'optionalAccess', _172 => _172.chat, 'optionalAccess', _173 => _173.completions, 'optionalAccess', _174 => _174.stream])) {
12524
+ const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _175 => _175.beta, 'optionalAccess', _176 => _176.chat, 'access', _177 => _177.completions]), {
12479
12525
  get(target, name, receiver) {
12480
12526
  const baseVal = Reflect.get(target, name, receiver);
12481
12527
  if (name === "parse") {
@@ -12523,7 +12569,7 @@ function wrapOpenAIv4(openai) {
12523
12569
  });
12524
12570
  }
12525
12571
  function logCompletionResponse(startTime, response, span) {
12526
- const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _181 => _181.usage]));
12572
+ const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _178 => _178.usage]));
12527
12573
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
12528
12574
  span.log({
12529
12575
  output: response.choices,
@@ -12618,55 +12664,80 @@ function logHeaders(response, span) {
12618
12664
  }
12619
12665
  }
12620
12666
  function wrapChatCompletion(completion) {
12621
- return async (allParams, options) => {
12667
+ return (allParams, options) => {
12622
12668
  const { span_info: _, ...params } = allParams;
12623
- const span = startSpan(
12624
- _core.mergeDicts.call(void 0,
12625
- {
12626
- name: "Chat Completion",
12627
- spanAttributes: {
12628
- type: _core.SpanTypeAttribute.LLM
12629
- }
12630
- },
12631
- parseChatCompletionParams(allParams)
12632
- )
12633
- );
12634
- const startTime = getCurrentUnixTimestamp();
12635
- if (params.stream) {
12636
- const { data: ret, response } = await completion(
12637
- // We could get rid of this type coercion if we could somehow enforce
12638
- // that `P extends ChatParams` BUT does not have the property
12639
- // `span_info`.
12640
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
12641
- params,
12642
- options
12643
- ).withResponse();
12644
- logHeaders(response, span);
12645
- const wrapperStream = new WrapperStream(span, startTime, ret.iterator());
12646
- ret.iterator = () => wrapperStream[Symbol.asyncIterator]();
12647
- return ret;
12648
- } else {
12649
- try {
12650
- const completionResponse = completion(
12651
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
12652
- params,
12653
- options
12654
- );
12655
- const { data: ret, response } = await completionResponse.withResponse();
12656
- logHeaders(response, span);
12657
- const { messages, ...rest } = params;
12658
- span.log({
12659
- input: messages,
12660
- metadata: {
12661
- ...rest
12669
+ let executionPromise = null;
12670
+ const executeWrapped = () => {
12671
+ if (!executionPromise) {
12672
+ executionPromise = (async () => {
12673
+ const span = startSpan(
12674
+ _core.mergeDicts.call(void 0,
12675
+ {
12676
+ name: "Chat Completion",
12677
+ spanAttributes: {
12678
+ type: _core.SpanTypeAttribute.LLM
12679
+ }
12680
+ },
12681
+ parseChatCompletionParams(allParams)
12682
+ )
12683
+ );
12684
+ const startTime = getCurrentUnixTimestamp();
12685
+ if (params.stream) {
12686
+ const { data: ret, response } = await completion(
12687
+ // We could get rid of this type coercion if we could somehow enforce
12688
+ // that `P extends ChatParams` BUT does not have the property
12689
+ // `span_info`.
12690
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
12691
+ params,
12692
+ options
12693
+ ).withResponse();
12694
+ logHeaders(response, span);
12695
+ const wrapperStream = new WrapperStream(
12696
+ span,
12697
+ startTime,
12698
+ ret.iterator()
12699
+ );
12700
+ ret.iterator = () => wrapperStream[Symbol.asyncIterator]();
12701
+ return { data: ret, response };
12702
+ } else {
12703
+ try {
12704
+ const completionResponse = completion(
12705
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
12706
+ params,
12707
+ options
12708
+ );
12709
+ const { data: ret, response } = await completionResponse.withResponse();
12710
+ logHeaders(response, span);
12711
+ const { messages, ...rest } = params;
12712
+ span.log({
12713
+ input: messages,
12714
+ metadata: {
12715
+ ...rest
12716
+ }
12717
+ });
12718
+ logCompletionResponse(startTime, ret, span);
12719
+ return { data: ret, response };
12720
+ } finally {
12721
+ span.end();
12722
+ }
12662
12723
  }
12663
- });
12664
- logCompletionResponse(startTime, ret, span);
12665
- return ret;
12666
- } finally {
12667
- span.end();
12724
+ })();
12668
12725
  }
12669
- }
12726
+ return executionPromise;
12727
+ };
12728
+ const dataPromise = executeWrapped().then((result) => result.data);
12729
+ return new Proxy(dataPromise, {
12730
+ get(target, prop, receiver) {
12731
+ if (prop === "withResponse") {
12732
+ return executeWrapped;
12733
+ }
12734
+ const value = Reflect.get(target, prop, receiver);
12735
+ if (typeof value === "function") {
12736
+ return value.bind(target);
12737
+ }
12738
+ return value;
12739
+ }
12740
+ });
12670
12741
  };
12671
12742
  }
12672
12743
  function parseBaseParams(allParams, inputField) {
@@ -12725,7 +12796,7 @@ function parseChatCompletionParams(params) {
12725
12796
  function processEmbeddingResponse(result, span) {
12726
12797
  span.log({
12727
12798
  output: { embedding_length: result.data[0].embedding.length },
12728
- metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _182 => _182.usage]))
12799
+ metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _179 => _179.usage]))
12729
12800
  });
12730
12801
  }
12731
12802
  function processModerationResponse(result, span) {
@@ -12755,10 +12826,10 @@ function postprocessStreamingResults(allResults) {
12755
12826
  if (result.usage) {
12756
12827
  metrics = {
12757
12828
  ...metrics,
12758
- ...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _183 => _183.usage]))
12829
+ ...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _180 => _180.usage]))
12759
12830
  };
12760
12831
  }
12761
- const delta = _optionalChain([result, 'access', _184 => _184.choices, 'optionalAccess', _185 => _185[0], 'optionalAccess', _186 => _186.delta]);
12832
+ const delta = _optionalChain([result, 'access', _181 => _181.choices, 'optionalAccess', _182 => _182[0], 'optionalAccess', _183 => _183.delta]);
12762
12833
  if (!delta) {
12763
12834
  continue;
12764
12835
  }
@@ -12863,17 +12934,17 @@ function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens =
12863
12934
 
12864
12935
  // src/wrappers/ai-sdk-v2.ts
12865
12936
  function detectProviderFromResult(result) {
12866
- if (!_optionalChain([result, 'optionalAccess', _187 => _187.providerMetadata])) {
12937
+ if (!_optionalChain([result, 'optionalAccess', _184 => _184.providerMetadata])) {
12867
12938
  return void 0;
12868
12939
  }
12869
12940
  const keys = Object.keys(result.providerMetadata);
12870
- return _optionalChain([keys, 'optionalAccess', _188 => _188.at, 'call', _189 => _189(0)]);
12941
+ return _optionalChain([keys, 'optionalAccess', _185 => _185.at, 'call', _186 => _186(0)]);
12871
12942
  }
12872
12943
  function extractModelFromResult(result) {
12873
- if (_optionalChain([result, 'optionalAccess', _190 => _190.response, 'optionalAccess', _191 => _191.modelId])) {
12944
+ if (_optionalChain([result, 'optionalAccess', _187 => _187.response, 'optionalAccess', _188 => _188.modelId])) {
12874
12945
  return result.response.modelId;
12875
12946
  }
12876
- if (_optionalChain([result, 'optionalAccess', _192 => _192.request, 'optionalAccess', _193 => _193.body, 'optionalAccess', _194 => _194.model])) {
12947
+ if (_optionalChain([result, 'optionalAccess', _189 => _189.request, 'optionalAccess', _190 => _190.body, 'optionalAccess', _191 => _191.model])) {
12877
12948
  return result.request.body.model;
12878
12949
  }
12879
12950
  return void 0;
@@ -12933,7 +13004,7 @@ function normalizeUsageMetrics(usage, provider, providerMetadata) {
12933
13004
  metrics.prompt_cached_tokens = cachedInputTokens;
12934
13005
  }
12935
13006
  if (provider === "anthropic") {
12936
- const anthropicMetadata = _optionalChain([providerMetadata, 'optionalAccess', _195 => _195.anthropic]);
13007
+ const anthropicMetadata = _optionalChain([providerMetadata, 'optionalAccess', _192 => _192.anthropic]);
12937
13008
  if (anthropicMetadata) {
12938
13009
  const cacheReadTokens = getNumberProperty(anthropicMetadata.usage, "cache_read_input_tokens") || 0;
12939
13010
  const cacheCreationTokens = getNumberProperty(
@@ -13098,7 +13169,7 @@ function BraintrustMiddleware(config = {}) {
13098
13169
  // src/wrappers/ai-sdk-v1.ts
13099
13170
  function wrapAISDKModel(model) {
13100
13171
  const m = model;
13101
- if (_optionalChain([m, 'optionalAccess', _196 => _196.specificationVersion]) === "v1" && typeof _optionalChain([m, 'optionalAccess', _197 => _197.provider]) === "string" && typeof _optionalChain([m, 'optionalAccess', _198 => _198.modelId]) === "string") {
13172
+ if (_optionalChain([m, 'optionalAccess', _193 => _193.specificationVersion]) === "v1" && typeof _optionalChain([m, 'optionalAccess', _194 => _194.provider]) === "string" && typeof _optionalChain([m, 'optionalAccess', _195 => _195.modelId]) === "string") {
13102
13173
  return new BraintrustLanguageModelWrapper(m);
13103
13174
  } else {
13104
13175
  console.warn("Unsupported AI SDK model. Not wrapping.");
@@ -13154,10 +13225,10 @@ var BraintrustLanguageModelWrapper = class {
13154
13225
  metrics: {
13155
13226
  time_to_first_token: getCurrentUnixTimestamp() - startTime,
13156
13227
  tokens: !isEmpty(ret.usage) ? ret.usage.promptTokens + ret.usage.completionTokens : void 0,
13157
- prompt_tokens: _optionalChain([ret, 'access', _199 => _199.usage, 'optionalAccess', _200 => _200.promptTokens]),
13158
- completion_tokens: _optionalChain([ret, 'access', _201 => _201.usage, 'optionalAccess', _202 => _202.completionTokens]),
13228
+ prompt_tokens: _optionalChain([ret, 'access', _196 => _196.usage, 'optionalAccess', _197 => _197.promptTokens]),
13229
+ completion_tokens: _optionalChain([ret, 'access', _198 => _198.usage, 'optionalAccess', _199 => _199.completionTokens]),
13159
13230
  cached: parseCachedHeader(
13160
- _nullishCoalesce(_optionalChain([ret, 'access', _203 => _203.rawResponse, 'optionalAccess', _204 => _204.headers, 'optionalAccess', _205 => _205[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _206 => _206.rawResponse, 'optionalAccess', _207 => _207.headers, 'optionalAccess', _208 => _208[LEGACY_CACHED_HEADER]])))
13231
+ _nullishCoalesce(_optionalChain([ret, 'access', _200 => _200.rawResponse, 'optionalAccess', _201 => _201.headers, 'optionalAccess', _202 => _202[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _203 => _203.rawResponse, 'optionalAccess', _204 => _204.headers, 'optionalAccess', _205 => _205[LEGACY_CACHED_HEADER]])))
13161
13232
  )
13162
13233
  }
13163
13234
  });
@@ -13249,10 +13320,10 @@ var BraintrustLanguageModelWrapper = class {
13249
13320
  metrics: {
13250
13321
  time_to_first_token,
13251
13322
  tokens: !isEmpty(usage) ? usage.promptTokens + usage.completionTokens : void 0,
13252
- prompt_tokens: _optionalChain([usage, 'optionalAccess', _209 => _209.promptTokens]),
13253
- completion_tokens: _optionalChain([usage, 'optionalAccess', _210 => _210.completionTokens]),
13323
+ prompt_tokens: _optionalChain([usage, 'optionalAccess', _206 => _206.promptTokens]),
13324
+ completion_tokens: _optionalChain([usage, 'optionalAccess', _207 => _207.completionTokens]),
13254
13325
  cached: parseCachedHeader(
13255
- _nullishCoalesce(_optionalChain([ret, 'access', _211 => _211.rawResponse, 'optionalAccess', _212 => _212.headers, 'optionalAccess', _213 => _213[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _214 => _214.rawResponse, 'optionalAccess', _215 => _215.headers, 'optionalAccess', _216 => _216[LEGACY_CACHED_HEADER]])))
13326
+ _nullishCoalesce(_optionalChain([ret, 'access', _208 => _208.rawResponse, 'optionalAccess', _209 => _209.headers, 'optionalAccess', _210 => _210[X_CACHED_HEADER]]), () => ( _optionalChain([ret, 'access', _211 => _211.rawResponse, 'optionalAccess', _212 => _212.headers, 'optionalAccess', _213 => _213[LEGACY_CACHED_HEADER]])))
13256
13327
  )
13257
13328
  }
13258
13329
  });
@@ -13299,7 +13370,7 @@ function postProcessPrompt(prompt) {
13299
13370
  return [
13300
13371
  {
13301
13372
  role: "assistant",
13302
- content: _optionalChain([textPart, 'optionalAccess', _217 => _217.text]),
13373
+ content: _optionalChain([textPart, 'optionalAccess', _214 => _214.text]),
13303
13374
  ...toolCallParts.length > 0 ? {
13304
13375
  tool_calls: toolCallParts.map((part) => ({
13305
13376
  id: part.toolCallId,
@@ -13536,9 +13607,9 @@ function streamNextProxy(stream, sspan) {
13536
13607
  return result;
13537
13608
  }
13538
13609
  const item = result.value;
13539
- switch (_optionalChain([item, 'optionalAccess', _218 => _218.type])) {
13610
+ switch (_optionalChain([item, 'optionalAccess', _215 => _215.type])) {
13540
13611
  case "message_start":
13541
- const msg = _optionalChain([item, 'optionalAccess', _219 => _219.message]);
13612
+ const msg = _optionalChain([item, 'optionalAccess', _216 => _216.message]);
13542
13613
  if (msg) {
13543
13614
  const event = parseEventFromMessage(msg);
13544
13615
  totals = { ...totals, ...event.metrics };
@@ -13546,20 +13617,20 @@ function streamNextProxy(stream, sspan) {
13546
13617
  }
13547
13618
  break;
13548
13619
  case "content_block_delta":
13549
- if (_optionalChain([item, 'access', _220 => _220.delta, 'optionalAccess', _221 => _221.type]) === "text_delta") {
13550
- const text = _optionalChain([item, 'optionalAccess', _222 => _222.delta, 'optionalAccess', _223 => _223.text]);
13620
+ if (_optionalChain([item, 'access', _217 => _217.delta, 'optionalAccess', _218 => _218.type]) === "text_delta") {
13621
+ const text = _optionalChain([item, 'optionalAccess', _219 => _219.delta, 'optionalAccess', _220 => _220.text]);
13551
13622
  if (text) {
13552
13623
  deltas.push(text);
13553
13624
  }
13554
13625
  }
13555
13626
  break;
13556
13627
  case "message_delta":
13557
- const usage = _optionalChain([item, 'optionalAccess', _224 => _224.usage]);
13628
+ const usage = _optionalChain([item, 'optionalAccess', _221 => _221.usage]);
13558
13629
  if (usage) {
13559
13630
  const metrics = parseMetricsFromUsage2(usage);
13560
13631
  totals = { ...totals, ...metrics };
13561
13632
  }
13562
- const delta = _optionalChain([item, 'optionalAccess', _225 => _225.delta]);
13633
+ const delta = _optionalChain([item, 'optionalAccess', _222 => _222.delta]);
13563
13634
  if (delta) {
13564
13635
  metadata = { ...metadata, ...delta };
13565
13636
  }
@@ -13571,8 +13642,8 @@ function streamNextProxy(stream, sspan) {
13571
13642
  };
13572
13643
  }
13573
13644
  function parseEventFromMessage(message) {
13574
- const output = _optionalChain([message, 'optionalAccess', _226 => _226.content]) || null;
13575
- const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _227 => _227.usage]));
13645
+ const output = _optionalChain([message, 'optionalAccess', _223 => _223.content]) || null;
13646
+ const metrics = parseMetricsFromUsage2(_optionalChain([message, 'optionalAccess', _224 => _224.usage]));
13576
13647
  const metas = ["stop_reason", "stop_sequence"];
13577
13648
  const metadata = {};
13578
13649
  for (const m of metas) {
@@ -13695,7 +13766,7 @@ var AISpanProcessor = class _AISpanProcessor {
13695
13766
  if (!span) {
13696
13767
  return false;
13697
13768
  }
13698
- if (!span.parentSpanContext) {
13769
+ if (!span.parentSpanContext && !span.parentSpanId) {
13699
13770
  return true;
13700
13771
  }
13701
13772
  if (this.customFilter) {