Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

use-transcription.ts

138 lines4.3 KB

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