Skip to content

Commit 776aa78

Browse files
committed
Fix namespace nonsense
1 parent 685e03f commit 776aa78

File tree

3 files changed

+87
-101
lines changed

3 files changed

+87
-101
lines changed

src/AiCommand.php

Lines changed: 85 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace WP_CLI\AiCommand;
44

5-
use AiCommand\Tools\FileTools;
6-
use AiCommand\Tools\URLTools;
5+
use WP_CLI\AiCommand\Tools\FileTools;
6+
use WP_CLI\AiCommand\Tools\URLTools;
77
use WP_CLI;
88
use WP_CLI_Command;
99
use WP_Community_Events;
@@ -48,19 +48,19 @@ class AiCommand extends WP_CLI_Command {
4848
*/
4949
public function __invoke( $args, $assoc_args ) {
5050
$server = new MCP\Server();
51-
$client = new MCP\Client( $server );
51+
$client = new MCP\Client($server);
5252

53-
$this->register_tools( $server, $client );
53+
$this->register_tools($server, $client);
5454

55-
$this->register_resources( $server );
55+
$this->register_resources($server);
5656

5757
$result = $client->call_ai_service_with_prompt( $args[0] );
5858

5959
WP_CLI::success( $result );
6060
}
6161

6262
// Register tools for AI processing
63-
private function register_tools( $server, $client ) {
63+
private function register_tools($server, $client) {
6464
$server->register_tool(
6565
[
6666
'name' => 'list_tools',
@@ -97,146 +97,132 @@ private function register_tools( $server, $client ) {
9797

9898
$server->register_tool(
9999
[
100-
'name' => 'calculate_total',
101-
'description' => 'Calculates the total price.',
100+
'name' => 'generate_image',
101+
'description' => 'Generates an image.',
102102
'inputSchema' => [
103103
'type' => 'object',
104104
'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.',
112108
],
113109
],
114-
'required' => [ 'price', 'quantity' ],
110+
'required' => [ 'prompt' ],
115111
],
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'] );
121114
},
122115
]
123116
);
124117

125118
$server->register_tool(
126119
[
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
136131
],
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+
}
170163

171164
// 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+
}
175168

176169
// 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 ) );
181173

182-
// Initialize a formatted event string
183-
$formatted_event = '';
174+
// Initialize a formatted event string
175+
$formatted_event = '';
184176

185-
// Format event title
186-
if ( isset( $event['title'] ) ) {
177+
// Format event title
178+
if ( isset( $event['title'] ) ) {
187179
$formatted_event .= $event['title'] . "\n";
188-
}
180+
}
189181

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";
192184

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+
}
197189

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" : '';
200192

201-
return $formatted_event;
202-
},
203-
$events['events']
204-
);
193+
return $formatted_event;
194+
}, $events['events'] );
205195

206196
// Combine the formatted events into a single string
207-
$formatted_events_output = implode( "\n", $formatted_events );
197+
$formatted_events_output = implode("\n", $formatted_events);
208198

209199
// Return the formatted events string
210200
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
212202
];
213-
},
203+
},
214204
]
215205
);
216206
}
217207

218208
// Register resources for AI access
219-
private function register_resources( $server ) {
209+
private function register_resources($server) {
220210
// Register Users resource
221-
$server->register_resource(
222-
[
211+
$server->register_resource([
223212
'name' => 'users',
224213
'uri' => 'data://users',
225214
'description' => 'List of users',
226215
'mimeType' => 'application/json',
227216
'dataKey' => 'users', // Data will be fetched from 'users'
228-
]
229-
);
217+
]);
230218

231219
// Register Product Catalog resource
232-
$server->register_resource(
233-
[
220+
$server->register_resource([
234221
'name' => 'product_catalog',
235222
'uri' => 'file://./products.json',
236223
'description' => 'Product catalog',
237224
'mimeType' => 'application/json',
238225
'filePath' => './products.json', // Data will be fetched from products.json
239-
]
240-
);
226+
]);
241227
}
242228
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace AiCommand\Tools;
3+
namespace WP_CLI\AiCommand\Tools;
44

55
class FileTools {
66

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace AiCommand\Tools;
3+
namespace WP_CLI\AiCommand\Tools;
44

55
class URLTools {
66

0 commit comments

Comments
 (0)