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