ai
Version:
AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript
17 lines (16 loc) • 394 B
text/typescript
export async function processTextStream({
stream,
onTextPart,
}: {
stream: ReadableStream<Uint8Array>;
onTextPart: (chunk: string) => Promise<void> | void;
}): Promise<void> {
const reader = stream.pipeThrough(new TextDecoderStream()).getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
await onTextPart(value);
}
}