Files
wp-agentic-writer/includes/class-provider-manager.php
Dwindi Ramadhana d2c10756ab Add AI writing assistant plugin with local backend, brave search, and image generation support
- Implement local backend AI provider with Ollama integration
- Add Brave Search API integration for real-time search suggestions
- Add image generation manager with multiple AI providers
- Create hybrid provider system with local/cloud fallback
- Add comprehensive settings UI with provider management
- Implement Gutenberg sidebar with writing assistance controls
- Add SEO schema generation for AI-generated content
- Multiple provider support: OpenRouter, local backend, Codex
2026-05-17 10:48:05 +07:00

153 lines
4.4 KiB
PHP

<?php
/**
* AI Provider Manager
*
* Routes AI requests to appropriate provider based on task type and configuration
*
* @package WP_Agentic_Writer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WP_Agentic_Writer_Provider_Manager {
/**
* Get provider instance for specific task type
*
* @param string $type Task type (chat, clarity, planning, writing, refinement, image).
* @return WP_Agentic_Writer_AI_Provider_Interface Provider instance.
*/
public static function get_provider_for_task( $type ) {
$settings = get_option( 'wp_agentic_writer_settings', array() );
$task_providers = $settings['task_providers'] ?? array();
// Determine which provider to use for this task
$provider_name = $task_providers[ $type ] ?? 'openrouter';
// Get provider instance with fallback logic
$provider = self::get_provider_instance( $provider_name, $type );
// If provider not configured or unavailable, fallback to OpenRouter
if ( ! $provider || ! $provider->is_configured() ) {
error_log( "Provider '{$provider_name}' not available for task '{$type}', using OpenRouter fallback" );
return WP_Agentic_Writer_OpenRouter_Provider::get_instance();
}
return $provider;
}
/**
* Get provider instance by name
*
* @param string $provider_name Provider identifier.
* @param string $task_type Task type for validation.
* @return WP_Agentic_Writer_AI_Provider_Interface|null Provider instance or null.
*/
private static function get_provider_instance( $provider_name, $task_type ) {
switch ( $provider_name ) {
case 'local_backend':
if ( ! class_exists( 'WP_Agentic_Writer_Local_Backend_Provider' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'class-local-backend-provider.php';
}
$provider = new WP_Agentic_Writer_Local_Backend_Provider();
break;
case 'codex':
if ( ! class_exists( 'WP_Agentic_Writer_Codex_Provider' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'class-codex-provider.php';
}
$provider = new WP_Agentic_Writer_Codex_Provider();
break;
case 'openrouter':
default:
$provider = WP_Agentic_Writer_OpenRouter_Provider::get_instance();
break;
}
// Validate provider supports this task type
if ( $provider && ! $provider->supports_task_type( $task_type ) ) {
error_log( "Provider '{$provider_name}' does not support task type '{$task_type}'" );
return null;
}
return $provider;
}
/**
* Get all available providers with their status
*
* @return array Array of provider info with name, status, supported tasks.
*/
public static function get_available_providers() {
$providers = array();
// OpenRouter (always available)
$openrouter = WP_Agentic_Writer_OpenRouter_Provider::get_instance();
$providers['openrouter'] = array(
'name' => 'OpenRouter',
'configured' => $openrouter->is_configured(),
'supports' => array( 'chat', 'clarity', 'planning', 'writing', 'refinement', 'image' ),
'icon' => '☁️',
);
// Local Backend
if ( class_exists( 'WP_Agentic_Writer_Local_Backend_Provider' ) ) {
$local = new WP_Agentic_Writer_Local_Backend_Provider();
$providers['local_backend'] = array(
'name' => 'Local Backend',
'configured' => $local->is_configured(),
'supports' => array( 'chat', 'clarity', 'planning', 'writing', 'refinement' ),
'icon' => '🏠',
);
}
// Codex
if ( class_exists( 'WP_Agentic_Writer_Codex_Provider' ) ) {
$codex = new WP_Agentic_Writer_Codex_Provider();
$providers['codex'] = array(
'name' => 'Codex (OpenAI)',
'configured' => $codex->is_configured(),
'supports' => array( 'chat', 'clarity', 'planning', 'writing', 'refinement' ),
'icon' => '🔗',
);
}
return $providers;
}
/**
* Test all configured providers
*
* @return array Results of connection tests.
*/
public static function test_all_providers() {
$results = array();
$providers = self::get_available_providers();
foreach ( $providers as $key => $info ) {
if ( ! $info['configured'] ) {
$results[ $key ] = array(
'success' => false,
'message' => 'Not configured',
);
continue;
}
$provider = self::get_provider_instance( $key, 'chat' );
if ( $provider ) {
$test_result = $provider->test_connection();
$results[ $key ] = is_wp_error( $test_result )
? array(
'success' => false,
'message' => $test_result->get_error_message(),
)
: $test_result;
}
}
return $results;
}
}