new OpenAI(), 'google' => new GoogleGemini(), 'grok' => new Grok(), 'deepseek' => new DeepSeek(), ]; } return self::$providers; } public static function get_provider( string $id ): ?Provider { $providers = self::get_providers(); return $providers[ $id ] ?? null; } public static function get_all_models(): array { $all_models = []; foreach ( self::get_providers() as $provider ) { if ( empty( $provider->get_api_key() ) ) { continue; } foreach ( $provider::models() as $model ) { $all_models[ $provider::id() ][ $model['id'] ] = $model; } } return $all_models; } public static function generate_post( int $agent_id ): ?int { $provider_id = get_post_meta( $agent_id, 'aibp_provider', true ) ?: get_post_meta( $agent_id, 'aibp_ai', true ); $model = get_post_meta( $agent_id, 'aibp_model', true ); $subject = get_post_meta( $agent_id, 'aibp_subject', true ); $length = (int) get_post_meta( $agent_id, 'aibp_length', true ); $prompt_title = get_post_meta( $agent_id, 'aibp_prompt_title', true ); $prompt_content = get_post_meta( $agent_id, 'aibp_prompt_content', true ); $temperature = (float) get_post_meta( $agent_id, 'aibp_temperature', true ) ?: 0.7; $max_tokens = (int) get_post_meta( $agent_id, 'aibp_max_tokens', true ) ?: 4000; $post_type = get_post_meta( $agent_id, 'aibp_post_type', true ) ?: 'post'; $post_status = get_post_meta( $agent_id, 'aibp_post_status', true ) ?: 'publish'; $post_categories = (array) get_post_meta( $agent_id, 'aibp_post_categories', true ); $provider = self::get_provider( $provider_id ); if ( ! $provider ) { throw new \Exception( esc_html( sprintf( 'Unknown AI provider: %s', $provider_id ) ) ); } $gen_args = [ 'model' => $model, 'temperature' => $temperature, 'max_tokens' => $max_tokens, ]; $results = []; // Generate title. $title_prompt = str_replace( '[subject]', $subject, $prompt_title ); $title_prompt = str_replace( '[length]', (string) $length, $title_prompt ); try { $results['post_title'] = self::remove_quotes( $provider->generate( $title_prompt, $gen_args ) ); } catch ( \Exception $e ) { ActivityLog::log( $agent_id, 'error', 'Title generation failed: ' . $e->getMessage() ); throw $e; } // Generate content. $content_prompt = str_replace( '[subject]', $subject, $prompt_content ); $content_prompt = str_replace( '[length]', (string) $length, $content_prompt ); try { $results['post_content'] = $provider->generate( $content_prompt, $gen_args ); } catch ( \Exception $e ) { ActivityLog::log( $agent_id, 'error', 'Content generation failed: ' . $e->getMessage() ); throw $e; } // Insert post. $post_id = wp_insert_post( [ 'post_type' => $post_type, 'post_status' => $post_status, 'post_title' => $results['post_title'], 'post_content' => $results['post_content'], 'post_author' => get_post_field( 'post_author', $agent_id ), 'post_category' => array_map( 'intval', $post_categories ), ] ); if ( is_wp_error( $post_id ) ) { ActivityLog::log( $agent_id, 'error', 'Post insertion failed: ' . $post_id->get_error_message() ); return null; } ActivityLog::log( $agent_id, 'success', sprintf( 'Generated post #%d: "%s"', $post_id, $results['post_title'] ), $post_id ); return $post_id; } private static function remove_quotes( string $str ): string { $length = strlen( $str ); if ( $length >= 2 && $str[0] === '"' && $str[ $length - 1 ] === '"' ) { return substr( $str, 1, -1 ); } return $str; } }