Skip to content

Commit bbb3b5a

Browse files
committed
Merge branch 'main' into feature/create_post
2 parents 49a8874 + 21ab49d commit bbb3b5a

File tree

4 files changed

+110
-111
lines changed

4 files changed

+110
-111
lines changed

src/AiCommand.php

Lines changed: 90 additions & 77 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;
@@ -54,19 +54,19 @@ public function __construct(
5454
*/
5555
public function __invoke( $args, $assoc_args ) {
5656
$server = new MCP\Server();
57-
$client = new MCP\Client( $server );
57+
$client = new MCP\Client($server);
5858

59-
$this->register_tools( $server, $client );
59+
$this->register_tools($server, $client);
6060

61-
$this->register_resources( $server );
61+
$this->register_resources($server);
6262

6363
$result = $client->call_ai_service_with_prompt( $args[0] );
6464

6565
WP_CLI::success( $result );
6666
}
6767

6868
// Register tools for AI processing
69-
private function register_tools( $server, $client ) {
69+
private function register_tools($server, $client) {
7070
$server->register_tool(
7171
[
7272
'name' => 'list_tools',
@@ -103,119 +103,132 @@ private function register_tools( $server, $client ) {
103103

104104
$server->register_tool(
105105
[
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.',
108108
'inputSchema' => [
109109
'type' => 'object',
110110
'properties' => [
111-
'location' => [
111+
'prompt' => [
112112
'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.',
114114
],
115115
],
116-
'required' => [ 'location' ], // We only require the location
116+
'required' => [ 'prompt' ],
117117
],
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+
);
144123

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

150170
// 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+
}
154174

155175
// 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 ) );
160179

161-
// Initialize a formatted event string
162-
$formatted_event = '';
180+
// Initialize a formatted event string
181+
$formatted_event = '';
163182

164-
// Format event title
165-
if ( isset( $event['title'] ) ) {
183+
// Format event title
184+
if ( isset( $event['title'] ) ) {
166185
$formatted_event .= $event['title'] . "\n";
167-
}
186+
}
168187

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

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

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

180-
return $formatted_event;
181-
},
182-
$events['events']
183-
);
199+
return $formatted_event;
200+
}, $events['events'] );
184201

185202
// Combine the formatted events into a single string
186-
$formatted_events_output = implode( "\n", $formatted_events );
203+
$formatted_events_output = implode("\n", $formatted_events);
187204

188205
// Return the formatted events string
189206
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
191208
];
192-
},
209+
},
193210
]
194211
);
195212
}
196213

197214
// Register resources for AI access
198-
private function register_resources( $server ) {
215+
private function register_resources($server) {
199216
// Register Users resource
200-
$server->register_resource(
201-
[
217+
$server->register_resource([
202218
'name' => 'users',
203219
'uri' => 'data://users',
204220
'description' => 'List of users',
205221
'mimeType' => 'application/json',
206222
'dataKey' => 'users', // Data will be fetched from 'users'
207-
]
208-
);
223+
]);
209224

210225
// Register Product Catalog resource
211-
$server->register_resource(
212-
[
226+
$server->register_resource([
213227
'name' => 'product_catalog',
214228
'uri' => 'file://./products.json',
215229
'description' => 'Product catalog',
216230
'mimeType' => 'application/json',
217231
'filePath' => './products.json', // Data will be fetched from products.json
218-
]
219-
);
232+
]);
220233
}
221234
}

src/MCP/Client.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,28 +191,28 @@ static function () {
191191
if ( $service->get_service_slug() === 'openai' ) {
192192
$model = 'gpt-4o';
193193
} else {
194-
$model = 'gemini-2.0-flash-exp';
194+
$model = 'gemini-2.0-flash';
195195
}
196196

197197
$candidates = $service
198198
->get_model(
199199
[
200200
'feature' => 'text-generation',
201201
'model' => $model,
202-
// 'tools' => $tools,
202+
'tools' => $tools,
203203
'capabilities' => [
204204
AI_Capability::MULTIMODAL_INPUT,
205205
AI_Capability::TEXT_GENERATION,
206-
// AI_Capability::FUNCTION_CALLING,
206+
AI_Capability::FUNCTION_CALLING,
207207
],
208-
'generationConfig' => Text_Generation_Config::from_array(
209-
array(
210-
'responseModalities' => array(
211-
'Text',
212-
'Image',
213-
),
214-
)
215-
),
208+
// 'generationConfig' => Text_Generation_Config::from_array(
209+
// array(
210+
// 'responseModalities' => array(
211+
// 'Text',
212+
// 'Image',
213+
// ),
214+
// )
215+
// ),
216216
]
217217
)
218218
->generate_text( $contents );
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
<?php
22

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

55
class FileTools {
66

7-
private $server;
8-
97
public function __construct( $server ) {
10-
$this->server = $server;
11-
$this->register_tools();
12-
}
13-
14-
public function register_tools() {
15-
$this->server->register_tool(
8+
$server->register_tool(
169
[
1710
'name' => 'write_file',
1811
'description' => 'Writes a file.',
@@ -38,7 +31,7 @@ public function register_tools() {
3831
]
3932
);
4033

41-
$this->server->register_tool(
34+
$server->register_tool(
4235
[
4336
'name' => 'delete_file',
4437
'description' => 'Deletes a file.',
@@ -59,7 +52,7 @@ public function register_tools() {
5952
]
6053
);
6154

62-
$this->server->register_tool(
55+
$server->register_tool(
6356
[
6457
'name' => 'read_file',
6558
'description' => 'Reads a file.',
@@ -80,7 +73,7 @@ public function register_tools() {
8073
]
8174
);
8275

83-
$this->server->register_tool(
76+
$server->register_tool(
8477
[
8578
'name' => 'move_file',
8679
'description' => 'Moves a file.',
@@ -106,7 +99,7 @@ public function register_tools() {
10699
]
107100
);
108101

109-
$this->server->register_tool(
102+
$server->register_tool(
110103
[
111104
'name' => 'list_files',
112105
'description' => 'Lists files in a directory.',
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
<?php
22

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

55
class URLTools {
66

7-
private $server;
8-
97
public function __construct( $server ) {
10-
$this->server = $server;
11-
$this->register_tools();
12-
}
13-
14-
public function register_tools() {
15-
$this->server->register_tool(
8+
$server->register_tool(
169
[
1710
'name' => 'retrieve_page',
1811
'description' => 'Retrieves a page from the web.',
@@ -35,7 +28,7 @@ public function register_tools() {
3528
]
3629
);
3730

38-
$this->server->register_tool(
31+
$server->register_tool(
3932
[
4033
'name' => 'retrieve_rss_feed',
4134
'description' => 'Retrieves an RSS feed.',

0 commit comments

Comments
 (0)