delete: images()
This commit is contained in:
@@ -3,7 +3,7 @@ import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getImageModel } from "../src/image-models.js";
|
||||
import { generateImages, images } from "../src/images.js";
|
||||
import { generateImages } from "../src/images.js";
|
||||
import type { ImageContent, ImagesContext, ImagesModel, ProviderImagesOptions } from "../src/types.js";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
@@ -24,36 +24,6 @@ async function basicImageGeneration<TApi extends string>(model: ImagesModel<TApi
|
||||
expect(response.timestamp).toBeGreaterThan(0);
|
||||
}
|
||||
|
||||
async function handleStreaming<TApi extends string>(model: ImagesModel<TApi>, options?: ImagesOptionsWithExtras) {
|
||||
const context: ImagesContext = {
|
||||
input: [{ type: "text", text: "Generate a simple blue square on a plain white background. No text." }],
|
||||
};
|
||||
|
||||
const s = images(model, context, options);
|
||||
let started = false;
|
||||
let imageStarted = false;
|
||||
let imageCompleted = false;
|
||||
|
||||
for await (const event of s) {
|
||||
if (event.type === "start") {
|
||||
started = true;
|
||||
} else if (event.type === "image_start") {
|
||||
imageStarted = true;
|
||||
} else if (event.type === "image_end") {
|
||||
imageCompleted = true;
|
||||
expect(event.image.type).toBe("image");
|
||||
expect(event.image.data.length).toBeGreaterThan(0);
|
||||
}
|
||||
}
|
||||
|
||||
const response = await s.result();
|
||||
expect(response.stopReason, `Error: ${response.errorMessage}`).toBe("stop");
|
||||
expect(started).toBe(true);
|
||||
expect(imageStarted).toBe(true);
|
||||
expect(imageCompleted).toBe(true);
|
||||
expect(response.output.some((item) => item.type === "image")).toBe(true);
|
||||
}
|
||||
|
||||
async function handleTextAndImageOutput<TApi extends string>(
|
||||
model: ImagesModel<TApi>,
|
||||
options?: ImagesOptionsWithExtras,
|
||||
@@ -108,10 +78,6 @@ describe("Images E2E Tests", () => {
|
||||
await basicImageGeneration(model);
|
||||
});
|
||||
|
||||
it("should handle image streaming", { retry: 3 }, async () => {
|
||||
await handleStreaming(model);
|
||||
});
|
||||
|
||||
it("should handle text plus image output", { retry: 3 }, async () => {
|
||||
await handleTextAndImageOutput(model);
|
||||
});
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { generateImages, images } from "../src/images.js";
|
||||
import { generateImages } from "../src/images.js";
|
||||
import type { ImagesContext, ImagesModel } from "../src/types.js";
|
||||
|
||||
const mockState = vi.hoisted(() => ({
|
||||
lastParams: undefined as unknown,
|
||||
lastRequestOptions: undefined as unknown,
|
||||
}));
|
||||
|
||||
vi.mock("openai", () => {
|
||||
class FakeOpenAI {
|
||||
chat = {
|
||||
completions: {
|
||||
create: (params: unknown) => {
|
||||
create: (params: unknown, requestOptions?: unknown) => {
|
||||
mockState.lastParams = params;
|
||||
mockState.lastRequestOptions = requestOptions;
|
||||
const signal = (requestOptions as { signal?: AbortSignal } | undefined)?.signal;
|
||||
if (signal?.aborted) {
|
||||
const error = new Error("Request aborted");
|
||||
return {
|
||||
withResponse: async () => {
|
||||
throw error;
|
||||
},
|
||||
};
|
||||
}
|
||||
const response = {
|
||||
id: "img-1",
|
||||
usage: {
|
||||
@@ -50,9 +61,10 @@ vi.mock("openai", () => {
|
||||
describe("openrouter images", () => {
|
||||
beforeEach(() => {
|
||||
mockState.lastParams = undefined;
|
||||
mockState.lastRequestOptions = undefined;
|
||||
});
|
||||
|
||||
it("emits image events and returns text plus images in final output", async () => {
|
||||
it("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",
|
||||
@@ -68,14 +80,8 @@ describe("openrouter images", () => {
|
||||
input: [{ type: "text", text: "Generate a dog" }],
|
||||
};
|
||||
|
||||
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"]);
|
||||
const output = await generateImages(model, context, { apiKey: "test" });
|
||||
expect(output.stopReason).toBe("stop");
|
||||
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=" });
|
||||
@@ -90,6 +96,29 @@ describe("openrouter images", () => {
|
||||
expect(params.messages?.[0]?.content?.[0]).toMatchObject({ type: "text", text: "Generate a dog" });
|
||||
});
|
||||
|
||||
it("passes through abort signal and returns aborted 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 dog" }],
|
||||
};
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
|
||||
const output = await generateImages(model, context, { apiKey: "test", signal: controller.signal });
|
||||
expect(output.stopReason).toBe("aborted");
|
||||
expect(output.errorMessage).toBe("Request aborted");
|
||||
expect(mockState.lastRequestOptions).toMatchObject({ signal: controller.signal });
|
||||
});
|
||||
|
||||
it("generateImages resolves the final assistant images result", async () => {
|
||||
const model: ImagesModel<"openrouter-images"> = {
|
||||
id: "black-forest-labs/flux.2-pro",
|
||||
|
||||
Reference in New Issue
Block a user