import { useGeneration } from './use-generation' import { reconstructImageResult } from '@tanstack/ai-client' import type { UseGenerationOptions, UseGenerationReturn, } from './use-generation' import type { ImageGenerationResult, StreamChunk } from '@tanstack/ai' import type { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, GenerationPersistenceOptions, ImageGenerateInput, InferGenerationOutputFromReturn, } from '@tanstack/ai-client' import type { Accessor } from 'solid-js' /** * Options for the useGenerateImage hook. * * @template TOutput - The transformed output type (defaults to ImageGenerationResult) */ export interface UseGenerateImageOptions< TOutput = ImageGenerationResult, > extends Pick< UseGenerationOptions, | 'persistence' | 'threadId' | 'hydrateGeneration' | 'joinRun' | 'byok' | 'byokProvider' > { /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */ connection?: ConnectConnectionAdapter /** Direct async function for image generation */ fetcher?: GenerationFetcher /** Additional body parameters to send with connect-based adapter requests */ body?: Record /** Display options for TanStack AI Devtools. */ devtools?: AIDevtoolsDisplayOptions /** * Callback when images are generated. Can optionally return a transformed value. * * - Return a non-null value to transform and store it as the result * - Return `null` to keep the previous result unchanged * - Return nothing (`void`) to store the raw result as-is */ onResult?: (result: ImageGenerationResult) => TOutput | null | void /** Callback when an error occurs */ onError?: (error: Error) => void /** Callback when progress is reported (0-100) */ onProgress?: (progress: number, message?: string) => void /** Callback for each stream chunk (connect-based adapter mode only) */ onChunk?: (chunk: StreamChunk) => void } /** * Return type for the useGenerateImage hook. * * @template TOutput - The transformed output type (defaults to ImageGenerationResult) */ export interface UseGenerateImageReturn< TOutput = ImageGenerationResult, > extends Omit, 'generate'> { /** Trigger image generation */ generate: (input: ImageGenerateInput) => Promise /** The generation result containing images, or null */ result: Accessor /** Whether generation is in progress */ isLoading: Accessor /** Current error, if any */ error: Accessor /** Current state of the generation */ status: Accessor } /** * Solid hook for generating images using AI models. * * Supports two transport modes: * - **ConnectConnectionAdapter** -- Streaming transport (SSE, HTTP stream, custom) * - **Fetcher** -- Direct async function call * * @example * ```tsx * import { useGenerateImage } from '@tanstack/ai-solid' * import { fetchServerSentEvents } from '@tanstack/ai-client' * * function ImageGenerator() { * const { generate, result, isLoading, error, reset } = useGenerateImage({ * connection: fetchServerSentEvents('/api/generate/image'), * }) * * return ( *
* * {isLoading() &&

Generating...

} * {error() &&

Error: {error()!.message}

} * {result()?.images.map((img, i) => ( * * ))} *
* ) * } * ``` */ export function useGenerateImage( options: Omit< UseGenerateImageOptions, 'onResult' | 'persistence' | 'threadId' > & { onResult?: (result: ImageGenerationResult) => TTransformed } & GenerationPersistenceOptions, ): UseGenerateImageReturn< InferGenerationOutputFromReturn > { const devtools = { ...options.devtools, framework: 'solid', hookName: 'useGenerateImage', outputKind: 'image' as const, } const generation = useGeneration< ImageGenerateInput, ImageGenerationResult, TTransformed >({ ...options, devtools, reconstructResult: reconstructImageResult, }) return generation }