|
2 | 2 |
|
3 | 3 | namespace WP_CLI\AiCommand; |
4 | 4 |
|
5 | | -use AiCommand\Tools\FileTools; |
6 | | -use AiCommand\Tools\URLTools; |
| 5 | +use WP_CLI\AiCommand\Tools\FileTools; |
| 6 | +use WP_CLI\AiCommand\Tools\URLTools; |
7 | 7 | use WP_CLI; |
8 | 8 | use WP_CLI_Command; |
9 | 9 | use WP_Community_Events; |
@@ -54,19 +54,19 @@ public function __construct( |
54 | 54 | */ |
55 | 55 | public function __invoke( $args, $assoc_args ) { |
56 | 56 | $server = new MCP\Server(); |
57 | | - $client = new MCP\Client( $server ); |
| 57 | + $client = new MCP\Client($server); |
58 | 58 |
|
59 | | - $this->register_tools( $server, $client ); |
| 59 | + $this->register_tools($server, $client); |
60 | 60 |
|
61 | | - $this->register_resources( $server ); |
| 61 | + $this->register_resources($server); |
62 | 62 |
|
63 | 63 | $result = $client->call_ai_service_with_prompt( $args[0] ); |
64 | 64 |
|
65 | 65 | WP_CLI::success( $result ); |
66 | 66 | } |
67 | 67 |
|
68 | 68 | // Register tools for AI processing |
69 | | - private function register_tools( $server, $client ) { |
| 69 | + private function register_tools($server, $client) { |
70 | 70 | $server->register_tool( |
71 | 71 | [ |
72 | 72 | 'name' => 'list_tools', |
@@ -103,119 +103,132 @@ private function register_tools( $server, $client ) { |
103 | 103 |
|
104 | 104 | $server->register_tool( |
105 | 105 | [ |
106 | | - 'name' => 'fetch_wp_community_events', |
107 | | - 'description' => 'Fetches upcoming WordPress community events near a specified city or the user\'s current location. If no events are found in the exact location, nearby events within a specific radius will be considered.', |
| 106 | + 'name' => 'generate_image', |
| 107 | + 'description' => 'Generates an image.', |
108 | 108 | 'inputSchema' => [ |
109 | 109 | 'type' => 'object', |
110 | 110 | 'properties' => [ |
111 | | - 'location' => [ |
| 111 | + 'prompt' => [ |
112 | 112 | 'type' => 'string', |
113 | | - 'description' => 'City name or "near me" for auto-detected location. If no events are found in the exact location, the tool will also consider nearby events within a specified radius (default: 100 km).', |
| 113 | + 'description' => 'The prompt for generating the image.', |
114 | 114 | ], |
115 | 115 | ], |
116 | | - 'required' => [ 'location' ], // We only require the location |
| 116 | + 'required' => [ 'prompt' ], |
117 | 117 | ], |
118 | | - 'callable' => function ( $params ) { |
119 | | - // Default user ID is 0 |
120 | | - $user_id = 0; |
121 | | - |
122 | | - // Get the location from the parameters (already supplied in the prompt) |
123 | | - $location_input = strtolower( trim( $params['location'] ) ); |
124 | | - |
125 | | - // Manually include the WP_Community_Events class if it's not loaded |
126 | | - if ( ! class_exists( 'WP_Community_Events' ) ) { |
127 | | - require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php'; |
128 | | - } |
129 | | - |
130 | | - // Determine location for the WP_Community_Events class |
131 | | - $location = null; |
132 | | - if ( $location_input !== 'near me' ) { |
133 | | - // Provide city name (WP will resolve coordinates) |
134 | | - $location = [ |
135 | | - 'description' => $location_input, |
136 | | - ]; |
137 | | - } |
138 | | - |
139 | | - // Instantiate WP_Community_Events with user ID (0) and optional location |
140 | | - $events_instance = new WP_Community_Events( $user_id, $location ); |
141 | | - |
142 | | - // Get events from WP_Community_Events |
143 | | - $events = $events_instance->get_events( $location_input ); |
| 118 | + 'callable' => function ( $params ) use ( $client ) { |
| 119 | + return $client->get_image_from_ai_service( $params['prompt'] ); |
| 120 | + }, |
| 121 | + ] |
| 122 | + ); |
144 | 123 |
|
145 | | - // Check for WP_Error |
146 | | - if ( is_wp_error( $events ) ) { |
147 | | - return [ 'error' => $events->get_error_message() ]; |
148 | | - } |
| 124 | + $server->register_tool( |
| 125 | + [ |
| 126 | + 'name' => 'fetch_wp_community_events', |
| 127 | + 'description' => 'Fetches upcoming WordPress community events near a specified city or the user\'s current location. If no events are found in the exact location, nearby events within a specific radius will be considered.', |
| 128 | + 'inputSchema' => [ |
| 129 | + 'type' => 'object', |
| 130 | + 'properties' => [ |
| 131 | + 'location' => [ |
| 132 | + 'type' => 'string', |
| 133 | + 'description' => 'City name or "near me" for auto-detected location. If no events are found in the exact location, the tool will also consider nearby events within a specified radius (default: 100 km).', |
| 134 | + ], |
| 135 | + ], |
| 136 | + 'required' => [ 'location' ], // We only require the location |
| 137 | + ], |
| 138 | + 'callable' => function ( $params ) { |
| 139 | + // Default user ID is 0 |
| 140 | + $user_id = 0; |
| 141 | + |
| 142 | + // Get the location from the parameters (already supplied in the prompt) |
| 143 | + $location_input = strtolower( trim( $params['location'] ) ); |
| 144 | + |
| 145 | + // Manually include the WP_Community_Events class if it's not loaded |
| 146 | + if ( ! class_exists( 'WP_Community_Events' ) ) { |
| 147 | + require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php'; |
| 148 | + } |
| 149 | + |
| 150 | + // Determine location for the WP_Community_Events class |
| 151 | + $location = null; |
| 152 | + if ( $location_input !== 'near me' ) { |
| 153 | + // Provide city name (WP will resolve coordinates) |
| 154 | + $location = [ |
| 155 | + 'description' => $location_input, |
| 156 | + ]; |
| 157 | + } |
| 158 | + |
| 159 | + // Instantiate WP_Community_Events with user ID (0) and optional location |
| 160 | + $events_instance = new WP_Community_Events( $user_id, $location ); |
| 161 | + |
| 162 | + // Get events from WP_Community_Events |
| 163 | + $events = $events_instance->get_events($location_input); |
| 164 | + |
| 165 | + // Check for WP_Error |
| 166 | + if ( is_wp_error( $events ) ) { |
| 167 | + return [ 'error' => $events->get_error_message() ]; |
| 168 | + } |
149 | 169 |
|
150 | 170 | // If no events found |
151 | | - if ( empty( $events['events'] ) ) { |
152 | | - return [ 'message' => 'No events found near ' . ( $location_input === 'near me' ? 'your location' : $location_input ) ]; |
153 | | - } |
| 171 | + if ( empty( $events['events'] ) ) { |
| 172 | + return [ 'message' => 'No events found near ' . ( $location_input === 'near me' ? 'your location' : $location_input ) ]; |
| 173 | + } |
154 | 174 |
|
155 | 175 | // Format and return the events correctly |
156 | | - $formatted_events = array_map( |
157 | | - function ( $event ) { |
158 | | - // Log event details to ensure properties are accessible |
159 | | - error_log( 'Event details: ' . print_r( $event, true ) ); |
| 176 | + $formatted_events = array_map( function ( $event ) { |
| 177 | + // Log event details to ensure properties are accessible |
| 178 | + error_log( 'Event details: ' . print_r( $event, true ) ); |
160 | 179 |
|
161 | | - // Initialize a formatted event string |
162 | | - $formatted_event = ''; |
| 180 | + // Initialize a formatted event string |
| 181 | + $formatted_event = ''; |
163 | 182 |
|
164 | | - // Format event title |
165 | | - if ( isset( $event['title'] ) ) { |
| 183 | + // Format event title |
| 184 | + if ( isset( $event['title'] ) ) { |
166 | 185 | $formatted_event .= $event['title'] . "\n"; |
167 | | - } |
| 186 | + } |
168 | 187 |
|
169 | | - // Format the date nicely |
170 | | - $formatted_event .= ' - Date: ' . ( isset( $event['date'] ) ? date( 'F j, Y g:i A', strtotime( $event['date'] ) ) : 'No date available' ) . "\n"; |
| 188 | + // Format the date nicely |
| 189 | + $formatted_event .= ' - Date: ' . ( isset( $event['date'] ) ? date( 'F j, Y g:i A', strtotime( $event['date'] ) ) : 'No date available' ) . "\n"; |
171 | 190 |
|
172 | | - // Format the location |
173 | | - if ( isset( $event['location']['location'] ) ) { |
174 | | - $formatted_event .= ' - Location: ' . $event['location']['location'] . "\n"; |
175 | | - } |
| 191 | + // Format the location |
| 192 | + if ( isset( $event['location']['location'] ) ) { |
| 193 | + $formatted_event .= ' - Location: ' . $event['location']['location'] . "\n"; |
| 194 | + } |
176 | 195 |
|
177 | | - // Format the event URL |
178 | | - $formatted_event .= isset( $event['url'] ) ? ' - URL: ' . $event['url'] . "\n" : ''; |
| 196 | + // Format the event URL |
| 197 | + $formatted_event .= isset( $event['url'] ) ? ' - URL: ' . $event['url'] . "\n" : ''; |
179 | 198 |
|
180 | | - return $formatted_event; |
181 | | - }, |
182 | | - $events['events'] |
183 | | - ); |
| 199 | + return $formatted_event; |
| 200 | + }, $events['events'] ); |
184 | 201 |
|
185 | 202 | // Combine the formatted events into a single string |
186 | | - $formatted_events_output = implode( "\n", $formatted_events ); |
| 203 | + $formatted_events_output = implode("\n", $formatted_events); |
187 | 204 |
|
188 | 205 | // Return the formatted events string |
189 | 206 | return [ |
190 | | - 'message' => 'OK. I found ' . count( $formatted_events ) . ' WordPress events near ' . ( $location_input === 'near me' ? 'your location' : $location_input ) . ":\n\n" . $formatted_events_output, |
| 207 | + 'message' => "OK. I found " . count($formatted_events) . " WordPress events near " . ( $location_input === 'near me' ? 'your location' : $location_input ) . ":\n\n" . $formatted_events_output |
191 | 208 | ]; |
192 | | - }, |
| 209 | + }, |
193 | 210 | ] |
194 | 211 | ); |
195 | 212 | } |
196 | 213 |
|
197 | 214 | // Register resources for AI access |
198 | | - private function register_resources( $server ) { |
| 215 | + private function register_resources($server) { |
199 | 216 | // Register Users resource |
200 | | - $server->register_resource( |
201 | | - [ |
| 217 | + $server->register_resource([ |
202 | 218 | 'name' => 'users', |
203 | 219 | 'uri' => 'data://users', |
204 | 220 | 'description' => 'List of users', |
205 | 221 | 'mimeType' => 'application/json', |
206 | 222 | 'dataKey' => 'users', // Data will be fetched from 'users' |
207 | | - ] |
208 | | - ); |
| 223 | + ]); |
209 | 224 |
|
210 | 225 | // Register Product Catalog resource |
211 | | - $server->register_resource( |
212 | | - [ |
| 226 | + $server->register_resource([ |
213 | 227 | 'name' => 'product_catalog', |
214 | 228 | 'uri' => 'file://./products.json', |
215 | 229 | 'description' => 'Product catalog', |
216 | 230 | 'mimeType' => 'application/json', |
217 | 231 | 'filePath' => './products.json', // Data will be fetched from products.json |
218 | | - ] |
219 | | - ); |
| 232 | + ]); |
220 | 233 | } |
221 | 234 | } |
0 commit comments