feat(settings): mother-tongue language routing table
Replace the single reverse-direction pair with a general routing table. The saved target is the mother tongue and the default target for every source; languageRoutes is an empty-by-default, source-unique list of source-to-target rules (for example Chinese -> English, Traditional Chinese -> English, German -> English). Rules apply only when the source is confidently detected; otherwise the mother tongue is used. Same-language rules are dropped.\n\nThe panel shows the effective direction and a banner explaining a rule, with a one-capture override back to the mother tongue; the panel picker is now a session-only override. Persistent rules are edited in Settings. The old autoSwitchTarget/fallbackTargetLanguage keys are ignored, so the default after migration is everything -> mother tongue.\n\nAdd LanguageRoute, generalize the direction policy and tests, add the rules UI, and update ADR-022, architecture, translation output, user guide, README and progress. Verified: make verify green; empty rules translate French -> German, a Chinese -> English rule produces Chinese (Simplified) -> English with the banner, and the rules table renders in Settings.
This commit is contained in:
1 parent
41f1cea769
commit
940abcc996
24 files changed
+454
-336
No files matched your search
@@ -18,9 +18,7 @@ struct AppSettingsTests {
|
||||
#expect(settings.maxCharactersCJK == 2000)
|
||||
#expect(settings.numCtx == 8192)
|
||||
#expect(settings.translationFontSize == 17)
|
||||
#expect(settings.autoSwitchTarget)
|
||||
#expect(HyMT3Languages.resolve(settings.fallbackTargetLanguage) != nil)
|
||||
#expect(settings.fallbackTargetLanguage != settings.targetLanguage)
|
||||
#expect(settings.languageRoutes.isEmpty)
|
||||
#expect(settings.renderMarkdown)
|
||||
#expect(settings.closeWithEscape)
|
||||
#expect(!settings.autoCopy)
|
||||
@@ -35,8 +33,7 @@ struct AppSettingsTests {
|
||||
settings.noteTarget(HyMT3Languages.german)
|
||||
settings.temperature = 0.2
|
||||
settings.translationFontSize = 22
|
||||
settings.autoSwitchTarget = false
|
||||
settings.fallbackTargetLanguage = "fr"
|
||||
settings.languageRoutes = [LanguageRoute(source: "zh", target: "en")]
|
||||
settings.renderMarkdown = false
|
||||
settings.closeWithEscape = false
|
||||
settings.launchAtLogin = true
|
||||
@@ -54,6 +51,8 @@ struct AppSettingsTests {
|
||||
"settingsVersion": 0,
|
||||
"endpoint": "http://127.0.0.1:11434",
|
||||
"targetLanguage": "de",
|
||||
"autoSwitchTarget": true,
|
||||
"fallbackTargetLanguage": "en",
|
||||
"autoCopy": true,
|
||||
"unknownFutureKey": "ignored"
|
||||
}
|
||||
@@ -62,9 +61,7 @@ struct AppSettingsTests {
|
||||
#expect(decoded.settingsVersion == AppSettings.currentVersion)
|
||||
#expect(decoded.targetLanguage == "de")
|
||||
#expect(decoded.model == HyMT3ModelPreset.defaultModel)
|
||||
#expect(decoded.autoSwitchTarget)
|
||||
#expect(HyMT3Languages.resolve(decoded.fallbackTargetLanguage) != nil)
|
||||
#expect(decoded.fallbackTargetLanguage != decoded.targetLanguage)
|
||||
#expect(decoded.languageRoutes.isEmpty)
|
||||
#expect(decoded.renderMarkdown)
|
||||
#expect(decoded.closeWithEscape)
|
||||
#expect(!decoded.autoCopy)
|
||||
@@ -99,7 +96,6 @@ struct AppSettingsTests {
|
||||
"repeatPenalty": 9,
|
||||
"keepModelLoaded": "forever",
|
||||
"translationFontSize": 99,
|
||||
"fallbackTargetLanguage": "xx",
|
||||
"targetLanguage": "xx"
|
||||
}
|
||||
"""
|
||||
@@ -115,8 +111,7 @@ struct AppSettingsTests {
|
||||
#expect(decoded.repeatPenalty == 2)
|
||||
#expect(decoded.keepModelLoaded == HyMT3ModelPreset.defaultKeepAlive)
|
||||
#expect(decoded.translationFontSize == 28)
|
||||
#expect(HyMT3Languages.resolve(decoded.fallbackTargetLanguage) != nil)
|
||||
#expect(decoded.fallbackTargetLanguage != decoded.targetLanguage)
|
||||
#expect(decoded.languageRoutes.isEmpty)
|
||||
#expect(HyMT3Languages.resolve(decoded.targetLanguage) != nil)
|
||||
}
|
||||
|
||||
@@ -128,7 +123,12 @@ struct AppSettingsTests {
|
||||
settings.topK = 0
|
||||
settings.keepModelLoaded = "forever"
|
||||
settings.translationFontSize = 99
|
||||
settings.fallbackTargetLanguage = "de"
|
||||
settings.languageRoutes = [
|
||||
LanguageRoute(source: "xx", target: "en"),
|
||||
LanguageRoute(source: "de", target: "de"),
|
||||
LanguageRoute(source: "de", target: "en"),
|
||||
LanguageRoute(source: "zh-Hans", target: "en"),
|
||||
]
|
||||
settings.autoCopy = true
|
||||
settings.targetLanguage = "de"
|
||||
settings.sanitize()
|
||||
@@ -139,33 +139,14 @@ struct AppSettingsTests {
|
||||
#expect(settings.topK == 1)
|
||||
#expect(settings.keepModelLoaded == HyMT3ModelPreset.defaultKeepAlive)
|
||||
#expect(settings.translationFontSize == 28)
|
||||
#expect(settings.fallbackTargetLanguage == "en")
|
||||
#expect(settings.languageRoutes == [
|
||||
LanguageRoute(source: "de", target: "en"),
|
||||
LanguageRoute(source: "zh", target: "en"),
|
||||
])
|
||||
#expect(!settings.autoCopy)
|
||||
#expect(HyMT3Languages.resolve(settings.targetLanguage) != nil)
|
||||
}
|
||||
|
||||
@Test func changingToTheFallbackSwapsThePair() {
|
||||
var settings = AppSettings()
|
||||
settings.targetLanguage = HyMT3Languages.german.code
|
||||
settings.fallbackTargetLanguage = HyMT3Languages.english.code
|
||||
settings.noteTarget(HyMT3Languages.english)
|
||||
#expect(settings.targetLanguage == HyMT3Languages.english.code)
|
||||
#expect(settings.fallbackTargetLanguage == HyMT3Languages.german.code)
|
||||
}
|
||||
|
||||
@Test func sanitizeKeepsTheFallbackDifferentFromTheTarget() {
|
||||
var settings = AppSettings()
|
||||
settings.targetLanguage = HyMT3Languages.german.code
|
||||
settings.fallbackTargetLanguage = HyMT3Languages.german.code
|
||||
settings.sanitize()
|
||||
#expect(settings.fallbackTargetLanguage == HyMT3Languages.english.code)
|
||||
|
||||
settings.targetLanguage = HyMT3Languages.english.code
|
||||
settings.fallbackTargetLanguage = HyMT3Languages.english.code
|
||||
settings.sanitize()
|
||||
#expect(settings.fallbackTargetLanguage == HyMT3Languages.german.code)
|
||||
}
|
||||
|
||||
@Test func endpointPolicyIsEnforced() throws {
|
||||
var settings = AppSettings()
|
||||
settings.endpoint = try #require(URL(string: "http://192.168.1.10:11434"))
|
||||
|
||||
@@ -88,26 +88,66 @@ struct PanelViewModelTests {
|
||||
#expect(panel.sameLanguageWarning == nil)
|
||||
}
|
||||
|
||||
@Test func autoSwitchesWhenTheSourceIsTheTarget() async {
|
||||
@Test func appliesAMatchingLanguageRule() async {
|
||||
let detector = FixedDetector(
|
||||
detection: LanguageDetection(code: "de", confidence: 0.9, alternatives: ["de"])
|
||||
)
|
||||
let (panel, _, _) = makePanel(streaming: EchoStreaming(), detector: detector, autoSwitch: true)
|
||||
let (panel, _, _) = makePanel(
|
||||
streaming: EchoStreaming(),
|
||||
detector: detector,
|
||||
routes: [LanguageRoute(source: "de", target: "en")]
|
||||
)
|
||||
|
||||
await panel.begin(germanCaptured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
#expect(panel.effectiveTarget == HyMT3Languages.english)
|
||||
#expect(panel.autoSwitchedFrom == HyMT3Languages.german)
|
||||
#expect(panel.routedSource == HyMT3Languages.german)
|
||||
#expect(panel.configuration.target == HyMT3Languages.german)
|
||||
#expect(panel.translation == "en")
|
||||
}
|
||||
|
||||
@Test func autoSwitchOverrideIsSessionOnly() async {
|
||||
@Test func noRuleKeepsTheMotherTongue() async {
|
||||
let detector = FixedDetector(
|
||||
detection: LanguageDetection(code: "de", confidence: 0.9, alternatives: ["de"])
|
||||
)
|
||||
let (panel, _, targets) = makePanel(streaming: EchoStreaming(), detector: detector, autoSwitch: true)
|
||||
let (panel, _, _) = makePanel(streaming: EchoStreaming(), detector: detector)
|
||||
|
||||
await panel.begin(germanCaptured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
#expect(panel.effectiveTarget == HyMT3Languages.german)
|
||||
#expect(panel.routedSource == nil)
|
||||
#expect(panel.translation == "de")
|
||||
}
|
||||
|
||||
@Test func routesAnArbitrarySourceLanguage() async {
|
||||
let detector = FixedDetector(
|
||||
detection: LanguageDetection(code: "fr", confidence: 0.9, alternatives: ["fr"])
|
||||
)
|
||||
let (panel, _, _) = makePanel(
|
||||
streaming: EchoStreaming(),
|
||||
detector: detector,
|
||||
routes: [LanguageRoute(source: "fr", target: "en")]
|
||||
)
|
||||
|
||||
await panel.begin(germanCaptured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
#expect(panel.effectiveTarget == HyMT3Languages.english)
|
||||
#expect(panel.routedSource == HyMT3Languages.resolve("fr"))
|
||||
#expect(panel.translation == "en")
|
||||
}
|
||||
|
||||
@Test func routeOverrideIsSessionOnly() async {
|
||||
let detector = FixedDetector(
|
||||
detection: LanguageDetection(code: "de", confidence: 0.9, alternatives: ["de"])
|
||||
)
|
||||
let (panel, _, targets) = makePanel(
|
||||
streaming: EchoStreaming(),
|
||||
detector: detector,
|
||||
routes: [LanguageRoute(source: "de", target: "en")]
|
||||
)
|
||||
await panel.begin(germanCaptured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
@@ -115,16 +155,20 @@ struct PanelViewModelTests {
|
||||
await waitFor { panel.phase == .ready && panel.translation == "de" }
|
||||
|
||||
#expect(panel.effectiveTarget == HyMT3Languages.german)
|
||||
#expect(panel.autoSwitchedFrom == nil)
|
||||
#expect(panel.routedSource == nil)
|
||||
#expect(panel.configuration.target == HyMT3Languages.german)
|
||||
#expect(targets.codes.isEmpty)
|
||||
}
|
||||
|
||||
@Test func explicitTargetPersistsAndDisablesAutoSwitch() async {
|
||||
@Test func explicitTargetPersistsAndDisablesTheRule() async {
|
||||
let detector = FixedDetector(
|
||||
detection: LanguageDetection(code: "de", confidence: 0.9, alternatives: ["de"])
|
||||
)
|
||||
let (panel, _, targets) = makePanel(streaming: EchoStreaming(), detector: detector, autoSwitch: true)
|
||||
let (panel, _, targets) = makePanel(
|
||||
streaming: EchoStreaming(),
|
||||
detector: detector,
|
||||
routes: [LanguageRoute(source: "de", target: "en")]
|
||||
)
|
||||
await panel.begin(germanCaptured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
@@ -133,7 +177,7 @@ struct PanelViewModelTests {
|
||||
|
||||
#expect(targets.codes == ["de"])
|
||||
#expect(panel.effectiveTarget == HyMT3Languages.german)
|
||||
#expect(panel.autoSwitchedFrom == nil)
|
||||
#expect(panel.routedSource == nil)
|
||||
}
|
||||
|
||||
@Test func retryKeepsTheManualTarget() async {
|
||||
@@ -141,7 +185,11 @@ struct PanelViewModelTests {
|
||||
detection: LanguageDetection(code: "de", confidence: 0.9, alternatives: ["de"])
|
||||
)
|
||||
let streaming = EchoStreaming()
|
||||
let (panel, _, _) = makePanel(streaming: streaming, detector: detector, autoSwitch: true)
|
||||
let (panel, _, _) = makePanel(
|
||||
streaming: streaming,
|
||||
detector: detector,
|
||||
routes: [LanguageRoute(source: "de", target: "en")]
|
||||
)
|
||||
await panel.begin(germanCaptured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
await panel.setTarget(HyMT3Languages.german)
|
||||
@@ -158,7 +206,11 @@ struct PanelViewModelTests {
|
||||
let detector = FixedDetector(
|
||||
detection: LanguageDetection(code: "de", confidence: 0.9, alternatives: ["de"])
|
||||
)
|
||||
let (panel, _, _) = makePanel(streaming: EchoStreaming(), detector: detector, autoSwitch: true)
|
||||
let (panel, _, _) = makePanel(
|
||||
streaming: EchoStreaming(),
|
||||
detector: detector,
|
||||
routes: [LanguageRoute(source: "de", target: "en")]
|
||||
)
|
||||
await panel.begin(germanCaptured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
await panel.overrideTarget(HyMT3Languages.german)
|
||||
@@ -168,7 +220,7 @@ struct PanelViewModelTests {
|
||||
await waitFor { panel.phase == .ready && panel.translation == "en" }
|
||||
|
||||
#expect(panel.effectiveTarget == HyMT3Languages.english)
|
||||
#expect(panel.autoSwitchedFrom == HyMT3Languages.german)
|
||||
#expect(panel.routedSource == HyMT3Languages.german)
|
||||
}
|
||||
|
||||
@Test func changingTargetRetranslatesAndNotifies() async {
|
||||
@@ -237,9 +289,8 @@ struct PanelViewModelTests {
|
||||
replacement: (@MainActor () async -> InsertionOutcome)? = nil,
|
||||
onClose: @escaping @MainActor () -> Void = {},
|
||||
detector: any LanguageDetecting = FixedDetector(detection: nil),
|
||||
autoSwitch: Bool = false,
|
||||
target: ModelLanguage = HyMT3Languages.german,
|
||||
fallback: ModelLanguage? = HyMT3Languages.english
|
||||
routes: [LanguageRoute] = []
|
||||
) -> (PanelViewModel, CopiedTextBox, TargetBox) {
|
||||
let session = TranslationSession(pipelineFactory: { streaming })
|
||||
let copies = CopiedTextBox()
|
||||
@@ -250,8 +301,7 @@ struct PanelViewModelTests {
|
||||
target: target,
|
||||
model: "test-hy-mt3",
|
||||
keepAlive: "30m",
|
||||
autoSwitchTarget: autoSwitch,
|
||||
fallbackTarget: fallback
|
||||
languageRoutes: routes
|
||||
),
|
||||
copy: { copies.text = $0 },
|
||||
replace: replacement,
|
||||
|
||||
+72
-64
@@ -15,103 +15,111 @@ struct TranslationDirectionTests {
|
||||
LanguageDetection(code: code, confidence: confidence, alternatives: alternatives ?? [code])
|
||||
}
|
||||
|
||||
@Test func keepsTheConfiguredTargetWhenTheSourceIsDifferent() {
|
||||
@Test func usesTheMotherTongueWithoutRules() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
fallbackTarget: english,
|
||||
routes: [],
|
||||
detection: detection("en"),
|
||||
autoSwitch: true,
|
||||
override: false
|
||||
)
|
||||
#expect(direction == TranslationDirection(target: german, didSwitch: false, originalTarget: nil))
|
||||
#expect(direction == TranslationDirection(target: german, didRoute: false, source: nil))
|
||||
}
|
||||
|
||||
@Test func switchesWhenTheSourceIsConfidentlyTheTarget() {
|
||||
@Test func appliesAMatchingRule() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
fallbackTarget: english,
|
||||
routes: [LanguageRoute(source: "de", target: "en")],
|
||||
detection: detection("de"),
|
||||
autoSwitch: true,
|
||||
override: false
|
||||
)
|
||||
#expect(direction == TranslationDirection(target: english, didSwitch: true, originalTarget: german))
|
||||
#expect(direction == TranslationDirection(target: english, didRoute: true, source: german))
|
||||
}
|
||||
|
||||
@Test func doesNotSwitchWhenDisabled() {
|
||||
@Test func appliesAnArbitrarySourceRule() throws {
|
||||
let french = try #require(HyMT3Languages.resolve("fr"))
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
fallbackTarget: english,
|
||||
detection: detection("de"),
|
||||
autoSwitch: false,
|
||||
routes: [LanguageRoute(source: "fr", target: "en")],
|
||||
detection: detection("fr"),
|
||||
override: false
|
||||
)
|
||||
#expect(direction.target == english)
|
||||
#expect(direction.didRoute)
|
||||
#expect(direction.source == french)
|
||||
}
|
||||
|
||||
@Test func supportsChineseToEnglish() throws {
|
||||
let chinese = try #require(HyMT3Languages.resolve("zh"))
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
routes: [LanguageRoute(source: "zh", target: "en")],
|
||||
detection: detection("zh-Hans"),
|
||||
override: false
|
||||
)
|
||||
#expect(direction.target == english)
|
||||
#expect(direction.source == chinese)
|
||||
}
|
||||
|
||||
@Test func keepsTheMotherTongueWhenTheRuleTargetIsTheMotherTongue() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
routes: [LanguageRoute(source: "en", target: "de")],
|
||||
detection: detection("en"),
|
||||
override: false
|
||||
)
|
||||
#expect(direction.target == german)
|
||||
#expect(!direction.didSwitch)
|
||||
#expect(!direction.didRoute)
|
||||
}
|
||||
|
||||
@Test func doesNotSwitchAfterAManualOverride() {
|
||||
@Test func ignoresARuleWhoseTargetEqualsItsSource() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
fallbackTarget: english,
|
||||
routes: [LanguageRoute(source: "de", target: "de")],
|
||||
detection: detection("de"),
|
||||
override: false
|
||||
)
|
||||
#expect(direction.target == german)
|
||||
#expect(!direction.didRoute)
|
||||
}
|
||||
|
||||
@Test func ignoresRulesOnLowConfidence() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
routes: [LanguageRoute(source: "de", target: "en")],
|
||||
detection: detection("de", confidence: 0.49),
|
||||
override: false
|
||||
)
|
||||
#expect(direction.target == german)
|
||||
}
|
||||
|
||||
@Test func ignoresRulesWhenTheAlternativesDisagree() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
routes: [LanguageRoute(source: "de", target: "en")],
|
||||
detection: detection("de", alternatives: ["de", "en"]),
|
||||
override: false
|
||||
)
|
||||
#expect(direction.target == german)
|
||||
}
|
||||
|
||||
@Test func manualOverrideWins() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
routes: [LanguageRoute(source: "de", target: "en")],
|
||||
detection: detection("de"),
|
||||
autoSwitch: true,
|
||||
override: true
|
||||
)
|
||||
#expect(direction.target == german)
|
||||
#expect(!direction.didSwitch)
|
||||
#expect(!direction.didRoute)
|
||||
}
|
||||
|
||||
@Test func doesNotSwitchWithLowConfidence() {
|
||||
@Test func noDetectionUsesTheMotherTongue() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
fallbackTarget: english,
|
||||
detection: detection("de", confidence: 0.49),
|
||||
autoSwitch: true,
|
||||
routes: [LanguageRoute(source: "de", target: "en")],
|
||||
detection: nil,
|
||||
override: false
|
||||
)
|
||||
#expect(direction.target == german)
|
||||
}
|
||||
|
||||
@Test func doesNotSwitchWhenTheAlternativesDisagree() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
fallbackTarget: english,
|
||||
detection: detection("de", alternatives: ["de", "en"]),
|
||||
autoSwitch: true,
|
||||
override: false
|
||||
)
|
||||
#expect(direction.target == german)
|
||||
}
|
||||
|
||||
@Test func doesNotSwitchWhenTheFallbackIsTheTarget() {
|
||||
let direction = TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
fallbackTarget: german,
|
||||
detection: detection("de"),
|
||||
autoSwitch: true,
|
||||
override: false
|
||||
)
|
||||
#expect(direction.target == german)
|
||||
}
|
||||
|
||||
@Test func doesNotSwitchWithoutAFallbackOrDetection() {
|
||||
#expect(
|
||||
TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
fallbackTarget: nil,
|
||||
detection: detection("de"),
|
||||
autoSwitch: true,
|
||||
override: false
|
||||
).target == german
|
||||
)
|
||||
#expect(
|
||||
TranslationDirectionPolicy.resolve(
|
||||
configuredTarget: german,
|
||||
fallbackTarget: english,
|
||||
detection: nil,
|
||||
autoSwitch: true,
|
||||
override: false
|
||||
).target == german
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user