Skip to content

Commit

Permalink
[Observability] Buffer partial chunks when streaming (#160124)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar authored Jun 21, 2023
1 parent 293a70b commit aa704e9
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]');

Expand Down

0 comments on commit aa704e9

Please sign in to comment.