@ai-sdk/gladia 2.0.8 → 2.0.10
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 +12 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/docs/120-gladia.mdx +294 -0
- package/package.json +8 -3
- package/src/gladia-api-types.ts +134 -0
- package/src/gladia-config.ts +9 -0
- package/src/gladia-error.test.ts +34 -0
- package/src/gladia-error.ts +16 -0
- package/src/gladia-provider.ts +117 -0
- package/src/gladia-transcription-model.test.ts +230 -0
- package/src/gladia-transcription-model.ts +652 -0
- package/src/index.ts +3 -0
- package/src/transcript-test.mp3 +0 -0
- package/src/version.ts +6 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { createTestServer } from '@ai-sdk/test-server/with-vitest';
|
|
2
|
+
import { GladiaTranscriptionModel } from './gladia-transcription-model';
|
|
3
|
+
import { createGladia } from './gladia-provider';
|
|
4
|
+
import { readFile } from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
7
|
+
|
|
8
|
+
vi.mock('./version', () => ({
|
|
9
|
+
VERSION: '0.0.0-test',
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
const audioData = await readFile(path.join(__dirname, 'transcript-test.mp3'));
|
|
13
|
+
const provider = createGladia({ apiKey: 'test-api-key' });
|
|
14
|
+
const model = provider.transcription();
|
|
15
|
+
|
|
16
|
+
const server = createTestServer({
|
|
17
|
+
'https://api.gladia.io/v2/upload': {
|
|
18
|
+
response: {
|
|
19
|
+
type: 'json-value',
|
|
20
|
+
body: {
|
|
21
|
+
audio_url: 'https://storage.gladia.io/mock-upload-url',
|
|
22
|
+
audio_metadata: {
|
|
23
|
+
id: 'test-id',
|
|
24
|
+
filename: 'test-file.mp3',
|
|
25
|
+
extension: 'mp3',
|
|
26
|
+
size: 1024,
|
|
27
|
+
audio_duration: 60,
|
|
28
|
+
number_of_channels: 2,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
'https://api.gladia.io/v2/pre-recorded': {},
|
|
34
|
+
'https://api.gladia.io/v2/transcription/test-id': {},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('doGenerate', () => {
|
|
38
|
+
function prepareJsonResponse({
|
|
39
|
+
headers,
|
|
40
|
+
}: {
|
|
41
|
+
headers?: Record<string, string>;
|
|
42
|
+
} = {}) {
|
|
43
|
+
// No need to set the upload response here as it's already set in the server creation
|
|
44
|
+
server.urls['https://api.gladia.io/v2/pre-recorded'].response = {
|
|
45
|
+
type: 'json-value',
|
|
46
|
+
headers,
|
|
47
|
+
body: {
|
|
48
|
+
id: 'test-id',
|
|
49
|
+
result_url: 'https://api.gladia.io/v2/transcription/test-id',
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
server.urls['https://api.gladia.io/v2/transcription/test-id'].response = {
|
|
53
|
+
type: 'json-value',
|
|
54
|
+
headers,
|
|
55
|
+
body: {
|
|
56
|
+
id: '45463597-20b7-4af7-b3b3-f5fb778203ab',
|
|
57
|
+
request_id: 'G-45463597',
|
|
58
|
+
version: 2,
|
|
59
|
+
status: 'done',
|
|
60
|
+
created_at: '2023-12-28T09:04:17.210Z',
|
|
61
|
+
completed_at: '2023-12-28T09:04:37.210Z',
|
|
62
|
+
custom_metadata: {},
|
|
63
|
+
error_code: null,
|
|
64
|
+
kind: 'pre-recorded',
|
|
65
|
+
file: {
|
|
66
|
+
id: 'test-id',
|
|
67
|
+
filename: 'test-file.mp3',
|
|
68
|
+
source: 'upload',
|
|
69
|
+
audio_duration: 60,
|
|
70
|
+
number_of_channels: 2,
|
|
71
|
+
},
|
|
72
|
+
request_params: {
|
|
73
|
+
audio_url: 'https://storage.gladia.io/mock-upload-url',
|
|
74
|
+
},
|
|
75
|
+
result: {
|
|
76
|
+
metadata: {
|
|
77
|
+
audio_duration: 60,
|
|
78
|
+
number_of_distinct_channels: 2,
|
|
79
|
+
billing_time: 60,
|
|
80
|
+
transcription_time: 20,
|
|
81
|
+
},
|
|
82
|
+
transcription: {
|
|
83
|
+
full_transcript: 'Smoke from hundreds of wildfires.',
|
|
84
|
+
languages: ['en'],
|
|
85
|
+
utterances: [
|
|
86
|
+
{
|
|
87
|
+
language: 'en',
|
|
88
|
+
start: 0,
|
|
89
|
+
end: 3,
|
|
90
|
+
confidence: 0.95,
|
|
91
|
+
channel: 1,
|
|
92
|
+
speaker: 1,
|
|
93
|
+
words: [
|
|
94
|
+
{
|
|
95
|
+
word: 'Smoke',
|
|
96
|
+
start: 0,
|
|
97
|
+
end: 1,
|
|
98
|
+
confidence: 0.95,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
word: 'from',
|
|
102
|
+
start: 1,
|
|
103
|
+
end: 2,
|
|
104
|
+
confidence: 0.95,
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
word: 'hundreds',
|
|
108
|
+
start: 2,
|
|
109
|
+
end: 3,
|
|
110
|
+
confidence: 0.95,
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
text: 'Smoke from hundreds of wildfires.',
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
it('should pass the model', async () => {
|
|
123
|
+
prepareJsonResponse();
|
|
124
|
+
|
|
125
|
+
await model.doGenerate({
|
|
126
|
+
audio: audioData,
|
|
127
|
+
mediaType: 'audio/wav',
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
expect(await server.calls[1].requestBodyJson).toMatchObject({
|
|
131
|
+
audio_url: 'https://storage.gladia.io/mock-upload-url',
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should pass headers', async () => {
|
|
136
|
+
prepareJsonResponse();
|
|
137
|
+
|
|
138
|
+
const provider = createGladia({
|
|
139
|
+
apiKey: 'test-api-key',
|
|
140
|
+
headers: {
|
|
141
|
+
'Custom-Provider-Header': 'provider-header-value',
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
await provider.transcription().doGenerate({
|
|
146
|
+
audio: audioData,
|
|
147
|
+
mediaType: 'audio/wav',
|
|
148
|
+
headers: {
|
|
149
|
+
'Custom-Request-Header': 'request-header-value',
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(server.calls[1].requestHeaders).toMatchObject({
|
|
154
|
+
'x-gladia-key': 'test-api-key',
|
|
155
|
+
'content-type': 'application/json',
|
|
156
|
+
'custom-provider-header': 'provider-header-value',
|
|
157
|
+
'custom-request-header': 'request-header-value',
|
|
158
|
+
});
|
|
159
|
+
expect(server.calls[0].requestUserAgent).toContain(
|
|
160
|
+
`ai-sdk/gladia/0.0.0-test`,
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('should extract the transcription text', async () => {
|
|
165
|
+
prepareJsonResponse();
|
|
166
|
+
|
|
167
|
+
const result = await model.doGenerate({
|
|
168
|
+
audio: audioData,
|
|
169
|
+
mediaType: 'audio/wav',
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
expect(result.text).toBe('Smoke from hundreds of wildfires.');
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('should include response data with timestamp, modelId and headers', async () => {
|
|
176
|
+
prepareJsonResponse({
|
|
177
|
+
headers: {
|
|
178
|
+
'x-request-id': 'test-request-id',
|
|
179
|
+
'x-ratelimit-remaining': '123',
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const testDate = new Date(0);
|
|
184
|
+
const customModel = new GladiaTranscriptionModel('default', {
|
|
185
|
+
provider: 'test-provider',
|
|
186
|
+
url: ({ path }) => `https://api.gladia.io${path}`,
|
|
187
|
+
headers: () => ({}),
|
|
188
|
+
_internal: {
|
|
189
|
+
currentDate: () => testDate,
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const result = await customModel.doGenerate({
|
|
194
|
+
audio: audioData,
|
|
195
|
+
mediaType: 'audio/wav',
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(result.response).toMatchObject({
|
|
199
|
+
timestamp: testDate,
|
|
200
|
+
modelId: 'default',
|
|
201
|
+
headers: {
|
|
202
|
+
'content-type': 'application/json',
|
|
203
|
+
'x-request-id': 'test-request-id',
|
|
204
|
+
'x-ratelimit-remaining': '123',
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('should use real date when no custom date provider is specified', async () => {
|
|
210
|
+
prepareJsonResponse();
|
|
211
|
+
|
|
212
|
+
const testDate = new Date(0);
|
|
213
|
+
const customModel = new GladiaTranscriptionModel('default', {
|
|
214
|
+
provider: 'test-provider',
|
|
215
|
+
url: ({ path }) => `https://api.gladia.io${path}`,
|
|
216
|
+
headers: () => ({}),
|
|
217
|
+
_internal: {
|
|
218
|
+
currentDate: () => testDate,
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
const result = await customModel.doGenerate({
|
|
223
|
+
audio: audioData,
|
|
224
|
+
mediaType: 'audio/wav',
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
expect(result.response.timestamp.getTime()).toEqual(testDate.getTime());
|
|
228
|
+
expect(result.response.modelId).toBe('default');
|
|
229
|
+
});
|
|
230
|
+
});
|