Skip to content

Commit

Permalink
feat: assistant fromReadableStream (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Mar 28, 2024
1 parent 1b1d357 commit 3dcaa34
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/lib/AssistantStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,32 @@ export class AssistantStream
};
}

static fromReadableStream(stream: ReadableStream): AssistantStream {
const runner = new AssistantStream();
runner._run(() => runner._fromReadableStream(stream));
return runner;
}

protected async _fromReadableStream(
readableStream: ReadableStream,
options?: Core.RequestOptions,
): Promise<Run> {
const signal = options?.signal;
if (signal) {
if (signal.aborted) this.controller.abort();
signal.addEventListener('abort', () => this.controller.abort());
}
this._connected();
const stream = Stream.fromReadableStream<AssistantStreamEvent>(readableStream, this.controller);
for await (const event of stream) {
this.#handleEvent(event);
}
if (stream.controller.signal?.aborted) {
throw new APIUserAbortError();
}
return this._addRun(this.#endRequest());
}

toReadableStream(): ReadableStream {
const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);
return stream.toReadableStream();
Expand Down Expand Up @@ -385,7 +411,7 @@ export class AssistantStream
throw new OpenAIError(`stream has ended, this shouldn't happen`);
}

if (!this.#finalRun) throw Error('Final run has been been received');
if (!this.#finalRun) throw Error('Final run has not been received');

return this.#finalRun;
}
Expand Down

0 comments on commit 3dcaa34

Please sign in to comment.