{"version":3,"file":"imageGeneration.cjs","names":[],"sources":["../../src/tools/imageGeneration.ts"],"sourcesContent":["import { OpenAI as OpenAIClient } from \"openai\";\nimport type { ServerTool } from \"@langchain/core/tools\";\n\n/**\n * Optional mask for inpainting. Allows you to specify areas of the image\n * that should be regenerated.\n */\nexport interface ImageGenerationInputMask {\n  /**\n   * Base64-encoded mask image URL.\n   */\n  imageUrl?: string;\n  /**\n   * File ID for the mask image (uploaded via OpenAI File API).\n   */\n  fileId?: string;\n}\n\n/**\n * Options for the Image Generation tool.\n */\nexport interface ImageGenerationOptions {\n  /**\n   * Whether to generate a new image or edit an existing image.\n   * - `generate`: Generate a new image from scratch\n   * - `edit`: Edit an existing image\n   * - `auto`: Let the model decide based on the input\n   * @default \"auto\"\n   */\n  action?: \"generate\" | \"edit\" | \"auto\";\n\n  /**\n   * Background type for the generated image.\n   * - `transparent`: Generate image with transparent background\n   * - `opaque`: Generate image with opaque background\n   * - `auto`: Let the model decide based on the prompt\n   * @default \"auto\"\n   */\n  background?: \"transparent\" | \"opaque\" | \"auto\";\n\n  /**\n   * Control how much effort the model will exert to match the style and features,\n   * especially facial features, of input images. This parameter is only supported\n   * for `gpt-image-1`. Unsupported for `gpt-image-1-mini`.\n   * - `high`: Higher fidelity to input images\n   * - `low`: Lower fidelity to input images\n   * @default \"low\"\n   */\n  inputFidelity?: \"high\" | \"low\";\n\n  /**\n   * Optional mask for inpainting. Use this to specify areas of an image\n   * that should be regenerated.\n   */\n  inputImageMask?: ImageGenerationInputMask;\n\n  /**\n   * The image generation model to use.\n   * @default \"gpt-image-1\"\n   */\n  model?: \"gpt-image-1\" | \"gpt-image-1-mini\" | \"gpt-image-1.5\";\n\n  /**\n   * Moderation level for the generated image.\n   * - `auto`: Standard moderation\n   * - `low`: Less restrictive moderation\n   * @default \"auto\"\n   */\n  moderation?: \"auto\" | \"low\";\n\n  /**\n   * Compression level for the output image (0-100).\n   * Only applies to JPEG and WebP formats.\n   * @default 100\n   */\n  outputCompression?: number;\n\n  /**\n   * The output format of the generated image.\n   * @default \"png\"\n   */\n  outputFormat?: \"png\" | \"webp\" | \"jpeg\";\n\n  /**\n   * Number of partial images to generate in streaming mode (0-3).\n   * When set, the model will return partial images as they are generated,\n   * providing faster visual feedback.\n   * @default 0\n   */\n  partialImages?: number;\n\n  /**\n   * The quality of the generated image.\n   * - `low`: Faster generation, lower quality\n   * - `medium`: Balanced generation time and quality\n   * - `high`: Slower generation, higher quality\n   * - `auto`: Let the model decide based on the prompt\n   * @default \"auto\"\n   */\n  quality?: \"low\" | \"medium\" | \"high\" | \"auto\";\n\n  /**\n   * The size of the generated image.\n   * - `1024x1024`: Square format\n   * - `1024x1536`: Portrait format\n   * - `1536x1024`: Landscape format\n   * - `auto`: Let the model decide based on the prompt\n   * @default \"auto\"\n   */\n  size?: \"1024x1024\" | \"1024x1536\" | \"1536x1024\" | \"auto\";\n}\n\n/**\n * OpenAI Image Generation tool type for the Responses API.\n */\nexport type ImageGenerationTool = OpenAIClient.Responses.Tool.ImageGeneration;\n\n/**\n * Converts input mask options to the API format.\n */\nfunction convertInputImageMask(\n  mask: ImageGenerationInputMask | undefined\n): ImageGenerationTool[\"input_image_mask\"] {\n  if (!mask) return undefined;\n  return {\n    image_url: mask.imageUrl,\n    file_id: mask.fileId,\n  };\n}\n\n/**\n * Creates an Image Generation tool that allows models to generate or edit images\n * using text prompts and optional image inputs.\n *\n * The image generation tool leverages the GPT Image model and automatically\n * optimizes text inputs for improved performance. When included in a request,\n * the model can decide when and how to generate images as part of the conversation.\n *\n * **Key Features**:\n * - Generate images from text descriptions\n * - Edit existing images with text instructions\n * - Multi-turn image editing by referencing previous responses\n * - Configurable output options (size, quality, format)\n * - Streaming support for partial image generation\n *\n * **Prompting Tips**:\n * - Use terms like \"draw\" or \"edit\" in your prompt for best results\n * - For combining images, say \"edit the first image by adding this element\" instead of \"combine\"\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-image-generation | OpenAI Image Generation Documentation}\n *\n * @param options - Configuration options for the Image Generation tool\n * @returns An Image Generation tool definition to be passed to the OpenAI Responses API\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n *\n * const model = new ChatOpenAI({ model: \"gpt-4o\" });\n *\n * // Basic usage - generate an image\n * const response = await model.invoke(\n *   \"Generate an image of a gray tabby cat hugging an otter with an orange scarf\",\n *   { tools: [tools.imageGeneration()] }\n * );\n *\n * // Access the generated image\n * const imageData = response.additional_kwargs.tool_outputs?.find(\n *   (output) => output.type === \"image_generation_call\"\n * );\n * if (imageData?.result) {\n *   // imageData.result contains the base64-encoded image\n *   const fs = await import(\"fs\");\n *   fs.writeFileSync(\"output.png\", Buffer.from(imageData.result, \"base64\"));\n * }\n *\n * // With custom options\n * const response = await model.invoke(\n *   \"Draw a beautiful sunset over mountains\",\n *   {\n *     tools: [tools.imageGeneration({\n *       size: \"1536x1024\",      // Landscape format\n *       quality: \"high\",        // Higher quality output\n *       outputFormat: \"jpeg\",   // JPEG format\n *       outputCompression: 90,  // 90% compression\n *     })]\n *   }\n * );\n *\n * // With transparent background\n * const response = await model.invoke(\n *   \"Create a logo with a transparent background\",\n *   {\n *     tools: [tools.imageGeneration({\n *       background: \"transparent\",\n *       outputFormat: \"png\",\n *     })]\n *   }\n * );\n *\n * // Force the model to use image generation\n * const response = await model.invoke(\n *   \"A serene lake at dawn\",\n *   {\n *     tools: [tools.imageGeneration()],\n *     tool_choice: { type: \"image_generation\" },\n *   }\n * );\n *\n * // Enable streaming with partial images\n * const response = await model.invoke(\n *   \"Draw a detailed fantasy castle\",\n *   {\n *     tools: [tools.imageGeneration({\n *       partialImages: 2,  // Get 2 partial images during generation\n *     })]\n *   }\n * );\n * ```\n *\n * @remarks\n * - Supported models: gpt-4o, gpt-4o-mini, gpt-4.1, gpt-4.1-mini, gpt-4.1-nano, o3\n * - The image generation process always uses `gpt-image-1` model internally\n * - The model will automatically revise prompts for improved performance\n * - Access the revised prompt via `revised_prompt` field in the output\n * - Multi-turn editing is supported by passing previous response messages\n */\nexport function imageGeneration(options?: ImageGenerationOptions): ServerTool {\n  return {\n    type: \"image_generation\",\n    action: options?.action,\n    background: options?.background,\n    input_fidelity: options?.inputFidelity,\n    input_image_mask: convertInputImageMask(options?.inputImageMask),\n    model: options?.model,\n    moderation: options?.moderation,\n    output_compression: options?.outputCompression,\n    output_format: options?.outputFormat,\n    partial_images: options?.partialImages,\n    quality: options?.quality,\n    size: options?.size,\n  } satisfies ImageGenerationTool;\n}\n"],"mappings":";;;;AAwHA,SAAS,sBACP,MACyC;AACzC,KAAI,CAAC,KAAM,QAAO,KAAA;AAClB,QAAO;EACL,WAAW,KAAK;EAChB,SAAS,KAAK;EACf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoGH,SAAgB,gBAAgB,SAA8C;AAC5E,QAAO;EACL,MAAM;EACN,QAAQ,SAAS;EACjB,YAAY,SAAS;EACrB,gBAAgB,SAAS;EACzB,kBAAkB,sBAAsB,SAAS,eAAe;EAChE,OAAO,SAAS;EAChB,YAAY,SAAS;EACrB,oBAAoB,SAAS;EAC7B,eAAe,SAAS;EACxB,gBAAgB,SAAS;EACzB,SAAS,SAAS;EAClB,MAAM,SAAS;EAChB"}