Skip to content

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.

fix: prioritize cancelled optimizer context

wuliend086de96f8b0
diff --git a/internal/service/dubbing/estimator_test.go b/internal/service/dubbing/estimator_test.go
index 713f0b11..5ad84b5b 100644
--- a/internal/service/dubbing/estimator_test.go
+++ b/internal/service/dubbing/estimator_test.go
@@ -55,3 +55,26 @@ func TestHeuristicFallbackReturnsLowConfidence(t *testing.T) {
t.Fatalf("seconds=%v confidence=%v", seconds, confidence)
}
}
+
+func TestStatisticalEstimatorSupportsRequiredProfiles(t *testing.T) {
+ est := NewStatisticalEstimator()
+ languages := []types.StandardLanguageCode{
+ types.LanguageNameSimplifiedChinese,
+ types.LanguageNameTraditionalChinese,
+ types.LanguageNameJapanese,
+ types.LanguageNameKorean,
+ types.LanguageNameEnglish,
+ types.LanguageNameGerman,
+ types.LanguageNameRussian,
+ types.LanguageNameTurkish,
+ }
+ for _, language := range languages {
+ seconds, confidence, err := est.Estimate("hello 世界", language)
+ if err != nil {
+ t.Fatalf("Estimate(%s) error = %v", language, err)
+ }
+ if seconds <= 0 || confidence < 0.75 {
+ t.Fatalf("Estimate(%s) seconds=%v confidence=%v", language, seconds, confidence)
+ }
+ }
+}
diff --git a/internal/service/dubbing/optimizer.go b/internal/service/dubbing/optimizer.go
index b5d47fc3..dd017540 100644
--- a/internal/service/dubbing/optimizer.go
+++ b/internal/service/dubbing/optimizer.go
@@ -16,14 +16,14 @@ func NewLLMOptimizer(chat types.ChatCompleter) *LLMOptimizer {
}
func (o *LLMOptimizer) Optimize(ctx context.Context, text string, availableSeconds float64, reason string) (string, error) {
- if o == nil || o.chat == nil {
- return text, nil
- }
if ctx != nil {
if err := ctx.Err(); err != nil {
return "", err
}
}
+ if o == nil || o.chat == nil {
+ return text, nil
+ }
prompt := fmt.Sprintf(`请把下面字幕改写成更自然、更短、 更适合口播的一句话。
要求:
1. 保留核心含义,不添加新事实。
diff --git a/internal/service/dubbing/optimizer_test.go b/internal/service/dubbing/optimizer_test.go
index 0232c939..7ca22b19 100644
--- a/internal/service/dubbing/optimizer_test.go
+++ b/internal/service/dubbing/optimizer_test.go
@@ -54,3 +54,11 @@ func TestLLMOptimizerHandlesNilAndCancelledContext(t *testing.T) {
t.Fatalf("cancelled context should return error")
}
}
+
+func TestLLMOptimizerCancelledContextWinsBeforeNilChat(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ if _, err := NewLLMOptimizer(nil).Optimize(ctx, "原文", 1.2, "test"); err == nil {
+ t.Fatalf("cancelled context with nil chat should return error")
+ }
+}