aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/DictationManager.ts
blob: 717473aa1edb2635b42cc698544b742a4ffe7d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import * as interpreter from 'words-to-numbers';
// @ts-ignore  bcz: how are you supposed to include these definitions since dom-speech-recognition isn't a module?
import type {} from '@types/dom-speech-recognition';
import { Doc, Opt } from '../../fields/Doc';
import { List } from '../../fields/List';
import { RichTextField } from '../../fields/RichTextField';
import { listSpec } from '../../fields/Schema';
import { Cast, CastCtor, DocCast } from '../../fields/Types';
import { AudioField, ImageField } from '../../fields/URLField';
import { Utils } from '../../Utils';
import { Docs } from '../documents/Documents';
import { DocumentType } from '../documents/DocumentTypes';
import { DictationOverlay } from '../views/DictationOverlay';
import { DocumentView, OpenWhere, OpenWhereMod } from '../views/nodes/DocumentView';
import { SelectionManager } from './SelectionManager';
import { UndoManager } from './UndoManager';

/**
 * This namespace provides a singleton instance of a manager that
 * handles the listening and text-conversion of user speech.
 *
 * The basic manager functionality can be attained by the DictationManager.Controls namespace, which provide
 * a simple recording operation that returns the interpreted text as a string.
 *
 * Additionally, however, the DictationManager also exposes the ability to execute voice commands within Dash.
 * It stores a default library of registered commands that can be triggered by listen()'ing for a phrase and then
 * passing the results into the execute() function.
 *
 * In addition to compile-time default commands, you can invoke DictationManager.Commands.Register(Independent|Dependent)
 * to add new commands as classes or components are constructed.
 */
export namespace DictationManager {
    /**
     * Some type maneuvering to access Webkit's built-in
     * speech recognizer.
     */
    namespace CORE {
        export interface IWindow extends Window {
            webkitSpeechRecognition: any;
        }
    }
    const { webkitSpeechRecognition }: CORE.IWindow = window as any as CORE.IWindow;
    export const placeholder = 'Listening...';

    export namespace Controls {
        export const Infringed = 'unable to process: dictation manager still involved in previous session';
        const browser = (() => {
            const identifier = navigator.userAgent.toLowerCase();
            if (identifier.indexOf('safari') >= 0) {
                return 'Safari';
            }
            if (identifier.indexOf('chrome') >= 0) {
                return 'Chrome';
            }
            if (identifier.indexOf('firefox') >= 0) {
                return 'Firefox';
            }
            return 'Unidentified Browser';
        })();
        const unsupported = `listening is not supported in ${browser}`;
        const intraSession = '. ';
        const interSession = ' ... ';

        export let isListening = false;
        let isManuallyStopped = false;

        let current: string | undefined = undefined;
        let sessionResults: string[] = [];

        const recognizer: Opt<SpeechRecognition> = webkitSpeechRecognition ? new webkitSpeechRecognition() : undefined;

        export type InterimResultHandler = (results: string) => any;
        export type ContinuityArgs = { indefinite: boolean } | false;
        export type DelimiterArgs = { inter: string; intra: string };
        export type ListeningUIStatus = { interim: boolean } | false;

        export interface ListeningOptions {
            useOverlay: boolean;
            language: string;
            continuous: ContinuityArgs;
            delimiters: DelimiterArgs;
            interimHandler: InterimResultHandler;
            tryExecute: boolean;
            terminators: string[];
        }

        let pendingListen: Promise<string> | string | undefined;

        export const listen = async (options?: Partial<ListeningOptions>) => {
            if (pendingListen instanceof Promise) return pendingListen.then(pl => innerListen(options));
            return innerListen(options);
        };
        const innerListen = async (options?: Partial<ListeningOptions>) => {
            let results: string | undefined;

            const overlay = options?.useOverlay;
            if (overlay) {
                DictationOverlay.Instance.dictationOverlayVisible = true;
                DictationOverlay.Instance.isListening = { interim: false };
            }

            try {
                results = await (pendingListen = listenImpl(options));
                pendingListen = undefined;
                if (results) {
                    Utils.CopyText(results);
                    if (overlay) {
                        DictationOverlay.Instance.isListening = false;
                        const execute = options?.tryExecute;
                        DictationOverlay.Instance.dictatedPhrase = execute ? results.toLowerCase() : results;
                        DictationOverlay.Instance.dictationSuccess = execute ? await DictationManager.Commands.execute(results) : true;
                    }
                    options?.tryExecute && (await DictationManager.Commands.execute(results));
                }
            } catch (e: any) {
                console.log(e);
                if (overlay) {
                    DictationOverlay.Instance.isListening = false;
                    DictationOverlay.Instance.dictatedPhrase = results = `dictation error: ${'error' in e ? e.error : 'unknown error'}`;
                    DictationOverlay.Instance.dictationSuccess = false;
                }
            } finally {
                overlay && DictationOverlay.Instance.initiateDictationFade();
            }

            return results;
        };

