1+ use base64:: Engine ;
12use futures_util:: StreamExt ;
23use once_cell:: sync:: Lazy ;
34use reqwest:: header:: { HeaderMap , HeaderValue , AUTHORIZATION , CONTENT_TYPE } ;
45use std:: collections:: HashMap ;
56use std:: sync:: Mutex ;
67use tauri:: { Emitter , Runtime } ;
78use tokio:: sync:: { mpsc, oneshot} ;
8- use base64:: Engine ;
99
1010static CANCEL_CHANNELS : Lazy < Mutex < HashMap < String , oneshot:: Sender < ( ) > > > > =
1111 Lazy :: new ( || Mutex :: new ( HashMap :: new ( ) ) ) ;
1212
13-
14-
1513#[ tauri:: command]
1614pub async fn chat_stream < R : Runtime > (
1715 window : tauri:: Window < R > ,
@@ -42,7 +40,10 @@ pub async fn chat_stream<R: Runtime>(
4240 . await
4341 . map_err ( |e| {
4442 println ! ( "请求发送失败: {}" , e) ;
45- window. clone ( ) . emit ( & format ! ( "chat-stream-error-{}" , request_id) , e. to_string ( ) ) . unwrap ( ) ;
43+ window
44+ . clone ( )
45+ . emit ( & format ! ( "chat-stream-error-{}" , request_id) , e. to_string ( ) )
46+ . unwrap ( ) ;
4647 e. to_string ( )
4748 } ) ?;
4849
@@ -134,10 +135,7 @@ pub async fn image_generate(
134135 AUTHORIZATION ,
135136 HeaderValue :: from_str ( & format ! ( "Bearer {}" , api_key) ) . map_err ( |e| e. to_string ( ) ) ?,
136137 ) ;
137- headers. insert (
138- "X-DashScope-Async" ,
139- HeaderValue :: from_static ( "enable" ) ,
140- ) ;
138+ headers. insert ( "X-DashScope-Async" , HeaderValue :: from_static ( "enable" ) ) ;
141139
142140 println ! ( "正在发送图像生成请求到: {}" , api_url) ;
143141 let response = client
@@ -154,7 +152,10 @@ pub async fn image_generate(
154152 if !response. status ( ) . is_success ( ) {
155153 let status = response. status ( ) ;
156154 let error_text = response. text ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
157- println ! ( "图像生成请求失败,状态码: {}, 错误信息: {}" , status, error_text) ;
155+ println ! (
156+ "图像生成请求失败,状态码: {}, 错误信息: {}" ,
157+ status, error_text
158+ ) ;
158159 return Err ( format ! ( "请求失败: {} - {}" , status, error_text) ) ;
159160 }
160161
@@ -163,10 +164,7 @@ pub async fn image_generate(
163164}
164165
165166#[ tauri:: command]
166- pub async fn image_result (
167- api_url : String ,
168- api_key : String ,
169- ) -> Result < serde_json:: Value , String > {
167+ pub async fn image_result ( api_url : String , api_key : String ) -> Result < serde_json:: Value , String > {
170168 let client = reqwest:: Client :: builder ( )
171169 . build ( )
172170 . map_err ( |e| e. to_string ( ) ) ?;
@@ -192,12 +190,15 @@ pub async fn image_result(
192190 if !response. status ( ) . is_success ( ) {
193191 let status = response. status ( ) ;
194192 let error_text = response. text ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
195- println ! ( "获取图像生成结果失败,状态码: {}, 错误信息: {}" , status, error_text) ;
193+ println ! (
194+ "获取图像生成结果失败,状态码: {}, 错误信息: {}" ,
195+ status, error_text
196+ ) ;
196197 return Err ( format ! ( "请求失败: {} - {}" , status, error_text) ) ;
197198 }
198199
199200 let mut response_json: serde_json:: Value = response. json ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
200-
201+
201202 // 如果任务成功完成,获取图片并转换为base64
202203 if let Some ( output) = response_json. get ( "output" ) {
203204 if let Some ( task_status) = output. get ( "task_status" ) {
@@ -211,19 +212,24 @@ pub async fn image_result(
211212 . send ( )
212213 . await
213214 . map_err ( |e| e. to_string ( ) ) ?;
214-
215- let image_bytes = image_response. bytes ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
216-
215+
216+ let image_bytes =
217+ image_response. bytes ( ) . await . map_err ( |e| e. to_string ( ) ) ?;
218+
217219 // 转换为base64
218- let base64_image = base64:: engine:: general_purpose:: STANDARD . encode ( & image_bytes) ;
220+ let base64_image =
221+ base64:: engine:: general_purpose:: STANDARD . encode ( & image_bytes) ;
219222 let data_url = format ! ( "data:image/png;base64,{}" , base64_image) ;
220-
223+
221224 // 创建新的结果对象
222225 if let Some ( output) = response_json. get_mut ( "output" ) {
223226 if let Some ( results) = output. get_mut ( "results" ) {
224227 if let Some ( first_result) = results. get_mut ( 0 ) {
225228 if let Some ( obj) = first_result. as_object_mut ( ) {
226- obj. insert ( "base64" . to_string ( ) , serde_json:: Value :: String ( data_url) ) ;
229+ obj. insert (
230+ "base64" . to_string ( ) ,
231+ serde_json:: Value :: String ( data_url) ,
232+ ) ;
227233 }
228234 }
229235 }
@@ -234,6 +240,6 @@ pub async fn image_result(
234240 }
235241 }
236242 }
237-
243+
238244 Ok ( response_json)
239245}
0 commit comments