From 4adb1777ff1623ff6256368fee3d926566aaf360 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 3 May 2024 12:55:13 -0700 Subject: [PATCH 1/2] Prevent signal parameter from being passed alongside input when calling replicate.stream --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f4c0e2cf..dd60ceca 100644 --- a/index.js +++ b/index.js @@ -274,7 +274,7 @@ class Replicate { * @yields {ServerSentEvent} Each streamed event from the prediction */ async *stream(ref, options) { - const { wait, ...data } = options; + const { wait, signal, ...data } = options; const identifier = ModelVersionIdentifier.parse(ref); @@ -296,11 +296,10 @@ class Replicate { } if (prediction.urls && prediction.urls.stream) { - const { signal } = options; const stream = createReadableStream({ url: prediction.urls.stream, fetch: this.fetch, - options: { signal }, + ...(signal ? { options: { signal } } : {}), }); yield* streamAsyncIterator(stream); From ff31e66182a35195f00ec4f24e7b4d8ddd1280ca Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Fri, 3 May 2024 12:58:40 -0700 Subject: [PATCH 2/2] Add test coverage for passing signal to replicate.stream --- integration/cloudflare-worker/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/integration/cloudflare-worker/index.js b/integration/cloudflare-worker/index.js index be18d53c..32ec9fc8 100644 --- a/integration/cloudflare-worker/index.js +++ b/integration/cloudflare-worker/index.js @@ -5,12 +5,14 @@ export default { const replicate = new Replicate({ auth: env.REPLICATE_API_TOKEN }); try { + const controller = new AbortController(); const output = replicate.stream( "replicate/canary:30e22229542eb3f79d4f945dacb58d32001b02cc313ae6f54eef27904edf3272", { input: { text: "Colin CloudFlare", }, + signal: controller.signal, } ); const stream = new ReadableStream({