Open History to see the last commit
1import { useGeneration } from './use-generation'
2import { reconstructImageResult } from '@tanstack/ai-client'
3import type {
4 UseGenerationOptions,
5 UseGenerationReturn,
6} from './use-generation'
7import type { ImageGenerationResult, StreamChunk } from '@tanstack/ai'
8import type {
9 AIDevtoolsDisplayOptions,
10 ConnectConnectionAdapter,
11 GenerationClientState,
12 GenerationFetcher,
13 GenerationPersistenceOptions,
14 ImageGenerateInput,
15 InferGenerationOutputFromReturn,
16} from '@tanstack/ai-client'
17import type { Accessor } from 'solid-js'
18
19/**
20 * Options for the useGenerateImage hook.
21 *
22 * @template TOutput - The transformed output type (defaults to ImageGenerationResult)
23 */
24export interface UseGenerateImageOptions<
25 TOutput = ImageGenerationResult,
26> extends Pick<
27 UseGenerationOptions<ImageGenerateInput, ImageGenerationResult, TOutput>,
28 | 'persistence'
29 | 'threadId'
30 | 'hydrateGeneration'
31 | 'joinRun'
32 | 'byok'
33 | 'byokProvider'
34> {
35 /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
36 connection?: ConnectConnectionAdapter
37 /** Direct async function for image generation */
38 fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>
39 /** Additional body parameters to send with connect-based adapter requests */
40 body?: Record<string, any>
41 /** Display options for TanStack AI Devtools. */
42 devtools?: AIDevtoolsDisplayOptions
43 /**
44 * Callback when images are generated. Can optionally return a transformed value.
45 *
46 * - Return a non-null value to transform and store it as the result
47 * - Return `null` to keep the previous result unchanged
48 * - Return nothing (`void`) to store the raw result as-is
49 */
50 onResult?: (result: ImageGenerationResult) => TOutput | null | void
51 /** Callback when an error occurs */
52 onError?: (error: Error) => void
53 /** Callback when progress is reported (0-100) */
54 onProgress?: (progress: number, message?: string) => void
55 /** Callback for each stream chunk (connect-based adapter mode only) */
56 onChunk?: (chunk: StreamChunk) => void
57}
58
59/**
60 * Return type for the useGenerateImage hook.
61 *
62 * @template TOutput - The transformed output type (defaults to ImageGenerationResult)
63 */
64export interface UseGenerateImageReturn<
65 TOutput = ImageGenerationResult,
66> extends Omit<UseGenerationReturn<TOutput>, 'generate'> {
67 /** Trigger image generation */
68 generate: (input: ImageGenerateInput) => Promise<void>
69 /** The generation result containing images, or null */
70 result: Accessor<TOutput | null>
71 /** Whether generation is in progress */
72 isLoading: Accessor<boolean>
73 /** Current error, if any */
74 error: Accessor<Error | undefined>
75 /** Current state of the generation */
76 status: Accessor<GenerationClientState>
77}
78
79/**
80 * Solid hook for generating images using AI models.
81 *
82 * Supports two transport modes:
83 * - **ConnectConnectionAdapter** -- Streaming transport (SSE, HTTP stream, custom)
84 * - **Fetcher** -- Direct async function call
85 *
86 * @example
87 * ```tsx
88 * import { useGenerateImage } from '@tanstack/ai-solid'
89 * import { fetchServerSentEvents } from '@tanstack/ai-client'
90 *
91 * function ImageGenerator() {
92 * const { generate, result, isLoading, error, reset } = useGenerateImage({
93 * connection: fetchServerSentEvents('/api/generate/image'),
94 * })
95 *
96 * return (
97 * <div>
98 * <button onClick={() => generate({ prompt: 'A sunset over mountains' })}>
99 * Generate
100 * </button>
101 * {isLoading() && <p>Generating...</p>}
102 * {error() && <p>Error: {error()!.message}</p>}
103 * {result()?.images.map((img, i) => (
104 * <img src={img.url || `data:image/png;base64,${img.b64Json}`} />
105 * ))}
106 * </div>
107 * )
108 * }
109 * ```
110 */
111export function useGenerateImage<TTransformed = void>(
112 options: Omit<
113 UseGenerateImageOptions,
114 'onResult' | 'persistence' | 'threadId'
115 > & {
116 onResult?: (result: ImageGenerationResult) => TTransformed
117 } & GenerationPersistenceOptions,
118): UseGenerateImageReturn<
119 InferGenerationOutputFromReturn<ImageGenerationResult, TTransformed>
120> {
121 const devtools = {
122 ...options.devtools,
123 framework: 'solid',
124 hookName: 'useGenerateImage',
125 outputKind: 'image' as const,
126 }
127 const generation = useGeneration<
128 ImageGenerateInput,
129 ImageGenerationResult,
130 TTransformed
131 >({
132 ...options,
133 devtools,
134 reconstructResult: reconstructImageResult,
135 })
136
137 return generation
138}