        const listenImpl = (options?: Partial<ListeningOptions>) => {
            if (!recognizer) {
                console.log('DictationManager:' + unsupported);
                return unsupported;
            }
            if (isListening) {
                return Infringed;
            }
            isListening = true;

            const handler = options?.interimHandler;
            const continuous = options?.continuous;
            const indefinite = continuous && continuous.indefinite;
            const language = options?.language;
            const intra = options?.delimiters?.intra;
            const inter = options?.delimiters?.inter;

            recognizer.onstart = () => console.log('initiating speech recognition session...');
            recognizer.interimResults = handler !== undefined;
            recognizer.continuous = continuous === undefined ? false : continuous !== false;
            recognizer.lang = language === undefined ? 'en-US' : language;

            recognizer.start();

            return new Promise<string>((resolve, reject) => {
                recognizer.onerror = (e: any) => {
                    // e is SpeechRecognitionError but where is that defined?
                    if (!(indefinite && e.error === 'no-speech')) {
                        recognizer.stop();
                        resolve(e);
                        //reject(e);
                    }
                };

                recognizer.onresult = (e: SpeechRecognitionEvent) => {
                    current = synthesize(e, intra);
                    let matchedTerminator: string | undefined;
                    if (options?.terminators && (matchedTerminator = options.terminators.find(end => (current ? current.trim().toLowerCase().endsWith(end.toLowerCase()) : false)))) {
                        current = matchedTerminator;
                        recognizer.abort();
                        return complete();
                    }
                    !isManuallyStopped && handler?.(current);
                    //isManuallyStopped && complete();
                };

                recognizer.onend = (e: Event) => {
                    if (!indefinite || isManuallyStopped) {
                        return complete();
                    }

                    if (current) {
                        !isManuallyStopped && sessionResults.push(current);
                        current = undefined;
                    }
                    recognizer.start();
                };

                const complete = () => {
                    if (indefinite) {
                        current && sessionResults.push(current);
                        sessionResults.length && resolve(sessionResults.join(inter || interSession));
                    } else {
                        resolve(current || '');
                    }
                    current = undefined;
                    sessionResults = [];
                    isListening = false;
                    isManuallyStopped = false;
                    recognizer.onresult = null;
                    recognizer.onerror = null;
                    recognizer.onend = null;
                };
            });
        };

        export const stop = (salvageSession = true) => {
            if (!isListening || !recognizer) {
                return;
            }
            isListening = false;
            isManuallyStopped = true;
            recognizer.stop(); // salvageSession ? recognizer.stop() : recognizer.abort();
        };

        const synthesize = (e: SpeechRecognitionEvent, delimiter?: string) => {
            const results = e.results;
            const transcripts: string[] = [];
            for (let i = 0; i < results.length; i++) {
                transcripts.push(results.item(i).item(0).transcript.trim());
            }
            return transcripts.join(delimiter || intraSession);
        };
    }

    export namespace Commands {
        export const dictationFadeDuration = 2000;

        export type IndependentAction = (target: DocumentView) => any | Promise<any>;
        export type IndependentEntry = { action: IndependentAction; restrictTo?: DocumentType[] };

        export type DependentAction = (target: DocumentView, matches: RegExpExecArray) => any | Promise<any>;
        export type DependentEntry = { expression: RegExp; action: DependentAction; restrictTo?: DocumentType[] };

        export const RegisterIndependent = (key: string, value: IndependentEntry) => Independent.set(key, value);
        export const RegisterDependent = (entry: DependentEntry) => Dependent.push(entry);

