image.types.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { Effect } from "effect"
  2. import {
  3. Image,
  4. ImageClient,
  5. ImageInput,
  6. ImageModel,
  7. type ImageModelOptions,
  8. type ImageOptions,
  9. type ImageRequestFor,
  10. type ImageRoute,
  11. } from "../src"
  12. import type { Service } from "../src/image-client"
  13. import { Google, OpenAI, XAI, ZAI } from "../src/providers"
  14. type Requirements<T> = T extends Effect.Effect<infer _A, infer _E, infer R> ? R : never
  15. type Equal<A, B> = [A, B] extends [B, A] ? true : false
  16. type Assert<T extends true> = T
  17. type GoogleLikeOptions = {
  18. readonly aspectRatio?: "1:1" | "16:9"
  19. readonly imageSize?: "1K" | "2K"
  20. } & Record<string, unknown>
  21. declare const route: ImageRoute<GoogleLikeOptions>
  22. const google = ImageModel.make<GoogleLikeOptions>({ id: "gemini-image", provider: "google", route })
  23. // @ts-expect-error Extracted model options retain known provider fields.
  24. const invalidGoogleOptions: ImageModelOptions<typeof google> = { aspectRatio: "wide" }
  25. void invalidGoogleOptions
  26. Image.generate({
  27. model: google,
  28. prompt: "A lighthouse",
  29. images: [
  30. ImageInput.bytes(Uint8Array.from([1, 2, 3]), "image/png"),
  31. ImageInput.url("data:image/jpeg;base64,AQID"),
  32. ImageInput.fileUri("https://generativelanguage.googleapis.com/v1beta/files/example", "image/webp"),
  33. ],
  34. options: { aspectRatio: "16:9", imageSize: "2K", futureOption: true },
  35. })
  36. const googleProvider = Google.configure({ apiKey: "test" }).image("any-model-id")
  37. Image.generate({
  38. model: googleProvider,
  39. prompt: "A lighthouse",
  40. options: {
  41. aspectRatio: "16:9",
  42. imageSize: "2K",
  43. seed: 42,
  44. thinkingLevel: "HIGH",
  45. includeThoughts: true,
  46. futureOption: true,
  47. },
  48. })
  49. Image.generate({
  50. model: googleProvider,
  51. prompt: "A lighthouse",
  52. options: { aspectRatio: "future-ratio", imageSize: "8K", thinkingLevel: "FUTURE" },
  53. })
  54. // @ts-expect-error Image generation options are request-scoped, not provider configuration.
  55. Google.configure({ image: { providerOptions: { imageSize: "2K" } } })
  56. // @ts-expect-error Known Google string options retain their value kind.
  57. Image.generate({ model: googleProvider, prompt: "A lighthouse", options: { imageSize: 2 } })
  58. // @ts-expect-error Known Google numeric options retain their value kind.
  59. Image.generate({ model: googleProvider, prompt: "A lighthouse", options: { seed: "42" } })
  60. // @ts-expect-error Known Google boolean options retain their value kind.
  61. Image.generate({ model: googleProvider, prompt: "A lighthouse", options: { includeThoughts: "yes" } })
  62. const openai = OpenAI.image("gpt-image-2")
  63. // @ts-expect-error Image generation options are request-scoped, not provider configuration.
  64. OpenAI.configure({ image: { options: { quality: "medium" } } })
  65. const futureOpenAIOptions: ImageModelOptions<typeof openai> = { quality: "future-quality" }
  66. void futureOpenAIOptions
  67. Image.generate({
  68. model: openai,
  69. prompt: "A lighthouse",
  70. images: [ImageInput.url("https://example.com/source.png"), ImageInput.file("file_123")],
  71. options: {
  72. mask: ImageInput.bytes(Uint8Array.from([1]), "image/png"),
  73. quality: "hd",
  74. outputFormat: "webp",
  75. size: "2048x2048",
  76. future_option: true,
  77. },
  78. })
  79. Image.generate({ model: openai, prompt: "A lighthouse", options: { quality: "future-quality", size: "256x256" } })
  80. Image.generate({ model: openai, prompt: "A lighthouse", options: { size: "1792x1024" } })
  81. Image.generate({ model: openai, prompt: "A lighthouse", options: { native_future_option: true } })
  82. // @ts-expect-error Known OpenAI string options retain their value kind.
  83. Image.generate({ model: openai, prompt: "A lighthouse", options: { quality: 1 } })
  84. // @ts-expect-error Known OpenAI numeric options retain their value kind.
  85. Image.generate({ model: openai, prompt: "A lighthouse", options: { outputCompression: "80" } })
  86. OpenAI.imageGeneration({ action: "future-action", quality: "future-quality", size: "2048x2048" })
  87. // @ts-expect-error Hosted image generation numeric options retain their value kind.
  88. OpenAI.imageGeneration({ partialImages: "2" })
  89. // @ts-expect-error Known Google-like options are inferred from the selected model.
  90. Image.generate({ model: google, prompt: "A lighthouse", options: { aspectRatio: "wide" } })
  91. const xai = XAI.configure({ apiKey: "test" }).image("any-model-id")
  92. // @ts-expect-error Image generation options are request-scoped, not provider configuration.
  93. XAI.configure({ image: { options: { resolution: "1k" } } })
  94. Image.generate({
  95. model: xai,
  96. prompt: "A lighthouse",
  97. images: [ImageInput.url("data:image/png;base64,AQID"), ImageInput.file("file_123")],
  98. options: {
  99. n: 2,
  100. aspectRatio: "future-ratio",
  101. resolution: "future-resolution",
  102. responseFormat: "future-format",
  103. future_option: true,
  104. },
  105. })
  106. Image.generate({
  107. model: xai,
  108. prompt: "A lighthouse",
  109. options: { aspect_ratio: "16:9", response_format: "b64_json", native_future_option: true },
  110. })
  111. // @ts-expect-error Known xAI numeric options retain their value kind.
  112. Image.generate({ model: xai, prompt: "A lighthouse", options: { n: "2" } })
  113. // @ts-expect-error Known xAI string options retain their value kind.
  114. Image.generate({ model: xai, prompt: "A lighthouse", options: { resolution: 2 } })
  115. const zai = ZAI.configure({ apiKey: "test" }).image("any-model-id")
  116. // @ts-expect-error Image generation options are request-scoped, not provider configuration.
  117. ZAI.configure({ image: { options: { quality: "hd" } } })
  118. Image.generate({
  119. model: zai,
  120. prompt: "A lighthouse",
  121. options: { quality: "future-quality", userID: "user-123", future_option: true },
  122. })
  123. Image.generate({ model: zai, prompt: "A lighthouse", options: { user_id: "raw-user" } })
  124. // @ts-expect-error Known Z.ai string options retain their value kind.
  125. Image.generate({ model: zai, prompt: "A lighthouse", options: { quality: 1 } })
  126. // @ts-expect-error Known Z.ai user IDs retain their value kind.
  127. Image.generate({ model: zai, prompt: "A lighthouse", options: { userID: 1 } })
  128. declare const generic: ImageModel<ImageOptions>
  129. Image.generate({ model: generic, prompt: "A lighthouse", options: { arbitrary: true } })
  130. const explicitImageInput: ImageInput = ImageInput.url("https://example.com/image.png")
  131. void explicitImageInput
  132. // @ts-expect-error Raw strings are ambiguous and are not image inputs.
  133. Image.generate({ model: openai, prompt: "A lighthouse", images: ["AQID"] })
  134. // @ts-expect-error Byte image inputs require an explicit MIME type.
  135. Image.generate({ model: openai, prompt: "A lighthouse", images: [{ type: "bytes", data: new Uint8Array() }] })
  136. // @ts-expect-error File URIs require an explicit MIME type for Gemini fileData.
  137. Image.generate({ model: google, prompt: "A lighthouse", images: [{ type: "file-uri", uri: "files/123" }] })
  138. const request = Image.request({
  139. model: google,
  140. prompt: "A lighthouse",
  141. options: { aspectRatio: "1:1", futureOption: true },
  142. })
  143. const typedRequest: ImageRequestFor<GoogleLikeOptions> = request
  144. void typedRequest
  145. const generated = ImageClient.generate(request)
  146. type GenerateRequirements = Assert<Equal<Requirements<typeof generated>, Service>>
  147. void (true satisfies GenerateRequirements)
  148. // @ts-expect-error Image requests no longer expose a common count option.
  149. Image.generate({ model: openai, prompt: "A lighthouse", count: 2 })
  150. // @ts-expect-error Image requests no longer expose a common size option.
  151. Image.generate({ model: openai, prompt: "A lighthouse", size: { width: 1024, height: 1024 } })
  152. // @ts-expect-error Image requests no longer expose a common aspectRatio option.
  153. Image.generate({ model: openai, prompt: "A lighthouse", aspectRatio: "16:9" })
  154. // @ts-expect-error Image requests no longer expose a common seed option.
  155. Image.generate({ model: openai, prompt: "A lighthouse", seed: 1 })
  156. // @ts-expect-error Image requests do not expose metadata.
  157. Image.generate({ model: openai, prompt: "A lighthouse", metadata: { trace: true } })
  158. // @ts-expect-error Masks are provider options, not a common image request field.
  159. Image.generate({ model: openai, prompt: "A lighthouse", mask: ImageInput.url("https://example.com/mask.png") })