This commit is contained in:
2026-04-20 12:40:24 -04:00
parent 2e7789df63
commit fbd1728aee
4 changed files with 59 additions and 15 deletions

View File

@@ -32,12 +32,19 @@ struct Delta {
content: Option<String>,
}
#[derive(Serialize, Clone)]
struct TranslationChunkEvent {
request_id: String,
chunk: String,
}
#[tauri::command]
async fn translate(
app: AppHandle,
api_address: String,
api_key: String,
payload: TranslationPayload,
request_id: String,
) -> Result<String, String> {
let client = Client::new();
// Ensure URL doesn't have double slashes if api_address ends with /
@@ -55,6 +62,12 @@ async fn translate(
.await
.map_err(|e| e.to_string())?;
let status = res.status();
if !status.is_success() {
let error_text = res.text().await.unwrap_or_else(|_| format!("HTTP {}", status));
return Err(format!("HTTP {}: {}", status, error_text));
}
if !payload.stream {
let text = res.text().await.map_err(|e| e.to_string())?;
return Ok(text);
@@ -78,7 +91,11 @@ async fn translate(
if let Some(choice) = json.choices.get(0) {
if let Some(delta) = &choice.delta {
if let Some(content) = &delta.content {
app.emit("translation-chunk", content).map_err(|e| e.to_string())?;
let event = TranslationChunkEvent {
request_id: request_id.clone(),
chunk: content.clone(),
};
app.emit("translation-chunk", event).map_err(|e| e.to_string())?;
}
}
}