Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature added: Integrated WebView with Native Speech and PiperTTS #3695

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
// Pass a directory to manually test in
"${workspaceFolder}/manual-testing-sandbox",
"${workspaceFolder}/manual-testing-sandbox/test.js",
"--extensionDevelopmentPath=${workspaceFolder}/extensions/vscode"
"--extensionDevelopmentPath=${workspaceFolder}/extensions/vscode",
"--disable-extensions"
],
"pauseForSourceMap": false,
"outFiles": ["${workspaceFolder}/extensions/vscode/out/extension.js"],
Expand Down
2 changes: 1 addition & 1 deletion core/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ declare global {
/**
* Automatically read LLM chat responses aloud using system TTS models
*/
readResponseTTS?: boolean;
readResponseTTS?: {type:string,lang?:string,flush?:boolean};

/**
* If set to true, we will attempt to pull down and install an instance of Chromium
Expand Down
18 changes: 14 additions & 4 deletions core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
} from "./util/paths";
import { Telemetry } from "./util/posthog";
import { getSymbolsForManyFiles } from "./util/treeSitter";
import { TTS } from "./util/tts";
import { sanitizeMessageForTTS, TTS } from "./util/tts";

import { type ContextItemId, type IDE, type IndexingProgressUpdate } from ".";
import type { FromCoreProtocol, ToCoreProtocol } from "./protocol";
Expand Down Expand Up @@ -392,7 +392,8 @@ export class Core {
}

// Stop TTS on new StreamChat
if (config.experimental?.readResponseTTS) {

if (config.experimental?.readResponseTTS?.type==="native") {
void TTS.kill();
}

Expand Down Expand Up @@ -426,10 +427,19 @@ export class Core {
next = await gen.next();
}

if (config.experimental?.readResponseTTS && "completion" in next.value) {
void TTS.read(next.value?.completion);
if("completion" in next.value){
let message=next.value?.completion;
const lang=config.experimental?.readResponseTTS?.lang??"en-US";
const flush=config.experimental?.readResponseTTS?.flush??false;
if (config.experimental?.readResponseTTS?.type==="native" ) {
void TTS.read(message,lang);
}else if (config.experimental?.readResponseTTS?.type==="piperTTS" ) {
message = sanitizeMessageForTTS(message);
void TTS.messenger.request("setPiperTTS",{lang,message,flush});
}
}


void Telemetry.capture(
"chat",
{
Expand Down
2 changes: 1 addition & 1 deletion core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ export interface ExperimentalConfig {
/**
* Automatically read LLM chat responses aloud using system TTS models
*/
readResponseTTS?: boolean;
readResponseTTS?: {type:string,lang?:string,flush?:boolean};

/**
* If set to true, we will attempt to pull down and install an instance of Chromium
Expand Down
2 changes: 2 additions & 0 deletions core/protocol/passThrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export const CORE_TO_WEBVIEW_PASS_THROUGH: (keyof ToWebviewFromCoreProtocol)[] =
"isContinueInputFocused",
"didChangeAvailableProfiles",
"setTTSActive",
"setTTSNative",
"setPiperTTS",
"getWebviewHistoryLength",
"getCurrentSessionId",
"signInToControlPlane",
Expand Down
2 changes: 2 additions & 0 deletions core/protocol/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type ToWebviewFromIdeOrCoreProtocol = {
void,
];
setTTSActive: [boolean, void];
setTTSNative:[{lang:string,message:string}, boolean];
setPiperTTS:[{lang:string,message:string,flush:boolean}, void];
getWebviewHistoryLength: [undefined, number];
getCurrentSessionId: [undefined, string];
signInToControlPlane: [undefined, void];
Expand Down
72 changes: 37 additions & 35 deletions core/util/tts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,47 @@ export class TTS {
static handle: ChildProcess | undefined = undefined;
static messenger: IMessenger<ToCoreProtocol, FromCoreProtocol>;

static async read(message: string) {
static async read(message: string,lang: string) {
message = sanitizeMessageForTTS(message);

try {
// Kill any active TTS processes
await TTS.kill();
} catch (e) {
console.warn("Error killing TTS process: ", e);
return;
}

switch (TTS.os) {
case "darwin":
TTS.handle = exec(`say "${message}"`);
break;
case "win32":
// Replace single quotes on windows
TTS.handle = exec(
`powershell -Command "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('${message.replace(
/'/g,
"''",
)}')"`,
);
break;
case "linux":
TTS.handle = exec(`espeak "${message}"`);
break;
default:
console.log(
"Text-to-speech is not supported on this operating system.",
);
const native = await TTS.messenger.request("setTTSNative",{lang,message});
if(!native){
try {
// Kill any active TTS processes
await TTS.kill();
} catch (e) {
console.warn("Error killing TTS process: ", e);
return;
}
}

switch (TTS.os) {
case "darwin":
TTS.handle = exec(`say "${message}"`);
break;
case "win32":
// Replace single quotes on windows
TTS.handle = exec(
`powershell -Command "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('${message.replace(
/'/g,
"''",
)}')"`,
);
break;
case "linux":
TTS.handle = exec(`espeak "${message}"`);
break;
default:
console.log(
"Text-to-speech is not supported on this operating system.",
);
return;
}

void TTS.messenger.request("setTTSActive", true);
void TTS.messenger.request("setTTSActive", true);

TTS.handle?.once("exit", () => {
void TTS.messenger.request("setTTSActive", false);
});
TTS.handle?.once("exit", () => {
void TTS.messenger.request("setTTSActive", false);
});
}
}

static async kill(): Promise<void> {
Expand Down
19 changes: 16 additions & 3 deletions extensions/vscode/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3064,9 +3064,22 @@
}
},
"readResponseTTS": {
"type": "boolean",
"default": true,
"description": "Automatically read LLM chat responses aloud using system TTS models"
"type": "object",
"description": "Automatically read LLM chat responses aloud using system TTS models",
"properties": {
"type": {
"type": "string",
"default": "native"
},
"lang": {
"type": "string",
"default": "en-US"
},
"flush": {
"type": "boolean",
"default": false
}
}
},
"promptPath": {
"type": "string"
Expand Down
4 changes: 2 additions & 2 deletions extensions/vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading