@charivo/stt 0.8.1 → 0.9.0
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/README.md +11 -0
- package/dist/gemini/index.d.mts +42 -0
- package/dist/gemini/index.d.ts +42 -0
- package/dist/gemini/index.js +297 -0
- package/dist/gemini/index.mjs +206 -0
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -29,6 +29,17 @@ const text = await sttManager.stop();
|
|
|
29
29
|
- `@charivo/stt/openai`: `createOpenAISTTTranscriber(config)` (browser
|
|
30
30
|
transcriber, dev/testing only) and, for server-side use,
|
|
31
31
|
`createOpenAISTTProvider(config)`, `OpenAISTTProvider`, `type OpenAISTTConfig`
|
|
32
|
+
- `@charivo/stt/gemini`: `createGeminiSTTTranscriber(config)` (browser
|
|
33
|
+
transcriber, dev/testing only) and, for server-side use,
|
|
34
|
+
`createGeminiSTTProvider(config)`, `GeminiSTTProvider`, `type GeminiSTTConfig`.
|
|
35
|
+
The recording is posted inline (base64) to Gemini's
|
|
36
|
+
`models/{model}:generateContent` over `fetch`, with a default model of
|
|
37
|
+
`gemini-3.5-transcribe`. `language` is optional and only a soft hint —
|
|
38
|
+
the model transcribes what it hears even when the hint is wrong. `timeoutMs`
|
|
39
|
+
defaults to 30s and also covers reading the response body. Inline requests
|
|
40
|
+
are capped at 20MB, and there is no streaming: the live transcription model
|
|
41
|
+
is a separate transport. Gemini's free-tier rate limit surfaces as a 429
|
|
42
|
+
wrapped in a provider error.
|
|
32
43
|
- `@charivo/stt/openai-realtime`: `createOpenAIRealtimeSTTTranscriber({ bootstrap })`
|
|
33
44
|
(live WebRTC streaming transcriber, `gpt-realtime-whisper`) — the app injects
|
|
34
45
|
`bootstrap(request) => Promise<{ answerSdp }>`, owns the credentials, and
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { STTProvider, STTOptions, STTTranscriber } from '@charivo/core';
|
|
2
|
+
|
|
3
|
+
interface GeminiSTTConfig {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
defaultModel?: string;
|
|
6
|
+
defaultLanguage?: string;
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
timeoutMs?: number;
|
|
9
|
+
dangerouslyAllowBrowser?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Server-side Gemini STT over `models/{model}:generateContent`.
|
|
13
|
+
*
|
|
14
|
+
* The audio is posted inline as base64, which Google caps at 20MB per request:
|
|
15
|
+
* callers bound the upload before calling, the way the demo's
|
|
16
|
+
* `STT_MAX_AUDIO_BYTES` does.
|
|
17
|
+
*
|
|
18
|
+
* `STTOptions.language` is optional and only a soft hint — the model
|
|
19
|
+
* transcribes what it hears even when the hint is wrong.
|
|
20
|
+
*
|
|
21
|
+
* One request per utterance, with no streaming; the live transcription model is
|
|
22
|
+
* a separate transport. Measured latency is 1.5-3.4s on a 4s clip.
|
|
23
|
+
*
|
|
24
|
+
* The free tier allows 3 requests per minute; beyond that the 429 surfaces as a
|
|
25
|
+
* CharivoProviderError carrying Google's retry hint.
|
|
26
|
+
*/
|
|
27
|
+
declare class GeminiSTTProvider implements STTProvider {
|
|
28
|
+
private apiKey;
|
|
29
|
+
private baseUrl;
|
|
30
|
+
private model;
|
|
31
|
+
private defaultLanguage?;
|
|
32
|
+
private timeoutMs;
|
|
33
|
+
private timeoutMessage;
|
|
34
|
+
constructor(config: GeminiSTTConfig);
|
|
35
|
+
transcribe(audio: Blob | ArrayBuffer, options?: STTOptions): Promise<string>;
|
|
36
|
+
}
|
|
37
|
+
declare function createGeminiSTTProvider(config: GeminiSTTConfig): GeminiSTTProvider;
|
|
38
|
+
|
|
39
|
+
type GeminiSTTTranscriberConfig = GeminiSTTConfig;
|
|
40
|
+
declare function createGeminiSTTTranscriber(config: GeminiSTTTranscriberConfig): STTTranscriber;
|
|
41
|
+
|
|
42
|
+
export { type GeminiSTTConfig, GeminiSTTProvider, type GeminiSTTTranscriberConfig, createGeminiSTTProvider, createGeminiSTTTranscriber };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { STTProvider, STTOptions, STTTranscriber } from '@charivo/core';
|
|
2
|
+
|
|
3
|
+
interface GeminiSTTConfig {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
defaultModel?: string;
|
|
6
|
+
defaultLanguage?: string;
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
timeoutMs?: number;
|
|
9
|
+
dangerouslyAllowBrowser?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Server-side Gemini STT over `models/{model}:generateContent`.
|
|
13
|
+
*
|
|
14
|
+
* The audio is posted inline as base64, which Google caps at 20MB per request:
|
|
15
|
+
* callers bound the upload before calling, the way the demo's
|
|
16
|
+
* `STT_MAX_AUDIO_BYTES` does.
|
|
17
|
+
*
|
|
18
|
+
* `STTOptions.language` is optional and only a soft hint — the model
|
|
19
|
+
* transcribes what it hears even when the hint is wrong.
|
|
20
|
+
*
|
|
21
|
+
* One request per utterance, with no streaming; the live transcription model is
|
|
22
|
+
* a separate transport. Measured latency is 1.5-3.4s on a 4s clip.
|
|
23
|
+
*
|
|
24
|
+
* The free tier allows 3 requests per minute; beyond that the 429 surfaces as a
|
|
25
|
+
* CharivoProviderError carrying Google's retry hint.
|
|
26
|
+
*/
|
|
27
|
+
declare class GeminiSTTProvider implements STTProvider {
|
|
28
|
+
private apiKey;
|
|
29
|
+
private baseUrl;
|
|
30
|
+
private model;
|
|
31
|
+
private defaultLanguage?;
|
|
32
|
+
private timeoutMs;
|
|
33
|
+
private timeoutMessage;
|
|
34
|
+
constructor(config: GeminiSTTConfig);
|
|
35
|
+
transcribe(audio: Blob | ArrayBuffer, options?: STTOptions): Promise<string>;
|
|
36
|
+
}
|
|
37
|
+
declare function createGeminiSTTProvider(config: GeminiSTTConfig): GeminiSTTProvider;
|
|
38
|
+
|
|
39
|
+
type GeminiSTTTranscriberConfig = GeminiSTTConfig;
|
|
40
|
+
declare function createGeminiSTTTranscriber(config: GeminiSTTTranscriberConfig): STTTranscriber;
|
|
41
|
+
|
|
42
|
+
export { type GeminiSTTConfig, GeminiSTTProvider, type GeminiSTTTranscriberConfig, createGeminiSTTProvider, createGeminiSTTTranscriber };
|
|
@@ -0,0 +1,297 @@
|
|
|
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/gemini/index.ts
|
|
21
|
+
var gemini_exports = {};
|
|
22
|
+
__export(gemini_exports, {
|
|
23
|
+
GeminiSTTProvider: () => GeminiSTTProvider,
|
|
24
|
+
createGeminiSTTProvider: () => createGeminiSTTProvider,
|
|
25
|
+
createGeminiSTTTranscriber: () => createGeminiSTTTranscriber
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(gemini_exports);
|
|
28
|
+
|
|
29
|
+
// src/media-recorder-helper.ts
|
|
30
|
+
var MediaRecorderHelper = class {
|
|
31
|
+
mediaRecorder = null;
|
|
32
|
+
audioChunks = [];
|
|
33
|
+
recording = false;
|
|
34
|
+
/**
|
|
35
|
+
* Start audio recording from user's microphone
|
|
36
|
+
*/
|
|
37
|
+
async start() {
|
|
38
|
+
if (this.recording) {
|
|
39
|
+
throw new Error("Already recording");
|
|
40
|
+
}
|
|
41
|
+
const stream = await navigator.mediaDevices.getUserMedia({
|
|
42
|
+
audio: {
|
|
43
|
+
echoCancellation: true,
|
|
44
|
+
noiseSuppression: true,
|
|
45
|
+
autoGainControl: true
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
this.mediaRecorder = new MediaRecorder(stream);
|
|
49
|
+
this.audioChunks = [];
|
|
50
|
+
this.mediaRecorder.ondataavailable = (event) => {
|
|
51
|
+
if (event.data.size > 0) {
|
|
52
|
+
this.audioChunks.push(event.data);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
this.mediaRecorder.start();
|
|
56
|
+
this.recording = true;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Stop audio recording and return the recorded audio as a Blob
|
|
60
|
+
*/
|
|
61
|
+
async stop() {
|
|
62
|
+
if (!this.recording || !this.mediaRecorder) {
|
|
63
|
+
throw new Error("Not recording");
|
|
64
|
+
}
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
this.mediaRecorder.onstop = () => {
|
|
67
|
+
const audioBlob = new Blob(this.audioChunks, { type: "audio/webm" });
|
|
68
|
+
this.audioChunks = [];
|
|
69
|
+
this.mediaRecorder?.stream.getTracks().forEach((track) => track.stop());
|
|
70
|
+
this.mediaRecorder = null;
|
|
71
|
+
this.recording = false;
|
|
72
|
+
resolve(audioBlob);
|
|
73
|
+
};
|
|
74
|
+
this.mediaRecorder.onerror = (error) => {
|
|
75
|
+
this.recording = false;
|
|
76
|
+
reject(error);
|
|
77
|
+
};
|
|
78
|
+
this.mediaRecorder.stop();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Check if currently recording
|
|
83
|
+
*/
|
|
84
|
+
isRecording() {
|
|
85
|
+
return this.recording;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Abort recording immediately without returning data
|
|
89
|
+
*/
|
|
90
|
+
abort() {
|
|
91
|
+
if (this.mediaRecorder) {
|
|
92
|
+
this.mediaRecorder.stream.getTracks().forEach((track) => track.stop());
|
|
93
|
+
this.mediaRecorder = null;
|
|
94
|
+
this.audioChunks = [];
|
|
95
|
+
this.recording = false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/gemini/provider.ts
|
|
101
|
+
var import_core = require("@charivo/core");
|
|
102
|
+
var DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com";
|
|
103
|
+
var DEFAULT_MODEL = "gemini-3.5-transcribe";
|
|
104
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
105
|
+
var FALLBACK_MIME_TYPE = "audio/wav";
|
|
106
|
+
var BASE64_CHUNK_BYTES = 32768;
|
|
107
|
+
var GeminiSTTProvider = class {
|
|
108
|
+
apiKey;
|
|
109
|
+
baseUrl;
|
|
110
|
+
model;
|
|
111
|
+
defaultLanguage;
|
|
112
|
+
timeoutMs;
|
|
113
|
+
timeoutMessage;
|
|
114
|
+
constructor(config) {
|
|
115
|
+
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
116
|
+
throw new import_core.CharivoStateError(
|
|
117
|
+
"Gemini STT provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing"
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
this.apiKey = config.apiKey;
|
|
121
|
+
this.baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\/$/, "");
|
|
122
|
+
this.model = config.defaultModel || DEFAULT_MODEL;
|
|
123
|
+
this.defaultLanguage = config.defaultLanguage;
|
|
124
|
+
this.timeoutMs = config.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
125
|
+
this.timeoutMessage = `Gemini STT request timed out after ${this.timeoutMs}ms`;
|
|
126
|
+
}
|
|
127
|
+
async transcribe(audio, options) {
|
|
128
|
+
const bytes = audio instanceof Blob ? new Uint8Array(await audio.arrayBuffer()) : new Uint8Array(audio);
|
|
129
|
+
const mimeType = audio instanceof Blob && audio.type ? audio.type : FALLBACK_MIME_TYPE;
|
|
130
|
+
const language = options?.language || this.defaultLanguage;
|
|
131
|
+
const url = `${this.baseUrl}/v1beta/models/${this.model}:generateContent`;
|
|
132
|
+
const body = JSON.stringify({
|
|
133
|
+
contents: [
|
|
134
|
+
{ parts: [{ inlineData: { mimeType, data: encodeBase64(bytes) } }] }
|
|
135
|
+
],
|
|
136
|
+
...language ? {
|
|
137
|
+
generationConfig: {
|
|
138
|
+
audioTranscriptionConfig: { languageCodes: [language] }
|
|
139
|
+
}
|
|
140
|
+
} : {}
|
|
141
|
+
});
|
|
142
|
+
try {
|
|
143
|
+
return await (0, import_core.fetchWithTimeout)(
|
|
144
|
+
url,
|
|
145
|
+
{
|
|
146
|
+
method: "POST",
|
|
147
|
+
headers: {
|
|
148
|
+
// Never in the URL: proxies and request logs capture query strings.
|
|
149
|
+
"x-goog-api-key": this.apiKey,
|
|
150
|
+
"Content-Type": "application/json"
|
|
151
|
+
},
|
|
152
|
+
body
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
timeoutMs: this.timeoutMs,
|
|
156
|
+
timeoutMessage: this.timeoutMessage,
|
|
157
|
+
// DNS/TLS/connection failures land here as raw fetch errors — map
|
|
158
|
+
// them so every failure escaping this provider is a CharivoError.
|
|
159
|
+
mapError: (error) => (0, import_core.toCharivoError)("provider", error, "Gemini STT request failed")
|
|
160
|
+
},
|
|
161
|
+
// Consumed inside the helper so the timeout also covers downloading and
|
|
162
|
+
// parsing the body.
|
|
163
|
+
async (response) => {
|
|
164
|
+
if (!response.ok) {
|
|
165
|
+
throw new import_core.CharivoProviderError(
|
|
166
|
+
`Gemini STT Error: ${await readResponseText(response)}`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return extractTranscript(await readResponseJson(response));
|
|
170
|
+
}
|
|
171
|
+
);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
throw (0, import_core.toCharivoError)("provider", error, "Gemini STT request failed");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
function createGeminiSTTProvider(config) {
|
|
178
|
+
return new GeminiSTTProvider(config);
|
|
179
|
+
}
|
|
180
|
+
function extractTranscript(payload) {
|
|
181
|
+
const candidates = isRecord(payload) && Array.isArray(payload.candidates) ? payload.candidates : [];
|
|
182
|
+
const candidate = candidates[0];
|
|
183
|
+
if (!isRecord(candidate) || !isRecord(candidate.content)) {
|
|
184
|
+
throw new import_core.CharivoProviderError(
|
|
185
|
+
"Gemini STT Error: response contained no transcription"
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
if (!Array.isArray(candidate.content.parts) || candidate.content.parts.length === 0) {
|
|
189
|
+
return "";
|
|
190
|
+
}
|
|
191
|
+
const texts = [];
|
|
192
|
+
for (const part of candidate.content.parts) {
|
|
193
|
+
if (isRecord(part) && isRecord(part.audioTranscription)) {
|
|
194
|
+
const { text } = part.audioTranscription;
|
|
195
|
+
if (typeof text === "string") {
|
|
196
|
+
texts.push(text);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (texts.length > 0) {
|
|
201
|
+
return texts.join("");
|
|
202
|
+
}
|
|
203
|
+
throw new import_core.CharivoProviderError(
|
|
204
|
+
"Gemini STT Error: response contained no transcription"
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
function encodeBase64(bytes) {
|
|
208
|
+
const chunks = [];
|
|
209
|
+
for (let i = 0; i < bytes.length; i += BASE64_CHUNK_BYTES) {
|
|
210
|
+
chunks.push(
|
|
211
|
+
String.fromCharCode(...bytes.subarray(i, i + BASE64_CHUNK_BYTES))
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
return btoa(chunks.join(""));
|
|
215
|
+
}
|
|
216
|
+
function isRecord(value) {
|
|
217
|
+
return typeof value === "object" && value !== null;
|
|
218
|
+
}
|
|
219
|
+
async function readResponseText(response) {
|
|
220
|
+
try {
|
|
221
|
+
return await response.text();
|
|
222
|
+
} catch (error) {
|
|
223
|
+
if (isAbortError(error)) {
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
throw (0, import_core.toCharivoError)(
|
|
227
|
+
"provider",
|
|
228
|
+
error,
|
|
229
|
+
"Failed to read Gemini STT response body"
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
async function readResponseJson(response) {
|
|
234
|
+
try {
|
|
235
|
+
return await response.json();
|
|
236
|
+
} catch (error) {
|
|
237
|
+
if (isAbortError(error)) {
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
throw (0, import_core.toCharivoError)(
|
|
241
|
+
"provider",
|
|
242
|
+
error,
|
|
243
|
+
"Failed to parse Gemini STT response body"
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function isAbortError(error) {
|
|
248
|
+
return error instanceof DOMException && error.name === "AbortError" || error instanceof Error && error.name === "AbortError";
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// src/gemini/index.ts
|
|
252
|
+
var GeminiSTTTranscriber = class {
|
|
253
|
+
provider;
|
|
254
|
+
recorder;
|
|
255
|
+
recordingOptions;
|
|
256
|
+
constructor(config) {
|
|
257
|
+
this.provider = createGeminiSTTProvider({
|
|
258
|
+
...config,
|
|
259
|
+
dangerouslyAllowBrowser: true
|
|
260
|
+
});
|
|
261
|
+
this.recorder = new MediaRecorderHelper();
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Start recording audio from microphone
|
|
265
|
+
*/
|
|
266
|
+
async startRecording(options) {
|
|
267
|
+
this.recordingOptions = options;
|
|
268
|
+
await this.recorder.start();
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Stop recording and transcribe audio to text
|
|
272
|
+
*/
|
|
273
|
+
async stopRecording() {
|
|
274
|
+
const audioBlob = await this.recorder.stop();
|
|
275
|
+
const transcription = await this.provider.transcribe(
|
|
276
|
+
audioBlob,
|
|
277
|
+
this.recordingOptions
|
|
278
|
+
);
|
|
279
|
+
this.recordingOptions = void 0;
|
|
280
|
+
return transcription;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Check if currently recording
|
|
284
|
+
*/
|
|
285
|
+
isRecording() {
|
|
286
|
+
return this.recorder.isRecording();
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
function createGeminiSTTTranscriber(config) {
|
|
290
|
+
return new GeminiSTTTranscriber(config);
|
|
291
|
+
}
|
|
292
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
293
|
+
0 && (module.exports = {
|
|
294
|
+
GeminiSTTProvider,
|
|
295
|
+
createGeminiSTTProvider,
|
|
296
|
+
createGeminiSTTTranscriber
|
|
297
|
+
});
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MediaRecorderHelper
|
|
3
|
+
} from "../chunk-YUPKYMAU.mjs";
|
|
4
|
+
|
|
5
|
+
// src/gemini/provider.ts
|
|
6
|
+
import {
|
|
7
|
+
CharivoProviderError,
|
|
8
|
+
CharivoStateError,
|
|
9
|
+
fetchWithTimeout,
|
|
10
|
+
toCharivoError
|
|
11
|
+
} from "@charivo/core";
|
|
12
|
+
var DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com";
|
|
13
|
+
var DEFAULT_MODEL = "gemini-3.5-transcribe";
|
|
14
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
15
|
+
var FALLBACK_MIME_TYPE = "audio/wav";
|
|
16
|
+
var BASE64_CHUNK_BYTES = 32768;
|
|
17
|
+
var GeminiSTTProvider = class {
|
|
18
|
+
apiKey;
|
|
19
|
+
baseUrl;
|
|
20
|
+
model;
|
|
21
|
+
defaultLanguage;
|
|
22
|
+
timeoutMs;
|
|
23
|
+
timeoutMessage;
|
|
24
|
+
constructor(config) {
|
|
25
|
+
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
26
|
+
throw new CharivoStateError(
|
|
27
|
+
"Gemini STT provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing"
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
this.apiKey = config.apiKey;
|
|
31
|
+
this.baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\/$/, "");
|
|
32
|
+
this.model = config.defaultModel || DEFAULT_MODEL;
|
|
33
|
+
this.defaultLanguage = config.defaultLanguage;
|
|
34
|
+
this.timeoutMs = config.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
35
|
+
this.timeoutMessage = `Gemini STT request timed out after ${this.timeoutMs}ms`;
|
|
36
|
+
}
|
|
37
|
+
async transcribe(audio, options) {
|
|
38
|
+
const bytes = audio instanceof Blob ? new Uint8Array(await audio.arrayBuffer()) : new Uint8Array(audio);
|
|
39
|
+
const mimeType = audio instanceof Blob && audio.type ? audio.type : FALLBACK_MIME_TYPE;
|
|
40
|
+
const language = options?.language || this.defaultLanguage;
|
|
41
|
+
const url = `${this.baseUrl}/v1beta/models/${this.model}:generateContent`;
|
|
42
|
+
const body = JSON.stringify({
|
|
43
|
+
contents: [
|
|
44
|
+
{ parts: [{ inlineData: { mimeType, data: encodeBase64(bytes) } }] }
|
|
45
|
+
],
|
|
46
|
+
...language ? {
|
|
47
|
+
generationConfig: {
|
|
48
|
+
audioTranscriptionConfig: { languageCodes: [language] }
|
|
49
|
+
}
|
|
50
|
+
} : {}
|
|
51
|
+
});
|
|
52
|
+
try {
|
|
53
|
+
return await fetchWithTimeout(
|
|
54
|
+
url,
|
|
55
|
+
{
|
|
56
|
+
method: "POST",
|
|
57
|
+
headers: {
|
|
58
|
+
// Never in the URL: proxies and request logs capture query strings.
|
|
59
|
+
"x-goog-api-key": this.apiKey,
|
|
60
|
+
"Content-Type": "application/json"
|
|
61
|
+
},
|
|
62
|
+
body
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
timeoutMs: this.timeoutMs,
|
|
66
|
+
timeoutMessage: this.timeoutMessage,
|
|
67
|
+
// DNS/TLS/connection failures land here as raw fetch errors — map
|
|
68
|
+
// them so every failure escaping this provider is a CharivoError.
|
|
69
|
+
mapError: (error) => toCharivoError("provider", error, "Gemini STT request failed")
|
|
70
|
+
},
|
|
71
|
+
// Consumed inside the helper so the timeout also covers downloading and
|
|
72
|
+
// parsing the body.
|
|
73
|
+
async (response) => {
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
throw new CharivoProviderError(
|
|
76
|
+
`Gemini STT Error: ${await readResponseText(response)}`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return extractTranscript(await readResponseJson(response));
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
throw toCharivoError("provider", error, "Gemini STT request failed");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
function createGeminiSTTProvider(config) {
|
|
88
|
+
return new GeminiSTTProvider(config);
|
|
89
|
+
}
|
|
90
|
+
function extractTranscript(payload) {
|
|
91
|
+
const candidates = isRecord(payload) && Array.isArray(payload.candidates) ? payload.candidates : [];
|
|
92
|
+
const candidate = candidates[0];
|
|
93
|
+
if (!isRecord(candidate) || !isRecord(candidate.content)) {
|
|
94
|
+
throw new CharivoProviderError(
|
|
95
|
+
"Gemini STT Error: response contained no transcription"
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
if (!Array.isArray(candidate.content.parts) || candidate.content.parts.length === 0) {
|
|
99
|
+
return "";
|
|
100
|
+
}
|
|
101
|
+
const texts = [];
|
|
102
|
+
for (const part of candidate.content.parts) {
|
|
103
|
+
if (isRecord(part) && isRecord(part.audioTranscription)) {
|
|
104
|
+
const { text } = part.audioTranscription;
|
|
105
|
+
if (typeof text === "string") {
|
|
106
|
+
texts.push(text);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (texts.length > 0) {
|
|
111
|
+
return texts.join("");
|
|
112
|
+
}
|
|
113
|
+
throw new CharivoProviderError(
|
|
114
|
+
"Gemini STT Error: response contained no transcription"
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
function encodeBase64(bytes) {
|
|
118
|
+
const chunks = [];
|
|
119
|
+
for (let i = 0; i < bytes.length; i += BASE64_CHUNK_BYTES) {
|
|
120
|
+
chunks.push(
|
|
121
|
+
String.fromCharCode(...bytes.subarray(i, i + BASE64_CHUNK_BYTES))
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return btoa(chunks.join(""));
|
|
125
|
+
}
|
|
126
|
+
function isRecord(value) {
|
|
127
|
+
return typeof value === "object" && value !== null;
|
|
128
|
+
}
|
|
129
|
+
async function readResponseText(response) {
|
|
130
|
+
try {
|
|
131
|
+
return await response.text();
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (isAbortError(error)) {
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
throw toCharivoError(
|
|
137
|
+
"provider",
|
|
138
|
+
error,
|
|
139
|
+
"Failed to read Gemini STT response body"
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function readResponseJson(response) {
|
|
144
|
+
try {
|
|
145
|
+
return await response.json();
|
|
146
|
+
} catch (error) {
|
|
147
|
+
if (isAbortError(error)) {
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
throw toCharivoError(
|
|
151
|
+
"provider",
|
|
152
|
+
error,
|
|
153
|
+
"Failed to parse Gemini STT response body"
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function isAbortError(error) {
|
|
158
|
+
return error instanceof DOMException && error.name === "AbortError" || error instanceof Error && error.name === "AbortError";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// src/gemini/index.ts
|
|
162
|
+
var GeminiSTTTranscriber = class {
|
|
163
|
+
provider;
|
|
164
|
+
recorder;
|
|
165
|
+
recordingOptions;
|
|
166
|
+
constructor(config) {
|
|
167
|
+
this.provider = createGeminiSTTProvider({
|
|
168
|
+
...config,
|
|
169
|
+
dangerouslyAllowBrowser: true
|
|
170
|
+
});
|
|
171
|
+
this.recorder = new MediaRecorderHelper();
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Start recording audio from microphone
|
|
175
|
+
*/
|
|
176
|
+
async startRecording(options) {
|
|
177
|
+
this.recordingOptions = options;
|
|
178
|
+
await this.recorder.start();
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Stop recording and transcribe audio to text
|
|
182
|
+
*/
|
|
183
|
+
async stopRecording() {
|
|
184
|
+
const audioBlob = await this.recorder.stop();
|
|
185
|
+
const transcription = await this.provider.transcribe(
|
|
186
|
+
audioBlob,
|
|
187
|
+
this.recordingOptions
|
|
188
|
+
);
|
|
189
|
+
this.recordingOptions = void 0;
|
|
190
|
+
return transcription;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Check if currently recording
|
|
194
|
+
*/
|
|
195
|
+
isRecording() {
|
|
196
|
+
return this.recorder.isRecording();
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
function createGeminiSTTTranscriber(config) {
|
|
200
|
+
return new GeminiSTTTranscriber(config);
|
|
201
|
+
}
|
|
202
|
+
export {
|
|
203
|
+
GeminiSTTProvider,
|
|
204
|
+
createGeminiSTTProvider,
|
|
205
|
+
createGeminiSTTTranscriber
|
|
206
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@charivo/stt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "STT manager and browser adapters for Charivo",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -21,6 +21,11 @@
|
|
|
21
21
|
"import": "./dist/openai/index.mjs",
|
|
22
22
|
"require": "./dist/openai/index.js"
|
|
23
23
|
},
|
|
24
|
+
"./gemini": {
|
|
25
|
+
"types": "./dist/gemini/index.d.ts",
|
|
26
|
+
"import": "./dist/gemini/index.mjs",
|
|
27
|
+
"require": "./dist/gemini/index.js"
|
|
28
|
+
},
|
|
24
29
|
"./openai-realtime": {
|
|
25
30
|
"types": "./dist/openai-realtime/index.d.ts",
|
|
26
31
|
"import": "./dist/openai-realtime/index.mjs",
|