@volley/recognition-client-sdk-node22 0.1.424

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.
Files changed (58) hide show
  1. package/README.md +344 -0
  2. package/dist/browser.bundled.d.ts +1280 -0
  3. package/dist/browser.d.ts +10 -0
  4. package/dist/browser.d.ts.map +1 -0
  5. package/dist/config-builder.d.ts +134 -0
  6. package/dist/config-builder.d.ts.map +1 -0
  7. package/dist/errors.d.ts +41 -0
  8. package/dist/errors.d.ts.map +1 -0
  9. package/dist/factory.d.ts +36 -0
  10. package/dist/factory.d.ts.map +1 -0
  11. package/dist/index.bundled.d.ts +2572 -0
  12. package/dist/index.d.ts +16 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +10199 -0
  15. package/dist/index.js.map +7 -0
  16. package/dist/recog-client-sdk.browser.d.ts +10 -0
  17. package/dist/recog-client-sdk.browser.d.ts.map +1 -0
  18. package/dist/recog-client-sdk.browser.js +5746 -0
  19. package/dist/recog-client-sdk.browser.js.map +7 -0
  20. package/dist/recognition-client.d.ts +128 -0
  21. package/dist/recognition-client.d.ts.map +1 -0
  22. package/dist/recognition-client.types.d.ts +271 -0
  23. package/dist/recognition-client.types.d.ts.map +1 -0
  24. package/dist/simplified-vgf-recognition-client.d.ts +178 -0
  25. package/dist/simplified-vgf-recognition-client.d.ts.map +1 -0
  26. package/dist/utils/audio-ring-buffer.d.ts +69 -0
  27. package/dist/utils/audio-ring-buffer.d.ts.map +1 -0
  28. package/dist/utils/message-handler.d.ts +45 -0
  29. package/dist/utils/message-handler.d.ts.map +1 -0
  30. package/dist/utils/url-builder.d.ts +28 -0
  31. package/dist/utils/url-builder.d.ts.map +1 -0
  32. package/dist/vgf-recognition-mapper.d.ts +66 -0
  33. package/dist/vgf-recognition-mapper.d.ts.map +1 -0
  34. package/dist/vgf-recognition-state.d.ts +91 -0
  35. package/dist/vgf-recognition-state.d.ts.map +1 -0
  36. package/package.json +74 -0
  37. package/src/browser.ts +24 -0
  38. package/src/config-builder.spec.ts +265 -0
  39. package/src/config-builder.ts +240 -0
  40. package/src/errors.ts +84 -0
  41. package/src/factory.spec.ts +215 -0
  42. package/src/factory.ts +47 -0
  43. package/src/index.ts +127 -0
  44. package/src/recognition-client.spec.ts +889 -0
  45. package/src/recognition-client.ts +844 -0
  46. package/src/recognition-client.types.ts +338 -0
  47. package/src/simplified-vgf-recognition-client.integration.spec.ts +718 -0
  48. package/src/simplified-vgf-recognition-client.spec.ts +1525 -0
  49. package/src/simplified-vgf-recognition-client.ts +524 -0
  50. package/src/utils/audio-ring-buffer.spec.ts +335 -0
  51. package/src/utils/audio-ring-buffer.ts +170 -0
  52. package/src/utils/message-handler.spec.ts +311 -0
  53. package/src/utils/message-handler.ts +131 -0
  54. package/src/utils/url-builder.spec.ts +252 -0
  55. package/src/utils/url-builder.ts +92 -0
  56. package/src/vgf-recognition-mapper.spec.ts +78 -0
  57. package/src/vgf-recognition-mapper.ts +232 -0
  58. package/src/vgf-recognition-state.ts +102 -0