        export const execute = async (phrase: string) => {
            return UndoManager.RunInBatch(async () => {
                console.log('PHRASE: ' + phrase);
                const targets = SelectionManager.Views();
                if (!targets || !targets.length) {
                    return;
                }

                phrase = phrase.toLowerCase();
                const entry = Independent.get(phrase);

                if (entry) {
                    let success = false;
                    const restrictTo = entry.restrictTo;
                    for (const target of targets) {
                        if (!restrictTo || validate(target, restrictTo)) {
                            await entry.action(target);
                            success = true;
                        }
                    }
                    return success;
                }

                for (const entry of Dependent) {
                    const regex = entry.expression;
                    const matches = regex.exec(phrase);
                    regex.lastIndex = 0;
                    if (matches !== null) {
                        let success = false;
                        const restrictTo = entry.restrictTo;
                        for (const target of targets) {
                            if (!restrictTo || validate(target, restrictTo)) {
                                await entry.action(target, matches);
                                success = true;
                            }
                        }
                        return success;
                    }
                }

                return false;
            }, 'Execute Command');
        };

        const ConstructorMap = new Map<DocumentType, CastCtor>([
            [DocumentType.COL, listSpec(Doc)],
            [DocumentType.AUDIO, AudioField],
            [DocumentType.IMG, ImageField],
            [DocumentType.IMPORT, listSpec(Doc)],
            [DocumentType.RTF, 'string'],
        ]);

        const tryCast = (view: DocumentView, type: DocumentType) => {
            const ctor = ConstructorMap.get(type);
            if (!ctor) {
                return false;
            }
            return Cast(Doc.GetProto(view.props.Document).data, ctor) !== undefined;
        };

        const validate = (target: DocumentView, types: DocumentType[]) => {
            for (const type of types) {
                if (tryCast(target, type)) {
                    return true;
                }
            }
            return false;
        };

        const interpretNumber = (number: string) => {
            const initial = parseInt(number);
            if (!isNaN(initial)) {
                return initial;
            }
            const converted = interpreter.wordsToNumbers(number, { fuzzy: true });
            if (converted === null) {
                return NaN;
            }
            return typeof converted === 'string' ? parseInt(converted) : converted;
        };

        const Independent = new Map<string, IndependentEntry>([
            [
                'clear',
                {
                    action: (target: DocumentView) => (Doc.GetProto(target.props.Document).data = new List()),
                    restrictTo: [DocumentType.COL],
                },
            ],

            [
                'new outline',
                {
                    action: (target: DocumentView) => {
                        const newBox = Docs.Create.TextDocument('', { _width: 400, _height: 200, title: 'My Outline', _layout_autoHeight: true });
                        const proto = DocCast(newBox.proto);
                        const prompt = 'Press alt + r to start dictating here...';
                        const head = 3;
                        const anchor = head + prompt.length;
                        const proseMirrorState = `{"doc":{"type":"doc","content":[{"type":"ordered_list","content":[{"type":"list_item","content":[{"type":"paragraph","content":[{"type":"text","text":"${prompt}"}]}]}]}]},"selection":{"type":"text","anchor":${anchor},"head":${head}}}`;
                        proto.data = new RichTextField(proseMirrorState);
                        proto.backgroundColor = '#eeffff';
                        target.props.addDocTab(newBox, OpenWhere.addRight);
                    },
                },
            ],
        ]);

        const Dependent = new Array<DependentEntry>(
            {
                expression: /create (\w+) documents of type (image|nested collection)/g,
                action: (target: DocumentView, matches: RegExpExecArray) => {
                    const count = interpretNumber(matches[1]);
                    const what = matches[2];
                    const dataDoc = Doc.GetProto(target.props.Document);
                    const fieldKey = 'data';
                    if (isNaN(count)) {
                        return;
                    }
                    for (let i = 0; i < count; i++) {
                        let created: Doc | undefined;
                        switch (what) {
                            case 'image':
                                created = Docs.Create.ImageDocument('https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg');
                                break;
                            case 'nested collection':
                                created = Docs.Create.FreeformDocument([], {});
                                break;
                        }
                        created && Doc.AddDocToList(dataDoc, fieldKey, created);
                    }
                },
                restrictTo: [DocumentType.COL],
            },

            {
                expression: /view as (freeform|stacking|masonry|schema|tree)/g,
                action: (target: DocumentView, matches: RegExpExecArray) => {
                    const mode = matches[1];
                    mode && (target.props.Document._type_collection = mode);
                },
                restrictTo: [DocumentType.COL],
            }
        );
    }
}