braintrust 3.27.0 → 3.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dev/dist/index.d.mts +519 -186
- package/dev/dist/index.d.ts +519 -186
- package/dev/dist/index.js +1837 -1009
- package/dev/dist/index.mjs +1172 -344
- package/dist/apply-auto-instrumentation.js +262 -210
- package/dist/apply-auto-instrumentation.mjs +54 -2
- package/dist/auto-instrumentations/bundler/esbuild.cjs +81 -2
- package/dist/auto-instrumentations/bundler/esbuild.mjs +2 -2
- package/dist/auto-instrumentations/bundler/next.cjs +81 -2
- package/dist/auto-instrumentations/bundler/next.mjs +3 -3
- package/dist/auto-instrumentations/bundler/rollup.cjs +81 -2
- package/dist/auto-instrumentations/bundler/rollup.mjs +2 -2
- package/dist/auto-instrumentations/bundler/vite.cjs +81 -2
- package/dist/auto-instrumentations/bundler/vite.mjs +2 -2
- package/dist/auto-instrumentations/bundler/webpack-loader.cjs +81 -2
- package/dist/auto-instrumentations/bundler/webpack.cjs +81 -2
- package/dist/auto-instrumentations/bundler/webpack.mjs +3 -3
- package/dist/auto-instrumentations/{chunk-XEYKUBLY.mjs → chunk-26PKVUKB.mjs} +80 -2
- package/dist/auto-instrumentations/{chunk-BW33ULMW.mjs → chunk-HD35AM3M.mjs} +1 -1
- package/dist/auto-instrumentations/{chunk-ZNHTSSGI.mjs → chunk-NP7V4XB2.mjs} +2 -1
- package/dist/auto-instrumentations/hook.mjs +236 -30
- package/dist/auto-instrumentations/index.cjs +2 -1
- package/dist/auto-instrumentations/index.mjs +1 -1
- package/dist/browser.d.mts +628 -53
- package/dist/browser.d.ts +628 -53
- package/dist/browser.js +2301 -284
- package/dist/browser.mjs +2301 -284
- package/dist/{chunk-MF7NU6BT.js → chunk-BBE7SNRV.js} +34 -4
- package/dist/{chunk-QRHGVBKU.js → chunk-OBBWQW6K.js} +1799 -1023
- package/dist/{chunk-YKD22IMR.mjs → chunk-UPFNQCGB.mjs} +966 -190
- package/dist/{chunk-CZM5JIQL.mjs → chunk-ZHUHZWFY.mjs} +33 -3
- package/dist/cli.js +1224 -398
- package/dist/edge-light.d.mts +1 -1
- package/dist/edge-light.d.ts +1 -1
- package/dist/edge-light.js +2301 -284
- package/dist/edge-light.mjs +2301 -284
- package/dist/index.d.mts +1212 -637
- package/dist/index.d.ts +1212 -637
- package/dist/index.js +1880 -590
- package/dist/index.mjs +1450 -160
- package/dist/instrumentation/index.d.mts +190 -6
- package/dist/instrumentation/index.d.ts +190 -6
- package/dist/instrumentation/index.js +823 -116
- package/dist/instrumentation/index.mjs +823 -116
- package/dist/vitest-evals-reporter.js +16 -16
- package/dist/vitest-evals-reporter.mjs +2 -2
- package/dist/workerd.d.mts +1 -1
- package/dist/workerd.d.ts +1 -1
- package/dist/workerd.js +2301 -284
- package/dist/workerd.mjs +2301 -284
- package/package.json +2 -3
- package/util/dist/index.d.mts +1545 -100
- package/util/dist/index.d.ts +1545 -100
|
@@ -2575,7 +2575,8 @@ var AclObjectType = z6.union([
|
|
|
2575
2575
|
"org_member",
|
|
2576
2576
|
"project_log",
|
|
2577
2577
|
"org_project",
|
|
2578
|
-
"org_audit_logs"
|
|
2578
|
+
"org_audit_logs",
|
|
2579
|
+
"project_group"
|
|
2579
2580
|
]),
|
|
2580
2581
|
z6.null()
|
|
2581
2582
|
]);
|
|
@@ -2704,7 +2705,8 @@ var AsyncScoringState = z6.union([
|
|
|
2704
2705
|
token: z6.string(),
|
|
2705
2706
|
function_ids: z6.array(z6.unknown()),
|
|
2706
2707
|
skip_logging: z6.union([z6.boolean(), z6.null()]).optional(),
|
|
2707
|
-
triggered_functions: z6.union([z6.record(TriggeredFunctionState), z6.null()]).optional()
|
|
2708
|
+
triggered_functions: z6.union([z6.record(TriggeredFunctionState), z6.null()]).optional(),
|
|
2709
|
+
last_triggered_xact_id: z6.union([z6.string(), z6.number(), z6.null()]).optional()
|
|
2708
2710
|
}),
|
|
2709
2711
|
z6.object({ status: z6.literal("disabled") }),
|
|
2710
2712
|
z6.null(),
|
|
@@ -2773,7 +2775,7 @@ var FunctionTypeEnum = z6.enum([
|
|
|
2773
2775
|
"parameters",
|
|
2774
2776
|
"sandbox"
|
|
2775
2777
|
]);
|
|
2776
|
-
var
|
|
2778
|
+
var FacetPreprocessorId = z6.union([
|
|
2777
2779
|
z6.object({
|
|
2778
2780
|
type: z6.literal("function"),
|
|
2779
2781
|
id: z6.string(),
|
|
@@ -2784,10 +2786,23 @@ var NullableSavedFunctionId = z6.union([
|
|
|
2784
2786
|
name: z6.string(),
|
|
2785
2787
|
function_type: FunctionTypeEnum.optional().default("scorer")
|
|
2786
2788
|
}),
|
|
2789
|
+
z6.object({ type: z6.literal("inline"), code: z6.string().min(1) }),
|
|
2787
2790
|
z6.null()
|
|
2788
2791
|
]);
|
|
2792
|
+
var SavedFunctionId = z6.union([
|
|
2793
|
+
z6.object({
|
|
2794
|
+
type: z6.literal("function"),
|
|
2795
|
+
id: z6.string(),
|
|
2796
|
+
version: z6.string().optional()
|
|
2797
|
+
}),
|
|
2798
|
+
z6.object({
|
|
2799
|
+
type: z6.literal("global"),
|
|
2800
|
+
name: z6.string(),
|
|
2801
|
+
function_type: FunctionTypeEnum.optional().default("scorer")
|
|
2802
|
+
})
|
|
2803
|
+
]);
|
|
2789
2804
|
var TopicMapGenerationSettings = z6.object({
|
|
2790
|
-
algorithm: z6.enum(["hdbscan", "kmeans"]),
|
|
2805
|
+
algorithm: z6.enum(["hdbscan", "kmeans", "community"]),
|
|
2791
2806
|
dimension_reduction: z6.enum(["umap", "pca", "none"]),
|
|
2792
2807
|
sample_size: z6.number().int().gt(0).optional(),
|
|
2793
2808
|
n_clusters: z6.number().int().gt(0).optional(),
|
|
@@ -2799,6 +2814,7 @@ var TopicMapGenerationSettings = z6.object({
|
|
|
2799
2814
|
var TopicMapData = z6.object({
|
|
2800
2815
|
type: z6.literal("topic_map"),
|
|
2801
2816
|
source_facet: z6.string(),
|
|
2817
|
+
source_facet_function: SavedFunctionId.and(z6.unknown()).optional(),
|
|
2802
2818
|
embedding_model: z6.string(),
|
|
2803
2819
|
bundle_key: z6.string().optional(),
|
|
2804
2820
|
report_key: z6.string().optional(),
|
|
@@ -2812,7 +2828,7 @@ var TopicMapData = z6.object({
|
|
|
2812
2828
|
});
|
|
2813
2829
|
var BatchedFacetData = z6.object({
|
|
2814
2830
|
type: z6.literal("batched_facet"),
|
|
2815
|
-
preprocessor:
|
|
2831
|
+
preprocessor: FacetPreprocessorId.optional(),
|
|
2816
2832
|
facets: z6.array(
|
|
2817
2833
|
z6.object({
|
|
2818
2834
|
name: z6.string(),
|
|
@@ -3073,18 +3089,6 @@ var ObjectReferenceNullish = z6.union([
|
|
|
3073
3089
|
}),
|
|
3074
3090
|
z6.null()
|
|
3075
3091
|
]);
|
|
3076
|
-
var SavedFunctionId = z6.union([
|
|
3077
|
-
z6.object({
|
|
3078
|
-
type: z6.literal("function"),
|
|
3079
|
-
id: z6.string(),
|
|
3080
|
-
version: z6.string().optional()
|
|
3081
|
-
}),
|
|
3082
|
-
z6.object({
|
|
3083
|
-
type: z6.literal("global"),
|
|
3084
|
-
name: z6.string(),
|
|
3085
|
-
function_type: FunctionTypeEnum.optional().default("scorer")
|
|
3086
|
-
})
|
|
3087
|
-
]);
|
|
3088
3092
|
var DatasetEvent = z6.object({
|
|
3089
3093
|
id: z6.string(),
|
|
3090
3094
|
_xact_id: z6.string(),
|
|
@@ -3306,7 +3310,7 @@ var ExtendedSavedFunctionId = z6.union([
|
|
|
3306
3310
|
]);
|
|
3307
3311
|
var FacetData = z6.object({
|
|
3308
3312
|
type: z6.literal("facet"),
|
|
3309
|
-
preprocessor:
|
|
3313
|
+
preprocessor: FacetPreprocessorId.optional(),
|
|
3310
3314
|
prompt: z6.string(),
|
|
3311
3315
|
model: z6.string().optional(),
|
|
3312
3316
|
embedding_model: z6.string().optional(),
|
|
@@ -3811,6 +3815,19 @@ var MessageRole = z6.enum([
|
|
|
3811
3815
|
"model",
|
|
3812
3816
|
"developer"
|
|
3813
3817
|
]);
|
|
3818
|
+
var NullableSavedFunctionId = z6.union([
|
|
3819
|
+
z6.object({
|
|
3820
|
+
type: z6.literal("function"),
|
|
3821
|
+
id: z6.string(),
|
|
3822
|
+
version: z6.string().optional()
|
|
3823
|
+
}),
|
|
3824
|
+
z6.object({
|
|
3825
|
+
type: z6.literal("global"),
|
|
3826
|
+
name: z6.string(),
|
|
3827
|
+
function_type: FunctionTypeEnum.optional().default("scorer")
|
|
3828
|
+
}),
|
|
3829
|
+
z6.null()
|
|
3830
|
+
]);
|
|
3814
3831
|
var ObjectReference = z6.object({
|
|
3815
3832
|
object_type: z6.enum([
|
|
3816
3833
|
"project_logs",
|
|
@@ -3832,6 +3849,7 @@ var TraceScope = z6.object({
|
|
|
3832
3849
|
});
|
|
3833
3850
|
var OnlineScoreConfig = z6.union([
|
|
3834
3851
|
z6.object({
|
|
3852
|
+
status: AutomationStatus.optional(),
|
|
3835
3853
|
sampling_rate: z6.number().gte(0).lte(1),
|
|
3836
3854
|
scorers: z6.array(SavedFunctionId),
|
|
3837
3855
|
btql_filter: z6.union([z6.string(), z6.null()]).optional(),
|
|
@@ -3912,6 +3930,72 @@ var Project = z6.object({
|
|
|
3912
3930
|
user_id: z6.union([z6.string(), z6.null()]).optional(),
|
|
3913
3931
|
settings: ProjectSettings.optional()
|
|
3914
3932
|
});
|
|
3933
|
+
var WindowedAutomationConfig = z6.object({
|
|
3934
|
+
event_type: z6.literal("windowed"),
|
|
3935
|
+
product_origin: z6.union([z6.literal("patterns"), z6.null()]).optional(),
|
|
3936
|
+
status: AutomationStatus.optional(),
|
|
3937
|
+
threshold: z6.object({
|
|
3938
|
+
calculation: z6.object({
|
|
3939
|
+
type: z6.literal("btql"),
|
|
3940
|
+
btql_query: z6.string().min(1),
|
|
3941
|
+
output: z6.object({
|
|
3942
|
+
type: z6.literal("scalar"),
|
|
3943
|
+
value_column: z6.string().min(1)
|
|
3944
|
+
})
|
|
3945
|
+
}),
|
|
3946
|
+
policy: z6.object({
|
|
3947
|
+
condition: z6.object({
|
|
3948
|
+
type: z6.literal("threshold"),
|
|
3949
|
+
operator: z6.enum(["lt", "lte", "gt", "gte", "eq", "neq"]),
|
|
3950
|
+
threshold: z6.number()
|
|
3951
|
+
}),
|
|
3952
|
+
pending_seconds: z6.number().int().gte(0).lte(2592e3),
|
|
3953
|
+
no_data_behavior: z6.enum(["keep_last", "resolve", "alert"]),
|
|
3954
|
+
renotify_interval_seconds: z6.union([z6.number(), z6.null()]).optional(),
|
|
3955
|
+
notify_on_recovery: z6.boolean().optional().default(true)
|
|
3956
|
+
})
|
|
3957
|
+
}).optional(),
|
|
3958
|
+
window: z6.object({
|
|
3959
|
+
window_seconds: z6.number().int().gte(1).lte(2592e3),
|
|
3960
|
+
schedule: z6.union([
|
|
3961
|
+
z6.object({
|
|
3962
|
+
type: z6.literal("interval"),
|
|
3963
|
+
evaluation_interval_seconds: z6.number().int().gte(1).lte(2592e3)
|
|
3964
|
+
}),
|
|
3965
|
+
z6.object({
|
|
3966
|
+
type: z6.literal("cron"),
|
|
3967
|
+
cron_expression: z6.string().min(1),
|
|
3968
|
+
timezone: z6.union([z6.string(), z6.null()]).optional()
|
|
3969
|
+
})
|
|
3970
|
+
]),
|
|
3971
|
+
evaluation_delay_seconds: z6.number().int().gte(0).lte(2592e3)
|
|
3972
|
+
}),
|
|
3973
|
+
loop: z6.object({
|
|
3974
|
+
prompt: z6.string().min(1).max(1e4),
|
|
3975
|
+
include_trigger_input: z6.boolean().optional().default(false),
|
|
3976
|
+
agent_slug: z6.string().min(1),
|
|
3977
|
+
auto_approve_tools: z6.array(z6.string().min(1)).optional().default([]),
|
|
3978
|
+
harness: z6.enum(["native", "codex", "claude-code"]).optional(),
|
|
3979
|
+
model: z6.string().min(1).optional(),
|
|
3980
|
+
reasoning_effort: z6.enum(["none", "minimal", "low", "medium", "high", "xhigh", "max"]).optional()
|
|
3981
|
+
}).optional(),
|
|
3982
|
+
actions: z6.array(
|
|
3983
|
+
z6.union([
|
|
3984
|
+
z6.object({
|
|
3985
|
+
type: z6.literal("webhook"),
|
|
3986
|
+
url: z6.string(),
|
|
3987
|
+
formatting_prompt: z6.string().min(1).max(1e4).optional()
|
|
3988
|
+
}),
|
|
3989
|
+
z6.object({
|
|
3990
|
+
type: z6.literal("slack"),
|
|
3991
|
+
workspace_id: z6.string(),
|
|
3992
|
+
channel: z6.string(),
|
|
3993
|
+
message_template: z6.string().optional(),
|
|
3994
|
+
formatting_prompt: z6.string().min(1).max(1e4).optional()
|
|
3995
|
+
})
|
|
3996
|
+
])
|
|
3997
|
+
).max(20).optional().default([])
|
|
3998
|
+
});
|
|
3915
3999
|
var TopicAutomationFacetModel = z6.union([
|
|
3916
4000
|
z6.enum(["brain-facet-latest", "brain-facet-1", "brain-facet-2"]),
|
|
3917
4001
|
z6.null()
|
|
@@ -3953,7 +4037,8 @@ var TopicDigestAutomationConfig = z6.object({
|
|
|
3953
4037
|
type: z6.literal("slack"),
|
|
3954
4038
|
workspace_id: z6.string(),
|
|
3955
4039
|
channel: z6.string(),
|
|
3956
|
-
message_template: z6.string().optional()
|
|
4040
|
+
message_template: z6.string().optional(),
|
|
4041
|
+
formatting_prompt: z6.string().min(1).max(1e4).optional()
|
|
3957
4042
|
}),
|
|
3958
4043
|
topic_map_function_ids: z6.array(z6.string()).max(10).optional()
|
|
3959
4044
|
});
|
|
@@ -3967,15 +4052,21 @@ var ProjectAutomation = z6.object({
|
|
|
3967
4052
|
config: z6.union([
|
|
3968
4053
|
z6.object({
|
|
3969
4054
|
event_type: z6.literal("logs"),
|
|
4055
|
+
status: AutomationStatus.optional(),
|
|
3970
4056
|
btql_filter: z6.string(),
|
|
3971
4057
|
interval_seconds: z6.number().gte(1).lte(2592e3),
|
|
3972
4058
|
action: z6.union([
|
|
3973
|
-
z6.object({
|
|
4059
|
+
z6.object({
|
|
4060
|
+
type: z6.literal("webhook"),
|
|
4061
|
+
url: z6.string(),
|
|
4062
|
+
formatting_prompt: z6.string().min(1).max(1e4).optional()
|
|
4063
|
+
}),
|
|
3974
4064
|
z6.object({
|
|
3975
4065
|
type: z6.literal("slack"),
|
|
3976
4066
|
workspace_id: z6.string(),
|
|
3977
4067
|
channel: z6.string(),
|
|
3978
|
-
message_template: z6.string().optional()
|
|
4068
|
+
message_template: z6.string().optional(),
|
|
4069
|
+
formatting_prompt: z6.string().min(1).max(1e4).optional()
|
|
3979
4070
|
})
|
|
3980
4071
|
])
|
|
3981
4072
|
}),
|
|
@@ -4026,21 +4117,38 @@ var ProjectAutomation = z6.object({
|
|
|
4026
4117
|
}),
|
|
4027
4118
|
z6.object({
|
|
4028
4119
|
event_type: z6.literal("environment_update"),
|
|
4120
|
+
status: AutomationStatus.optional(),
|
|
4029
4121
|
environment_filter: z6.array(z6.string()).optional(),
|
|
4030
4122
|
action: z6.union([
|
|
4031
|
-
z6.object({
|
|
4123
|
+
z6.object({
|
|
4124
|
+
type: z6.literal("webhook"),
|
|
4125
|
+
url: z6.string(),
|
|
4126
|
+
formatting_prompt: z6.string().min(1).max(1e4).optional()
|
|
4127
|
+
}),
|
|
4032
4128
|
z6.object({
|
|
4033
4129
|
type: z6.literal("slack"),
|
|
4034
4130
|
workspace_id: z6.string(),
|
|
4035
4131
|
channel: z6.string(),
|
|
4036
|
-
message_template: z6.string().optional()
|
|
4132
|
+
message_template: z6.string().optional(),
|
|
4133
|
+
formatting_prompt: z6.string().min(1).max(1e4).optional()
|
|
4037
4134
|
})
|
|
4038
4135
|
])
|
|
4039
4136
|
}),
|
|
4137
|
+
WindowedAutomationConfig,
|
|
4040
4138
|
TopicAutomationConfig,
|
|
4041
4139
|
TopicDigestAutomationConfig
|
|
4042
4140
|
])
|
|
4043
4141
|
});
|
|
4142
|
+
var ProjectGroup = z6.object({
|
|
4143
|
+
id: z6.string().uuid(),
|
|
4144
|
+
org_id: z6.string().uuid(),
|
|
4145
|
+
user_id: z6.union([z6.string(), z6.null()]).optional(),
|
|
4146
|
+
created: z6.union([z6.string(), z6.null()]).optional(),
|
|
4147
|
+
name: z6.string(),
|
|
4148
|
+
description: z6.union([z6.string(), z6.null()]).optional(),
|
|
4149
|
+
deleted_at: z6.union([z6.string(), z6.null()]).optional(),
|
|
4150
|
+
member_projects: z6.array(z6.string().uuid()).max(1e4)
|
|
4151
|
+
});
|
|
4044
4152
|
var ProjectLogsEvent = z6.object({
|
|
4045
4153
|
id: z6.string(),
|
|
4046
4154
|
_xact_id: z6.string(),
|
|
@@ -4258,7 +4366,8 @@ var RunEval = z6.object({
|
|
|
4258
4366
|
dataset_environment: z6.union([z6.string(), z6.null()]).optional(),
|
|
4259
4367
|
_internal_btql: z6.union([z6.object({}).partial().passthrough(), z6.null()]).optional()
|
|
4260
4368
|
}),
|
|
4261
|
-
z6.object({ data: z6.array(z6.unknown()) })
|
|
4369
|
+
z6.object({ data: z6.array(z6.unknown()) }),
|
|
4370
|
+
z6.object({ experiment_name: z6.string() })
|
|
4262
4371
|
]),
|
|
4263
4372
|
name: z6.string().optional(),
|
|
4264
4373
|
parameters: z6.object({}).partial().passthrough().optional(),
|
|
@@ -4463,7 +4572,9 @@ var View = z6.object({
|
|
|
4463
4572
|
"for_review_datasets"
|
|
4464
4573
|
]),
|
|
4465
4574
|
name: z6.string(),
|
|
4575
|
+
description: z6.union([z6.string(), z6.null()]).optional(),
|
|
4466
4576
|
created: z6.union([z6.string(), z6.null()]).optional(),
|
|
4577
|
+
updated_at: z6.union([z6.string(), z6.null()]).optional(),
|
|
4467
4578
|
view_data: ViewData.optional(),
|
|
4468
4579
|
options: ViewOptions.optional(),
|
|
4469
4580
|
user_id: z6.union([z6.string(), z6.null()]).optional(),
|
|
@@ -5562,12 +5673,13 @@ var INSTRUMENTATION_NAMES = {
|
|
|
5562
5673
|
OPENROUTER: "openrouter",
|
|
5563
5674
|
OPENROUTER_AGENT: "openrouter-agent",
|
|
5564
5675
|
PI_CODING_AGENT: "pi-coding-agent",
|
|
5565
|
-
STRANDS_AGENT_SDK: "strands-agent-sdk"
|
|
5676
|
+
STRANDS_AGENT_SDK: "strands-agent-sdk",
|
|
5677
|
+
VOYAGEAI: "voyageai"
|
|
5566
5678
|
};
|
|
5567
5679
|
var INTERNAL_SPAN_INSTRUMENTATION_NAME = /* @__PURE__ */ Symbol.for(
|
|
5568
5680
|
"braintrust.spanInstrumentationName"
|
|
5569
5681
|
);
|
|
5570
|
-
var SDK_VERSION = true ? "3.
|
|
5682
|
+
var SDK_VERSION = true ? "3.28.0" : "0.0.0";
|
|
5571
5683
|
function withSpanInstrumentationName(args, instrumentationName) {
|
|
5572
5684
|
return {
|
|
5573
5685
|
...args,
|
|
@@ -5762,6 +5874,9 @@ function applyMaskingToField(maskingFunction, data, fieldName) {
|
|
|
5762
5874
|
var INITIAL_SPAN_WRITE_AS_MERGE = /* @__PURE__ */ Symbol(
|
|
5763
5875
|
"braintrust.initial-span-write-as-merge"
|
|
5764
5876
|
);
|
|
5877
|
+
var RESUME_SPAN_WITHOUT_INITIAL_WRITE = /* @__PURE__ */ Symbol(
|
|
5878
|
+
"braintrust.resume-span-without-initial-write"
|
|
5879
|
+
);
|
|
5765
5880
|
var INTERNAL_SPAN_CONTEXT = /* @__PURE__ */ Symbol("braintrust.internal-span-context");
|
|
5766
5881
|
var BRAINTRUST_CURRENT_SPAN_STORE = /* @__PURE__ */ Symbol.for(
|
|
5767
5882
|
"braintrust.currentSpanStore"
|
|
@@ -8611,7 +8726,7 @@ var ObjectFetcher = class {
|
|
|
8611
8726
|
const objectId = await this.id;
|
|
8612
8727
|
const batchLimit = batchSize ?? DEFAULT_FETCH_BATCH_SIZE;
|
|
8613
8728
|
const internalLimit = getInternalBtqlLimit(this._internal_btql);
|
|
8614
|
-
|
|
8729
|
+
let remainingLimit = internalLimit;
|
|
8615
8730
|
const internalBtqlWithoutReservedQueryKeys = Object.fromEntries(
|
|
8616
8731
|
Object.entries(this._internal_btql ?? {}).filter(
|
|
8617
8732
|
([key]) => key !== "cursor" && key !== "limit" && key !== "select" && key !== "from"
|
|
@@ -8620,6 +8735,10 @@ var ObjectFetcher = class {
|
|
|
8620
8735
|
let cursor = void 0;
|
|
8621
8736
|
let iterations = 0;
|
|
8622
8737
|
while (true) {
|
|
8738
|
+
if (remainingLimit !== void 0 && remainingLimit <= 0) {
|
|
8739
|
+
return;
|
|
8740
|
+
}
|
|
8741
|
+
const limit = remainingLimit === void 0 ? batchLimit : Math.min(batchLimit, remainingLimit);
|
|
8623
8742
|
const resp = await state.apiConn().post(
|
|
8624
8743
|
`btql`,
|
|
8625
8744
|
{
|
|
@@ -8659,7 +8778,14 @@ var ObjectFetcher = class {
|
|
|
8659
8778
|
const respJson = await resp.json();
|
|
8660
8779
|
const mutate = this.mutateRecord;
|
|
8661
8780
|
for (const record of respJson.data ?? []) {
|
|
8662
|
-
|
|
8781
|
+
if (remainingLimit !== void 0 && remainingLimit <= 0) {
|
|
8782
|
+
return;
|
|
8783
|
+
}
|
|
8784
|
+
const mutatedRecord = mutate ? mutate(record) : record;
|
|
8785
|
+
if (remainingLimit !== void 0) {
|
|
8786
|
+
remainingLimit--;
|
|
8787
|
+
}
|
|
8788
|
+
yield mutatedRecord;
|
|
8663
8789
|
}
|
|
8664
8790
|
if (!respJson.cursor) {
|
|
8665
8791
|
break;
|
|
@@ -9136,7 +9262,9 @@ var SpanImpl = class _SpanImpl {
|
|
|
9136
9262
|
this._rootSpanId = resolvedIds.rootSpanId;
|
|
9137
9263
|
this._spanParents = resolvedIds.spanParents;
|
|
9138
9264
|
this.isMerge = args[INITIAL_SPAN_WRITE_AS_MERGE] === true;
|
|
9139
|
-
|
|
9265
|
+
if (!args[RESUME_SPAN_WITHOUT_INITIAL_WRITE]) {
|
|
9266
|
+
this.logInternal({ event, internalData });
|
|
9267
|
+
}
|
|
9140
9268
|
this.isMerge = true;
|
|
9141
9269
|
}
|
|
9142
9270
|
getParentInfo() {
|
|
@@ -11015,6 +11143,22 @@ function processInputAttachments(input) {
|
|
|
11015
11143
|
};
|
|
11016
11144
|
}
|
|
11017
11145
|
}
|
|
11146
|
+
const voyageBase64Key = node.type === "image_base64" ? Object.hasOwn(node, "imageBase64") ? "imageBase64" : "image_base64" : node.type === "video_base64" ? Object.hasOwn(node, "videoBase64") ? "videoBase64" : "video_base64" : void 0;
|
|
11147
|
+
const voyageBase64Value = voyageBase64Key ? node[voyageBase64Key] : void 0;
|
|
11148
|
+
if (voyageBase64Key && typeof voyageBase64Value === "string" && voyageBase64Value.startsWith("data:")) {
|
|
11149
|
+
const mediaType = inferMediaTypeFromDataUrl(
|
|
11150
|
+
voyageBase64Value,
|
|
11151
|
+
node.type === "video_base64" ? "video/mp4" : "image/png"
|
|
11152
|
+
);
|
|
11153
|
+
const filename = `${node.type === "video_base64" ? "video" : "image"}.${getExtensionFromMediaType(mediaType)}`;
|
|
11154
|
+
const attachment = toAttachment(voyageBase64Value, mediaType, filename);
|
|
11155
|
+
if (attachment) {
|
|
11156
|
+
return {
|
|
11157
|
+
...node,
|
|
11158
|
+
[voyageBase64Key]: attachment
|
|
11159
|
+
};
|
|
11160
|
+
}
|
|
11161
|
+
}
|
|
11018
11162
|
if (node.type === "file" && node.file && typeof node.file === "object" && typeof node.file.file_data === "string" && node.file.file_data.startsWith("data:")) {
|
|
11019
11163
|
const mediaType = inferMediaTypeFromDataUrl(
|
|
11020
11164
|
node.file.file_data,
|
|
@@ -12362,12 +12506,30 @@ function logInstrumentationError(context, error) {
|
|
|
12362
12506
|
|
|
12363
12507
|
// src/wrappers/anthropic-tokens-util.ts
|
|
12364
12508
|
function finalizeAnthropicTokens(metrics) {
|
|
12365
|
-
const
|
|
12366
|
-
|
|
12509
|
+
const hasSplitCacheCreationTokens = metrics.prompt_cache_creation_5m_tokens !== void 0 || metrics.prompt_cache_creation_1h_tokens !== void 0;
|
|
12510
|
+
const splitCacheCreationTokens = (metrics.prompt_cache_creation_5m_tokens || 0) + (metrics.prompt_cache_creation_1h_tokens || 0);
|
|
12511
|
+
const aggregateCacheCreationTokens = metrics.prompt_cache_creation_tokens || 0;
|
|
12512
|
+
const effectiveCacheCreationTokens = Math.max(
|
|
12513
|
+
aggregateCacheCreationTokens,
|
|
12514
|
+
splitCacheCreationTokens
|
|
12515
|
+
);
|
|
12516
|
+
const prompt_tokens = (metrics.prompt_tokens || 0) + (metrics.prompt_cached_tokens || 0) + effectiveCacheCreationTokens;
|
|
12517
|
+
const finalized = {
|
|
12367
12518
|
...metrics,
|
|
12368
12519
|
prompt_tokens,
|
|
12369
12520
|
tokens: prompt_tokens + (metrics.completion_tokens || 0)
|
|
12370
12521
|
};
|
|
12522
|
+
if (hasSplitCacheCreationTokens && splitCacheCreationTokens >= aggregateCacheCreationTokens) {
|
|
12523
|
+
delete finalized.prompt_cache_creation_tokens;
|
|
12524
|
+
}
|
|
12525
|
+
return finalized;
|
|
12526
|
+
}
|
|
12527
|
+
function toNumericMetrics(metrics) {
|
|
12528
|
+
return Object.fromEntries(
|
|
12529
|
+
Object.entries(metrics).filter(
|
|
12530
|
+
(entry) => entry[1] !== void 0
|
|
12531
|
+
)
|
|
12532
|
+
);
|
|
12371
12533
|
}
|
|
12372
12534
|
function extractAnthropicCacheTokens(cacheReadTokens = 0, cacheCreationTokens = 0) {
|
|
12373
12535
|
const cacheTokens = {};
|
|
@@ -12960,6 +13122,18 @@ function parseMetricsFromUsage2(usage) {
|
|
|
12960
13122
|
saveIfExistsTo("output_tokens", "completion_tokens");
|
|
12961
13123
|
saveIfExistsTo("cache_read_input_tokens", "prompt_cached_tokens");
|
|
12962
13124
|
saveIfExistsTo("cache_creation_input_tokens", "prompt_cache_creation_tokens");
|
|
13125
|
+
if (isObject(usage.cache_creation)) {
|
|
13126
|
+
const cacheCreation = usage.cache_creation;
|
|
13127
|
+
for (const [source, target] of [
|
|
13128
|
+
["ephemeral_5m_input_tokens", "prompt_cache_creation_5m_tokens"],
|
|
13129
|
+
["ephemeral_1h_input_tokens", "prompt_cache_creation_1h_tokens"]
|
|
13130
|
+
]) {
|
|
13131
|
+
const value = cacheCreation[source];
|
|
13132
|
+
if (typeof value === "number") {
|
|
13133
|
+
metrics[target] = value;
|
|
13134
|
+
}
|
|
13135
|
+
}
|
|
13136
|
+
}
|
|
12963
13137
|
if (isObject(usage.server_tool_use)) {
|
|
12964
13138
|
for (const [name, value] of Object.entries(usage.server_tool_use)) {
|
|
12965
13139
|
if (typeof value === "number") {
|
|
@@ -18258,37 +18432,97 @@ function seedTaskToolUseIdMapping(taskIdToToolUseId, message) {
|
|
|
18258
18432
|
taskIdToToolUseId.set(message.task_id, message.tool_use_id);
|
|
18259
18433
|
}
|
|
18260
18434
|
}
|
|
18261
|
-
function
|
|
18262
|
-
|
|
18263
|
-
|
|
18264
|
-
|
|
18265
|
-
usage = message.message?.usage;
|
|
18266
|
-
} else if (message.type === "result") {
|
|
18267
|
-
usage = message.usage;
|
|
18268
|
-
}
|
|
18435
|
+
function tokenCount(value) {
|
|
18436
|
+
return typeof value === "number" && Number.isFinite(value) && Number.isInteger(value) && value >= 0 ? value : void 0;
|
|
18437
|
+
}
|
|
18438
|
+
function copyUsage(usage) {
|
|
18269
18439
|
if (!usage || typeof usage !== "object") {
|
|
18270
|
-
return
|
|
18440
|
+
return void 0;
|
|
18441
|
+
}
|
|
18442
|
+
const copy = {};
|
|
18443
|
+
for (const key of [
|
|
18444
|
+
"input_tokens",
|
|
18445
|
+
"output_tokens",
|
|
18446
|
+
"cache_read_input_tokens",
|
|
18447
|
+
"cache_creation_input_tokens"
|
|
18448
|
+
]) {
|
|
18449
|
+
const value = tokenCount(Reflect.get(usage, key));
|
|
18450
|
+
if (value !== void 0) {
|
|
18451
|
+
copy[key] = value;
|
|
18452
|
+
}
|
|
18453
|
+
}
|
|
18454
|
+
const cacheCreation = Reflect.get(usage, "cache_creation");
|
|
18455
|
+
if (cacheCreation && typeof cacheCreation === "object") {
|
|
18456
|
+
const cacheCreationCopy = {};
|
|
18457
|
+
for (const key of [
|
|
18458
|
+
"ephemeral_5m_input_tokens",
|
|
18459
|
+
"ephemeral_1h_input_tokens"
|
|
18460
|
+
]) {
|
|
18461
|
+
const value = tokenCount(Reflect.get(cacheCreation, key));
|
|
18462
|
+
if (value !== void 0) {
|
|
18463
|
+
cacheCreationCopy[key] = value;
|
|
18464
|
+
}
|
|
18465
|
+
}
|
|
18466
|
+
if (Object.keys(cacheCreationCopy).length > 0) {
|
|
18467
|
+
copy.cache_creation = cacheCreationCopy;
|
|
18468
|
+
}
|
|
18469
|
+
}
|
|
18470
|
+
return Object.keys(copy).length > 0 ? copy : void 0;
|
|
18471
|
+
}
|
|
18472
|
+
function mergeUsage(base, override) {
|
|
18473
|
+
if (!base || !override) {
|
|
18474
|
+
return override ?? base;
|
|
18475
|
+
}
|
|
18476
|
+
const cacheCreation = base.cache_creation || override.cache_creation ? { ...base.cache_creation, ...override.cache_creation } : void 0;
|
|
18477
|
+
return {
|
|
18478
|
+
...base,
|
|
18479
|
+
...override,
|
|
18480
|
+
...cacheCreation && { cache_creation: cacheCreation }
|
|
18481
|
+
};
|
|
18482
|
+
}
|
|
18483
|
+
function extractUsage(usage, includeOutput) {
|
|
18484
|
+
const metrics = {};
|
|
18485
|
+
if (!usage) {
|
|
18486
|
+
return {};
|
|
18271
18487
|
}
|
|
18272
18488
|
const inputTokens = getNumberProperty(usage, "input_tokens");
|
|
18273
18489
|
if (inputTokens !== void 0) {
|
|
18274
18490
|
metrics.prompt_tokens = inputTokens;
|
|
18275
18491
|
}
|
|
18276
|
-
|
|
18277
|
-
|
|
18278
|
-
|
|
18492
|
+
if (includeOutput) {
|
|
18493
|
+
const outputTokens = getNumberProperty(usage, "output_tokens");
|
|
18494
|
+
if (outputTokens !== void 0) {
|
|
18495
|
+
metrics.completion_tokens = outputTokens;
|
|
18496
|
+
}
|
|
18279
18497
|
}
|
|
18280
18498
|
const cacheReadTokens = getNumberProperty(usage, "cache_read_input_tokens") || 0;
|
|
18281
18499
|
const cacheCreationTokens = getNumberProperty(usage, "cache_creation_input_tokens") || 0;
|
|
18282
|
-
|
|
18283
|
-
|
|
18284
|
-
|
|
18285
|
-
|
|
18286
|
-
|
|
18500
|
+
Object.assign(
|
|
18501
|
+
metrics,
|
|
18502
|
+
extractAnthropicCacheTokens(cacheReadTokens, cacheCreationTokens)
|
|
18503
|
+
);
|
|
18504
|
+
const cacheCreation5mTokens = getNumberProperty(
|
|
18505
|
+
usage.cache_creation,
|
|
18506
|
+
"ephemeral_5m_input_tokens"
|
|
18507
|
+
);
|
|
18508
|
+
const cacheCreation1hTokens = getNumberProperty(
|
|
18509
|
+
usage.cache_creation,
|
|
18510
|
+
"ephemeral_1h_input_tokens"
|
|
18511
|
+
);
|
|
18512
|
+
if (cacheCreation5mTokens !== void 0) {
|
|
18513
|
+
metrics.prompt_cache_creation_5m_tokens = cacheCreation5mTokens;
|
|
18287
18514
|
}
|
|
18288
|
-
if (
|
|
18289
|
-
|
|
18515
|
+
if (cacheCreation1hTokens !== void 0) {
|
|
18516
|
+
metrics.prompt_cache_creation_1h_tokens = cacheCreation1hTokens;
|
|
18290
18517
|
}
|
|
18291
|
-
|
|
18518
|
+
if (Object.keys(metrics).length === 0) {
|
|
18519
|
+
return {};
|
|
18520
|
+
}
|
|
18521
|
+
const finalized = finalizeAnthropicTokens(metrics);
|
|
18522
|
+
if (metrics.completion_tokens === void 0) {
|
|
18523
|
+
delete finalized.tokens;
|
|
18524
|
+
}
|
|
18525
|
+
return toNumericMetrics(finalized);
|
|
18292
18526
|
}
|
|
18293
18527
|
function buildLLMInput(promptMessages, conversationHistory) {
|
|
18294
18528
|
const inputParts = [...promptMessages, ...conversationHistory];
|
|
@@ -18322,16 +18556,16 @@ function buildRootPromptMessages(prompt, capturedPromptMessages) {
|
|
|
18322
18556
|
function formatCapturedMessages(messages) {
|
|
18323
18557
|
return messages.length > 0 ? messages : [];
|
|
18324
18558
|
}
|
|
18325
|
-
async function createLLMSpanForMessages(messages, promptMessages, conversationHistory, options, startTime, parentSpan, existingSpan) {
|
|
18559
|
+
async function createLLMSpanForMessages(messages, promptMessages, conversationHistory, options, startTime, parentSpan, usage, hasFinalOutputUsage, existingSpan) {
|
|
18326
18560
|
if (messages.length === 0) {
|
|
18327
18561
|
return void 0;
|
|
18328
18562
|
}
|
|
18329
18563
|
const lastMessage = messages[messages.length - 1];
|
|
18330
|
-
if (lastMessage.type !== "assistant"
|
|
18564
|
+
if (lastMessage.type !== "assistant") {
|
|
18331
18565
|
return void 0;
|
|
18332
18566
|
}
|
|
18333
|
-
const model = lastMessage.message
|
|
18334
|
-
const
|
|
18567
|
+
const model = lastMessage.message?.model || options.model;
|
|
18568
|
+
const metrics = options.includePartialMessages ? extractUsage(usage, hasFinalOutputUsage) : {};
|
|
18335
18569
|
const input = buildLLMInput(promptMessages, conversationHistory);
|
|
18336
18570
|
const outputs = messages.map(
|
|
18337
18571
|
(m) => m.message?.content && m.message?.role ? { content: m.message.content, role: m.message.role } : void 0
|
|
@@ -18353,8 +18587,8 @@ async function createLLMSpanForMessages(messages, promptMessages, conversationHi
|
|
|
18353
18587
|
);
|
|
18354
18588
|
span.log({
|
|
18355
18589
|
input,
|
|
18356
|
-
metadata: model
|
|
18357
|
-
metrics:
|
|
18590
|
+
metadata: { ...model && { model }, provider: "anthropic" },
|
|
18591
|
+
...Object.keys(metrics).length > 0 ? { metrics } : {},
|
|
18358
18592
|
output: outputs
|
|
18359
18593
|
});
|
|
18360
18594
|
const spanExport = await span.export();
|
|
@@ -18444,11 +18678,17 @@ function prepareLocalToolHandlersInMcpServers(mcpServers) {
|
|
|
18444
18678
|
}
|
|
18445
18679
|
return { hasLocalToolHandlers, localToolHookNames };
|
|
18446
18680
|
}
|
|
18447
|
-
function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers, localToolHookNames, skipLocalToolHooks, subAgentDetailsByToolUseId, subAgentSpans, endedSubAgentSpans) {
|
|
18681
|
+
function createToolTracingHooks(resolveParentSpan, taskIdToToolUseId, toolUseToParent, activeToolSpans, mcpServers, localToolHookNames, skipLocalToolHooks, subAgentDetailsByToolUseId, subAgentSpans, endedSubAgentSpans) {
|
|
18448
18682
|
const preToolUse = async (input, toolUseID) => {
|
|
18449
18683
|
if (input.hook_event_name !== "PreToolUse" || !toolUseID) {
|
|
18450
18684
|
return {};
|
|
18451
18685
|
}
|
|
18686
|
+
if (!toolUseToParent.has(toolUseID) && input.agent_id) {
|
|
18687
|
+
const parentToolUseId = taskIdToToolUseId.get(input.agent_id);
|
|
18688
|
+
if (parentToolUseId) {
|
|
18689
|
+
toolUseToParent.set(toolUseID, parentToolUseId);
|
|
18690
|
+
}
|
|
18691
|
+
}
|
|
18452
18692
|
if (skipLocalToolHooks && (isLocalToolUse(input.tool_name, mcpServers) || localToolHookNames.has(input.tool_name))) {
|
|
18453
18693
|
return {};
|
|
18454
18694
|
}
|
|
@@ -18644,9 +18884,6 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
|
|
|
18644
18884
|
}
|
|
18645
18885
|
const metadata = {
|
|
18646
18886
|
...subAgentDetailsToMetadata(details),
|
|
18647
|
-
...input.agent_transcript_path && {
|
|
18648
|
-
"claude_agent_sdk.agent_transcript_path": input.agent_transcript_path
|
|
18649
|
-
},
|
|
18650
18887
|
"claude_agent_sdk.stop_hook_active": input.stop_hook_active
|
|
18651
18888
|
};
|
|
18652
18889
|
try {
|
|
@@ -18668,7 +18905,7 @@ function createToolTracingHooks(resolveParentSpan, activeToolSpans, mcpServers,
|
|
|
18668
18905
|
subagentStop
|
|
18669
18906
|
};
|
|
18670
18907
|
}
|
|
18671
|
-
function injectTracingHooks(options, resolveParentSpan, activeToolSpans, localToolHookNames, skipLocalToolHooks, subAgentDetailsByToolUseId, subAgentSpans, endedSubAgentSpans) {
|
|
18908
|
+
function injectTracingHooks(options, resolveParentSpan, taskIdToToolUseId, toolUseToParent, activeToolSpans, localToolHookNames, skipLocalToolHooks, subAgentDetailsByToolUseId, subAgentSpans, endedSubAgentSpans) {
|
|
18672
18909
|
const {
|
|
18673
18910
|
preToolUse,
|
|
18674
18911
|
postToolUse,
|
|
@@ -18677,6 +18914,8 @@ function injectTracingHooks(options, resolveParentSpan, activeToolSpans, localTo
|
|
|
18677
18914
|
subagentStop
|
|
18678
18915
|
} = createToolTracingHooks(
|
|
18679
18916
|
resolveParentSpan,
|
|
18917
|
+
taskIdToToolUseId,
|
|
18918
|
+
toolUseToParent,
|
|
18680
18919
|
activeToolSpans,
|
|
18681
18920
|
options.mcpServers,
|
|
18682
18921
|
localToolHookNames,
|
|
@@ -18758,6 +18997,13 @@ async function finalizeCurrentMessageGroup(state) {
|
|
|
18758
18997
|
}
|
|
18759
18998
|
}
|
|
18760
18999
|
const existingLlmSpan = state.activeLlmSpansByParentToolUse.get(parentKey);
|
|
19000
|
+
const lastMessage = state.currentMessages[state.currentMessages.length - 1];
|
|
19001
|
+
const messageId = lastMessage?.message?.id;
|
|
19002
|
+
const usage = state.options.includePartialMessages ? mergeUsage(
|
|
19003
|
+
copyUsage(lastMessage?.message?.usage),
|
|
19004
|
+
messageId ? state.usageByMessageId.get(messageId) : void 0
|
|
19005
|
+
) : void 0;
|
|
19006
|
+
const hasFinalOutputUsage = messageId !== void 0 && state.finalOutputUsageMessageIds.has(messageId);
|
|
18761
19007
|
const llmSpanResult = await createLLMSpanForMessages(
|
|
18762
19008
|
state.currentMessages,
|
|
18763
19009
|
promptMessages,
|
|
@@ -18765,6 +19011,8 @@ async function finalizeCurrentMessageGroup(state) {
|
|
|
18765
19011
|
state.options,
|
|
18766
19012
|
state.currentMessageStartTime,
|
|
18767
19013
|
parentSpan,
|
|
19014
|
+
usage,
|
|
19015
|
+
hasFinalOutputUsage,
|
|
18768
19016
|
existingLlmSpan
|
|
18769
19017
|
);
|
|
18770
19018
|
if (llmSpanResult) {
|
|
@@ -18782,9 +19030,17 @@ async function finalizeCurrentMessageGroup(state) {
|
|
|
18782
19030
|
}
|
|
18783
19031
|
}
|
|
18784
19032
|
state.activeLlmSpansByParentToolUse.delete(parentKey);
|
|
18785
|
-
|
|
18786
|
-
|
|
18787
|
-
state.
|
|
19033
|
+
if (messageId) {
|
|
19034
|
+
state.usageByMessageId.delete(messageId);
|
|
19035
|
+
state.finalOutputUsageMessageIds.delete(messageId);
|
|
19036
|
+
for (const [
|
|
19037
|
+
parent,
|
|
19038
|
+
activeMessageId
|
|
19039
|
+
] of state.activePartialMessageIdByParentKey) {
|
|
19040
|
+
if (activeMessageId === messageId) {
|
|
19041
|
+
state.activePartialMessageIdByParentKey.delete(parent);
|
|
19042
|
+
}
|
|
19043
|
+
}
|
|
18788
19044
|
}
|
|
18789
19045
|
state.currentMessages.length = 0;
|
|
18790
19046
|
}
|
|
@@ -18875,6 +19131,10 @@ async function ensureActiveLlmSpanForParentToolUse(rootSpan, activeLlmSpansByPar
|
|
|
18875
19131
|
);
|
|
18876
19132
|
llmParentSpan = await subAgentSpan.export();
|
|
18877
19133
|
}
|
|
19134
|
+
const racedLlmSpan = activeLlmSpansByParentToolUse.get(parentKey);
|
|
19135
|
+
if (racedLlmSpan) {
|
|
19136
|
+
return racedLlmSpan;
|
|
19137
|
+
}
|
|
18878
19138
|
const llmSpan = startSpan(
|
|
18879
19139
|
withSpanInstrumentationName(
|
|
18880
19140
|
{
|
|
@@ -18994,7 +19254,49 @@ async function maybeHandleTaskLifecycleMessage(state, message) {
|
|
|
18994
19254
|
}
|
|
18995
19255
|
return true;
|
|
18996
19256
|
}
|
|
19257
|
+
function handlePartialUsageMessage(state, message) {
|
|
19258
|
+
if (message.type !== "stream_event") {
|
|
19259
|
+
return false;
|
|
19260
|
+
}
|
|
19261
|
+
const event = message.event;
|
|
19262
|
+
if (!event || typeof event !== "object") {
|
|
19263
|
+
return true;
|
|
19264
|
+
}
|
|
19265
|
+
const parentKey = llmParentKey(message.parent_tool_use_id ?? null);
|
|
19266
|
+
if (event.type === "message_start") {
|
|
19267
|
+
const messageId2 = event.message?.id;
|
|
19268
|
+
const usage = copyUsage(event.message?.usage);
|
|
19269
|
+
if (messageId2) {
|
|
19270
|
+
state.activePartialMessageIdByParentKey.set(parentKey, messageId2);
|
|
19271
|
+
if (usage) {
|
|
19272
|
+
state.usageByMessageId.set(messageId2, usage);
|
|
19273
|
+
}
|
|
19274
|
+
}
|
|
19275
|
+
return true;
|
|
19276
|
+
}
|
|
19277
|
+
const messageId = state.activePartialMessageIdByParentKey.get(parentKey);
|
|
19278
|
+
if (!messageId) {
|
|
19279
|
+
return true;
|
|
19280
|
+
}
|
|
19281
|
+
if (event.type === "message_delta") {
|
|
19282
|
+
const update = copyUsage(event.usage);
|
|
19283
|
+
if (update) {
|
|
19284
|
+
const usage = state.usageByMessageId.get(messageId) ?? {};
|
|
19285
|
+
Object.assign(usage, update);
|
|
19286
|
+
state.usageByMessageId.set(messageId, usage);
|
|
19287
|
+
if (update.output_tokens !== void 0) {
|
|
19288
|
+
state.finalOutputUsageMessageIds.add(messageId);
|
|
19289
|
+
}
|
|
19290
|
+
}
|
|
19291
|
+
} else if (event.type === "message_stop") {
|
|
19292
|
+
state.activePartialMessageIdByParentKey.delete(parentKey);
|
|
19293
|
+
}
|
|
19294
|
+
return true;
|
|
19295
|
+
}
|
|
18997
19296
|
async function handleStreamMessage(state, message) {
|
|
19297
|
+
if (handlePartialUsageMessage(state, message)) {
|
|
19298
|
+
return;
|
|
19299
|
+
}
|
|
18998
19300
|
maybeTrackToolUseContext(state, message);
|
|
18999
19301
|
if (await maybeHandleTaskLifecycleMessage(state, message)) {
|
|
19000
19302
|
return;
|
|
@@ -19041,36 +19343,9 @@ async function handleStreamMessage(state, message) {
|
|
|
19041
19343
|
);
|
|
19042
19344
|
state.currentMessages.push(message);
|
|
19043
19345
|
}
|
|
19044
|
-
if (message.type !== "result"
|
|
19346
|
+
if (message.type !== "result") {
|
|
19045
19347
|
return;
|
|
19046
19348
|
}
|
|
19047
|
-
const finalUsageMetrics = extractUsageFromMessage(message);
|
|
19048
|
-
if (state.currentMessages.length > 0 && finalUsageMetrics.completion_tokens !== void 0) {
|
|
19049
|
-
const lastMessage = state.currentMessages[state.currentMessages.length - 1];
|
|
19050
|
-
if (lastMessage?.message?.usage) {
|
|
19051
|
-
const adjustedTokens = finalUsageMetrics.completion_tokens - state.accumulatedOutputTokens;
|
|
19052
|
-
if (adjustedTokens >= 0) {
|
|
19053
|
-
lastMessage.message.usage.output_tokens = adjustedTokens;
|
|
19054
|
-
}
|
|
19055
|
-
const resultUsage = message.usage;
|
|
19056
|
-
if (resultUsage && typeof resultUsage === "object") {
|
|
19057
|
-
const cacheReadTokens = getNumberProperty(
|
|
19058
|
-
resultUsage,
|
|
19059
|
-
"cache_read_input_tokens"
|
|
19060
|
-
);
|
|
19061
|
-
if (cacheReadTokens !== void 0) {
|
|
19062
|
-
lastMessage.message.usage.cache_read_input_tokens = cacheReadTokens;
|
|
19063
|
-
}
|
|
19064
|
-
const cacheCreationTokens = getNumberProperty(
|
|
19065
|
-
resultUsage,
|
|
19066
|
-
"cache_creation_input_tokens"
|
|
19067
|
-
);
|
|
19068
|
-
if (cacheCreationTokens !== void 0) {
|
|
19069
|
-
lastMessage.message.usage.cache_creation_input_tokens = cacheCreationTokens;
|
|
19070
|
-
}
|
|
19071
|
-
}
|
|
19072
|
-
}
|
|
19073
|
-
}
|
|
19074
19349
|
const metadata = {};
|
|
19075
19350
|
if (message.num_turns !== void 0) {
|
|
19076
19351
|
metadata.num_turns = message.num_turns;
|
|
@@ -19078,8 +19353,12 @@ async function handleStreamMessage(state, message) {
|
|
|
19078
19353
|
if (message.session_id !== void 0) {
|
|
19079
19354
|
metadata.session_id = message.session_id;
|
|
19080
19355
|
}
|
|
19081
|
-
|
|
19082
|
-
|
|
19356
|
+
const metrics = state.options.includePartialMessages ? {} : extractUsage(copyUsage(message.usage), true);
|
|
19357
|
+
if (Object.keys(metadata).length > 0 || Object.keys(metrics).length > 0) {
|
|
19358
|
+
state.span.log({
|
|
19359
|
+
...Object.keys(metadata).length > 0 ? { metadata } : {},
|
|
19360
|
+
...Object.keys(metrics).length > 0 ? { metrics } : {}
|
|
19361
|
+
});
|
|
19083
19362
|
}
|
|
19084
19363
|
}
|
|
19085
19364
|
async function finalizeQuerySpan(state) {
|
|
@@ -19103,6 +19382,9 @@ async function finalizeQuerySpan(state) {
|
|
|
19103
19382
|
llmSpan.end();
|
|
19104
19383
|
}
|
|
19105
19384
|
state.activeLlmSpansByParentToolUse.clear();
|
|
19385
|
+
state.activePartialMessageIdByParentKey.clear();
|
|
19386
|
+
state.finalOutputUsageMessageIds.clear();
|
|
19387
|
+
state.usageByMessageId.clear();
|
|
19106
19388
|
for (const toolSpan of state.activeToolSpans.values()) {
|
|
19107
19389
|
toolSpan.end();
|
|
19108
19390
|
}
|
|
@@ -19227,6 +19509,8 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
19227
19509
|
const optionsWithHooks = injectTracingHooks(
|
|
19228
19510
|
options,
|
|
19229
19511
|
resolveToolUseParentSpan,
|
|
19512
|
+
taskIdToToolUseId,
|
|
19513
|
+
toolUseToParent,
|
|
19230
19514
|
activeToolSpans,
|
|
19231
19515
|
localToolHookNames,
|
|
19232
19516
|
skipLocalToolHooks,
|
|
@@ -19237,8 +19521,8 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
19237
19521
|
params.options = optionsWithHooks;
|
|
19238
19522
|
event.arguments[0] = params;
|
|
19239
19523
|
spans.set(event, {
|
|
19240
|
-
accumulatedOutputTokens: 0,
|
|
19241
19524
|
activeLlmSpansByParentToolUse,
|
|
19525
|
+
activePartialMessageIdByParentKey: /* @__PURE__ */ new Map(),
|
|
19242
19526
|
activeToolSpans,
|
|
19243
19527
|
conversationHistoryByParentKey,
|
|
19244
19528
|
capturedPromptMessages,
|
|
@@ -19246,6 +19530,7 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
19246
19530
|
currentMessageStartTime: startTime,
|
|
19247
19531
|
currentMessages: [],
|
|
19248
19532
|
endedSubAgentSpans,
|
|
19533
|
+
finalOutputUsageMessageIds: /* @__PURE__ */ new Set(),
|
|
19249
19534
|
finalResults: [],
|
|
19250
19535
|
options: optionsWithHooks,
|
|
19251
19536
|
originalPrompt,
|
|
@@ -19261,6 +19546,7 @@ var ClaudeAgentSDKPlugin = class extends BasePlugin {
|
|
|
19261
19546
|
latestLlmParentBySubAgentToolUse,
|
|
19262
19547
|
latestRootLlmParentRef,
|
|
19263
19548
|
toolUseToParent,
|
|
19549
|
+
usageByMessageId: /* @__PURE__ */ new Map(),
|
|
19264
19550
|
localToolContext
|
|
19265
19551
|
});
|
|
19266
19552
|
},
|
|
@@ -21735,9 +22021,9 @@ function extractEmbedPromptTokenCount(response) {
|
|
|
21735
22021
|
let sawAny = false;
|
|
21736
22022
|
for (const embedding of embeddings) {
|
|
21737
22023
|
const embeddingStats = tryToDict(tryToDict(embedding)?.statistics);
|
|
21738
|
-
const
|
|
21739
|
-
if (typeof
|
|
21740
|
-
total +=
|
|
22024
|
+
const tokenCount2 = embeddingStats?.tokenCount;
|
|
22025
|
+
if (typeof tokenCount2 === "number" && Number.isFinite(tokenCount2)) {
|
|
22026
|
+
total += tokenCount2;
|
|
21741
22027
|
sawAny = true;
|
|
21742
22028
|
}
|
|
21743
22029
|
}
|
|
@@ -28632,7 +28918,7 @@ function getStringProperty2(obj, key) {
|
|
|
28632
28918
|
return typeof value === "string" ? value : void 0;
|
|
28633
28919
|
}
|
|
28634
28920
|
function extractMetricsFromUsage(usage) {
|
|
28635
|
-
const
|
|
28921
|
+
const rawMetrics = {
|
|
28636
28922
|
prompt_tokens: usage.inputTokens,
|
|
28637
28923
|
completion_tokens: usage.outputTokens,
|
|
28638
28924
|
...extractAnthropicCacheTokens(
|
|
@@ -28641,10 +28927,10 @@ function extractMetricsFromUsage(usage) {
|
|
|
28641
28927
|
)
|
|
28642
28928
|
};
|
|
28643
28929
|
if (usage.reasoningTokens !== void 0) {
|
|
28644
|
-
|
|
28645
|
-
|
|
28930
|
+
rawMetrics.completion_reasoning_tokens = usage.reasoningTokens;
|
|
28931
|
+
rawMetrics.reasoning_tokens = usage.reasoningTokens;
|
|
28646
28932
|
}
|
|
28647
|
-
|
|
28933
|
+
const metrics = finalizeAnthropicTokens(rawMetrics);
|
|
28648
28934
|
const metadata = {
|
|
28649
28935
|
model: usage.model
|
|
28650
28936
|
};
|
|
@@ -29583,17 +29869,20 @@ var FlueObserveBridge = class {
|
|
|
29583
29869
|
return;
|
|
29584
29870
|
}
|
|
29585
29871
|
const metadata = {
|
|
29872
|
+
...event.runId ? this.runsById.get(event.runId)?.metadata : {},
|
|
29586
29873
|
...extractEventMetadata(event),
|
|
29587
29874
|
"flue.operation": event.operationKind,
|
|
29588
29875
|
provider: "flue"
|
|
29589
29876
|
};
|
|
29590
29877
|
const parent = this.parentSpanForEvent(event);
|
|
29591
|
-
const
|
|
29878
|
+
const args = {
|
|
29592
29879
|
name: `flue.${event.operationKind}`,
|
|
29593
29880
|
spanAttributes: { type: "task" /* TASK */ },
|
|
29594
29881
|
startTime: eventTime(event.timestamp),
|
|
29595
29882
|
event: { metadata }
|
|
29596
|
-
}
|
|
29883
|
+
};
|
|
29884
|
+
const runSpan = event.runId ? this.runsById.get(event.runId)?.span : void 0;
|
|
29885
|
+
const span = event.operationKind === "prompt" && (!parent || parent === runSpan) ? startFlueRootSpan(args) : startFlueSpan(parent, args);
|
|
29597
29886
|
this.operationsById.set(event.operationId, { metadata, span });
|
|
29598
29887
|
}
|
|
29599
29888
|
handleOperation(event) {
|
|
@@ -29608,6 +29897,11 @@ var FlueObserveBridge = class {
|
|
|
29608
29897
|
...event.isError !== void 0 ? { "flue.is_error": event.isError } : {},
|
|
29609
29898
|
...event.usage ? { "flue.usage": event.usage } : {}
|
|
29610
29899
|
};
|
|
29900
|
+
const input = flueOperationInput(event);
|
|
29901
|
+
if (!state.loggedInput && input !== void 0) {
|
|
29902
|
+
safeLog3(state.span, { input });
|
|
29903
|
+
state.loggedInput = true;
|
|
29904
|
+
}
|
|
29611
29905
|
this.finishPendingChildrenForOperation(event, output);
|
|
29612
29906
|
safeLog3(state.span, {
|
|
29613
29907
|
...event.isError ? { error: toLoggedError(event.errorInfo ?? event.error) } : {},
|
|
@@ -29624,6 +29918,8 @@ var FlueObserveBridge = class {
|
|
|
29624
29918
|
return;
|
|
29625
29919
|
}
|
|
29626
29920
|
const input = flueTurnRequestInput(event);
|
|
29921
|
+
const operation = event.operationId ? this.operationsById.get(event.operationId) : void 0;
|
|
29922
|
+
const turnInput = prepareFlueTurnInput(event, input, operation);
|
|
29627
29923
|
const model = flueTurnRequestModel(event);
|
|
29628
29924
|
const provider = flueTurnRequestProvider(event);
|
|
29629
29925
|
const api = flueTurnRequestApi(event);
|
|
@@ -29636,8 +29932,7 @@ var FlueObserveBridge = class {
|
|
|
29636
29932
|
...provider ? { "flue.provider": provider } : {},
|
|
29637
29933
|
...event.purpose ? { "flue.turn_purpose": event.purpose } : {},
|
|
29638
29934
|
...reasoning ? { reasoning } : {},
|
|
29639
|
-
...
|
|
29640
|
-
...input?.tools ? { tools: input.tools } : {}
|
|
29935
|
+
...turnInput.metadata
|
|
29641
29936
|
};
|
|
29642
29937
|
const parent = this.parentSpanForTurn(event);
|
|
29643
29938
|
const span = startFlueSpan(parent, {
|
|
@@ -29645,11 +29940,14 @@ var FlueObserveBridge = class {
|
|
|
29645
29940
|
spanAttributes: { type: "llm" /* LLM */ },
|
|
29646
29941
|
startTime: eventTime(event.timestamp),
|
|
29647
29942
|
event: {
|
|
29648
|
-
input:
|
|
29943
|
+
input: turnInput.messages,
|
|
29649
29944
|
metadata
|
|
29650
29945
|
}
|
|
29651
29946
|
});
|
|
29652
|
-
this.logOperationInput(
|
|
29947
|
+
this.logOperationInput(
|
|
29948
|
+
event.operationId,
|
|
29949
|
+
latestUserMessageInput(input?.messages)
|
|
29950
|
+
);
|
|
29653
29951
|
this.turnsByKey.set(key, { metadata, span });
|
|
29654
29952
|
}
|
|
29655
29953
|
handleTurn(event) {
|
|
@@ -29896,16 +30194,20 @@ var FlueObserveBridge = class {
|
|
|
29896
30194
|
}
|
|
29897
30195
|
startSyntheticOperation(event) {
|
|
29898
30196
|
const metadata = {
|
|
30197
|
+
...event.runId ? this.runsById.get(event.runId)?.metadata : {},
|
|
29899
30198
|
...extractEventMetadata(event),
|
|
29900
30199
|
"flue.operation": event.operationKind,
|
|
29901
30200
|
provider: "flue"
|
|
29902
30201
|
};
|
|
29903
|
-
const
|
|
30202
|
+
const args = {
|
|
29904
30203
|
name: `flue.${event.operationKind}`,
|
|
29905
30204
|
spanAttributes: { type: "task" /* TASK */ },
|
|
29906
30205
|
startTime: eventTime(event.timestamp),
|
|
29907
30206
|
event: { metadata }
|
|
29908
|
-
}
|
|
30207
|
+
};
|
|
30208
|
+
const parent = this.parentSpanForEvent(event);
|
|
30209
|
+
const runSpan = event.runId ? this.runsById.get(event.runId)?.span : void 0;
|
|
30210
|
+
const span = event.operationKind === "prompt" && (!parent || parent === runSpan) ? startFlueRootSpan(args) : startFlueSpan(parent, args);
|
|
29909
30211
|
return { metadata, span };
|
|
29910
30212
|
}
|
|
29911
30213
|
startSyntheticTurn(event) {
|
|
@@ -30084,6 +30386,71 @@ function flueRunInput(event) {
|
|
|
30084
30386
|
function flueTurnRequestInput(event) {
|
|
30085
30387
|
return event.request?.input ?? event.input;
|
|
30086
30388
|
}
|
|
30389
|
+
function prepareFlueTurnInput(event, input, operation) {
|
|
30390
|
+
const messages = input?.messages;
|
|
30391
|
+
const tracksUserTurn = event.purpose === "agent" && operation?.metadata["flue.operation"] === "prompt" && Array.isArray(messages);
|
|
30392
|
+
if (!tracksUserTurn) {
|
|
30393
|
+
return {
|
|
30394
|
+
messages,
|
|
30395
|
+
metadata: {
|
|
30396
|
+
...input?.systemPrompt ? { "flue.system_prompt": input.systemPrompt } : {},
|
|
30397
|
+
...input?.tools ? { tools: input.tools } : {}
|
|
30398
|
+
}
|
|
30399
|
+
};
|
|
30400
|
+
}
|
|
30401
|
+
const previous = operation.turnInputState;
|
|
30402
|
+
const previousMessageCount = previous?.messageCount ?? 0;
|
|
30403
|
+
const boundaryFingerprint = messages.length > 0 ? fingerprintJsonValue(messages[messages.length - 1]) : void 0;
|
|
30404
|
+
const continuesPreviousInput = previous !== void 0 && messages.length >= previousMessageCount && (previousMessageCount === 0 || previous.boundaryFingerprint !== void 0 && previous.boundaryFingerprint === fingerprintJsonValue(messages[previousMessageCount - 1]));
|
|
30405
|
+
const inputMode = previous === void 0 ? "full" : continuesPreviousInput ? "delta" : "reset";
|
|
30406
|
+
const systemPromptFingerprint = fingerprintJsonValue(input?.systemPrompt);
|
|
30407
|
+
const toolsFingerprint = fingerprintJsonValue(input?.tools);
|
|
30408
|
+
operation.turnInputState = {
|
|
30409
|
+
...boundaryFingerprint !== void 0 ? { boundaryFingerprint } : {},
|
|
30410
|
+
messageCount: messages.length,
|
|
30411
|
+
...systemPromptFingerprint !== void 0 ? { systemPromptFingerprint } : {},
|
|
30412
|
+
...toolsFingerprint !== void 0 ? { toolsFingerprint } : {}
|
|
30413
|
+
};
|
|
30414
|
+
return {
|
|
30415
|
+
messages: continuesPreviousInput ? messages.slice(previousMessageCount) : messages,
|
|
30416
|
+
metadata: {
|
|
30417
|
+
"flue.input_mode": inputMode,
|
|
30418
|
+
...continuesPreviousInput ? { "flue.input_message_offset": previousMessageCount } : {},
|
|
30419
|
+
...input?.systemPrompt && (!continuesPreviousInput || systemPromptFingerprint !== previous?.systemPromptFingerprint) ? { "flue.system_prompt": input.systemPrompt } : {},
|
|
30420
|
+
...input?.tools && (!continuesPreviousInput || toolsFingerprint !== previous?.toolsFingerprint) ? { tools: input.tools } : {}
|
|
30421
|
+
}
|
|
30422
|
+
};
|
|
30423
|
+
}
|
|
30424
|
+
function fingerprintJsonValue(value) {
|
|
30425
|
+
try {
|
|
30426
|
+
const serialized = JSON.stringify(value);
|
|
30427
|
+
if (serialized === void 0) {
|
|
30428
|
+
return void 0;
|
|
30429
|
+
}
|
|
30430
|
+
let hash = 2166136261;
|
|
30431
|
+
for (let i = 0; i < serialized.length; i++) {
|
|
30432
|
+
hash = Math.imul(hash ^ serialized.charCodeAt(i), 16777619);
|
|
30433
|
+
}
|
|
30434
|
+
return `${serialized.length}:${hash >>> 0}`;
|
|
30435
|
+
} catch {
|
|
30436
|
+
return void 0;
|
|
30437
|
+
}
|
|
30438
|
+
}
|
|
30439
|
+
function latestUserMessageInput(messages) {
|
|
30440
|
+
if (!messages) {
|
|
30441
|
+
return void 0;
|
|
30442
|
+
}
|
|
30443
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
30444
|
+
const message = messages[i];
|
|
30445
|
+
if (isObjectLike(message) && Reflect.get(message, "role") === "user") {
|
|
30446
|
+
return [message];
|
|
30447
|
+
}
|
|
30448
|
+
}
|
|
30449
|
+
return void 0;
|
|
30450
|
+
}
|
|
30451
|
+
function flueOperationInput(event) {
|
|
30452
|
+
return typeof event.agentInput?.text === "string" ? [{ content: event.agentInput.text, role: "user" }] : void 0;
|
|
30453
|
+
}
|
|
30087
30454
|
function flueTurnRequestModel(event) {
|
|
30088
30455
|
return event.request?.requestedModel ?? event.request?.model ?? event.model;
|
|
30089
30456
|
}
|
|
@@ -30241,6 +30608,21 @@ function startFlueSpan(parent, args) {
|
|
|
30241
30608
|
withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE)
|
|
30242
30609
|
);
|
|
30243
30610
|
}
|
|
30611
|
+
function startFlueRootSpan(args) {
|
|
30612
|
+
const state = _internalGetGlobalState();
|
|
30613
|
+
const spanId = state.idGenerator.getSpanId();
|
|
30614
|
+
const rootSpanId = state.idGenerator.shareRootSpanId() ? spanId : state.idGenerator.getTraceId();
|
|
30615
|
+
return withCurrent(
|
|
30616
|
+
NOOP_SPAN,
|
|
30617
|
+
() => startSpan({
|
|
30618
|
+
...withSpanInstrumentationName(args, INSTRUMENTATION_NAMES.FLUE),
|
|
30619
|
+
parentSpanIds: { parentSpanIds: [], rootSpanId },
|
|
30620
|
+
spanId,
|
|
30621
|
+
state
|
|
30622
|
+
}),
|
|
30623
|
+
state
|
|
30624
|
+
);
|
|
30625
|
+
}
|
|
30244
30626
|
function runWithCurrentSpanStore(span, next) {
|
|
30245
30627
|
const state = _internalGetGlobalState();
|
|
30246
30628
|
const contextManager = state?.contextManager;
|
|
@@ -32140,8 +32522,7 @@ function startAgentStream(event, activeChildParents) {
|
|
|
32140
32522
|
...extractAgentMetadata2(agent),
|
|
32141
32523
|
...extractModelMetadata3(model),
|
|
32142
32524
|
"strands.operation": "Agent.stream",
|
|
32143
|
-
provider: extractProvider(model)
|
|
32144
|
-
...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
|
|
32525
|
+
provider: extractProvider(model)
|
|
32145
32526
|
};
|
|
32146
32527
|
const parentSpan = agent ? getOnlyChildParent(activeChildParents, agent) : void 0;
|
|
32147
32528
|
const attachmentCache = createStrandsAttachmentCache();
|
|
@@ -32191,8 +32572,7 @@ function startMultiAgentStream(event, operation, activeChildParents) {
|
|
|
32191
32572
|
const metadata = {
|
|
32192
32573
|
"strands.operation": operation,
|
|
32193
32574
|
provider: "strands",
|
|
32194
|
-
...orchestrator?.id ? { "strands.orchestrator.id": orchestrator.id } : {}
|
|
32195
|
-
...event.moduleVersion ? { "strands_agent_sdk.version": event.moduleVersion } : {}
|
|
32575
|
+
...orchestrator?.id ? { "strands.orchestrator.id": orchestrator.id } : {}
|
|
32196
32576
|
};
|
|
32197
32577
|
const parentSpan = orchestrator ? getOnlyChildParent(activeChildParents, orchestrator) : void 0;
|
|
32198
32578
|
const input = processStrandsInputAttachments(event.arguments[0]);
|
|
@@ -32978,6 +33358,320 @@ function logInstrumentationError5(context, error) {
|
|
|
32978
33358
|
debugLogger.debug(`${context}:`, error);
|
|
32979
33359
|
}
|
|
32980
33360
|
|
|
33361
|
+
// src/instrumentation/plugins/voyageai-channels.ts
|
|
33362
|
+
var voyageAIChannels = defineChannels(
|
|
33363
|
+
"voyageai",
|
|
33364
|
+
{
|
|
33365
|
+
embed: channel({
|
|
33366
|
+
channelName: "embed",
|
|
33367
|
+
kind: "async"
|
|
33368
|
+
}),
|
|
33369
|
+
multimodalEmbed: channel({
|
|
33370
|
+
channelName: "multimodalEmbed",
|
|
33371
|
+
kind: "async"
|
|
33372
|
+
}),
|
|
33373
|
+
rerank: channel({
|
|
33374
|
+
channelName: "rerank",
|
|
33375
|
+
kind: "async"
|
|
33376
|
+
}),
|
|
33377
|
+
contextualizedEmbed: channel({
|
|
33378
|
+
channelName: "contextualizedEmbed",
|
|
33379
|
+
kind: "async"
|
|
33380
|
+
})
|
|
33381
|
+
},
|
|
33382
|
+
{ instrumentationName: INSTRUMENTATION_NAMES.VOYAGEAI }
|
|
33383
|
+
);
|
|
33384
|
+
|
|
33385
|
+
// src/instrumentation/plugins/voyageai-plugin.ts
|
|
33386
|
+
var RERANK_METADATA_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
33387
|
+
"model",
|
|
33388
|
+
"returnDocuments",
|
|
33389
|
+
"topK",
|
|
33390
|
+
"truncation"
|
|
33391
|
+
]);
|
|
33392
|
+
var VoyageAIPlugin = class extends BasePlugin {
|
|
33393
|
+
onEnable() {
|
|
33394
|
+
this.unsubscribers.push(
|
|
33395
|
+
interceptVoyageAICall(
|
|
33396
|
+
voyageAIChannels.embed,
|
|
33397
|
+
"voyageai.embed",
|
|
33398
|
+
extractTextEmbeddingInput,
|
|
33399
|
+
summarizeEmbeddingOutput,
|
|
33400
|
+
extractEmbeddingUsageMetrics
|
|
33401
|
+
),
|
|
33402
|
+
interceptVoyageAICall(
|
|
33403
|
+
voyageAIChannels.multimodalEmbed,
|
|
33404
|
+
"voyageai.multimodalEmbed",
|
|
33405
|
+
extractMultimodalEmbeddingInput,
|
|
33406
|
+
summarizeEmbeddingOutput,
|
|
33407
|
+
extractEmbeddingUsageMetrics
|
|
33408
|
+
),
|
|
33409
|
+
interceptVoyageAICall(
|
|
33410
|
+
voyageAIChannels.rerank,
|
|
33411
|
+
"voyageai.rerank",
|
|
33412
|
+
extractRerankInput,
|
|
33413
|
+
summarizeRerankOutput
|
|
33414
|
+
),
|
|
33415
|
+
interceptVoyageAICall(
|
|
33416
|
+
voyageAIChannels.contextualizedEmbed,
|
|
33417
|
+
"voyageai.contextualizedEmbed",
|
|
33418
|
+
extractContextualizedEmbeddingInput,
|
|
33419
|
+
summarizeContextualizedEmbeddingOutput,
|
|
33420
|
+
extractEmbeddingUsageMetrics
|
|
33421
|
+
)
|
|
33422
|
+
);
|
|
33423
|
+
}
|
|
33424
|
+
onDisable() {
|
|
33425
|
+
this.unsubscribers = unsubscribeAll(this.unsubscribers);
|
|
33426
|
+
}
|
|
33427
|
+
};
|
|
33428
|
+
function interceptVoyageAICall(channel2, name, extractInput2, extractOutput2, extractMetrics2 = extractUsageMetrics3) {
|
|
33429
|
+
return channel2.intercept((target, thisArg, args) => {
|
|
33430
|
+
const invokeTarget = () => Reflect.apply(target, thisArg, args);
|
|
33431
|
+
if (isAutoInstrumentationSuppressed()) {
|
|
33432
|
+
return invokeTarget();
|
|
33433
|
+
}
|
|
33434
|
+
let span;
|
|
33435
|
+
try {
|
|
33436
|
+
const { input, metadata } = extractInput2(args);
|
|
33437
|
+
span = startSpan(
|
|
33438
|
+
withSpanInstrumentationName(
|
|
33439
|
+
{
|
|
33440
|
+
event: { input, metadata },
|
|
33441
|
+
name,
|
|
33442
|
+
spanAttributes: { type: "llm" /* LLM */ }
|
|
33443
|
+
},
|
|
33444
|
+
INSTRUMENTATION_NAMES.VOYAGEAI
|
|
33445
|
+
)
|
|
33446
|
+
);
|
|
33447
|
+
} catch (error) {
|
|
33448
|
+
debugLogger.error(`Error starting span for ${name}:`, error);
|
|
33449
|
+
return invokeTarget();
|
|
33450
|
+
}
|
|
33451
|
+
let result;
|
|
33452
|
+
try {
|
|
33453
|
+
result = withCurrent(
|
|
33454
|
+
span,
|
|
33455
|
+
() => runWithAutoInstrumentationSuppressed(invokeTarget)
|
|
33456
|
+
);
|
|
33457
|
+
} catch (error) {
|
|
33458
|
+
finishVoyageAISpan(span, name, () => span.log({ error }));
|
|
33459
|
+
throw error;
|
|
33460
|
+
}
|
|
33461
|
+
void Promise.resolve(result).then(
|
|
33462
|
+
(value) => finishVoyageAISpan(span, name, () => {
|
|
33463
|
+
const metadata = extractResponseMetadata3(value);
|
|
33464
|
+
span.log({
|
|
33465
|
+
output: extractOutput2(value),
|
|
33466
|
+
...metadata ? { metadata } : {},
|
|
33467
|
+
metrics: extractMetrics2(value)
|
|
33468
|
+
});
|
|
33469
|
+
}),
|
|
33470
|
+
(error) => finishVoyageAISpan(span, name, () => span.log({ error }))
|
|
33471
|
+
);
|
|
33472
|
+
return result;
|
|
33473
|
+
});
|
|
33474
|
+
}
|
|
33475
|
+
function finishVoyageAISpan(span, name, log) {
|
|
33476
|
+
try {
|
|
33477
|
+
log();
|
|
33478
|
+
} catch (error) {
|
|
33479
|
+
debugLogger.error(`Error logging span for ${name}:`, error);
|
|
33480
|
+
}
|
|
33481
|
+
try {
|
|
33482
|
+
span.end();
|
|
33483
|
+
} catch (error) {
|
|
33484
|
+
debugLogger.error(`Error ending span for ${name}:`, error);
|
|
33485
|
+
}
|
|
33486
|
+
}
|
|
33487
|
+
function getRequestArg2(args) {
|
|
33488
|
+
if (Array.isArray(args)) {
|
|
33489
|
+
return isObject(args[0]) ? args[0] : void 0;
|
|
33490
|
+
}
|
|
33491
|
+
if (!isObject(args)) {
|
|
33492
|
+
return void 0;
|
|
33493
|
+
}
|
|
33494
|
+
const firstArg = Reflect.get(args, "0");
|
|
33495
|
+
return isObject(firstArg) ? firstArg : void 0;
|
|
33496
|
+
}
|
|
33497
|
+
function pickMetadata(request, allowlist) {
|
|
33498
|
+
const metadata = {};
|
|
33499
|
+
if (request) {
|
|
33500
|
+
for (const key of allowlist) {
|
|
33501
|
+
if (!Object.hasOwn(request, key)) {
|
|
33502
|
+
continue;
|
|
33503
|
+
}
|
|
33504
|
+
const value = request[key];
|
|
33505
|
+
if (value !== void 0) {
|
|
33506
|
+
metadata[key] = value;
|
|
33507
|
+
}
|
|
33508
|
+
}
|
|
33509
|
+
}
|
|
33510
|
+
return {
|
|
33511
|
+
...metadata,
|
|
33512
|
+
provider: "voyage"
|
|
33513
|
+
};
|
|
33514
|
+
}
|
|
33515
|
+
function buildEmbeddingInput(inputs, request) {
|
|
33516
|
+
const outputDimensions = request?.outputDimension;
|
|
33517
|
+
return {
|
|
33518
|
+
inputs,
|
|
33519
|
+
...typeof outputDimensions === "number" && Number.isFinite(outputDimensions) ? { output_dimensions: outputDimensions } : {}
|
|
33520
|
+
};
|
|
33521
|
+
}
|
|
33522
|
+
function embeddingMetadata(request) {
|
|
33523
|
+
return {
|
|
33524
|
+
...typeof request?.model === "string" ? { model: request.model } : {},
|
|
33525
|
+
provider: "voyage"
|
|
33526
|
+
};
|
|
33527
|
+
}
|
|
33528
|
+
function extractTextEmbeddingInput(args) {
|
|
33529
|
+
const request = getRequestArg2(args);
|
|
33530
|
+
const rawInput = request?.input;
|
|
33531
|
+
const values = Array.isArray(rawInput) ? rawInput : [rawInput];
|
|
33532
|
+
return {
|
|
33533
|
+
input: buildEmbeddingInput(
|
|
33534
|
+
values.flatMap(
|
|
33535
|
+
(value) => typeof value === "string" ? [{ content: value }] : []
|
|
33536
|
+
),
|
|
33537
|
+
request
|
|
33538
|
+
),
|
|
33539
|
+
metadata: embeddingMetadata(request)
|
|
33540
|
+
};
|
|
33541
|
+
}
|
|
33542
|
+
function extractContextualizedEmbeddingInput(args) {
|
|
33543
|
+
const request = getRequestArg2(args);
|
|
33544
|
+
const rawInputs = request?.inputs;
|
|
33545
|
+
const values = Array.isArray(rawInputs) ? rawInputs.flatMap((value) => Array.isArray(value) ? value : [value]) : [];
|
|
33546
|
+
return {
|
|
33547
|
+
input: buildEmbeddingInput(
|
|
33548
|
+
values.flatMap(
|
|
33549
|
+
(value) => typeof value === "string" ? [{ content: value }] : []
|
|
33550
|
+
),
|
|
33551
|
+
request
|
|
33552
|
+
),
|
|
33553
|
+
metadata: embeddingMetadata(request)
|
|
33554
|
+
};
|
|
33555
|
+
}
|
|
33556
|
+
function extractMultimodalEmbeddingInput(args) {
|
|
33557
|
+
const request = getRequestArg2(args);
|
|
33558
|
+
const rawInputs = request?.inputs;
|
|
33559
|
+
const inputs = Array.isArray(rawInputs) ? rawInputs.map((rawInput) => {
|
|
33560
|
+
const rawContent = isObject(rawInput) ? rawInput.content : void 0;
|
|
33561
|
+
return {
|
|
33562
|
+
content: Array.isArray(rawContent) ? rawContent.flatMap(normalizeMultimodalContentPart) : []
|
|
33563
|
+
};
|
|
33564
|
+
}) : [];
|
|
33565
|
+
const input = buildEmbeddingInput(inputs, request);
|
|
33566
|
+
const processedInput = processInputAttachments(input);
|
|
33567
|
+
return {
|
|
33568
|
+
input: hasInlineEmbeddingMedia(processedInput) ? input : processedInput,
|
|
33569
|
+
metadata: embeddingMetadata(request)
|
|
33570
|
+
};
|
|
33571
|
+
}
|
|
33572
|
+
function normalizeMultimodalContentPart(part) {
|
|
33573
|
+
if (!isObject(part) || typeof part.type !== "string") {
|
|
33574
|
+
return [];
|
|
33575
|
+
}
|
|
33576
|
+
if (part.type === "text") {
|
|
33577
|
+
return typeof part.text === "string" ? [{ type: "text", text: part.text }] : [];
|
|
33578
|
+
}
|
|
33579
|
+
const camelCaseField = {
|
|
33580
|
+
image_base64: "imageBase64",
|
|
33581
|
+
image_url: "imageUrl",
|
|
33582
|
+
video_base64: "videoBase64",
|
|
33583
|
+
video_url: "videoUrl"
|
|
33584
|
+
};
|
|
33585
|
+
const field = camelCaseField[part.type];
|
|
33586
|
+
if (!field) {
|
|
33587
|
+
return [];
|
|
33588
|
+
}
|
|
33589
|
+
const data = typeof part[field] === "string" ? part[field] : part[part.type];
|
|
33590
|
+
if (typeof data !== "string") {
|
|
33591
|
+
return [];
|
|
33592
|
+
}
|
|
33593
|
+
return part.type.startsWith("image_") ? [{ type: "image_url", image_url: { url: data } }] : [{ type: "file", file: { file_data: data } }];
|
|
33594
|
+
}
|
|
33595
|
+
function hasInlineEmbeddingMedia(input) {
|
|
33596
|
+
return input.inputs.some(
|
|
33597
|
+
({ content }) => Array.isArray(content) ? content.some((part) => {
|
|
33598
|
+
const value = part.type === "image_url" ? part.image_url.url : part.type === "file" ? part.file.file_data : void 0;
|
|
33599
|
+
return typeof value === "string" && value.startsWith("data:");
|
|
33600
|
+
}) : false
|
|
33601
|
+
);
|
|
33602
|
+
}
|
|
33603
|
+
function extractRerankInput(args) {
|
|
33604
|
+
const request = getRequestArg2(args);
|
|
33605
|
+
const documents = request?.documents;
|
|
33606
|
+
return {
|
|
33607
|
+
input: {
|
|
33608
|
+
documents,
|
|
33609
|
+
query: request?.query
|
|
33610
|
+
},
|
|
33611
|
+
metadata: {
|
|
33612
|
+
...pickMetadata(request, RERANK_METADATA_ALLOWLIST),
|
|
33613
|
+
...Array.isArray(documents) ? { document_count: documents.length } : {}
|
|
33614
|
+
}
|
|
33615
|
+
};
|
|
33616
|
+
}
|
|
33617
|
+
function extractResponseMetadata3(result) {
|
|
33618
|
+
if (!isObject(result)) {
|
|
33619
|
+
return void 0;
|
|
33620
|
+
}
|
|
33621
|
+
const rawResponse = isObject(result.rawResponse) ? result.rawResponse : void 0;
|
|
33622
|
+
const model = typeof result.model === "string" ? result.model : typeof rawResponse?.model === "string" ? rawResponse.model : void 0;
|
|
33623
|
+
return model ? { model } : void 0;
|
|
33624
|
+
}
|
|
33625
|
+
function summarizeEmbeddingOutput(result) {
|
|
33626
|
+
return {
|
|
33627
|
+
count: isObject(result) && Array.isArray(result.data) ? result.data.length : 0
|
|
33628
|
+
};
|
|
33629
|
+
}
|
|
33630
|
+
function summarizeRerankOutput(result) {
|
|
33631
|
+
if (!isObject(result) || !Array.isArray(result.data)) {
|
|
33632
|
+
return void 0;
|
|
33633
|
+
}
|
|
33634
|
+
return result.data.slice(0, 100).map((item) => ({
|
|
33635
|
+
index: isObject(item) ? item.index : void 0,
|
|
33636
|
+
relevance_score: isObject(item) ? (typeof item.relevanceScore === "number" ? item.relevanceScore : item.relevance_score) ?? null : null
|
|
33637
|
+
}));
|
|
33638
|
+
}
|
|
33639
|
+
function summarizeContextualizedEmbeddingOutput(result) {
|
|
33640
|
+
if (!isObject(result)) {
|
|
33641
|
+
return { count: 0 };
|
|
33642
|
+
}
|
|
33643
|
+
if (Array.isArray(result.results)) {
|
|
33644
|
+
return {
|
|
33645
|
+
count: result.results.reduce(
|
|
33646
|
+
(count, item) => count + (isObject(item) && Array.isArray(item.embeddings) ? item.embeddings.length : 0),
|
|
33647
|
+
0
|
|
33648
|
+
)
|
|
33649
|
+
};
|
|
33650
|
+
}
|
|
33651
|
+
if (!Array.isArray(result.data)) {
|
|
33652
|
+
return { count: 0 };
|
|
33653
|
+
}
|
|
33654
|
+
return {
|
|
33655
|
+
count: result.data.reduce(
|
|
33656
|
+
(count, item) => count + (isObject(item) && Array.isArray(item.data) ? item.data.length : 0),
|
|
33657
|
+
0
|
|
33658
|
+
)
|
|
33659
|
+
};
|
|
33660
|
+
}
|
|
33661
|
+
function extractEmbeddingUsageMetrics(result) {
|
|
33662
|
+
const metrics = extractUsageMetrics3(result);
|
|
33663
|
+
return typeof metrics.tokens === "number" ? { prompt_tokens: metrics.tokens, tokens: metrics.tokens } : {};
|
|
33664
|
+
}
|
|
33665
|
+
function extractUsageMetrics3(result) {
|
|
33666
|
+
if (!isObject(result)) {
|
|
33667
|
+
return {};
|
|
33668
|
+
}
|
|
33669
|
+
const rawResponse = isObject(result.rawResponse) ? result.rawResponse : void 0;
|
|
33670
|
+
const usage = isObject(result.usage) ? result.usage : isObject(rawResponse?.usage) ? rawResponse.usage : void 0;
|
|
33671
|
+
const tokens = typeof result.totalTokens === "number" ? result.totalTokens : usage?.totalTokens ?? usage?.total_tokens;
|
|
33672
|
+
return typeof tokens === "number" && Number.isFinite(tokens) && tokens >= 0 ? { tokens } : {};
|
|
33673
|
+
}
|
|
33674
|
+
|
|
32981
33675
|
// src/instrumentation/plugins/cloudflare-ai-chat-channels.ts
|
|
32982
33676
|
var cloudflareAIChatChannels = defineChannels(
|
|
32983
33677
|
"@cloudflare/ai-chat",
|
|
@@ -33533,6 +34227,7 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
33533
34227
|
langSmithPlugin = null;
|
|
33534
34228
|
piCodingAgentPlugin = null;
|
|
33535
34229
|
strandsAgentSDKPlugin = null;
|
|
34230
|
+
voyageAIPlugin = null;
|
|
33536
34231
|
cloudflareAIChatPlugin = null;
|
|
33537
34232
|
cloudflareAgentsPlugin = null;
|
|
33538
34233
|
constructor(config = {}) {
|
|
@@ -33607,6 +34302,10 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
33607
34302
|
this.coherePlugin = new CoherePlugin();
|
|
33608
34303
|
this.coherePlugin.enable();
|
|
33609
34304
|
}
|
|
34305
|
+
if (integrations.voyageai !== false) {
|
|
34306
|
+
this.voyageAIPlugin = new VoyageAIPlugin();
|
|
34307
|
+
this.voyageAIPlugin.enable();
|
|
34308
|
+
}
|
|
33610
34309
|
if (integrations.groq !== false) {
|
|
33611
34310
|
this.groqPlugin = new GroqPlugin();
|
|
33612
34311
|
this.groqPlugin.enable();
|
|
@@ -33723,6 +34422,10 @@ var BraintrustPlugin = class extends BasePlugin {
|
|
|
33723
34422
|
this.coherePlugin.disable();
|
|
33724
34423
|
this.coherePlugin = null;
|
|
33725
34424
|
}
|
|
34425
|
+
if (this.voyageAIPlugin) {
|
|
34426
|
+
this.voyageAIPlugin.disable();
|
|
34427
|
+
this.voyageAIPlugin = null;
|
|
34428
|
+
}
|
|
33726
34429
|
if (this.groqPlugin) {
|
|
33727
34430
|
this.groqPlugin.disable();
|
|
33728
34431
|
this.groqPlugin = null;
|
|
@@ -35493,7 +36196,10 @@ var envIntegrationAliases = {
|
|
|
35493
36196
|
"langchain-js": "langchain",
|
|
35494
36197
|
"@langchain": "langchain",
|
|
35495
36198
|
langgraph: "langgraph",
|
|
35496
|
-
langsmith: "langsmith"
|
|
36199
|
+
langsmith: "langsmith",
|
|
36200
|
+
voyage: "voyageai",
|
|
36201
|
+
"voyage-ai": "voyageai",
|
|
36202
|
+
voyageai: "voyageai"
|
|
35497
36203
|
};
|
|
35498
36204
|
function getDefaultInstrumentationIntegrations() {
|
|
35499
36205
|
return {
|
|
@@ -35528,6 +36234,7 @@ function getDefaultInstrumentationIntegrations() {
|
|
|
35528
36234
|
langchain: true,
|
|
35529
36235
|
langgraph: true,
|
|
35530
36236
|
langsmith: true,
|
|
36237
|
+
voyageai: true,
|
|
35531
36238
|
piCodingAgent: true,
|
|
35532
36239
|
strandsAgentSDK: true,
|
|
35533
36240
|
cloudflareAgents: true
|