From aa704e9f389b09d2d6a5f66ccc23532150fffd11 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Wed, 21 Jun 2023 18:46:14 +0200 Subject: [PATCH] [Observability] Buffer partial chunks when streaming (#160124) --- .../create_co_pilot_service.ts | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/observability/public/context/co_pilot_context/create_co_pilot_service.ts b/x-pack/plugins/observability/public/context/co_pilot_context/create_co_pilot_service.ts index 07b1e3b33cf27..c493bff4e1df8 100644 --- a/x-pack/plugins/observability/public/context/co_pilot_context/create_co_pilot_service.ts +++ b/x-pack/plugins/observability/public/context/co_pilot_context/create_co_pilot_service.ts @@ -48,6 +48,8 @@ export function createCoPilotService({ enabled, http }: { enabled: boolean; http const chunks: CreateChatCompletionResponseChunk[] = []; + let prev: string = ''; + function read() { reader!.read().then(({ done, value }) => { try { @@ -61,10 +63,20 @@ export function createCoPilotService({ enabled, http }: { enabled: boolean; http return; } - const lines = decoder - .decode(value) - .trim() - .split('\n') + let lines = (prev + decoder.decode(value)).split('\n'); + + const lastLine = lines[lines.length - 1]; + + const isPartialChunk = !!lastLine && lastLine !== 'data: [DONE]'; + + if (isPartialChunk) { + prev = lastLine; + lines.pop(); + } else { + prev = ''; + } + + lines = lines .map((str) => str.substr(6)) .filter((str) => !!str && str !== '[DONE]');