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,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BrowserSpeechRecognizer = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Wraps the browser Web Speech API `SpeechRecognition` for speech-to-text.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const recognizer = new BrowserSpeechRecognizer({ lang: 'it-IT' });
|
|
10
|
+
*
|
|
11
|
+
* // One-shot usage — returns final transcript as a Promise
|
|
12
|
+
* const text = await recognizer.listen();
|
|
13
|
+
*
|
|
14
|
+
* // Event-based usage
|
|
15
|
+
* recognizer.start({
|
|
16
|
+
* onResult: (r) => console.log(r.transcript, r.isFinal),
|
|
17
|
+
* onEnd: () => console.log('done'),
|
|
18
|
+
* });
|
|
19
|
+
* // later...
|
|
20
|
+
* recognizer.stop();
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
class BrowserSpeechRecognizer {
|
|
24
|
+
options;
|
|
25
|
+
recognition = null;
|
|
26
|
+
_isListening = false;
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.options = {
|
|
29
|
+
lang: options?.lang ?? '',
|
|
30
|
+
interimResults: options?.interimResults ?? true,
|
|
31
|
+
continuous: options?.continuous ?? false,
|
|
32
|
+
maxAlternatives: options?.maxAlternatives ?? 1,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** Whether recognition is currently active. */
|
|
36
|
+
get isListening() {
|
|
37
|
+
return this._isListening;
|
|
38
|
+
}
|
|
39
|
+
/** Returns `true` if the browser supports the Web Speech Recognition API. */
|
|
40
|
+
static isSupported() {
|
|
41
|
+
return typeof window !== 'undefined' &&
|
|
42
|
+
!!(window.SpeechRecognition || window.webkitSpeechRecognition);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Start recognition with event callbacks.
|
|
46
|
+
* If already listening, this is a no-op.
|
|
47
|
+
*/
|
|
48
|
+
start(callbacks) {
|
|
49
|
+
if (this._isListening)
|
|
50
|
+
return;
|
|
51
|
+
const SpeechRecognitionCtor = window.SpeechRecognition || window.webkitSpeechRecognition;
|
|
52
|
+
if (!SpeechRecognitionCtor) {
|
|
53
|
+
callbacks?.onError?.(new Error('SpeechRecognition API is not supported in this browser.'));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const recognition = new SpeechRecognitionCtor();
|
|
57
|
+
this.recognition = recognition;
|
|
58
|
+
if (this.options.lang)
|
|
59
|
+
recognition.lang = this.options.lang;
|
|
60
|
+
recognition.continuous = this.options.continuous;
|
|
61
|
+
recognition.interimResults = this.options.interimResults;
|
|
62
|
+
recognition.maxAlternatives = this.options.maxAlternatives;
|
|
63
|
+
recognition.onstart = () => {
|
|
64
|
+
this._isListening = true;
|
|
65
|
+
callbacks?.onStart?.();
|
|
66
|
+
};
|
|
67
|
+
recognition.onresult = (event) => {
|
|
68
|
+
for (let i = event.resultIndex; i < event.results.length; i++) {
|
|
69
|
+
const result = event.results[i];
|
|
70
|
+
const alt = result[0];
|
|
71
|
+
callbacks?.onResult?.({
|
|
72
|
+
transcript: alt.transcript,
|
|
73
|
+
confidence: alt.confidence,
|
|
74
|
+
isFinal: result.isFinal,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
recognition.onerror = (event) => {
|
|
79
|
+
// 'no-speech' and 'aborted' are common non-fatal errors
|
|
80
|
+
const nonFatal = ['no-speech', 'aborted'];
|
|
81
|
+
if (!nonFatal.includes(event.error)) {
|
|
82
|
+
callbacks?.onError?.(new Error(`SpeechRecognition error: ${event.error} — ${event.message}`));
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
recognition.onend = () => {
|
|
86
|
+
this._isListening = false;
|
|
87
|
+
this.recognition = null;
|
|
88
|
+
callbacks?.onEnd?.();
|
|
89
|
+
};
|
|
90
|
+
recognition.start();
|
|
91
|
+
}
|
|
92
|
+
/** Stop recognition gracefully (delivers final result). */
|
|
93
|
+
stop() {
|
|
94
|
+
if (this.recognition) {
|
|
95
|
+
this.recognition.stop();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Abort recognition immediately (no final result). */
|
|
99
|
+
abort() {
|
|
100
|
+
if (this.recognition) {
|
|
101
|
+
this.recognition.abort();
|
|
102
|
+
this._isListening = false;
|
|
103
|
+
this.recognition = null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* One-shot recognition: starts listening and returns a Promise that resolves
|
|
108
|
+
* with the final transcript when speech ends.
|
|
109
|
+
*
|
|
110
|
+
* @param timeoutMs - Maximum wait time (default: 15000ms). Resolves with '' on timeout.
|
|
111
|
+
*/
|
|
112
|
+
listen(timeoutMs = 15000) {
|
|
113
|
+
return new Promise((resolve, reject) => {
|
|
114
|
+
let finalTranscript = '';
|
|
115
|
+
let timer = null;
|
|
116
|
+
const cleanup = () => {
|
|
117
|
+
if (timer) {
|
|
118
|
+
clearTimeout(timer);
|
|
119
|
+
timer = null;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
if (timeoutMs > 0) {
|
|
123
|
+
timer = setTimeout(() => {
|
|
124
|
+
this.stop();
|
|
125
|
+
// onEnd will fire and resolve with whatever we have
|
|
126
|
+
}, timeoutMs);
|
|
127
|
+
}
|
|
128
|
+
this.start({
|
|
129
|
+
onResult: (result) => {
|
|
130
|
+
if (result.isFinal) {
|
|
131
|
+
finalTranscript += result.transcript;
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
onEnd: () => {
|
|
135
|
+
cleanup();
|
|
136
|
+
resolve(finalTranscript);
|
|
137
|
+
},
|
|
138
|
+
onError: (err) => {
|
|
139
|
+
cleanup();
|
|
140
|
+
reject(err);
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** Update language at runtime (takes effect on next `start()`). */
|
|
146
|
+
setLang(lang) {
|
|
147
|
+
this.options.lang = lang;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.BrowserSpeechRecognizer = BrowserSpeechRecognizer;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for BrowserSpeechSynthesizer.
|
|
3
|
+
*/
|
|
4
|
+
export interface BrowserSpeechSynthesizerOptions {
|
|
5
|
+
/** BCP-47 language code (e.g. 'it-IT', 'en-US'). Default: browser default. */
|
|
6
|
+
lang?: string;
|
|
7
|
+
/** Speech rate (0.1 - 10). Default: `1`. */
|
|
8
|
+
rate?: number;
|
|
9
|
+
/** Pitch (0 - 2). Default: `1`. */
|
|
10
|
+
pitch?: number;
|
|
11
|
+
/** Volume (0 - 1). Default: `1`. */
|
|
12
|
+
volume?: number;
|
|
13
|
+
/** Preferred voice name (e.g. 'Google italiano'). If not found, uses first matching lang voice. */
|
|
14
|
+
voiceName?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Sentence delimiters used to split streaming text into speakable chunks.
|
|
17
|
+
* Default: `['.', '!', '?', '\n']`.
|
|
18
|
+
*/
|
|
19
|
+
sentenceDelimiters?: string[];
|
|
20
|
+
/**
|
|
21
|
+
* Minimum character count before flushing a sentence for speech.
|
|
22
|
+
* Prevents speaking very short fragments. Default: `20`.
|
|
23
|
+
*/
|
|
24
|
+
minCharsBeforeSpeak?: number;
|
|
25
|
+
/**
|
|
26
|
+
* When `true`, prefer browser-local voices (no network round-trip) over
|
|
27
|
+
* cloud/remote voices. Local voices synthesize instantly, eliminating
|
|
28
|
+
* the audible gap between sentences caused by network latency.
|
|
29
|
+
* Default: `true`.
|
|
30
|
+
*/
|
|
31
|
+
preferLocalVoice?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Callbacks emitted by BrowserSpeechSynthesizer.
|
|
35
|
+
*/
|
|
36
|
+
export interface BrowserSpeechSynthesizerCallbacks {
|
|
37
|
+
/** Called when a sentence starts being spoken. */
|
|
38
|
+
onSpeakStart?: (text: string) => void;
|
|
39
|
+
/** Called when a sentence finishes being spoken. */
|
|
40
|
+
onSpeakEnd?: (text: string) => void;
|
|
41
|
+
/** Called when all queued speech has been spoken. */
|
|
42
|
+
onQueueEmpty?: () => void;
|
|
43
|
+
/** Called on speech error. */
|
|
44
|
+
onError?: (error: Error) => void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Wraps the browser Web Speech API `SpeechSynthesis` for text-to-speech,
|
|
48
|
+
* with built-in sentence accumulation for streaming scenarios.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const synth = new BrowserSpeechSynthesizer({ lang: 'it-IT', rate: 1.1 });
|
|
53
|
+
*
|
|
54
|
+
* // Simple usage
|
|
55
|
+
* await synth.speak("Ciao, come stai?");
|
|
56
|
+
*
|
|
57
|
+
* // Streaming usage — feed text chunks as they arrive
|
|
58
|
+
* synth.feedChunk("Ciao, ");
|
|
59
|
+
* synth.feedChunk("come stai? ");
|
|
60
|
+
* synth.feedChunk("Tutto bene.");
|
|
61
|
+
* synth.flush(); // speak any remaining text
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare class BrowserSpeechSynthesizer {
|
|
65
|
+
private options;
|
|
66
|
+
private callbacks;
|
|
67
|
+
private buffer;
|
|
68
|
+
private _isSpeaking;
|
|
69
|
+
/** Number of utterances currently in the browser's native speech queue. */
|
|
70
|
+
private _pendingCount;
|
|
71
|
+
private resolvedVoice;
|
|
72
|
+
private voiceResolved;
|
|
73
|
+
private warmedUp;
|
|
74
|
+
/** Chrome keepalive: periodic pause/resume to prevent auto-pause after ~15s. */
|
|
75
|
+
private keepAliveTimer;
|
|
76
|
+
constructor(options?: BrowserSpeechSynthesizerOptions, callbacks?: BrowserSpeechSynthesizerCallbacks);
|
|
77
|
+
/** Whether speech is currently being synthesized. */
|
|
78
|
+
get isSpeaking(): boolean;
|
|
79
|
+
/** Returns `true` if the browser supports the Web Speech Synthesis API. */
|
|
80
|
+
static isSupported(): boolean;
|
|
81
|
+
/** Set event callbacks. */
|
|
82
|
+
setCallbacks(callbacks: BrowserSpeechSynthesizerCallbacks): void;
|
|
83
|
+
/** Update language at runtime. Resets the resolved voice cache. */
|
|
84
|
+
setLang(lang: string): void;
|
|
85
|
+
/**
|
|
86
|
+
* Get available voices. Due to browser async loading, you may need to call this
|
|
87
|
+
* after the `voiceschanged` event fires, or retry after a short delay.
|
|
88
|
+
*/
|
|
89
|
+
static getVoices(): SpeechSynthesisVoice[];
|
|
90
|
+
/**
|
|
91
|
+
* Speak a complete text. Returns a Promise that resolves when the utterance finishes.
|
|
92
|
+
*/
|
|
93
|
+
speak(text: string): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Feed a streaming text chunk. Text is accumulated until a sentence boundary is found,
|
|
96
|
+
* then the complete sentence is queued for speaking.
|
|
97
|
+
*/
|
|
98
|
+
feedChunk(chunk: string): void;
|
|
99
|
+
/**
|
|
100
|
+
* Flush any remaining buffered text (e.g. at end of stream) and queue it for speaking.
|
|
101
|
+
*/
|
|
102
|
+
flush(): void;
|
|
103
|
+
/**
|
|
104
|
+
* Flush and wait for all queued sentences to finish speaking.
|
|
105
|
+
* Returns a Promise that resolves when the queue is empty and speech is done.
|
|
106
|
+
*/
|
|
107
|
+
flushAndWait(): Promise<void>;
|
|
108
|
+
/** Cancel all speech and clear the queue. */
|
|
109
|
+
cancel(): void;
|
|
110
|
+
/**
|
|
111
|
+
* Pre-warm Chrome's TTS engine by speaking a zero-volume silent utterance.
|
|
112
|
+
* This primes the internal synthesis pipeline (voice loading, audio context
|
|
113
|
+
* creation, etc.) so that the first real utterance starts faster.
|
|
114
|
+
*/
|
|
115
|
+
private preWarm;
|
|
116
|
+
/** Pause speech. */
|
|
117
|
+
pause(): void;
|
|
118
|
+
/** Resume speech. */
|
|
119
|
+
resume(): void;
|
|
120
|
+
private extractAndQueueSentences;
|
|
121
|
+
/**
|
|
122
|
+
* Submit a sentence directly to the browser's native speech queue.
|
|
123
|
+
* The browser pre-processes queued utterances so the next one starts
|
|
124
|
+
* immediately when the current one finishes — no gap between sentences.
|
|
125
|
+
*/
|
|
126
|
+
private enqueue;
|
|
127
|
+
private createUtterance;
|
|
128
|
+
/**
|
|
129
|
+
* Normalize a BCP-47 lang code: lowercase and replace underscores with hyphens.
|
|
130
|
+
*/
|
|
131
|
+
private static normalizeLang;
|
|
132
|
+
private resolveVoice;
|
|
133
|
+
/**
|
|
134
|
+
* Chrome workaround: keeps speech synthesis alive.
|
|
135
|
+
* Chrome auto-pauses `speechSynthesis` after ~15 seconds of continuous speech.
|
|
136
|
+
* Calling `pause()` then `resume()` periodically resets this internal timer.
|
|
137
|
+
*/
|
|
138
|
+
private startKeepAlive;
|
|
139
|
+
/** Stop the Chrome keepalive timer. */
|
|
140
|
+
private stopKeepAlive;
|
|
141
|
+
/**
|
|
142
|
+
* Strip common Markdown formatting so TTS reads only the plain text.
|
|
143
|
+
* Handles: bold, italic, strikethrough, inline code, code blocks,
|
|
144
|
+
* headers, links, images, blockquotes, list markers, horizontal rules.
|
|
145
|
+
*/
|
|
146
|
+
static stripMarkdown(text: string): string;
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=BrowserSpeechSynthesizer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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