22
33namespace McpWp \AiCommand ;
44
5- use Mcp \Client \ClientSession ;
6- use McpWp \AiCommand \AI \AiClient ;
75use McpWp \AiCommand \AI \WpAiClient ;
8- use McpWp \AiCommand \MCP \Client ;
9- use McpWp \AiCommand \Utils \CliLogger ;
10- use McpWp \AiCommand \Utils \McpConfig ;
11- use McpWp \MCP \Servers \WordPress \WordPress ;
126use WP_CLI ;
137use WP_CLI \Utils ;
148use WP_CLI_Command ;
159
1610/**
1711 * AI command class.
1812 *
19- * Allows interacting with an LLM using MCP.
20- *
21- * @phpstan-import-type ToolDefinition from AiClient
13+ * Allows interacting with an LLM using the WP AI Client.
2214 */
2315class AiCommand extends WP_CLI_Command {
2416
@@ -30,38 +22,26 @@ class AiCommand extends WP_CLI_Command {
3022 * <prompt>
3123 * : AI prompt.
3224 *
33- * [--skip-builtin-servers[=<server>]]
34- * : Skip loading the built-in servers for WP-CLI and the current WordPress site.
35- * Can be set to 'all' (skip both), 'cli' (skip the WP-CLI server),
36- * or 'wp' (skip the WordPress server).
37- *
3825 * [--skip-wordpress]
3926 * : Run command without loading WordPress. (Not implemented yet)
4027 *
41- * [--approval-mode]
42- * : Approve tool usage before running.
43- *
4428 * [--service=<service>]
4529 * : Manually specify the AI service to use.
46- * Depends on the available AI services.
4730 * Examples: 'google', 'anthropic', 'openai'.
4831 *
4932 * [--model=<model>]
5033 * : Manually specify the LLM model that should be used.
51- * Depends on the available AI services.
52- * Examples: 'gemini-2.0-flash', 'gpt-4o'.
34+ * Examples: 'gemini-2.0-flash', 'gpt-4o', 'claude-sonnet-4-5'.
5335 *
5436 * ## EXAMPLES
5537 *
56- * # Get data from WordPress
57- * $ wp ai "What are the titles of my last three posts?"
58- * - Hello world
59- * - My awesome post
60- * - Another post
38+ * # Ask a simple question
39+ * $ wp ai "Explain WordPress in one sentence"
40+ * WordPress is a free and open-source content management system...
6141 *
62- * # Interact with multiple MCP servers.
63- * $ wp ai "Take file foo.txt and create a new blog post from it"
64- * Success: Blog post created.
42+ * # Use a specific model
43+ * $ wp ai "Summarize the history of WordPress" --model=gpt-4o
44+ * WordPress was created in 2003.. .
6545 *
6646 * @when before_wp_load
6747 *
@@ -76,36 +56,15 @@ public function __invoke( array $args, array $assoc_args ): void {
7656 WP_CLI ::error ( 'Not implemented yet. ' );
7757 }
7858
79- $ approval_mode = (bool ) Utils \get_flag_value ( $ assoc_args , 'approval-mode ' , false );
80- $ service = Utils \get_flag_value ( $ assoc_args , 'service ' );
81- $ model = Utils \get_flag_value ( $ assoc_args , 'model ' );
82-
83- // Check if WP AI Client is available (preferred for simple prompts).
84- $ use_wp_ai_client = class_exists ( '\WordPress\AI_Client\AI_Client ' );
85-
86- // If using WP AI Client and no MCP tools/approval is needed, use simplified path.
87- if ( $ use_wp_ai_client && ! $ approval_mode ) {
88- $ skip_builtin_servers = Utils \get_flag_value ( $ assoc_args , 'skip-builtin-servers ' );
89- // Only use WP AI Client if MCP servers are skipped.
90- if ( $ skip_builtin_servers ) {
91- $ ai_client = new WpAiClient ( [], $ approval_mode , $ service , $ model );
92- $ ai_client ->call_ai_service_with_prompt ( $ args [0 ] );
93- return ;
94- }
95- }
96-
97- // Otherwise, use the full MCP integration with AI Services (required for tools).
98- if ( ! function_exists ( '\ai_services ' ) ) {
99- WP_CLI ::error ( 'This command requires the AI Services plugin for MCP tool integration. You can install it with `wp plugin install ai-services --activate`. Alternatively, use `--skip-builtin-servers=all` to use WP AI Client without MCP tools. ' );
59+ // Ensure WP AI Client is available.
60+ if ( ! class_exists ( '\WordPress\AI_Client\AI_Client ' ) ) {
61+ WP_CLI ::error ( 'This command requires the WP AI Client. Please ensure WordPress 7.0+ or the AI plugin is installed and activated. ' );
10062 }
10163
102- $ skip_builtin_servers = Utils \get_flag_value ( $ assoc_args , 'skip-builtin-servers ' , 'all ' );
103-
104- $ sessions = $ this ->get_sessions ( $ with_wordpress && 'cli ' === $ skip_builtin_servers , 'wp ' === $ skip_builtin_servers );
105- $ tools = $ this ->get_tools ( $ sessions );
106-
107- $ ai_client = new AiClient ( $ tools , $ approval_mode , $ service , $ model );
64+ $ service = Utils \get_flag_value ( $ assoc_args , 'service ' );
65+ $ model = Utils \get_flag_value ( $ assoc_args , 'model ' );
10866
67+ $ ai_client = new WpAiClient ( [], false , $ service , $ model );
10968 $ ai_client ->call_ai_service_with_prompt ( $ args [0 ] );
11069 }
11170
0 commit comments