@tanstack/ai-solid 0.6.3 → 0.6.6

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.
@@ -0,0 +1,123 @@
1
+ import { useGeneration } from './use-generation'
2
+ import type { StreamChunk, TranscriptionResult } from '@tanstack/ai'
3
+ import type {
4
+ ConnectionAdapter,
5
+ GenerationClientState,
6
+ GenerationFetcher,
7
+ InferGenerationOutput,
8
+ TranscriptionGenerateInput,
9
+ } from '@tanstack/ai-client'
10
+ import type { Accessor } from 'solid-js'
11
+
12
+ /**
13
+ * Options for the useTranscription hook.
14
+ *
15
+ * @template TOutput - The transformed output type (defaults to TranscriptionResult)
16
+ */
17
+ export interface UseTranscriptionOptions<TOutput = TranscriptionResult> {
18
+ /** Connection adapter for streaming transport (SSE, HTTP stream, custom) */
19
+ connection?: ConnectionAdapter
20
+ /** Direct async function for transcription */
21
+ fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>
22
+ /** Unique identifier for this generation instance */
23
+ id?: string
24
+ /** Additional body parameters to send with ConnectionAdapter requests */
25
+ body?: Record<string, any>
26
+ /**
27
+ * Callback when transcription is complete. Can optionally return a transformed value.
28
+ *
29
+ * - Return a non-null value to transform and store it as the result
30
+ * - Return `null` to keep the previous result unchanged
31
+ * - Return nothing (`void`) to store the raw result as-is
32
+ */
33
+ onResult?: (result: TranscriptionResult) => TOutput | null | void
34
+ /** Callback when an error occurs */
35
+ onError?: (error: Error) => void
36
+ /** Callback when progress is reported (0-100) */
37
+ onProgress?: (progress: number, message?: string) => void
38
+ /** Callback for each stream chunk (ConnectionAdapter mode only) */
39
+ onChunk?: (chunk: StreamChunk) => void
40
+ }
41
+
42
+ /**
43
+ * Return type for the useTranscription hook.
44
+ *
45
+ * @template TOutput - The transformed output type (defaults to TranscriptionResult)
46
+ */
47
+ export interface UseTranscriptionReturn<TOutput = TranscriptionResult> {
48
+ /** Trigger transcription */
49
+ generate: (input: TranscriptionGenerateInput) => Promise<void>
50
+ /** The transcription result, or null */
51
+ result: Accessor<TOutput | null>
52
+ /** Whether transcription is in progress */
53
+ isLoading: Accessor<boolean>
54
+ /** Current error, if any */
55
+ error: Accessor<Error | undefined>
56
+ /** Current state of the generation */
57
+ status: Accessor<GenerationClientState>
58
+ /** Abort the current transcription */
59
+ stop: () => void
60
+ /** Clear result, error, and return to idle */
61
+ reset: () => void
62
+ }
63
+
64
+ /**
65
+ * Solid hook for transcribing audio to text using AI models.
66
+ *
67
+ * @example
68
+ * ```tsx
69
+ * import { useTranscription } from '@tanstack/ai-solid'
70
+ * import { fetchServerSentEvents } from '@tanstack/ai-client'
71
+ *
72
+ * function Transcriber() {
73
+ * const { generate, result, isLoading } = useTranscription({
74
+ * connection: fetchServerSentEvents('/api/transcribe'),
75
+ * })
76
+ *
77
+ * const handleFile = (e: Event) => {
78
+ * const input = e.target as HTMLInputElement
79
+ * const file = input.files?.[0]
80
+ * if (file) {
81
+ * const reader = new FileReader()
82
+ * reader.onload = () => {
83
+ * generate({ audio: reader.result as string, language: 'en' })
84
+ * }
85
+ * reader.readAsDataURL(file)
86
+ * }
87
+ * }
88
+ *
89
+ * return (
90
+ * <div>
91
+ * <input type="file" accept="audio/*" onChange={handleFile} />
92
+ * {isLoading() && <p>Transcribing...</p>}
93
+ * {result() && <p>{result()!.text}</p>}
94
+ * </div>
95
+ * )
96
+ * }
97
+ * ```
98
+ */
99
+ export function useTranscription<
100
+ TOnResult extends ((result: TranscriptionResult) => any) | undefined =
101
+ undefined,
102
+ >(
103
+ options: Omit<UseTranscriptionOptions, 'onResult'> & {
104
+ onResult?: TOnResult
105
+ },
106
+ ): UseTranscriptionReturn<
107
+ InferGenerationOutput<TranscriptionResult, TOnResult>
108
+ > {
109
+ const { generate, result, isLoading, error, status, stop, reset } =
110
+ useGeneration<TranscriptionGenerateInput, TranscriptionResult, TOnResult>(
111
+ options,
112
+ )
113
+
114
+ return {
115
+ generate: generate as (input: TranscriptionGenerateInput) => Promise<void>,
116
+ result,
117
+ isLoading,
118
+ error,
119
+ status,
120
+ stop,
121
+ reset,
122
+ }
123
+ }