88use Felix_Arntz \AI_Services \Services \API \Helpers ;
99use Felix_Arntz \AI_Services \Services \API \Types \Content ;
1010use Felix_Arntz \AI_Services \Services \API \Types \Parts ;
11+ use Felix_Arntz \AI_Services \Services \API \Types \Parts \File_Data_Part ;
1112use Felix_Arntz \AI_Services \Services \API \Types \Parts \Function_Call_Part ;
13+ use Felix_Arntz \AI_Services \Services \API \Types \Parts \Inline_Data_Part ;
1214use Felix_Arntz \AI_Services \Services \API \Types \Parts \Text_Part ;
1315use Felix_Arntz \AI_Services \Services \API \Types \Tools ;
16+ use WP_CLI ;
1417
1518class Client {
1619
@@ -55,7 +58,8 @@ public function read_resource( $uri ) {
5558 return $ this ->sendRequest ( 'resources/read ' , [ 'uri ' => $ uri ] );
5659 }
5760
58- public function generate_image ( string $ prompt ) {
61+ // Must not have the same name as the tool, otherwise it takes precedence.
62+ public function get_image_from_ai_service ( string $ prompt ) {
5963 // See https://github.com/felixarntz/ai-services/issues/25.
6064 add_filter (
6165 'map_meta_cap ' ,
@@ -83,26 +87,43 @@ static function () {
8387 )
8488 ->generate_image ( $ prompt );
8589
86- $ image_url = '' ;
87- foreach ( $ candidates ->get ( 0 )->get_content ()->get_parts () as $ part ) {
88- if ( $ part instanceof \Felix_Arntz \AI_Services \Services \API \Types \Parts \Inline_Data_Part ) {
89- $ image_url = $ part ->get_base64_data (); // Data URL.
90- break ;
91- }
92- if ( $ part instanceof \Felix_Arntz \AI_Services \Services \API \Types \Parts \File_Data_Part ) {
93- $ image_url = $ part ->get_file_uri (); // Actual URL. May have limited TTL (often 1 hour).
94- break ;
90+ } catch ( Exception $ e ) {
91+ WP_CLI ::error ( $ e ->getMessage () );
92+ }
93+
94+ $ image_url = '' ;
95+ foreach ( $ candidates ->get ( 0 )->get_content ()->get_parts () as $ part ) {
96+ if ( $ part instanceof Inline_Data_Part ) {
97+ $ image_url = $ part ->get_base64_data (); // Data URL.
98+ $ image_blob = Helpers::base64_data_url_to_blob ( $ image_url );
99+
100+ if ( $ image_blob ) {
101+ $ filename = tempnam ("/tmp " , "ai-generated-image " );
102+ $ parts = explode ( "/ " , $ part ->get_mime_type () );
103+ $ extension = $ parts [1 ];
104+ rename ( $ filename , $ filename . ". " . $ extension );
105+ $ filename .= ". " . $ extension ;
106+
107+ file_put_contents ($ filename , $ image_blob ->get_binary_data () );
108+
109+ $ image_url = $ filename ;
95110 }
96- }
97111
98- // See https://github.com/felixarntz/ai-services/blob/main/docs/Accessing-AI-Services-in-PHP.md for further processing.
112+ break ;
113+ }
99114
100- return $ image_url ;
101- } catch ( Exception $ e ) {
102- \WP_CLI ::error ( $ e ->getMessage () );
115+ if ( $ part instanceof File_Data_Part ) {
116+ $ image_url = $ part ->get_file_uri (); // Actual URL. May have limited TTL (often 1 hour).
117+ // TODO: Save as file or so.
118+ break ;
119+ }
103120 }
104121
105- \WP_CLI ::error ( 'Could not generate image. ' );
122+ // See https://github.com/felixarntz/ai-services/blob/main/docs/Accessing-AI-Services-in-PHP.md for further processing.
123+
124+ WP_CLI ::log ( "Generated image: $ image_url " );
125+
126+ return $ image_url ;
106127 }
107128
108129 public function call_ai_service_with_prompt ( string $ prompt ) {
@@ -174,6 +195,7 @@ static function () {
174195 }
175196 $ text .= $ part ->get_text ();
176197 } elseif ( $ part instanceof Function_Call_Part ) {
198+ var_dump ('call function ' , $ part );
177199 $ function_result = $ this ->{$ part ->get_name ()}( $ part ->get_args () );
178200
179201 // Odd limitation of add_function_response_part().
@@ -200,7 +222,7 @@ static function () {
200222
201223 return $ text ;
202224 } catch ( Exception $ e ) {
203- \ WP_CLI ::error ( $ e ->getMessage () );
225+ WP_CLI ::error ( $ e ->getMessage () );
204226 }
205227 }
206228}
0 commit comments