image-generation.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { createProviderToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils"
  2. import { z } from "zod/v4"
  3. export const imageGenerationArgsSchema = z
  4. .object({
  5. background: z.enum(["auto", "opaque", "transparent"]).optional(),
  6. inputFidelity: z.enum(["low", "high"]).optional(),
  7. inputImageMask: z
  8. .object({
  9. fileId: z.string().optional(),
  10. imageUrl: z.string().optional(),
  11. })
  12. .optional(),
  13. model: z.string().optional(),
  14. moderation: z.enum(["auto"]).optional(),
  15. outputCompression: z.number().int().min(0).max(100).optional(),
  16. outputFormat: z.enum(["png", "jpeg", "webp"]).optional(),
  17. partialImages: z.number().int().min(0).max(3).optional(),
  18. quality: z.enum(["auto", "low", "medium", "high"]).optional(),
  19. size: z.enum(["1024x1024", "1024x1536", "1536x1024", "auto"]).optional(),
  20. })
  21. .strict()
  22. export const imageGenerationOutputSchema = z.object({
  23. result: z.string(),
  24. })
  25. type ImageGenerationArgs = {
  26. /**
  27. * Background type for the generated image. Default is 'auto'.
  28. */
  29. background?: "auto" | "opaque" | "transparent"
  30. /**
  31. * Input fidelity for the generated image. Default is 'low'.
  32. */
  33. inputFidelity?: "low" | "high"
  34. /**
  35. * Optional mask for inpainting.
  36. * Contains image_url (string, optional) and file_id (string, optional).
  37. */
  38. inputImageMask?: {
  39. /**
  40. * File ID for the mask image.
  41. */
  42. fileId?: string
  43. /**
  44. * Base64-encoded mask image.
  45. */
  46. imageUrl?: string
  47. }
  48. /**
  49. * The image generation model to use. Default: gpt-image-1.
  50. */
  51. model?: string
  52. /**
  53. * Moderation level for the generated image. Default: auto.
  54. */
  55. moderation?: "auto"
  56. /**
  57. * Compression level for the output image. Default: 100.
  58. */
  59. outputCompression?: number
  60. /**
  61. * The output format of the generated image. One of png, webp, or jpeg.
  62. * Default: png
  63. */
  64. outputFormat?: "png" | "jpeg" | "webp"
  65. /**
  66. * Number of partial images to generate in streaming mode, from 0 (default value) to 3.
  67. */
  68. partialImages?: number
  69. /**
  70. * The quality of the generated image.
  71. * One of low, medium, high, or auto. Default: auto.
  72. */
  73. quality?: "auto" | "low" | "medium" | "high"
  74. /**
  75. * The size of the generated image.
  76. * One of 1024x1024, 1024x1536, 1536x1024, or auto.
  77. * Default: auto.
  78. */
  79. size?: "auto" | "1024x1024" | "1024x1536" | "1536x1024"
  80. }
  81. const imageGenerationToolFactory = createProviderToolFactoryWithOutputSchema<
  82. {},
  83. {
  84. /**
  85. * The generated image encoded in base64.
  86. */
  87. result: string
  88. },
  89. ImageGenerationArgs
  90. >({
  91. id: "openai.image_generation",
  92. inputSchema: z.object({}),
  93. outputSchema: imageGenerationOutputSchema,
  94. })
  95. export const imageGeneration = (
  96. args: ImageGenerationArgs = {}, // default
  97. ) => {
  98. return imageGenerationToolFactory(args)
  99. }