@tanstack/ai-solid 0.16.3 → 0.18.1
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 +2 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/types.d.ts +9 -9
- package/dist/use-chat.d.ts +2 -2
- package/dist/use-chat.js +7 -4
- package/dist/use-chat.js.map +1 -1
- package/dist/use-generate-audio.d.ts +1 -5
- package/dist/use-generate-audio.js.map +1 -1
- package/dist/use-generate-image.d.ts +1 -5
- package/dist/use-generate-image.js.map +1 -1
- package/dist/use-generate-speech.d.ts +1 -5
- package/dist/use-generate-speech.js.map +1 -1
- package/dist/use-generate-video.d.ts +3 -7
- package/dist/use-generate-video.js +5 -4
- package/dist/use-generate-video.js.map +1 -1
- package/dist/use-generation.d.ts +3 -7
- package/dist/use-generation.js +7 -4
- package/dist/use-generation.js.map +1 -1
- package/dist/use-summarize.d.ts +1 -5
- package/dist/use-summarize.js.map +1 -1
- package/dist/use-transcription.d.ts +1 -5
- package/dist/use-transcription.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +2 -0
- package/src/types.ts +21 -11
- package/src/use-chat.ts +36 -19
- package/src/use-generate-audio.ts +1 -5
- package/src/use-generate-image.ts +1 -5
- package/src/use-generate-speech.ts +1 -5
- package/src/use-generate-video.ts +13 -17
- package/src/use-generation.ts +21 -17
- package/src/use-summarize.ts +1 -5
- package/src/use-transcription.ts +1 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-generation.js","names":["clientOptions: GenerationClientOptions<TInput, TResult, TOutput>"],"sources":["../src/use-generation.ts"],"sourcesContent":["import { GenerationClient } from '@tanstack/ai-client'\nimport { createGenerationDevtoolsBridge } from '@tanstack/ai-client/devtools'\nimport {\n createEffect,\n createSignal,\n createUniqueId,\n onCleanup,\n onMount,\n untrack,\n} from 'solid-js'\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'\nimport type { Accessor } from 'solid-js'\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 transformed output type (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 (possibly transformed from the raw result)\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: Accessor<TOutput | null>\n /** Whether a generation is currently in progress */\n isLoading: Accessor<boolean>\n /** Current error, if any */\n error: Accessor<Error | undefined>\n /** Current state of the generation client */\n status: Accessor<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: Accessor<string | null>\n}\n\n/**\n * Generic Solid 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 * @template TOutput - The transformed output type (defaults to TResult)\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 = createUniqueId()\n\n const [result, setResult] = createSignal<TOutput | null>(null)\n const [isLoading, setIsLoading] = createSignal(false)\n const [error, setError] = createSignal<Error | undefined>(undefined)\n const [status, setStatus] = createSignal<GenerationClientState>('idle')\n const [runId, setRunId] = createSignal<string | null>(null)\n let disposed = false\n\n // Built once. `untrack` keeps the option reads below from subscribing\n // construction to `options.persistence` / `options.devtools` /\n // `options.body`: a re-run would build a second client\n // and orphan the first (only the live one is disposed on cleanup). Later\n // `options.body` changes are pushed through `updateOptions` instead.\n const client = untrack((): GenerationClient<TInput, TResult, TOutput> => {\n // Conditional spread on `body`: `GenerationClientOptions.body` is a\n // strict optional (`body?: Record<string, any>`) and EOPT forbids\n // assigning the source `T | undefined` directly.\n const clientOptions: GenerationClientOptions<TInput, TResult, TOutput> = {\n body: options.body,\n // Identity: pass `threadId` alone when set (never also pass deprecated `id`).\n ...(options.threadId !== undefined\n ? { threadId: options.threadId }\n : { id: options.id ?? hookId }),\n ...(options.persistence !== undefined && {\n persistence: options.persistence,\n }),\n ...(options.hydrateGeneration !== undefined && {\n hydrateGeneration: options.hydrateGeneration,\n }),\n ...(options.joinRun !== undefined && { joinRun: options.joinRun }),\n ...(options.reconstructResult\n ? { reconstructResult: options.reconstructResult }\n : {}),\n devtoolsBridgeFactory: createGenerationDevtoolsBridge,\n devtools: {\n ...options.devtools,\n framework: 'solid',\n hookName: 'useGeneration',\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) => options.onResult?.(r)) as (\n result: TResult,\n ) => TOutput | null | void,\n onError: (e: Error) => {\n if (!disposed) options.onError?.(e)\n },\n onProgress: (p: number, m?: string) => {\n if (!disposed) options.onProgress?.(p, m)\n },\n onChunk: (c: StreamChunk) => {\n if (!disposed) options.onChunk?.(c)\n },\n onResultChange: (r) => {\n if (!disposed) setResult(() => r)\n },\n onLoadingChange: (l) => {\n if (!disposed) setIsLoading(l)\n },\n onErrorChange: (e) => {\n if (!disposed) setError(e)\n },\n onStatusChange: (s) => {\n if (!disposed) setStatus(s)\n },\n onResumeStateChange: (rs) => {\n if (!disposed) setRunId(rs?.runId ?? null)\n },\n }\n\n if (options.connection) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n connection: options.connection,\n })\n }\n\n if (options.fetcher) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n fetcher: options.fetcher,\n })\n }\n\n throw new Error(\n 'useGeneration requires either a connection or fetcher option',\n )\n })\n\n // Sync body changes without recreating client\n createEffect(() => {\n const currentBody = options.body\n client.updateOptions({\n ...(currentBody !== undefined && { body: currentBody }),\n })\n })\n\n // Mount devtools only. Generation runs are never auto-started on mount — a\n // persisted snapshot is hydrated for display, never replayed.\n onMount(() => {\n client.mountDevtools()\n })\n\n // Cleanup on unmount: stop any in-flight requests and unregister devtools\n onCleanup(() => {\n disposed = true\n client.dispose()\n })\n\n const generate = async (input: TInput) => {\n await client.generate(input)\n }\n\n const stop = () => {\n client.stop()\n }\n\n const reset = () => {\n client.reset()\n }\n\n return {\n generate,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n runId,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAmKA,SAAgB,cAKd,SASA;CAEA,MAAM,SAAS,gBAAgB;CAE/B,MAAM,CAAC,QAAQ,aAAa,aAA6B,KAAK;CAC9D,MAAM,CAAC,WAAW,gBAAgB,aAAa,MAAM;CACrD,MAAM,CAAC,OAAO,YAAY,aAAgC,OAAU;CACpE,MAAM,CAAC,QAAQ,aAAa,aAAoC,OAAO;CACvE,MAAM,CAAC,OAAO,YAAY,aAA4B,KAAK;CAC3D,IAAI,WAAW;CAOf,MAAM,SAAS,cAA0D;EAIvE,MAAMA,gBAAmE;GACvE,MAAM,QAAQ;GAEd,GAAI,QAAQ,aAAa,SACrB,EAAE,UAAU,QAAQ,UAAU,GAC9B,EAAE,IAAI,QAAQ,MAAM,QAAQ;GAChC,GAAI,QAAQ,gBAAgB,UAAa,EACvC,aAAa,QAAQ,aACtB;GACD,GAAI,QAAQ,sBAAsB,UAAa,EAC7C,mBAAmB,QAAQ,mBAC5B;GACD,GAAI,QAAQ,YAAY,UAAa,EAAE,SAAS,QAAQ,SAAS;GACjE,GAAI,QAAQ,oBACR,EAAE,mBAAmB,QAAQ,mBAAmB,GAChD,EAAE;GACN,uBAAuB;GACvB,UAAU;IACR,GAAG,QAAQ;IACX,WAAW;IACX,UAAU;IACX;GAID,YAAY,MAAe,QAAQ,WAAW,EAAE;GAGhD,UAAU,MAAa;AACrB,QAAI,CAAC,SAAU,SAAQ,UAAU,EAAE;;GAErC,aAAa,GAAW,MAAe;AACrC,QAAI,CAAC,SAAU,SAAQ,aAAa,GAAG,EAAE;;GAE3C,UAAU,MAAmB;AAC3B,QAAI,CAAC,SAAU,SAAQ,UAAU,EAAE;;GAErC,iBAAiB,MAAM;AACrB,QAAI,CAAC,SAAU,iBAAgB,EAAE;;GAEnC,kBAAkB,MAAM;AACtB,QAAI,CAAC,SAAU,cAAa,EAAE;;GAEhC,gBAAgB,MAAM;AACpB,QAAI,CAAC,SAAU,UAAS,EAAE;;GAE5B,iBAAiB,MAAM;AACrB,QAAI,CAAC,SAAU,WAAU,EAAE;;GAE7B,sBAAsB,OAAO;AAC3B,QAAI,CAAC,SAAU,UAAS,IAAI,SAAS,KAAK;;GAE7C;AAED,MAAI,QAAQ,WACV,QAAO,IAAI,iBAA2C;GACpD,GAAG;GACH,YAAY,QAAQ;GACrB,CAAC;AAGJ,MAAI,QAAQ,QACV,QAAO,IAAI,iBAA2C;GACpD,GAAG;GACH,SAAS,QAAQ;GAClB,CAAC;AAGJ,QAAM,IAAI,MACR,+DACD;GACD;AAGF,oBAAmB;EACjB,MAAM,cAAc,QAAQ;AAC5B,SAAO,cAAc,EACnB,GAAI,gBAAgB,UAAa,EAAE,MAAM,aAAa,EACvD,CAAC;GACF;AAIF,eAAc;AACZ,SAAO,eAAe;GACtB;AAGF,iBAAgB;AACd,aAAW;AACX,SAAO,SAAS;GAChB;CAEF,MAAM,WAAW,OAAO,UAAkB;AACxC,QAAM,OAAO,SAAS,MAAM;;CAG9B,MAAM,aAAa;AACjB,SAAO,MAAM;;CAGf,MAAM,cAAc;AAClB,SAAO,OAAO;;AAGhB,QAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD"}
|
|
1
|
+
{"version":3,"file":"use-generation.js","names":["clientOptions: Omit<\n GenerationClientOptions<TInput, TResult, TOutput>,\n 'persistence' | 'threadId'\n >"],"sources":["../src/use-generation.ts"],"sourcesContent":["import { GenerationClient } from '@tanstack/ai-client'\nimport { createGenerationDevtoolsBridge } from '@tanstack/ai-client/devtools'\nimport {\n createEffect,\n createSignal,\n onCleanup,\n onMount,\n untrack,\n} from 'solid-js'\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'\nimport type { Accessor } from 'solid-js'\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 transformed output type (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 (possibly transformed from the raw result)\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: Accessor<TOutput | null>\n /** Whether a generation is currently in progress */\n isLoading: Accessor<boolean>\n /** Current error, if any */\n error: Accessor<Error | undefined>\n /** Current state of the generation client */\n status: Accessor<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: Accessor<string | null>\n}\n\n/**\n * Generic Solid 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 * @template TOutput - The transformed output type (defaults to TResult)\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\n const [result, setResult] = createSignal<TOutput | null>(null)\n const [isLoading, setIsLoading] = createSignal(false)\n const [error, setError] = createSignal<Error | undefined>(undefined)\n const [status, setStatus] = createSignal<GenerationClientState>('idle')\n const [runId, setRunId] = createSignal<string | null>(null)\n let disposed = false\n\n // Built once. `untrack` keeps the option reads below from subscribing\n // construction to `options.persistence` / `options.devtools` /\n // `options.body`: a re-run would build a second client\n // and orphan the first (only the live one is disposed on cleanup). Later\n // `options.body` changes are pushed through `updateOptions` instead.\n const client = untrack((): GenerationClient<TInput, TResult, TOutput> => {\n // Conditional spread on `body`: `GenerationClientOptions.body` is a\n // strict optional (`body?: Record<string, any>`) and EOPT forbids\n // assigning the source `T | undefined` directly.\n const clientOptions: Omit<\n GenerationClientOptions<TInput, TResult, TOutput>,\n 'persistence' | 'threadId'\n > = {\n body: options.body,\n ...(options.hydrateGeneration !== undefined && {\n hydrateGeneration: options.hydrateGeneration,\n }),\n ...(options.joinRun !== undefined && { joinRun: options.joinRun }),\n ...(options.reconstructResult\n ? { reconstructResult: options.reconstructResult }\n : {}),\n devtoolsBridgeFactory: createGenerationDevtoolsBridge,\n devtools: {\n ...options.devtools,\n framework: 'solid',\n hookName: 'useGeneration',\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) => options.onResult?.(r)) as (\n result: TResult,\n ) => TOutput | null | void,\n onError: (e: Error) => {\n if (!disposed) options.onError?.(e)\n },\n onProgress: (p: number, m?: string) => {\n if (!disposed) options.onProgress?.(p, m)\n },\n onChunk: (c: StreamChunk) => {\n if (!disposed) options.onChunk?.(c)\n },\n onResultChange: (r) => {\n if (!disposed) setResult(() => r)\n },\n onLoadingChange: (l) => {\n if (!disposed) setIsLoading(l)\n },\n onErrorChange: (e) => {\n if (!disposed) setError(e)\n },\n onStatusChange: (s) => {\n if (!disposed) setStatus(s)\n },\n onResumeStateChange: (rs) => {\n if (!disposed) setRunId(rs?.runId ?? null)\n },\n }\n\n const persistenceProps =\n typeof options.threadId === 'string' && options.persistence\n ? {\n persistence: options.persistence,\n threadId: options.threadId,\n }\n : {\n ...(options.threadId !== undefined && {\n threadId: options.threadId,\n }),\n }\n\n if (options.connection) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n ...persistenceProps,\n connection: options.connection,\n })\n }\n\n if (options.fetcher) {\n return new GenerationClient<TInput, TResult, TOutput>({\n ...clientOptions,\n ...persistenceProps,\n fetcher: options.fetcher,\n })\n }\n\n throw new Error(\n 'useGeneration requires either a connection or fetcher option',\n )\n })\n\n // Sync body changes without recreating client\n createEffect(() => {\n const currentBody = options.body\n client.updateOptions({\n ...(currentBody !== undefined && { body: currentBody }),\n })\n })\n\n // Mount devtools only. Generation runs are never auto-started on mount — a\n // persisted snapshot is hydrated for display, never replayed.\n onMount(() => {\n client.mountDevtools()\n })\n\n // Cleanup on unmount: stop any in-flight requests and unregister devtools\n onCleanup(() => {\n disposed = true\n client.dispose()\n })\n\n const generate = async (input: TInput) => {\n await client.generate(input)\n }\n\n const stop = () => {\n client.stop()\n }\n\n const reset = () => {\n client.reset()\n }\n\n return {\n generate,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n runId,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA8JA,SAAgB,cAKd,SASA;CAGA,MAAM,CAAC,QAAQ,aAAa,aAA6B,KAAK;CAC9D,MAAM,CAAC,WAAW,gBAAgB,aAAa,MAAM;CACrD,MAAM,CAAC,OAAO,YAAY,aAAgC,OAAU;CACpE,MAAM,CAAC,QAAQ,aAAa,aAAoC,OAAO;CACvE,MAAM,CAAC,OAAO,YAAY,aAA4B,KAAK;CAC3D,IAAI,WAAW;CAOf,MAAM,SAAS,cAA0D;EAIvE,MAAMA,gBAGF;GACF,MAAM,QAAQ;GACd,GAAI,QAAQ,sBAAsB,UAAa,EAC7C,mBAAmB,QAAQ,mBAC5B;GACD,GAAI,QAAQ,YAAY,UAAa,EAAE,SAAS,QAAQ,SAAS;GACjE,GAAI,QAAQ,oBACR,EAAE,mBAAmB,QAAQ,mBAAmB,GAChD,EAAE;GACN,uBAAuB;GACvB,UAAU;IACR,GAAG,QAAQ;IACX,WAAW;IACX,UAAU;IACX;GAID,YAAY,MAAe,QAAQ,WAAW,EAAE;GAGhD,UAAU,MAAa;AACrB,QAAI,CAAC,SAAU,SAAQ,UAAU,EAAE;;GAErC,aAAa,GAAW,MAAe;AACrC,QAAI,CAAC,SAAU,SAAQ,aAAa,GAAG,EAAE;;GAE3C,UAAU,MAAmB;AAC3B,QAAI,CAAC,SAAU,SAAQ,UAAU,EAAE;;GAErC,iBAAiB,MAAM;AACrB,QAAI,CAAC,SAAU,iBAAgB,EAAE;;GAEnC,kBAAkB,MAAM;AACtB,QAAI,CAAC,SAAU,cAAa,EAAE;;GAEhC,gBAAgB,MAAM;AACpB,QAAI,CAAC,SAAU,UAAS,EAAE;;GAE5B,iBAAiB,MAAM;AACrB,QAAI,CAAC,SAAU,WAAU,EAAE;;GAE7B,sBAAsB,OAAO;AAC3B,QAAI,CAAC,SAAU,UAAS,IAAI,SAAS,KAAK;;GAE7C;EAED,MAAM,mBACJ,OAAO,QAAQ,aAAa,YAAY,QAAQ,cAC5C;GACE,aAAa,QAAQ;GACrB,UAAU,QAAQ;GACnB,GACD,EACE,GAAI,QAAQ,aAAa,UAAa,EACpC,UAAU,QAAQ,UACnB,EACF;AAEP,MAAI,QAAQ,WACV,QAAO,IAAI,iBAA2C;GACpD,GAAG;GACH,GAAG;GACH,YAAY,QAAQ;GACrB,CAAC;AAGJ,MAAI,QAAQ,QACV,QAAO,IAAI,iBAA2C;GACpD,GAAG;GACH,GAAG;GACH,SAAS,QAAQ;GAClB,CAAC;AAGJ,QAAM,IAAI,MACR,+DACD;GACD;AAGF,oBAAmB;EACjB,MAAM,cAAc,QAAQ;AAC5B,SAAO,cAAc,EACnB,GAAI,gBAAgB,UAAa,EAAE,MAAM,aAAa,EACvD,CAAC;GACF;AAIF,eAAc;AACZ,SAAO,eAAe;GACtB;AAGF,iBAAgB;AACd,aAAW;AACX,SAAO,SAAS;GAChB;CAEF,MAAM,WAAW,OAAO,UAAkB;AACxC,QAAM,OAAO,SAAS,MAAM;;CAG9B,MAAM,aAAa;AACjB,SAAO,MAAM;;CAGf,MAAM,cAAc;AAClB,SAAO,OAAO;;AAGhB,QAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD"}
|
package/dist/use-summarize.d.ts
CHANGED
|
@@ -15,10 +15,6 @@ interface UseSummarizeOptions<TOutput = SummarizationResult> extends Pick<UseGen
|
|
|
15
15
|
connection?: ConnectConnectionAdapter;
|
|
16
16
|
/** Direct async function for summarization */
|
|
17
17
|
fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>;
|
|
18
|
-
/**
|
|
19
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
20
|
-
*/
|
|
21
|
-
id?: string;
|
|
22
18
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
23
19
|
body?: Record<string, any>;
|
|
24
20
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -84,7 +80,7 @@ interface UseSummarizeReturn<TOutput = SummarizationResult> extends Omit<UseGene
|
|
|
84
80
|
* }
|
|
85
81
|
* ```
|
|
86
82
|
*/
|
|
87
|
-
declare function useSummarize<TTransformed = void>(options: Omit<UseSummarizeOptions, 'onResult' | 'persistence' | 'threadId'
|
|
83
|
+
declare function useSummarize<TTransformed = void>(options: Omit<UseSummarizeOptions, 'onResult' | 'persistence' | 'threadId'> & {
|
|
88
84
|
onResult?: (result: SummarizationResult) => TTransformed;
|
|
89
85
|
} & GenerationPersistenceOptions): UseSummarizeReturn<InferGenerationOutputFromReturn<SummarizationResult, TTransformed>>;
|
|
90
86
|
//#endregion
|
|
@@ -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 {\n UseGenerationOptions,\n UseGenerationReturn,\n} from './use-generation'\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'\nimport type { Accessor } from 'solid-js'\n\n/**\n * Options for the useSummarize hook.\n *\n * @template TOutput - The transformed output type (defaults to SummarizationResult)\n */\nexport interface UseSummarizeOptions<\n TOutput = SummarizationResult,\n> extends Pick<\n UseGenerationOptions<SummarizeGenerateInput, SummarizationResult, TOutput>,\n 'persistence' | 'threadId' | 'hydrateGeneration' | 'joinRun'\n> {\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 {\n UseGenerationOptions,\n UseGenerationReturn,\n} from './use-generation'\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'\nimport type { Accessor } from 'solid-js'\n\n/**\n * Options for the useSummarize hook.\n *\n * @template TOutput - The transformed output type (defaults to SummarizationResult)\n */\nexport interface UseSummarizeOptions<\n TOutput = SummarizationResult,\n> extends Pick<\n UseGenerationOptions<SummarizeGenerateInput, SummarizationResult, TOutput>,\n 'persistence' | 'threadId' | 'hydrateGeneration' | 'joinRun'\n> {\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 * 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 transformed output type (defaults to SummarizationResult)\n */\nexport interface UseSummarizeReturn<TOutput = SummarizationResult> extends Omit<\n UseGenerationReturn<TOutput>,\n 'generate'\n> {\n /** Trigger summarization */\n generate: (input: SummarizeGenerateInput) => Promise<void>\n /** The summarization result, or null */\n result: Accessor<TOutput | null>\n /** Whether summarization is in progress */\n isLoading: Accessor<boolean>\n /** Current error, if any */\n error: Accessor<Error | undefined>\n /** Current state of the generation */\n status: Accessor<GenerationClientState>\n}\n\n/**\n * Solid hook for summarizing text using AI models.\n *\n * @example\n * ```tsx\n * import { useSummarize } from '@tanstack/ai-solid'\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: 'solid',\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuGA,SAAgB,aACd,SAQA;CACA,MAAM,WAAW;EACf,GAAG,QAAQ;EACX,WAAW;EACX,UAAU;EACV,YAAY;EACb;AAWD,QAVmB,cAIjB;EACA,GAAG;EACH;EACA,mBAAmB;EACpB,CAAC"}
|
|
@@ -15,10 +15,6 @@ interface UseTranscriptionOptions<TOutput = TranscriptionResult> extends Pick<Us
|
|
|
15
15
|
connection?: ConnectConnectionAdapter;
|
|
16
16
|
/** Direct async function for transcription */
|
|
17
17
|
fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>;
|
|
18
|
-
/**
|
|
19
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
20
|
-
*/
|
|
21
|
-
id?: string;
|
|
22
18
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
23
19
|
body?: Record<string, any>;
|
|
24
20
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -90,7 +86,7 @@ interface UseTranscriptionReturn<TOutput = TranscriptionResult> extends Omit<Use
|
|
|
90
86
|
* }
|
|
91
87
|
* ```
|
|
92
88
|
*/
|
|
93
|
-
declare function useTranscription<TTransformed = void>(options: Omit<UseTranscriptionOptions, 'onResult' | 'persistence' | 'threadId'
|
|
89
|
+
declare function useTranscription<TTransformed = void>(options: Omit<UseTranscriptionOptions, 'onResult' | 'persistence' | 'threadId'> & {
|
|
94
90
|
onResult?: (result: TranscriptionResult) => TTransformed;
|
|
95
91
|
} & GenerationPersistenceOptions): UseTranscriptionReturn<InferGenerationOutputFromReturn<TranscriptionResult, TTransformed>>;
|
|
96
92
|
//#endregion
|
|
@@ -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 {\n UseGenerationOptions,\n UseGenerationReturn,\n} from './use-generation'\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'\nimport type { Accessor } from 'solid-js'\n\n/**\n * Options for the useTranscription hook.\n *\n * @template TOutput - The transformed output type (defaults to TranscriptionResult)\n */\nexport interface UseTranscriptionOptions<\n TOutput = TranscriptionResult,\n> extends Pick<\n UseGenerationOptions<\n TranscriptionGenerateInput,\n TranscriptionResult,\n TOutput\n >,\n 'persistence' | 'threadId' | 'hydrateGeneration' | 'joinRun'\n> {\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 {\n UseGenerationOptions,\n UseGenerationReturn,\n} from './use-generation'\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'\nimport type { Accessor } from 'solid-js'\n\n/**\n * Options for the useTranscription hook.\n *\n * @template TOutput - The transformed output type (defaults to TranscriptionResult)\n */\nexport interface UseTranscriptionOptions<\n TOutput = TranscriptionResult,\n> extends Pick<\n UseGenerationOptions<\n TranscriptionGenerateInput,\n TranscriptionResult,\n TOutput\n >,\n 'persistence' | 'threadId' | 'hydrateGeneration' | 'joinRun'\n> {\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 * 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 transformed output type (defaults to TranscriptionResult)\n */\nexport interface UseTranscriptionReturn<\n TOutput = TranscriptionResult,\n> extends Omit<UseGenerationReturn<TOutput>, 'generate'> {\n /** Trigger transcription */\n generate: (input: TranscriptionGenerateInput) => Promise<void>\n /** The transcription result, or null */\n result: Accessor<TOutput | null>\n /** Whether transcription is in progress */\n isLoading: Accessor<boolean>\n /** Current error, if any */\n error: Accessor<Error | undefined>\n /** Current state of the generation */\n status: Accessor<GenerationClientState>\n}\n\n/**\n * Solid hook for transcribing audio to text using AI models.\n *\n * @example\n * ```tsx\n * import { useTranscription } from '@tanstack/ai-solid'\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: Event) => {\n * const input = e.target as HTMLInputElement\n * const file = input.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: 'solid',\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgHA,SAAgB,iBACd,SAQA;CACA,MAAM,WAAW;EACf,GAAG,QAAQ;EACX,WAAW;EACX,UAAU;EACV,YAAY;EACb;AAOD,QANmB,cAIjB;EAAE,GAAG;EAAS;EAAU,mBAAmB;EAAgC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-solid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.1",
|
|
4
4
|
"description": "Solid hooks for TanStack AI streaming chat, structured outputs, and media generation.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,11 +45,11 @@
|
|
|
45
45
|
"media-generation"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@tanstack/ai-client": "^0.
|
|
48
|
+
"@tanstack/ai-client": "^0.25.1"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"solid-js": ">=1.9.10",
|
|
52
|
-
"@tanstack/ai": "^0.
|
|
52
|
+
"@tanstack/ai": "^0.47.1"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@solidjs/testing-library": "^0.8.10",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"tsdown": "^0.17.0-beta.6",
|
|
62
62
|
"typescript": "5.9.3",
|
|
63
63
|
"vitest": "^4.1.10",
|
|
64
|
-
"@tanstack/ai": "0.
|
|
64
|
+
"@tanstack/ai": "0.47.1"
|
|
65
65
|
},
|
|
66
66
|
"scripts": {
|
|
67
67
|
"clean": "premove ./build ./dist",
|
package/src/index.ts
CHANGED
|
@@ -77,6 +77,7 @@ export {
|
|
|
77
77
|
xhrHttpStream,
|
|
78
78
|
stream,
|
|
79
79
|
rpcStream,
|
|
80
|
+
webSocket,
|
|
80
81
|
createChatClientOptions,
|
|
81
82
|
type ConnectionAdapter,
|
|
82
83
|
type ConnectConnectionAdapter,
|
|
@@ -84,6 +85,7 @@ export {
|
|
|
84
85
|
type RunAgentInputContext,
|
|
85
86
|
type FetchConnectionOptions,
|
|
86
87
|
type XhrConnectionOptions,
|
|
88
|
+
type WebSocketConnectionOptions,
|
|
87
89
|
type InferChatMessages,
|
|
88
90
|
type GenerationClientState,
|
|
89
91
|
type ImageGenerateInput,
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AnyClientTool,
|
|
3
|
+
InterruptDefinition,
|
|
3
4
|
InferSchemaType,
|
|
4
5
|
ModelMessage,
|
|
5
6
|
RunAgentResumeItem,
|
|
@@ -10,7 +11,7 @@ import type {
|
|
|
10
11
|
BoundInterrupts,
|
|
11
12
|
ChatClientOptions,
|
|
12
13
|
ChatClientState,
|
|
13
|
-
|
|
14
|
+
ResolvableChatInterrupt,
|
|
14
15
|
ChatInterruptState,
|
|
15
16
|
ChatRequestBody,
|
|
16
17
|
ChatResumeState,
|
|
@@ -79,8 +80,10 @@ export type UseChatOptions<
|
|
|
79
80
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
80
81
|
TSchema extends SchemaInput | undefined = undefined,
|
|
81
82
|
TContext = InferredClientContext<TTools>,
|
|
83
|
+
TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =
|
|
84
|
+
readonly [],
|
|
82
85
|
> = DistributedOmit<
|
|
83
|
-
ChatClientOptions<TTools, TContext>,
|
|
86
|
+
ChatClientOptions<TTools, TContext, TInterrupts>,
|
|
84
87
|
| 'onMessagesChange'
|
|
85
88
|
| 'onLoadingChange'
|
|
86
89
|
| 'onErrorChange'
|
|
@@ -93,10 +96,6 @@ export type UseChatOptions<
|
|
|
93
96
|
| 'onRunIdChange'
|
|
94
97
|
| 'context'
|
|
95
98
|
| 'devtools'
|
|
96
|
-
// `id` is not a hook option: the hook's identity is its `threadId`, which is
|
|
97
|
-
// also the persistence key. Persist across reloads by passing a stable
|
|
98
|
-
// `threadId`; there is no separate id to set.
|
|
99
|
-
| 'id'
|
|
100
99
|
> & {
|
|
101
100
|
/** Display options for TanStack AI Devtools. */
|
|
102
101
|
devtools?: AIDevtoolsDisplayOptions
|
|
@@ -115,9 +114,12 @@ export type UseChatOptions<
|
|
|
115
114
|
export type UseChatReturn<
|
|
116
115
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
117
116
|
TSchema extends SchemaInput | undefined = undefined,
|
|
117
|
+
TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =
|
|
118
|
+
readonly [],
|
|
118
119
|
> = BaseUseChatReturn<
|
|
119
120
|
TTools,
|
|
120
|
-
TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown
|
|
121
|
+
TSchema extends SchemaInput ? InferSchemaType<TSchema> : unknown,
|
|
122
|
+
TInterrupts
|
|
121
123
|
> &
|
|
122
124
|
(TSchema extends SchemaInput
|
|
123
125
|
? {
|
|
@@ -137,6 +139,8 @@ export type UseChatReturn<
|
|
|
137
139
|
interface BaseUseChatReturn<
|
|
138
140
|
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
139
141
|
TData = unknown,
|
|
142
|
+
TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =
|
|
143
|
+
readonly [],
|
|
140
144
|
> {
|
|
141
145
|
/**
|
|
142
146
|
* Current messages in the conversation. When `outputSchema` is supplied,
|
|
@@ -204,14 +208,20 @@ interface BaseUseChatReturn<
|
|
|
204
208
|
* it, correlate a log line).
|
|
205
209
|
*/
|
|
206
210
|
runId: Accessor<string | null>
|
|
207
|
-
interrupts: Accessor<BoundInterrupts<TTools>>
|
|
211
|
+
interrupts: Accessor<BoundInterrupts<TTools, TInterrupts>>
|
|
208
212
|
/** @deprecated Use `interrupts`. */
|
|
209
|
-
pendingInterrupts: Accessor<BoundInterrupts<TTools>>
|
|
210
|
-
interruptErrors: Accessor<
|
|
213
|
+
pendingInterrupts: Accessor<BoundInterrupts<TTools, TInterrupts>>
|
|
214
|
+
interruptErrors: Accessor<
|
|
215
|
+
ChatInterruptState<TTools, TInterrupts>['interruptErrors']
|
|
216
|
+
>
|
|
211
217
|
resuming: Accessor<boolean>
|
|
212
218
|
resolveInterrupts: {
|
|
213
219
|
(approved: boolean): void
|
|
214
|
-
(
|
|
220
|
+
(
|
|
221
|
+
resolver: (
|
|
222
|
+
interrupt: ResolvableChatInterrupt<TTools, TInterrupts>,
|
|
223
|
+
) => undefined,
|
|
224
|
+
): void
|
|
215
225
|
}
|
|
216
226
|
cancelInterrupts: () => void
|
|
217
227
|
retryInterrupts: () => void
|
package/src/use-chat.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { ChatClient } from '@tanstack/ai-client'
|
|
|
11
11
|
import { createChatDevtoolsBridge } from '@tanstack/ai-client/devtools'
|
|
12
12
|
import type {
|
|
13
13
|
ChatClientState,
|
|
14
|
-
|
|
14
|
+
ResolvableChatInterrupt,
|
|
15
15
|
ChatInterruptState,
|
|
16
16
|
ChatResumeState,
|
|
17
17
|
ConnectionStatus,
|
|
@@ -22,6 +22,7 @@ import type {
|
|
|
22
22
|
} from '@tanstack/ai-client'
|
|
23
23
|
import type {
|
|
24
24
|
AnyClientTool,
|
|
25
|
+
InterruptDefinition,
|
|
25
26
|
InferSchemaType,
|
|
26
27
|
ModelMessage,
|
|
27
28
|
RunAgentResumeItem,
|
|
@@ -43,17 +44,20 @@ export function useChat<
|
|
|
43
44
|
const TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
44
45
|
TSchema extends SchemaInput | undefined = undefined,
|
|
45
46
|
TContext = InferredClientContext<TTools>,
|
|
47
|
+
const TInterrupts extends ReadonlyArray<
|
|
48
|
+
InterruptDefinition<any, any, any, any>
|
|
49
|
+
> = readonly [],
|
|
46
50
|
>(
|
|
47
|
-
options: UseChatOptions<
|
|
51
|
+
options: UseChatOptions<
|
|
48
52
|
TTools,
|
|
49
53
|
TSchema,
|
|
50
|
-
TContext
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
//
|
|
55
|
-
//
|
|
56
|
-
// given
|
|
54
|
+
TContext,
|
|
55
|
+
TInterrupts
|
|
56
|
+
> = {} as UseChatOptions<TTools, TSchema, TContext, TInterrupts>,
|
|
57
|
+
): UseChatReturn<TTools, TSchema, TInterrupts> {
|
|
58
|
+
// The hook's identity is its `threadId`. Reload with the same `threadId`
|
|
59
|
+
// restores the same conversation. `hookId` is only a recreation key when no
|
|
60
|
+
// `threadId` is given. It is never sent on the wire.
|
|
57
61
|
const hookId = createUniqueId()
|
|
58
62
|
const clientId = options.threadId ?? hookId
|
|
59
63
|
|
|
@@ -70,7 +74,7 @@ export function useChat<
|
|
|
70
74
|
const [queue, setQueue] = createSignal<Array<QueuedMessage>>([])
|
|
71
75
|
const [runId, setRunId] = createSignal<string | null>(null)
|
|
72
76
|
const [interruptState, setInterruptState] = createSignal<
|
|
73
|
-
ChatInterruptState<TTools>
|
|
77
|
+
ChatInterruptState<TTools, TInterrupts>
|
|
74
78
|
>({
|
|
75
79
|
interrupts: EMPTY_INTERRUPTS,
|
|
76
80
|
pendingInterrupts: EMPTY_INTERRUPTS,
|
|
@@ -104,20 +108,26 @@ export function useChat<
|
|
|
104
108
|
const transport = options.connection
|
|
105
109
|
? { connection: options.connection }
|
|
106
110
|
: { fetcher: options.fetcher }
|
|
107
|
-
return new ChatClient<TTools, TContext>({
|
|
111
|
+
return new ChatClient<TTools, TContext, TInterrupts>({
|
|
108
112
|
devtoolsBridgeFactory: createChatDevtoolsBridge,
|
|
109
113
|
...transport,
|
|
110
114
|
...(options.initialMessages !== undefined && {
|
|
111
115
|
initialMessages: options.initialMessages,
|
|
112
116
|
}),
|
|
113
|
-
...(options.
|
|
114
|
-
|
|
115
|
-
|
|
117
|
+
...(typeof options.threadId === 'string' && options.persistence
|
|
118
|
+
? {
|
|
119
|
+
persistence: options.persistence,
|
|
120
|
+
threadId: options.threadId,
|
|
121
|
+
}
|
|
122
|
+
: {
|
|
123
|
+
...(options.threadId !== undefined && {
|
|
124
|
+
threadId: options.threadId,
|
|
125
|
+
}),
|
|
126
|
+
}),
|
|
116
127
|
...(options.initialResumeSnapshot !== undefined && {
|
|
117
128
|
initialResumeSnapshot: options.initialResumeSnapshot,
|
|
118
129
|
}),
|
|
119
130
|
body: options.body,
|
|
120
|
-
...(options.threadId !== undefined && { threadId: options.threadId }),
|
|
121
131
|
...(options.forwardedProps !== undefined && {
|
|
122
132
|
forwardedProps: options.forwardedProps,
|
|
123
133
|
}),
|
|
@@ -139,6 +149,9 @@ export function useChat<
|
|
|
139
149
|
options.onError?.(err)
|
|
140
150
|
},
|
|
141
151
|
tools: options.tools,
|
|
152
|
+
...(options.interrupts !== undefined && {
|
|
153
|
+
interrupts: options.interrupts,
|
|
154
|
+
}),
|
|
142
155
|
onCustomEvent: (eventType, data, context) =>
|
|
143
156
|
options.onCustomEvent?.(eventType, data, context),
|
|
144
157
|
...(options.streamProcessor !== undefined && {
|
|
@@ -180,9 +193,9 @@ export function useChat<
|
|
|
180
193
|
pendingInterrupts: nextPendingInterrupts,
|
|
181
194
|
}))
|
|
182
195
|
},
|
|
183
|
-
onInterruptStateChange: (nextInterruptState) => {
|
|
196
|
+
onInterruptStateChange: (nextInterruptState, context) => {
|
|
184
197
|
setInterruptState(nextInterruptState)
|
|
185
|
-
options.onInterruptStateChange?.(nextInterruptState)
|
|
198
|
+
options.onInterruptStateChange?.(nextInterruptState, context)
|
|
186
199
|
},
|
|
187
200
|
})
|
|
188
201
|
// Only recreate when clientId changes
|
|
@@ -323,7 +336,11 @@ export function useChat<
|
|
|
323
336
|
}
|
|
324
337
|
|
|
325
338
|
const resolveInterrupts = (
|
|
326
|
-
resolution:
|
|
339
|
+
resolution:
|
|
340
|
+
| boolean
|
|
341
|
+
| ((
|
|
342
|
+
interrupt: ResolvableChatInterrupt<TTools, TInterrupts>,
|
|
343
|
+
) => undefined),
|
|
327
344
|
) => {
|
|
328
345
|
if (typeof resolution === 'boolean') {
|
|
329
346
|
client().resolveInterrupts(resolution)
|
|
@@ -421,5 +438,5 @@ export function useChat<
|
|
|
421
438
|
resumeInterrupts,
|
|
422
439
|
partial,
|
|
423
440
|
final,
|
|
424
|
-
} as unknown as UseChatReturn<TTools, TSchema>
|
|
441
|
+
} as unknown as UseChatReturn<TTools, TSchema, TInterrupts>
|
|
425
442
|
}
|
|
@@ -31,10 +31,6 @@ export interface UseGenerateAudioOptions<
|
|
|
31
31
|
connection?: ConnectConnectionAdapter
|
|
32
32
|
/** Direct async function for audio generation */
|
|
33
33
|
fetcher?: GenerationFetcher<AudioGenerateInput, AudioGenerationResult>
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
36
|
-
*/
|
|
37
|
-
id?: string
|
|
38
34
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
39
35
|
body?: Record<string, any>
|
|
40
36
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -102,7 +98,7 @@ export interface UseGenerateAudioReturn<
|
|
|
102
98
|
export function useGenerateAudio<TTransformed = void>(
|
|
103
99
|
options: Omit<
|
|
104
100
|
UseGenerateAudioOptions,
|
|
105
|
-
'onResult' | 'persistence' | 'threadId'
|
|
101
|
+
'onResult' | 'persistence' | 'threadId'
|
|
106
102
|
> & {
|
|
107
103
|
onResult?: (result: AudioGenerationResult) => TTransformed
|
|
108
104
|
} & GenerationPersistenceOptions,
|
|
@@ -31,10 +31,6 @@ export interface UseGenerateImageOptions<
|
|
|
31
31
|
connection?: ConnectConnectionAdapter
|
|
32
32
|
/** Direct async function for image generation */
|
|
33
33
|
fetcher?: GenerationFetcher<ImageGenerateInput, ImageGenerationResult>
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
36
|
-
*/
|
|
37
|
-
id?: string
|
|
38
34
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
39
35
|
body?: Record<string, any>
|
|
40
36
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -110,7 +106,7 @@ export interface UseGenerateImageReturn<
|
|
|
110
106
|
export function useGenerateImage<TTransformed = void>(
|
|
111
107
|
options: Omit<
|
|
112
108
|
UseGenerateImageOptions,
|
|
113
|
-
'onResult' | 'persistence' | 'threadId'
|
|
109
|
+
'onResult' | 'persistence' | 'threadId'
|
|
114
110
|
> & {
|
|
115
111
|
onResult?: (result: ImageGenerationResult) => TTransformed
|
|
116
112
|
} & GenerationPersistenceOptions,
|
|
@@ -29,10 +29,6 @@ export interface UseGenerateSpeechOptions<TOutput = TTSResult> extends Pick<
|
|
|
29
29
|
connection?: ConnectConnectionAdapter
|
|
30
30
|
/** Direct async function for speech generation */
|
|
31
31
|
fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>
|
|
32
|
-
/**
|
|
33
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
34
|
-
*/
|
|
35
|
-
id?: string
|
|
36
32
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
37
33
|
body?: Record<string, any>
|
|
38
34
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -103,7 +99,7 @@ export interface UseGenerateSpeechReturn<TOutput = TTSResult> extends Omit<
|
|
|
103
99
|
export function useGenerateSpeech<TTransformed = void>(
|
|
104
100
|
options: Omit<
|
|
105
101
|
UseGenerateSpeechOptions,
|
|
106
|
-
'onResult' | 'persistence' | 'threadId'
|
|
102
|
+
'onResult' | 'persistence' | 'threadId'
|
|
107
103
|
> & {
|
|
108
104
|
onResult?: (result: TTSResult) => TTransformed
|
|
109
105
|
} & GenerationPersistenceOptions,
|
|
@@ -3,7 +3,6 @@ import { createVideoDevtoolsBridge } from '@tanstack/ai-client/devtools'
|
|
|
3
3
|
import {
|
|
4
4
|
createEffect,
|
|
5
5
|
createSignal,
|
|
6
|
-
createUniqueId,
|
|
7
6
|
onCleanup,
|
|
8
7
|
onMount,
|
|
9
8
|
untrack,
|
|
@@ -32,10 +31,6 @@ export interface UseGenerateVideoOptions<TOutput = VideoGenerateResult> {
|
|
|
32
31
|
connection?: ConnectConnectionAdapter
|
|
33
32
|
/** Direct async function that returns a completed video result */
|
|
34
33
|
fetcher?: GenerationFetcher<VideoGenerateInput, VideoGenerateResult>
|
|
35
|
-
/**
|
|
36
|
-
* @deprecated Prefer `threadId`. Only allowed when `threadId` is omitted (see `GenerationPersistenceOptions`).
|
|
37
|
-
*/
|
|
38
|
-
id?: string
|
|
39
34
|
/** Additional body parameters to send with connect-based adapter requests */
|
|
40
35
|
body?: Record<string, any>
|
|
41
36
|
/** Display options for TanStack AI Devtools. */
|
|
@@ -59,8 +54,8 @@ export interface UseGenerateVideoOptions<TOutput = VideoGenerateResult> {
|
|
|
59
54
|
* id on the wire, which the protocol requires.
|
|
60
55
|
*
|
|
61
56
|
* **Required whenever `persistence` is set** — an app that cannot name the
|
|
62
|
-
* scope has nothing to restore to. Optional for ephemeral generations
|
|
63
|
-
*
|
|
57
|
+
* scope has nothing to restore to. Optional for ephemeral generations. If
|
|
58
|
+
* omitted, the client mints a wire id after mount.
|
|
64
59
|
*/
|
|
65
60
|
threadId?: string
|
|
66
61
|
/**
|
|
@@ -167,7 +162,7 @@ export interface UseGenerateVideoReturn<TOutput = VideoGenerateResult> {
|
|
|
167
162
|
export function useGenerateVideo<TTransformed = void>(
|
|
168
163
|
options: Omit<
|
|
169
164
|
UseGenerateVideoOptions,
|
|
170
|
-
'onResult' | 'persistence' | 'threadId'
|
|
165
|
+
'onResult' | 'persistence' | 'threadId'
|
|
171
166
|
> & {
|
|
172
167
|
onResult?: (result: VideoGenerateResult) => TTransformed
|
|
173
168
|
} & GenerationPersistenceOptions,
|
|
@@ -178,8 +173,6 @@ export function useGenerateVideo<TTransformed = void>(
|
|
|
178
173
|
VideoGenerateResult,
|
|
179
174
|
TTransformed
|
|
180
175
|
>
|
|
181
|
-
const hookId = createUniqueId()
|
|
182
|
-
|
|
183
176
|
const [result, setResult] = createSignal<TOutput | null>(null)
|
|
184
177
|
const [jobId, setJobId] = createSignal<string | null>(null)
|
|
185
178
|
const [videoStatus, setVideoStatus] = createSignal<VideoStatusInfo | null>(
|
|
@@ -201,13 +194,16 @@ export function useGenerateVideo<TTransformed = void>(
|
|
|
201
194
|
// is a strict optional; EOPT forbids passing `T | undefined`.
|
|
202
195
|
const baseOptions = {
|
|
203
196
|
body: options.body,
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
197
|
+
...(typeof options.threadId === 'string' && options.persistence
|
|
198
|
+
? {
|
|
199
|
+
persistence: options.persistence,
|
|
200
|
+
threadId: options.threadId,
|
|
201
|
+
}
|
|
202
|
+
: {
|
|
203
|
+
...(options.threadId !== undefined && {
|
|
204
|
+
threadId: options.threadId,
|
|
205
|
+
}),
|
|
206
|
+
}),
|
|
211
207
|
...(options.hydrateGeneration !== undefined && {
|
|
212
208
|
hydrateGeneration: options.hydrateGeneration,
|
|
213
209
|
}),
|