@librechat/agents 3.2.45 → 3.2.51

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 (44) hide show
  1. package/dist/cjs/llm/anthropic/index.cjs +4 -3
  2. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  3. package/dist/cjs/llm/anthropic/utils/tools.cjs +2 -1
  4. package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -1
  5. package/dist/cjs/llm/bedrock/cachePoints.cjs +28 -0
  6. package/dist/cjs/llm/bedrock/cachePoints.cjs.map +1 -0
  7. package/dist/cjs/llm/bedrock/index.cjs +10 -1
  8. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  9. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  10. package/dist/cjs/messages/format.cjs +1 -1
  11. package/dist/cjs/messages/format.cjs.map +1 -1
  12. package/dist/esm/llm/anthropic/index.mjs +4 -3
  13. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  14. package/dist/esm/llm/anthropic/utils/tools.mjs +2 -1
  15. package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -1
  16. package/dist/esm/llm/bedrock/cachePoints.mjs +28 -0
  17. package/dist/esm/llm/bedrock/cachePoints.mjs.map +1 -0
  18. package/dist/esm/llm/bedrock/index.mjs +10 -1
  19. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  20. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  21. package/dist/esm/messages/format.mjs +1 -1
  22. package/dist/esm/messages/format.mjs.map +1 -1
  23. package/dist/types/llm/anthropic/utils/tools.d.ts +1 -1
  24. package/dist/types/llm/bedrock/cachePoints.d.ts +9 -0
  25. package/dist/types/messages/format.d.ts +4 -0
  26. package/package.json +16 -21
  27. package/src/llm/anthropic/index.ts +13 -2
  28. package/src/llm/anthropic/inherited-content-utils.spec.ts +244 -0
  29. package/src/llm/anthropic/inherited-stream-events.spec.ts +752 -0
  30. package/src/llm/anthropic/inherited-strict.spec.ts +302 -0
  31. package/src/llm/anthropic/llm.spec.ts +65 -0
  32. package/src/llm/anthropic/utils/tools.ts +7 -1
  33. package/src/llm/bedrock/cachePoints.ts +86 -0
  34. package/src/llm/bedrock/index.ts +9 -0
  35. package/src/llm/bedrock/inherited-cache.spec.ts +144 -0
  36. package/src/llm/bedrock/inherited.spec.ts +724 -0
  37. package/src/llm/google/inherited-stream-events.spec.ts +350 -0
  38. package/src/llm/openai/inherited-deepseek.spec.ts +347 -0
  39. package/src/llm/openai/inherited-xai.spec.ts +416 -0
  40. package/src/llm/openai/llm.spec.ts +1568 -0
  41. package/src/llm/openrouter/index.ts +1 -3
  42. package/src/llm/vertexai/inherited-stream-events.spec.ts +271 -0
  43. package/src/messages/format.ts +6 -1
  44. package/src/messages/formatAgentMessages.test.ts +76 -0
