懒翻译要等 1-2 秒。真 gopls 实证 go/doc 从本地源码提取的文档与 gopls 补全/悬停正文规范化空白后完全一致,批量预热方案成立: - internal/godoc: go/doc 提取器 + 36 个常用标准库清单 + NormKey - app.PreheatDocs: 6 并发翻译,进度走输出面板,已缓存自动跳过; 菜单手动触发 + 打开项目 8 秒后自动跑(每会话一次) - tools/preheat: 命令行批量工具,直接写用户缓存文件 - 缓存 key 统一 NormKey,预热/懒翻译/旧缓存三方互命中 - 已实测灌入 10 包 394 条译文(272 秒),常用包全部秒出
127 lines
3.0 KiB
Go
127 lines
3.0 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"mygoland/internal/ai"
|
|
"mygoland/internal/godoc"
|
|
)
|
|
|
|
// normDocKey 是文档翻译缓存的 key:规范化空白(见 godoc.NormKey)。
|
|
func normDocKey(s string) string {
|
|
return godoc.NormKey(s)
|
|
}
|
|
|
|
// docTranslation 返回一段补全文档的中文译文。
|
|
//
|
|
// 没有现成译文时后台发起 LLM 翻译(同一文本只发一次),先返回英文原文——
|
|
// 补全面板是即时模式,翻译回来 win.Update 重绘,下一帧自然换上译文。
|
|
// 必须在主线程调用(读写缓存 map)。
|
|
func (a *App) docTranslation(text string) string {
|
|
if !a.translateOn() || text == "" {
|
|
return text
|
|
}
|
|
key := normDocKey(text)
|
|
if tr, ok := a.trans[key]; ok {
|
|
if tr == "" {
|
|
// 空串哨兵:这个会话里翻译失败过,别再烧 API,直接原文。
|
|
return text
|
|
}
|
|
return tr
|
|
}
|
|
if a.transInflight[key] {
|
|
return text
|
|
}
|
|
if a.trans == nil {
|
|
a.trans = map[string]string{}
|
|
a.transInflight = map[string]bool{}
|
|
}
|
|
a.transInflight[key] = true
|
|
cfg := a.aiConfig()
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
|
|
defer cancel()
|
|
tr, err := ai.TranslateDoc(ctx, cfg, text)
|
|
a.win.Update(func() {
|
|
delete(a.transInflight, key)
|
|
if err != nil || tr == "" {
|
|
// 失败:缓存哨兵,同会话不再重试(重启后会再试)。
|
|
logf("翻译失败: %v", err)
|
|
a.trans[key] = ""
|
|
return
|
|
}
|
|
a.trans[key] = tr
|
|
a.saveTranslations()
|
|
})
|
|
}()
|
|
return text
|
|
}
|
|
|
|
// translateOn 判断文档翻译是否可用:开了开关且三件套配齐。
|
|
func (a *App) translateOn() bool {
|
|
s := a.set
|
|
return s.TranslateDocs && ai.Config{
|
|
BaseURL: s.AIBaseURL, APIKey: s.AIAPIKey, Model: s.AIModel,
|
|
}.Ready()
|
|
}
|
|
|
|
// aiConfig 从设置里取 AI 接口配置。
|
|
func (a *App) aiConfig() ai.Config {
|
|
return ai.Config{BaseURL: a.set.AIBaseURL, APIKey: a.set.AIAPIKey, Model: a.set.AIModel}
|
|
}
|
|
|
|
// translationsPath 是译文缓存文件。
|
|
func translationsPath() string {
|
|
dir := sessionDir()
|
|
if dir == "" {
|
|
return ""
|
|
}
|
|
return filepath.Join(dir, "translations.json")
|
|
}
|
|
|
|
// loadTranslations 启动时装载译文缓存:标准库文档是固定的,重启后秒出译文。
|
|
func (a *App) loadTranslations() {
|
|
p := translationsPath()
|
|
if p == "" {
|
|
return
|
|
}
|
|
b, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var m map[string]string
|
|
if err := json.Unmarshal(b, &m); err != nil {
|
|
return
|
|
}
|
|
a.trans = map[string]string{}
|
|
for k, v := range m {
|
|
if k != "" && v != "" {
|
|
// 旧版缓存 key 是原文,统一规范化,能被预热与懒翻译共同命中。
|
|
a.trans[normDocKey(k)] = v
|
|
}
|
|
}
|
|
}
|
|
|
|
// saveTranslations 把译文缓存落盘(空串哨兵不存,重启后失败的可重试)。
|
|
func (a *App) saveTranslations() {
|
|
p := translationsPath()
|
|
if p == "" || len(a.trans) == 0 {
|
|
return
|
|
}
|
|
m := map[string]string{}
|
|
for k, v := range a.trans {
|
|
if v != "" {
|
|
m[k] = v
|
|
}
|
|
}
|
|
b, err := json.MarshalIndent(m, "", " ")
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = os.WriteFile(p, b, 0o644)
|
|
}
|