66use Felix_Arntz \AI_Services \Services \API \Enums \AI_Capability ;
77use Felix_Arntz \AI_Services \Services \API \Enums \Content_Role ;
88use Felix_Arntz \AI_Services \Services \API \Helpers ;
9+ use Felix_Arntz \AI_Services \Services \API \Types \Blob ;
910use Felix_Arntz \AI_Services \Services \API \Types \Content ;
1011use Felix_Arntz \AI_Services \Services \API \Types \Parts ;
1112use Felix_Arntz \AI_Services \Services \API \Types \Parts \File_Data_Part ;
1213use Felix_Arntz \AI_Services \Services \API \Types \Parts \Function_Call_Part ;
1314use Felix_Arntz \AI_Services \Services \API \Types \Parts \Inline_Data_Part ;
1415use Felix_Arntz \AI_Services \Services \API \Types \Parts \Text_Part ;
16+ use Felix_Arntz \AI_Services \Services \API \Types \Text_Generation_Config ;
1517use Felix_Arntz \AI_Services \Services \API \Types \Tools ;
1618use WP_CLI ;
1719
@@ -128,11 +130,23 @@ static function () {
128130 }
129131
130132 public function call_ai_service_with_prompt ( string $ prompt ) {
133+ \WP_CLI ::debug ( "Prompt: {$ prompt }" , 'mcp_server ' );
131134 $ parts = new Parts ();
132135 $ parts ->add_text_part ( $ prompt );
133- $ content = new Content ( Content_Role::USER , $ parts );
134136
135- return $ this ->call_ai_service ( [ $ content ] );
137+ $ contents = [
138+ new Content ( Content_Role::USER , $ parts ),
139+ ];
140+
141+ // $parts = new Parts();
142+ // $parts->add_inline_data_part(
143+ // 'image/png',
144+ // Helpers::blob_to_base64_data_url( new Blob( file_get_contents( '/private/tmp/ai-generated-imaget1sjmomi30i31C1YtZy.png' ), 'image/png' ) ),
145+ // );
146+ //
147+ // $contents[] = $parts;
148+
149+ return $ this ->call_ai_service ( $ contents );
136150 }
137151
138152 private function call_ai_service ( $ contents ) {
@@ -172,7 +186,7 @@ static function () {
172186 ]
173187 );
174188
175- \WP_CLI ::debug ( 'Making request... ' . print_r ( $ contents , true ), 'ai ' );
189+ \WP_CLI ::debug ( 'Making request... ' . print_r ( $ contents , true ), 'ai ' );
176190
177191 if ( $ service ->get_service_slug () === 'openai ' ) {
178192 $ model = 'gpt-4o ' ;
@@ -183,14 +197,22 @@ static function () {
183197 $ candidates = $ service
184198 ->get_model (
185199 [
186- 'feature ' => 'text-generation ' ,
187- 'model ' => $ model ,
188- 'tools ' => $ tools ,
189- 'capabilities ' => [
190- AI_Capability::MULTIMODAL_INPUT ,
191- AI_Capability::TEXT_GENERATION ,
192- AI_Capability::FUNCTION_CALLING ,
193- ],
200+ 'feature ' => 'text-generation ' ,
201+ 'model ' => $ model ,
202+ 'tools ' => $ tools ,
203+ 'capabilities ' => [
204+ AI_Capability::MULTIMODAL_INPUT ,
205+ AI_Capability::TEXT_GENERATION ,
206+ AI_Capability::FUNCTION_CALLING ,
207+ ],
208+ // 'generationConfig' => Text_Generation_Config::from_array(
209+ // array(
210+ // 'responseModalities' => array(
211+ // 'Text',
212+ // 'Image',
213+ // ),
214+ // )
215+ // ),
194216 ]
195217 )
196218 ->generate_text ( $ contents );
@@ -224,6 +246,46 @@ static function () {
224246 $ parts ->add_function_response_part ( $ part ->get_id (), $ part ->get_name (), $ function_result );
225247 $ content = new Content ( Content_Role::USER , $ parts );
226248 $ new_contents [] = $ content ;
249+ } elseif ( $ part instanceof Inline_Data_Part ) {
250+ $ image_url = $ part ->get_base64_data (); // Data URL.
251+ $ image_blob = Helpers::base64_data_url_to_blob ( $ image_url );
252+
253+ if ( $ image_blob ) {
254+ $ filename = tempnam ( '/tmp ' , 'ai-generated-image ' );
255+ $ parts = explode ( '/ ' , $ part ->get_mime_type () );
256+ $ extension = $ parts [1 ];
257+ rename ( $ filename , $ filename . '. ' . $ extension );
258+ $ filename .= '. ' . $ extension ;
259+
260+ file_put_contents ( $ filename , $ image_blob ->get_binary_data () );
261+
262+ $ image_url = $ filename ;
263+ } else {
264+ $ binary_data = base64_decode ( $ image_url );
265+ if ( false !== $ binary_data ) {
266+ $ image_blob = new Blob ( $ binary_data , $ part ->get_mime_type () );
267+
268+ $ filename = tempnam ( '/tmp ' , 'ai-generated-image ' );
269+ $ parts = explode ( '/ ' , $ part ->get_mime_type () );
270+ $ extension = $ parts [1 ];
271+ rename ( $ filename , $ filename . '. ' . $ extension );
272+ $ filename .= '. ' . $ extension ;
273+
274+ file_put_contents ( $ filename , $ image_blob ->get_binary_data () );
275+
276+ $ image_url = $ filename ;
277+ }
278+ }
279+
280+ $ text .= "Generated image: $ image_url \n" ;
281+
282+ break ;
283+ }
284+
285+ if ( $ part instanceof File_Data_Part ) {
286+ $ image_url = $ part ->get_file_uri (); // Actual URL. May have limited TTL (often 1 hour).
287+ // TODO: Save as file or so.
288+ break ;
227289 }
228290 }
229291
0 commit comments