image.types.ts 8.1 KB

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