ai-input-react 1.0.0-beta.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,255 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+
4
+ /**
5
+ * Current state of the AiInput component
6
+ */
7
+ type AiInputState = 'idle' | 'loading' | 'success' | 'error' | 'rate-limited' | 'recording';
8
+ /**
9
+ * Rate limiting configuration for UI protection
10
+ * Note: This is soft rate limiting for UX only.
11
+ * Actual rate limiting should be handled by the AI provider.
12
+ */
13
+ interface RateLimitConfig {
14
+ /** Cooldown between requests in milliseconds */
15
+ cooldownMs: number;
16
+ /** Maximum number of requests allowed in the time window */
17
+ maxRequests: number;
18
+ /** Time window in milliseconds for counting requests */
19
+ windowMs: number;
20
+ }
21
+ /**
22
+ * Audio recording configuration
23
+ */
24
+ interface AudioConfig {
25
+ /** Maximum recording duration in milliseconds */
26
+ maxDurationMs: number;
27
+ /**
28
+ * Allowed MIME types for recording
29
+ * @example ['audio/webm', 'audio/mp4', 'audio/ogg']
30
+ */
31
+ mimeTypes: string[];
32
+ }
33
+ /**
34
+ * Transport function for sending input to AI API.
35
+ * Must be provided by the host application.
36
+ *
37
+ * @param input - Text string or audio Blob to send
38
+ * @returns Promise resolving to the API response
39
+ */
40
+ type SendFunction = (input: string | Blob) => Promise<unknown>;
41
+ /**
42
+ * Props passed to render function for headless usage
43
+ */
44
+ interface AiInputRenderProps {
45
+ /** Current component state */
46
+ state: AiInputState;
47
+ /** Error if state is 'error' */
48
+ error: Error | null;
49
+ /** Result from last successful request */
50
+ result: unknown;
51
+ /** Current text value (controlled) */
52
+ text: string;
53
+ /** Update text value */
54
+ setText: (value: string) => void;
55
+ /** Submit current text or audio */
56
+ submit: () => void;
57
+ /** Whether submit is currently allowed */
58
+ canSubmit: boolean;
59
+ /** Whether currently recording */
60
+ isRecording: boolean;
61
+ /** Start audio recording */
62
+ startRecording: () => Promise<void>;
63
+ /** Stop audio recording and send */
64
+ stopRecording: () => void;
65
+ /** Cancel audio recording (discard) */
66
+ cancelRecording: () => void;
67
+ /** Current recording duration in milliseconds */
68
+ recordingDuration: number;
69
+ /** Maximum recording duration in milliseconds */
70
+ maxRecordingDuration: number;
71
+ /** Audio levels for waveform visualization (0-1 normalized, 12 bars) */
72
+ audioLevels: number[];
73
+ /** Remaining cooldown time in milliseconds */
74
+ cooldownRemaining: number;
75
+ /** Remaining requests in current window */
76
+ requestsRemaining: number;
77
+ /** Reset component to idle state */
78
+ reset: () => void;
79
+ }
80
+ /**
81
+ * Props for the AiInput component
82
+ */
83
+ interface AiInputProps {
84
+ /**
85
+ * Transport function for sending input to AI API.
86
+ * Must be provided by the host application.
87
+ */
88
+ send: SendFunction;
89
+ /**
90
+ * Transport function specifically for audio (optional).
91
+ * If provided, audio will be sent via this function.
92
+ * If not provided, audio will be sent via `send`.
93
+ */
94
+ sendAudio?: SendFunction;
95
+ /** Rate limiting configuration (optional) */
96
+ rateLimit?: Partial<RateLimitConfig>;
97
+ /** Audio configuration (optional) */
98
+ audioConfig?: Partial<AudioConfig>;
99
+ /** Callback when request succeeds */
100
+ onSuccess?: (result: unknown) => void;
101
+ /** Callback when request fails */
102
+ onError?: (error: Error) => void;
103
+ /**
104
+ * Callback when audio transcription is received.
105
+ * Use this to set the text in the input after transcription.
106
+ */
107
+ onTranscription?: (text: string) => void;
108
+ /**
109
+ * Render function for headless usage.
110
+ * When provided, default UI is not rendered.
111
+ */
112
+ children?: (props: AiInputRenderProps) => ReactNode;
113
+ /** Placeholder text for input */
114
+ placeholder?: string;
115
+ /** Additional CSS classes for the container */
116
+ className?: string;
117
+ /** Whether the input is disabled */
118
+ disabled?: boolean;
119
+ }
120
+ /**
121
+ * Options for useRateLimiter hook
122
+ */
123
+ interface UseRateLimiterOptions {
124
+ cooldownMs: number;
125
+ maxRequests: number;
126
+ windowMs: number;
127
+ }
128
+ /**
129
+ * Return type for useRateLimiter hook
130
+ */
131
+ interface UseRateLimiterReturn {
132
+ canRequest: boolean;
133
+ cooldownRemaining: number;
134
+ requestsRemaining: number;
135
+ recordRequest: () => void;
136
+ reset: () => void;
137
+ }
138
+ /**
139
+ * Options for useAudioRecorder hook
140
+ */
141
+ interface UseAudioRecorderOptions {
142
+ maxDurationMs: number;
143
+ mimeTypes: string[];
144
+ onRecordingComplete?: (blob: Blob) => void;
145
+ }
146
+ /**
147
+ * Return type for useAudioRecorder hook
148
+ */
149
+ interface UseAudioRecorderReturn {
150
+ isRecording: boolean;
151
+ isSupported: boolean;
152
+ duration: number;
153
+ audioBlob: Blob | null;
154
+ audioLevels: number[];
155
+ error: Error | null;
156
+ startRecording: () => Promise<void>;
157
+ stopRecording: () => void;
158
+ cancelRecording: () => void;
159
+ reset: () => void;
160
+ }
161
+ /**
162
+ * Options for useAiInput hook
163
+ */
164
+ interface UseAiInputOptions {
165
+ send: SendFunction;
166
+ sendAudio?: SendFunction;
167
+ rateLimit?: Partial<RateLimitConfig>;
168
+ audioConfig?: Partial<AudioConfig>;
169
+ onSuccess?: (result: unknown) => void;
170
+ onError?: (error: Error) => void;
171
+ onTranscription?: (text: string) => void;
172
+ }
173
+ /**
174
+ * Return type for useAiInput hook
175
+ */
176
+ type UseAiInputReturn = AiInputRenderProps;
177
+ /** @deprecated Use AiInputProps without mode */
178
+ type AiInputMode = 'text' | 'audio';
179
+
180
+ /**
181
+ * AiInput Component
182
+ *
183
+ * A React component for text/audio input with AI API integration.
184
+ * Unified design with text input and audio recording in a single component.
185
+ *
186
+ * @example
187
+ * // Basic usage
188
+ * <AiInput
189
+ * send={async (input) => {
190
+ * const response = await fetch('/api/chat', {
191
+ * method: 'POST',
192
+ * body: JSON.stringify({ message: input }),
193
+ * })
194
+ * return response.json()
195
+ * }}
196
+ * onSuccess={(result) => console.log(result)}
197
+ * />
198
+ *
199
+ * @example
200
+ * // With separate audio handler and transcription
201
+ * <AiInput
202
+ * send={sendTextFn}
203
+ * sendAudio={sendAudioFn}
204
+ * onTranscription={(text) => console.log('Transcribed:', text)}
205
+ * />
206
+ *
207
+ * @example
208
+ * // Headless mode with custom UI
209
+ * <AiInput send={sendFn}>
210
+ * {({ text, setText, submit, state, isRecording, audioLevels }) => (
211
+ * <div>
212
+ * {isRecording ? (
213
+ * <MyWaveform levels={audioLevels} />
214
+ * ) : (
215
+ * <input value={text} onChange={(e) => setText(e.target.value)} />
216
+ * )}
217
+ * <button onClick={submit}>Send</button>
218
+ * </div>
219
+ * )}
220
+ * </AiInput>
221
+ */
222
+ declare function AiInput({ send, sendAudio, rateLimit, audioConfig, onSuccess, onError, onTranscription, children, placeholder, className, disabled, }: AiInputProps): react_jsx_runtime.JSX.Element;
223
+
224
+ /**
225
+ * Main hook for AI input functionality.
226
+ * Combines rate limiting, audio recording, and API communication.
227
+ * Unified design - text and audio in single component.
228
+ *
229
+ * @param options - Configuration options
230
+ * @returns Complete state and controls for AI input
231
+ */
232
+ declare function useAiInput(options: UseAiInputOptions): UseAiInputReturn;
233
+
234
+ /**
235
+ * Hook for audio recording using Web APIs.
236
+ * Uses navigator.mediaDevices, MediaRecorder, and Web Audio API for visualization.
237
+ *
238
+ * @param options - Audio recording configuration
239
+ * @returns Audio recorder state and controls
240
+ */
241
+ declare function useAudioRecorder(options?: Partial<UseAudioRecorderOptions>): UseAudioRecorderReturn;
242
+
243
+ /**
244
+ * Hook for soft rate limiting at the UI level.
245
+ * Provides UX protection by tracking requests and enforcing cooldowns.
246
+ *
247
+ * Note: This is not a security measure. Actual rate limiting
248
+ * should be handled by the AI provider or backend.
249
+ *
250
+ * @param options - Rate limiting configuration
251
+ * @returns Rate limiter state and controls
252
+ */
253
+ declare function useRateLimiter(options?: Partial<UseRateLimiterOptions>): UseRateLimiterReturn;
254
+
255
+ export { AiInput, type AiInputMode, type AiInputProps, type AiInputRenderProps, type AiInputState, type AudioConfig, type RateLimitConfig, type SendFunction, type UseAiInputOptions, type UseAiInputReturn, type UseAudioRecorderOptions, type UseAudioRecorderReturn, type UseRateLimiterOptions, type UseRateLimiterReturn, useAiInput, useAudioRecorder, useRateLimiter };