|
| 1 | +import { PassThrough } from "node:stream"; |
| 2 | +import type { VoiceMetadata, ReadableStreamType } from "@voltagent/core"; |
| 3 | +import { BaseVoiceProvider } from "../base"; |
| 4 | +import { XsaiVoiceOptions, XsaiSpeakOptions, XsaiListenOptions } from "./types"; |
| 5 | +import { generateSpeech, GenerateSpeechOptions } from "@xsai/generate-speech"; |
| 6 | +import { generateTranscription, GenerateTranscriptionOptions } from "@xsai/generate-transcription"; |
| 7 | + |
| 8 | +/* ------------------------------------------------------------------ */ |
| 9 | +/* Helper: bufferise a Node stream */ |
| 10 | +/* ------------------------------------------------------------------ */ |
| 11 | +async function collectChunks(stream: NodeJS.ReadableStream): Promise<Buffer> { |
| 12 | + const chunks: Buffer[] = []; |
| 13 | + for await (const c of stream) { |
| 14 | + chunks.push(typeof c === "string" ? Buffer.from(c) : c); |
| 15 | + } |
| 16 | + return Buffer.concat(chunks); |
| 17 | +} |
| 18 | + |
| 19 | +/* ------------------------------------------------------------------ */ |
| 20 | +/* xsAI provider */ |
| 21 | +/* ------------------------------------------------------------------ */ |
| 22 | +export class XsAIVoiceProvider extends BaseVoiceProvider { |
| 23 | + private readonly apiKey: string; |
| 24 | + private readonly baseURL: string; |
| 25 | + private readonly ttsModel: string; |
| 26 | + private readonly speechModel: string; |
| 27 | + private readonly voice: string; |
| 28 | + private readonly headers?: Record<string, string>; |
| 29 | + |
| 30 | + constructor(options: XsaiVoiceOptions) { |
| 31 | + super(options); |
| 32 | + |
| 33 | + this.apiKey = options.apiKey; |
| 34 | + this.baseURL = options.baseURL ?? "https://api.openai.com/v1"; |
| 35 | + this.ttsModel = options.ttsModel ?? "tts-1"; |
| 36 | + this.speechModel = options.speechModel ?? "whisper-1"; |
| 37 | + this.voice = options.voice ?? "alloy"; |
| 38 | + this.headers = options.options?.headers; |
| 39 | + } |
| 40 | + |
| 41 | + /* ------------------------------------------------------------------ */ |
| 42 | + /* TEXT ➜ SPEECH */ |
| 43 | + /* ------------------------------------------------------------------ */ |
| 44 | + async speak( |
| 45 | + input: string | NodeJS.ReadableStream, |
| 46 | + opts: XsaiSpeakOptions = {}, |
| 47 | + ): Promise<NodeJS.ReadableStream> { |
| 48 | + try { |
| 49 | + const text = |
| 50 | + typeof input === "string" ? input : (await collectChunks(input)).toString("utf8"); |
| 51 | + |
| 52 | + if (!text.trim()) throw new Error("Input text is empty"); |
| 53 | + this.emit("speaking", { text }); |
| 54 | + |
| 55 | + // Dynamically import the module |
| 56 | + |
| 57 | + const generateSpeechOptions: GenerateSpeechOptions = { |
| 58 | + input: text, |
| 59 | + voice: opts.voice ?? this.voice ?? "default", |
| 60 | + responseFormat: opts.format ?? "mp3", |
| 61 | + speed: opts.speed ?? 1.0, |
| 62 | + /* CommonRequestOptions */ |
| 63 | + apiKey: this.apiKey, |
| 64 | + baseURL: this.baseURL, |
| 65 | + model: this.ttsModel, |
| 66 | + headers: this.headers, |
| 67 | + }; |
| 68 | + |
| 69 | + const arrayBuf = await generateSpeech(generateSpeechOptions); |
| 70 | + |
| 71 | + const stream = new PassThrough(); |
| 72 | + stream.end(Buffer.from(arrayBuf)); |
| 73 | + return stream; |
| 74 | + } catch (err) { |
| 75 | + this.emit("error", { |
| 76 | + message: err instanceof Error ? err.message : "Unknown error", |
| 77 | + code: "SPEAK_ERROR", |
| 78 | + details: err, |
| 79 | + }); |
| 80 | + throw err; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /* ------------------------------------------------------------------ */ |
| 85 | + /* SPEECH ➜ TEXT */ |
| 86 | + /* ------------------------------------------------------------------ */ |
| 87 | + async listen( |
| 88 | + audio: NodeJS.ReadableStream, |
| 89 | + opts: XsaiListenOptions = {}, |
| 90 | + ): Promise<string | ReadableStreamType> { |
| 91 | + try { |
| 92 | + this.emit("listening", { audio }); |
| 93 | + const buf = await collectChunks(audio); |
| 94 | + |
| 95 | + const blob = new Blob([buf]); |
| 96 | + |
| 97 | + const generateTranscriptionOptions: GenerateTranscriptionOptions = { |
| 98 | + file: blob, |
| 99 | + fileName: opts.fileName ?? "audio.wav", |
| 100 | + language: opts.language, |
| 101 | + prompt: opts.prompt, |
| 102 | + temperature: opts.temperature, |
| 103 | + /* CommonRequestOptions */ |
| 104 | + apiKey: this.apiKey, |
| 105 | + baseURL: this.baseURL, |
| 106 | + model: this.speechModel, |
| 107 | + headers: this.headers, |
| 108 | + }; |
| 109 | + |
| 110 | + const { text } = await generateTranscription(generateTranscriptionOptions); |
| 111 | + |
| 112 | + return text; |
| 113 | + } catch (err) { |
| 114 | + this.emit("error", { |
| 115 | + message: err instanceof Error ? err.message : "Unknown error", |
| 116 | + code: "LISTEN_ERROR", |
| 117 | + details: err, |
| 118 | + }); |
| 119 | + throw err; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /* ------------------------------------------------------------------ */ |
| 124 | + /* Real‑time streaming not yet available */ |
| 125 | + /* ------------------------------------------------------------------ */ |
| 126 | + async connect(): Promise<void> { |
| 127 | + throw new Error("Real‑time streaming not supported by xsAI"); |
| 128 | + } |
| 129 | + disconnect(): void { |
| 130 | + /* noop */ |
| 131 | + } |
| 132 | + async send(): Promise<void> { |
| 133 | + throw new Error("Real‑time streaming not supported by xsAI"); |
| 134 | + } |
| 135 | + |
| 136 | + /* ------------------------------------------------------------------ */ |
| 137 | + /* xsAI hasn't published a voice list API – stub with default */ |
| 138 | + /* ------------------------------------------------------------------ */ |
| 139 | + async getVoices(): Promise<VoiceMetadata[]> { |
| 140 | + return [ |
| 141 | + { |
| 142 | + id: this.voice ?? "default", |
| 143 | + name: "xsAI default", |
| 144 | + language: "en", |
| 145 | + gender: "neutral", |
| 146 | + }, |
| 147 | + ]; |
| 148 | + } |
| 149 | +} |
0 commit comments