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