@librechat/agents 3.2.63 → 3.2.65

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.
Files changed (67) hide show
  1. package/dist/cjs/graphs/Graph.cjs +3 -0
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/llm/anthropic/index.cjs +73 -9
  4. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  5. package/dist/cjs/llm/anthropic/types.cjs.map +1 -1
  6. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +41 -9
  7. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  8. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +3 -1
  9. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
  10. package/dist/cjs/llm/anthropic/utils/stream_events.cjs +337 -0
  11. package/dist/cjs/llm/anthropic/utils/stream_events.cjs.map +1 -0
  12. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +82 -34
  13. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
  14. package/dist/cjs/tools/search/crw-scraper.cjs +165 -0
  15. package/dist/cjs/tools/search/crw-scraper.cjs.map +1 -0
  16. package/dist/cjs/tools/search/crw-search.cjs +105 -0
  17. package/dist/cjs/tools/search/crw-search.cjs.map +1 -0
  18. package/dist/cjs/tools/search/search.cjs +4 -2
  19. package/dist/cjs/tools/search/search.cjs.map +1 -1
  20. package/dist/cjs/tools/search/tool.cjs +15 -3
  21. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  22. package/dist/esm/graphs/Graph.mjs +3 -0
  23. package/dist/esm/graphs/Graph.mjs.map +1 -1
  24. package/dist/esm/llm/anthropic/index.mjs +73 -9
  25. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  26. package/dist/esm/llm/anthropic/types.mjs.map +1 -1
  27. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +41 -9
  28. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  29. package/dist/esm/llm/anthropic/utils/message_outputs.mjs +3 -2
  30. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
  31. package/dist/esm/llm/anthropic/utils/stream_events.mjs +337 -0
  32. package/dist/esm/llm/anthropic/utils/stream_events.mjs.map +1 -0
  33. package/dist/esm/llm/bedrock/utils/message_inputs.mjs +82 -34
  34. package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
  35. package/dist/esm/tools/search/crw-scraper.mjs +163 -0
  36. package/dist/esm/tools/search/crw-scraper.mjs.map +1 -0
  37. package/dist/esm/tools/search/crw-search.mjs +103 -0
  38. package/dist/esm/tools/search/crw-search.mjs.map +1 -0
  39. package/dist/esm/tools/search/search.mjs +4 -2
  40. package/dist/esm/tools/search/search.mjs.map +1 -1
  41. package/dist/esm/tools/search/tool.mjs +15 -3
  42. package/dist/esm/tools/search/tool.mjs.map +1 -1
  43. package/dist/types/llm/anthropic/index.d.ts +2 -0
  44. package/dist/types/llm/anthropic/types.d.ts +2 -0
  45. package/dist/types/llm/anthropic/utils/message_outputs.d.ts +1 -2
  46. package/dist/types/llm/anthropic/utils/stream_events.d.ts +25 -0
  47. package/dist/types/tools/search/crw-scraper.d.ts +41 -0
  48. package/dist/types/tools/search/crw-search.d.ts +4 -0
  49. package/dist/types/tools/search/types.d.ts +88 -3
  50. package/package.json +1 -1
  51. package/src/graphs/Graph.ts +15 -0
  52. package/src/llm/anthropic/index.ts +134 -10
  53. package/src/llm/anthropic/inherited-content-utils.spec.ts +10 -5
  54. package/src/llm/anthropic/inherited-stream-events.spec.ts +513 -9
  55. package/src/llm/anthropic/llm.spec.ts +100 -16
  56. package/src/llm/anthropic/types.ts +3 -0
  57. package/src/llm/anthropic/utils/message_inputs.ts +60 -3
  58. package/src/llm/anthropic/utils/message_outputs.ts +10 -2
  59. package/src/llm/anthropic/utils/stream_events.ts +471 -0
  60. package/src/llm/bedrock/utils/message_inputs.test.ts +276 -1
  61. package/src/llm/bedrock/utils/message_inputs.ts +121 -73
  62. package/src/tools/search/crw-scraper.ts +244 -0
  63. package/src/tools/search/crw-search.ts +167 -0
  64. package/src/tools/search/crw.test.ts +836 -0
  65. package/src/tools/search/search.ts +7 -1
  66. package/src/tools/search/tool.ts +23 -3
  67. package/src/tools/search/types.ts +103 -3
@@ -1,5 +1,6 @@
1
1
  import { _convertMessagesToAnthropicPayload, stripUnsupportedAssistantPrefill } from "./utils/message_inputs.mjs";
2
2
  import { _makeMessageChunkFromAnthropicEvent } from "./utils/message_outputs.mjs";
3
+ import { convertAnthropicStream } from "./utils/stream_events.mjs";
3
4
  import { handleToolChoice } from "./utils/tools.mjs";
4
5
  import { AIMessageChunk } from "@langchain/core/messages";
5
6
  import { ChatGenerationChunk } from "@langchain/core/outputs";
