feat: 标准库文档批量预热——补全/悬停翻译秒出
懒翻译要等 1-2 秒。真 gopls 实证 go/doc 从本地源码提取的文档与 gopls 补全/悬停正文规范化空白后完全一致,批量预热方案成立: - internal/godoc: go/doc 提取器 + 36 个常用标准库清单 + NormKey - app.PreheatDocs: 6 并发翻译,进度走输出面板,已缓存自动跳过; 菜单手动触发 + 打开项目 8 秒后自动跑(每会话一次) - tools/preheat: 命令行批量工具,直接写用户缓存文件 - 缓存 key 统一 NormKey,预热/懒翻译/旧缓存三方互命中 - 已实测灌入 10 包 394 条译文(272 秒),常用包全部秒出
This commit is contained in:
1 parent
a7d53f25d4
commit
f5bfbac6c3
10 files changed
+663
-10
No files matched your search
@@ -181,6 +181,12 @@ type App struct {
|
||||
// 失败过、不再重试),transInflight 去重进行中的请求。见 translate.go。
|
||||
trans map[string]string
|
||||
transInflight map[string]bool
|
||||
// preheat 是标准库文档批量预热的进度(见 preheat.go),
|
||||
// preheatInflight 去重预热中已排队的条目。
|
||||
preheat preheatState
|
||||
preheatInflight map[string]bool
|
||||
// preheatAuto 保证每个会话只自动预热一次(打开项目后延时触发)。
|
||||
preheatAuto bool
|
||||
// wantFocus 表示下一帧要把焦点交回编辑器(弹窗/面板关闭后)。
|
||||
wantFocus bool
|
||||
|
||||
@@ -438,6 +444,12 @@ func (a *App) setRoot(abs string) {
|
||||
a.startLSP(abs)
|
||||
// 恢复这个工作区上次的标签、断点、面板与主题。
|
||||
a.restoreSession(abs)
|
||||
// 标准库文档预热:打开项目 8 秒后自动跑(等界面先稳),每个会话只自动
|
||||
// 一次;菜单里可随时手动重跑,已缓存的自动跳过。
|
||||
if !a.preheatAuto {
|
||||
a.preheatAuto = true
|
||||
time.AfterFunc(8*time.Second, a.PreheatDocs)
|
||||
}
|
||||
// 上次异常退出留下的未保存内容:先记下来,等界面就绪再问。
|
||||
a.collectPending()
|
||||
// 光标与滚动位置要等编辑器排过版才有意义,放到第一帧之后再套。
|
||||
|
||||
@@ -36,11 +36,11 @@ func TestSplitHoverDoc(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestHoverTranslationAssembly 验证拼回:译文插到正文位置,签名与链接原样。
|
||||
// 预填翻译缓存绕过 LLM 与窗口依赖。
|
||||
// 预填翻译缓存绕过 LLM 与窗口依赖;key 用 normDocKey(与运行时一致)。
|
||||
func TestHoverTranslationAssembly(t *testing.T) {
|
||||
docJoined, _, _ := splitHoverDoc(hoverSample)
|
||||
a := &App{trans: map[string]string{
|
||||
"Println formats using the default formats for its operands and writes to standard output.\nSpaces are always added between operands and a newline is appended.":
|
||||
"Println 用其操作数的默认格式进行格式化并写入标准输出,操作数之间总会添加空格,并附加换行。",
|
||||
normDocKey(docJoined): "Println 用其操作数的默认格式进行格式化并写入标准输出,操作数之间总会添加空格,并附加换行。",
|
||||
}, transInflight: map[string]bool{}, set: settings{TranslateDocs: true, AIBaseURL: "x", AIAPIKey: "y", AIModel: "z"}}
|
||||
|
||||
got := a.hoverTranslation(hoverSample)
|
||||
|
||||
@@ -183,6 +183,9 @@ func (a *App) menuItems() []*mygo.MenuItem {
|
||||
{Label: "重启语言服务器", Click: func(*mygo.MenuItem, *mygo.Window) {
|
||||
a.restartLSP()
|
||||
}},
|
||||
{Label: "预翻译标准库文档(AI)", Click: func(*mygo.MenuItem, *mygo.Window) {
|
||||
a.PreheatDocs()
|
||||
}},
|
||||
mygo.Separator(),
|
||||
{Label: "清空输出", Click: func(*mygo.MenuItem, *mygo.Window) {
|
||||
a.ClearOutput()
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"mygoland/internal/ai"
|
||||
"mygoland/internal/godoc"
|
||||
)
|
||||
|
||||
// preheatWorkers 是预热时的并发翻译数:免费接口也扛得住,太猛会限流。
|
||||
const preheatWorkers = 6
|
||||
|
||||
// preheatState 是标准库文档预热的进度。
|
||||
type preheatState struct {
|
||||
running bool
|
||||
done int
|
||||
total int
|
||||
pkg string
|
||||
}
|
||||
|
||||
// PreheatDocs 批量预热标准库文档翻译:本地源码提取 → 并发 LLM 翻译 → 入缓存。
|
||||
// 已缓存的自动跳过,反复调用是增量补齐;几百条译文之后补全/悬停全部秒出中文。
|
||||
// 进度走输出面板。须在主线程调用。
|
||||
func (a *App) PreheatDocs() {
|
||||
if !a.translateOn() {
|
||||
a.out = append(a.out, OutLine{Text: "AI 预热:请先在设置里配好「文档翻译」接口", Kind: OutWarn})
|
||||
a.panel, a.panelTab = true, 0
|
||||
a.refresh()
|
||||
return
|
||||
}
|
||||
if a.preheat.running {
|
||||
return
|
||||
}
|
||||
a.preheat.running = true
|
||||
a.panel, a.panelTab = true, 0
|
||||
a.out = append(a.out, OutLine{Text: "AI 预热:开始翻译标准库文档(已缓存的自动跳过)", Kind: OutWarn})
|
||||
a.refresh()
|
||||
|
||||
cfg := a.aiConfig()
|
||||
start := time.Now()
|
||||
jobs := make(chan string, 64)
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < preheatWorkers; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for text := range jobs {
|
||||
a.preheatOne(cfg, text)
|
||||
}
|
||||
}()
|
||||
}
|
||||
go func() {
|
||||
defer close(jobs)
|
||||
pkgs := 0
|
||||
for _, p := range godoc.CommonPkgs {
|
||||
docs, err := godoc.Docs(p)
|
||||
if err != nil {
|
||||
a.win.Update(func() {
|
||||
a.out = append(a.out, OutLine{Text: "AI 预热:跳过 " + p + "(" + firstLine(err.Error()) + ")", Kind: OutWarn})
|
||||
a.refresh()
|
||||
})
|
||||
continue
|
||||
}
|
||||
pkgs++
|
||||
// 读 a.trans(过滤已缓存)只能在主线程;过滤结果经 channel
|
||||
// 交给本 goroutine 再灌 worker 队列——主线程绝不阻塞等 LLM。
|
||||
listCh := make(chan []string, 1)
|
||||
a.win.Update(func() {
|
||||
a.preheat.pkg = p
|
||||
var list []string
|
||||
for _, d := range docs {
|
||||
k := normDocKey(d)
|
||||
if _, ok := a.trans[k]; ok {
|
||||
continue
|
||||
}
|
||||
if a.preheatInflight == nil {
|
||||
a.preheatInflight = map[string]bool{}
|
||||
}
|
||||
if a.preheatInflight[k] {
|
||||
continue
|
||||
}
|
||||
a.preheatInflight[k] = true
|
||||
a.preheat.total++
|
||||
list = append(list, d)
|
||||
}
|
||||
a.refresh()
|
||||
listCh <- list
|
||||
})
|
||||
for _, d := range <-listCh {
|
||||
jobs <- d
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
a.win.Update(func() {
|
||||
a.preheat.running = false
|
||||
a.saveTranslations()
|
||||
a.out = append(a.out, OutLine{
|
||||
Text: "✔ AI 预热完成:新增 " + strconv.Itoa(a.preheat.done) + " 条译文,覆盖 " +
|
||||
strconv.Itoa(pkgs) + " 个包,耗时 " + time.Since(start).Round(time.Second).String(),
|
||||
Kind: OutSuccess,
|
||||
})
|
||||
a.preheat = preheatState{}
|
||||
a.refresh()
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
// preheatOne 翻译一条并回主线程入缓存。
|
||||
func (a *App) preheatOne(cfg ai.Config, text string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
|
||||
defer cancel()
|
||||
tr, err := ai.TranslateDoc(ctx, cfg, text)
|
||||
a.win.Update(func() {
|
||||
k := normDocKey(text)
|
||||
delete(a.preheatInflight, k)
|
||||
a.preheat.done++
|
||||
if err != nil || tr == "" {
|
||||
// 失败不写哨兵:预热是尽力而为,下次预热还能重试。
|
||||
return
|
||||
}
|
||||
if a.trans == nil {
|
||||
a.trans = map[string]string{}
|
||||
}
|
||||
a.trans[k] = tr
|
||||
if a.preheat.done%40 == 0 {
|
||||
a.saveTranslations() // 阶段落盘,中途退出也不白翻。
|
||||
}
|
||||
a.refresh()
|
||||
})
|
||||
}
|
||||
@@ -8,8 +8,14 @@ import (
|
||||
"time"
|
||||
|
||||
"mygoland/internal/ai"
|
||||
"mygoland/internal/godoc"
|
||||
)
|
||||
|
||||
// normDocKey 是文档翻译缓存的 key:规范化空白(见 godoc.NormKey)。
|
||||
func normDocKey(s string) string {
|
||||
return godoc.NormKey(s)
|
||||
}
|
||||
|
||||
// docTranslation 返回一段补全文档的中文译文。
|
||||
//
|
||||
// 没有现成译文时后台发起 LLM 翻译(同一文本只发一次),先返回英文原文——
|
||||
@@ -19,35 +25,36 @@ func (a *App) docTranslation(text string) string {
|
||||
if !a.translateOn() || text == "" {
|
||||
return text
|
||||
}
|
||||
if tr, ok := a.trans[text]; ok {
|
||||
key := normDocKey(text)
|
||||
if tr, ok := a.trans[key]; ok {
|
||||
if tr == "" {
|
||||
// 空串哨兵:这个会话里翻译失败过,别再烧 API,直接原文。
|
||||
return text
|
||||
}
|
||||
return tr
|
||||
}
|
||||
if a.transInflight[text] {
|
||||
if a.transInflight[key] {
|
||||
return text
|
||||
}
|
||||
if a.trans == nil {
|
||||
a.trans = map[string]string{}
|
||||
a.transInflight = map[string]bool{}
|
||||
}
|
||||
a.transInflight[text] = true
|
||||
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, text)
|
||||
delete(a.transInflight, key)
|
||||
if err != nil || tr == "" {
|
||||
// 失败:缓存哨兵,同会话不再重试(重启后会再试)。
|
||||
logf("翻译失败: %v", err)
|
||||
a.trans[text] = ""
|
||||
a.trans[key] = ""
|
||||
return
|
||||
}
|
||||
a.trans[text] = tr
|
||||
a.trans[key] = tr
|
||||
a.saveTranslations()
|
||||
})
|
||||
}()
|
||||
@@ -93,7 +100,8 @@ func (a *App) loadTranslations() {
|
||||
a.trans = map[string]string{}
|
||||
for k, v := range m {
|
||||
if k != "" && v != "" {
|
||||
a.trans[k] = v
|
||||
// 旧版缓存 key 是原文,统一规范化,能被预热与懒翻译共同命中。
|
||||
a.trans[normDocKey(k)] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user