Просмотр исходного кода

fix: fall back on empty length normalization wrappers

Ma 7 месяцев назад
Родитель
Сommit
e38446e866

+ 29 - 0
packages/core/src/__tests__/length-normalizer.test.ts

@@ -165,4 +165,33 @@ describe("LengthNormalizerAgent", () => {
     expect(result.normalizedContent).not.toContain("我先压缩一下正文");
     expect(result.normalizedContent).not.toContain("我先压缩一下正文");
     expect(result.normalizedContent).not.toContain("```");
     expect(result.normalizedContent).not.toContain("```");
   });
   });
+
+  it("falls back to the original chapter when the response contains only wrappers", async () => {
+    const agent = createAgent();
+    const chatSpy = vi.spyOn(BaseAgent.prototype as never, "chat").mockResolvedValue({
+      content: "我先压缩一下正文。",
+      usage: ZERO_USAGE,
+    });
+    const lengthSpec = LengthSpecSchema.parse({
+      target: 220,
+      softMin: 190,
+      softMax: 250,
+      hardMin: 160,
+      hardMax: 280,
+      countingMode: "zh_chars",
+      normalizeMode: "compress",
+    });
+    const draft = "开头。" + "冗余句子。".repeat(40) + "[[KEEP_ME]]";
+
+    const result = await agent.normalizeChapter({
+      chapterContent: draft,
+      lengthSpec,
+      chapterIntent: "Preserve [[KEEP_ME]] only.",
+      reducedControlBlock: "No extra commentary.",
+    });
+
+    expect(chatSpy).toHaveBeenCalledTimes(1);
+    expect(result.normalizedContent).toBe(draft);
+    expect(result.finalCount).toBe(countChapterLength(draft, "zh_chars"));
+  });
 });
 });

+ 1 - 1
packages/core/src/agents/length-normalizer.ts

@@ -139,7 +139,7 @@ ${input.chapterContent}`;
 
 
     if (this.looksLikeResponseWrapper(trimmed)) {
     if (this.looksLikeResponseWrapper(trimmed)) {
       const stripped = this.stripCommonWrappers(trimmed);
       const stripped = this.stripCommonWrappers(trimmed);
-      if (stripped) return stripped;
+      return stripped || fallbackContent;
     }
     }
 
 
     return trimmed;
     return trimmed;