Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(internal): minor streaming updates #264

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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