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
fix(runtime): unify LLM JSON selection and reject empty results
Leggettca087d916405
diff --git a/runtime/krillinai/internal/service/audio2subtitle.go b/runtime/krillinai/internal/service/audio2subtitle.goindex 306402fa..f899bac7 100644--- a/runtime/krillinai/internal/service/audio2subtitle.go+++ b/runtime/krillinai/internal/service/audio2subtitle.go@@ -1876,10 +1876,16 @@ func (s Service) splitLongSentence(item *TranslatedItem) ([]*TranslatedItem, errlog.GetLogger().Error("splitLongSentence parse split result error", zap.Error(err), zap.Any("response", response))return nil, fmt.Errorf("parse split result error: %w", err)}+ if len(splitResult.Align) == 0 {+ return nil, fmt.Errorf("splitLongSentence returned no aligned segments")+ }// 转换为TranslatedItem切片var splitItems []*TranslatedItemfor _, part := range splitResult.Align {+ if strings.TrimSpace(part.OriginPart) == "" || strings.TrimSpace(part.TranslatedPart) == "" {+ return nil, fmt.Errorf("splitLongSentence returned an empty aligned segment")+ }splitItems = append(splitItems, &TranslatedItem{OriginText: part.OriginPart,TranslatedText: part.TranslatedPart,@@ -1916,10 +1922,22 @@ func (s Service) splitOriginLongSentence(sentence string) ([]string, error) {log.GetLogger().Error("splitOriginLongSentence parse split result error", zap.Error(err), zap.Any("response", response))continue}+ if len(splitResult.ShortSentences) == 0 {+ err = fmt.Errorf("splitOriginLongSentence returned no sentences")+ continue+ }+ shortSentences = shortSentences[:0]for _, shortSentence := range splitResult.ShortSentences {+ if strings.TrimSpace(shortSentence.Text) == "" {+ err = fmt.Errorf("splitOriginLongSentence returned an empty sentence")+ break+ }shortSentences = append(shortSentences, shortSentence.Text)}+ if err != nil {+ continue+ }break}diff --git a/runtime/krillinai/internal/service/audio2subtitle_test.go b/runtime/krillinai/internal/service/audio2subtitle_test.goindex 01a8c086..8eb69663 100644--- a/runtime/krillinai/internal/service/audio2subtitle_test.go+++ b/runtime/krillinai/internal/service/audio2subtitle_test.go@@ -554,3 +554,46 @@ func TestSplitLongSentenceStillFailsOnGarbage(t *testing.T) {t.Fatal("splitLongSentence() error = nil, want parse error")}}++func TestSplitLongSentenceSelectsAnswerAfterEmptyExample(t *testing.T) {+ log.InitLogger()+ completer := &scriptedCompleter{responses: []string{+ "示例格式:{\"align\":[]}\n实际结果:{\"align\":[{\"origin_part\":\"hello\",\"translated_part\":\"你好\"}]}",+ }}+ service := Service{ChatCompleter: completer}+ items, err := service.splitLongSentence(&TranslatedItem{OriginText: "hello", TranslatedText: "你好"})+ if err != nil || len(items) != 1 || items[0].TranslatedText != "你好" {+ t.Fatalf("split result = %+v, error = %v; want the actual translation", items, err)+ }+}++func TestSplitLongSentenceRejectsEmptyAnswer(t *testing.T) {+ log.InitLogger()+ service := Service{ChatCompleter: &scriptedCompleter{responses: []string{`{"align":[]}`}}}+ if items, err := service.splitLongSentence(&TranslatedItem{OriginText: "hello", TranslatedText: "你好"}); err == nil {+ t.Fatalf("empty split silently succeeded: %+v", items)+ }+}++func TestSplitOriginLongSentenceRetriesEmptyAnswer(t *testing.T) {+ log.InitLogger()+ completer := &scriptedCompleter{responses: []string{+ `{"short_sentences":null}`,+ `{"short_sentences":[{"text":"hello"}]}`,+ }}+ service := Service{ChatCompleter: completer}+ sentences, err := service.splitOriginLongSentence("hello world")+ if err != nil || len(sentences) != 1 || sentences[0] != "hello" || completer.calls != 2 {+ t.Fatalf("split = %v, calls = %d, error = %v", sentences, completer.calls, err)+ }+}++func TestSplitLongSentenceRejectsAmbiguousAnswers(t *testing.T) {+ log.InitLogger()+ service := Service{ChatCompleter: &scriptedCompleter{responses: []string{+ `{"align":[{"origin_part":"hello","translated_part":"你好"}]} {"align":[{"origin_part":"other","translated_part":"别的"}]}`,+ }}}+ if items, err := service.splitLongSentence(&TranslatedItem{OriginText: "hello", TranslatedText: "你好"}); err == nil {+ t.Fatalf("ambiguous split silently succeeded: %+v", items)+ }+}diff --git a/runtime/krillinai/internal/service/translate.go b/runtime/krillinai/internal/service/translate.goindex 1c56de26..432bee7d 100644--- a/runtime/krillinai/internal/service/translate.go+++ b/runtime/krillinai/internal/service/translate.go@@ -144,14 +144,24 @@ func (t *Translator) splitOriginLongSentence(sentence string) ([]string, error)log.GetLogger().Error("splitOriginLongSentence parse split result error", zap.Error(err), zap.Any("response", response))continue}+ if len(splitResult.ShortSentences) == 0 {+ err = fmt.Errorf("splitOriginLongSentence returned no sentences")+ continue+ }+ shortSentences = shortSentences[:0]for _, shortSentence := range splitResult.ShortSentences {// 清理文本,移除多余的引号cleanText := strings.TrimSpace(shortSentence.Text)cleanText = strings.Trim(cleanText, `"'`)- if cleanText != "" {- shortSentences = append(shortSentences, cleanText)+ if cleanText == "" {+ err = fmt.Errorf("splitOriginLongSentence returned an empty sentence")+ break}+ shortSentences = append(shortSentences, cleanText)+ }+ if err != nil {+ continue}break}@@ -760,10 +770,17 @@ Required JSON format (output ONLY this structure):// 提取翻译结果translations := make([]string, len(texts))+ var invalidResultErr errorfor _, trans := range result.Translations {- if trans.Index > 0 && trans.Index <= len(texts) {- translations[trans.Index-1] = strings.TrimSpace(trans.Text)+ if trans.Index <= 0 || trans.Index > len(texts) || translations[trans.Index-1] != "" || strings.TrimSpace(trans.Text) == "" {+ invalidResultErr = fmt.Errorf("翻译结果包含无效索引或空文本: %d", trans.Index)+ break}+ translations[trans.Index-1] = strings.TrimSpace(trans.Text)+ }+ if invalidResultErr != nil {+ lastErr = invalidResultErr+ continue}return translations, nildiff --git a/runtime/krillinai/internal/service/translate_test.go b/runtime/krillinai/internal/service/translate_test.goindex 1562cd27..2e29362c 100644--- a/runtime/krillinai/internal/service/translate_test.go+++ b/runtime/krillinai/internal/service/translate_test.go@@ -116,3 +116,46 @@ func TestBatchTranslateTextsRejectsDecoyObject(t *testing.T) {t.Fatalf("translations = %v, want %v", translations, want)}}++func TestTranslatorSplitOriginLongSentenceRetriesEmptyAnswer(t *testing.T) {+ log.InitLogger()+ completer := &scriptedCompleter{responses: []string{+ `{"short_sentences":[]}`,+ `{"short_sentences":[{"text":"hello"}]}`,+ }}+ translator := &Translator{chatCompleter: completer}+ sentences, err := translator.splitOriginLongSentence("hello world")+ if err != nil || len(sentences) != 1 || sentences[0] != "hello" || completer.calls != 2 {+ t.Fatalf("split = %v, calls = %d, error = %v", sentences, completer.calls, err)+ }+}++func TestBatchTranslateTextsRejectsEmptyExampleAndRetriesEmptyText(t *testing.T) {+ log.InitLogger()+ completer := &scriptedCompleter{responses: []string{+ `{"translations":[]} actual: {"translations":[{"index":1,"text":""}]}`,+ `{"translations":[{"index":1,"text":"你好"}]}`,+ }}+ translator := &Translator{chatCompleter: completer}+ translations, err := translator.batchTranslateTexts(+ []string{"hello"}, types.StandardLanguageCode("en"), types.StandardLanguageCode("zh_cn"),+ )+ if err != nil || len(translations) != 1 || translations[0] != "你好" || completer.calls != 2 {+ t.Fatalf("translations = %v, calls = %d, error = %v", translations, completer.calls, err)+ }+}++func TestBatchTranslateTextsRetriesDuplicateIndex(t *testing.T) {+ log.InitLogger()+ completer := &scriptedCompleter{responses: []string{+ `{"translations":[{"index":1,"text":"一"},{"index":1,"text":"二"}]}`,+ `{"translations":[{"index":1,"text":"一"},{"index":2,"text":"二"}]}`,+ }}+ translator := &Translator{chatCompleter: completer}+ translations, err := translator.batchTranslateTexts(+ []string{"one", "two"}, types.StandardLanguageCode("en"), types.StandardLanguageCode("zh_cn"),+ )+ if err != nil || len(translations) != 2 || translations[0] != "一" || translations[1] != "二" || completer.calls != 2 {+ t.Fatalf("translations = %v, calls = %d, error = %v", translations, completer.calls, err)+ }+}diff --git a/runtime/krillinai/pkg/util/base.go b/runtime/krillinai/pkg/util/base.goindex 3ddc76eb..edde200b 100644--- a/runtime/krillinai/pkg/util/base.go+++ b/runtime/krillinai/pkg/util/base.go@@ -356,11 +356,6 @@ func unwrapFenceInfo(line string) string {return info}-// CleanLLMJSON 清理大模型返回的 JSON 文本:取围栏代码块的内容,并移除结尾多余逗号-func CleanLLMJSON(response string) string {- return StripJSONTrailingCommas(CleanMarkdownCodeBlock(response))-}-// StripJSONTrailingCommas 移除 JSON 中对象或数组结尾的多余逗号,如 {"a": 1,} -> {"a": 1}// 字符串字面量内部的逗号会被保留func StripJSONTrailingCommas(jsonText string) string {@@ -402,7 +397,7 @@ func StripJSONTrailingCommas(jsonText string) string {func closesAfterWhitespace(jsonText string, from int) bool {for i := from; i < len(jsonText); i++ {switch jsonText[i] {- case ' ', '\t', '\n', '\r':+ case ' ', '\t', '\n', '\r', ',':continuecase '}', ']':return truediff --git a/runtime/krillinai/pkg/util/base_test.go b/runtime/krillinai/pkg/util/base_test.goindex 64c19f00..7c0880a8 100644--- a/runtime/krillinai/pkg/util/base_test.go+++ b/runtime/krillinai/pkg/util/base_test.go@@ -156,7 +156,7 @@ func TestStripJSONTrailingCommas(t *testing.T) {// 大模型经常在 ```json 代码块前后附加说明文字,并偶尔输出结尾多余逗号,// 两者都会让 json.Unmarshal 失败,见 issue #291-func TestCleanLLMJSON(t *testing.T) {+func TestExtractJSONObjectSanitizesFencedReply(t *testing.T) {response := "以下是分割后的结果:\n\n```json\n{\n \"align\": [\n {\"origin_part\": \"a\", \"translated_part\": \"A\",},\n ],\n}\n```\n\n希望有帮助。"var splitResult struct {@@ -165,8 +165,9 @@ func TestCleanLLMJSON(t *testing.T) {TranslatedPart string `json:"translated_part"`} `json:"align"`}- if err := json.Unmarshal([]byte(CleanLLMJSON(response)), &splitResult); err != nil {- t.Fatalf("CleanLLMJSON() produced unparsable JSON %q: %v", CleanLLMJSON(response), err)+ cleaned := ExtractJSONObject(response, "align")+ if err := json.Unmarshal([]byte(cleaned), &splitResult); err != nil {+ t.Fatalf("ExtractJSONObject() produced unparsable JSON %q: %v", cleaned, err)}if len(splitResult.Align) != 1 {t.Fatalf("expected 1 aligned part, got %d", len(splitResult.Align))diff --git a/runtime/krillinai/pkg/util/json.go b/runtime/krillinai/pkg/util/json.goindex f8791e54..8e054d47 100644--- a/runtime/krillinai/pkg/util/json.go+++ b/runtime/krillinai/pkg/util/json.go@@ -9,12 +9,11 @@ import (// 本地模型(如通过 Ollama 运行的 llama3.1)经常在 JSON 前后附带对话式说明,// 或在 } 、] 之前多输出一个逗号,这两种情况都会让 encoding/json 解析失败。// 说明里还可能带上示例或解释用的 JSON(如 {} 或 {"note":"..."}),-// 因此这里按顺序扫描文本中的每个完整 JSON 值,只接受顶层带 requiredKey 的对象,-// 避免把示例当成模型真正的回答,从而解析出一个空结果。-// 若文本中没有这样的对象,则原样返回去除首尾空白的输入,-// 让调用方拿到真实的解析错误并走原有的重试逻辑。+// 因此这里扫描所有完整 JSON 值,跳过空的示例;多个有效回答则保留原始输入,+// 让调用方走现有的解析错误路径,而不是猜测一个结果。func ExtractJSONObject(response, requiredKey string) string {- trimmed := CleanMarkdownCodeBlock(response)+ trimmed := strings.TrimSpace(response)+ var selected stringfor offset := 0; offset < len(trimmed); {value, end, ok := extractFirstJSONValue(trimmed, offset)@@ -22,18 +21,24 @@ func ExtractJSONObject(response, requiredKey string) string {break}if hasTopLevelKey(value, requiredKey) {- return value+ if selected != "" {+ return trimmed+ }+ selected = value}offset = end}- return trimmed+ if selected != "" {+ return selected+ }+ return CleanMarkdownCodeBlock(response)}// extractFirstJSONValue 从 from 开始找到第一个 { 或 [,按嵌套深度取到匹配的结束符,// 返回该 JSON 值、它在 s 中结束后的下一个位置,以及是否找到完整结构。-// 扫描会跳过字符串字面量中的内容,因此正文里的括号、引号和逗号不会被破坏;-// 结束符前的尾随逗号会被去掉。结构不完整(缺少结束符)时不做猜测。+// 扫描会跳过字符串字面量中的内容,因此正文里的括号、引号和逗号不会被破坏。+// 结构不完整(缺少结束符)时不做猜测。func extractFirstJSONValue(s string, from int) (string, int, bool) {start := strings.IndexAny(s[from:], "{[")if start < 0 {@@ -41,17 +46,12 @@ func extractFirstJSONValue(s string, from int) (string, int, bool) {}start += from- var (- builder strings.Builder- depth int- inString bool- escaped bool- )+ var depth int+ var inString, escaped boolfor i := start; i < len(s); i++ {c := s[i]if inString {- builder.WriteByte(c)switch {case escaped:escaped = false@@ -69,14 +69,11 @@ func extractFirstJSONValue(s string, from int) (string, int, bool) {case '{', '[':depth++case '}', ']':- trimTrailingComma(&builder)depth--}- builder.WriteByte(c)-if depth == 0 {- return builder.String(), i + 1, true+ return StripJSONTrailingCommas(s[start : i+1]), i + 1, true}}@@ -90,18 +87,10 @@ func hasTopLevelKey(value, key string) bool {if err := json.Unmarshal([]byte(value), &fields); err != nil {return false}- _, ok := fields[key]- return ok-}--// trimTrailingComma 去掉已写入内容末尾的逗号(含其后的空白),用于容忍结束符前的尾随逗号。-// 末尾没有逗号时保持原内容不变,避免改动合法 JSON 的缩进。-func trimTrailingComma(builder *strings.Builder) {- current := builder.String()- cleaned := strings.TrimRight(current, " \t\r\n,")- if !strings.Contains(current[len(cleaned):], ",") {- return+ field, ok := fields[key]+ if !ok {+ return false}- builder.Reset()- builder.WriteString(cleaned)+ valueText := strings.TrimSpace(string(field))+ return valueText != "null" && valueText != "[]"}diff --git a/runtime/krillinai/pkg/util/json_test.go b/runtime/krillinai/pkg/util/json_test.goindex 1de64caa..e13524e1 100644--- a/runtime/krillinai/pkg/util/json_test.go+++ b/runtime/krillinai/pkg/util/json_test.go@@ -25,13 +25,13 @@ func TestExtractJSONObject(t *testing.T) {name: "object trailing comma",response: "{\n\"short_sentences\":[{\n\"text\": \"the owl is a symbol\",\n\t}] \n}",key: "short_sentences",- want: "{\n\"short_sentences\":[{\n\"text\": \"the owl is a symbol\"}] \n}",+ want: "{\n\"short_sentences\":[{\n\"text\": \"the owl is a symbol\"\n\t}] \n}",},{name: "array trailing comma",response: `{"short_sentences":[{"text":"a"},{"text":"b"}, ]}`,key: "short_sentences",- want: `{"short_sentences":[{"text":"a"},{"text":"b"}]}`,+ want: `{"short_sentences":[{"text":"a"},{"text":"b"} ]}`,},{name: "nested trailing commas",@@ -43,7 +43,7 @@ func TestExtractJSONObject(t *testing.T) {name: "crlf trailing comma",response: "{\r\n\"a\": 1,\r\n}",key: "a",- want: "{\r\n\"a\": 1}",+ want: "{\r\n\"a\": 1\r\n}",},{name: "fence",@@ -58,22 +58,40 @@ func TestExtractJSONObject(t *testing.T) {want: `{"translations":[{"index":1,"text":"一"}]}`,},{- name: "first carrier",+ name: "ambiguous carriers",response: `{"align":[{"origin_part":"a"}]} {"align":[{"origin_part":"b"}]}`,key: "align",- want: `{"align":[{"origin_part":"a"}]}`,+ want: `{"align":[{"origin_part":"a"}]} {"align":[{"origin_part":"b"}]}`,},{name: "empty value",response: `{"align":[]} {"align":[{"origin_part":"a"}]}`,key: "align",- want: `{"align":[]}`,+ want: `{"align":[{"origin_part":"a"}]}`,},{name: "null value",response: `{"align":null} {"align":[{"origin_part":"a"}]}`,key: "align",- want: `{"align":null}`,+ want: `{"align":[{"origin_part":"a"}]}`,+ },+ {+ name: "first fence is an unrelated example",+ response: "```json\n{\"example\":[]}\n```\n实际结果:\n```json\n{\"align\":[{\"origin_part\":\"a\"}]}\n```",+ key: "align",+ want: `{"align":[{"origin_part":"a"}]}`,+ },+ {+ name: "first fence has an empty example",+ response: "```json\n{\"align\":[]}\n```\n实际结果:\n```json\n{\"align\":[{\"origin_part\":\"a\"}]}\n```",+ key: "align",+ want: `{"align":[{"origin_part":"a"}]}`,+ },+ {+ name: "multiple valid fences remain ambiguous",+ response: "```json\n{\"align\":[{\"origin_part\":\"a\"}]}\n```\n```json\n{\"align\":[{\"origin_part\":\"b\"}]}\n```",+ key: "align",+ want: "```json\n{\"align\":[{\"origin_part\":\"a\"}]}\n```\n```json\n{\"align\":[{\"origin_part\":\"b\"}]}\n```",},{name: "key case",