{"version":3,"file":"cache.mjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import {\n  AIMessage,\n  BaseMessage,\n  ToolMessage,\n  HumanMessage,\n  SystemMessage,\n  MessageContentComplex,\n} from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\n\ntype MessageWithContent = {\n  content?: string | MessageContentComplex[];\n};\n\n/** Debug logger for cache operations - set ILLUMA_DEBUG_CACHE=true to enable */\nconst debugCache = (message: string, data?: unknown): void => {\n  if (process.env.ILLUMA_DEBUG_CACHE === 'true') {\n    // eslint-disable-next-line no-console\n    console.log(\n      `[Cache] ${message}`,\n      data !== undefined ? JSON.stringify(data, null, 2) : ''\n    );\n  }\n};\n\n/**\n * Deep clones a message's content to prevent mutation of the original.\n */\nfunction deepCloneContent<T extends string | MessageContentComplex[]>(\n  content: T\n): T {\n  if (typeof content === 'string') {\n    return content;\n  }\n  if (Array.isArray(content)) {\n    return content.map((block) => ({ ...block })) as T;\n  }\n  return content;\n}\n\n/**\n * Clones a message with new content. For LangChain BaseMessage instances,\n * constructs a proper class instance so that `instanceof` checks are preserved\n * in downstream code (e.g., ensureThinkingBlockInMessages).\n * For plain objects (AnthropicMessage), uses object spread.\n */\nfunction cloneMessage<T extends MessageWithContent>(\n  message: T,\n  content: string | MessageContentComplex[]\n): T {\n  if (message instanceof BaseMessage) {\n    const baseParams = {\n      content,\n      additional_kwargs: { ...message.additional_kwargs },\n      response_metadata: { ...message.response_metadata },\n      id: message.id,\n      name: message.name,\n    };\n\n    const msgType = message.getType();\n    switch (msgType) {\n    case 'ai':\n      return new AIMessage({\n        ...baseParams,\n        tool_calls: (message as unknown as AIMessage).tool_calls,\n      }) as unknown as T;\n    case 'human':\n      return new HumanMessage(baseParams) as unknown as T;\n    case 'system':\n      return new SystemMessage(baseParams) as unknown as T;\n    case 'tool':\n      return new ToolMessage({\n        ...baseParams,\n        tool_call_id: (message as unknown as ToolMessage).tool_call_id,\n      }) as unknown as T;\n    default:\n      break;\n    }\n  }\n\n  const {\n    lc_kwargs: _lc_kwargs,\n    lc_serializable: _lc_serializable,\n    lc_namespace: _lc_namespace,\n    ...rest\n  } = message as T & {\n    lc_kwargs?: unknown;\n    lc_serializable?: unknown;\n    lc_namespace?: unknown;\n  };\n\n  const cloned = { ...rest, content } as T;\n\n  // Sync lc_kwargs.content with the new content to prevent LangChain coercion issues\n  const lcKwargs = (message as Record<string, unknown>).lc_kwargs as\n    | Record<string, unknown>\n    | undefined;\n  if (lcKwargs != null) {\n    (cloned as Record<string, unknown>).lc_kwargs = {\n      ...lcKwargs,\n      content: content,\n    };\n  }\n\n  // LangChain messages don't have a direct 'role' property - derive it from getType()\n  if (\n    'getType' in message &&\n    typeof message.getType === 'function' &&\n    !('role' in cloned)\n  ) {\n    const msgType = (message as unknown as BaseMessage).getType();\n    const roleMap: Record<string, string> = {\n      human: 'user',\n      ai: 'assistant',\n      system: 'system',\n      tool: 'tool',\n    };\n    (cloned as Record<string, unknown>).role = roleMap[msgType] || msgType;\n  }\n\n  return cloned;\n}\n\n/**\n * Checks if a content block is a cache point\n */\nfunction isCachePoint(block: MessageContentComplex): boolean {\n  return 'cachePoint' in block && !('type' in block);\n}\n\n/**\n * Checks if a message's content needs cache control stripping.\n * Returns true if content has cachePoint blocks or cache_control fields.\n */\nfunction needsCacheStripping(content: MessageContentComplex[]): boolean {\n  for (let i = 0; i < content.length; i++) {\n    const block = content[i];\n    if (isCachePoint(block)) return true;\n    if ('cache_control' in block) return true;\n  }\n  return false;\n}\n\n/**\n * Checks if a message's content has Anthropic cache_control fields.\n */\nfunction hasAnthropicCacheControl(content: MessageContentComplex[]): boolean {\n  for (let i = 0; i < content.length; i++) {\n    if ('cache_control' in content[i]) return true;\n  }\n  return false;\n}\n\n/**\n * Checks if a message's content has Bedrock cachePoint blocks.\n */\nfunction hasBedrockCachePoint(content: MessageContentComplex[]): boolean {\n  for (let i = 0; i < content.length; i++) {\n    if (isCachePoint(content[i])) return true;\n  }\n  return false;\n}\n\n/**\n * Anthropic API: Adds cache control to the appropriate user messages in the payload.\n * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,\n * then adds fresh cache control to the last 2 user messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * Returns a new array - only clones messages that require modification.\n * @param messages - The array of message objects.\n * @returns - A new array of message objects with cache control added.\n */\nexport function addCacheControl<T extends AnthropicMessage | BaseMessage>(\n  messages: T[]\n): T[] {\n  if (!Array.isArray(messages) || messages.length < 2) {\n    return messages;\n  }\n\n  const updatedMessages: T[] = [...messages];\n  let userMessagesModified = 0;\n\n  for (let i = updatedMessages.length - 1; i >= 0; i--) {\n    const originalMessage = updatedMessages[i];\n    const content = originalMessage.content;\n    const isUserMessage =\n      ('getType' in originalMessage && originalMessage.getType() === 'human') ||\n      ('role' in originalMessage && originalMessage.role === 'user');\n\n    const hasArrayContent = Array.isArray(content);\n    const needsStripping =\n      hasArrayContent &&\n      needsCacheStripping(content as MessageContentComplex[]);\n    const needsCacheAdd =\n      userMessagesModified < 2 &&\n      isUserMessage &&\n      (typeof content === 'string' || hasArrayContent);\n\n    if (!needsStripping && !needsCacheAdd) {\n      continue;\n    }\n\n    let workingContent: MessageContentComplex[];\n\n    if (hasArrayContent) {\n      workingContent = deepCloneContent(\n        content as MessageContentComplex[]\n      ).filter((block) => !isCachePoint(block as MessageContentComplex));\n\n      for (let j = 0; j < workingContent.length; j++) {\n        const block = workingContent[j] as Record<string, unknown>;\n        if ('cache_control' in block) {\n          delete block.cache_control;\n        }\n      }\n    } else if (typeof content === 'string') {\n      workingContent = [\n        { type: 'text', text: content },\n      ] as MessageContentComplex[];\n    } else {\n      workingContent = [];\n    }\n\n    if (userMessagesModified >= 2 || !isUserMessage) {\n      updatedMessages[i] = cloneMessage(\n        originalMessage as MessageWithContent,\n        workingContent\n      ) as T;\n      continue;\n    }\n\n    for (let j = workingContent.length - 1; j >= 0; j--) {\n      const contentPart = workingContent[j];\n      if ('type' in contentPart && contentPart.type === 'text') {\n        (contentPart as Anthropic.TextBlockParam).cache_control = {\n          type: 'ephemeral',\n        };\n        userMessagesModified++;\n        break;\n      }\n    }\n\n    updatedMessages[i] = cloneMessage(\n      originalMessage as MessageWithContent,\n      workingContent\n    ) as T;\n  }\n\n  return updatedMessages;\n}\n\n/**\n * Removes all Anthropic cache_control fields from messages\n * Used when switching from Anthropic to Bedrock provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripAnthropicCacheControl<T extends MessageWithContent>(\n  messages: T[]\n): T[] {\n  if (!Array.isArray(messages)) {\n    return messages;\n  }\n\n  const updatedMessages: T[] = [...messages];\n\n  for (let i = 0; i < updatedMessages.length; i++) {\n    const originalMessage = updatedMessages[i];\n    const content = originalMessage.content;\n\n    if (!Array.isArray(content) || !hasAnthropicCacheControl(content)) {\n      continue;\n    }\n\n    const clonedContent = deepCloneContent(content);\n    for (let j = 0; j < clonedContent.length; j++) {\n      const block = clonedContent[j] as Record<string, unknown>;\n      if ('cache_control' in block) {\n        delete block.cache_control;\n      }\n    }\n    updatedMessages[i] = cloneMessage(originalMessage, clonedContent) as T;\n  }\n\n  return updatedMessages;\n}\n\n/**\n * Removes all Bedrock cachePoint blocks from messages\n * Used when switching from Bedrock to Anthropic provider\n * Returns a new array - only clones messages that require modification.\n */\nexport function stripBedrockCacheControl<T extends MessageWithContent>(\n  messages: T[]\n): T[] {\n  if (!Array.isArray(messages)) {\n    return messages;\n  }\n\n  const updatedMessages: T[] = [...messages];\n\n  for (let i = 0; i < updatedMessages.length; i++) {\n    const originalMessage = updatedMessages[i];\n    const content = originalMessage.content;\n\n    if (!Array.isArray(content) || !hasBedrockCachePoint(content)) {\n      continue;\n    }\n\n    const clonedContent = deepCloneContent(content).filter(\n      (block) => !isCachePoint(block as MessageContentComplex)\n    );\n    updatedMessages[i] = cloneMessage(originalMessage, clonedContent) as T;\n  }\n\n  return updatedMessages;\n}\n\n/**\n * Adds Bedrock Converse API cache points using \"Stable Prefix Caching\" strategy.\n *\n * STRATEGY: Place cache point after the LAST ASSISTANT message only.\n * This ensures the prefix (everything before the cache point) remains STABLE\n * as the conversation grows, maximizing cache hits.\n *\n * Why this works:\n * - System message has its own cachePoint (added in AgentContext)\n * - Tools have their own cachePoint (added in IllumaBedrockConverse)\n * - Conversation history grows, but the PREFIX stays the same\n * - Only the NEW user message is uncached (it's always different)\n *\n * Example conversation flow:\n * Request 1: [System+cachePoint][Tools+cachePoint][User1] → No conversation cache yet\n * Request 2: [System][Tools][User1][Assistant1+cachePoint][User2] → Cache User1+Assistant1\n * Request 3: [System][Tools][User1][Assistant1][User2][Assistant2+cachePoint][User3]\n *            → Cache reads User1+A1+User2+A2, cache writes new portion\n *\n * Claude's \"Simplified Cache Management\" automatically looks back up to 20 content\n * blocks from the cache checkpoint to find the longest matching prefix.\n *\n * @param messages - The array of message objects (excluding system message).\n * @returns - The updated array with a single cache point after the last assistant message.\n */\nexport function addBedrockCacheControl<\n  T extends Partial<BaseMessage> & MessageWithContent,\n>(messages: T[]): T[] {\n  if (!Array.isArray(messages) || messages.length < 1) {\n    debugCache('addBedrockCacheControl: Skipping - no messages', {\n      count: messages.length,\n    });\n    return messages;\n  }\n\n  debugCache(\n    'addBedrockCacheControl: Processing messages with stable prefix strategy',\n    {\n      count: messages.length,\n    }\n  );\n\n  // Clone messages to avoid mutating originals\n  const updatedMessages: T[] = messages.map((msg) => {\n    const content = msg.content;\n    if (Array.isArray(content)) {\n      // Strip existing cachePoint blocks and Anthropic-style cache_control\n      const stripped = content\n        .filter((block) => !isCachePoint(block))\n        .map((block) => {\n          const rec = block as Record<string, unknown>;\n          if ('cache_control' in rec) {\n            const { cache_control: _, ...rest } = rec;\n            return rest as MessageContentComplex;\n          }\n          return block;\n        });\n      if (needsCacheStripping(content) || hasAnthropicCacheControl(content)) {\n        return cloneMessage(msg, stripped) as T;\n      }\n    }\n    return cloneMessage(msg, content as string | MessageContentComplex[]) as T;\n  });\n\n  // Helper function to check if a message contains reasoning/thinking blocks\n  const hasReasoningBlock = (message: T): boolean => {\n    const content = message.content;\n    if (!Array.isArray(content)) {\n      return false;\n    }\n    for (const block of content) {\n      const type = (block as { type?: string }).type;\n      if (\n        type === 'reasoning_content' ||\n        type === 'reasoning' ||\n        type === 'thinking' ||\n        type === 'redacted_thinking'\n      ) {\n        return true;\n      }\n    }\n    return false;\n  };\n\n  /**\n   * Helper to add a cachePoint to a message's content.\n   * For strings: wraps into [{type: 'text', text}, {cachePoint}]\n   * For arrays: inserts cachePoint after the last non-whitespace text block\n   * Returns the new message or null if no suitable insertion point.\n   */\n  const addCachePointToMessage = (message: T): T | null => {\n    const msgContent = message.content;\n\n    if (typeof msgContent === 'string' && msgContent !== '') {\n      const newContent = [\n        { type: ContentTypes.TEXT, text: msgContent },\n        { cachePoint: { type: 'default' } },\n      ] as MessageContentComplex[];\n      return cloneMessage(message, newContent) as T;\n    }\n\n    if (Array.isArray(msgContent) && msgContent.length > 0) {\n      if (hasReasoningBlock(message)) {\n        return null;\n      }\n      // Find the last text block and insert cache point after it\n      for (let j = msgContent.length - 1; j >= 0; j--) {\n        const type = (msgContent[j] as { type?: string }).type;\n        if (type === ContentTypes.TEXT || type === 'text') {\n          const text = (msgContent[j] as { text?: string }).text;\n          if (text != null && text.trim() !== '') {\n            const newContent = [\n              ...msgContent.slice(0, j + 1),\n              { cachePoint: { type: 'default' } } as MessageContentComplex,\n              ...msgContent.slice(j + 1),\n            ];\n            return cloneMessage(message, newContent) as T;\n          }\n        }\n      }\n    }\n\n    return null;\n  };\n\n  // Add cache points to the last 2 messages (from the end) that have eligible content.\n  // This mirrors Anthropic's two-breakpoint strategy for Bedrock's cache API.\n  // Skip messages with only whitespace, empty content, or reasoning blocks.\n  const MAX_CACHE_POINTS = 2;\n  let applied = 0;\n\n  for (\n    let i = updatedMessages.length - 1;\n    i >= 0 && applied < MAX_CACHE_POINTS;\n    i--\n  ) {\n    const message = updatedMessages[i];\n    const msgContent = message.content;\n\n    // Skip empty/whitespace-only content\n    if (msgContent == null) continue;\n    if (typeof msgContent === 'string' && msgContent.trim() === '') continue;\n    if (Array.isArray(msgContent) && msgContent.length === 0) continue;\n\n    // Skip non-string, non-array content (e.g., number, object without type)\n    if (typeof msgContent !== 'string' && !Array.isArray(msgContent)) continue;\n\n    // Skip AI messages with only whitespace text and reasoning blocks (tool-call scenario)\n    const messageType =\n      'getType' in message && typeof message.getType === 'function'\n        ? message.getType()\n        : 'unknown';\n    const role = (message as Record<string, unknown>).role as\n      | string\n      | undefined;\n    const isAi = messageType === 'ai' || role === 'assistant';\n    if (isAi && hasReasoningBlock(message)) {\n      // Check if all text blocks are whitespace-only\n      if (Array.isArray(msgContent)) {\n        const hasNonWhitespaceText = msgContent.some((block) => {\n          const type = (block as { type?: string }).type;\n          if (type === ContentTypes.TEXT || type === 'text') {\n            const text = (block as { text?: string }).text;\n            return text != null && text.trim() !== '';\n          }\n          return false;\n        });\n        if (!hasNonWhitespaceText) {\n          debugCache(\n            `⚠️ Message cachePoint SKIPPED at index ${i} (AI with whitespace-only text + reasoning)`\n          );\n          continue;\n        }\n      }\n    }\n\n    // Skip messages that already have cache points (multi-agent scenarios)\n    if (\n      Array.isArray(msgContent) &&\n      msgContent.some((b) => isCachePoint(b as MessageContentComplex))\n    ) {\n      continue;\n    }\n\n    const updated = addCachePointToMessage(message);\n    if (updated != null) {\n      updatedMessages[i] = updated;\n      applied++;\n      debugCache(\n        `📍 Message cachePoint at index ${i} (${typeof message.content === 'string' ? 'string' : 'array'})`\n      );\n    }\n  }\n\n  debugCache(\n    'addBedrockCacheControl: Complete - stable prefix caching applied',\n    {\n      appliedCachePoints: applied,\n      totalMessages: updatedMessages.length,\n    }\n  );\n\n  return updatedMessages;\n}\n"],"names":[],"mappings":";;;AAgBA;AACA,MAAM,UAAU,GAAG,CAAC,OAAe,EAAE,IAAc,KAAU;IAC3D,IAAI,OAAO,CAAC,GAAG,CAAC,kBAAkB,KAAK,MAAM,EA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