@mastra/voice-cloudflare 0.1.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,44 @@
1
+ Elastic License 2.0 (ELv2)
2
+
3
+ **Acceptance**
4
+ By using the software, you agree to all of the terms and conditions below.
5
+
6
+ **Copyright License**
7
+ The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below
8
+
9
+ **Limitations**
10
+ You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
11
+
12
+ You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
13
+
14
+ You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
15
+
16
+ **Patents**
17
+ The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
18
+
19
+ **Notices**
20
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
21
+
22
+ If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
23
+
24
+ **No Other Rights**
25
+ These terms do not imply any licenses other than those expressly granted in these terms.
26
+
27
+ **Termination**
28
+ If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
29
+
30
+ **No Liability**
31
+ As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
32
+
33
+ **Definitions**
34
+ The _licensor_ is the entity offering these terms, and the _software_ is the software the licensor makes available under these terms, including any portion of it.
35
+
36
+ _you_ refers to the individual or entity agreeing to these terms.
37
+
38
+ _your company_ is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. _control_ means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
39
+
40
+ _your licenses_ are all the licenses granted to you for the software under these terms.
41
+
42
+ _use_ means anything you do with the software requiring one of your licenses.
43
+
44
+ _trademark_ means trademarks, service marks, and similar rights.
package/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # @mastra/voice-cloudflare
2
+
3
+ Cloudflare Voice integration for Mastra, providing Text-to-Speech (TTS) capabilities using open source speech models.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @mastra/voice-cloudflare
9
+ ```
10
+
11
+ ## Configuration
12
+
13
+ The module requires the following environment variables:
14
+
15
+ If using Cloudflare Native Bindings:
16
+
17
+ `./wrangler.jsonc`
18
+
19
+ ```json
20
+ "ai": {
21
+ "binding": "AI",
22
+ },
23
+ ```
24
+
25
+ If using Cloudflare REST API:
26
+
27
+ ```bash
28
+ CLOUDFLARE_AI_API_KEY=your_api_key
29
+ CLOUDFLARE_ACCOUNT_ID=your_account_id
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ```typescript
35
+ import { CloudflareVoice } from '@mastra/voice-cloudflare';
36
+
37
+ // Native Bindings
38
+ const voice = new CloudflareVoice({
39
+ binding: env.AI,
40
+ listeningModel: {
41
+ model: '@cf/openai/whisper-large-v3-turbo',
42
+ },
43
+ });
44
+
45
+ // REST API
46
+ const voice = new CloudflareVoice({
47
+ listeningModel: {
48
+ apiKey: 'YOUR_API_KEY',
49
+ model: '@cf/openai/whisper-large-v3-turbo',
50
+ account_id: 'YOUR_ACC_ID',
51
+ },
52
+ });
53
+
54
+ // Generate Text from an audio stream
55
+ const text = await voice.listen(audioStream);
56
+ ```
57
+
58
+ ## Features
59
+
60
+ - Open source models
61
+
62
+ ## Available Models
63
+
64
+ ### Speech Models
65
+
66
+ The following speech-to-text models are available:
67
+
68
+ | Model | Description |
69
+ | ----------------------------------- | ------------------------------ |
70
+ | `@cf/openai/whisper-tiny-en` | Lightweight English-only model |
71
+ | `@cf/openai/whisper` | Standard multilingual model |
72
+ | `@cf/openai/whisper-large-v3-turbo` | High-accuracy turbo model |
@@ -0,0 +1,26 @@
1
+ import type { Ai as Ai_2 } from '@cloudflare/workers-types';
2
+ import { MastraVoice } from '@mastra/core/voice';
3
+
4
+ declare interface CloudflareListenOptions {
5
+ apiKey?: string;
6
+ model?: '@cf/openai/whisper-tiny-en' | '@cf/openai/whisper' | '@cf/openai/whisper-large-v3-turbo';
7
+ account_id?: string;
8
+ }
9
+
10
+ export declare class CloudflareVoice extends MastraVoice {
11
+ private apiToken?;
12
+ private client;
13
+ private binding?;
14
+ constructor({ listeningModel, binding, }?: {
15
+ listeningModel?: CloudflareListenOptions;
16
+ binding?: Ai_2;
17
+ });
18
+ listen(audioStream: NodeJS.ReadableStream, options?: CloudflareListenOptions): Promise<string>;
19
+ speak(): Promise<NodeJS.ReadableStream>;
20
+ getSpeakers(): Promise<Array<{
21
+ voiceId: string;
22
+ [key: string]: any;
23
+ }>>;
24
+ }
25
+
26
+ export { }
@@ -0,0 +1,26 @@
1
+ import type { Ai as Ai_2 } from '@cloudflare/workers-types';
2
+ import { MastraVoice } from '@mastra/core/voice';
3
+
4
+ declare interface CloudflareListenOptions {
5
+ apiKey?: string;
6
+ model?: '@cf/openai/whisper-tiny-en' | '@cf/openai/whisper' | '@cf/openai/whisper-large-v3-turbo';
7
+ account_id?: string;
8
+ }
9
+
10
+ export declare class CloudflareVoice extends MastraVoice {
11
+ private apiToken?;
12
+ private client;
13
+ private binding?;
14
+ constructor({ listeningModel, binding, }?: {
15
+ listeningModel?: CloudflareListenOptions;
16
+ binding?: Ai_2;
17
+ });
18
+ listen(audioStream: NodeJS.ReadableStream, options?: CloudflareListenOptions): Promise<string>;
19
+ speak(): Promise<NodeJS.ReadableStream>;
20
+ getSpeakers(): Promise<Array<{
21
+ voiceId: string;
22
+ [key: string]: any;
23
+ }>>;
24
+ }
25
+
26
+ export { }
package/dist/index.cjs ADDED
@@ -0,0 +1,74 @@
1
+ 'use strict';
2
+
3
+ var voice = require('@mastra/core/voice');
4
+ var Cloudflare = require('cloudflare');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var Cloudflare__default = /*#__PURE__*/_interopDefault(Cloudflare);
9
+
10
+ // src/index.ts
11
+ var defaultListeningModel = {
12
+ model: "@cf/openai/whisper-large-v3-turbo",
13
+ apiKey: process.env.CLOUDFLARE_AI_API_KEY,
14
+ account_id: process.env.CLOUDFLARE_ACCOUNT_ID
15
+ };
16
+ var CloudflareVoice = class extends voice.MastraVoice {
17
+ apiToken;
18
+ client = null;
19
+ binding;
20
+ constructor({
21
+ listeningModel,
22
+ binding
23
+ } = {}) {
24
+ super({
25
+ listeningModel: {
26
+ name: listeningModel?.model ?? defaultListeningModel.model,
27
+ apiKey: listeningModel?.apiKey ?? defaultListeningModel.apiKey
28
+ }
29
+ });
30
+ this.binding = binding;
31
+ if (!binding) {
32
+ this.apiToken = listeningModel?.apiKey || defaultListeningModel.apiKey;
33
+ if (!this.apiToken) {
34
+ throw new Error("CLOUDFLARE_AI_API_KEY must be set when not using bindings");
35
+ }
36
+ this.client = new Cloudflare__default.default({ apiToken: this.apiToken });
37
+ }
38
+ }
39
+ async listen(audioStream, options) {
40
+ return this.traced(async () => {
41
+ const chunks = [];
42
+ for await (const chunk of audioStream) {
43
+ if (typeof chunk === "string") {
44
+ chunks.push(Buffer.from(chunk));
45
+ } else {
46
+ chunks.push(chunk);
47
+ }
48
+ }
49
+ const audioBuffer = Buffer.concat(chunks);
50
+ const base64Audio = audioBuffer.toString("base64");
51
+ const model = options?.model || defaultListeningModel.model;
52
+ if (this.binding) {
53
+ const response = await this.binding.run(model, {
54
+ audio: base64Audio
55
+ });
56
+ return response.text;
57
+ } else if (this.client) {
58
+ const payload = { audio: base64Audio, account_id: options?.account_id || defaultListeningModel.account_id };
59
+ const response = await this.client.ai.run(model, payload);
60
+ return response.text;
61
+ } else {
62
+ throw new Error("Neither binding nor REST client is configured");
63
+ }
64
+ }, "voice.cloudflare.listen")();
65
+ }
66
+ async speak() {
67
+ throw new Error("This feature is not yet implemented.");
68
+ }
69
+ async getSpeakers() {
70
+ throw new Error("This feature is not yet implemented.");
71
+ }
72
+ };
73
+
74
+ exports.CloudflareVoice = CloudflareVoice;
@@ -0,0 +1 @@
1
+ export { CloudflareVoice } from './_tsup-dts-rollup.cjs';
@@ -0,0 +1 @@
1
+ export { CloudflareVoice } from './_tsup-dts-rollup.js';
package/dist/index.js ADDED
@@ -0,0 +1,68 @@
1
+ import { MastraVoice } from '@mastra/core/voice';
2
+ import Cloudflare from 'cloudflare';
3
+
4
+ // src/index.ts
5
+ var defaultListeningModel = {
6
+ model: "@cf/openai/whisper-large-v3-turbo",
7
+ apiKey: process.env.CLOUDFLARE_AI_API_KEY,
8
+ account_id: process.env.CLOUDFLARE_ACCOUNT_ID
9
+ };
10
+ var CloudflareVoice = class extends MastraVoice {
11
+ apiToken;
12
+ client = null;
13
+ binding;
14
+ constructor({
15
+ listeningModel,
16
+ binding
17
+ } = {}) {
18
+ super({
19
+ listeningModel: {
20
+ name: listeningModel?.model ?? defaultListeningModel.model,
21
+ apiKey: listeningModel?.apiKey ?? defaultListeningModel.apiKey
22
+ }
23
+ });
24
+ this.binding = binding;
25
+ if (!binding) {
26
+ this.apiToken = listeningModel?.apiKey || defaultListeningModel.apiKey;
27
+ if (!this.apiToken) {
28
+ throw new Error("CLOUDFLARE_AI_API_KEY must be set when not using bindings");
29
+ }
30
+ this.client = new Cloudflare({ apiToken: this.apiToken });
31
+ }
32
+ }
33
+ async listen(audioStream, options) {
34
+ return this.traced(async () => {
35
+ const chunks = [];
36
+ for await (const chunk of audioStream) {
37
+ if (typeof chunk === "string") {
38
+ chunks.push(Buffer.from(chunk));
39
+ } else {
40
+ chunks.push(chunk);
41
+ }
42
+ }
43
+ const audioBuffer = Buffer.concat(chunks);
44
+ const base64Audio = audioBuffer.toString("base64");
45
+ const model = options?.model || defaultListeningModel.model;
46
+ if (this.binding) {
47
+ const response = await this.binding.run(model, {
48
+ audio: base64Audio
49
+ });
50
+ return response.text;
51
+ } else if (this.client) {
52
+ const payload = { audio: base64Audio, account_id: options?.account_id || defaultListeningModel.account_id };
53
+ const response = await this.client.ai.run(model, payload);
54
+ return response.text;
55
+ } else {
56
+ throw new Error("Neither binding nor REST client is configured");
57
+ }
58
+ }, "voice.cloudflare.listen")();
59
+ }
60
+ async speak() {
61
+ throw new Error("This feature is not yet implemented.");
62
+ }
63
+ async getSpeakers() {
64
+ throw new Error("This feature is not yet implemented.");
65
+ }
66
+ };
67
+
68
+ export { CloudflareVoice };
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@mastra/voice-cloudflare",
3
+ "version": "0.1.1-alpha.1",
4
+ "description": "Mastra Sarvam AI voice integration",
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "main": "dist/index.js",
10
+ "types": "dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ },
17
+ "require": {
18
+ "types": "./dist/index.d.cts",
19
+ "default": "./dist/index.cjs"
20
+ }
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "dependencies": {
25
+ "cloudflare": "^4.0.0",
26
+ "zod": "^3.24.2",
27
+ "@mastra/core": "^0.6.4-alpha.1"
28
+ },
29
+ "devDependencies": {
30
+ "@cloudflare/workers-types": "^4.20250317.0",
31
+ "@microsoft/api-extractor": "^7.52.1",
32
+ "@types/node": "^22.13.10",
33
+ "eslint": "^9.22.0",
34
+ "tsup": "^8.4.0",
35
+ "typescript": "^5.8.2",
36
+ "vitest": "^2.1.9",
37
+ "@internal/lint": "0.0.1"
38
+ },
39
+ "keywords": [
40
+ "mastra",
41
+ "cloudflare",
42
+ "tts",
43
+ "stt",
44
+ "open-source",
45
+ "speech-to-text",
46
+ "text-to-speech",
47
+ "speech-recognition"
48
+ ],
49
+ "scripts": {
50
+ "build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
51
+ "build:watch": "pnpm build --watch",
52
+ "test": "vitest run",
53
+ "test:watch": "vitest watch",
54
+ "lint": "eslint ."
55
+ }
56
+ }