@volley/recognition-client-sdk 0.1.255 → 0.1.287

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 (38) hide show
  1. package/dist/browser.d.ts +10 -0
  2. package/dist/browser.d.ts.map +1 -0
  3. package/dist/config-builder.d.ts +129 -0
  4. package/dist/config-builder.d.ts.map +1 -0
  5. package/dist/errors.d.ts +41 -0
  6. package/dist/errors.d.ts.map +1 -0
  7. package/dist/factory.d.ts +36 -0
  8. package/dist/factory.d.ts.map +1 -0
  9. package/dist/index.d.ts +15 -1079
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +9760 -2109
  12. package/dist/index.js.map +7 -1
  13. package/dist/recog-client-sdk.browser.d.ts +10 -2
  14. package/dist/recog-client-sdk.browser.d.ts.map +1 -0
  15. package/dist/recog-client-sdk.browser.js +4597 -701
  16. package/dist/recog-client-sdk.browser.js.map +7 -1
  17. package/dist/recognition-client.d.ts +119 -0
  18. package/dist/recognition-client.d.ts.map +1 -0
  19. package/dist/recognition-client.types.d.ts +247 -0
  20. package/dist/recognition-client.types.d.ts.map +1 -0
  21. package/dist/simplified-vgf-recognition-client.d.ts +155 -0
  22. package/dist/simplified-vgf-recognition-client.d.ts.map +1 -0
  23. package/dist/utils/audio-ring-buffer.d.ts +69 -0
  24. package/dist/utils/audio-ring-buffer.d.ts.map +1 -0
  25. package/dist/utils/message-handler.d.ts +45 -0
  26. package/dist/utils/message-handler.d.ts.map +1 -0
  27. package/dist/utils/url-builder.d.ts +26 -0
  28. package/dist/utils/url-builder.d.ts.map +1 -0
  29. package/dist/vgf-recognition-mapper.d.ts +53 -0
  30. package/dist/vgf-recognition-mapper.d.ts.map +1 -0
  31. package/dist/vgf-recognition-state.d.ts +81 -0
  32. package/dist/vgf-recognition-state.d.ts.map +1 -0
  33. package/package.json +7 -8
  34. package/src/index.ts +4 -0
  35. package/src/recognition-client.spec.ts +19 -14
  36. package/src/recognition-client.ts +4 -0
  37. package/src/utils/url-builder.spec.ts +5 -3
  38. package/dist/browser-BZs4BL_w.d.ts +0 -1118
