levix-bot 4.2.0 → 4.2.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/package.json +1 -1
- package/src/commands/stt.cjs +10 -6
- package/src/services/aiAgent.cjs +7 -11
- package/src/utils/geminiMedia.cjs +70 -1
package/package.json
CHANGED
package/src/commands/stt.cjs
CHANGED
|
@@ -4,7 +4,12 @@ const { downloadContentFromMessage } = require("@whiskeysockets/baileys");
|
|
|
4
4
|
const logger = require("../utils/logger.cjs");
|
|
5
5
|
const { createStatus } = require("../utils/statusMessage.cjs");
|
|
6
6
|
const { unwrapMessage } = require("../utils/messageContent.cjs");
|
|
7
|
-
const {
|
|
7
|
+
const {
|
|
8
|
+
INLINE_MEDIA_LIMIT,
|
|
9
|
+
baseMimeType,
|
|
10
|
+
downloadMedia,
|
|
11
|
+
uploadToGemini,
|
|
12
|
+
} = require("../utils/geminiMedia.cjs");
|
|
8
13
|
|
|
9
14
|
const settings = require("../config/settings.cjs");
|
|
10
15
|
const {
|
|
@@ -63,10 +68,9 @@ const TRANSCRIBE_PROMPT =
|
|
|
63
68
|
"Please transcribe this audio message accurately. Return ONLY the transcription text without any additional commentary, explanations, or formatting. Just the raw transcribed text.";
|
|
64
69
|
|
|
65
70
|
// A voice note is a few hundred KB, so it rides inline in the request: one
|
|
66
|
-
// round trip, instead of an upload, a finalize and then the request. Past
|
|
67
|
-
//
|
|
68
|
-
// API instead.
|
|
69
|
-
const INLINE_AUDIO_LIMIT = 15 * 1024 * 1024;
|
|
71
|
+
// round trip, instead of an upload, a finalize and then the request. Past
|
|
72
|
+
// INLINE_MEDIA_LIMIT (Gemini caps a whole inline request at 20 MB) it goes
|
|
73
|
+
// through the Files API instead.
|
|
70
74
|
|
|
71
75
|
async function transcribeWithGemini(audioBuffer, mimetype) {
|
|
72
76
|
const gemini = geminiStt();
|
|
@@ -82,7 +86,7 @@ async function transcribeWithGemini(audioBuffer, mimetype) {
|
|
|
82
86
|
// `audio/ogg`, and the codec parameter is not part of it.
|
|
83
87
|
const mimeType = baseMimeType(mimetype, "audio");
|
|
84
88
|
let audioPart;
|
|
85
|
-
if (audioBuffer.length <=
|
|
89
|
+
if (audioBuffer.length <= INLINE_MEDIA_LIMIT) {
|
|
86
90
|
audioPart = { inlineData: { mimeType, data: audioBuffer.toString("base64") } };
|
|
87
91
|
} else {
|
|
88
92
|
const file = await uploadToGemini(gemini.genAI, audioBuffer, mimeType, {
|
package/src/services/aiAgent.cjs
CHANGED
|
@@ -21,7 +21,7 @@ const {
|
|
|
21
21
|
mediaKind,
|
|
22
22
|
baseMimeType,
|
|
23
23
|
downloadMedia,
|
|
24
|
-
|
|
24
|
+
geminiMediaPart,
|
|
25
25
|
} = require("../utils/geminiMedia.cjs");
|
|
26
26
|
const { tr } = require("../utils/i18n.cjs");
|
|
27
27
|
const aiIdentity = require("../config/ai-identity.cjs");
|
|
@@ -455,23 +455,19 @@ class GeminiProvider extends BaseAIProvider {
|
|
|
455
455
|
return null;
|
|
456
456
|
}
|
|
457
457
|
|
|
458
|
-
//
|
|
459
|
-
//
|
|
460
|
-
// mimeType".
|
|
458
|
+
// Uploaded through the Files API, or inline when the configured endpoint
|
|
459
|
+
// has none (a gateway / reverse proxy) — see utils/geminiMedia.cjs.
|
|
461
460
|
const mimeType = baseMimeType(rawMime, kind);
|
|
462
461
|
const buffer = await downloadMedia(
|
|
463
462
|
downloadContentFromMessage,
|
|
464
463
|
mediaMessage,
|
|
465
464
|
downloadType || kind,
|
|
466
465
|
);
|
|
467
|
-
const
|
|
468
|
-
|
|
469
|
-
fileData: {
|
|
470
|
-
fileUri: file.uri,
|
|
471
|
-
mimeType: file.mimeType || mimeType,
|
|
472
|
-
},
|
|
466
|
+
const part = await geminiMediaPart(genAI, buffer, mimeType, {
|
|
467
|
+
baseUrl: settings.get("gemini_base_url"),
|
|
473
468
|
});
|
|
474
|
-
|
|
469
|
+
parts.push(part);
|
|
470
|
+
return part.fileData?.fileUri || `inline:${kind}`;
|
|
475
471
|
}
|
|
476
472
|
|
|
477
473
|
async runTurn(options) {
|
|
@@ -14,6 +14,18 @@
|
|
|
14
14
|
// Nothing here touches the disk. The bytes go up as a Blob, so there is no temp
|
|
15
15
|
// file written next to the code (which a global install, a packaged binary or
|
|
16
16
|
// the Android build may not even allow) and nothing to clean up afterwards.
|
|
17
|
+
//
|
|
18
|
+
// And not every Gemini endpoint has a Files API. A gateway or reverse proxy set
|
|
19
|
+
// as `gemini_base_url` often forwards generateContent and nothing else — and
|
|
20
|
+
// even one that forwards everything breaks if it lives under a path: the SDK
|
|
21
|
+
// takes the upload URL Google hands back and swaps in the base URL's host only,
|
|
22
|
+
// so `https://proxy.example/gemini` uploads to `https://proxy.example/upload/…`
|
|
23
|
+
// and gets the proxy's own 404. Text and voice notes worked (they ride inside
|
|
24
|
+
// the request); every photo failed. geminiMediaPart() tries the upload, and
|
|
25
|
+
// when the endpoint isn't there sends the bytes inline instead — and remembers,
|
|
26
|
+
// so the next photo doesn't pay for the failed upload again.
|
|
27
|
+
|
|
28
|
+
const logger = require("./logger.cjs");
|
|
17
29
|
|
|
18
30
|
// Used only when WhatsApp sent no MIME type at all.
|
|
19
31
|
const FALLBACK_MIME = {
|
|
@@ -23,6 +35,10 @@ const FALLBACK_MIME = {
|
|
|
23
35
|
document: "application/octet-stream",
|
|
24
36
|
};
|
|
25
37
|
|
|
38
|
+
// Gemini caps a whole inline request at 20 MB; media past this goes through the
|
|
39
|
+
// Files API or not at all.
|
|
40
|
+
const INLINE_MEDIA_LIMIT = 15 * 1024 * 1024;
|
|
41
|
+
|
|
26
42
|
// A video is usable only once Google has processed it; images and audio are
|
|
27
43
|
// normally ACTIVE straight away.
|
|
28
44
|
const PROCESSING_POLL_MS = 1500;
|
|
@@ -96,4 +112,57 @@ async function uploadToGemini(
|
|
|
96
112
|
return file;
|
|
97
113
|
}
|
|
98
114
|
|
|
99
|
-
|
|
115
|
+
/**
|
|
116
|
+
* True when an upload failed because the endpoint has no Files API — not
|
|
117
|
+
* because of the key, the quota or the file. A 404/405/501, an HTML page where
|
|
118
|
+
* JSON belongs, or the SDK's own complaints about the upload handshake.
|
|
119
|
+
*/
|
|
120
|
+
function isMissingUploadEndpoint(error) {
|
|
121
|
+
const status = Number(error?.status || error?.code || error?.response?.status);
|
|
122
|
+
if ([404, 405, 501].includes(status)) return true;
|
|
123
|
+
const message = String(error?.message || "");
|
|
124
|
+
return /<html|upload url|upload status is not finalized/i.test(message);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Base URLs whose Files API turned out to be missing. In memory on purpose: a
|
|
128
|
+
// restart (or a changed base URL) tries the upload again.
|
|
129
|
+
const inlineOnly = new Set();
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The part that puts `buffer` in front of Gemini: a `fileData` reference when
|
|
133
|
+
* the Files API is there, else the bytes inline.
|
|
134
|
+
*
|
|
135
|
+
* Inline media rides along in the stored conversation, the way the
|
|
136
|
+
* OpenAI-compatible and Anthropic providers already keep their images, so a
|
|
137
|
+
* follow-up question can still see the photo.
|
|
138
|
+
*/
|
|
139
|
+
async function geminiMediaPart(genAI, buffer, mimeType, { baseUrl = "", ...uploadOptions } = {}) {
|
|
140
|
+
const key = baseUrl || "(default)";
|
|
141
|
+
const inline = () => ({ inlineData: { mimeType, data: buffer.toString("base64") } });
|
|
142
|
+
const fitsInline = buffer.length <= INLINE_MEDIA_LIMIT;
|
|
143
|
+
|
|
144
|
+
if (inlineOnly.has(key) && fitsInline) return inline();
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const file = await uploadToGemini(genAI, buffer, mimeType, uploadOptions);
|
|
148
|
+
return { fileData: { fileUri: file.uri, mimeType: file.mimeType || mimeType } };
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (!fitsInline || !isMissingUploadEndpoint(error)) throw error;
|
|
151
|
+
inlineOnly.add(key);
|
|
152
|
+
logger.warn(
|
|
153
|
+
{ status: error?.status, baseUrl: baseUrl || null },
|
|
154
|
+
"[geminiMedia] this Gemini endpoint has no Files API — sending media inline from now on",
|
|
155
|
+
);
|
|
156
|
+
return inline();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
module.exports = {
|
|
161
|
+
INLINE_MEDIA_LIMIT,
|
|
162
|
+
mediaKind,
|
|
163
|
+
baseMimeType,
|
|
164
|
+
downloadMedia,
|
|
165
|
+
uploadToGemini,
|
|
166
|
+
geminiMediaPart,
|
|
167
|
+
isMissingUploadEndpoint,
|
|
168
|
+
};
|