Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/MCP/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ static function () {
file_put_contents( $filename, $image_blob->get_binary_data() );

$image_url = $filename;
$image_id = \WP_CLI\AiCommand\MediaManager::upload_to_media_library($image_url);
}

break;
Expand All @@ -120,6 +121,8 @@ static function () {
// TODO: Save as file or so.
break;
}

return $image_id || 'no image found';
}

// See https://github.com/felixarntz/ai-services/blob/main/docs/Accessing-AI-Services-in-PHP.md for further processing.
Expand Down Expand Up @@ -188,17 +191,17 @@ static function () {

\WP_CLI::debug( 'Making request...' . print_r( $contents, true ), 'ai' );

if ( $service->get_service_slug() === 'openai' ) {
$model = 'gpt-4o';
} else {
$model = 'gemini-2.0-flash';
}
// if ( $service->get_service_slug() === 'openai' ) {
// $model = 'gpt-4o';
// } else {
// $model = 'gemini-2.0-flash';
// }

$candidates = $service
->get_model(
[
'feature' => 'text-generation',
'model' => $model,
// 'model' => $model,
'tools' => $tools,
'capabilities' => [
AI_Capability::MULTIMODAL_INPUT,
Expand Down
38 changes: 38 additions & 0 deletions src/MediaManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace WP_CLI\AiCommand;

class MediaManager {

public static function upload_to_media_library($media_path) {
// Get WordPress upload directory information
$upload_dir = wp_upload_dir();

// Get the file name from the path
$file_name = basename($media_path);

// Copy file to the upload directory
$new_file_path = $upload_dir['path'] . '/' . $file_name;
copy($media_path, $new_file_path);

// Prepare attachment data
$wp_filetype = wp_check_filetype($file_name, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($file_name),
'post_content' => '',
'post_status' => 'inherit'
);

// Insert the attachment
$attach_id = wp_insert_attachment($attachment, $new_file_path);

// Generate attachment metadata
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $new_file_path);
wp_update_attachment_metadata($attach_id, $attach_data);

return $attach_id;

}
}