@ai-sdk/elevenlabs 0.0.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/CHANGELOG.md +7 -0
- package/LICENSE +13 -0
- package/README.md +38 -0
- package/dist/index.d.mts +65 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +204 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +182 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +65 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023 Vercel, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# AI SDK - ElevenLabs Provider
|
|
2
|
+
|
|
3
|
+
The **[ElevenLabs provider](https://sdk.vercel.ai/providers/ai-sdk-providers/elevenlabs)** for the [AI SDK](https://sdk.vercel.ai/docs)
|
|
4
|
+
contains language model support for the ElevenLabs chat and completion APIs and embedding model support for the ElevenLabs embeddings API.
|
|
5
|
+
|
|
6
|
+
## Setup
|
|
7
|
+
|
|
8
|
+
The ElevenLabs provider is available in the `@ai-sdk/elevenlabs` module. You can install it with
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm i @ai-sdk/elevenlabs
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Provider Instance
|
|
15
|
+
|
|
16
|
+
You can import the default provider instance `elevenlabs` from `@ai-sdk/elevenlabs`:
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { elevenlabs } from '@ai-sdk/elevenlabs';
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Example
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { elevenlabs } from '@ai-sdk/elevenlabs';
|
|
26
|
+
import { experimental_transcribe as transcribe } from 'ai';
|
|
27
|
+
|
|
28
|
+
const { text } = await transcribe({
|
|
29
|
+
model: elevenlabs.transcription('scribe_v1'),
|
|
30
|
+
audio: new URL(
|
|
31
|
+
'https://github.com/vercel/ai/raw/refs/heads/main/examples/ai-core/data/galileo.mp3',
|
|
32
|
+
),
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Documentation
|
|
37
|
+
|
|
38
|
+
Please check out the **[ElevenLabs provider documentation](https://sdk.vercel.ai/providers/ai-sdk-providers/elevenlabs)** for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { TranscriptionModelV1, ProviderV1 } from '@ai-sdk/provider';
|
|
2
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
+
|
|
4
|
+
type ElevenLabsConfig = {
|
|
5
|
+
provider: string;
|
|
6
|
+
url: (options: {
|
|
7
|
+
modelId: string;
|
|
8
|
+
path: string;
|
|
9
|
+
}) => string;
|
|
10
|
+
headers: () => Record<string, string | undefined>;
|
|
11
|
+
fetch?: FetchFunction;
|
|
12
|
+
generateId?: () => string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type ElevenLabsTranscriptionModelId = 'scribe_v1' | 'scribe_v1_experimental' | (string & {});
|
|
16
|
+
|
|
17
|
+
interface ElevenLabsTranscriptionModelConfig extends ElevenLabsConfig {
|
|
18
|
+
_internal?: {
|
|
19
|
+
currentDate?: () => Date;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
declare class ElevenLabsTranscriptionModel implements TranscriptionModelV1 {
|
|
23
|
+
readonly modelId: ElevenLabsTranscriptionModelId;
|
|
24
|
+
private readonly config;
|
|
25
|
+
readonly specificationVersion = "v1";
|
|
26
|
+
get provider(): string;
|
|
27
|
+
constructor(modelId: ElevenLabsTranscriptionModelId, config: ElevenLabsTranscriptionModelConfig);
|
|
28
|
+
private getArgs;
|
|
29
|
+
doGenerate(options: Parameters<TranscriptionModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<TranscriptionModelV1['doGenerate']>>>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface ElevenLabsProvider extends Pick<ProviderV1, 'transcriptionModel'> {
|
|
33
|
+
(modelId: 'scribe_v1', settings?: {}): {
|
|
34
|
+
transcription: ElevenLabsTranscriptionModel;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
Creates a model for transcription.
|
|
38
|
+
*/
|
|
39
|
+
transcription(modelId: ElevenLabsTranscriptionModelId): TranscriptionModelV1;
|
|
40
|
+
}
|
|
41
|
+
interface ElevenLabsProviderSettings {
|
|
42
|
+
/**
|
|
43
|
+
API key for authenticating requests.
|
|
44
|
+
*/
|
|
45
|
+
apiKey?: string;
|
|
46
|
+
/**
|
|
47
|
+
Custom headers to include in the requests.
|
|
48
|
+
*/
|
|
49
|
+
headers?: Record<string, string>;
|
|
50
|
+
/**
|
|
51
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
52
|
+
or to provide a custom fetch implementation for e.g. testing.
|
|
53
|
+
*/
|
|
54
|
+
fetch?: FetchFunction;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
Create an ElevenLabs provider instance.
|
|
58
|
+
*/
|
|
59
|
+
declare function createElevenLabs(options?: ElevenLabsProviderSettings): ElevenLabsProvider;
|
|
60
|
+
/**
|
|
61
|
+
Default ElevenLabs provider instance.
|
|
62
|
+
*/
|
|
63
|
+
declare const elevenlabs: ElevenLabsProvider;
|
|
64
|
+
|
|
65
|
+
export { type ElevenLabsProvider, type ElevenLabsProviderSettings, createElevenLabs, elevenlabs };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { TranscriptionModelV1, ProviderV1 } from '@ai-sdk/provider';
|
|
2
|
+
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
+
|
|
4
|
+
type ElevenLabsConfig = {
|
|
5
|
+
provider: string;
|
|
6
|
+
url: (options: {
|
|
7
|
+
modelId: string;
|
|
8
|
+
path: string;
|
|
9
|
+
}) => string;
|
|
10
|
+
headers: () => Record<string, string | undefined>;
|
|
11
|
+
fetch?: FetchFunction;
|
|
12
|
+
generateId?: () => string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type ElevenLabsTranscriptionModelId = 'scribe_v1' | 'scribe_v1_experimental' | (string & {});
|
|
16
|
+
|
|
17
|
+
interface ElevenLabsTranscriptionModelConfig extends ElevenLabsConfig {
|
|
18
|
+
_internal?: {
|
|
19
|
+
currentDate?: () => Date;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
declare class ElevenLabsTranscriptionModel implements TranscriptionModelV1 {
|
|
23
|
+
readonly modelId: ElevenLabsTranscriptionModelId;
|
|
24
|
+
private readonly config;
|
|
25
|
+
readonly specificationVersion = "v1";
|
|
26
|
+
get provider(): string;
|
|
27
|
+
constructor(modelId: ElevenLabsTranscriptionModelId, config: ElevenLabsTranscriptionModelConfig);
|
|
28
|
+
private getArgs;
|
|
29
|
+
doGenerate(options: Parameters<TranscriptionModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<TranscriptionModelV1['doGenerate']>>>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface ElevenLabsProvider extends Pick<ProviderV1, 'transcriptionModel'> {
|
|
33
|
+
(modelId: 'scribe_v1', settings?: {}): {
|
|
34
|
+
transcription: ElevenLabsTranscriptionModel;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
Creates a model for transcription.
|
|
38
|
+
*/
|
|
39
|
+
transcription(modelId: ElevenLabsTranscriptionModelId): TranscriptionModelV1;
|
|
40
|
+
}
|
|
41
|
+
interface ElevenLabsProviderSettings {
|
|
42
|
+
/**
|
|
43
|
+
API key for authenticating requests.
|
|
44
|
+
*/
|
|
45
|
+
apiKey?: string;
|
|
46
|
+
/**
|
|
47
|
+
Custom headers to include in the requests.
|
|
48
|
+
*/
|
|
49
|
+
headers?: Record<string, string>;
|
|
50
|
+
/**
|
|
51
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
52
|
+
or to provide a custom fetch implementation for e.g. testing.
|
|
53
|
+
*/
|
|
54
|
+
fetch?: FetchFunction;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
Create an ElevenLabs provider instance.
|
|
58
|
+
*/
|
|
59
|
+
declare function createElevenLabs(options?: ElevenLabsProviderSettings): ElevenLabsProvider;
|
|
60
|
+
/**
|
|
61
|
+
Default ElevenLabs provider instance.
|
|
62
|
+
*/
|
|
63
|
+
declare const elevenlabs: ElevenLabsProvider;
|
|
64
|
+
|
|
65
|
+
export { type ElevenLabsProvider, type ElevenLabsProviderSettings, createElevenLabs, elevenlabs };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
createElevenLabs: () => createElevenLabs,
|
|
24
|
+
elevenlabs: () => elevenlabs
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// src/elevenlabs-provider.ts
|
|
29
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
30
|
+
|
|
31
|
+
// src/elevenlabs-transcription-model.ts
|
|
32
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
33
|
+
var import_zod2 = require("zod");
|
|
34
|
+
|
|
35
|
+
// src/elevenlabs-error.ts
|
|
36
|
+
var import_zod = require("zod");
|
|
37
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
38
|
+
var elevenlabsErrorDataSchema = import_zod.z.object({
|
|
39
|
+
error: import_zod.z.object({
|
|
40
|
+
message: import_zod.z.string(),
|
|
41
|
+
code: import_zod.z.number()
|
|
42
|
+
})
|
|
43
|
+
});
|
|
44
|
+
var elevenlabsFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
45
|
+
errorSchema: elevenlabsErrorDataSchema,
|
|
46
|
+
errorToMessage: (data) => data.error.message
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// src/elevenlabs-transcription-model.ts
|
|
50
|
+
var elevenLabsProviderOptionsSchema = import_zod2.z.object({
|
|
51
|
+
languageCode: import_zod2.z.string().nullish(),
|
|
52
|
+
tagAudioEvents: import_zod2.z.boolean().nullish().default(true),
|
|
53
|
+
numSpeakers: import_zod2.z.number().int().min(1).max(32).nullish(),
|
|
54
|
+
timestampsGranularity: import_zod2.z.enum(["none", "word", "character"]).nullish().default("word"),
|
|
55
|
+
diarize: import_zod2.z.boolean().nullish().default(false),
|
|
56
|
+
file_format: import_zod2.z.enum(["pcm_s16le_16", "other"]).nullish().default("other")
|
|
57
|
+
});
|
|
58
|
+
var ElevenLabsTranscriptionModel = class {
|
|
59
|
+
constructor(modelId, config) {
|
|
60
|
+
this.modelId = modelId;
|
|
61
|
+
this.config = config;
|
|
62
|
+
this.specificationVersion = "v1";
|
|
63
|
+
}
|
|
64
|
+
get provider() {
|
|
65
|
+
return this.config.provider;
|
|
66
|
+
}
|
|
67
|
+
getArgs({
|
|
68
|
+
audio,
|
|
69
|
+
mediaType,
|
|
70
|
+
providerOptions
|
|
71
|
+
}) {
|
|
72
|
+
var _a, _b, _c, _d, _e;
|
|
73
|
+
const warnings = [];
|
|
74
|
+
const elevenlabsOptions = (0, import_provider_utils2.parseProviderOptions)({
|
|
75
|
+
provider: "elevenlabs",
|
|
76
|
+
providerOptions,
|
|
77
|
+
schema: elevenLabsProviderOptionsSchema
|
|
78
|
+
});
|
|
79
|
+
const formData = new FormData();
|
|
80
|
+
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([(0, import_provider_utils2.convertBase64ToUint8Array)(audio)]);
|
|
81
|
+
formData.append("model_id", this.modelId);
|
|
82
|
+
formData.append("file", new File([blob], "audio", { type: mediaType }));
|
|
83
|
+
formData.append("diarize", "true");
|
|
84
|
+
if (elevenlabsOptions) {
|
|
85
|
+
const transcriptionModelOptions = {
|
|
86
|
+
language_code: (_a = elevenlabsOptions.languageCode) != null ? _a : void 0,
|
|
87
|
+
tag_audio_events: (_b = elevenlabsOptions.tagAudioEvents) != null ? _b : void 0,
|
|
88
|
+
num_speakers: (_c = elevenlabsOptions.numSpeakers) != null ? _c : void 0,
|
|
89
|
+
timestamps_granularity: (_d = elevenlabsOptions.timestampsGranularity) != null ? _d : void 0,
|
|
90
|
+
file_format: (_e = elevenlabsOptions.file_format) != null ? _e : void 0
|
|
91
|
+
};
|
|
92
|
+
if (typeof elevenlabsOptions.diarize === "boolean") {
|
|
93
|
+
formData.append("diarize", String(elevenlabsOptions.diarize));
|
|
94
|
+
}
|
|
95
|
+
for (const key in transcriptionModelOptions) {
|
|
96
|
+
const value = transcriptionModelOptions[key];
|
|
97
|
+
if (value !== void 0) {
|
|
98
|
+
formData.append(key, String(value));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
formData,
|
|
104
|
+
warnings
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
async doGenerate(options) {
|
|
108
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
109
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
110
|
+
const { formData, warnings } = this.getArgs(options);
|
|
111
|
+
const {
|
|
112
|
+
value: response,
|
|
113
|
+
responseHeaders,
|
|
114
|
+
rawValue: rawResponse
|
|
115
|
+
} = await (0, import_provider_utils2.postFormDataToApi)({
|
|
116
|
+
url: this.config.url({
|
|
117
|
+
path: "/v1/speech-to-text",
|
|
118
|
+
modelId: this.modelId
|
|
119
|
+
}),
|
|
120
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
|
121
|
+
formData,
|
|
122
|
+
failedResponseHandler: elevenlabsFailedResponseHandler,
|
|
123
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
|
124
|
+
elevenlabsTranscriptionResponseSchema
|
|
125
|
+
),
|
|
126
|
+
abortSignal: options.abortSignal,
|
|
127
|
+
fetch: this.config.fetch
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
text: response.text,
|
|
131
|
+
segments: (_e = (_d = response.words) == null ? void 0 : _d.map((word) => {
|
|
132
|
+
var _a2, _b2;
|
|
133
|
+
return {
|
|
134
|
+
text: word.text,
|
|
135
|
+
startSecond: (_a2 = word.start) != null ? _a2 : 0,
|
|
136
|
+
endSecond: (_b2 = word.end) != null ? _b2 : 0
|
|
137
|
+
};
|
|
138
|
+
})) != null ? _e : [],
|
|
139
|
+
language: response.language_code,
|
|
140
|
+
durationInSeconds: (_h = (_g = (_f = response.words) == null ? void 0 : _f.at(-1)) == null ? void 0 : _g.end) != null ? _h : void 0,
|
|
141
|
+
warnings,
|
|
142
|
+
response: {
|
|
143
|
+
timestamp: currentDate,
|
|
144
|
+
modelId: this.modelId,
|
|
145
|
+
headers: responseHeaders,
|
|
146
|
+
body: rawResponse
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
var elevenlabsTranscriptionResponseSchema = import_zod2.z.object({
|
|
152
|
+
language_code: import_zod2.z.string(),
|
|
153
|
+
language_probability: import_zod2.z.number(),
|
|
154
|
+
text: import_zod2.z.string(),
|
|
155
|
+
words: import_zod2.z.array(
|
|
156
|
+
import_zod2.z.object({
|
|
157
|
+
text: import_zod2.z.string(),
|
|
158
|
+
type: import_zod2.z.enum(["word", "spacing", "audio_event"]),
|
|
159
|
+
start: import_zod2.z.number().nullish(),
|
|
160
|
+
end: import_zod2.z.number().nullish(),
|
|
161
|
+
speaker_id: import_zod2.z.string().nullish(),
|
|
162
|
+
characters: import_zod2.z.array(
|
|
163
|
+
import_zod2.z.object({
|
|
164
|
+
text: import_zod2.z.string(),
|
|
165
|
+
start: import_zod2.z.number().nullish(),
|
|
166
|
+
end: import_zod2.z.number().nullish()
|
|
167
|
+
})
|
|
168
|
+
).nullish()
|
|
169
|
+
})
|
|
170
|
+
).nullish()
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// src/elevenlabs-provider.ts
|
|
174
|
+
function createElevenLabs(options = {}) {
|
|
175
|
+
const getHeaders = () => ({
|
|
176
|
+
"xi-api-key": (0, import_provider_utils3.loadApiKey)({
|
|
177
|
+
apiKey: options.apiKey,
|
|
178
|
+
environmentVariableName: "ELEVENLABS_API_KEY",
|
|
179
|
+
description: "ElevenLabs"
|
|
180
|
+
}),
|
|
181
|
+
...options.headers
|
|
182
|
+
});
|
|
183
|
+
const createTranscriptionModel = (modelId) => new ElevenLabsTranscriptionModel(modelId, {
|
|
184
|
+
provider: `elevenlabs.transcription`,
|
|
185
|
+
url: ({ path }) => `https://api.elevenlabs.io${path}`,
|
|
186
|
+
headers: getHeaders,
|
|
187
|
+
fetch: options.fetch
|
|
188
|
+
});
|
|
189
|
+
const provider = function(modelId) {
|
|
190
|
+
return {
|
|
191
|
+
transcription: createTranscriptionModel(modelId)
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
provider.transcription = createTranscriptionModel;
|
|
195
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
196
|
+
return provider;
|
|
197
|
+
}
|
|
198
|
+
var elevenlabs = createElevenLabs();
|
|
199
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
200
|
+
0 && (module.exports = {
|
|
201
|
+
createElevenLabs,
|
|
202
|
+
elevenlabs
|
|
203
|
+
});
|
|
204
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/elevenlabs-provider.ts","../src/elevenlabs-transcription-model.ts","../src/elevenlabs-error.ts"],"sourcesContent":["export { createElevenLabs, elevenlabs } from './elevenlabs-provider';\nexport type {\n ElevenLabsProvider,\n ElevenLabsProviderSettings,\n} from './elevenlabs-provider';\n","import { TranscriptionModelV1, ProviderV1 } from '@ai-sdk/provider';\nimport { FetchFunction, loadApiKey } from '@ai-sdk/provider-utils';\nimport { ElevenLabsTranscriptionModel } from './elevenlabs-transcription-model';\nimport { ElevenLabsTranscriptionModelId } from './elevenlabs-transcription-settings';\n\nexport interface ElevenLabsProvider\n extends Pick<ProviderV1, 'transcriptionModel'> {\n (\n modelId: 'scribe_v1',\n settings?: {},\n ): {\n transcription: ElevenLabsTranscriptionModel;\n };\n\n /**\nCreates a model for transcription.\n */\n transcription(modelId: ElevenLabsTranscriptionModelId): TranscriptionModelV1;\n}\n\nexport interface ElevenLabsProviderSettings {\n /**\nAPI key for authenticating requests.\n */\n apiKey?: string;\n\n /**\nCustom headers to include in the requests.\n */\n headers?: Record<string, string>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\n/**\nCreate an ElevenLabs provider instance.\n */\nexport function createElevenLabs(\n options: ElevenLabsProviderSettings = {},\n): ElevenLabsProvider {\n const getHeaders = () => ({\n 'xi-api-key': loadApiKey({\n apiKey: options.apiKey,\n environmentVariableName: 'ELEVENLABS_API_KEY',\n description: 'ElevenLabs',\n }),\n ...options.headers,\n });\n\n const createTranscriptionModel = (modelId: ElevenLabsTranscriptionModelId) =>\n new ElevenLabsTranscriptionModel(modelId, {\n provider: `elevenlabs.transcription`,\n url: ({ path }) => `https://api.elevenlabs.io${path}`,\n headers: getHeaders,\n fetch: options.fetch,\n });\n\n const provider = function (modelId: ElevenLabsTranscriptionModelId) {\n return {\n transcription: createTranscriptionModel(modelId),\n };\n };\n\n provider.transcription = createTranscriptionModel;\n provider.transcriptionModel = createTranscriptionModel;\n\n return provider as ElevenLabsProvider;\n}\n\n/**\nDefault ElevenLabs provider instance.\n */\nexport const elevenlabs = createElevenLabs();\n","import {\n TranscriptionModelV1,\n TranscriptionModelV1CallOptions,\n TranscriptionModelV1CallWarning,\n} from '@ai-sdk/provider';\nimport {\n combineHeaders,\n convertBase64ToUint8Array,\n createJsonResponseHandler,\n parseProviderOptions,\n postFormDataToApi,\n} from '@ai-sdk/provider-utils';\nimport { z } from 'zod';\nimport { ElevenLabsConfig } from './elevenlabs-config';\nimport { elevenlabsFailedResponseHandler } from './elevenlabs-error';\nimport { ElevenLabsTranscriptionModelId } from './elevenlabs-transcription-settings';\nimport { ElevenLabsTranscriptionAPITypes } from './elevenlabs-api-types';\n\n// https://elevenlabs.io/docs/api-reference/speech-to-text/convert\nconst elevenLabsProviderOptionsSchema = z.object({\n languageCode: z.string().nullish(),\n tagAudioEvents: z.boolean().nullish().default(true),\n numSpeakers: z.number().int().min(1).max(32).nullish(),\n timestampsGranularity: z\n .enum(['none', 'word', 'character'])\n .nullish()\n .default('word'),\n diarize: z.boolean().nullish().default(false),\n file_format: z.enum(['pcm_s16le_16', 'other']).nullish().default('other'),\n});\n\nexport type ElevenLabsTranscriptionCallOptions = z.infer<\n typeof elevenLabsProviderOptionsSchema\n>;\n\ninterface ElevenLabsTranscriptionModelConfig extends ElevenLabsConfig {\n _internal?: {\n currentDate?: () => Date;\n };\n}\n\nexport class ElevenLabsTranscriptionModel implements TranscriptionModelV1 {\n readonly specificationVersion = 'v1';\n\n get provider(): string {\n return this.config.provider;\n }\n\n constructor(\n readonly modelId: ElevenLabsTranscriptionModelId,\n private readonly config: ElevenLabsTranscriptionModelConfig,\n ) {}\n\n private getArgs({\n audio,\n mediaType,\n providerOptions,\n }: Parameters<TranscriptionModelV1['doGenerate']>[0]) {\n const warnings: TranscriptionModelV1CallWarning[] = [];\n\n // Parse provider options\n const elevenlabsOptions = parseProviderOptions({\n provider: 'elevenlabs',\n providerOptions,\n schema: elevenLabsProviderOptionsSchema,\n });\n\n // Create form data with base fields\n const formData = new FormData();\n const blob =\n audio instanceof Uint8Array\n ? new Blob([audio])\n : new Blob([convertBase64ToUint8Array(audio)]);\n\n formData.append('model_id', this.modelId);\n formData.append('file', new File([blob], 'audio', { type: mediaType }));\n formData.append('diarize', 'true');\n\n // Add provider-specific options\n if (elevenlabsOptions) {\n const transcriptionModelOptions: ElevenLabsTranscriptionAPITypes = {\n language_code: elevenlabsOptions.languageCode ?? undefined,\n tag_audio_events: elevenlabsOptions.tagAudioEvents ?? undefined,\n num_speakers: elevenlabsOptions.numSpeakers ?? undefined,\n timestamps_granularity:\n elevenlabsOptions.timestampsGranularity ?? undefined,\n file_format: elevenlabsOptions.file_format ?? undefined,\n };\n\n if (typeof elevenlabsOptions.diarize === 'boolean') {\n formData.append('diarize', String(elevenlabsOptions.diarize));\n }\n\n for (const key in transcriptionModelOptions) {\n const value =\n transcriptionModelOptions[\n key as keyof ElevenLabsTranscriptionAPITypes\n ];\n if (value !== undefined) {\n formData.append(key, String(value));\n }\n }\n }\n\n return {\n formData,\n warnings,\n };\n }\n\n async doGenerate(\n options: Parameters<TranscriptionModelV1['doGenerate']>[0],\n ): Promise<Awaited<ReturnType<TranscriptionModelV1['doGenerate']>>> {\n const currentDate = this.config._internal?.currentDate?.() ?? new Date();\n const { formData, warnings } = this.getArgs(options);\n\n const {\n value: response,\n responseHeaders,\n rawValue: rawResponse,\n } = await postFormDataToApi({\n url: this.config.url({\n path: '/v1/speech-to-text',\n modelId: this.modelId,\n }),\n headers: combineHeaders(this.config.headers(), options.headers),\n formData,\n failedResponseHandler: elevenlabsFailedResponseHandler,\n successfulResponseHandler: createJsonResponseHandler(\n elevenlabsTranscriptionResponseSchema,\n ),\n abortSignal: options.abortSignal,\n fetch: this.config.fetch,\n });\n\n return {\n text: response.text,\n segments:\n response.words?.map(word => ({\n text: word.text,\n startSecond: word.start ?? 0,\n endSecond: word.end ?? 0,\n })) ?? [],\n language: response.language_code,\n durationInSeconds: response.words?.at(-1)?.end ?? undefined,\n warnings,\n response: {\n timestamp: currentDate,\n modelId: this.modelId,\n headers: responseHeaders,\n body: rawResponse,\n },\n };\n }\n}\n\nconst elevenlabsTranscriptionResponseSchema = z.object({\n language_code: z.string(),\n language_probability: z.number(),\n text: z.string(),\n words: z\n .array(\n z.object({\n text: z.string(),\n type: z.enum(['word', 'spacing', 'audio_event']),\n start: z.number().nullish(),\n end: z.number().nullish(),\n speaker_id: z.string().nullish(),\n characters: z\n .array(\n z.object({\n text: z.string(),\n start: z.number().nullish(),\n end: z.number().nullish(),\n }),\n )\n .nullish(),\n }),\n )\n .nullish(),\n});\n","import { z } from 'zod';\nimport { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils';\n\nexport const elevenlabsErrorDataSchema = z.object({\n error: z.object({\n message: z.string(),\n code: z.number(),\n }),\n});\n\nexport type ElevenLabsErrorData = z.infer<typeof elevenlabsErrorDataSchema>;\n\nexport const elevenlabsFailedResponseHandler = createJsonErrorResponseHandler({\n errorSchema: elevenlabsErrorDataSchema,\n errorToMessage: data => data.error.message,\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,IAAAA,yBAA0C;;;ACI1C,IAAAC,yBAMO;AACP,IAAAC,cAAkB;;;ACZlB,iBAAkB;AAClB,4BAA+C;AAExC,IAAM,4BAA4B,aAAE,OAAO;AAAA,EAChD,OAAO,aAAE,OAAO;AAAA,IACd,SAAS,aAAE,OAAO;AAAA,IAClB,MAAM,aAAE,OAAO;AAAA,EACjB,CAAC;AACH,CAAC;AAIM,IAAM,sCAAkC,sDAA+B;AAAA,EAC5E,aAAa;AAAA,EACb,gBAAgB,UAAQ,KAAK,MAAM;AACrC,CAAC;;;ADID,IAAM,kCAAkC,cAAE,OAAO;AAAA,EAC/C,cAAc,cAAE,OAAO,EAAE,QAAQ;AAAA,EACjC,gBAAgB,cAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,IAAI;AAAA,EAClD,aAAa,cAAE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,QAAQ;AAAA,EACrD,uBAAuB,cACpB,KAAK,CAAC,QAAQ,QAAQ,WAAW,CAAC,EAClC,QAAQ,EACR,QAAQ,MAAM;AAAA,EACjB,SAAS,cAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,KAAK;AAAA,EAC5C,aAAa,cAAE,KAAK,CAAC,gBAAgB,OAAO,CAAC,EAAE,QAAQ,EAAE,QAAQ,OAAO;AAC1E,CAAC;AAYM,IAAM,+BAAN,MAAmE;AAAA,EAOxE,YACW,SACQ,QACjB;AAFS;AACQ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AAE,OAAO;AAAA,MACf,MAAM,cAAE,KAAK,CAAC,QAAQ,WAAW,aAAa,CAAC;AAAA,MAC/C,OAAO,cAAE,OAAO,EAAE,QAAQ;AAAA,MAC1B,KAAK,cAAE,OAAO,EAAE,QAAQ;AAAA,MACxB,YAAY,cAAE,OAAO,EAAE,QAAQ;AAAA,MAC/B,YAAY,cACT;AAAA,QACC,cAAE,OAAO;AAAA,UACP,MAAM,cAAE,OAAO;AAAA,UACf,OAAO,cAAE,OAAO,EAAE,QAAQ;AAAA,UAC1B,KAAK,cAAE,OAAO,EAAE,QAAQ;AAAA,QAC1B,CAAC;AAAA,MACH,EACC,QAAQ;AAAA,IACb,CAAC;AAAA,EACH,EACC,QAAQ;AACb,CAAC;;;AD3IM,SAAS,iBACd,UAAsC,CAAC,GACnB;AACpB,QAAM,aAAa,OAAO;AAAA,IACxB,kBAAc,mCAAW;AAAA,MACvB,QAAQ,QAAQ;AAAA,MAChB,yBAAyB;AAAA,MACzB,aAAa;AAAA,IACf,CAAC;AAAA,IACD,GAAG,QAAQ;AAAA,EACb;AAEA,QAAM,2BAA2B,CAAC,YAChC,IAAI,6BAA6B,SAAS;AAAA,IACxC,UAAU;AAAA,IACV,KAAK,CAAC,EAAE,KAAK,MAAM,4BAA4B,IAAI;AAAA,IACnD,SAAS;AAAA,IACT,OAAO,QAAQ;AAAA,EACjB,CAAC;AAEH,QAAM,WAAW,SAAU,SAAyC;AAClE,WAAO;AAAA,MACL,eAAe,yBAAyB,OAAO;AAAA,IACjD;AAAA,EACF;AAEA,WAAS,gBAAgB;AACzB,WAAS,qBAAqB;AAE9B,SAAO;AACT;AAKO,IAAM,aAAa,iBAAiB;","names":["import_provider_utils","import_provider_utils","import_zod","_a","_b"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// src/elevenlabs-provider.ts
|
|
2
|
+
import { loadApiKey } from "@ai-sdk/provider-utils";
|
|
3
|
+
|
|
4
|
+
// src/elevenlabs-transcription-model.ts
|
|
5
|
+
import {
|
|
6
|
+
combineHeaders,
|
|
7
|
+
convertBase64ToUint8Array,
|
|
8
|
+
createJsonResponseHandler,
|
|
9
|
+
parseProviderOptions,
|
|
10
|
+
postFormDataToApi
|
|
11
|
+
} from "@ai-sdk/provider-utils";
|
|
12
|
+
import { z as z2 } from "zod";
|
|
13
|
+
|
|
14
|
+
// src/elevenlabs-error.ts
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
17
|
+
var elevenlabsErrorDataSchema = z.object({
|
|
18
|
+
error: z.object({
|
|
19
|
+
message: z.string(),
|
|
20
|
+
code: z.number()
|
|
21
|
+
})
|
|
22
|
+
});
|
|
23
|
+
var elevenlabsFailedResponseHandler = createJsonErrorResponseHandler({
|
|
24
|
+
errorSchema: elevenlabsErrorDataSchema,
|
|
25
|
+
errorToMessage: (data) => data.error.message
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// src/elevenlabs-transcription-model.ts
|
|
29
|
+
var elevenLabsProviderOptionsSchema = z2.object({
|
|
30
|
+
languageCode: z2.string().nullish(),
|
|
31
|
+
tagAudioEvents: z2.boolean().nullish().default(true),
|
|
32
|
+
numSpeakers: z2.number().int().min(1).max(32).nullish(),
|
|
33
|
+
timestampsGranularity: z2.enum(["none", "word", "character"]).nullish().default("word"),
|
|
34
|
+
diarize: z2.boolean().nullish().default(false),
|
|
35
|
+
file_format: z2.enum(["pcm_s16le_16", "other"]).nullish().default("other")
|
|
36
|
+
});
|
|
37
|
+
var ElevenLabsTranscriptionModel = class {
|
|
38
|
+
constructor(modelId, config) {
|
|
39
|
+
this.modelId = modelId;
|
|
40
|
+
this.config = config;
|
|
41
|
+
this.specificationVersion = "v1";
|
|
42
|
+
}
|
|
43
|
+
get provider() {
|
|
44
|
+
return this.config.provider;
|
|
45
|
+
}
|
|
46
|
+
getArgs({
|
|
47
|
+
audio,
|
|
48
|
+
mediaType,
|
|
49
|
+
providerOptions
|
|
50
|
+
}) {
|
|
51
|
+
var _a, _b, _c, _d, _e;
|
|
52
|
+
const warnings = [];
|
|
53
|
+
const elevenlabsOptions = parseProviderOptions({
|
|
54
|
+
provider: "elevenlabs",
|
|
55
|
+
providerOptions,
|
|
56
|
+
schema: elevenLabsProviderOptionsSchema
|
|
57
|
+
});
|
|
58
|
+
const formData = new FormData();
|
|
59
|
+
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
|
|
60
|
+
formData.append("model_id", this.modelId);
|
|
61
|
+
formData.append("file", new File([blob], "audio", { type: mediaType }));
|
|
62
|
+
formData.append("diarize", "true");
|
|
63
|
+
if (elevenlabsOptions) {
|
|
64
|
+
const transcriptionModelOptions = {
|
|
65
|
+
language_code: (_a = elevenlabsOptions.languageCode) != null ? _a : void 0,
|
|
66
|
+
tag_audio_events: (_b = elevenlabsOptions.tagAudioEvents) != null ? _b : void 0,
|
|
67
|
+
num_speakers: (_c = elevenlabsOptions.numSpeakers) != null ? _c : void 0,
|
|
68
|
+
timestamps_granularity: (_d = elevenlabsOptions.timestampsGranularity) != null ? _d : void 0,
|
|
69
|
+
file_format: (_e = elevenlabsOptions.file_format) != null ? _e : void 0
|
|
70
|
+
};
|
|
71
|
+
if (typeof elevenlabsOptions.diarize === "boolean") {
|
|
72
|
+
formData.append("diarize", String(elevenlabsOptions.diarize));
|
|
73
|
+
}
|
|
74
|
+
for (const key in transcriptionModelOptions) {
|
|
75
|
+
const value = transcriptionModelOptions[key];
|
|
76
|
+
if (value !== void 0) {
|
|
77
|
+
formData.append(key, String(value));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
formData,
|
|
83
|
+
warnings
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async doGenerate(options) {
|
|
87
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
88
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
89
|
+
const { formData, warnings } = this.getArgs(options);
|
|
90
|
+
const {
|
|
91
|
+
value: response,
|
|
92
|
+
responseHeaders,
|
|
93
|
+
rawValue: rawResponse
|
|
94
|
+
} = await postFormDataToApi({
|
|
95
|
+
url: this.config.url({
|
|
96
|
+
path: "/v1/speech-to-text",
|
|
97
|
+
modelId: this.modelId
|
|
98
|
+
}),
|
|
99
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
100
|
+
formData,
|
|
101
|
+
failedResponseHandler: elevenlabsFailedResponseHandler,
|
|
102
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
103
|
+
elevenlabsTranscriptionResponseSchema
|
|
104
|
+
),
|
|
105
|
+
abortSignal: options.abortSignal,
|
|
106
|
+
fetch: this.config.fetch
|
|
107
|
+
});
|
|
108
|
+
return {
|
|
109
|
+
text: response.text,
|
|
110
|
+
segments: (_e = (_d = response.words) == null ? void 0 : _d.map((word) => {
|
|
111
|
+
var _a2, _b2;
|
|
112
|
+
return {
|
|
113
|
+
text: word.text,
|
|
114
|
+
startSecond: (_a2 = word.start) != null ? _a2 : 0,
|
|
115
|
+
endSecond: (_b2 = word.end) != null ? _b2 : 0
|
|
116
|
+
};
|
|
117
|
+
})) != null ? _e : [],
|
|
118
|
+
language: response.language_code,
|
|
119
|
+
durationInSeconds: (_h = (_g = (_f = response.words) == null ? void 0 : _f.at(-1)) == null ? void 0 : _g.end) != null ? _h : void 0,
|
|
120
|
+
warnings,
|
|
121
|
+
response: {
|
|
122
|
+
timestamp: currentDate,
|
|
123
|
+
modelId: this.modelId,
|
|
124
|
+
headers: responseHeaders,
|
|
125
|
+
body: rawResponse
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
var elevenlabsTranscriptionResponseSchema = z2.object({
|
|
131
|
+
language_code: z2.string(),
|
|
132
|
+
language_probability: z2.number(),
|
|
133
|
+
text: z2.string(),
|
|
134
|
+
words: z2.array(
|
|
135
|
+
z2.object({
|
|
136
|
+
text: z2.string(),
|
|
137
|
+
type: z2.enum(["word", "spacing", "audio_event"]),
|
|
138
|
+
start: z2.number().nullish(),
|
|
139
|
+
end: z2.number().nullish(),
|
|
140
|
+
speaker_id: z2.string().nullish(),
|
|
141
|
+
characters: z2.array(
|
|
142
|
+
z2.object({
|
|
143
|
+
text: z2.string(),
|
|
144
|
+
start: z2.number().nullish(),
|
|
145
|
+
end: z2.number().nullish()
|
|
146
|
+
})
|
|
147
|
+
).nullish()
|
|
148
|
+
})
|
|
149
|
+
).nullish()
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// src/elevenlabs-provider.ts
|
|
153
|
+
function createElevenLabs(options = {}) {
|
|
154
|
+
const getHeaders = () => ({
|
|
155
|
+
"xi-api-key": loadApiKey({
|
|
156
|
+
apiKey: options.apiKey,
|
|
157
|
+
environmentVariableName: "ELEVENLABS_API_KEY",
|
|
158
|
+
description: "ElevenLabs"
|
|
159
|
+
}),
|
|
160
|
+
...options.headers
|
|
161
|
+
});
|
|
162
|
+
const createTranscriptionModel = (modelId) => new ElevenLabsTranscriptionModel(modelId, {
|
|
163
|
+
provider: `elevenlabs.transcription`,
|
|
164
|
+
url: ({ path }) => `https://api.elevenlabs.io${path}`,
|
|
165
|
+
headers: getHeaders,
|
|
166
|
+
fetch: options.fetch
|
|
167
|
+
});
|
|
168
|
+
const provider = function(modelId) {
|
|
169
|
+
return {
|
|
170
|
+
transcription: createTranscriptionModel(modelId)
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
provider.transcription = createTranscriptionModel;
|
|
174
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
175
|
+
return provider;
|
|
176
|
+
}
|
|
177
|
+
var elevenlabs = createElevenLabs();
|
|
178
|
+
export {
|
|
179
|
+
createElevenLabs,
|
|
180
|
+
elevenlabs
|
|
181
|
+
};
|
|
182
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/elevenlabs-provider.ts","../src/elevenlabs-transcription-model.ts","../src/elevenlabs-error.ts"],"sourcesContent":["import { TranscriptionModelV1, ProviderV1 } from '@ai-sdk/provider';\nimport { FetchFunction, loadApiKey } from '@ai-sdk/provider-utils';\nimport { ElevenLabsTranscriptionModel } from './elevenlabs-transcription-model';\nimport { ElevenLabsTranscriptionModelId } from './elevenlabs-transcription-settings';\n\nexport interface ElevenLabsProvider\n extends Pick<ProviderV1, 'transcriptionModel'> {\n (\n modelId: 'scribe_v1',\n settings?: {},\n ): {\n transcription: ElevenLabsTranscriptionModel;\n };\n\n /**\nCreates a model for transcription.\n */\n transcription(modelId: ElevenLabsTranscriptionModelId): TranscriptionModelV1;\n}\n\nexport interface ElevenLabsProviderSettings {\n /**\nAPI key for authenticating requests.\n */\n apiKey?: string;\n\n /**\nCustom headers to include in the requests.\n */\n headers?: Record<string, string>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\n/**\nCreate an ElevenLabs provider instance.\n */\nexport function createElevenLabs(\n options: ElevenLabsProviderSettings = {},\n): ElevenLabsProvider {\n const getHeaders = () => ({\n 'xi-api-key': loadApiKey({\n apiKey: options.apiKey,\n environmentVariableName: 'ELEVENLABS_API_KEY',\n description: 'ElevenLabs',\n }),\n ...options.headers,\n });\n\n const createTranscriptionModel = (modelId: ElevenLabsTranscriptionModelId) =>\n new ElevenLabsTranscriptionModel(modelId, {\n provider: `elevenlabs.transcription`,\n url: ({ path }) => `https://api.elevenlabs.io${path}`,\n headers: getHeaders,\n fetch: options.fetch,\n });\n\n const provider = function (modelId: ElevenLabsTranscriptionModelId) {\n return {\n transcription: createTranscriptionModel(modelId),\n };\n };\n\n provider.transcription = createTranscriptionModel;\n provider.transcriptionModel = createTranscriptionModel;\n\n return provider as ElevenLabsProvider;\n}\n\n/**\nDefault ElevenLabs provider instance.\n */\nexport const elevenlabs = createElevenLabs();\n","import {\n TranscriptionModelV1,\n TranscriptionModelV1CallOptions,\n TranscriptionModelV1CallWarning,\n} from '@ai-sdk/provider';\nimport {\n combineHeaders,\n convertBase64ToUint8Array,\n createJsonResponseHandler,\n parseProviderOptions,\n postFormDataToApi,\n} from '@ai-sdk/provider-utils';\nimport { z } from 'zod';\nimport { ElevenLabsConfig } from './elevenlabs-config';\nimport { elevenlabsFailedResponseHandler } from './elevenlabs-error';\nimport { ElevenLabsTranscriptionModelId } from './elevenlabs-transcription-settings';\nimport { ElevenLabsTranscriptionAPITypes } from './elevenlabs-api-types';\n\n// https://elevenlabs.io/docs/api-reference/speech-to-text/convert\nconst elevenLabsProviderOptionsSchema = z.object({\n languageCode: z.string().nullish(),\n tagAudioEvents: z.boolean().nullish().default(true),\n numSpeakers: z.number().int().min(1).max(32).nullish(),\n timestampsGranularity: z\n .enum(['none', 'word', 'character'])\n .nullish()\n .default('word'),\n diarize: z.boolean().nullish().default(false),\n file_format: z.enum(['pcm_s16le_16', 'other']).nullish().default('other'),\n});\n\nexport type ElevenLabsTranscriptionCallOptions = z.infer<\n typeof elevenLabsProviderOptionsSchema\n>;\n\ninterface ElevenLabsTranscriptionModelConfig extends ElevenLabsConfig {\n _internal?: {\n currentDate?: () => Date;\n };\n}\n\nexport class ElevenLabsTranscriptionModel implements TranscriptionModelV1 {\n readonly specificationVersion = 'v1';\n\n get provider(): string {\n return this.config.provider;\n }\n\n constructor(\n readonly modelId: ElevenLabsTranscriptionModelId,\n private readonly config: ElevenLabsTranscriptionModelConfig,\n ) {}\n\n private getArgs({\n audio,\n mediaType,\n providerOptions,\n }: Parameters<TranscriptionModelV1['doGenerate']>[0]) {\n const warnings: TranscriptionModelV1CallWarning[] = [];\n\n // Parse provider options\n const elevenlabsOptions = parseProviderOptions({\n provider: 'elevenlabs',\n providerOptions,\n schema: elevenLabsProviderOptionsSchema,\n });\n\n // Create form data with base fields\n const formData = new FormData();\n const blob =\n audio instanceof Uint8Array\n ? new Blob([audio])\n : new Blob([convertBase64ToUint8Array(audio)]);\n\n formData.append('model_id', this.modelId);\n formData.append('file', new File([blob], 'audio', { type: mediaType }));\n formData.append('diarize', 'true');\n\n // Add provider-specific options\n if (elevenlabsOptions) {\n const transcriptionModelOptions: ElevenLabsTranscriptionAPITypes = {\n language_code: elevenlabsOptions.languageCode ?? undefined,\n tag_audio_events: elevenlabsOptions.tagAudioEvents ?? undefined,\n num_speakers: elevenlabsOptions.numSpeakers ?? undefined,\n timestamps_granularity:\n elevenlabsOptions.timestampsGranularity ?? undefined,\n file_format: elevenlabsOptions.file_format ?? undefined,\n };\n\n if (typeof elevenlabsOptions.diarize === 'boolean') {\n formData.append('diarize', String(elevenlabsOptions.diarize));\n }\n\n for (const key in transcriptionModelOptions) {\n const value =\n transcriptionModelOptions[\n key as keyof ElevenLabsTranscriptionAPITypes\n ];\n if (value !== undefined) {\n formData.append(key, String(value));\n }\n }\n }\n\n return {\n formData,\n warnings,\n };\n }\n\n async doGenerate(\n options: Parameters<TranscriptionModelV1['doGenerate']>[0],\n ): Promise<Awaited<ReturnType<TranscriptionModelV1['doGenerate']>>> {\n const currentDate = this.config._internal?.currentDate?.() ?? new Date();\n const { formData, warnings } = this.getArgs(options);\n\n const {\n value: response,\n responseHeaders,\n rawValue: rawResponse,\n } = await postFormDataToApi({\n url: this.config.url({\n path: '/v1/speech-to-text',\n modelId: this.modelId,\n }),\n headers: combineHeaders(this.config.headers(), options.headers),\n formData,\n failedResponseHandler: elevenlabsFailedResponseHandler,\n successfulResponseHandler: createJsonResponseHandler(\n elevenlabsTranscriptionResponseSchema,\n ),\n abortSignal: options.abortSignal,\n fetch: this.config.fetch,\n });\n\n return {\n text: response.text,\n segments:\n response.words?.map(word => ({\n text: word.text,\n startSecond: word.start ?? 0,\n endSecond: word.end ?? 0,\n })) ?? [],\n language: response.language_code,\n durationInSeconds: response.words?.at(-1)?.end ?? undefined,\n warnings,\n response: {\n timestamp: currentDate,\n modelId: this.modelId,\n headers: responseHeaders,\n body: rawResponse,\n },\n };\n }\n}\n\nconst elevenlabsTranscriptionResponseSchema = z.object({\n language_code: z.string(),\n language_probability: z.number(),\n text: z.string(),\n words: z\n .array(\n z.object({\n text: z.string(),\n type: z.enum(['word', 'spacing', 'audio_event']),\n start: z.number().nullish(),\n end: z.number().nullish(),\n speaker_id: z.string().nullish(),\n characters: z\n .array(\n z.object({\n text: z.string(),\n start: z.number().nullish(),\n end: z.number().nullish(),\n }),\n )\n .nullish(),\n }),\n )\n .nullish(),\n});\n","import { z } from 'zod';\nimport { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils';\n\nexport const elevenlabsErrorDataSchema = z.object({\n error: z.object({\n message: z.string(),\n code: z.number(),\n }),\n});\n\nexport type ElevenLabsErrorData = z.infer<typeof elevenlabsErrorDataSchema>;\n\nexport const elevenlabsFailedResponseHandler = createJsonErrorResponseHandler({\n errorSchema: elevenlabsErrorDataSchema,\n errorToMessage: data => data.error.message,\n});\n"],"mappings":";AACA,SAAwB,kBAAkB;;;ACI1C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,KAAAA,UAAS;;;ACZlB,SAAS,SAAS;AAClB,SAAS,sCAAsC;AAExC,IAAM,4BAA4B,EAAE,OAAO;AAAA,EAChD,OAAO,EAAE,OAAO;AAAA,IACd,SAAS,EAAE,OAAO;AAAA,IAClB,MAAM,EAAE,OAAO;AAAA,EACjB,CAAC;AACH,CAAC;AAIM,IAAM,kCAAkC,+BAA+B;AAAA,EAC5E,aAAa;AAAA,EACb,gBAAgB,UAAQ,KAAK,MAAM;AACrC,CAAC;;;ADID,IAAM,kCAAkCC,GAAE,OAAO;AAAA,EAC/C,cAAcA,GAAE,OAAO,EAAE,QAAQ;AAAA,EACjC,gBAAgBA,GAAE,QAAQ,EAAE,QAAQ,EAAE,QAA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AA4B,IAAI;AAAA,IACnD,SAAS;AAAA,IACT,OAAO,QAAQ;AAAA,EACjB,CAAC;AAEH,QAAM,WAAW,SAAU,SAAyC;AAClE,WAAO;AAAA,MACL,eAAe,yBAAyB,OAAO;AAAA,IACjD;AAAA,EACF;AAEA,WAAS,gBAAgB;AACzB,WAAS,qBAAqB;AAE9B,SAAO;AACT;AAKO,IAAM,aAAa,iBAAiB;","names":["z","z","_a","_b"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ai-sdk/elevenlabs",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/**/*",
|
|
11
|
+
"internal/dist/**/*",
|
|
12
|
+
"CHANGELOG.md"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
"./package.json": "./package.json",
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.mjs",
|
|
19
|
+
"require": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@ai-sdk/provider": "1.1.2",
|
|
24
|
+
"@ai-sdk/provider-utils": "2.2.6"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "20.17.24",
|
|
28
|
+
"tsup": "^8",
|
|
29
|
+
"typescript": "5.6.3",
|
|
30
|
+
"zod": "3.23.8",
|
|
31
|
+
"@vercel/ai-tsconfig": "0.0.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"zod": "^3.0.0"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=18"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://sdk.vercel.ai/docs",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/vercel/ai.git"
|
|
46
|
+
},
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/vercel/ai/issues"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"ai"
|
|
52
|
+
],
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsup",
|
|
55
|
+
"build:watch": "tsup --watch",
|
|
56
|
+
"clean": "rm -rf dist && rm -rf internal/dist",
|
|
57
|
+
"lint": "eslint \"./**/*.ts*\"",
|
|
58
|
+
"type-check": "tsc --noEmit",
|
|
59
|
+
"prettier-check": "prettier --check \"./**/*.ts*\"",
|
|
60
|
+
"test": "pnpm test:node && pnpm test:edge",
|
|
61
|
+
"test:edge": "vitest --config vitest.edge.config.js --run",
|
|
62
|
+
"test:node": "vitest --config vitest.node.config.js --run",
|
|
63
|
+
"test:node:watch": "vitest --config vitest.node.config.js --watch"
|
|
64
|
+
}
|
|
65
|
+
}
|