diff --git a/src/routes/chat-completions/handler.ts b/src/routes/chat-completions/handler.ts index 04a5ae9ed..0a475b6bb 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,18 @@ export async function handleCompletion(c: Context) { const isNonStreaming = ( response: Awaited>, ): response is ChatCompletionResponse => Object.hasOwn(response, "choices") + +const isFilterResult = (response: string): 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 + ) +}