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

refactor(client)!: remove Stream.toReadableStream() #110

Merged
merged 1 commit into from
Aug 11, 2023
Merged
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
33 changes: 0 additions & 33 deletions src/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Response } from '@anthropic-ai/sdk/_shims/fetch';
import { ReadableStream } from '@anthropic-ai/sdk/_shims/ReadableStream';

import { safeJSON, createResponseHeaders } from '@anthropic-ai/sdk/core';
import { APIError } from '@anthropic-ai/sdk/error';
Expand Down Expand Up @@ -81,38 +80,6 @@ export class Stream<Item> implements AsyncIterable<Item> {
if (!done) this.controller.abort();
}
}

toReadableStream(): ReadableStream {
const self = this;
let iter: AsyncIterator<Item>;
const encoder = new TextEncoder();

return new ReadableStream({
async start() {
iter = self[Symbol.asyncIterator]();
},
async pull(ctrl) {
try {
const { value, done } = await iter.next();
if (done) return ctrl.close();

const str =
typeof value === 'string' ? value : (
// Add a newline after JSON to make it easier to parse newline-separated JSON on the frontend.
JSON.stringify(value) + '\n'
);
const bytes = encoder.encode(str);

ctrl.enqueue(bytes);
} catch (err) {
ctrl.error(err);
}
},
async cancel() {
await iter.return?.();
},
});
}
}

class SSEDecoder {
Expand Down