false, 'summary' => '

API Key is missing.

', ); } $model = aiso_get_setting( 'model', 'openai/gpt-4o-mini' ); $temperature = (float) aiso_get_setting( 'temperature', 0.3 ); $max_tokens = (int) aiso_get_setting( 'max_tokens', 300 ); $cache_hours = (int) aiso_get_setting( 'cache_time', 24 ); $post_id = get_the_ID(); $cached = AISO_Cache::get( $post_id ); AISO_Cache::set( $post_id, $summary ); if ( false !== $cached ) { return array( 'success' => true, 'summary' => $cached, 'prompt_tokens' => 'Cached', 'output_tokens' => 'Cached', 'total_tokens' => 0, ); } $prompt = "Article Title: {$title} Generate an executive overview of the blog. Requirements: - Keep between 90-120 words. - Preserve meaning. - Use professional language. - No opinions. - No recommendations. Output:

Quick Summary

Return only HTML. Content: {$content}"; $args = array( 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', 'HTTP-Referer' => home_url(), 'X-Title' => get_bloginfo( 'name' ), ), 'body' => wp_json_encode( array( 'model' => $model, 'messages' => array( array( 'role' => 'user', 'content' => $prompt, ), ), 'temperature' => $temperature, 'max_tokens' => $max_tokens, ) ), 'timeout' => 30, ); $response = wp_remote_post( 'https://openrouter.ai/api/v1/chat/completions', $args ); if ( is_wp_error( $response ) ) { return array( 'summary' => '

Connection failed.

', ); } $response_body = wp_remote_retrieve_body( $response ); $body = json_decode( $response_body, true ); if ( isset( $body['error'] ) ) { return array( 'summary' => '

' . esc_html( $body['error']['message'] ) . '

', ); } $summary = $body['choices'][0]['message']['content'] ?? ''; $summary = preg_replace('/^```(?:html)?/i', '', $summary); $summary = preg_replace('/```$/', '', $summary); $summary = trim($summary); $summary = wp_kses_post( trim( $summary ) ); AISO_Cache::set( $title, $content, $summary ); return array( 'success' => true, 'summary' => $summary, 'prompt_tokens' => $body['usage']['prompt_tokens'] ?? 0, 'output_tokens' => $body['usage']['completion_tokens'] ?? 0, 'total_tokens' => $body['usage']['total_tokens'] ?? 0, ); } }