-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: streaming JSON types and error reporting tests (#315)
Co-authored-by: Adam Howard <[email protected]>
- Loading branch information
1 parent
ba5618e
commit 3e18b44
Showing
4 changed files
with
76 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,47 @@ | ||
import { z } from "zod"; | ||
|
||
const UnknownStreamMessageSchema = z | ||
.object({ | ||
type: z.string(), | ||
}) | ||
.passthrough(); | ||
|
||
type UnknownStreamMessage = z.infer<typeof UnknownStreamMessageSchema>; | ||
export async function consumeStream( | ||
stream: ReadableStream<Uint8Array>, | ||
): Promise<string> { | ||
const reader = stream.getReader(); | ||
let result = ""; | ||
let isComplete = false; | ||
const maxTimeout = 30000; // 30 seconds timeout | ||
const startTime = Date.now(); | ||
|
||
while (true) { | ||
while (!isComplete && Date.now() - startTime < maxTimeout) { | ||
const { value, done } = await reader.read(); | ||
if (done) break; | ||
if (done) { | ||
isComplete = true; | ||
break; | ||
} | ||
if (value) { | ||
result += new TextDecoder().decode(value); | ||
} | ||
} | ||
|
||
if (!isComplete) { | ||
throw new Error("Stream reading timed out"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function extractStreamMessage(streamedText: string) { | ||
export function extractStreamMessage( | ||
streamedText: string, | ||
): UnknownStreamMessage { | ||
const content = streamedText.match(/0:"(.*)"/); | ||
if (!content?.[1]) { | ||
throw new Error("No message found in streamed text"); | ||
} | ||
const strippedContent = content[1].replace(/\\"/g, '"'); | ||
return JSON.parse(strippedContent); | ||
const parsedMessage = JSON.parse(strippedContent) as unknown; | ||
return UnknownStreamMessageSchema.parse(parsedMessage); | ||
} |