| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import { Effect } from "effect"
- import {
- Image,
- ImageClient,
- ImageInput,
- ImageModel,
- type ImageModelOptions,
- type ImageOptions,
- type ImageRequestFor,
- type ImageRoute,
- } from "../src"
- import type { Service } from "../src/image-client"
- import { Google, OpenAI, XAI, ZAI } from "../src/providers"
- type Requirements<T> = T extends Effect.Effect<infer _A, infer _E, infer R> ? R : never
- type Equal<A, B> = [A, B] extends [B, A] ? true : false
- type Assert<T extends true> = T
- type GoogleLikeOptions = {
- readonly aspectRatio?: "1:1" | "16:9"
- readonly imageSize?: "1K" | "2K"
- } & Record<string, unknown>
- declare const route: ImageRoute<GoogleLikeOptions>
- const google = ImageModel.make<GoogleLikeOptions>({ id: "gemini-image", provider: "google", route })
- // @ts-expect-error Extracted model options retain known provider fields.
- const invalidGoogleOptions: ImageModelOptions<typeof google> = { aspectRatio: "wide" }
- void invalidGoogleOptions
- Image.generate({
- model: google,
- prompt: "A lighthouse",
- images: [
- ImageInput.bytes(Uint8Array.from([1, 2, 3]), "image/png"),
- ImageInput.url("data:image/jpeg;base64,AQID"),
- ImageInput.fileUri("https://generativelanguage.googleapis.com/v1beta/files/example", "image/webp"),
- ],
- options: { aspectRatio: "16:9", imageSize: "2K", futureOption: true },
- })
- const googleProvider = Google.configure({ apiKey: "test" }).image("any-model-id")
- Image.generate({
- model: googleProvider,
- prompt: "A lighthouse",
- options: {
- aspectRatio: "16:9",
- imageSize: "2K",
- seed: 42,
- thinkingLevel: "HIGH",
- includeThoughts: true,
- futureOption: true,
- },
- })
- Image.generate({
- model: googleProvider,
- prompt: "A lighthouse",
- options: { aspectRatio: "future-ratio", imageSize: "8K", thinkingLevel: "FUTURE" },
- })
- // @ts-expect-error Image generation options are request-scoped, not provider configuration.
- Google.configure({ image: { providerOptions: { imageSize: "2K" } } })
- // @ts-expect-error Known Google string options retain their value kind.
- Image.generate({ model: googleProvider, prompt: "A lighthouse", options: { imageSize: 2 } })
- // @ts-expect-error Known Google numeric options retain their value kind.
- Image.generate({ model: googleProvider, prompt: "A lighthouse", options: { seed: "42" } })
- // @ts-expect-error Known Google boolean options retain their value kind.
- Image.generate({ model: googleProvider, prompt: "A lighthouse", options: { includeThoughts: "yes" } })
- const openai = OpenAI.image("gpt-image-2")
- // @ts-expect-error Image generation options are request-scoped, not provider configuration.
- OpenAI.configure({ image: { options: { quality: "medium" } } })
- const futureOpenAIOptions: ImageModelOptions<typeof openai> = { quality: "future-quality" }
- void futureOpenAIOptions
- Image.generate({
- model: openai,
- prompt: "A lighthouse",
- images: [ImageInput.url("https://example.com/source.png"), ImageInput.file("file_123")],
- options: {
- mask: ImageInput.bytes(Uint8Array.from([1]), "image/png"),
- quality: "hd",
- outputFormat: "webp",
- size: "2048x2048",
- future_option: true,
- },
- })
- Image.generate({ model: openai, prompt: "A lighthouse", options: { quality: "future-quality", size: "256x256" } })
- Image.generate({ model: openai, prompt: "A lighthouse", options: { size: "1792x1024" } })
- Image.generate({ model: openai, prompt: "A lighthouse", options: { native_future_option: true } })
- // @ts-expect-error Known OpenAI string options retain their value kind.
- Image.generate({ model: openai, prompt: "A lighthouse", options: { quality: 1 } })
- // @ts-expect-error Known OpenAI numeric options retain their value kind.
- Image.generate({ model: openai, prompt: "A lighthouse", options: { outputCompression: "80" } })
- OpenAI.imageGeneration({ action: "future-action", quality: "future-quality", size: "2048x2048" })
- // @ts-expect-error Hosted image generation numeric options retain their value kind.
- OpenAI.imageGeneration({ partialImages: "2" })
- // @ts-expect-error Known Google-like options are inferred from the selected model.
- Image.generate({ model: google, prompt: "A lighthouse", options: { aspectRatio: "wide" } })
- const xai = XAI.configure({ apiKey: "test" }).image("any-model-id")
- // @ts-expect-error Image generation options are request-scoped, not provider configuration.
- XAI.configure({ image: { options: { resolution: "1k" } } })
- Image.generate({
- model: xai,
- prompt: "A lighthouse",
- images: [ImageInput.url("data:image/png;base64,AQID"), ImageInput.file("file_123")],
- options: {
- n: 2,
- aspectRatio: "future-ratio",
- resolution: "future-resolution",
- responseFormat: "future-format",
- future_option: true,
- },
- })
- Image.generate({
- model: xai,
- prompt: "A lighthouse",
- options: { aspect_ratio: "16:9", response_format: "b64_json", native_future_option: true },
- })
- // @ts-expect-error Known xAI numeric options retain their value kind.
- Image.generate({ model: xai, prompt: "A lighthouse", options: { n: "2" } })
- // @ts-expect-error Known xAI string options retain their value kind.
- Image.generate({ model: xai, prompt: "A lighthouse", options: { resolution: 2 } })
- const zai = ZAI.configure({ apiKey: "test" }).image("any-model-id")
- // @ts-expect-error Image generation options are request-scoped, not provider configuration.
- ZAI.configure({ image: { options: { quality: "hd" } } })
- Image.generate({
- model: zai,
- prompt: "A lighthouse",
- options: { quality: "future-quality", userID: "user-123", future_option: true },
- })
- Image.generate({ model: zai, prompt: "A lighthouse", options: { user_id: "raw-user" } })
- // @ts-expect-error Known Z.ai string options retain their value kind.
- Image.generate({ model: zai, prompt: "A lighthouse", options: { quality: 1 } })
- // @ts-expect-error Known Z.ai user IDs retain their value kind.
- Image.generate({ model: zai, prompt: "A lighthouse", options: { userID: 1 } })
- declare const generic: ImageModel<ImageOptions>
- Image.generate({ model: generic, prompt: "A lighthouse", options: { arbitrary: true } })
- const explicitImageInput: ImageInput = ImageInput.url("https://example.com/image.png")
- void explicitImageInput
- // @ts-expect-error Raw strings are ambiguous and are not image inputs.
- Image.generate({ model: openai, prompt: "A lighthouse", images: ["AQID"] })
- // @ts-expect-error Byte image inputs require an explicit MIME type.
- Image.generate({ model: openai, prompt: "A lighthouse", images: [{ type: "bytes", data: new Uint8Array() }] })
- // @ts-expect-error File URIs require an explicit MIME type for Gemini fileData.
- Image.generate({ model: google, prompt: "A lighthouse", images: [{ type: "file-uri", uri: "files/123" }] })
- const request = Image.request({
- model: google,
- prompt: "A lighthouse",
- options: { aspectRatio: "1:1", futureOption: true },
- })
- const typedRequest: ImageRequestFor<GoogleLikeOptions> = request
- void typedRequest
- const generated = ImageClient.generate(request)
- type GenerateRequirements = Assert<Equal<Requirements<typeof generated>, Service>>
- void (true satisfies GenerateRequirements)
- // @ts-expect-error Image requests no longer expose a common count option.
- Image.generate({ model: openai, prompt: "A lighthouse", count: 2 })
- // @ts-expect-error Image requests no longer expose a common size option.
- Image.generate({ model: openai, prompt: "A lighthouse", size: { width: 1024, height: 1024 } })
- // @ts-expect-error Image requests no longer expose a common aspectRatio option.
- Image.generate({ model: openai, prompt: "A lighthouse", aspectRatio: "16:9" })
- // @ts-expect-error Image requests no longer expose a common seed option.
- Image.generate({ model: openai, prompt: "A lighthouse", seed: 1 })
- // @ts-expect-error Image requests do not expose metadata.
- Image.generate({ model: openai, prompt: "A lighthouse", metadata: { trace: true } })
- // @ts-expect-error Masks are provider options, not a common image request field.
- Image.generate({ model: openai, prompt: "A lighthouse", mask: ImageInput.url("https://example.com/mask.png") })
|