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