@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.
- package/README.md +344 -0
- package/dist/browser.bundled.d.ts +1280 -0
- package/dist/browser.d.ts +10 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/config-builder.d.ts +134 -0
- package/dist/config-builder.d.ts.map +1 -0
- package/dist/errors.d.ts +41 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/factory.d.ts +36 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/index.bundled.d.ts +2572 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10199 -0
- package/dist/index.js.map +7 -0
- package/dist/recog-client-sdk.browser.d.ts +10 -0
- package/dist/recog-client-sdk.browser.d.ts.map +1 -0
- package/dist/recog-client-sdk.browser.js +5746 -0
- package/dist/recog-client-sdk.browser.js.map +7 -0
- package/dist/recognition-client.d.ts +128 -0
- package/dist/recognition-client.d.ts.map +1 -0
- package/dist/recognition-client.types.d.ts +271 -0
- package/dist/recognition-client.types.d.ts.map +1 -0
- package/dist/simplified-vgf-recognition-client.d.ts +178 -0
- package/dist/simplified-vgf-recognition-client.d.ts.map +1 -0
- package/dist/utils/audio-ring-buffer.d.ts +69 -0
- package/dist/utils/audio-ring-buffer.d.ts.map +1 -0
- package/dist/utils/message-handler.d.ts +45 -0
- package/dist/utils/message-handler.d.ts.map +1 -0
- package/dist/utils/url-builder.d.ts +28 -0
- package/dist/utils/url-builder.d.ts.map +1 -0
- package/dist/vgf-recognition-mapper.d.ts +66 -0
- package/dist/vgf-recognition-mapper.d.ts.map +1 -0
- package/dist/vgf-recognition-state.d.ts +91 -0
- package/dist/vgf-recognition-state.d.ts.map +1 -0
- package/package.json +74 -0
- package/src/browser.ts +24 -0
- package/src/config-builder.spec.ts +265 -0
- package/src/config-builder.ts +240 -0
- package/src/errors.ts +84 -0
- package/src/factory.spec.ts +215 -0
- package/src/factory.ts +47 -0
- package/src/index.ts +127 -0
- package/src/recognition-client.spec.ts +889 -0
- package/src/recognition-client.ts +844 -0
- package/src/recognition-client.types.ts +338 -0
- package/src/simplified-vgf-recognition-client.integration.spec.ts +718 -0
- package/src/simplified-vgf-recognition-client.spec.ts +1525 -0
- package/src/simplified-vgf-recognition-client.ts +524 -0
- package/src/utils/audio-ring-buffer.spec.ts +335 -0
- package/src/utils/audio-ring-buffer.ts +170 -0
- package/src/utils/message-handler.spec.ts +311 -0
- package/src/utils/message-handler.ts +131 -0
- package/src/utils/url-builder.spec.ts +252 -0
- package/src/utils/url-builder.ts +92 -0
- package/src/vgf-recognition-mapper.spec.ts +78 -0
- package/src/vgf-recognition-mapper.ts +232 -0
- package/src/vgf-recognition-state.ts +102 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RealTimeTwoWayWebSocketRecognitionClient - Clean, compact SDK for real-time speech recognition
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Ring buffer-based audio storage with fixed memory footprint
|
|
6
|
+
* - Automatic buffering when disconnected, immediate send when connected
|
|
7
|
+
* - Buffer persists after flush (for future retry/reconnection scenarios)
|
|
8
|
+
* - Built on WebSocketAudioClient for robust protocol handling
|
|
9
|
+
* - Simple API: connect() → sendAudio() → stopRecording()
|
|
10
|
+
* - Type-safe message handling with callbacks
|
|
11
|
+
* - Automatic backpressure management
|
|
12
|
+
* - Overflow detection with buffer state tracking
|
|
13
|
+
*
|
|
14
|
+
* Example:
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const client = new RealTimeTwoWayWebSocketRecognitionClient({
|
|
17
|
+
* url: 'ws://localhost:3101/ws/v1/recognize',
|
|
18
|
+
* onTranscript: (result) => console.log(result.finalTranscript),
|
|
19
|
+
* onError: (error) => console.error(error),
|
|
20
|
+
* maxBufferDurationSec: 60 // Ring buffer for 60 seconds
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await client.connect();
|
|
24
|
+
*
|
|
25
|
+
* // Send audio chunks - always stored in ring buffer, sent if connected
|
|
26
|
+
* micStream.on('data', (chunk) => client.sendAudio(chunk));
|
|
27
|
+
*
|
|
28
|
+
* // Signal end of audio and wait for final results
|
|
29
|
+
* await client.stopRecording();
|
|
30
|
+
*
|
|
31
|
+
* // Server will close connection after sending finals
|
|
32
|
+
* // No manual cleanup needed - browser handles it
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
import { WebSocketAudioClient } from '@recog/websocket';
|
|
36
|
+
import { type TranscriptionResultV1 } from '@recog/shared-types';
|
|
37
|
+
import { ClientState } from './recognition-client.types.js';
|
|
38
|
+
import type { IRecognitionClient, IRecognitionClientStats, RealTimeTwoWayWebSocketRecognitionClientConfig } from './recognition-client.types.js';
|
|
39
|
+
/**
|
|
40
|
+
* Check if a WebSocket close code indicates normal closure
|
|
41
|
+
* @param code - WebSocket close code
|
|
42
|
+
* @returns true if the disconnection was normal/expected, false if it was an error
|
|
43
|
+
*/
|
|
44
|
+
export declare function isNormalDisconnection(code: number): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Re-export TranscriptionResultV1 as TranscriptionResult for backward compatibility
|
|
47
|
+
*/
|
|
48
|
+
export type TranscriptionResult = TranscriptionResultV1;
|
|
49
|
+
export type { RealTimeTwoWayWebSocketRecognitionClientConfig } from './recognition-client.types.js';
|
|
50
|
+
/**
|
|
51
|
+
* RealTimeTwoWayWebSocketRecognitionClient - SDK-level client for real-time speech recognition
|
|
52
|
+
*
|
|
53
|
+
* Implements IRecognitionClient interface for dependency injection and testing.
|
|
54
|
+
* Extends WebSocketAudioClient with local audio buffering and simple callback-based API.
|
|
55
|
+
*/
|
|
56
|
+
export declare class RealTimeTwoWayWebSocketRecognitionClient extends WebSocketAudioClient<number, any, any> implements IRecognitionClient {
|
|
57
|
+
private static readonly PROTOCOL_VERSION;
|
|
58
|
+
private config;
|
|
59
|
+
private audioBuffer;
|
|
60
|
+
private messageHandler;
|
|
61
|
+
private state;
|
|
62
|
+
private connectionPromise;
|
|
63
|
+
private isDebugLogEnabled;
|
|
64
|
+
private audioBytesSent;
|
|
65
|
+
private audioChunksSent;
|
|
66
|
+
private audioStatsLogInterval;
|
|
67
|
+
private lastAudioStatsLog;
|
|
68
|
+
constructor(config: RealTimeTwoWayWebSocketRecognitionClientConfig);
|
|
69
|
+
/**
|
|
70
|
+
* Internal logging helper - only logs if a logger was provided in config
|
|
71
|
+
* Debug logs are additionally gated by isDebugLogEnabled flag
|
|
72
|
+
* @param level - Log level: debug, info, warn, or error
|
|
73
|
+
* @param message - Message to log
|
|
74
|
+
* @param data - Optional additional data to log
|
|
75
|
+
*/
|
|
76
|
+
private log;
|
|
77
|
+
/**
|
|
78
|
+
* Clean up internal resources to free memory
|
|
79
|
+
* Called when connection closes (normally or abnormally)
|
|
80
|
+
*/
|
|
81
|
+
private cleanup;
|
|
82
|
+
connect(): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Attempt to connect with retry logic
|
|
85
|
+
* Only retries on initial connection establishment, not mid-stream interruptions
|
|
86
|
+
*/
|
|
87
|
+
private connectWithRetry;
|
|
88
|
+
sendAudio(audioData: ArrayBuffer | ArrayBufferView | Blob): void;
|
|
89
|
+
private sendAudioInternal;
|
|
90
|
+
/**
|
|
91
|
+
* Only active ehwne client is in READY state. otherwise it will return immediately.
|
|
92
|
+
* @returns Promise that resolves when the recording is stopped
|
|
93
|
+
*/
|
|
94
|
+
stopRecording(): Promise<void>;
|
|
95
|
+
stopAbnormally(): void;
|
|
96
|
+
getAudioUtteranceId(): string;
|
|
97
|
+
getUrl(): string;
|
|
98
|
+
getState(): ClientState;
|
|
99
|
+
isConnected(): boolean;
|
|
100
|
+
isConnecting(): boolean;
|
|
101
|
+
isStopping(): boolean;
|
|
102
|
+
isTranscriptionFinished(): boolean;
|
|
103
|
+
isBufferOverflowing(): boolean;
|
|
104
|
+
getStats(): IRecognitionClientStats;
|
|
105
|
+
protected onConnected(): void;
|
|
106
|
+
protected onDisconnected(code: number, reason: string): void;
|
|
107
|
+
/**
|
|
108
|
+
* Get human-readable description for WebSocket close code
|
|
109
|
+
*/
|
|
110
|
+
private getCloseCodeDescription;
|
|
111
|
+
protected onError(error: Event): void;
|
|
112
|
+
protected onMessage(msg: {
|
|
113
|
+
v: number;
|
|
114
|
+
type: string;
|
|
115
|
+
data: any;
|
|
116
|
+
}): void;
|
|
117
|
+
/**
|
|
118
|
+
* Handle control messages from server
|
|
119
|
+
* @param msg - Control message containing server actions
|
|
120
|
+
*/
|
|
121
|
+
private handleControlMessage;
|
|
122
|
+
/**
|
|
123
|
+
* Send audio immediately to the server (without buffering)
|
|
124
|
+
* @param audioData - Audio data to send
|
|
125
|
+
*/
|
|
126
|
+
private sendAudioNow;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=recognition-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recognition-client.d.ts","sourceRoot":"","sources":["../src/recognition-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAML,KAAK,qBAAqB,EAS3B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC5D,OAAO,KAAK,EACV,kBAAkB,EAClB,uBAAuB,EACvB,8CAA8C,EAE/C,MAAM,+BAA+B,CAAC;AAUvC;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE3D;AAgCD;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,qBAAqB,CAAC;AAGxD,YAAY,EAAE,8CAA8C,EAAE,MAAM,+BAA+B,CAAC;AAgCpG;;;;;GAKG;AACH,qBAAa,wCACX,SAAQ,oBAAoB,CAAC,MAAM,EAAE,GAAG,EAA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,KAAK,GAAG,IAAI;cAYlB,SAAS,CAAC,GAAG,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,CAAA;KAAE,GAAG,IAAI;IAQ/E;;;OAGG;IACH,OAAO,CAAC,oBAAoB;IAyB5B;;;OAGG;IACH,OAAO,CAAC,YAAY;CAuBrB"}
|
|
@@ -0,0 +1,271 @@
|
|
|
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
|
+
import { TranscriptionResultV1, FunctionCallResultV1, MetadataResultV1, ErrorResultV1, ASRRequestConfig, GameContextV1, Stage } from '@recog/shared-types';
|
|
8
|
+
/**
|
|
9
|
+
* Client connection state enum
|
|
10
|
+
* Represents the various states a recognition client can be in during its lifecycle
|
|
11
|
+
*/
|
|
12
|
+
export declare enum ClientState {
|
|
13
|
+
/** Initial state, no connection established */
|
|
14
|
+
INITIAL = "initial",
|
|
15
|
+
/** Actively establishing WebSocket connection */
|
|
16
|
+
CONNECTING = "connecting",
|
|
17
|
+
/** WebSocket connected but waiting for server ready signal */
|
|
18
|
+
CONNECTED = "connected",
|
|
19
|
+
/** Server ready, can send audio */
|
|
20
|
+
READY = "ready",
|
|
21
|
+
/** Sent stop signal, waiting for final transcript */
|
|
22
|
+
STOPPING = "stopping",
|
|
23
|
+
/** Connection closed normally after stop */
|
|
24
|
+
STOPPED = "stopped",
|
|
25
|
+
/** Connection failed or lost unexpectedly */
|
|
26
|
+
FAILED = "failed"
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Callback URL configuration with message type filtering
|
|
30
|
+
*/
|
|
31
|
+
export interface RecognitionCallbackUrl {
|
|
32
|
+
/** The callback URL endpoint */
|
|
33
|
+
url: string;
|
|
34
|
+
/** Array of message types to send to this URL. If empty/undefined, all types are sent */
|
|
35
|
+
messageTypes?: Array<string | number>;
|
|
36
|
+
}
|
|
37
|
+
export type IRecognitionCallbackUrl = RecognitionCallbackUrl;
|
|
38
|
+
export interface IRecognitionClientConfig {
|
|
39
|
+
/**
|
|
40
|
+
* WebSocket endpoint URL (optional)
|
|
41
|
+
* Either `url` or `stage` must be provided.
|
|
42
|
+
* If both are provided, `url` takes precedence.
|
|
43
|
+
*
|
|
44
|
+
* Example with explicit URL:
|
|
45
|
+
* ```typescript
|
|
46
|
+
* { url: 'wss://custom-endpoint.example.com/ws/v1/recognize' }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
url?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Stage for recognition service (recommended)
|
|
52
|
+
* Either `url` or `stage` must be provided.
|
|
53
|
+
* If both are provided, `url` takes precedence.
|
|
54
|
+
* Defaults to production if neither is provided.
|
|
55
|
+
*
|
|
56
|
+
* Example with STAGES enum (recommended):
|
|
57
|
+
* ```typescript
|
|
58
|
+
* import { STAGES } from '@recog/shared-types';
|
|
59
|
+
* { stage: STAGES.STAGING }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* String values also accepted:
|
|
63
|
+
* ```typescript
|
|
64
|
+
* { stage: 'staging' } // STAGES.LOCAL | STAGES.DEV | STAGES.STAGING | STAGES.PRODUCTION
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
stage?: Stage | string;
|
|
68
|
+
/** ASR configuration (provider, model, language, etc.) - optional */
|
|
69
|
+
asrRequestConfig?: ASRRequestConfig;
|
|
70
|
+
/** Game context for improved recognition accuracy */
|
|
71
|
+
gameContext?: GameContextV1;
|
|
72
|
+
/**
|
|
73
|
+
* Game ID for tracking and routing purposes (optional)
|
|
74
|
+
* If provided, this is added to the WebSocket URL as a query parameter.
|
|
75
|
+
* If gameContext is also provided, this takes precedence over gameContext.gameId.
|
|
76
|
+
*/
|
|
77
|
+
gameId?: string;
|
|
78
|
+
/** Audio utterance ID (optional) - if not provided, a UUID v4 will be generated */
|
|
79
|
+
audioUtteranceId?: string;
|
|
80
|
+
/** Callback URLs for server-side notifications with optional message type filtering (optional)
|
|
81
|
+
* Game side only need to use it if another service need to be notified about the transcription results.
|
|
82
|
+
*/
|
|
83
|
+
callbackUrls?: RecognitionCallbackUrl[];
|
|
84
|
+
/** User identification (optional) */
|
|
85
|
+
userId?: string;
|
|
86
|
+
/** Game session identification (optional). called 'sessionId' in Platform and most games. */
|
|
87
|
+
gameSessionId?: string;
|
|
88
|
+
/** Device identification (optional) */
|
|
89
|
+
deviceId?: string;
|
|
90
|
+
/** Account identification (optional) */
|
|
91
|
+
accountId?: string;
|
|
92
|
+
/** Question answer identifier for tracking Q&A sessions (optional and tracking purpose only) */
|
|
93
|
+
questionAnswerId?: string;
|
|
94
|
+
/** Platform for audio recording device (optional, e.g., 'ios', 'android', 'web', 'unity') */
|
|
95
|
+
platform?: string;
|
|
96
|
+
/** Callback when transcript is received */
|
|
97
|
+
onTranscript?: (result: TranscriptionResultV1) => void;
|
|
98
|
+
/**
|
|
99
|
+
* Callback when function call is received
|
|
100
|
+
* Note: Not supported in 2025. P2 feature for future speech-to-function-call capability.
|
|
101
|
+
*/
|
|
102
|
+
onFunctionCall?: (result: FunctionCallResultV1) => void;
|
|
103
|
+
/** Callback when metadata is received. Only once after transcription is complete.*/
|
|
104
|
+
onMetadata?: (metadata: MetadataResultV1) => void;
|
|
105
|
+
/** Callback when error occurs */
|
|
106
|
+
onError?: (error: ErrorResultV1) => void;
|
|
107
|
+
/** Callback when connected to WebSocket */
|
|
108
|
+
onConnected?: () => void;
|
|
109
|
+
/**
|
|
110
|
+
* Callback when WebSocket disconnects
|
|
111
|
+
* @param code - WebSocket close code (1000 = normal, 1006 = abnormal, etc.)
|
|
112
|
+
* @param reason - Close reason string
|
|
113
|
+
*/
|
|
114
|
+
onDisconnected?: (code: number, reason: string) => void;
|
|
115
|
+
/** High water mark for backpressure control (bytes) */
|
|
116
|
+
highWaterMark?: number;
|
|
117
|
+
/** Low water mark for backpressure control (bytes) */
|
|
118
|
+
lowWaterMark?: number;
|
|
119
|
+
/** Maximum buffer duration in seconds (default: 60s) */
|
|
120
|
+
maxBufferDurationSec?: number;
|
|
121
|
+
/** Expected chunks per second for ring buffer sizing (default: 100) */
|
|
122
|
+
chunksPerSecond?: number;
|
|
123
|
+
/**
|
|
124
|
+
* Connection retry configuration (optional)
|
|
125
|
+
* Only applies to initial connection establishment, not mid-stream interruptions.
|
|
126
|
+
*
|
|
127
|
+
* Default: { maxAttempts: 4, delayMs: 200 } (try once, retry 3 times = 4 total attempts)
|
|
128
|
+
*
|
|
129
|
+
* Timing: Attempt 1 → FAIL → wait 200ms → Attempt 2 → FAIL → wait 200ms → Attempt 3 → FAIL → wait 200ms → Attempt 4
|
|
130
|
+
*
|
|
131
|
+
* Example:
|
|
132
|
+
* ```typescript
|
|
133
|
+
* {
|
|
134
|
+
* connectionRetry: {
|
|
135
|
+
* maxAttempts: 2, // Try connecting up to 2 times (1 retry)
|
|
136
|
+
* delayMs: 500 // Wait 500ms between attempts
|
|
137
|
+
* }
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
connectionRetry?: {
|
|
142
|
+
/** Maximum number of connection attempts (default: 4, min: 1, max: 5) */
|
|
143
|
+
maxAttempts?: number;
|
|
144
|
+
/** Delay in milliseconds between retry attempts (default: 200ms) */
|
|
145
|
+
delayMs?: number;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Optional logger function for debugging
|
|
149
|
+
* If not provided, no logging will occur
|
|
150
|
+
* @param level - Log level: 'debug', 'info', 'warn', 'error'
|
|
151
|
+
* @param message - Log message
|
|
152
|
+
* @param data - Optional additional data
|
|
153
|
+
*/
|
|
154
|
+
logger?: (level: 'debug' | 'info' | 'warn' | 'error', message: string, data?: any) => void;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Recognition Client Interface
|
|
158
|
+
*
|
|
159
|
+
* Main interface for real-time speech recognition clients.
|
|
160
|
+
* Provides methods for connection management, audio streaming, and session control.
|
|
161
|
+
*/
|
|
162
|
+
export interface IRecognitionClient {
|
|
163
|
+
/**
|
|
164
|
+
* Connect to the WebSocket endpoint
|
|
165
|
+
* @returns Promise that resolves when connected
|
|
166
|
+
* @throws Error if connection fails or times out
|
|
167
|
+
*/
|
|
168
|
+
connect(): Promise<void>;
|
|
169
|
+
/**
|
|
170
|
+
* Send audio data to the recognition service
|
|
171
|
+
* Audio is buffered locally and sent when connection is ready.
|
|
172
|
+
* @param audioData - PCM audio data as ArrayBuffer, typed array view, or Blob
|
|
173
|
+
*/
|
|
174
|
+
sendAudio(audioData: ArrayBuffer | ArrayBufferView | Blob): void;
|
|
175
|
+
/**
|
|
176
|
+
* Stop recording and wait for final transcript
|
|
177
|
+
* The server will close the connection after sending the final transcript.
|
|
178
|
+
* @returns Promise that resolves when final transcript is received
|
|
179
|
+
*/
|
|
180
|
+
stopRecording(): Promise<void>;
|
|
181
|
+
/**
|
|
182
|
+
* Force stop and immediately close connection without waiting for server
|
|
183
|
+
*
|
|
184
|
+
* WARNING: This is an abnormal shutdown that bypasses the graceful stop flow:
|
|
185
|
+
* - Does NOT wait for server to process remaining audio
|
|
186
|
+
* - Does NOT receive final transcript from server
|
|
187
|
+
* - Immediately closes WebSocket connection
|
|
188
|
+
* - Cleans up resources (buffers, listeners)
|
|
189
|
+
*
|
|
190
|
+
* Use Cases:
|
|
191
|
+
* - User explicitly cancels/abandons session
|
|
192
|
+
* - Timeout scenarios where waiting is not acceptable
|
|
193
|
+
* - Need immediate cleanup and can't wait for server
|
|
194
|
+
*
|
|
195
|
+
* RECOMMENDED: Use stopRecording() for normal shutdown.
|
|
196
|
+
* Only use this when immediate disconnection is required.
|
|
197
|
+
*/
|
|
198
|
+
stopAbnormally(): void;
|
|
199
|
+
/**
|
|
200
|
+
* Get the audio utterance ID for this session
|
|
201
|
+
* Available immediately after client construction.
|
|
202
|
+
* @returns UUID v4 string identifying this recognition session
|
|
203
|
+
*/
|
|
204
|
+
getAudioUtteranceId(): string;
|
|
205
|
+
/**
|
|
206
|
+
* Get the current state of the client
|
|
207
|
+
* @returns Current ClientState value
|
|
208
|
+
*/
|
|
209
|
+
getState(): ClientState;
|
|
210
|
+
/**
|
|
211
|
+
* Check if WebSocket connection is open
|
|
212
|
+
* @returns true if connected and ready to communicate
|
|
213
|
+
*/
|
|
214
|
+
isConnected(): boolean;
|
|
215
|
+
/**
|
|
216
|
+
* Check if client is currently connecting
|
|
217
|
+
* @returns true if connection is in progress
|
|
218
|
+
*/
|
|
219
|
+
isConnecting(): boolean;
|
|
220
|
+
/**
|
|
221
|
+
* Check if client is currently stopping
|
|
222
|
+
* @returns true if stopRecording() is in progress
|
|
223
|
+
*/
|
|
224
|
+
isStopping(): boolean;
|
|
225
|
+
/**
|
|
226
|
+
* Check if transcription has finished
|
|
227
|
+
* @returns true if the transcription is complete
|
|
228
|
+
*/
|
|
229
|
+
isTranscriptionFinished(): boolean;
|
|
230
|
+
/**
|
|
231
|
+
* Check if the audio buffer has overflowed
|
|
232
|
+
* @returns true if the ring buffer has wrapped around
|
|
233
|
+
*/
|
|
234
|
+
isBufferOverflowing(): boolean;
|
|
235
|
+
/**
|
|
236
|
+
* Get client statistics
|
|
237
|
+
* @returns Statistics about audio transmission and buffering
|
|
238
|
+
*/
|
|
239
|
+
getStats(): IRecognitionClientStats;
|
|
240
|
+
/**
|
|
241
|
+
* Get the WebSocket URL being used by this client
|
|
242
|
+
* Available immediately after client construction.
|
|
243
|
+
* @returns WebSocket URL string
|
|
244
|
+
*/
|
|
245
|
+
getUrl(): string;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Client statistics interface
|
|
249
|
+
*/
|
|
250
|
+
export interface IRecognitionClientStats {
|
|
251
|
+
/** Total audio bytes sent to server */
|
|
252
|
+
audioBytesSent: number;
|
|
253
|
+
/** Total number of audio chunks sent */
|
|
254
|
+
audioChunksSent: number;
|
|
255
|
+
/** Total number of audio chunks buffered */
|
|
256
|
+
audioChunksBuffered: number;
|
|
257
|
+
/** Number of times the ring buffer overflowed */
|
|
258
|
+
bufferOverflowCount: number;
|
|
259
|
+
/** Current number of chunks in buffer */
|
|
260
|
+
currentBufferedChunks: number;
|
|
261
|
+
/** Whether the ring buffer has wrapped (overwritten old data) */
|
|
262
|
+
hasWrapped: boolean;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Configuration for RealTimeTwoWayWebSocketRecognitionClient
|
|
266
|
+
* This extends IRecognitionClientConfig and is the main configuration interface
|
|
267
|
+
* for creating a new RealTimeTwoWayWebSocketRecognitionClient instance.
|
|
268
|
+
*/
|
|
269
|
+
export interface RealTimeTwoWayWebSocketRecognitionClientConfig extends IRecognitionClientConfig {
|
|
270
|
+
}
|
|
271
|
+
//# sourceMappingURL=recognition-client.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recognition-client.types.d.ts","sourceRoot":"","sources":["../src/recognition-client.types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,qBAAqB,EACrB,oBAAoB,EACpB,gBAAgB,EAChB,aAAa,EACb,gBAAgB,EAChB,aAAa,EACb,KAAK,EACN,MAAM,qBAAqB,CAAC;AAE7B;;;GAGG;AACH,oBAAY,WAAW;IACrB,+CAA+C;IAC/C,OAAO,YAAY;IAEnB,iDAAiD;IACjD,UAAU,eAAe;IAEzB,8DAA8D;IAC9D,SAAS,cAAc;IAEvB,mCAAmC;IACnC,KAAK,UAAU;IAEf,qDAAqD;IACrD,QAAQ,aAAa;IAErB,4CAA4C;IAC5C,OAAO,YAAY;IAEnB,6CAA6C;IAC7C,MAAM,WAAW;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,gCAAgC;IAChC,GAAG,EAAE,MAAM,CAAC;IAEZ,yFAAyF;IACzF,YAAY,CAAC,EAAE,KAA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BAAuB,IAAI,OAAO,CAAC;IAEnC;;;OAGG;IACH,mBAAmB,IAAI,OAAO,CAAC;IAE/B;;;OAGG;IACH,QAAQ,IAAI,uBAAuB,CAAC;IAEpC;;;;OAIG;IACH,MAAM,IAAI,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,uCAAuC;IACvC,cAAc,EAAE,MAAM,CAAC;IAEvB,wCAAwC;IACxC,eAAe,EAAE,MAAM,CAAC;IAExB,4CAA4C;IAC5C,mBAAmB,EAAE,MAAM,CAAC;IAE5B,iDAAiD;IACjD,mBAAmB,EAAE,MAAM,CAAC;IAE5B,yCAAyC;IACzC,qBAAqB,EAAE,MAAM,CAAC;IAE9B,iEAAiE;IACjE,UAAU,EAAE,OAAO,CAAC;CACrB;AAED;;;;GAIG;AACH,MAAM,WAAW,8CAA+C,SAAQ,wBAAwB;CAG/F"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simplified VGF Recognition Client
|
|
3
|
+
*
|
|
4
|
+
* A thin wrapper around RealTimeTwoWayWebSocketRecognitionClient that maintains
|
|
5
|
+
* a VGF RecognitionState as a pure sink/output of recognition events.
|
|
6
|
+
*
|
|
7
|
+
* The VGF state is updated based on events but never influences client behavior.
|
|
8
|
+
* All functionality is delegated to the underlying client.
|
|
9
|
+
*/
|
|
10
|
+
import { RecognitionState } from './vgf-recognition-state.js';
|
|
11
|
+
import { IRecognitionClientConfig, ClientState } from './recognition-client.types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for SimplifiedVGFRecognitionClient
|
|
14
|
+
*/
|
|
15
|
+
export interface SimplifiedVGFClientConfig extends IRecognitionClientConfig {
|
|
16
|
+
/**
|
|
17
|
+
* Callback invoked whenever the VGF state changes
|
|
18
|
+
* Use this to update your UI or React state
|
|
19
|
+
*/
|
|
20
|
+
onStateChange?: (state: RecognitionState) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Optional initial state to restore from a previous session
|
|
23
|
+
* If provided, audioUtteranceId will be extracted and used
|
|
24
|
+
*/
|
|
25
|
+
initialState?: RecognitionState;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Interface for SimplifiedVGFRecognitionClient
|
|
29
|
+
*
|
|
30
|
+
* A simplified client that maintains VGF state for game developers.
|
|
31
|
+
* All methods from the underlying client are available, plus VGF state management.
|
|
32
|
+
*/
|
|
33
|
+
export interface ISimplifiedVGFRecognitionClient {
|
|
34
|
+
/**
|
|
35
|
+
* Connect to the recognition service WebSocket
|
|
36
|
+
* @returns Promise that resolves when connected and ready
|
|
37
|
+
*/
|
|
38
|
+
connect(): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Send audio data for transcription
|
|
41
|
+
* @param audioData - PCM audio data as ArrayBuffer, typed array, or Blob
|
|
42
|
+
*/
|
|
43
|
+
sendAudio(audioData: ArrayBuffer | ArrayBufferView | Blob): void;
|
|
44
|
+
/**
|
|
45
|
+
* Stop recording and wait for final transcription
|
|
46
|
+
* @returns Promise that resolves when transcription is complete
|
|
47
|
+
*/
|
|
48
|
+
stopRecording(): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Force stop and immediately close connection without waiting for server
|
|
51
|
+
*
|
|
52
|
+
* WARNING: This is an abnormal shutdown that bypasses the graceful stop flow:
|
|
53
|
+
* - Does NOT wait for server to process remaining audio
|
|
54
|
+
* - Does NOT receive final transcript from server (VGF state set to empty)
|
|
55
|
+
* - Immediately closes WebSocket connection
|
|
56
|
+
* - Cleans up resources (buffers, listeners)
|
|
57
|
+
*
|
|
58
|
+
* Use Cases:
|
|
59
|
+
* - User explicitly cancels/abandons the session
|
|
60
|
+
* - Timeout scenarios where waiting is not acceptable
|
|
61
|
+
* - Need immediate cleanup and can't wait for server
|
|
62
|
+
*
|
|
63
|
+
* RECOMMENDED: Use stopRecording() for normal shutdown.
|
|
64
|
+
* Only use this when immediate disconnection is required.
|
|
65
|
+
*/
|
|
66
|
+
stopAbnormally(): void;
|
|
67
|
+
/**
|
|
68
|
+
* Get the current VGF recognition state
|
|
69
|
+
* @returns Current RecognitionState with all transcription data
|
|
70
|
+
*/
|
|
71
|
+
getVGFState(): RecognitionState;
|
|
72
|
+
/**
|
|
73
|
+
* Check if connected to the WebSocket
|
|
74
|
+
*/
|
|
75
|
+
isConnected(): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Check if currently connecting
|
|
78
|
+
*/
|
|
79
|
+
isConnecting(): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Check if currently stopping
|
|
82
|
+
*/
|
|
83
|
+
isStopping(): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Check if transcription has finished
|
|
86
|
+
*/
|
|
87
|
+
isTranscriptionFinished(): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Check if the audio buffer has overflowed
|
|
90
|
+
*/
|
|
91
|
+
isBufferOverflowing(): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Get the audio utterance ID for this session
|
|
94
|
+
*/
|
|
95
|
+
getAudioUtteranceId(): string;
|
|
96
|
+
/**
|
|
97
|
+
* Get the WebSocket URL being used
|
|
98
|
+
*/
|
|
99
|
+
getUrl(): string;
|
|
100
|
+
/**
|
|
101
|
+
* Get the underlying client state (for advanced usage)
|
|
102
|
+
*/
|
|
103
|
+
getState(): ClientState;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* This wrapper ONLY maintains VGF state as a sink.
|
|
107
|
+
* All actual functionality is delegated to the underlying client.
|
|
108
|
+
*/
|
|
109
|
+
export declare class SimplifiedVGFRecognitionClient implements ISimplifiedVGFRecognitionClient {
|
|
110
|
+
private client;
|
|
111
|
+
private state;
|
|
112
|
+
private isRecordingAudio;
|
|
113
|
+
private stateChangeCallback;
|
|
114
|
+
private expectedUuid;
|
|
115
|
+
private logger;
|
|
116
|
+
private lastSentTerminalUuid;
|
|
117
|
+
constructor(config: SimplifiedVGFClientConfig);
|
|
118
|
+
connect(): Promise<void>;
|
|
119
|
+
sendAudio(audioData: ArrayBuffer | ArrayBufferView | Blob): void;
|
|
120
|
+
stopRecording(): Promise<void>;
|
|
121
|
+
stopAbnormally(): void;
|
|
122
|
+
getAudioUtteranceId(): string;
|
|
123
|
+
getUrl(): string;
|
|
124
|
+
getState(): ClientState;
|
|
125
|
+
isConnected(): boolean;
|
|
126
|
+
isConnecting(): boolean;
|
|
127
|
+
isStopping(): boolean;
|
|
128
|
+
isTranscriptionFinished(): boolean;
|
|
129
|
+
isBufferOverflowing(): boolean;
|
|
130
|
+
getVGFState(): RecognitionState;
|
|
131
|
+
private isTerminalStatus;
|
|
132
|
+
private notifyStateChange;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Factory function for creating simplified client
|
|
136
|
+
* Usage examples:
|
|
137
|
+
*
|
|
138
|
+
* // Basic usage
|
|
139
|
+
* const client = createSimplifiedVGFClient({
|
|
140
|
+
* asrRequestConfig: { provider: 'deepgram', language: 'en' },
|
|
141
|
+
* onStateChange: (state) => {
|
|
142
|
+
* console.log('VGF State updated:', state);
|
|
143
|
+
* // Update React state, game UI, etc.
|
|
144
|
+
* }
|
|
145
|
+
* });
|
|
146
|
+
*
|
|
147
|
+
* // With initial state (e.g., restoring from previous session)
|
|
148
|
+
* const client = createSimplifiedVGFClient({
|
|
149
|
+
* asrRequestConfig: { provider: 'deepgram', language: 'en' },
|
|
150
|
+
* initialState: previousState, // Will use audioUtteranceId from state
|
|
151
|
+
* onStateChange: (state) => setVGFState(state)
|
|
152
|
+
* });
|
|
153
|
+
*
|
|
154
|
+
* // With initial state containing promptSlotMap for enhanced recognition
|
|
155
|
+
* const stateWithSlots: RecognitionState = {
|
|
156
|
+
* audioUtteranceId: 'session-123',
|
|
157
|
+
* promptSlotMap: {
|
|
158
|
+
* 'song_title': ['one time', 'baby'],
|
|
159
|
+
* 'artists': ['justin bieber']
|
|
160
|
+
* }
|
|
161
|
+
* };
|
|
162
|
+
* const client = createSimplifiedVGFClient({
|
|
163
|
+
* asrRequestConfig: { provider: 'deepgram', language: 'en' },
|
|
164
|
+
* gameContext: {
|
|
165
|
+
* type: RecognitionContextTypeV1.GAME_CONTEXT,
|
|
166
|
+
* gameId: 'music-quiz', // Your game's ID
|
|
167
|
+
* gamePhase: 'song-guessing' // Current game phase
|
|
168
|
+
* },
|
|
169
|
+
* initialState: stateWithSlots, // promptSlotMap will be added to gameContext
|
|
170
|
+
* onStateChange: (state) => setVGFState(state)
|
|
171
|
+
* });
|
|
172
|
+
*
|
|
173
|
+
* await client.connect();
|
|
174
|
+
* client.sendAudio(audioData);
|
|
175
|
+
* // VGF state automatically updates based on transcription results
|
|
176
|
+
*/
|
|
177
|
+
export declare function createSimplifiedVGFClient(config: SimplifiedVGFClientConfig): ISimplifiedVGFRecognitionClient;
|
|
178
|
+
//# sourceMappingURL=simplified-vgf-recognition-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simplified-vgf-recognition-client.d.ts","sourceRoot":"","sources":["../src/simplified-vgf-recognition-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EACH,gBAAgB,EAInB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAEH,wBAAwB,EACxB,WAAW,EACd,MAAM,+BAA+B,CAAC;AAWvC;;GAEG;AACH,MAAM,WAAW,yBAA0B,SAAQ,wBAAwB;IACvE;;;OAGG;IACH,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,CAAC;IAElD;;;OAGG;IACH,YAAY,CAAC,EAAE,gBAAgB,CAAC;CACnC;AAED;;;;;GAKG;AACH,MAAM,WAAW,+BAA+B;IAE5C;;;OAGG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzB;;;OAGG;IACH,SAAS,CAAC,SAAS,EAAE,WAAW,GAAG,eAAe,GAAG,IAAI,GAAG,IAAI,CAAC;IAEjE;;;OAGG;IACH,aAAa,IAAI,OAAO,CAAC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,CAAC,gBAAgB;IAMxB,OAAO,CAAC,iBAAiB;CA8B5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,yBAAyB,CAAC,MAAM,EAAE,yBAAyB,GAAG,+BAA+B,CAE5G"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audio Ring Buffer
|
|
3
|
+
* Manages circular buffer for audio data with overflow detection
|
|
4
|
+
*/
|
|
5
|
+
export interface BufferedAudio {
|
|
6
|
+
data: ArrayBuffer | ArrayBufferView;
|
|
7
|
+
timestamp: number;
|
|
8
|
+
}
|
|
9
|
+
export interface AudioRingBufferConfig {
|
|
10
|
+
maxBufferDurationSec: number;
|
|
11
|
+
chunksPerSecond: number;
|
|
12
|
+
logger?: (level: 'debug' | 'info' | 'warn' | 'error', message: string, data?: any) => void;
|
|
13
|
+
}
|
|
14
|
+
export declare class AudioRingBuffer {
|
|
15
|
+
private buffer;
|
|
16
|
+
private bufferSize;
|
|
17
|
+
private writeIndex;
|
|
18
|
+
private readIndex;
|
|
19
|
+
private hasWrapped;
|
|
20
|
+
private totalBufferedBytes;
|
|
21
|
+
private overflowCount;
|
|
22
|
+
private chunksBuffered;
|
|
23
|
+
private logger?;
|
|
24
|
+
constructor(config: AudioRingBufferConfig);
|
|
25
|
+
/**
|
|
26
|
+
* Write audio chunk to ring buffer with overflow detection
|
|
27
|
+
*/
|
|
28
|
+
write(audioData: ArrayBuffer | ArrayBufferView): void;
|
|
29
|
+
/**
|
|
30
|
+
* Read next chunk from buffer
|
|
31
|
+
*/
|
|
32
|
+
read(): BufferedAudio | null;
|
|
33
|
+
/**
|
|
34
|
+
* Read all buffered chunks without removing them
|
|
35
|
+
*/
|
|
36
|
+
readAll(): BufferedAudio[];
|
|
37
|
+
/**
|
|
38
|
+
* Flush all buffered data and advance read pointer
|
|
39
|
+
*/
|
|
40
|
+
flush(): BufferedAudio[];
|
|
41
|
+
/**
|
|
42
|
+
* Get count of buffered chunks
|
|
43
|
+
*/
|
|
44
|
+
getBufferedCount(): number;
|
|
45
|
+
/**
|
|
46
|
+
* Check if buffer is empty
|
|
47
|
+
*/
|
|
48
|
+
isEmpty(): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Check if buffer has overflowed
|
|
51
|
+
*/
|
|
52
|
+
isOverflowing(): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Clear the buffer and reset all counters
|
|
55
|
+
* Frees memory by releasing all stored audio chunks
|
|
56
|
+
*/
|
|
57
|
+
clear(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Get buffer statistics
|
|
60
|
+
*/
|
|
61
|
+
getStats(): {
|
|
62
|
+
chunksBuffered: number;
|
|
63
|
+
currentBufferedChunks: number;
|
|
64
|
+
overflowCount: number;
|
|
65
|
+
hasWrapped: boolean;
|
|
66
|
+
totalBufferedBytes: number;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=audio-ring-buffer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-ring-buffer.d.ts","sourceRoot":"","sources":["../../src/utils/audio-ring-buffer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,WAAW,GAAG,eAAe,CAAC;IACpC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,qBAAqB;IACpC,oBAAoB,EAAE,MAAM,CAAC;IAC7B,eAAe,EAAE,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC;CAC5F;AAED,qBAAa,eAAe;IAC1B,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,UAAU,CA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