@mastra/voice-cloudflare
Version:
Mastra Cloudflare AI voice integration
82 lines (77 loc) • 2.47 kB
JavaScript
;
var voice = require('@mastra/core/voice');
var Cloudflare = require('cloudflare');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var Cloudflare__default = /*#__PURE__*/_interopDefault(Cloudflare);
// src/index.ts
var defaultListeningModel = {
model: "@cf/openai/whisper-large-v3-turbo",
apiKey: process.env.CLOUDFLARE_AI_API_KEY,
account_id: process.env.CLOUDFLARE_ACCOUNT_ID
};
var CloudflareVoice = class extends voice.MastraVoice {
apiToken;
client = null;
binding;
constructor({
listeningModel,
binding
} = {}) {
super({
listeningModel: {
name: listeningModel?.model ?? defaultListeningModel.model,
apiKey: listeningModel?.apiKey ?? defaultListeningModel.apiKey
}
});
this.binding = binding;
if (!binding) {
this.apiToken = listeningModel?.apiKey || defaultListeningModel.apiKey;
if (!this.apiToken) {
throw new Error("CLOUDFLARE_AI_API_KEY must be set when not using bindings");
}
this.client = new Cloudflare__default.default({ apiToken: this.apiToken });
}
}
/**
* Checks if listening capabilities are enabled.
*
* @returns {Promise<{ enabled: boolean }>}
*/
async getListener() {
return { enabled: true };
}
async listen(audioStream, options) {
const chunks = [];
for await (const chunk of audioStream) {
if (typeof chunk === "string") {
chunks.push(Buffer.from(chunk));
} else {
chunks.push(chunk);
}
}
const audioBuffer = Buffer.concat(chunks);
const base64Audio = audioBuffer.toString("base64");
const model = options?.model || defaultListeningModel.model;
if (this.binding) {
const response = await this.binding.run(model, {
audio: base64Audio
});
return response.text;
} else if (this.client) {
const payload = { audio: base64Audio, account_id: options?.account_id || defaultListeningModel.account_id };
const response = await this.client.ai.run(model, payload);
return response.text;
} else {
throw new Error("Neither binding nor REST client is configured");
}
}
async speak() {
throw new Error("This feature is not yet implemented.");
}
async getSpeakers() {
throw new Error("This feature is not yet implemented.");
}
};
exports.CloudflareVoice = CloudflareVoice;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map