image.types.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 { 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 openai = OpenAI.image("gpt-image-2")
  57. // @ts-expect-error Image generation options are request-scoped, not provider configuration.
  58. OpenAI.configure({ image: { options: { quality: "medium" } } })
  59. const futureOpenAIOptions: ImageModelOptions<typeof openai> = { quality: "future-quality" }
  60. void futureOpenAIOptions
  61. Image.generate({
  62. model: openai,
  63. prompt: "A lighthouse",
  64. images: [ImageInput.url("https://example.com/source.png"), ImageInput.file("file_123")],
  65. options: {
  66. mask: ImageInput.bytes(Uint8Array.from([1]), "image/png"),
  67. quality: "hd",
  68. outputFormat: "webp",
  69. size: "2048x2048",
  70. future_option: true,
  71. },
  72. })
  73. Image.generate({ model: openai, prompt: "A lighthouse", options: { quality: "future-quality", size: "256x256" } })
  74. Image.generate({ model: openai, prompt: "A lighthouse", options: { size: "1792x1024" } })
  75. Image.generate({ model: openai, prompt: "A lighthouse", options: { native_future_option: true } })
  76. // @ts-expect-error Known OpenAI string options retain their value kind.
  77. Image.generate({ model: openai, prompt: "A lighthouse", options: { quality: 1 } })
  78. // @ts-expect-error Known OpenAI numeric options retain their value kind.
  79. Image.generate({ model: openai, prompt: "A lighthouse", options: { outputCompression: "80" } })
  80. OpenAI.imageGeneration({ action: "future-action", quality: "future-quality", size: "2048x2048" })
  81. // @ts-expect-error Hosted image generation numeric options retain their value kind.
  82. OpenAI.imageGeneration({ partialImages: "2" })
  83. // @ts-expect-error Known Google-like options are inferred from the selected model.
  84. Image.generate({ model: google, prompt: "A lighthouse", options: { aspectRatio: "wide" } })
  85. const xai = XAI.configure({ apiKey: "test" }).image("any-model-id")
  86. // @ts-expect-error Image generation options are request-scoped, not provider configuration.
  87. XAI.configure({ image: { options: { resolution: "1k" } } })
  88. Image.generate({
  89. model: xai,
  90. prompt: "A lighthouse",
  91. images: [ImageInput.url("data:image/png;base64,AQID"), ImageInput.file("file_123")],
  92. options: {
  93. n: 2,
  94. aspectRatio: "future-ratio",
  95. resolution: "future-resolution",
  96. responseFormat: "future-format",
  97. future_option: true,
  98. },
  99. })
  100. Image.generate({
  101. model: xai,
  102. prompt: "A lighthouse",
  103. options: { aspect_ratio: "16:9", response_format: "b64_json", native_future_option: true },
  104. })
  105. // @ts-expect-error Known xAI numeric options retain their value kind.
  106. Image.generate({ model: xai, prompt: "A lighthouse", options: { n: "2" } })
  107. // @ts-expect-error Known xAI string options retain their value kind.
  108. Image.generate({ model: xai, prompt: "A lighthouse", options: { resolution: 2 } })
  109. const zai = ZAI.configure({ apiKey: "test" }).image("any-model-id")
  110. // @ts-expect-error Image generation options are request-scoped, not provider configuration.
  111. ZAI.configure({ image: { options: { quality: "hd" } } })
  112. Image.generate({
  113. model: zai,
  114. prompt: "A lighthouse",
  115. options: { quality: "future-quality", userID: "user-123", future_option: true },
  116. })
  117. Image.generate({ model: zai, prompt: "A lighthouse", options: { user_id: "raw-user" } })
  118. // @ts-expect-error Known Z.ai string options retain their value kind.
  119. Image.generate({ model: zai, prompt: "A lighthouse", options: { quality: 1 } })
  120. // @ts-expect-error Known Z.ai user IDs retain their value kind.
  121. Image.generate({ model: zai, prompt: "A lighthouse", options: { userID: 1 } })
  122. declare const generic: ImageModel<ImageOptions>
  123. Image.generate({ model: generic, prompt: "A lighthouse", options: { arbitrary: true } })
  124. const explicitImageInput: ImageInput = ImageInput.url("https://example.com/image.png")
  125. void explicitImageInput
  126. // @ts-expect-error Raw strings are ambiguous and are not image inputs.
  127. Image.generate({ model: openai, prompt: "A lighthouse", images: ["AQID"] })
  128. // @ts-expect-error Byte image inputs require an explicit MIME type.
  129. Image.generate({ model: openai, prompt: "A lighthouse", images: [{ type: "bytes", data: new Uint8Array() }] })
  130. // @ts-expect-error File URIs require an explicit MIME type for Gemini fileData.
  131. Image.generate({ model: google, prompt: "A lighthouse", images: [{ type: "file-uri", uri: "files/123" }] })
  132. const request = Image.request({
  133. model: google,
  134. prompt: "A lighthouse",
  135. options: { aspectRatio: "1:1", futureOption: true },
  136. })
  137. const typedRequest: ImageRequestFor<GoogleLikeOptions> = request
  138. void typedRequest
  139. // @ts-expect-error Image requests no longer expose a common count option.
  140. Image.generate({ model: openai, prompt: "A lighthouse", count: 2 })
  141. // @ts-expect-error Image requests no longer expose a common size option.
  142. Image.generate({ model: openai, prompt: "A lighthouse", size: { width: 1024, height: 1024 } })
  143. // @ts-expect-error Image requests no longer expose a common aspectRatio option.
  144. Image.generate({ model: openai, prompt: "A lighthouse", aspectRatio: "16:9" })
  145. // @ts-expect-error Image requests no longer expose a common seed option.
  146. Image.generate({ model: openai, prompt: "A lighthouse", seed: 1 })
  147. // @ts-expect-error Image requests do not expose metadata.
  148. Image.generate({ model: openai, prompt: "A lighthouse", metadata: { trace: true } })
  149. // @ts-expect-error Masks are provider options, not a common image request field.
  150. Image.generate({ model: openai, prompt: "A lighthouse", mask: ImageInput.url("https://example.com/mask.png") })