* @author Maximum Software Inc * @license GPL-3.0-only * @link https://agenistrator.com/ * * SPDX-FileCopyrightText: 2025-2026 Alex Ruban * SPDX-FileCopyrightText: 2025-2026 Maximum Software Inc * SPDX-License-Identifier: GPL-3.0-only */ namespace Agenistrator; // Direct access prevention if ( ! defined( 'ABSPATH' ) ) { exit; } /** * LLM_Chat_OpenRouter class. */ class LLM_Chat_OpenRouter extends LLM_Chat_OpenAI { /** * Create a new context object specific to OpenRouter * * @param string $system_message Optional custom system message * @return Context_OpenRouter OpenRouter-specific context object * @since 1.0.0 */ public function create_context( $system_message = null ) { return new Context_OpenRouter( $system_message ); } /** * Get additional API parameters for OpenRouter requests. * * Enables OpenRouter's automatic prompt caching by adding a top-level * cache_control directive. OpenRouter applies the cache breakpoint to the * last cacheable block and advances it as the conversation grows, then * translates it to the provider-native format (cache_control for * Anthropic/Google, prompt_cache_breakpoint for OpenAI) and strips it for * providers that do not support prompt caching. * * @see https://openrouter.ai/docs/guides/best-practices/prompt-caching * * @return array Additional parameters to merge into API request * @since 1.0.4 */ protected function get_additional_params() { $params = parent::get_additional_params(); $params['cache_control'] = array( 'type' => 'ephemeral' ); return $params; } /** * Validate that the context is the correct type for this provider * * @param Context $context Context object to validate * @throws \Exception If context is not the correct type * @since 1.0.0 */ protected function validate_context( $context ) { if ( ! ( $context instanceof Context_OpenRouter ) ) { // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception messages are not HTML output throw new \Exception( String_Utils::replace_tags( __( 'Invalid context type: expected {expected}, got {actual}', 'agenistrator' ), array( 'expected' => 'Context_OpenRouter', 'actual' => get_class( $context ), ) ) ); // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped } } }