Skip to content

Commit 5a5959e

Browse files
committed
fix(runtime): stream Python image results in chunks
1 parent 92830d1 commit 5a5959e

5 files changed

Lines changed: 51 additions & 25 deletions

File tree

packages/runtime/src/providers/python-provider.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,22 +270,26 @@ export class PythonProvider extends BaseProvider {
270270
// ── Media generation ──────────────────────────────────────────────
271271

272272
async textToImage(params: TextToImageParams): Promise<Uint8Array> {
273+
const { signal, ...wireParams } = params;
273274
return this._bridge.providerTextToImage(
274275
this._pythonProviderId,
275-
{ ...params },
276-
this._secrets
276+
{ ...wireParams, model: params.model.id },
277+
this._secrets,
278+
signal
277279
);
278280
}
279281

280282
async imageToImage(
281283
images: Uint8Array[],
282284
params: ImageToImageParams
283285
): Promise<Uint8Array> {
286+
const { signal, ...wireParams } = params;
284287
return this._bridge.providerImageToImage(
285288
this._pythonProviderId,
286289
images[0] ?? new Uint8Array(),
287-
{ ...params },
288-
this._secrets
290+
{ ...wireParams, model: params.model.id },
291+
this._secrets,
292+
signal
289293
);
290294
}
291295

packages/runtime/src/python-bridge-base.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,29 +1087,39 @@ export abstract class PythonBridgeBase
10871087
async providerTextToImage(
10881088
providerId: string,
10891089
params: Record<string, unknown>,
1090-
secrets?: Record<string, string>
1090+
secrets?: Record<string, string>,
1091+
signal?: AbortSignal
10911092
): Promise<Uint8Array> {
1092-
const result = await this._providerCall("provider.text_to_image", {
1093-
provider: providerId,
1094-
params,
1095-
secrets: secrets ?? {}
1096-
});
1097-
return (result as { blobs: Record<string, Uint8Array> }).blobs.image;
1093+
const result = await this._providerBlobCall(
1094+
"provider.text_to_image",
1095+
{
1096+
provider: providerId,
1097+
params,
1098+
secrets: secrets ?? {}
1099+
},
1100+
signal
1101+
);
1102+
return result.blobs.image;
10981103
}
10991104

11001105
async providerImageToImage(
11011106
providerId: string,
11021107
image: Uint8Array,
11031108
params: Record<string, unknown>,
1104-
secrets?: Record<string, string>
1109+
secrets?: Record<string, string>,
1110+
signal?: AbortSignal
11051111
): Promise<Uint8Array> {
1106-
const result = await this._providerCall("provider.image_to_image", {
1107-
provider: providerId,
1108-
image,
1109-
params,
1110-
secrets: secrets ?? {}
1111-
});
1112-
return (result as { blobs: Record<string, Uint8Array> }).blobs.image;
1112+
const result = await this._providerBlobCall(
1113+
"provider.image_to_image",
1114+
{
1115+
provider: providerId,
1116+
image,
1117+
params,
1118+
secrets: secrets ?? {}
1119+
},
1120+
signal
1121+
);
1122+
return result.blobs.image;
11131123
}
11141124

11151125
async providerTextToVideo(

packages/runtime/src/python-bridge-types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,13 +567,15 @@ export interface PythonBridge extends EventEmitter {
567567
providerTextToImage(
568568
providerId: string,
569569
params: Record<string, unknown>,
570-
secrets?: Record<string, string>
570+
secrets?: Record<string, string>,
571+
signal?: AbortSignal
571572
): Promise<Uint8Array>;
572573
providerImageToImage(
573574
providerId: string,
574575
image: Uint8Array,
575576
params: Record<string, unknown>,
576-
secrets?: Record<string, string>
577+
secrets?: Record<string, string>,
578+
signal?: AbortSignal
577579
): Promise<Uint8Array>;
578580
providerTextToVideo(
579581
providerId: string,

packages/runtime/src/swappable-python-bridge.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,25 @@ export class SwappableBridge extends EventEmitter implements PythonBridge {
226226
providerTextToImage(
227227
providerId: string,
228228
params: Record<string, unknown>,
229-
secrets?: Record<string, string>
229+
secrets?: Record<string, string>,
230+
signal?: AbortSignal
230231
): Promise<Uint8Array> {
231-
return this._target.providerTextToImage(providerId, params, secrets);
232+
return this._target.providerTextToImage(providerId, params, secrets, signal);
232233
}
233234

234235
providerImageToImage(
235236
providerId: string,
236237
image: Uint8Array,
237238
params: Record<string, unknown>,
238-
secrets?: Record<string, string>
239+
secrets?: Record<string, string>,
240+
signal?: AbortSignal
239241
): Promise<Uint8Array> {
240242
return this._target.providerImageToImage(
241243
providerId,
242244
image,
243245
params,
244-
secrets
246+
secrets,
247+
signal
245248
);
246249
}
247250

packages/runtime/tests/python-bridge-base-coverage.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,10 @@ describe("PythonBridgeBase — provider RPCs", () => {
658658
it("providerTextToImage returns the image blob", async () => {
659659
const bytes = new Uint8Array([1, 2, 3]);
660660
const p = bridge.providerTextToImage("fal", { prompt: "x" });
661+
const frame = bridge.sent.find((f) => f.type === "provider.text_to_image")!;
662+
expect((frame.data as Record<string, unknown>).blob_transfer).toBe(
663+
"chunked-v1"
664+
);
661665
reply("provider.text_to_image", { blobs: { image: bytes } });
662666
await expect(p).resolves.toBe(bytes);
663667
});
@@ -670,6 +674,9 @@ describe("PythonBridgeBase — provider RPCs", () => {
670674
(f) => f.type === "provider.image_to_image"
671675
)!;
672676
expect((frame.data as Record<string, unknown>).image).toBe(input);
677+
expect((frame.data as Record<string, unknown>).blob_transfer).toBe(
678+
"chunked-v1"
679+
);
673680
reply("provider.image_to_image", { blobs: { image: output } });
674681
await expect(p).resolves.toBe(output);
675682
});

0 commit comments

Comments
 (0)