Skip to content

Commit

Permalink
refactor(client)!: remove Stream.toReadableStream() (#110)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Stream.toReadableStream() has been removed
  • Loading branch information
stainless-bot authored and rattrayalex committed Aug 12, 2023
1 parent 58ac305 commit c370412
Showing 1 changed file with 0 additions and 33 deletions.
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

0 comments on commit c370412

Please sign in to comment.