feat: image content
This commit is contained in:
103
packages/ai/test/google-image-output.test.ts
Normal file
103
packages/ai/test/google-image-output.test.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("@google/genai", () => {
|
||||
class GoogleGenAI {
|
||||
models = {
|
||||
generateContentStream: async function* () {
|
||||
yield {
|
||||
responseId: "google-image-1",
|
||||
candidates: [
|
||||
{
|
||||
content: {
|
||||
parts: [
|
||||
{ text: "Here is your image." },
|
||||
{ inlineData: { mimeType: "image/png", data: "ZmFrZS1wbmc=" } },
|
||||
],
|
||||
},
|
||||
finishReason: "STOP",
|
||||
},
|
||||
],
|
||||
usageMetadata: {
|
||||
promptTokenCount: 5,
|
||||
candidatesTokenCount: 7,
|
||||
totalTokenCount: 12,
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
GoogleGenAI,
|
||||
FinishReason: {
|
||||
STOP: "STOP",
|
||||
MAX_TOKENS: "MAX_TOKENS",
|
||||
BLOCKLIST: "BLOCKLIST",
|
||||
PROHIBITED_CONTENT: "PROHIBITED_CONTENT",
|
||||
SPII: "SPII",
|
||||
SAFETY: "SAFETY",
|
||||
IMAGE_SAFETY: "IMAGE_SAFETY",
|
||||
IMAGE_PROHIBITED_CONTENT: "IMAGE_PROHIBITED_CONTENT",
|
||||
IMAGE_RECITATION: "IMAGE_RECITATION",
|
||||
IMAGE_OTHER: "IMAGE_OTHER",
|
||||
RECITATION: "RECITATION",
|
||||
FINISH_REASON_UNSPECIFIED: "FINISH_REASON_UNSPECIFIED",
|
||||
OTHER: "OTHER",
|
||||
LANGUAGE: "LANGUAGE",
|
||||
MALFORMED_FUNCTION_CALL: "MALFORMED_FUNCTION_CALL",
|
||||
UNEXPECTED_TOOL_CALL: "UNEXPECTED_TOOL_CALL",
|
||||
NO_IMAGE: "NO_IMAGE",
|
||||
},
|
||||
FunctionCallingConfigMode: {
|
||||
AUTO: "AUTO",
|
||||
ANY: "ANY",
|
||||
NONE: "NONE",
|
||||
},
|
||||
ThinkingLevel: {
|
||||
THINKING_LEVEL_UNSPECIFIED: "THINKING_LEVEL_UNSPECIFIED",
|
||||
MINIMAL: "MINIMAL",
|
||||
LOW: "LOW",
|
||||
MEDIUM: "MEDIUM",
|
||||
HIGH: "HIGH",
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
import { streamGoogle } from "../src/providers/google.js";
|
||||
import type { Context, Model } from "../src/types.js";
|
||||
|
||||
describe("google image output", () => {
|
||||
it("emits assistant image blocks for inlineData parts", async () => {
|
||||
const model: Model<"google-generative-ai"> = {
|
||||
id: "gemini-2.5-flash",
|
||||
name: "Gemini 2.5 Flash",
|
||||
api: "google-generative-ai",
|
||||
provider: "google",
|
||||
baseUrl: "https://generativelanguage.googleapis.com",
|
||||
reasoning: false,
|
||||
input: ["text", "image"],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 1000000,
|
||||
maxTokens: 8192,
|
||||
};
|
||||
const context: Context = {
|
||||
messages: [{ role: "user", content: "Generate an image", timestamp: Date.now() }],
|
||||
};
|
||||
|
||||
const result = streamGoogle(model, context, { apiKey: "test" });
|
||||
const eventTypes: string[] = [];
|
||||
for await (const event of result) {
|
||||
eventTypes.push(event.type);
|
||||
}
|
||||
|
||||
const message = await result.result();
|
||||
expect(eventTypes).toEqual(["start", "text_start", "text_delta", "text_end", "image_start", "image_end", "done"]);
|
||||
expect(message.responseId).toBe("google-image-1");
|
||||
expect(message.content[0]).toMatchObject({ type: "text", text: "Here is your image." });
|
||||
expect(message.content[1]).toMatchObject({
|
||||
type: "image",
|
||||
mimeType: "image/png",
|
||||
data: "ZmFrZS1wbmc=",
|
||||
});
|
||||
});
|
||||
});
|
||||
98
packages/ai/test/openai-completions-image-output.test.ts
Normal file
98
packages/ai/test/openai-completions-image-output.test.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { streamSimple } from "../src/stream.js";
|
||||
import type { Context, Model } 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: "chatcmpl-image-1",
|
||||
usage: {
|
||||
prompt_tokens: 12,
|
||||
completion_tokens: 34,
|
||||
prompt_tokens_details: { cached_tokens: 0 },
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
finish_reason: "stop",
|
||||
message: {
|
||||
role: "assistant",
|
||||
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("openai-completions image output", () => {
|
||||
beforeEach(() => {
|
||||
mockState.lastParams = undefined;
|
||||
});
|
||||
|
||||
it("switches OpenRouter image generation models to non-streaming and emits image events", async () => {
|
||||
const model: Model<"openai-completions"> = {
|
||||
id: "black-forest-labs/flux.2-pro",
|
||||
name: "Black Forest Labs: FLUX.2 Pro",
|
||||
api: "openai-completions",
|
||||
provider: "openrouter",
|
||||
baseUrl: "https://openrouter.ai/api/v1",
|
||||
reasoning: false,
|
||||
input: ["text", "image"],
|
||||
cost: { input: 0.015, output: 0.03, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 100000,
|
||||
maxTokens: 100000,
|
||||
};
|
||||
const context: Context = {
|
||||
messages: [{ role: "user", content: "Generate a cat", timestamp: Date.now() }],
|
||||
};
|
||||
|
||||
const result = streamSimple(model, context, { apiKey: "test" });
|
||||
const eventTypes: string[] = [];
|
||||
for await (const event of result) {
|
||||
eventTypes.push(event.type);
|
||||
}
|
||||
|
||||
const message = await result.result();
|
||||
expect(eventTypes).toEqual(["start", "text_start", "text_delta", "text_end", "image_start", "image_end", "done"]);
|
||||
expect(message.responseId).toBe("chatcmpl-image-1");
|
||||
expect(message.content[0]).toMatchObject({ type: "text", text: "Here is your image." });
|
||||
expect(message.content[1]).toMatchObject({
|
||||
type: "image",
|
||||
mimeType: "image/png",
|
||||
data: "ZmFrZS1wbmc=",
|
||||
});
|
||||
|
||||
const params = mockState.lastParams as { stream?: boolean; stream_options?: unknown };
|
||||
expect(params.stream).toBe(false);
|
||||
expect("stream_options" in (params as object)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -34,6 +34,7 @@ const compat = {
|
||||
requiresReasoningContentOnAssistantMessages: false,
|
||||
thinkingFormat: "openai",
|
||||
openRouterRouting: {},
|
||||
openRouterImageGeneration: false,
|
||||
vercelGatewayRouting: {},
|
||||
zaiToolStream: false,
|
||||
supportsStrictMode: true,
|
||||
|
||||
@@ -32,6 +32,7 @@ const compat: Required<OpenAICompletionsCompat> = {
|
||||
requiresReasoningContentOnAssistantMessages: false,
|
||||
thinkingFormat: "openai",
|
||||
openRouterRouting: {},
|
||||
openRouterImageGeneration: false,
|
||||
vercelGatewayRouting: {},
|
||||
zaiToolStream: false,
|
||||
supportsStrictMode: true,
|
||||
|
||||
Reference in New Issue
Block a user