← All @molecule/* packages · App templates
@molecule/api-ai-image-generationCore interface · ai-image-generation · API (Node) · v1.0.1 · Apache-2.0
AI image-generation core interface — generate images from text prompts, with optional edit/image-to-image/upscale, via swappable providers.
npm install @molecule/api-ai-image-generation@molecule/api-ai-image-generation is the ai-image-generation core interface on the API (Node) side: the API your app calls, with no vendor inside.
Choose the implementation by bonding one of its 2 providers: @molecule/api-ai-image-generation-openai, @molecule/api-ai-image-generation-stability.
import { setProvider, requireProvider } from '@molecule/api-ai-image-generation'
import { createProvider } from '@molecule/api-ai-image-generation-openai'
// Wire at startup. See the bond package for its config/env (e.g. OPENAI_API_KEY).
setProvider(createProvider())
// Use anywhere after startup.
const { images } = await requireProvider().generate({
prompt: 'A watercolor fox reading a book',
size: '1024x1024',
responseFormat: 'base64',
})
// images[0] may carry url, base64, or data — handle what the bonded provider returns.Providers (2): @molecule/api-ai-image-generation-openai, @molecule/api-ai-image-generation-stability
Works with: @molecule/api-bond
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
AI image-generation core interface for molecule.dev.
Defines the AIImageGenerationProvider contract — generate images from text
prompts, plus optional edit (inpainting), image-to-image, and upscale
operations — and the accessor (setProvider/getProvider/hasProvider/
requireProvider). Interface-only: bond a provider package (e.g.
@molecule/api-ai-image-generation-openai,
@molecule/api-ai-image-generation-stability).
import { setProvider, requireProvider } from '@molecule/api-ai-image-generation'
import { createProvider } from '@molecule/api-ai-image-generation-openai'
// Wire at startup. See the bond package for its config/env (e.g. OPENAI_API_KEY).
setProvider(createProvider())
// Use anywhere after startup.
const { images } = await requireProvider().generate({
prompt: 'A watercolor fox reading a book',
size: '1024x1024',
responseFormat: 'base64',
})
// images[0] may carry url, base64, or data — handle what the bonded provider returns.
core
npm install @molecule/api-ai-image-generation @molecule/api-bond
AIImageGenerationConfigBase configuration for image generation providers.
interface AIImageGenerationConfig {
/** API key for the image generation service. */
apiKey?: string
/** Default model to use for generation. */
defaultModel?: string
/** Base URL override (for proxies or self-hosted endpoints). */
baseUrl?: string
/** Additional provider-specific options. */
[key: string]: unknown
}
AIImageGenerationProviderAIImageGeneration provider interface.
Providers generate images from text prompts, edit existing images with text-guided inpainting, transform images, and perform upscaling operations.
interface AIImageGenerationProvider {
/** Provider name identifier. */
readonly name: string
/**
* Generate images from a text prompt.
*
* @param params - Generation parameters including prompt, model, size, and quality.
* @returns Generated image(s) with metadata.
*/
generate(params: ImageGenerateParams): Promise<ImageGenerationResult>
/**
* Generate images from a text prompt (Stability AI-style params).
*
* @param params - Generation parameters including prompt, dimensions, and model.
* @returns Generated images with metadata.
*/
generateImage?(params: GenerateImageParams): Promise<ImageGenerationResult>
/**
* Edit an existing image using a text prompt and optional mask.
* Not all providers support this operation.
*
* @param params - Edit parameters including source image, prompt, and optional mask.
* @returns Edited image(s) with metadata.
*/
edit?(params: ImageEditParams): Promise<ImageGenerationResult>
/**
* Transform an existing image guided by a text prompt (image-to-image).
*
* @param params - Transformation parameters including source image, prompt, and strength.
* @returns Transformed images with metadata.
*/
imageToImage?(params: ImageToImageParams): Promise<ImageGenerationResult>
/**
* Upscale an image to a higher resolution.
*
* @param params - Upscaling parameters including source image and target dimensions.
* @returns Upscaled image with metadata.
*/
upscale?(params: UpscaleImageParams): Promise<ImageGenerationResult>
}
GeneratedImageA single generated or edited image.
interface GeneratedImage {
/** URL of the generated image (when responseFormat is 'url'). */
url?: string
/** Base64-encoded image data (when responseFormat is 'base64'). */
base64?: string
/** The prompt after provider revision (some providers rewrite prompts for safety/quality). */
revisedPrompt?: string
/** The image data as a Buffer (when returned as raw data). */
data?: Buffer
/** The MIME type of the image (e.g. 'image/png'). */
mimeType?: string
/** The seed used to generate this image, for reproducibility. */
seed?: number
}
GenerateImageParamsParameters for text-to-image generation (Stability AI-style).
interface GenerateImageParams {
/** The text prompt describing the desired image. */
prompt: string
/** Negative prompt — things to exclude from the image. */
negativePrompt?: string
/** Model to use for generation (provider-specific). */
model?: string
/** Width of the generated image in pixels. */
width?: number
/** Height of the generated image in pixels. */
height?: number
/** Number of images to generate. Defaults to 1. */
count?: number
/** Random seed for reproducible generation. */
seed?: number
/** Guidance scale / CFG scale — how closely to follow the prompt. */
guidanceScale?: number
/** Number of inference steps. Higher = more detail, slower. */
steps?: number
/** Output format. Defaults to 'png'. */
outputFormat?: ImageOutputFormat
/** Aspect ratio as a string (e.g. '16:9', '1:1'). Alternative to width/height. */
aspectRatio?: string
/** Style preset (provider-specific, e.g. 'photographic', 'anime'). */
stylePreset?: string
}
ImageEditParamsParameters for editing an existing image with a text prompt.
interface ImageEditParams {
/** The source image to edit, as a Buffer of PNG data or a base64-encoded string. */
image: Buffer | string
/** Text description of the desired edits. */
prompt: string
/** Optional mask indicating areas to edit (white = edit, black = keep). */
mask?: Buffer | string
/** Model to use for editing (provider-specific). */
model?: string
/** Number of edited images to generate. */
n?: number
/** Output image size as a "widthxheight" string. */
size?: string
/** Format of the returned image data. */
responseFormat?: ImageResponseFormat
}
ImageGenerateParamsParameters for generating images from a text prompt.
interface ImageGenerateParams {
/** Text description of the desired image(s). */
prompt: string
/** Model to use for generation (provider-specific). */
model?: string
/** Number of images to generate. */
n?: number
/** Image size as a "widthxheight" string (e.g. "1024x1024"). */
size?: string
/** Quality level (provider-specific, e.g. "standard", "hd", "high"). */
quality?: string
/** Style preset (provider-specific, e.g. "vivid", "natural"). */
style?: string
/** Format of the returned image data. */
responseFormat?: ImageResponseFormat
}
ImageGenerationResultResult of an image generation or edit request.
interface ImageGenerationResult {
/** The generated images. */
images: GeneratedImage[]
/** The model that produced the images. */
model: string
}
ImageToImageParamsParameters for image-to-image transformation.
interface ImageToImageParams extends GenerateImageParams {
/** The source image as a Buffer or base64-encoded string. */
image: Buffer | string
/** How much to transform the source image (0.0 = no change, 1.0 = full generation). */
strength?: number
}
UpscaleImageParamsParameters for image upscaling.
interface UpscaleImageParams {
/** The source image as a Buffer or base64-encoded string. */
image: Buffer | string
/** The desired output width in pixels. */
width?: number
/** The desired output height in pixels. */
height?: number
/** The upscale factor (e.g. 2, 4). Alternative to explicit width/height. */
factor?: number
/** Prompt to guide the upscaling (if supported). */
prompt?: string
/** Output format. Defaults to 'png'. */
outputFormat?: ImageOutputFormat
}
ImageOutputFormatSupported output image formats.
type ImageOutputFormat = 'png' | 'jpeg' | 'webp'
ImageResponseFormatFormat of the generated image data returned by the provider.
type ImageResponseFormat = 'url' | 'base64'
getProvider()Returns the bonded AI image generation provider, or null if none is registered.
function getProvider(): AIImageGenerationProvider | null
Returns: The active provider, or null.
hasProvider()Returns whether an AI image generation provider has been registered.
function hasProvider(): boolean
Returns: true if a provider is bonded.
requireProvider()Returns the bonded AI image generation provider, throwing if none is configured.
function requireProvider(): AIImageGenerationProvider
Returns: The active provider.
setProvider(provider)Registers the AI image generation provider singleton.
function setProvider(provider: AIImageGenerationProvider): void
provider — The AI image generation provider implementation to register.| Provider | Package |
|---|---|
| Ai Image Generation | @molecule/api-ai-image-generation-openai |
| Ai Image Generation | @molecule/api-ai-image-generation-stability |
Peer dependencies:
@molecule/api-bond ^1.0.1@molecule/api-bond
Wire it at startup with setProvider(...) — or the equivalent
bond('ai-image-generation', provider). This core routes through the shared
@molecule/api-bond registry, so either call registers the same provider and
validateBonds() reports it as missing when unwired.
Feature-detect the optional methods. Only generate() is required;
edit/imageToImage/upscale/generateImage are optional — guard with
if (provider.edit) and surface "not supported" instead of calling
unconditionally (an absent method is a runtime TypeError that type-checks).
Two param dialects exist; only prompt (+ model) is portable. The core
ships ImageGenerateParams (size: '1024x1024', quality, n) AND
Stability-style GenerateImageParams (width/height, negativePrompt,
count, steps). A provider honors ITS dialect and silently ignores the
other's fields — check the bonded provider's docs before relying on anything
beyond prompt.
Handle every result shape, and persist what you must keep. A
GeneratedImage may carry url, base64, or raw data bytes — provider URLs
are typically short-lived, so download and store (e.g. via the uploads bond)
anything the app needs to keep.
Server-side only, gated and budgeted. Keep the provider key on the API; require auth and rate-limit user-triggered generation — every image is billed.
Integration checklist — drive the real UI (live preview, no mocks), adapt each item to this app's actual screens/flows, and check every box off one by one. A box you can't check is an integration bug to fix — not a skip:
img element actually
decoded (its naturalWidth/naturalHeight are non-zero) and the picture
visibly reflects the prompt (a "red bicycle" prompt shows a red bicycle).img src (and any saved record) points at the app's uploads/storage, not
the provider's temporary URL. Provider url results expire — reload the
page (or revisit the record later) and the image must still load. Download
the provider result server-side and persist it (uploads bond); never
hotlink the provider URL — it 404s later and can leak the API request
context. (base64/data results must likewise be saved, not held only in
the response.)n) of N renders N
images. If the UI exposes no such options, this box is n/a — say so.