Skip to content

Commit

Permalink
chore(internal): minor streaming updates (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Jan 23, 2024
1 parent bc4f115 commit d4414ff
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
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
Expand Up @@ -4,9 +4,9 @@ import { AnthropicError } from './error';
import { safeJSON, createResponseHeaders } from '@anthropic-ai/sdk/core';
import { APIError } from '@anthropic-ai/sdk/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 @@ -396,7 +396,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 d4414ff

Please sign in to comment.