Repository: continuedev/continue
File: core/llm/llms/OpenRouter.ts (TypeScript)

Code before:
            cache_control: { type: "ephemeral" },
          };
        }
        return part;
      });
    }

    return content;
  }

  /**
   * Override modifyChatBody to add Anthropic caching when appropriate
   */
  protected modifyChatBody(
    body: ChatCompletionCreateParams,
  ): ChatCompletionCreateParams {
    // First apply parent modifications
    body = super.modifyChatBody(body);

    // Check if we should apply Anthropic caching
    if (!this.isAnthropicModel(body.model) || !this.cacheBehavior) {
      return body;
    }

    const shouldCacheConversation = this.cacheBehavior.cacheConversation;
    const shouldCacheSystemMessage = this.cacheBehavior.cacheSystemMessage;

    if (!shouldCacheConversation && !shouldCacheSystemMessage) {
      return body;
    }

    // Follow the same logic as Anthropic.ts: filter out system messages first
    const filteredMessages = body.messages.filter(
      (m: any) => m.role !== "system" && !!m.content,
    );

    // Find the last two user message indices from the filtered array
    const lastTwoUserMsgIndices = filteredMessages
      .map((msg: any, index: number) => (msg.role === "user" ? index : -1))
      .filter((index: number) => index !== -1)
      .slice(-2);

    // Create a mapping from filtered indices to original indices
    let filteredIndex = 0;
    const filteredToOriginalIndexMap: number[] = [];
    body.messages.forEach((msg: any, originalIndex: number) => {
      if (msg.role !== "system" && !!msg.content) {
        filteredToOriginalIndexMap[filteredIndex] = originalIndex;
        filteredIndex++;
      }
    });

Review comment (suggestion):
Also handle the deprecated but relevant completion option: completionOptions.promptCaching
See relevant hotfix here: https://github.com/continuedev/continue/pull/7652

Code after:
        }
        return part;
      });
    }

    return content;
  }

  /**
   * Override modifyChatBody to add Anthropic caching when appropriate
   */
  protected modifyChatBody(
    body: ChatCompletionCreateParams,
  ): ChatCompletionCreateParams {
    // First apply parent modifications
    body = super.modifyChatBody(body);

    // Check if we should apply Anthropic caching
    if (
      !this.isAnthropicModel(body.model) ||
      (!this.cacheBehavior && !this.completionOptions.promptCaching)
    ) {
      return body;
    }

    const shouldCacheConversation =
      this.cacheBehavior?.cacheConversation ||
      this.completionOptions.promptCaching;
    const shouldCacheSystemMessage =
      this.cacheBehavior?.cacheSystemMessage ||
      this.completionOptions.promptCaching;

    if (!shouldCacheConversation && !shouldCacheSystemMessage) {
      return body;
    }

    // Follow the same logic as Anthropic.ts: filter out system messages first
    const filteredMessages = body.messages.filter(
      (m: any) => m.role !== "system" && !!m.content,
    );

    // Find the last two user message indices from the filtered array
    const lastTwoUserMsgIndices = filteredMessages
      .map((msg: any, index: number) => (msg.role === "user" ? index : -1))
      .filter((index: number) => index !== -1)
      .slice(-2);

    // Create a mapping from filtered indices to original indices
    let filteredIndex = 0;
    const filteredToOriginalIndexMap: number[] = [];
    body.messages.forEach((msg: any, originalIndex: number) => {