d6a781ba5b
WeKnora's system prompt hard-enforces 'ALWAYS respond in {{language}}',
fed by its Language middleware (WEKNORA_LANGUAGE env -> Accept-Language
header -> zh-CN fallback). The previous localizedQuery() prompt prefix
lost against that instruction, so a user-set 'en' preference still got
Chinese answers on English questions.
- AgentChatRequest.Locale is transmitted as the Accept-Language header
(never in the JSON body); WeKnora maps it to the prompt language
- 'auto' detects the query script client-side (latin->en, Han->zh-CN,
kana->ja, Hangul->ko); es is never auto-guessed
- unset/unknown locale sends no header, preserving prior server behavior
- drop localizedQuery prompt prefixes entirely
68 lines
2.0 KiB
Go
68 lines
2.0 KiB
Go
package weknora
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
// fixedLocales are the response languages the platform understands end-to-end
|
|
// (settings command, preference storage, and WeKnora's LanguageLocaleName map).
|
|
var fixedLocales = map[string]bool{
|
|
"zh-CN": true, "en": true, "ja": true, "ko": true, "es": true,
|
|
}
|
|
|
|
// resolveLocale maps a user preference + query text to an Accept-Language
|
|
// value understood by WeKnora's Language middleware.
|
|
//
|
|
// - "" -> "" (send no header; WeKnora keeps its own fallback)
|
|
// - "auto" -> best-effort detection from the query script
|
|
// - a known fixed locale (e.g. "en", "ja") -> passed through verbatim
|
|
//
|
|
// WeKnora matches the first Accept-Language tag and maps it to a human
|
|
// language name for its prompts, so the returned tag must be one it knows
|
|
// ("zh-CN", "en", "ja", "ko", "es", ...). An "auto" query whose script
|
|
// cannot be classified returns "" so the server behaves exactly as before.
|
|
func resolveLocale(locale, query string) string {
|
|
locale = strings.TrimSpace(locale)
|
|
if fixedLocales[locale] {
|
|
return locale
|
|
}
|
|
if locale == "auto" {
|
|
return detectLocale(query)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// detectLocale classifies a query by the dominant non-ASCII script. Latin
|
|
// text maps to "en"; CJK ideographs to "zh-CN"; kana to "ja"; hangul to "ko";
|
|
// Spanish is not reliably script-detectable and is never auto-selected.
|
|
// Pure punctuation/digits/empty text return "" (no guess).
|
|
func detectLocale(query string) string {
|
|
var cjk, kana, hangul, latin int
|
|
for _, r := range query {
|
|
switch {
|
|
case unicode.Is(unicode.Hiragana, r) || unicode.Is(unicode.Katakana, r):
|
|
kana++
|
|
case unicode.Is(unicode.Han, r):
|
|
cjk++
|
|
case unicode.Is(unicode.Hangul, r):
|
|
hangul++
|
|
case r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z':
|
|
latin++
|
|
}
|
|
}
|
|
// Japanese mixes kanji in, so any kana presence wins over Han count.
|
|
switch {
|
|
case kana > 0:
|
|
return "ja"
|
|
case hangul > 0:
|
|
return "ko"
|
|
case cjk > 0:
|
|
return "zh-CN"
|
|
case latin > 0:
|
|
return "en"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|