UNPKG

langcode

Version:

A Plugin-Based Framework for Managing and Using LangChain

90 lines (89 loc) 3.39 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const types_1 = require("../../types"); const axios_1 = __importDefault(require("axios")); const promises_1 = __importDefault(require("fs/promises")); class TextToSpeechPlugin { constructor() { this.name = "textToSpeech"; this.description = "Metni ElevenLabs üzerinden sese çeviren plugin."; this.type = types_1.PluginType.Tool; this.RunConfigExample = { text: "", outputFilePath: "", voiceId: "" }; this.InitConfigExample = { elevenLabsApiKey: "api-...", defaultVoiceId: "EXAVITQu4vr4xnSDxMaL", }; this.apiKey = null; this.defaultVoiceId = "EXAVITQu4vr4xnSDxMaL"; } expose() { return { name: this.name, description: this.description, type: this.type, InitConfigExample: this.InitConfigExample, RunConfigExample: this.RunConfigExample, apiKey: this.apiKey, defaultVoiceId: this.defaultVoiceId, }; } async init(config) { if (!config.elevenLabsApiKey) { throw new Error("ElevenLabs API key (elevenLabsApiKey) is required."); } this.apiKey = config.elevenLabsApiKey; if (config.defaultVoiceId) { this.defaultVoiceId = config.defaultVoiceId; } } async run(args) { if (!this.apiKey) { throw new Error("Plugin has not been initialized with an ElevenLabs API key."); } const { text, voiceId, outputFilePath } = args; if (!text) { throw new Error("Lütfen 'text' parametresini sağlayın."); } // Kullanıcı bir voiceId geçmezse, init aşamasındaki defaultVoiceId’yi kullan const usedVoiceId = voiceId || this.defaultVoiceId; try { // 1) ElevenLabs API'ine POST isteği // https://api.elevenlabs.io/v1/text-to-speech/{voiceId} const response = await axios_1.default.post(`https://api.elevenlabs.io/v1/text-to-speech/${usedVoiceId}`, { text, }, { headers: { "xi-api-key": this.apiKey, "Content-Type": "application/json", }, // responseType: "arraybuffer" -> mp3 veya ogg binary gelecek responseType: "arraybuffer", }); const audioBuffer = Buffer.from(response.data); // 2) Kullanıcı isterse ses dosyası diske kaydedilebilir if (outputFilePath) { await promises_1.default.writeFile(outputFilePath, audioBuffer); return { message: `Audio file saved to ${outputFilePath}`, size: audioBuffer.length, }; } // 3) Aksi halde Buffer olarak direkt döndürelim return { audioBuffer, size: audioBuffer.length, }; } catch (err) { throw new Error(`Text-to-speech isteği başarısız: ${err.message}`); } } } exports.default = TextToSpeechPlugin;