@@ -0,0 +1,338 @@
1
+ /**
2
+ * Recognition Client Types
3
+ *
4
+ * Type definitions and interfaces for the recognition client SDK.
5
+ * These interfaces enable dependency injection, testing, and alternative implementations.
6
+ */
7
+
8
+ import {
9
+ TranscriptionResultV1,
10
+ FunctionCallResultV1,
11
+ MetadataResultV1,
12
+ ErrorResultV1,
13
+ ASRRequestConfig,
14
+ GameContextV1,
15
+ Stage
16
+ } from '@recog/shared-types';
17
+
18
+ /**
19
+ * Client connection state enum
20
+ * Represents the various states a recognition client can be in during its lifecycle
21
+ */
22
+ export enum ClientState {
23
+ /** Initial state, no connection established */
24
+ INITIAL = 'initial',
25
+
26
+ /** Actively establishing WebSocket connection */
27
+ CONNECTING = 'connecting',
28
+
29
+ /** WebSocket connected but waiting for server ready signal */
30
+ CONNECTED = 'connected',
31
+
32
+ /** Server ready, can send audio */
33
+ READY = 'ready',
34
+
35
+ /** Sent stop signal, waiting for final transcript */
36
+ STOPPING = 'stopping',
37
+
38
+ /** Connection closed normally after stop */
39
+ STOPPED = 'stopped',
40
+
41
+ /** Connection failed or lost unexpectedly */
42
+ FAILED = 'failed'
43
+ }
44
+
45
+ /**
46
+ * Callback URL configuration with message type filtering
47
+ */
48
+ export interface RecognitionCallbackUrl {
49
+ /** The callback URL endpoint */
50
+ url: string;
51
+
52
+ /** Array of message types to send to this URL. If empty/undefined, all types are sent */
53
+ messageTypes?: Array<string | number>;
54
+ }
55
+
56
+ // Legacy alias for backward compatibility
57
+ export type IRecognitionCallbackUrl = RecognitionCallbackUrl;
58
+
59
+ export interface IRecognitionClientConfig {
60
+ /**
61
+ * WebSocket endpoint URL (optional)
62
+ * Either `url` or `stage` must be provided.
63
+ * If both are provided, `url` takes precedence.
64
+ *
65
+ * Example with explicit URL:
66
+ * ```typescript
67
+ * { url: 'wss://custom-endpoint.example.com/ws/v1/recognize' }
68
+ * ```
69
+ */
70
+ url?: string;
71
+
72
+ /**
73
+ * Stage for recognition service (recommended)
74
+ * Either `url` or `stage` must be provided.
75
+ * If both are provided, `url` takes precedence.
76
+ * Defaults to production if neither is provided.
77
+ *
78
+ * Example with STAGES enum (recommended):
79
+ * ```typescript
80
+ * import { STAGES } from '@recog/shared-types';
81
+ * { stage: STAGES.STAGING }
82
+ * ```
83
+ *
84
+ * String values also accepted:
85
+ * ```typescript
86
+ * { stage: 'staging' } // STAGES.LOCAL | STAGES.DEV | STAGES.STAGING | STAGES.PRODUCTION
87
+ * ```
88
+ */
89
+ stage?: Stage | string;
90
+
91
+ /** ASR configuration (provider, model, language, etc.) - optional */
92
+ asrRequestConfig?: ASRRequestConfig;
93
+
94
+ /** Game context for improved recognition accuracy */
95
+ gameContext?: GameContextV1;
96
+
97
+ /**
98
+ * Game ID for tracking and routing purposes (optional)
99
+ * If provided, this is added to the WebSocket URL as a query parameter.
100
+ * If gameContext is also provided, this takes precedence over gameContext.gameId.
101
+ */
102
+ gameId?: string;
103
+
104
+ /** Audio utterance ID (optional) - if not provided, a UUID v4 will be generated */
105
+ audioUtteranceId?: string;
106
+
107
+ /** Callback URLs for server-side notifications with optional message type filtering (optional)
108
+ * Game side only need to use it if another service need to be notified about the transcription results.
109
+ */
110
+ callbackUrls?: RecognitionCallbackUrl[];
111
+
112
+ /** User identification (optional) */
113
+ userId?: string;
114
+
115
+ /** Game session identification (optional). called 'sessionId' in Platform and most games. */
116
+ gameSessionId?: string;
117
+
118
+ /** Device identification (optional) */
119
+ deviceId?: string;
120
+
121
+ /** Account identification (optional) */
122
+ accountId?: string;
123
+
124
+ /** Question answer identifier for tracking Q&A sessions (optional and tracking purpose only) */
125
+ questionAnswerId?: string;
126
+
127
+ /** Platform for audio recording device (optional, e.g., 'ios', 'android', 'web', 'unity') */
128
+ platform?: string;
129
+
130
+ /** Callback when transcript is received */
131
+ onTranscript?: (result: TranscriptionResultV1) => void;
132
+
133
+ /**
134
+ * Callback when function call is received
135
+ * Note: Not supported in 2025. P2 feature for future speech-to-function-call capability.
136
+ */
137
+ onFunctionCall?: (result: FunctionCallResultV1) => void;
138
+
139
+ /** Callback when metadata is received. Only once after transcription is complete.*/
140
+ onMetadata?: (metadata: MetadataResultV1) => void;
141
+
142
+ /** Callback when error occurs */
143
+ onError?: (error: ErrorResultV1) => void;
144
+
145
+ /** Callback when connected to WebSocket */
146
+ onConnected?: () => void;
147
+
148
+ /**
149
+ * Callback when WebSocket disconnects
150
+ * @param code - WebSocket close code (1000 = normal, 1006 = abnormal, etc.)
151
+ * @param reason - Close reason string
152
+ */
153
+ onDisconnected?: (code: number, reason: string) => void;
154
+
155
+ /** High water mark for backpressure control (bytes) */
156
+ highWaterMark?: number;
157
+
158
+ /** Low water mark for backpressure control (bytes) */
159
+ lowWaterMark?: number;
160
+
161
+ /** Maximum buffer duration in seconds (default: 60s) */
162
+ maxBufferDurationSec?: number;
163
+
164
+ /** Expected chunks per second for ring buffer sizing (default: 100) */
165
+ chunksPerSecond?: number;
166
+
167
+ /**
168
+ * Connection retry configuration (optional)
169
+ * Only applies to initial connection establishment, not mid-stream interruptions.
170
+ *
171
+ * Default: { maxAttempts: 4, delayMs: 200 } (try once, retry 3 times = 4 total attempts)
172
+ *
173
+ * Timing: Attempt 1 → FAIL → wait 200ms → Attempt 2 → FAIL → wait 200ms → Attempt 3 → FAIL → wait 200ms → Attempt 4
174
+ *
175
+ * Example:
176
+ * ```typescript
177
+ * {
178
+ * connectionRetry: {
179
+ * maxAttempts: 2, // Try connecting up to 2 times (1 retry)
180
+ * delayMs: 500 // Wait 500ms between attempts
181
+ * }
182
+ * }
183
+ * ```
184
+ */
185
+ connectionRetry?: {
186
+ /** Maximum number of connection attempts (default: 4, min: 1, max: 5) */
187
+ maxAttempts?: number;
188
+ /** Delay in milliseconds between retry attempts (default: 200ms) */
189
+ delayMs?: number;
190
+ };
191
+
192
+ /**
193
+ * Optional logger function for debugging
194
+ * If not provided, no logging will occur
195
+ * @param level - Log level: 'debug', 'info', 'warn', 'error'
196
+ * @param message - Log message
197
+ * @param data - Optional additional data
198
+ */
199
+ logger?: (level: 'debug' | 'info' | 'warn' | 'error', message: string, data?: any) => void;
200
+ }
201
+
202
+ /**
203
+ * Recognition Client Interface
204
+ *
205
+ * Main interface for real-time speech recognition clients.
206
+ * Provides methods for connection management, audio streaming, and session control.
207
+ */
208
+ export interface IRecognitionClient {
209
+ /**
210
+ * Connect to the WebSocket endpoint
211
+ * @returns Promise that resolves when connected
212
+ * @throws Error if connection fails or times out
213
+ */
214
+ connect(): Promise<void>;
215
+
216
+ /**
217
+ * Send audio data to the recognition service
218
+ * Audio is buffered locally and sent when connection is ready.
219
+ * @param audioData - PCM audio data as ArrayBuffer, typed array view, or Blob
220
+ */
221
+ sendAudio(audioData: ArrayBuffer | ArrayBufferView | Blob): void;
222
+
223
+ /**
224
+ * Stop recording and wait for final transcript
225
+ * The server will close the connection after sending the final transcript.
226
+ * @returns Promise that resolves when final transcript is received
227
+ */
228
+ stopRecording(): Promise<void>;
229
+
230
+ /**
231
+ * Force stop and immediately close connection without waiting for server
232
+ *
233
+ * WARNING: This is an abnormal shutdown that bypasses the graceful stop flow:
234
+ * - Does NOT wait for server to process remaining audio
235
+ * - Does NOT receive final transcript from server
236
+ * - Immediately closes WebSocket connection
237
+ * - Cleans up resources (buffers, listeners)
238
+ *
239
+ * Use Cases:
240
+ * - User explicitly cancels/abandons session
241
+ * - Timeout scenarios where waiting is not acceptable
242
+ * - Need immediate cleanup and can't wait for server
243
+ *
244
+ * RECOMMENDED: Use stopRecording() for normal shutdown.
245
+ * Only use this when immediate disconnection is required.
246
+ */
247
+ stopAbnormally(): void;
248
+
249
+ /**
250
+ * Get the audio utterance ID for this session
251
+ * Available immediately after client construction.
252
+ * @returns UUID v4 string identifying this recognition session
253
+ */
254
+ getAudioUtteranceId(): string;
255
+
256
+ /**
257
+ * Get the current state of the client
258
+ * @returns Current ClientState value
259
+ */
260
+ getState(): ClientState;
261
+
262
+ /**
263
+ * Check if WebSocket connection is open
264
+ * @returns true if connected and ready to communicate
265
+ */
266
+ isConnected(): boolean;
267
+
268
+ /**
269
+ * Check if client is currently connecting
270
+ * @returns true if connection is in progress
271
+ */
272
+ isConnecting(): boolean;
273
+
274
+ /**
275
+ * Check if client is currently stopping
276
+ * @returns true if stopRecording() is in progress
277
+ */
278
+ isStopping(): boolean;
279
+
280
+ /**
281
+ * Check if transcription has finished
282
+ * @returns true if the transcription is complete
283
+ */
284
+ isTranscriptionFinished(): boolean;
285
+
286
+ /**
287
+ * Check if the audio buffer has overflowed
288
+ * @returns true if the ring buffer has wrapped around
289
+ */
290
+ isBufferOverflowing(): boolean;
291
+
292
+ /**
293
+ * Get client statistics
294
+ * @returns Statistics about audio transmission and buffering
295
+ */
296
+ getStats(): IRecognitionClientStats;
297
+
298
+ /**
299
+ * Get the WebSocket URL being used by this client
300
+ * Available immediately after client construction.
301
+ * @returns WebSocket URL string
302
+ */
303
+ getUrl(): string;
304
+ }
305
+
306
+ /**
307
+ * Client statistics interface
308
+ */
309
+ export interface IRecognitionClientStats {
310
+ /** Total audio bytes sent to server */
311
+ audioBytesSent: number;
312
+
313
+ /** Total number of audio chunks sent */
314
+ audioChunksSent: number;
315
+
316
+ /** Total number of audio chunks buffered */
317
+ audioChunksBuffered: number;
318
+
319
+ /** Number of times the ring buffer overflowed */
320
+ bufferOverflowCount: number;
321
+
322
+ /** Current number of chunks in buffer */
323
+ currentBufferedChunks: number;
324
+
325
+ /** Whether the ring buffer has wrapped (overwritten old data) */
326
+ hasWrapped: boolean;
327
+ }
328
+
329
+ /**
330
+ * Configuration for RealTimeTwoWayWebSocketRecognitionClient
331
+ * This extends IRecognitionClientConfig and is the main configuration interface
332
+ * for creating a new RealTimeTwoWayWebSocketRecognitionClient instance.
333
+ */
334
+ export interface RealTimeTwoWayWebSocketRecognitionClientConfig extends IRecognitionClientConfig {
335
+ // All fields are inherited from IRecognitionClientConfig
336
+ // This interface exists for backward compatibility and clarity
337
+ }
338
+