@@ -216,16 +216,17 @@ var CustomAnthropic = class extends _langchain_anthropic.ChatAnthropicMessages {
216
216
  const compactionBetas = getCompactionBetas(contextManagement);
217
217
  const taskBudgetBetas = getTaskBudgetBetas(this.model, mergedOutputConfig);
218
218
  const sharedParams = {
219
- tools: this.formatStructuredToolToAnthropic(options?.tools),
219
+ tools: this.formatStructuredToolToAnthropic(options?.tools, { strict: options?.strict }),
220
220
  tool_choice,
221
- thinking: this.thinking,
221
+ thinking: this.thinkingExplicitlySet ? this.thinking : void 0,
222
222
  context_management: contextManagement,
223
223
  ...this.invocationKwargs,
224
224
  container: callOptions?.container,
225
225
  betas: combineBetas(this.betas, callOptions?.betas, toolBetas, compactionBetas, taskBudgetBetas),
226
226
  output_config: mergedOutputConfig,
227
227
  inference_geo: inferenceGeo,
228
- mcp_servers: callOptions?.mcp_servers
228
+ mcp_servers: callOptions?.mcp_servers,
229
+ cache_control: options?.cache_control
229
230
  };
230
231
  validateInvocationParamCompatibility({
231
232
  model: this.model,
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["AIMessageChunk","ChatAnthropicMessages","handleToolChoice","ChatGenerationChunk","stripUnsupportedAssistantPrefill","_convertMessagesToAnthropicPayload","_makeMessageChunkFromAnthropicEvent"],"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 | 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(options?.tools);\n\n const sharedParams = {\n tools: formattedTools,\n tool_choice,\n thinking: this.thinking,\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 };\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,IAAI,OAAO,QAAQ,YAAY,UAC7B;EAEF,KAAK,MAAM,SAAS,QAAQ,SAAS;GACnC,MAAM,aAAsB;GAC5B,IACE,OAAO,eAAe,YACtB,eAAe,QACf,UAAU,cACV,WAAW,SAAS,cACpB,eAAe,cACf,WAAW,aAAa,QACxB,OAAO,WAAW,cAAc,YAChC,aAAa,WAAW,aACxB,WAAW,UAAU,YAAY,MAEjC,OAAO;EAEX;CACF;CACA,OAAO;AACT;AAEA,SAAS,kBACP,QACS;CACT,OAAO,CAAC,EACN,OAAO,aACN,OAAO,SAAS,SAAS,aAAa,OAAO,SAAS,SAAS;AAEpE;AAEA,SAAS,oBACP,QAMS;CACT,OACE,OAAO,oBAAoB,OAAO,MAC/B,SAAS,KAAK,SAAS,kBAC1B,MAAM;AAEV;AAEA,SAAS,kBAAkB,UAAkD;CAC3E,O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,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.cjs","names":["AIMessageChunk","ChatAnthropicMessages","handleToolChoice","ChatGenerationChunk","stripUnsupportedAssistantPrefill","_convertMessagesToAnthropicPayload","_makeMessageChunkFromAnthropicEvent"],"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"],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AAa,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,8 +1,9 @@
1
1
  //#region src/llm/anthropic/utils/tools.ts
2
2
  function handleToolChoice(toolChoice) {
3
3
  if (toolChoice == null) return;
4
- else if (toolChoice === "any") return { type: "any" };
4
+ else if (toolChoice === "any" || toolChoice === "required") return { type: "any" };
5
5
  else if (toolChoice === "auto") return { type: "auto" };
6
+ else if (toolChoice === "none") return { type: "none" };
6
7
  else if (typeof toolChoice === "string") return {
7
8
  type: "tool",
8
9
  name: toolChoice
@@ -1 +1 @@
1
- {"version":3,"file":"tools.cjs","names":[],"sources":["../../../../../src/llm/anthropic/utils/tools.ts"],"sourcesContent":["import type { Anthropic } from '@anthropic-ai/sdk';\nimport { AnthropicToolChoice } from '../types.js';\n\nexport function handleToolChoice(\n toolChoice?: AnthropicToolChoice\n):\n | Anthropic.Messages.ToolChoiceAuto\n | Anthropic.Messages.ToolChoiceAny\n | Anthropic.Messages.ToolChoiceTool\n | undefined {\n if (toolChoice == null) {\n return undefined;\n } else if (toolChoice === 'any') {\n return {\n type: 'any',\n };\n } else if (toolChoice === 'auto') {\n return {\n type: 'auto',\n };\n } else if (typeof toolChoice === 'string') {\n return {\n type: 'tool',\n name: toolChoice,\n };\n } else {\n return toolChoice;\n }\n}\n"],"mappings":";AAGA,SAAgB,iBACd,YAKY;CACZ,IAAI,cAAc,MAChB;MACK,IAAI,eAAe,OACxB,OAAO,EACL,MAAM,MACR;MACK,IAAI,eAAe,QACxB,OAAO,EACL,MAAM,OACR;MACK,IAAI,OAAO,eAAe,UAC/B,OAAO;EACL,MAAM;EACN,MAAM;CACR;MAEA,OAAO;AAEX"}
1
+ {"version":3,"file":"tools.cjs","names":[],"sources":["../../../../../src/llm/anthropic/utils/tools.ts"],"sourcesContent":["import type { Anthropic } from '@anthropic-ai/sdk';\nimport { AnthropicToolChoice } from '../types.js';\n\nexport function handleToolChoice(\n toolChoice?: AnthropicToolChoice\n):\n | Anthropic.Messages.ToolChoiceAuto\n | Anthropic.Messages.ToolChoiceAny\n | Anthropic.Messages.ToolChoiceTool\n | Anthropic.Messages.ToolChoiceNone\n | undefined {\n if (toolChoice == null) {\n return undefined;\n } else if (toolChoice === 'any' || toolChoice === 'required') {\n // \"required\" is the OpenAI-style alias for forcing tool use.\n return {\n type: 'any',\n };\n } else if (toolChoice === 'auto') {\n return {\n type: 'auto',\n };\n } else if (toolChoice === 'none') {\n return {\n type: 'none',\n };\n } else if (typeof toolChoice === 'string') {\n return {\n type: 'tool',\n name: toolChoice,\n };\n } else {\n return toolChoice;\n }\n}\n"],"mappings":";AAGA,SAAgB,iBACd,YAMY;CACZ,IAAI,cAAc,MAChB;MACK,IAAI,eAAe,SAAS,eAAe,YAEhD,OAAO,EACL,MAAM,MACR;MACK,IAAI,eAAe,QACxB,OAAO,EACL,MAAM,OACR;MACK,IAAI,eAAe,QACxB,OAAO,EACL,MAAM,OACR;MACK,IAAI,OAAO,eAAe,UAC/B,OAAO;EACL,MAAM;EACN,MAAM;CACR;MAEA,OAAO;AAEX"}
@@ -0,0 +1,28 @@
1
+ //#region src/llm/bedrock/cachePoints.ts
2
+ function isConverseCachePoint(block) {
3
+ return Boolean(typeof block === "object" && block !== null && "cachePoint" in block && block.cachePoint && typeof block.cachePoint === "object" && block.cachePoint !== null && "type" in block.cachePoint);
4
+ }
5
+ function createConverseCachePointBlock(cacheControl, isNovaModel) {
6
+ const ttl = !isNovaModel && cacheControl.ttl && cacheControl.ttl !== "5m" ? cacheControl.ttl : void 0;
7
+ return { cachePoint: {
8
+ type: "default",
9
+ ...ttl ? { ttl } : {}
10
+ } };
11
+ }
12
+ function applyCachePointsToConversePayload(fields) {
13
+ const { cacheControl, system, messages, params, modelId } = fields;
14
+ if (!cacheControl) return;
15
+ const isNovaModel = modelId.toLowerCase().includes("amazon.nova");
16
+ const cacheBlock = createConverseCachePointBlock(cacheControl, isNovaModel);
17
+ if (system.length > 0 && !system.some((block) => isConverseCachePoint(block))) system.push(cacheBlock);
18
+ const lastContent = messages[messages.length - 1].content;
19
+ if (Array.isArray(lastContent)) {
20
+ if (!(isNovaModel && lastContent.some((block) => typeof block === "object" && block !== null && ("toolResult" in block || "toolUse" in block))) && !lastContent.some((block) => isConverseCachePoint(block))) lastContent.push(cacheBlock);
21
+ }
22
+ const tools = params?.toolConfig?.tools;
23
+ if (!isNovaModel && Array.isArray(tools) && !tools.some((tool) => isConverseCachePoint(tool))) tools.push(cacheBlock);
24
+ }
25
+ //#endregion
26
+ exports.applyCachePointsToConversePayload = applyCachePointsToConversePayload;
27
+
28
+ //# sourceMappingURL=cachePoints.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cachePoints.cjs","names":[],"sources":["../../../../src/llm/bedrock/cachePoints.ts"],"sourcesContent":["// Vendored from @langchain/aws@1.4.2 (utils/message_inputs.ts) because the\n// upstream `applyCachePointsToConversePayload` export is internal.\nimport type {\n BedrockPromptCacheControl,\n ConverseCommandParams,\n} from '@langchain/aws';\nimport type * as Bedrock from '@aws-sdk/client-bedrock-runtime';\n\nfunction isConverseCachePoint(block: unknown): boolean {\n return Boolean(\n typeof block === 'object' &&\n block !== null &&\n 'cachePoint' in block &&\n block.cachePoint &&\n typeof block.cachePoint === 'object' &&\n block.cachePoint !== null &&\n 'type' in block.cachePoint\n );\n}\n\nfunction createConverseCachePointBlock(\n cacheControl: BedrockPromptCacheControl,\n isNovaModel: boolean\n): { cachePoint: { type: 'default'; ttl?: '1h' } } {\n const ttl =\n !isNovaModel && cacheControl.ttl && cacheControl.ttl !== '5m'\n ? cacheControl.ttl\n : undefined;\n return {\n cachePoint: {\n type: 'default',\n ...(ttl ? { ttl } : {}),\n },\n };\n}\n\nexport function applyCachePointsToConversePayload(fields: {\n cacheControl?: BedrockPromptCacheControl;\n system: Bedrock.SystemContentBlock[];\n messages: Bedrock.Message[];\n params?: Partial<ConverseCommandParams>;\n modelId: string;\n}): void {\n const { cacheControl, system, messages, params, modelId } = fields;\n if (!cacheControl) {\n return;\n }\n\n const isNovaModel = modelId.toLowerCase().includes('amazon.nova');\n const cacheBlock = createConverseCachePointBlock(cacheControl, isNovaModel);\n\n if (\n system.length > 0 &&\n !system.some((block) => isConverseCachePoint(block))\n ) {\n system.push(cacheBlock);\n }\n\n const lastMessage = messages[messages.length - 1];\n const lastContent = lastMessage.content;\n if (Array.isArray(lastContent)) {\n const hasNovaToolBlock =\n isNovaModel &&\n lastContent.some(\n (block) =>\n typeof block === 'object' &&\n block !== null &&\n ('toolResult' in block || 'toolUse' in block)\n );\n if (\n !hasNovaToolBlock &&\n !lastContent.some((block) => isConverseCachePoint(block))\n ) {\n lastContent.push(cacheBlock);\n }\n }\n\n const tools = params?.toolConfig?.tools;\n if (\n !isNovaModel &&\n Array.isArray(tools) &&\n !tools.some((tool) => isConverseCachePoint(tool))\n ) {\n tools.push(cacheBlock as unknown as Bedrock.Tool);\n }\n}\n"],"mappings":";AAQA,SAAS,qBAAqB,OAAyB;CACrD,OAAO,QACL,OAAO,UAAU,YACf,UAAU,QACV,gBAAgB,SAChB,MAAM,cACN,OAAO,MAAM,eAAe,YAC5B,MAAM,eAAe,QACrB,UAAU,MAAM,UACpB;AACF;AAEA,SAAS,8BACP,cACA,aACiD;CACjD,MAAM,MACJ,CAAC,eAAe,aAAa,OAAO,aAAa,QAAQ,OACrD,aAAa,MACb,KAAA;CACN,OAAO,EACL,YAAY;EACV,MAAM;EACN,GAAI,MAAM,EAAE,IAAI,IAAI,CAAC;CACvB,EACF;AACF;AAEA,SAAgB,kCAAkC,QAMzC;CACP,MAAM,EAAE,cAAc,QAAQ,UAAU,QAAQ,YAAY;CAC5D,IAAI,CAAC,cACH;CAGF,MAAM,cAAc,QAAQ,YAAY,CAAC,CAAC,SAAS,aAAa;CAChE,MAAM,aAAa,8BAA8B,cAAc,WAAW;CAE1E,IACE,OAAO,SAAS,KAChB,CAAC,OAAO,MAAM,UAAU,qBAAqB,KAAK,CAAC,GAEnD,OAAO,KAAK,UAAU;CAIxB,MAAM,cADc,SAAS,SAAS,SAAS,EAChB,CAAC;CAChC,IAAI,MAAM,QAAQ,WAAW;MAUzB,EARA,eACA,YAAY,MACT,UACC,OAAO,UAAU,YACjB,UAAU,SACT,gBAAgB,SAAS,aAAa,MAC3C,MAGA,CAAC,YAAY,MAAM,UAAU,qBAAqB,KAAK,CAAC,GAExD,YAAY,KAAK,UAAU;CAAA;CAI/B,MAAM,QAAQ,QAAQ,YAAY;CAClC,IACE,CAAC,eACD,MAAM,QAAQ,KAAK,KACnB,CAAC,MAAM,MAAM,SAAS,qBAAqB,IAAI,CAAC,GAEhD,MAAM,KAAK,UAAqC;AAEpD"}
@@ -3,6 +3,7 @@ const require_toolCache = require("./toolCache.cjs");
3
3
  const require_message_inputs = require("./utils/message_inputs.cjs");
4
4
  const require_message_outputs = require("./utils/message_outputs.cjs");
5
5
  require("./utils/index.cjs");
6
+ const require_cachePoints = require("./cachePoints.cjs");
6
7
  let _langchain_core_messages = require("@langchain/core/messages");
7
8
  let _langchain_core_outputs = require("@langchain/core/outputs");
8
9
  let _langchain_aws = require("@langchain/aws");
@@ -111,8 +112,16 @@ var CustomChatBedrockConverse = class extends _langchain_aws.ChatBedrockConverse
111
112
  const params = this.invocationParams(options);
112
113
  let { streamUsage } = this;
113
114
  if (options.streamUsage !== void 0) streamUsage = options.streamUsage;
115
+ const modelId = this.getModelId();
116
+ require_cachePoints.applyCachePointsToConversePayload({
117
+ cacheControl: options.cache_control,
118
+ system: converseSystem,
119
+ messages: converseMessages,
120
+ params,
121
+ modelId
122
+ });
114
123
  const command = new _aws_sdk_client_bedrock_runtime.ConverseStreamCommand({
115
- modelId: this.getModelId(),
124
+ modelId,
116
125
  messages: converseMessages,
117
126
  system: converseSystem,
118
127
  ...params
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["ChatBedrockConverse","supportsBedrockToolCache","insertBedrockToolCachePoint","resolveBedrockPromptCacheTtl","convertToConverseMessages","ConverseStreamCommand","handleConverseStreamContentBlockStart","handleConverseStreamContentBlockDelta","handleConverseStreamMetadata","createConverseToolUseStopChunk","ChatGenerationChunk","AIMessageChunk"],"sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes content block merging for\n * streaming responses and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock's `@langchain/aws` library does not include an `index` property on content\n * blocks (unlike Anthropic/OpenAI), which causes LangChain's `_mergeLists` to append\n * each streaming chunk as a separate array entry instead of merging by index.\n *\n * This wrapper takes full ownership of the stream by directly interfacing with the\n * AWS SDK client (`this.client`) and using custom handlers from `./utils/` that\n * include `contentBlockIndex` in response_metadata for every delta type. It then\n * promotes `contentBlockIndex` to an `index` property on each content block\n * (mirroring Anthropic's pattern) and strips it from metadata to avoid\n * `_mergeDicts` conflicts.\n *\n * When multiple content block types are present (e.g. reasoning + text), text deltas\n * are promoted from strings to array form with `index` so they merge correctly once\n * the accumulated content is already an array.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport {\n ConverseStreamCommand,\n type GuardrailConfiguration,\n type GuardrailStreamConfiguration,\n} from '@aws-sdk/client-bedrock-runtime';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, ResponseMetadata } from '@langchain/core/messages';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport {\n convertToConverseMessages,\n createConverseToolUseStopChunk,\n handleConverseStreamContentBlockStart,\n handleConverseStreamContentBlockDelta,\n handleConverseStreamMetadata,\n} from './utils';\nimport {\n resolveBedrockPromptCacheTtl,\n supportsBedrockToolCache,\n type PromptCacheTtl,\n} from '@/messages/cache';\nimport { insertBedrockToolCachePoint } from './toolCache';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\nexport type CustomGuardrailConfiguration = GuardrailConfiguration &\n Pick<GuardrailStreamConfiguration, 'streamProcessingMode'>;\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Enables Bedrock prompt cache checkpoints for message and tool prefixes.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL. Defaults to `'1h'` (extended cache) when\n * `promptCache` is enabled; set `'5m'` for the legacy 5-minute behavior.\n * Bedrock models that don't support the 1-hour TTL downgrade to 5m\n * server-side (verified on Sonnet/Opus 4.6), so the default is safe to leave\n * on; use `'5m'` for any model that rejects it.\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Guardrail configuration for Converse and ConverseStream invocations.\n * `streamProcessingMode` is only used by ConverseStream.\n */\n guardrailConfig?: CustomGuardrailConfiguration;\n\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n guardrailConfig?: CustomGuardrailConfiguration;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n /**\n * Whether to insert Bedrock prompt cache checkpoints when available.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL (`'5m'` legacy or `'1h'` extended cache).\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n /**\n * The configured model id, captured at construction so it survives the\n * temporary `this.model` swap to an application-inference-profile ARN during\n * generation. Used to gate the Bedrock tool cache point to Claude models\n * (see {@link supportsBedrockToolCache}).\n */\n private readonly cacheModelId: string;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.promptCache = fields?.promptCache;\n this.promptCacheTtl = fields?.promptCacheTtl;\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n // `super(fields)` initializes `this.model` to LangChain's default Claude\n // model when `fields.model` is omitted, so fall back to it rather than ''\n // (which would treat the default Claude model as tool-cache-unsupported).\n this.cacheModelId = fields?.model ?? this.model;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n const toolConfig =\n this.promptCache === true && supportsBedrockToolCache(this.cacheModelId)\n ? insertBedrockToolCachePoint(\n baseParams.toolConfig,\n true,\n resolveBedrockPromptCacheTtl(this.promptCacheTtl, this.cacheModelId)\n )\n : baseParams.toolConfig;\n\n /** Service tier from options or fall back to class-level setting */\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n toolConfig,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n * Uses the same model-swapping pattern as streaming for consistency.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n return await super._generateNonStreaming(messages, options, runManager);\n } finally {\n this.model = originalModel;\n }\n }\n\n /**\n * Own the stream end-to-end so we have direct access to every\n * `contentBlockDelta.contentBlockIndex` from the AWS SDK.\n *\n * This replaces the parent's implementation which strips contentBlockIndex\n * from text and reasoning deltas, making it impossible to merge correctly.\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n let { streamUsage } = this;\n if ((options as Record<string, unknown>).streamUsage !== undefined) {\n streamUsage = (options as Record<string, unknown>).streamUsage as boolean;\n }\n\n const modelId = this.getModelId();\n\n const command = new ConverseStreamCommand({\n modelId,\n messages: converseMessages,\n system: converseSystem,\n ...(params as Record<string, unknown>),\n });\n\n const response = await this.client.send(command, {\n abortSignal: options.signal,\n });\n\n if (!response.stream) {\n return;\n }\n\n const seenBlockIndices = new Set<number>();\n const toolUseBlockIndices = new Set<number>();\n /**\n * Guardrails can reject an already-streamed toolUse block at\n * `messageStop` (`guardrail_intervened`), after `contentBlockStop` has\n * passed. Only emit eager-execution seals when no guardrails are\n * configured, so a later intervention can't race an eagerly started tool.\n */\n const sealToolUseOnStop =\n options.guardrailConfig == null && this.guardrailConfig == null;\n\n for await (const event of response.stream) {\n if (event.contentBlockStart != null) {\n const startChunk = handleConverseStreamContentBlockStart(\n event.contentBlockStart\n );\n if (startChunk != null) {\n const idx = event.contentBlockStart.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n if (event.contentBlockStart.start?.toolUse != null) {\n toolUseBlockIndices.add(idx);\n }\n }\n yield this.enrichChunk(startChunk, seenBlockIndices);\n\n // Registered stream handlers receive chunks through callback\n // events, not the yielded generator — dispatch the start chunk so\n // they see the tool call's id/name (eager chunk state needs both).\n await runManager?.handleLLMNewToken(\n startChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: startChunk }\n );\n }\n } else if (event.contentBlockDelta != null) {\n const deltaChunk = handleConverseStreamContentBlockDelta(\n event.contentBlockDelta\n );\n\n const idx = event.contentBlockDelta.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n }\n\n yield this.enrichChunk(deltaChunk, seenBlockIndices);\n\n await runManager?.handleLLMNewToken(\n deltaChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: deltaChunk }\n );\n } else if (event.metadata != null) {\n yield handleConverseStreamMetadata(event.metadata, { streamUsage });\n } else if (event.contentBlockStop != null) {\n const stopIdx = event.contentBlockStop.contentBlockIndex;\n if (stopIdx != null) {\n seenBlockIndices.add(stopIdx);\n if (sealToolUseOnStop && toolUseBlockIndices.has(stopIdx)) {\n // Converse guarantees the block's input is complete at stop, so\n // emit an explicit seal chunk for eager tool execution — through\n // the callback path too, for registered stream handlers.\n const sealChunk = createConverseToolUseStopChunk(stopIdx);\n yield sealChunk;\n await runManager?.handleLLMNewToken(\n sealChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: sealChunk }\n );\n }\n }\n } else {\n yield new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: '',\n response_metadata: { ...event } as ResponseMetadata,\n }),\n });\n }\n }\n }\n\n /**\n * Inject `index` on content blocks for proper merge behaviour, then strip\n * `contentBlockIndex` from response_metadata to prevent `_mergeDicts` conflicts.\n *\n * Text string content is promoted to array form only when the stream contains\n * multiple content block indices (e.g. reasoning at index 0, text at index 1),\n * ensuring text merges correctly with the already-array accumulated content.\n */\n private enrichChunk(\n chunk: ChatGenerationChunk,\n seenBlockIndices: Set<number>\n ): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const blockIndex = this.extractContentBlockIndex(metadata);\n const hasMetadataIndex = blockIndex != null;\n\n let content: AIMessageChunk['content'] = message.content;\n let contentModified = false;\n\n if (Array.isArray(content) && blockIndex != null) {\n content = content.map((block) =>\n typeof block === 'object' && !('index' in block)\n ? { ...block, index: blockIndex }\n : block\n );\n contentModified = true;\n } else if (\n typeof content === 'string' &&\n content !== '' &&\n blockIndex != null &&\n seenBlockIndices.size > 1\n ) {\n content = [{ type: 'text', text: content, index: blockIndex }];\n contentModified = true;\n }\n\n if (!contentModified && !hasMetadataIndex) {\n return chunk;\n }\n\n const cleanedMetadata = hasMetadataIndex\n ? (this.removeContentBlockIndex(metadata) as Record<string, unknown>)\n : metadata;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n content,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Extract `contentBlockIndex` from the top level of response_metadata.\n * Our custom handlers always place it at the top level.\n */\n private extractContentBlockIndex(\n metadata: Record<string, unknown>\n ): number | undefined {\n if (\n 'contentBlockIndex' in metadata &&\n typeof metadata.contentBlockIndex === 'number'\n ) {\n return metadata.contentBlockIndex;\n }\n return undefined;\n }\n\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuHA,IAAa,4BAAb,cAA+CA,eAAAA,oBAAoB;;;;CAIjE;;;;CAKA;;;;CAKA;;;;CAKA;;;;;;;CAQA;CAEA,YAAY,QAAyC;EACnD,MAAM,MAAM;EACZ,KAAK,cAAc,QAAQ;EAC3B,KAAK,iBAAiB,QAAQ;EAC9B,KAAK,8BAA8B,QAAQ;EAC3C,KAAK,cAAc,QAAQ;EAI3B,KAAK,eAAe,QAAQ,SAAS,KAAK;CAC5C;CAEA,OAAO,UAAkB;EACvB,OAAO;CACT;;;;;CAMA,aAA+B;EAC7B,OAAO,KAA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AAM,CAAC,KAAK,UAAU,OAAO,QAAQ,GAAG,GAC3C,IAAI,QAAQ,qBACV,QAAQ,OAAO,KAAK,wBAAwB,KAAK;GAGrD,OAAO;EACT;EAEA,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"index.cjs","names":["ChatBedrockConverse","supportsBedrockToolCache","insertBedrockToolCachePoint","resolveBedrockPromptCacheTtl","convertToConverseMessages","ConverseStreamCommand","handleConverseStreamContentBlockStart","handleConverseStreamContentBlockDelta","handleConverseStreamMetadata","createConverseToolUseStopChunk","ChatGenerationChunk","AIMessageChunk"],"sources":["../../../../src/llm/bedrock/index.ts"],"sourcesContent":["/**\n * Optimized ChatBedrockConverse wrapper that fixes content block merging for\n * streaming responses and adds support for latest @langchain/aws features:\n *\n * - Application Inference Profiles (PR #9129)\n * - Service Tiers (Priority/Standard/Flex) (PR #9785) - requires AWS SDK 3.966.0+\n *\n * Bedrock's `@langchain/aws` library does not include an `index` property on content\n * blocks (unlike Anthropic/OpenAI), which causes LangChain's `_mergeLists` to append\n * each streaming chunk as a separate array entry instead of merging by index.\n *\n * This wrapper takes full ownership of the stream by directly interfacing with the\n * AWS SDK client (`this.client`) and using custom handlers from `./utils/` that\n * include `contentBlockIndex` in response_metadata for every delta type. It then\n * promotes `contentBlockIndex` to an `index` property on each content block\n * (mirroring Anthropic's pattern) and strips it from metadata to avoid\n * `_mergeDicts` conflicts.\n *\n * When multiple content block types are present (e.g. reasoning + text), text deltas\n * are promoted from strings to array form with `index` so they merge correctly once\n * the accumulated content is already an array.\n */\n\nimport { ChatBedrockConverse } from '@langchain/aws';\nimport { AIMessageChunk } from '@langchain/core/messages';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport {\n ConverseStreamCommand,\n type GuardrailConfiguration,\n type GuardrailStreamConfiguration,\n} from '@aws-sdk/client-bedrock-runtime';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, ResponseMetadata } from '@langchain/core/messages';\nimport type { ChatBedrockConverseInput } from '@langchain/aws';\nimport {\n convertToConverseMessages,\n createConverseToolUseStopChunk,\n handleConverseStreamContentBlockStart,\n handleConverseStreamContentBlockDelta,\n handleConverseStreamMetadata,\n} from './utils';\nimport {\n resolveBedrockPromptCacheTtl,\n supportsBedrockToolCache,\n type PromptCacheTtl,\n} from '@/messages/cache';\nimport { applyCachePointsToConversePayload } from './cachePoints';\nimport { insertBedrockToolCachePoint } from './toolCache';\n\n/**\n * Service tier type for Bedrock invocations.\n * Requires AWS SDK >= 3.966.0 to actually work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\nexport type ServiceTierType = 'priority' | 'default' | 'flex' | 'reserved';\n\nexport type CustomGuardrailConfiguration = GuardrailConfiguration &\n Pick<GuardrailStreamConfiguration, 'streamProcessingMode'>;\n\n/**\n * Extended input interface with additional features:\n * - applicationInferenceProfile: Use an inference profile ARN instead of model ID\n * - serviceTier: Specify service tier (Priority, Standard, Flex, Reserved)\n */\nexport interface CustomChatBedrockConverseInput\n extends ChatBedrockConverseInput {\n /**\n * Enables Bedrock prompt cache checkpoints for message and tool prefixes.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL. Defaults to `'1h'` (extended cache) when\n * `promptCache` is enabled; set `'5m'` for the legacy 5-minute behavior.\n * Bedrock models that don't support the 1-hour TTL downgrade to 5m\n * server-side (verified on Sonnet/Opus 4.6), so the default is safe to leave\n * on; use `'5m'` for any model that rejects it.\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Guardrail configuration for Converse and ConverseStream invocations.\n * `streamProcessingMode` is only used by ConverseStream.\n */\n guardrailConfig?: CustomGuardrailConfiguration;\n\n /**\n * Application Inference Profile ARN to use for the model.\n * For example, \"arn:aws:bedrock:eu-west-1:123456789102:application-inference-profile/fm16bt65tzgx\"\n * When provided, this ARN will be used for the actual inference calls instead of the model ID.\n * Must still provide `model` as normal modelId to benefit from all the metadata.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-create.html\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n * Specifies the processing tier type used for serving the request.\n * Supported values are 'priority', 'default', 'flex', and 'reserved'.\n *\n * - 'priority': Prioritized processing for lower latency\n * - 'default': Standard processing tier\n * - 'flex': Flexible processing tier with lower cost\n * - 'reserved': Reserved capacity for consistent performance\n *\n * If not provided, AWS uses the default tier.\n * Note: Requires AWS SDK >= 3.966.0 to work.\n * @see https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html\n */\n serviceTier?: ServiceTierType;\n}\n\n/**\n * Extended call options with serviceTier override support.\n */\nexport interface CustomChatBedrockConverseCallOptions {\n serviceTier?: ServiceTierType;\n guardrailConfig?: CustomGuardrailConfiguration;\n}\n\nexport class CustomChatBedrockConverse extends ChatBedrockConverse {\n /**\n * Whether to insert Bedrock prompt cache checkpoints when available.\n */\n promptCache?: boolean;\n\n /**\n * Prompt-cache checkpoint TTL (`'5m'` legacy or `'1h'` extended cache).\n */\n promptCacheTtl?: PromptCacheTtl;\n\n /**\n * Application Inference Profile ARN to use instead of model ID.\n */\n applicationInferenceProfile?: string;\n\n /**\n * Service tier for model invocation.\n */\n serviceTier?: ServiceTierType;\n\n /**\n * The configured model id, captured at construction so it survives the\n * temporary `this.model` swap to an application-inference-profile ARN during\n * generation. Used to gate the Bedrock tool cache point to Claude models\n * (see {@link supportsBedrockToolCache}).\n */\n private readonly cacheModelId: string;\n\n constructor(fields?: CustomChatBedrockConverseInput) {\n super(fields);\n this.promptCache = fields?.promptCache;\n this.promptCacheTtl = fields?.promptCacheTtl;\n this.applicationInferenceProfile = fields?.applicationInferenceProfile;\n this.serviceTier = fields?.serviceTier;\n // `super(fields)` initializes `this.model` to LangChain's default Claude\n // model when `fields.model` is omitted, so fall back to it rather than ''\n // (which would treat the default Claude model as tool-cache-unsupported).\n this.cacheModelId = fields?.model ?? this.model;\n }\n\n static lc_name(): string {\n return 'LibreChatBedrockConverse';\n }\n\n /**\n * Get the model ID to use for API calls.\n * Returns applicationInferenceProfile if set, otherwise returns this.model.\n */\n protected getModelId(): string {\n return this.applicationInferenceProfile ?? this.model;\n }\n\n /**\n * Override invocationParams to add serviceTier support.\n */\n override invocationParams(\n options?: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions\n ): ReturnType<ChatBedrockConverse['invocationParams']> & {\n serviceTier?: { type: ServiceTierType };\n } {\n const baseParams = super.invocationParams(options);\n const toolConfig =\n this.promptCache === true && supportsBedrockToolCache(this.cacheModelId)\n ? insertBedrockToolCachePoint(\n baseParams.toolConfig,\n true,\n resolveBedrockPromptCacheTtl(this.promptCacheTtl, this.cacheModelId)\n )\n : baseParams.toolConfig;\n\n /** Service tier from options or fall back to class-level setting */\n const serviceTierType = options?.serviceTier ?? this.serviceTier;\n\n return {\n ...baseParams,\n toolConfig,\n serviceTier: serviceTierType ? { type: serviceTierType } : undefined,\n };\n }\n\n /**\n * Override _generateNonStreaming to use applicationInferenceProfile as modelId.\n * Uses the same model-swapping pattern as streaming for consistency.\n */\n override async _generateNonStreaming(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n const originalModel = this.model;\n if (\n this.applicationInferenceProfile != null &&\n this.applicationInferenceProfile !== ''\n ) {\n this.model = this.applicationInferenceProfile;\n }\n\n try {\n return await super._generateNonStreaming(messages, options, runManager);\n } finally {\n this.model = originalModel;\n }\n }\n\n /**\n * Own the stream end-to-end so we have direct access to every\n * `contentBlockDelta.contentBlockIndex` from the AWS SDK.\n *\n * This replaces the parent's implementation which strips contentBlockIndex\n * from text and reasoning deltas, making it impossible to merge correctly.\n */\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'] & CustomChatBedrockConverseCallOptions,\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const { converseMessages, converseSystem } =\n convertToConverseMessages(messages);\n const params = this.invocationParams(options);\n\n let { streamUsage } = this;\n if ((options as Record<string, unknown>).streamUsage !== undefined) {\n streamUsage = (options as Record<string, unknown>).streamUsage as boolean;\n }\n\n const modelId = this.getModelId();\n\n applyCachePointsToConversePayload({\n cacheControl: options.cache_control,\n system: converseSystem,\n messages: converseMessages,\n params,\n modelId,\n });\n\n const command = new ConverseStreamCommand({\n modelId,\n messages: converseMessages,\n system: converseSystem,\n ...(params as Record<string, unknown>),\n });\n\n const response = await this.client.send(command, {\n abortSignal: options.signal,\n });\n\n if (!response.stream) {\n return;\n }\n\n const seenBlockIndices = new Set<number>();\n const toolUseBlockIndices = new Set<number>();\n /**\n * Guardrails can reject an already-streamed toolUse block at\n * `messageStop` (`guardrail_intervened`), after `contentBlockStop` has\n * passed. Only emit eager-execution seals when no guardrails are\n * configured, so a later intervention can't race an eagerly started tool.\n */\n const sealToolUseOnStop =\n options.guardrailConfig == null && this.guardrailConfig == null;\n\n for await (const event of response.stream) {\n if (event.contentBlockStart != null) {\n const startChunk = handleConverseStreamContentBlockStart(\n event.contentBlockStart\n );\n if (startChunk != null) {\n const idx = event.contentBlockStart.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n if (event.contentBlockStart.start?.toolUse != null) {\n toolUseBlockIndices.add(idx);\n }\n }\n yield this.enrichChunk(startChunk, seenBlockIndices);\n\n // Registered stream handlers receive chunks through callback\n // events, not the yielded generator — dispatch the start chunk so\n // they see the tool call's id/name (eager chunk state needs both).\n await runManager?.handleLLMNewToken(\n startChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: startChunk }\n );\n }\n } else if (event.contentBlockDelta != null) {\n const deltaChunk = handleConverseStreamContentBlockDelta(\n event.contentBlockDelta\n );\n\n const idx = event.contentBlockDelta.contentBlockIndex;\n if (idx != null) {\n seenBlockIndices.add(idx);\n }\n\n yield this.enrichChunk(deltaChunk, seenBlockIndices);\n\n await runManager?.handleLLMNewToken(\n deltaChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: deltaChunk }\n );\n } else if (event.metadata != null) {\n yield handleConverseStreamMetadata(event.metadata, { streamUsage });\n } else if (event.contentBlockStop != null) {\n const stopIdx = event.contentBlockStop.contentBlockIndex;\n if (stopIdx != null) {\n seenBlockIndices.add(stopIdx);\n if (sealToolUseOnStop && toolUseBlockIndices.has(stopIdx)) {\n // Converse guarantees the block's input is complete at stop, so\n // emit an explicit seal chunk for eager tool execution — through\n // the callback path too, for registered stream handlers.\n const sealChunk = createConverseToolUseStopChunk(stopIdx);\n yield sealChunk;\n await runManager?.handleLLMNewToken(\n sealChunk.text,\n undefined,\n undefined,\n undefined,\n undefined,\n { chunk: sealChunk }\n );\n }\n }\n } else {\n yield new ChatGenerationChunk({\n text: '',\n message: new AIMessageChunk({\n content: '',\n response_metadata: { ...event } as ResponseMetadata,\n }),\n });\n }\n }\n }\n\n /**\n * Inject `index` on content blocks for proper merge behaviour, then strip\n * `contentBlockIndex` from response_metadata to prevent `_mergeDicts` conflicts.\n *\n * Text string content is promoted to array form only when the stream contains\n * multiple content block indices (e.g. reasoning at index 0, text at index 1),\n * ensuring text merges correctly with the already-array accumulated content.\n */\n private enrichChunk(\n chunk: ChatGenerationChunk,\n seenBlockIndices: Set<number>\n ): ChatGenerationChunk {\n const message = chunk.message;\n if (!(message instanceof AIMessageChunk)) {\n return chunk;\n }\n\n const metadata = message.response_metadata as Record<string, unknown>;\n const blockIndex = this.extractContentBlockIndex(metadata);\n const hasMetadataIndex = blockIndex != null;\n\n let content: AIMessageChunk['content'] = message.content;\n let contentModified = false;\n\n if (Array.isArray(content) && blockIndex != null) {\n content = content.map((block) =>\n typeof block === 'object' && !('index' in block)\n ? { ...block, index: blockIndex }\n : block\n );\n contentModified = true;\n } else if (\n typeof content === 'string' &&\n content !== '' &&\n blockIndex != null &&\n seenBlockIndices.size > 1\n ) {\n content = [{ type: 'text', text: content, index: blockIndex }];\n contentModified = true;\n }\n\n if (!contentModified && !hasMetadataIndex) {\n return chunk;\n }\n\n const cleanedMetadata = hasMetadataIndex\n ? (this.removeContentBlockIndex(metadata) as Record<string, unknown>)\n : metadata;\n\n return new ChatGenerationChunk({\n text: chunk.text,\n message: new AIMessageChunk({\n ...message,\n content,\n response_metadata: cleanedMetadata,\n }),\n generationInfo: chunk.generationInfo,\n });\n }\n\n /**\n * Extract `contentBlockIndex` from the top level of response_metadata.\n * Our custom handlers always place it at the top level.\n */\n private extractContentBlockIndex(\n metadata: Record<string, unknown>\n ): number | undefined {\n if (\n 'contentBlockIndex' in metadata &&\n typeof metadata.contentBlockIndex === 'number'\n ) {\n return metadata.contentBlockIndex;\n }\n return undefined;\n }\n\n private removeContentBlockIndex(obj: unknown): unknown {\n if (obj === null || obj === undefined) {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => this.removeContentBlockIndex(item));\n }\n\n if (typeof obj === 'object') {\n const cleaned: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(obj)) {\n if (key !== 'contentBlockIndex') {\n cleaned[key] = this.removeContentBlockIndex(value);\n }\n }\n return cleaned;\n }\n\n return obj;\n }\n}\n\nexport type { ChatBedrockConverseInput };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwHA,IAAa,4BAAb,cAA+CA,eAAAA,oBAAoB;;;;CAIjE;;;;CAKA;;;;CAKA;;;;CAKA;;;;;;;CAQA;CAEA,YAAY,QAAyC;EACnD,MAAM,MAAM;EACZ,KAAK,cAAc,QAAQ;EAC3B,KAAK,iBAAiB,QAAQ;EAC9B,KAAK,8BAA8B,QAAQ;EAC3C,KAAK,cAAc,QAAQ;EAI3B,KAAK,eAAe,QAAQ,SAAS,KAAK;CAC5C;CAEA,OAAO,UAAkB;EACvB,OAAO;CACT;;;;;CAMA,aAA+B;EAC7B,OAAO,KAAK,+BAA+B,KAAK;CAClD;;;;CAKA,iBACE,SAGA;EACA,MAAM,aAAa,MAAM,iBAAiB,OAAO;E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GAC1C,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,GAAG,GAC3C,IAAI,QAAQ,qBACV,QAAQ,OAAO,KAAK,wBAAwB,KAAK;GAGrD,OAAO;EACT;EAEA,OAAO;CACT;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["ChatOpenAI","emitStreamChunkCallback"],"sources":["../../../../src/llm/openrouter/index.ts"],"sourcesContent":["import type {\n ChatOpenAICallOptions,\n OpenAIChatInput,\n OpenAIClient,\n} from '@langchain/openai';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport type { PromptCacheTtl } from '@/messages/cache';\nimport { ChatOpenAI, emitStreamChunkCallback } from '@/llm/openai';\n\nexport type OpenRouterReasoningEffort =\n | 'xhigh'\n | 'high'\n | 'medium'\n | 'low'\n | 'minimal'\n | 'none';\n\nexport interface OpenRouterReasoning {\n effort?: OpenRouterReasoningEffort;\n max_tokens?: number;\n exclude?: boolean;\n enabled?: boolean;\n}\n\nexport interface ChatOpenRouterCallOptions\n extends Omit<ChatOpenAICallOptions, 'reasoning'> {\n /** @deprecated Use `reasoning` object instead */\n include_reasoning?: boolean;\n reasoning?: OpenRouterReasoning;\n modelKwargs?: OpenAIChatInput['modelKwargs'];\n promptCache?: boolean;\n /**\n * Prompt-cache breakpoint TTL. Defaults to `'1h'` (extended cache) when\n * `promptCache` is enabled; set `'5m'` for the legacy 5-minute behavior.\n * OpenRouter forwards this to Claude upstreams (Anthropic / Bedrock / Vertex),\n * which downgrade to 5m where the extended TTL isn't supported.\n */\n promptCacheTtl?: PromptCacheTtl;\n}\n\nexport type ChatOpenRouterInput = Partial<\n ChatOpenRouterCallOptions & OpenAIChatInput\n>;\n\n/** invocationParams return type extended with OpenRouter reasoning */\nexport type OpenRouterInvocationParams = Omit<\n OpenAIClient.Chat.ChatCompletionCreateParams,\n 'messages'\n> & {\n reasoning?: OpenRouterReasoning;\n};\n\ntype InvocationParamsExtra = {\n streaming?: boolean;\n};\n\ninterface OpenRouterReasoningTextDetail {\n type: 'reasoning.text';\n text?: string;\n format?: string;\n index?: number;\n}\n\ninterface OpenRouterReasoningEncryptedDetail {\n type: 'reasoning.encrypted';\n id?: string;\n data?: string;\n format?: string;\n index?: number;\n}\n\ntype OpenRouterReasoningDetail =\n | OpenRouterReasoningTextDetail\n | OpenRouterReasoningEncryptedDetail;\n\nfunction isReasoningTextDetail(\n value: unknown\n): value is OpenRouterReasoningTextDetail {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n value.type === 'reasoning.text'\n );\n}\n\nfunction isReasoningEncryptedDetail(\n value: unknown\n): value is OpenRouterReasoningEncryptedDetail {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n value.type === 'reasoning.encrypted'\n );\n}\n\nfunction getReasoningDetails(value: unknown): OpenRouterReasoningDetail[] {\n if (!Array.isArray(value)) {\n return [];\n }\n return value.filter(\n (detail): detail is OpenRouterReasoningDetail =>\n isReasoningTextDetail(detail) || isReasoningEncryptedDetail(detail)\n );\n}\n\nexport class ChatOpenRouter extends ChatOpenAI {\n private openRouterReasoning?: OpenRouterReasoning;\n /** @deprecated Use `reasoning` object instead */\n private includeReasoning?: boolean;\n\n constructor(_fields: ChatOpenRouterInput) {\n const fieldsWithoutPromptCache: ChatOpenRouterInput = { ..._fields };\n delete fieldsWithoutPromptCache.promptCache;\n delete fieldsWithoutPromptCache.promptCacheTtl;\n\n const {\n include_reasoning,\n reasoning: openRouterReasoning,\n modelKwargs = {},\n ...fields\n } = fieldsWithoutPromptCache;\n\n // Extract reasoning from modelKwargs if provided there (e.g., from LLMConfig)\n const { reasoning: mkReasoning, ...restModelKwargs } = modelKwargs as {\n reasoning?: OpenRouterReasoning;\n } & Record<string, unknown>;\n const mergedReasoning =\n mkReasoning != null || openRouterReasoning != null\n ? {\n ...mkReasoning,\n ...openRouterReasoning,\n }\n : undefined;\n const runtimeReasoning =\n mergedReasoning ??\n (include_reasoning === true ? { enabled: true } : undefined);\n const parentModelKwargs =\n runtimeReasoning == null\n ? restModelKwargs\n : { ...restModelKwargs, reasoning: runtimeReasoning };\n\n super({\n ...fields,\n modelKwargs: parentModelKwargs,\n includeReasoningDetails: true,\n convertReasoningDetailsToContent: true,\n });\n\n // Merge reasoning config: modelKwargs.reasoning < constructor reasoning\n if (mergedReasoning != null) {\n this.openRouterReasoning = mergedReasoning;\n }\n\n this.includeReasoning = include_reasoning;\n }\n static lc_name(): 'LibreChatOpenRouter' {\n return 'LibreChatOpenRouter';\n }\n\n // @ts-expect-error - OpenRouter reasoning extends OpenAI Reasoning with additional\n // effort levels ('xhigh' | 'none' | 'minimal') not in ReasoningEffort.\n // The parent's generic conditional return type cannot be widened in an override.\n override invocationParams(\n options?: this['ParsedCallOptions'],\n extra?: InvocationParamsExtra\n ): OpenRouterInvocationParams {\n type MutableParams = Omit<\n OpenAIClient.Chat.ChatCompletionCreateParams,\n 'messages'\n > & { reasoning_effort?: string; reasoning?: OpenRouterReasoning };\n\n const optionsWithDefaults = this._combineCallOptions(options);\n const params = (\n this._useResponsesApi(options)\n ? this.responses.invocationParams(optionsWithDefaults)\n : this.completions.invocationParams(optionsWithDefaults, extra)\n ) as MutableParams;\n\n // Remove the OpenAI-native reasoning_effort that the parent sets;\n // OpenRouter uses a `reasoning` object instead\n delete params.reasoning_effort;\n\n // Build the OpenRouter reasoning config\n const reasoning = this.buildOpenRouterReasoning(optionsWithDefaults);\n if (reasoning != null) {\n params.reasoning = reasoning;\n } else {\n delete params.reasoning;\n }\n\n return params;\n }\n\n private buildOpenRouterReasoning(\n options?: this['ParsedCallOptions']\n ): OpenRouterReasoning | undefined {\n let reasoning: OpenRouterReasoning | undefined;\n\n // 1. Instance-level reasoning config (from constructor)\n if (this.openRouterReasoning != null) {\n reasoning = { ...this.openRouterReasoning };\n }\n\n // 2. LangChain-style reasoning params (from parent's `this.reasoning`)\n const lcReasoning = this.getReasoningParams(options);\n if (lcReasoning?.effort != null) {\n reasoning = {\n ...reasoning,\n effort: lcReasoning.effort as OpenRouterReasoningEffort,\n };\n }\n\n // 3. Call-level reasoning override\n const callReasoning = (options as ChatOpenRouterCallOptions | undefined)\n ?.reasoning;\n if (callReasoning != null) {\n reasoning = { ...reasoning, ...callReasoning };\n }\n\n // 4. Legacy include_reasoning backward compatibility\n if (reasoning == null && this.includeReasoning === true) {\n reasoning = { enabled: true };\n }\n\n return reasoning;\n }\n\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const reasoningTextByIndex = new Map<\n number,\n OpenRouterReasoningTextDetail\n >();\n const reasoningEncryptedById = new Map<\n string,\n OpenRouterReasoningEncryptedDetail\n >();\n\n for await (const generationChunk of super._streamResponseChunks(\n messages,\n options,\n undefined\n )) {\n let currentReasoningText = '';\n const reasoningDetails = getReasoningDetails(\n generationChunk.message.additional_kwargs.reasoning_details\n );\n\n for (const detail of reasoningDetails) {\n if (detail.type === 'reasoning.text') {\n currentReasoningText += detail.text ?? '';\n const index = detail.index ?? 0;\n const existing = reasoningTextByIndex.get(index);\n if (existing != null) {\n existing.text = `${existing.text ?? ''}${detail.text ?? ''}`;\n continue;\n }\n reasoningTextByIndex.set(index, {\n ...detail,\n text: detail.text ?? '',\n });\n continue;\n }\n if (detail.id != null) {\n reasoningEncryptedById.set(detail.id, { ...detail });\n }\n }\n\n if (\n currentReasoningText.length > 0 &&\n generationChunk.message.additional_kwargs.reasoning == null\n ) {\n generationChunk.message.additional_kwargs.reasoning =\n currentReasoningText;\n }\n\n if (generationChunk.generationInfo?.finish_reason != null) {\n const finalReasoningDetails = [\n ...reasoningTextByIndex.values(),\n ...reasoningEncryptedById.values(),\n ];\n if (finalReasoningDetails.length > 0) {\n generationChunk.message.additional_kwargs.reasoning_details =\n finalReasoningDetails;\n } else {\n delete generationChunk.message.additional_kwargs.reasoning_details;\n }\n await emitStreamChunkCallback(generationChunk, runManager);\n yield generationChunk;\n continue;\n }\n\n delete generationChunk.message.additional_kwargs.reasoning_details;\n await emitStreamChunkCallback(generationChunk, runManager);\n yield generationChunk;\n }\n }\n}\n"],"mappings":";;AA6EA,SAAS,sBACP,OACwC;CACxC,OACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,MAAM,SAAS;AAEnB;AAEA,SAAS,2BACP,OAC6C;CAC7C,OACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,MAAM,SAAS;AAEnB;AAEA,SAAS,oBAAoB,OAA6C;CACxE,IAAI,CAAC,MAAM,QAAQ,KAAK,GACtB,OAAO,CAAC;CAEV,OAAO,MAAM,QACV,WACC,sBAAsB,MAAM,KAAK,2BAA2B,MAAM,CACtE;AACF;AAEA,IAAa,iBAAb,cAAoCA,cAAAA,WAAW;CAC7C;;CAEA;CAEA,YAAY,SAA8B;EACxC,MAAM,2BAAgD,EAAE,GAAG,QAAQ;EACnE,OAAO,yBAAyB;EAChC,OAAO,yBAAyB;EAEhC,MAAM,EACJ,mBACA,WAAW,qBACX,cAAc,CAAC,GACf,GAAG,WACD;EAGJ,MAAM,EAAE,WAAW,aAAa,GAAG,oBAAoB;EAGvD,MAAM,kBACJ,eAAe,QAAQ,uBAAuB,OAC1C;GACA,GAAG;GACH,GAAG;EACL,IACE,KAAA;EACN,MAAM,mBACJ,oBACC,sBAAsB,OAAO,EAAE,SAAS,KAAK,IAAI,KAAA;EACpD,MAAM,oBACJ,oBAAoB,OAChB,kBACA;GAAE,GAAG;GAAiB,WAAW;EAAiB;EAExD,MAAM;GACJ,GAAG;GACH,aAAa;GACb,yBAAyB;GACzB,kCAAkC;EACpC,CAAC;EAGD,IAAI,mBAAmB,MACrB,KAAK,sBAAsB;EAG7B,KAAK,mBAAmB;CAC1B;CACA,OAAO,UAAiC;EACtC,OAAO;CACT;CAKA,iBACE,SACA,OAC4B;E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O,QAAQ;KACvB,CAAC;KACD;IACF;IACA,IAAI,OAAO,MAAM,MACf,uBAAuB,IAAI,OAAO,IAAI,EAAE,GAAG,OAAO,CAAC;GAEvD;GAEA,IACE,qBAAqB,SAAS,KAC9B,gBAAgB,QAAQ,kBAAkB,aAAa,MAEvD,gBAAgB,QAAQ,kBAAkB,YACxC;GAGJ,IAAI,gBAAgB,gBAAgB,iBAAiB,MAAM;IACzD,MAAM,wBAAwB,CAC5B,GAAG,qBAAqB,OAAO,GAC/B,GAAG,uBAAuB,OAAO,CACnC;IACA,IAAI,sBAAsB,SAAS,GACjC,gBAAgB,QAAQ,kBAAkB,oBACxC;SAEF,OAAO,gBAAgB,QAAQ,kBAAkB;IAEnD,MAAMC,cAAAA,wBAAwB,iBAAiB,UAAU;IACzD,MAAM;IACN;GACF;GAEA,OAAO,gBAAgB,QAAQ,kBAAkB;GACjD,MAAMA,cAAAA,wBAAwB,iBAAiB,UAAU;GACzD,MAAM;EACR;CACF;AACF"}
1
+ {"version":3,"file":"index.cjs","names":["ChatOpenAI","emitStreamChunkCallback"],"sources":["../../../../src/llm/openrouter/index.ts"],"sourcesContent":["import type {\n ChatOpenAICallOptions,\n OpenAIChatInput,\n OpenAIClient,\n} from '@langchain/openai';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport type { PromptCacheTtl } from '@/messages/cache';\nimport { ChatOpenAI, emitStreamChunkCallback } from '@/llm/openai';\n\nexport type OpenRouterReasoningEffort =\n | 'xhigh'\n | 'high'\n | 'medium'\n | 'low'\n | 'minimal'\n | 'none';\n\nexport interface OpenRouterReasoning {\n effort?: OpenRouterReasoningEffort;\n max_tokens?: number;\n exclude?: boolean;\n enabled?: boolean;\n}\n\nexport interface ChatOpenRouterCallOptions\n extends Omit<ChatOpenAICallOptions, 'reasoning'> {\n /** @deprecated Use `reasoning` object instead */\n include_reasoning?: boolean;\n reasoning?: OpenRouterReasoning;\n modelKwargs?: OpenAIChatInput['modelKwargs'];\n promptCache?: boolean;\n /**\n * Prompt-cache breakpoint TTL. Defaults to `'1h'` (extended cache) when\n * `promptCache` is enabled; set `'5m'` for the legacy 5-minute behavior.\n * OpenRouter forwards this to Claude upstreams (Anthropic / Bedrock / Vertex),\n * which downgrade to 5m where the extended TTL isn't supported.\n */\n promptCacheTtl?: PromptCacheTtl;\n}\n\nexport type ChatOpenRouterInput = Partial<\n ChatOpenRouterCallOptions & OpenAIChatInput\n>;\n\n/** invocationParams return type extended with OpenRouter reasoning */\nexport type OpenRouterInvocationParams = Omit<\n OpenAIClient.Chat.ChatCompletionCreateParams,\n 'messages'\n> & {\n reasoning?: OpenRouterReasoning;\n};\n\ntype InvocationParamsExtra = {\n streaming?: boolean;\n};\n\ninterface OpenRouterReasoningTextDetail {\n type: 'reasoning.text';\n text?: string;\n format?: string;\n index?: number;\n}\n\ninterface OpenRouterReasoningEncryptedDetail {\n type: 'reasoning.encrypted';\n id?: string;\n data?: string;\n format?: string;\n index?: number;\n}\n\ntype OpenRouterReasoningDetail =\n | OpenRouterReasoningTextDetail\n | OpenRouterReasoningEncryptedDetail;\n\nfunction isReasoningTextDetail(\n value: unknown\n): value is OpenRouterReasoningTextDetail {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n value.type === 'reasoning.text'\n );\n}\n\nfunction isReasoningEncryptedDetail(\n value: unknown\n): value is OpenRouterReasoningEncryptedDetail {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'type' in value &&\n value.type === 'reasoning.encrypted'\n );\n}\n\nfunction getReasoningDetails(value: unknown): OpenRouterReasoningDetail[] {\n if (!Array.isArray(value)) {\n return [];\n }\n return value.filter(\n (detail): detail is OpenRouterReasoningDetail =>\n isReasoningTextDetail(detail) || isReasoningEncryptedDetail(detail)\n );\n}\n\nexport class ChatOpenRouter extends ChatOpenAI {\n private openRouterReasoning?: OpenRouterReasoning;\n /** @deprecated Use `reasoning` object instead */\n private includeReasoning?: boolean;\n\n constructor(_fields: ChatOpenRouterInput) {\n const fieldsWithoutPromptCache: ChatOpenRouterInput = { ..._fields };\n delete fieldsWithoutPromptCache.promptCache;\n delete fieldsWithoutPromptCache.promptCacheTtl;\n\n const {\n include_reasoning,\n reasoning: openRouterReasoning,\n modelKwargs = {},\n ...fields\n } = fieldsWithoutPromptCache;\n\n // Extract reasoning from modelKwargs if provided there (e.g., from LLMConfig)\n const { reasoning: mkReasoning, ...restModelKwargs } = modelKwargs as {\n reasoning?: OpenRouterReasoning;\n } & Record<string, unknown>;\n const mergedReasoning =\n mkReasoning != null || openRouterReasoning != null\n ? {\n ...mkReasoning,\n ...openRouterReasoning,\n }\n : undefined;\n const runtimeReasoning =\n mergedReasoning ??\n (include_reasoning === true ? { enabled: true } : undefined);\n const parentModelKwargs =\n runtimeReasoning == null\n ? restModelKwargs\n : { ...restModelKwargs, reasoning: runtimeReasoning };\n\n super({\n ...fields,\n modelKwargs: parentModelKwargs,\n includeReasoningDetails: true,\n convertReasoningDetailsToContent: true,\n });\n\n // Merge reasoning config: modelKwargs.reasoning < constructor reasoning\n if (mergedReasoning != null) {\n this.openRouterReasoning = mergedReasoning;\n }\n\n this.includeReasoning = include_reasoning;\n }\n static lc_name(): 'LibreChatOpenRouter' {\n return 'LibreChatOpenRouter';\n }\n\n // OpenRouter widens OpenAI reasoning with extra effort levels ('xhigh' | 'none' | 'minimal').\n override invocationParams(\n options?: this['ParsedCallOptions'],\n extra?: InvocationParamsExtra\n ): OpenRouterInvocationParams {\n type MutableParams = Omit<\n OpenAIClient.Chat.ChatCompletionCreateParams,\n 'messages'\n > & { reasoning_effort?: string; reasoning?: OpenRouterReasoning };\n\n const optionsWithDefaults = this._combineCallOptions(options);\n const params = (\n this._useResponsesApi(options)\n ? this.responses.invocationParams(optionsWithDefaults)\n : this.completions.invocationParams(optionsWithDefaults, extra)\n ) as MutableParams;\n\n // Remove the OpenAI-native reasoning_effort that the parent sets;\n // OpenRouter uses a `reasoning` object instead\n delete params.reasoning_effort;\n\n // Build the OpenRouter reasoning config\n const reasoning = this.buildOpenRouterReasoning(optionsWithDefaults);\n if (reasoning != null) {\n params.reasoning = reasoning;\n } else {\n delete params.reasoning;\n }\n\n return params;\n }\n\n private buildOpenRouterReasoning(\n options?: this['ParsedCallOptions']\n ): OpenRouterReasoning | undefined {\n let reasoning: OpenRouterReasoning | undefined;\n\n // 1. Instance-level reasoning config (from constructor)\n if (this.openRouterReasoning != null) {\n reasoning = { ...this.openRouterReasoning };\n }\n\n // 2. LangChain-style reasoning params (from parent's `this.reasoning`)\n const lcReasoning = this.getReasoningParams(options);\n if (lcReasoning?.effort != null) {\n reasoning = {\n ...reasoning,\n effort: lcReasoning.effort as OpenRouterReasoningEffort,\n };\n }\n\n // 3. Call-level reasoning override\n const callReasoning = (options as ChatOpenRouterCallOptions | undefined)\n ?.reasoning;\n if (callReasoning != null) {\n reasoning = { ...reasoning, ...callReasoning };\n }\n\n // 4. Legacy include_reasoning backward compatibility\n if (reasoning == null && this.includeReasoning === true) {\n reasoning = { enabled: true };\n }\n\n return reasoning;\n }\n\n override async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const reasoningTextByIndex = new Map<\n number,\n OpenRouterReasoningTextDetail\n >();\n const reasoningEncryptedById = new Map<\n string,\n OpenRouterReasoningEncryptedDetail\n >();\n\n for await (const generationChunk of super._streamResponseChunks(\n messages,\n options,\n undefined\n )) {\n let currentReasoningText = '';\n const reasoningDetails = getReasoningDetails(\n generationChunk.message.additional_kwargs.reasoning_details\n );\n\n for (const detail of reasoningDetails) {\n if (detail.type === 'reasoning.text') {\n currentReasoningText += detail.text ?? '';\n const index = detail.index ?? 0;\n const existing = reasoningTextByIndex.get(index);\n if (existing != null) {\n existing.text = `${existing.text ?? ''}${detail.text ?? ''}`;\n continue;\n }\n reasoningTextByIndex.set(index, {\n ...detail,\n text: detail.text ?? '',\n });\n continue;\n }\n if (detail.id != null) {\n reasoningEncryptedById.set(detail.id, { ...detail });\n }\n }\n\n if (\n currentReasoningText.length > 0 &&\n generationChunk.message.additional_kwargs.reasoning == null\n ) {\n generationChunk.message.additional_kwargs.reasoning =\n currentReasoningText;\n }\n\n if (generationChunk.generationInfo?.finish_reason != null) {\n const finalReasoningDetails = [\n ...reasoningTextByIndex.values(),\n ...reasoningEncryptedById.values(),\n ];\n if (finalReasoningDetails.length > 0) {\n generationChunk.message.additional_kwargs.reasoning_details =\n finalReasoningDetails;\n } else {\n delete generationChunk.message.additional_kwargs.reasoning_details;\n }\n await emitStreamChunkCallback(generationChunk, runManager);\n yield generationChunk;\n continue;\n }\n\n delete generationChunk.message.additional_kwargs.reasoning_details;\n await emitStreamChunkCallback(generationChunk, runManager);\n yield generationChunk;\n }\n }\n}\n"],"mappings":";;AA6EA,SAAS,sBACP,OACwC;CACxC,OACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,MAAM,SAAS;AAEnB;AAEA,SAAS,2BACP,OAC6C;CAC7C,OACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,MAAM,SAAS;AAEnB;AAEA,SAAS,oBAAoB,OAA6C;CACxE,IAAI,CAAC,MAAM,QAAQ,KAAK,GACtB,OAAO,CAAC;CAEV,OAAO,MAAM,QACV,WACC,sBAAsB,MAAM,KAAK,2BAA2B,MAAM,CACtE;AACF;AAEA,IAAa,iBAAb,cAAoCA,cAAAA,WAAW;CAC7C;;CAEA;CAEA,YAAY,SAA8B;EACxC,MAAM,2BAAgD,EAAE,GAAG,QAAQ;EACnE,OAAO,yBAAyB;EAChC,OAAO,yBAAyB;EAEhC,MAAM,EACJ,mBACA,WAAW,qBACX,cAAc,CAAC,GACf,GAAG,WACD;EAGJ,MAAM,EAAE,WAAW,aAAa,GAAG,oBAAoB;EAGvD,MAAM,kBACJ,eAAe,QAAQ,uBAAuB,OAC1C;G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I,uBAAuB;GAC3B,MAAM,mBAAmB,oBACvB,gBAAgB,QAAQ,kBAAkB,iBAC5C;GAEA,KAAK,MAAM,UAAU,kBAAkB;IACrC,IAAI,OAAO,SAAS,kBAAkB;KACpC,wBAAwB,OAAO,QAAQ;KACvC,MAAM,QAAQ,OAAO,SAAS;KAC9B,MAAM,WAAW,qBAAqB,IAAI,KAAK;KAC/C,IAAI,YAAY,MAAM;MACpB,SAAS,OAAO,GAAG,SAAS,QAAQ,KAAK,OAAO,QAAQ;MACxD;KACF;KACA,qBAAqB,IAAI,OAAO;MAC9B,GAAG;MACH,MAAM,OAAO,QAAQ;KACvB,CAAC;KACD;IACF;IACA,IAAI,OAAO,MAAM,MACf,uBAAuB,IAAI,OAAO,IAAI,EAAE,GAAG,OAAO,CAAC;GAEvD;GAEA,IACE,qBAAqB,SAAS,KAC9B,gBAAgB,QAAQ,kBAAkB,aAAa,MAEvD,gBAAgB,QAAQ,kBAAkB,YACxC;GAGJ,IAAI,gBAAgB,gBAAgB,iBAAiB,MAAM;IACzD,MAAM,wBAAwB,CAC5B,GAAG,qBAAqB,OAAO,GAC/B,GAAG,uBAAuB,OAAO,CACnC;IACA,IAAI,sBAAsB,SAAS,GACjC,gBAAgB,QAAQ,kBAAkB,oBACxC;SAEF,OAAO,gBAAgB,QAAQ,kBAAkB;IAEnD,MAAMC,cAAAA,wBAAwB,iBAAiB,UAAU;IACzD,MAAM;IACN;GACF;GAEA,OAAO,gBAAgB,QAAQ,kBAAkB;GACjD,MAAMA,cAAAA,wBAAwB,iBAAiB,UAAU;GACzD,MAAM;EACR;CACF;AACF"}
@@ -669,7 +669,7 @@ const formatAgentMessages = (payload, indexTokenCountMap, tools, skills, options
669
669
  }
670
670
  const formattedMessages = formatAssistantMessage(processedMessage, {
671
671
  preserveUnpairedServerToolUses: i === payload.length - 1,
672
- preserveReasoningContent: options?.provider === "deepseek",
672
+ preserveReasoningContent: options?.preserveReasoningContent ?? options?.provider === "deepseek",
673
673
  provider: options?.provider
674
674
  });
675
675
  if (sourceMessageId != null && sourceMessageId !== "") for (const formattedMessage of formattedMessages) formattedMessage.id = sourceMessageId;