fix(agent): stabilize stream token output
This commit is contained in:
@@ -19,14 +19,9 @@ const createEventStream = (events: unknown[]) => ({
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
describe("streamPromptResponse", () => {
|
||||
it("detects Chinese punctuation chunks for smooth token output", () => {
|
||||
expect(detectTokenSmoothingChunk("管网压力异常,需要继续分析", "zh")).toBe(
|
||||
"管网压力异常,",
|
||||
);
|
||||
|
||||
expect(detectTokenSmoothingChunk("管网压力异常需要继续分析。后续排查阀门", "zh")).toBe(
|
||||
"管网压力异常需要继续分析。",
|
||||
);
|
||||
it("does not split short Chinese text only because it has punctuation", () => {
|
||||
expect(detectTokenSmoothingChunk("管网压力异常,需要继续分析", "zh")).toBeNull();
|
||||
expect(detectTokenSmoothingChunk("管网压力异常需要继续分析。", "zh")).toBeNull();
|
||||
});
|
||||
|
||||
it("does not immediately release a single Chinese character", () => {
|
||||
@@ -67,7 +62,7 @@ describe("streamPromptResponse", () => {
|
||||
expect(events.map((item) => item.data.content)).toEqual(["第一段", "第二段"]);
|
||||
});
|
||||
|
||||
it("buffers Chinese single-character deltas until punctuation", async () => {
|
||||
it("buffers Chinese single-character deltas until a bounded chunk is available", async () => {
|
||||
const events: Array<{ event: string; data: Record<string, unknown> }> = [];
|
||||
const smoother = createTokenSmoother({
|
||||
enabled: true,
|
||||
@@ -81,13 +76,15 @@ describe("streamPromptResponse", () => {
|
||||
await sleep(8);
|
||||
expect(events).toHaveLength(0);
|
||||
|
||||
for (const char of Array.from("网压力异常,")) {
|
||||
const content = "网压力异常需要继续分析东部主干供水走廊和相关阀门状态";
|
||||
for (const char of Array.from(content)) {
|
||||
smoother.writeToken(char);
|
||||
}
|
||||
await sleep(20);
|
||||
|
||||
expect(events.map((item) => item.data.content).join("")).toBe("管网压力异常,");
|
||||
expect(events.length).toBeGreaterThan(0);
|
||||
await smoother.flush();
|
||||
expect(events.map((item) => item.data.content).join("")).toBe(`管${content}`);
|
||||
});
|
||||
|
||||
it("smooths long Chinese text without waiting for paragraph-sized buffers", async () => {
|
||||
|
||||
Reference in New Issue
Block a user