GigaChat: broaden leaked prelude stripping

This commit is contained in:
Alexander Davydov 2026-03-18 15:02:03 +03:00
parent 62faef905a
commit af83a32c9e
2 changed files with 7 additions and 3 deletions

View File

@ -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]",
]),

View File

@ -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 ──────────────────────────────────────────────