Skip to content

Commit

Permalink
chore(internal): minor streaming updates (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Jan 23, 2024
1 parent 156084b commit 2f073e4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {

// Note: there is an invariant here that isn't represented in the type system
// that if you set `stream: true` the response type must also be `Stream<T>`

if (props.options.__streamClass) {
return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;
}

return Stream.fromSSEResponse(response, props.controller) as any;
}

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

__binaryResponse?: boolean | undefined;
__streamClass?: typeof Stream;
};

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

__binaryResponse: true,
__streamClass: true,
};

export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
Expand Down
6 changes: 3 additions & 3 deletions src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { OpenAIError } from './error';

import { APIError } from 'openai/error';

type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
export type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;

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

const reader = stream.getReader();
Expand Down

0 comments on commit 2f073e4

Please sign in to comment.