Skip to content

Commit

Permalink
fix: edge runtime json parsing bug (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Jul 11, 2024
1 parent 1debfdd commit eacec25
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/react/src/runtimes/edge/streams/assistantDecoderStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ export function assistantDecoderStream() {

return new TransformStream<string, LanguageModelV1StreamPart>({
transform(chunk, controller) {
const [code, valueJson] = chunk.split(":") as [
AssistantStreamChunkType,
string,
];
const [code, valueJson] = parseStreamPart(chunk);
const value = JSON.parse(valueJson);

if (
Expand Down Expand Up @@ -76,3 +73,12 @@ export function assistantDecoderStream() {
},
});
}

const parseStreamPart = (part: string): [AssistantStreamChunkType, string] => {
const index = part.indexOf(":");
if (index === -1) throw new Error("Invalid stream part");
return [
part.slice(0, index) as AssistantStreamChunkType,
part.slice(index + 1),
] as const;
};

0 comments on commit eacec25

Please sign in to comment.