mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-26 15:48:41 +00:00
fix(ai): give Cloudflare image requests a timeout and a reply size cap (#4733)
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
9e1ee69ac5
commit
0dbf84bc3e
3 files changed
+52
-4
No files matched your search
@@ -286,6 +286,45 @@ describe('CloudflareImageProvider.generate request shape', () => {
|
||||
expect(body).toEqual({ prompt: 'a tiny red dot', steps: 4 });
|
||||
});
|
||||
|
||||
it('bounds the call with a timeout', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(
|
||||
okJsonResponse({ result: { image: 'AA' } }),
|
||||
);
|
||||
|
||||
await withTestActor(() =>
|
||||
makeProvider().generate({
|
||||
model: '@cf/black-forest-labs/flux-1-schnell',
|
||||
prompt: 'hi',
|
||||
} as never),
|
||||
);
|
||||
|
||||
const [, init] = fetchSpy.mock.calls[0]!;
|
||||
expect((init as RequestInit).signal).toBeInstanceOf(AbortSignal);
|
||||
});
|
||||
|
||||
it('refuses a reply larger than the cap as an upstream failure', async () => {
|
||||
fetchSpy.mockResolvedValueOnce(
|
||||
new Response('{}', {
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'content-length': String(65 * 1024 * 1024),
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(
|
||||
withTestActor(() =>
|
||||
makeProvider().generate({
|
||||
model: '@cf/black-forest-labs/flux-1-schnell',
|
||||
prompt: 'hi',
|
||||
} as never),
|
||||
),
|
||||
).rejects.toMatchObject({
|
||||
statusCode: 502,
|
||||
legacyCode: 'upstream_failed',
|
||||
});
|
||||
});
|
||||
|
||||
it('honours an apiBaseUrl override', async () => {
|
||||
const provider = makeProvider({
|
||||
apiBaseUrl: 'https://custom.cf.example/api/v4',
|
||||
|
||||
@@ -34,6 +34,8 @@ import {
|
||||
CLOUDFLARE_IMAGE_GENERATION_MODELS,
|
||||
CloudflareImageModel,
|
||||
} from './models.js';
|
||||
import { readBoundedBody } from '../../../../util/fileInput.js';
|
||||
import { malformedUpstreamResponse } from '../../../../util/upstreamErrors.js';
|
||||
import { fetchCloudflareImageCatalog } from './catalog.js';
|
||||
import {
|
||||
assertInputImageString,
|
||||
@@ -43,6 +45,10 @@ import {
|
||||
parseDataUri,
|
||||
} from '../../../interfaces/image/inputImage.js';
|
||||
|
||||
// One synchronous call covers the whole generation.
|
||||
const REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
|
||||
const MAX_RESPONSE_BYTES = 64 * 1024 * 1024;
|
||||
|
||||
type CloudflareGenerateParams = IGenerateParams & {
|
||||
steps?: number;
|
||||
num_steps?: number;
|
||||
@@ -498,17 +504,20 @@ export class CloudflareImageProvider implements IImageProvider {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body,
|
||||
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
||||
});
|
||||
const raw = await readBoundedBody(response, MAX_RESPONSE_BYTES, () =>
|
||||
malformedUpstreamResponse('cloudflare', 'Cloudflare'),
|
||||
);
|
||||
|
||||
const contentType = (
|
||||
response.headers.get('content-type') || ''
|
||||
).toLowerCase();
|
||||
if (contentType.startsWith('image/')) {
|
||||
const imageBuffer = Buffer.from(await response.arrayBuffer());
|
||||
return `data:${contentType};base64,${imageBuffer.toString('base64')}`;
|
||||
return `data:${contentType};base64,${raw.toString('base64')}`;
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
const text = raw.toString('utf8');
|
||||
let payload: unknown;
|
||||
try {
|
||||
payload = text ? JSON.parse(text) : {};
|
||||
|
||||
@@ -186,7 +186,7 @@ A web search is metered at a flat per-request rate in addition to the tokens it
|
||||
| All | Up to 16 reference images; more fails with `bad_request`. A reference given as a URL is fetched with a 30-second timeout and capped at 30 MB. |
|
||||
| xAI | Up to 5 reference images; more fails with `bad_request`. |
|
||||
| Together | Image routes are excluded because they require third-party data sharing; generation fails before any upstream call. |
|
||||
| Cloudflare | Output dimensions are clamped per side: FLUX.2 256–1920; Lucid Origin 64–2500; Phoenix 64–2048; SDXL and Inpainting 256–2048. Schnell is fixed at 1024×1024. One reference image on FLUX.2 and Inpainting models. Steps: Schnell 1–8; Lucid Origin 1–40; Phoenix and FLUX.2 Dev 1–50; Klein exactly 4; SDXL and Inpainting 1–20. |
|
||||
| Cloudflare | Output dimensions are clamped per side: FLUX.2 256–1920; Lucid Origin 64–2500; Phoenix 64–2048; SDXL and Inpainting 256–2048. Schnell is fixed at 1024×1024. One reference image on FLUX.2 and Inpainting models. Steps: Schnell 1–8; Lucid Origin 1–40; Phoenix and FLUX.2 Dev 1–50; Klein exactly 4; SDXL and Inpainting 1–20. A call times out after 5 minutes and fails with `504 upstream_timeout`. A reply over 64 MiB fails with `502 upstream_failed`. Neither is charged. |
|
||||
| Replicate | At most 10 references, or the model's lower limit; FLUX 1.1 Pro accepts one. Each fetched reference is capped at 30 MB. Riverflow accepts at most two fonts. Native options follow each model's schema. For other models with width/height controls, dimensions round to multiples of 8 and clamp to the schema bounds (64–4096 per side if none are given). A call has 10 minutes for its predictions, which are polled every 2 seconds. A prediction that a model's safety checker rejects is run again up to two more times when no `seed` is set. Cancellation cleanup polls for up to 30 seconds, plus an in-flight request. Network timeouts: 90 seconds for creation, 30 seconds for other requests. |
|
||||
| BytePlus | Pro: 10 references; other Seedream models: 14. |
|
||||
| Kling | `kling-v3-omni` and `kling-image-o1`: 10 references; `kling-v3` and `kling-v2-1`: one. Prompt up to 2,500 characters. Each reference is a JPEG or PNG up to 10 MB. Jobs are polled every 2 seconds for up to 5 minutes, then fail with `504 upstream_timeout` and aren't charged. Each call to Kling times out after 60 seconds. |
|
||||
|
||||
Reference in new issue
Block a user