@tanstack/ai-solid 0.13.14 → 0.14.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/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/types.d.ts +139 -0
- package/dist/use-audio-recorder.d.ts +37 -0
- package/dist/use-audio-recorder.js +39 -0
- package/dist/use-audio-recorder.js.map +1 -0
- package/dist/use-chat.d.ts +9 -0
- package/dist/use-chat.js +162 -0
- package/dist/use-chat.js.map +1 -0
- package/dist/use-generate-audio.d.ts +88 -0
- package/dist/use-generate-audio.js +52 -0
- package/dist/use-generate-audio.js.map +1 -0
- package/dist/use-generate-image.d.ts +96 -0
- package/dist/use-generate-image.js +60 -0
- package/dist/use-generate-image.js.map +1 -0
- package/dist/use-generate-speech.d.ts +90 -0
- package/dist/use-generate-speech.js +54 -0
- package/dist/use-generate-speech.js.map +1 -0
- package/dist/use-generate-video.d.ts +103 -0
- package/dist/use-generate-video.js +114 -0
- package/dist/use-generate-video.js.map +1 -0
- package/dist/use-generation.d.ts +88 -0
- package/dist/use-generation.js +94 -0
- package/dist/use-generation.js.map +1 -0
- package/dist/use-summarize.d.ts +93 -0
- package/dist/use-summarize.js +57 -0
- package/dist/use-summarize.js.map +1 -0
- package/dist/use-transcription.d.ts +99 -0
- package/dist/use-transcription.js +63 -0
- package/dist/use-transcription.js.map +1 -0
- package/package.json +4 -4
- package/src/index.ts +6 -0
- package/src/use-audio-recorder.ts +87 -0
- package/src/use-generate-audio.ts +5 -8
- package/src/use-generate-image.ts +5 -8
- package/src/use-generate-speech.ts +7 -7
- package/src/use-generate-video.ts +17 -9
- package/src/use-generation.ts +17 -6
- package/src/use-summarize.ts +7 -8
- package/src/use-transcription.ts +9 -11
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { createEffect, createMemo, createSignal, createUniqueId, onCleanup, onMount } from "solid-js";
|
|
2
|
+
import { GenerationClient } from "@tanstack/ai-client";
|
|
3
|
+
import { createGenerationDevtoolsBridge } from "@tanstack/ai-client/devtools";
|
|
4
|
+
|
|
5
|
+
//#region src/use-generation.ts
|
|
6
|
+
/**
|
|
7
|
+
* Generic Solid hook for one-shot generation tasks.
|
|
8
|
+
*
|
|
9
|
+
* This is the base hook used by `useGenerateImage`, `useGenerateSpeech`,
|
|
10
|
+
* `useTranscription`, and `useSummarize`. You can also use it directly
|
|
11
|
+
* for custom generation types.
|
|
12
|
+
*
|
|
13
|
+
* @template TInput - The input type for the generation request
|
|
14
|
+
* @template TResult - The result type returned by the generation
|
|
15
|
+
* @template TOutput - The transformed output type (defaults to TResult)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* const { generate, result, isLoading } = useGeneration<MyInput, MyResult>({
|
|
20
|
+
* connection: fetchServerSentEvents('/api/generate/custom'),
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* await generate({ prompt: 'Hello' })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function useGeneration(options) {
|
|
27
|
+
const hookId = createUniqueId();
|
|
28
|
+
const clientId = options.id || hookId;
|
|
29
|
+
const [result, setResult] = createSignal(null);
|
|
30
|
+
const [isLoading, setIsLoading] = createSignal(false);
|
|
31
|
+
const [error, setError] = createSignal(void 0);
|
|
32
|
+
const [status, setStatus] = createSignal("idle");
|
|
33
|
+
const client = createMemo(() => {
|
|
34
|
+
const clientOptions = {
|
|
35
|
+
id: clientId,
|
|
36
|
+
body: options.body,
|
|
37
|
+
devtoolsBridgeFactory: createGenerationDevtoolsBridge,
|
|
38
|
+
devtools: {
|
|
39
|
+
...options.devtools,
|
|
40
|
+
framework: "solid",
|
|
41
|
+
hookName: "useGeneration"
|
|
42
|
+
},
|
|
43
|
+
onResult: ((r) => options.onResult?.(r)),
|
|
44
|
+
onError: (e) => options.onError?.(e),
|
|
45
|
+
onProgress: (p, m) => options.onProgress?.(p, m),
|
|
46
|
+
onChunk: (c) => options.onChunk?.(c),
|
|
47
|
+
onResultChange: setResult,
|
|
48
|
+
onLoadingChange: setIsLoading,
|
|
49
|
+
onErrorChange: setError,
|
|
50
|
+
onStatusChange: setStatus
|
|
51
|
+
};
|
|
52
|
+
if (options.connection) return new GenerationClient({
|
|
53
|
+
...clientOptions,
|
|
54
|
+
connection: options.connection
|
|
55
|
+
});
|
|
56
|
+
if (options.fetcher) return new GenerationClient({
|
|
57
|
+
...clientOptions,
|
|
58
|
+
fetcher: options.fetcher
|
|
59
|
+
});
|
|
60
|
+
throw new Error("useGeneration requires either a connection or fetcher option");
|
|
61
|
+
}, [clientId]);
|
|
62
|
+
createEffect(() => {
|
|
63
|
+
const currentBody = options.body;
|
|
64
|
+
client().updateOptions({ ...currentBody !== void 0 && { body: currentBody } });
|
|
65
|
+
});
|
|
66
|
+
onMount(() => {
|
|
67
|
+
client().mountDevtools();
|
|
68
|
+
});
|
|
69
|
+
onCleanup(() => {
|
|
70
|
+
client().dispose();
|
|
71
|
+
});
|
|
72
|
+
const generate = async (input) => {
|
|
73
|
+
await client().generate(input);
|
|
74
|
+
};
|
|
75
|
+
const stop = () => {
|
|
76
|
+
client().stop();
|
|
77
|
+
};
|
|
78
|
+
const reset = () => {
|
|
79
|
+
client().reset();
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
generate,
|
|
83
|
+
result,
|
|
84
|
+
isLoading,
|
|
85
|
+
error,
|
|
86
|
+
status,
|
|
87
|
+
stop,
|
|
88
|
+
reset
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
export { useGeneration };
|
|
94
|
+
//# sourceMappingURL=use-generation.js.map
|
|
@@ -0,0 +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 createMemo,\n createSignal,\n createUniqueId,\n onCleanup,\n onMount,\n} from 'solid-js'\nimport type { StreamChunk } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientOptions,\n GenerationClientState,\n GenerationFetcher,\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 /** Unique identifier for this generation instance */\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 * 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\n/**\n * Return type for the useGeneration hook.\n *\n * @template TOutput - The output type (possibly transformed from the raw result)\n */\nexport interface UseGenerationReturn<TOutput> {\n /** Trigger a generation request */\n generate: (input: Record<string, any>) => 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\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<UseGenerationOptions<TInput, TResult>, 'onResult'> & {\n onResult?: (result: TResult) => TTransformed\n },\n): UseGenerationReturn<InferGenerationOutputFromReturn<TResult, TTransformed>> {\n type TOutput = InferGenerationOutputFromReturn<TResult, TTransformed>\n const hookId = createUniqueId()\n const clientId = options.id || hookId\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\n const client = createMemo(() => {\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 id: clientId,\n body: options.body,\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) => options.onError?.(e),\n onProgress: (p: number, m?: string) => options.onProgress?.(p, m),\n onChunk: (c: StreamChunk) => options.onChunk?.(c),\n onResultChange: setResult,\n onLoadingChange: setIsLoading,\n onErrorChange: setError,\n onStatusChange: setStatus,\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 }, [clientId])\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 onMount(() => {\n client().mountDevtools()\n })\n\n // Cleanup on unmount: stop any in-flight requests and unregister devtools\n onCleanup(() => {\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: generate as (input: Record<string, any>) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAyGA,SAAgB,cAKd,SAG6E;CAE7E,MAAM,SAAS,gBAAgB;CAC/B,MAAM,WAAW,QAAQ,MAAM;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;CAEvE,MAAM,SAAS,iBAAiB;EAI9B,MAAMA,gBAAmE;GACvE,IAAI;GACJ,MAAM,QAAQ;GACd,uBAAuB;GACvB,UAAU;IACR,GAAG,QAAQ;IACX,WAAW;IACX,UAAU;IACX;GAID,YAAY,MAAe,QAAQ,WAAW,EAAE;GAGhD,UAAU,MAAa,QAAQ,UAAU,EAAE;GAC3C,aAAa,GAAW,MAAe,QAAQ,aAAa,GAAG,EAAE;GACjE,UAAU,MAAmB,QAAQ,UAAU,EAAE;GACjD,gBAAgB;GAChB,iBAAiB;GACjB,eAAe;GACf,gBAAgB;GACjB;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;IACA,CAAC,SAAS,CAAC;AAGd,oBAAmB;EACjB,MAAM,cAAc,QAAQ;AAC5B,UAAQ,CAAC,cAAc,EACrB,GAAI,gBAAgB,UAAa,EAAE,MAAM,aAAa,EACvD,CAAC;GACF;AAEF,eAAc;AACZ,UAAQ,CAAC,eAAe;GACxB;AAGF,iBAAgB;AACd,UAAQ,CAAC,SAAS;GAClB;CAEF,MAAM,WAAW,OAAO,UAAkB;AACxC,QAAM,QAAQ,CAAC,SAAS,MAAM;;CAGhC,MAAM,aAAa;AACjB,UAAQ,CAAC,MAAM;;CAGjB,MAAM,cAAc;AAClB,UAAQ,CAAC,OAAO;;AAGlB,QAAO;EACK;EACV;EACA;EACA;EACA;EACA;EACA;EACD"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Accessor } from "solid-js";
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn, SummarizeGenerateInput } from "@tanstack/ai-client";
|
|
3
|
+
import { StreamChunk, SummarizationResult } from "@tanstack/ai";
|
|
4
|
+
|
|
5
|
+
//#region src/use-summarize.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for the useSummarize hook.
|
|
9
|
+
*
|
|
10
|
+
* @template TOutput - The transformed output type (defaults to SummarizationResult)
|
|
11
|
+
*/
|
|
12
|
+
interface UseSummarizeOptions<TOutput = SummarizationResult> {
|
|
13
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
14
|
+
connection?: ConnectConnectionAdapter;
|
|
15
|
+
/** Direct async function for summarization */
|
|
16
|
+
fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>;
|
|
17
|
+
/** Unique identifier for this generation instance */
|
|
18
|
+
id?: string;
|
|
19
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
20
|
+
body?: Record<string, any>;
|
|
21
|
+
/** Display options for TanStack AI Devtools. */
|
|
22
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
23
|
+
/**
|
|
24
|
+
* Callback when summarization is complete. Can optionally return a transformed value.
|
|
25
|
+
*
|
|
26
|
+
* - Return a non-null value to transform and store it as the result
|
|
27
|
+
* - Return `null` to keep the previous result unchanged
|
|
28
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
29
|
+
*/
|
|
30
|
+
onResult?: (result: SummarizationResult) => TOutput | null | void;
|
|
31
|
+
/** Callback when an error occurs */
|
|
32
|
+
onError?: (error: Error) => void;
|
|
33
|
+
/** Callback when progress is reported (0-100) */
|
|
34
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
35
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
36
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Return type for the useSummarize hook.
|
|
40
|
+
*
|
|
41
|
+
* @template TOutput - The transformed output type (defaults to SummarizationResult)
|
|
42
|
+
*/
|
|
43
|
+
interface UseSummarizeReturn<TOutput = SummarizationResult> {
|
|
44
|
+
/** Trigger summarization */
|
|
45
|
+
generate: (input: SummarizeGenerateInput) => Promise<void>;
|
|
46
|
+
/** The summarization result, or null */
|
|
47
|
+
result: Accessor<TOutput | null>;
|
|
48
|
+
/** Whether summarization is in progress */
|
|
49
|
+
isLoading: Accessor<boolean>;
|
|
50
|
+
/** Current error, if any */
|
|
51
|
+
error: Accessor<Error | undefined>;
|
|
52
|
+
/** Current state of the generation */
|
|
53
|
+
status: Accessor<GenerationClientState>;
|
|
54
|
+
/** Abort the current summarization */
|
|
55
|
+
stop: () => void;
|
|
56
|
+
/** Clear result, error, and return to idle */
|
|
57
|
+
reset: () => void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Solid hook for summarizing text using AI models.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* import { useSummarize } from '@tanstack/ai-solid'
|
|
65
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
66
|
+
*
|
|
67
|
+
* function Summarizer() {
|
|
68
|
+
* const { generate, result, isLoading } = useSummarize({
|
|
69
|
+
* connection: fetchServerSentEvents('/api/summarize'),
|
|
70
|
+
* })
|
|
71
|
+
*
|
|
72
|
+
* return (
|
|
73
|
+
* <div>
|
|
74
|
+
* <button onClick={() => generate({
|
|
75
|
+
* text: 'Long article text...',
|
|
76
|
+
* style: 'bullet-points',
|
|
77
|
+
* maxLength: 200,
|
|
78
|
+
* })}>
|
|
79
|
+
* Summarize
|
|
80
|
+
* </button>
|
|
81
|
+
* {isLoading() && <p>Summarizing...</p>}
|
|
82
|
+
* {result() && <p>{result()!.summary}</p>}
|
|
83
|
+
* </div>
|
|
84
|
+
* )
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
declare function useSummarize<TTransformed = void>(options: Omit<UseSummarizeOptions, 'onResult'> & {
|
|
89
|
+
onResult?: (result: SummarizationResult) => TTransformed;
|
|
90
|
+
}): UseSummarizeReturn<InferGenerationOutputFromReturn<SummarizationResult, TTransformed>>;
|
|
91
|
+
//#endregion
|
|
92
|
+
export { UseSummarizeOptions, UseSummarizeReturn, useSummarize };
|
|
93
|
+
//# sourceMappingURL=use-summarize.d.ts.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
|
|
3
|
+
//#region src/use-summarize.ts
|
|
4
|
+
/**
|
|
5
|
+
* Solid hook for summarizing text using AI models.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { useSummarize } from '@tanstack/ai-solid'
|
|
10
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
11
|
+
*
|
|
12
|
+
* function Summarizer() {
|
|
13
|
+
* const { generate, result, isLoading } = useSummarize({
|
|
14
|
+
* connection: fetchServerSentEvents('/api/summarize'),
|
|
15
|
+
* })
|
|
16
|
+
*
|
|
17
|
+
* return (
|
|
18
|
+
* <div>
|
|
19
|
+
* <button onClick={() => generate({
|
|
20
|
+
* text: 'Long article text...',
|
|
21
|
+
* style: 'bullet-points',
|
|
22
|
+
* maxLength: 200,
|
|
23
|
+
* })}>
|
|
24
|
+
* Summarize
|
|
25
|
+
* </button>
|
|
26
|
+
* {isLoading() && <p>Summarizing...</p>}
|
|
27
|
+
* {result() && <p>{result()!.summary}</p>}
|
|
28
|
+
* </div>
|
|
29
|
+
* )
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
function useSummarize(options) {
|
|
34
|
+
const devtools = {
|
|
35
|
+
...options.devtools,
|
|
36
|
+
framework: "solid",
|
|
37
|
+
hookName: "useSummarize",
|
|
38
|
+
outputKind: "text"
|
|
39
|
+
};
|
|
40
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({
|
|
41
|
+
...options,
|
|
42
|
+
devtools
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
generate,
|
|
46
|
+
result,
|
|
47
|
+
isLoading,
|
|
48
|
+
error,
|
|
49
|
+
status,
|
|
50
|
+
stop,
|
|
51
|
+
reset
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
export { useSummarize };
|
|
57
|
+
//# sourceMappingURL=use-summarize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-summarize.js","names":[],"sources":["../src/use-summarize.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, SummarizationResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\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<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 /** Unique identifier for this generation instance */\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 * 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> {\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 /** Abort the current summarization */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\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<UseSummarizeOptions, 'onResult'> & {\n onResult?: (result: SummarizationResult) => TTransformed\n },\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 { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<SummarizeGenerateInput, SummarizationResult, TTransformed>({\n ...options,\n devtools,\n })\n\n return {\n generate: generate as (input: SummarizeGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+FA,SAAgB,aACd,SAKA;CACA,MAAM,WAAW;EACf,GAAG,QAAQ;EACX,WAAW;EACX,UAAU;EACV,YAAY;EACb;CACD,MAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,UACxD,cAAyE;EACvE,GAAG;EACH;EACD,CAAC;AAEJ,QAAO;EACK;EACV;EACA;EACA;EACA;EACA;EACA;EACD"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Accessor } from "solid-js";
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn, TranscriptionGenerateInput } from "@tanstack/ai-client";
|
|
3
|
+
import { StreamChunk, TranscriptionResult } from "@tanstack/ai";
|
|
4
|
+
|
|
5
|
+
//#region src/use-transcription.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for the useTranscription hook.
|
|
9
|
+
*
|
|
10
|
+
* @template TOutput - The transformed output type (defaults to TranscriptionResult)
|
|
11
|
+
*/
|
|
12
|
+
interface UseTranscriptionOptions<TOutput = TranscriptionResult> {
|
|
13
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
14
|
+
connection?: ConnectConnectionAdapter;
|
|
15
|
+
/** Direct async function for transcription */
|
|
16
|
+
fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>;
|
|
17
|
+
/** Unique identifier for this generation instance */
|
|
18
|
+
id?: string;
|
|
19
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
20
|
+
body?: Record<string, any>;
|
|
21
|
+
/** Display options for TanStack AI Devtools. */
|
|
22
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
23
|
+
/**
|
|
24
|
+
* Callback when transcription is complete. Can optionally return a transformed value.
|
|
25
|
+
*
|
|
26
|
+
* - Return a non-null value to transform and store it as the result
|
|
27
|
+
* - Return `null` to keep the previous result unchanged
|
|
28
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
29
|
+
*/
|
|
30
|
+
onResult?: (result: TranscriptionResult) => TOutput | null | void;
|
|
31
|
+
/** Callback when an error occurs */
|
|
32
|
+
onError?: (error: Error) => void;
|
|
33
|
+
/** Callback when progress is reported (0-100) */
|
|
34
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
35
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
36
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Return type for the useTranscription hook.
|
|
40
|
+
*
|
|
41
|
+
* @template TOutput - The transformed output type (defaults to TranscriptionResult)
|
|
42
|
+
*/
|
|
43
|
+
interface UseTranscriptionReturn<TOutput = TranscriptionResult> {
|
|
44
|
+
/** Trigger transcription */
|
|
45
|
+
generate: (input: TranscriptionGenerateInput) => Promise<void>;
|
|
46
|
+
/** The transcription result, or null */
|
|
47
|
+
result: Accessor<TOutput | null>;
|
|
48
|
+
/** Whether transcription is in progress */
|
|
49
|
+
isLoading: Accessor<boolean>;
|
|
50
|
+
/** Current error, if any */
|
|
51
|
+
error: Accessor<Error | undefined>;
|
|
52
|
+
/** Current state of the generation */
|
|
53
|
+
status: Accessor<GenerationClientState>;
|
|
54
|
+
/** Abort the current transcription */
|
|
55
|
+
stop: () => void;
|
|
56
|
+
/** Clear result, error, and return to idle */
|
|
57
|
+
reset: () => void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Solid hook for transcribing audio to text using AI models.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* import { useTranscription } from '@tanstack/ai-solid'
|
|
65
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
66
|
+
*
|
|
67
|
+
* function Transcriber() {
|
|
68
|
+
* const { generate, result, isLoading } = useTranscription({
|
|
69
|
+
* connection: fetchServerSentEvents('/api/transcribe'),
|
|
70
|
+
* })
|
|
71
|
+
*
|
|
72
|
+
* const handleFile = (e: Event) => {
|
|
73
|
+
* const input = e.target as HTMLInputElement
|
|
74
|
+
* const file = input.files?.[0]
|
|
75
|
+
* if (file) {
|
|
76
|
+
* const reader = new FileReader()
|
|
77
|
+
* reader.onload = () => {
|
|
78
|
+
* generate({ audio: reader.result as string, language: 'en' })
|
|
79
|
+
* }
|
|
80
|
+
* reader.readAsDataURL(file)
|
|
81
|
+
* }
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* return (
|
|
85
|
+
* <div>
|
|
86
|
+
* <input type="file" accept="audio/*" onChange={handleFile} />
|
|
87
|
+
* {isLoading() && <p>Transcribing...</p>}
|
|
88
|
+
* {result() && <p>{result()!.text}</p>}
|
|
89
|
+
* </div>
|
|
90
|
+
* )
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
declare function useTranscription<TTransformed = void>(options: Omit<UseTranscriptionOptions, 'onResult'> & {
|
|
95
|
+
onResult?: (result: TranscriptionResult) => TTransformed;
|
|
96
|
+
}): UseTranscriptionReturn<InferGenerationOutputFromReturn<TranscriptionResult, TTransformed>>;
|
|
97
|
+
//#endregion
|
|
98
|
+
export { UseTranscriptionOptions, UseTranscriptionReturn, useTranscription };
|
|
99
|
+
//# sourceMappingURL=use-transcription.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useGeneration } from "./use-generation.js";
|
|
2
|
+
|
|
3
|
+
//#region src/use-transcription.ts
|
|
4
|
+
/**
|
|
5
|
+
* Solid hook for transcribing audio to text using AI models.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { useTranscription } from '@tanstack/ai-solid'
|
|
10
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
11
|
+
*
|
|
12
|
+
* function Transcriber() {
|
|
13
|
+
* const { generate, result, isLoading } = useTranscription({
|
|
14
|
+
* connection: fetchServerSentEvents('/api/transcribe'),
|
|
15
|
+
* })
|
|
16
|
+
*
|
|
17
|
+
* const handleFile = (e: Event) => {
|
|
18
|
+
* const input = e.target as HTMLInputElement
|
|
19
|
+
* const file = input.files?.[0]
|
|
20
|
+
* if (file) {
|
|
21
|
+
* const reader = new FileReader()
|
|
22
|
+
* reader.onload = () => {
|
|
23
|
+
* generate({ audio: reader.result as string, language: 'en' })
|
|
24
|
+
* }
|
|
25
|
+
* reader.readAsDataURL(file)
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* return (
|
|
30
|
+
* <div>
|
|
31
|
+
* <input type="file" accept="audio/*" onChange={handleFile} />
|
|
32
|
+
* {isLoading() && <p>Transcribing...</p>}
|
|
33
|
+
* {result() && <p>{result()!.text}</p>}
|
|
34
|
+
* </div>
|
|
35
|
+
* )
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function useTranscription(options) {
|
|
40
|
+
const devtools = {
|
|
41
|
+
...options.devtools,
|
|
42
|
+
framework: "solid",
|
|
43
|
+
hookName: "useTranscription",
|
|
44
|
+
outputKind: "text"
|
|
45
|
+
};
|
|
46
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({
|
|
47
|
+
...options,
|
|
48
|
+
devtools
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
generate,
|
|
52
|
+
result,
|
|
53
|
+
isLoading,
|
|
54
|
+
error,
|
|
55
|
+
status,
|
|
56
|
+
stop,
|
|
57
|
+
reset
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { useTranscription };
|
|
63
|
+
//# sourceMappingURL=use-transcription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-transcription.js","names":[],"sources":["../src/use-transcription.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, TranscriptionResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\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<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 /** Unique identifier for this generation instance */\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 * 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<TOutput = TranscriptionResult> {\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 /** Abort the current transcription */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\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<UseTranscriptionOptions, 'onResult'> & {\n onResult?: (result: TranscriptionResult) => TTransformed\n },\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 { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<\n TranscriptionGenerateInput,\n TranscriptionResult,\n TTransformed\n >({ ...options, devtools })\n\n return {\n generate: generate as (input: TranscriptionGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqGA,SAAgB,iBACd,SAKA;CACA,MAAM,WAAW;EACf,GAAG,QAAQ;EACX,WAAW;EACX,UAAU;EACV,YAAY;EACb;CACD,MAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,UACxD,cAIE;EAAE,GAAG;EAAS;EAAU,CAAC;AAE7B,QAAO;EACK;EACV;EACA;EACA;EACA;EACA;EACA;EACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-solid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
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.19.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"solid-js": ">=1.9.10",
|
|
52
|
-
"@tanstack/ai": "^0.
|
|
52
|
+
"@tanstack/ai": "^0.38.0"
|
|
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.0.14",
|
|
64
|
-
"@tanstack/ai": "0.
|
|
64
|
+
"@tanstack/ai": "0.38.0"
|
|
65
65
|
},
|
|
66
66
|
"scripts": {
|
|
67
67
|
"clean": "premove ./build ./dist",
|
package/src/index.ts
CHANGED
|
@@ -47,6 +47,12 @@ export type {
|
|
|
47
47
|
UseGenerateVideoReturn,
|
|
48
48
|
} from './use-generate-video'
|
|
49
49
|
|
|
50
|
+
export { useAudioRecorder } from './use-audio-recorder'
|
|
51
|
+
export type {
|
|
52
|
+
UseAudioRecorderOptions,
|
|
53
|
+
UseAudioRecorderReturn,
|
|
54
|
+
} from './use-audio-recorder'
|
|
55
|
+
|
|
50
56
|
// Re-export from ai-client for convenience
|
|
51
57
|
export {
|
|
52
58
|
fetchServerSentEvents,
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createSignal, onCleanup } from 'solid-js'
|
|
2
|
+
import { AudioRecorder } from '@tanstack/ai-client'
|
|
3
|
+
import type {
|
|
4
|
+
AudioRecorderOptions,
|
|
5
|
+
AudioRecording,
|
|
6
|
+
InferAudioRecordingOutput,
|
|
7
|
+
} from '@tanstack/ai-client'
|
|
8
|
+
|
|
9
|
+
export type UseAudioRecorderOptions<TOnComplete> = AudioRecorderOptions & {
|
|
10
|
+
/**
|
|
11
|
+
* Optional transform applied to the recording when `stop()` resolves. Its
|
|
12
|
+
* (awaited) return value becomes `recording` and the resolved value of
|
|
13
|
+
* `stop()`. Return nothing to keep the raw `AudioRecording`.
|
|
14
|
+
*/
|
|
15
|
+
onComplete?: TOnComplete
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface UseAudioRecorderReturn<TOutput> {
|
|
19
|
+
/** Solid accessor: latest recording (transformed if `onComplete` provided), or null. */
|
|
20
|
+
recording: () => TOutput | null
|
|
21
|
+
/** Solid accessor: true while actively capturing audio. */
|
|
22
|
+
isRecording: () => boolean
|
|
23
|
+
/** Whether the browser supports recording. */
|
|
24
|
+
isSupported: boolean
|
|
25
|
+
start: () => Promise<void>
|
|
26
|
+
/** Stop and resolve with the completed recording (transformed if `onComplete` provided). */
|
|
27
|
+
stop: () => Promise<TOutput>
|
|
28
|
+
/** Discard the in-progress recording and release the mic. */
|
|
29
|
+
cancel: () => void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Solid hook for recording an audio message. The resolved recording carries
|
|
34
|
+
* `.part` (for `useChat.sendMessage`) and `.base64` (for generation hooks).
|
|
35
|
+
*
|
|
36
|
+
* Errors are delivered via `onError`. `start()` and `stop()` also reject on
|
|
37
|
+
* failure (and `stop()` rejects with `Recording cancelled` if `cancel()` runs
|
|
38
|
+
* while a stop is in flight, e.g. on unmount) — handle one channel, not both.
|
|
39
|
+
*/
|
|
40
|
+
export function useAudioRecorder<
|
|
41
|
+
TOnComplete extends (recording: AudioRecording) => unknown,
|
|
42
|
+
>(
|
|
43
|
+
options: UseAudioRecorderOptions<TOnComplete>,
|
|
44
|
+
): UseAudioRecorderReturn<InferAudioRecordingOutput<TOnComplete>>
|
|
45
|
+
export function useAudioRecorder(
|
|
46
|
+
options?: UseAudioRecorderOptions<undefined>,
|
|
47
|
+
): UseAudioRecorderReturn<AudioRecording>
|
|
48
|
+
export function useAudioRecorder(
|
|
49
|
+
options: UseAudioRecorderOptions<(recording: AudioRecording) => unknown> = {},
|
|
50
|
+
): UseAudioRecorderReturn<unknown> {
|
|
51
|
+
const recorder = new AudioRecorder({
|
|
52
|
+
...(options.audio !== undefined && { audio: options.audio }),
|
|
53
|
+
...(options.mimeType !== undefined && { mimeType: options.mimeType }),
|
|
54
|
+
...(options.onError !== undefined && { onError: options.onError }),
|
|
55
|
+
})
|
|
56
|
+
const [isRecording, setIsRecording] = createSignal(false)
|
|
57
|
+
const [recording, setRecording] = createSignal<unknown>(null)
|
|
58
|
+
|
|
59
|
+
const unsubscribe = recorder.subscribe((state) => {
|
|
60
|
+
setIsRecording(state === 'recording')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
onCleanup(() => {
|
|
64
|
+
unsubscribe()
|
|
65
|
+
recorder.cancel()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
const stop = async (): Promise<unknown> => {
|
|
69
|
+
const rawRecording = await recorder.stop()
|
|
70
|
+
const transformed = await options.onComplete?.(rawRecording)
|
|
71
|
+
// Only `undefined` (returning nothing) keeps the raw recording; a returned
|
|
72
|
+
// null is a real value, matching the inferred output type.
|
|
73
|
+
const output = transformed === undefined ? rawRecording : transformed
|
|
74
|
+
// Store via updater so a function-valued transform result isn't invoked.
|
|
75
|
+
setRecording(() => output)
|
|
76
|
+
return output
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
recording,
|
|
81
|
+
isRecording,
|
|
82
|
+
isSupported: AudioRecorder.isSupported(),
|
|
83
|
+
start: () => recorder.start(),
|
|
84
|
+
stop,
|
|
85
|
+
cancel: () => recorder.cancel(),
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
ConnectConnectionAdapter,
|
|
7
7
|
GenerationClientState,
|
|
8
8
|
GenerationFetcher,
|
|
9
|
-
|
|
9
|
+
InferGenerationOutputFromReturn,
|
|
10
10
|
} from '@tanstack/ai-client'
|
|
11
11
|
import type { Accessor } from 'solid-js'
|
|
12
12
|
|
|
@@ -88,15 +88,12 @@ export interface UseGenerateAudioReturn<TOutput = AudioGenerationResult> {
|
|
|
88
88
|
* }
|
|
89
89
|
* ```
|
|
90
90
|
*/
|
|
91
|
-
export function useGenerateAudio<
|
|
92
|
-
TOnResult extends ((result: AudioGenerationResult) => any) | undefined =
|
|
93
|
-
undefined,
|
|
94
|
-
>(
|
|
91
|
+
export function useGenerateAudio<TTransformed = void>(
|
|
95
92
|
options: Omit<UseGenerateAudioOptions, 'onResult'> & {
|
|
96
|
-
onResult?:
|
|
93
|
+
onResult?: (result: AudioGenerationResult) => TTransformed
|
|
97
94
|
},
|
|
98
95
|
): UseGenerateAudioReturn<
|
|
99
|
-
|
|
96
|
+
InferGenerationOutputFromReturn<AudioGenerationResult, TTransformed>
|
|
100
97
|
> {
|
|
101
98
|
const devtools = {
|
|
102
99
|
...options.devtools,
|
|
@@ -105,7 +102,7 @@ export function useGenerateAudio<
|
|
|
105
102
|
outputKind: 'audio' as const,
|
|
106
103
|
}
|
|
107
104
|
const { generate, result, isLoading, error, status, stop, reset } =
|
|
108
|
-
useGeneration<AudioGenerateInput, AudioGenerationResult,
|
|
105
|
+
useGeneration<AudioGenerateInput, AudioGenerationResult, TTransformed>({
|
|
109
106
|
...options,
|
|
110
107
|
devtools,
|
|
111
108
|
})
|
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
GenerationClientState,
|
|
7
7
|
GenerationFetcher,
|
|
8
8
|
ImageGenerateInput,
|
|
9
|
-
|
|
9
|
+
InferGenerationOutputFromReturn,
|
|
10
10
|
} from '@tanstack/ai-client'
|
|
11
11
|
import type { Accessor } from 'solid-js'
|
|
12
12
|
|
|
@@ -96,15 +96,12 @@ export interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {
|
|
|
96
96
|
* }
|
|
97
97
|
* ```
|
|
98
98
|
*/
|
|
99
|
-
export function useGenerateImage<
|
|
100
|
-
TOnResult extends ((result: ImageGenerationResult) => any) | undefined =
|
|
101
|
-
undefined,
|
|
102
|
-
>(
|
|
99
|
+
export function useGenerateImage<TTransformed = void>(
|
|
103
100
|
options: Omit<UseGenerateImageOptions, 'onResult'> & {
|
|
104
|
-
onResult?:
|
|
101
|
+
onResult?: (result: ImageGenerationResult) => TTransformed
|
|
105
102
|
},
|
|
106
103
|
): UseGenerateImageReturn<
|
|
107
|
-
|
|
104
|
+
InferGenerationOutputFromReturn<ImageGenerationResult, TTransformed>
|
|
108
105
|
> {
|
|
109
106
|
const devtools = {
|
|
110
107
|
...options.devtools,
|
|
@@ -113,7 +110,7 @@ export function useGenerateImage<
|
|
|
113
110
|
outputKind: 'image' as const,
|
|
114
111
|
}
|
|
115
112
|
const { generate, result, isLoading, error, status, stop, reset } =
|
|
116
|
-
useGeneration<ImageGenerateInput, ImageGenerationResult,
|
|
113
|
+
useGeneration<ImageGenerateInput, ImageGenerationResult, TTransformed>({
|
|
117
114
|
...options,
|
|
118
115
|
devtools,
|
|
119
116
|
})
|