@librechat/agents 3.2.32 → 3.2.33
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/cjs/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/cjs/agents/AgentContext.cjs +844 -1046
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/constants.cjs +13 -13
- package/dist/cjs/common/constants.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +233 -240
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/common/index.cjs +2 -0
- package/dist/cjs/events.cjs +121 -169
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +1389 -1807
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +713 -945
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/graphs/index.cjs +2 -0
- package/dist/cjs/hitl/askUserQuestion.cjs +60 -62
- package/dist/cjs/hitl/askUserQuestion.cjs.map +1 -1
- package/dist/cjs/hitl/index.cjs +1 -0
- package/dist/cjs/hooks/HookRegistry.cjs +176 -202
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
- package/dist/cjs/hooks/createToolPolicyHook.cjs +71 -101
- package/dist/cjs/hooks/createToolPolicyHook.cjs.map +1 -1
- package/dist/cjs/hooks/createWorkspacePolicyHook.cjs +170 -273
- package/dist/cjs/hooks/createWorkspacePolicyHook.cjs.map +1 -1
- package/dist/cjs/hooks/executeHooks.cjs +227 -282
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -1
- package/dist/cjs/hooks/index.cjs +6 -0
- package/dist/cjs/hooks/matchers.cjs +196 -230
- package/dist/cjs/hooks/matchers.cjs.map +1 -1
- package/dist/cjs/hooks/types.cjs +24 -24
- package/dist/cjs/hooks/types.cjs.map +1 -1
- package/dist/cjs/instrumentation.cjs +110 -137
- package/dist/cjs/instrumentation.cjs.map +1 -1
- package/dist/cjs/langchain/google-common.cjs +0 -3
- package/dist/cjs/langchain/index.cjs +80 -43
- package/dist/cjs/langchain/language_models/chat_models.cjs +0 -3
- package/dist/cjs/langchain/messages/tool.cjs +0 -3
- package/dist/cjs/langchain/messages.cjs +35 -18
- package/dist/cjs/langchain/openai.cjs +0 -3
- package/dist/cjs/langchain/prompts.cjs +5 -8
- package/dist/cjs/langchain/runnables.cjs +11 -10
- package/dist/cjs/langchain/tools.cjs +14 -11
- package/dist/cjs/langchain/utils/env.cjs +5 -8
- package/dist/cjs/langfuse.cjs +60 -79
- package/dist/cjs/langfuse.cjs.map +1 -1
- package/dist/cjs/langfuseToolOutputTracing.cjs +267 -399
- package/dist/cjs/langfuseToolOutputTracing.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/index.cjs +432 -562
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/types.cjs +23 -47
- package/dist/cjs/llm/anthropic/types.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +441 -731
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +171 -256
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/output_parsers.cjs +2 -0
- package/dist/cjs/llm/anthropic/utils/tools.cjs +12 -26
- package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +195 -240
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/toolCache.cjs +84 -106
- package/dist/cjs/llm/bedrock/toolCache.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/utils/index.cjs +2 -0
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +357 -620
- package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/utils/message_outputs.cjs +105 -149
- package/dist/cjs/llm/bedrock/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/fake.cjs +86 -96
- package/dist/cjs/llm/fake.cjs.map +1 -1
- package/dist/cjs/llm/google/index.cjs +183 -237
- package/dist/cjs/llm/google/index.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/common.cjs +398 -674
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/zod_to_genai_parameters.cjs +2 -0
- package/dist/cjs/llm/init.cjs +44 -53
- package/dist/cjs/llm/init.cjs.map +1 -1
- package/dist/cjs/llm/invoke.cjs +142 -182
- package/dist/cjs/llm/invoke.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +991 -1276
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs +189 -316
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/index.cjs +102 -153
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/toolCache.cjs +35 -44
- package/dist/cjs/llm/openrouter/toolCache.cjs.map +1 -1
- package/dist/cjs/llm/providers.cjs +29 -37
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/llm/request.cjs +20 -33
- package/dist/cjs/llm/request.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs +427 -453
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +547 -528
- package/dist/cjs/messages/anthropicToolCache.cjs +68 -119
- package/dist/cjs/messages/anthropicToolCache.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +305 -418
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/content.cjs +36 -49
- package/dist/cjs/messages/content.cjs.map +1 -1
- package/dist/cjs/messages/contextPruning.cjs +112 -145
- package/dist/cjs/messages/contextPruning.cjs.map +1 -1
- package/dist/cjs/messages/contextPruningSettings.cjs +36 -46
- package/dist/cjs/messages/contextPruningSettings.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +256 -397
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +904 -1387
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/ids.cjs +16 -20
- package/dist/cjs/messages/ids.cjs.map +1 -1
- package/dist/cjs/messages/index.cjs +12 -0
- package/dist/cjs/messages/langchain.cjs +18 -18
- package/dist/cjs/messages/langchain.cjs.map +1 -1
- package/dist/cjs/messages/prune.cjs +1054 -1517
- package/dist/cjs/messages/prune.cjs.map +1 -1
- package/dist/cjs/messages/recency.cjs +77 -95
- package/dist/cjs/messages/recency.cjs.map +1 -1
- package/dist/cjs/messages/reducer.cjs +63 -78
- package/dist/cjs/messages/reducer.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +51 -79
- package/dist/cjs/messages/tools.cjs.map +1 -1
- package/dist/cjs/openai/index.cjs +171 -217
- package/dist/cjs/openai/index.cjs.map +1 -1
- package/dist/cjs/responses/index.cjs +302 -391
- package/dist/cjs/responses/index.cjs.map +1 -1
- package/dist/cjs/run.cjs +903 -1113
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +805 -986
- package/dist/cjs/session/AgentSession.cjs.map +1 -1
- package/dist/cjs/session/JsonlSessionStore.cjs +327 -410
- package/dist/cjs/session/JsonlSessionStore.cjs.map +1 -1
- package/dist/cjs/session/handlers.cjs +192 -208
- package/dist/cjs/session/handlers.cjs.map +1 -1
- package/dist/cjs/session/ids.cjs +9 -10
- package/dist/cjs/session/ids.cjs.map +1 -1
- package/dist/cjs/session/index.cjs +4 -0
- package/dist/cjs/session/messageSerialization.cjs +94 -156
- package/dist/cjs/session/messageSerialization.cjs.map +1 -1
- package/dist/cjs/splitStream.cjs +147 -206
- package/dist/cjs/splitStream.cjs.map +1 -1
- package/dist/cjs/stream.cjs +856 -1344
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/index.cjs +57 -101
- package/dist/cjs/summarization/index.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +643 -796
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +110 -136
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +165 -245
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/Calculator.cjs +36 -57
- package/dist/cjs/tools/Calculator.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +126 -168
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/CodeSessionFileSummary.cjs +36 -46
- package/dist/cjs/tools/CodeSessionFileSummary.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +459 -649
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ReadFile.cjs +17 -20
- package/dist/cjs/tools/ReadFile.cjs.map +1 -1
- package/dist/cjs/tools/SkillTool.cjs +26 -27
- package/dist/cjs/tools/SkillTool.cjs.map +1 -1
- package/dist/cjs/tools/SubagentTool.cjs +59 -61
- package/dist/cjs/tools/SubagentTool.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +2109 -2686
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +663 -825
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareBridgeRuntime.cjs +248 -340
- package/dist/cjs/tools/cloudflare/CloudflareBridgeRuntime.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareProgrammaticToolCalling.cjs +170 -197
- package/dist/cjs/tools/cloudflare/CloudflareProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareSandboxExecutionEngine.cjs +425 -520
- package/dist/cjs/tools/cloudflare/CloudflareSandboxExecutionEngine.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareSandboxTools.cjs +91 -124
- package/dist/cjs/tools/cloudflare/CloudflareSandboxTools.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/index.cjs +4 -0
- package/dist/cjs/tools/eagerEventExecution.cjs +75 -99
- package/dist/cjs/tools/eagerEventExecution.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +200 -262
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/local/CompileCheckTool.cjs +150 -212
- package/dist/cjs/tools/local/CompileCheckTool.cjs.map +1 -1
- package/dist/cjs/tools/local/FileCheckpointer.cjs +77 -85
- package/dist/cjs/tools/local/FileCheckpointer.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalCodingTools.cjs +763 -1022
- package/dist/cjs/tools/local/LocalCodingTools.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs +666 -941
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalExecutionTools.cjs +49 -92
- package/dist/cjs/tools/local/LocalExecutionTools.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs +286 -354
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/local/attachments.cjs +108 -165
- package/dist/cjs/tools/local/attachments.cjs.map +1 -1
- package/dist/cjs/tools/local/bashAst.cjs +99 -113
- package/dist/cjs/tools/local/bashAst.cjs.map +1 -1
- package/dist/cjs/tools/local/editStrategies.cjs +126 -169
- package/dist/cjs/tools/local/editStrategies.cjs.map +1 -1
- package/dist/cjs/tools/local/index.cjs +12 -0
- package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs +136 -218
- package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs.map +1 -1
- package/dist/cjs/tools/local/syntaxCheck.cjs +142 -161
- package/dist/cjs/tools/local/syntaxCheck.cjs.map +1 -1
- package/dist/cjs/tools/local/textEncoding.cjs +25 -23
- package/dist/cjs/tools/local/textEncoding.cjs.map +1 -1
- package/dist/cjs/tools/local/workspaceFS.cjs +38 -46
- package/dist/cjs/tools/local/workspaceFS.cjs.map +1 -1
- package/dist/cjs/tools/ptcTimeout.cjs +27 -47
- package/dist/cjs/tools/ptcTimeout.cjs.map +1 -1
- package/dist/cjs/tools/schema.cjs +24 -23
- package/dist/cjs/tools/schema.cjs.map +1 -1
- package/dist/cjs/tools/search/anthropic.cjs +24 -33
- package/dist/cjs/tools/search/anthropic.cjs.map +1 -1
- package/dist/cjs/tools/search/content.cjs +95 -137
- package/dist/cjs/tools/search/content.cjs.map +1 -1
- package/dist/cjs/tools/search/firecrawl.cjs +141 -172
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
- package/dist/cjs/tools/search/format.cjs +128 -196
- package/dist/cjs/tools/search/format.cjs.map +1 -1
- package/dist/cjs/tools/search/highlights.cjs +165 -232
- package/dist/cjs/tools/search/highlights.cjs.map +1 -1
- package/dist/cjs/tools/search/index.cjs +2 -0
- package/dist/cjs/tools/search/rerankers.cjs +151 -174
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
- package/dist/cjs/tools/search/schema.cjs +40 -39
- package/dist/cjs/tools/search/schema.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs +428 -530
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/serper-scraper.cjs +106 -127
- package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -1
- package/dist/cjs/tools/search/tavily-scraper.cjs +129 -181
- package/dist/cjs/tools/search/tavily-scraper.cjs.map +1 -1
- package/dist/cjs/tools/search/tavily-search.cjs +295 -359
- package/dist/cjs/tools/search/tavily-search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +260 -299
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs +74 -117
- package/dist/cjs/tools/search/utils.cjs.map +1 -1
- package/dist/cjs/tools/skillCatalog.cjs +54 -72
- package/dist/cjs/tools/skillCatalog.cjs.map +1 -1
- package/dist/cjs/tools/streamedToolCallSeals.cjs +19 -36
- package/dist/cjs/tools/streamedToolCallSeals.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +612 -771
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/subagent/index.cjs +1 -0
- package/dist/cjs/tools/toolOutputReferences.cjs +523 -630
- package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -1
- package/dist/cjs/utils/callbacks.cjs +11 -21
- package/dist/cjs/utils/callbacks.cjs.map +1 -1
- package/dist/cjs/utils/errors.cjs +70 -95
- package/dist/cjs/utils/errors.cjs.map +1 -1
- package/dist/cjs/utils/events.cjs +32 -42
- package/dist/cjs/utils/events.cjs.map +1 -1
- package/dist/cjs/utils/graph.cjs +8 -12
- package/dist/cjs/utils/graph.cjs.map +1 -1
- package/dist/cjs/utils/handlers.cjs +60 -82
- package/dist/cjs/utils/handlers.cjs.map +1 -1
- package/dist/cjs/utils/index.cjs +9 -0
- package/dist/cjs/utils/llm.cjs +19 -27
- package/dist/cjs/utils/llm.cjs.map +1 -1
- package/dist/cjs/utils/misc.cjs +30 -46
- package/dist/cjs/utils/misc.cjs.map +1 -1
- package/dist/cjs/utils/run.cjs +50 -66
- package/dist/cjs/utils/run.cjs.map +1 -1
- package/dist/cjs/utils/schema.cjs +11 -19
- package/dist/cjs/utils/schema.cjs.map +1 -1
- package/dist/cjs/utils/title.cjs +71 -106
- package/dist/cjs/utils/title.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +186 -283
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/cjs/utils/truncation.cjs +95 -114
- package/dist/cjs/utils/truncation.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +844 -1044
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/constants.mjs +13 -11
- package/dist/esm/common/constants.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +221 -238
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/common/index.mjs +3 -0
- package/dist/esm/events.mjs +121 -167
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +1388 -1804
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +713 -943
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/graphs/index.mjs +3 -0
- package/dist/esm/hitl/askUserQuestion.mjs +60 -60
- package/dist/esm/hitl/askUserQuestion.mjs.map +1 -1
- package/dist/esm/hitl/index.mjs +2 -0
- package/dist/esm/hooks/HookRegistry.mjs +176 -200
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
- package/dist/esm/hooks/createToolPolicyHook.mjs +71 -99
- package/dist/esm/hooks/createToolPolicyHook.mjs.map +1 -1
- package/dist/esm/hooks/createWorkspacePolicyHook.mjs +170 -271
- package/dist/esm/hooks/createWorkspacePolicyHook.mjs.map +1 -1
- package/dist/esm/hooks/executeHooks.mjs +227 -280
- package/dist/esm/hooks/executeHooks.mjs.map +1 -1
- package/dist/esm/hooks/index.mjs +7 -0
- package/dist/esm/hooks/matchers.mjs +196 -228
- package/dist/esm/hooks/matchers.mjs.map +1 -1
- package/dist/esm/hooks/types.mjs +24 -22
- package/dist/esm/hooks/types.mjs.map +1 -1
- package/dist/esm/instrumentation.mjs +109 -132
- package/dist/esm/instrumentation.mjs.map +1 -1
- package/dist/esm/langchain/google-common.mjs +1 -2
- package/dist/esm/langchain/index.mjs +5 -5
- package/dist/esm/langchain/language_models/chat_models.mjs +1 -2
- package/dist/esm/langchain/messages/tool.mjs +1 -2
- package/dist/esm/langchain/messages.mjs +2 -2
- package/dist/esm/langchain/openai.mjs +1 -2
- package/dist/esm/langchain/prompts.mjs +2 -2
- package/dist/esm/langchain/runnables.mjs +2 -2
- package/dist/esm/langchain/tools.mjs +2 -2
- package/dist/esm/langchain/utils/env.mjs +2 -2
- package/dist/esm/langfuse.mjs +60 -76
- package/dist/esm/langfuse.mjs.map +1 -1
- package/dist/esm/langfuseToolOutputTracing.mjs +267 -395
- package/dist/esm/langfuseToolOutputTracing.mjs.map +1 -1
- package/dist/esm/llm/anthropic/index.mjs +432 -559
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/types.mjs +23 -45
- package/dist/esm/llm/anthropic/types.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +439 -725
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +171 -253
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/output_parsers.mjs +3 -0
- package/dist/esm/llm/anthropic/utils/tools.mjs +12 -24
- package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +195 -238
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/bedrock/toolCache.mjs +84 -104
- package/dist/esm/llm/bedrock/toolCache.mjs.map +1 -1
- package/dist/esm/llm/bedrock/utils/index.mjs +3 -0
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs +357 -618
- package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/bedrock/utils/message_outputs.mjs +105 -147
- package/dist/esm/llm/bedrock/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/fake.mjs +86 -94
- package/dist/esm/llm/fake.mjs.map +1 -1
- package/dist/esm/llm/google/index.mjs +183 -235
- package/dist/esm/llm/google/index.mjs.map +1 -1
- package/dist/esm/llm/google/utils/common.mjs +397 -666
- package/dist/esm/llm/google/utils/common.mjs.map +1 -1
- package/dist/esm/llm/google/utils/zod_to_genai_parameters.mjs +3 -0
- package/dist/esm/llm/init.mjs +44 -51
- package/dist/esm/llm/init.mjs.map +1 -1
- package/dist/esm/llm/invoke.mjs +142 -180
- package/dist/esm/llm/invoke.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +991 -1271
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs +188 -312
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/index.mjs +102 -151
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/toolCache.mjs +35 -42
- package/dist/esm/llm/openrouter/toolCache.mjs.map +1 -1
- package/dist/esm/llm/providers.mjs +29 -34
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/llm/request.mjs +20 -31
- package/dist/esm/llm/request.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +427 -449
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/esm/main.mjs +99 -87
- package/dist/esm/messages/anthropicToolCache.mjs +68 -117
- package/dist/esm/messages/anthropicToolCache.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +305 -416
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/content.mjs +36 -47
- package/dist/esm/messages/content.mjs.map +1 -1
- package/dist/esm/messages/contextPruning.mjs +112 -143
- package/dist/esm/messages/contextPruning.mjs.map +1 -1
- package/dist/esm/messages/contextPruningSettings.mjs +36 -44
- package/dist/esm/messages/contextPruningSettings.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +254 -393
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +902 -1383
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/ids.mjs +16 -18
- package/dist/esm/messages/ids.mjs.map +1 -1
- package/dist/esm/messages/index.mjs +13 -0
- package/dist/esm/messages/langchain.mjs +18 -16
- package/dist/esm/messages/langchain.mjs.map +1 -1
- package/dist/esm/messages/prune.mjs +1053 -1514
- package/dist/esm/messages/prune.mjs.map +1 -1
- package/dist/esm/messages/recency.mjs +77 -93
- package/dist/esm/messages/recency.mjs.map +1 -1
- package/dist/esm/messages/reducer.mjs +63 -76
- package/dist/esm/messages/reducer.mjs.map +1 -1
- package/dist/esm/messages/tools.mjs +49 -75
- package/dist/esm/messages/tools.mjs.map +1 -1
- package/dist/esm/openai/index.mjs +170 -215
- package/dist/esm/openai/index.mjs.map +1 -1
- package/dist/esm/responses/index.mjs +301 -389
- package/dist/esm/responses/index.mjs.map +1 -1
- package/dist/esm/run.mjs +903 -1111
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +806 -985
- package/dist/esm/session/AgentSession.mjs.map +1 -1
- package/dist/esm/session/JsonlSessionStore.mjs +326 -407
- package/dist/esm/session/JsonlSessionStore.mjs.map +1 -1
- package/dist/esm/session/handlers.mjs +192 -206
- package/dist/esm/session/handlers.mjs.map +1 -1
- package/dist/esm/session/ids.mjs +9 -8
- package/dist/esm/session/ids.mjs.map +1 -1
- package/dist/esm/session/index.mjs +5 -0
- package/dist/esm/session/messageSerialization.mjs +94 -154
- package/dist/esm/session/messageSerialization.mjs.map +1 -1
- package/dist/esm/splitStream.mjs +147 -204
- package/dist/esm/splitStream.mjs.map +1 -1
- package/dist/esm/stream.mjs +854 -1341
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/index.mjs +57 -99
- package/dist/esm/summarization/index.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +640 -790
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +103 -129
- package/dist/esm/tools/BashExecutor.mjs.map +1 -1
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +162 -239
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/Calculator.mjs +34 -36
- package/dist/esm/tools/Calculator.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +123 -164
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/CodeSessionFileSummary.mjs +36 -44
- package/dist/esm/tools/CodeSessionFileSummary.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +454 -644
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ReadFile.mjs +17 -18
- package/dist/esm/tools/ReadFile.mjs.map +1 -1
- package/dist/esm/tools/SkillTool.mjs +26 -25
- package/dist/esm/tools/SkillTool.mjs.map +1 -1
- package/dist/esm/tools/SubagentTool.mjs +59 -59
- package/dist/esm/tools/SubagentTool.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +2107 -2684
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearch.mjs +659 -804
- package/dist/esm/tools/ToolSearch.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareBridgeRuntime.mjs +248 -338
- package/dist/esm/tools/cloudflare/CloudflareBridgeRuntime.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareProgrammaticToolCalling.mjs +170 -195
- package/dist/esm/tools/cloudflare/CloudflareProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareSandboxExecutionEngine.mjs +424 -517
- package/dist/esm/tools/cloudflare/CloudflareSandboxExecutionEngine.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareSandboxTools.mjs +91 -122
- package/dist/esm/tools/cloudflare/CloudflareSandboxTools.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/index.mjs +5 -0
- package/dist/esm/tools/eagerEventExecution.mjs +75 -96
- package/dist/esm/tools/eagerEventExecution.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +200 -260
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/local/CompileCheckTool.mjs +150 -210
- package/dist/esm/tools/local/CompileCheckTool.mjs.map +1 -1
- package/dist/esm/tools/local/FileCheckpointer.mjs +77 -83
- package/dist/esm/tools/local/FileCheckpointer.mjs.map +1 -1
- package/dist/esm/tools/local/LocalCodingTools.mjs +760 -1017
- package/dist/esm/tools/local/LocalCodingTools.mjs.map +1 -1
- package/dist/esm/tools/local/LocalExecutionEngine.mjs +663 -936
- package/dist/esm/tools/local/LocalExecutionEngine.mjs.map +1 -1
- package/dist/esm/tools/local/LocalExecutionTools.mjs +49 -90
- package/dist/esm/tools/local/LocalExecutionTools.mjs.map +1 -1
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs +283 -349
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/local/attachments.mjs +108 -163
- package/dist/esm/tools/local/attachments.mjs.map +1 -1
- package/dist/esm/tools/local/bashAst.mjs +99 -111
- package/dist/esm/tools/local/bashAst.mjs.map +1 -1
- package/dist/esm/tools/local/editStrategies.mjs +126 -167
- package/dist/esm/tools/local/editStrategies.mjs.map +1 -1
- package/dist/esm/tools/local/index.mjs +13 -0
- package/dist/esm/tools/local/resolveLocalExecutionTools.mjs +136 -216
- package/dist/esm/tools/local/resolveLocalExecutionTools.mjs.map +1 -1
- package/dist/esm/tools/local/syntaxCheck.mjs +138 -155
- package/dist/esm/tools/local/syntaxCheck.mjs.map +1 -1
- package/dist/esm/tools/local/textEncoding.mjs +25 -21
- package/dist/esm/tools/local/textEncoding.mjs.map +1 -1
- package/dist/esm/tools/local/workspaceFS.mjs +38 -44
- package/dist/esm/tools/local/workspaceFS.mjs.map +1 -1
- package/dist/esm/tools/ptcTimeout.mjs +27 -42
- package/dist/esm/tools/ptcTimeout.mjs.map +1 -1
- package/dist/esm/tools/schema.mjs +24 -21
- package/dist/esm/tools/schema.mjs.map +1 -1
- package/dist/esm/tools/search/anthropic.mjs +24 -31
- package/dist/esm/tools/search/anthropic.mjs.map +1 -1
- package/dist/esm/tools/search/content.mjs +93 -116
- package/dist/esm/tools/search/content.mjs.map +1 -1
- package/dist/esm/tools/search/firecrawl.mjs +139 -169
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
- package/dist/esm/tools/search/format.mjs +128 -194
- package/dist/esm/tools/search/format.mjs.map +1 -1
- package/dist/esm/tools/search/highlights.mjs +165 -230
- package/dist/esm/tools/search/highlights.mjs.map +1 -1
- package/dist/esm/tools/search/index.mjs +3 -0
- package/dist/esm/tools/search/rerankers.mjs +149 -168
- package/dist/esm/tools/search/rerankers.mjs.map +1 -1
- package/dist/esm/tools/search/schema.mjs +39 -37
- package/dist/esm/tools/search/schema.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs +426 -528
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/serper-scraper.mjs +104 -124
- package/dist/esm/tools/search/serper-scraper.mjs.map +1 -1
- package/dist/esm/tools/search/tavily-scraper.mjs +127 -178
- package/dist/esm/tools/search/tavily-scraper.mjs.map +1 -1
- package/dist/esm/tools/search/tavily-search.mjs +293 -357
- package/dist/esm/tools/search/tavily-search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +259 -297
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs +74 -115
- package/dist/esm/tools/search/utils.mjs.map +1 -1
- package/dist/esm/tools/skillCatalog.mjs +54 -70
- package/dist/esm/tools/skillCatalog.mjs.map +1 -1
- package/dist/esm/tools/streamedToolCallSeals.mjs +19 -31
- package/dist/esm/tools/streamedToolCallSeals.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +612 -768
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/subagent/index.mjs +2 -0
- package/dist/esm/tools/toolOutputReferences.mjs +523 -624
- package/dist/esm/tools/toolOutputReferences.mjs.map +1 -1
- package/dist/esm/utils/callbacks.mjs +11 -19
- package/dist/esm/utils/callbacks.mjs.map +1 -1
- package/dist/esm/utils/errors.mjs +70 -93
- package/dist/esm/utils/errors.mjs.map +1 -1
- package/dist/esm/utils/events.mjs +32 -40
- package/dist/esm/utils/events.mjs.map +1 -1
- package/dist/esm/utils/graph.mjs +8 -10
- package/dist/esm/utils/graph.mjs.map +1 -1
- package/dist/esm/utils/handlers.mjs +60 -80
- package/dist/esm/utils/handlers.mjs.map +1 -1
- package/dist/esm/utils/index.mjs +10 -0
- package/dist/esm/utils/llm.mjs +19 -25
- package/dist/esm/utils/llm.mjs.map +1 -1
- package/dist/esm/utils/misc.mjs +30 -44
- package/dist/esm/utils/misc.mjs.map +1 -1
- package/dist/esm/utils/run.mjs +50 -64
- package/dist/esm/utils/run.mjs.map +1 -1
- package/dist/esm/utils/schema.mjs +11 -17
- package/dist/esm/utils/schema.mjs.map +1 -1
- package/dist/esm/utils/title.mjs +71 -104
- package/dist/esm/utils/title.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +186 -281
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/esm/utils/truncation.mjs +95 -112
- package/dist/esm/utils/truncation.mjs.map +1 -1
- package/dist/types/tools/search/tool.d.ts +17 -0
- package/dist/types/tools/search/types.d.ts +4 -0
- package/package.json +4 -10
- package/src/tools/search/highlights.ts +9 -1
- package/src/tools/search/search.ts +41 -3
- package/src/tools/search/source-processing.test.ts +373 -0
- package/src/tools/search/tool.ts +22 -2
- package/src/tools/search/types.ts +4 -0
- package/dist/cjs/langchain/google-common.cjs.map +0 -1
- package/dist/cjs/langchain/index.cjs.map +0 -1
- package/dist/cjs/langchain/language_models/chat_models.cjs.map +0 -1
- package/dist/cjs/langchain/messages/tool.cjs.map +0 -1
- package/dist/cjs/langchain/messages.cjs.map +0 -1
- package/dist/cjs/langchain/openai.cjs.map +0 -1
- package/dist/cjs/langchain/prompts.cjs.map +0 -1
- package/dist/cjs/langchain/runnables.cjs.map +0 -1
- package/dist/cjs/langchain/tools.cjs.map +0 -1
- package/dist/cjs/langchain/utils/env.cjs.map +0 -1
- package/dist/cjs/main.cjs.map +0 -1
- package/dist/esm/langchain/google-common.mjs.map +0 -1
- package/dist/esm/langchain/index.mjs.map +0 -1
- package/dist/esm/langchain/language_models/chat_models.mjs.map +0 -1
- package/dist/esm/langchain/messages/tool.mjs.map +0 -1
- package/dist/esm/langchain/messages.mjs.map +0 -1
- package/dist/esm/langchain/openai.mjs.map +0 -1
- package/dist/esm/langchain/prompts.mjs.map +0 -1
- package/dist/esm/langchain/runnables.mjs.map +0 -1
- package/dist/esm/langchain/tools.mjs.map +0 -1
- package/dist/esm/langchain/utils/env.mjs.map +0 -1
- package/dist/esm/main.mjs.map +0 -1
|
@@ -1,607 +1,480 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
1
|
+
import { _convertMessagesToAnthropicPayload, stripUnsupportedAssistantPrefill } from "./utils/message_inputs.mjs";
|
|
2
|
+
import { _makeMessageChunkFromAnthropicEvent } from "./utils/message_outputs.mjs";
|
|
3
|
+
import { handleToolChoice } from "./utils/tools.mjs";
|
|
4
|
+
import { AIMessageChunk } from "@langchain/core/messages";
|
|
5
|
+
import { ChatGenerationChunk } from "@langchain/core/outputs";
|
|
6
|
+
import { ChatAnthropicMessages } from "@langchain/anthropic";
|
|
7
|
+
//#region src/llm/anthropic/index.ts
|
|
8
8
|
const DEFAULT_STREAM_DELAY = 25;
|
|
9
9
|
const MAX_STREAM_QUEUE_CHUNKS = 256;
|
|
10
10
|
const MAX_STREAM_QUEUE_TEXT_CHARS = 8192;
|
|
11
11
|
const STREAM_CHUNK_MIN_SIZE = 4;
|
|
12
|
-
const STREAM_BOUNDARIES = new Set([
|
|
12
|
+
const STREAM_BOUNDARIES = new Set([
|
|
13
|
+
" ",
|
|
14
|
+
".",
|
|
15
|
+
",",
|
|
16
|
+
"!",
|
|
17
|
+
"?",
|
|
18
|
+
";",
|
|
19
|
+
":"
|
|
20
|
+
]);
|
|
13
21
|
const ANTHROPIC_TOOL_BETAS = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
tool_search_tool_regex_20251119: "advanced-tool-use-2025-11-20",
|
|
23
|
+
tool_search_tool_bm25_20251119: "advanced-tool-use-2025-11-20",
|
|
24
|
+
memory_20250818: "context-management-2025-06-27",
|
|
25
|
+
web_fetch_20250910: "web-fetch-2025-09-10",
|
|
26
|
+
code_execution_20250825: "code-execution-2025-08-25",
|
|
27
|
+
computer_20251124: "computer-use-2025-11-24",
|
|
28
|
+
computer_20250124: "computer-use-2025-01-24",
|
|
29
|
+
mcp_toolset: "mcp-client-2025-11-20"
|
|
22
30
|
};
|
|
23
31
|
function _toolsInParams(params) {
|
|
24
|
-
|
|
32
|
+
return !!(params.tools && params.tools.length > 0);
|
|
25
33
|
}
|
|
26
34
|
function _documentsInParams(params) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
'type' in maybeBlock &&
|
|
36
|
-
maybeBlock.type === 'document' &&
|
|
37
|
-
'citations' in maybeBlock &&
|
|
38
|
-
maybeBlock.citations != null &&
|
|
39
|
-
typeof maybeBlock.citations === 'object' &&
|
|
40
|
-
'enabled' in maybeBlock.citations &&
|
|
41
|
-
maybeBlock.citations.enabled === true) {
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return false;
|
|
35
|
+
for (const message of params.messages) {
|
|
36
|
+
if (typeof message.content === "string") continue;
|
|
37
|
+
for (const block of message.content) {
|
|
38
|
+
const maybeBlock = block;
|
|
39
|
+
if (typeof maybeBlock === "object" && maybeBlock !== null && "type" in maybeBlock && maybeBlock.type === "document" && "citations" in maybeBlock && maybeBlock.citations != null && typeof maybeBlock.citations === "object" && "enabled" in maybeBlock.citations && maybeBlock.citations.enabled === true) return true;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
47
43
|
}
|
|
48
44
|
function _thinkingInParams(params) {
|
|
49
|
-
|
|
50
|
-
(params.thinking.type === 'enabled' || params.thinking.type === 'adaptive'));
|
|
45
|
+
return !!(params.thinking && (params.thinking.type === "enabled" || params.thinking.type === "adaptive"));
|
|
51
46
|
}
|
|
52
47
|
function _compactionInParams(params) {
|
|
53
|
-
|
|
48
|
+
return params.context_management?.edits?.some((edit) => edit.type === "compact_20260112") === true;
|
|
54
49
|
}
|
|
55
50
|
function isThinkingEnabled(thinking) {
|
|
56
|
-
|
|
51
|
+
return thinking.type === "enabled" || thinking.type === "adaptive";
|
|
57
52
|
}
|
|
58
53
|
function isOpus47Model(model) {
|
|
59
|
-
|
|
54
|
+
return /^claude-opus-4-7(?:-|$)/.test(model ?? "");
|
|
60
55
|
}
|
|
61
56
|
function combineBetas(...betaGroups) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
betas.add(beta);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return [...betas];
|
|
57
|
+
const betas = /* @__PURE__ */ new Set();
|
|
58
|
+
for (const betaGroup of betaGroups) for (const beta of betaGroup ?? []) betas.add(beta);
|
|
59
|
+
return [...betas];
|
|
69
60
|
}
|
|
70
61
|
function getToolBetas(tools) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
betas.add(beta);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return [...betas];
|
|
62
|
+
const betas = /* @__PURE__ */ new Set();
|
|
63
|
+
for (const tool of tools ?? []) {
|
|
64
|
+
if (typeof tool !== "object" || !("type" in tool)) continue;
|
|
65
|
+
const beta = ANTHROPIC_TOOL_BETAS[String(tool.type)];
|
|
66
|
+
if (beta != null) betas.add(beta);
|
|
67
|
+
}
|
|
68
|
+
return [...betas];
|
|
82
69
|
}
|
|
83
70
|
function getCompactionBetas(contextManagement) {
|
|
84
|
-
|
|
85
|
-
? ['compact-2026-01-12']
|
|
86
|
-
: [];
|
|
71
|
+
return contextManagement?.edits?.some((edit) => edit.type === "compact_20260112") === true ? ["compact-2026-01-12"] : [];
|
|
87
72
|
}
|
|
88
73
|
function getTaskBudgetBetas(model, outputConfig) {
|
|
89
|
-
|
|
90
|
-
outputConfig != null &&
|
|
91
|
-
'task_budget' in outputConfig &&
|
|
92
|
-
outputConfig.task_budget != null
|
|
93
|
-
? ['task-budgets-2026-03-13']
|
|
94
|
-
: [];
|
|
74
|
+
return isOpus47Model(model) && outputConfig != null && "task_budget" in outputConfig && outputConfig.task_budget != null ? ["task-budgets-2026-03-13"] : [];
|
|
95
75
|
}
|
|
96
76
|
function isSetSamplingValue(value) {
|
|
97
|
-
|
|
77
|
+
return value != null && value !== -1;
|
|
98
78
|
}
|
|
99
79
|
function isNonDefaultTemperature(value) {
|
|
100
|
-
|
|
80
|
+
return isSetSamplingValue(value) && value !== 1;
|
|
101
81
|
}
|
|
102
|
-
function validateInvocationParamCompatibility({ model, thinking, topK, topP, temperature
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
throw new Error('topP is not supported for claude-opus-4-7 when set to non-default values');
|
|
116
|
-
}
|
|
117
|
-
if (isNonDefaultTemperature(temperature)) {
|
|
118
|
-
throw new Error('temperature is not supported for claude-opus-4-7 when set to non-default values');
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (!isThinkingEnabled(thinking)) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
if (isSetSamplingValue(topK)) {
|
|
125
|
-
throw new Error('topK is not supported when thinking is enabled');
|
|
126
|
-
}
|
|
127
|
-
if (isSetSamplingValue(topP)) {
|
|
128
|
-
throw new Error('topP is not supported when thinking is enabled');
|
|
129
|
-
}
|
|
130
|
-
if (isNonDefaultTemperature(temperature)) {
|
|
131
|
-
throw new Error('temperature is not supported when thinking is enabled');
|
|
132
|
-
}
|
|
82
|
+
function validateInvocationParamCompatibility({ model, thinking, topK, topP, temperature }) {
|
|
83
|
+
const opus47 = isOpus47Model(model);
|
|
84
|
+
if (opus47 && thinking.type === "enabled") throw new Error("thinking.type=\"enabled\" is not supported for claude-opus-4-7; use thinking.type=\"adaptive\" instead");
|
|
85
|
+
if (opus47 && "budget_tokens" in thinking) throw new Error("thinking.budget_tokens is not supported for claude-opus-4-7; use outputConfig.effort instead");
|
|
86
|
+
if (opus47) {
|
|
87
|
+
if (isSetSamplingValue(topK)) throw new Error("topK is not supported for claude-opus-4-7; omit topK/topP/temperature or use model prompting instead");
|
|
88
|
+
if (isSetSamplingValue(topP) && topP !== 1) throw new Error("topP is not supported for claude-opus-4-7 when set to non-default values");
|
|
89
|
+
if (isNonDefaultTemperature(temperature)) throw new Error("temperature is not supported for claude-opus-4-7 when set to non-default values");
|
|
90
|
+
}
|
|
91
|
+
if (!isThinkingEnabled(thinking)) return;
|
|
92
|
+
if (isSetSamplingValue(topK)) throw new Error("topK is not supported when thinking is enabled");
|
|
93
|
+
if (isSetSamplingValue(topP)) throw new Error("topP is not supported when thinking is enabled");
|
|
94
|
+
if (isNonDefaultTemperature(temperature)) throw new Error("temperature is not supported when thinking is enabled");
|
|
133
95
|
}
|
|
134
|
-
function getSamplingParams({ model, thinking, topK, topP, temperature
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
...(isSetSamplingValue(topP) ? { top_p: topP } : {}),
|
|
142
|
-
};
|
|
96
|
+
function getSamplingParams({ model, thinking, topK, topP, temperature }) {
|
|
97
|
+
if (isThinkingEnabled(thinking) || isOpus47Model(model)) return {};
|
|
98
|
+
return {
|
|
99
|
+
...isSetSamplingValue(temperature) ? { temperature } : {},
|
|
100
|
+
...isSetSamplingValue(topK) ? { top_k: topK } : {},
|
|
101
|
+
...isSetSamplingValue(topP) ? { top_p: topP } : {}
|
|
102
|
+
};
|
|
143
103
|
}
|
|
144
104
|
function findStreamChunkBoundary(text, minSize) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
for (let position = minSize; position < text.length; position++) {
|
|
149
|
-
if (STREAM_BOUNDARIES.has(text[position])) {
|
|
150
|
-
return position + 1;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return text.length;
|
|
105
|
+
if (minSize >= text.length) return text.length;
|
|
106
|
+
for (let position = minSize; position < text.length; position++) if (STREAM_BOUNDARIES.has(text[position])) return position + 1;
|
|
107
|
+
return text.length;
|
|
154
108
|
}
|
|
155
109
|
function splitStreamToken(text) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
return chunks;
|
|
110
|
+
const chunks = [];
|
|
111
|
+
let currentIndex = 0;
|
|
112
|
+
while (currentIndex < text.length) {
|
|
113
|
+
const chunkSize = findStreamChunkBoundary(text.slice(currentIndex), STREAM_CHUNK_MIN_SIZE);
|
|
114
|
+
chunks.push(text.slice(currentIndex, currentIndex + chunkSize));
|
|
115
|
+
currentIndex += chunkSize;
|
|
116
|
+
}
|
|
117
|
+
return chunks;
|
|
165
118
|
}
|
|
166
|
-
function getCadencedStreamDelay({ targetDelay, lastVisibleTextAt, now
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
return Math.max(0, targetDelay - (now - lastVisibleTextAt));
|
|
119
|
+
function getCadencedStreamDelay({ targetDelay, lastVisibleTextAt, now }) {
|
|
120
|
+
if (targetDelay <= 0 || lastVisibleTextAt == null) return 0;
|
|
121
|
+
return Math.max(0, targetDelay - (now - lastVisibleTextAt));
|
|
171
122
|
}
|
|
172
123
|
async function waitForStreamDelay(delay, signal) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}, delay);
|
|
189
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
190
|
-
if (isSignalAborted(signal)) {
|
|
191
|
-
onAbort();
|
|
192
|
-
}
|
|
193
|
-
});
|
|
124
|
+
if (delay <= 0 || isSignalAborted(signal)) return;
|
|
125
|
+
await new Promise((resolve) => {
|
|
126
|
+
const timeoutRef = {};
|
|
127
|
+
const onAbort = () => {
|
|
128
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
129
|
+
signal?.removeEventListener("abort", onAbort);
|
|
130
|
+
resolve();
|
|
131
|
+
};
|
|
132
|
+
timeoutRef.current = setTimeout(() => {
|
|
133
|
+
signal?.removeEventListener("abort", onAbort);
|
|
134
|
+
resolve();
|
|
135
|
+
}, delay);
|
|
136
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
137
|
+
if (isSignalAborted(signal)) onAbort();
|
|
138
|
+
});
|
|
194
139
|
}
|
|
195
140
|
function isSignalAborted(signal) {
|
|
196
|
-
|
|
141
|
+
return signal?.aborted === true;
|
|
197
142
|
}
|
|
198
143
|
function extractToken(chunk) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
else if (Array.isArray(chunk.content) &&
|
|
210
|
-
chunk.content.length >= 1 &&
|
|
211
|
-
'text' in chunk.content[0]) {
|
|
212
|
-
const text = chunk.content[0].text;
|
|
213
|
-
return typeof text === 'string' ? [text, 'content'] : [undefined];
|
|
214
|
-
}
|
|
215
|
-
else if (Array.isArray(chunk.content) &&
|
|
216
|
-
chunk.content.length >= 1 &&
|
|
217
|
-
'thinking' in chunk.content[0]) {
|
|
218
|
-
const thinking = chunk.content[0].thinking;
|
|
219
|
-
return typeof thinking === 'string' ? [thinking, 'content'] : [undefined];
|
|
220
|
-
}
|
|
221
|
-
return [undefined];
|
|
144
|
+
if (typeof chunk.content === "string") return [chunk.content, "string"];
|
|
145
|
+
else if (Array.isArray(chunk.content) && chunk.content.length >= 1 && "input" in chunk.content[0]) return typeof chunk.content[0].input === "string" ? [chunk.content[0].input, "input"] : [JSON.stringify(chunk.content[0].input), "input"];
|
|
146
|
+
else if (Array.isArray(chunk.content) && chunk.content.length >= 1 && "text" in chunk.content[0]) {
|
|
147
|
+
const text = chunk.content[0].text;
|
|
148
|
+
return typeof text === "string" ? [text, "content"] : [void 0];
|
|
149
|
+
} else if (Array.isArray(chunk.content) && chunk.content.length >= 1 && "thinking" in chunk.content[0]) {
|
|
150
|
+
const thinking = chunk.content[0].thinking;
|
|
151
|
+
return typeof thinking === "string" ? [thinking, "content"] : [void 0];
|
|
152
|
+
}
|
|
153
|
+
return [void 0];
|
|
222
154
|
}
|
|
223
155
|
function cloneChunk(text, tokenType, chunk) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (content.type === 'text') {
|
|
232
|
-
return new AIMessageChunk(Object.assign({}, chunk, {
|
|
233
|
-
content: [Object.assign({}, content, { text })],
|
|
234
|
-
}));
|
|
235
|
-
}
|
|
236
|
-
else if (content.type === 'text_delta') {
|
|
237
|
-
return new AIMessageChunk(Object.assign({}, chunk, {
|
|
238
|
-
content: [Object.assign({}, content, { text })],
|
|
239
|
-
}));
|
|
240
|
-
}
|
|
241
|
-
else if (typeof content.type === 'string' &&
|
|
242
|
-
content.type.startsWith('thinking')) {
|
|
243
|
-
return new AIMessageChunk(Object.assign({}, chunk, {
|
|
244
|
-
content: [Object.assign({}, content, { thinking: text })],
|
|
245
|
-
}));
|
|
246
|
-
}
|
|
247
|
-
return chunk;
|
|
156
|
+
if (tokenType === "string") return new AIMessageChunk(Object.assign({}, chunk, { content: text }));
|
|
157
|
+
else if (tokenType === "input") return chunk;
|
|
158
|
+
const content = chunk.content[0];
|
|
159
|
+
if (content.type === "text") return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));
|
|
160
|
+
else if (content.type === "text_delta") return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { text })] }));
|
|
161
|
+
else if (typeof content.type === "string" && content.type.startsWith("thinking")) return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { thinking: text })] }));
|
|
162
|
+
return chunk;
|
|
248
163
|
}
|
|
249
164
|
function withIncrementalMessageDeltaUsage(chunk, previousOutputTokens) {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
class CustomAnthropic extends ChatAnthropicMessages {
|
|
267
|
-
_lc_stream_delay;
|
|
268
|
-
tools_in_params;
|
|
269
|
-
top_k;
|
|
270
|
-
outputConfig;
|
|
271
|
-
inferenceGeo;
|
|
272
|
-
contextManagement;
|
|
273
|
-
constructor(fields) {
|
|
274
|
-
super(fields);
|
|
275
|
-
this.resetTokenEvents();
|
|
276
|
-
this.setDirectFields(fields);
|
|
277
|
-
this._lc_stream_delay = Math.max(0, fields?._lc_stream_delay ?? DEFAULT_STREAM_DELAY);
|
|
278
|
-
this.outputConfig = fields?.outputConfig;
|
|
279
|
-
this.inferenceGeo = fields?.inferenceGeo;
|
|
280
|
-
this.contextManagement = fields?.contextManagement;
|
|
281
|
-
}
|
|
282
|
-
static lc_name() {
|
|
283
|
-
return 'LibreChatAnthropic';
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* Get the parameters used to invoke the model
|
|
287
|
-
*/
|
|
288
|
-
invocationParams(options) {
|
|
289
|
-
const tool_choice = handleToolChoice(options?.tool_choice);
|
|
290
|
-
const callOptions = options;
|
|
291
|
-
const mergedOutputConfig = (() => {
|
|
292
|
-
const base = {
|
|
293
|
-
...this.outputConfig,
|
|
294
|
-
...callOptions?.outputConfig,
|
|
295
|
-
};
|
|
296
|
-
if (callOptions?.outputFormat && !base.format) {
|
|
297
|
-
base.format = callOptions.outputFormat;
|
|
298
|
-
}
|
|
299
|
-
return Object.keys(base).length > 0 ? base : undefined;
|
|
300
|
-
})();
|
|
301
|
-
const inferenceGeo = callOptions?.inferenceGeo ?? this.inferenceGeo;
|
|
302
|
-
const contextManagement = this.contextManagement;
|
|
303
|
-
const toolBetas = getToolBetas(options?.tools);
|
|
304
|
-
const compactionBetas = getCompactionBetas(contextManagement);
|
|
305
|
-
const taskBudgetBetas = getTaskBudgetBetas(this.model, mergedOutputConfig);
|
|
306
|
-
const formattedTools = this.formatStructuredToolToAnthropic(options?.tools);
|
|
307
|
-
const sharedParams = {
|
|
308
|
-
tools: formattedTools,
|
|
309
|
-
tool_choice,
|
|
310
|
-
thinking: this.thinking,
|
|
311
|
-
context_management: contextManagement,
|
|
312
|
-
...this.invocationKwargs,
|
|
313
|
-
container: callOptions?.container,
|
|
314
|
-
betas: combineBetas(this.betas, callOptions?.betas, toolBetas, compactionBetas, taskBudgetBetas),
|
|
315
|
-
output_config: mergedOutputConfig,
|
|
316
|
-
inference_geo: inferenceGeo,
|
|
317
|
-
mcp_servers: callOptions?.mcp_servers,
|
|
318
|
-
};
|
|
319
|
-
validateInvocationParamCompatibility({
|
|
320
|
-
model: this.model,
|
|
321
|
-
thinking: this.thinking,
|
|
322
|
-
topK: this.top_k,
|
|
323
|
-
topP: this.topP,
|
|
324
|
-
temperature: this.temperature,
|
|
325
|
-
});
|
|
326
|
-
return {
|
|
327
|
-
model: this.model,
|
|
328
|
-
stop_sequences: options?.stop ?? this.stopSequences,
|
|
329
|
-
stream: this.streaming,
|
|
330
|
-
max_tokens: this.maxTokens,
|
|
331
|
-
...getSamplingParams({
|
|
332
|
-
model: this.model,
|
|
333
|
-
thinking: this.thinking,
|
|
334
|
-
topK: this.top_k,
|
|
335
|
-
topP: this.topP,
|
|
336
|
-
temperature: this.temperature,
|
|
337
|
-
}),
|
|
338
|
-
...sharedParams,
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
resetTokenEvents() {
|
|
342
|
-
this.tools_in_params = undefined;
|
|
343
|
-
}
|
|
344
|
-
setDirectFields(fields) {
|
|
345
|
-
this.temperature = fields?.temperature ?? undefined;
|
|
346
|
-
this.topP = fields?.topP ?? undefined;
|
|
347
|
-
this.top_k = fields?.topK;
|
|
348
|
-
if (this.temperature === -1 || this.temperature === 1) {
|
|
349
|
-
this.temperature = undefined;
|
|
350
|
-
}
|
|
351
|
-
if (this.topP === -1) {
|
|
352
|
-
this.topP = undefined;
|
|
353
|
-
}
|
|
354
|
-
if (this.top_k === -1) {
|
|
355
|
-
this.top_k = undefined;
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
createGenerationChunk({ token, chunk, shouldStreamUsage, }) {
|
|
359
|
-
const usage_metadata = shouldStreamUsage ? chunk.usage_metadata : undefined;
|
|
360
|
-
return new ChatGenerationChunk({
|
|
361
|
-
message: new AIMessageChunk({
|
|
362
|
-
// Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().
|
|
363
|
-
content: chunk.content,
|
|
364
|
-
additional_kwargs: chunk.additional_kwargs,
|
|
365
|
-
tool_call_chunks: chunk.tool_call_chunks,
|
|
366
|
-
response_metadata: chunk.response_metadata,
|
|
367
|
-
usage_metadata,
|
|
368
|
-
id: chunk.id,
|
|
369
|
-
}),
|
|
370
|
-
text: token ?? '',
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
async createStreamWithRetry(request, options) {
|
|
374
|
-
return super.createStreamWithRetry(stripUnsupportedAssistantPrefill(request), options);
|
|
375
|
-
}
|
|
376
|
-
async completionWithRetry(request, options) {
|
|
377
|
-
return super.completionWithRetry(stripUnsupportedAssistantPrefill(request), options);
|
|
378
|
-
}
|
|
379
|
-
async *_streamResponseChunks(messages, options, runManager) {
|
|
380
|
-
this.resetTokenEvents();
|
|
381
|
-
const params = this.invocationParams(options);
|
|
382
|
-
const formattedMessages = _convertMessagesToAnthropicPayload(messages);
|
|
383
|
-
const payload = stripUnsupportedAssistantPrefill({
|
|
384
|
-
...params,
|
|
385
|
-
...formattedMessages,
|
|
386
|
-
stream: true,
|
|
387
|
-
});
|
|
388
|
-
const coerceContentToString = !_toolsInParams(payload) &&
|
|
389
|
-
!_documentsInParams(payload) &&
|
|
390
|
-
!_thinkingInParams(payload) &&
|
|
391
|
-
!_compactionInParams(payload);
|
|
392
|
-
const stream = await this.createStreamWithRetry(payload, {
|
|
393
|
-
headers: options.headers,
|
|
394
|
-
signal: options.signal,
|
|
395
|
-
});
|
|
396
|
-
const shouldStreamUsage = options.streamUsage ?? this.streamUsage;
|
|
397
|
-
let messageDeltaOutputTokens = 0;
|
|
398
|
-
const queuedChunks = [];
|
|
399
|
-
const producerState = { done: false };
|
|
400
|
-
let queuedChunkIndex = 0;
|
|
401
|
-
let bufferedTextLength = 0;
|
|
402
|
-
let consumerClosed = false;
|
|
403
|
-
let notifyConsumer;
|
|
404
|
-
let notifyProducer;
|
|
405
|
-
const notifyConsumerForChunk = () => {
|
|
406
|
-
notifyConsumer?.();
|
|
407
|
-
notifyConsumer = undefined;
|
|
408
|
-
};
|
|
409
|
-
const notifyProducerForSpace = () => {
|
|
410
|
-
notifyProducer?.();
|
|
411
|
-
notifyProducer = undefined;
|
|
412
|
-
};
|
|
413
|
-
const hasQueuedChunks = () => queuedChunkIndex < queuedChunks.length;
|
|
414
|
-
const getQueuedChunkCount = () => queuedChunks.length - queuedChunkIndex;
|
|
415
|
-
const isQueueAtCapacity = () => getQueuedChunkCount() >= MAX_STREAM_QUEUE_CHUNKS ||
|
|
416
|
-
bufferedTextLength >= MAX_STREAM_QUEUE_TEXT_CHARS;
|
|
417
|
-
const waitForNextChunk = async () => {
|
|
418
|
-
if (hasQueuedChunks() ||
|
|
419
|
-
producerState.done ||
|
|
420
|
-
producerState.error != null) {
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
|
-
await new Promise((resolve) => {
|
|
424
|
-
notifyConsumer = resolve;
|
|
425
|
-
});
|
|
426
|
-
};
|
|
427
|
-
const waitForQueueSpace = async () => {
|
|
428
|
-
while (isQueueAtCapacity() &&
|
|
429
|
-
!consumerClosed &&
|
|
430
|
-
!isSignalAborted(options.signal)) {
|
|
431
|
-
await new Promise((resolve) => {
|
|
432
|
-
const signal = options.signal;
|
|
433
|
-
const onAbort = () => {
|
|
434
|
-
signal?.removeEventListener('abort', onAbort);
|
|
435
|
-
resolve();
|
|
436
|
-
};
|
|
437
|
-
const onSpace = () => {
|
|
438
|
-
signal?.removeEventListener('abort', onAbort);
|
|
439
|
-
resolve();
|
|
440
|
-
};
|
|
441
|
-
notifyProducer = onSpace;
|
|
442
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
443
|
-
if (isSignalAborted(signal)) {
|
|
444
|
-
onAbort();
|
|
445
|
-
}
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
const dequeue = () => {
|
|
450
|
-
if (!hasQueuedChunks()) {
|
|
451
|
-
return undefined;
|
|
452
|
-
}
|
|
453
|
-
const queuedChunk = queuedChunks[queuedChunkIndex];
|
|
454
|
-
queuedChunkIndex++;
|
|
455
|
-
if (queuedChunkIndex > 128 &&
|
|
456
|
-
queuedChunkIndex * 2 >= queuedChunks.length) {
|
|
457
|
-
queuedChunks.splice(0, queuedChunkIndex);
|
|
458
|
-
queuedChunkIndex = 0;
|
|
459
|
-
}
|
|
460
|
-
return queuedChunk;
|
|
461
|
-
};
|
|
462
|
-
const enqueue = async (queuedChunk) => {
|
|
463
|
-
await waitForQueueSpace();
|
|
464
|
-
if (consumerClosed || isSignalAborted(options.signal)) {
|
|
465
|
-
stream.controller.abort();
|
|
466
|
-
throw new Error('AbortError: User aborted the request.');
|
|
467
|
-
}
|
|
468
|
-
queuedChunks.push(queuedChunk);
|
|
469
|
-
if (queuedChunk.smooth) {
|
|
470
|
-
bufferedTextLength += queuedChunk.textLength;
|
|
471
|
-
}
|
|
472
|
-
notifyConsumerForChunk();
|
|
473
|
-
};
|
|
474
|
-
const enqueueChunk = async ({ token, chunk, smooth, }) => {
|
|
475
|
-
await enqueue({
|
|
476
|
-
token,
|
|
477
|
-
smooth,
|
|
478
|
-
textLength: smooth ? token.length : 0,
|
|
479
|
-
chunk: this.createGenerationChunk({
|
|
480
|
-
token,
|
|
481
|
-
chunk,
|
|
482
|
-
shouldStreamUsage,
|
|
483
|
-
}),
|
|
484
|
-
});
|
|
485
|
-
};
|
|
486
|
-
const enqueueTextChunks = (token, tokenType, chunk) => {
|
|
487
|
-
if (token === '') {
|
|
488
|
-
return Promise.resolve();
|
|
489
|
-
}
|
|
490
|
-
if (this._lc_stream_delay <= 0) {
|
|
491
|
-
return enqueueChunk({ token, chunk, smooth: false });
|
|
492
|
-
}
|
|
493
|
-
const tokenChunks = splitStreamToken(token);
|
|
494
|
-
if (tokenChunks.length <= 1) {
|
|
495
|
-
return enqueueChunk({ token, chunk, smooth: true });
|
|
496
|
-
}
|
|
497
|
-
let emittedUsage = false;
|
|
498
|
-
return tokenChunks.reduce(async (previous, currentToken) => {
|
|
499
|
-
await previous;
|
|
500
|
-
const newChunk = cloneChunk(currentToken, tokenType, chunk);
|
|
501
|
-
const chunkForToken = emittedUsage && newChunk.usage_metadata != null
|
|
502
|
-
? new AIMessageChunk(Object.assign({}, newChunk, { usage_metadata: undefined }))
|
|
503
|
-
: newChunk;
|
|
504
|
-
await enqueueChunk({
|
|
505
|
-
token: currentToken,
|
|
506
|
-
chunk: chunkForToken,
|
|
507
|
-
smooth: true,
|
|
508
|
-
});
|
|
509
|
-
if (newChunk.usage_metadata != null && !emittedUsage) {
|
|
510
|
-
emittedUsage = true;
|
|
511
|
-
}
|
|
512
|
-
}, Promise.resolve());
|
|
513
|
-
};
|
|
514
|
-
const producer = (async () => {
|
|
515
|
-
try {
|
|
516
|
-
for await (const data of stream) {
|
|
517
|
-
if (isSignalAborted(options.signal)) {
|
|
518
|
-
stream.controller.abort();
|
|
519
|
-
throw new Error('AbortError: User aborted the request.');
|
|
520
|
-
}
|
|
521
|
-
const result = _makeMessageChunkFromAnthropicEvent(data, {
|
|
522
|
-
streamUsage: shouldStreamUsage,
|
|
523
|
-
coerceContentToString,
|
|
524
|
-
});
|
|
525
|
-
if (!result) {
|
|
526
|
-
continue;
|
|
527
|
-
}
|
|
528
|
-
let { chunk } = result;
|
|
529
|
-
if (data.type === 'message_delta') {
|
|
530
|
-
const incremental = withIncrementalMessageDeltaUsage(chunk, messageDeltaOutputTokens);
|
|
531
|
-
chunk = incremental.chunk;
|
|
532
|
-
messageDeltaOutputTokens = incremental.outputTokens;
|
|
533
|
-
}
|
|
534
|
-
const [token = '', tokenType] = extractToken(chunk);
|
|
535
|
-
if (!tokenType ||
|
|
536
|
-
tokenType === 'input' ||
|
|
537
|
-
(token === '' && (chunk.usage_metadata != null || chunk.id != null))) {
|
|
538
|
-
await enqueueChunk({ token, chunk, smooth: false });
|
|
539
|
-
continue;
|
|
540
|
-
}
|
|
541
|
-
await enqueueTextChunks(token, tokenType, chunk);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
catch (error) {
|
|
545
|
-
producerState.error = error;
|
|
546
|
-
}
|
|
547
|
-
finally {
|
|
548
|
-
producerState.done = true;
|
|
549
|
-
notifyConsumerForChunk();
|
|
550
|
-
}
|
|
551
|
-
})();
|
|
552
|
-
let hasEmittedText = false;
|
|
553
|
-
let lastVisibleTextAt;
|
|
554
|
-
let keepStreaming = true;
|
|
555
|
-
try {
|
|
556
|
-
while (keepStreaming) {
|
|
557
|
-
if (isSignalAborted(options.signal)) {
|
|
558
|
-
stream.controller.abort();
|
|
559
|
-
throw new Error('AbortError: User aborted the request.');
|
|
560
|
-
}
|
|
561
|
-
await waitForNextChunk();
|
|
562
|
-
const queuedChunk = dequeue();
|
|
563
|
-
if (!queuedChunk) {
|
|
564
|
-
if (producerState.error != null) {
|
|
565
|
-
throw producerState.error;
|
|
566
|
-
}
|
|
567
|
-
if (producerState.done) {
|
|
568
|
-
keepStreaming = false;
|
|
569
|
-
}
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
|
-
if (queuedChunk.smooth) {
|
|
573
|
-
bufferedTextLength = Math.max(0, bufferedTextLength - queuedChunk.textLength);
|
|
574
|
-
notifyProducerForSpace();
|
|
575
|
-
await waitForStreamDelay(getCadencedStreamDelay({
|
|
576
|
-
targetDelay: hasEmittedText ? this._lc_stream_delay : 0,
|
|
577
|
-
lastVisibleTextAt,
|
|
578
|
-
now: Date.now(),
|
|
579
|
-
}), options.signal);
|
|
580
|
-
if (isSignalAborted(options.signal)) {
|
|
581
|
-
stream.controller.abort();
|
|
582
|
-
throw new Error('AbortError: User aborted the request.');
|
|
583
|
-
}
|
|
584
|
-
hasEmittedText = true;
|
|
585
|
-
lastVisibleTextAt = Date.now();
|
|
586
|
-
}
|
|
587
|
-
else {
|
|
588
|
-
notifyProducerForSpace();
|
|
589
|
-
}
|
|
590
|
-
yield queuedChunk.chunk;
|
|
591
|
-
await runManager?.handleLLMNewToken(queuedChunk.token, undefined, undefined, undefined, undefined, { chunk: queuedChunk.chunk });
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
finally {
|
|
595
|
-
consumerClosed = true;
|
|
596
|
-
if (!producerState.done) {
|
|
597
|
-
stream.controller.abort();
|
|
598
|
-
notifyProducerForSpace();
|
|
599
|
-
}
|
|
600
|
-
await producer;
|
|
601
|
-
this.resetTokenEvents();
|
|
602
|
-
}
|
|
603
|
-
}
|
|
165
|
+
const usage = chunk.usage_metadata;
|
|
166
|
+
if (usage == null) return {
|
|
167
|
+
chunk,
|
|
168
|
+
outputTokens: previousOutputTokens
|
|
169
|
+
};
|
|
170
|
+
const outputTokens = Math.max(0, usage.output_tokens - previousOutputTokens);
|
|
171
|
+
return {
|
|
172
|
+
chunk: new AIMessageChunk(Object.assign({}, chunk, { usage_metadata: {
|
|
173
|
+
...usage,
|
|
174
|
+
output_tokens: outputTokens,
|
|
175
|
+
total_tokens: usage.input_tokens + outputTokens
|
|
176
|
+
} })),
|
|
177
|
+
outputTokens: usage.output_tokens
|
|
178
|
+
};
|
|
604
179
|
}
|
|
180
|
+
var CustomAnthropic = class extends ChatAnthropicMessages {
|
|
181
|
+
_lc_stream_delay;
|
|
182
|
+
tools_in_params;
|
|
183
|
+
top_k;
|
|
184
|
+
outputConfig;
|
|
185
|
+
inferenceGeo;
|
|
186
|
+
contextManagement;
|
|
187
|
+
constructor(fields) {
|
|
188
|
+
super(fields);
|
|
189
|
+
this.resetTokenEvents();
|
|
190
|
+
this.setDirectFields(fields);
|
|
191
|
+
this._lc_stream_delay = Math.max(0, fields?._lc_stream_delay ?? DEFAULT_STREAM_DELAY);
|
|
192
|
+
this.outputConfig = fields?.outputConfig;
|
|
193
|
+
this.inferenceGeo = fields?.inferenceGeo;
|
|
194
|
+
this.contextManagement = fields?.contextManagement;
|
|
195
|
+
}
|
|
196
|
+
static lc_name() {
|
|
197
|
+
return "LibreChatAnthropic";
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get the parameters used to invoke the model
|
|
201
|
+
*/
|
|
202
|
+
invocationParams(options) {
|
|
203
|
+
const tool_choice = handleToolChoice(options?.tool_choice);
|
|
204
|
+
const callOptions = options;
|
|
205
|
+
const mergedOutputConfig = (() => {
|
|
206
|
+
const base = {
|
|
207
|
+
...this.outputConfig,
|
|
208
|
+
...callOptions?.outputConfig
|
|
209
|
+
};
|
|
210
|
+
if (callOptions?.outputFormat && !base.format) base.format = callOptions.outputFormat;
|
|
211
|
+
return Object.keys(base).length > 0 ? base : void 0;
|
|
212
|
+
})();
|
|
213
|
+
const inferenceGeo = callOptions?.inferenceGeo ?? this.inferenceGeo;
|
|
214
|
+
const contextManagement = this.contextManagement;
|
|
215
|
+
const toolBetas = getToolBetas(options?.tools);
|
|
216
|
+
const compactionBetas = getCompactionBetas(contextManagement);
|
|
217
|
+
const taskBudgetBetas = getTaskBudgetBetas(this.model, mergedOutputConfig);
|
|
218
|
+
const sharedParams = {
|
|
219
|
+
tools: this.formatStructuredToolToAnthropic(options?.tools),
|
|
220
|
+
tool_choice,
|
|
221
|
+
thinking: this.thinking,
|
|
222
|
+
context_management: contextManagement,
|
|
223
|
+
...this.invocationKwargs,
|
|
224
|
+
container: callOptions?.container,
|
|
225
|
+
betas: combineBetas(this.betas, callOptions?.betas, toolBetas, compactionBetas, taskBudgetBetas),
|
|
226
|
+
output_config: mergedOutputConfig,
|
|
227
|
+
inference_geo: inferenceGeo,
|
|
228
|
+
mcp_servers: callOptions?.mcp_servers
|
|
229
|
+
};
|
|
230
|
+
validateInvocationParamCompatibility({
|
|
231
|
+
model: this.model,
|
|
232
|
+
thinking: this.thinking,
|
|
233
|
+
topK: this.top_k,
|
|
234
|
+
topP: this.topP,
|
|
235
|
+
temperature: this.temperature
|
|
236
|
+
});
|
|
237
|
+
return {
|
|
238
|
+
model: this.model,
|
|
239
|
+
stop_sequences: options?.stop ?? this.stopSequences,
|
|
240
|
+
stream: this.streaming,
|
|
241
|
+
max_tokens: this.maxTokens,
|
|
242
|
+
...getSamplingParams({
|
|
243
|
+
model: this.model,
|
|
244
|
+
thinking: this.thinking,
|
|
245
|
+
topK: this.top_k,
|
|
246
|
+
topP: this.topP,
|
|
247
|
+
temperature: this.temperature
|
|
248
|
+
}),
|
|
249
|
+
...sharedParams
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
resetTokenEvents() {
|
|
253
|
+
this.tools_in_params = void 0;
|
|
254
|
+
}
|
|
255
|
+
setDirectFields(fields) {
|
|
256
|
+
this.temperature = fields?.temperature ?? void 0;
|
|
257
|
+
this.topP = fields?.topP ?? void 0;
|
|
258
|
+
this.top_k = fields?.topK;
|
|
259
|
+
if (this.temperature === -1 || this.temperature === 1) this.temperature = void 0;
|
|
260
|
+
if (this.topP === -1) this.topP = void 0;
|
|
261
|
+
if (this.top_k === -1) this.top_k = void 0;
|
|
262
|
+
}
|
|
263
|
+
createGenerationChunk({ token, chunk, shouldStreamUsage }) {
|
|
264
|
+
const usage_metadata = shouldStreamUsage ? chunk.usage_metadata : void 0;
|
|
265
|
+
return new ChatGenerationChunk({
|
|
266
|
+
message: new AIMessageChunk({
|
|
267
|
+
content: chunk.content,
|
|
268
|
+
additional_kwargs: chunk.additional_kwargs,
|
|
269
|
+
tool_call_chunks: chunk.tool_call_chunks,
|
|
270
|
+
response_metadata: chunk.response_metadata,
|
|
271
|
+
usage_metadata,
|
|
272
|
+
id: chunk.id
|
|
273
|
+
}),
|
|
274
|
+
text: token ?? ""
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
async createStreamWithRetry(request, options) {
|
|
278
|
+
return super.createStreamWithRetry(stripUnsupportedAssistantPrefill(request), options);
|
|
279
|
+
}
|
|
280
|
+
async completionWithRetry(request, options) {
|
|
281
|
+
return super.completionWithRetry(stripUnsupportedAssistantPrefill(request), options);
|
|
282
|
+
}
|
|
283
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
284
|
+
this.resetTokenEvents();
|
|
285
|
+
const params = this.invocationParams(options);
|
|
286
|
+
const formattedMessages = _convertMessagesToAnthropicPayload(messages);
|
|
287
|
+
const payload = stripUnsupportedAssistantPrefill({
|
|
288
|
+
...params,
|
|
289
|
+
...formattedMessages,
|
|
290
|
+
stream: true
|
|
291
|
+
});
|
|
292
|
+
const coerceContentToString = !_toolsInParams(payload) && !_documentsInParams(payload) && !_thinkingInParams(payload) && !_compactionInParams(payload);
|
|
293
|
+
const stream = await this.createStreamWithRetry(payload, {
|
|
294
|
+
headers: options.headers,
|
|
295
|
+
signal: options.signal
|
|
296
|
+
});
|
|
297
|
+
const shouldStreamUsage = options.streamUsage ?? this.streamUsage;
|
|
298
|
+
let messageDeltaOutputTokens = 0;
|
|
299
|
+
const queuedChunks = [];
|
|
300
|
+
const producerState = { done: false };
|
|
301
|
+
let queuedChunkIndex = 0;
|
|
302
|
+
let bufferedTextLength = 0;
|
|
303
|
+
let consumerClosed = false;
|
|
304
|
+
let notifyConsumer;
|
|
305
|
+
let notifyProducer;
|
|
306
|
+
const notifyConsumerForChunk = () => {
|
|
307
|
+
notifyConsumer?.();
|
|
308
|
+
notifyConsumer = void 0;
|
|
309
|
+
};
|
|
310
|
+
const notifyProducerForSpace = () => {
|
|
311
|
+
notifyProducer?.();
|
|
312
|
+
notifyProducer = void 0;
|
|
313
|
+
};
|
|
314
|
+
const hasQueuedChunks = () => queuedChunkIndex < queuedChunks.length;
|
|
315
|
+
const getQueuedChunkCount = () => queuedChunks.length - queuedChunkIndex;
|
|
316
|
+
const isQueueAtCapacity = () => getQueuedChunkCount() >= MAX_STREAM_QUEUE_CHUNKS || bufferedTextLength >= MAX_STREAM_QUEUE_TEXT_CHARS;
|
|
317
|
+
const waitForNextChunk = async () => {
|
|
318
|
+
if (hasQueuedChunks() || producerState.done || producerState.error != null) return;
|
|
319
|
+
await new Promise((resolve) => {
|
|
320
|
+
notifyConsumer = resolve;
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
const waitForQueueSpace = async () => {
|
|
324
|
+
while (isQueueAtCapacity() && !consumerClosed && !isSignalAborted(options.signal)) await new Promise((resolve) => {
|
|
325
|
+
const signal = options.signal;
|
|
326
|
+
const onAbort = () => {
|
|
327
|
+
signal?.removeEventListener("abort", onAbort);
|
|
328
|
+
resolve();
|
|
329
|
+
};
|
|
330
|
+
const onSpace = () => {
|
|
331
|
+
signal?.removeEventListener("abort", onAbort);
|
|
332
|
+
resolve();
|
|
333
|
+
};
|
|
334
|
+
notifyProducer = onSpace;
|
|
335
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
336
|
+
if (isSignalAborted(signal)) onAbort();
|
|
337
|
+
});
|
|
338
|
+
};
|
|
339
|
+
const dequeue = () => {
|
|
340
|
+
if (!hasQueuedChunks()) return;
|
|
341
|
+
const queuedChunk = queuedChunks[queuedChunkIndex];
|
|
342
|
+
queuedChunkIndex++;
|
|
343
|
+
if (queuedChunkIndex > 128 && queuedChunkIndex * 2 >= queuedChunks.length) {
|
|
344
|
+
queuedChunks.splice(0, queuedChunkIndex);
|
|
345
|
+
queuedChunkIndex = 0;
|
|
346
|
+
}
|
|
347
|
+
return queuedChunk;
|
|
348
|
+
};
|
|
349
|
+
const enqueue = async (queuedChunk) => {
|
|
350
|
+
await waitForQueueSpace();
|
|
351
|
+
if (consumerClosed || isSignalAborted(options.signal)) {
|
|
352
|
+
stream.controller.abort();
|
|
353
|
+
throw new Error("AbortError: User aborted the request.");
|
|
354
|
+
}
|
|
355
|
+
queuedChunks.push(queuedChunk);
|
|
356
|
+
if (queuedChunk.smooth) bufferedTextLength += queuedChunk.textLength;
|
|
357
|
+
notifyConsumerForChunk();
|
|
358
|
+
};
|
|
359
|
+
const enqueueChunk = async ({ token, chunk, smooth }) => {
|
|
360
|
+
await enqueue({
|
|
361
|
+
token,
|
|
362
|
+
smooth,
|
|
363
|
+
textLength: smooth ? token.length : 0,
|
|
364
|
+
chunk: this.createGenerationChunk({
|
|
365
|
+
token,
|
|
366
|
+
chunk,
|
|
367
|
+
shouldStreamUsage
|
|
368
|
+
})
|
|
369
|
+
});
|
|
370
|
+
};
|
|
371
|
+
const enqueueTextChunks = (token, tokenType, chunk) => {
|
|
372
|
+
if (token === "") return Promise.resolve();
|
|
373
|
+
if (this._lc_stream_delay <= 0) return enqueueChunk({
|
|
374
|
+
token,
|
|
375
|
+
chunk,
|
|
376
|
+
smooth: false
|
|
377
|
+
});
|
|
378
|
+
const tokenChunks = splitStreamToken(token);
|
|
379
|
+
if (tokenChunks.length <= 1) return enqueueChunk({
|
|
380
|
+
token,
|
|
381
|
+
chunk,
|
|
382
|
+
smooth: true
|
|
383
|
+
});
|
|
384
|
+
let emittedUsage = false;
|
|
385
|
+
return tokenChunks.reduce(async (previous, currentToken) => {
|
|
386
|
+
await previous;
|
|
387
|
+
const newChunk = cloneChunk(currentToken, tokenType, chunk);
|
|
388
|
+
await enqueueChunk({
|
|
389
|
+
token: currentToken,
|
|
390
|
+
chunk: emittedUsage && newChunk.usage_metadata != null ? new AIMessageChunk(Object.assign({}, newChunk, { usage_metadata: void 0 })) : newChunk,
|
|
391
|
+
smooth: true
|
|
392
|
+
});
|
|
393
|
+
if (newChunk.usage_metadata != null && !emittedUsage) emittedUsage = true;
|
|
394
|
+
}, Promise.resolve());
|
|
395
|
+
};
|
|
396
|
+
const producer = (async () => {
|
|
397
|
+
try {
|
|
398
|
+
for await (const data of stream) {
|
|
399
|
+
if (isSignalAborted(options.signal)) {
|
|
400
|
+
stream.controller.abort();
|
|
401
|
+
throw new Error("AbortError: User aborted the request.");
|
|
402
|
+
}
|
|
403
|
+
const result = _makeMessageChunkFromAnthropicEvent(data, {
|
|
404
|
+
streamUsage: shouldStreamUsage,
|
|
405
|
+
coerceContentToString
|
|
406
|
+
});
|
|
407
|
+
if (!result) continue;
|
|
408
|
+
let { chunk } = result;
|
|
409
|
+
if (data.type === "message_delta") {
|
|
410
|
+
const incremental = withIncrementalMessageDeltaUsage(chunk, messageDeltaOutputTokens);
|
|
411
|
+
chunk = incremental.chunk;
|
|
412
|
+
messageDeltaOutputTokens = incremental.outputTokens;
|
|
413
|
+
}
|
|
414
|
+
const [token = "", tokenType] = extractToken(chunk);
|
|
415
|
+
if (!tokenType || tokenType === "input" || token === "" && (chunk.usage_metadata != null || chunk.id != null)) {
|
|
416
|
+
await enqueueChunk({
|
|
417
|
+
token,
|
|
418
|
+
chunk,
|
|
419
|
+
smooth: false
|
|
420
|
+
});
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
await enqueueTextChunks(token, tokenType, chunk);
|
|
424
|
+
}
|
|
425
|
+
} catch (error) {
|
|
426
|
+
producerState.error = error;
|
|
427
|
+
} finally {
|
|
428
|
+
producerState.done = true;
|
|
429
|
+
notifyConsumerForChunk();
|
|
430
|
+
}
|
|
431
|
+
})();
|
|
432
|
+
let hasEmittedText = false;
|
|
433
|
+
let lastVisibleTextAt;
|
|
434
|
+
let keepStreaming = true;
|
|
435
|
+
try {
|
|
436
|
+
while (keepStreaming) {
|
|
437
|
+
if (isSignalAborted(options.signal)) {
|
|
438
|
+
stream.controller.abort();
|
|
439
|
+
throw new Error("AbortError: User aborted the request.");
|
|
440
|
+
}
|
|
441
|
+
await waitForNextChunk();
|
|
442
|
+
const queuedChunk = dequeue();
|
|
443
|
+
if (!queuedChunk) {
|
|
444
|
+
if (producerState.error != null) throw producerState.error;
|
|
445
|
+
if (producerState.done) keepStreaming = false;
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (queuedChunk.smooth) {
|
|
449
|
+
bufferedTextLength = Math.max(0, bufferedTextLength - queuedChunk.textLength);
|
|
450
|
+
notifyProducerForSpace();
|
|
451
|
+
await waitForStreamDelay(getCadencedStreamDelay({
|
|
452
|
+
targetDelay: hasEmittedText ? this._lc_stream_delay : 0,
|
|
453
|
+
lastVisibleTextAt,
|
|
454
|
+
now: Date.now()
|
|
455
|
+
}), options.signal);
|
|
456
|
+
if (isSignalAborted(options.signal)) {
|
|
457
|
+
stream.controller.abort();
|
|
458
|
+
throw new Error("AbortError: User aborted the request.");
|
|
459
|
+
}
|
|
460
|
+
hasEmittedText = true;
|
|
461
|
+
lastVisibleTextAt = Date.now();
|
|
462
|
+
} else notifyProducerForSpace();
|
|
463
|
+
yield queuedChunk.chunk;
|
|
464
|
+
await runManager?.handleLLMNewToken(queuedChunk.token, void 0, void 0, void 0, void 0, { chunk: queuedChunk.chunk });
|
|
465
|
+
}
|
|
466
|
+
} finally {
|
|
467
|
+
consumerClosed = true;
|
|
468
|
+
if (!producerState.done) {
|
|
469
|
+
stream.controller.abort();
|
|
470
|
+
notifyProducerForSpace();
|
|
471
|
+
}
|
|
472
|
+
await producer;
|
|
473
|
+
this.resetTokenEvents();
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
//#endregion
|
|
478
|
+
export { CustomAnthropic };
|
|
605
479
|
|
|
606
|
-
|
|
607
|
-
//# sourceMappingURL=index.mjs.map
|
|
480
|
+
//# sourceMappingURL=index.mjs.map
|