@@ -1,1118 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- /**
4
- * Provider types and enums for recognition services
5
- * NOTE_TO_AI: DO NOT CHANGE THIS UNLESS EXPLICITLY ASKED. Always ask before making any changes.
6
- */
7
- /**
8
- * Supported speech recognition providers
9
- */
10
- declare enum RecognitionProvider {
11
- ASSEMBLYAI = "assemblyai",
12
- DEEPGRAM = "deepgram",
13
- GOOGLE = "google",
14
- GEMINI_BATCH = "gemini-batch",
15
- OPENAI_BATCH = "openai-batch"
16
- }
17
- /**
18
- * ASR API type - distinguishes between streaming and file-based transcription APIs
19
- * - STREAMING: Real-time streaming APIs (Deepgram, AssemblyAI, Google)
20
- * - FILE_BASED: File upload/batch APIs (OpenAI Batch, Gemini Batch)
21
- */
22
- declare enum ASRApiType {
23
- STREAMING = "streaming",
24
- FILE_BASED = "file-based"
25
- }
26
- /**
27
- * Deepgram model names
28
- */
29
- declare enum DeepgramModel {
30
- NOVA_2 = "nova-2",
31
- NOVA_3 = "nova-3",
32
- FLUX_GENERAL_EN = "flux-general-en"
33
- }
34
- /**
35
- * Google Cloud Speech models
36
- * @see https://cloud.google.com/speech-to-text/docs/transcription-model
37
- */
38
- declare enum GoogleModel {
39
- LATEST_LONG = "latest_long",
40
- LATEST_SHORT = "latest_short",
41
- TELEPHONY = "telephony",
42
- TELEPHONY_SHORT = "telephony_short",
43
- MEDICAL_DICTATION = "medical_dictation",
44
- MEDICAL_CONVERSATION = "medical_conversation",
45
- DEFAULT = "default",
46
- COMMAND_AND_SEARCH = "command_and_search",
47
- PHONE_CALL = "phone_call",
48
- VIDEO = "video"
49
- }
50
- /**
51
- * Type alias for any model from any provider
52
- */
53
- type RecognitionModel = DeepgramModel | GoogleModel | string;
54
-
55
- /**
56
- * Audio encoding types
57
- */
58
- declare enum AudioEncoding {
59
- ENCODING_UNSPECIFIED = 0,
60
- LINEAR16 = 1,
61
- OGG_OPUS = 2,
62
- FLAC = 3,
63
- MULAW = 4,
64
- ALAW = 5
65
- }
66
- declare namespace AudioEncoding {
67
- /**
68
- * Convert numeric ID to AudioEncoding enum
69
- * @param id - Numeric encoding identifier (0-5)
70
- * @returns AudioEncoding enum value or undefined if invalid
71
- */
72
- function fromId(id: number): AudioEncoding | undefined;
73
- /**
74
- * Convert string name to AudioEncoding enum
75
- * @param nameStr - String name like "linear16", "LINEAR16", "ogg_opus", "OGG_OPUS", etc. (case insensitive)
76
- * @returns AudioEncoding enum value or undefined if invalid
77
- */
78
- function fromName(nameStr: string): AudioEncoding | undefined;
79
- /**
80
- * Convert AudioEncoding enum to numeric ID
81
- * @param encoding - AudioEncoding enum value
82
- * @returns Numeric ID (0-5)
83
- */
84
- function toId(encoding: AudioEncoding): number;
85
- /**
86
- * Convert AudioEncoding enum to string name
87
- * @param encoding - AudioEncoding enum value
88
- * @returns String name like "LINEAR16", "MULAW", etc.
89
- */
90
- function toName(encoding: AudioEncoding): string;
91
- /**
92
- * Check if a numeric ID is a valid encoding
93
- * @param id - Numeric identifier to validate
94
- * @returns true if valid encoding ID
95
- */
96
- function isIdValid(id: number): boolean;
97
- /**
98
- * Check if a string name is a valid encoding
99
- * @param nameStr - String name to validate
100
- * @returns true if valid encoding name
101
- */
102
- function isNameValid(nameStr: string): boolean;
103
- }
104
- /**
105
- * Common sample rates (in Hz)
106
- */
107
- declare enum SampleRate {
108
- RATE_8000 = 8000,
109
- RATE_16000 = 16000,
110
- RATE_22050 = 22050,
111
- RATE_24000 = 24000,
112
- RATE_32000 = 32000,
113
- RATE_44100 = 44100,
114
- RATE_48000 = 48000
115
- }
116
- declare namespace SampleRate {
117
- /**
118
- * Convert Hz value to SampleRate enum
119
- * @param hz - Sample rate in Hz (8000, 16000, etc.)
120
- * @returns SampleRate enum value or undefined if invalid
121
- */
122
- function fromHz(hz: number): SampleRate | undefined;
123
- /**
124
- * Convert string name to SampleRate enum
125
- * @param nameStr - String name like "rate_8000", "RATE_16000", etc. (case insensitive)
126
- * @returns SampleRate enum value or undefined if invalid
127
- */
128
- function fromName(nameStr: string): SampleRate | undefined;
129
- /**
130
- * Convert SampleRate enum to Hz value
131
- * @param rate - SampleRate enum value
132
- * @returns Hz value (8000, 16000, etc.)
133
- */
134
- function toHz(rate: SampleRate): number;
135
- /**
136
- * Convert SampleRate enum to string name
137
- * @param rate - SampleRate enum value
138
- * @returns String name like "RATE_8000", "RATE_16000", etc.
139
- */
140
- function toName(rate: SampleRate): string;
141
- /**
142
- * Check if a numeric Hz value is a valid sample rate
143
- * @param hz - Hz value to validate
144
- * @returns true if valid sample rate
145
- */
146
- function isHzValid(hz: number): boolean;
147
- /**
148
- * Check if a string name is a valid sample rate
149
- * @param nameStr - String name to validate
150
- * @returns true if valid sample rate name
151
- */
152
- function isNameValid(nameStr: string): boolean;
153
- }
154
- /**
155
- * Supported languages for recognition
156
- * Using BCP-47 language tags
157
- */
158
- declare enum Language {
159
- ENGLISH_US = "en-US",
160
- ENGLISH_GB = "en-GB",
161
- SPANISH_ES = "es-ES",
162
- SPANISH_MX = "es-MX",
163
- FRENCH_FR = "fr-FR",
164
- GERMAN_DE = "de-DE",
165
- ITALIAN_IT = "it-IT",
166
- PORTUGUESE_BR = "pt-BR",
167
- JAPANESE_JP = "ja-JP",
168
- KOREAN_KR = "ko-KR",
169
- CHINESE_CN = "zh-CN",
170
- CHINESE_TW = "zh-TW"
171
- }
172
-
173
- /**
174
- * Recognition Result Types V1
175
- * NOTE_TO_AI: DO NOT CHANGE THIS UNLESS EXPLICITLY ASKED. Always ask before making any changes.
176
- * Types and schemas for recognition results sent to SDK clients
177
- */
178
-
179
- /**
180
- * Message type discriminator for recognition results V1
181
- */
182
- declare enum RecognitionResultTypeV1 {
183
- TRANSCRIPTION = "Transcription",// Transcript message contains all in the history. result of STT(Speech to text)
184
- FUNCTION_CALL = "FunctionCall",// Not supported in P1.result of STF(Speedch to function call) Function call schema
185
- METADATA = "Metadata",// Metadata message contains all the timestamps, provider info, and ASR config
186
- ERROR = "Error",// Error message contains the error details
187
- CLIENT_CONTROL_MESSAGE = "ClientControlMessage"
188
- }
189
- /**
190
- * Transcription result V1 - contains transcript message
191
- * In the long run game side should not need to know it. In the short run it is send back to client.
192
- * NOTE_TO_AI: DO NOT CHANGE THIS UNLESS EXPLICITLY ASKED. Always ask before making any changes.
193
- */
194
- declare const TranscriptionResultSchemaV1: z.ZodObject<{
195
- type: z.ZodLiteral<RecognitionResultTypeV1.TRANSCRIPTION>;
196
- audioUtteranceId: z.ZodString;
197
- finalTranscript: z.ZodString;
198
- finalTranscriptConfidence: z.ZodOptional<z.ZodNumber>;
199
- pendingTranscript: z.ZodOptional<z.ZodString>;
200
- pendingTranscriptConfidence: z.ZodOptional<z.ZodNumber>;
201
- is_finished: z.ZodBoolean;
202
- voiceStart: z.ZodOptional<z.ZodNumber>;
203
- voiceDuration: z.ZodOptional<z.ZodNumber>;
204
- voiceEnd: z.ZodOptional<z.ZodNumber>;
205
- startTimestamp: z.ZodOptional<z.ZodNumber>;
206
- endTimestamp: z.ZodOptional<z.ZodNumber>;
207
- receivedAtMs: z.ZodOptional<z.ZodNumber>;
208
- accumulatedAudioTimeMs: z.ZodOptional<z.ZodNumber>;
209
- }, "strip", z.ZodTypeAny, {
210
- type: RecognitionResultTypeV1.TRANSCRIPTION;
211
- audioUtteranceId: string;
212
- finalTranscript: string;
213
- is_finished: boolean;
214
- finalTranscriptConfidence?: number | undefined;
215
- pendingTranscript?: string | undefined;
216
- pendingTranscriptConfidence?: number | undefined;
217
- voiceStart?: number | undefined;
218
- voiceDuration?: number | undefined;
219
- voiceEnd?: number | undefined;
220
- startTimestamp?: number | undefined;
221
- endTimestamp?: number | undefined;
222
- receivedAtMs?: number | undefined;
223
- accumulatedAudioTimeMs?: number | undefined;
224
- }, {
225
- type: RecognitionResultTypeV1.TRANSCRIPTION;
226
- audioUtteranceId: string;
227
- finalTranscript: string;
228
- is_finished: boolean;
229
- finalTranscriptConfidence?: number | undefined;
230
- pendingTranscript?: string | undefined;
231
- pendingTranscriptConfidence?: number | undefined;
232
- voiceStart?: number | undefined;
233
- voiceDuration?: number | undefined;
234
- voiceEnd?: number | undefined;
235
- startTimestamp?: number | undefined;
236
- endTimestamp?: number | undefined;
237
- receivedAtMs?: number | undefined;
238
- accumulatedAudioTimeMs?: number | undefined;
239
- }>;
240
- type TranscriptionResultV1 = z.infer<typeof TranscriptionResultSchemaV1>;
241
- /**
242
- * Function call result V1 - similar to LLM function call
243
- * In the long run game server should know it, rather than TV or client.
244
- */
245
- declare const FunctionCallResultSchemaV1: z.ZodObject<{
246
- type: z.ZodLiteral<RecognitionResultTypeV1.FUNCTION_CALL>;
247
- audioUtteranceId: z.ZodString;
248
- functionName: z.ZodString;
249
- functionArgJson: z.ZodString;
250
- }, "strip", z.ZodTypeAny, {
251
- type: RecognitionResultTypeV1.FUNCTION_CALL;
252
- audioUtteranceId: string;
253
- functionName: string;
254
- functionArgJson: string;
255
- }, {
256
- type: RecognitionResultTypeV1.FUNCTION_CALL;
257
- audioUtteranceId: string;
258
- functionName: string;
259
- functionArgJson: string;
260
- }>;
261
- type FunctionCallResultV1 = z.infer<typeof FunctionCallResultSchemaV1>;
262
- /**
263
- * Metadata result V1 - contains metadata, timing information, and ASR config
264
- * Sent when the provider connection closes to provide final timing metrics and config
265
- * In the long run game server should know it, rather than TV or client.
266
- */
267
- declare const MetadataResultSchemaV1: z.ZodObject<{
268
- type: z.ZodLiteral<RecognitionResultTypeV1.METADATA>;
269
- audioUtteranceId: z.ZodString;
270
- recordingStartMs: z.ZodOptional<z.ZodNumber>;
271
- recordingEndMs: z.ZodOptional<z.ZodNumber>;
272
- transcriptEndMs: z.ZodOptional<z.ZodNumber>;
273
- socketCloseAtMs: z.ZodOptional<z.ZodNumber>;
274
- duration: z.ZodOptional<z.ZodNumber>;
275
- volume: z.ZodOptional<z.ZodNumber>;
276
- accumulatedAudioTimeMs: z.ZodOptional<z.ZodNumber>;
277
- costInUSD: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
278
- apiType: z.ZodOptional<z.ZodNativeEnum<typeof ASRApiType>>;
279
- asrConfig: z.ZodOptional<z.ZodString>;
280
- rawAsrMetadata: z.ZodOptional<z.ZodString>;
281
- }, "strip", z.ZodTypeAny, {
282
- type: RecognitionResultTypeV1.METADATA;
283
- audioUtteranceId: string;
284
- accumulatedAudioTimeMs?: number | undefined;
285
- recordingStartMs?: number | undefined;
286
- recordingEndMs?: number | undefined;
287
- transcriptEndMs?: number | undefined;
288
- socketCloseAtMs?: number | undefined;
289
- duration?: number | undefined;
290
- volume?: number | undefined;
291
- costInUSD?: number | undefined;
292
- apiType?: ASRApiType | undefined;
293
- asrConfig?: string | undefined;
294
- rawAsrMetadata?: string | undefined;
295
- }, {
296
- type: RecognitionResultTypeV1.METADATA;
297
- audioUtteranceId: string;
298
- accumulatedAudioTimeMs?: number | undefined;
299
- recordingStartMs?: number | undefined;
300
- recordingEndMs?: number | undefined;
301
- transcriptEndMs?: number | undefined;
302
- socketCloseAtMs?: number | undefined;
303
- duration?: number | undefined;
304
- volume?: number | undefined;
305
- costInUSD?: number | undefined;
306
- apiType?: ASRApiType | undefined;
307
- asrConfig?: string | undefined;
308
- rawAsrMetadata?: string | undefined;
309
- }>;
310
- type MetadataResultV1 = z.infer<typeof MetadataResultSchemaV1>;
311
- /**
312
- * Error type enum V1 - categorizes different types of errors
313
- */
314
- declare enum ErrorTypeV1 {
315
- AUTHENTICATION_ERROR = "authentication_error",// Authentication/authorization failures
316
- VALIDATION_ERROR = "validation_error",// Invalid input or configuration
317
- PROVIDER_ERROR = "provider_error",// Error from ASR provider (Deepgram, Google, etc.) Unlikely to happen with fall
318
- TIMEOUT_ERROR = "timeout_error",// Request or operation timeout. Likely business logic did not handle timeout.
319
- QUOTA_EXCEEDED = "quota_exceeded",// Quota or rate limit exceeded. Unlikely to happen with fallbakcs
320
- CONNECTION_ERROR = "connection_error",// Connection establishment or network error
321
- UNKNOWN_ERROR = "unknown_error"
322
- }
323
- /**
324
- * Error result V1 - contains error message
325
- * In the long run game server should know it, rather than TV or client.
326
- */
327
- declare const ErrorResultSchemaV1: z.ZodObject<{
328
- type: z.ZodLiteral<RecognitionResultTypeV1.ERROR>;
329
- audioUtteranceId: z.ZodString;
330
- errorType: z.ZodOptional<z.ZodNativeEnum<typeof ErrorTypeV1>>;
331
- message: z.ZodOptional<z.ZodString>;
332
- code: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
333
- description: z.ZodOptional<z.ZodString>;
334
- }, "strip", z.ZodTypeAny, {
335
- type: RecognitionResultTypeV1.ERROR;
336
- audioUtteranceId: string;
337
- code?: string | number | undefined;
338
- message?: string | undefined;
339
- errorType?: ErrorTypeV1 | undefined;
340
- description?: string | undefined;
341
- }, {
342
- type: RecognitionResultTypeV1.ERROR;
343
- audioUtteranceId: string;
344
- code?: string | number | undefined;
345
- message?: string | undefined;
346
- errorType?: ErrorTypeV1 | undefined;
347
- description?: string | undefined;
348
- }>;
349
- type ErrorResultV1 = z.infer<typeof ErrorResultSchemaV1>;
350
-
351
- /**
352
- * Recognition Context Types V1
353
- * NOTE_TO_AI: DO NOT CHANGE THIS UNLESS EXPLICITLY ASKED. Always ask before making any changes.
354
- * Types and schemas for recognition context data
355
- */
356
-
357
- /**
358
- * Message type discriminator for recognition context V1
359
- */
360
- declare enum RecognitionContextTypeV1 {
361
- GAME_CONTEXT = "GameContext",
362
- CONTROL_SIGNAL = "ControlSignal",
363
- ASR_REQUEST = "ASRRequest"
364
- }
365
- /**
366
- * Control signal types for recognition V1
367
- */
368
- declare enum ControlSignalTypeV1 {
369
- START_RECORDING = "start_recording",
370
- STOP_RECORDING = "stop_recording"
371
- }
372
- /**
373
- * Game context V1 - contains game state information
374
- */
375
- declare const GameContextSchemaV1: z.ZodObject<{
376
- type: z.ZodLiteral<RecognitionContextTypeV1.GAME_CONTEXT>;
377
- gameId: z.ZodString;
378
- gamePhase: z.ZodString;
379
- promptSTT: z.ZodOptional<z.ZodString>;
380
- promptSTF: z.ZodOptional<z.ZodString>;
381
- promptTTF: z.ZodOptional<z.ZodString>;
382
- slotMap: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString, "many">>>;
383
- }, "strip", z.ZodTypeAny, {
384
- type: RecognitionContextTypeV1.GAME_CONTEXT;
385
- gameId: string;
386
- gamePhase: string;
387
- promptSTT?: string | undefined;
388
- promptSTF?: string | undefined;
389
- promptTTF?: string | undefined;
390
- slotMap?: Record<string, string[]> | undefined;
391
- }, {
392
- type: RecognitionContextTypeV1.GAME_CONTEXT;
393
- gameId: string;
394
- gamePhase: string;
395
- promptSTT?: string | undefined;
396
- promptSTF?: string | undefined;
397
- promptTTF?: string | undefined;
398
- slotMap?: Record<string, string[]> | undefined;
399
- }>;
400
- type GameContextV1 = z.infer<typeof GameContextSchemaV1>;
401
- /**
402
- * ASR Request V1 - contains complete ASR setup information
403
- * Sent once at connection start to configure the session
404
- */
405
- declare const ASRRequestSchemaV1: z.ZodObject<{
406
- type: z.ZodLiteral<RecognitionContextTypeV1.ASR_REQUEST>;
407
- audioUtteranceId: z.ZodOptional<z.ZodString>;
408
- provider: z.ZodString;
409
- model: z.ZodOptional<z.ZodString>;
410
- language: z.ZodString;
411
- sampleRate: z.ZodNumber;
412
- encoding: z.ZodNumber;
413
- interimResults: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
414
- useContext: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
415
- debugCommand: z.ZodOptional<z.ZodObject<{
416
- enableDebugLog: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
417
- enableAudioStorage: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
418
- enableSongQuizSessionIdCheck: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
419
- enablePilotModels: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
420
- }, "strip", z.ZodTypeAny, {
421
- enableDebugLog: boolean;
422
- enableAudioStorage: boolean;
423
- enableSongQuizSessionIdCheck: boolean;
424
- enablePilotModels: boolean;
425
- }, {
426
- enableDebugLog?: boolean | undefined;
427
- enableAudioStorage?: boolean | undefined;
428
- enableSongQuizSessionIdCheck?: boolean | undefined;
429
- enablePilotModels?: boolean | undefined;
430
- }>>;
431
- }, "strip", z.ZodTypeAny, {
432
- provider: string;
433
- language: string;
434
- sampleRate: number;
435
- encoding: number;
436
- interimResults: boolean;
437
- useContext: boolean;
438
- type: RecognitionContextTypeV1.ASR_REQUEST;
439
- model?: string | undefined;
440
- audioUtteranceId?: string | undefined;
441
- debugCommand?: {
442
- enableDebugLog: boolean;
443
- enableAudioStorage: boolean;
444
- enableSongQuizSessionIdCheck: boolean;
445
- enablePilotModels: boolean;
446
- } | undefined;
447
- }, {
448
- provider: string;
449
- language: string;
450
- sampleRate: number;
451
- encoding: number;
452
- type: RecognitionContextTypeV1.ASR_REQUEST;
453
- model?: string | undefined;
454
- interimResults?: boolean | undefined;
455
- useContext?: boolean | undefined;
456
- audioUtteranceId?: string | undefined;
457
- debugCommand?: {
458
- enableDebugLog?: boolean | undefined;
459
- enableAudioStorage?: boolean | undefined;
460
- enableSongQuizSessionIdCheck?: boolean | undefined;
461
- enablePilotModels?: boolean | undefined;
462
- } | undefined;
463
- }>;
464
- type ASRRequestV1 = z.infer<typeof ASRRequestSchemaV1>;
465
-
466
- /**
467
- * Unified ASR Request Configuration
468
- *
469
- * Provider-agnostic configuration for ASR (Automatic Speech Recognition) requests.
470
- * This interface provides a consistent API for clients regardless of the underlying provider.
471
- *
472
- * All fields use library-defined enums for type safety and consistency.
473
- * Provider-specific mappers will convert these to provider-native formats.
474
- */
475
-
476
- /**
477
- * Unified ASR request configuration
478
- *
479
- * This configuration is used by:
480
- * - Client SDKs to specify recognition parameters
481
- * - Demo applications for user input
482
- * - Service layer to configure provider sessions
483
- *
484
- * Core fields only - all provider-specific options go in providerOptions
485
- *
486
- * @example
487
- * ```typescript
488
- * const config: ASRRequestConfig = {
489
- * provider: RecognitionProvider.GOOGLE,
490
- * model: GoogleModel.LATEST_LONG,
491
- * language: Language.ENGLISH_US,
492
- * sampleRate: SampleRate.RATE_16000, // or just 16000
493
- * encoding: AudioEncoding.LINEAR16,
494
- * providerOptions: {
495
- * google: {
496
- * enableAutomaticPunctuation: true,
497
- * interimResults: true,
498
- * singleUtterance: false
499
- * }
500
- * }
501
- * };
502
- * ```
503
- */
504
- interface ASRRequestConfig {
505
- /**
506
- * The ASR provider to use
507
- * Must be one of the supported providers in RecognitionProvider enum
508
- */
509
- provider: RecognitionProvider | string;
510
- /**
511
- * Optional model specification for the provider
512
- * Can be provider-specific model enum or string
513
- * If not specified, provider's default model will be used
514
- */
515
- model?: RecognitionModel;
516
- /**
517
- * Language/locale for recognition
518
- * Use Language enum for common languages
519
- * Can also accept BCP-47 language tags as strings
520
- */
521
- language: Language | string;
522
- /**
523
- * Audio sample rate in Hz
524
- * Prefer using SampleRate enum values for standard rates
525
- * Can also accept numeric Hz values (e.g., 16000)
526
- */
527
- sampleRate: SampleRate | number;
528
- /**
529
- * Audio encoding format
530
- * Must match the actual audio data being sent
531
- * Use AudioEncoding enum for standard formats
532
- */
533
- encoding: AudioEncoding | string;
534
- /**
535
- * Enable interim (partial) results during recognition
536
- * When true, receive real-time updates before finalization
537
- * When false, only receive final results
538
- * Default: false
539
- */
540
- interimResults?: boolean;
541
- /**
542
- * Require GameContext before starting recognition such as song titles
543
- * When true, server waits for GameContext message before processing audio
544
- * When false, recognition starts immediately
545
- * Default: false
546
- */
547
- useContext?: boolean;
548
- /**
549
- * Additional provider-specific options
550
- *
551
- * Common options per provider:
552
- * - Deepgram: punctuate, smart_format, diarize, utterances
553
- * - Google: enableAutomaticPunctuation, singleUtterance, enableWordTimeOffsets
554
- * - AssemblyAI: formatTurns, filter_profanity, word_boost
555
- *
556
- * Note: interimResults is now a top-level field, but can still be overridden per provider
557
- *
558
- * @example
559
- * ```typescript
560
- * providerOptions: {
561
- * google: {
562
- * enableAutomaticPunctuation: true,
563
- * singleUtterance: false,
564
- * enableWordTimeOffsets: false
565
- * }
566
- * }
567
- * ```
568
- */
569
- providerOptions?: Record<string, any>;
570
- /**
571
- * Optional fallback ASR configurations
572
- *
573
- * List of alternative ASR configurations to use if the primary fails.
574
- * Each fallback config is a complete ASRRequestConfig that will be tried
575
- * in order until one succeeds.
576
- *
577
- * @example
578
- * ```typescript
579
- * fallbackModels: [
580
- * {
581
- * provider: RecognitionProvider.DEEPGRAM,
582
- * model: DeepgramModel.NOVA_2,
583
- * language: Language.ENGLISH_US,
584
- * sampleRate: 16000,
585
- * encoding: AudioEncoding.LINEAR16
586
- * },
587
- * {
588
- * provider: RecognitionProvider.GOOGLE,
589
- * model: GoogleModel.LATEST_SHORT,
590
- * language: Language.ENGLISH_US,
591
- * sampleRate: 16000,
592
- * encoding: AudioEncoding.LINEAR16
593
- * }
594
- * ]
595
- * ```
596
- */
597
- fallbackModels?: ASRRequestConfig[];
598
- }
599
-
600
- /**
601
- * Standard stage/environment constants used across all services
602
- */
603
- declare const STAGES: {
604
- readonly LOCAL: "local";
605
- readonly DEV: "dev";
606
- readonly STAGING: "staging";
607
- readonly PRODUCTION: "production";
608
- };
609
- type Stage = typeof STAGES[keyof typeof STAGES];
610
-
611
- /**
612
- * Generic WebSocket protocol types and utilities
613
- * Supports flexible versioning and message types
614
- * Used by both client and server implementations
615
- */
616
-
617
- /**
618
- * Base message structure - completely flexible
619
- * @template V - Version type (number, string, etc.)
620
- */
621
- interface Message<V = number> {
622
- v: V;
623
- type: string;
624
- data?: unknown;
625
- }
626
- /**
627
- * Version serializer interface
628
- * Converts between version type V and byte representation
629
- */
630
- interface VersionSerializer<V> {
631
- serialize: (v: V) => number;
632
- deserialize: (byte: number) => V;
633
- }
634
-
635
- /**
636
- * WebSocketAudioClient - Abstract base class for WebSocket clients
637
- * Sends audio and control messages, receives responses from server
638
- *
639
- * Features:
640
- * - Generic version type support (number, string, etc.)
641
- * - Type-safe upward/downward message data
642
- * - Client-side backpressure monitoring
643
- * - Abstract hooks for application-specific logic
644
- * - Format-agnostic audio protocol (supports any encoding)
645
- */
646
-
647
- type ClientConfig = {
648
- url: string;
649
- highWM?: number;
650
- lowWM?: number;
651
- };
652
- /**
653
- * WebSocketAudioClient - Abstract base class for WebSocket clients
654
- * that send audio frames and JSON messages
655
- *
656
- * @template V - Version type (number, string, object, etc.)
657
- * @template TUpward - Type of upward message data (Client -> Server)
658
- * @template TDownward - Type of downward message data (Server -> Client)
659
- *
660
- * @example
661
- * ```typescript
662
- * class MyClient extends WebSocketAudioClient<number, MyUpMsg, MyDownMsg> {
663
- * protected onConnected() {
664
- * console.log('Connected!');
665
- * }
666
- *
667
- * protected onMessage(msg) {
668
- * console.log('Received:', msg.type, msg.data);
669
- * }
670
- *
671
- * protected onDisconnected(code, reason) {
672
- * console.log('Disconnected:', code, reason);
673
- * }
674
- *
675
- * protected onError(error) {
676
- * console.error('Error:', error);
677
- * }
678
- * }
679
- *
680
- * const client = new MyClient({ url: 'ws://localhost:8080' });
681
- * client.connect();
682
- * client.sendMessage(1, 'configure', { language: 'en' });
683
- * client.sendAudio(audioData);
684
- * ```
685
- */
686
- declare abstract class WebSocketAudioClient<V = number, // Version type (default: number)
687
- TUpward = unknown, // Upward message data type
688
- TDownward = unknown> {
689
- private cfg;
690
- protected versionSerializer: VersionSerializer<V>;
691
- private ws;
692
- private seq;
693
- private HWM;
694
- private LWM;
695
- constructor(cfg: ClientConfig, versionSerializer?: VersionSerializer<V>);
696
- /**
697
- * Hook: Called when WebSocket connection is established
698
- */
699
- protected abstract onConnected(): void;
700
- /**
701
- * Hook: Called when WebSocket connection closes
702
- * @param code - Close code (see WebSocketCloseCode enum)
703
- * @param reason - Human-readable close reason
704
- */
705
- protected abstract onDisconnected(code: number, reason: string): void;
706
- /**
707
- * Hook: Called when WebSocket error occurs
708
- */
709
- protected abstract onError(error: Event): void;
710
- /**
711
- * Hook: Called when downward message arrives from server
712
- * Override this to handle messages (optional - default does nothing)
713
- */
714
- protected onMessage(msg: Message<V> & {
715
- data: TDownward;
716
- }): void;
717
- connect(): void;
718
- /**
719
- * Send JSON message to server
720
- * @param version - Message version
721
- * @param type - Message type (developer defined)
722
- * @param data - Message payload (typed)
723
- */
724
- sendMessage(version: V, type: string, data: TUpward): void;
725
- /**
726
- * Send audio frame with specified encoding and sample rate
727
- * @param audioData - Audio data (any format: Int16Array, Uint8Array, ArrayBuffer, etc.)
728
- * @param version - Audio frame version
729
- * @param encodingId - Audio encoding ID (0-5, e.g., AudioEncoding.LINEAR16)
730
- * @param sampleRate - Sample rate in Hz (e.g., 16000)
731
- */
732
- sendAudio(audioData: ArrayBuffer | ArrayBufferView, version: V, encodingId: number, sampleRate: number): void;
733
- /**
734
- * Get current WebSocket buffer size
735
- */
736
- getBufferedAmount(): number;
737
- /**
738
- * Check if local buffer is backpressured
739
- */
740
- isLocalBackpressured(): boolean;
741
- /**
742
- * Check if ready to send audio
743
- * Verifies: connection open, no local buffer pressure
744
- */
745
- canSend(): boolean;
746
- /**
747
- * Check if connection is open
748
- */
749
- isOpen(): boolean;
750
- /**
751
- * Get current connection state
752
- */
753
- getReadyState(): number;
754
- }
755
-
756
- /**
757
- * Recognition Client Types
758
- *
759
- * Type definitions and interfaces for the recognition client SDK.
760
- * These interfaces enable dependency injection, testing, and alternative implementations.
761
- */
762
-
763
- /**
764
- * Client connection state enum
765
- * Represents the various states a recognition client can be in during its lifecycle
766
- */
767
- declare enum ClientState {
768
- /** Initial state, no connection established */
769
- INITIAL = "initial",
770
- /** Actively establishing WebSocket connection */
771
- CONNECTING = "connecting",
772
- /** WebSocket connected but waiting for server ready signal */
773
- CONNECTED = "connected",
774
- /** Server ready, can send audio */
775
- READY = "ready",
776
- /** Sent stop signal, waiting for final transcript */
777
- STOPPING = "stopping",
778
- /** Connection closed normally after stop */
779
- STOPPED = "stopped",
780
- /** Connection failed or lost unexpectedly */
781
- FAILED = "failed"
782
- }
783
- /**
784
- * Callback URL configuration with message type filtering
785
- */
786
- interface RecognitionCallbackUrl {
787
- /** The callback URL endpoint */
788
- url: string;
789
- /** Array of message types to send to this URL. If empty/undefined, all types are sent */
790
- messageTypes?: Array<string | number>;
791
- }
792
- interface IRecognitionClientConfig {
793
- /**
794
- * WebSocket endpoint URL (optional)
795
- * Either `url` or `stage` must be provided.
796
- * If both are provided, `url` takes precedence.
797
- *
798
- * Example with explicit URL:
799
- * ```typescript
800
- * { url: 'wss://custom-endpoint.example.com/ws/v1/recognize' }
801
- * ```
802
- */
803
- url?: string;
804
- /**
805
- * Stage for recognition service (recommended)
806
- * Either `url` or `stage` must be provided.
807
- * If both are provided, `url` takes precedence.
808
- * Defaults to production if neither is provided.
809
- *
810
- * Example with STAGES enum (recommended):
811
- * ```typescript
812
- * import { STAGES } from '@recog/shared-types';
813
- * { stage: STAGES.STAGING }
814
- * ```
815
- *
816
- * String values also accepted:
817
- * ```typescript
818
- * { stage: 'staging' } // STAGES.LOCAL | STAGES.DEV | STAGES.STAGING | STAGES.PRODUCTION
819
- * ```
820
- */
821
- stage?: Stage | string;
822
- /** ASR configuration (provider, model, language, etc.) - optional */
823
- asrRequestConfig?: ASRRequestConfig;
824
- /** Game context for improved recognition accuracy */
825
- gameContext?: GameContextV1;
826
- /** Audio utterance ID (optional) - if not provided, a UUID v4 will be generated */
827
- audioUtteranceId?: string;
828
- /** Callback URLs for server-side notifications with optional message type filtering (optional)
829
- * Game side only need to use it if another service need to be notified about the transcription results.
830
- */
831
- callbackUrls?: RecognitionCallbackUrl[];
832
- /** User identification (optional) */
833
- userId?: string;
834
- /** Game session identification (optional). called 'sessionId' in Platform and most games. */
835
- gameSessionId?: string;
836
- /** Device identification (optional) */
837
- deviceId?: string;
838
- /** Account identification (optional) */
839
- accountId?: string;
840
- /** Question answer identifier for tracking Q&A sessions (optional and tracking purpose only) */
841
- questionAnswerId?: string;
842
- /** Platform for audio recording device (optional, e.g., 'ios', 'android', 'web', 'unity') */
843
- platform?: string;
844
- /** Callback when transcript is received */
845
- onTranscript?: (result: TranscriptionResultV1) => void;
846
- /**
847
- * Callback when function call is received
848
- * Note: Not supported in 2025. P2 feature for future speech-to-function-call capability.
849
- */
850
- onFunctionCall?: (result: FunctionCallResultV1) => void;
851
- /** Callback when metadata is received. Only once after transcription is complete.*/
852
- onMetadata?: (metadata: MetadataResultV1) => void;
853
- /** Callback when error occurs */
854
- onError?: (error: ErrorResultV1) => void;
855
- /** Callback when connected to WebSocket */
856
- onConnected?: () => void;
857
- /**
858
- * Callback when WebSocket disconnects
859
- * @param code - WebSocket close code (1000 = normal, 1006 = abnormal, etc.)
860
- * @param reason - Close reason string
861
- */
862
- onDisconnected?: (code: number, reason: string) => void;
863
- /** High water mark for backpressure control (bytes) */
864
- highWaterMark?: number;
865
- /** Low water mark for backpressure control (bytes) */
866
- lowWaterMark?: number;
867
- /** Maximum buffer duration in seconds (default: 60s) */
868
- maxBufferDurationSec?: number;
869
- /** Expected chunks per second for ring buffer sizing (default: 100) */
870
- chunksPerSecond?: number;
871
- /**
872
- * Connection retry configuration (optional)
873
- * Only applies to initial connection establishment, not mid-stream interruptions.
874
- *
875
- * Default: { maxAttempts: 4, delayMs: 200 } (try once, retry 3 times = 4 total attempts)
876
- *
877
- * Timing: Attempt 1 → FAIL → wait 200ms → Attempt 2 → FAIL → wait 200ms → Attempt 3 → FAIL → wait 200ms → Attempt 4
878
- *
879
- * Example:
880
- * ```typescript
881
- * {
882
- * connectionRetry: {
883
- * maxAttempts: 2, // Try connecting up to 2 times (1 retry)
884
- * delayMs: 500 // Wait 500ms between attempts
885
- * }
886
- * }
887
- * ```
888
- */
889
- connectionRetry?: {
890
- /** Maximum number of connection attempts (default: 4, min: 1, max: 5) */
891
- maxAttempts?: number;
892
- /** Delay in milliseconds between retry attempts (default: 200ms) */
893
- delayMs?: number;
894
- };
895
- /**
896
- * Optional logger function for debugging
897
- * If not provided, no logging will occur
898
- * @param level - Log level: 'debug', 'info', 'warn', 'error'
899
- * @param message - Log message
900
- * @param data - Optional additional data
901
- */
902
- logger?: (level: 'debug' | 'info' | 'warn' | 'error', message: string, data?: any) => void;
903
- }
904
- /**
905
- * Recognition Client Interface
906
- *
907
- * Main interface for real-time speech recognition clients.
908
- * Provides methods for connection management, audio streaming, and session control.
909
- */
910
- interface IRecognitionClient {
911
- /**
912
- * Connect to the WebSocket endpoint
913
- * @returns Promise that resolves when connected
914
- * @throws Error if connection fails or times out
915
- */
916
- connect(): Promise<void>;
917
- /**
918
- * Send audio data to the recognition service
919
- * Audio is buffered locally and sent when connection is ready.
920
- * @param audioData - PCM audio data as ArrayBuffer, typed array view, or Blob
921
- */
922
- sendAudio(audioData: ArrayBuffer | ArrayBufferView | Blob): void;
923
- /**
924
- * Stop recording and wait for final transcript
925
- * The server will close the connection after sending the final transcript.
926
- * @returns Promise that resolves when final transcript is received
927
- */
928
- stopRecording(): Promise<void>;
929
- /**
930
- * Get the audio utterance ID for this session
931
- * Available immediately after client construction.
932
- * @returns UUID v4 string identifying this recognition session
933
- */
934
- getAudioUtteranceId(): string;
935
- /**
936
- * Get the current state of the client
937
- * @returns Current ClientState value
938
- */
939
- getState(): ClientState;
940
- /**
941
- * Check if WebSocket connection is open
942
- * @returns true if connected and ready to communicate
943
- */
944
- isConnected(): boolean;
945
- /**
946
- * Check if client is currently connecting
947
- * @returns true if connection is in progress
948
- */
949
- isConnecting(): boolean;
950
- /**
951
- * Check if client is currently stopping
952
- * @returns true if stopRecording() is in progress
953
- */
954
- isStopping(): boolean;
955
- /**
956
- * Check if transcription has finished
957
- * @returns true if the transcription is complete
958
- */
959
- isTranscriptionFinished(): boolean;
960
- /**
961
- * Check if the audio buffer has overflowed
962
- * @returns true if the ring buffer has wrapped around
963
- */
964
- isBufferOverflowing(): boolean;
965
- /**
966
- * Get client statistics
967
- * @returns Statistics about audio transmission and buffering
968
- */
969
- getStats(): IRecognitionClientStats;
970
- /**
971
- * Get the WebSocket URL being used by this client
972
- * Available immediately after client construction.
973
- * @returns WebSocket URL string
974
- */
975
- getUrl(): string;
976
- }
977
- /**
978
- * Client statistics interface
979
- */
980
- interface IRecognitionClientStats {
981
- /** Total audio bytes sent to server */
982
- audioBytesSent: number;
983
- /** Total number of audio chunks sent */
984
- audioChunksSent: number;
985
- /** Total number of audio chunks buffered */
986
- audioChunksBuffered: number;
987
- /** Number of times the ring buffer overflowed */
988
- bufferOverflowCount: number;
989
- /** Current number of chunks in buffer */
990
- currentBufferedChunks: number;
991
- /** Whether the ring buffer has wrapped (overwritten old data) */
992
- hasWrapped: boolean;
993
- }
994
- /**
995
- * Configuration for RealTimeTwoWayWebSocketRecognitionClient
996
- * This extends IRecognitionClientConfig and is the main configuration interface
997
- * for creating a new RealTimeTwoWayWebSocketRecognitionClient instance.
998
- */
999
- interface RealTimeTwoWayWebSocketRecognitionClientConfig extends IRecognitionClientConfig {
1000
- }
1001
-
1002
- /**
1003
- * RealTimeTwoWayWebSocketRecognitionClient - Clean, compact SDK for real-time speech recognition
1004
- *
1005
- * Features:
1006
- * - Ring buffer-based audio storage with fixed memory footprint
1007
- * - Automatic buffering when disconnected, immediate send when connected
1008
- * - Buffer persists after flush (for future retry/reconnection scenarios)
1009
- * - Built on WebSocketAudioClient for robust protocol handling
1010
- * - Simple API: connect() → sendAudio() → stopRecording()
1011
- * - Type-safe message handling with callbacks
1012
- * - Automatic backpressure management
1013
- * - Overflow detection with buffer state tracking
1014
- *
1015
- * Example:
1016
- * ```typescript
1017
- * const client = new RealTimeTwoWayWebSocketRecognitionClient({
1018
- * url: 'ws://localhost:3101/ws/v1/recognize',
1019
- * onTranscript: (result) => console.log(result.finalTranscript),
1020
- * onError: (error) => console.error(error),
1021
- * maxBufferDurationSec: 60 // Ring buffer for 60 seconds
1022
- * });
1023
- *
1024
- * await client.connect();
1025
- *
1026
- * // Send audio chunks - always stored in ring buffer, sent if connected
1027
- * micStream.on('data', (chunk) => client.sendAudio(chunk));
1028
- *
1029
- * // Signal end of audio and wait for final results
1030
- * await client.stopRecording();
1031
- *
1032
- * // Server will close connection after sending finals
1033
- * // No manual cleanup needed - browser handles it
1034
- * ```
1035
- */
1036
-
1037
- /**
1038
- * Check if a WebSocket close code indicates normal closure
1039
- * @param code - WebSocket close code
1040
- * @returns true if the disconnection was normal/expected, false if it was an error
1041
- */
1042
- declare function isNormalDisconnection(code: number): boolean;
1043
- /**
1044
- * Re-export TranscriptionResultV1 as TranscriptionResult for backward compatibility
1045
- */
1046
- type TranscriptionResult = TranscriptionResultV1;
1047
-
1048
- /**
1049
- * RealTimeTwoWayWebSocketRecognitionClient - SDK-level client for real-time speech recognition
1050
- *
1051
- * Implements IRecognitionClient interface for dependency injection and testing.
1052
- * Extends WebSocketAudioClient with local audio buffering and simple callback-based API.
1053
- */
1054
- declare class RealTimeTwoWayWebSocketRecognitionClient extends WebSocketAudioClient<number, any, any> implements IRecognitionClient {
1055
- private static readonly PROTOCOL_VERSION;
1056
- private config;
1057
- private audioBuffer;
1058
- private messageHandler;
1059
- private state;
1060
- private connectionPromise;
1061
- private isDebugLogEnabled;
1062
- private audioBytesSent;
1063
- private audioChunksSent;
1064
- private audioStatsLogInterval;
1065
- private lastAudioStatsLog;
1066
- constructor(config: RealTimeTwoWayWebSocketRecognitionClientConfig);
1067
- /**
1068
- * Internal logging helper - only logs if a logger was provided in config
1069
- * Debug logs are additionally gated by isDebugLogEnabled flag
1070
- * @param level - Log level: debug, info, warn, or error
1071
- * @param message - Message to log
1072
- * @param data - Optional additional data to log
1073
- */
1074
- private log;
1075
- /**
1076
- * Clean up internal resources to free memory
1077
- * Called when connection closes (normally or abnormally)
1078
- */
1079
- private cleanup;
1080
- connect(): Promise<void>;
1081
- /**
1082
- * Attempt to connect with retry logic
1083
- * Only retries on initial connection establishment, not mid-stream interruptions
1084
- */
1085
- private connectWithRetry;
1086
- sendAudio(audioData: ArrayBuffer | ArrayBufferView | Blob): void;
1087
- private sendAudioInternal;
1088
- stopRecording(): Promise<void>;
1089
- getAudioUtteranceId(): string;
1090
- getUrl(): string;
1091
- getState(): ClientState;
1092
- isConnected(): boolean;
1093
- isConnecting(): boolean;
1094
- isStopping(): boolean;
1095
- isTranscriptionFinished(): boolean;
1096
- isBufferOverflowing(): boolean;
1097
- getStats(): IRecognitionClientStats;
1098
- protected onConnected(): void;
1099
- protected onDisconnected(code: number, reason: string): void;
1100
- protected onError(error: Event): void;
1101
- protected onMessage(msg: {
1102
- v: number;
1103
- type: string;
1104
- data: any;
1105
- }): void;
1106
- /**
1107
- * Handle control messages from server
1108
- * @param msg - Control message containing server actions
1109
- */
1110
- private handleControlMessage;
1111
- /**
1112
- * Send audio immediately to the server (without buffering)
1113
- * @param audioData - Audio data to send
1114
- */
1115
- private sendAudioNow;
1116
- }
1117
-
1118
- export { type ASRRequestConfig as A, ClientState as C, DeepgramModel as D, ErrorTypeV1 as E, type FunctionCallResultV1 as F, type GameContextV1 as G, type IRecognitionClient as I, Language as L, type MetadataResultV1 as M, RecognitionProvider as R, type Stage as S, type TranscriptionResultV1 as T, type RecognitionCallbackUrl as a, type ErrorResultV1 as b, type RealTimeTwoWayWebSocketRecognitionClientConfig as c, type IRecognitionClientConfig as d, RealTimeTwoWayWebSocketRecognitionClient as e, type TranscriptionResult as f, type IRecognitionClientStats as g, AudioEncoding as h, isNormalDisconnection as i, RecognitionContextTypeV1 as j, ControlSignalTypeV1 as k, RecognitionResultTypeV1 as l, type ASRRequestV1 as m, GoogleModel as n, SampleRate as o, STAGES as p };