From 1475ec2eefb859e253af592cee09f2c01b72a399 Mon Sep 17 00:00:00 2001 From: "Eason(G Ray)" <30045503+Eason0729@users.noreply.github.com> Date: Fri, 17 Oct 2025 23:37:00 +0800 Subject: [PATCH 1/2] fix: check if chunk is filterResult --- src/routes/chat-completions/handler.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/routes/chat-completions/handler.ts b/src/routes/chat-completions/handler.ts index 04a5ae9ed..53516e320 100644 --- a/src/routes/chat-completions/handler.ts +++ b/src/routes/chat-completions/handler.ts @@ -58,7 +58,12 @@ export async function handleCompletion(c: Context) { return streamSSE(c, async (stream) => { for await (const chunk of response) { consola.debug("Streaming chunk:", JSON.stringify(chunk)) - await stream.writeSSE(chunk as SSEMessage) + + const typedChunk = chunk as SSEMessage + + if (isFilterResult(typedChunk.data)) continue + + await stream.writeSSE(typedChunk) } }) } @@ -66,3 +71,20 @@ export async function handleCompletion(c: Context) { const isNonStreaming = ( response: Awaited>, ): response is ChatCompletionResponse => Object.hasOwn(response, "choices") + +const isFilterResult = ( + response: Awaited>, +): boolean => { + let parsedData: { choice?: Array } + try { + parsedData = JSON.parse(response) as { choice?: Array } + } catch { + return false + } + + return ( + !("choices" in parsedData) + || !Array.isArray(parsedData.choices) + || parsedData.choices.length <= 0 + ) +} From 1f70088cf4912d2bf886858f4075b6a443c6309a Mon Sep 17 00:00:00 2001 From: "Eason(G Ray)" <30045503+Eason0729@users.noreply.github.com> Date: Fri, 17 Oct 2025 23:43:38 +0800 Subject: [PATCH 2/2] fix: type --- src/routes/chat-completions/handler.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/routes/chat-completions/handler.ts b/src/routes/chat-completions/handler.ts index 53516e320..0a475b6bb 100644 --- a/src/routes/chat-completions/handler.ts +++ b/src/routes/chat-completions/handler.ts @@ -72,9 +72,7 @@ const isNonStreaming = ( response: Awaited>, ): response is ChatCompletionResponse => Object.hasOwn(response, "choices") -const isFilterResult = ( - response: Awaited>, -): boolean => { +const isFilterResult = (response: string): boolean => { let parsedData: { choice?: Array } try { parsedData = JSON.parse(response) as { choice?: Array }