src/vs/workbench/contrib/chat/common/voiceChatService.ts
251 LOC · 244 covered · 7 uncovered · 20 ranges · 4 concepts · 4 introducers · 3 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
voiceChatService.ts ×11
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from '../../../../nls.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { Emitter, Event } from '../../../../base/common/event.js';
import { Disposable, DisposableStore } from '../../../../base/common/lifecycle.js';
import { rtrim } from '../../../../base/common/strings.js';
import { IContextKey, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
import { IChatAgentService } from './participants/chatAgents.js';
import { IChatModel } from './model/chatModel.js';
import { chatAgentLeader, chatSubcommandLeader } from './requestParser/chatParserTypes.js';
import { ISpeechService, ISpeechToTextEvent, SpeechToTextStatus } from '../../speech/common/speechService.js';
export const IVoiceChatService = createDecorator<IVoiceChatService>('voiceChatService');
export interface IVoiceChatSessionOptions {
readonly usesAgents?: boolean;
readonly model?: IChatModel;
}
export interface IVoiceChatService {
readonly _serviceBrand: undefined;
/**
* Similar to `ISpeechService.createSpeechToTextSession`, but with
* support for agent prefixes and command prefixes. For example,
* if the user says "at workspace slash fix this problem", the result
* will be "@workspace /fix this problem".
*/
createVoiceChatSession(token: CancellationToken, options: IVoiceChatSessionOptions): Promise<IVoiceChatSession>;
}
export interface IVoiceChatTextEvent extends ISpeechToTextEvent {
/**
* This property will be `true` when the text recognized
* so far only consists of agent prefixes (`@workspace`)
* and/or command prefixes (`@workspace /fix`).
*/
readonly waitingForInput?: boolean;
}
export interface IVoiceChatSession {
readonly onDidChange: Event<IVoiceChatTextEvent>;
}
interface IPhraseValue {
readonly agent: string;
readonly command?: string;
}
enum PhraseTextType {
AGENT = 1,
COMMAND = 2,
AGENT_AND_COMMAND = 3
}
export const VoiceChatInProgress = new RawContextKey<boolean>('voiceChatInProgress', false, { type: 'boolean', description: localize('voiceChatInProgress', "A speech-to-text session is in progress for chat.") });
export class VoiceChatService extends Disposable implements IVoiceChatService {
readonly _serviceBrand: undefined;
private static readonly AGENT_PREFIX = chatAgentLeader;
private static readonly COMMAND_PREFIX = chatSubcommandLeader;
private static readonly PHRASES_LOWER = {
[this.AGENT_PREFIX]: 'at',
[this.COMMAND_PREFIX]: 'slash'
};
private static readonly PHRASES_UPPER = {
[this.AGENT_PREFIX]: 'At',
[this.COMMAND_PREFIX]: 'Slash'
};
private static readonly CHAT_AGENT_ALIAS = new Map<string, string>([['vscode', 'code']]);
private readonly voiceChatInProgress: IContextKey<boolean>;
private activeVoiceChatSessions = 0;
constructor(
@ISpeechService private readonly speechService: ISpeechService,
@IChatAgentService private readonly chatAgentService: IChatAgentService,
@IContextKeyService contextKeyService: IContextKeyService
) {
super();
this.voiceChatInProgress = VoiceChatInProgress.bindTo(contextKeyService);
}
private createPhrases(model?: IChatModel): Map<string, IPhraseValue> {
const phrases = new Map<string, IPhraseValue>();
for (const agent of this.chatAgentService.getActivatedAgents()) {
const agentPhrase = `${VoiceChatService.PHRASES_LOWER[VoiceChatService.AGENT_PREFIX]} ${VoiceChatService.CHAT_AGENT_ALIAS.get(agent.name) ?? agent.name}`.toLowerCase();
phrases.set(agentPhrase, { agent: agent.name });
for (const slashCommand of agent.slashCommands) {
const slashCommandPhrase = `${VoiceChatService.PHRASES_LOWER[VoiceChatService.COMMAND_PREFIX]} ${slashCommand.name}`.toLowerCase();
phrases.set(slashCommandPhrase, { agent: agent.name, command: slashCommand.name });
const agentSlashCommandPhrase = `${agentPhrase} ${slashCommandPhrase}`.toLowerCase();
phrases.set(agentSlashCommandPhrase, { agent: agent.name, command: slashCommand.name });
}
}
return phrases;
}
private toText(value: IPhraseValue, type: PhraseTextType): string {
switch (type) {
case PhraseTextType.AGENT:
return `${VoiceChatService.AGENT_PREFIX}${value.agent} ${VoiceChatService.COMMAND_PREFIX}${value.command}`;
voiceChatService.ts ×4
}
async createVoiceChatSession(token: CancellationToken, options: IVoiceChatSessionOptions): Promise<IVoiceChatSession> {
const disposables = new DisposableStore();
const onSessionStoppedOrCanceled = (dispose: boolean) => {
this.activeVoiceChatSessions = Math.max(0, this.activeVoiceChatSessions - 1);
if (this.activeVoiceChatSessions === 0) {
this.voiceChatInProgress.reset();
}
if (dispose) {
disposables.dispose();
}
};
disposables.add(token.onCancellationRequested(() => onSessionStoppedOrCanceled(true)));
let detectedAgent = false;
let detectedSlashCommand = false;
const emitter = disposables.add(new Emitter<IVoiceChatTextEvent>());
const session = await this.speechService.createSpeechToTextSession(token, 'chat');
if (token.isCancellationRequested) {
onSessionStoppedOrCanceled(true);
}
const phrases = this.createPhrases(options.model);
disposables.add(session.onDidChange(e => {
switch (e.status) {
case SpeechToTextStatus.Recognizing:
case SpeechToTextStatus.Recognized: {
let massagedEvent: IVoiceChatTextEvent = e;
if (e.text) {
const startsWithAgent = e.text.startsWith(VoiceChatService.PHRASES_UPPER[VoiceChatService.AGENT_PREFIX]) || e.text.startsWith(VoiceChatService.PHRASES_LOWER[VoiceChatService.AGENT_PREFIX]);
const startsWithSlashCommand = e.text.startsWith(VoiceChatService.PHRASES_UPPER[VoiceChatService.COMMAND_PREFIX]) || e.text.startsWith(VoiceChatService.PHRASES_LOWER[VoiceChatService.COMMAND_PREFIX]);
if (startsWithAgent || startsWithSlashCommand) {
const originalWords = e.text.split(' ');
let transformedWords: string[] | undefined;
let waitingForInput = false;
// Check for agent + slash command
if (options.usesAgents && startsWithAgent && !detectedAgent && !detectedSlashCommand && originalWords.length >= 4) {
const phrase = phrases.get(originalWords.slice(0, 4).map(word => this.normalizeWord(word)).join(' '));
voiceChatService.ts ×4
if (phrase) {
transformedWords = [this.toText(phrase, PhraseTextType.AGENT_AND_COMMAND), ...originalWords.slice(4)];
waitingForInput = originalWords.length === 4;
if (e.status === SpeechToTextStatus.Recognized) {
detectedAgent = true;
detectedSlashCommand = true;
}
}
}
// Check for agent (if not done already)
if (options.usesAgents && startsWithAgent && !detectedAgent && !transformedWords && originalWords.length >= 2) {
const phrase = phrases.get(originalWords.slice(0, 2).map(word => this.normalizeWord(word)).join(' '));
voiceChatService.ts ×4
if (phrase) {
transformedWords = [this.toText(phrase, PhraseTextType.AGENT), ...originalWords.slice(2)];
waitingForInput = originalWords.length === 2;
if (e.status === SpeechToTextStatus.Recognized) {
detectedAgent = true;
}
}
}
// Check for slash command (if not done already)
if (startsWithSlashCommand && !detectedSlashCommand && !transformedWords && originalWords.length >= 2) {
const phrase = phrases.get(originalWords.slice(0, 2).map(word => this.normalizeWord(word)).join(' '));
voiceChatService.ts ×4
if (phrase) {
transformedWords = [this.toText(phrase, options.usesAgents && !detectedAgent ?
PhraseTextType.AGENT_AND_COMMAND : // rewrite `/fix` to `@workspace /foo` in this case
voiceChatService.ts ×1
PhraseTextType.COMMAND // when we have not yet detected an agent before
voiceChatService.ts ×4
), ...originalWords.slice(2)];
waitingForInput = originalWords.length === 2;
if (e.status === SpeechToTextStatus.Recognized) {
detectedSlashCommand = true;
}
}
}
massagedEvent = {
status: e.status,
text: (transformedWords ?? originalWords).join(' '),
waitingForInput
};
}
}
emitter.fire(massagedEvent);
break;
}
case SpeechToTextStatus.Started:
this.voiceChatInProgress.set(true);
emitter.fire(e);
break;
onSessionStoppedOrCanceled(false);
emitter.fire(e);
break;
emitter.fire(e);
break;
}));
return {
onDidChange: emitter.event
};
}
private normalizeWord(word: string): string {
word = rtrim(word, '.');
word = rtrim(word, ',');
word = rtrim(word, '?');
return word.toLowerCase();
}
}