@nomyx/assistant
Version:
A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)
117 lines (116 loc) • 4.88 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpeechMiddleware = void 0;
const fs_1 = __importDefault(require("fs"));
const play_sound_1 = __importDefault(require("play-sound"));
class SpeechMiddleware {
constructor(aiProvider) {
this.apiKey = process.env.PLAYHT_AUTHORIZATION || '';
this.userId = process.env.PLAYHT_USER_ID || '';
this.maleVoice = process.env.PLAYHT_MALE_VOICE || '';
this.femaleVoice = process.env.PLAYHT_FEMALE_VOICE || '';
this.aiProvider = aiProvider;
}
async process(response) {
if (typeof response === 'string') {
await this.speakText(response);
return `(aloud) ${response}`;
}
else if (response && typeof response === 'object' && response.content) {
await this.speakText(response.content);
response.content = `(aloud) ${response.content}`;
return response;
}
return response;
}
async speakText(text) {
const sentences = await this.convertToSpeakableSentences(text);
for (const sentence of sentences) {
await this.speakSentence(sentence, 'female'); // Default to female voice
}
}
async convertToSpeakableSentences(text) {
const messages = [
{
role: 'system',
content: 'Convert the given text into a number of sentences meant to be spoken aloud. This means breaking the text into sentences that are easy to read and understand as well as phonetically pronouncing any difficult words, urls, or acronyms.*** Return your response as a RAW JSON ARRAY of strings. ***',
},
{
role: 'user',
content: text + '\n\n*** Return your response as a RAW JSON ARRAY of strings with NO SURROUNDING CODE BLOCKS. ***',
},
];
const response = await this.aiProvider.chat(messages, {});
let sentences;
try {
sentences = JSON.parse(response.content);
if (!Array.isArray(sentences)) {
throw new Error('Parsed content is not an array');
}
}
catch (error) {
console.error('Failed to parse sentences:', error);
sentences = [response.content]; // Fallback to using the entire content as a single sentence
}
return sentences;
}
getNonce() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
async speakSentence(sentence, voice) {
if (!sentence)
return;
const PlayHT = await Promise.resolve().then(() => __importStar(require('playht')));
PlayHT.init({
apiKey: this.apiKey,
userId: this.userId,
});
const stream = await PlayHT.stream(sentence, {
voiceEngine: 'PlayHT2.0-turbo',
voiceId: voice === 'male' ? this.maleVoice : this.femaleVoice,
});
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
return new Promise((resolve, reject) => {
stream.on('end', () => {
const buf = Buffer.concat(chunks);
const filename = `${this.getNonce()}.mp3`;
fs_1.default.writeFileSync(filename, buf);
(0, play_sound_1.default)().play(filename, (err) => {
fs_1.default.unlinkSync(filename);
if (err)
resolve(err);
else
resolve();
});
});
});
}
}
exports.SpeechMiddleware = SpeechMiddleware;