|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { RunOnceScheduler } from 'vs/base/common/async'; |
| 7 | +import { CancellationTokenSource } from 'vs/base/common/cancellation'; |
| 8 | +import { Disposable, DisposableStore } from 'vs/base/common/lifecycle'; |
| 9 | +import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; |
| 10 | +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; |
| 11 | +import { AccessibilityVoiceSettingId, SpeechTimeoutDefault } from 'vs/workbench/contrib/accessibility/browser/accessibilityConfiguration'; |
| 12 | +import { ISpeechService, ISpeechToTextEvent, SpeechToTextStatus } from 'vs/workbench/contrib/speech/common/speechService'; |
| 13 | +import { ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal'; |
| 14 | +import { isNumber } from 'vs/base/common/types'; |
| 15 | +import type { IDecoration } from '@xterm/xterm'; |
| 16 | +import { IXtermMarker } from 'vs/platform/terminal/common/capabilities/capabilities'; |
| 17 | +import { ThemeIcon } from 'vs/base/common/themables'; |
| 18 | +import { Codicon } from 'vs/base/common/codicons'; |
| 19 | + |
| 20 | +const symbolMap: { [key: string]: string } = { |
| 21 | + 'Ampersand': '&', |
| 22 | + 'ampersand': '&', |
| 23 | + 'Dollar': '$', |
| 24 | + 'dollar': '$', |
| 25 | + 'Percent': '%', |
| 26 | + 'percent': '%', |
| 27 | + 'Asterisk': '*', |
| 28 | + 'asterisk': '*', |
| 29 | + 'Plus': '+', |
| 30 | + 'plus': '+', |
| 31 | + 'Equals': '=', |
| 32 | + 'equals': '=', |
| 33 | + 'Exclamation': '!', |
| 34 | + 'exclamation': '!', |
| 35 | + 'Slash': '/', |
| 36 | + 'slash': '/', |
| 37 | + 'Backslash': '\\', |
| 38 | + 'backslash': '\\', |
| 39 | + 'Dot': '.', |
| 40 | + 'dot': '.', |
| 41 | + 'Period': '.', |
| 42 | + 'period': '.', |
| 43 | + 'Quote': '\'', |
| 44 | + 'quote': '\'', |
| 45 | + 'double quote': '"', |
| 46 | + 'Double quote': '"', |
| 47 | +}; |
| 48 | + |
| 49 | +export class TerminalSpeechToTextSession extends Disposable { |
| 50 | + private _input: string = ''; |
| 51 | + private _ghostText: IDecoration | undefined; |
| 52 | + private _decoration: IDecoration | undefined; |
| 53 | + private _marker: IXtermMarker | undefined; |
| 54 | + private _ghostTextMarker: IXtermMarker | undefined; |
| 55 | + private static _instance: TerminalSpeechToTextSession | undefined = undefined; |
| 56 | + private _acceptTranscriptionScheduler: RunOnceScheduler | undefined; |
| 57 | + static getInstance(instantiationService: IInstantiationService): TerminalSpeechToTextSession { |
| 58 | + if (!TerminalSpeechToTextSession._instance) { |
| 59 | + TerminalSpeechToTextSession._instance = instantiationService.createInstance(TerminalSpeechToTextSession); |
| 60 | + } |
| 61 | + |
| 62 | + return TerminalSpeechToTextSession._instance; |
| 63 | + } |
| 64 | + private _cancellationTokenSource: CancellationTokenSource | undefined; |
| 65 | + private readonly _disposables: DisposableStore; |
| 66 | + constructor( |
| 67 | + @ISpeechService private readonly _speechService: ISpeechService, |
| 68 | + @ITerminalService readonly _terminalService: ITerminalService, |
| 69 | + @IConfigurationService readonly configurationService: IConfigurationService, |
| 70 | + @IInstantiationService readonly _instantationService: IInstantiationService |
| 71 | + ) { |
| 72 | + super(); |
| 73 | + this._register(this._terminalService.onDidChangeActiveInstance(() => this.stop())); |
| 74 | + this._register(this._terminalService.onDidDisposeInstance(() => this.stop())); |
| 75 | + this._disposables = this._register(new DisposableStore()); |
| 76 | + } |
| 77 | + |
| 78 | + start(): void { |
| 79 | + this.stop(); |
| 80 | + let voiceTimeout = this.configurationService.getValue<number>(AccessibilityVoiceSettingId.SpeechTimeout); |
| 81 | + if (!isNumber(voiceTimeout) || voiceTimeout < 0) { |
| 82 | + voiceTimeout = SpeechTimeoutDefault; |
| 83 | + } |
| 84 | + this._acceptTranscriptionScheduler = this._disposables.add(new RunOnceScheduler(() => { |
| 85 | + this._terminalService.activeInstance?.sendText(this._input, false); |
| 86 | + this.stop(); |
| 87 | + }, voiceTimeout)); |
| 88 | + this._cancellationTokenSource = this._register(new CancellationTokenSource()); |
| 89 | + const session = this._disposables.add(this._speechService.createSpeechToTextSession(this._cancellationTokenSource!.token)); |
| 90 | + |
| 91 | + this._disposables.add(session.onDidChange((e) => { |
| 92 | + if (this._cancellationTokenSource?.token.isCancellationRequested) { |
| 93 | + return; |
| 94 | + } |
| 95 | + switch (e.status) { |
| 96 | + case SpeechToTextStatus.Started: |
| 97 | + // TODO: play start audio cue |
| 98 | + if (!this._decoration) { |
| 99 | + this._createDecoration(); |
| 100 | + } |
| 101 | + break; |
| 102 | + case SpeechToTextStatus.Recognizing: { |
| 103 | + this._updateInput(e); |
| 104 | + this._renderGhostText(e); |
| 105 | + if (voiceTimeout > 0) { |
| 106 | + this._acceptTranscriptionScheduler!.cancel(); |
| 107 | + } |
| 108 | + break; |
| 109 | + } |
| 110 | + case SpeechToTextStatus.Recognized: |
| 111 | + this._updateInput(e); |
| 112 | + if (voiceTimeout > 0) { |
| 113 | + this._acceptTranscriptionScheduler!.schedule(); |
| 114 | + } |
| 115 | + break; |
| 116 | + case SpeechToTextStatus.Stopped: |
| 117 | + // TODO: play stop audio cue |
| 118 | + this.stop(); |
| 119 | + break; |
| 120 | + } |
| 121 | + })); |
| 122 | + } |
| 123 | + stop(send?: boolean): void { |
| 124 | + this._setInactive(); |
| 125 | + if (send) { |
| 126 | + this._acceptTranscriptionScheduler!.cancel(); |
| 127 | + this._terminalService.activeInstance?.sendText(this._input, false); |
| 128 | + } |
| 129 | + this._marker?.dispose(); |
| 130 | + this._ghostTextMarker?.dispose(); |
| 131 | + this._ghostText?.dispose(); |
| 132 | + this._ghostText = undefined; |
| 133 | + this._decoration?.dispose(); |
| 134 | + this._decoration = undefined; |
| 135 | + this._cancellationTokenSource?.cancel(); |
| 136 | + this._disposables.clear(); |
| 137 | + this._input = ''; |
| 138 | + } |
| 139 | + |
| 140 | + private _updateInput(e: ISpeechToTextEvent): void { |
| 141 | + if (e.text) { |
| 142 | + let input = e.text.replaceAll(/[.,?;!]/g, ''); |
| 143 | + for (const symbol of Object.entries(symbolMap)) { |
| 144 | + input = input.replace(new RegExp('\\b' + symbol[0] + '\\b'), symbol[1]); |
| 145 | + } |
| 146 | + this._input = ' ' + input; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private _createDecoration(): void { |
| 151 | + const activeInstance = this._terminalService.activeInstance; |
| 152 | + const xterm = activeInstance?.xterm?.raw; |
| 153 | + if (!xterm) { |
| 154 | + return; |
| 155 | + } |
| 156 | + this._marker = activeInstance.registerMarker(-1); |
| 157 | + if (!this._marker) { |
| 158 | + return; |
| 159 | + } |
| 160 | + this._decoration = xterm.registerDecoration({ |
| 161 | + marker: this._marker, |
| 162 | + layer: 'top', |
| 163 | + x: xterm.buffer.active.cursorX ?? 0, |
| 164 | + }); |
| 165 | + this._decoration?.onRender((e: HTMLElement) => { |
| 166 | + e.classList.add(...ThemeIcon.asClassNameArray(Codicon.micFilled), 'terminal-speech-to-text', 'recording'); |
| 167 | + e.style.transform = 'translate(-5px, -5px)'; |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + private _setInactive(): void { |
| 172 | + this._decoration?.element?.classList.remove('recording'); |
| 173 | + } |
| 174 | + |
| 175 | + private _renderGhostText(e: ISpeechToTextEvent): void { |
| 176 | + this._ghostText?.dispose(); |
| 177 | + const text = e.text; |
| 178 | + if (!text) { |
| 179 | + return; |
| 180 | + } |
| 181 | + const activeInstance = this._terminalService.activeInstance; |
| 182 | + const xterm = activeInstance?.xterm?.raw; |
| 183 | + if (!xterm) { |
| 184 | + return; |
| 185 | + } |
| 186 | + this._ghostTextMarker = activeInstance.registerMarker(); |
| 187 | + if (!this._ghostTextMarker) { |
| 188 | + return; |
| 189 | + } |
| 190 | + this._ghostText = xterm.registerDecoration({ |
| 191 | + marker: this._ghostTextMarker, |
| 192 | + layer: 'top', |
| 193 | + x: xterm.buffer.active.cursorX + 1 ?? 0, |
| 194 | + }); |
| 195 | + this._ghostText?.onRender((e: HTMLElement) => { |
| 196 | + e.classList.add('terminal-speech-progress-text'); |
| 197 | + e.textContent = text; |
| 198 | + e.style.width = 'fit-content'; |
| 199 | + }); |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | + |
0 commit comments