UNPKG

assistant-stream

Version:

Streaming utilities for AI assistants

62 lines (61 loc) • 1.87 kB
import { createControllerStreamPair } from "../utils/stream/createControllerStream.js"; import { closeIfOpen, enqueueIfOpen } from "../utils/stream/controller-guards.js"; // src/core/modules/text.ts var TextStreamControllerImpl = class { _controller; _strict; _isClosed = false; _ignoreAppends = false; _warnedDropped = false; constructor(controller, options = {}) { this._controller = controller; this._strict = options.strict ?? true; } append(textDelta, providerMetadata) { if (this._ignoreAppends) return this; const chunk = { type: "text-delta", path: [], textDelta, ...providerMetadata != null && { providerMetadata } }; if (this._isClosed) { if (this._strict) throw new TypeError("Cannot append to a closed TextStreamController"); enqueueIfOpen(this._controller, chunk, this._warnDroppedAfterClose); return this; } enqueueIfOpen(this._controller, chunk); return this; } _warnDroppedAfterClose = (error) => { if (this._warnedDropped) return; this._warnedDropped = true; console.error(`Dropped text delta for closed stream: ${String(error)}`); }; close(providerMetadata) { if (this._isClosed) return; this._isClosed = true; enqueueIfOpen(this._controller, { type: "part-finish", path: [], ...providerMetadata != null && { providerMetadata } }); closeIfOpen(this._controller); } __internal_close() { if (this._isClosed) return; this._ignoreAppends = true; this.close(); } __internal_truncate() { if (this._isClosed) return; this._ignoreAppends = true; this._isClosed = true; closeIfOpen(this._controller); } }; const createTextStreamController = (options = {}) => { return createControllerStreamPair((controller) => new TextStreamControllerImpl(controller, options)); }; export { TextStreamControllerImpl, createTextStreamController }; //# sourceMappingURL=text.js.map