Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
feat(krillinai): add paired single-line subtitle translation
wulien-centerc5be806cb8db
diff --git a/runtime/krillinai/config/config-example.toml b/runtime/krillinai/config/config-example.tomlindex f8ed4b71..41771330 100644--- a/runtime/krillinai/config/config-example.toml+++ b/runtime/krillinai/config/config-example.toml@@ -4,7 +4,7 @@translate_parallel_num = 3 # 并发进行翻译的数量上限,建议值:3,倍于转录的并发量,如果使用TPM限制严格的API,可以适当调低transcribe_max_attempts = 3 # 转录最大尝试次数,建议值:3translate_max_attempts = 5 # 翻译最大尝试次数,建议值:5,如果模型参数量较少或翻译失败率较高可以适当调高- max_sentence_length = 70 # 每句最大字符数,超过这个长度的句子会被拆分,建议值:50-70+ max_sentence_length = 70 # 原文和译文单行上限:宽字素簇计2,其余基础字素簇及空格计1;50约为25个汉字enable_block_vtt_batch = false # 是否启用块级VTT批量翻译,建议值:false(默认关闭)vtt_batch_size = 10 # VTT批量翻译的批次大小,建议值:10target_language_first = true # 双语字幕中目标语言是否在上方,建议值:true(目标语言在上)diff --git a/runtime/krillinai/config/config.go b/runtime/krillinai/config/config.goindex 35b4385b..e03ed49c 100644--- a/runtime/krillinai/config/config.go+++ b/runtime/krillinai/config/config.go@@ -21,11 +21,11 @@ type App struct {TranslateParallelNum int `toml:"translate_parallel_num"`TranscribeMaxAttempts int `toml:"transcribe_max_attempts"`TranslateMaxAttempts int `toml:"translate_max_attempts"`- MaxSentenceLength int `toml:"max_sentence_length"`+ MaxSentenceLength int `toml:"max_sentence_length"` // 原文和译文单行上限:宽字素簇计2,其余基础字素簇计1EnableBlockVttBatch bool `toml:"enable_block_vtt_batch"`VttBatchSize int `toml:"vtt_batch_size"`TargetLanguageFirst bool `toml:"target_language_first"` // 双语字幕中目标语言是否在上- ShortSubtitleMaxChars int `toml:"short_subtitle_max_chars"` // 短字幕英文每行最大字符数+ ShortSubtitleMaxChars int `toml:"short_subtitle_max_chars"` // 短字幕原文按词分组的字符长度,独立于普通字幕的目标语言长度Proxy string `toml:"proxy"`ParsedProxy *url.URL `toml:"-"`}diff --git a/runtime/krillinai/docs/paired-subtitle-translation.md b/runtime/krillinai/docs/paired-subtitle-translation.mdnew file mode 100644index 00000000..7e029297--- /dev/null+++ b/runtime/krillinai/docs/paired-subtitle-translation.md@@ -0,0 +1,49 @@+# 双语字幕的单行分段与输入保护++YouTube 词级字幕、块级字幕、音频转录和原文 SRT 导入统一通过 `paired_translation.go` 翻译。正常路径每批调用一次模型,同时完成翻译、拆段和原译配对;不增加常规的第二次模型拆句或审校。++## 输入保护++输入先按 Unicode 句子边界组织语句,保留既有英文缩写例外。无标点或异常长句在模型调用前确定性拆分,优先 Unicode 词边界,没有自然边界的长词、URL才退到完整字素簇边界。单个输入片段最多 1024 个 UTF-8 字节,显示上限较小时进一步减小输入片段。这个预算与最终字幕显示上限分别控制。++相邻前后各两句作为上下文,每句上下文最多 256 字节,仅供理解,不输出到译文。批次同时限制句数、请求字节预算和预估响应预算,避免预拆之后再次形成超大请求。请求预算包含 Prompt、JSON、上下文和纠错预留;纠错反馈也限长。当前响应上限为 8192 token,预估响应预算为 6500,请求字节预算为 16000。它们是保守估算,不是针对所有模型的精确 tokenizer 或语义扩写上界。异常字素簇或无法满足预算的输入明确报错。++VTT/SRT 文本读取允许单行最多 4 MiB,之后才进入上述模型输入保护,超过读取上限则明确失败。++## 翻译与校验++原文和译文每段分别满足 `max_sentence_length`,任意一侧超限都要增加成对字幕条数。两侧都放得下时要求保持一对;同一句内允许译文按目标语言习惯调整语序,原文只能顺序切分,完整覆盖检查忽略空白但保留其他字符。++超过原文显示上限的输入附带 `source_segments_hint`,由本地按词及字素边界生成,参考片段均不超限且完整覆盖原文。末尾两段优先选择较均衡的词边界或标点边界,减少孤立短尾句。模型优先采用该参考,也可以为译文长度和语义对应调整边界;最终原译分段仍由同一次回答给出,并进行双侧校验。参考数据计入请求预算。++长度采用字素簇近似显示单位:宽字符或全角字符簇计2,其他基础字素簇计1,组合符、连接符和方向控制符不重复计宽,空格也计入上限。使用 `github.com/clipperhouse/uax29/v2` 的 Unicode 字素、词和句子边界,不按 UTF-8 字节或单个 rune 强行截断。50单位约为25个汉字,包含中文标点;该计数不等同于实际字体的像素宽度。++同一次模型回答先返回整句完整译文 `target_text`,再返回 `segments` 配对数组,每条直接包含 `source` 和 `target`。这种结构避免两个独立数组出现段数不一致,每一对仍检查两侧实际内容,禁止空字符串占位。全部 `target` 拼接必须与完整译文一致,覆盖检查忽略空白但保留其他字符;出现重复、遗漏、改写或乱序即拒绝,避免对每个原文片段重新翻译整句。此校验不增加正常路径的模型调用次数,也不能保证完整译文本身没有语义重复,仍需抽查。++本地检查 JSON、ID、顺序、两侧片段数量、实际内容、两侧长度、单行、字素边界、原文及译文覆盖和完整句子的目标语言特征。禁止实际换行、Unicode 换行控制符和会被 ASS 解释为换行的字面量转义。源文清理保留 ZWJ/ZWNJ,避免破坏 Emoji、印度文字和波斯文等内容。音频语气词过滤仅作用于无实际含义的译文填充词。++撤销模型多余切分时,必须两侧完整文本都满足长度上限,并能确认译文边界如何连接;无法确定是否为词内切分时保留原配对,避免引入或删除词间空格。++结构校验失败后附具体问题和上一轮结果,整批最多三次输出纠错。429和5xx使用独立的 `translate_max_attempts` 退避预算,支持取消,不消耗结构纠错次数。失败明确报错,不回填源文冒充译文。并发窗口全部成功后才按原顺序提交检查点。++DeepSeek V4 的 `deepseek-flash`、`deepseek-v4-flash` 和 `deepseek-v4-pro` 使用 `temperature=0.2`,减少结构和分段的随机变化,并发送 `reasoning_effort=none`,避免推理耗尽字幕 JSON 的输出预算;实测其低强度推理也可能耗尽预算。其他模型保留默认采样,不附加这些参数。接口返回 `finish_reason=length` 时明确报 `llm_translation_truncated`,不把被截断的内容当成完整译文或普通空响应。++## 时间与产物++普通原文、译文和双语 SRT 共享条数、编号和时间区间。时间定位使用去空白后的原文 rune 位置,显示单位不参与时间计算。词内切分在已知词时间内插值;仅有 cue 时间则在原 cue 区间内插值。这不是音素级对齐。++普通字幕和短字幕共用位置映射。短字幕保留完整目标片段,再独立拆分源文;YouTube 使用 `short_subtitle_max_chars`,音频使用 `MaxWordOneLine`,同时保留原文单行显示上限。无词时间的块级字幕保留普通双语兼容输出。匹配不到精确词文本时使用已有 cue 时间,避免再次整词搜索把合法词内片段判为失败。音频转录全文与词列表标点不同的兼容情况仍通过既有匹配器生成 cue,再共用 cue 位置映射。++原文和译文 SRT 的每条文本内部保持单行,双语 SRT 的两行分别对应目标语言和源语言。第三方播放器的视口、字体和自动折行由播放器控制,本功能仅负责翻译、配对、时间轴和字幕文件。++## 验证++定向测试:++```powershell+go test ./internal/service ./internal/pipeline ./internal/subtitle_style ./pkg/util ./pkg/openai ./internal/cli ./config -count=1 -timeout=120s+```++单元测试覆盖中文、日韩、泰文、阿拉伯文、印地文、重音组合、Emoji和混合文字的字素边界与长度规则,以及两万词无标点输入、双侧长度、不安全切分、重试与取消、词内普通/短字幕时间一致性。++结构检查不等于所有语言的翻译语义均已验证;真实模型的翻译质量仍需抽查。diff --git a/runtime/krillinai/go.mod b/runtime/krillinai/go.modindex 65887ab9..a708af15 100644--- a/runtime/krillinai/go.mod+++ b/runtime/krillinai/go.mod@@ -6,15 +6,17 @@ require (github.com/BurntSushi/toml v1.4.0github.com/aliyun/alibaba-cloud-sdk-go v1.63.72github.com/aliyun/alibabacloud-oss-go-sdk-v2 v1.1.3+ github.com/clipperhouse/uax29/v2 v2.7.0github.com/gin-gonic/gin v1.10.0github.com/go-ego/gse v1.0.2github.com/go-resty/resty/v2 v2.7.0github.com/google/uuid v1.4.0github.com/samber/lo v1.38.1- github.com/sashabaranov/go-openai v1.36.0+ github.com/sashabaranov/go-openai v1.42.0github.com/texttheater/golang-levenshtein v1.0.1go.uber.org/zap v1.25.0golang.org/x/sync v0.9.0+ golang.org/x/text v0.20.0)require (@@ -49,7 +51,6 @@ require (golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirectgolang.org/x/net v0.25.0 // indirectgolang.org/x/sys v0.20.0 // indirect- golang.org/x/text v0.20.0 // indirectgolang.org/x/time v0.4.0 // indirectgoogle.golang.org/protobuf v1.34.1 // indirectgopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirectdiff --git a/runtime/krillinai/go.sum b/runtime/krillinai/go.sumindex 03447767..728f8a54 100644--- a/runtime/krillinai/go.sum+++ b/runtime/krillinai/go.sum@@ -14,6 +14,8 @@ github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrcgithub.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=+github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=+github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=@@ -95,6 +97,8 @@ github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=github.com/sashabaranov/go-openai v1.36.0 h1:fcSrn8uGuorzPWCBp8L0aCR95Zjb/Dd+ZSML0YZy9EI=github.com/sashabaranov/go-openai v1.36.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=+github.com/sashabaranov/go-openai v1.42.0 h1:fgeZx7/D8dRT//PwXAGe9ylOMtj6vrs999uWF71K+f8=+github.com/sashabaranov/go-openai v1.42.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=diff --git a/runtime/krillinai/internal/pipeline/service_adapter.go b/runtime/krillinai/internal/pipeline/service_adapter.goindex 6512bf1d..36afe71e 100644--- a/runtime/krillinai/internal/pipeline/service_adapter.go+++ b/runtime/krillinai/internal/pipeline/service_adapter.go@@ -9,7 +9,7 @@ import ()type StageService interface {- TranslateSubtitleBlocks(context.Context, []*util.SrtBlock, *types.SubtitleTaskStepParam) error+ TranslateSubtitleBlocks(context.Context, []*util.SrtBlock, *types.SubtitleTaskStepParam) ([]*util.SrtBlock, error)PrepareMedia(context.Context, *types.SubtitleTaskStepParam) errorGenerateSubtitlesFromAudio(context.Context, *types.SubtitleTaskStepParam) errorGenerateSpeechFromSRT(context.Context, *types.SubtitleTaskStepParam) error@@ -20,7 +20,7 @@ type StageService interface {GenerateCoverImage(context.Context, pkgimage.GenerateRequest) (pkgimage.GenerateResult, error)}-func (a *ServiceAdapter) TranslateSubtitleBlocks(ctx context.Context, blocks []*util.SrtBlock, p *types.SubtitleTaskStepParam) error {+func (a *ServiceAdapter) TranslateSubtitleBlocks(ctx context.Context, blocks []*util.SrtBlock, p *types.SubtitleTaskStepParam) ([]*util.SrtBlock, error) {return a.svc.TranslateSubtitleBlocks(ctx, blocks, p)}diff --git a/runtime/krillinai/internal/pipeline/subtitle_import.go b/runtime/krillinai/internal/pipeline/subtitle_import.goindex 5b7dcbc1..6bbaa178 100644--- a/runtime/krillinai/internal/pipeline/subtitle_import.go+++ b/runtime/krillinai/internal/pipeline/subtitle_import.go@@ -34,7 +34,8 @@ func importSubtitleFile(ctx context.Context, svc StageService, req SubtitleReque}if !req.SRTTranslated {reportSubtitleProgress(req, "translating_subtitles", 40, "正在翻译导入的原文字幕")- if err := svc.TranslateSubtitleBlocks(ctx, blocks, step); err != nil {+ blocks, err = svc.TranslateSubtitleBlocks(ctx, blocks, step)+ if err != nil {return err}}diff --git a/runtime/krillinai/internal/pipeline/subtitle_paired_test.go b/runtime/krillinai/internal/pipeline/subtitle_paired_test.gonew file mode 100644index 00000000..cb68e278--- /dev/null+++ b/runtime/krillinai/internal/pipeline/subtitle_paired_test.go@@ -0,0 +1,36 @@+package pipeline++import (+ "context"+ "krillin-ai/pkg/util"+ "os"+ "path/filepath"+ "testing"+)++func TestImportedSubtitleExportsUseReturnedPairedSegments(t *testing.T) {+ root := t.TempDir()+ path := filepath.Join(root, "input.srt")+ if err := os.WriteFile(path, []byte("1\n00:00:00,000 --> 00:00:02,000\nWe can launch after review.\n\n"), 0600); err != nil {+ t.Fatal(err)+ }+ fake := &fakeStageService{translatedBlocks: []*util.SrtBlock{+ {Index: 1, Timestamp: "00:00:00,000 --> 00:00:01,000", OriginLanguageSentence: "We can launch", TargetLanguageSentence: "审核后,"},+ {Index: 2, Timestamp: "00:00:01,000 --> 00:00:02,000", OriginLanguageSentence: "after review.", TargetLanguageSentence: "我们就能发布。"},+ }}+ response, err := GenerateSubtitles(context.Background(), fake, SubtitleRequest{Input: "https://www.youtube.com/watch?v=import", Workdir: root, TaskID: "paired", OriginLang: "en", TargetLang: "zh_cn", PrepareVideo: true, InputSRT: path})+ if err != nil {+ t.Fatal(err)+ }+ for _, output := range []string{response.Outputs.OriginSRT, response.Outputs.TargetSRT, response.Outputs.BilingualSRT, response.Outputs.ShortOriginMixedSRT} {+ blocks, err := readSRTBlocks(output)+ if err != nil || len(blocks) != 2 {+ t.Fatalf("未使用重新分段后的结果 %s: %v, %v", output, blocks, err)+ }+ for i, block := range blocks {+ if block.Timestamp != fake.translatedBlocks[i].Timestamp {+ t.Fatalf("时间轴不一致: %s", output)+ }+ }+ }+}diff --git a/runtime/krillinai/internal/pipeline/subtitle_test.go b/runtime/krillinai/internal/pipeline/subtitle_test.goindex fbb4e9cc..a6c75f2e 100644--- a/runtime/krillinai/internal/pipeline/subtitle_test.go+++ b/runtime/krillinai/internal/pipeline/subtitle_test.go@@ -34,6 +34,7 @@ type fakeStageService struct {audioProgress []uint8audioErr erroromitPreparedVideo bool+ translatedBlocks []*util.SrtBlock}func TestImportedSubtitlesSkipRecognitionAndPreserveMultilineText(t *testing.T) {@@ -105,12 +106,15 @@ func fmtBool(value bool) string {return "source"}-func (f *fakeStageService) TranslateSubtitleBlocks(_ context.Context, blocks []*util.SrtBlock, _ *types.SubtitleTaskStepParam) error {+func (f *fakeStageService) TranslateSubtitleBlocks(_ context.Context, blocks []*util.SrtBlock, _ *types.SubtitleTaskStepParam) ([]*util.SrtBlock, error) {f.calls = append(f.calls, "translate-import")+ if f.translatedBlocks != nil {+ return f.translatedBlocks, f.processErr+ }for _, block := range blocks {block.TargetLanguageSentence = "translated " + block.OriginLanguageSentence}- return f.processErr+ return blocks, f.processErr}func (f *fakeStageService) PrepareMedia(_ context.Context, p *types.SubtitleTaskStepParam) error {diff --git a/runtime/krillinai/internal/service/audio2subtitle.go b/runtime/krillinai/internal/service/audio2subtitle.goindex 7937ff61..f27055da 100644--- a/runtime/krillinai/internal/service/audio2subtitle.go+++ b/runtime/krillinai/internal/service/audio2subtitle.go@@ -17,7 +17,6 @@ import ("runtime/debug""strconv""strings"- "sync""unicode""go.uber.org/zap"@@ -419,51 +418,11 @@ func (s Service) splitTextAndTranslateV2(id int,reportProgress func(completed, total int) error,) ([]*TranslatedItem, error) {- sentences := util.SplitTextSentences(inputText, config.Conf.App.MaxSentenceLength)- if len(sentences) == 0 {- return []*TranslatedItem{}, nil- }- // 补丁:whisper转录中文的时候很多句子后面不输出符号,导致上面基于符号的切分失效- if IsSplitUseSpace(originLang) {- newSentences := make([]string, 0)- for _, sentence := range sentences {- newSentences = append(newSentences, strings.Split(sentence, " ")...)- }- sentences = newSentences- }-- shortSentences := make([]string, 0)- //判断句子如果还是过长,就继续用大模型拆句- for _, sentence := range sentences {- if sentence == "" {- continue- }- if util.CountEffectiveChars(sentence) <= config.Conf.App.MaxSentenceLength {- shortSentences = append(shortSentences, sentence)- continue- }-- // 递归拆分长句子直到满足长度要求,保持顺序- splitSentences, err := s.splitSentenceRecursively(sentence, 0, 5) // 最多5层递归- if err != nil {- log.GetLogger().Error("splitSentenceRecursively error", zap.Error(err), zap.Any("sentence", sentence))- // 如果拆分失败,直接添加原句子- shortSentences = append(shortSentences, sentence)- } else {- shortSentences = append(shortSentences, splitSentences...)- }- }-- sentences = shortSentences- results := make([]*TranslatedItem, len(sentences))- var checkpointMu sync.Mutex- completed := 0- checkpoint := func(start int, items []*TranslatedItem) error {- // Checkpoint serialization reads every result, so publish under one lock.- checkpointMu.Lock()- defer checkpointMu.Unlock()- copy(results[start:], items)- completed += len(items)+ sentences := splitTranslationSentences(inputText)+ translator := &Translator{chatCompleter: s.ChatCompleter, filterNonSemanticFillers: enableModalFilter}+ var results []*TranslatedItem+ _, err := translator.translatePairedSentences(ctx, sentences, originLang, targetLang, subtitleTargetLimit(), translationBatchSize, func(batch []pairedSentence, completed, total int) error {+ results = append(results, flattenPairedSentences(batch)...)if basePath != "" {path := filepath.Join(basePath, fmt.Sprintf(types.SubtitleTaskTranslationDataPersistenceFileNamePattern, id))if err := util.SaveToDisk(results, path); err != nil {@@ -471,18 +430,9 @@ func (s Service) splitTextAndTranslateV2(}}if reportProgress != nil {- return reportProgress(completed, len(sentences))+ return reportProgress(completed, total)}return nil- }-- err := runTranslationBatches(ctx, (len(sentences)+translationBatchSize-1)/translationBatchSize, func(ctx context.Context, batchIndex int) error {- start := batchIndex * translationBatchSize- end := start + translationBatchSize- if end > len(sentences) {- end = len(sentences)- }- return s.translateSentenceRange(ctx, sentences, start, end, targetLang, enableModalFilter, checkpoint)})if err != nil {return nil, err@@ -834,8 +784,6 @@ func (s Service) startTranslateWorker(ctx context.Context, eg *errgroup.Group, spendingTranslationQueue chan DataWithId[string], translationProgressQueue chan DataWithId[TranslationProgress],translatedQueue chan DataWithId[[]*TranslatedItem]) {- const primaryTranslationProgressShare = 0.35-eg.Go(func() error {for {select {@@ -849,12 +797,9 @@ func (s Service) startTranslateWorker(ctx context.Context, eg *errgroup.Group, svar err error// 翻译文本log.GetLogger().Info("Begin to translate", zap.Any("taskId", stepParam.TaskId), zap.Any("splitId", translateItem.Id))- attempts := config.Conf.App.TranslateMaxAttempts- if attempts < 1 {- attempts = 1- }+reportProgress := func(completed, total int) error {- fraction := scaledProgress(completed, total, 0, primaryTranslationProgressShare)+ fraction := scaledProgress(completed, total, 0, 1)select {case translationProgressQueue <- DataWithId[TranslationProgress]{Data: TranslationProgress{Fraction: fraction},@@ -865,44 +810,17 @@ func (s Service) startTranslateWorker(ctx context.Context, eg *errgroup.Group, sreturn ctx.Err()}}- for range attempts {- translatedResults, err = s.splitTextAndTranslateV2(ctx, stepParam.TaskBasePath, translateItem.Data, stepParam.OriginLanguage, stepParam.TargetLanguage, stepParam.EnableModalFilter, translateItem.Id, reportProgress)- if err == nil {- break- }- }+ translatedResults, err = s.splitTextAndTranslateV2(ctx, stepParam.TaskBasePath, translateItem.Data, stepParam.OriginLanguage, stepParam.TargetLanguage, stepParam.EnableModalFilter, translateItem.Id, reportProgress)if err != nil {return fmt.Errorf("audioToSubtitle audioToSrt splitTextAndTranslate err: %w", err)}_ = util.SaveToDisk(translatedResults, filepath.Join(stepParam.TaskBasePath, fmt.Sprintf(types.SubtitleTaskTranslationDataPersistenceFileNamePattern, translateItem.Id)))log.GetLogger().Info("Translate completed", zap.Any("taskId", stepParam.TaskId), zap.Any("splitId", translateItem.Id))- reportTranslationProgress(- ctx,- translationProgressQueue,- translateItem.Id,- primaryTranslationProgressShare,- )- // 二次分割长句- splitResults, err := s.splitTranslateItem(translatedResults, func(completed, total int) {- reportTranslationProgress(- ctx,- translationProgressQueue,- translateItem.Id,- scaledProgress(completed, total, primaryTranslationProgressShare, 1),- )- })- if err != nil {- // 不中断- log.GetLogger().Error("audioToSubtitle audioToSrt splitTranslateItem err", zap.Any("taskId", stepParam.TaskId), zap.Any("splitId", translateItem.Id), zap.Error(err))- translatedQueue <- DataWithId[[]*TranslatedItem]{- Data: translatedResults,- Id: translateItem.Id,- }- } else {- translatedQueue <- DataWithId[[]*TranslatedItem]{- Data: splitResults,- Id: translateItem.Id,- }+ reportTranslationProgress(ctx, translationProgressQueue, translateItem.Id, 1)+ select {+ case translatedQueue <- DataWithId[[]*TranslatedItem]{Data: translatedResults, Id: translateItem.Id}:+ case <-ctx.Done():+ return ctx.Err()}}}@@ -1534,205 +1452,59 @@ func generateSrtWithTimestamps(srtBlocks []*util.SrtBlock, tsOffset float64, worif len(srtBlocks) == 0 || len(words) == 0 {return nil}-- // 获取每个字幕块的时间戳- var lastTs float64- shortOriginSrtMap := make(map[int][]util.SrtBlock, 0)- timeMatcher := NewTimestampGenerator()- newSrtBlocks, err := timeMatcher.GenerateTimestamps(srtBlocks, words, stepParam.OriginLanguage, tsOffset)+ blocks, spans, err := pairedAudioTiming(srtBlocks, words, stepParam.OriginLanguage, tsOffset)if err != nil {- return fmt.Errorf("audioToSubtitle generateTimestamps GenerateTimestamps error: %w", err)+ return fmt.Errorf("generate paired subtitle timestamps: %w", err)}- for index := range srtBlocks {- srtBlocks[index].Timestamp = newSrtBlocks[index].Timestamp+ if err := normalizeSrtBlocks(blocks, false); err != nil {+ return err}-- for blockIndex, srtBlock := range srtBlocks {- if srtBlock.OriginLanguageSentence == "" {- continue- }- var sentenceTs types.SrtSentence- var sentenceWords []types.Word- var ts float64- if stepParam.OriginLanguage == types.LanguageNameThai {- sentenceTs, sentenceWords, ts, err = getSentenceTimestampsFromGeneratedBlock(- words,- newSrtBlocks[blockIndex].Timestamp,- lastTs,- tsOffset,- )- } else {- sentenceTs, sentenceWords, ts, err = getSentenceTimestamps(words, srtBlock.OriginLanguageSentence, lastTs, stepParam.OriginLanguage)- }- if err != nil || ts < lastTs {- continue- }- srtBlock.Timestamp = fmt.Sprintf("%s --> %s", util.FormatTime(float32(sentenceTs.Start+tsOffset)), util.FormatTime(float32(sentenceTs.End+tsOffset)))-- // 生成短句子的英文字幕- var (- originSentence string- startWord types.Word- endWord types.Word- )-- if len(sentenceWords) <= stepParam.MaxWordOneLine {- shortOriginSrtMap[srtBlock.Index] = append(shortOriginSrtMap[srtBlock.Index], util.SrtBlock{- Index: srtBlock.Index,- Timestamp: fmt.Sprintf("%s --> %s", util.FormatTime(float32(sentenceTs.Start+tsOffset)), util.FormatTime(float32(sentenceTs.End+tsOffset))),- OriginLanguageSentence: srtBlock.OriginLanguageSentence,- })- lastTs = ts- continue- }-- thisLineWord := stepParam.MaxWordOneLine- if len(sentenceWords) > stepParam.MaxWordOneLine && len(sentenceWords) <= 2*stepParam.MaxWordOneLine {- thisLineWord = len(sentenceWords)/2 + 1- } else if len(sentenceWords) > 2*stepParam.MaxWordOneLine && len(sentenceWords) <= 3*stepParam.MaxWordOneLine {- thisLineWord = len(sentenceWords)/3 + 1- } else if len(sentenceWords) > 3*stepParam.MaxWordOneLine && len(sentenceWords) <= 4*stepParam.MaxWordOneLine {- thisLineWord = len(sentenceWords)/4 + 1- } else if len(sentenceWords) > 4*stepParam.MaxWordOneLine && len(sentenceWords) <= 5*stepParam.MaxWordOneLine {- thisLineWord = len(sentenceWords)/5 + 1- }-- i := 1- nextStart := true- for _, word := range sentenceWords {- if nextStart {- startWord = word- if startWord.Start < lastTs {- startWord.Start = lastTs- }- if startWord.Start < endWord.End {- startWord.Start = endWord.End- }-- if startWord.Start < sentenceTs.Start {- startWord.Start = sentenceTs.Start- }- // 首个单词的开始时间戳大于句子的结束时间戳,说明这个单词找错了,放弃掉- if startWord.End > sentenceTs.End {- originSentence += word.Text + " "- continue- }- originSentence += word.Text + " "- endWord = startWord- i++- nextStart = false- continue- }-- originSentence += word.Text + " "- if endWord.End < word.End {- endWord = word- }-- if endWord.End > sentenceTs.End {- endWord.End = sentenceTs.End- }-- if i%thisLineWord == 0 && i > 1 {- shortOriginSrtMap[srtBlock.Index] = append(shortOriginSrtMap[srtBlock.Index], util.SrtBlock{- Index: srtBlock.Index,- Timestamp: fmt.Sprintf("%s --> %s", util.FormatTime(float32(startWord.Start+tsOffset)), util.FormatTime(float32(endWord.End+tsOffset))),- OriginLanguageSentence: originSentence,- })- originSentence = ""- nextStart = true- }- i++- }-- if originSentence != "" {- shortOriginSrtMap[srtBlock.Index] = append(shortOriginSrtMap[srtBlock.Index], util.SrtBlock{- Index: srtBlock.Index,- Timestamp: fmt.Sprintf("%s --> %s", util.FormatTime(float32(startWord.Start+tsOffset)), util.FormatTime(float32(endWord.End+tsOffset))),- OriginLanguageSentence: originSentence,- })- }- lastTs = ts+ for i := range blocks {+ srtBlocks[i].Timestamp = blocks[i].Timestamp}- if err := normalizeSrtBlocks(newSrtBlocks, false); err != nil {- return fmt.Errorf("normalize bilingual subtitle timeline: %w", err)+ // 普通和短字幕共享位置映射,允许边界落在转录器的一个词内部。+ maxWords := stepParam.MaxWordOneLine+ if maxWords <= 0 {+ maxWords = 12}- if err := normalizeSrtBlocks(srtBlocks, true); err != nil {- return fmt.Errorf("normalize mixed subtitle timeline: %w", err)+ shortOriginSrtMap, err := splitShortSubtitleBlocks(blocks, spans, subtitleTargetLimit(), maxWords)+ if err != nil {+ return err}- shortTimeline := make([]*util.SrtBlock, 0)- for _, srtBlock := range srtBlocks {- blocks := shortOriginSrtMap[srtBlock.Index]- for index := range blocks {- shortTimeline = append(shortTimeline, &blocks[index])+ var shortBlocks, mixedBlocks []*util.SrtBlock+ for _, block := range srtBlocks {+ for _, short := range shortOriginSrtMap[block.Index] {+ short.Index = len(shortBlocks) + 1+ shortBlocks = append(shortBlocks, &short)}- shortOriginSrtMap[srtBlock.Index] = blocks}- if err := normalizeSrtBlocks(shortTimeline, false); err != nil {- return fmt.Errorf("normalize vertical subtitle timeline: %w", err)+ if err := normalizeSrtBlocks(shortBlocks, false); err != nil {+ return fmt.Errorf("normalize short subtitle timeline: %w", err)}-- // 保存带时间戳的原始字幕- finalBilingualSrtFileName := fmt.Sprintf("%s/%s", stepParam.TaskBasePath, fmt.Sprintf(types.SubtitleTaskSplitBilingualSrtFileNamePattern, segmentIdx))- finalBilingualSrtFile, err := os.Create(finalBilingualSrtFileName)- if err != nil {- return fmt.Errorf("audioToSubtitle generateTimestamps create bilingual srt file error: %w", err)- }- defer finalBilingualSrtFile.Close()-- // 写入字幕文件- for _, srtBlock := range newSrtBlocks {- _, _ = finalBilingualSrtFile.WriteString(fmt.Sprintf("%d\n", srtBlock.Index))- _, _ = finalBilingualSrtFile.WriteString(srtBlock.Timestamp + "\n")- if stepParam.SubtitleResultType == types.SubtitleResultTypeBilingualTranslationOnTop {- _, _ = finalBilingualSrtFile.WriteString(srtBlock.TargetLanguageSentence + "\n")- _, _ = finalBilingualSrtFile.WriteString(srtBlock.OriginLanguageSentence + "\n\n")- } else {- // on bottom 或者单语类型,都用on bottom- _, _ = finalBilingualSrtFile.WriteString(srtBlock.OriginLanguageSentence + "\n")- _, _ = finalBilingualSrtFile.WriteString(srtBlock.TargetLanguageSentence + "\n\n")+ shortIndex := 0+ for _, block := range srtBlocks {+ mixedBlocks = append(mixedBlocks, &util.SrtBlock{+ Index: len(mixedBlocks) + 1, Timestamp: block.Timestamp, OriginLanguageSentence: block.TargetLanguageSentence,+ })+ for range shortOriginSrtMap[block.Index] {+ short := *shortBlocks[shortIndex]+ short.Index = len(mixedBlocks) + 1+ mixedBlocks = append(mixedBlocks, &short)+ shortIndex++}}-- // 保存带时间戳的字幕,长中文+短英文(示意,也支持其他语言)- srtShortOriginMixedFileName := fmt.Sprintf("%s/%s", stepParam.TaskBasePath, fmt.Sprintf(types.SubtitleTaskSplitShortOriginMixedSrtFileNamePattern, segmentIdx))- srtShortOriginMixedFile, err := os.Create(srtShortOriginMixedFileName)- if err != nil {- return fmt.Errorf("audioToSubtitle generateTimestamps create srtShortOriginMixedFile err: %w", err)- }- defer srtShortOriginMixedFile.Close()-- // 保存带时间戳的短英文字幕- srtShortOriginFileName := fmt.Sprintf("%s/%s", stepParam.TaskBasePath, fmt.Sprintf(types.SubtitleTaskSplitShortOriginSrtFileNamePattern, segmentIdx))- srtShortOriginFile, err := os.Create(srtShortOriginFileName)- if err != nil {- return fmt.Errorf("audioToSubtitle generateTimestamps create srtShortOriginFile err: %w", err)+ svc := &YouTubeSubtitleService{}+ targetFirst := stepParam.SubtitleResultType == types.SubtitleResultTypeBilingualTranslationOnTop+ bilingual := filepath.Join(stepParam.TaskBasePath, fmt.Sprintf(types.SubtitleTaskSplitBilingualSrtFileNamePattern, segmentIdx))+ if err := svc.writeBilingualSrtFile(blocks, bilingual, targetFirst); err != nil {+ return err}- defer srtShortOriginFile.Close()-- mixedSrtNum := 1- shortSrtNum := 1- // 写入短英文混合字幕文件- for _, srtBlock := range srtBlocks {- srtShortOriginMixedFile.WriteString(fmt.Sprintf("%d\n", mixedSrtNum))- srtShortOriginMixedFile.WriteString(srtBlock.Timestamp + "\n")- srtShortOriginMixedFile.WriteString(srtBlock.TargetLanguageSentence + "\n\n")- mixedSrtNum++- shortOriginSentence := shortOriginSrtMap[srtBlock.Index]- for _, shortOriginBlock := range shortOriginSentence {- srtShortOriginMixedFile.WriteString(fmt.Sprintf("%d\n", mixedSrtNum))- srtShortOriginMixedFile.WriteString(shortOriginBlock.Timestamp + "\n")- srtShortOriginMixedFile.WriteString(shortOriginBlock.OriginLanguageSentence + "\n\n")- mixedSrtNum++-- srtShortOriginFile.WriteString(fmt.Sprintf("%d\n", shortSrtNum))- srtShortOriginFile.WriteString(shortOriginBlock.Timestamp + "\n")- srtShortOriginFile.WriteString(shortOriginBlock.OriginLanguageSentence + "\n\n")- shortSrtNum++- }+ mixed := filepath.Join(stepParam.TaskBasePath, fmt.Sprintf(types.SubtitleTaskSplitShortOriginMixedSrtFileNamePattern, segmentIdx))+ if err := svc.writeOriginLanguageSrtFile(mixedBlocks, mixed); err != nil {+ return err}-- return nil+ source := filepath.Join(stepParam.TaskBasePath, fmt.Sprintf(types.SubtitleTaskSplitShortOriginSrtFileNamePattern, segmentIdx))+ return svc.writeOriginLanguageSrtFile(shortBlocks, source)}func getSentenceTimestampsFromGeneratedBlock(diff --git a/runtime/krillinai/internal/service/audio2subtitle_test.go b/runtime/krillinai/internal/service/audio2subtitle_test.goindex 92b4dae8..84dc1eaa 100644--- a/runtime/krillinai/internal/service/audio2subtitle_test.go+++ b/runtime/krillinai/internal/service/audio2subtitle_test.go@@ -2,7 +2,6 @@ package serviceimport ("context"- "encoding/json""errors""fmt""krillin-ai/config"@@ -309,40 +308,20 @@ type echoBatchCompleter struct {}func (c *echoBatchCompleter) ChatCompletion(prompt string) (string, error) {- const marker = "输入 JSON:\n"- start := strings.LastIndex(prompt, marker)- if start < 0 {- return "", errors.New("missing input JSON")- }- var input struct {- Items []struct {- Index int `json:"index"`- Text string `json:"text"`- } `json:"items"`- }- if err := json.Unmarshal([]byte(prompt[start+len(marker):]), &input); err != nil {+ input, err := decodePairedTestInput(prompt)+ if err != nil {return "", err}c.mu.Lock()- c.batchSizes = append(c.batchSizes, len(input.Items))+ c.batchSizes = append(c.batchSizes, len(input.Sentences))c.mu.Unlock()- output := struct {- Translations []struct {- Index int `json:"index"`- Text string `json:"text"`- } `json:"translations"`- }{}- for _, item := range input.Items {- output.Translations = append(output.Translations, struct {- Index int `json:"index"`- Text string `json:"text"`- }{Index: item.Index, Text: "translated: " + item.Text})- }- data, err := json.Marshal(output)- return string(data), err+ return echoPairedTestResponse(input)}func TestSplitTextAndTranslateUsesBatchesAndReportsProgress(t *testing.T) {+ previousParallel := config.Conf.App.TranslateParallelNum+ config.Conf.App.TranslateParallelNum = 1+ t.Cleanup(func() { config.Conf.App.TranslateParallelNum = previousParallel })previousMaxSentenceLength := config.Conf.App.MaxSentenceLengthconfig.Conf.App.MaxSentenceLength = 100t.Cleanup(func() { config.Conf.App.MaxSentenceLength = previousMaxSentenceLength })diff --git a/runtime/krillinai/internal/service/paired_translation.go b/runtime/krillinai/internal/service/paired_translation.gonew file mode 100644index 00000000..8547bd58--- /dev/null+++ b/runtime/krillinai/internal/service/paired_translation.go@@ -0,0 +1,567 @@+package service++import (+ "context"+ _ "embed"+ "encoding/json"+ "errors"+ "fmt"+ "krillin-ai/config"+ "krillin-ai/internal/types"+ "krillin-ai/log"+ "krillin-ai/pkg/util"+ "net/http"+ "sort"+ "strconv"+ "strings"+ "time"+ "unicode"+ "unicode/utf8"++ "github.com/clipperhouse/uax29/v2/sentences"+ openaiapi "github.com/sashabaranov/go-openai"+ "go.uber.org/zap"+ "golang.org/x/sync/errgroup"+)++//go:embed paired_translation_prompt.txt+var pairedTranslationPrompt string++//go:embed paired_translation_retry.txt+var pairedTranslationRetry string++type pairedSentence struct {+ ID string `json:"sentence_id"`+ FullTarget string `json:"target_text"`+ Target []string `json:"target_segments"`+ Source []string `json:"source_segments"`+}++type pairedTranslationOutput struct {+ Sentences []pairedTranslationOutputSentence `json:"sentences"`+}++type pairedTranslationOutputSentence struct {+ ID string `json:"sentence_id"`+ FullTarget string `json:"target_text"`+ Segments []pairedTranslationSegment `json:"segments"`+}++type pairedTranslationSegment struct {+ Source string `json:"source"`+ Target string `json:"target"`+}++func flattenPairedSentences(sentences []pairedSentence) []*TranslatedItem {+ var items []*TranslatedItem+ for _, sentence := range sentences {+ for i, source := range sentence.Source {+ items = append(items, &TranslatedItem{OriginText: strings.TrimSpace(source), TranslatedText: strings.TrimSpace(sentence.Target[i])})+ }+ }+ return items+}++type pairedTranslationInput struct {+ SourceLanguage string `json:"source_language"`+ TargetLanguage string `json:"target_language"`+ MaxTargetChars int `json:"max_target_display_units"`+ MaxSourceChars int `json:"max_source_display_units"`+ MaxFullwidthChars int `json:"max_fullwidth_chars"`+ Sentences []pairedTranslationSource `json:"sentences"`+}++type pairedTranslationSource struct {+ ID string `json:"sentence_id"`+ Text string `json:"text"`+ SourceHint []string `json:"source_segments_hint,omitempty"`+ Before []string `json:"context_before"`+ After []string `json:"context_after"`+}++const (+ translationSentenceBytes = 1024+ translationRequestBytes = 16000+ translationOutputBudget = 6500 // 为 8192 token 响应留出 JSON 和纠错余量。+)++func buildPairedTranslationBatches(texts []string, originLang, targetLang types.StandardLanguageCode, limit, batchSize int) ([]pairedTranslationInput, error) {+ var batches []pairedTranslationInput+ for start := 0; start < len(texts); {+ input := pairedTranslationInput{+ SourceLanguage: types.GetStandardLanguageName(originLang), TargetLanguage: types.GetStandardLanguageName(targetLang),+ MaxSourceChars: limit, MaxTargetChars: limit, MaxFullwidthChars: limit / 2,+ }+ outputBudget := 0+ for i := start; i < min(len(texts), start+batchSize); i++ {+ source := pairedTranslationSource{ID: strconv.Itoa(i + 1), Text: texts[i]}+ if subtitleTargetLength(source.Text) > limit {+ var err error+ source.SourceHint, err = splitSubtitleSourceHint(source.Text, limit)+ if err != nil {+ return nil, err+ }+ }+ // 上下文独立限长,避免相邻超长句重新扩大请求。+ for j := max(0, i-2); j < i; j++ {+ parts, _ := splitSubtitleText(texts[j], func(s string) bool { return len(s) <= 256 })+ if len(parts) > 0 {+ source.Before = append(source.Before, parts[len(parts)-1])+ }+ }+ for j := i + 1; j < min(len(texts), i+3); j++ {+ parts, _ := splitSubtitleText(texts[j], func(s string) bool { return len(s) <= 256 })+ if len(parts) > 0 {+ source.After = append(source.After, parts[0])+ }+ }+ estimate := 4*len(texts[i]) + (3*subtitleTargetLength(texts[i])/limit+1)*48+ input.Sentences = append(input.Sentences, source)+ payload, err := json.Marshal(input)+ if err != nil {+ return nil, err+ }+ if len(payload)+len(pairedTranslationPrompt)+6000 > translationRequestBytes || outputBudget+estimate > translationOutputBudget {+ input.Sentences = input.Sentences[:len(input.Sentences)-1]+ break+ }+ outputBudget += estimate+ }+ if len(input.Sentences) == 0 {+ return nil, fmt.Errorf("第%d句超过翻译请求预算,无法安全提交", start+1)+ }+ start += len(input.Sentences)+ batches = append(batches, input)+ }+ return batches, nil+}++// 原文和译文使用相同的单行显示上限。+func subtitleTargetLimit() int {+ limit := config.Conf.App.MaxSentenceLength+ if limit <= 0 {+ limit = 50+ }+ return limit+}++func compactSubtitleText(text string) string {+ return strings.Map(func(r rune) rune {+ if unicode.IsSpace(r) {+ return -1+ }+ return r+ }, text)+}++func hasSubtitleContent(text string) bool {+ return strings.ContainsFunc(text, func(r rune) bool { return unicode.IsLetter(r) || unicode.IsNumber(r) || unicode.IsSymbol(r) })+}++func splitTranslationSentences(text string) []string {+ var texts []string+ var pending strings.Builder+ iter := sentences.FromString(text)+ for iter.Next() {+ pending.WriteString(iter.Value())+ fields := strings.Fields(pending.String())+ if len(fields) > 0 && subtitleAbbreviations[strings.ToLower(fields[len(fields)-1])] {+ continue+ }+ if sentence := strings.TrimSpace(pending.String()); sentence != "" {+ texts = append(texts, sentence)+ }+ pending.Reset()+ }+ if sentence := strings.TrimSpace(pending.String()); sentence != "" {+ texts = append(texts, sentence)+ }+ return texts+}++func (t *Translator) translatePairedSentences(ctx context.Context, texts []string, originLang, targetLang types.StandardLanguageCode, limit, batchSize int, onBatch func([]pairedSentence, int, int) error) ([]pairedSentence, error) {+ if limit < 1 || batchSize < 1 {+ return nil, fmt.Errorf("目标长度和批次大小必须大于0")+ }+ var protected []string+ var inputSentences []string+ for _, text := range texts {+ inputSentences = append(inputSentences, splitTranslationSentences(text)...)+ }+ for _, text := range inputSentences {+ parts, err := splitSubtitleText(strings.Join(strings.Fields(text), " "), func(s string) bool { return len(s) <= min(translationSentenceBytes, max(32, limit*16)) })+ if err != nil {+ return nil, err+ }+ protected = append(protected, parts...)+ }+ texts = protected+ inputs, err := buildPairedTranslationBatches(texts, originLang, targetLang, limit, batchSize)+ if err != nil {+ return nil, err+ }+ translateBatch := func(ctx context.Context, input pairedTranslationInput) ([]pairedSentence, error) {+ start, _ := strconv.Atoi(input.Sentences[0].ID)+ end := start + len(input.Sentences) - 1+ payload, err := json.Marshal(input)+ if err != nil {+ return nil, err+ }+ replacer := strings.NewReplacer("{{MAX_TARGET_CHARS}}", strconv.Itoa(limit), "{{MAX_FULLWIDTH_CHARS}}", strconv.Itoa(limit/2))+ basePrompt := replacer.Replace(pairedTranslationPrompt)+ if t.filterNonSemanticFillers {+ basePrompt += "\n启用语气词过滤:仅译文可以忽略 um、uh、ah 等没有实际语义的填充词;原文仍须原样完整保留。Yes/No、否定、可能性、时间和其他有含义的词不能省略。\n"+ }+ basePrompt += "\n输入 JSON:\n" + string(payload)+ prompt := basePrompt+ var batch []pairedSentence+ requestFailures := 0+ for attempt := 0; attempt < 3; {+ if err = ctx.Err(); err != nil {+ return nil, err+ }+ var response string+ response, err = chatCompletionWithContext(ctx, t.chatCompleter, prompt)+ if ctx.Err() != nil {+ return nil, ctx.Err()+ }+ if isTransientTranslationRequestError(err) {+ if requestFailures >= max(1, config.Conf.App.TranslateMaxAttempts)-1 {+ break+ }+ delay := time.Second << min(requestFailures, 4)+ requestFailures+++ log.GetLogger().Warn("字幕翻译接口临时失败,退避后重试", zap.Int("start_sentence", start), zap.Int("end_sentence", end), zap.Int("request_retry", requestFailures), zap.Duration("delay", delay), zap.Error(err))+ select {+ case <-ctx.Done():+ return nil, ctx.Err()+ case <-time.After(delay):+ }+ continue+ }+ attempt+++ if err == nil {+ batch, err = t.parsePairedTranslation(response, input, originLang, targetLang)+ if err == nil {+ break+ }+ log.GetLogger().Warn("字幕配对本地检查未通过", zap.Int("start_sentence", start), zap.Int("end_sentence", end), zap.Int("attempt", attempt), zap.Error(err))+ feedback := strings.NewReplacer("{{ISSUES}}", subtitleTextPreview(err.Error(), 3500), "{{PREVIOUS_RESPONSE}}", subtitleTextPreview(response, 1000)).Replace(pairedTranslationRetry)+ prompt = basePrompt + "\n\n" + replacer.Replace(feedback) + fmt.Sprintf("\n本次是第%d/3次请求。必须按上面的具体错误重新生成,不能再次照抄错误结果。", attempt+1)+ }+ }+ if err != nil {+ return nil, fmt.Errorf("句子%d-%d翻译失败: %w", start, end, err)+ }+ return batch, nil+ }+ parallel := translationParallelism()+ var result []pairedSentence+ done := 0+ for start := 0; start < len(inputs); {+ count := min(parallel, len(inputs)-start)+ batches := make([][]pairedSentence, count)+ group, batchCtx := errgroup.WithContext(ctx)+ for i := range count {+ input := inputs[start+i]+ group.Go(func() error {+ batch, err := translateBatch(batchCtx, input)+ if err == nil {+ batches[i] = batch+ }+ return err+ })+ }+ if err := group.Wait(); err != nil {+ return nil, err+ }+ // 检查点和时间轴按原顺序提交,并将未提交结果限制在一个并发窗口内。+ for _, batch := range batches {+ if err := ctx.Err(); err != nil {+ return nil, err+ }+ done += len(batch)+ result = append(result, batch...)+ if onBatch != nil {+ if err := onBatch(batch, done, len(texts)); err != nil {+ return nil, err+ }+ }+ }+ start += count+ }+ return result, nil+}++func isTransientTranslationRequestError(err error) bool {+ var apiError *openaiapi.APIError+ var requestError *openaiapi.RequestError+ status := 0+ if errors.As(err, &apiError) {+ status = apiError.HTTPStatusCode+ } else if errors.As(err, &requestError) {+ status = requestError.HTTPStatusCode+ }+ return status == http.StatusTooManyRequests || status >= 500 && status < 600+}++func (t *Translator) parsePairedTranslation(response string, input pairedTranslationInput, originLang, targetLang types.StandardLanguageCode) ([]pairedSentence, error) {+ var output pairedTranslationOutput+ if err := json.Unmarshal([]byte(util.ExtractJSONObject(response, "sentences")), &output); err != nil {+ return nil, fmt.Errorf("无效JSON: %w", err)+ }+ if len(output.Sentences) != len(input.Sentences) {+ return nil, fmt.Errorf("句子数量为%d,要求%d", len(output.Sentences), len(input.Sentences))+ }+ var result []pairedSentence+ var issues []error+ for i, item := range output.Sentences {+ sentence := pairedSentence{ID: item.ID, FullTarget: item.FullTarget}+ for _, segment := range item.Segments {+ sentence.Source = append(sentence.Source, segment.Source)+ sentence.Target = append(sentence.Target, segment.Target)+ }+ result = append(result, sentence)+ original := input.Sentences[i]+ if sentence.ID != original.ID {+ issues = append(issues, fmt.Errorf("句子ID或顺序错误:收到%q,要求%q", sentence.ID, original.ID))+ continue+ }+ if len(item.Segments) == 0 {+ issues = append(issues, fmt.Errorf("句子%s缺少segments原译配对,必须返回至少一条含source和target的字幕", sentence.ID))+ }+ if !hasSubtitleContent(sentence.FullTarget) || hasSubtitleLineBreak(sentence.FullTarget) {+ issues = append(issues, fmt.Errorf("句子%s缺少有效的完整译文target_text,必须先完整翻译一次,再只插入分段边界", sentence.ID))+ } else if compactSubtitleText(strings.Join(sentence.Target, "")) != compactSubtitleText(sentence.FullTarget) {+ issues = append(issues, fmt.Errorf("句子%s译文分段覆盖错误,存在重复、遗漏、改写或乱序。完整译文=%q;错误的译文拼接=%q。请丢弃该句错误的target分段,只从完整译文按顺序插入分割边界,禁止改动任何文字和标点。若原译语序不适合对应,先统一重写该句target_text,再从这份译文顺序分段,不可分别翻译", sentence.ID, sentence.FullTarget, strings.Join(sentence.Target, "")))+ }+ for j := 0; j < max(len(sentence.Source), len(sentence.Target)); j++ {+ var source, target string+ if j < len(sentence.Source) {+ source = sentence.Source[j]+ }+ if j < len(sentence.Target) {+ target = sentence.Target[j]+ }+ if !hasSubtitleContent(source) || !hasSubtitleContent(target) {+ issues = append(issues, fmt.Errorf("句子%s第%d对缺少实际文字,source=%q target=%q;不能用空白或标点占位,请从完整原文和target_text重新划分该句的全部segments", sentence.ID, j+1, source, target))+ }+ if hasSubtitleLineBreak(source) || hasSubtitleLineBreak(target) {+ issues = append(issues, fmt.Errorf("句子%s第%d对存在换行,必须为单行", sentence.ID, j+1))+ }+ if length := subtitleTargetLength(source); length > input.MaxTargetChars {+ issues = append(issues, fmt.Errorf("句子%s第%d段原文%q计%d显示单位,超过硬上限%d;该段至少拆成%d段,请参考source_segments_hint重新配对原译片段,禁止再次返回此超长片段", sentence.ID, j+1, source, length, input.MaxTargetChars, (length+max(1, input.MaxTargetChars)-1)/max(1, input.MaxTargetChars)))+ }+ if length := subtitleTargetLength(target); length > input.MaxTargetChars {+ issues = append(issues, fmt.Errorf("句子%s第%d段译文%q计%d字符,超过硬上限%d(汉字和全角字符计2,英文、数字、半角标点和空格计1);该段至少拆成%d段,再将对应原文切成相同段数,不删减内容", sentence.ID, j+1, target, length, input.MaxTargetChars, (length+max(1, input.MaxTargetChars)-1)/max(1, input.MaxTargetChars)))+ }+ }+ if !subtitleSegmentsHaveSafeBoundaries(sentence.Source) || !subtitleSegmentsHaveSafeBoundaries(sentence.Target) {+ issues = append(issues, fmt.Errorf("句子%s在字素簇内部切分,必须保留组合字符、连接符、Emoji和文字方向控制字符", sentence.ID))+ }+ if compactSubtitleText(strings.Join(sentence.Source, "")) != compactSubtitleText(original.Text) {+ issues = append(issues, fmt.Errorf("句子%s原文覆盖错误,存在改写、遗漏、重复或乱序。仅译文可以调整语序,segments的source只能在以下正确原文中插入分割边界,禁止移动任何词。正确原文=%q;错误的原文拼接=%q。请丢弃该句错误的source分段,直接从正确原文按顺序重新切成%d段", sentence.ID, original.Text, strings.Join(sentence.Source, " "), len(sentence.Target)))+ }+ // 检查完整句子,允许其中的专名片段保持原样及跨片段调整语序。+ if !t.isTranslationValid(original.Text, sentence.FullTarget, originLang, targetLang) {+ issues = append(issues, fmt.Errorf("句子%s没有有效目标语言译文或原样返回了整句原文", sentence.ID))+ }+ }+ if len(issues) > 0 {+ return nil, errors.Join(issues...)+ }+ // 整句放得下一行时撤销模型多余的切分,不增加翻译请求。+ for i, sentence := range result {+ if len(sentence.Target) <= 1 {+ continue+ }+ if !canJoinPairedTargetSegments(sentence.Target, targetLang) {+ continue+ }+ target := joinPairedTargetSegments(sentence.Target, targetLang)+ if subtitleTargetLength(target) <= input.MaxTargetChars && subtitleTargetLength(input.Sentences[i].Text) <= input.MaxTargetChars {+ result[i].Source = []string{input.Sentences[i].Text}+ result[i].Target = []string{target}+ }+ }+ return result, nil+}++func joinPairedTargetSegments(segments []string, targetLang types.StandardLanguageCode) string {+ compact := subtitleLanguageWithoutWordSpaces(targetLang)+ var text strings.Builder+ var previous byte+ previousSpace := false+ isWordByte := func(c byte) bool {+ return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'+ }+ for _, segment := range segments {+ leadingSpace := len(segment) > 0 && strings.TrimLeftFunc(segment, unicode.IsSpace) != segment+ trailingSpace := len(segment) > 0 && strings.TrimRightFunc(segment, unicode.IsSpace) != segment+ segment = strings.TrimSpace(segment)+ if segment == "" {+ continue+ }+ if text.Len() > 0 && (previousSpace || leadingSpace || !compact || isWordByte(previous) && isWordByte(segment[0])) {+ text.WriteByte(' ')+ }+ text.WriteString(segment)+ previous = segment[len(segment)-1]+ previousSpace = trailingSpace+ }+ return text.String()+}++func subtitleLanguageWithoutWordSpaces(language types.StandardLanguageCode) bool {+ return language == types.LanguageNameSimplifiedChinese || language == types.LanguageNameTraditionalChinese || language == types.LanguageNameJapanese || language == types.LanguageNameThai || language == types.LanguageNameKhmer || language == types.LanguageNameLao || language == "my"+}++func canJoinPairedTargetSegments(segments []string, language types.StandardLanguageCode) bool {+ for i := 1; i < len(segments); i++ {+ previous, next := segments[i-1], segments[i]+ if strings.TrimRightFunc(previous, unicode.IsSpace) != previous || strings.TrimLeftFunc(next, unicode.IsSpace) != next {+ continue+ }+ last, _ := utf8.DecodeLastRuneInString(previous)+ first, _ := utf8.DecodeRuneInString(next)+ latinWord := func(r rune) bool { return unicode.In(r, unicode.Latin) || unicode.IsDigit(r) }+ if !subtitleLanguageWithoutWordSpaces(language) || latinWord(last) && latinWord(first) {+ return false+ }+ }+ return true+}++type subtitleTimingSpan struct {+ from, to int+ start, end int64+}++func subtitleWordTimingSpans(words []VttWord, expected string) ([]subtitleTimingSpan, error) {+ var raw []subtitleTimingSpan+ var wordTexts []string+ position := 0+ for _, word := range words {+ length := utf8.RuneCountInString(compactSubtitleText(word.Text))+ if length == 0 {+ continue+ }+ start, end, err := parseSRTTimeline(strings.ReplaceAll(word.Start+" --> "+word.End, ".", ","))+ if err != nil || start < 0 || start-end > 20 {+ return nil, fmt.Errorf("词级字幕时间无效: %q (%s --> %s)", word.Text, word.Start, word.End)+ }+ raw = append(raw, subtitleTimingSpan{position, position + length, start, end})+ position += length+ wordTexts = append(wordTexts, word.Text)+ }+ if compactSubtitleText(strings.Join(wordTexts, "")) != compactSubtitleText(expected) {+ return nil, fmt.Errorf("词级时间与完整原文不匹配")+ }+ var spans []subtitleTimingSpan+ for i, span := range raw {+ if span.end <= span.start {+ // 平台有时把相邻词放在同一时间点;共享邻词区间并保留全部字符。+ next := i + 1+ for next < len(raw) && raw[next].end <= raw[next].start && raw[next].start == span.start {+ next+++ }+ if next < len(raw) && raw[next].start == span.start && raw[next].end > raw[next].start {+ raw[next].from = min(raw[next].from, span.from)+ continue+ }+ if len(spans) > 0 && spans[len(spans)-1].end == span.start {+ spans[len(spans)-1].to = span.to+ continue+ }+ return nil, fmt.Errorf("词级字幕时间无法与相邻词对齐: %q", wordTexts[i])+ }+ if len(spans) > 0 && spans[len(spans)-1].start == span.start && spans[len(spans)-1].end == span.end {+ spans[len(spans)-1].to = span.to+ } else {+ spans = append(spans, span)+ }+ }+ return spans, nil+}++func subtitlePositionTime(spans []subtitleTimingSpan, position int, ending bool) int64 {+ index := sort.Search(len(spans), func(i int) bool { return position < spans[i].to || ending && position == spans[i].to })+ if index == len(spans) {+ return spans[len(spans)-1].end+ }+ span := spans[index]+ return span.start + (span.end-span.start)*int64(max(0, position-span.from))/int64(span.to-span.from)+}++// BatchTranslateSrtBlocks 按完整句子翻译,以两侧单行长度重新生成成对字幕和共享时间轴。+// 有词级时间时沿用词时间;仅有 cue 时间时在原 cue 内按原文字符位置估算边界。+func (t *Translator) BatchTranslateSrtBlocks(ctx context.Context, blocks []*util.SrtBlock, originLang, targetLang string, taskPtr *types.SubtitleTask, wordTimings ...[]VttWord) ([]*util.SrtBlock, error) {+ var texts []string+ var spans []subtitleTimingSpan+ var pending []string+ position := 0+ for _, block := range blocks {+ if block == nil || !hasSubtitleContent(block.OriginLanguageSentence) {+ return nil, fmt.Errorf("输入字幕缺少原文")+ }+ start, end, err := parseSRTTimeline(block.Timestamp)+ if err != nil || start < 0 || end <= start {+ return nil, fmt.Errorf("第%d条原文字幕时间无效", block.Index)+ }+ text := strings.Join(strings.Fields(block.OriginLanguageSentence), " ")+ length := utf8.RuneCountInString(compactSubtitleText(text))+ spans = append(spans, subtitleTimingSpan{position, position + length, start, end})+ position += length+ pending = append(pending, text)+ if isSentenceEnding(text) {+ texts = append(texts, strings.Join(pending, " "))+ pending = nil+ }+ }+ if len(pending) > 0 {+ texts = append(texts, strings.Join(pending, " "))+ }+ if len(wordTimings) > 0 && len(wordTimings[0]) > 0 {+ var err error+ spans, err = subtitleWordTimingSpans(wordTimings[0], strings.Join(texts, ""))+ if err != nil {+ return nil, err+ }+ }+ batchSize := config.Conf.App.VttBatchSize+ if batchSize <= 0 {+ batchSize = 10+ }+ batchSize = min(3, batchSize)+ paired, err := t.translatePairedSentences(ctx, texts, types.StandardLanguageCode(originLang), types.StandardLanguageCode(targetLang), subtitleTargetLimit(), batchSize, func(_ []pairedSentence, done, total int) error {+ if taskPtr != nil {+ taskPtr.SetProgress(40 + uint8(done*50/total))+ }+ return nil+ })+ if err != nil {+ if len(blocks) > 0 {+ return nil, fmt.Errorf("字幕%d-%d: %w", blocks[0].Index, blocks[len(blocks)-1].Index, err)+ }+ return nil, err+ }+ var output []*util.SrtBlock+ position = 0+ for _, sentence := range paired {+ for i, source := range sentence.Source {+ start := subtitlePositionTime(spans, position, false)+ position += utf8.RuneCountInString(compactSubtitleText(source))+ end := subtitlePositionTime(spans, position, true)+ if end <= start {+ return nil, fmt.Errorf("分段后第%d条字幕时间无效", len(output)+1)+ }+ output = append(output, &util.SrtBlock{+ Index: len(output) + 1,+ Timestamp: formatSRTTimeline(start, end),+ OriginLanguageSentence: strings.TrimSpace(source),+ TargetLanguageSentence: strings.TrimSpace(sentence.Target[i]),+ })+ }+ }+ return output, nil+}diff --git a/runtime/krillinai/internal/service/paired_translation_live_test.go b/runtime/krillinai/internal/service/paired_translation_live_test.gonew file mode 100644index 00000000..5d64a8fd--- /dev/null+++ b/runtime/krillinai/internal/service/paired_translation_live_test.go@@ -0,0 +1,343 @@+package service++import (+ "context"+ "encoding/json"+ "fmt"+ "krillin-ai/config"+ "krillin-ai/internal/types"+ "krillin-ai/log"+ "krillin-ai/pkg/util"+ "os"+ "path/filepath"+ "strconv"+ "strings"+ "testing"+ "unicode/utf8"++ "github.com/BurntSushi/toml"+)++// 只审计已有产物,不调用模型,不修改字幕文件。+func TestGeneratedWordSubtitleAuditLive(t *testing.T) {+ dir := os.Getenv("KRILLINAI_SUBTITLE_AUDIT_DIR")+ vtt := os.Getenv("KRILLINAI_SUBTITLE_AUDIT_VTT")+ if dir == "" || vtt == "" {+ t.Skip("指定新视频的字幕目录和VTT后审计")+ }+ log.InitLogger()+ svc := &YouTubeSubtitleService{}+ words, err := svc.ExtractWordsFromVtt(vtt)+ if err != nil {+ t.Fatal(err)+ }+ var rawSource strings.Builder+ for _, word := range words {+ rawSource.WriteString(word.Text)+ }+ read := func(name string) []*util.SrtBlock {+ t.Helper()+ blocks, err := util.ParseSrtFile(filepath.Join(dir, name))+ if err != nil || len(blocks) == 0 {+ t.Fatalf("无法读取字幕%s: %v", name, err)+ }+ for i, block := range blocks {+ start, end, err := parseSRTTimeline(block.Timestamp)+ if err != nil || start < 0 || end <= start || block.Index != i+1 {+ t.Fatalf("字幕%s第%d条编号或时间无效", name, i+1)+ }+ }+ return blocks+ }+ origin := read(types.SubtitleTaskOriginLanguageSrtFileName)+ target := read(types.SubtitleTaskTargetLanguageSrtFileName)+ bilingual := read(types.SubtitleTaskBilingualSrtFileName)+ short := read(types.SubtitleTaskShortOriginMixedSrtFileName)+ if len(origin) != len(target) || len(origin) != len(bilingual) {+ t.Fatal("普通字幕三类产物条数不一致")+ }+ var source strings.Builder+ limit := 50+ if value := os.Getenv("KRILLINAI_SUBTITLE_AUDIT_LIMIT"); value != "" {+ var err error+ limit, err = strconv.Atoi(value)+ if err != nil || limit < 1 {+ t.Fatal("KRILLINAI_SUBTITLE_AUDIT_LIMIT 必须为正整数")+ }+ }+ maxChars, maxSourceChars, totalChars, maxRunes := 0, 0, 0, 0+ spans, err := subtitleWordTimingSpans(words, rawSource.String())+ if err != nil {+ t.Fatal(err)+ }+ sourcePosition := 0+ var previousEnd int64+ for i, block := range origin {+ source.WriteString(block.OriginLanguageSentence)+ translation := target[i].OriginLanguageSentence+ size := subtitleTargetLength(translation)+ sourceSize := subtitleTargetLength(block.OriginLanguageSentence)+ if size > limit || sourceSize > limit || !hasSubtitleContent(translation) || !hasSubtitleContent(block.OriginLanguageSentence) || hasSubtitleLineBreak(translation) || hasSubtitleLineBreak(block.OriginLanguageSentence) {+ t.Fatalf("普通字幕第%d条原文或译文无效,或超出%d显示单位: source=%d target=%d", i+1, limit, sourceSize, size)+ }+ if block.Timestamp != target[i].Timestamp || block.Timestamp != bilingual[i].Timestamp || bilingual[i].OriginLanguageSentence != translation || bilingual[i].TargetLanguageSentence != block.OriginLanguageSentence {+ t.Fatalf("普通字幕第%d条双语配对或时间不一致", i+1)+ }+ start, end, _ := parseSRTTimeline(block.Timestamp)+ if start < previousEnd {+ t.Fatalf("普通字幕第%d条时间重叠", i+1)+ }+ nextPosition := sourcePosition + utf8.RuneCountInString(compactSubtitleText(block.OriginLanguageSentence))+ if start != subtitlePositionTime(spans, sourcePosition, false) || end != subtitlePositionTime(spans, nextPosition, true) {+ t.Fatalf("普通字幕第%d条时间与原文词位置不一致", i+1)+ }+ sourcePosition = nextPosition+ previousEnd = end+ maxChars = max(maxChars, size)+ maxSourceChars = max(maxSourceChars, sourceSize)+ maxRunes = max(maxRunes, utf8.RuneCountInString(translation))+ totalChars += size+ }+ if compactSubtitleText(source.String()) != compactSubtitleText(rawSource.String()) {+ t.Fatal("全片原文存在遗漏、重复或改写")+ }+ sentences := svc.splitByPrimarySentencePunctuation(words)+ position, unchanged, split := 0, 0, 0+ for _, sentence := range sentences {+ expected := compactSubtitleText(sentence.Text)+ var joined strings.Builder+ segments := 0+ var sourceSegments, targetSegments []string+ for joined.String() != expected {+ if position >= len(origin) {+ t.Fatalf("原句没有对应字幕: %s", sentence.Text)+ }+ joined.WriteString(compactSubtitleText(origin[position].OriginLanguageSentence))+ if !strings.HasPrefix(expected, joined.String()) {+ t.Fatalf("字幕跨句或原文错序: %s", sentence.Text)+ }+ sourceSegments = append(sourceSegments, origin[position].OriginLanguageSentence)+ targetSegments = append(targetSegments, target[position].OriginLanguageSentence)+ position+++ segments+++ }+ if segments == 1 {+ unchanged+++ } else {+ split+++ }+ if !subtitleSegmentsHaveSafeBoundaries(sourceSegments) || !subtitleSegmentsHaveSafeBoundaries(targetSegments) {+ t.Fatalf("原句的字幕分段破坏字素簇: %s", sentence.Text)+ }+ }+ shortPosition, shortSourceCount, shortMaxChars := 0, 0, 0+ var shortPreviousEnd int64+ for i, block := range origin {+ if shortPosition >= len(short) || short[shortPosition].OriginLanguageSentence != target[i].OriginLanguageSentence || short[shortPosition].Timestamp != block.Timestamp {+ t.Fatalf("短字幕第%d组缺少完整译文或改变了译文时间", i+1)+ }+ shortPosition+++ expected := compactSubtitleText(block.OriginLanguageSentence)+ var joined strings.Builder+ blockStart, blockEnd, _ := parseSRTTimeline(block.Timestamp)+ for joined.String() != expected {+ if shortPosition >= len(short) {+ t.Fatalf("短字幕第%d组原文不完整", i+1)+ }+ cue := short[shortPosition]+ if !hasSubtitleContent(cue.OriginLanguageSentence) || hasSubtitleLineBreak(cue.OriginLanguageSentence) {+ t.Fatalf("短字幕第%d条为空或存在语言内部换行", shortPosition+1)+ }+ joined.WriteString(compactSubtitleText(cue.OriginLanguageSentence))+ start, end, _ := parseSRTTimeline(cue.Timestamp)+ if !strings.HasPrefix(expected, joined.String()) || start < shortPreviousEnd || start < blockStart-20 || end > blockEnd+20 {+ t.Fatalf("短字幕第%d组原文错序或词时间无效: %+v", i+1, cue)+ }+ shortPreviousEnd = end+ shortMaxChars = max(shortMaxChars, utf8.RuneCountInString(cue.OriginLanguageSentence))+ shortSourceCount+++ shortPosition+++ }+ }+ if position != len(origin) || shortPosition != len(short) {+ t.Fatal("字幕末尾有额外内容")+ }+ report := map[string]any{+ "检查通过": true, "原始单词数": len(words), "原始句子数": len(sentences),+ "普通字幕条数": len(origin), "保留完整原句数": unchanged, "拆分原句数": split,+ "普通字幕长度规则": "宽字素簇计2,其余基础字素簇计1;原文和译文使用同一上限",+ "普通字幕双侧显示单位上限": limit, "普通原文最大加权字符数": maxSourceChars,+ "普通译文最大加权字符数": maxChars, "普通译文平均加权字符数": float64(totalChars) / float64(len(origin)), "普通译文最大Unicode字符数": maxRunes,+ "短字幕总条数": len(short), "短字幕完整译文条数": len(target), "短字幕原文条数": shortSourceCount,+ "短字幕原文最大字符数": shortMaxChars, "短字幕说明": "按词及字素边界拆分原文并保留标点,复用词级时间;译文保留完整配对片段",+ "全片原文完整覆盖": true, "普通字幕三类配对一致": true, "短字幕两种语言均完整": true, "词级时间映射一致": true,+ }+ data, err := json.MarshalIndent(report, "", " ")+ if err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(dir, "subtitle_audit.json"), data, 0644); err != nil {+ t.Fatal(err)+ }+ t.Log(string(data))+}++func TestTask792FailedBatchRetryLive(t *testing.T) {+ configPath := os.Getenv("KRILLINAI_TRANSLATION_CONFIG")+ output := os.Getenv("KRILLINAI_TRANSLATION_OUTPUT")+ if configPath == "" || output == "" {+ t.Skip("指定真实配置及独立输出目录后复测792失败批次")+ }+ log.InitLogger()+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ if _, err := toml.DecodeFile(configPath, &config.Conf); err != nil {+ t.Fatal(err)+ }+ if err := os.MkdirAll(output, 0755); err != nil {+ t.Fatal(err)+ }+ texts := []string{+ "So, let me give you a model that I teach my movement maker clients.",+ "Make a list of 50 people who could each be worth six or seven figures to your business.",+ "Not customers, connectors, people who already have the audience or the network that you really want to reach.",+ }+ cases := [][]string{texts, {+ "They found out that a lab with billions of dollars of computers was short on cardiologist and then they went and they hired them for that.",+ "The experts get paid 75 to 150 bucks an hour, which is real money for a few hours a week on top of a existing day job.",+ "A radiologist reads scans all day long, then spends three evenings a month telling an LLM where it got a scan wrong.",+ }}+ for caseIndex, texts := range cases {+ for round := range 3 {+ translator := NewTranslator()+ result, err := translator.translatePairedSentences(context.Background(), texts, "en", "zh_cn", subtitleTargetLimit(), 3, nil)+ if err != nil {+ t.Fatalf("第%d轮失败: %v", round+1, err)+ }+ data, err := json.MarshalIndent(result, "", " ")+ if err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(output, fmt.Sprintf("failed-batch-%d-round-%d.json", caseIndex+1, round+1)), data, 0600); err != nil {+ t.Fatal(err)+ }+ t.Logf("第%d轮原译配对、原文覆盖和50加权长度检查通过", round+1)+ }+ }+}++func TestTask792WordPairedLive(t *testing.T) {+ configPath, vttPath, output := os.Getenv("KRILLINAI_TRANSLATION_CONFIG"), os.Getenv("KRILLINAI_TASK792_VTT"), os.Getenv("KRILLINAI_TRANSLATION_OUTPUT")+ if configPath == "" || vttPath == "" || output == "" {+ t.Skip("指定真实配置、任务792的VTT和独立输出目录后运行")+ }+ testWordPairedLive(t, configPath, vttPath, output, []string{"We might have AI", "YouTube just threw", "heartpounding"})+}++func TestFailedTasks20261015WordPairedLive(t *testing.T) {+ configPath, taskRoot, output := os.Getenv("KRILLINAI_TRANSLATION_CONFIG"), os.Getenv("KRILLINAI_FAILED_TASKS_ROOT"), os.Getenv("KRILLINAI_TRANSLATION_OUTPUT")+ if configPath == "" || taskRoot == "" || output == "" {+ t.Skip("指定真实配置、失败任务根目录和独立输出目录后运行")+ }+ for _, sample := range []struct {+ id, directory, vtt string+ phrases []string+ }{+ {"800", "watch_vHo5JdXgp_Gwjh", "Ho5JdXgpnkA.en-orig.vtt", []string{"Those are skills", "here's roughly"}},+ {"803", "watch_v2g3xb7PK_dJZa", "2g3xb7PKcXs.en-orig.vtt", []string{"This is exactly what", "Robert Green addresses", "The envious person knows"}},+ {"792", "watch_vFzet_OPh_Joh2", "Fzet_OPhZMk.en-orig.vtt", []string{"YouTube just threw"}},+ } {+ t.Run(sample.id, func(t *testing.T) {+ testWordPairedLive(t, configPath, filepath.Join(taskRoot, sample.directory, sample.vtt), filepath.Join(output, "task-"+sample.id), sample.phrases)+ })+ }+}++func testWordPairedLive(t *testing.T, configPath, vttPath, output string, phrases []string) {+ t.Helper()+ log.InitLogger()+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ if _, err := toml.DecodeFile(configPath, &config.Conf); err != nil {+ t.Fatal(err)+ }+ svc := &YouTubeSubtitleService{translator: NewTranslator()}+ words, err := svc.ExtractWordsFromVtt(vttPath)+ if err != nil {+ t.Fatal(err)+ }+ sentences := svc.splitByPrimarySentencePunctuation(words)+ calls := 0+ completer := svc.translator.chatCompleter+ svc.translator.chatCompleter = translationCompleterFunc(func(prompt string) (string, error) { calls++; return completer.ChatCompletion(prompt) })+ for _, phrase := range phrases {+ t.Run(phrase, func(t *testing.T) {+ index := -1+ for i, sentence := range sentences {+ if strings.Contains(sentence.Text, phrase) {+ index = i+ break+ }+ }+ if index < 0 {+ t.Fatalf("原始VTT没有找到问题句: %s", phrase)+ }+ selected := sentences[index : index+1]+ if phrase == "heartpounding" {+ selected = sentences[index:min(index+3, len(sentences))]+ }+ dir := filepath.Join(output, fmt.Sprintf("word-%d", index))+ if err := os.MkdirAll(dir, 0755); err != nil {+ t.Fatal(err)+ }+ var selectedWords []VttWord+ for _, sentence := range selected {+ selectedWords = append(selectedWords, sentence.Words...)+ }+ originPath := filepath.Join(dir, "origin_language_srt.srt")+ source, err := svc.generateOriginLanguageSrt(selected, originPath, &YoutubeSubtitleReq{})+ if err != nil {+ t.Fatal(err)+ }+ blocks, err := svc.translator.BatchTranslateSrtBlocks(context.Background(), source, "en", "zh_cn", nil, selectedWords)+ if err != nil {+ t.Fatal(err)+ }+ assertPairedOutput(t, source, blocks, subtitleTargetLimit())+ if err := svc.validateYouTubeSubtitleBlocks(blocks, true); err != nil {+ t.Fatal(err)+ }+ if err := svc.writeOriginLanguageSrtFile(blocks, originPath); err != nil {+ t.Fatal(err)+ }+ if err := svc.writeTargetLanguageSrtFile(blocks, filepath.Join(dir, "target_language_srt.srt")); err != nil {+ t.Fatal(err)+ }+ if err := svc.writeBilingualSrtFile(blocks, filepath.Join(dir, "bilingual_srt.srt"), true); err != nil {+ t.Fatal(err)+ }+ if err := svc.writeShortSubtitleFile(blocks, selected, filepath.Join(dir, "short_origin_mixed.srt"), true); err != nil {+ t.Fatal(err)+ }+ for _, name := range []string{"origin_language_srt.srt", "target_language_srt.srt", "bilingual_srt.srt"} {+ parsed, err := util.ParseSrtFile(filepath.Join(dir, name))+ if err != nil || len(parsed) != len(blocks) {+ t.Fatalf("产物配对数量不一致 %s: %v", name, err)+ }+ for i := range parsed {+ if parsed[i].Timestamp != blocks[i].Timestamp {+ t.Fatalf("产物时间轴不一致: %s", name)+ }+ }+ }+ short, err := util.ParseSrtFile(filepath.Join(dir, "short_origin_mixed.srt"))+ if err != nil || len(short) < len(blocks)*2 {+ t.Fatalf("短字幕未独立导出完整双轨: %v", err)+ }+ for _, block := range blocks {+ t.Logf("%s | %s | %s", block.Timestamp, block.OriginLanguageSentence, block.TargetLanguageSentence)+ }+ })+ }+ t.Logf("真实VTT问题句%d处,模型调用%d次", len(phrases), calls)+}diff --git a/runtime/krillinai/internal/service/paired_translation_prompt.txt b/runtime/krillinai/internal/service/paired_translation_prompt.txtnew file mode 100644index 00000000..f48a9014--- /dev/null+++ b/runtime/krillinai/internal/service/paired_translation_prompt.txt@@ -0,0 +1,17 @@+将输入翻译为指定目标语言,在同一次回答中完成翻译、拆段和原译配对。++硬约束:原文和译文每段都必须单行,且分别不超过 {{MAX_TARGET_CHARS}} 显示单位。max_source_display_units 和 max_target_display_units 分别约束两侧;max_fullwidth_chars 表示纯全角文本每段最多 {{MAX_FULLWIDTH_CHARS}} 个字符,包括标点。汉字、日韩宽字符和全角字符计2,普通字母、数字、标点和空格计1。按 Unicode 字素簇计数:重音组合、印度文字组合、Emoji及其连接序列不可拆开,同一字素簇取其基础字符的最大宽度,组合符和方向控制符不额外计宽。这是字幕长度规则,不能将汉字当成半角字符。++1. 先理解完整语句,将整句忠实自然地翻译一次,写入target_text,再将这份完整译文切分。每条字幕直接写成segments中的一个对象,source是原文片段,target是对应译文片段。按顺序拼接所有target必须恰好还原target_text,仅允许边界空白变化。禁止分别对每个原文片段重新翻译整句,禁止相邻片段重复表达已经翻译过的内容。任意一侧超限,就按自然意群增加配对字幕。两侧整句都在上限内时保持一对;满足两侧上限的前提下尽量减少段数。+2. segments的source:只在正确原文中插入分割边界,顺序、拼写、大小写、标点、组合符、连接符和方向控制符完整保留,仅允许边界空白变化。禁止改写、纠错、移动词语、重复或遗漏。无空格语言也要切分;极长专名、单词、URL没有自然边界时允许在完整字素簇之间切分。+3. target_text与segments的target:完整译文保留全部含义、专名、数字、否定、可能性、条件、时间和因果,不重复表达原文只出现一次的意思。先确定适合原译意群对应的整句译文语序,写入target_text后只能顺序插入边界,不能再改变语序、文字或标点,不得逐段增译、改写、删减、截断或发明缩写。保留目标语言片段边界必要的空格,使拼接能够无损还原完整译文。若需要调整整句语序,必须同步重写target_text及其分段。禁止跨 sentence_id 移动内容。+4. 每个segments对象必须同时含有source和target,两者都必须含实际文字、数字或有意义的符号,禁止空串、空白或纯标点占位。任意一侧继续切分时,另一侧也要重新划分为相同数量,不能用空字符串补齐。+5. 每段禁止实际换行、Unicode 换行控制符、字面量反斜杠加 N 或 n 等会被字幕解释为换行的转义。禁止通过 ASS 标签或 HTML 标签插入换行。保留 Emoji连接序列、重音组合和语言的书写方向。+6. 短小但有实际含义的词也必须保留,包括独立 Yes/No、转折词和 may/might。um、uh 等用于演示卡壳、犹豫或引语时具有实际意义,译文必须体现。上下文仅供理解,不翻译进结果。+7. 输入可能已为模型预算切成相邻片段,结合 context_before/context_after 理解,但只输出当前 text。检查每个 sentence_id 按输入顺序恰好出现一次、每条字幕两侧均有实际内容且不超限、字素边界安全、原文和完整译文均覆盖后返回。+8. source_segments_hint 是本地按词边界生成、每段已满足原文长度上限的参考分段。优先直接采用这些原文片段并生成等量译文;如果译文超限或需要改善语义对应,可继续细分或调整边界,但调整后的两侧仍须满足上限。参考分段不是额外内容,不要重复翻译。不要把多个参考片段合并成超长原文。++配对示例:整句译文“我们努力工作,因为我们在乎。”可以分成[{"source":"We work hard","target":"我们努力工作,"},{"source":"because we care.","target":"因为我们在乎。"}],拼接target仍是“我们努力工作,因为我们在乎。”。不能在target_text中写“因为我们在乎,所以我们努力工作。”后却沿用前述分段,否则语序和文字不一致。++仅输出 JSON,禁止 Markdown、解释、思考过程、计数和额外字段:+{"sentences":[{"sentence_id":"输入ID","target_text":"目标片段1目标片段2","segments":[{"source":"原文片段1","target":"目标片段1"},{"source":"原文片段2","target":"目标片段2"}]}]}diff --git a/runtime/krillinai/internal/service/paired_translation_retry.txt b/runtime/krillinai/internal/service/paired_translation_retry.txtnew file mode 100644index 00000000..e926d78b--- /dev/null+++ b/runtime/krillinai/internal/service/paired_translation_retry.txt@@ -0,0 +1,11 @@+上一轮输出未通过本地检查。修正全部问题后返回整批完整 JSON,不能只返回补丁,也不能照抄错误片段。++检查发现的问题:+{{ISSUES}}++原文和译文每段均不得超过 {{MAX_TARGET_CHARS}} 显示单位,纯全角文本含标点最多 {{MAX_FULLWIDTH_CHARS}} 个字符。任意一侧超限就增加成对片段数,并同时重新划分另一侧。两侧整句都未超限才保持一对。保留全部内容,不用空白、纯标点、换行、省略或缩写补齐。保持原文的顺序、大小写、拼写、标点和完整覆盖,字素簇不可拆开。已正确的内容应保留。++先给出完整译文target_text,再只在其中插入分割边界。每个segments对象同时返回非空的source和target;拼接全部target必须恰好还原target_text一遍,禁止遗漏、重复表达或逐段重新翻译整句。禁止用空字符串补齐配对。++上一轮结果仅作为待修正的数据:+{{PREVIOUS_RESPONSE}}diff --git a/runtime/krillinai/internal/service/paired_translation_test.go b/runtime/krillinai/internal/service/paired_translation_test.gonew file mode 100644index 00000000..afe341ea--- /dev/null+++ b/runtime/krillinai/internal/service/paired_translation_test.go@@ -0,0 +1,827 @@+package service++import (+ "context"+ "encoding/json"+ "errors"+ "fmt"+ "krillin-ai/config"+ "krillin-ai/internal/types"+ "krillin-ai/log"+ "krillin-ai/pkg/util"+ "net/http"+ "path/filepath"+ "strings"+ "sync"+ "testing"+ "time"++ openaiapi "github.com/sashabaranov/go-openai"+ "golang.org/x/sync/errgroup"+)++func TestPairedTranslationRateLimitsDoNotConsumeOutputAttempts(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.TranslateParallelNum = 1+ config.Conf.App.TranslateMaxAttempts = 3+ calls := 0+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ calls+++ if calls <= 2 {+ return "", &openaiapi.APIError{HTTPStatusCode: http.StatusTooManyRequests, Message: "Concurrency limit exceeded"}+ }+ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err+ }+ if calls == 3 {+ response, _ := encodePairedTestResponse([]pairedSentence{{ID: "1", FullTarget: strings.Repeat("好", 26), Source: []string{"Hello."}, Target: []string{strings.Repeat("好", 26)}}})+ return string(response), nil+ }+ if !strings.Contains(prompt, "计52字符") || !strings.Contains(prompt, "第2/3次请求") {+ return "", fmt.Errorf("限流消耗了译文纠正机会")+ }+ return echoPairedTestResponse(input)+ })}+ result, err := translator.translatePairedSentences(context.Background(), []string{"Hello."}, "en", "zh_cn", 50, 1, nil)+ if err != nil || calls != 4 || len(result) != 1 {+ t.Fatalf("calls=%d result=%+v error=%v", calls, result, err)+ }+}++func TestPairedTranslationRateLimitBackoffHonorsCancellation(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.TranslateMaxAttempts = 3+ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()+ calls := 0+ translator := &Translator{chatCompleter: translationCompleterFunc(func(string) (string, error) {+ calls+++ time.AfterFunc(20*time.Millisecond, cancel)+ return "", &openaiapi.APIError{HTTPStatusCode: http.StatusTooManyRequests}+ })}+ started := time.Now()+ _, err := translator.translatePairedSentences(ctx, []string{"Hello."}, "en", "zh_cn", 50, 1, nil)+ if !errors.Is(err, context.Canceled) || calls != 1 || time.Since(started) > 500*time.Millisecond {+ t.Fatalf("取消退避失败: calls=%d elapsed=%s error=%v", calls, time.Since(started), err)+ }+}++func TestPairedTranslationRateLimitRetriesAreBounded(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.TranslateMaxAttempts = 2+ calls := 0+ translator := &Translator{chatCompleter: translationCompleterFunc(func(string) (string, error) {+ calls+++ return "", &openaiapi.APIError{HTTPStatusCode: http.StatusTooManyRequests}+ })}+ _, err := translator.translatePairedSentences(context.Background(), []string{"Hello."}, "en", "zh_cn", 50, 1, nil)+ if err == nil || calls != 2 {+ t.Fatalf("限流未在配置次数后停止: calls=%d error=%v", calls, err)+ }+}++func decodePairedTestInput(prompt string) (pairedTranslationInput, error) {+ var input pairedTranslationInput+ _, payload, found := strings.Cut(prompt, "输入 JSON:\n")+ if !found {+ return input, fmt.Errorf("missing paired input")+ }+ err := json.NewDecoder(strings.NewReader(payload)).Decode(&input)+ return input, err+}++func TestPairedTranslationRetryReportsAllBatchIssues(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.TranslateParallelNum = 1+ calls := 0+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ calls+++ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err+ }+ if calls == 1 {+ response, _ := encodePairedTestResponse([]pairedSentence{+ {ID: "1", FullTarget: "这是我教给客户的一个方法。", Source: []string{"So, let me give you a model, that I teach my movement maker clients."}, Target: []string{"这是我教给客户的一个方法。"}},+ {ID: "2", FullTarget: "他们每个人都可能为你的企业带来六位数或七位数的价值。", Source: []string{input.Sentences[1].Text}, Target: []string{"他们每个人都可能为你的企业带来六位数或七位数的价值。"}},+ {ID: "3", FullTarget: strings.Repeat("好", 26), Source: []string{input.Sentences[2].Text}, Target: []string{strings.Repeat("好", 26)}},+ })+ return string(response), nil+ }+ for _, expected := range []string{"句子1原文覆盖错误", "句子2第1段", "计52字符", "句子3第1段", "该段至少拆成2段"} {+ if !strings.Contains(prompt, expected) {+ return "", fmt.Errorf("重试漏报批次问题: %s", expected)+ }+ }+ return echoPairedTestResponse(input)+ })}+ result, err := translator.translatePairedSentences(context.Background(), []string{+ "So, let me give you a model that I teach my movement maker clients.",+ "Make a list of 50 people who could each be worth six or seven figures to your business.",+ "Not customers, connectors.",+ }, "en", "zh_cn", 50, 3, nil)+ if err != nil || calls != 2 || len(result) != 3 {+ t.Fatalf("calls=%d result=%+v error=%v", calls, result, err)+ }+}++func encodePairedTestResponse(sentences []pairedSentence) ([]byte, error) {+ var output pairedTranslationOutput+ for _, sentence := range sentences {+ item := pairedTranslationOutputSentence{ID: sentence.ID, FullTarget: sentence.FullTarget}+ for i := range max(len(sentence.Source), len(sentence.Target)) {+ var segment pairedTranslationSegment+ if i < len(sentence.Source) {+ segment.Source = sentence.Source[i]+ }+ if i < len(sentence.Target) {+ segment.Target = sentence.Target[i]+ }+ item.Segments = append(item.Segments, segment)+ }+ output.Sentences = append(output.Sentences, item)+ }+ return json.Marshal(output)+}++func echoPairedTestResponse(input pairedTranslationInput) (string, error) {+ var sentences []pairedSentence+ for _, item := range input.Sentences {+ parts, err := splitSubtitleText(item.Text, func(s string) bool { return subtitleTargetLength(s) <= input.MaxTargetChars })+ if err != nil {+ return "", err+ }+ target := make([]string, len(parts))+ for i := range target {+ target[i] = "测试"+ }+ sentences = append(sentences, pairedSentence{ID: item.ID, FullTarget: strings.Join(target, ""), Source: parts, Target: target})+ }+ data, err := encodePairedTestResponse(sentences)+ return string(data), err+}++func TestPairedTranslationParallelBatchesCommitInSourceOrder(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.TranslateParallelNum = 3+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)+ defer cancel()+ var mu sync.Mutex+ active, peak, laterFinished := 0, 0, 0+ allStarted, laterDone := make(chan struct{}), make(chan struct{})+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err+ }+ mu.Lock()+ active+++ peak = max(peak, active)+ if active == 3 && input.Sentences[0].ID <= "3" {+ close(allStarted)+ }+ mu.Unlock()+ defer func() { mu.Lock(); active--; mu.Unlock() }()+ if input.Sentences[0].ID <= "3" {+ select {+ case <-allStarted:+ case <-ctx.Done():+ return "", ctx.Err()+ }+ if input.Sentences[0].ID == "1" {+ select {+ case <-laterDone:+ case <-ctx.Done():+ return "", ctx.Err()+ }+ } else {+ mu.Lock()+ laterFinished+++ if laterFinished == 2 {+ close(laterDone)+ }+ mu.Unlock()+ }+ }+ return echoPairedTestResponse(input)+ })}+ var callbacks []int+ result, err := translator.translatePairedSentences(ctx, []string{"One.", "Two.", "Three.", "Four.", "Five.", "Six.", "Seven."}, "en", "zh_cn", 20, 1, func(batch []pairedSentence, done, total int) error {+ if batch[0].ID != fmt.Sprint(done) || total != 7 {+ return fmt.Errorf("回调顺序错误: %+v, %d/%d", batch, done, total)+ }+ callbacks = append(callbacks, done)+ return nil+ })+ if err != nil || peak != 3 || len(result) != 7 || fmt.Sprint(callbacks) != "[1 2 3 4 5 6 7]" {+ t.Fatalf("peak=%d result=%+v callbacks=%v error=%v", peak, result, callbacks, err)+ }+}++func TestPairedTranslationStopsBeforeCommittingFailedWindow(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.TranslateParallelNum = 2+ var mu sync.Mutex+ calls := make(map[string]int)+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err+ }+ id := input.Sentences[0].ID+ mu.Lock()+ calls[id]+++ mu.Unlock()+ if id == "2" {+ return "not-json", nil+ }+ return echoPairedTestResponse(input)+ })}+ commits := 0+ result, err := translator.translatePairedSentences(context.Background(), []string{"One.", "Two.", "Three."}, "en", "zh_cn", 20, 1, func([]pairedSentence, int, int) error { commits++; return nil })+ if err == nil || result != nil || calls["2"] != 3 || calls["3"] != 0 || commits != 0 {+ t.Fatalf("calls=%v commits=%d error=%v", calls, commits, err)+ }+}++func assertPairedOutput(t *testing.T, source, output []*util.SrtBlock, limit int) {+ t.Helper()+ var before, after strings.Builder+ for _, block := range source {+ before.WriteString(compactSubtitleText(block.OriginLanguageSentence))+ }+ var lastEnd int64 = -1+ for i, block := range output {+ after.WriteString(compactSubtitleText(block.OriginLanguageSentence))+ if block.Index != i+1 || !hasSubtitleContent(block.OriginLanguageSentence) || !hasSubtitleContent(block.TargetLanguageSentence) || subtitleTargetLength(block.TargetLanguageSentence) > limit || subtitleTargetLength(block.OriginLanguageSentence) > limit {+ t.Fatalf("无效配对: %+v", block)+ }+ start, end, err := parseSRTTimeline(block.Timestamp)+ if err != nil || start < lastEnd || end <= start {+ t.Fatalf("无效配对时间: %+v, error=%v", block, err)+ }+ lastEnd = end+ }+ if len(output) == 0 || before.String() != after.String() {+ t.Fatalf("原文覆盖发生变化: %q -> %q", before.String(), after.String())+ }+}++func TestPairedTranslationRetriesOnlyInvalidBatchWithSpecificFeedback(t *testing.T) {+ text := "We used Drake's Tootsie Slide without permission."+ valid := pairedSentence{ID: "1", FullTarget: "我们使用了Drake的Tootsie Slide,未经许可。", Source: []string{"We used Drake's", "Tootsie Slide", "without permission."}, Target: []string{"我们使用了Drake的", "Tootsie Slide,", "未经许可。"}}+ tests := []struct {+ name string+ mutate func(*pairedSentence)+ }{+ {"超长英文专名", func(s *pairedSentence) { s.Target[1] = "《Drake's Tootsie Slide》" }},+ {"超长汉字", func(s *pairedSentence) { s.Target[1] = strings.Repeat("好", 13) }},+ {"配对缺译文", func(s *pairedSentence) { s.Target = s.Target[:1] }},+ {"原文改写", func(s *pairedSentence) { s.Source[0] = "We use Drake's Tootsie" }},+ {"空译文", func(s *pairedSentence) { s.Target[1] = "" }},+ {"纯标点", func(s *pairedSentence) { s.Target[1] = "》。" }},+ {"换行", func(s *pairedSentence) { s.Target[1] = "Tootsie\nSlide" }},+ {"错误ID", func(s *pairedSentence) { s.ID = "2" }},+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ // 每次从 JSON 复制,避免切片共享影响其他样例。+ data, _ := json.Marshal(valid)+ var invalid pairedSentence+ _ = json.Unmarshal(data, &invalid)+ tc.mutate(&invalid)+ invalid.FullTarget = strings.Join(invalid.Target, "")+ var prompts []string+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ prompts = append(prompts, prompt)+ sentence := invalid+ if len(prompts) > 1 {+ sentence = valid+ }+ data, err := encodePairedTestResponse([]pairedSentence{sentence})+ return string(data), err+ })}+ _, err := translator.translatePairedSentences(context.Background(), []string{text}, "en", "zh_cn", 24, 3, nil)+ if err != nil || len(prompts) != 2 {+ t.Fatalf("calls=%d error=%v", len(prompts), err)+ }+ if strings.Contains(prompts[0], "检查发现的问题") || !strings.Contains(prompts[1], "检查发现的问题") || !strings.Contains(prompts[1], text) {+ t.Fatal("失败反馈必须保留原始输入和具体错误")+ }+ if tc.name == "配对缺译文" && (!strings.Contains(prompts[1], `source="Tootsie Slide" target=""`) || !strings.Contains(prompts[1], "重新划分该句的全部segments")) {+ t.Fatal("配对缺失反馈必须包含具体片段和修正要求")+ }+ })+ }+}++func TestPairedTranslationRequiresFullTargetCoverage(t *testing.T) {+ input := pairedTranslationInput{MaxTargetChars: 24, Sentences: []pairedTranslationSource{{ID: "1", Text: "Take any skill and teach it to a class."}}}+ for _, tc := range []struct {+ name string+ full string+ target []string+ issue string+ }{+ {"重复", "选一项技能,给全班讲解。", []string{"选一项技能,", "选一项技能,给全班讲解。"}, "译文分段覆盖错误"},+ {"遗漏", "选一项技能,给全班讲解。", []string{"选一项技能,", "讲解。"}, "译文分段覆盖错误"},+ {"乱序", "选一项技能,给全班讲解。", []string{"给全班讲解。", "选一项技能,"}, "译文分段覆盖错误"},+ {"缺完整译文", "", []string{"选一项技能,", "给全班讲解。"}, "缺少有效的完整译文target_text"},+ {"完整译文换行", "选一项技能,\n给全班讲解。", []string{"选一项技能,", "给全班讲解。"}, "缺少有效的完整译文target_text"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ response, err := encodePairedTestResponse([]pairedSentence{{ID: "1", FullTarget: tc.full, Source: []string{"Take any skill and", "teach it to a class."}, Target: tc.target}})+ if err != nil {+ t.Fatal(err)+ }+ _, err = (&Translator{}).parsePairedTranslation(string(response), input, "en", "zh_cn")+ if err == nil || !strings.Contains(err.Error(), tc.issue) {+ t.Fatalf("未拒绝无效译文覆盖: %v", err)+ }+ if tc.issue == "译文分段覆盖错误" && (!strings.Contains(err.Error(), tc.full) || !strings.Contains(err.Error(), strings.Join(tc.target, ""))) {+ t.Fatalf("译文覆盖反馈必须包含完整译文和实际拼接: %v", err)+ }+ })+ }+}++func TestPairedTranslationFullTargetAndSegmentsUseOneCall(t *testing.T) {+ calls := 0+ translator := &Translator{chatCompleter: translationCompleterFunc(func(string) (string, error) {+ calls+++ return `{"sentences":[{"sentence_id":"1","target_text":"选一项技能,给全班讲解。","segments":[{"source":"Take any skill and","target":"选一项技能,"},{"source":"teach it to a class.","target":"给全班讲解。"}]}]}`, nil+ })}+ result, err := translator.translatePairedSentences(context.Background(), []string{"Take any skill and teach it to a class."}, "en", "zh_cn", 24, 1, nil)+ if err != nil || calls != 1 || len(result) != 1 || len(result[0].Target) != 2 {+ t.Fatalf("完整翻译和分段应由一次调用完成: calls=%d result=%+v error=%v", calls, result, err)+ }+}++func TestPairedTranslationRejectsIncompletePairs(t *testing.T) {+ input := pairedTranslationInput{MaxTargetChars: 50, Sentences: []pairedTranslationSource{{ID: "1", Text: "Hello."}}}+ for _, segments := range []string{+ `[]`,+ `[{"source":"Hello."}]`,+ `[{"target":"你好。"}]`,+ `[{"source":"Hello.","target":"你好。"},{"source":"","target":""}]`,+ } {+ response := `{"sentences":[{"sentence_id":"1","target_text":"你好。","segments":` + segments + `}]}`+ _, err := (&Translator{}).parsePairedTranslation(response, input, "en", "zh_cn")+ if err == nil || !strings.Contains(err.Error(), "缺少") {+ t.Fatalf("未拒绝缺失或空白配对: segments=%s error=%v", segments, err)+ }+ }+}++func TestPairedSourceHintKeepsShortTailWithItsClause(t *testing.T) {+ text := "No jargon, no notes, just explain it as simply as you can."+ parts, err := splitSubtitleSourceHint(text, 50)+ if err != nil || len(parts) != 2 || parts[0] != "No jargon, no notes," || parts[1] != "just explain it as simply as you can." {+ t.Fatalf("短尾句应随意群保留: parts=%q error=%v", parts, err)+ }+ for _, text := range []string{+ strings.Repeat("مرحبا بالعالم، ", 8),+ strings.Repeat("नमस्ते दुनिया ", 8),+ strings.Repeat("👨👩👧👦 café ", 8),+ strings.Repeat("ภาษาไทย", 12),+ strings.Repeat("a", 120),+ } {+ parts, err := splitSubtitleSourceHint(text, 30)+ if err != nil || compactSubtitleText(strings.Join(parts, "")) != compactSubtitleText(text) || !subtitleSegmentsHaveSafeBoundaries(parts) {+ t.Fatalf("参考分段必须完整覆盖且保留字素边界: parts=%q error=%v", parts, err)+ }+ for _, part := range parts {+ if !hasSubtitleContent(part) || subtitleTargetLength(part) > 30 {+ t.Fatalf("参考分段不得为空或超限: %q", part)+ }+ }+ }+}++func TestSubtitleTargetLengthUsesWeightedCharacters(t *testing.T) {+ for _, tc := range []struct {+ text string+ want int+ }{+ {"", 0},+ {"中文", 4},+ {"𠮷", 2},+ {",。!?《》", 12},+ {"AB12", 8},+ {"Hello, AI 2024!", 15},+ {"Tootsie Slide", 13},+ {"《Tootsie Slide》", 17},+ {"中文 AI 2024。", 14},+ {"café", 4},+ } {+ if got := subtitleTargetLength(tc.text); got != tc.want {+ t.Errorf("%q计数=%d,要求%d", tc.text, got, tc.want)+ }+ }+}++func TestPairedTranslationWeightedCountIncludesSpacesAndPunctuation(t *testing.T) {+ for _, text := range []string{"测试译文:a b", "测试译文:𠮷"} {+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ input, _ := decodePairedTestInput(prompt)+ data, _ := encodePairedTestResponse([]pairedSentence{{ID: input.Sentences[0].ID, FullTarget: text, Source: []string{"Hello."}, Target: []string{text}}})+ return string(data), nil+ })}+ _, err := translator.translatePairedSentences(context.Background(), []string{"Hello."}, "en", "zh_cn", subtitleTargetLength(text), 3, nil)+ if err != nil {+ t.Fatal(err)+ }+ }+}++func TestPairedTranslationSourceOrderFeedbackShowsExactOriginal(t *testing.T) {+ text := "Your distribution and your product end up getting built at the very same time by the same one person for free."+ wrong := "Your distribution and your product end up getting built by the same one person at the very same time for free."+ translator := &Translator{}+ input := pairedTranslationInput{MaxTargetChars: 50, Sentences: []pairedTranslationSource{{ID: "588", Text: text}}}+ response, _ := encodePairedTestResponse([]pairedSentence{{ID: "588", FullTarget: "渠道和产品由同一个人免费同时打造。", Source: []string{wrong}, Target: []string{"渠道和产品由同一个人免费同时打造。"}}})+ _, err := translator.parsePairedTranslation(string(response), input, "en", "zh_cn")+ if err == nil || !strings.Contains(err.Error(), text) || !strings.Contains(err.Error(), wrong) || !strings.Contains(err.Error(), "重新切成1段") {+ t.Fatalf("未提供可执行的原文修正信息: %v", err)+ }+}++func TestPairedTranslationMergesUnnecessarySegmentsLocally(t *testing.T) {+ for _, tc := range []struct {+ name string+ targetLang types.StandardLanguageCode+ target []string+ limit int+ want string+ }{+ {"中文整句正好50", "zh_cn", []string{strings.Repeat("好", 12), strings.Repeat("好", 13)}, 50, strings.Repeat("好", 25)},+ {"中文超长保留切分", "zh_cn", []string{strings.Repeat("好", 13), strings.Repeat("好", 13)}, 50, ""},+ {"英文单词保留空格", "en", []string{"Use AI ", "tools today."}, 50, "Use AI tools today."},+ {"中文中的英文词边界", "zh_cn", []string{"使用AI ", "tools提高效率。"}, 50, "使用AI tools提高效率。"},+ {"无法确认词边界保留切分", "en", []string{"cafe", "teria today."}, 50, ""},+ } {+ t.Run(tc.name, func(t *testing.T) {+ calls := 0+ text := "Use AI tools today."+ originLang := types.LanguageNameEnglish+ if tc.targetLang == types.LanguageNameEnglish {+ text = "今天使用AI工具。"+ originLang = types.LanguageNameSimplifiedChinese+ }+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ calls+++ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err+ }+ source := []string{text[:3], text[3:]}+ if originLang == types.LanguageNameSimplifiedChinese {+ source = []string{"今天", "使用AI工具。"}+ }+ data, err := encodePairedTestResponse([]pairedSentence{{ID: input.Sentences[0].ID, FullTarget: strings.Join(tc.target, ""), Source: source, Target: tc.target}})+ return string(data), err+ })}+ result, err := translator.translatePairedSentences(context.Background(), []string{text}, originLang, tc.targetLang, tc.limit, 1, nil)+ if err != nil || calls != 1 || len(result) != 1 {+ t.Fatalf("calls=%d result=%+v error=%v", calls, result, err)+ }+ if tc.want == "" {+ if len(result[0].Source) != 2 || len(result[0].Target) != 2 {+ t.Fatalf("超长字幕被合并: %+v", result[0])+ }+ } else if len(result[0].Source) != 1 || result[0].Source[0] != text || len(result[0].Target) != 1 || result[0].Target[0] != tc.want {+ t.Fatalf("合并结果错误: %+v", result[0])+ }+ })+ }+}++func TestPairedTranslationUsesWordTimingAndVariableCueCount(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.MaxSentenceLength = 16+ config.Conf.App.ShortSubtitleMaxChars = 20+ words := []VttWord{+ {Text: "We", Start: "00:00:00.000", End: "00:00:00.200"},+ {Text: "can", Start: "00:00:00.200", End: "00:00:00.500"},+ {Text: "launch", Start: "00:00:00.600", End: "00:00:01.000"},+ {Text: "after", Start: "00:00:01.500", End: "00:00:01.800"},+ {Text: "review.", Start: "00:00:01.800", End: "00:00:02.500"},+ }+ source := []*util.SrtBlock{{Index: 1, Timestamp: "00:00:00,000 --> 00:00:02,500", OriginLanguageSentence: "We can launch after review."}}+ completer := &scriptedCompleter{responses: []string{`{"sentences":[{"sentence_id":"1","target_text":"审核后,我们就能发布。","segments":[{"source":"We can launch","target":"审核后,"},{"source":"after review.","target":"我们就能发布。"}]}]}`}}+ blocks, err := (&Translator{chatCompleter: completer}).BatchTranslateSrtBlocks(context.Background(), source, "en", "zh_cn", nil, words)+ if err != nil {+ t.Fatal(err)+ }+ assertPairedOutput(t, source, blocks, 16)+ if len(blocks) != 2 || blocks[0].Timestamp != "00:00:00,000 --> 00:00:01,000" || blocks[1].Timestamp != "00:00:01,500 --> 00:00:02,500" {+ t.Fatalf("未使用词时间: %+v", blocks)+ }+ svc := &YouTubeSubtitleService{}+ dir := t.TempDir()+ if err := svc.writeBilingualSrtFile(blocks, filepath.Join(dir, "bilingual.srt"), true); err != nil {+ t.Fatal(err)+ }+ if err := svc.writeShortSubtitleFile(blocks, []Sentence{{Words: words}}, filepath.Join(dir, "short.srt"), true); err != nil {+ t.Fatal(err)+ }+ short, err := util.ParseSrtFile(filepath.Join(dir, "short.srt"))+ if err != nil || len(short) != 4 {+ t.Fatalf("短字幕应保留译文并独立导出原文: %+v, %v", short, err)+ }+ for i, block := range blocks {+ if short[i*2].OriginLanguageSentence != block.TargetLanguageSentence || short[i*2].Timestamp != block.Timestamp || short[i*2+1].OriginLanguageSentence != block.OriginLanguageSentence {+ t.Fatalf("短字幕缺失整条译文或原文: %+v", short)+ }+ }+}++func TestNormalSubtitleLimitIsIndependentOfShortSubtitles(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.MaxSentenceLength = 50+ source := "The stress primarily comes from not taking action over something that you can have some control over."+ target := "压力主要源于没有采取行动,针对自己能够掌控的事情。"+ for _, shortLimit := range []int{8, 20, 60} {+ config.Conf.App.ShortSubtitleMaxChars = shortLimit+ calls := 0+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ calls+++ input, err := decodePairedTestInput(prompt)+ if err != nil || input.MaxTargetChars != 50 || input.MaxSourceChars != 50 || input.MaxFullwidthChars != 25 || !strings.Contains(prompt, "两侧整句都在上限内时保持一对") || !strings.Contains(prompt, "全角字符计2") || strings.Contains(prompt, "SAFE_TARGET_CHARS") {+ t.Fatalf("普通字幕未使用独立50字符约束: %+v, %v", input, err)+ }+ data, err := encodePairedTestResponse([]pairedSentence{{ID: "1", FullTarget: "压力主要源于没有采取行动,针对自己能够掌控的事情。", Source: []string{"The stress primarily comes from", "not taking action over something", "that you can have some control over."}, Target: []string{"压力主要源于", "没有采取行动,", "针对自己能够掌控的事情。"}}})+ return string(data), err+ })}+ blocks, err := translator.BatchTranslateSrtBlocks(context.Background(), []*util.SrtBlock{{Index: 1, Timestamp: "00:00:07,359 --> 00:00:15,430", OriginLanguageSentence: source}}, "en", "zh_cn", nil)+ if err != nil || len(blocks) != 3 || calls != 1 || strings.Join([]string{blocks[0].TargetLanguageSentence, blocks[1].TargetLanguageSentence, blocks[2].TargetLanguageSentence}, "") != target {+ t.Fatalf("短字幕参数%d影响普通字幕: %+v calls=%d error=%v", shortLimit, blocks, calls, err)+ }+ }+}++func TestNormalSubtitleFiftyCharacterBoundary(t *testing.T) {+ translator := &Translator{}+ input := pairedTranslationInput{MaxTargetChars: 50, Sentences: []pairedTranslationSource{{ID: "1", Text: "Hello."}}}+ for _, tc := range []struct {+ name, text string+ invalid bool+ }{+ {"25个汉字", strings.Repeat("好", 25), false},+ {"26个汉字", strings.Repeat("好", 26), true},+ {"24个汉字加全角标点", strings.Repeat("好", 24) + "。", false},+ {"25个汉字加全角标点", strings.Repeat("好", 25) + "。", true},+ {"中英混排50", strings.Repeat("好", 24) + "ab", false},+ {"中英混排51", strings.Repeat("好", 24) + "abc", true},+ {"半角空格计入50", strings.Repeat("好", 24) + " a", false},+ {"半角空格计入51", strings.Repeat("好", 24) + " ab", true},+ } {+ data, _ := encodePairedTestResponse([]pairedSentence{{ID: "1", FullTarget: tc.text, Source: []string{"Hello."}, Target: []string{tc.text}}})+ _, err := translator.parsePairedTranslation(string(data), input, "en", "zh_cn")+ if (err != nil) != tc.invalid {+ t.Errorf("%s边界检查错误: %v", tc.name, err)+ }+ }+}++func TestYouTubeSubtitleValidationUsesWeightedTargetLength(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.MaxSentenceLength = 50+ svc := &YouTubeSubtitleService{}+ for _, size := range []int{25, 26} {+ blocks := []*util.SrtBlock{{Index: 1, Timestamp: "00:00:00,000 --> 00:00:02,000", OriginLanguageSentence: "Hello.", TargetLanguageSentence: strings.Repeat("好", size)}}+ if err := svc.validateYouTubeSubtitleBlocks(blocks, true); (err != nil) != (size > 25) {+ t.Errorf("最终字幕校验与翻译计数不一致,汉字数%d: %v", size, err)+ }+ }+}++func TestAudioShortSubtitlesPreserveTranslationAndSplitSourceByWords(t *testing.T) {+ texts := strings.Fields("The stress primarily comes from not taking action over something that you can have some control over.")+ var words []types.Word+ for i, text := range texts {+ words = append(words, types.Word{Num: i, Text: text, Start: float64(i), End: float64(i + 1)})+ }+ dir := t.TempDir()+ target := "压力主要源于没有对自己能够掌控的事情采取行动。"+ blocks := []*util.SrtBlock{{Index: 1, OriginLanguageSentence: strings.Join(texts, " "), TargetLanguageSentence: target}}+ param := &types.SubtitleTaskStepParam{TaskBasePath: dir, OriginLanguage: "en", MaxWordOneLine: 5, SubtitleResultType: types.SubtitleResultTypeBilingualTranslationOnTop}+ if err := generateSrtWithTimestamps(blocks, 0, words, 0, param); err != nil {+ t.Fatal(err)+ }+ normal, err := util.ParseSrtFile(filepath.Join(dir, fmt.Sprintf(types.SubtitleTaskSplitBilingualSrtFileNamePattern, 0)))+ if err != nil || len(normal) != 1 {+ t.Fatalf("普通字幕被短字幕参数影响: %+v, %v", normal, err)+ }+ short, err := util.ParseSrtFile(filepath.Join(dir, fmt.Sprintf(types.SubtitleTaskSplitShortOriginSrtFileNamePattern, 0)))+ if err != nil || len(short) < 2 {+ t.Fatalf("短原文没有独立拆分: %+v, %v", short, err)+ }+ var fragments []string+ for _, block := range short {+ if len(strings.Fields(block.OriginLanguageSentence)) > 5 {+ t.Fatalf("短原文超过指定词数: %+v", block)+ }+ fragments = append(fragments, block.OriginLanguageSentence)+ }+ if strings.Join(fragments, " ") != strings.Join(texts, " ") {+ t.Fatalf("短字幕丢失原文: fragments=%q want=%q", fragments, strings.Join(texts, " "))+ }+ mixed, err := util.ParseSrtFile(filepath.Join(dir, fmt.Sprintf(types.SubtitleTaskSplitShortOriginMixedSrtFileNamePattern, 0)))+ if err != nil || len(mixed) != len(short)+1 || mixed[0].OriginLanguageSentence != target || mixed[0].Timestamp != normal[0].Timestamp {+ t.Fatalf("混合短字幕未保留完整译文: %+v, %v", mixed, err)+ }+}++func TestShortSubtitleSplitsOnlySourceAtConfiguredLength(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ source := "The stress primarily comes from not taking action over something that you can have some control over."+ target := "压力主要源于没有对自己能够掌控的事情采取行动。"+ block := &util.SrtBlock{Index: 1, Timestamp: "00:00:00,000 --> 00:00:17,000", OriginLanguageSentence: source, TargetLanguageSentence: target}+ var words []VttWord+ for i, text := range strings.Fields(source) {+ words = append(words, VttWord{Text: text, Start: fmt.Sprintf("00:00:%02d.000", i), End: fmt.Sprintf("00:00:%02d.000", i+1)})+ }+ svc := &YouTubeSubtitleService{}+ counts := map[int]int{}+ for _, limit := range []int{20, 40} {+ config.Conf.App.ShortSubtitleMaxChars = limit+ path := filepath.Join(t.TempDir(), "short.srt")+ if err := svc.writeShortSubtitleFile([]*util.SrtBlock{block}, []Sentence{{Words: words}}, path, true); err != nil {+ t.Fatal(err)+ }+ parsed, err := util.ParseSrtFile(path)+ if err != nil || len(parsed) < 3 || parsed[0].OriginLanguageSentence != target || parsed[0].Timestamp != block.Timestamp {+ t.Fatalf("整条译文丢失或时间变化: %+v, %v", parsed, err)+ }+ var fragments []string+ for _, cue := range parsed[1:] {+ fragments = append(fragments, cue.OriginLanguageSentence)+ }+ if strings.Join(fragments, " ") != source {+ t.Fatalf("短字幕原文不完整: %v", fragments)+ }+ counts[limit] = len(parsed)+ }+ if counts[20] <= counts[40] {+ t.Fatalf("短字幕未按独立参数拆分: %v", counts)+ }+}++func TestPairedTranslationKeepsSourceLengthLimitAfterJoiningCues(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.MaxSentenceLength = 16+ config.Conf.App.ShortSubtitleMaxChars = 20+ source := []*util.SrtBlock{+ {Index: 1, Timestamp: "00:00:00,000 --> 00:00:01,000", OriginLanguageSentence: "I started reaching out"},+ {Index: 2, Timestamp: "00:00:01,000 --> 00:00:02,000", OriginLanguageSentence: "to companies."},+ }+ completer := &scriptedCompleter{responses: []string{`{"sentences":[{"sentence_id":"1","target_text":"我开始联系公司。","segments":[{"source":"I started","target":"我开始"},{"source":"reaching out","target":"联系"},{"source":"to companies.","target":"公司。"}]}]}`}}+ blocks, err := (&Translator{chatCompleter: completer}).BatchTranslateSrtBlocks(context.Background(), source, "en", "zh_cn", nil)+ if err != nil || len(blocks) != 3 || completer.calls != 1 {+ t.Fatalf("output=%+v calls=%d error=%v", blocks, completer.calls, err)+ }+ assertPairedOutput(t, source, blocks, 16)+}++func TestSubtitleWordTimingPreservesZeroDurationWords(t *testing.T) {+ for _, tc := range []struct {+ name, text string+ words []VttWord+ want []subtitleTimingSpan+ }{+ {"金额符号", "$ 20", []VttWord{{Text: "$", Start: "00:00:01.000", End: "00:00:01.000"}, {Text: "20", Start: "00:00:01.000", End: "00:00:01.300"}}, []subtitleTimingSpan{{0, 3, 1000, 1300}}},+ {"有含义的词", "was in", []VttWord{{Text: "was", Start: "00:00:01.000", End: "00:00:01.000"}, {Text: "in", Start: "00:00:01.000", End: "00:00:01.300"}}, []subtitleTimingSpan{{0, 5, 1000, 1300}}},+ {"轻微倒退", "a hundred", []VttWord{{Text: "a", Start: "00:00:01.000", End: "00:00:00.990"}, {Text: "hundred", Start: "00:00:01.000", End: "00:00:01.300"}}, []subtitleTimingSpan{{0, 8, 1000, 1300}}},+ {"连续零时长", "at the end", []VttWord{{Text: "at", Start: "00:00:01.000", End: "00:00:01.000"}, {Text: "the", Start: "00:00:01.000", End: "00:00:01.000"}, {Text: "end", Start: "00:00:01.000", End: "00:00:01.300"}}, []subtitleTimingSpan{{0, 8, 1000, 1300}}},+ {"末尾零时长", "the end", []VttWord{{Text: "the", Start: "00:00:01.000", End: "00:00:01.300"}, {Text: "end", Start: "00:00:01.300", End: "00:00:01.300"}}, []subtitleTimingSpan{{0, 6, 1000, 1300}}},+ } {+ t.Run(tc.name, func(t *testing.T) {+ spans, err := subtitleWordTimingSpans(tc.words, tc.text)+ if err != nil || fmt.Sprint(spans) != fmt.Sprint(tc.want) {+ t.Fatalf("spans=%+v want=%+v error=%v", spans, tc.want, err)+ }+ })+ }+}++func TestSubtitleWordTimingRejectsUnrecoverableInput(t *testing.T) {+ for _, words := range [][]VttWord{+ {{Text: "lost", Start: "bad", End: "00:00:01.000"}},+ {{Text: "lost", Start: "00:00:02.000", End: "00:00:01.000"}},+ {{Text: "lost", Start: "00:00:01.000", End: "00:00:01.000"}},+ {{Text: "lost", Start: "00:00:01.000", End: "00:00:01.000"}, {Text: "next", Start: "00:00:04.000", End: "00:00:05.000"}},+ } {+ var text []string+ for _, word := range words {+ text = append(text, word.Text)+ }+ if _, err := subtitleWordTimingSpans(words, strings.Join(text, " ")); err == nil {+ t.Fatalf("未拒绝无法恢复的时间: %+v", words)+ }+ }+}++func TestAudioTranslationWorkerCapsRetriesWithoutExtraSplitCall(t *testing.T) {+ log.InitLogger()+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.TranslateMaxAttempts = 9+ for _, failure := range []bool{false, true} {+ t.Run(fmt.Sprint(failure), func(t *testing.T) {+ calls := 0+ svc := Service{ChatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ calls+++ if failure {+ return "not-json", nil+ }+ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err+ }+ return echoPairedTestResponse(input)+ })}+ group, ctx := errgroup.WithContext(context.Background())+ pending := make(chan DataWithId[string], 1)+ pending <- DataWithId[string]{Data: strings.Repeat("a long original phrase ", 20) + "ends.", Id: 0}+ close(pending)+ progress := make(chan DataWithId[TranslationProgress], 10)+ output := make(chan DataWithId[[]*TranslatedItem], 1)+ svc.startTranslateWorker(ctx, group, &types.SubtitleTaskStepParam{TaskBasePath: t.TempDir(), OriginLanguage: "en", TargetLanguage: "zh_cn"}, pending, progress, output)+ err := group.Wait()+ want := 1+ if failure {+ want = 3+ }+ if calls != want || (err != nil) != failure {+ t.Fatalf("calls=%d want=%d error=%v", calls, want, err)+ }+ })+ }+}++func TestWordLevelSubtitleRouteExportsPairedNormalAndIndependentShortSubtitles(t *testing.T) {+ log.InitLogger()+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.MaxSentenceLength = 16+ config.Conf.App.ShortSubtitleMaxChars = 8+ fixture := "WEBVTT\n\n00:00:00.000 --> 00:00:02.500\nWe<00:00:00.200> can<00:00:00.500> launch<00:00:01.000> after<00:00:01.500> review.\n"+ req := subtitleRoutingTestRequest(t, fixture)+ completer := &scriptedCompleter{responses: []string{`{"sentences":[{"sentence_id":"1","target_text":"审核后,我们就能发布。","segments":[{"source":"We can launch","target":"审核后,"},{"source":"after review.","target":"我们就能发布。"}]}]}`}}+ svc := &YouTubeSubtitleService{translator: &Translator{chatCompleter: completer}}+ if _, err := svc.processYouTubeSubtitle(context.Background(), req); err != nil {+ t.Fatal(err)+ }+ if completer.calls != 1 {+ t.Fatalf("翻译和分段只应调用一次模型,实际%d", completer.calls)+ }+ var timestamps []string+ for _, name := range []string{types.SubtitleTaskOriginLanguageSrtFileName, types.SubtitleTaskTargetLanguageSrtFileName, types.SubtitleTaskBilingualSrtFileName} {+ blocks, err := util.ParseSrtFile(filepath.Join(req.TaskBasePath, name))+ if err != nil || len(blocks) != 2 {+ t.Fatalf("字幕产物%s: %v, %+v", name, err, blocks)+ }+ if timestamps == nil {+ for _, block := range blocks {+ timestamps = append(timestamps, block.Timestamp)+ }+ }+ for i, block := range blocks {+ if block.Timestamp != timestamps[i] {+ t.Fatalf("字幕产物时间轴不一致: %s", name)+ }+ }+ }+ short, err := util.ParseSrtFile(filepath.Join(req.TaskBasePath, types.SubtitleTaskShortOriginMixedSrtFileName))+ if err != nil || len(short) <= 2 || short[0].OriginLanguageSentence != "审核后," {+ t.Fatalf("短字幕未独立导出: %+v, %v", short, err)+ }+}++func TestAudioModalFilterPreservesPairedSource(t *testing.T) {+ calls := 0+ svc := Service{ChatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ calls+++ if !strings.Contains(prompt, "启用语气词过滤") || !strings.Contains(prompt, "原文仍须原样完整保留") {+ t.Fatal("语气词过滤只能作用于译文")+ }+ return `{"sentences":[{"sentence_id":"1","target_text":"不,我可能会来。","segments":[{"source":"Um, no, I might come.","target":"不,我可能会来。"}]}]}`, nil+ })}+ items, err := svc.splitTextAndTranslateV2(context.Background(), "", "Um, no, I might come.", "en", "zh_cn", true, 0, nil)+ if err != nil || calls != 1 || len(items) != 1 || items[0].OriginText != "Um, no, I might come." {+ t.Fatalf("items=%+v calls=%d error=%v", items, calls, err)+ }+}diff --git a/runtime/krillinai/internal/service/stage_exports.go b/runtime/krillinai/internal/service/stage_exports.goindex 234719da..aace9a66 100644--- a/runtime/krillinai/internal/service/stage_exports.go+++ b/runtime/krillinai/internal/service/stage_exports.go@@ -3,7 +3,6 @@ package serviceimport ("context""errors"- "fmt""krillin-ai/config""krillin-ai/internal/deps""krillin-ai/internal/types"@@ -14,35 +13,14 @@ import (var ErrYouTubeSubtitleServiceNotInitialized = errors.New("youtube subtitle service not initialized")var ErrImageClientNotInitialized = errors.New("image client not initialized")-func (s Service) TranslateSubtitleBlocks(ctx context.Context, blocks []*util.SrtBlock, p *types.SubtitleTaskStepParam) error {+func (s Service) TranslateSubtitleBlocks(ctx context.Context, blocks []*util.SrtBlock, p *types.SubtitleTaskStepParam) ([]*util.SrtBlock, error) {if err := ctx.Err(); err != nil {- return err+ return nil, err}if s.YouTubeSubtitleSrv == nil || s.YouTubeSubtitleSrv.translator == nil {- return ErrYouTubeSubtitleServiceNotInitialized+ return nil, ErrYouTubeSubtitleServiceNotInitialized}- return runTranslationBatches(ctx, (len(blocks)+9)/10, func(ctx context.Context, batchIndex int) error {- start := batchIndex * 10- batch := blocks[start:min(start+10, len(blocks))]- texts := make([]string, len(batch))- for index, block := range batch {- texts[index] = block.OriginLanguageSentence- }- translations, err := s.YouTubeSubtitleSrv.translator.batchTranslateTextsContext(ctx, texts, p.OriginLanguage, p.TargetLanguage)- if err != nil {- return err- }- if len(translations) != len(batch) {- return fmt.Errorf("subtitle translation count mismatch")- }- for index, text := range translations {- if text == "" {- return fmt.Errorf("subtitle translation missing for cue %d", batch[index].Index)- }- batch[index].TargetLanguageSentence = text- }- return nil- })+ return s.YouTubeSubtitleSrv.translator.BatchTranslateSrtBlocks(ctx, blocks, string(p.OriginLanguage), string(p.TargetLanguage), p.TaskPtr)}type DownloadMediaResult struct {diff --git a/runtime/krillinai/internal/service/stage_exports_test.go b/runtime/krillinai/internal/service/stage_exports_test.goindex 45f0acbc..8f26d45b 100644--- a/runtime/krillinai/internal/service/stage_exports_test.go+++ b/runtime/krillinai/internal/service/stage_exports_test.go@@ -25,8 +25,8 @@ func TestImportedSubtitleTranslationDoesNotFallBackToOriginalText(t *testing.T)log.InitLogger()completer := &scriptedCompleter{responses: []string{"not-json", "not-json", "not-json"}}svc := Service{YouTubeSubtitleSrv: &YouTubeSubtitleService{translator: &Translator{chatCompleter: completer}}}- blocks := []*util.SrtBlock{{Index: 1, OriginLanguageSentence: "source"}}- err := svc.TranslateSubtitleBlocks(context.Background(), blocks, &types.SubtitleTaskStepParam{OriginLanguage: "en", TargetLanguage: "zh_cn"})+ blocks := []*util.SrtBlock{{Index: 1, Timestamp: "00:00:00,000 --> 00:00:01,000", OriginLanguageSentence: "source"}}+ _, err := svc.TranslateSubtitleBlocks(context.Background(), blocks, &types.SubtitleTaskStepParam{OriginLanguage: "en", TargetLanguage: "zh_cn"})if err == nil || blocks[0].TargetLanguageSentence != "" {t.Fatalf("failed translation returned a fake result: %v, %+v", err, blocks[0])}diff --git a/runtime/krillinai/internal/service/subtitle_single_line_test.go b/runtime/krillinai/internal/service/subtitle_single_line_test.gonew file mode 100644index 00000000..67d8a5b0--- /dev/null+++ b/runtime/krillinai/internal/service/subtitle_single_line_test.go@@ -0,0 +1,177 @@+package service++import (+ "context"+ "krillin-ai/config"+ "krillin-ai/internal/types"+ "krillin-ai/pkg/util"+ "path/filepath"+ "strings"+ "testing"+)++func TestSubtitleUnicodeBoundariesAndDisplayUnits(t *testing.T) {+ for _, text := range []string{"中文。日本語。한국어.", "ภาษาไทยไม่มีช่องว่าง", "العَرَبِيَّة فارسی\u200cمتن", "हिन्दी क्षि नमस्ते", "cafe\u0301 déjà vu", "👩🏽💻👨👩👧👦🇨🇳 test", "English 中文 العربية हिन्दी"} {+ t.Run(text, func(t *testing.T) {+ parts, err := splitSubtitleText(text, func(s string) bool { return subtitleTargetLength(s) <= 8 })+ if err != nil {+ t.Fatal(err)+ }+ if compactSubtitleText(strings.Join(parts, "")) != compactSubtitleText(text) || !subtitleSegmentsHaveSafeBoundaries(parts) {+ t.Fatalf("字素或内容被破坏: %q", parts)+ }+ for _, part := range parts {+ if subtitleTargetLength(part) > 8 || hasSubtitleLineBreak(part) {+ t.Fatalf("片段超限: %q", part)+ }+ }+ })+ }+ for _, text := range []string{"e\u0301", "क्षि", "👩🏽💻", "👨👩👧👦"} {+ if subtitleTargetLength(text) > 2 {+ t.Errorf("组合序列重复计宽: %q", text)+ }+ if _, err := splitSubtitleText(text, func(s string) bool { return len(s) < len(text) }); err == nil {+ t.Errorf("不应拆开单个字素簇: %q", text)+ }+ }+}++func TestPairedTranslationProtectsUnpunctuatedInputAndBatchBudget(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.TranslateParallelNum = 1+ original := strings.Repeat("hello ", 20000)+ calls, doneCount := 0, 0+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ calls+++ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err+ }+ if len(prompt) > translationRequestBytes || input.MaxSourceChars != 50 {+ t.Fatalf("请求超限: %d", len(prompt))+ }+ for _, sentence := range input.Sentences {+ if len(sentence.Text) > translationSentenceBytes {+ t.Fatalf("输入未保护: %d", len(sentence.Text))+ }+ if subtitleTargetLength(sentence.Text) > input.MaxSourceChars {+ if len(sentence.SourceHint) < 2 || compactSubtitleText(strings.Join(sentence.SourceHint, "")) != compactSubtitleText(sentence.Text) || !subtitleSegmentsHaveSafeBoundaries(sentence.SourceHint) {+ t.Fatalf("原文分段参考不完整或破坏字素: %q", sentence.SourceHint)+ }+ for _, part := range sentence.SourceHint {+ if subtitleTargetLength(part) > input.MaxSourceChars {+ t.Fatalf("原文分段参考超限: %q", part)+ }+ }+ }+ }+ return echoPairedTestResponse(input)+ })}+ paired, err := translator.translatePairedSentences(context.Background(), []string{original}, "en", "zh_cn", 50, 12, func(_ []pairedSentence, done, total int) error {+ if done <= doneCount || done > total {+ t.Fatalf("进度异常: %d/%d", done, total)+ }+ doneCount = done+ return nil+ })+ if err != nil {+ t.Fatal(err)+ }+ var restored strings.Builder+ for _, sentence := range paired {+ for _, part := range sentence.Source {+ if subtitleTargetLength(part) > 50 {+ t.Fatal("原文超限")+ }+ restored.WriteString(part)+ }+ }+ if calls < 2 || doneCount != len(paired) || compactSubtitleText(restored.String()) != compactSubtitleText(original) {+ t.Fatalf("输入遗漏或进度错误: calls=%d done=%d count=%d", calls, doneCount, len(paired))+ }+}++func TestShortSubtitleKeepsTrailingPunctuationWithText(t *testing.T) {+ text := "And you can do this with almost anything."+ blocks := []*util.SrtBlock{{Index: 1, Timestamp: "00:00:00,000 --> 00:00:04,000", OriginLanguageSentence: text}}+ spans, _, err := subtitleBlockTimingSpans(blocks)+ if err != nil {+ t.Fatal(err)+ }+ short, err := splitShortSubtitleBlocks(blocks, spans, 20, 0)+ if err != nil {+ t.Fatal(err)+ }+ var parts []string+ for _, block := range short[1] {+ if !hasSubtitleContent(block.OriginLanguageSentence) || subtitleTargetLength(block.OriginLanguageSentence) > 20 {+ t.Fatalf("短字幕不能独立显示标点或超限: %+v", block)+ }+ parts = append(parts, block.OriginLanguageSentence)+ }+ if compactSubtitleText(strings.Join(parts, "")) != compactSubtitleText(text) {+ t.Fatalf("短字幕内容丢失: %q", parts)+ }+}++func TestPairedTranslationRejectsSourceOverflowAndUnsafeBreaks(t *testing.T) {+ translator := &Translator{}+ for _, tc := range []struct {+ source string+ parts, target []string+ }{+ {"This original is longer than twenty.", []string{"This original is longer than twenty."}, []string{"译文短。"}},+ {"cafe\u0301", []string{"cafe", "\u0301"}, []string{"测试", "文字"}},+ {"Hello.", []string{"Hello."}, []string{`测试\N换行`}},+ {"Hello.", []string{"Hello."}, []string{"测试\u0085换行"}},+ } {+ input := pairedTranslationInput{MaxTargetChars: 20, Sentences: []pairedTranslationSource{{ID: "1", Text: tc.source}}}+ data, _ := encodePairedTestResponse([]pairedSentence{{ID: "1", FullTarget: strings.Join(tc.target, ""), Source: tc.parts, Target: tc.target}})+ if _, err := translator.parsePairedTranslation(string(data), input, "en", "zh_cn"); err == nil {+ t.Fatalf("未拒绝无效输出: %s", data)+ }+ }+ if got := splitTranslationSentences("第一句。第二句!第三句?"); len(got) != 3 {+ t.Fatalf("无空格语句未拆分: %q", got)+ }+ if got := splitTranslationSentences("Dr. Smith met Mr. Jones. Then they left."); len(got) != 2 {+ t.Fatalf("缩写被错误拆句: %q", got)+ }+}++func TestShortSubtitleSharesWordInteriorTiming(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.ShortSubtitleMaxChars = 8+ words := []VttWord{{Text: "We", Start: "00:00:00.000", End: "00:00:00.200"}, {Text: "can", Start: "00:00:00.200", End: "00:00:00.400"}, {Text: "launch", Start: "00:00:00.400", End: "00:00:01.000"}, {Text: "after", Start: "00:00:01.000", End: "00:00:01.500"}, {Text: "review.", Start: "00:00:01.500", End: "00:00:02.000"}}+ blocks := []*util.SrtBlock{{Index: 1, Timestamp: "00:00:00,000 --> 00:00:00,700", OriginLanguageSentence: "We can lau", TargetLanguageSentence: "审核后,"}, {Index: 2, Timestamp: "00:00:00,700 --> 00:00:02,000", OriginLanguageSentence: "nch after review.", TargetLanguageSentence: "我们能发布。"}}+ dir := t.TempDir()+ file := filepath.Join(dir, "short.srt")+ if err := (&YouTubeSubtitleService{}).writeShortSubtitleFile(blocks, []Sentence{{Words: words}}, file, true); err != nil {+ t.Fatal(err)+ }+ parsed, err := util.ParseSrtFile(file)+ if err != nil {+ t.Fatal(err)+ }+ var source []string+ for _, block := range parsed {+ if block.OriginLanguageSentence != "审核后," && block.OriginLanguageSentence != "我们能发布。" {+ source = append(source, block.OriginLanguageSentence)+ }+ }+ if compactSubtitleText(strings.Join(source, "")) != "Wecanlaunchafterreview." {+ t.Fatalf("词内原文丢失: %q", source)+ }+ var audioWords []types.Word+ for _, word := range words {+ start, end, _ := parseSRTTimeline(strings.ReplaceAll(word.Start+" --> "+word.End, ".", ","))+ audioWords = append(audioWords, types.Word{Text: word.Text, Start: float64(start) / 1000, End: float64(end) / 1000})+ }+ audio, _, err := pairedAudioTiming(blocks, audioWords, "en", 0)+ if err != nil || audio[0].Timestamp != blocks[0].Timestamp || audio[1].Timestamp != blocks[1].Timestamp {+ t.Fatalf("音频和词字幕映射不一致: %+v %v", audio, err)+ }+}diff --git a/runtime/krillinai/internal/service/subtitle_text.go b/runtime/krillinai/internal/service/subtitle_text.gonew file mode 100644index 00000000..9f6a991e--- /dev/null+++ b/runtime/krillinai/internal/service/subtitle_text.go@@ -0,0 +1,180 @@+package service++import (+ "fmt"+ "strings"+ "unicode"+ "unicode/utf8"++ "github.com/clipperhouse/uax29/v2/graphemes"+ "github.com/clipperhouse/uax29/v2/words"+ "golang.org/x/text/width"+)++var subtitleAbbreviations = map[string]bool{+ "dr.": true, "mr.": true, "mrs.": true, "ms.": true, "prof.": true,+ "vs.": true, "etc.": true, "inc.": true, "ltd.": true, "corp.": true,+ "co.": true, "jr.": true, "sr.": true, "st.": true, "ave.": true,+ "blvd.": true, "rd.": true, "apt.": true, "no.": true, "vol.": true,+ "ch.": true, "sec.": true, "fig.": true, "pg.": true, "pp.": true,+ "i.e.": true, "e.g.": true, "cf.": true, "et.": true, "al.": true,+}++// 字素簇用于显示和切分;时间定位仍使用去空白后的 rune 坐标。+func subtitleTargetLength(text string) int {+ length := 0+ iter := graphemes.FromString(text)+ for iter.Next() {+ clusterWidth := 0+ for _, r := range iter.Value() {+ if unicode.IsMark(r) || unicode.Is(unicode.Cf, r) || r == 0xfe0f {+ continue+ }+ units := 1+ switch width.LookupRune(r).Kind() {+ case width.EastAsianWide, width.EastAsianFullwidth:+ units = 2+ }+ clusterWidth = max(clusterWidth, units)+ }+ length += clusterWidth+ }+ return length+}++func hasSubtitleLineBreak(text string) bool {+ return strings.ContainsAny(text, "\r\n\v\f\u0085\u2028\u2029") ||+ strings.Contains(text, `\N`) || strings.Contains(text, `\n`)+}++// 优先在 Unicode 词边界切分,无自然边界的长词才回退到完整字素簇。+func splitSubtitleText(text string, fits func(string) bool) ([]string, error) {+ wordEnds := make(map[int]bool)+ wordIter := words.FromString(text)+ for wordIter.Next() {+ wordEnds[wordIter.End()] = true+ }+ ends := []int{0}+ iter := graphemes.FromString(text)+ for iter.Next() {+ ends = append(ends, iter.End())+ }+ var parts []string+ for index := 0; index+1 < len(ends); {+ start, cut, preferred := ends[index], index, index+ for j := index + 1; j < len(ends); j++ {+ if !fits(strings.TrimSpace(text[start:ends[j]])) {+ break+ }+ cut = j+ if wordEnds[ends[j]] {+ preferred = j+ }+ }+ if cut == index {+ return nil, fmt.Errorf("单个字素簇超过字幕或输入上限: %q", text[start:ends[index+1]])+ }+ if cut+1 < len(ends) && preferred > index && ends[preferred]-start >= (ends[cut]-start)/2 {+ cut = preferred+ }+ if tail := strings.TrimSpace(text[ends[cut]:]); tail != "" && !hasSubtitleContent(tail) {+ adjusted := 0+ for _, preferWords := range []bool{true, false} {+ for j := cut - 1; j > index; j-- {+ if preferWords && !wordEnds[ends[j]] {+ continue+ }+ if hasSubtitleContent(text[start:ends[j]]) && hasSubtitleContent(text[ends[j]:]) && fits(strings.TrimSpace(text[ends[j]:])) {+ adjusted = j+ break+ }+ }+ if adjusted > 0 {+ break+ }+ }+ if adjusted == 0 {+ return nil, fmt.Errorf("当前上限无法将末尾标点与实际文字保留在同一字幕片段")+ }+ cut = adjusted+ }+ if part := strings.TrimSpace(text[start:ends[cut]]); part != "" {+ parts = append(parts, part)+ }+ index = cut+ }+ return parts, nil+}++func splitSubtitleSourceHint(text string, limit int) ([]string, error) {+ parts, err := splitSubtitleText(text, func(s string) bool { return subtitleTargetLength(s) <= limit })+ if err != nil || len(parts) < 2 {+ return parts, err+ }+ // 调整末尾两段,避免贪心切分把助词或短尾句单独留给翻译。+ tail := strings.TrimSpace(text)+ for _, part := range parts[:len(parts)-2] {+ tail = strings.TrimSpace(tail[len(part):])+ }+ bestCut, bestScore := 0, int(^uint(0)>>1)+ iter := words.FromString(tail)+ for iter.Next() {+ cut := iter.End()+ left, right := strings.TrimSpace(tail[:cut]), strings.TrimSpace(tail[cut:])+ leftSize, rightSize := subtitleTargetLength(left), subtitleTargetLength(right)+ if leftSize > limit {+ break+ }+ if rightSize > limit || !hasSubtitleContent(left) || !hasSubtitleContent(right) {+ continue+ }+ score := max(leftSize, rightSize) - min(leftSize, rightSize)+ last, _ := utf8.DecodeLastRuneInString(left)+ if unicode.IsPunct(last) && min(leftSize, rightSize)*4 >= leftSize+rightSize {+ score -= limit / 2+ }+ if score < bestScore && subtitleSegmentsHaveSafeBoundaries([]string{left, right}) {+ bestCut, bestScore = cut, score+ }+ }+ if bestCut > 0 {+ parts[len(parts)-2] = strings.TrimSpace(tail[:bestCut])+ parts[len(parts)-1] = strings.TrimSpace(tail[bestCut:])+ }+ return parts, nil+}++func subtitleTextPreview(text string, maxBytes int) string {+ if len(text) <= maxBytes {+ return text+ }+ end := 0+ iter := graphemes.FromString(text)+ for iter.Next() {+ if iter.End() > maxBytes {+ break+ }+ end = iter.End()+ }+ return text[:end] + " [内容过长,已截短反馈]"+}++func subtitleSegmentsHaveSafeBoundaries(segments []string) bool {+ text := compactSubtitleText(strings.Join(segments, ""))+ boundaries := map[int]bool{0: true}+ iter := graphemes.FromString(text)+ for iter.Next() {+ boundaries[iter.End()] = true+ }+ position := 0+ for _, segment := range segments {+ if !utf8.ValidString(segment) {+ return false+ }+ position += len(compactSubtitleText(segment))+ if !boundaries[position] {+ return false+ }+ }+ return true+}diff --git a/runtime/krillinai/internal/service/subtitle_timing.go b/runtime/krillinai/internal/service/subtitle_timing.gonew file mode 100644index 00000000..353962a1--- /dev/null+++ b/runtime/krillinai/internal/service/subtitle_timing.go@@ -0,0 +1,90 @@+package service++import (+ "fmt"+ "krillin-ai/internal/types"+ "krillin-ai/pkg/util"+ "strings"+ "unicode/utf8"+)++func pairedAudioTiming(blocks []*util.SrtBlock, words []types.Word, language types.StandardLanguageCode, offset float64) ([]*util.SrtBlock, []subtitleTimingSpan, error) {+ var vttWords []VttWord+ var expected strings.Builder+ for _, block := range blocks {+ expected.WriteString(block.OriginLanguageSentence)+ }+ for _, word := range words {+ vttWords = append(vttWords, VttWord{Text: word.Text, Start: strings.ReplaceAll(util.FormatTime(float32(word.Start+offset)), ",", "."), End: strings.ReplaceAll(util.FormatTime(float32(word.End+offset)), ",", ".")})+ }+ spans, err := subtitleWordTimingSpans(vttWords, expected.String())+ if err != nil {+ // 部分转录器的全文与词列表标点不同,沿用既有匹配器生成 cue 时间。+ generated, err := NewTimestampGenerator().GenerateTimestamps(blocks, words, language, offset)+ if err != nil {+ return nil, nil, err+ }+ if err := normalizeSrtBlocks(generated, false); err != nil {+ return nil, nil, err+ }+ spans, _, err = subtitleBlockTimingSpans(generated)+ return generated, spans, err+ }+ position := 0+ generated := make([]*util.SrtBlock, 0, len(blocks))+ for _, block := range blocks {+ copy := *block+ start := subtitlePositionTime(spans, position, false)+ position += utf8.RuneCountInString(compactSubtitleText(block.OriginLanguageSentence))+ end := subtitlePositionTime(spans, position, true)+ if end <= start {+ return nil, nil, fmt.Errorf("字幕%d分段后时间不足", block.Index)+ }+ copy.Timestamp = formatSRTTimeline(start, end)+ generated = append(generated, ©)+ }+ return generated, spans, nil+}++func subtitleBlockTimingSpans(blocks []*util.SrtBlock) ([]subtitleTimingSpan, string, error) {+ var spans []subtitleTimingSpan+ var texts []string+ position := 0+ for _, block := range blocks {+ start, end, err := parseSRTTimeline(block.Timestamp)+ if err != nil || end <= start {+ return nil, "", fmt.Errorf("字幕%d时间无效", block.Index)+ }+ length := utf8.RuneCountInString(compactSubtitleText(block.OriginLanguageSentence))+ if length == 0 {+ return nil, "", fmt.Errorf("字幕%d缺少原文", block.Index)+ }+ spans = append(spans, subtitleTimingSpan{position, position + length, start, end})+ position += length+ texts = append(texts, block.OriginLanguageSentence)+ }+ return spans, strings.Join(texts, ""), nil+}++func splitShortSubtitleBlocks(blocks []*util.SrtBlock, spans []subtitleTimingSpan, maxChars, maxWords int) (map[int][]util.SrtBlock, error) {+ result := make(map[int][]util.SrtBlock)+ position := 0+ for _, block := range blocks {+ parts, err := splitSubtitleText(block.OriginLanguageSentence, func(text string) bool {+ return (maxChars <= 0 || subtitleTargetLength(text) <= maxChars) && (maxWords <= 0 || len(strings.Fields(text)) <= maxWords)+ })+ if err != nil {+ return nil, err+ }+ for _, part := range parts {+ start := subtitlePositionTime(spans, position, false)+ position += utf8.RuneCountInString(compactSubtitleText(part))+ end := subtitlePositionTime(spans, position, true)+ if end <= start {+ return nil, fmt.Errorf("短字幕%d分段后时间不足", block.Index)+ }+ result[block.Index] = append(result[block.Index], util.SrtBlock{Timestamp: formatSRTTimeline(start, end), OriginLanguageSentence: part})+ }+ }+ return result, nil+}diff --git a/runtime/krillinai/internal/service/translate.go b/runtime/krillinai/internal/service/translate.goindex 15cfb0f5..fa165c60 100644--- a/runtime/krillinai/internal/service/translate.go+++ b/runtime/krillinai/internal/service/translate.go@@ -10,14 +10,15 @@ import ("krillin-ai/pkg/openai""krillin-ai/pkg/util""strings"- "sync""time"+ "unicode""go.uber.org/zap")type Translator struct {- chatCompleter types.ChatCompleter+ chatCompleter types.ChatCompleter+ filterNonSemanticFillers bool}func NewTranslator() *Translator {@@ -27,102 +28,12 @@ func NewTranslator() *Translator {}func (t *Translator) SplitTextAndTranslate(inputText string, originLang, targetLang types.StandardLanguageCode) ([]*TranslatedItem, error) {- return t.splitTextAndTranslate(inputText, originLang, targetLang, translationParallelism())-}--func (t *Translator) splitTextAndTranslate(inputText string, originLang, targetLang types.StandardLanguageCode, parallelism int) ([]*TranslatedItem, error) {- sentences := util.SplitTextSentences(inputText, config.Conf.App.MaxSentenceLength)- if len(sentences) == 0 {- return []*TranslatedItem{}, nil- }-- // 补丁:whisper转录中文的时候很多句子后面不输出符号,导致上面基于符号的切分失效- if IsSplitUseSpace(originLang) {- newSentences := make([]string, 0)- for _, sentence := range sentences {- newSentences = append(newSentences, strings.Split(sentence, " ")...)- }- sentences = newSentences- }-- shortSentences := make([]string, 0)- // 使用递归拆句确保所有句子都满足长度要求- for _, sentence := range sentences {- if sentence == "" {- continue- }- recursiveSplitItems := t.recursiveSplitSentence(sentence, 0)- shortSentences = append(shortSentences, recursiveSplitItems...)- }-- sentences = shortSentences-- var (- signal = make(chan struct{}, parallelism) // 控制最大并发数- wg sync.WaitGroup- results = make([]*TranslatedItem, len(sentences))- // errChan = make(chan error, 1)- // mutex sync.Mutex- )-- for i, sentence := range sentences {- wg.Add(1)- signal <- struct{}{}-- go func(index int, originText string) {- defer wg.Done()- defer func() { <-signal }()-- contextSentenceNum := 3-- // 生成前面3个句子的string- var previousSentences string- if index > 0 {- start := 0- if index-contextSentenceNum > 0 {- start = index - contextSentenceNum- }- for i := start; i < index; i++ {- previousSentences += sentences[i] + "\n"- }- }-- // 生成后面3个句子的string- var nextSentences string- if index < len(sentences)-1 {- end := len(sentences) - 1- if index+contextSentenceNum < end {- end = index + contextSentenceNum- }- for i := index + 1; i <= end; i++ {- if i > index+1 {- nextSentences += "\n"- }- nextSentences += sentences[i]- }- }-- prompt := fmt.Sprintf(types.SplitTextWithContextPrompt, types.GetStandardLanguageName(targetLang), previousSentences, originText, nextSentences)-- translatedText, err := t.translateWithRetry(prompt, originText, originLang, targetLang)- if err != nil {- log.GetLogger().Error("splitTextAndTranslate llm translate error after retries", zap.Error(err), zap.Any("original text", originText))- results[index] = &TranslatedItem{- OriginText: originText,- TranslatedText: originText,- }- } else {- results[index] = &TranslatedItem{- OriginText: originText,- TranslatedText: translatedText,- }- }- }(i, sentence)+ sentences := splitTranslationSentences(inputText)+ paired, err := t.translatePairedSentences(context.Background(), sentences, originLang, targetLang, subtitleTargetLimit(), 3, nil)+ if err != nil {+ return nil, err}-- wg.Wait()-- return results, nil+ return flattenPairedSentences(paired), nil}func (t *Translator) splitOriginLongSentence(sentence string) ([]string, error) {@@ -288,39 +199,19 @@ func (t *Translator) translateWithRetry(prompt, originText string, originLang, t// isTranslationValid 检查翻译是否有效func (t *Translator) isTranslationValid(originText, translatedText string, originLang, targetLang types.StandardLanguageCode) bool {- // 1. 翻译不能为空- if strings.TrimSpace(translatedText) == "" {+ originText = strings.Join(strings.Fields(originText), " ")+ translatedText = strings.Join(strings.Fields(translatedText), " ")+ if translatedText == "" {return false}-- // 2. 翻译不能与原文完全相同(除非是特殊情况)- if strings.TrimSpace(originText) == strings.TrimSpace(translatedText) {- // 检查是否是专有名词、数字或特殊符号- if t.isSpecialContent(originText) {- return true // 专有名词等可以保持原文- }- return false- }-- // 3. 检查语言特征(简单的启发式检查)- if !t.hasTargetLanguageCharacteristics(translatedText, targetLang) {- return false+ if originLang == targetLang {+ return true}-- // 4. 长度合理性检查(翻译结果不应该过长或过短)- originLen := len(strings.TrimSpace(originText))- translatedLen := len(strings.TrimSpace(translatedText))-- // 翻译结果长度应该在原文的0.3-3倍之间(考虑语言特性)- if float64(translatedLen) < float64(originLen)*0.3 || float64(translatedLen) > float64(originLen)*3 {- // 但对于很短的文本,允许更大的变化范围- if originLen < 10 {- return true- }- return false+ if strings.EqualFold(originText, translatedText) {+ return t.isSpecialContent(originText)}- return true+ return t.hasTargetLanguageCharacteristics(translatedText, targetLang)}// isSpecialContent 检查是否是专有名词、数字等特殊内容@@ -330,33 +221,42 @@ func (t *Translator) isSpecialContent(text string) bool {return false}- // 检查是否主要包含数字、符号、英文名词等- nonAlphaCount := 0+ letterCount := 0+ allUpper := truefor _, r := range text {- if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')) {- nonAlphaCount+++ if unicode.IsLetter(r) {+ letterCount+++ if r < 'A' || r > 'Z' {+ allUpper = false+ }}}-- // 如果超过一半的字符不是字母,可能是特殊内容- if float64(nonAlphaCount) > float64(len(text))*0.5 {+ if letterCount == 0 {+ return true+ }+ if allUpper && letterCount >= 2 && letterCount <= 10 && len(strings.Fields(text)) == 1 {return true}- // 检查常见的专有名词模式+ // 仅允许完整的已知专名;句子包含专名不代表整句可以不翻译。commonProperNouns := []string{"Dr.", "Mr.", "Mrs.", "Ms.", "Prof.","Andrew", "Huberman", "OpenAI", "ChatGPT", "YouTube",}-- textLower := strings.ToLower(text)- for _, noun := range commonProperNouns {- if strings.Contains(textLower, strings.ToLower(noun)) {- return true+ trimPunctuation := func(r rune) bool { return unicode.IsPunct(r) || unicode.IsSpace(r) }+ for _, word := range strings.Fields(text) {+ known := false+ for _, noun := range commonProperNouns {+ if strings.EqualFold(strings.TrimFunc(word, trimPunctuation), strings.TrimFunc(noun, trimPunctuation)) {+ known = true+ break+ }+ }+ if !known {+ return false}}-- return false+ return true}// hasTargetLanguageCharacteristics 检查文本是否具有目标语言特征@@ -418,6 +318,7 @@ IMPORTANT: The previous translation was inadequate. Please ensure:// isSentenceEnding 判断文本是否以句子结束符号结尾func isSentenceEnding(text string) bool {text = strings.TrimSpace(text)+ text = strings.TrimRight(text, "\"'”’)]}")if text == "" {return false}@@ -439,253 +340,15 @@ func isSentenceEnding(text string) bool {return false}-// BatchTranslateSrtBlocks 批量翻译SRT字幕块(智能分组:按完整句子分组,最多10个块)-func (t *Translator) BatchTranslateSrtBlocks(ctx context.Context, blocks []*util.SrtBlock, originLang, targetLang string, taskPtr *types.SubtitleTask) error {- if len(blocks) == 0 {- return nil- }-- maxBatchSize := config.Conf.App.VttBatchSize- if maxBatchSize <= 0 {- maxBatchSize = 10 // 默认最大批次大小+func subtitleTranslationContext(blocks []*util.SrtBlock, start, end int) ([]string, []string) {+ var previousTexts, nextTexts []string+ for _, block := range blocks[max(0, start-2):start] {+ previousTexts = append(previousTexts, block.OriginLanguageSentence)}-- originLangCode := types.StandardLanguageCode(originLang)- targetLangCode := types.StandardLanguageCode(targetLang)-- // 统计有内容的字幕块数量- validBlocksCount := 0- for _, block := range blocks {- if block.OriginLanguageSentence != "" {- validBlocksCount++- }+ for _, block := range blocks[end:min(len(blocks), end+2)] {+ nextTexts = append(nextTexts, block.OriginLanguageSentence)}-- log.GetLogger().Info("开始智能批量翻译SRT字幕块",- zap.Int("总块数", len(blocks)),- zap.Int("有效块数", validBlocksCount),- zap.Int("最大批次大小", maxBatchSize),- zap.String("源语言", originLang),- zap.String("目标语言", targetLang))-- // 第一步:按句子分组(识别完整句子)- var sentences [][]*util.SrtBlock- var currentSentence []*util.SrtBlock-- for _, block := range blocks {- // 跳过空块- if block.OriginLanguageSentence == "" {- continue- }-- currentSentence = append(currentSentence, block)-- // 兜底保证:即使没有句子结束符,也不能超过最大批次大小- if len(currentSentence) >= maxBatchSize {- log.GetLogger().Warn("句子累积达到最大批次大小,强制结束句子",- zap.Int("当前块数", len(currentSentence)),- zap.Int("最大批次大小", maxBatchSize))- sentences = append(sentences, currentSentence)- currentSentence = make([]*util.SrtBlock, 0)- continue- }-- // 遇到句子结尾,结束当前句子- if isSentenceEnding(block.OriginLanguageSentence) {- sentences = append(sentences, currentSentence)- currentSentence = make([]*util.SrtBlock, 0)- }- }-- // 处理最后一个未结束的句子- if len(currentSentence) > 0 {- sentences = append(sentences, currentSentence)- }-- log.GetLogger().Info("句子识别完成",- zap.Int("识别到的句子数", len(sentences)))-- // 第二步:智能合并句子成批次(3/2/1策略)- var batches [][]*util.SrtBlock- i := 0-- for i < len(sentences) {- // 策略1: 尝试合并3个句子- if i+2 < len(sentences) {- combined := make([]*util.SrtBlock, 0)- combined = append(combined, sentences[i]...)- combined = append(combined, sentences[i+1]...)- combined = append(combined, sentences[i+2]...)-- // 确保合并后不超过最大批次大小- if len(combined) > 0 && len(combined) <= maxBatchSize {- batches = append(batches, combined)- log.GetLogger().Debug("合并3个句子",- zap.Int("批次块数", len(combined)),- zap.Int("句子索引", i))- i += 3- continue- }- }-- // 策略2: 尝试合并2个句子- if i+1 < len(sentences) {- combined := make([]*util.SrtBlock, 0)- combined = append(combined, sentences[i]...)- combined = append(combined, sentences[i+1]...)-- // 确保合并后不超过最大批次大小- if len(combined) > 0 && len(combined) <= maxBatchSize {- batches = append(batches, combined)- log.GetLogger().Debug("合并2个句子",- zap.Int("批次块数", len(combined)),- zap.Int("句子索引", i))- i += 2- continue- }- }-- // 策略3: 单个句子- // 确保单个句子不超过最大批次大小- if len(sentences[i]) > 0 && len(sentences[i]) <= maxBatchSize {- batches = append(batches, sentences[i])- log.GetLogger().Debug("单个句子",- zap.Int("批次块数", len(sentences[i])),- zap.Int("句子索引", i))- i++- } else {- // 如果单个句子超过最大批次大小,需要拆分- log.GetLogger().Warn("单个句子超过最大批次大小,强制拆分",- zap.Int("句子块数", len(sentences[i])),- zap.Int("最大批次大小", maxBatchSize))-- for j := 0; j < len(sentences[i]); j += maxBatchSize {- end := j + maxBatchSize- if end > len(sentences[i]) {- end = len(sentences[i])- }- batches = append(batches, sentences[i][j:end])- }- i++- }- }-- totalBatches := len(batches)-- // 统计合并策略使用情况- var totalBlocks int- for _, batch := range batches {- totalBlocks += len(batch)- }-- log.GetLogger().Info("智能分组完成",- zap.Int("总句子数", len(sentences)),- zap.Int("总批次数", totalBatches),- zap.String("平均每批次", fmt.Sprintf("%.1f个块", float64(totalBlocks)/float64(max(totalBatches, 1)))),- zap.String("分组策略", "3/2/1句子合并策略,最大"+fmt.Sprintf("%d", maxBatchSize)+"个块"))-- var progressMu sync.Mutex- completedBatches := 0- reportCompleted := func() {- progressMu.Lock()- defer progressMu.Unlock()- completedBatches++- if taskPtr != nil {- progress := float64(completedBatches) / float64(totalBatches)- taskPtr.SetProgress(40 + uint8(progress*50))- }- }-- err := runTranslationBatches(ctx, totalBatches, func(ctx context.Context, batchIdx int) error {- batch := batches[batchIdx]- currentBatchNum := batchIdx + 1- log.GetLogger().Info("处理批次",- zap.Int("当前批次", currentBatchNum),- zap.Int("总批次", totalBatches),- zap.Int("批次块数", len(batch)),- zap.String("进度", fmt.Sprintf("%d/%d", currentBatchNum, totalBatches)))-- // 构建批量翻译的输入文本- var originTexts []string- for _, block := range batch {- if block.OriginLanguageSentence != "" {- originTexts = append(originTexts, block.OriginLanguageSentence)- }- }-- if len(originTexts) == 0 {- reportCompleted()- return nil- }-- // 调用批量翻译- translations, err := t.batchTranslateTextsContext(ctx, originTexts, originLangCode, targetLangCode)- if err != nil {- if ctx.Err() != nil {- return ctx.Err()- }- log.GetLogger().Error("批量翻译失败,尝试单独翻译",- zap.Error(err),- zap.Int("当前批次", currentBatchNum),- zap.Int("总批次", totalBatches),- zap.Int("批次块数", len(batch)))-- // 失败时回退到单独翻译每个字幕- for _, block := range batch {- if block.OriginLanguageSentence == "" {- continue- }-- log.GetLogger().Info("单独翻译字幕",- zap.Int("块索引", block.Index),- zap.String("文本预览", block.OriginLanguageSentence[:min(len(block.OriginLanguageSentence), 50)]))-- translatedText, err := t.translateSingleTextContext(- ctx,- block.OriginLanguageSentence,- originLangCode,- targetLangCode)-- if err != nil {- if ctx.Err() != nil {- return ctx.Err()- }- log.GetLogger().Error("单独翻译失败,使用原文",- zap.Error(err),- zap.Int("块索引", block.Index))- block.TargetLanguageSentence = block.OriginLanguageSentence- } else {- block.TargetLanguageSentence = translatedText- }- }-- reportCompleted()- return nil- }-- log.GetLogger().Info("批量翻译成功",- zap.Int("当前批次", currentBatchNum),- zap.Int("翻译数量", len(translations)))-- // 将翻译结果赋值回SRT块- for j, translation := range translations {- if j < len(batch) {- batch[j].TargetLanguageSentence = translation- }- }-- reportCompleted()- return nil- })- if err != nil {- return err- }-- log.GetLogger().Info("智能批量翻译完成",- zap.Int("总块数", len(blocks)),- zap.Int("处理批次", totalBatches),- zap.String("平均每批次", fmt.Sprintf("%.1f个块", float64(validBlocksCount)/float64(max(totalBatches, 1)))))- return nil+ return previousTexts, nextTexts}// batchTranslateTexts 批量翻译多个文本(通过单次LLM调用)@@ -694,6 +357,14 @@ func (t *Translator) batchTranslateTexts(texts []string, originLang, targetLang}func (t *Translator) batchTranslateTextsContext(ctx context.Context, texts []string, originLang, targetLang types.StandardLanguageCode) ([]string, error) {+ return t.batchTranslateTextsRequest(ctx, texts, originLang, targetLang, nil, nil)+}++func (t *Translator) batchTranslateTextsWithContext(texts []string, originLang, targetLang types.StandardLanguageCode, previousTexts, nextTexts []string) ([]string, error) {+ return t.batchTranslateTextsRequest(context.Background(), texts, originLang, targetLang, previousTexts, nextTexts)+}++func (t *Translator) batchTranslateTextsRequest(ctx context.Context, texts []string, originLang, targetLang types.StandardLanguageCode, previousTexts, nextTexts []string) ([]string, error) {if len(texts) == 0 {return []string{}, nil}@@ -706,7 +377,7 @@ func (t *Translator) batchTranslateTextsContext(ctx context.Context, texts []str// 构建语言说明,如果是中文特别强调简体targetLangName := types.GetStandardLanguageName(targetLang)- if targetLang == "zh" || string(targetLang) == "Chinese" {+ if targetLang == types.LanguageNameSimplifiedChinese || targetLang == "zh" || string(targetLang) == "Chinese" {targetLangName = "Simplified Chinese (简体中文)"}@@ -715,13 +386,22 @@ func (t *Translator) batchTranslateTextsContext(ctx context.Context, texts []strCRITICAL INSTRUCTIONS:1. DO NOT modify, add, or remove any content from the original text - translate ONLY2. Translate naturally and fluently in %s-3. If target language is Chinese, MUST use Simplified Chinese characters (简体中文), NOT Traditional Chinese (繁体中文)+3. For Simplified Chinese, use 简体中文; for Traditional Chinese, use 繁體中文4. Maintain the exact same number of subtitles (%d items)5. Preserve punctuation and formatting exactly as they appear6. Keep proper nouns, numbers, and special terms accurate7. Do NOT add explanations, interpretations, or extra information8. Output ONLY valid JSON, NO markdown code blocks, NO explanations, NO notes9. Start directly with { and end with }+10. Each input index MUST appear exactly once with non-empty translated text; never merge or move content to another index+11. Use the surrounding context to resolve references and keep terminology consistent; translate ONLY the numbered input subtitles+12. Each index has its own video timestamp. Translate each fragment at its original index, even if target-language word order differs. Never move a time phrase or other meaning to another index, and never replace spoken words with only punctuation++Previous context (reference only, do NOT include in output):+%s++Following context (reference only, do NOT include in output):+%sInput subtitles:%s@@ -733,11 +413,14 @@ Required JSON format (output ONLY this structure):targetLangName,targetLangName,len(texts),+ strings.Join(previousTexts, "\n"),+ strings.Join(nextTexts, "\n"),textList.String())// 调用LLMmaxAttempts := 3var lastErr error+ requestPrompt := promptfor attempt := 0; attempt < maxAttempts; attempt++ {if err := ctx.Err(); err != nil {@@ -747,7 +430,7 @@ Required JSON format (output ONLY this structure):zap.Int("文本数", len(texts)),zap.Int("尝试次数", attempt+1))- response, err := chatCompletionWithContext(ctx, t.chatCompleter, prompt)+ response, err := chatCompletionWithContext(ctx, t.chatCompleter, requestPrompt)if err != nil {if ctx.Err() != nil {return nil, ctx.Err()@@ -756,13 +439,11 @@ Required JSON format (output ONLY this structure):log.GetLogger().Warn("批量翻译LLM调用失败,重试",zap.Error(err),zap.Int("尝试次数", attempt+1))- if attempt+1 < maxAttempts {- timer := time.NewTimer(time.Second * time.Duration(attempt+1))+ if attempt < maxAttempts-1 {select {case <-ctx.Done():- timer.Stop()return nil, ctx.Err()- case <-timer.C:+ case <-time.After(time.Second * time.Duration(attempt+1)):}}continue@@ -815,20 +496,27 @@ Required JSON format (output ONLY this structure):break}// 空行会被 SRT 解析器当成字幕块边界,统一为单行文本。- translations[trans.Index-1] = strings.Join(strings.Fields(trans.Text), " ")+ translation := strings.Join(strings.Fields(trans.Text), " ")+ if !t.isTranslationValid(texts[trans.Index-1], translation, originLang, targetLang) {+ invalidResultErr = fmt.Errorf("第%d条译文未通过检查(原文未翻译、仅标点或目标语言不符): 原文=%q, 译文=%q", trans.Index, texts[trans.Index-1], translation)+ break+ }+ translations[trans.Index-1] = translation}if invalidResultErr != nil {lastErr = invalidResultErr+ log.GetLogger().Warn("批量翻译结果无效", zap.Error(lastErr), zap.Int("尝试次数", attempt+1))+ requestPrompt = prompt + fmt.Sprintf("\nRETRY CORRECTION: The previous response failed validation: %s. Translate the source fragment at the SAME index; do not move its meaning to a neighboring subtitle or use a blank/punctuation-only placeholder. Return the COMPLETE corrected JSON for all %d input subtitles.\n", invalidResultErr, len(texts))continue}return translations, nil}- return nil, fmt.Errorf("批量翻译失败,已重试%d次: %w", maxAttempts, lastErr)+ return nil, fmt.Errorf("批量翻译失败,已尝试%d次: %w", maxAttempts, lastErr)}-// translateSingleText 翻译单个文本(用作批量翻译失败时的回退)+// translateSingleText 通过结构化接口翻译单个文本。func (t *Translator) translateSingleText(text string, originLang, targetLang types.StandardLanguageCode) (string, error) {return t.translateSingleTextContext(context.Background(), text, originLang, targetLang)}@@ -838,8 +526,5 @@ func (t *Translator) translateSingleTextContext(ctx context.Context, text stringif err != nil {return "", fmt.Errorf("单文本翻译失败: %w", err)}- if !t.isTranslationValid(text, translations[0], originLang, targetLang) {- return "", fmt.Errorf("单文本翻译质量检查失败")- }return translations[0], nil}diff --git a/runtime/krillinai/internal/service/translate_task792_live_test.go b/runtime/krillinai/internal/service/translate_task792_live_test.gonew file mode 100644index 00000000..56509e39--- /dev/null+++ b/runtime/krillinai/internal/service/translate_task792_live_test.go@@ -0,0 +1,227 @@+package service++import (+ "context"+ "crypto/sha256"+ "encoding/json"+ "fmt"+ "krillin-ai/config"+ "krillin-ai/internal/types"+ "krillin-ai/log"+ "krillin-ai/pkg/util"+ "os"+ "path/filepath"+ "strconv"+ "strings"+ "sync"+ "testing"+ "time"++ "github.com/BurntSushi/toml"+)++type task792ContextCompleter func(context.Context, string) (string, error)++func (f task792ContextCompleter) ChatCompletion(prompt string) (string, error) {+ return f(context.Background(), prompt)+}++func (f task792ContextCompleter) ChatCompletionContext(ctx context.Context, prompt string) (string, error) {+ return f(ctx, prompt)+}++func TestTranslationTask792FullLive(t *testing.T) {+ configPath := os.Getenv("KRILLINAI_TASK792_CONFIG")+ sourcePath := os.Getenv("KRILLINAI_TASK792_SOURCE")+ outputPath := os.Getenv("KRILLINAI_TASK792_OUTPUT")+ if configPath == "" || sourcePath == "" || outputPath == "" {+ t.Skip("指定配置、原始字幕和独立输出目录后运行任务792全量翻译复测")+ }+ log.InitLogger()+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ if _, err := toml.DecodeFile(configPath, &config.Conf); err != nil {+ t.Fatal(err)+ }+ if value := os.Getenv("KRILLINAI_TASK792_PARALLEL"); value != "" {+ parallel, err := strconv.Atoi(value)+ if err != nil || parallel < 1 {+ t.Fatalf("无效复测并发数: %q", value)+ }+ config.Conf.App.TranslateParallelNum = parallel+ }+ blocks, err := util.ParseSrtFile(sourcePath)+ if err != nil || len(blocks) == 0 {+ t.Fatalf("原始字幕条数=%d, error=%v", len(blocks), err)+ }+ svc := &YouTubeSubtitleService{translator: NewTranslator()}+ if err := svc.validateYouTubeSubtitleBlocks(blocks, false); err != nil {+ t.Fatal(err)+ }+ if err := os.MkdirAll(outputPath, 0755); err != nil {+ t.Fatal(err)+ }+ var words []VttWord+ if vttPath := os.Getenv("KRILLINAI_TASK792_VTT"); vttPath != "" {+ words, err = svc.ExtractWordsFromVtt(vttPath)+ if err != nil {+ t.Fatal(err)+ }+ }+ source := blocks+ if err := svc.writeOriginLanguageSrtFile(source, filepath.Join(outputPath, "source-before-translation.srt")); err != nil {+ t.Fatal(err)+ }+ writeJSON := func(name string, value any) {+ t.Helper()+ data, err := json.MarshalIndent(value, "", " ")+ if err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(outputPath, name), data, 0600); err != nil {+ t.Fatal(err)+ }+ }+ type exchange struct {+ Prompt string `json:"prompt"`+ Response string `json:"response"`+ Error string `json:"error,omitempty"`+ }+ var recent []exchange+ var stateMu sync.Mutex+ calls := 0+ cached := 0+ started := time.Now()+ cachePath := filepath.Join(outputPath, "validated-batches")+ if err := os.MkdirAll(cachePath, 0700); err != nil {+ t.Fatal(err)+ }+ completer := svc.translator.chatCompleter+ svc.translator.chatCompleter = task792ContextCompleter(func(ctx context.Context, prompt string) (string, error) {+ input, parseErr := decodePairedTestInput(prompt)+ if parseErr != nil {+ return "", parseErr+ }+ inputJSON, _ := json.Marshal(input)+ key := sha256.Sum256(append([]byte(pairedTranslationPrompt+pairedTranslationRetry+config.Conf.Llm.Model), inputJSON...))+ cacheFile := filepath.Join(cachePath, fmt.Sprintf("%x.json", key))+ if data, readErr := os.ReadFile(cacheFile); readErr == nil {+ var item exchange+ if json.Unmarshal(data, &item) == nil && item.Error == "" {+ if _, checkErr := svc.translator.parsePairedTranslation(item.Response, input, "en", "zh_cn"); checkErr == nil {+ stateMu.Lock()+ cached+++ stateMu.Unlock()+ return item.Response, nil+ }+ }+ }+ stateMu.Lock()+ calls+++ stateMu.Unlock()+ response, err := chatCompletionWithContext(ctx, completer, prompt)+ item := exchange{Prompt: prompt, Response: response}+ if err != nil {+ item.Error = err.Error()+ }+ if err == nil {+ if _, checkErr := svc.translator.parsePairedTranslation(response, input, "en", "zh_cn"); checkErr == nil {+ data, _ := json.MarshalIndent(item, "", " ")+ if writeErr := os.WriteFile(cacheFile, data, 0600); writeErr != nil {+ return "", writeErr+ }+ }+ }+ stateMu.Lock()+ defer stateMu.Unlock()+ recent = append(recent, item)+ if len(recent) > 3 {+ recent = recent[1:]+ }+ writeJSON("activity.json", map[string]any{"calls": calls, "cached_batches": cached, "sentence_ids": []string{input.Sentences[0].ID, input.Sentences[len(input.Sentences)-1].ID}, "elapsed_seconds": time.Since(started).Seconds(), "updated_at": time.Now().Format(time.RFC3339)})+ return response, err+ })+ task := &types.SubtitleTask{TaskId: "task-792-full-retest"}+ task.SetProgressReporter(func(progress uint8) {+ stateMu.Lock()+ defer stateMu.Unlock()+ writeJSON("progress.json", map[string]any{"progress": progress, "calls": calls, "cached_batches": cached, "elapsed_seconds": time.Since(started).Seconds(), "updated_at": time.Now().Format(time.RFC3339)})+ })+ task.SetProgress(40)+ blocks, err = svc.translator.BatchTranslateSrtBlocks(context.Background(), blocks, "en", "zh_cn", task, words)+ if err == nil {+ err = svc.validateYouTubeSubtitleBlocks(blocks, true)+ }+ if err != nil {+ writeJSON("failure.json", map[string]any{"error": err.Error(), "calls": calls, "recent_exchanges": recent})+ t.Fatal(err)+ }+ assertPairedOutput(t, source, blocks, subtitleTargetLimit())+ if err := svc.writeOriginLanguageSrtFile(blocks, filepath.Join(outputPath, types.SubtitleTaskOriginLanguageSrtFileName)); err != nil {+ t.Fatal(err)+ }+ if err := svc.writeTargetLanguageSrtFile(blocks, filepath.Join(outputPath, "target_language_srt.srt")); err != nil {+ t.Fatal(err)+ }+ if err := svc.writeBilingualSrtFile(blocks, filepath.Join(outputPath, "bilingual_srt.srt"), true); err != nil {+ t.Fatal(err)+ }+ var sentences []Sentence+ if len(words) > 0 {+ sentences = svc.splitByPrimarySentencePunctuation(words)+ }+ if err := svc.writeShortSubtitleFile(blocks, sentences, filepath.Join(outputPath, types.SubtitleTaskShortOriginMixedSrtFileName), true); err != nil {+ t.Fatal(err)+ }+ writeJSON("summary.json", map[string]any{"success": true, "task_id": 792, "source_cues": len(source), "word_count": len(words), "cue_count": len(blocks), "llm_calls": calls, "cached_batches": cached, "elapsed_seconds": time.Since(started).Seconds(), "completed_at": time.Now().Format(time.RFC3339)})+ task.SetProgress(100)+ t.Logf("任务792全量翻译通过:%d条字幕,%d次调用,产物目录:%s", len(blocks), calls, outputPath)+}++func TestTask792FullTimingOffline(t *testing.T) {+ vttPath := os.Getenv("KRILLINAI_TASK792_VTT")+ if vttPath == "" {+ t.Skip("指定792原始VTT后执行全量离线时间映射检查")+ }+ log.InitLogger()+ svc := &YouTubeSubtitleService{translator: &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err+ }+ return echoPairedTestResponse(input)+ })}}+ words, err := svc.ExtractWordsFromVtt(vttPath)+ if err != nil {+ t.Fatal(err)+ }+ sentences := svc.splitByPrimarySentencePunctuation(words)+ source, err := svc.generateOriginLanguageSrt(sentences, filepath.Join(t.TempDir(), "source.srt"), &YoutubeSubtitleReq{})+ if err != nil {+ t.Fatal(err)+ }+ if err := svc.validateYouTubeSubtitleBlocks(source, false); err != nil {+ t.Fatal(err)+ }+ output, err := svc.translator.BatchTranslateSrtBlocks(context.Background(), source, "en", "zh_cn", nil, words)+ if err != nil {+ t.Fatal(err)+ }+ assertPairedOutput(t, source, output, subtitleTargetLimit())+ spans, err := subtitleWordTimingSpans(words, strings.Join(func() []string {+ var result []string+ for _, block := range source {+ result = append(result, block.OriginLanguageSentence)+ }+ return result+ }(), ""))+ if err != nil {+ t.Fatal(err)+ }+ for i, span := range spans {+ if span.end <= span.start || i > 0 && (span.from != spans[i-1].to || span.start < spans[i-1].end) {+ t.Fatalf("第%d个词区间不连续或时间重叠: %+v", i, span)+ }+ }+ t.Logf("整份离线检查通过:%d个词,%d个原句,%d个有效时间区间,未调用真实模型", len(words), len(source), len(spans))+}diff --git a/runtime/krillinai/internal/service/translate_test.go b/runtime/krillinai/internal/service/translate_test.goindex 7651aaa4..349a62e6 100644--- a/runtime/krillinai/internal/service/translate_test.go+++ b/runtime/krillinai/internal/service/translate_test.go@@ -1,10 +1,19 @@package serviceimport (+ "context"+ "encoding/json""fmt"+ "krillin-ai/config""krillin-ai/internal/types""krillin-ai/log"+ "krillin-ai/pkg/util"+ "os"+ "path/filepath"+ "strings""testing"++ "github.com/BurntSushi/toml")func TestTranslatorSplitOriginLongSentenceToleratesLLMNoise(t *testing.T) {@@ -189,3 +198,235 @@ func TestTranslateSingleTextRejectsSubtitleMarkupAndUsesStructuredTranslation(tt.Fatalf("translation = %q, calls = %d, error = %v", translation, completer.calls, err)}}++type translationCompleterFunc func(string) (string, error)++func (f translationCompleterFunc) ChatCompletion(prompt string) (string, error) {+ return f(prompt)+}++func TestTranslationValidationKeepsOnlyWholeSpecialContent(t *testing.T) {+ translator := &Translator{}+ tests := []struct {+ name, origin, translation string+ want bool+ }{+ {"普通整句不能原样返回", "I was 20 years old.", "I was 20 years old.", false},+ {"包含专名也要翻译", "OpenAI had the customers we needed.", "OpenAI had the customers we needed.", false},+ {"空格和大小写不能绕过检查", "Andrew proposed a partnership.", "ANDREW proposed a partnership.", false},+ {"数字多也不能豁免整句", "I am 20, 21, 22, 23.", "I am 20, 21, 22, 23.", false},+ {"纯数字", "2026", "2026", true},+ {"纯符号", "...", "...", true},+ {"缩写", "NASA", "NASA", true},+ {"品牌专名", "OpenAI", "OpenAI", true},+ {"人名", "Andrew Huberman", "Andrew Huberman", true},+ {"空译文", "hello", " \n ", false},+ {"无目标语言", "hello", "a greeting", false},+ {"短译文不受字节长度限制", "Yes, yes, yes, yes, yes, yes, yes.", "是。", true},+ {"自然译文", "that had the customers we needed", "那些拥有我们所需客户的公司", true},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ if got := translator.isTranslationValid(tt.origin, tt.translation, "en", "zh_cn"); got != tt.want {+ t.Fatalf("isTranslationValid(%q, %q) = %v, want %v", tt.origin, tt.translation, got, tt.want)+ }+ })+ }+ if !translator.isTranslationValid("hello", "hello", "en", "en") {+ t.Fatal("同语言字幕应允许保持原文")+ }+}++func task792TranslationBlocks() []*util.SrtBlock {+ return []*util.SrtBlock{+ {Index: 4841, Timestamp: "02:48:22,560 --> 02:48:24,960", OriginLanguageSentence: "So, heartpounding, I started reaching out to companies"},+ {Index: 4842, Timestamp: "02:48:24,960 --> 02:48:26,240", OriginLanguageSentence: "that had the customers we needed"},+ {Index: 4843, Timestamp: "02:48:26,240 --> 02:48:28,080", OriginLanguageSentence: "and I proposed a partnership."},+ {Index: 4844, Timestamp: "02:48:28,080 --> 02:48:28,731", OriginLanguageSentence: "I was a nobody."},+ {Index: 4845, Timestamp: "02:48:28,880 --> 02:48:29,920", OriginLanguageSentence: "I was 20 years old."},+ }+}++func TestBatchTranslateSrtBlocksTask792RetriesIncompleteResults(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.VttBatchSize = 10+ config.Conf.App.MaxSentenceLength = 50+ config.Conf.App.ShortSubtitleMaxChars = 20+ complete := task792PairedResponse()+ for _, invalid := range []string{+ `{"sentences":[]}`,+ strings.Replace(complete, `"source":"So, heartpounding,`, `"source":"So,`, 1),+ strings.Replace(complete, `"我提出了合作。"`, `"。"`, 1),+ } {+ completer := &scriptedCompleter{responses: []string{invalid, complete}}+ blocks, err := (&Translator{chatCompleter: completer}).BatchTranslateSrtBlocks(context.Background(), task792TranslationBlocks(), "en", "zh_cn", nil)+ if err != nil || completer.calls != 2 || len(blocks) != 5 {+ t.Fatalf("output=%v calls=%d error=%v", blocks, completer.calls, err)+ }+ if err := (&YouTubeSubtitleService{}).validateYouTubeSubtitleBlocks(blocks, true); err != nil {+ t.Fatal(err)+ }+ }+}++func task792PairedResponse() string {+ return `{"sentences":[+ {"sentence_id":"1","target_text":"于是,我心跳加速地联系了拥有所需客户的公司,我提出了合作。","segments":[{"source":"So, heartpounding, I started reaching out","target":"于是,我心跳加速地"},{"source":"to companies that had the customers we needed","target":"联系了拥有所需客户的公司,"},{"source":"and I proposed a partnership.","target":"我提出了合作。"}]},+ {"sentence_id":"2","target_text":"当时我还是无名小卒。","segments":[{"source":"I was a nobody.","target":"当时我还是无名小卒。"}]},+ {"sentence_id":"3","target_text":"那年我20岁。","segments":[{"source":"I was 20 years old.","target":"那年我20岁。"}]}]}`+}++func TestBatchTranslateSrtBlocksFailsAfterThreeAttemptsWithoutOriginalFallback(t *testing.T) {+ log.InitLogger()+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.VttBatchSize = 10+ completer := &scriptedCompleter{responses: []string{"not-json", "not-json", "not-json"}}+ translator := &Translator{chatCompleter: completer}+ blocks := task792TranslationBlocks()+ _, err := translator.BatchTranslateSrtBlocks(context.Background(), blocks, "en", "zh_cn", nil)+ if err == nil || !strings.Contains(err.Error(), "4841-4845") || completer.calls != 3 {+ t.Fatalf("翻译错误 = %v, 调用次数 = %d, want 3", err, completer.calls)+ }+ for _, block := range blocks {+ if block.TargetLanguageSentence != "" {+ t.Fatalf("第%d条失败后不应回填译文: %q", block.Index, block.TargetLanguageSentence)+ }+ }+}++func TestBatchTranslateTextsRetriesUntranslatedSentenceWithProperNoun(t *testing.T) {+ log.InitLogger()+ completer := &scriptedCompleter{responses: []string{+ `{"translations":[{"index":1,"text":"OpenAI had the customers we needed."}]}`,+ `{"translations":[{"index":1,"text":"OpenAI拥有我们所需的客户。"}]}`,+ }}+ translator := &Translator{chatCompleter: completer}+ translations, err := translator.batchTranslateTexts([]string{"OpenAI had the customers we needed."}, "en", "zh_cn")+ if err != nil || completer.calls != 2 || len(translations) != 1 || translations[0] != "OpenAI拥有我们所需的客户。" {+ t.Fatalf("translations = %v, calls = %d, error = %v", translations, completer.calls, err)+ }+}++func TestSplitTextAndTranslateDoesNotReturnOriginalOnFailure(t *testing.T) {+ log.InitLogger()+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.MaxSentenceLength = 200+ config.Conf.App.TranslateParallelNum = 1+ completer := &scriptedCompleter{responses: []string{"I was a nobody.", "I was a nobody.", "I was a nobody."}}+ translator := &Translator{chatCompleter: completer}+ items, err := translator.SplitTextAndTranslate("I was a nobody.", "en", "zh_cn")+ if err == nil || items != nil || completer.calls != 3 {+ t.Fatalf("items = %v, calls = %d, error = %v", items, completer.calls, err)+ }+}++func TestSubtitleTranslationUsesSurroundingContextWithoutExtraCalls(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.VttBatchSize = 2+ config.Conf.App.TranslateParallelNum = 1+ for _, imported := range []bool{false, true} {+ t.Run(fmt.Sprintf("导入字幕=%v", imported), func(t *testing.T) {+ blocks := make([]*util.SrtBlock, 6)+ for i := range blocks {+ blocks[i] = &util.SrtBlock{Index: i + 1, Timestamp: formatSRTTimeline(int64(i*1000), int64((i+1)*1000)), OriginLanguageSentence: fmt.Sprintf("source %d.", i+1)}+ }+ var inputs []pairedTranslationInput+ translator := &Translator{chatCompleter: translationCompleterFunc(func(prompt string) (string, error) {+ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err+ }+ inputs = append(inputs, input)+ return echoPairedTestResponse(input)+ })}+ var err error+ if imported {+ _, err = (Service{YouTubeSubtitleSrv: &YouTubeSubtitleService{translator: translator}}).TranslateSubtitleBlocks(context.Background(), blocks, &types.SubtitleTaskStepParam{OriginLanguage: "en", TargetLanguage: "zh_cn"})+ } else {+ _, err = translator.BatchTranslateSrtBlocks(context.Background(), blocks, "en", "zh_cn", nil)+ }+ if err != nil || len(inputs) != 3 {+ t.Fatalf("calls=%d error=%v", len(inputs), err)+ }+ middle := inputs[1].Sentences[0]+ if fmt.Sprint(middle.Before) != "[source 1. source 2.]" || fmt.Sprint(middle.After) != "[source 4. source 5.]" {+ t.Fatalf("缺少整句上下文: %+v", middle)+ }+ })+ }+}++func TestTranslationTask792Live(t *testing.T) {+ configPath := os.Getenv("KRILLINAI_TRANSLATION_CONFIG")+ if configPath == "" {+ t.Skip("未指定真实接口配置")+ }+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ log.InitLogger()+ if _, err := toml.DecodeFile(configPath, &config.Conf); err != nil {+ t.Fatal(err)+ }+ translator := NewTranslator()+ completer := translator.chatCompleter+ calls := 0+ translator.chatCompleter = translationCompleterFunc(func(prompt string) (string, error) { calls++; return completer.ChatCompletion(prompt) })+ cases := []struct {+ name string+ blocks []*util.SrtBlock+ }{+ {"partnership", task792TranslationBlocks()},+ {"time", []*util.SrtBlock{+ {Index: 1273, Timestamp: "00:00:00,000 --> 00:00:03,000", OriginLanguageSentence: "We might have AI that is smarter than any human"},+ {Index: 1274, Timestamp: "00:00:03,000 --> 00:00:05,000", OriginLanguageSentence: "by the end of this year."},+ }},+ {"song", []*util.SrtBlock{+ {Index: 2518, Timestamp: "00:00:00,000 --> 00:00:03,000", OriginLanguageSentence: "No. YouTube just threw you a copyright notice"},+ {Index: 2519, Timestamp: "00:00:03,000 --> 00:00:05,000", OriginLanguageSentence: "for using Drake's Tootsie"},+ {Index: 2520, Timestamp: "00:00:05,000 --> 00:00:07,000", OriginLanguageSentence: "Slide without permission."},+ }},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ blocks, err := translator.BatchTranslateSrtBlocks(context.Background(), tc.blocks, "en", "zh_cn", nil)+ if err != nil {+ t.Fatal(err)+ }+ assertPairedOutput(t, tc.blocks, blocks, subtitleTargetLimit())+ if output := os.Getenv("KRILLINAI_TRANSLATION_OUTPUT"); output != "" {+ if err := os.MkdirAll(output, 0755); err != nil {+ t.Fatal(err)+ }+ data, _ := json.MarshalIndent(blocks, "", " ")+ if err := os.WriteFile(filepath.Join(output, tc.name+".json"), data, 0600); err != nil {+ t.Fatal(err)+ }+ }+ for _, block := range blocks {+ t.Logf("%s | %s | %s", block.Timestamp, block.OriginLanguageSentence, block.TargetLanguageSentence)+ }+ })+ }+ t.Logf("三组问题句共调用%d次", calls)+}++func TestPairedTranslationAllowsReorderedTimePhrase(t *testing.T) {+ previous := config.Conf+ t.Cleanup(func() { config.Conf = previous })+ config.Conf.App.MaxSentenceLength = 40+ config.Conf.App.ShortSubtitleMaxChars = 20+ completer := &scriptedCompleter{responses: []string{`{"sentences":[{"sentence_id":"1","target_text":"到今年年底,我们可能会拥有比任何人类都更聪明的AI。","segments":[{"source":"We might have AI that is smarter","target":"到今年年底,我们可能会拥有"},{"source":"than any human by the end of this year.","target":"比任何人类都更聪明的AI。"}]}]}`}}+ source := []*util.SrtBlock{+ {Index: 1273, Timestamp: "00:00:00,000 --> 00:00:03,000", OriginLanguageSentence: "We might have AI that is smarter than any human"},+ {Index: 1274, Timestamp: "00:00:03,000 --> 00:00:05,000", OriginLanguageSentence: "by the end of this year."},+ }+ blocks, err := (&Translator{chatCompleter: completer}).BatchTranslateSrtBlocks(context.Background(), source, "en", "zh_cn", nil)+ if err != nil || completer.calls != 1 {+ t.Fatalf("calls=%d error=%v", completer.calls, err)+ }+ assertPairedOutput(t, source, blocks, 40)+}diff --git a/runtime/krillinai/internal/service/translation_parallel_test.go b/runtime/krillinai/internal/service/translation_parallel_test.goindex c353dbaa..49d91481 100644--- a/runtime/krillinai/internal/service/translation_parallel_test.go+++ b/runtime/krillinai/internal/service/translation_parallel_test.go@@ -22,7 +22,6 @@ type blockingTranslationCompleter struct {active atomic.Int32peak atomic.Int32calls atomic.Int32- echo echoBatchCompleter}func (c *blockingTranslationCompleter) ChatCompletion(prompt string) (string, error) {@@ -44,10 +43,7 @@ func (c *blockingTranslationCompleter) ChatCompletionContext(ctx context.Contextreturn "", ctx.Err()case <-c.release:}- if strings.Contains(prompt, "Input subtitles:\n") {- return (subtitleRoutingCompleter{}).ChatCompletion(prompt)- }- return c.echo.ChatCompletion(prompt)+ return (subtitleRoutingCompleter{}).ChatCompletion(prompt)}func TestSubtitleTranslationConcurrencyAndCancellation(t *testing.T) {@@ -76,7 +72,11 @@ func TestSubtitleTranslationConcurrencyAndCancellation(t *testing.T) {blocks := make([]*util.SrtBlock, len(parts))for index := range parts {parts[index] = fmt.Sprintf("Sentence number %d is here.", index+1)- blocks[index] = &util.SrtBlock{Index: index + 1, OriginLanguageSentence: parts[index]}+ blocks[index] = &util.SrtBlock{+ Index: index + 1,+ Timestamp: fmt.Sprintf("00:00:%02d,000 --> 00:00:%02d,900", index, index),+ OriginLanguageSentence: parts[index],+ }}var results []*TranslatedItemvar progress []int@@ -86,19 +86,19 @@ func TestSubtitleTranslationConcurrencyAndCancellation(t *testing.T) {checkpointDir := t.TempDir()done := make(chan error, 1)go func() {+ var err errorswitch path {case "platform":- done <- translator.BatchTranslateSrtBlocks(ctx, blocks, "en", "zh_cn", task)+ blocks, err = translator.BatchTranslateSrtBlocks(ctx, blocks, "en", "zh_cn", task)case "imported":- done <- service.TranslateSubtitleBlocks(ctx, blocks, &types.SubtitleTaskStepParam{OriginLanguage: "en", TargetLanguage: "zh_cn"})+ blocks, err = service.TranslateSubtitleBlocks(ctx, blocks, &types.SubtitleTaskStepParam{OriginLanguage: "en", TargetLanguage: "zh_cn"})case "transcribed":- var err errorresults, err = service.splitTextAndTranslateV2(ctx, checkpointDir, strings.Join(parts, " "), "en", "zh_cn", false, 0, func(completed, total int) error {progress = append(progress, completed)return nil})- done <- err}+ done <- err}()for range 3 {@@ -125,7 +125,7 @@ func TestSubtitleTranslationConcurrencyAndCancellation(t *testing.T) {}for index, part := range parts {if path == "transcribed" {- if len(results) != len(parts) || results[index].OriginText != part || results[index].TranslatedText != "translated: "+part {+ if len(results) != len(parts) || results[index].OriginText != part || results[index].TranslatedText != "测试译文:"+part {t.Fatalf("translation order mismatch at %d", index)}} else if blocks[index].TargetLanguageSentence != "测试译文:"+part {diff --git a/runtime/krillinai/internal/service/youtube_caption_text_test.go b/runtime/krillinai/internal/service/youtube_caption_text_test.goindex daafa47a..f73e3cee 100644--- a/runtime/krillinai/internal/service/youtube_caption_text_test.go+++ b/runtime/krillinai/internal/service/youtube_caption_text_test.go@@ -19,6 +19,27 @@ func TestCleanVttTextDecodesSpeakerMarkersBeforeFiltering(t *testing.T) {}}+func TestWordCaptionsPreserveDemonstratedHesitation(t *testing.T) {+ dir := t.TempDir()+ path := filepath.Join(dir, "hesitation.vtt")+ content := "WEBVTT\n\n00:02:04.000 --> 00:02:06.510\nAnd<00:02:04.080><c> the</c><00:02:04.160><c> second</c><00:02:04.440><c> your</c><00:02:04.520><c> explanation</c><00:02:05.120><c> becomes</c>\n\n00:02:06.520 --> 00:02:12.510\nAnd the second your explanation becomes\nuh\n\n00:02:12.520 --> 00:02:14.350\num\n\n00:02:14.360 --> 00:02:15.680\num\nyou<00:02:14.440><c> know</c><00:02:14.560><c> what</c><00:02:14.680><c> you</c><00:02:14.720><c> need</c><00:02:14.880><c> to</c><00:02:14.959><c> work</c><00:02:15.160><c> on.</c>\n"+ if err := os.WriteFile(path, []byte(content), 0600); err != nil {+ t.Fatal(err)+ }+ words, err := (&YouTubeSubtitleService{}).ExtractWordsFromVtt(path)+ if err != nil {+ t.Fatal(err)+ }+ var text []string+ for _, word := range words {+ text = append(text, word.Text)+ }+ want := "And the second your explanation becomes uh um you know what you need to work on."+ if strings.Join(text, " ") != want {+ t.Fatalf("有实际含义的演示停顿丢失或重复: %q", strings.Join(text, " "))+ }+}+func TestProcessYouTubeMusicCaptionsPreservesBriefUtterance(t *testing.T) {dir := t.TempDir()path := filepath.Join(dir, "captions.vtt")diff --git a/runtime/krillinai/internal/service/youtube_subtitle.go b/runtime/krillinai/internal/service/youtube_subtitle.goindex f7dfeb22..f36855ea 100644--- a/runtime/krillinai/internal/service/youtube_subtitle.go+++ b/runtime/krillinai/internal/service/youtube_subtitle.go@@ -7,6 +7,7 @@ import ("errors""fmt""html"+ "math""net/url""os""os/exec"@@ -562,7 +563,7 @@ func (s *YouTubeSubtitleService) processYouTubeSubtitle(ctx context.Context, reqif req.SourceOnly {sentences = s.groupWordsIntoSentencesWithoutLLM(vttWords)} else {- sentences = s.groupWordsIntoSentences(vttWords)+ sentences = s.splitByPrimarySentencePunctuation(vttWords)}if len(sentences) == 0 {return "", fmt.Errorf("%w: no sentences formed from VTT words", ErrYouTubeCaptionInvalid)@@ -599,7 +600,7 @@ func (s *YouTubeSubtitleService) processYouTubeSubtitle(ctx context.Context, req}// 4. 批量翻译生成目标语言SRT(40%-90%进度)- err = s.translator.BatchTranslateSrtBlocks(ctx, srtBlocks, req.OriginLanguage, req.TargetLanguage, req.TaskPtr)+ srtBlocks, err = s.translator.BatchTranslateSrtBlocks(ctx, srtBlocks, req.OriginLanguage, req.TargetLanguage, req.TaskPtr, vttWords)if err != nil {return "", fmt.Errorf("failed to batch translate: %w", err)}@@ -607,6 +608,9 @@ func (s *YouTubeSubtitleService) processYouTubeSubtitle(ctx context.Context, reqif err := s.validateYouTubeSubtitleBlocks(srtBlocks, true); err != nil {return "", err}+ if err := s.writeOriginLanguageSrtFile(srtBlocks, originSrtFile); err != nil {+ return "", err+ }// 更新进度:翻译完成if req.TaskPtr != nil {@@ -644,7 +648,7 @@ func (s *YouTubeSubtitleService) processYouTubeSubtitle(ctx context.Context, reqerr = s.writeShortSubtitleFile(srtBlocks, sentences, shortSrtFile, req.TargetLanguageFirst)if err != nil {log.GetLogger().Warn("生成短字幕失败", zap.Error(err))- // 不影响主流程,继续执行+ return "", fmt.Errorf("failed to write short subtitles: %w", err)} else {log.GetLogger().Info("生成短字幕完成", zap.String("文件", shortSrtFile))}@@ -684,6 +688,7 @@ func (s *YouTubeSubtitleService) ExtractWordsFromVtt(vttFile string) ([]VttWord,var words []VttWordscanner := bufio.NewScanner(file)+ scanner.Buffer(make([]byte, 4096), 4*1024*1024)var blockStartTime, blockEndTime stringwordNum := 0@@ -828,7 +833,7 @@ func (s *YouTubeSubtitleService) cleanVttText(text string) string {return text}text = html.UnescapeString(text)- text = strings.NewReplacer("\u200b", "", "\u200c", "", "\u200d", "", "\u00a0", " ").Replace(text)+ text = strings.NewReplacer("\u200b", "", "\u00a0", " ").Replace(text)// 先过滤音乐和其他提示标记(方括号内容)// 匹配 [music], [applause], [laughter], [inaudible] 等标记@@ -846,11 +851,6 @@ func (s *YouTubeSubtitleService) cleanVttText(text string) string {// 过滤 >> 符号(YouTube自动字幕的提示符号)cleanedText = strings.ReplaceAll(cleanedText, ">>", "")- // 过滤常见的语气词(位于句首或独立出现时)- // 匹配 Um, Uh, Er, Ah, Oh, Mm, Hmm 等,支持大小写- fillerWordsRegex := regexp.MustCompile(`(?i)^\s*(um|uh|er|ah|oh|mm|hmm|hm|eh)\b\s*[,,]?\s*`)- cleanedText = fillerWordsRegex.ReplaceAllString(cleanedText, "")-// 移除多余的空格cleanedText = strings.TrimSpace(cleanedText)@@ -1102,182 +1102,28 @@ func (s *YouTubeSubtitleService) findVttFileInDirectory(dir string) (string, errreturn "", fmt.Errorf("no VTT file found in directory: %s", dir)}-// writeVttWordsToSrt 将VttWord数组写入SRT文件,支持翻译和时间戳生成+// writeVttWordsToSrt 兼容旧入口,使用与主流程相同的目标驱动配对翻译。func (s *YouTubeSubtitleService) writeVttWordsToSrt(vttWords []VttWord, srtFile string, req *YoutubeSubtitleReq) error {if len(vttWords) == 0 {return fmt.Errorf("no VTT words to write")}-- // 初始进度基准(从当前进度开始,到90%结束)- baseProgress := uint8(10) // 假设函数开始时已有10%进度- if req.TaskPtr != nil && req.TaskPtr.ProcessPct > 0 {- baseProgress = req.TaskPtr.ProcessPct- }- targetProgress := uint8(90) // 函数完成时的目标进度-- // 步骤1: 根据标点符号将单词整理成完整的句子 (约占总进度的10%)- sentences := s.groupWordsIntoSentences(vttWords)- // 输出句子到调试文件- debugFile := filepath.Join(req.TaskBasePath, "no_ts.txt")- if err := s.writeSentencesToDebugFile(sentences, debugFile); err != nil {- log.GetLogger().Warn("Failed to write sentences debug file", zap.Error(err))- }- if len(sentences) == 0 {- return fmt.Errorf("no sentences formed from VTT words")- }-- // 更新进度到15%- if req.TaskPtr != nil {- req.TaskPtr.SetProgress(baseProgress + uint8(float64(targetProgress-baseProgress)*0.1))- log.GetLogger().Info("Progress updated after grouping sentences",- zap.Uint8("progress", req.TaskPtr.ProcessPct))- }-- log.GetLogger().Info("Grouped VTT words into sentences", zap.Int("句子数", len(sentences)))-- // 创建初始的SrtBlock列表- srtBlocks := make([]*util.SrtBlock, 0, 2*len(sentences))-- // 使用并发翻译,同时保证顺序- type translationResult struct {- index int- blocks []*util.SrtBlock- err error- }-- // 创建结果通道和goroutine数量控制- resultCh := make(chan translationResult, len(sentences))- maxConcurrency := translationParallelism()- semaphore := make(chan struct{}, maxConcurrency)-- // 启动并发翻译- for idx, sentence := range sentences {- go func(index int, sent Sentence) {- // 获取信号量- semaphore <- struct{}{}- defer func() { <-semaphore }()-- // The outer sentence workers already enforce the translation limit.- translatedBlocks, err := s.translator.splitTextAndTranslate(sent.Text, types.StandardLanguageCode(req.OriginLanguage), types.StandardLanguageCode(req.TargetLanguage), 1)- if err != nil {- log.GetLogger().Warn("Translation failed, using original text",- zap.Int("index", index),- zap.Error(err))- resultCh <- translationResult{index: index, blocks: nil, err: err}- return- }-- // 构建临时SrtBlock- notsSrtBlock := make([]*util.SrtBlock, 0, len(translatedBlocks))- for _, block := range translatedBlocks {- notsSrtBlock = append(notsSrtBlock, &util.SrtBlock{- OriginLanguageSentence: block.OriginText,- TargetLanguageSentence: block.TranslatedText,- })- }-- // 生成时间戳- updatedBlocks, err := s.timestampGenerator.GenerateTimestamps(- notsSrtBlock,- s.convertVttWordsToTypesWords(sent.Words),- types.StandardLanguageCode("base"), // 默认使用base语言类型- 0.0, // 时间偏移- )- if err != nil {- log.GetLogger().Warn("Timestamp generation failed",- zap.Int("index", index),- zap.Error(err))- updatedBlocks = notsSrtBlock // 使用未生成时间戳的块- }-- resultCh <- translationResult{index: index, blocks: updatedBlocks, err: nil}- }(idx, sentence)- }-- // 收集结果,按顺序排列,实时更新进度 (占总进度的70%)- results := make(map[int][]*util.SrtBlock)- completedTasks := 0- translationProgressBase := baseProgress + uint8(float64(targetProgress-baseProgress)*0.1) // 15%- translationProgressRange := uint8(float64(targetProgress-baseProgress) * 0.7) // 70%的进度范围-- for i := 0; i < len(sentences); i++ {- result := <-resultCh- completedTasks++-- if result.err == nil {- results[result.index] = result.blocks- }-- // 实时更新翻译进度- if req.TaskPtr != nil {- currentTranslationProgress := float64(completedTasks) / float64(len(sentences))- req.TaskPtr.SetProgress(translationProgressBase + uint8(float64(translationProgressRange)*currentTranslationProgress))-- // 每完成5个或完成所有任务时记录日志- if completedTasks%5 == 0 || completedTasks == len(sentences) {- log.GetLogger().Info("Translation progress updated",- zap.Int("completed", completedTasks),- zap.Int("total", len(sentences)),- zap.Uint8("progress", req.TaskPtr.ProcessPct))- }- }- }-- // 按顺序添加到最终的srtBlocks (占总进度的10%)- var blockIndex int- for i := 0; i < len(sentences); i++ {- if blocks, exists := results[i]; exists {- for _, block := range blocks {- srtBlocks = append(srtBlocks, &util.SrtBlock{- Index: blockIndex + 1,- Timestamp: block.Timestamp,- OriginLanguageSentence: block.OriginLanguageSentence,- TargetLanguageSentence: block.TargetLanguageSentence,- })- blockIndex++- }- }- }-- // 更新进度到85%(结果整理完成)- if req.TaskPtr != nil {- req.TaskPtr.SetProgress(baseProgress + uint8(float64(targetProgress-baseProgress)*0.85))- log.GetLogger().Info("Progress updated after organizing results",- zap.Uint8("progress", req.TaskPtr.ProcessPct))- }-- // 步骤6: 写入正常的SRT文件- err := s.writeSrtBlocksToFile(srtBlocks, srtFile)+ sentences := s.splitByPrimarySentencePunctuation(vttWords)+ originPath := filepath.Join(req.TaskBasePath, types.SubtitleTaskOriginLanguageSrtFileName)+ blocks, err := s.generateOriginLanguageSrt(sentences, originPath, req)if err != nil {return err}-- // 更新进度到88%(正常SRT文件写入完成)- if req.TaskPtr != nil {- req.TaskPtr.SetProgress(baseProgress + uint8(float64(targetProgress-baseProgress)*0.88))- log.GetLogger().Info("Progress updated after writing SRT file",- zap.Uint8("progress", req.TaskPtr.ProcessPct))- }-- // 步骤7: 生成短字幕文件- shortSrtFile := filepath.Join(filepath.Dir(srtFile), types.SubtitleTaskShortOriginMixedSrtFileName)- err = s.writeShortSubtitleFile(srtBlocks, sentences, shortSrtFile, req.TargetLanguageFirst)+ blocks, err = s.translator.BatchTranslateSrtBlocks(context.Background(), blocks, req.OriginLanguage, req.TargetLanguage, req.TaskPtr, vttWords)if err != nil {- log.GetLogger().Warn("生成短字幕失败", zap.Error(err))- // 不影响主流程,继续执行- } else {- log.GetLogger().Info("生成短字幕完成", zap.String("文件", shortSrtFile))+ return err}-- // 最终更新进度到90%(所有操作完成)- if req.TaskPtr != nil {- req.TaskPtr.SetProgress(targetProgress)- log.GetLogger().Info("writeVttWordsToSrt completed",- zap.Uint8("final_progress", req.TaskPtr.ProcessPct),- zap.Int("total_srt_blocks", len(srtBlocks)))+ if err := s.writeOriginLanguageSrtFile(blocks, originPath); err != nil {+ return err}-- return nil+ if err := s.writeBilingualSrtFile(blocks, srtFile, req.TargetLanguageFirst); err != nil {+ return err+ }+ return s.writeShortSubtitleFile(blocks, sentences, filepath.Join(filepath.Dir(srtFile), types.SubtitleTaskShortOriginMixedSrtFileName), req.TargetLanguageFirst)}// Sentence 表示一个完整的句子及其时间信息@@ -1660,16 +1506,6 @@ func (s *YouTubeSubtitleService) splitByPrimarySentencePunctuation(words []VttWovar currentWords []VttWordprimaryEndPunctuation := []rune{'.', '!', '?', '。', '!', '?'}- // 常见的缩写词,不应该作为句子结束标志- abbreviations := map[string]bool{- "dr.": true, "mr.": true, "mrs.": true, "ms.": true, "prof.": true,- "vs.": true, "etc.": true, "inc.": true, "ltd.": true, "corp.": true,- "co.": true, "jr.": true, "sr.": true, "st.": true, "ave.": true,- "blvd.": true, "rd.": true, "apt.": true, "no.": true, "vol.": true,- "ch.": true, "sec.": true, "fig.": true, "pg.": true, "pp.": true,- "i.e.": true, "e.g.": true, "cf.": true, "et.": true, "al.": true,- }-// 跟踪引号状态var inQuotes bool@@ -1686,7 +1522,7 @@ func (s *YouTubeSubtitleService) splitByPrimarySentencePunctuation(words []VttWowordLower := strings.ToLower(strings.TrimSpace(word.Text))// 如果是缩写词,不分句- if abbreviations[wordLower] {+ if subtitleAbbreviations[wordLower] {continue}@@ -2109,6 +1945,16 @@ func (s *YouTubeSubtitleService) convertVttWordsToTypesWords(vttWords []VttWord)return typesWords}+func (s *YouTubeSubtitleService) writeOriginLanguageSrtFile(blocks []*util.SrtBlock, path string) error {+ originals := make([]*util.SrtBlock, len(blocks))+ for i, block := range blocks {+ copy := *block+ copy.TargetLanguageSentence = ""+ originals[i] = ©+ }+ return s.writeSrtBlocksToFile(originals, path)+}+// writeSrtBlocksToFile 将SrtBlock数组写入文件func (s *YouTubeSubtitleService) writeSrtBlocksToFile(blocks []*util.SrtBlock, srtFile string) error {file, err := os.Create(srtFile)@@ -2596,26 +2442,15 @@ func (s *YouTubeSubtitleService) findVttWordsForSrtBlock(useTimeFilter = true}- // 在所有句子中查找匹配的单词序列+ var closestWords []VttWord+ var closestDistance float64+ // 同一句话可能重复出现,按当前字幕时间选择最近的一次。for _, sentence := range sentences {if len(sentence.Words) < len(expectedWords) {continue}- // 如果启用时间过滤,检查句子的时间范围是否与 SRT 块匹配- if useTimeFilter && len(sentence.Words) > 0 {- // 获取句子第一个单词的开始时间- firstWordTime, err := s.parseVttTime(sentence.Words[0].Start)- if err == nil {- // 如果句子开始时间与 SRT 块开始时间相差超过 1 秒,跳过- // 这样可以避免匹配到重复的文本- timeDiff := firstWordTime - srtStartTime- if timeDiff < -0.5 || timeDiff > 1.0 {- continue- }- }- }-+ // 一个完整句可能被译文长度拆成多条,时间检查应针对匹配片段。// 尝试匹配for i := 0; i <= len(sentence.Words)-len(expectedWords); i++ {match := true@@ -2645,7 +2480,11 @@ func (s *YouTubeSubtitleService) findVttWordsForSrtBlock(zap.String("originText", originText),zap.Float64("srtStart", srtStartTime),zap.Float64("vttStart", firstTime))- return matchedWords+ distance := math.Abs(firstTime-srtStartTime) + math.Abs(lastTime-srtEndTime)+ if len(closestWords) == 0 || distance < closestDistance {+ closestWords = matchedWords+ closestDistance = distance+ }}}} else {@@ -2656,6 +2495,9 @@ func (s *YouTubeSubtitleService) findVttWordsForSrtBlock(}}+ if len(closestWords) > 0 {+ return closestWords+ }log.GetLogger().Debug("No VTT words found for SRT block",zap.String("originText", originText),zap.String("timestamp", srtBlock.Timestamp))@@ -3535,7 +3377,7 @@ func (s *YouTubeSubtitleService) ProcessBlockLevelVtt(ctx context.Context, req *}// 4. 批量翻译(40%-90%的进度在BatchTranslateSrtBlocks内部更新)- err = s.translator.BatchTranslateSrtBlocks(ctx, srtBlocks, req.OriginLanguage, req.TargetLanguage, req.TaskPtr)+ srtBlocks, err = s.translator.BatchTranslateSrtBlocks(ctx, srtBlocks, req.OriginLanguage, req.TargetLanguage, req.TaskPtr)if err != nil {return "", fmt.Errorf("批量翻译失败: %w", err)}@@ -3543,6 +3385,9 @@ func (s *YouTubeSubtitleService) ProcessBlockLevelVtt(ctx context.Context, req *if err := s.validateYouTubeSubtitleBlocks(srtBlocks, true); err != nil {return "", err}+ if err := s.writeOriginLanguageSrtFile(srtBlocks, originSrtFile); err != nil {+ return "", err+ }// 更新进度:翻译完成if req.TaskPtr != nil {@@ -3569,6 +3414,10 @@ func (s *YouTubeSubtitleService) ProcessBlockLevelVtt(ctx context.Context, req *return "", fmt.Errorf("写入双语字幕失败: %w", err)}log.GetLogger().Info("生成双语字幕完成", zap.String("bilingualSrtFile", bilingualSrtFile))+ shortSrtFile := filepath.Join(req.TaskBasePath, types.SubtitleTaskShortOriginMixedSrtFileName)+ if err := s.writeShortSubtitleFile(srtBlocks, nil, shortSrtFile, req.TargetLanguageFirst); err != nil {+ return "", err+ }if err := s.writeYouTubeSubtitleTextFiles(srtBlocks, req); err != nil {return "", err}@@ -3597,8 +3446,8 @@ func (s *YouTubeSubtitleService) validateYouTubeSubtitleBlocks(blocks []*util.Srif err != nil || start < 0 || end <= start {return fmt.Errorf("%w: 字幕质量检查失败: 第%d条时间区间无效", ErrYouTubeCaptionInvalid, index+1)}- if translated && strings.TrimSpace(block.TargetLanguageSentence) == "" {- return fmt.Errorf("字幕质量检查失败: 第%d条缺少译文", index+1)+ if translated && (!hasSubtitleContent(block.TargetLanguageSentence) || hasSubtitleLineBreak(block.TargetLanguageSentence) || hasSubtitleLineBreak(block.OriginLanguageSentence) || subtitleTargetLength(block.TargetLanguageSentence) > subtitleTargetLimit() || subtitleTargetLength(block.OriginLanguageSentence) > subtitleTargetLimit()) {+ return fmt.Errorf("字幕质量检查失败: 第%d条原文或译文缺少内容、包含换行或超过单行长度上限%d", index+1, subtitleTargetLimit())}}return nil@@ -3683,104 +3532,58 @@ func wordGroupTextLength(words []VttWord) int {return length}-// writeShortSubtitleFile 生成短字幕文件(中文完整 + 英文拆分)-// 适用于竖屏视频:中文保持完整便于理解,英文按字符长度拆分避免一行太长+// writeShortSubtitleFile 保留整条译文,按词时间和短字幕字符参数独立拆分原文。func (s *YouTubeSubtitleService) writeShortSubtitleFile(srtBlocks []*util.SrtBlock,sentences []Sentence,shortSrtFile string,targetLanguageFirst bool,) error {+ // 块级字幕没有词时间,沿用普通双语字幕作为兼容输出。+ if len(sentences) == 0 {+ return s.writeBilingualSrtFile(srtBlocks, shortSrtFile, targetLanguageFirst)+ }file, err := os.Create(shortSrtFile)if err != nil {return fmt.Errorf("failed to create short subtitle file: %w", err)}defer file.Close()-- blockIndex := 1maxChars := config.Conf.App.ShortSubtitleMaxCharsif maxChars <= 0 {- maxChars = 20 // 默认值+ maxChars = 20}-- log.GetLogger().Info("开始生成短字幕文件",- zap.String("file", shortSrtFile),- zap.Int("maxChars", maxChars),- zap.Int("srtBlocksCount", len(srtBlocks)))-- for _, srtBlock := range srtBlocks {- // 1. 写入完整的中文翻译- if srtBlock.TargetLanguageSentence != "" {- _, err = file.WriteString(fmt.Sprintf("%d\n", blockIndex))- if err != nil {- return err- }- _, err = file.WriteString(srtBlock.Timestamp + "\n")- if err != nil {- return err- }- _, err = file.WriteString(srtBlock.TargetLanguageSentence + "\n\n")- if err != nil {+ spans, expected, err := subtitleBlockTimingSpans(srtBlocks)+ if err != nil {+ return err+ }+ var allWords []VttWord+ for _, sentence := range sentences {+ allWords = append(allWords, sentence.Words...)+ }+ if wordSpans, timingErr := subtitleWordTimingSpans(allWords, expected); timingErr == nil {+ spans = wordSpans+ }+ shortBlocks, err := splitShortSubtitleBlocks(srtBlocks, spans, maxChars, 0)+ if err != nil {+ return err+ }+ index := 1+ writeCue := func(timestamp, text string) error {+ _, err := fmt.Fprintf(file, "%d\n%s\n%s\n\n", index, timestamp, text)+ index+++ return err+ }+ for _, block := range srtBlocks {+ if block.TargetLanguageSentence != "" {+ if err := writeCue(block.Timestamp, block.TargetLanguageSentence); err != nil {return err}- blockIndex++- }-- // 2. 找到对应的 VTT 单词- vttWords := s.findVttWordsForSrtBlock(srtBlock, sentences)- if len(vttWords) == 0 {- log.GetLogger().Warn("No VTT words found for SRT block, skipping English split",- zap.String("originText", srtBlock.OriginLanguageSentence))- continue}-- // 3. 按字符长度分组- groups := s.groupWordsByCharLength(vttWords, maxChars)-- log.GetLogger().Debug("Split English into groups",- zap.String("originText", srtBlock.OriginLanguageSentence),- zap.Int("groupsCount", len(groups)))-- // 4. 为每个英文片段生成字幕块- for _, group := range groups {- startTime := group[0].Start- endTime := group[len(group)-1].End-- // 拼接单词- var words []string- for _, word := range group {- words = append(words, word.Text)- }- text := strings.Join(words, " ")-- // 转换时间戳格式- timestamp, err := s.convertToSrtTimestamp(startTime, endTime)- if err != nil {- log.GetLogger().Warn("Failed to convert timestamp, using SRT block timestamp",- zap.Error(err))- timestamp = srtBlock.Timestamp- }-- // 写入英文片段- _, err = file.WriteString(fmt.Sprintf("%d\n", blockIndex))- if err != nil {+ for _, short := range shortBlocks[block.Index] {+ if err := writeCue(short.Timestamp, short.OriginLanguageSentence); err != nil {return err}- _, err = file.WriteString(timestamp + "\n")- if err != nil {- return err- }- _, err = file.WriteString(text + "\n\n")- if err != nil {- return err- }- blockIndex++}}-- log.GetLogger().Info("Short subtitle file written successfully",- zap.String("file", shortSrtFile),- zap.Int("totalBlocks", blockIndex-1))-return nil}diff --git a/runtime/krillinai/internal/service/youtube_subtitle_routing_test.go b/runtime/krillinai/internal/service/youtube_subtitle_routing_test.goindex c5d1e440..616a7b2b 100644--- a/runtime/krillinai/internal/service/youtube_subtitle_routing_test.go+++ b/runtime/krillinai/internal/service/youtube_subtitle_routing_test.go@@ -2,11 +2,9 @@ package serviceimport ("context"- "encoding/json""errors""os""path/filepath"- "regexp""strconv""strings""testing"@@ -21,21 +19,16 @@ import (type subtitleRoutingCompleter struct{}func (subtitleRoutingCompleter) ChatCompletion(prompt string) (string, error) {- _, input, found := strings.Cut(prompt, "Input subtitles:\n")- if !found {- return "", nil+ input, err := decodePairedTestInput(prompt)+ if err != nil {+ return "", err}- input, _, _ = strings.Cut(input, "\nRequired JSON format")- matches := regexp.MustCompile("(?m)^(\\d+)\\. (.+)$").FindAllStringSubmatch(input, -1)- translations := make([]map[string]any, 0, len(matches))- for _, match := range matches {- index, err := strconv.Atoi(match[1])- if err != nil {- return "", err- }- translations = append(translations, map[string]any{"index": index, "text": "测试译文:" + match[2]})+ var sentences []pairedSentence+ for _, item := range input.Sentences {+ target := "测试译文:" + item.Text+ sentences = append(sentences, pairedSentence{ID: item.ID, FullTarget: target, Source: []string{item.Text}, Target: []string{target}})}- data, err := json.Marshal(map[string]any{"translations": translations})+ data, err := encodePairedTestResponse(sentences)return string(data), err}@@ -46,6 +39,8 @@ func subtitleRoutingTestService(test *testing.T) *YouTubeSubtitleService {previousApp := config.Conf.Appconfig.Conf.App.EnableBlockVttBatch = falseconfig.Conf.App.VttBatchSize = 10+ config.Conf.App.MaxSentenceLength = 200+ config.Conf.App.ShortSubtitleMaxChars = 200test.Cleanup(func() {log.Logger = previousLoggerconfig.Conf.App = previousAppdiff --git a/runtime/krillinai/pkg/openai/init.go b/runtime/krillinai/pkg/openai/init.goindex b94cf1fd..096b00ba 100644--- a/runtime/krillinai/pkg/openai/init.go+++ b/runtime/krillinai/pkg/openai/init.go@@ -31,7 +31,8 @@ func newClient(baseUrl, apiKey, proxyAddr string, requestTimeout time.Duration)KeepAlive: 30 * time.Second,}).DialContexttransport.TLSHandshakeTimeout = 10 * time.Second- transport.ResponseHeaderTimeout = 30 * time.Second+ // 非流式响应可能在生成完成后才发送响应头,由请求 context 统一限制总耗时。+ transport.ResponseHeaderTimeout = 0if proxyAddr != "" {if proxyURL, err := url.Parse(proxyAddr); err == nil {transport.Proxy = http.ProxyURL(proxyURL)diff --git a/runtime/krillinai/pkg/openai/openai.go b/runtime/krillinai/pkg/openai/openai.goindex 79a8b2cf..17989b54 100644--- a/runtime/krillinai/pkg/openai/openai.go+++ b/runtime/krillinai/pkg/openai/openai.go@@ -7,7 +7,6 @@ import ("fmt"openai "github.com/sashabaranov/go-openai""go.uber.org/zap"- "io""krillin-ai/config""krillin-ai/log""strings"@@ -37,37 +36,29 @@ func (c *Client) ChatCompletionContext(ctx context.Context, query string) (strinContent: query,},},- Stream: true,MaxTokens: 8192,ResponseFormat: responseFormat,}+ // DeepSeek V4 的推理可能耗尽字幕 JSON 的输出预算。+ switch strings.ToLower(strings.TrimSpace(req.Model)) {+ case "deepseek-flash", "deepseek-v4-flash", "deepseek-v4-pro":+ req.ReasoningEffort = "none"+ req.Temperature = 0.2+ }- stream, err := c.client.CreateChatCompletionStream(ctx, req)+ response, err := c.client.CreateChatCompletion(ctx, req)if err != nil {- log.GetLogger().Error("openai create chat completion stream failed", zap.Error(err))+ log.GetLogger().Error("openai chat completion failed", zap.Error(err))return "", normalizeChatCompletionError(err)}- defer stream.Close()-- var resContent string- for {- response, err := stream.Recv()- if err == io.EOF {- break- }- if err != nil {- log.GetLogger().Error("openai stream receive failed", zap.Error(err))- return "", normalizeChatCompletionError(err)- }- if len(response.Choices) == 0 {- log.GetLogger().Info("openai stream receive no choices", zap.Any("response", response))- continue- }-- resContent += response.Choices[0].Delta.Content+ log.GetLogger().Info("openai chat completion received", zap.String("id", response.ID), zap.String("model", response.Model), zap.Any("usage", response.Usage))+ if len(response.Choices) > 0 && response.Choices[0].FinishReason == openai.FinishReasonLength {+ return "", fmt.Errorf("llm_translation_truncated: response %s exceeded its output token budget", response.ID)}-- return resContent, nil+ if len(response.Choices) == 0 || strings.TrimSpace(response.Choices[0].Message.Content) == "" {+ return "", fmt.Errorf("llm_translation_empty_response: response %s has no content", response.ID)+ }+ return response.Choices[0].Message.Content, nil}func normalizeChatCompletionError(err error) error {diff --git a/runtime/krillinai/pkg/openai/openai_test.go b/runtime/krillinai/pkg/openai/openai_test.goindex f2b991ad..3ed88740 100644--- a/runtime/krillinai/pkg/openai/openai_test.go+++ b/runtime/krillinai/pkg/openai/openai_test.go@@ -4,6 +4,7 @@ import ("context""encoding/json""errors"+ "fmt""krillin-ai/config""krillin-ai/log""net/http"@@ -14,29 +15,90 @@ import ("time")-func TestChatCompletionUsesDefaultSampling(t *testing.T) {+func TestChatCompletionReceivesCompleteJSON(t *testing.T) {setTestModel(t)server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- var body map[string]any- if err := json.NewDecoder(r.Body).Decode(&body); err != nil {- t.Error(err)- w.WriteHeader(http.StatusBadRequest)- return+ var request struct {+ Stream bool `json:"stream"`}- if _, exists := body["temperature"]; exists {- t.Error("translation must use the model's default sampling")- w.WriteHeader(http.StatusBadRequest)- return+ if err := json.NewDecoder(r.Body).Decode(&request); err != nil || request.Stream {+ t.Errorf("请求应使用非流式JSON: stream=%v error=%v", request.Stream, err)}- w.Header().Set("Content-Type", "text/event-stream")- _, _ = w.Write([]byte("data: {\"choices\":[]}\n\ndata: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"translated\"}}]}\n\ndata: [DONE]\n\n"))+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"id":"complete-json","choices":[{"message":{"role":"assistant","content":"完整译文"}}]}`))}))defer server.Close()+ text, err := newClient(server.URL, "test-key", "", time.Second).ChatCompletion("translate")+ if err != nil || text != "完整译文" {+ t.Fatalf("text=%q error=%v", text, err)+ }+}- client := newClient(server.URL, "test-key", "", time.Second)- content, err := client.ChatCompletion("translate")- if err != nil || content != "translated" {- t.Fatalf("ChatCompletion() = %q, %v; want translated", content, err)+func TestChatCompletionRejectsEmptyResponse(t *testing.T) {+ setTestModel(t)+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"id":"empty","choices":[]}`))+ }))+ defer server.Close()+ _, err := newClient(server.URL, "test-key", "", time.Second).ChatCompletion("translate")+ if err == nil || !strings.Contains(err.Error(), "llm_translation_empty_response") {+ t.Fatalf("未明确拒绝空响应: %v", err)+ }+}++func TestChatCompletionDisablesDeepSeekV4Thinking(t *testing.T) {+ for _, model := range []string{"deepseek-flash", "deepseek-v4-flash", "deepseek-v4-pro", "deepseek-chat", "test-model"} {+ t.Run(model, func(t *testing.T) {+ setTestModel(t)+ config.Conf.Llm.Model = model+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ var request struct {+ Model string `json:"model"`+ ReasoningEffort string `json:"reasoning_effort"`+ Temperature *float32 `json:"temperature"`+ }+ if err := json.NewDecoder(r.Body).Decode(&request); err != nil {+ t.Error(err)+ }+ want := "none"+ if model == "deepseek-chat" || model == "test-model" {+ want = ""+ }+ if request.Model != model || request.ReasoningEffort != want {+ t.Errorf("model=%q reasoning_effort=%q, want %q %q", request.Model, request.ReasoningEffort, model, want)+ }+ if want == "none" && (request.Temperature == nil || *request.Temperature != 0.2) {+ t.Errorf("DeepSeek V4 temperature = %v, want 0.2", request.Temperature)+ }+ if want == "" && request.Temperature != nil {+ t.Errorf("model %q should use default sampling", model)+ }+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"id":"translated","choices":[{"finish_reason":"stop","message":{"role":"assistant","content":"完整译文"}}]}`))+ }))+ defer server.Close()+ if _, err := newClient(server.URL, "test-key", "", time.Second).ChatCompletion("translate"); err != nil {+ t.Fatal(err)+ }+ })+ }+}++func TestChatCompletionRejectsTruncatedResponse(t *testing.T) {+ for _, content := range []string{"", `{"sentences":[`} {+ t.Run(content, func(t *testing.T) {+ setTestModel(t)+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _, _ = fmt.Fprintf(w, `{"id":"truncated","choices":[{"finish_reason":"length","message":{"role":"assistant","content":%q}}]}`, content)+ }))+ defer server.Close()+ text, err := newClient(server.URL, "test-key", "", time.Second).ChatCompletion("translate")+ if text != "" || err == nil || !strings.Contains(err.Error(), "llm_translation_truncated") {+ t.Fatalf("截断响应不应作为译文或普通空响应返回: text=%q error=%v", text, err)+ }+ })}}@@ -64,11 +126,31 @@ func TestChatCompletionTimesOut(t *testing.T) {}}-func TestChatCompletionReportsStreamErrorAfterKeepAlive(t *testing.T) {+func TestChatCompletionUsesDefaultSampling(t *testing.T) {+ setTestModel(t)+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ var request map[string]json.RawMessage+ if err := json.NewDecoder(r.Body).Decode(&request); err != nil {+ t.Error(err)+ }+ if _, exists := request["temperature"]; exists {+ t.Error("default model should not send temperature")+ }+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"id":"default-sampling","choices":[{"message":{"role":"assistant","content":"完整译文"}}]}`))+ }))+ defer server.Close()+ if _, err := newClient(server.URL, "test-key", "", time.Second).ChatCompletion("translate"); err != nil {+ t.Fatal(err)+ }+}++func TestChatCompletionReportsAPIError(t *testing.T) {setTestModel(t)server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- w.Header().Set("Content-Type", "text/event-stream")- _, _ = w.Write([]byte("data: {\"choices\":[]}\n\ndata: {\"error\":{\"type\":\"upstream_error\",\"code\":\"official_service_unavailable\",\"message\":\"Official service request could not be completed\"}}\n\n"))+ w.Header().Set("Content-Type", "application/json")+ w.WriteHeader(http.StatusServiceUnavailable)+ _, _ = w.Write([]byte(`{"error":{"type":"upstream_error","code":"official_service_unavailable","message":"Official service request could not be completed"}}`))}))defer server.Close()diff --git a/runtime/krillinai/pkg/util/youtube_subtitle_helper.go b/runtime/krillinai/pkg/util/youtube_subtitle_helper.goindex 0591d39e..f866530c 100644--- a/runtime/krillinai/pkg/util/youtube_subtitle_helper.go+++ b/runtime/krillinai/pkg/util/youtube_subtitle_helper.go@@ -357,6 +357,7 @@ func ParseVttToWords(vttPath string) ([]Word, error) {var words []Wordscanner := bufio.NewScanner(file)+ scanner.Buffer(make([]byte, 4096), 4*1024*1024)var blockStartTime, blockEndTime float64wordNum := 0@@ -460,6 +461,7 @@ func ParseSrtFile(srtFilePath string) ([]*SrtBlock, error) {var srtBlocks []*SrtBlockscanner := bufio.NewScanner(file)+ scanner.Buffer(make([]byte, 4096), 4*1024*1024)var currentBlock []stringfor scanner.Scan() {