@@ -140,6 +141,15 @@ async function waitForStreamDelay(delay, signal) {
140
141
  function isSignalAborted(signal) {
141
142
  return signal?.aborted === true;
142
143
  }
144
+ async function* abortableAnthropicStream(source, signal) {
145
+ for await (const data of source) {
146
+ if (isSignalAborted(signal)) {
147
+ source.controller?.abort();
148
+ return;
149
+ }
150
+ yield data;
151
+ }
152
+ }
143
153
  function extractToken(chunk) {
144
154
  if (typeof chunk.content === "string") return [chunk.content, "string"];
145
155
  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"];
@@ -161,20 +171,47 @@ function cloneChunk(text, tokenType, chunk) {
161
171
  else if (typeof content.type === "string" && content.type.startsWith("thinking")) return new AIMessageChunk(Object.assign({}, chunk, { content: [Object.assign({}, content, { thinking: text })] }));
162
172
  return chunk;
163
173
  }
164
- function withIncrementalMessageDeltaUsage(chunk, previousOutputTokens) {
174
+ function resolveCumulativeUsageValue(current, previous) {
175
+ if (current == null) return {
176
+ cumulative: previous,
177
+ increment: 0
178
+ };
179
+ const cumulative = Math.max(previous, current);
180
+ return {
181
+ cumulative,
182
+ increment: cumulative - previous
183
+ };
184
+ }
185
+ function withIncrementalMessageDeltaUsage(chunk, previousUsage, cumulativeUsage) {
165
186
  const usage = chunk.usage_metadata;
166
187
  if (usage == null) return {
167
188
  chunk,
168
- outputTokens: previousOutputTokens
189
+ usage: previousUsage
190
+ };
191
+ const inputTokens = resolveCumulativeUsageValue(cumulativeUsage.input_tokens, previousUsage.inputTokens);
192
+ const cacheCreationInputTokens = resolveCumulativeUsageValue(cumulativeUsage.cache_creation_input_tokens, previousUsage.cacheCreationInputTokens);
193
+ const cacheReadInputTokens = resolveCumulativeUsageValue(cumulativeUsage.cache_read_input_tokens, previousUsage.cacheReadInputTokens);
194
+ const outputTokens = resolveCumulativeUsageValue(cumulativeUsage.output_tokens, previousUsage.outputTokens);
195
+ const incrementalInputTokens = inputTokens.increment + cacheCreationInputTokens.increment + cacheReadInputTokens.increment;
196
+ const hasCacheUsage = cumulativeUsage.cache_creation_input_tokens != null || cumulativeUsage.cache_read_input_tokens != null;
197
+ const inputTokenDetails = {
198
+ ...cumulativeUsage.cache_creation_input_tokens != null && { cache_creation: cacheCreationInputTokens.increment },
199
+ ...cumulativeUsage.cache_read_input_tokens != null && { cache_read: cacheReadInputTokens.increment }
169
200
  };
170
- const outputTokens = Math.max(0, usage.output_tokens - previousOutputTokens);
171
201
  return {
172
202
  chunk: new AIMessageChunk(Object.assign({}, chunk, { usage_metadata: {
173
203
  ...usage,
174
- output_tokens: outputTokens,
175
- total_tokens: usage.input_tokens + outputTokens
204
+ input_tokens: incrementalInputTokens,
205
+ output_tokens: outputTokens.increment,
206
+ total_tokens: incrementalInputTokens + outputTokens.increment,
207
+ input_token_details: hasCacheUsage ? inputTokenDetails : void 0
176
208
  } })),
177
- outputTokens: usage.output_tokens
209
+ usage: {
210
+ inputTokens: inputTokens.cumulative,
211
+ cacheCreationInputTokens: cacheCreationInputTokens.cumulative,
212
+ cacheReadInputTokens: cacheReadInputTokens.cumulative,
213
+ outputTokens: outputTokens.cumulative
214
+ }
178
215
  };
179
216
  }
180
217
  var CustomAnthropic = class extends ChatAnthropicMessages {
@@ -281,6 +318,21 @@ var CustomAnthropic = class extends ChatAnthropicMessages {
281
318
  async completionWithRetry(request, options) {
282
319
  return super.completionWithRetry(stripUnsupportedAssistantPrefill(request), options);
283
320
  }
321
+ async *_streamChatModelEvents(messages, options, _runManager) {
322
+ const params = this.invocationParams(options);
323
+ const formattedMessages = _convertMessagesToAnthropicPayload(messages);
324
+ const payload = stripUnsupportedAssistantPrefill({
325
+ ...params,
326
+ ...formattedMessages,
327
+ stream: true
328
+ });
329
+ const stream = await this.createStreamWithRetry(payload, {
330
+ headers: options.headers,
331
+ signal: options.signal
332
+ });
333
+ const shouldStreamUsage = options.streamUsage ?? this.streamUsage;
334
+ yield* convertAnthropicStream(abortableAnthropicStream(stream, options.signal), { streamUsage: shouldStreamUsage });
335
+ }
284
336
  async *_streamResponseChunks(messages, options, runManager) {
285
337
  this.resetTokenEvents();
286
338
  const params = this.invocationParams(options);
@@ -296,7 +348,12 @@ var CustomAnthropic = class extends ChatAnthropicMessages {
296
348
  signal: options.signal
297
349
  });
298
350
  const shouldStreamUsage = options.streamUsage ?? this.streamUsage;
299
- let messageDeltaOutputTokens = 0;
351
+ let streamUsage = {
352
+ inputTokens: 0,
353
+ cacheCreationInputTokens: 0,
354
+ cacheReadInputTokens: 0,
355
+ outputTokens: 0
356
+ };
300
357
  const queuedChunks = [];
301
358
  const producerState = { done: false };
302
359
  let queuedChunkIndex = 0;
@@ -407,10 +464,17 @@ var CustomAnthropic = class extends ChatAnthropicMessages {
407
464
  });
408
465
  if (!result) continue;
409
466
  let { chunk } = result;
467
+ if (data.type === "message_start") streamUsage = {
468
+ ...streamUsage,
469
+ inputTokens: data.message.usage.input_tokens,
470
+ outputTokens: data.message.usage.output_tokens,
471
+ cacheCreationInputTokens: data.message.usage.cache_creation_input_tokens ?? 0,
472
+ cacheReadInputTokens: data.message.usage.cache_read_input_tokens ?? 0
473
+ };
410
474
  if (data.type === "message_delta") {
411
- const incremental = withIncrementalMessageDeltaUsage(chunk, messageDeltaOutputTokens);
475
+ const incremental = withIncrementalMessageDeltaUsage(chunk, streamUsage, data.usage);
412
476
  chunk = incremental.chunk;
413
- messageDeltaOutputTokens = incremental.outputTokens;
477
+ streamUsage = incremental.usage;
414
478
  }
415
479
  const [token = "", tokenType] = extractToken(chunk);
416
480
  if (!tokenType || tokenType === "input" || token === "" && (chunk.usage_metadata != null || chunk.id != null)) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/llm/anthropic/index.ts"],"sourcesContent":["import { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatAnthropicMessages } from '@langchain/anthropic';\nimport { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { BaseChatModelParams } from '@langchain/core/language_models/chat_models';\nimport type {\n BaseMessage,\n MessageContentComplex,\n} from '@langchain/core/messages';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { AnthropicInput } from '@langchain/anthropic';\nimport type { Anthropic } from '@anthropic-ai/sdk';\nimport type {\n AnthropicMessageCreateParams,\n AnthropicStreamingMessageCreateParams,\n AnthropicOutputConfig,\n AnthropicBeta,\n ChatAnthropicToolType,\n AnthropicMCPServerURLDefinition,\n AnthropicContextManagementConfigParam,\n AnthropicRequestOptions,\n} from '@/llm/anthropic/types';\nimport {\n _convertMessagesToAnthropicPayload,\n stripUnsupportedAssistantPrefill,\n} from './utils/message_inputs';\nimport { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';\nimport { handleToolChoice } from './utils/tools';\n\nconst DEFAULT_STREAM_DELAY = 25;\nconst MAX_STREAM_QUEUE_CHUNKS = 256;\nconst MAX_STREAM_QUEUE_TEXT_CHARS = 8192;\nconst STREAM_CHUNK_MIN_SIZE = 4;\nconst STREAM_BOUNDARIES = new Set([' ', '.', ',', '!', '?', ';', ':']);\n\ntype StreamTokenType = 'string' | 'input' | 'content';\n\nconst ANTHROPIC_TOOL_BETAS: Partial<Record<string, AnthropicBeta>> = {\n tool_search_tool_regex_20251119: 'advanced-tool-use-2025-11-20',\n tool_search_tool_bm25_20251119: 'advanced-tool-use-2025-11-20',\n memory_20250818: 'context-management-2025-06-27',\n web_fetch_20250910: 'web-fetch-2025-09-10',\n code_execution_20250825: 'code-execution-2025-08-25',\n computer_20251124: 'computer-use-2025-11-24',\n computer_20250124: 'computer-use-2025-01-24',\n mcp_toolset: 'mcp-client-2025-11-20',\n};\n\nfunction _toolsInParams(\n params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams\n): boolean {\n return !!(params.tools && params.tools.length > 0);\n}\nexport function _documentsInParams(\n params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams\n): boolean {\n for (const message of params.messages) {\n if (typeof message.content === 'string') {\n continue;\n }\n for (const block of message.content) {\n const maybeBlock: unknown = block;\n if (\n typeof maybeBlock === 'object' &&\n maybeBlock !== null &&\n 'type' in maybeBlock &&\n maybeBlock.type === 'document' &&\n 'citations' in maybeBlock &&\n maybeBlock.citations != null &&\n typeof maybeBlock.citations === 'object' &&\n 'enabled' in maybeBlock.citations &&\n maybeBlock.citations.enabled === true\n ) {\n return true;\n }\n }\n }\n return false;\n}\n\nfunction _thinkingInParams(\n params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams\n): boolean {\n return !!(\n params.thinking &&\n (params.thinking.type === 'enabled' || params.thinking.type === 'adaptive')\n );\n}\n\nfunction _compactionInParams(\n params: (\n | AnthropicMessageCreateParams\n | AnthropicStreamingMessageCreateParams\n ) & {\n context_management?: AnthropicContextManagementConfigParam;\n }\n): boolean {\n return (\n params.context_management?.edits?.some(\n (edit) => edit.type === 'compact_20260112'\n ) === true\n );\n}\n\nfunction isThinkingEnabled(thinking: Anthropic.ThinkingConfigParam): boolean {\n return thinking.type === 'enabled' || thinking.type === 'adaptive';\n}\n\nfunction isOpus47Model(model?: string): boolean {\n return /^claude-opus-4-7(?:-|$)/.test(model ?? '');\n}\n\nfunction combineBetas(\n ...betaGroups: (AnthropicBeta[] | undefined)[]\n): AnthropicBeta[] {\n const betas = new Set<AnthropicBeta>();\n for (const betaGroup of betaGroups) {\n for (const beta of betaGroup ?? []) {\n betas.add(beta);\n }\n }\n return [...betas];\n}\n\nfunction getToolBetas(tools?: ChatAnthropicToolType[]): AnthropicBeta[] {\n const betas = new Set<AnthropicBeta>();\n for (const tool of tools ?? []) {\n if (typeof tool !== 'object' || !('type' in tool)) {\n continue;\n }\n const beta = ANTHROPIC_TOOL_BETAS[String(tool.type)];\n if (beta != null) {\n betas.add(beta);\n }\n }\n return [...betas];\n}\n\nfunction getCompactionBetas(\n contextManagement?: AnthropicContextManagementConfigParam\n): AnthropicBeta[] {\n return contextManagement?.edits?.some(\n (edit) => edit.type === 'compact_20260112'\n ) === true\n ? ['compact-2026-01-12']\n : [];\n}\n\nfunction getTaskBudgetBetas(\n model: string,\n outputConfig?: AnthropicOutputConfig\n): AnthropicBeta[] {\n return isOpus47Model(model) &&\n outputConfig != null &&\n 'task_budget' in outputConfig &&\n outputConfig.task_budget != null\n ? ['task-budgets-2026-03-13']\n : [];\n}\n\nfunction isSetSamplingValue(value?: number | null): value is number {\n return value != null && value !== -1;\n}\n\nfunction isNonDefaultTemperature(value?: number): boolean {\n return isSetSamplingValue(value) && value !== 1;\n}\n\nfunction validateInvocationParamCompatibility({\n model,\n thinking,\n topK,\n topP,\n temperature,\n}: {\n model: string;\n thinking: Anthropic.ThinkingConfigParam;\n topK?: number;\n topP?: number | null;\n temperature?: number;\n}): void {\n const opus47 = isOpus47Model(model);\n if (opus47 && thinking.type === 'enabled') {\n throw new Error(\n 'thinking.type=\"enabled\" is not supported for claude-opus-4-7; use thinking.type=\"adaptive\" instead'\n );\n }\n if (opus47 && 'budget_tokens' in thinking) {\n throw new Error(\n 'thinking.budget_tokens is not supported for claude-opus-4-7; use outputConfig.effort instead'\n );\n }\n if (opus47) {\n if (isSetSamplingValue(topK)) {\n throw new Error(\n 'topK is not supported for claude-opus-4-7; omit topK/topP/temperature or use model prompting instead'\n );\n }\n if (isSetSamplingValue(topP) && topP !== 1) {\n throw new Error(\n 'topP is not supported for claude-opus-4-7 when set to non-default values'\n );\n }\n if (isNonDefaultTemperature(temperature)) {\n throw new Error(\n 'temperature is not supported for claude-opus-4-7 when set to non-default values'\n );\n }\n }\n if (!isThinkingEnabled(thinking)) {\n return;\n }\n if (isSetSamplingValue(topK)) {\n throw new Error('topK is not supported when thinking is enabled');\n }\n if (isSetSamplingValue(topP)) {\n throw new Error('topP is not supported when thinking is enabled');\n }\n if (isNonDefaultTemperature(temperature)) {\n throw new Error('temperature is not supported when thinking is enabled');\n }\n}\n\nfunction getSamplingParams({\n model,\n thinking,\n topK,\n topP,\n temperature,\n}: {\n model: string;\n thinking: Anthropic.ThinkingConfigParam;\n topK?: number;\n topP?: number | null;\n temperature?: number;\n}): {\n temperature?: number;\n top_k?: number;\n top_p?: number;\n} {\n if (isThinkingEnabled(thinking) || isOpus47Model(model)) {\n return {};\n }\n return {\n ...(isSetSamplingValue(temperature) ? { temperature } : {}),\n ...(isSetSamplingValue(topK) ? { top_k: topK } : {}),\n ...(isSetSamplingValue(topP) ? { top_p: topP } : {}),\n };\n}\n\nfunction findStreamChunkBoundary(text: string, minSize: number): number {\n if (minSize >= text.length) {\n return text.length;\n }\n\n for (let position = minSize; position < text.length; position++) {\n if (STREAM_BOUNDARIES.has(text[position])) {\n return position + 1;\n }\n }\n\n return text.length;\n}\n\nfunction splitStreamToken(text: string): string[] {\n const chunks: string[] = [];\n let currentIndex = 0;\n\n while (currentIndex < text.length) {\n const remainingText = text.slice(currentIndex);\n const chunkSize = findStreamChunkBoundary(\n remainingText,\n STREAM_CHUNK_MIN_SIZE\n );\n chunks.push(text.slice(currentIndex, currentIndex + chunkSize));\n currentIndex += chunkSize;\n }\n\n return chunks;\n}\n\nfunction getCadencedStreamDelay({\n targetDelay,\n lastVisibleTextAt,\n now,\n}: {\n targetDelay: number;\n lastVisibleTextAt?: number;\n now: number;\n}): number {\n if (targetDelay <= 0 || lastVisibleTextAt == null) {\n return 0;\n }\n return Math.max(0, targetDelay - (now - lastVisibleTextAt));\n}\n\nasync function waitForStreamDelay(\n delay: number,\n signal?: AbortSignal\n): Promise<void> {\n if (delay <= 0 || isSignalAborted(signal)) {\n return;\n }\n await new Promise<void>((resolve) => {\n const timeoutRef: { current?: ReturnType<typeof setTimeout> } = {};\n const onAbort = (): void => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n signal?.removeEventListener('abort', onAbort);\n resolve();\n };\n timeoutRef.current = setTimeout(() => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n }, delay);\n signal?.addEventListener('abort', onAbort, { once: true });\n if (isSignalAborted(signal)) {\n onAbort();\n }\n });\n}\n\nfunction isSignalAborted(signal?: AbortSignal): boolean {\n return signal?.aborted === true;\n}\n\nfunction extractToken(\n chunk: AIMessageChunk\n): [string, StreamTokenType] | [undefined] {\n if (typeof chunk.content === 'string') {\n return [chunk.content, 'string'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'input' in chunk.content[0]\n ) {\n return typeof chunk.content[0].input === 'string'\n ? [chunk.content[0].input, 'input']\n : [JSON.stringify(chunk.content[0].input), 'input'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'text' in chunk.content[0]\n ) {\n const text = chunk.content[0].text;\n return typeof text === 'string' ? [text, 'content'] : [undefined];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'thinking' in chunk.content[0]\n ) {\n const thinking = chunk.content[0].thinking;\n return typeof thinking === 'string' ? [thinking, 'content'] : [undefined];\n }\n return [undefined];\n}\n\nfunction cloneChunk(\n text: string,\n tokenType: StreamTokenType,\n chunk: AIMessageChunk\n): AIMessageChunk {\n if (tokenType === 'string') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: text }));\n } else if (tokenType === 'input') {\n return chunk;\n }\n const content = chunk.content[0] as MessageContentComplex;\n if (content.type === 'text') {\n return new AIMessageChunk(\n Object.assign({}, chunk, {\n content: [Object.assign({}, content, { text })],\n })\n );\n } else if (content.type === 'text_delta') {\n return new AIMessageChunk(\n Object.assign({}, chunk, {\n content: [Object.assign({}, content, { text })],\n })\n );\n } else if (\n typeof content.type === 'string' &&\n content.type.startsWith('thinking')\n ) {\n return new AIMessageChunk(\n Object.assign({}, chunk, {\n content: [Object.assign({}, content, { thinking: text })],\n })\n );\n }\n\n return chunk;\n}\n\nfunction withIncrementalMessageDeltaUsage(\n chunk: AIMessageChunk,\n previousOutputTokens: number\n): { chunk: AIMessageChunk; outputTokens: number } {\n const usage = chunk.usage_metadata;\n if (usage == null) {\n return { chunk, outputTokens: previousOutputTokens };\n }\n\n const outputTokens = Math.max(0, usage.output_tokens - previousOutputTokens);\n return {\n chunk: new AIMessageChunk(\n Object.assign({}, chunk, {\n usage_metadata: {\n ...usage,\n output_tokens: outputTokens,\n total_tokens: usage.input_tokens + outputTokens,\n },\n })\n ),\n outputTokens: usage.output_tokens,\n };\n}\n\nexport type CustomAnthropicInput = AnthropicInput & {\n _lc_stream_delay?: number;\n outputConfig?: AnthropicOutputConfig;\n inferenceGeo?: string;\n contextManagement?: AnthropicContextManagementConfigParam;\n} & BaseChatModelParams;\n\nexport type CustomAnthropicCallOptions = {\n outputConfig?: AnthropicOutputConfig;\n outputFormat?: Anthropic.Messages.JSONOutputFormat;\n inferenceGeo?: string;\n betas?: AnthropicBeta[];\n container?: string;\n mcp_servers?: AnthropicMCPServerURLDefinition[];\n};\n\ntype CustomAnthropicInvocationParams = {\n betas?: AnthropicBeta[];\n container?: string;\n context_management?: AnthropicContextManagementConfigParam;\n inference_geo?: string;\n mcp_servers?: AnthropicMCPServerURLDefinition[];\n output_config?: AnthropicOutputConfig;\n};\n\ntype QueuedGenerationChunk = {\n chunk: ChatGenerationChunk;\n token: string;\n smooth: boolean;\n textLength: number;\n};\n\nexport class CustomAnthropic extends ChatAnthropicMessages {\n _lc_stream_delay: number;\n private tools_in_params?: boolean;\n top_k: number | undefined;\n outputConfig?: AnthropicOutputConfig;\n inferenceGeo?: string;\n contextManagement?: AnthropicContextManagementConfigParam;\n constructor(fields?: CustomAnthropicInput) {\n super(fields);\n this.resetTokenEvents();\n this.setDirectFields(fields);\n this._lc_stream_delay = Math.max(\n 0,\n fields?._lc_stream_delay ?? DEFAULT_STREAM_DELAY\n );\n this.outputConfig = fields?.outputConfig;\n this.inferenceGeo = fields?.inferenceGeo;\n this.contextManagement = fields?.contextManagement;\n }\n\n static lc_name(): 'LibreChatAnthropic' {\n return 'LibreChatAnthropic';\n }\n\n /**\n * Get the parameters used to invoke the model\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomAnthropicCallOptions\n ): Omit<\n AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams,\n 'messages'\n > &\n CustomAnthropicInvocationParams {\n const tool_choice:\n | Anthropic.Messages.ToolChoiceAuto\n | Anthropic.Messages.ToolChoiceAny\n | Anthropic.Messages.ToolChoiceTool\n | Anthropic.Messages.ToolChoiceNone\n | undefined = handleToolChoice(options?.tool_choice);\n\n const callOptions = options as CustomAnthropicCallOptions | undefined;\n const mergedOutputConfig: AnthropicOutputConfig | undefined = (():\n | AnthropicOutputConfig\n | undefined => {\n const base = {\n ...this.outputConfig,\n ...callOptions?.outputConfig,\n };\n if (callOptions?.outputFormat && !base.format) {\n base.format = callOptions.outputFormat;\n }\n return Object.keys(base).length > 0 ? base : undefined;\n })();\n\n const inferenceGeo = callOptions?.inferenceGeo ?? this.inferenceGeo;\n\n const contextManagement = this.contextManagement;\n const toolBetas = getToolBetas(options?.tools);\n const compactionBetas = getCompactionBetas(contextManagement);\n const taskBudgetBetas = getTaskBudgetBetas(this.model, mergedOutputConfig);\n const formattedTools = this.formatStructuredToolToAnthropic(\n options?.tools,\n {\n strict: options?.strict,\n }\n );\n\n const sharedParams = {\n tools: formattedTools,\n tool_choice,\n // Match upstream: omit `thinking` unless the user set it, so we don't send\n // `{ type: 'disabled' }` (an unsupported param on some models) by default.\n thinking: this.thinkingExplicitlySet ? this.thinking : undefined,\n context_management: contextManagement,\n ...this.invocationKwargs,\n container: callOptions?.container,\n betas: combineBetas(\n this.betas,\n callOptions?.betas,\n toolBetas,\n compactionBetas,\n taskBudgetBetas\n ),\n output_config: mergedOutputConfig,\n inference_geo: inferenceGeo,\n mcp_servers: callOptions?.mcp_servers,\n // Top-level request cache_control (1.5.x): API auto-advances the cache\n // breakpoint across turns. Additive — independent of our block-level cache.\n cache_control: options?.cache_control,\n };\n validateInvocationParamCompatibility({\n model: this.model,\n thinking: this.thinking,\n topK: this.top_k,\n topP: this.topP,\n temperature: this.temperature,\n });\n\n return {\n model: this.model,\n stop_sequences: options?.stop ?? this.stopSequences,\n stream: this.streaming,\n max_tokens: this.maxTokens,\n ...getSamplingParams({\n model: this.model,\n thinking: this.thinking,\n topK: this.top_k,\n topP: this.topP,\n temperature: this.temperature,\n }),\n ...sharedParams,\n };\n }\n\n resetTokenEvents(): void {\n this.tools_in_params = undefined;\n }\n\n setDirectFields(fields?: CustomAnthropicInput): void {\n this.temperature = fields?.temperature ?? undefined;\n this.topP = fields?.topP ?? undefined;\n this.top_k = fields?.topK;\n if (this.temperature === -1 || this.temperature === 1) {\n this.temperature = undefined;\n }\n if (this.topP === -1) {\n this.topP = undefined;\n }\n if (this.top_k === -1) {\n this.top_k = undefined;\n }\n }\n\n private createGenerationChunk({\n token,\n chunk,\n shouldStreamUsage,\n }: {\n token?: string;\n chunk: AIMessageChunk;\n shouldStreamUsage: boolean;\n }): ChatGenerationChunk {\n const usage_metadata = shouldStreamUsage ? chunk.usage_metadata : undefined;\n return new ChatGenerationChunk({\n message: new AIMessageChunk({\n // Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().\n content: chunk.content,\n additional_kwargs: chunk.additional_kwargs,\n tool_call_chunks: chunk.tool_call_chunks,\n response_metadata: chunk.response_metadata,\n usage_metadata,\n id: chunk.id,\n }),\n text: token ?? '',\n });\n }\n\n protected override async createStreamWithRetry(\n request: AnthropicStreamingMessageCreateParams,\n options?: AnthropicRequestOptions\n ): ReturnType<ChatAnthropicMessages['createStreamWithRetry']> {\n return super.createStreamWithRetry(\n stripUnsupportedAssistantPrefill(request),\n options\n );\n }\n\n protected override async completionWithRetry(\n request: AnthropicMessageCreateParams,\n options: AnthropicRequestOptions\n ): ReturnType<ChatAnthropicMessages['completionWithRetry']> {\n return super.completionWithRetry(\n stripUnsupportedAssistantPrefill(request),\n options\n );\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n this.resetTokenEvents();\n const params = this.invocationParams(options);\n const formattedMessages = _convertMessagesToAnthropicPayload(messages);\n const payload = stripUnsupportedAssistantPrefill({\n ...params,\n ...formattedMessages,\n stream: true,\n } as const);\n const coerceContentToString =\n !_toolsInParams(payload) &&\n !_documentsInParams(payload) &&\n !_thinkingInParams(payload) &&\n !_compactionInParams(payload);\n\n const stream = await this.createStreamWithRetry(payload, {\n headers: options.headers,\n signal: options.signal,\n });\n\n const shouldStreamUsage = options.streamUsage ?? this.streamUsage;\n let messageDeltaOutputTokens = 0;\n const queuedChunks: QueuedGenerationChunk[] = [];\n const producerState: {\n done: boolean;\n error?: unknown;\n } = { done: false };\n let queuedChunkIndex = 0;\n let bufferedTextLength = 0;\n let consumerClosed = false;\n let notifyConsumer: (() => void) | undefined;\n let notifyProducer: (() => void) | undefined;\n\n const notifyConsumerForChunk = (): void => {\n notifyConsumer?.();\n notifyConsumer = undefined;\n };\n\n const notifyProducerForSpace = (): void => {\n notifyProducer?.();\n notifyProducer = undefined;\n };\n\n const hasQueuedChunks = (): boolean =>\n queuedChunkIndex < queuedChunks.length;\n\n const getQueuedChunkCount = (): number =>\n queuedChunks.length - queuedChunkIndex;\n\n const isQueueAtCapacity = (): boolean =>\n getQueuedChunkCount() >= MAX_STREAM_QUEUE_CHUNKS ||\n bufferedTextLength >= MAX_STREAM_QUEUE_TEXT_CHARS;\n\n const waitForNextChunk = async (): Promise<void> => {\n if (\n hasQueuedChunks() ||\n producerState.done ||\n producerState.error != null\n ) {\n return;\n }\n await new Promise<void>((resolve) => {\n notifyConsumer = resolve;\n });\n };\n\n const waitForQueueSpace = async (): Promise<void> => {\n while (\n isQueueAtCapacity() &&\n !consumerClosed &&\n !isSignalAborted(options.signal)\n ) {\n await new Promise<void>((resolve) => {\n const signal = options.signal;\n const onAbort = (): void => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n };\n const onSpace = (): void => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n };\n notifyProducer = onSpace;\n signal?.addEventListener('abort', onAbort, { once: true });\n if (isSignalAborted(signal)) {\n onAbort();\n }\n });\n }\n };\n\n const dequeue = (): QueuedGenerationChunk | undefined => {\n if (!hasQueuedChunks()) {\n return undefined;\n }\n const queuedChunk = queuedChunks[queuedChunkIndex];\n queuedChunkIndex++;\n if (\n queuedChunkIndex > 128 &&\n queuedChunkIndex * 2 >= queuedChunks.length\n ) {\n queuedChunks.splice(0, queuedChunkIndex);\n queuedChunkIndex = 0;\n }\n return queuedChunk;\n };\n\n const enqueue = async (\n queuedChunk: QueuedGenerationChunk\n ): Promise<void> => {\n await waitForQueueSpace();\n if (consumerClosed || isSignalAborted(options.signal)) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n queuedChunks.push(queuedChunk);\n if (queuedChunk.smooth) {\n bufferedTextLength += queuedChunk.textLength;\n }\n notifyConsumerForChunk();\n };\n\n const enqueueChunk = async ({\n token,\n chunk,\n smooth,\n }: {\n token: string;\n chunk: AIMessageChunk;\n smooth: boolean;\n }): Promise<void> => {\n await enqueue({\n token,\n smooth,\n textLength: smooth ? token.length : 0,\n chunk: this.createGenerationChunk({\n token,\n chunk,\n shouldStreamUsage,\n }),\n });\n };\n\n const enqueueTextChunks = (\n token: string,\n tokenType: StreamTokenType,\n chunk: AIMessageChunk\n ): Promise<void> => {\n if (token === '') {\n return Promise.resolve();\n }\n if (this._lc_stream_delay <= 0) {\n return enqueueChunk({ token, chunk, smooth: false });\n }\n\n const tokenChunks = splitStreamToken(token);\n if (tokenChunks.length <= 1) {\n return enqueueChunk({ token, chunk, smooth: true });\n }\n\n let emittedUsage = false;\n return tokenChunks.reduce(async (previous, currentToken) => {\n await previous;\n const newChunk = cloneChunk(currentToken, tokenType, chunk);\n const chunkForToken =\n emittedUsage && newChunk.usage_metadata != null\n ? new AIMessageChunk(\n Object.assign({}, newChunk, { usage_metadata: undefined })\n )\n : newChunk;\n\n await enqueueChunk({\n token: currentToken,\n chunk: chunkForToken,\n smooth: true,\n });\n\n if (newChunk.usage_metadata != null && !emittedUsage) {\n emittedUsage = true;\n }\n }, Promise.resolve());\n };\n\n const producer = (async (): Promise<void> => {\n try {\n for await (const data of stream) {\n if (isSignalAborted(options.signal)) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n\n const result = _makeMessageChunkFromAnthropicEvent(\n data as Anthropic.Beta.Messages.BetaRawMessageStreamEvent,\n {\n streamUsage: shouldStreamUsage,\n coerceContentToString,\n }\n );\n if (!result) {\n continue;\n }\n\n let { chunk } = result;\n if (data.type === 'message_delta') {\n const incremental = withIncrementalMessageDeltaUsage(\n chunk,\n messageDeltaOutputTokens\n );\n chunk = incremental.chunk;\n messageDeltaOutputTokens = incremental.outputTokens;\n }\n\n const [token = '', tokenType] = extractToken(chunk);\n if (\n !tokenType ||\n tokenType === 'input' ||\n (token === '' && (chunk.usage_metadata != null || chunk.id != null))\n ) {\n await enqueueChunk({ token, chunk, smooth: false });\n continue;\n }\n\n await enqueueTextChunks(token, tokenType, chunk);\n }\n } catch (error) {\n producerState.error = error;\n } finally {\n producerState.done = true;\n notifyConsumerForChunk();\n }\n })();\n\n let hasEmittedText = false;\n let lastVisibleTextAt: number | undefined;\n let keepStreaming = true;\n try {\n while (keepStreaming) {\n if (isSignalAborted(options.signal)) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n\n await waitForNextChunk();\n const queuedChunk = dequeue();\n\n if (!queuedChunk) {\n if (producerState.error != null) {\n throw producerState.error;\n }\n if (producerState.done) {\n keepStreaming = false;\n }\n continue;\n }\n\n if (queuedChunk.smooth) {\n bufferedTextLength = Math.max(\n 0,\n bufferedTextLength - queuedChunk.textLength\n );\n notifyProducerForSpace();\n await waitForStreamDelay(\n getCadencedStreamDelay({\n targetDelay: hasEmittedText ? this._lc_stream_delay : 0,\n lastVisibleTextAt,\n now: Date.now(),\n }),\n options.signal\n );\n if (isSignalAborted(options.signal)) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n hasEmittedText = true;\n lastVisibleTextAt = Date.now();\n } else {\n notifyProducerForSpace();\n }\n\n yield queuedChunk.chunk;\n await runManager?.handleLLMNewToken(\n queuedChunk.token,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: queuedChunk.chunk }\n );\n }\n } finally {\n consumerClosed = true;\n if (!producerState.done) {\n stream.controller.abort();\n notifyProducerForSpace();\n }\n await producer;\n this.resetTokenEvents();\n }\n }\n}\n"],"mappings":";;;;;;;AA4BA,MAAM,uBAAuB;AAC7B,MAAM,0BAA0B;AAChC,MAAM,8BAA8B;AACpC,MAAM,wBAAwB;AAC9B,MAAM,oBAAoB,IAAI,IAAI;CAAC;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;AAAG,CAAC;AAIrE,MAAM,uBAA+D;CACnE,iCAAiC;CACjC,gCAAgC;CAChC,iBAAiB;CACjB,oBAAoB;CACpB,yBAAyB;CACzB,mBAAmB;CACnB,mBAAmB;CACnB,aAAa;AACf;AAEA,SAAS,eACP,QACS;CACT,OAAO,CAAC,EAAE,OAAO,SAAS,OAAO,MAAM,SAAS;AAClD;AACA,SAAgB,mBACd,QACS;CACT,KAAK,MAAM,WAAW,OAAO,UAAU;EACrC,IA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ACF;KAEA,MAAM,kBAAkB,OAAO,WAAW,KAAK;IACjD;GACF,SAAS,OAAO;IACd,cAAc,QAAQ;GACxB,UAAU;IACR,cAAc,OAAO;IACrB,uBAAuB;GACzB;EACF,EAAA,CAAG;EAEH,IAAI,iBAAiB;EACrB,IAAI;EACJ,IAAI,gBAAgB;EACpB,IAAI;GACF,OAAO,eAAe;IACpB,IAAI,gBAAgB,QAAQ,MAAM,GAAG;KACnC,OAAO,WAAW,MAAM;KACxB,MAAM,IAAI,MAAM,uCAAuC;IACzD;IAEA,MAAM,iBAAiB;IACvB,MAAM,cAAc,QAAQ;IAE5B,IAAI,CAAC,aAAa;KAChB,IAAI,cAAc,SAAS,MACzB,MAAM,cAAc;KAEtB,IAAI,cAAc,MAChB,gBAAgB;KAElB;IACF;IAEA,IAAI,YAAY,QAAQ;KACtB,qBAAqB,KAAK,IACxB,GACA,qBAAqB,YAAY,UACnC;KACA,uBAAuB;KACvB,MAAM,mBACJ,uBAAuB;MACrB,aAAa,iBAAiB,KAAK,mBAAmB;MACtD;MACA,KAAK,KAAK,IAAI;KAChB,CAAC,GACD,QAAQ,MACV;KACA,IAAI,gBAAgB,QAAQ,MAAM,GAAG;MACnC,OAAO,WAAW,MAAM;MACxB,MAAM,IAAI,MAAM,uCAAuC;KACzD;KACA,iBAAiB;KACjB,oBAAoB,KAAK,IAAI;IAC/B,OACE,uBAAuB;IAGzB,MAAM,YAAY;IAClB,MAAM,YAAY,kBAChB,YAAY,OACZ,KAAA,GACA,KAAA,GACA,KAAA,GACA,KAAA,GACA,EAAE,OAAO,YAAY,MAAM,CAC7B;GACF;EACF,UAAU;GACR,iBAAiB;GACjB,IAAI,CAAC,cAAc,MAAM;IACvB,OAAO,WAAW,MAAM;IACxB,uBAAuB;GACzB;GACA,MAAM;GACN,KAAK,iBAAiB;EACxB;CACF;AACF"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/llm/anthropic/index.ts"],"sourcesContent":["import { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatAnthropicMessages } from '@langchain/anthropic';\nimport { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { BaseChatModelParams } from '@langchain/core/language_models/chat_models';\nimport type {\n BaseMessage,\n MessageContentComplex,\n} from '@langchain/core/messages';\nimport type { ChatModelStreamEvent } from '@langchain/core/language_models/event';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { AnthropicInput } from '@langchain/anthropic';\nimport type { Anthropic } from '@anthropic-ai/sdk';\nimport type {\n AnthropicMessageCreateParams,\n AnthropicStreamingMessageCreateParams,\n AnthropicOutputConfig,\n AnthropicBeta,\n ChatAnthropicToolType,\n AnthropicMCPServerURLDefinition,\n AnthropicContextManagementConfigParam,\n AnthropicRequestOptions,\n AnthropicMessageStreamEvent,\n} from '@/llm/anthropic/types';\nimport type { AnthropicUsageData } from './utils/message_outputs';\nimport {\n _convertMessagesToAnthropicPayload,\n stripUnsupportedAssistantPrefill,\n} from './utils/message_inputs';\nimport { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';\nimport { convertAnthropicStream } from './utils/stream_events';\nimport { handleToolChoice } from './utils/tools';\n\nconst DEFAULT_STREAM_DELAY = 25;\nconst MAX_STREAM_QUEUE_CHUNKS = 256;\nconst MAX_STREAM_QUEUE_TEXT_CHARS = 8192;\nconst STREAM_CHUNK_MIN_SIZE = 4;\nconst STREAM_BOUNDARIES = new Set([' ', '.', ',', '!', '?', ';', ':']);\n\ntype StreamTokenType = 'string' | 'input' | 'content';\n\ninterface AnthropicStreamUsage {\n inputTokens: number;\n cacheCreationInputTokens: number;\n cacheReadInputTokens: number;\n outputTokens: number;\n}\n\ninterface CumulativeUsageValue {\n cumulative: number;\n increment: number;\n}\n\ninterface AnthropicEventStream\n extends AsyncIterable<AnthropicMessageStreamEvent> {\n controller?: { abort: () => void };\n}\n\nconst ANTHROPIC_TOOL_BETAS: Partial<Record<string, AnthropicBeta>> = {\n tool_search_tool_regex_20251119: 'advanced-tool-use-2025-11-20',\n tool_search_tool_bm25_20251119: 'advanced-tool-use-2025-11-20',\n memory_20250818: 'context-management-2025-06-27',\n web_fetch_20250910: 'web-fetch-2025-09-10',\n code_execution_20250825: 'code-execution-2025-08-25',\n computer_20251124: 'computer-use-2025-11-24',\n computer_20250124: 'computer-use-2025-01-24',\n mcp_toolset: 'mcp-client-2025-11-20',\n};\n\nfunction _toolsInParams(\n params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams\n): boolean {\n return !!(params.tools && params.tools.length > 0);\n}\nexport function _documentsInParams(\n params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams\n): boolean {\n for (const message of params.messages) {\n if (typeof message.content === 'string') {\n continue;\n }\n for (const block of message.content) {\n const maybeBlock: unknown = block;\n if (\n typeof maybeBlock === 'object' &&\n maybeBlock !== null &&\n 'type' in maybeBlock &&\n maybeBlock.type === 'document' &&\n 'citations' in maybeBlock &&\n maybeBlock.citations != null &&\n typeof maybeBlock.citations === 'object' &&\n 'enabled' in maybeBlock.citations &&\n maybeBlock.citations.enabled === true\n ) {\n return true;\n }\n }\n }\n return false;\n}\n\nfunction _thinkingInParams(\n params: AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams\n): boolean {\n return !!(\n params.thinking &&\n (params.thinking.type === 'enabled' || params.thinking.type === 'adaptive')\n );\n}\n\nfunction _compactionInParams(\n params: (\n | AnthropicMessageCreateParams\n | AnthropicStreamingMessageCreateParams\n ) & {\n context_management?: AnthropicContextManagementConfigParam;\n }\n): boolean {\n return (\n params.context_management?.edits?.some(\n (edit) => edit.type === 'compact_20260112'\n ) === true\n );\n}\n\nfunction isThinkingEnabled(thinking: Anthropic.ThinkingConfigParam): boolean {\n return thinking.type === 'enabled' || thinking.type === 'adaptive';\n}\n\nfunction isOpus47Model(model?: string): boolean {\n return /^claude-opus-4-7(?:-|$)/.test(model ?? '');\n}\n\nfunction combineBetas(\n ...betaGroups: (AnthropicBeta[] | undefined)[]\n): AnthropicBeta[] {\n const betas = new Set<AnthropicBeta>();\n for (const betaGroup of betaGroups) {\n for (const beta of betaGroup ?? []) {\n betas.add(beta);\n }\n }\n return [...betas];\n}\n\nfunction getToolBetas(tools?: ChatAnthropicToolType[]): AnthropicBeta[] {\n const betas = new Set<AnthropicBeta>();\n for (const tool of tools ?? []) {\n if (typeof tool !== 'object' || !('type' in tool)) {\n continue;\n }\n const beta = ANTHROPIC_TOOL_BETAS[String(tool.type)];\n if (beta != null) {\n betas.add(beta);\n }\n }\n return [...betas];\n}\n\nfunction getCompactionBetas(\n contextManagement?: AnthropicContextManagementConfigParam\n): AnthropicBeta[] {\n return contextManagement?.edits?.some(\n (edit) => edit.type === 'compact_20260112'\n ) === true\n ? ['compact-2026-01-12']\n : [];\n}\n\nfunction getTaskBudgetBetas(\n model: string,\n outputConfig?: AnthropicOutputConfig\n): AnthropicBeta[] {\n return isOpus47Model(model) &&\n outputConfig != null &&\n 'task_budget' in outputConfig &&\n outputConfig.task_budget != null\n ? ['task-budgets-2026-03-13']\n : [];\n}\n\nfunction isSetSamplingValue(value?: number | null): value is number {\n return value != null && value !== -1;\n}\n\nfunction isNonDefaultTemperature(value?: number): boolean {\n return isSetSamplingValue(value) && value !== 1;\n}\n\nfunction validateInvocationParamCompatibility({\n model,\n thinking,\n topK,\n topP,\n temperature,\n}: {\n model: string;\n thinking: Anthropic.ThinkingConfigParam;\n topK?: number;\n topP?: number | null;\n temperature?: number;\n}): void {\n const opus47 = isOpus47Model(model);\n if (opus47 && thinking.type === 'enabled') {\n throw new Error(\n 'thinking.type=\"enabled\" is not supported for claude-opus-4-7; use thinking.type=\"adaptive\" instead'\n );\n }\n if (opus47 && 'budget_tokens' in thinking) {\n throw new Error(\n 'thinking.budget_tokens is not supported for claude-opus-4-7; use outputConfig.effort instead'\n );\n }\n if (opus47) {\n if (isSetSamplingValue(topK)) {\n throw new Error(\n 'topK is not supported for claude-opus-4-7; omit topK/topP/temperature or use model prompting instead'\n );\n }\n if (isSetSamplingValue(topP) && topP !== 1) {\n throw new Error(\n 'topP is not supported for claude-opus-4-7 when set to non-default values'\n );\n }\n if (isNonDefaultTemperature(temperature)) {\n throw new Error(\n 'temperature is not supported for claude-opus-4-7 when set to non-default values'\n );\n }\n }\n if (!isThinkingEnabled(thinking)) {\n return;\n }\n if (isSetSamplingValue(topK)) {\n throw new Error('topK is not supported when thinking is enabled');\n }\n if (isSetSamplingValue(topP)) {\n throw new Error('topP is not supported when thinking is enabled');\n }\n if (isNonDefaultTemperature(temperature)) {\n throw new Error('temperature is not supported when thinking is enabled');\n }\n}\n\nfunction getSamplingParams({\n model,\n thinking,\n topK,\n topP,\n temperature,\n}: {\n model: string;\n thinking: Anthropic.ThinkingConfigParam;\n topK?: number;\n topP?: number | null;\n temperature?: number;\n}): {\n temperature?: number;\n top_k?: number;\n top_p?: number;\n} {\n if (isThinkingEnabled(thinking) || isOpus47Model(model)) {\n return {};\n }\n return {\n ...(isSetSamplingValue(temperature) ? { temperature } : {}),\n ...(isSetSamplingValue(topK) ? { top_k: topK } : {}),\n ...(isSetSamplingValue(topP) ? { top_p: topP } : {}),\n };\n}\n\nfunction findStreamChunkBoundary(text: string, minSize: number): number {\n if (minSize >= text.length) {\n return text.length;\n }\n\n for (let position = minSize; position < text.length; position++) {\n if (STREAM_BOUNDARIES.has(text[position])) {\n return position + 1;\n }\n }\n\n return text.length;\n}\n\nfunction splitStreamToken(text: string): string[] {\n const chunks: string[] = [];\n let currentIndex = 0;\n\n while (currentIndex < text.length) {\n const remainingText = text.slice(currentIndex);\n const chunkSize = findStreamChunkBoundary(\n remainingText,\n STREAM_CHUNK_MIN_SIZE\n );\n chunks.push(text.slice(currentIndex, currentIndex + chunkSize));\n currentIndex += chunkSize;\n }\n\n return chunks;\n}\n\nfunction getCadencedStreamDelay({\n targetDelay,\n lastVisibleTextAt,\n now,\n}: {\n targetDelay: number;\n lastVisibleTextAt?: number;\n now: number;\n}): number {\n if (targetDelay <= 0 || lastVisibleTextAt == null) {\n return 0;\n }\n return Math.max(0, targetDelay - (now - lastVisibleTextAt));\n}\n\nasync function waitForStreamDelay(\n delay: number,\n signal?: AbortSignal\n): Promise<void> {\n if (delay <= 0 || isSignalAborted(signal)) {\n return;\n }\n await new Promise<void>((resolve) => {\n const timeoutRef: { current?: ReturnType<typeof setTimeout> } = {};\n const onAbort = (): void => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n signal?.removeEventListener('abort', onAbort);\n resolve();\n };\n timeoutRef.current = setTimeout(() => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n }, delay);\n signal?.addEventListener('abort', onAbort, { once: true });\n if (isSignalAborted(signal)) {\n onAbort();\n }\n });\n}\n\nfunction isSignalAborted(signal?: AbortSignal): boolean {\n return signal?.aborted === true;\n}\n\nasync function* abortableAnthropicStream(\n source: AnthropicEventStream,\n signal?: AbortSignal\n): AsyncGenerator<AnthropicMessageStreamEvent> {\n for await (const data of source) {\n if (isSignalAborted(signal)) {\n source.controller?.abort();\n return;\n }\n yield data;\n }\n}\n\nfunction extractToken(\n chunk: AIMessageChunk\n): [string, StreamTokenType] | [undefined] {\n if (typeof chunk.content === 'string') {\n return [chunk.content, 'string'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'input' in chunk.content[0]\n ) {\n return typeof chunk.content[0].input === 'string'\n ? [chunk.content[0].input, 'input']\n : [JSON.stringify(chunk.content[0].input), 'input'];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'text' in chunk.content[0]\n ) {\n const text = chunk.content[0].text;\n return typeof text === 'string' ? [text, 'content'] : [undefined];\n } else if (\n Array.isArray(chunk.content) &&\n chunk.content.length >= 1 &&\n 'thinking' in chunk.content[0]\n ) {\n const thinking = chunk.content[0].thinking;\n return typeof thinking === 'string' ? [thinking, 'content'] : [undefined];\n }\n return [undefined];\n}\n\nfunction cloneChunk(\n text: string,\n tokenType: StreamTokenType,\n chunk: AIMessageChunk\n): AIMessageChunk {\n if (tokenType === 'string') {\n return new AIMessageChunk(Object.assign({}, chunk, { content: text }));\n } else if (tokenType === 'input') {\n return chunk;\n }\n const content = chunk.content[0] as MessageContentComplex;\n if (content.type === 'text') {\n return new AIMessageChunk(\n Object.assign({}, chunk, {\n content: [Object.assign({}, content, { text })],\n })\n );\n } else if (content.type === 'text_delta') {\n return new AIMessageChunk(\n Object.assign({}, chunk, {\n content: [Object.assign({}, content, { text })],\n })\n );\n } else if (\n typeof content.type === 'string' &&\n content.type.startsWith('thinking')\n ) {\n return new AIMessageChunk(\n Object.assign({}, chunk, {\n content: [Object.assign({}, content, { thinking: text })],\n })\n );\n }\n\n return chunk;\n}\n\nfunction resolveCumulativeUsageValue(\n current: number | null | undefined,\n previous: number\n): CumulativeUsageValue {\n if (current == null) {\n return { cumulative: previous, increment: 0 };\n }\n const cumulative = Math.max(previous, current);\n return { cumulative, increment: cumulative - previous };\n}\n\nfunction withIncrementalMessageDeltaUsage(\n chunk: AIMessageChunk,\n previousUsage: AnthropicStreamUsage,\n cumulativeUsage: AnthropicUsageData\n): { chunk: AIMessageChunk; usage: AnthropicStreamUsage } {\n const usage = chunk.usage_metadata;\n if (usage == null) {\n return { chunk, usage: previousUsage };\n }\n\n const inputTokens = resolveCumulativeUsageValue(\n cumulativeUsage.input_tokens,\n previousUsage.inputTokens\n );\n const cacheCreationInputTokens = resolveCumulativeUsageValue(\n cumulativeUsage.cache_creation_input_tokens,\n previousUsage.cacheCreationInputTokens\n );\n const cacheReadInputTokens = resolveCumulativeUsageValue(\n cumulativeUsage.cache_read_input_tokens,\n previousUsage.cacheReadInputTokens\n );\n const outputTokens = resolveCumulativeUsageValue(\n cumulativeUsage.output_tokens,\n previousUsage.outputTokens\n );\n const incrementalInputTokens =\n inputTokens.increment +\n cacheCreationInputTokens.increment +\n cacheReadInputTokens.increment;\n const hasCacheUsage =\n cumulativeUsage.cache_creation_input_tokens != null ||\n cumulativeUsage.cache_read_input_tokens != null;\n const inputTokenDetails = {\n ...(cumulativeUsage.cache_creation_input_tokens != null && {\n cache_creation: cacheCreationInputTokens.increment,\n }),\n ...(cumulativeUsage.cache_read_input_tokens != null && {\n cache_read: cacheReadInputTokens.increment,\n }),\n };\n\n return {\n chunk: new AIMessageChunk(\n Object.assign({}, chunk, {\n usage_metadata: {\n ...usage,\n input_tokens: incrementalInputTokens,\n output_tokens: outputTokens.increment,\n total_tokens: incrementalInputTokens + outputTokens.increment,\n input_token_details: hasCacheUsage ? inputTokenDetails : undefined,\n },\n })\n ),\n usage: {\n inputTokens: inputTokens.cumulative,\n cacheCreationInputTokens: cacheCreationInputTokens.cumulative,\n cacheReadInputTokens: cacheReadInputTokens.cumulative,\n outputTokens: outputTokens.cumulative,\n },\n };\n}\n\nexport type CustomAnthropicInput = AnthropicInput & {\n _lc_stream_delay?: number;\n outputConfig?: AnthropicOutputConfig;\n inferenceGeo?: string;\n contextManagement?: AnthropicContextManagementConfigParam;\n} & BaseChatModelParams;\n\nexport type CustomAnthropicCallOptions = {\n outputConfig?: AnthropicOutputConfig;\n outputFormat?: Anthropic.Messages.JSONOutputFormat;\n inferenceGeo?: string;\n betas?: AnthropicBeta[];\n container?: string;\n mcp_servers?: AnthropicMCPServerURLDefinition[];\n};\n\ntype CustomAnthropicInvocationParams = {\n betas?: AnthropicBeta[];\n container?: string;\n context_management?: AnthropicContextManagementConfigParam;\n inference_geo?: string;\n mcp_servers?: AnthropicMCPServerURLDefinition[];\n output_config?: AnthropicOutputConfig;\n};\n\ntype QueuedGenerationChunk = {\n chunk: ChatGenerationChunk;\n token: string;\n smooth: boolean;\n textLength: number;\n};\n\nexport class CustomAnthropic extends ChatAnthropicMessages {\n _lc_stream_delay: number;\n private tools_in_params?: boolean;\n top_k: number | undefined;\n outputConfig?: AnthropicOutputConfig;\n inferenceGeo?: string;\n contextManagement?: AnthropicContextManagementConfigParam;\n constructor(fields?: CustomAnthropicInput) {\n super(fields);\n this.resetTokenEvents();\n this.setDirectFields(fields);\n this._lc_stream_delay = Math.max(\n 0,\n fields?._lc_stream_delay ?? DEFAULT_STREAM_DELAY\n );\n this.outputConfig = fields?.outputConfig;\n this.inferenceGeo = fields?.inferenceGeo;\n this.contextManagement = fields?.contextManagement;\n }\n\n static lc_name(): 'LibreChatAnthropic' {\n return 'LibreChatAnthropic';\n }\n\n /**\n * Get the parameters used to invoke the model\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomAnthropicCallOptions\n ): Omit<\n AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams,\n 'messages'\n > &\n CustomAnthropicInvocationParams {\n const tool_choice:\n | Anthropic.Messages.ToolChoiceAuto\n | Anthropic.Messages.ToolChoiceAny\n | Anthropic.Messages.ToolChoiceTool\n | Anthropic.Messages.ToolChoiceNone\n | undefined = handleToolChoice(options?.tool_choice);\n\n const callOptions = options as CustomAnthropicCallOptions | undefined;\n const mergedOutputConfig: AnthropicOutputConfig | undefined = (():\n | AnthropicOutputConfig\n | undefined => {\n const base = {\n ...this.outputConfig,\n ...callOptions?.outputConfig,\n };\n if (callOptions?.outputFormat && !base.format) {\n base.format = callOptions.outputFormat;\n }\n return Object.keys(base).length > 0 ? base : undefined;\n })();\n\n const inferenceGeo = callOptions?.inferenceGeo ?? this.inferenceGeo;\n\n const contextManagement = this.contextManagement;\n const toolBetas = getToolBetas(options?.tools);\n const compactionBetas = getCompactionBetas(contextManagement);\n const taskBudgetBetas = getTaskBudgetBetas(this.model, mergedOutputConfig);\n const formattedTools = this.formatStructuredToolToAnthropic(\n options?.tools,\n {\n strict: options?.strict,\n }\n );\n\n const sharedParams = {\n tools: formattedTools,\n tool_choice,\n // Match upstream: omit `thinking` unless the user set it, so we don't send\n // `{ type: 'disabled' }` (an unsupported param on some models) by default.\n thinking: this.thinkingExplicitlySet ? this.thinking : undefined,\n context_management: contextManagement,\n ...this.invocationKwargs,\n container: callOptions?.container,\n betas: combineBetas(\n this.betas,\n callOptions?.betas,\n toolBetas,\n compactionBetas,\n taskBudgetBetas\n ),\n output_config: mergedOutputConfig,\n inference_geo: inferenceGeo,\n mcp_servers: callOptions?.mcp_servers,\n // Top-level request cache_control (1.5.x): API auto-advances the cache\n // breakpoint across turns. Additive — independent of our block-level cache.\n cache_control: options?.cache_control,\n };\n validateInvocationParamCompatibility({\n model: this.model,\n thinking: this.thinking,\n topK: this.top_k,\n topP: this.topP,\n temperature: this.temperature,\n });\n\n return {\n model: this.model,\n stop_sequences: options?.stop ?? this.stopSequences,\n stream: this.streaming,\n max_tokens: this.maxTokens,\n ...getSamplingParams({\n model: this.model,\n thinking: this.thinking,\n topK: this.top_k,\n topP: this.topP,\n temperature: this.temperature,\n }),\n ...sharedParams,\n };\n }\n\n resetTokenEvents(): void {\n this.tools_in_params = undefined;\n }\n\n setDirectFields(fields?: CustomAnthropicInput): void {\n this.temperature = fields?.temperature ?? undefined;\n this.topP = fields?.topP ?? undefined;\n this.top_k = fields?.topK;\n if (this.temperature === -1 || this.temperature === 1) {\n this.temperature = undefined;\n }\n if (this.topP === -1) {\n this.topP = undefined;\n }\n if (this.top_k === -1) {\n this.top_k = undefined;\n }\n }\n\n private createGenerationChunk({\n token,\n chunk,\n shouldStreamUsage,\n }: {\n token?: string;\n chunk: AIMessageChunk;\n shouldStreamUsage: boolean;\n }): ChatGenerationChunk {\n const usage_metadata = shouldStreamUsage ? chunk.usage_metadata : undefined;\n return new ChatGenerationChunk({\n message: new AIMessageChunk({\n // Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().\n content: chunk.content,\n additional_kwargs: chunk.additional_kwargs,\n tool_call_chunks: chunk.tool_call_chunks,\n response_metadata: chunk.response_metadata,\n usage_metadata,\n id: chunk.id,\n }),\n text: token ?? '',\n });\n }\n\n protected override async createStreamWithRetry(\n request: AnthropicStreamingMessageCreateParams,\n options?: AnthropicRequestOptions\n ): ReturnType<ChatAnthropicMessages['createStreamWithRetry']> {\n return super.createStreamWithRetry(\n stripUnsupportedAssistantPrefill(request),\n options\n );\n }\n\n protected override async completionWithRetry(\n request: AnthropicMessageCreateParams,\n options: AnthropicRequestOptions\n ): ReturnType<ChatAnthropicMessages['completionWithRetry']> {\n return super.completionWithRetry(\n stripUnsupportedAssistantPrefill(request),\n options\n );\n }\n\n async *_streamChatModelEvents(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n _runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatModelStreamEvent> {\n const params = this.invocationParams(options);\n const formattedMessages = _convertMessagesToAnthropicPayload(messages);\n const payload = stripUnsupportedAssistantPrefill({\n ...params,\n ...formattedMessages,\n stream: true,\n } as const);\n const stream = await this.createStreamWithRetry(payload, {\n headers: options.headers,\n signal: options.signal,\n });\n const shouldStreamUsage = options.streamUsage ?? this.streamUsage;\n yield* convertAnthropicStream(\n abortableAnthropicStream(stream, options.signal),\n { streamUsage: shouldStreamUsage }\n );\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n this.resetTokenEvents();\n const params = this.invocationParams(options);\n const formattedMessages = _convertMessagesToAnthropicPayload(messages);\n const payload = stripUnsupportedAssistantPrefill({\n ...params,\n ...formattedMessages,\n stream: true,\n } as const);\n const coerceContentToString =\n !_toolsInParams(payload) &&\n !_documentsInParams(payload) &&\n !_thinkingInParams(payload) &&\n !_compactionInParams(payload);\n\n const stream = await this.createStreamWithRetry(payload, {\n headers: options.headers,\n signal: options.signal,\n });\n\n const shouldStreamUsage = options.streamUsage ?? this.streamUsage;\n let streamUsage: AnthropicStreamUsage = {\n inputTokens: 0,\n cacheCreationInputTokens: 0,\n cacheReadInputTokens: 0,\n outputTokens: 0,\n };\n const queuedChunks: QueuedGenerationChunk[] = [];\n const producerState: {\n done: boolean;\n error?: unknown;\n } = { done: false };\n let queuedChunkIndex = 0;\n let bufferedTextLength = 0;\n let consumerClosed = false;\n let notifyConsumer: (() => void) | undefined;\n let notifyProducer: (() => void) | undefined;\n\n const notifyConsumerForChunk = (): void => {\n notifyConsumer?.();\n notifyConsumer = undefined;\n };\n\n const notifyProducerForSpace = (): void => {\n notifyProducer?.();\n notifyProducer = undefined;\n };\n\n const hasQueuedChunks = (): boolean =>\n queuedChunkIndex < queuedChunks.length;\n\n const getQueuedChunkCount = (): number =>\n queuedChunks.length - queuedChunkIndex;\n\n const isQueueAtCapacity = (): boolean =>\n getQueuedChunkCount() >= MAX_STREAM_QUEUE_CHUNKS ||\n bufferedTextLength >= MAX_STREAM_QUEUE_TEXT_CHARS;\n\n const waitForNextChunk = async (): Promise<void> => {\n if (\n hasQueuedChunks() ||\n producerState.done ||\n producerState.error != null\n ) {\n return;\n }\n await new Promise<void>((resolve) => {\n notifyConsumer = resolve;\n });\n };\n\n const waitForQueueSpace = async (): Promise<void> => {\n while (\n isQueueAtCapacity() &&\n !consumerClosed &&\n !isSignalAborted(options.signal)\n ) {\n await new Promise<void>((resolve) => {\n const signal = options.signal;\n const onAbort = (): void => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n };\n const onSpace = (): void => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n };\n notifyProducer = onSpace;\n signal?.addEventListener('abort', onAbort, { once: true });\n if (isSignalAborted(signal)) {\n onAbort();\n }\n });\n }\n };\n\n const dequeue = (): QueuedGenerationChunk | undefined => {\n if (!hasQueuedChunks()) {\n return undefined;\n }\n const queuedChunk = queuedChunks[queuedChunkIndex];\n queuedChunkIndex++;\n if (\n queuedChunkIndex > 128 &&\n queuedChunkIndex * 2 >= queuedChunks.length\n ) {\n queuedChunks.splice(0, queuedChunkIndex);\n queuedChunkIndex = 0;\n }\n return queuedChunk;\n };\n\n const enqueue = async (\n queuedChunk: QueuedGenerationChunk\n ): Promise<void> => {\n await waitForQueueSpace();\n if (consumerClosed || isSignalAborted(options.signal)) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n queuedChunks.push(queuedChunk);\n if (queuedChunk.smooth) {\n bufferedTextLength += queuedChunk.textLength;\n }\n notifyConsumerForChunk();\n };\n\n const enqueueChunk = async ({\n token,\n chunk,\n smooth,\n }: {\n token: string;\n chunk: AIMessageChunk;\n smooth: boolean;\n }): Promise<void> => {\n await enqueue({\n token,\n smooth,\n textLength: smooth ? token.length : 0,\n chunk: this.createGenerationChunk({\n token,\n chunk,\n shouldStreamUsage,\n }),\n });\n };\n\n const enqueueTextChunks = (\n token: string,\n tokenType: StreamTokenType,\n chunk: AIMessageChunk\n ): Promise<void> => {\n if (token === '') {\n return Promise.resolve();\n }\n if (this._lc_stream_delay <= 0) {\n return enqueueChunk({ token, chunk, smooth: false });\n }\n\n const tokenChunks = splitStreamToken(token);\n if (tokenChunks.length <= 1) {\n return enqueueChunk({ token, chunk, smooth: true });\n }\n\n let emittedUsage = false;\n return tokenChunks.reduce(async (previous, currentToken) => {\n await previous;\n const newChunk = cloneChunk(currentToken, tokenType, chunk);\n const chunkForToken =\n emittedUsage && newChunk.usage_metadata != null\n ? new AIMessageChunk(\n Object.assign({}, newChunk, { usage_metadata: undefined })\n )\n : newChunk;\n\n await enqueueChunk({\n token: currentToken,\n chunk: chunkForToken,\n smooth: true,\n });\n\n if (newChunk.usage_metadata != null && !emittedUsage) {\n emittedUsage = true;\n }\n }, Promise.resolve());\n };\n\n const producer = (async (): Promise<void> => {\n try {\n for await (const data of stream) {\n if (isSignalAborted(options.signal)) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n\n const result = _makeMessageChunkFromAnthropicEvent(\n data as Anthropic.Beta.Messages.BetaRawMessageStreamEvent,\n {\n streamUsage: shouldStreamUsage,\n coerceContentToString,\n }\n );\n if (!result) {\n continue;\n }\n\n let { chunk } = result;\n if (data.type === 'message_start') {\n streamUsage = {\n ...streamUsage,\n inputTokens: data.message.usage.input_tokens,\n outputTokens: data.message.usage.output_tokens,\n cacheCreationInputTokens:\n data.message.usage.cache_creation_input_tokens ?? 0,\n cacheReadInputTokens:\n data.message.usage.cache_read_input_tokens ?? 0,\n };\n }\n if (data.type === 'message_delta') {\n const incremental = withIncrementalMessageDeltaUsage(\n chunk,\n streamUsage,\n data.usage\n );\n chunk = incremental.chunk;\n streamUsage = incremental.usage;\n }\n\n const [token = '', tokenType] = extractToken(chunk);\n if (\n !tokenType ||\n tokenType === 'input' ||\n (token === '' && (chunk.usage_metadata != null || chunk.id != null))\n ) {\n await enqueueChunk({ token, chunk, smooth: false });\n continue;\n }\n\n await enqueueTextChunks(token, tokenType, chunk);\n }\n } catch (error) {\n producerState.error = error;\n } finally {\n producerState.done = true;\n notifyConsumerForChunk();\n }\n })();\n\n let hasEmittedText = false;\n let lastVisibleTextAt: number | undefined;\n let keepStreaming = true;\n try {\n while (keepStreaming) {\n if (isSignalAborted(options.signal)) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n\n await waitForNextChunk();\n const queuedChunk = dequeue();\n\n if (!queuedChunk) {\n if (producerState.error != null) {\n throw producerState.error;\n }\n if (producerState.done) {\n keepStreaming = false;\n }\n continue;\n }\n\n if (queuedChunk.smooth) {\n bufferedTextLength = Math.max(\n 0,\n bufferedTextLength - queuedChunk.textLength\n );\n notifyProducerForSpace();\n await waitForStreamDelay(\n getCadencedStreamDelay({\n targetDelay: hasEmittedText ? this._lc_stream_delay : 0,\n lastVisibleTextAt,\n now: Date.now(),\n }),\n options.signal\n );\n if (isSignalAborted(options.signal)) {\n stream.controller.abort();\n throw new Error('AbortError: User aborted the request.');\n }\n hasEmittedText = true;\n lastVisibleTextAt = Date.now();\n } else {\n notifyProducerForSpace();\n }\n\n yield queuedChunk.chunk;\n await runManager?.handleLLMNewToken(\n queuedChunk.token,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: queuedChunk.chunk }\n );\n }\n } finally {\n consumerClosed = true;\n if (!producerState.done) {\n stream.controller.abort();\n notifyProducerForSpace();\n }\n await producer;\n this.resetTokenEvents();\n }\n }\n}\n"],"mappings":";;;;;;;;AAgCA,MAAM,uBAAuB;AAC7B,MAAM,0BAA0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;KAElB;IACF;IAEA,IAAI,YAAY,QAAQ;KACtB,qBAAqB,KAAK,IACxB,GACA,qBAAqB,YAAY,UACnC;KACA,uBAAuB;KACvB,MAAM,mBACJ,uBAAuB;MACrB,aAAa,iBAAiB,KAAK,mBAAmB;MACtD;MACA,KAAK,KAAK,IAAI;KAChB,CAAC,GACD,QAAQ,MACV;KACA,IAAI,gBAAgB,QAAQ,MAAM,GAAG;MACnC,OAAO,WAAW,MAAM;MACxB,MAAM,IAAI,MAAM,uCAAuC;KACzD;KACA,iBAAiB;KACjB,oBAAoB,KAAK,IAAI;IAC/B,OACE,uBAAuB;IAGzB,MAAM,YAAY;IAClB,MAAM,YAAY,kBAChB,YAAY,OACZ,KAAA,GACA,KAAA,GACA,KAAA,GACA,KAAA,GACA,EAAE,OAAO,YAAY,MAAM,CAC7B;GACF;EACF,UAAU;GACR,iBAAiB;GACjB,IAAI,CAAC,cAAc,MAAM;IACvB,OAAO,WAAW,MAAM;IACxB,uBAAuB;GACzB;GACA,MAAM;GACN,KAAK,iBAAiB;EACxB;CACF;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"types.mjs","names":[],"sources":["../../../../src/llm/anthropic/types.ts"],"sourcesContent":["import Anthropic from '@anthropic-ai/sdk';\n\nimport type { BindToolsInput } from '@langchain/core/language_models/chat_models';\nimport type { AnthropicBeta } from '@anthropic-ai/sdk/resources';\n\nexport type { AnthropicBeta };\n\nexport type AnthropicStreamUsage = Anthropic.Usage;\nexport type AnthropicMessageDeltaEvent = Anthropic.MessageDeltaEvent;\nexport type AnthropicMessageStartEvent = Anthropic.MessageStartEvent;\n\nexport type AnthropicToolResponse = {\n type: 'tool_use' | 'server_tool_use';\n id: string;\n name: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: Record<string, any>;\n};\n\nexport type AnthropicMessageParam = Anthropic.MessageParam;\nexport type AnthropicMessageResponse =\n | Anthropic.ContentBlock\n | AnthropicToolResponse;\nexport type AnthropicMessageCreateParams =\n Anthropic.MessageCreateParamsNonStreaming;\nexport type AnthropicStreamingMessageCreateParams =\n Anthropic.MessageCreateParamsStreaming;\nexport type AnthropicThinkingConfigParam = Anthropic.ThinkingConfigParam;\nexport type AnthropicContextManagementConfigParam =\n Anthropic.Beta.BetaContextManagementConfig;\nexport type AnthropicMessageStreamEvent = Anthropic.MessageStreamEvent;\nexport type AnthropicRequestOptions = Anthropic.RequestOptions;\nexport type AnthropicMCPServerURLDefinition =\n Anthropic.Beta.Messages.BetaRequestMCPServerURLDefinition;\nexport type AnthropicToolChoice =\n | {\n type: 'tool';\n name: string;\n }\n | 'any'\n | 'auto'\n | 'none'\n | string;\nexport type ChatAnthropicToolType = Anthropic.Messages.Tool | BindToolsInput;\nexport type AnthropicTextBlockParam = Anthropic.Messages.TextBlockParam;\nexport type AnthropicImageBlockParam = Anthropic.Messages.ImageBlockParam;\nexport type AnthropicToolUseBlockParam = Anthropic.Messages.ToolUseBlockParam;\nexport type AnthropicToolResultBlockParam =\n Anthropic.Messages.ToolResultBlockParam;\nexport type AnthropicDocumentBlockParam = Anthropic.Messages.DocumentBlockParam;\nexport type AnthropicThinkingBlockParam = Anthropic.Messages.ThinkingBlockParam;\nexport type AnthropicRedactedThinkingBlockParam =\n Anthropic.Messages.RedactedThinkingBlockParam;\nexport type AnthropicServerToolUseBlockParam =\n Anthropic.Messages.ServerToolUseBlockParam;\nexport type AnthropicWebSearchToolResultBlockParam =\n Anthropic.Messages.WebSearchToolResultBlockParam;\nexport type AnthropicWebSearchResultBlockParam =\n Anthropic.Messages.WebSearchResultBlockParam;\nexport type AnthropicSearchResultBlockParam =\n Anthropic.Beta.BetaSearchResultBlockParam;\nexport type AnthropicCompactionBlockParam =\n Anthropic.Beta.BetaCompactionBlockParam;\nexport type AnthropicOutputConfig = Anthropic.Messages.OutputConfig;\nexport type ChatAnthropicOutputFormat = Anthropic.Messages.JSONOutputFormat;\n\n// Union of all possible content block types including server tool use\nexport type AnthropicContentBlock =\n | AnthropicTextBlockParam\n | AnthropicImageBlockParam\n | AnthropicToolUseBlockParam\n | AnthropicToolResultBlockParam\n | AnthropicDocumentBlockParam\n | AnthropicThinkingBlockParam\n | AnthropicRedactedThinkingBlockParam\n | AnthropicServerToolUseBlockParam\n | AnthropicWebSearchToolResultBlockParam\n | AnthropicWebSearchResultBlockParam\n | AnthropicCompactionBlockParam;\n\n// Union of all possible content block types including server tool use\nexport type ChatAnthropicContentBlock =\n | AnthropicTextBlockParam\n | AnthropicImageBlockParam\n | AnthropicToolUseBlockParam\n | AnthropicToolResultBlockParam\n | AnthropicDocumentBlockParam\n | AnthropicThinkingBlockParam\n | AnthropicRedactedThinkingBlockParam\n | AnthropicServerToolUseBlockParam\n | AnthropicWebSearchToolResultBlockParam\n | AnthropicWebSearchResultBlockParam\n | AnthropicSearchResultBlockParam\n | AnthropicCompactionBlockParam;\n\nexport function isAnthropicImageBlockParam(\n block: unknown\n): block is AnthropicImageBlockParam {\n if (block == null) {\n return false;\n }\n if (typeof block !== 'object') {\n return false;\n }\n if (!('type' in block) || block.type !== 'image') {\n return false;\n }\n\n if (!('source' in block) || typeof block.source !== 'object') {\n return false;\n }\n\n if (block.source == null) {\n return false;\n }\n\n if (!('type' in block.source)) {\n return false;\n }\n\n if (block.source.type === 'base64') {\n if (!('media_type' in block.source)) {\n return false;\n }\n\n if (typeof block.source.media_type !== 'string') {\n return false;\n }\n\n if (!('data' in block.source)) {\n return false;\n }\n\n if (typeof block.source.data !== 'string') {\n return false;\n }\n\n return true;\n }\n\n if (block.source.type === 'url') {\n if (!('url' in block.source)) {\n return false;\n }\n\n if (typeof block.source.url !== 'string') {\n return false;\n }\n\n return true;\n }\n\n return false;\n}\n"],"mappings":";AA+FA,SAAgB,2BACd,OACmC;CACnC,IAAI,SAAS,MACX,OAAO;CAET,IAAI,OAAO,UAAU,UACnB,OAAO;CAET,IAAI,EAAE,UAAU,UAAU,MAAM,SAAS,SACvC,OAAO;CAGT,IAAI,EAAE,YAAY,UAAU,OAAO,MAAM,WAAW,UAClD,OAAO;CAGT,IAAI,MAAM,UAAU,MAClB,OAAO;CAGT,IAAI,EAAE,UAAU,MAAM,SACpB,OAAO;CAGT,IAAI,MAAM,OAAO,SAAS,UAAU;EAClC,IAAI,EAAE,gBAAgB,MAAM,SAC1B,OAAO;EAGT,IAAI,OAAO,MAAM,OAAO,eAAe,UACrC,OAAO;EAGT,IAAI,EAAE,UAAU,MAAM,SACpB,OAAO;EAGT,IAAI,OAAO,MAAM,OAAO,SAAS,UAC/B,OAAO;EAGT,OAAO;CACT;CAEA,IAAI,MAAM,OAAO,SAAS,OAAO;EAC/B,IAAI,EAAE,SAAS,MAAM,SACnB,OAAO;EAGT,IAAI,OAAO,MAAM,OAAO,QAAQ,UAC9B,OAAO;EAGT,OAAO;CACT;CAEA,OAAO;AACT"}
1
+ {"version":3,"file":"types.mjs","names":[],"sources":["../../../../src/llm/anthropic/types.ts"],"sourcesContent":["import Anthropic from '@anthropic-ai/sdk';\n\nimport type { BindToolsInput } from '@langchain/core/language_models/chat_models';\nimport type { AnthropicBeta } from '@anthropic-ai/sdk/resources';\n\nexport type { AnthropicBeta };\n\nexport type AnthropicStreamUsage = Anthropic.Usage;\nexport type AnthropicMessageDeltaEvent = Anthropic.MessageDeltaEvent;\nexport type AnthropicMessageStartEvent = Anthropic.MessageStartEvent;\n\nexport type AnthropicToolResponse = {\n type: 'tool_use' | 'server_tool_use';\n id: string;\n name: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: Record<string, any>;\n};\n\nexport type AnthropicMessageParam = Anthropic.MessageParam;\nexport type AnthropicMessageResponse =\n | Anthropic.ContentBlock\n | AnthropicToolResponse;\nexport type AnthropicMessageCreateParams =\n Anthropic.MessageCreateParamsNonStreaming;\nexport type AnthropicStreamingMessageCreateParams =\n Anthropic.MessageCreateParamsStreaming;\nexport type AnthropicThinkingConfigParam = Anthropic.ThinkingConfigParam;\nexport type AnthropicContextManagementConfigParam =\n Anthropic.Beta.BetaContextManagementConfig;\nexport type AnthropicMessageStreamEvent = Anthropic.MessageStreamEvent;\nexport type AnthropicRequestOptions = Anthropic.RequestOptions;\nexport type AnthropicMCPServerURLDefinition =\n Anthropic.Beta.Messages.BetaRequestMCPServerURLDefinition;\nexport type AnthropicToolChoice =\n | {\n type: 'tool';\n name: string;\n }\n | 'any'\n | 'auto'\n | 'none'\n | string;\nexport type ChatAnthropicToolType = Anthropic.Messages.Tool | BindToolsInput;\nexport type AnthropicTextBlockParam = Anthropic.Messages.TextBlockParam;\nexport type AnthropicImageBlockParam = Anthropic.Messages.ImageBlockParam;\nexport type AnthropicToolUseBlockParam = Anthropic.Messages.ToolUseBlockParam;\nexport type AnthropicToolResultBlockParam =\n Anthropic.Messages.ToolResultBlockParam;\nexport type AnthropicDocumentBlockParam = Anthropic.Messages.DocumentBlockParam;\nexport type AnthropicThinkingBlockParam = Anthropic.Messages.ThinkingBlockParam;\nexport type AnthropicRedactedThinkingBlockParam =\n Anthropic.Messages.RedactedThinkingBlockParam;\nexport type AnthropicServerToolUseBlockParam =\n Anthropic.Messages.ServerToolUseBlockParam;\nexport type AnthropicWebSearchToolResultBlockParam =\n Anthropic.Messages.WebSearchToolResultBlockParam;\nexport type AnthropicWebSearchResultBlockParam =\n Anthropic.Messages.WebSearchResultBlockParam;\nexport type AnthropicSearchResultBlockParam =\n Anthropic.Beta.BetaSearchResultBlockParam;\nexport type AnthropicCompactionBlockParam =\n Anthropic.Beta.BetaCompactionBlockParam;\nexport type AnthropicCompactionBlock = Anthropic.Beta.BetaCompactionBlock;\nexport type AnthropicCompactionContentBlockDelta =\n Anthropic.Beta.BetaCompactionContentBlockDelta;\nexport type AnthropicOutputConfig = Anthropic.Messages.OutputConfig;\nexport type ChatAnthropicOutputFormat = Anthropic.Messages.JSONOutputFormat;\n\n// Union of all possible content block types including server tool use\nexport type AnthropicContentBlock =\n | AnthropicTextBlockParam\n | AnthropicImageBlockParam\n | AnthropicToolUseBlockParam\n | AnthropicToolResultBlockParam\n | AnthropicDocumentBlockParam\n | AnthropicThinkingBlockParam\n | AnthropicRedactedThinkingBlockParam\n | AnthropicServerToolUseBlockParam\n | AnthropicWebSearchToolResultBlockParam\n | AnthropicWebSearchResultBlockParam\n | AnthropicCompactionBlockParam;\n\n// Union of all possible content block types including server tool use\nexport type ChatAnthropicContentBlock =\n | AnthropicTextBlockParam\n | AnthropicImageBlockParam\n | AnthropicToolUseBlockParam\n | AnthropicToolResultBlockParam\n | AnthropicDocumentBlockParam\n | AnthropicThinkingBlockParam\n | AnthropicRedactedThinkingBlockParam\n | AnthropicServerToolUseBlockParam\n | AnthropicWebSearchToolResultBlockParam\n | AnthropicWebSearchResultBlockParam\n | AnthropicSearchResultBlockParam\n | AnthropicCompactionBlockParam;\n\nexport function isAnthropicImageBlockParam(\n block: unknown\n): block is AnthropicImageBlockParam {\n if (block == null) {\n return false;\n }\n if (typeof block !== 'object') {\n return false;\n }\n if (!('type' in block) || block.type !== 'image') {\n return false;\n }\n\n if (!('source' in block) || typeof block.source !== 'object') {\n return false;\n }\n\n if (block.source == null) {\n return false;\n }\n\n if (!('type' in block.source)) {\n return false;\n }\n\n if (block.source.type === 'base64') {\n if (!('media_type' in block.source)) {\n return false;\n }\n\n if (typeof block.source.media_type !== 'string') {\n return false;\n }\n\n if (!('data' in block.source)) {\n return false;\n }\n\n if (typeof block.source.data !== 'string') {\n return false;\n }\n\n return true;\n }\n\n if (block.source.type === 'url') {\n if (!('url' in block.source)) {\n return false;\n }\n\n if (typeof block.source.url !== 'string') {\n return false;\n }\n\n return true;\n }\n\n return false;\n}\n"],"mappings":";AAkGA,SAAgB,2BACd,OACmC;CACnC,IAAI,SAAS,MACX,OAAO;CAET,IAAI,OAAO,UAAU,UACnB,OAAO;CAET,IAAI,EAAE,UAAU,UAAU,MAAM,SAAS,SACvC,OAAO;CAGT,IAAI,EAAE,YAAY,UAAU,OAAO,MAAM,WAAW,UAClD,OAAO;CAGT,IAAI,MAAM,UAAU,MAClB,OAAO;CAGT,IAAI,EAAE,UAAU,MAAM,SACpB,OAAO;CAGT,IAAI,MAAM,OAAO,SAAS,UAAU;EAClC,IAAI,EAAE,gBAAgB,MAAM,SAC1B,OAAO;EAGT,IAAI,OAAO,MAAM,OAAO,eAAe,UACrC,OAAO;EAGT,IAAI,EAAE,UAAU,MAAM,SACpB,OAAO;EAGT,IAAI,OAAO,MAAM,OAAO,SAAS,UAC/B,OAAO;EAGT,OAAO;CACT;CAEA,IAAI,MAAM,OAAO,SAAS,OAAO;EAC/B,IAAI,EAAE,SAAS,MAAM,SACnB,OAAO;EAGT,IAAI,OAAO,MAAM,OAAO,QAAQ,UAC9B,OAAO;EAGT,OAAO;CACT;CAEA,OAAO;AACT"}
@@ -257,14 +257,14 @@ function _formatContent(message) {
257
257
  const filteredContentBlocks = contentParts.map((contentPart) => {
258
258
  /**
259
259
  * Normalize server_tool_use blocks into a clean shape the API accepts.
260
- * These blocks may arrive with the correct type (server_tool_use) or mislabeled
261
- * as text/tool_use after chunk concatenation or state serialization.
260
+ * These blocks may arrive with the correct type (server_tool_use), as a
261
+ * standardized server_tool_call, or mislabeled after state serialization.
262
262
  * Regardless of current type, if the id starts with 'srvtoolu_' we rebuild
263
263
  * a clean block with only the properties the API expects.
264
264
  */
265
265
  if ("id" in contentPart && typeof contentPart.id === "string" && contentPart.id.startsWith("srvtoolu_") && "name" in contentPart) {
266
266
  const rawPart = contentPart;
267
- let input = rawPart.input;
267
+ let input = rawPart.input ?? rawPart.args;
268
268
  if (typeof input === "string") try {
269
269
  input = JSON.parse(input);
270
270
  } catch {
@@ -293,6 +293,35 @@ function _formatContent(message) {
293
293
  return null;
294
294
  }
295
295
  /**
296
+ * Native ChatModelStream output standardizes client tool uses as
297
+ * `tool_call` blocks. Convert those blocks back to Anthropic's wire shape
298
+ * before the generic content formatter sees them.
299
+ */
300
+ if (contentPart.type === "tool_call" && "id" in contentPart && typeof contentPart.id === "string" && "name" in contentPart && typeof contentPart.name === "string" && "args" in contentPart) {
301
+ let args = contentPart.args;
302
+ if (typeof args === "string") try {
303
+ args = JSON.parse(args);
304
+ } catch {
305
+ args = {};
306
+ }
307
+ if (args == null || typeof args !== "object" || Array.isArray(args)) args = {};
308
+ return _convertLangChainToolCallToAnthropic({
309
+ id: contentPart.id,
310
+ name: contentPart.name,
311
+ args
312
+ });
313
+ }
314
+ /**
315
+ * Native Anthropic thinking is exposed through the standard reasoning
316
+ * stream, but its signature makes it safe and necessary to round-trip.
317
+ * Keep unsigned or cross-provider reasoning on the existing drop path.
318
+ */
319
+ if (contentPart.type === "reasoning" && isAIMessage(message) && message.response_metadata.model_provider === "anthropic" && "reasoning" in contentPart && typeof contentPart.reasoning === "string" && "signature" in contentPart && typeof contentPart.signature === "string" && contentPart.signature !== "") return {
320
+ type: "thinking",
321
+ thinking: contentPart.reasoning,
322
+ signature: contentPart.signature
323
+ };
324
+ /**
296
325
  * Skip non-server malformed blocks that have tool fields mixed with text type.
297
326
  */
298
327
  if ("id" in contentPart && "name" in contentPart && "input" in contentPart && contentPart.type === "text") return null;
@@ -340,12 +369,15 @@ function _formatContent(message) {
340
369
  ..."citations" in contentPart && contentPart.citations != null ? { citations: contentPart.citations } : {},
341
370
  content: searchResultPart.content
342
371
  };
343
- } else if (contentPart.type === "compaction") return {
344
- type: "compaction",
345
- content: contentPart.content,
346
- ...cacheControl != null ? { cache_control: cacheControl } : {}
347
- };
348
- else if (textTypes.some((t) => t === contentPart.type) && "text" in contentPart) return {
372
+ } else if (contentPart.type === "compaction") {
373
+ const compactionPart = contentPart;
374
+ return {
375
+ type: "compaction",
376
+ content: compactionPart.content,
377
+ ..."encrypted_content" in compactionPart ? { encrypted_content: compactionPart.encrypted_content } : {},
378
+ ...cacheControl != null ? { cache_control: cacheControl } : {}
379
+ };
380
+ } else if (textTypes.some((t) => t === contentPart.type) && "text" in contentPart) return {
349
381
  type: "text",
350
382
  text: contentPart.text,
351
383
  ...cacheControl != null ? { cache_control: cacheControl } : {},