mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
Add Cloudflare Workers AI image generation (#973)
This commit is contained in:
@@ -21,6 +21,7 @@ describe("handleImageGenerationCore", () => {
|
||||
|
||||
afterEach(() => {
|
||||
global.fetch = originalFetch;
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("validates required prompt field", async () => {
|
||||
@@ -156,29 +157,54 @@ describe("handleImageGenerationCore", () => {
|
||||
});
|
||||
|
||||
it("generates image with NanoBanana format", async () => {
|
||||
global.fetch.mockResolvedValueOnce(
|
||||
new Response(
|
||||
JSON.stringify({ image: "base64nanobanana" }),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
vi.useFakeTimers();
|
||||
global.fetch
|
||||
.mockResolvedValueOnce(
|
||||
new Response(
|
||||
JSON.stringify({ code: 200, data: { taskId: "task-123" } }),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
)
|
||||
)
|
||||
);
|
||||
.mockResolvedValueOnce(
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
data: {
|
||||
successFlag: 1,
|
||||
response: { resultImageUrl: "https://example.com/nanobanana.png" },
|
||||
},
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
)
|
||||
);
|
||||
|
||||
const result = await handleImageGenerationCore({
|
||||
const pending = handleImageGenerationCore({
|
||||
body: { prompt: "A robot", n: 2, size: "1024x1792" },
|
||||
modelInfo: { provider: "nanobanana", model: "nanobanana-flash" },
|
||||
credentials: { apiKey: "test-key" },
|
||||
log: null,
|
||||
});
|
||||
|
||||
await vi.advanceTimersByTimeAsync(1500);
|
||||
const result = await pending;
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
const fetchCall = global.fetch.mock.calls[0];
|
||||
const requestBody = JSON.parse(fetchCall[1].body);
|
||||
expect(requestBody.type).toBe("TEXTTOIAMGE");
|
||||
expect(requestBody.numImages).toBe(2);
|
||||
expect(requestBody.image_size).toBe("9:16");
|
||||
expect(global.fetch).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"https://api.nanobananaapi.ai/api/v1/nanobanana/record-info?taskId=task-123",
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({
|
||||
Authorization: "Bearer test-key",
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
const responseBody = await result.response.json();
|
||||
expect(responseBody.data[0].b64_json).toBe("base64nanobanana");
|
||||
expect(responseBody.data[0].url).toBe("https://example.com/nanobanana.png");
|
||||
});
|
||||
|
||||
it("generates image with SD WebUI format", async () => {
|
||||
@@ -258,6 +284,123 @@ describe("handleImageGenerationCore", () => {
|
||||
expect(responseBody.data[0].b64_json).toBeTruthy();
|
||||
});
|
||||
|
||||
it("generates image with Cloudflare Workers AI JSON response", async () => {
|
||||
global.fetch.mockResolvedValueOnce(
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
result: { image: "base64cloudflare" },
|
||||
success: true,
|
||||
errors: [],
|
||||
messages: [],
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
)
|
||||
);
|
||||
|
||||
const result = await handleImageGenerationCore({
|
||||
body: { prompt: "A lighthouse", size: "1024x1536" },
|
||||
modelInfo: { provider: "cloudflare-ai", model: "@cf/leonardo/lucid-origin" },
|
||||
credentials: {
|
||||
apiKey: "cf-token",
|
||||
providerSpecificData: { accountId: "cf-account" },
|
||||
},
|
||||
log: null,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(global.fetch).toHaveBeenCalledWith(
|
||||
"https://api.cloudflare.com/client/v4/accounts/cf-account/ai/run/@cf/leonardo/lucid-origin",
|
||||
expect.objectContaining({
|
||||
method: "POST",
|
||||
headers: expect.objectContaining({
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer cf-token",
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
const fetchCall = global.fetch.mock.calls[0];
|
||||
const requestBody = JSON.parse(fetchCall[1].body);
|
||||
expect(requestBody.prompt).toBe("A lighthouse");
|
||||
expect(requestBody.width).toBe(1024);
|
||||
expect(requestBody.height).toBe(1536);
|
||||
|
||||
const responseBody = await result.response.json();
|
||||
expect(responseBody.data[0].b64_json).toBe("base64cloudflare");
|
||||
});
|
||||
|
||||
it("uses multipart form data for Cloudflare FLUX.2 models", async () => {
|
||||
global.fetch.mockResolvedValueOnce(
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
result: { image: "base64flux2" },
|
||||
success: true,
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
)
|
||||
);
|
||||
|
||||
const result = await handleImageGenerationCore({
|
||||
body: { prompt: "A mountain lake", size: "1792x1024", steps: 4 },
|
||||
modelInfo: { provider: "cloudflare-ai", model: "@cf/black-forest-labs/flux-2-klein-9b" },
|
||||
credentials: {
|
||||
apiKey: "cf-token",
|
||||
providerSpecificData: { accountId: "cf-account" },
|
||||
},
|
||||
log: null,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
const fetchCall = global.fetch.mock.calls[0];
|
||||
expect(fetchCall[1].headers).not.toHaveProperty("Content-Type");
|
||||
expect(fetchCall[1].body).toBeInstanceOf(FormData);
|
||||
expect(fetchCall[1].body.get("prompt")).toBe("A mountain lake");
|
||||
expect(fetchCall[1].body.get("width")).toBe("1792");
|
||||
expect(fetchCall[1].body.get("height")).toBe("1024");
|
||||
expect(fetchCall[1].body.get("steps")).toBe("4");
|
||||
});
|
||||
|
||||
it("resolves Cloudflare img2img and inpainting URL inputs before sending", async () => {
|
||||
global.fetch
|
||||
.mockResolvedValueOnce(new Response(new Uint8Array([1, 2, 3]), { status: 200, headers: { "Content-Type": "image/png" } }))
|
||||
.mockResolvedValueOnce(new Response(new Uint8Array([4, 5, 6]), { status: 200, headers: { "Content-Type": "image/png" } }))
|
||||
.mockResolvedValueOnce(
|
||||
new Response(
|
||||
JSON.stringify({ result: { image: "base64inpaint" }, success: true }),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
)
|
||||
);
|
||||
|
||||
const result = await handleImageGenerationCore({
|
||||
body: {
|
||||
prompt: "Change to a lion",
|
||||
image: "https://example.com/source.png",
|
||||
mask_image: "https://example.com/mask.png",
|
||||
size: "512x512",
|
||||
},
|
||||
modelInfo: { provider: "cloudflare-ai", model: "@cf/runwayml/stable-diffusion-v1-5-inpainting" },
|
||||
credentials: {
|
||||
apiKey: "cf-token",
|
||||
providerSpecificData: { accountId: "cf-account" },
|
||||
},
|
||||
log: null,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(global.fetch).toHaveBeenNthCalledWith(1, "https://example.com/source.png");
|
||||
expect(global.fetch).toHaveBeenNthCalledWith(2, "https://example.com/mask.png");
|
||||
|
||||
const providerCall = global.fetch.mock.calls[2];
|
||||
expect(providerCall[0]).toBe("https://api.cloudflare.com/client/v4/accounts/cf-account/ai/run/@cf/runwayml/stable-diffusion-v1-5-inpainting");
|
||||
const requestBody = JSON.parse(providerCall[1].body);
|
||||
expect(requestBody.image).toEqual([1, 2, 3]);
|
||||
expect(requestBody.image_b64).toBe(Buffer.from([1, 2, 3]).toString("base64"));
|
||||
expect(requestBody.mask).toEqual([4, 5, 6]);
|
||||
expect(requestBody.mask_image).toEqual([4, 5, 6]);
|
||||
expect(requestBody.mask_b64).toBe(Buffer.from([4, 5, 6]).toString("base64"));
|
||||
});
|
||||
|
||||
it("handles provider error responses", async () => {
|
||||
global.fetch.mockResolvedValueOnce(
|
||||
new Response(
|
||||
|
||||
Reference in New Issue
Block a user