Fix automatic translation request handling

This commit is contained in:
2026-08-01 19:18:01 +02:00
parent bda3c400ef
commit 932b73cd97
5 changed files with 101 additions and 50 deletions

View File

@@ -121,6 +121,42 @@ pub struct OneShotRequest {
pub content: Value,
}
pub fn post_translation_request(
title: &str,
excerpt: Option<&str>,
content: &str,
target_language: &str,
) -> OneShotRequest {
OneShotRequest {
operation: OneShotOperation::TranslatePost {
target_language: target_language.to_string(),
},
content: json!({
"title": title,
"excerpt": excerpt.unwrap_or_default(),
"content": content,
}),
}
}
pub fn media_translation_request(
title: Option<&str>,
alt: Option<&str>,
caption: Option<&str>,
target_language: &str,
) -> OneShotRequest {
OneShotRequest {
operation: OneShotOperation::TranslateMedia {
target_language: target_language.to_string(),
},
content: json!({
"title": title.unwrap_or_default(),
"alt": alt.unwrap_or_default(),
"caption": caption.unwrap_or_default(),
}),
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaxonomySuggestion {
pub tags: Vec<String>,
@@ -1477,6 +1513,33 @@ mod tests {
);
}
#[test]
fn translation_requests_normalize_missing_fields() {
let post = post_translation_request("Hello", None, "Body", "de");
assert_eq!(
post.operation,
OneShotOperation::TranslatePost {
target_language: "de".to_string()
}
);
assert_eq!(
post.content,
json!({"title": "Hello", "excerpt": "", "content": "Body"})
);
let media = media_translation_request(None, None, None, "fr");
assert_eq!(
media.operation,
OneShotOperation::TranslateMedia {
target_language: "fr".to_string()
}
);
assert_eq!(
media.content,
json!({"title": "", "alt": "", "caption": ""})
);
}
#[test]
fn run_one_shot_supports_image_analysis_via_airplane_endpoint() {
let response = run_airplane_one_shot(

View File

@@ -2,16 +2,11 @@ use std::collections::HashSet;
use std::fs;
use std::path::Path;
use serde_json::json;
use crate::db::DbConnection as Connection;
use crate::db::queries::{
media as qm, media_translation as qmt, post as qp, post_media, post_translation,
};
use crate::engine::ai::{
self, MediaTranslationResult, OneShotOperation, OneShotRequest, OneShotResponse,
TranslationResult,
};
use crate::engine::ai::{self, MediaTranslationResult, OneShotResponse, TranslationResult};
use crate::engine::{EngineError, EngineResult};
use crate::i18n::{UiLocale, translate, translate_with};
use crate::model::{Media, Post, PostStatus};
@@ -445,16 +440,12 @@ fn translate_post_ai(
match ai::run_one_shot(
conn,
offline_mode,
&OneShotRequest {
operation: OneShotOperation::TranslatePost {
target_language: language.to_string(),
},
content: json!({
"title": post.title,
"excerpt": post.excerpt,
"content": post.content,
}),
},
&ai::post_translation_request(
&post.title,
post.excerpt.as_deref(),
post.content.as_deref().unwrap_or_default(),
language,
),
)? {
(OneShotResponse::Translation(result), _usage) => Ok(result),
_ => Err(EngineError::Parse(
@@ -472,16 +463,12 @@ fn translate_media_ai(
match ai::run_one_shot(
conn,
offline_mode,
&OneShotRequest {
operation: OneShotOperation::TranslateMedia {
target_language: language.to_string(),
},
content: json!({
"title": media.title,
"alt": media.alt,
"caption": media.caption,
}),
},
&ai::media_translation_request(
media.title.as_deref(),
media.alt.as_deref(),
media.caption.as_deref(),
language,
),
)? {
(OneShotResponse::MediaTranslation(result), _usage) => Ok(result),
_ => Err(EngineError::Parse(