Skip to content

Commit 8d9eeeb

Browse files
committed
chore(internal): minor streaming updates (#647)
1 parent fff5046 commit 8d9eeeb

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
4444

4545
// Note: there is an invariant here that isn't represented in the type system
4646
// that if you set `stream: true` the response type must also be `Stream<T>`
47+
48+
if (props.options.__streamClass) {
49+
return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;
50+
}
51+
4752
return Stream.fromSSEResponse(response, props.controller) as any;
4853
}
4954

@@ -743,6 +748,7 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
743748
idempotencyKey?: string;
744749

745750
__binaryResponse?: boolean | undefined;
751+
__streamClass?: typeof Stream;
746752
};
747753

748754
// This is required so that we can determine if a given object matches the RequestOptions
@@ -763,6 +769,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
763769
idempotencyKey: true,
764770

765771
__binaryResponse: true,
772+
__streamClass: true,
766773
};
767774

768775
export const isRequestOptions = (obj: unknown): obj is RequestOptions => {

src/streaming.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { OpenAIError } from './error';
33

44
import { APIError } from 'openai/error';
55

6-
type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
6+
export type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
77

8-
type ServerSentEvent = {
8+
export type ServerSentEvent = {
99
event: string | null;
1010
data: string;
1111
raw: string[];
@@ -381,7 +381,7 @@ function partition(str: string, delimiter: string): [string, string, string] {
381381
*
382382
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
383383
*/
384-
function readableStreamAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
384+
export function readableStreamAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
385385
if (stream[Symbol.asyncIterator]) return stream;
386386

387387
const reader = stream.getReader();

0 commit comments

Comments
 (0)