import { Accessor } from "solid-js"; import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, GenerationPersistenceOptions, GenerationRestoredResult, InferGenerationOutputFromReturn } from "@tanstack/ai-client"; import { ByokClient } from "@tanstack/ai-client/byok"; import { StreamChunk } from "@tanstack/ai"; import { ProviderId } from "@tanstack/ai/byok"; //#region src/use-generation.d.ts /** * Options for the useGeneration hook. * * Accepts either a `connection` (streaming transport) or a `fetcher` (direct async call). * * @template TInput - The input type for the generation request * @template TResult - The result type returned by the generation * @template TOutput - The transformed output type (defaults to TResult) */ interface UseGenerationOptions { /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */ connection?: ConnectConnectionAdapter; /** Direct async function for one-shot generation (no streaming protocol needed) */ fetcher?: GenerationFetcher; /** Additional body parameters to send with connect-based adapter requests */ body?: Record; /** Optional BYOK keyring. Keys go in `x-byok-*` headers, never the body. */ byok?: ByokClient; /** Optional provider id. If it returns a slug, only that key is sent. If no slug resolves (`byokProvider`, then `body.provider`), generate throws. */ byokProvider?: () => ProviderId | undefined; /** Display options for TanStack AI Devtools. */ devtools?: AIDevtoolsDisplayOptions; /** * How this generation persists across reloads. * - Omit / `false`: ephemeral, in-memory only. * - `true`: server-driven — on mount the client hydrates the last generation * for its `threadId` from the server (needs a connection with a * `hydrateGeneration` handler) and repaints it; it never auto-starts a run. */ persistence?: boolean; /** * The **scope** this generation belongs to: a stable, app-chosen name for the * slot successive runs fill — not a link to a chat conversation. * * The hook starts empty and produces many runs over its life; each gets its * own `runId`, but all belong to one scope. Persistence keys on this, so * derive it from your own domain and keep it identical across reloads (e.g. * `` `video-${videoId}-start-frame` ``). It is also sent as the AG-UI thread * id on the wire, which the protocol requires. * * **Required whenever `persistence` is set** — an app that cannot name the * scope has nothing to restore to. Optional for ephemeral generations. If * omitted, the client mints a wire id after mount. */ threadId?: string; /** * Server-driven hydration handler for `persistence: true` when the * connection doesn't carry one (e.g. alongside `fetcher`, or a `stream()` / * `rpcStream()` adapter built without handlers) — typically a one-line * server-function call. The connection's own handler takes precedence. */ hydrateGeneration?: ConnectConnectionAdapter['hydrateGeneration']; /** * Re-attach handler that replays a run still generating to completion on * mount, when the connection doesn't carry one. Without it, a restored * `running` snapshot surfaces as an (interrupted) error. The connection's * own handler takes precedence. */ joinRun?: ConnectConnectionAdapter['joinRun']; /** * Callback when a result is received. 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: TResult) => 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; /** * @internal Rebuild a typed result from a restored snapshot, injected by each * specialized hook (image / speech / audio / transcription / summarize). * Forwarded to the client so a server-hydrate restore repaints `result`. */ reconstructResult?: (restored: GenerationRestoredResult) => TResult | null; } /** * Return type for the useGeneration hook. * * @template TOutput - The output type (possibly transformed from the raw result) * @template TInput - The input type accepted by `generate` (defaults to any object) */ interface UseGenerationReturn = Record> { /** Trigger a generation request */ generate: (input: TInput) => Promise; /** The generation result, or null if not yet generated */ result: Accessor; /** Whether a generation is currently in progress */ isLoading: Accessor; /** Current error, if any */ error: Accessor; /** Current state of the generation client */ status: Accessor; /** Abort the current generation */ stop: () => void; /** Clear result, error, and return to idle */ reset: () => void; /** * The id of the generation job currently running, or `null` when nothing is in * flight. Each call to `generate` is one job with its own id. Pass it to your * own endpoint to cancel or poll the provider job — `stop()` only aborts the * local stream, it does not stop work already running on the provider. */ runId: Accessor; } /** * Generic Solid hook for one-shot generation tasks. * * This is the base hook used by `useGenerateImage`, `useGenerateSpeech`, * `useTranscription`, and `useSummarize`. You can also use it directly * for custom generation types. * * @template TInput - The input type for the generation request * @template TResult - The result type returned by the generation * @template TOutput - The transformed output type (defaults to TResult) * * @example * ```tsx * const { generate, result, isLoading } = useGeneration({ * connection: fetchServerSentEvents('/api/generate/custom'), * }) * * await generate({ prompt: 'Hello' }) * ``` */ declare function useGeneration, TResult, TTransformed = void>(options: Omit, 'onResult' | 'persistence' | 'threadId'> & { onResult?: (result: TResult) => TTransformed; } & GenerationPersistenceOptions): UseGenerationReturn, TInput>; //#endregion export { UseGenerationOptions, UseGenerationReturn, useGeneration }; //# sourceMappingURL=use-generation.d.ts.map