Skip to content
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
6 changes: 1 addition & 5 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/1.0.0/schema.json",
"files": {
"ignore": [
".wrangler",
"node_modules",
"vendor/*"
]
"ignore": [".wrangler", "node_modules", "vendor/*"]
},
"formatter": {
"indentStyle": "space",
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ declare module "replicate" {
webhook?: string;
webhook_events_filter?: WebhookEventType[];
signal?: AbortSignal;
useFileOutput?: boolean;
}
): AsyncGenerator<ServerSentEvent>;

Expand Down
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ class Replicate {
* @yields {ServerSentEvent} Each streamed event from the prediction
*/
async *stream(ref, options) {
const { wait, signal, ...data } = options;
const {
wait,
signal,
useFileOutput = this.useFileOutput,
...data
} = options;

const identifier = ModelVersionIdentifier.parse(ref);

Expand All @@ -338,7 +343,10 @@ class Replicate {
const stream = createReadableStream({
url: prediction.urls.stream,
fetch: this.fetch,
...(signal ? { options: { signal } } : {}),
options: {
useFileOutput,
...(signal ? { signal } : {}),
},
});

yield* streamAsyncIterator(stream);
Expand Down
99 changes: 97 additions & 2 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1906,8 +1906,12 @@ describe("Replicate client", () => {
// Continue with tests for other methods

describe("createReadableStream", () => {
function createStream(body: string | ReadableStream, status = 200) {
const streamEndpoint = "https://stream.replicate.com/fake_stream";
function createStream(
body: string | ReadableStream,
status = 200,
streamEndpoint = "https://stream.replicate.com/fake_stream",
options: { useFileOutput?: boolean } = {}
) {
const fetch = jest.fn((url) => {
if (url !== streamEndpoint) {
throw new Error(`Unmocked call to fetch() with url: ${url}`);
Expand All @@ -1917,6 +1921,7 @@ describe("Replicate client", () => {
return createReadableStream({
url: streamEndpoint,
fetch: fetch as any,
options,
});
}

Expand Down Expand Up @@ -2193,5 +2198,95 @@ describe("Replicate client", () => {
);
expect(await iterator.next()).toEqual({ done: true });
});

describe("file streams", () => {
test("emits FileOutput objects", async () => {
const stream = createStream(
`
event: output
id: EVENT_1
data: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==

event: output
id: EVENT_2
data: https://delivery.replicate.com/my_file.png

event: done
id: EVENT_3
data: {}

`.replace(/^[ ]+/gm, ""),
200,
"https://stream.replicate.com/v1/files/abcd"
);

const iterator = stream[Symbol.asyncIterator]();
const { value: event1 } = await iterator.next();
expect(event1.data).toBeInstanceOf(ReadableStream);
expect(event1.data.url().href).toEqual(
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
);

const { value: event2 } = await iterator.next();
expect(event2.data).toBeInstanceOf(ReadableStream);
expect(event2.data.url().href).toEqual(
"https://delivery.replicate.com/my_file.png"
);

expect(await iterator.next()).toEqual({
done: false,
value: { event: "done", id: "EVENT_3", data: "{}" },
});

expect(await iterator.next()).toEqual({ done: true });
});

test("emits strings when useFileOutput is false", async () => {
const stream = createStream(
`
event: output
id: EVENT_1
data: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==

event: output
id: EVENT_2
data: https://delivery.replicate.com/my_file.png

event: done
id: EVENT_3
data: {}

`.replace(/^[ ]+/gm, ""),
200,
"https://stream.replicate.com/v1/files/abcd",
{ useFileOutput: false }
);

const iterator = stream[Symbol.asyncIterator]();

expect(await iterator.next()).toEqual({
done: false,
value: {
event: "output",
id: "EVENT_1",
data: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
},
});
expect(await iterator.next()).toEqual({
done: false,
value: {
event: "output",
id: "EVENT_2",
data: "https://delivery.replicate.com/my_file.png",
},
});
expect(await iterator.next()).toEqual({
done: false,
value: { event: "done", id: "EVENT_3", data: "{}" },
});

expect(await iterator.next()).toEqual({ done: true });
});
});
});
});
16 changes: 12 additions & 4 deletions lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ServerSentEvent {
*/
function createReadableStream({ url, fetch, options = {} }) {
const { useFileOutput = true, headers = {}, ...initOptions } = options;
const shouldProcessFileOutput = useFileOutput && isFileStream(url);

return new ReadableStream({
async start(controller) {
Expand Down Expand Up @@ -89,11 +90,11 @@ function createReadableStream({ url, fetch, options = {} }) {

let data = event.data;
if (
useFileOutput &&
typeof data === "string" &&
(data.startsWith("https:") || data.startsWith("data:"))
event.event === "output" &&
shouldProcessFileOutput &&
typeof data === "string"
) {
data = createFileOutput({ data, fetch });
data = createFileOutput({ url: data, fetch });
}
controller.enqueue(new ServerSentEvent(event.event, data, event.id));

Expand Down Expand Up @@ -169,6 +170,13 @@ function createFileOutput({ url, fetch }) {
});
}

function isFileStream(url) {
try {
return new URL(url).pathname.startsWith("/v1/files/");
} catch {}
return false;
}

module.exports = {
createFileOutput,
createReadableStream,
Expand Down
Loading