feat: add OpenRouter reasoning mode support (v0.4.9)

Enables reasoning-capable models (Grok 4.1 Fast, DeepSeek-R1, etc.)
via new admin settings: Enable Reasoning toggle and Reasoning Effort
selector. Provider auto-injects reasoning params when enabled; callers
can override per-call via options array. Response parser captures
reasoning_tokens and reasoning_content for visibility.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-19 23:54:26 +07:00
parent 10c812d191
commit 39570e4586
5 changed files with 60 additions and 3 deletions

View File

@@ -36,7 +36,8 @@ class PRAutoBlogger_OpenRouter_Provider implements PRAutoBlogger_LLM_Provider_In
* temperature?: float,
* max_tokens?: int,
* response_format?: array{type: string},
* } $options Optional parameters.
* reasoning?: array{enabled: bool, effort?: string},
* } $options Optional parameters. Pass 'reasoning' to override the global setting.
*
* @return array{
* content: string,
@@ -88,6 +89,16 @@ class PRAutoBlogger_OpenRouter_Provider implements PRAutoBlogger_LLM_Provider_In
$body['response_format'] = $options['response_format'];
}
// Reasoning mode: explicit caller override takes priority, then global setting.
if ( isset( $options['reasoning'] ) ) {
$body['reasoning'] = $options['reasoning'];
} elseif ( '1' === get_option( 'prautoblogger_reasoning_enabled', '0' ) ) {
$body['reasoning'] = [
'enabled' => true,
'effort' => get_option( 'prautoblogger_reasoning_effort', 'medium' ),
];
}
$last_error = '';
$config = new PRAutoBlogger_OpenRouter_Config();

View File

@@ -43,8 +43,10 @@ class PRAutoBlogger_OpenRouter_Response_Parser {
* model: string,
* prompt_tokens: int,
* completion_tokens: int,
* reasoning_tokens: int,
* total_tokens: int,
* finish_reason: string,
* reasoning_content: string,
* }
*
* @throws \RuntimeException If response structure is invalid.
@@ -94,13 +96,18 @@ class PRAutoBlogger_OpenRouter_Response_Parser {
$usage = $data['usage'] ?? [];
// Reasoning content lives alongside 'content' in the message object.
$reasoning_content = $data['choices'][0]['message']['reasoning'] ?? '';
return [
'content' => (string) $content,
'model' => $data['model'] ?? $model,
'prompt_tokens' => (int) ( $usage['prompt_tokens'] ?? 0 ),
'completion_tokens' => (int) ( $usage['completion_tokens'] ?? 0 ),
'reasoning_tokens' => (int) ( $usage['reasoning_tokens'] ?? 0 ),
'total_tokens' => (int) ( $usage['total_tokens'] ?? 0 ),
'finish_reason' => $data['choices'][0]['finish_reason'] ?? 'unknown',
'reasoning_content' => (string) $reasoning_content,
];
}