@tanstack/ai-react 0.19.3 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -5
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/types.d.ts +1 -1
- package/dist/esm/use-chat.js +4 -2
- package/dist/esm/use-chat.js.map +1 -1
- package/dist/esm/use-generate-audio.d.ts +3 -7
- package/dist/esm/use-generate-audio.js.map +1 -1
- package/dist/esm/use-generate-image.d.ts +3 -7
- package/dist/esm/use-generate-image.js.map +1 -1
- package/dist/esm/use-generate-speech.d.ts +3 -7
- package/dist/esm/use-generate-speech.js.map +1 -1
- package/dist/esm/use-generate-video.d.ts +3 -7
- package/dist/esm/use-generate-video.js +7 -3
- package/dist/esm/use-generate-video.js.map +1 -1
- package/dist/esm/use-generation.d.ts +3 -7
- package/dist/esm/use-generation.js +7 -3
- package/dist/esm/use-generation.js.map +1 -1
- package/dist/esm/use-summarize.d.ts +3 -7
- package/dist/esm/use-summarize.js.map +1 -1
- package/dist/esm/use-transcription.d.ts +3 -7
- package/dist/esm/use-transcription.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +2 -0
- package/src/types.ts +0 -4
- package/src/use-chat.ts +14 -10
- package/src/use-generate-audio.ts +3 -7
- package/src/use-generate-image.ts +3 -7
- package/src/use-generate-speech.ts +3 -7
- package/src/use-generate-video.ts +22 -15
- package/src/use-generation.ts +21 -15
- package/src/use-summarize.ts +3 -7
- package/src/use-transcription.ts +3 -7
|
@@ -14,10 +14,6 @@ export interface UseGenerationOptions<TInput, TResult, TOutput = TResult> {
|
|
|
14
14
|
connection?: ConnectConnectionAdapter;
|
|
15
15
|
/** Direct async function for one-shot generation (no streaming protocol needed) */
|
|
16
16
|
fetcher?: GenerationFetcher<TInput, TResult>;
|
|
17
|
-
/**
|
|
18
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
19
|
-
*/
|
|
20
|
-
id?: string;
|
|
21
17
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
22
18
|
body?: Record<string, any>;
|
|
23
19
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -41,8 +37,8 @@ export interface UseGenerationOptions<TInput, TResult, TOutput = TResult> {
|
|
|
41
37
|
* id on the wire, which the protocol requires.
|
|
42
38
|
*
|
|
43
39
|
* **Required whenever `persistence` is set** — an app that cannot name the
|
|
44
|
-
* scope has nothing to restore to. Optional for ephemeral generations
|
|
45
|
-
*
|
|
40
|
+
* scope has nothing to restore to. Optional for ephemeral generations. If
|
|
41
|
+
* omitted, the client mints a wire id after mount.
|
|
46
42
|
*/
|
|
47
43
|
threadId?: string;
|
|
48
44
|
/**
|
|
@@ -128,6 +124,6 @@ export interface UseGenerationReturn<TOutput, TInput extends Record<string, any>
|
|
|
128
124
|
* await generate({ prompt: 'Hello' })
|
|
129
125
|
* ```
|
|
130
126
|
*/
|
|
131
|
-
export declare function useGeneration<TInput extends Record<string, any>, TResult, TTransformed = void>(options: Omit<UseGenerationOptions<TInput, TResult>, 'onResult' | 'persistence' | 'threadId'
|
|
127
|
+
export declare function useGeneration<TInput extends Record<string, any>, TResult, TTransformed = void>(options: Omit<UseGenerationOptions<TInput, TResult>, 'onResult' | 'persistence' | 'threadId'> & {
|
|
132
128
|
onResult?: (result: TResult) => TTransformed;
|
|
133
129
|
} & GenerationPersistenceOptions): UseGenerationReturn<InferGenerationOutputFromReturn<TResult, TTransformed>, TInput>;
|
|
@@ -23,7 +23,7 @@ import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react"
|
|
|
23
23
|
*/
|
|
24
24
|
function useGeneration(options) {
|
|
25
25
|
const hookId = useId();
|
|
26
|
-
const clientIdentity = options.threadId ??
|
|
26
|
+
const clientIdentity = options.threadId ?? hookId;
|
|
27
27
|
const [result, setResult] = useState(null);
|
|
28
28
|
const [isLoading, setIsLoading] = useState(false);
|
|
29
29
|
const [error, setError] = useState(void 0);
|
|
@@ -36,8 +36,6 @@ function useGeneration(options) {
|
|
|
36
36
|
const opts = optionsRef.current;
|
|
37
37
|
const clientOptions = {
|
|
38
38
|
body: opts.body,
|
|
39
|
-
...opts.threadId !== void 0 ? { threadId: opts.threadId } : { id: opts.id ?? hookId },
|
|
40
|
-
...opts.persistence !== void 0 && { persistence: opts.persistence },
|
|
41
39
|
...opts.hydrateGeneration !== void 0 && { hydrateGeneration: opts.hydrateGeneration },
|
|
42
40
|
...opts.joinRun !== void 0 && { joinRun: opts.joinRun },
|
|
43
41
|
...opts.reconstructResult ? { reconstructResult: opts.reconstructResult } : {},
|
|
@@ -73,12 +71,18 @@ function useGeneration(options) {
|
|
|
73
71
|
if (!disposedRef.current) setRunId(rs?.runId ?? null);
|
|
74
72
|
}
|
|
75
73
|
};
|
|
74
|
+
const persistenceProps = typeof opts.threadId === "string" && opts.persistence ? {
|
|
75
|
+
persistence: opts.persistence,
|
|
76
|
+
threadId: opts.threadId
|
|
77
|
+
} : { ...opts.threadId !== void 0 && { threadId: opts.threadId } };
|
|
76
78
|
if (opts.connection) return new GenerationClient({
|
|
77
79
|
...clientOptions,
|
|
80
|
+
...persistenceProps,
|
|
78
81
|
connection: opts.connection
|
|
79
82
|
});
|
|
80
83
|
if (opts.fetcher) return new GenerationClient({
|
|
81
84
|
...clientOptions,
|
|
85
|
+
...persistenceProps,
|
|
82
86
|
fetcher: opts.fetcher
|
|
83
87
|
});
|
|
84
88
|
throw new Error("useGeneration requires either a connection or fetcher option");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-generation.js","names":[],"sources":["../../src/use-generation.ts"],"sourcesContent":["import { GenerationClient } from '@tanstack/ai-client'\nimport { createGenerationDevtoolsBridge } from '@tanstack/ai-client/devtools'\nimport { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport type { StreamChunk } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientOptions,\n GenerationClientState,\n GenerationFetcher,\n GenerationPersistenceOptions,\n GenerationRestoredResult,\n InferGenerationOutputFromReturn,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useGeneration hook.\n *\n * Accepts either a `connection` (streaming transport) or a `fetcher` (direct async call).\n *\n * @template TInput - The input type for the generation request\n * @template TResult - The result type returned by the generation\n * @template TOutput - The output type after optional transform (defaults to TResult)\n */\nexport interface UseGenerationOptions<TInput, TResult, TOutput = TResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for one-shot generation (no streaming protocol needed) */\n fetcher?: GenerationFetcher<TInput, TResult>\n /**\n * @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).\n */\n id?: string\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * How this generation persists across reloads.\n * - Omit / `false`: ephemeral, in-memory only.\n * - `true`: server-driven — on mount the client hydrates the last generation\n * for its `threadId` from the server (needs a connection with a\n * `hydrateGeneration` handler) and repaints it; it never auto-starts a run.\n */\n persistence?: boolean\n /**\n * The **scope** this generation belongs to: a stable, app-chosen name for the\n * slot successive runs fill — not a link to a chat conversation.\n *\n * The hook starts empty and produces many runs over its life; each gets its\n * own `runId`, but all belong to one scope. Persistence keys on this, so\n * derive it from your own domain and keep it identical across reloads (e.g.\n * `` `video-${videoId}-start-frame` ``). It is also sent as the AG-UI thread\n * id on the wire, which the protocol requires.\n *\n * **Required whenever `persistence` is set** — an app that cannot name the\n * scope has nothing to restore to. Optional for ephemeral generations, where\n * it falls back to `id` purely to satisfy the wire.\n */\n threadId?: string\n /**\n * Server-driven hydration handler for `persistence: true` when the\n * connection doesn't carry one (e.g. alongside `fetcher`, or a `stream()` /\n * `rpcStream()` adapter built without handlers) — typically a one-line\n * server-function call. The connection's own handler takes precedence.\n */\n hydrateGeneration?: ConnectConnectionAdapter['hydrateGeneration']\n /**\n * Re-attach handler that replays a run still generating to completion on\n * mount, when the connection doesn't carry one. Without it, a restored\n * `running` snapshot surfaces as an (interrupted) error. The connection's\n * own handler takes precedence.\n */\n joinRun?: ConnectConnectionAdapter['joinRun']\n /**\n * Callback when a result is received. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: TResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n /**\n * @internal Rebuild a typed result from a restored snapshot, injected by each\n * specialized hook (image / speech / audio / transcription / summarize).\n * Forwarded to the client so a server-hydrate restore repaints `result`.\n */\n reconstructResult?: (restored: GenerationRestoredResult) => TResult | null\n}\n\n/**\n * Return type for the useGeneration hook.\n *\n * @template TOutput - The output type (after optional transform)\n * @template TInput - The input type accepted by `generate` (defaults to any object)\n */\nexport interface UseGenerationReturn<\n TOutput,\n TInput extends Record<string, any> = Record<string, any>,\n> {\n /** Trigger a generation request */\n generate: (input: TInput) => Promise<void>\n /** The generation result, or null if not yet generated */\n result: TOutput | null\n /** Whether a generation is currently in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation client */\n status: GenerationClientState\n /** Abort the current generation */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n /**\n * The id of the generation job currently running, or `null` when nothing is in\n * flight. Each call to `generate` is one job with its own id. Pass it to your\n * own endpoint to cancel or poll the provider job — `stop()` only aborts the\n * local stream, it does not stop work already running on the provider.\n */\n runId: string | null\n}\n\n/**\n * Generic React hook for one-shot generation tasks.\n *\n * This is the base hook used by `useGenerateImage`, `useGenerateSpeech`,\n * `useTranscription`, and `useSummarize`. You can also use it directly\n * for custom generation types.\n *\n * @template TInput - The input type for the generation request\n * @template TResult - The result type returned by the generation\n *\n * @example\n * ```tsx\n * const { generate, result, isLoading } = useGeneration<MyInput, MyResult>({\n * connection: fetchServerSentEvents('/api/generate/custom'),\n * })\n *\n * await generate({ prompt: 'Hello' })\n * ```\n */\n// `TTransformed` infers from the `onResult` return position (a covariant\n// inference site that works even for an optional nested property), which types\n// the callback parameter as `TResult` and narrows `result`. Inferring the\n// whole callback as a defaulted type parameter instead collapses to the\n// default, leaving the parameter `any` — a hard error under `strict`. See\n// issue #848.\nexport function useGeneration<\n TInput extends Record<string, any>,\n TResult,\n TTransformed = void,\n>(\n options: Omit<\n UseGenerationOptions<TInput, TResult>,\n 'onResult' | 'persistence' | 'threadId' | 'id'\n > & {\n onResult?: (result: TResult) => TTransformed\n } & GenerationPersistenceOptions,\n): UseGenerationReturn<\n InferGenerationOutputFromReturn<TResult, TTransformed>,\n TInput\n> {\n type TOutput = InferGenerationOutputFromReturn<TResult, TTransformed>\n const hookId = useId()\n // Single identity: prefer `threadId`; deprecated `id` only when no threadId.\n const clientIdentity = options.threadId ?? options.id ?? hookId\n\n const [result, setResult] = useState<TOutput | null>(null)\n const [isLoading, setIsLoading] = useState(false)\n const [error, setError] = useState<Error | undefined>(undefined)\n const [status, setStatus] = useState<GenerationClientState>('idle')\n const [runId, setRunId] = useState<string | null>(null)\n\n const optionsRef = useRef(options)\n optionsRef.current = options\n const disposedRef = useRef(false)\n\n const client = useMemo(() => {\n const opts = optionsRef.current\n\n // Conditional spread for `body` (strict-optional in target;\n // local source is `Record<string, any> | undefined`). Callbacks\n // wrap optional ones in non-returning bodies so `?.()`'s\n // implicit `undefined` doesn't pollute the function return type.\n // Identity: pass `threadId` alone when set (never also pass deprecated `id`).\n const clientOptions: GenerationClientOptions<TInput, TResult, TOutput> = {\n body: opts.body,\n ...(opts.threadId !== undefined\n ? { threadId: opts.threadId }\n : { id: opts.id ?? hookId }),\n ...(opts.persistence !== undefined && { persistence: opts.persistence }),\n ...(opts.hydrateGeneration !== undefined && {\n hydrateGeneration: opts.hydrateGeneration,\n }),\n ...(opts.joinRun !== undefined && { joinRun: opts.joinRun }),\n ...(opts.reconstructResult\n ? { reconstructResult: opts.reconstructResult }\n : {}),\n devtoolsBridgeFactory: createGenerationDevtoolsBridge,\n devtools: {\n hookName: 'useGeneration',\n framework: 'react',\n ...opts.devtools,\n },\n // The transform's raw return type (`TTransformed`) and the stored output\n // (`TOutput`, with null/void/undefined stripped) are identical at runtime;\n // the cast bridges the relationship that the conditional type hides.\n onResult: ((r: TResult) => optionsRef.current.onResult?.(r)) as (\n result: TResult,\n ) => TOutput | null | void,\n onError: (e: Error) => {\n if (!disposedRef.current) optionsRef.current.onError?.(e)\n },\n onProgress: (p: number, m?: string) => {\n if (!disposedRef.current) optionsRef.current.onProgress?.(p, m)\n },\n onChunk: (c: StreamChunk) => {\n if (!disposedRef.current) optionsRef.current.onChunk?.(c)\n },\n onResultChange: (r) => {\n if (!disposedRef.current) setResult(r)\n },\n onLoadingChange: (l) => {\n if (!disposedRef.current) setIsLoading(l)\n },\n onErrorChange: (e) => {\n if (!disposedRef.current) setError(e)\n },\n onStatusChange: (s) => {\n if (!disposedRef.current) setStatus(s)\n },\n onResumeStateChange: (rs) => {\n if (!disposedRef.current) setRunId(rs?.runId ?? null)\n },\n }\n\n if (opts.connection) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n connection: opts.connection,\n })\n }\n\n if (opts.fetcher) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n fetcher: opts.fetcher,\n })\n }\n\n throw new Error(\n 'useGeneration requires either a connection or fetcher option',\n )\n }, [clientIdentity, hookId])\n\n // Sync body changes without recreating client\n useEffect(() => {\n // Conditional spread: target uses strict-optional `body?: T`.\n client.updateOptions({\n ...(options.body !== undefined && { body: options.body }),\n })\n }, [client, options.body])\n\n // Mount devtools and clean up on unmount. Generation runs are never\n // auto-started on mount — persisted state is only displayed. Mounting\n // revives the client after a StrictMode dispose → remount replay.\n useEffect(() => {\n disposedRef.current = false\n client.mountDevtools()\n\n return () => {\n disposedRef.current = true\n client.dispose()\n }\n }, [client])\n\n const generate = useCallback(\n async (input: TInput) => {\n await client.generate(input)\n },\n [client],\n )\n\n const stop = useCallback(() => {\n client.stop()\n }, [client])\n\n const reset = useCallback(() => {\n client.reset()\n }, [client])\n\n return {\n generate,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n runId,\n }\n}\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"use-generation.js","names":[],"sources":["../../src/use-generation.ts"],"sourcesContent":["import { GenerationClient } from '@tanstack/ai-client'\nimport { createGenerationDevtoolsBridge } from '@tanstack/ai-client/devtools'\nimport { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport type { StreamChunk } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientOptions,\n GenerationClientState,\n GenerationFetcher,\n GenerationPersistenceOptions,\n GenerationRestoredResult,\n InferGenerationOutputFromReturn,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useGeneration hook.\n *\n * Accepts either a `connection` (streaming transport) or a `fetcher` (direct async call).\n *\n * @template TInput - The input type for the generation request\n * @template TResult - The result type returned by the generation\n * @template TOutput - The output type after optional transform (defaults to TResult)\n */\nexport interface UseGenerationOptions<TInput, TResult, TOutput = TResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for one-shot generation (no streaming protocol needed) */\n fetcher?: GenerationFetcher<TInput, TResult>\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * How this generation persists across reloads.\n * - Omit / `false`: ephemeral, in-memory only.\n * - `true`: server-driven — on mount the client hydrates the last generation\n * for its `threadId` from the server (needs a connection with a\n * `hydrateGeneration` handler) and repaints it; it never auto-starts a run.\n */\n persistence?: boolean\n /**\n * The **scope** this generation belongs to: a stable, app-chosen name for the\n * slot successive runs fill — not a link to a chat conversation.\n *\n * The hook starts empty and produces many runs over its life; each gets its\n * own `runId`, but all belong to one scope. Persistence keys on this, so\n * derive it from your own domain and keep it identical across reloads (e.g.\n * `` `video-${videoId}-start-frame` ``). It is also sent as the AG-UI thread\n * id on the wire, which the protocol requires.\n *\n * **Required whenever `persistence` is set** — an app that cannot name the\n * scope has nothing to restore to. Optional for ephemeral generations. If\n * omitted, the client mints a wire id after mount.\n */\n threadId?: string\n /**\n * Server-driven hydration handler for `persistence: true` when the\n * connection doesn't carry one (e.g. alongside `fetcher`, or a `stream()` /\n * `rpcStream()` adapter built without handlers) — typically a one-line\n * server-function call. The connection's own handler takes precedence.\n */\n hydrateGeneration?: ConnectConnectionAdapter['hydrateGeneration']\n /**\n * Re-attach handler that replays a run still generating to completion on\n * mount, when the connection doesn't carry one. Without it, a restored\n * `running` snapshot surfaces as an (interrupted) error. The connection's\n * own handler takes precedence.\n */\n joinRun?: ConnectConnectionAdapter['joinRun']\n /**\n * Callback when a result is received. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: TResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n /**\n * @internal Rebuild a typed result from a restored snapshot, injected by each\n * specialized hook (image / speech / audio / transcription / summarize).\n * Forwarded to the client so a server-hydrate restore repaints `result`.\n */\n reconstructResult?: (restored: GenerationRestoredResult) => TResult | null\n}\n\n/**\n * Return type for the useGeneration hook.\n *\n * @template TOutput - The output type (after optional transform)\n * @template TInput - The input type accepted by `generate` (defaults to any object)\n */\nexport interface UseGenerationReturn<\n TOutput,\n TInput extends Record<string, any> = Record<string, any>,\n> {\n /** Trigger a generation request */\n generate: (input: TInput) => Promise<void>\n /** The generation result, or null if not yet generated */\n result: TOutput | null\n /** Whether a generation is currently in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation client */\n status: GenerationClientState\n /** Abort the current generation */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n /**\n * The id of the generation job currently running, or `null` when nothing is in\n * flight. Each call to `generate` is one job with its own id. Pass it to your\n * own endpoint to cancel or poll the provider job — `stop()` only aborts the\n * local stream, it does not stop work already running on the provider.\n */\n runId: string | null\n}\n\n/**\n * Generic React hook for one-shot generation tasks.\n *\n * This is the base hook used by `useGenerateImage`, `useGenerateSpeech`,\n * `useTranscription`, and `useSummarize`. You can also use it directly\n * for custom generation types.\n *\n * @template TInput - The input type for the generation request\n * @template TResult - The result type returned by the generation\n *\n * @example\n * ```tsx\n * const { generate, result, isLoading } = useGeneration<MyInput, MyResult>({\n * connection: fetchServerSentEvents('/api/generate/custom'),\n * })\n *\n * await generate({ prompt: 'Hello' })\n * ```\n */\n// `TTransformed` infers from the `onResult` return position (a covariant\n// inference site that works even for an optional nested property), which types\n// the callback parameter as `TResult` and narrows `result`. Inferring the\n// whole callback as a defaulted type parameter instead collapses to the\n// default, leaving the parameter `any` — a hard error under `strict`. See\n// issue #848.\nexport function useGeneration<\n TInput extends Record<string, any>,\n TResult,\n TTransformed = void,\n>(\n options: Omit<\n UseGenerationOptions<TInput, TResult>,\n 'onResult' | 'persistence' | 'threadId'\n > & {\n onResult?: (result: TResult) => TTransformed\n } & GenerationPersistenceOptions,\n): UseGenerationReturn<\n InferGenerationOutputFromReturn<TResult, TTransformed>,\n TInput\n> {\n type TOutput = InferGenerationOutputFromReturn<TResult, TTransformed>\n const hookId = useId()\n // The hook identity is `threadId`. `hookId` is only a React recreation key.\n const clientIdentity = options.threadId ?? hookId\n\n const [result, setResult] = useState<TOutput | null>(null)\n const [isLoading, setIsLoading] = useState(false)\n const [error, setError] = useState<Error | undefined>(undefined)\n const [status, setStatus] = useState<GenerationClientState>('idle')\n const [runId, setRunId] = useState<string | null>(null)\n\n const optionsRef = useRef(options)\n optionsRef.current = options\n const disposedRef = useRef(false)\n\n const client = useMemo(() => {\n const opts = optionsRef.current\n\n // Conditional spread for `body` (strict-optional in target;\n // local source is `Record<string, any> | undefined`). Callbacks\n // wrap optional ones in non-returning bodies so `?.()`'s\n // implicit `undefined` doesn't pollute the function return type.\n const clientOptions: Omit<\n GenerationClientOptions<TInput, TResult, TOutput>,\n 'persistence' | 'threadId'\n > = {\n body: opts.body,\n ...(opts.hydrateGeneration !== undefined && {\n hydrateGeneration: opts.hydrateGeneration,\n }),\n ...(opts.joinRun !== undefined && { joinRun: opts.joinRun }),\n ...(opts.reconstructResult\n ? { reconstructResult: opts.reconstructResult }\n : {}),\n devtoolsBridgeFactory: createGenerationDevtoolsBridge,\n devtools: {\n hookName: 'useGeneration',\n framework: 'react',\n ...opts.devtools,\n },\n // The transform's raw return type (`TTransformed`) and the stored output\n // (`TOutput`, with null/void/undefined stripped) are identical at runtime;\n // the cast bridges the relationship that the conditional type hides.\n onResult: ((r: TResult) => optionsRef.current.onResult?.(r)) as (\n result: TResult,\n ) => TOutput | null | void,\n onError: (e: Error) => {\n if (!disposedRef.current) optionsRef.current.onError?.(e)\n },\n onProgress: (p: number, m?: string) => {\n if (!disposedRef.current) optionsRef.current.onProgress?.(p, m)\n },\n onChunk: (c: StreamChunk) => {\n if (!disposedRef.current) optionsRef.current.onChunk?.(c)\n },\n onResultChange: (r) => {\n if (!disposedRef.current) setResult(r)\n },\n onLoadingChange: (l) => {\n if (!disposedRef.current) setIsLoading(l)\n },\n onErrorChange: (e) => {\n if (!disposedRef.current) setError(e)\n },\n onStatusChange: (s) => {\n if (!disposedRef.current) setStatus(s)\n },\n onResumeStateChange: (rs) => {\n if (!disposedRef.current) setRunId(rs?.runId ?? null)\n },\n }\n\n const persistenceProps =\n typeof opts.threadId === 'string' && opts.persistence\n ? {\n persistence: opts.persistence,\n threadId: opts.threadId,\n }\n : {\n ...(opts.threadId !== undefined && { threadId: opts.threadId }),\n }\n\n if (opts.connection) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n ...persistenceProps,\n connection: opts.connection,\n })\n }\n\n if (opts.fetcher) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n ...persistenceProps,\n fetcher: opts.fetcher,\n })\n }\n\n throw new Error(\n 'useGeneration requires either a connection or fetcher option',\n )\n }, [clientIdentity, hookId])\n\n // Sync body changes without recreating client\n useEffect(() => {\n // Conditional spread: target uses strict-optional `body?: T`.\n client.updateOptions({\n ...(options.body !== undefined && { body: options.body }),\n })\n }, [client, options.body])\n\n // Mount devtools and clean up on unmount. Generation runs are never\n // auto-started on mount — persisted state is only displayed. Mounting\n // revives the client after a StrictMode dispose → remount replay.\n useEffect(() => {\n disposedRef.current = false\n client.mountDevtools()\n\n return () => {\n disposedRef.current = true\n client.dispose()\n }\n }, [client])\n\n const generate = useCallback(\n async (input: TInput) => {\n await client.generate(input)\n },\n [client],\n )\n\n const stop = useCallback(() => {\n client.stop()\n }, [client])\n\n const reset = useCallback(() => {\n client.reset()\n }, [client])\n\n return {\n generate,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n runId,\n }\n}\n"],"mappings":"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"}
|
|
@@ -10,10 +10,6 @@ export interface UseSummarizeOptions<TOutput = SummarizationResult> {
|
|
|
10
10
|
connection?: ConnectConnectionAdapter;
|
|
11
11
|
/** Direct async function for summarization */
|
|
12
12
|
fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>;
|
|
13
|
-
/**
|
|
14
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
15
|
-
*/
|
|
16
|
-
id?: string;
|
|
17
13
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
18
14
|
body?: Record<string, any>;
|
|
19
15
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -37,8 +33,8 @@ export interface UseSummarizeOptions<TOutput = SummarizationResult> {
|
|
|
37
33
|
* id on the wire, which the protocol requires.
|
|
38
34
|
*
|
|
39
35
|
* **Required whenever `persistence` is set** — an app that cannot name the
|
|
40
|
-
* scope has nothing to restore to. Optional for ephemeral generations
|
|
41
|
-
*
|
|
36
|
+
* scope has nothing to restore to. Optional for ephemeral generations. If
|
|
37
|
+
* omitted, the client mints a wire id after mount.
|
|
42
38
|
*/
|
|
43
39
|
threadId?: string;
|
|
44
40
|
/**
|
|
@@ -127,6 +123,6 @@ export interface UseSummarizeReturn<TOutput = SummarizationResult> {
|
|
|
127
123
|
* }
|
|
128
124
|
* ```
|
|
129
125
|
*/
|
|
130
|
-
export declare function useSummarize<TTransformed = void>(options: Omit<UseSummarizeOptions, 'onResult' | 'persistence' | 'threadId'
|
|
126
|
+
export declare function useSummarize<TTransformed = void>(options: Omit<UseSummarizeOptions, 'onResult' | 'persistence' | 'threadId'> & {
|
|
131
127
|
onResult?: (result: SummarizationResult) => TTransformed;
|
|
132
128
|
} & GenerationPersistenceOptions): UseSummarizeReturn<InferGenerationOutputFromReturn<SummarizationResult, TTransformed>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-summarize.js","names":[],"sources":["../../src/use-summarize.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport { reconstructSummarizeResult } from '@tanstack/ai-client'\nimport type { StreamChunk, SummarizationResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n GenerationPersistenceOptions,\n InferGenerationOutputFromReturn,\n SummarizeGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useSummarize hook.\n *\n * @template TOutput - The output type after optional transform (defaults to SummarizationResult)\n */\nexport interface UseSummarizeOptions<TOutput = SummarizationResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for summarization */\n fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>\n
|
|
1
|
+
{"version":3,"file":"use-summarize.js","names":[],"sources":["../../src/use-summarize.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport { reconstructSummarizeResult } from '@tanstack/ai-client'\nimport type { StreamChunk, SummarizationResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n GenerationPersistenceOptions,\n InferGenerationOutputFromReturn,\n SummarizeGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useSummarize hook.\n *\n * @template TOutput - The output type after optional transform (defaults to SummarizationResult)\n */\nexport interface UseSummarizeOptions<TOutput = SummarizationResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for summarization */\n fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * How this generation persists across reloads.\n * - Omit / `false`: ephemeral, in-memory only.\n * - `true`: server-driven — on mount the client hydrates the last generation\n * for its `threadId` from the server (needs a connection with a\n * `hydrateGeneration` handler) and repaints it; it never auto-starts a run.\n */\n persistence?: boolean\n /**\n * The **scope** this generation belongs to: a stable, app-chosen name for the\n * slot successive runs fill — not a link to a chat conversation.\n *\n * The hook starts empty and produces many runs over its life; each gets its\n * own `runId`, but all belong to one scope. Persistence keys on this, so\n * derive it from your own domain and keep it identical across reloads (e.g.\n * `` `video-${videoId}-start-frame` ``). It is also sent as the AG-UI thread\n * id on the wire, which the protocol requires.\n *\n * **Required whenever `persistence` is set** — an app that cannot name the\n * scope has nothing to restore to. Optional for ephemeral generations. If\n * omitted, the client mints a wire id after mount.\n */\n threadId?: string\n /**\n * Server-driven hydration handler for `persistence: true` when the\n * connection doesn't carry one (e.g. alongside `fetcher`, or a `stream()` /\n * `rpcStream()` adapter built without handlers) — typically a one-line\n * server-function call. The connection's own handler takes precedence.\n */\n hydrateGeneration?: ConnectConnectionAdapter['hydrateGeneration']\n /**\n * Re-attach handler that replays a run still generating to completion on\n * mount, when the connection doesn't carry one. Without it, a restored\n * `running` snapshot surfaces as an (interrupted) error. The connection's\n * own handler takes precedence.\n */\n joinRun?: ConnectConnectionAdapter['joinRun']\n /**\n * Callback when summarization is complete. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: SummarizationResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useSummarize hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseSummarizeReturn<TOutput = SummarizationResult> {\n /** Trigger summarization */\n generate: (input: SummarizeGenerateInput) => Promise<void>\n /** The summarization result, or null */\n result: TOutput | null\n /** Whether summarization is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current summarization */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n /**\n * The id of the generation job currently running, or `null` when nothing is in\n * flight. Each call to `generate` is one job with its own id. Pass it to your\n * own endpoint to cancel or poll the provider job — `stop()` only aborts the\n * local stream, it does not stop work already running on the provider.\n */\n runId: string | null\n}\n\n/**\n * React hook for summarizing text using AI models.\n *\n * @example\n * ```tsx\n * import { useSummarize } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function Summarizer() {\n * const { generate, result, isLoading } = useSummarize({\n * connection: fetchServerSentEvents('/api/summarize'),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({\n * text: 'Long article text...',\n * style: 'bullet-points',\n * maxLength: 200,\n * })}>\n * Summarize\n * </button>\n * {isLoading && <p>Summarizing...</p>}\n * {result && <p>{result.summary}</p>}\n * </div>\n * )\n * }\n * ```\n */\nexport function useSummarize<TTransformed = void>(\n options: Omit<\n UseSummarizeOptions,\n 'onResult' | 'persistence' | 'threadId'\n > & {\n onResult?: (result: SummarizationResult) => TTransformed\n } & GenerationPersistenceOptions,\n): UseSummarizeReturn<\n InferGenerationOutputFromReturn<SummarizationResult, TTransformed>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useSummarize',\n outputKind: 'text' as const,\n }\n const generation = useGeneration<\n SummarizeGenerateInput,\n SummarizationResult,\n TTransformed\n >({\n ...options,\n devtools,\n reconstructResult: reconstructSummarizeResult,\n })\n\n return generation\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0IA,SAAgB,aACd,SAQA;CACA,MAAM,WAAW;EACf,GAAG,QAAQ;EACX,WAAW;EACX,UAAU;EACV,YAAY;CACd;CAWA,OAVmB,cAIjB;EACA,GAAG;EACH;EACA,mBAAmB;CACrB,CAEO;AACT"}
|
|
@@ -10,10 +10,6 @@ export interface UseTranscriptionOptions<TOutput = TranscriptionResult> {
|
|
|
10
10
|
connection?: ConnectConnectionAdapter;
|
|
11
11
|
/** Direct async function for transcription */
|
|
12
12
|
fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>;
|
|
13
|
-
/**
|
|
14
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
15
|
-
*/
|
|
16
|
-
id?: string;
|
|
17
13
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
18
14
|
body?: Record<string, any>;
|
|
19
15
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -37,8 +33,8 @@ export interface UseTranscriptionOptions<TOutput = TranscriptionResult> {
|
|
|
37
33
|
* id on the wire, which the protocol requires.
|
|
38
34
|
*
|
|
39
35
|
* **Required whenever `persistence` is set** — an app that cannot name the
|
|
40
|
-
* scope has nothing to restore to. Optional for ephemeral generations
|
|
41
|
-
*
|
|
36
|
+
* scope has nothing to restore to. Optional for ephemeral generations. If
|
|
37
|
+
* omitted, the client mints a wire id after mount.
|
|
42
38
|
*/
|
|
43
39
|
threadId?: string;
|
|
44
40
|
/**
|
|
@@ -132,6 +128,6 @@ export interface UseTranscriptionReturn<TOutput = TranscriptionResult> {
|
|
|
132
128
|
* }
|
|
133
129
|
* ```
|
|
134
130
|
*/
|
|
135
|
-
export declare function useTranscription<TTransformed = void>(options: Omit<UseTranscriptionOptions, 'onResult' | 'persistence' | 'threadId'
|
|
131
|
+
export declare function useTranscription<TTransformed = void>(options: Omit<UseTranscriptionOptions, 'onResult' | 'persistence' | 'threadId'> & {
|
|
136
132
|
onResult?: (result: TranscriptionResult) => TTransformed;
|
|
137
133
|
} & GenerationPersistenceOptions): UseTranscriptionReturn<InferGenerationOutputFromReturn<TranscriptionResult, TTransformed>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-transcription.js","names":[],"sources":["../../src/use-transcription.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport { reconstructTranscriptionResult } from '@tanstack/ai-client'\nimport type { StreamChunk, TranscriptionResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n GenerationPersistenceOptions,\n InferGenerationOutputFromReturn,\n TranscriptionGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useTranscription hook.\n *\n * @template TOutput - The output type after optional transform (defaults to TranscriptionResult)\n */\nexport interface UseTranscriptionOptions<TOutput = TranscriptionResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for transcription */\n fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>\n
|
|
1
|
+
{"version":3,"file":"use-transcription.js","names":[],"sources":["../../src/use-transcription.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport { reconstructTranscriptionResult } from '@tanstack/ai-client'\nimport type { StreamChunk, TranscriptionResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n GenerationPersistenceOptions,\n InferGenerationOutputFromReturn,\n TranscriptionGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useTranscription hook.\n *\n * @template TOutput - The output type after optional transform (defaults to TranscriptionResult)\n */\nexport interface UseTranscriptionOptions<TOutput = TranscriptionResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for transcription */\n fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * How this generation persists across reloads.\n * - Omit / `false`: ephemeral, in-memory only.\n * - `true`: server-driven — on mount the client hydrates the last generation\n * for its `threadId` from the server (needs a connection with a\n * `hydrateGeneration` handler) and repaints it; it never auto-starts a run.\n */\n persistence?: boolean\n /**\n * The **scope** this generation belongs to: a stable, app-chosen name for the\n * slot successive runs fill — not a link to a chat conversation.\n *\n * The hook starts empty and produces many runs over its life; each gets its\n * own `runId`, but all belong to one scope. Persistence keys on this, so\n * derive it from your own domain and keep it identical across reloads (e.g.\n * `` `video-${videoId}-start-frame` ``). It is also sent as the AG-UI thread\n * id on the wire, which the protocol requires.\n *\n * **Required whenever `persistence` is set** — an app that cannot name the\n * scope has nothing to restore to. Optional for ephemeral generations. If\n * omitted, the client mints a wire id after mount.\n */\n threadId?: string\n /**\n * Server-driven hydration handler for `persistence: true` when the\n * connection doesn't carry one (e.g. alongside `fetcher`, or a `stream()` /\n * `rpcStream()` adapter built without handlers) — typically a one-line\n * server-function call. The connection's own handler takes precedence.\n */\n hydrateGeneration?: ConnectConnectionAdapter['hydrateGeneration']\n /**\n * Re-attach handler that replays a run still generating to completion on\n * mount, when the connection doesn't carry one. Without it, a restored\n * `running` snapshot surfaces as an (interrupted) error. The connection's\n * own handler takes precedence.\n */\n joinRun?: ConnectConnectionAdapter['joinRun']\n /**\n * Callback when transcription is complete. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: TranscriptionResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useTranscription hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseTranscriptionReturn<TOutput = TranscriptionResult> {\n /** Trigger transcription */\n generate: (input: TranscriptionGenerateInput) => Promise<void>\n /** The transcription result, or null */\n result: TOutput | null\n /** Whether transcription is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current transcription */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n /**\n * The id of the generation job currently running, or `null` when nothing is in\n * flight. Each call to `generate` is one job with its own id. Pass it to your\n * own endpoint to cancel or poll the provider job — `stop()` only aborts the\n * local stream, it does not stop work already running on the provider.\n */\n runId: string | null\n}\n\n/**\n * React hook for transcribing audio to text using AI models.\n *\n * @example\n * ```tsx\n * import { useTranscription } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function Transcriber() {\n * const { generate, result, isLoading } = useTranscription({\n * connection: fetchServerSentEvents('/api/transcribe'),\n * })\n *\n * const handleFile = (e: React.ChangeEvent<HTMLInputElement>) => {\n * const file = e.target.files?.[0]\n * if (file) {\n * const reader = new FileReader()\n * reader.onload = () => {\n * generate({ audio: reader.result as string, language: 'en' })\n * }\n * reader.readAsDataURL(file)\n * }\n * }\n *\n * return (\n * <div>\n * <input type=\"file\" accept=\"audio/*\" onChange={handleFile} />\n * {isLoading && <p>Transcribing...</p>}\n * {result && <p>{result.text}</p>}\n * </div>\n * )\n * }\n * ```\n */\nexport function useTranscription<TTransformed = void>(\n options: Omit<\n UseTranscriptionOptions,\n 'onResult' | 'persistence' | 'threadId'\n > & {\n onResult?: (result: TranscriptionResult) => TTransformed\n } & GenerationPersistenceOptions,\n): UseTranscriptionReturn<\n InferGenerationOutputFromReturn<TranscriptionResult, TTransformed>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useTranscription',\n outputKind: 'text' as const,\n }\n const generation = useGeneration<\n TranscriptionGenerateInput,\n TranscriptionResult,\n TTransformed\n >({ ...options, devtools, reconstructResult: reconstructTranscriptionResult })\n\n return generation\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+IA,SAAgB,iBACd,SAQA;CACA,MAAM,WAAW;EACf,GAAG,QAAQ;EACX,WAAW;EACX,UAAU;EACV,YAAY;CACd;CAOA,OANmB,cAIjB;EAAE,GAAG;EAAS;EAAU,mBAAmB;CAA+B,CAErE;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "React hooks for TanStack AI streaming chat, realtime voice, structured outputs, and media generation.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -49,13 +49,13 @@
|
|
|
49
49
|
"media-generation"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@tanstack/ai-client": "^0.
|
|
52
|
+
"@tanstack/ai-client": "^0.24.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@mcp-ui/client": "^7",
|
|
56
56
|
"@types/react": ">=18.0.0",
|
|
57
57
|
"react": ">=18.0.0",
|
|
58
|
-
"@tanstack/ai": "^0.
|
|
58
|
+
"@tanstack/ai": "^0.46.0"
|
|
59
59
|
},
|
|
60
60
|
"peerDependenciesMeta": {
|
|
61
61
|
"@mcp-ui/client": {
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"jsdom": "^27.4.0",
|
|
72
72
|
"react": "^19.2.3",
|
|
73
73
|
"vite": "^8.2.1",
|
|
74
|
-
"@tanstack/ai": "0.
|
|
74
|
+
"@tanstack/ai": "0.46.0"
|
|
75
75
|
},
|
|
76
76
|
"scripts": {
|
|
77
77
|
"clean": "premove ./build ./dist",
|
package/src/index.ts
CHANGED
|
@@ -84,6 +84,7 @@ export {
|
|
|
84
84
|
xhrHttpStream,
|
|
85
85
|
stream,
|
|
86
86
|
rpcStream,
|
|
87
|
+
webSocket,
|
|
87
88
|
createChatClientOptions,
|
|
88
89
|
createMcpAppBridge,
|
|
89
90
|
type McpAppBridge,
|
|
@@ -97,6 +98,7 @@ export {
|
|
|
97
98
|
type RunAgentInputContext,
|
|
98
99
|
type FetchConnectionOptions,
|
|
99
100
|
type XhrConnectionOptions,
|
|
101
|
+
type WebSocketConnectionOptions,
|
|
100
102
|
type InferChatMessages,
|
|
101
103
|
type GenerationClientState,
|
|
102
104
|
type ImageGenerateInput,
|
package/src/types.ts
CHANGED
|
@@ -97,10 +97,6 @@ export type UseChatOptions<
|
|
|
97
97
|
| 'onRunIdChange'
|
|
98
98
|
| 'context'
|
|
99
99
|
| 'devtools'
|
|
100
|
-
// `id` is not a hook option: the hook's identity is its `threadId`, which is
|
|
101
|
-
// also the persistence key. Persist across reloads by passing a stable
|
|
102
|
-
// `threadId`; there is no separate id to set.
|
|
103
|
-
| 'id'
|
|
104
100
|
> & {
|
|
105
101
|
/** Display options for TanStack AI Devtools. */
|
|
106
102
|
devtools?: AIDevtoolsDisplayOptions
|
package/src/use-chat.ts
CHANGED
|
@@ -39,10 +39,9 @@ export function useChat<
|
|
|
39
39
|
>(
|
|
40
40
|
options: UseChatOptions<TTools, TSchema, TContext>,
|
|
41
41
|
): UseChatReturn<TTools, TSchema> {
|
|
42
|
-
// The hook's identity is its `threadId
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
// `threadId` is given (an ephemeral chat), never a persistence key.
|
|
42
|
+
// The hook's identity is its `threadId`. Reload with the same `threadId`
|
|
43
|
+
// restores the same conversation. `hookId` is only a React recreation key
|
|
44
|
+
// when no `threadId` is given. It is never sent on the wire.
|
|
46
45
|
const hookId = useId()
|
|
47
46
|
const clientId = options.threadId ?? hookId
|
|
48
47
|
|
|
@@ -127,16 +126,21 @@ export function useChat<
|
|
|
127
126
|
devtoolsBridgeFactory: createChatDevtoolsBridge,
|
|
128
127
|
...transport,
|
|
129
128
|
initialMessages: messagesToUse,
|
|
129
|
+
...(typeof initialOptions.threadId === 'string' &&
|
|
130
|
+
initialOptions.persistence
|
|
131
|
+
? {
|
|
132
|
+
persistence: initialOptions.persistence,
|
|
133
|
+
threadId: initialOptions.threadId,
|
|
134
|
+
}
|
|
135
|
+
: {
|
|
136
|
+
...(initialOptions.threadId !== undefined && {
|
|
137
|
+
threadId: initialOptions.threadId,
|
|
138
|
+
}),
|
|
139
|
+
}),
|
|
130
140
|
...(initialOptions.body !== undefined && { body: initialOptions.body }),
|
|
131
|
-
...(initialOptions.threadId !== undefined && {
|
|
132
|
-
threadId: initialOptions.threadId,
|
|
133
|
-
}),
|
|
134
141
|
...(initialOptions.forwardedProps !== undefined && {
|
|
135
142
|
forwardedProps: initialOptions.forwardedProps,
|
|
136
143
|
}),
|
|
137
|
-
...(initialOptions.persistence !== undefined && {
|
|
138
|
-
persistence: initialOptions.persistence,
|
|
139
|
-
}),
|
|
140
144
|
...(initialOptions.initialResumeSnapshot !== undefined && {
|
|
141
145
|
initialResumeSnapshot: initialOptions.initialResumeSnapshot,
|
|
142
146
|
}),
|
|
@@ -21,10 +21,6 @@ export interface UseGenerateAudioOptions<TOutput = AudioGenerationResult> {
|
|
|
21
21
|
connection?: ConnectConnectionAdapter
|
|
22
22
|
/** Direct async function for audio generation */
|
|
23
23
|
fetcher?: GenerationFetcher<AudioGenerateInput, AudioGenerationResult>
|
|
24
|
-
/**
|
|
25
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
26
|
-
*/
|
|
27
|
-
id?: string
|
|
28
24
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
29
25
|
body?: Record<string, any>
|
|
30
26
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -48,8 +44,8 @@ export interface UseGenerateAudioOptions<TOutput = AudioGenerationResult> {
|
|
|
48
44
|
* id on the wire, which the protocol requires.
|
|
49
45
|
*
|
|
50
46
|
* **Required whenever `persistence` is set** — an app that cannot name the
|
|
51
|
-
* scope has nothing to restore to. Optional for ephemeral generations
|
|
52
|
-
*
|
|
47
|
+
* scope has nothing to restore to. Optional for ephemeral generations. If
|
|
48
|
+
* omitted, the client mints a wire id after mount.
|
|
53
49
|
*/
|
|
54
50
|
threadId?: string
|
|
55
51
|
/**
|
|
@@ -144,7 +140,7 @@ export interface UseGenerateAudioReturn<TOutput = AudioGenerationResult> {
|
|
|
144
140
|
export function useGenerateAudio<TTransformed = void>(
|
|
145
141
|
options: Omit<
|
|
146
142
|
UseGenerateAudioOptions,
|
|
147
|
-
'onResult' | 'persistence' | 'threadId'
|
|
143
|
+
'onResult' | 'persistence' | 'threadId'
|
|
148
144
|
> & {
|
|
149
145
|
onResult?: (result: AudioGenerationResult) => TTransformed
|
|
150
146
|
} & GenerationPersistenceOptions,
|
|
@@ -21,10 +21,6 @@ export interface UseGenerateImageOptions<TOutput = ImageGenerationResult> {
|
|
|
21
21
|
connection?: ConnectConnectionAdapter
|
|
22
22
|
/** Direct async function for image generation */
|
|
23
23
|
fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>
|
|
24
|
-
/**
|
|
25
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
26
|
-
*/
|
|
27
|
-
id?: string
|
|
28
24
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
29
25
|
body?: Record<string, any>
|
|
30
26
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -48,8 +44,8 @@ export interface UseGenerateImageOptions<TOutput = ImageGenerationResult> {
|
|
|
48
44
|
* id on the wire, which the protocol requires.
|
|
49
45
|
*
|
|
50
46
|
* **Required whenever `persistence` is set** — an app that cannot name the
|
|
51
|
-
* scope has nothing to restore to. Optional for ephemeral generations
|
|
52
|
-
*
|
|
47
|
+
* scope has nothing to restore to. Optional for ephemeral generations. If
|
|
48
|
+
* omitted, the client mints a wire id after mount.
|
|
53
49
|
*/
|
|
54
50
|
threadId?: string
|
|
55
51
|
/**
|
|
@@ -146,7 +142,7 @@ export interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {
|
|
|
146
142
|
export function useGenerateImage<TTransformed = void>(
|
|
147
143
|
options: Omit<
|
|
148
144
|
UseGenerateImageOptions,
|
|
149
|
-
'onResult' | 'persistence' | 'threadId'
|
|
145
|
+
'onResult' | 'persistence' | 'threadId'
|
|
150
146
|
> & {
|
|
151
147
|
onResult?: (result: ImageGenerationResult) => TTransformed
|
|
152
148
|
} & GenerationPersistenceOptions,
|
|
@@ -21,10 +21,6 @@ export interface UseGenerateSpeechOptions<TOutput = TTSResult> {
|
|
|
21
21
|
connection?: ConnectConnectionAdapter
|
|
22
22
|
/** Direct async function for speech generation */
|
|
23
23
|
fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>
|
|
24
|
-
/**
|
|
25
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
26
|
-
*/
|
|
27
|
-
id?: string
|
|
28
24
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
29
25
|
body?: Record<string, any>
|
|
30
26
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -48,8 +44,8 @@ export interface UseGenerateSpeechOptions<TOutput = TTSResult> {
|
|
|
48
44
|
* id on the wire, which the protocol requires.
|
|
49
45
|
*
|
|
50
46
|
* **Required whenever `persistence` is set** — an app that cannot name the
|
|
51
|
-
* scope has nothing to restore to. Optional for ephemeral generations
|
|
52
|
-
*
|
|
47
|
+
* scope has nothing to restore to. Optional for ephemeral generations. If
|
|
48
|
+
* omitted, the client mints a wire id after mount.
|
|
53
49
|
*/
|
|
54
50
|
threadId?: string
|
|
55
51
|
/**
|
|
@@ -140,7 +136,7 @@ export interface UseGenerateSpeechReturn<TOutput = TTSResult> {
|
|
|
140
136
|
export function useGenerateSpeech<TTransformed = void>(
|
|
141
137
|
options: Omit<
|
|
142
138
|
UseGenerateSpeechOptions,
|
|
143
|
-
'onResult' | 'persistence' | 'threadId'
|
|
139
|
+
'onResult' | 'persistence' | 'threadId'
|
|
144
140
|
> & {
|
|
145
141
|
onResult?: (result: TTSResult) => TTransformed
|
|
146
142
|
} & GenerationPersistenceOptions,
|
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
InferGenerationOutputFromReturn,
|
|
12
12
|
VideoGenerateInput,
|
|
13
13
|
VideoGenerateResult,
|
|
14
|
+
VideoGenerationClientOptions,
|
|
14
15
|
VideoStatusInfo,
|
|
15
16
|
} from '@tanstack/ai-client'
|
|
16
17
|
|
|
@@ -22,10 +23,6 @@ export interface UseGenerateVideoOptions<TOutput = VideoGenerateResult> {
|
|
|
22
23
|
connection?: ConnectConnectionAdapter
|
|
23
24
|
/** Direct async function that returns a completed video result */
|
|
24
25
|
fetcher?: GenerationFetcher<VideoGenerateInput, VideoGenerateResult>
|
|
25
|
-
/**
|
|
26
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
27
|
-
*/
|
|
28
|
-
id?: string
|
|
29
26
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
30
27
|
body?: Record<string, any>
|
|
31
28
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -49,8 +46,8 @@ export interface UseGenerateVideoOptions<TOutput = VideoGenerateResult> {
|
|
|
49
46
|
* id on the wire, which the protocol requires.
|
|
50
47
|
*
|
|
51
48
|
* **Required whenever `persistence` is set** — an app that cannot name the
|
|
52
|
-
* scope has nothing to restore to. Optional for ephemeral generations
|
|
53
|
-
*
|
|
49
|
+
* scope has nothing to restore to. Optional for ephemeral generations. If
|
|
50
|
+
* omitted, the client mints a wire id after mount.
|
|
54
51
|
*/
|
|
55
52
|
threadId?: string
|
|
56
53
|
/**
|
|
@@ -157,7 +154,7 @@ export interface UseGenerateVideoReturn<TOutput = VideoGenerateResult> {
|
|
|
157
154
|
export function useGenerateVideo<TTransformed = void>(
|
|
158
155
|
options: Omit<
|
|
159
156
|
UseGenerateVideoOptions,
|
|
160
|
-
'onResult' | 'persistence' | 'threadId'
|
|
157
|
+
'onResult' | 'persistence' | 'threadId'
|
|
161
158
|
> & {
|
|
162
159
|
onResult?: (result: VideoGenerateResult) => TTransformed
|
|
163
160
|
} & GenerationPersistenceOptions,
|
|
@@ -169,8 +166,8 @@ export function useGenerateVideo<TTransformed = void>(
|
|
|
169
166
|
TTransformed
|
|
170
167
|
>
|
|
171
168
|
const hookId = useId()
|
|
172
|
-
//
|
|
173
|
-
const clientIdentity = options.threadId ??
|
|
169
|
+
// The hook identity is `threadId`. `hookId` is only a React recreation key.
|
|
170
|
+
const clientIdentity = options.threadId ?? hookId
|
|
174
171
|
|
|
175
172
|
const [result, setResult] = useState<TOutput | null>(null)
|
|
176
173
|
const [jobId, setJobId] = useState<string | null>(null)
|
|
@@ -192,13 +189,11 @@ export function useGenerateVideo<TTransformed = void>(
|
|
|
192
189
|
// `?.()`'s implicit `undefined` doesn't widen the function
|
|
193
190
|
// return type (which `exactOptionalPropertyTypes` rejects
|
|
194
191
|
// against the strict-optional target).
|
|
195
|
-
|
|
196
|
-
|
|
192
|
+
const baseOptions: Omit<
|
|
193
|
+
VideoGenerationClientOptions<TOutput>,
|
|
194
|
+
'persistence' | 'threadId'
|
|
195
|
+
> = {
|
|
197
196
|
body: opts.body,
|
|
198
|
-
...(opts.threadId !== undefined
|
|
199
|
-
? { threadId: opts.threadId }
|
|
200
|
-
: { id: opts.id ?? hookId }),
|
|
201
|
-
...(opts.persistence !== undefined && { persistence: opts.persistence }),
|
|
202
197
|
...(opts.hydrateGeneration !== undefined && {
|
|
203
198
|
hydrateGeneration: opts.hydrateGeneration,
|
|
204
199
|
}),
|
|
@@ -255,9 +250,20 @@ export function useGenerateVideo<TTransformed = void>(
|
|
|
255
250
|
},
|
|
256
251
|
}
|
|
257
252
|
|
|
253
|
+
const persistenceProps =
|
|
254
|
+
typeof opts.threadId === 'string' && opts.persistence
|
|
255
|
+
? {
|
|
256
|
+
persistence: opts.persistence,
|
|
257
|
+
threadId: opts.threadId,
|
|
258
|
+
}
|
|
259
|
+
: {
|
|
260
|
+
...(opts.threadId !== undefined && { threadId: opts.threadId }),
|
|
261
|
+
}
|
|
262
|
+
|
|
258
263
|
if (opts.connection) {
|
|
259
264
|
return new VideoGenerationClient<TOutput>({
|
|
260
265
|
...baseOptions,
|
|
266
|
+
...persistenceProps,
|
|
261
267
|
connection: opts.connection,
|
|
262
268
|
})
|
|
263
269
|
}
|
|
@@ -265,6 +271,7 @@ export function useGenerateVideo<TTransformed = void>(
|
|
|
265
271
|
if (opts.fetcher) {
|
|
266
272
|
return new VideoGenerationClient<TOutput>({
|
|
267
273
|
...baseOptions,
|
|
274
|
+
...persistenceProps,
|
|
268
275
|
fetcher: opts.fetcher,
|
|
269
276
|
})
|
|
270
277
|
}
|