feat: openrouter images
This commit is contained in:
111
packages/ai/test/openrouter-images.test.ts
Normal file
111
packages/ai/test/openrouter-images.test.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { completeImages, images } from "../src/images.js";
|
||||
import type { ImagesContext, ImagesModel } from "../src/types.js";
|
||||
|
||||
const mockState = vi.hoisted(() => ({
|
||||
lastParams: undefined as unknown,
|
||||
}));
|
||||
|
||||
vi.mock("openai", () => {
|
||||
class FakeOpenAI {
|
||||
chat = {
|
||||
completions: {
|
||||
create: (params: unknown) => {
|
||||
mockState.lastParams = params;
|
||||
const response = {
|
||||
id: "img-1",
|
||||
usage: {
|
||||
prompt_tokens: 12,
|
||||
completion_tokens: 34,
|
||||
prompt_tokens_details: { cached_tokens: 0 },
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: "Here is your image.",
|
||||
images: [{ image_url: "data:image/png;base64,ZmFrZS1wbmc=" }],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const promise = Promise.resolve(response) as Promise<typeof response> & {
|
||||
withResponse: () => Promise<{
|
||||
data: typeof response;
|
||||
response: { status: number; headers: Headers };
|
||||
}>;
|
||||
};
|
||||
promise.withResponse = async () => ({
|
||||
data: response,
|
||||
response: { status: 200, headers: new Headers() },
|
||||
});
|
||||
return promise;
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return { default: FakeOpenAI };
|
||||
});
|
||||
|
||||
describe("openrouter images", () => {
|
||||
beforeEach(() => {
|
||||
mockState.lastParams = undefined;
|
||||
});
|
||||
|
||||
it("emits image events and returns text plus images in final output", async () => {
|
||||
const model: ImagesModel<"openrouter-images"> = {
|
||||
id: "google/gemini-3.1-flash-image-preview",
|
||||
name: "Gemini 3.1 Flash Image Preview",
|
||||
api: "openrouter-images",
|
||||
provider: "openrouter",
|
||||
baseUrl: "https://openrouter.ai/api/v1",
|
||||
input: ["text", "image"],
|
||||
output: ["text", "image"],
|
||||
cost: { input: 0.015, output: 0.03, cacheRead: 0, cacheWrite: 0 },
|
||||
headers: { "HTTP-Referer": "https://example.com" },
|
||||
};
|
||||
const context: ImagesContext = {
|
||||
input: [{ type: "text", text: "Generate a cat" }],
|
||||
};
|
||||
|
||||
const result = images(model, context, { apiKey: "test" });
|
||||
const eventTypes: string[] = [];
|
||||
for await (const event of result) {
|
||||
eventTypes.push(event.type);
|
||||
}
|
||||
|
||||
const output = await result.result();
|
||||
expect(eventTypes).toEqual(["start", "image_start", "image_end", "done"]);
|
||||
expect(output.responseId).toBe("img-1");
|
||||
expect(output.output[0]).toMatchObject({ type: "text", text: "Here is your image." });
|
||||
expect(output.output[1]).toMatchObject({ type: "image", mimeType: "image/png", data: "ZmFrZS1wbmc=" });
|
||||
|
||||
const params = mockState.lastParams as {
|
||||
stream?: boolean;
|
||||
modalities?: string[];
|
||||
messages?: [{ content?: [{ type: string; text?: string }] }];
|
||||
};
|
||||
expect(params.stream).toBe(false);
|
||||
expect(params.modalities).toEqual(["image", "text"]);
|
||||
expect(params.messages?.[0]?.content?.[0]).toMatchObject({ type: "text", text: "Generate a cat" });
|
||||
});
|
||||
|
||||
it("completeImages resolves the final assistant images result", async () => {
|
||||
const model: ImagesModel<"openrouter-images"> = {
|
||||
id: "black-forest-labs/flux.2-pro",
|
||||
name: "FLUX.2 Pro",
|
||||
api: "openrouter-images",
|
||||
provider: "openrouter",
|
||||
baseUrl: "https://openrouter.ai/api/v1",
|
||||
input: ["text", "image"],
|
||||
output: ["image"],
|
||||
cost: { input: 0.015, output: 0.03, cacheRead: 0, cacheWrite: 0 },
|
||||
};
|
||||
const context: ImagesContext = {
|
||||
input: [{ type: "text", text: "Generate a cat" }],
|
||||
};
|
||||
|
||||
const output = await completeImages(model, context, { apiKey: "test" });
|
||||
expect(output.output.some((item) => item.type === "image")).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user