From af83a32c9efe70593112e4dded0f9f61f08c6616 Mon Sep 17 00:00:00 2001 From: Alexander Davydov Date: Wed, 18 Mar 2026 15:02:03 +0300 Subject: [PATCH] GigaChat: broaden leaked prelude stripping --- src/agents/gigachat-stream.leaked-prelude.test.ts | 8 ++++++-- src/agents/gigachat-stream.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/agents/gigachat-stream.leaked-prelude.test.ts b/src/agents/gigachat-stream.leaked-prelude.test.ts index 251516566c1..e41c852f4d2 100644 --- a/src/agents/gigachat-stream.leaked-prelude.test.ts +++ b/src/agents/gigachat-stream.leaked-prelude.test.ts @@ -22,11 +22,15 @@ function createSseStream(lines: string[]): Readable { } describe("GigaChat leaked function-call prelude cleanup", () => { - it("drops leaked assistant text preludes when a function call is present", async () => { + it.each([ + "assistant function callrecipient{", + "assistant function callweather{", + "assistant function call recipient {", + ])("drops leaked assistant text prelude %p when a function call is present", async (prelude) => { request.mockResolvedValueOnce({ status: 200, data: createSseStream([ - 'data: {"choices":[{"delta":{"content":"assistant function callrecipient{","role":"assistant"},"index":0}]}', + `data: {"choices":[{"delta":{"content":"${prelude}","role":"assistant"},"index":0}]}`, 'data: {"choices":[{"delta":{"content":"","role":"assistant","function_call":{"name":"message","arguments":"{\\"action\\":\\"send\\",\\"message\\":\\"hello\\"}"}},"index":0}]}', "data: [DONE]", ]), diff --git a/src/agents/gigachat-stream.ts b/src/agents/gigachat-stream.ts index 27fb5339dd6..504fe61a0a0 100644 --- a/src/agents/gigachat-stream.ts +++ b/src/agents/gigachat-stream.ts @@ -40,7 +40,7 @@ export type GigachatStreamOptions = { }; function stripLeakedFunctionCallPrelude(text: string): string { - return text.replace(/^\s*assistant\s+function\s+call(?:recipient)?\{\s*/i, ""); + return text.replace(/^\s*assistant\s+function\s+call(?:\s*([A-Za-z0-9_.:/-]+))?\s*\{\s*/i, ""); } // ── Function name sanitization ──────────────────────────────────────────────