diff --git a/CHANGELOG.md b/CHANGELOG.md index c8847b4..0204aa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ All notable changes are listed here. Versions follow [Semantic Versioning](https://semver.org/). +## [0.8.5] — 2026-10-21 + +- Translation with a ChatGPT subscription works: the list of models the subscription answers + with is now understood (it differs from the API's), so requests no longer go out without a + model and fail with "error 400". OpenAI's preferred model is used. A failed check now shows + in Settings › Translation, and translation errors are written to chesspenguin.log. +- Comments of a sentence or two in another language are offered for translation too, and text + in brackets ("[source: …]") is translated as well. + ## [0.8.4] — 2026-10-21 - Move notation (Settings › Board & pieces): Latin letters (Nf3), figurines as on Lichess diff --git a/Cargo.lock b/Cargo.lock index 088fa74..d3aba33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -661,7 +661,7 @@ dependencies = [ [[package]] name = "chess-core" -version = "0.8.4" +version = "0.8.5" dependencies = [ "encoding_rs", "pgn-reader", @@ -671,7 +671,7 @@ dependencies = [ [[package]] name = "chess-import" -version = "0.8.4" +version = "0.8.5" dependencies = [ "chess-core", "encoding_rs", @@ -684,7 +684,7 @@ dependencies = [ [[package]] name = "chesspenguin" -version = "0.8.4" +version = "0.8.5" dependencies = [ "arboard", "base64 0.22.1", @@ -4290,7 +4290,7 @@ checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" [[package]] name = "uci" -version = "0.8.4" +version = "0.8.5" dependencies = [ "thiserror 2.0.21", ] diff --git a/Cargo.toml b/Cargo.toml index 882c4d9..662790b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "3" members = ["crates/chess-core", "crates/chess-import", "crates/uci", "apps/desktop"] [workspace.package] -version = "0.8.4" +version = "0.8.5" edition = "2024" rust-version = "1.97" license = "GPL-3.0-or-later" diff --git a/apps/desktop/src/chatgpt.rs b/apps/desktop/src/chatgpt.rs index 1ab25e4..8064025 100644 --- a/apps/desktop/src/chatgpt.rs +++ b/apps/desktop/src/chatgpt.rs @@ -504,27 +504,61 @@ pub fn models() -> Result, Failure> { } let v: Value = serde_json::from_str(&body).map_err(|e| Failure::Service(status, e.to_string()))?; - let mut ids: Vec = v - .get("data") - .and_then(Value::as_array) - .into_iter() - .flatten() - .filter(|m| { - m.get("visibility") + Ok(model_list(&v)) +} + +/// The listed models, OpenAI's preferred first. The plan's answer is +/// `{"models": [{"slug", "visibility", "priority", "supported_in_api"}]}`; the API's +/// usual `{"data": [{"id"}]}` is read too. +fn model_list(v: &Value) -> Vec { + let mut found: Vec<(i64, String)> = Vec::new(); + for (key, name) in [("models", "slug"), ("data", "id")] { + for m in v.get(key).and_then(Value::as_array).into_iter().flatten() { + let listed = m + .get("visibility") .and_then(Value::as_str) - .is_none_or(|s| s == "list") - }) - .filter_map(|m| m.get("id").and_then(Value::as_str).map(str::to_owned)) - .collect(); - ids.sort(); - Ok(ids) + .is_none_or(|s| s == "list"); + let api = m + .get("supported_in_api") + .and_then(Value::as_bool) + .unwrap_or(true); + if let (true, true, Some(id)) = (listed, api, m.get(name).and_then(Value::as_str)) { + let priority = m + .get("priority") + .and_then(Value::as_i64) + .unwrap_or(i64::MAX); + found.push((priority, id.to_owned())); + } + } + } + found.sort_by_key(|(p, _)| *p); + found.into_iter().map(|(_, id)| id).collect() } /// The model to start with: a small fast one if listed (translation needs no more). pub fn default_model(models: &[String]) -> Option { - ["mini", "fast", "flash"] + // OpenAI's preferred chat model (the list comes in its order); models for other + // work (code review, search, sound, pictures) do not take a plain text request. + const OTHER: [&str; 12] = [ + "codex", + "search", + "research", + "audio", + "realtime", + "image", + "tts", + "transcribe", + "embedding", + "moderation", + "computer", + "review", + ]; + models .iter() - .find_map(|w| models.iter().find(|m| m.contains(w))) + .find(|m| { + let m = m.to_lowercase(); + !OTHER.iter().any(|w| m.contains(w)) + }) .or_else(|| models.first()) .cloned() } @@ -709,6 +743,31 @@ mod tests { assert_eq!(nonce_of(&t).as_deref(), Some("n1")); } + #[test] + fn the_plan_model_list_is_read() { + // The shape the plan's /v1/models answers with (seen 2026-10-21). + let v: Value = serde_json::from_str( + r#"{"models": [ + {"slug": "gpt-6-luna", "visibility": "list", "priority": 5, "supported_in_api": true}, + {"slug": "codex-auto-review", "visibility": "hide", "priority": 0, "supported_in_api": true}, + {"slug": "gpt-6.1-sol", "visibility": "list", "priority": 1, "supported_in_api": true}, + {"slug": "gpt-reserve", "visibility": "hide", "priority": 3} + ]}"#, + ) + .unwrap(); + let list = model_list(&v); + assert_eq!(list, vec!["gpt-6.1-sol", "gpt-6-luna"]); + assert_eq!(default_model(&list).as_deref(), Some("gpt-6.1-sol")); + let api: Value = serde_json::from_str(r#"{"data": [{"id": "gpt-x"}]}"#).unwrap(); + assert_eq!(model_list(&api), vec!["gpt-x"]); + let m = |v: &[&str]| default_model(&v.iter().map(|s| s.to_string()).collect::>()); + assert_eq!( + m(&["gpt-6.1-codex", "gpt-6.1-sol"]), + Some("gpt-6.1-sol".into()) + ); + assert_eq!(m(&[]), None); + } + #[test] fn the_plan_scope_is_checked() { let t = Tokens { diff --git a/apps/desktop/src/translate.rs b/apps/desktop/src/translate.rs index 6bf8beb..14b08f7 100644 --- a/apps/desktop/src/translate.rs +++ b/apps/desktop/src/translate.rs @@ -430,7 +430,7 @@ pub fn plan(texts: &[String], target: Lang) -> Option { .map(|(i, _)| i) .collect(); let foreign_letters: usize = foreign.iter().map(|&i| detected[i].0).sum(); - if foreign.len() < 3 && foreign_letters < 150 { + if foreign.len() < 3 && foreign_letters < 60 { return None; } let source = letters @@ -595,7 +595,7 @@ fn instructions(target: Lang) -> String { Rules:\n\ 1. Translate only the natural-language text of each comment into {lang}.\n\ 2. Copy every chess move and square exactly as written, character for character: e4, Nf3, Bxe5+, exd8=Q#, O-O, O-O-O, 12.Nf3, 12...Nf6, the d-file, the e5-square. Never translate, transliterate or replace the piece letters K, Q, R, B, N inside moves, and never add or remove moves.\n\ - 3. Keep the names of people, events and places as written; keep numbers, results (1-0, ½-½), evaluation symbols (+-, =, ±, ∓, !, ?, !?, ?!), URLs and anything in square brackets.\n\ + 3. Keep the names of people, events and places as written; keep numbers, results (1-0, ½-½), evaluation symbols (+-, =, ±, ∓, !, ?, !?, ?!) and URLs. Text in brackets is text: translate it too.\n\ 4. A part that is already in {lang} stays as it is.\n\ 5. Use the chess terms {lang}-speaking players use. Glossary: {glossary}.\n\ 6. Do not add, drop, explain, summarize or comment on anything. Keep the tone, the paragraph breaks and the meaning.\n\ @@ -1012,7 +1012,7 @@ fn lang_code(l: Lang) -> &'static str { /// The format version is part of the key: new instructions give new translations. fn key(text: &str) -> String { let mut h = Sha256::new(); - h.update(b"v1\0"); + h.update(b"v2\0"); h.update(text.as_bytes()); h.finalize().iter().map(|b| format!("{b:02x}")).collect() } @@ -1186,6 +1186,24 @@ fn run( } } } + // ChatGPT without a model yet (the check after the sign-in did not get that far). + if let Backend::ChatGpt { model } = &mut backend + && model.is_empty() + { + match crate::chatgpt::models() { + Ok(m) => match crate::chatgpt::default_model(&m) { + Some(d) => *model = d, + None => { + let _ = tx.send(Event::Failed(Failure::NoModel(String::new()))); + return; + } + }, + Err(e) => { + let _ = tx.send(Event::Failed(e)); + return; + } + } + } let backend = &backend; let mut batches: Vec> = Vec::new(); let mut chars = 0; @@ -1262,6 +1280,12 @@ fn run( mod tests { use super::*; + #[test] + #[ignore = "prints the instructions for a manual check"] + fn print_instructions() { + println!("{}", instructions(Lang::Ru)); + } + #[test] fn moves_are_found_in_text() { let t = "After 12.Nf3 Bxe5+ 13.dxe5 (13.O-O!?) the e5-square is weak; 14...Kh8."; diff --git a/apps/desktop/src/translate_ui.rs b/apps/desktop/src/translate_ui.rs index 40f6831..8abaa5e 100644 --- a/apps/desktop/src/translate_ui.rs +++ b/apps/desktop/src/translate_ui.rs @@ -228,11 +228,7 @@ impl Hub { let (models, model) = match connection { Connection::ChatGpt => match crate::chatgpt::models() { Ok(m) => { - let pick = if m.contains(&wanted) { - Some(wanted) - } else { - crate::chatgpt::default_model(&m) - }; + let pick = crate::chatgpt::default_model(&m).or(Some(wanted)); (m, pick.unwrap_or_default()) } Err(e) => { @@ -371,6 +367,9 @@ impl Hub { } } let ok = c.result.is_ok(); + if let Err(e) = &c.result { + crate::graphics::log(&format!("translation check via {}: {e}", s.service_id())); + } self.check_result = Some(c.result); let flow = self.resume || (self.setup_open && self.step == Step::Checking); self.resume = false; @@ -1142,6 +1141,12 @@ impl Hub { }); } ChatGptState::SignedOut | ChatGptState::Unknown => { + if let Some(Err(e)) = &self.check_result + && active + { + ui.label(RichText::new(failure_text(e, s)).color(warn)) + .on_hover_text(e.to_string()); + } if active && ui.button(tr("Check")).clicked() { self.start_check(ui.ctx(), s, now); } @@ -1635,6 +1640,7 @@ impl GameTranslation { } } Event::Failed(f) => { + crate::graphics::log(&format!("translation via {}: {f}", self.job_service)); match f { Failure::PlanLimit => hub.chatgpt = ChatGptState::Limit, Failure::SignedOut => hub.chatgpt = ChatGptState::SignedOut, diff --git a/packaging/io.github.vrubelroman.chessPenguin.metainfo.xml b/packaging/io.github.vrubelroman.chessPenguin.metainfo.xml index e0ede40..f3de630 100644 --- a/packaging/io.github.vrubelroman.chessPenguin.metainfo.xml +++ b/packaging/io.github.vrubelroman.chessPenguin.metainfo.xml @@ -43,6 +43,11 @@ BoardGame + + +

Translation with a ChatGPT subscription works; short comments and text in brackets are translated too.

+
+

Comments in another language are translated into yours — with a ChatGPT Plus or Pro subscription, an API key, or a model on this computer; moves are never changed and nothing is written to the PGN. Moves can be shown as figurines or in the letters of your language. Pieces slide on the board, as on Lichess.