diff --git a/Cargo.toml b/Cargo.toml index a4ba63f..95e6f2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ name = "anybot" path = "src/main.rs" [dependencies] -zhconv = { version = "0.4", default-features = false, features = ["mediawiki", "opencc"] } +zhconv = { version = "0.4", default-features = false, features = ["mediawiki"] } eframe = "0.37" ureq = { version = "3", features = ["json"] } # Web pages that are not UTF-8 (Big5, GBK, Shift_JIS…), read as what they say they are. diff --git a/FEATURES.md b/FEATURES.md index 04101ec..a933855 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -2116,13 +2116,18 @@ computer_use_screenshot、click、mouse_move、mouse_button(按住/放開) - English、日本語:翻譯了約 370 個常見字串(側邊欄、聊天頂部、輸入框、右側面板、整個設定頁);較少見的訊息仍是中文。 - 其他語言:選單用英文。 - **Bot 回答**:在指示最後加上「一律用 {語言} 回答」,37 種都適用。 -- **繁體中文,不管模型寫什麼**(2026-11-12 重做):模型常常不理指示、寫出簡體字(剛讀過簡體網頁時尤其會),所以不靠指示:模型寫給你看的字,在每一次模型呼叫回來的地方(聊天、摘要、目標檢查…同一個出口)直接轉成繁體字和台灣用語。 - - 什麼時候轉:語言設「繁體中文」時一律轉;設「自動偵測」時看你怎麼寫——你最近的訊息是繁體就轉,是簡體就不轉,看不出來(只有「你好」這種兩邊都一樣的字,或沒有中文)就照系統語言,不是簡體中文就轉。以前只有設「繁體中文」才轉,而伺服器上的大腦系統語言是英文,「自動偵測」等於完全不轉。 - - 什麼時候不轉:語言設成简体中文或其他語言;或你最新一則訊息要簡體(「翻成簡體」「用简体写」),只那一次。說「不要再出簡體」「為什麼一直出簡體」不算要簡體。 - - 轉哪些:回覆、思考過程,以及給你看的步驟內容(問題卡、生活卡片、頁面、記住的事、例行任務的名稱和任務)。不轉:檔名、指令、搜尋的字、收件人——那些要照原樣。 - - 不動的地方:程式碼區塊和 `反引號裡的字`(檔案可能真的叫「文档.txt」);有假名的那一行(你要的日文翻譯,「学校」不會變「學校」)。 - - 用語:字轉完再換台灣的說法,約 55 個只有一個意思的電腦用語(網絡→網路、文件夾→資料夾、默認→預設、視頻→影片、信息→資訊、屏幕→螢幕、插件→外掛、算法→演算法…)。有別的意思的不換(餐廳的「菜單」、聯絡「窗口」、「質量」「項目」「程序」)。 - - 指示也加強了:「自動偵測」多一句「使用者寫繁體中文時,用繁體中文和台灣用語回答,不要用簡體字」;「繁體中文」多一句不要用中國大陸的用語。 +- **回答的語言,三層**(2026-11-12 重做): + 1. **指示裡一句**:「一律用 {語言} 回答」。對話一長,這句離模型要回答的地方很遠,容易被蓋過去。 + 2. **每則訊息後面再說一次**(主要靠這層):你每一則訊息的尾端、「送出時間」那幾行的最後,加上要用的語言——繁體中文是「〔用繁體中文和台灣用語回答,不要用簡體字〕」,简体中文是「〔用简体中文回答〕」,其他語言是「〔Answer in Japanese (日本語)〕」這樣。37 種都有,用語也由模型自己寫對(網路、資料夾),不靠對照表。 + - 「自動偵測」時:那則訊息是繁體中文才加(模型自己最常弄錯的就是這個),其他語言不加,模型照你用的語言回答。 + - 那則訊息要簡體時(「翻成簡體」)不加。 + - 只由設定和那則訊息本身決定,所以同一段對話每回合送出去的內容都一樣(模型的快取不受影響);改語言設定後第一回合會重讀一次。 + 3. **保險:字的轉換,只管繁體中文**。有些模型(Qwen、Grok,或剛讀過簡體網頁時)還是會漏幾個簡體字,所以模型寫給你看的字,在每一次模型呼叫回來的地方(聊天、摘要、目標檢查…同一個出口)用整套字表轉成繁體。只轉字,沒有手寫的用語表;本來就對的字不動(「台」不會變「臺」,「了解」不會變「瞭解」)。 + - 什麼時候轉:語言設「繁體中文」時一律轉;設「自動偵測」時看你怎麼寫——你最近的訊息是繁體就轉,是簡體就不轉,看不出來(只有「你好」這種兩邊都一樣的字,或沒有中文)就照系統語言,不是簡體中文就轉。以前只有設「繁體中文」才轉,而伺服器上的大腦系統語言是英文,「自動偵測」等於完全不轉。 + - 什麼時候不轉:語言設成简体中文或其他語言;或你最新一則訊息要簡體,只那一次。說「不要再出簡體」「為什麼一直出簡體」不算要簡體。 + - 轉哪些:回覆、思考過程,以及給你看的步驟內容(問題卡、生活卡片、頁面、記住的事、例行任務的名稱和任務)。不轉:檔名、指令、搜尋的字、收件人。 + - 不動的地方:程式碼區塊和 `反引號裡的字`(檔案可能真的叫「文档.txt」);有假名的那一行(你要的日文翻譯,「学校」不會變「學校」)。 + - 已知的限制:字表偶爾分不出同一個簡體字的兩種繁體(「设置里的」的「里」);這種靠第 2 層讓模型一開始就寫對。 ### 設定的分類(照原版) diff --git a/src/app.rs b/src/app.rs index 252f7b7..ddf21b7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5005,6 +5005,10 @@ fn group_history(messages: &[Message], speaker: Option, st: &State) -> Vec< let line = crate::memory::named_skill_line(k); m.context = if m.context.is_empty() { line } else { format!("{}\n{line}", m.context) }; } + // 語言: the language to answer in, last — next to where the model answers. + if let Some(line) = crate::i18n::reply_reminder(&st.language, &m.text) { + m.context = if m.context.is_empty() { line } else { format!("{}\n{line}", m.context) }; + } } // A result set aside in a long chat: the model reads its first words (as it did then). if m.role == Role::Tool && m.folded { @@ -5637,8 +5641,8 @@ mod tests { assert!(!sys1.contains("現在是") && sys1.contains("- 咖啡不加糖") && sys1.contains("search_memory")); let view = |h: &[Message]| h.iter().map(|m| (m.role.clone(), m.text.clone(), m.context.clone())).collect::>(); assert_eq!(view(&h2[..h1.len()]), view(&h1), "the first turn is the beginning of the second, word for word"); - assert_eq!(h1[0].context, "〔送出時間:2025-11-11(週二)10:00〕\n〔相關記憶(AnyBot 依這則訊息找出來的,不一定都用得上):\n- 我喜歡靠窗的座位〕"); - assert_eq!(h2[2].context, "〔送出時間:2025-11-11(週二)11:00〕"); + assert_eq!(h1[0].context, "〔送出時間:2025-11-11(週二)10:00〕\n〔相關記憶(AnyBot 依這則訊息找出來的,不一定都用得上):\n- 我喜歡靠窗的座位〕\n〔用繁體中文和台灣用語回答,不要用簡體字〕"); + assert_eq!(h2[2].context, "〔送出時間:2025-11-11(週二)11:00〕\n〔用繁體中文和台灣用語回答,不要用簡體字〕"); assert!(h1[1].context.is_empty(), "only the person's messages get the tail"); } @@ -5655,7 +5659,7 @@ mod tests { Message::user("/Users/daniel/a.txt 是什麼"), ]; let h = group_history(&msgs, None, &st); - assert_eq!(h[0].context, "〔送出時間:2025-11-11(週二)10:00〕\n〔使用者用 /pptx 指定了技能「pptx」:先用 use_skill 讀出它的完整做法,再照著做這件事〕"); + assert_eq!(h[0].context, "〔送出時間:2025-11-11(週二)10:00〕\n〔使用者用 /pptx 指定了技能「pptx」:先用 use_skill 讀出它的完整做法,再照著做這件事〕\n〔用繁體中文和台灣用語回答,不要用簡體字〕"); assert_eq!(h[0].text, "/pptx 把這份週報做成五頁簡報", "the person's words stay as typed"); assert!(!h[2].context.contains("技能")); } diff --git a/src/i18n.rs b/src/i18n.rs index 8b1d4b1..2983bc4 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -55,11 +55,15 @@ fn current() -> &'static RwLock { L.get_or_init(|| RwLock::new("zh-TW".into())) } -// 繁體中文, whatever the model writes. Models slip into Simplified characters despite every -// instruction (Qwen and Grok especially, and anything that has just read a Simplified page), so -// the instruction isn't what is relied on: what a model wrote for the person is converted — -// Traditional characters, Taiwan wording — at the one place every model call comes back -// through (providers::run_step). Whether to is read off the request itself, so it holds for +// The language of the answers, in three layers: +// 1. reply_instruction: one line in the bot's instructions (far from the answer in a long chat). +// 2. reply_reminder: the same, after every one of the person's messages — next to where the +// model answers, for whichever language is chosen. This is what does the work: it also +// gets the wording right (網路, not 網絡), which no table of words would. +// 3. hant: a net under the first two, for Traditional Chinese only. Some models (Qwen, Grok, +// anything that has just read a Simplified page) still slip a few Simplified characters +// in, so what a model wrote for the person is converted, character tables and nothing +// hand-written, at the one place every model call comes back through (providers::run_step). Whether to is read off the request itself, so it holds for // the chat, the summaries, the goal checks and every other call alike: // • the language setting is 繁體中文, or // • it is 跟著系統 and the person writes Traditional Chinese (a brain on a server has an @@ -75,78 +79,6 @@ fn is_han(c: char) -> bool { matches!(c as u32, 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xF900..=0xFAFF | 0x20000..=0x2FA1F) } -/// Mainland wording the converter's tables leave alone, and what Taiwan says instead. Only -/// words that mean one thing: not 菜單 (a restaurant's), 窗口 (a contact), 文本, 在線, 質量, -/// 項目, 程序, 對象… whose other meaning is everyday Taiwan Chinese. Longer words first. -const TW_WORDS: &[(&str, &str)] = &[ - ("筆記本電腦", "筆記型電腦"), - ("演示文稿", "簡報"), - ("電子表格", "試算表"), - ("快捷方式", "捷徑"), - ("文件夾", "資料夾"), - ("字符串", "字串"), - ("攝像頭", "攝影機"), - ("二維碼", "QR Code"), - ("回收站", "資源回收筒"), - ("剪貼板", "剪貼簿"), - ("快捷鍵", "快速鍵"), - ("用戶名", "使用者名稱"), - ("台式機", "桌上型電腦"), - ("幻燈片", "投影片"), - ("充電寶", "行動電源"), - ("網絡", "網路"), - ("信息", "資訊"), - ("視頻", "影片"), - ("音頻", "音訊"), - ("默認", "預設"), - ("屏幕", "螢幕"), - ("登錄", "登入"), - ("博客", "部落格"), - ("內存", "記憶體"), - ("字符", "字元"), - ("短信", "簡訊"), - ("卸載", "解除安裝"), - ("兼容", "相容"), - ("緩存", "快取"), - ("插件", "外掛"), - ("光標", "游標"), - ("加載", "載入"), - ("搜索", "搜尋"), - ("代碼", "程式碼"), - ("模塊", "模組"), - ("變量", "變數"), - ("郵箱", "信箱"), - ("設置", "設定"), - ("激活", "啟用"), - ("截屏", "截圖"), - ("文檔", "文件"), - ("寬帶", "寬頻"), - ("編程", "寫程式"), - ("調試", "除錯"), - ("線程", "執行緒"), - ("粘貼", "貼上"), - ("發送", "傳送"), - ("U盤", "隨身碟"), - ("實時", "即時"), - ("圖標", "圖示"), - ("數組", "陣列"), - ("主頁", "首頁"), - ("拖拽", "拖曳"), - ("算法", "演算法"), -]; - -/// Traditional characters first (the converter), then Taiwan's words for what is left. -fn to_taiwan(t: &str) -> String { - let mut s = zhconv::zhconv(t, zhconv::Variant::ZhTW); - for (from, to) in TW_WORDS { - if s.contains(from) { - s = s.replace(from, to); - } - } - // (演算法 was already right.) - s.replace("演演算法", "演算法") -} - /// Japanese: kana in the line. Its kanji are not Chinese to be converted (学校 stays 学校). fn is_japanese(line: &str) -> bool { line.chars().any(|c| matches!(c as u32, 0x3041..=0x309F | 0x30A1..=0x30FA)) @@ -159,7 +91,7 @@ pub fn hant(s: &str) -> String { if !s.chars().any(is_han) { return s.to_string(); } - let convert = |t: &str| if t.chars().any(is_han) { to_taiwan(t) } else { t.to_string() }; + let convert = |t: &str| if t.chars().any(is_han) { zhconv::zhconv(t, zhconv::Variant::ZhTW) } else { t.to_string() }; let mut fenced = false; let mut out = String::with_capacity(s.len()); for line in s.split_inclusive('\n') { @@ -201,6 +133,23 @@ pub fn written_traditional(s: &str) -> Option { } } +/// After each of the person's messages, the language to answer in (see the three layers +/// above). Made from the setting and that message alone, so a chat reads the same every turn. +/// Nothing for 跟著系統 unless the message is in Traditional Chinese — the one case models get +/// wrong on their own — and nothing when the message itself asks for Simplified. +pub fn reply_reminder(setting: &str, text: &str) -> Option { + if asks_simplified(text) { + return None; + } + let hant_line = || "〔用繁體中文和台灣用語回答,不要用簡體字〕".to_string(); + match setting { + "auto" | "" => (written_traditional(text) == Some(true)).then(hant_line), + "zh-TW" => Some(hant_line()), + "zh-CN" => Some("〔用简体中文回答〕".into()), + t => Some(format!("〔Answer in {} ({})〕", english_name(t), endonym(t))), + } +} + /// The person asks for Simplified Chinese this time (「翻成簡體」「用简体写」) — and isn't saying /// they don't want it (「不要再出簡體」). pub fn asks_simplified(text: &str) -> bool { @@ -388,11 +337,21 @@ mod tests { #[test] fn replies_are_put_into_traditional_chinese_for_someone_who_writes_it() { use crate::model::Message; - assert_eq!(hant("指令/檔案工具全挂,这台是 Ubuntu,根本没有这个文件夹"), "指令/檔案工具全掛,這台是 Ubuntu,根本沒有這個資料夾"); + assert_eq!(hant("指令/檔案工具全挂,这台是 Ubuntu,根本没有这个文件夹"), "指令/檔案工具全掛,這台是 Ubuntu,根本沒有這個文件夾"); assert_eq!(hant("no Chinese here: /Users/daniel"), "no Chinese here: /Users/daniel"); - // Taiwan's words, not only its characters — and what is already right stays right. - assert_eq!(hant("网络和默认设置里的视频插件,用这个算法搜索信息"), "網路和預設設定裡的影片外掛,用這個演算法搜尋資訊"); - assert_eq!(hant("演算法、網路、資料夾、這家餐廳的菜單、聯絡窗口"), "演算法、網路、資料夾、這家餐廳的菜單、聯絡窗口"); + // Characters only: the wording is the reminder's work, not a table's. + assert_eq!(hant("默认设置和视频"), "默認設置和視頻"); + // What is already right is left as it is (台 isn't made 臺, 了解 isn't made 瞭解). + assert_eq!(hant("這台電腦的平台在台灣,了解了"), "這台電腦的平台在台灣,了解了"); + // The reminder after each message: the chosen language, whichever it is. + assert_eq!(reply_reminder("zh-TW", "hello").as_deref(), Some("〔用繁體中文和台灣用語回答,不要用簡體字〕")); + assert_eq!(reply_reminder("auto", "確認一下你現在用的電腦"), reply_reminder("zh-TW", "x")); + assert_eq!(reply_reminder("auto", "确认一下"), None); + assert_eq!(reply_reminder("auto", "hello"), None); + assert_eq!(reply_reminder("zh-CN", "確認").as_deref(), Some("〔用简体中文回答〕")); + assert_eq!(reply_reminder("ja", "確認").as_deref(), Some("〔Answer in Japanese (日本語)〕")); + assert_eq!(reply_reminder("zh-TW", "把這段翻成簡體中文"), None); + assert!(reply_reminder("zh-TW", "不要再出簡體中文").is_some()); // A Japanese line is left alone (its 学 and 国 are not Simplified Chinese); the Chinese around it isn't. assert_eq!(hant("这句的日文是:\n学校に行きます。国語の授業です。\n意思是去学校。\n"), "這句的日文是:\n学校に行きます。国語の授業です。\n意思是去學校。\n"); assert_eq!(written_traditional("這句日文是什麼意思\n学校に行きます。国語の授業"), Some(true)); @@ -454,3 +413,4 @@ mod tests { } } +