rystem.playframework.client 0.1.1 → 0.1.3
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 +1617 -47
- package/lib/engine/ClientInteractionRegistry.d.ts +48 -1
- package/lib/engine/ClientInteractionRegistry.d.ts.map +1 -1
- package/lib/engine/ClientInteractionRegistry.js +84 -13
- package/lib/engine/PlayFrameworkClient.d.ts +58 -0
- package/lib/engine/PlayFrameworkClient.d.ts.map +1 -1
- package/lib/engine/PlayFrameworkClient.js +271 -24
- package/lib/index.d.ts +17 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +15 -1
- package/lib/models/AiSceneResponse.d.ts +2 -2
- package/lib/models/AiSceneResponse.d.ts.map +1 -1
- package/lib/models/ClientInteractionRequest.d.ts +15 -0
- package/lib/models/ClientInteractionRequest.d.ts.map +1 -1
- package/lib/models/CommandResult.d.ts +31 -0
- package/lib/models/CommandResult.d.ts.map +1 -0
- package/lib/models/CommandResult.js +18 -0
- package/lib/models/ExecutionState.d.ts +35 -0
- package/lib/models/ExecutionState.d.ts.map +1 -0
- package/lib/models/ExecutionState.js +2 -0
- package/lib/models/PlayFrameworkRequest.d.ts +10 -0
- package/lib/models/PlayFrameworkRequest.d.ts.map +1 -1
- package/lib/models/StoredConversation.d.ts +88 -0
- package/lib/models/StoredConversation.d.ts.map +1 -0
- package/lib/models/StoredConversation.js +17 -0
- package/lib/models/StoredMessage.d.ts +30 -0
- package/lib/models/StoredMessage.d.ts.map +1 -0
- package/lib/models/StoredMessage.js +2 -0
- package/lib/models/VoiceResponse.d.ts +39 -0
- package/lib/models/VoiceResponse.d.ts.map +1 -0
- package/lib/models/VoiceResponse.js +2 -0
- package/lib/utils/BrowserSpeechRecognizer.d.ts +128 -0
- package/lib/utils/BrowserSpeechRecognizer.d.ts.map +1 -0
- package/lib/utils/BrowserSpeechRecognizer.js +150 -0
- package/lib/utils/BrowserSpeechSynthesizer.d.ts +148 -0
- package/lib/utils/BrowserSpeechSynthesizer.d.ts.map +1 -0
- package/lib/utils/BrowserSpeechSynthesizer.js +400 -0
- package/lib/utils/BrowserVoiceClient.d.ts +142 -0
- package/lib/utils/BrowserVoiceClient.d.ts.map +1 -0
- package/lib/utils/BrowserVoiceClient.js +200 -0
- package/lib/utils/ContentUrlConverter.d.ts +62 -0
- package/lib/utils/ContentUrlConverter.d.ts.map +1 -0
- package/lib/utils/ContentUrlConverter.js +151 -0
- package/lib/utils/VoiceRecorder.d.ts +90 -0
- package/lib/utils/VoiceRecorder.d.ts.map +1 -0
- package/lib/utils/VoiceRecorder.js +195 -0
- package/package.json +1 -1
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VoiceRecorder = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Cross-browser voice recorder with support for VAD, push-to-talk, and hold-to-talk modes.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const recorder = new VoiceRecorder({ mode: 'vad', silenceDurationMs: 2000 });
|
|
10
|
+
*
|
|
11
|
+
* // Start recording — in VAD mode it stops automatically on silence
|
|
12
|
+
* recorder.start({
|
|
13
|
+
* onStart: () => console.log('Recording...'),
|
|
14
|
+
* onTick: (s) => console.log(`${s}s`),
|
|
15
|
+
* onRecorded: (blob) => sendToServer(blob),
|
|
16
|
+
* onStop: () => console.log('Done'),
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // For pushToTalk: call recorder.stop() when user presses the button again.
|
|
20
|
+
* // For holdToTalk: call recorder.stop() on pointerup / mouseup.
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
class VoiceRecorder {
|
|
24
|
+
_options;
|
|
25
|
+
_mediaRecorder = null;
|
|
26
|
+
_stream = null;
|
|
27
|
+
_audioContext = null;
|
|
28
|
+
_analyser = null;
|
|
29
|
+
_vadIntervalId = null;
|
|
30
|
+
_tickIntervalId = null;
|
|
31
|
+
_maxTimeoutId = null;
|
|
32
|
+
_recording = false;
|
|
33
|
+
_startTime = 0;
|
|
34
|
+
_callbacks = {};
|
|
35
|
+
constructor(options) {
|
|
36
|
+
this._options = {
|
|
37
|
+
mode: options?.mode ?? 'vad',
|
|
38
|
+
mimeType: options?.mimeType ?? 'audio/webm',
|
|
39
|
+
maxDurationMs: options?.maxDurationMs ?? 30_000,
|
|
40
|
+
silenceThreshold: options?.silenceThreshold ?? 0.01,
|
|
41
|
+
silenceDurationMs: options?.silenceDurationMs ?? 1500,
|
|
42
|
+
minRecordingMs: options?.minRecordingMs ?? 500,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Whether the recorder is currently active. */
|
|
46
|
+
get isRecording() {
|
|
47
|
+
return this._recording;
|
|
48
|
+
}
|
|
49
|
+
/** Current recording mode. */
|
|
50
|
+
get mode() {
|
|
51
|
+
return this._options.mode;
|
|
52
|
+
}
|
|
53
|
+
set mode(value) {
|
|
54
|
+
if (this._recording)
|
|
55
|
+
throw new Error('Cannot change mode while recording');
|
|
56
|
+
this._options.mode = value;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Start recording. In `vad` mode the recording stops automatically on silence.
|
|
60
|
+
* In `pushToTalk` and `holdToTalk` you must call `stop()` manually.
|
|
61
|
+
*/
|
|
62
|
+
async start(callbacks) {
|
|
63
|
+
if (this._recording)
|
|
64
|
+
return;
|
|
65
|
+
this._callbacks = callbacks ?? {};
|
|
66
|
+
this._recording = true;
|
|
67
|
+
this._startTime = Date.now();
|
|
68
|
+
try {
|
|
69
|
+
this._stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
70
|
+
const recorder = new MediaRecorder(this._stream, { mimeType: this._options.mimeType });
|
|
71
|
+
this._mediaRecorder = recorder;
|
|
72
|
+
const chunks = [];
|
|
73
|
+
recorder.ondataavailable = (e) => {
|
|
74
|
+
if (e.data.size > 0)
|
|
75
|
+
chunks.push(e.data);
|
|
76
|
+
};
|
|
77
|
+
recorder.onstop = () => {
|
|
78
|
+
const blob = new Blob(chunks, { type: this._options.mimeType });
|
|
79
|
+
this._cleanup();
|
|
80
|
+
this._callbacks.onRecorded?.(blob);
|
|
81
|
+
this._callbacks.onStop?.();
|
|
82
|
+
};
|
|
83
|
+
recorder.onerror = () => {
|
|
84
|
+
this._cleanup();
|
|
85
|
+
this._callbacks.onError?.(new Error('MediaRecorder error'));
|
|
86
|
+
this._callbacks.onStop?.();
|
|
87
|
+
};
|
|
88
|
+
recorder.start();
|
|
89
|
+
this._callbacks.onStart?.();
|
|
90
|
+
// Tick timer (1s intervals)
|
|
91
|
+
let seconds = 0;
|
|
92
|
+
this._tickIntervalId = setInterval(() => {
|
|
93
|
+
seconds++;
|
|
94
|
+
this._callbacks.onTick?.(seconds);
|
|
95
|
+
}, 1000);
|
|
96
|
+
// Max duration safety cap
|
|
97
|
+
this._maxTimeoutId = setTimeout(() => {
|
|
98
|
+
this.stop();
|
|
99
|
+
}, this._options.maxDurationMs);
|
|
100
|
+
// VAD: set up silence detection
|
|
101
|
+
if (this._options.mode === 'vad') {
|
|
102
|
+
this._setupVAD();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
this._recording = false;
|
|
107
|
+
this._cleanup();
|
|
108
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
109
|
+
this._callbacks.onError?.(error);
|
|
110
|
+
this._callbacks.onStop?.();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Stop recording manually. Required for `pushToTalk` and `holdToTalk`,
|
|
115
|
+
* also works for `vad` (e.g. user wants to stop early).
|
|
116
|
+
*/
|
|
117
|
+
stop() {
|
|
118
|
+
if (!this._recording)
|
|
119
|
+
return;
|
|
120
|
+
this._recording = false;
|
|
121
|
+
if (this._mediaRecorder && this._mediaRecorder.state !== 'inactive') {
|
|
122
|
+
this._mediaRecorder.stop(); // triggers onstop -> onRecorded -> onStop
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
this._cleanup();
|
|
126
|
+
this._callbacks.onStop?.();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// ── Private ──────────────────────────────────────────────────────
|
|
130
|
+
_setupVAD() {
|
|
131
|
+
if (!this._stream)
|
|
132
|
+
return;
|
|
133
|
+
this._audioContext = new AudioContext();
|
|
134
|
+
const source = this._audioContext.createMediaStreamSource(this._stream);
|
|
135
|
+
this._analyser = this._audioContext.createAnalyser();
|
|
136
|
+
this._analyser.fftSize = 2048;
|
|
137
|
+
source.connect(this._analyser);
|
|
138
|
+
const dataArray = new Uint8Array(this._analyser.fftSize);
|
|
139
|
+
let silenceStart = null;
|
|
140
|
+
this._vadIntervalId = setInterval(() => {
|
|
141
|
+
if (!this._analyser || !this._recording)
|
|
142
|
+
return;
|
|
143
|
+
this._analyser.getByteTimeDomainData(dataArray);
|
|
144
|
+
// Compute RMS (root mean square) level
|
|
145
|
+
let sum = 0;
|
|
146
|
+
for (let i = 0; i < dataArray.length; i++) {
|
|
147
|
+
const normalized = (dataArray[i] - 128) / 128; // -1..1
|
|
148
|
+
sum += normalized * normalized;
|
|
149
|
+
}
|
|
150
|
+
const rms = Math.sqrt(sum / dataArray.length);
|
|
151
|
+
const elapsed = Date.now() - this._startTime;
|
|
152
|
+
if (rms < this._options.silenceThreshold) {
|
|
153
|
+
// Below threshold — start or continue silence timer
|
|
154
|
+
if (silenceStart === null) {
|
|
155
|
+
silenceStart = Date.now();
|
|
156
|
+
}
|
|
157
|
+
else if (elapsed >= this._options.minRecordingMs &&
|
|
158
|
+
Date.now() - silenceStart >= this._options.silenceDurationMs) {
|
|
159
|
+
// Enough silence after minimum recording time → stop
|
|
160
|
+
this.stop();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
// Audio detected — reset silence timer
|
|
165
|
+
silenceStart = null;
|
|
166
|
+
}
|
|
167
|
+
}, 100); // Check every 100ms
|
|
168
|
+
}
|
|
169
|
+
_cleanup() {
|
|
170
|
+
this._recording = false;
|
|
171
|
+
if (this._vadIntervalId !== null) {
|
|
172
|
+
clearInterval(this._vadIntervalId);
|
|
173
|
+
this._vadIntervalId = null;
|
|
174
|
+
}
|
|
175
|
+
if (this._tickIntervalId !== null) {
|
|
176
|
+
clearInterval(this._tickIntervalId);
|
|
177
|
+
this._tickIntervalId = null;
|
|
178
|
+
}
|
|
179
|
+
if (this._maxTimeoutId !== null) {
|
|
180
|
+
clearTimeout(this._maxTimeoutId);
|
|
181
|
+
this._maxTimeoutId = null;
|
|
182
|
+
}
|
|
183
|
+
if (this._audioContext) {
|
|
184
|
+
this._audioContext.close().catch(() => { });
|
|
185
|
+
this._audioContext = null;
|
|
186
|
+
this._analyser = null;
|
|
187
|
+
}
|
|
188
|
+
if (this._stream) {
|
|
189
|
+
this._stream.getTracks().forEach(t => t.stop());
|
|
190
|
+
this._stream = null;
|
|
191
|
+
}
|
|
192
|
+
this._mediaRecorder = null;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
exports.VoiceRecorder = VoiceRecorder;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rystem.playframework.client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"author": "Alessandro Rapiti <alessandro.rapiti44@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "TypeScript client for Rystem PlayFramework HTTP API with step-by-step and token-level streaming support.",
|