initial TTS functionality
This commit is contained in:
parent
2f1da9bb33
commit
88b56763a3
4 changed files with 123 additions and 0 deletions
|
|
@ -10,6 +10,7 @@ import {
|
|||
import ContinueProxyEmbeddingsProvider from "../../indexing/embeddings/ContinueProxyEmbeddingsProvider.js";
|
||||
import ContinueProxy from "../../llm/llms/stubs/ContinueProxy.js";
|
||||
import { Telemetry } from "../../util/posthog.js";
|
||||
import { TTS } from "../../util/tts.js";
|
||||
import { loadFullConfigNode } from "../load.js";
|
||||
|
||||
export default async function doLoadConfig(
|
||||
|
|
@ -51,6 +52,9 @@ export default async function doLoadConfig(
|
|||
ideInfo.extensionVersion,
|
||||
);
|
||||
|
||||
// TODO: pass config to pre-load non-system TTS models
|
||||
await TTS.setup();
|
||||
|
||||
if (newConfig.analytics) {
|
||||
await TeamAnalytics.setup(
|
||||
newConfig.analytics as any, // TODO: Need to get rid of index.d.ts once and for all
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import historyManager from "./util/history";
|
|||
import type { IMessenger, Message } from "./util/messenger";
|
||||
import { editConfigJson } from "./util/paths";
|
||||
import { Telemetry } from "./util/posthog";
|
||||
import { TTS } from "./util/tts";
|
||||
import { streamDiffLines } from "./util/verticalEdit";
|
||||
|
||||
export class Core {
|
||||
|
|
@ -383,6 +384,11 @@ export class Core {
|
|||
next = await gen.next();
|
||||
}
|
||||
|
||||
const config = await configHandler.loadConfig();
|
||||
if(config.experimental?.readResponseTTS && "completion" in next.value) {
|
||||
TTS.read(next.value?.completion);
|
||||
}
|
||||
|
||||
return { done: true, content: next.value };
|
||||
}
|
||||
|
||||
|
|
|
|||
5
core/index.d.ts
vendored
5
core/index.d.ts
vendored
|
|
@ -887,6 +887,11 @@ interface ExperimentalConfig {
|
|||
* function and class declarations.
|
||||
*/
|
||||
quickActions?: QuickActionConfig[];
|
||||
|
||||
/**
|
||||
* Automatically read LLM chat responses aloud using system TTS models
|
||||
*/
|
||||
readResponseTTS?: boolean;
|
||||
}
|
||||
|
||||
interface AnalyticsConfig {
|
||||
|
|
|
|||
108
core/util/tts.ts
Normal file
108
core/util/tts.ts
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
import os from "node:os";
|
||||
import { exec, ChildProcess } from "child_process";
|
||||
|
||||
// The amount of time before a process is declared
|
||||
// a zombie after executing .kill()
|
||||
const ttsKillTimeout: number = 5000;
|
||||
|
||||
/**
|
||||
* Removes code blocks from a message.
|
||||
*
|
||||
* Return modified message text.
|
||||
*/
|
||||
function removeCodeBlocks(msgText: string): string {
|
||||
const codeBlockRegex = /```[\s\S]*?```/g;
|
||||
|
||||
// Remove code blocks from the message text
|
||||
const textWithoutCodeBlocks = msgText.replace(codeBlockRegex, "");
|
||||
|
||||
return textWithoutCodeBlocks.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans a message text to safely be used in 'exec' context on host.
|
||||
*
|
||||
* Return modified message text.
|
||||
*/
|
||||
function sanitizeMessageForTTS(message: string): string {
|
||||
message = removeCodeBlocks(message);
|
||||
|
||||
// Remove or replace problematic characters
|
||||
message = message
|
||||
.replace(/"/g, "")
|
||||
.replace(/`/g, "")
|
||||
.replace(/\$/g, "")
|
||||
.replace(/\\/g, "")
|
||||
.replace(/[&|;()<>]/g, "");
|
||||
|
||||
message = message.trim().replace(/\s+/g, " ");
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
export class TTS {
|
||||
static os: string | undefined = undefined;
|
||||
static handle: ChildProcess | undefined = undefined;
|
||||
|
||||
static async read(message: 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.",
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static async kill(): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
// Only kill a TTS process if it's still running
|
||||
if (TTS.handle && TTS.handle.exitCode === null) {
|
||||
// Use a timeout in case of zombie processes
|
||||
let killTimeout: NodeJS.Timeout = setTimeout(() => {
|
||||
reject(`Unable to kill TTS process: ${TTS.handle?.pid}`);
|
||||
}, ttsKillTimeout);
|
||||
|
||||
// Resolve our promise once the program has exited
|
||||
TTS.handle.once("exit", () => {
|
||||
clearTimeout(killTimeout);
|
||||
TTS.handle = undefined;
|
||||
resolve();
|
||||
});
|
||||
|
||||
TTS.handle.kill();
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static async setup() {
|
||||
TTS.os = os.platform();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue