UNPKG

assistant-stream

Version:

Streaming utilities for AI assistants

28 lines (24 loc) • 819 B
import type { AssistantStreamChunk } from "../../AssistantStreamChunk"; // A controller throws TypeError once its stream is closed or cancelled; that // is the only portable signal a producer gets that the consumer went away. const isClosedControllerError = (error: unknown): error is TypeError => error instanceof TypeError; export const enqueueIfOpen = ( controller: { enqueue(chunk: AssistantStreamChunk): void }, chunk: AssistantStreamChunk, onDrop?: (error: TypeError) => void, ) => { try { controller.enqueue(chunk); } catch (error) { if (!isClosedControllerError(error)) throw error; onDrop?.(error); } }; export const closeIfOpen = (controller: { close(): void }) => { try { controller.close(); } catch (error) { if (!isClosedControllerError(error)) throw error; } };