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