levix-bot 4.0.4 → 4.2.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/package.json +1 -1
- package/public/dashboard/assets/index-4AuqfnH5.js +431 -0
- package/public/dashboard/assets/index-LvfJ8ybs.css +2 -0
- package/public/dashboard/index.html +2 -2
- package/scheduler.cjs +25 -1
- package/src/commands/autoschedule.cjs +40 -10
- package/src/commands/blacklist.cjs +35 -10
- package/src/commands/block.cjs +18 -5
- package/src/commands/calc.cjs +90 -24
- package/src/commands/debt.cjs +59 -33
- package/src/commands/deleteschedule.cjs +21 -5
- package/src/commands/digit.cjs +34 -8
- package/src/commands/gemini.cjs +235 -108
- package/src/commands/group/add.cjs +46 -13
- package/src/commands/group/all.cjs +15 -5
- package/src/commands/group/antiSpam.cjs +11 -4
- package/src/commands/group/antilink.cjs +39 -19
- package/src/commands/group/approveall.cjs +22 -7
- package/src/commands/group/clearwarns.cjs +18 -5
- package/src/commands/group/deletenote.cjs +18 -5
- package/src/commands/group/demote.cjs +25 -7
- package/src/commands/group/kick.cjs +28 -12
- package/src/commands/group/media.cjs +34 -13
- package/src/commands/group/members.cjs +21 -6
- package/src/commands/group/note.cjs +14 -4
- package/src/commands/group/promote.cjs +22 -6
- package/src/commands/group/removeall.cjs +13 -3
- package/src/commands/group/rules.cjs +20 -5
- package/src/commands/group/save.cjs +20 -5
- package/src/commands/group/setname.cjs +19 -6
- package/src/commands/group/setpp.cjs +21 -6
- package/src/commands/group/setrules.cjs +17 -5
- package/src/commands/group/setwarn.cjs +23 -7
- package/src/commands/group/tagadmins.cjs +16 -6
- package/src/commands/group/warn.cjs +35 -15
- package/src/commands/group/warns.cjs +33 -13
- package/src/commands/group/welcome.cjs +34 -16
- package/src/commands/group.cjs +96 -70
- package/src/commands/help.cjs +270 -129
- package/src/commands/lang.cjs +46 -13
- package/src/commands/listschedules.cjs +28 -11
- package/src/commands/loop.cjs +43 -9
- package/src/commands/memory.cjs +90 -30
- package/src/commands/mod.cjs +72 -28
- package/src/commands/notes.cjs +15 -5
- package/src/commands/perm.cjs +52 -20
- package/src/commands/ping.cjs +15 -3
- package/src/commands/poll.cjs +71 -24
- package/src/commands/prayer.cjs +28 -12
- package/src/commands/qr.cjs +20 -5
- package/src/commands/rand.cjs +54 -12
- package/src/commands/restart.cjs +13 -3
- package/src/commands/schedule.cjs +32 -9
- package/src/commands/score.cjs +26 -10
- package/src/commands/setprefix.cjs +27 -5
- package/src/commands/shortlink.cjs +36 -10
- package/src/commands/shutdown.cjs +13 -3
- package/src/commands/status.cjs +32 -11
- package/src/commands/sticker.cjs +34 -7
- package/src/commands/stt.cjs +159 -107
- package/src/commands/todo.cjs +31 -9
- package/src/commands/tts.cjs +61 -36
- package/src/commands/unblock.cjs +17 -4
- package/src/commands/weather.cjs +55 -17
- package/src/config/baileys.config.js +5 -1
- package/src/config/settings.cjs +11 -3
- package/src/core/connection.js +21 -2
- package/src/core/socket.js +31 -7
- package/src/db/store.cjs +24 -0
- package/src/handlers/command.handler.js +55 -13
- package/src/handlers/group.handler.js +5 -1
- package/src/handlers/message.handler.js +9 -0
- package/src/middleware/antispam.middleware.js +13 -3
- package/src/middleware/blacklist.middleware.js +4 -1
- package/src/middleware/permissions.middleware.js +38 -51
- package/src/routes/dashboard.api.esm.js +91 -10
- package/src/services/aiAgent.cjs +51 -41
- package/src/services/aiProviders.cjs +46 -19
- package/src/services/aiRouter.cjs +11 -3
- package/src/services/aiToolAuth.cjs +114 -0
- package/src/services/aiTools.cjs +492 -56
- package/src/services/modelRegistry.cjs +20 -5
- package/src/utils/commandDocs.cjs +58 -0
- package/src/utils/geminiMedia.cjs +99 -0
- package/src/utils/groupMetadataCache.cjs +83 -0
- package/src/utils/i18n.cjs +113 -0
- package/src/utils/markdownParser.cjs +4 -2
- package/src/utils/memory.cjs +37 -21
- package/src/utils/permissionLevel.cjs +83 -0
- package/src/utils/providerUrl.cjs +29 -2
- package/src/utils/sendBotMessage.cjs +7 -3
- package/src/utils/sendBotMessage.esm.js +27 -3
- package/src/utils/statusMessage.cjs +11 -4
- package/src/utils/storage-hub.cjs +21 -0
- package/public/dashboard/assets/index-BnJbz8iW.css +0 -2
- package/public/dashboard/assets/index-Dn8ZyzsW.js +0 -426
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
|
|
12
12
|
const require = createRequire(import.meta.url);
|
|
13
13
|
const runtimeConfig = require("../config/runtime-config.cjs");
|
|
14
|
+
const { evaluatePermissionLevel } = require("../utils/permissionLevel.cjs");
|
|
14
15
|
const logger = require("../utils/logger.cjs");
|
|
15
16
|
|
|
16
17
|
/**
|
|
@@ -28,79 +29,65 @@ function resolvePermissionLevel(commandName) {
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
|
-
*
|
|
32
|
+
* Who sent `msg`, as far as permissions are concerned. The same answer backs
|
|
33
|
+
* the command dispatcher and the AI agent's tools (services/aiToolAuth.cjs),
|
|
34
|
+
* so a person is never an admin for one door and a member for the other.
|
|
32
35
|
*
|
|
33
|
-
* @param {
|
|
34
|
-
* @param {object} msg - Baileys message
|
|
36
|
+
* @param {object} msg - Baileys message
|
|
35
37
|
* @param {object} groupMetadata - Group metadata (null for private chats)
|
|
36
38
|
* @param {object} sock - Baileys socket
|
|
37
|
-
* @returns {{
|
|
39
|
+
* @returns {{isGroup: boolean, senderId: string|null, candidates: string[],
|
|
40
|
+
* isOwner: boolean, isBotAdmin: boolean, isGroupAdmin: boolean,
|
|
41
|
+
* isSenderAdmin: boolean}}
|
|
38
42
|
*/
|
|
39
|
-
export function
|
|
40
|
-
const isGroup = msg
|
|
43
|
+
export function resolveSender(msg, groupMetadata, sock) {
|
|
44
|
+
const isGroup = Boolean(msg?.key?.remoteJid?.endsWith("@g.us"));
|
|
41
45
|
const senderId = getSenderId(msg, sock);
|
|
42
46
|
|
|
43
47
|
// v7/LID: check every identifier the sender could appear under (LID + PN
|
|
44
48
|
// alternates), so owner/admin detection isn't defeated by a LID<->PN mismatch.
|
|
45
49
|
const senderCandidates = getSenderCandidates(msg, sock);
|
|
46
|
-
const candidates = senderCandidates.length ? senderCandidates : [senderId];
|
|
50
|
+
const candidates = senderCandidates.length ? senderCandidates : [senderId].filter(Boolean);
|
|
47
51
|
|
|
48
52
|
// fromMe is an immediate owner indicator (the bot is always its own owner).
|
|
49
|
-
const isOwner = msg
|
|
53
|
+
const isOwner = Boolean(msg?.key?.fromMe) || candidates.some((c) => isOwnerJid(c));
|
|
50
54
|
|
|
51
55
|
// Bot-level admins (granted with `!perm add admin`, from the dashboard, or by
|
|
52
56
|
// asking the AI) count as admins everywhere — including DMs, where there is
|
|
53
57
|
// no group roster to consult.
|
|
54
58
|
const isBotAdmin = !isOwner && candidates.some((c) => isBotAdminUser(c));
|
|
55
59
|
|
|
56
|
-
const
|
|
57
|
-
|
|
60
|
+
const isGroupAdmin = isGroup && candidates.some((c) => isAdminInGroup(groupMetadata, c));
|
|
61
|
+
const isSenderAdmin = isBotAdmin || isGroupAdmin;
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
let hasPermission = false;
|
|
62
|
-
let reason = "";
|
|
63
|
-
|
|
64
|
-
switch (permissionLevel) {
|
|
65
|
-
case "MEMBERS":
|
|
66
|
-
case "ALL":
|
|
67
|
-
hasPermission = true;
|
|
68
|
-
break;
|
|
69
|
-
|
|
70
|
-
case "OWNER_ONLY":
|
|
71
|
-
hasPermission = isOwner;
|
|
72
|
-
reason = "🚫 هذا الأمر متاح للمالك فقط.";
|
|
73
|
-
break;
|
|
74
|
-
|
|
75
|
-
case "ADMINS_ONLY":
|
|
76
|
-
if (!isGroup) {
|
|
77
|
-
// Owners and bot-admins can run admin-only commands from anywhere —
|
|
78
|
-
// useful for the operator pinging the bot privately to manage a group.
|
|
79
|
-
hasPermission = isOwner || isBotAdmin;
|
|
80
|
-
reason = "⚠️ هذا الأمر يعمل في المجموعات فقط (أو للمالك في الخاص).";
|
|
81
|
-
} else {
|
|
82
|
-
hasPermission = isSenderAdmin || isOwner;
|
|
83
|
-
reason = "🚫 هذا الأمر متاح للمشرفين فقط.";
|
|
84
|
-
}
|
|
85
|
-
break;
|
|
63
|
+
return { isGroup, senderId, candidates, isOwner, isBotAdmin, isGroupAdmin, isSenderAdmin };
|
|
64
|
+
}
|
|
86
65
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Check if user has permission to execute a command.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} commandName - Canonical command name
|
|
70
|
+
* @param {object} msg - Baileys message
|
|
71
|
+
* @param {object} groupMetadata - Group metadata (null for private chats)
|
|
72
|
+
* @param {object} sock - Baileys socket
|
|
73
|
+
* @returns {{hasPermission: boolean, reason: string, isOwner: boolean, isSenderAdmin: boolean}}
|
|
74
|
+
*/
|
|
75
|
+
export function checkCommandPermission(commandName, msg, groupMetadata, sock) {
|
|
76
|
+
const sender = resolveSender(msg, groupMetadata, sock);
|
|
77
|
+
const permissionLevel = resolvePermissionLevel(commandName);
|
|
78
|
+
const { hasPermission, reason, unknownLevel } = evaluatePermissionLevel(permissionLevel, sender);
|
|
96
79
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
hasPermission = false;
|
|
100
|
-
reason = "🚫 مستوى الصلاحية غير معروف.";
|
|
80
|
+
if (unknownLevel) {
|
|
81
|
+
logger.warn(`Unknown permission level: ${permissionLevel} for command: ${commandName}`);
|
|
101
82
|
}
|
|
102
83
|
|
|
103
|
-
return {
|
|
84
|
+
return {
|
|
85
|
+
hasPermission,
|
|
86
|
+
reason,
|
|
87
|
+
isOwner: sender.isOwner,
|
|
88
|
+
isSenderAdmin: sender.isSenderAdmin,
|
|
89
|
+
isBotAdmin: sender.isBotAdmin,
|
|
90
|
+
};
|
|
104
91
|
}
|
|
105
92
|
|
|
106
93
|
/**
|
|
@@ -323,6 +323,8 @@ router.patch("/settings", (req, res) => {
|
|
|
323
323
|
visionEnabled: key === "ai_vision_enabled" ? value : settings.get("ai_vision_enabled"),
|
|
324
324
|
sttEnabled: key === "ai_stt_enabled" ? value : settings.get("ai_stt_enabled"),
|
|
325
325
|
capabilities: resolved.capabilities,
|
|
326
|
+
capabilitySource: resolved.capabilitySource,
|
|
327
|
+
autoDetect: settings.get("ai_auto_detect_capabilities"),
|
|
326
328
|
});
|
|
327
329
|
if (key === "ai_vision_enabled") value = next.visionEnabled;
|
|
328
330
|
if (key === "ai_stt_enabled") value = next.sttEnabled;
|
|
@@ -359,6 +361,8 @@ router.patch("/settings", (req, res) => {
|
|
|
359
361
|
visionEnabled: settings.get("ai_vision_enabled"),
|
|
360
362
|
sttEnabled: settings.get("ai_stt_enabled"),
|
|
361
363
|
capabilities: resolved.capabilities,
|
|
364
|
+
capabilitySource: resolved.capabilitySource,
|
|
365
|
+
autoDetect: settings.get("ai_auto_detect_capabilities"),
|
|
362
366
|
});
|
|
363
367
|
if (Boolean(settings.get("ai_vision_enabled")) !== next.visionEnabled) {
|
|
364
368
|
settings.set("ai_vision_enabled", next.visionEnabled);
|
|
@@ -512,16 +516,16 @@ router.get("/ai/memory", (req, res) => {
|
|
|
512
516
|
try {
|
|
513
517
|
const scopes = [];
|
|
514
518
|
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
}
|
|
519
|
+
// Global is always offered — even before the file exists — so the editor
|
|
520
|
+
// can create it with the first save.
|
|
521
|
+
const stat = fs.existsSync(memory.GLOBAL_FILE) ? fs.statSync(memory.GLOBAL_FILE) : null;
|
|
522
|
+
scopes.push({
|
|
523
|
+
scope: "global",
|
|
524
|
+
label: "Global memory",
|
|
525
|
+
entries: memory.listMemory({ scope: "global" }).length,
|
|
526
|
+
bytes: stat?.size ?? 0,
|
|
527
|
+
updatedAt: stat?.mtimeMs ?? null,
|
|
528
|
+
});
|
|
525
529
|
|
|
526
530
|
if (fs.existsSync(memory.CHATS_DIR)) {
|
|
527
531
|
for (const file of fs.readdirSync(memory.CHATS_DIR)) {
|
|
@@ -581,6 +585,83 @@ router.delete("/ai/memory/:scope", (req, res) => {
|
|
|
581
585
|
}
|
|
582
586
|
});
|
|
583
587
|
|
|
588
|
+
// ===========================================================================
|
|
589
|
+
// AI conversation inspector — read-only views of the stored `ai_history`
|
|
590
|
+
//
|
|
591
|
+
// The bot deliberately keeps only its own conversation windows, and the panel
|
|
592
|
+
// may look at them but never change them: deletes stay with `!del` / `!delall`
|
|
593
|
+
// in WhatsApp, so this endpoint is GET-only on purpose.
|
|
594
|
+
// ===========================================================================
|
|
595
|
+
|
|
596
|
+
const MAX_INSPECTOR_MESSAGES = 200;
|
|
597
|
+
const MAX_INSPECTOR_TEXT_CHARS = 2000;
|
|
598
|
+
|
|
599
|
+
router.get("/ai/conversations", (req, res) => {
|
|
600
|
+
getChatHistoryListAsync()
|
|
601
|
+
.then((conversations) => res.json({ success: true, conversations }))
|
|
602
|
+
.catch((error) => fail(res, error, "Error listing conversations"));
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
router.get("/ai/conversations/:chatId", (req, res) => {
|
|
606
|
+
getChatConversationAsync(req.params.chatId)
|
|
607
|
+
.then((conversation) => {
|
|
608
|
+
if (!conversation) return badRequest(res, "No stored conversation for that chat");
|
|
609
|
+
res.json({ success: true, ...conversation });
|
|
610
|
+
})
|
|
611
|
+
.catch((error) => fail(res, error, "Error reading conversation"));
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
async function getChatHistoryListAsync() {
|
|
615
|
+
const { listChatHistoriesAsync } = require("../utils/storage-hub.cjs");
|
|
616
|
+
const rows = await listChatHistoriesAsync();
|
|
617
|
+
return (rows || []).map((row) => ({
|
|
618
|
+
chatId: row.chatId,
|
|
619
|
+
turns: row.turns ?? 0,
|
|
620
|
+
updatedAt: row.updatedAt ?? null,
|
|
621
|
+
}));
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// Flatten the canonical Gemini-parts history into display rows: text, media
|
|
625
|
+
// references (mime only — the bytes were never stored) and tool activity.
|
|
626
|
+
// Oldest first, capped to the most recent MAX_INSPECTOR_MESSAGES rows.
|
|
627
|
+
async function getChatConversationAsync(chatId) {
|
|
628
|
+
const { getChatHistoryWithMetaAsync } = require("../utils/storage-hub.cjs");
|
|
629
|
+
const meta = await getChatHistoryWithMetaAsync(chatId);
|
|
630
|
+
if (!meta) return null;
|
|
631
|
+
|
|
632
|
+
const messages = [];
|
|
633
|
+
for (const entry of Array.isArray(meta.history) ? meta.history : []) {
|
|
634
|
+
const role = entry?.role === "model" ? "model" : "user";
|
|
635
|
+
for (const part of Array.isArray(entry?.parts) ? entry.parts : []) {
|
|
636
|
+
if (messages.length >= MAX_INSPECTOR_MESSAGES) break;
|
|
637
|
+
if (typeof part?.text === "string" && part.text.trim()) {
|
|
638
|
+
messages.push({ role, kind: "text", text: part.text.slice(0, MAX_INSPECTOR_TEXT_CHARS) });
|
|
639
|
+
} else if (part?.fileData || part?.inlineData) {
|
|
640
|
+
messages.push({
|
|
641
|
+
role,
|
|
642
|
+
kind: "media",
|
|
643
|
+
mimeType: part?.fileData?.mimeType || part?.inlineData?.mimeType || null,
|
|
644
|
+
});
|
|
645
|
+
} else if (part?.functionCall) {
|
|
646
|
+
messages.push({
|
|
647
|
+
role: "model",
|
|
648
|
+
kind: "tool",
|
|
649
|
+
name: part.functionCall.name || "unknown",
|
|
650
|
+
});
|
|
651
|
+
} else if (part?.functionResponse) {
|
|
652
|
+
messages.push({
|
|
653
|
+
role: "user",
|
|
654
|
+
kind: "toolResult",
|
|
655
|
+
name: part.functionResponse.name || "unknown",
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
if (messages.length >= MAX_INSPECTOR_MESSAGES) break;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
return { chatId, updatedAt: meta.updatedAt ?? null, truncated: messages.length >= MAX_INSPECTOR_MESSAGES, messages };
|
|
663
|
+
}
|
|
664
|
+
|
|
584
665
|
// ===========================================================================
|
|
585
666
|
// Groups
|
|
586
667
|
// ===========================================================================
|
package/src/services/aiAgent.cjs
CHANGED
|
@@ -14,10 +14,16 @@
|
|
|
14
14
|
// runtime block and whatever is in the long-term memory files.
|
|
15
15
|
|
|
16
16
|
const fs = require("fs");
|
|
17
|
-
const path = require("path");
|
|
18
17
|
const { GoogleGenAI, FunctionCallingConfigMode } = require("@google/genai");
|
|
19
18
|
|
|
20
19
|
const logger = require("../utils/logger.cjs");
|
|
20
|
+
const {
|
|
21
|
+
mediaKind,
|
|
22
|
+
baseMimeType,
|
|
23
|
+
downloadMedia,
|
|
24
|
+
uploadToGemini,
|
|
25
|
+
} = require("../utils/geminiMedia.cjs");
|
|
26
|
+
const { tr } = require("../utils/i18n.cjs");
|
|
21
27
|
const aiIdentity = require("../config/ai-identity.cjs");
|
|
22
28
|
const settings = require("../config/settings.cjs");
|
|
23
29
|
const memory = require("../utils/memory.cjs");
|
|
@@ -238,7 +244,15 @@ function withTimeout(promise, ms, label) {
|
|
|
238
244
|
promise,
|
|
239
245
|
new Promise((_, reject) => {
|
|
240
246
|
timer = setTimeout(
|
|
241
|
-
() =>
|
|
247
|
+
() =>
|
|
248
|
+
reject(
|
|
249
|
+
new Error(
|
|
250
|
+
tr(
|
|
251
|
+
`${label} took longer than ${Math.round(ms / 1000)} seconds`,
|
|
252
|
+
`${label} تأخرت أكتر من ${Math.round(ms / 1000)} ثانية`,
|
|
253
|
+
),
|
|
254
|
+
),
|
|
255
|
+
),
|
|
242
256
|
ms,
|
|
243
257
|
);
|
|
244
258
|
}),
|
|
@@ -385,7 +399,7 @@ function formatSources(sources) {
|
|
|
385
399
|
lines.push(`• ${label} — ${source.uri}`);
|
|
386
400
|
if (lines.length >= MAX_SOURCES) break;
|
|
387
401
|
}
|
|
388
|
-
return lines.length ? `\n\n*Sources
|
|
402
|
+
return lines.length ? `\n\n${tr("*Sources:*", "*المصادر:*")}\n${lines.join("\n")}` : "";
|
|
389
403
|
}
|
|
390
404
|
|
|
391
405
|
/**
|
|
@@ -399,7 +413,9 @@ function formatSources(sources) {
|
|
|
399
413
|
* @param {Array} options.parts - the Gemini parts for this message
|
|
400
414
|
* @param {Array} [options.history]- prior conversation
|
|
401
415
|
* @param {object} [options.status] - live status message (see utils/statusMessage.cjs)
|
|
402
|
-
* @param {object} [options.context]- { chatId, senderId, senderName, isOwner, isAdmin, isGroup, chatName }
|
|
416
|
+
* @param {object} [options.context]- { caller, chatId, senderId, senderName, isOwner, isAdmin, isGroup, chatName }
|
|
417
|
+
* `caller` (aiToolAuth.resolveCaller) is the only thing the
|
|
418
|
+
* tools authorize against; the rest feeds the system prompt.
|
|
403
419
|
* @param {boolean} [options.useTools=true]
|
|
404
420
|
* @returns {Promise<{text: string, history: Array, toolCalls: Array, steps: number}>}
|
|
405
421
|
*/
|
|
@@ -420,54 +436,42 @@ class GeminiProvider extends BaseAIProvider {
|
|
|
420
436
|
parts,
|
|
421
437
|
mediaMessage,
|
|
422
438
|
mimeOverride,
|
|
423
|
-
{ downloadContentFromMessage, genAI,
|
|
439
|
+
{ downloadContentFromMessage, genAI, downloadType } = {},
|
|
424
440
|
) {
|
|
425
441
|
if (!genAI) throw new Error("Gemini client unavailable");
|
|
426
|
-
const
|
|
442
|
+
const rawMime = mimeOverride || mediaMessage?.mimetype || "";
|
|
443
|
+
const kind = mediaKind(rawMime);
|
|
427
444
|
const caps = detectModelCapabilities("gemini");
|
|
428
|
-
if (
|
|
445
|
+
if (kind === "image" && (!settings.get("ai_vision_enabled") || !caps.supportsVision)) {
|
|
429
446
|
parts.push({
|
|
430
447
|
text: "[تم إرفاق صورة — الرؤية غير مفعّلة للنموذج المحدد / Vision is disabled for the selected model]",
|
|
431
448
|
});
|
|
432
449
|
return null;
|
|
433
450
|
}
|
|
434
|
-
if (
|
|
451
|
+
if (kind === "audio" && (!settings.get("ai_stt_enabled") || !caps.supportsAudioStt)) {
|
|
435
452
|
parts.push({
|
|
436
453
|
text: "[تم إرفاق صوت — تحويل الكلام غير مفعّل للنموذج المحدد / Speech-to-text is disabled for the selected model]",
|
|
437
454
|
});
|
|
438
455
|
return null;
|
|
439
456
|
}
|
|
440
|
-
|
|
441
|
-
|
|
457
|
+
|
|
458
|
+
// The MIME type goes in `config` (see utils/geminiMedia.cjs) — passing it
|
|
459
|
+
// anywhere else is what made every upload fail with "Can not determine
|
|
460
|
+
// mimeType".
|
|
461
|
+
const mimeType = baseMimeType(rawMime, kind);
|
|
462
|
+
const buffer = await downloadMedia(
|
|
463
|
+
downloadContentFromMessage,
|
|
442
464
|
mediaMessage,
|
|
443
|
-
|
|
444
|
-
? "image"
|
|
445
|
-
: mediaMessage.mimetype?.startsWith("video/")
|
|
446
|
-
? "video"
|
|
447
|
-
: mediaMessage.mimetype?.startsWith("audio/")
|
|
448
|
-
? "audio"
|
|
449
|
-
: "document",
|
|
465
|
+
downloadType || kind,
|
|
450
466
|
);
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
mimeType: mediaMessage.mimetype,
|
|
460
|
-
});
|
|
461
|
-
parts.push({
|
|
462
|
-
fileData: {
|
|
463
|
-
fileUri: uploadResult.uri,
|
|
464
|
-
mimeType: uploadResult.mimeType,
|
|
465
|
-
},
|
|
466
|
-
});
|
|
467
|
-
return uploadResult.uri;
|
|
468
|
-
} finally {
|
|
469
|
-
await fs.promises.unlink(tempFilePath).catch(() => {});
|
|
470
|
-
}
|
|
467
|
+
const file = await uploadToGemini(genAI, buffer, mimeType);
|
|
468
|
+
parts.push({
|
|
469
|
+
fileData: {
|
|
470
|
+
fileUri: file.uri,
|
|
471
|
+
mimeType: file.mimeType || mimeType,
|
|
472
|
+
},
|
|
473
|
+
});
|
|
474
|
+
return file.uri;
|
|
471
475
|
}
|
|
472
476
|
|
|
473
477
|
async runTurn(options) {
|
|
@@ -499,9 +503,10 @@ async function runGeminiTurn({
|
|
|
499
503
|
useTools = true,
|
|
500
504
|
maxSteps = null,
|
|
501
505
|
systemInstruction = null,
|
|
506
|
+
search = null,
|
|
502
507
|
} = {}) {
|
|
503
508
|
const genAI = geminiClient();
|
|
504
|
-
if (!genAI) throw new Error("GEMINI_API_KEY غير معرف");
|
|
509
|
+
if (!genAI) throw new Error(tr("No Gemini API key is set", "GEMINI_API_KEY غير معرف"));
|
|
505
510
|
await assertProviderRequestUrl(settings.get("gemini_base_url"), { allowLoopback: true });
|
|
506
511
|
|
|
507
512
|
const stepBudget = maxSteps ?? settings.get("ai_max_tool_steps");
|
|
@@ -530,7 +535,9 @@ async function runGeminiTurn({
|
|
|
530
535
|
});
|
|
531
536
|
};
|
|
532
537
|
|
|
533
|
-
|
|
538
|
+
// One-shot callers (the summarize tool) pass search:false explicitly; the
|
|
539
|
+
// chat agent follows the setting.
|
|
540
|
+
let searchOffered = search === null ? googleSearchEnabled() : Boolean(search);
|
|
534
541
|
let chat = openChat(searchOffered);
|
|
535
542
|
const toolCalls = [];
|
|
536
543
|
// Only ever filled from real grounding metadata; see extractSources().
|
|
@@ -599,7 +606,7 @@ async function runGeminiTurn({
|
|
|
599
606
|
});
|
|
600
607
|
}
|
|
601
608
|
|
|
602
|
-
if (status) await status.update("🤖 بجهّز الرد...");
|
|
609
|
+
if (status) await status.update(tr("🤖 Writing the answer...", "🤖 بجهّز الرد..."));
|
|
603
610
|
// One message carrying every functionResponse part, which is what the API
|
|
604
611
|
// expects when the model asked for several calls in one turn.
|
|
605
612
|
response = await chat.sendMessage({ message: responses });
|
|
@@ -617,7 +624,10 @@ async function runGeminiTurn({
|
|
|
617
624
|
}
|
|
618
625
|
|
|
619
626
|
if (!text && steps >= stepBudget) {
|
|
620
|
-
text =
|
|
627
|
+
text = tr(
|
|
628
|
+
"I ran the tools I have but couldn't reach a final answer. Try asking more clearly.",
|
|
629
|
+
"شغّلت الأدوات المتاحة بس مقدرتش أوصل لإجابة نهائية. جرّب تسأل بصيغة أوضح.",
|
|
630
|
+
);
|
|
621
631
|
}
|
|
622
632
|
|
|
623
633
|
let newHistory = [];
|
|
@@ -32,6 +32,7 @@ const logger = require("../utils/logger.cjs");
|
|
|
32
32
|
const settings = require("../config/settings.cjs");
|
|
33
33
|
const { toolDeclarations, describeCall, runTool } = require("./aiTools.cjs");
|
|
34
34
|
const { assertProviderBaseUrl, safeProviderFetch } = require("../utils/providerUrl.cjs");
|
|
35
|
+
const { baseMimeType, downloadMedia } = require("../utils/geminiMedia.cjs");
|
|
35
36
|
|
|
36
37
|
// Shown in place of a media part, on the wire and in the canonical history.
|
|
37
38
|
const MEDIA_NOTE = "[تم إرفاق ملف/وسائط في الرسالة]";
|
|
@@ -102,6 +103,7 @@ const {
|
|
|
102
103
|
getProvider,
|
|
103
104
|
detectModelCapabilities,
|
|
104
105
|
} = require("./aiRouter.cjs");
|
|
106
|
+
const { tr } = require("../utils/i18n.cjs");
|
|
105
107
|
|
|
106
108
|
function visionAllowed(providerId) {
|
|
107
109
|
const id = providerId || settings.get("ai_provider");
|
|
@@ -114,23 +116,24 @@ async function prepareInlineImageMedia(
|
|
|
114
116
|
mediaMessage,
|
|
115
117
|
mimeOverride,
|
|
116
118
|
downloadContentFromMessage,
|
|
119
|
+
downloadType,
|
|
117
120
|
) {
|
|
118
121
|
const isVisionOn = visionAllowed(providerId);
|
|
119
122
|
const mime = mimeOverride || mediaMessage?.mimetype || "ملف";
|
|
120
123
|
if (isVisionOn && mime.startsWith("image/") && typeof downloadContentFromMessage === "function") {
|
|
121
124
|
try {
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
125
|
+
const buffer = await downloadMedia(
|
|
126
|
+
downloadContentFromMessage,
|
|
127
|
+
mediaMessage,
|
|
128
|
+
downloadType || "image",
|
|
129
|
+
);
|
|
130
|
+
parts.push({
|
|
131
|
+
inlineData: {
|
|
132
|
+
mimeType: baseMimeType(mime, "image"),
|
|
133
|
+
data: buffer.toString("base64"),
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
return "inline:image";
|
|
134
137
|
} catch (err) {
|
|
135
138
|
logger.warn({ err }, `[${providerId}] failed to extract inline image for vision`);
|
|
136
139
|
}
|
|
@@ -154,13 +157,19 @@ class OpenAIProvider extends BaseAIProvider {
|
|
|
154
157
|
});
|
|
155
158
|
}
|
|
156
159
|
|
|
157
|
-
async prepareMedia(
|
|
160
|
+
async prepareMedia(
|
|
161
|
+
parts,
|
|
162
|
+
mediaMessage,
|
|
163
|
+
mimeOverride,
|
|
164
|
+
{ downloadContentFromMessage, downloadType } = {},
|
|
165
|
+
) {
|
|
158
166
|
return prepareInlineImageMedia(
|
|
159
167
|
"openai",
|
|
160
168
|
parts,
|
|
161
169
|
mediaMessage,
|
|
162
170
|
mimeOverride,
|
|
163
171
|
downloadContentFromMessage,
|
|
172
|
+
downloadType,
|
|
164
173
|
);
|
|
165
174
|
}
|
|
166
175
|
|
|
@@ -182,13 +191,19 @@ class AnthropicProvider extends BaseAIProvider {
|
|
|
182
191
|
});
|
|
183
192
|
}
|
|
184
193
|
|
|
185
|
-
async prepareMedia(
|
|
194
|
+
async prepareMedia(
|
|
195
|
+
parts,
|
|
196
|
+
mediaMessage,
|
|
197
|
+
mimeOverride,
|
|
198
|
+
{ downloadContentFromMessage, downloadType } = {},
|
|
199
|
+
) {
|
|
186
200
|
return prepareInlineImageMedia(
|
|
187
201
|
"anthropic",
|
|
188
202
|
parts,
|
|
189
203
|
mediaMessage,
|
|
190
204
|
mimeOverride,
|
|
191
205
|
downloadContentFromMessage,
|
|
206
|
+
downloadType,
|
|
192
207
|
);
|
|
193
208
|
}
|
|
194
209
|
|
|
@@ -202,7 +217,7 @@ registerProvider("anthropic", new AnthropicProvider());
|
|
|
202
217
|
|
|
203
218
|
function adapterFor(provider) {
|
|
204
219
|
const adapter = ADAPTERS[provider];
|
|
205
|
-
if (!adapter) throw new Error(`مزود غير معروف: ${provider}`);
|
|
220
|
+
if (!adapter) throw new Error(tr(`Unknown provider: ${provider}`, `مزود غير معروف: ${provider}`));
|
|
206
221
|
return adapter;
|
|
207
222
|
}
|
|
208
223
|
|
|
@@ -223,7 +238,7 @@ const PROVIDER_KEY_SETTINGS = Object.freeze({
|
|
|
223
238
|
|
|
224
239
|
function activeProviderKeySetting(provider = settings.get("ai_provider")) {
|
|
225
240
|
const keySetting = PROVIDER_KEY_SETTINGS[provider];
|
|
226
|
-
if (!keySetting) throw new Error(`مزود غير معروف: ${provider}`);
|
|
241
|
+
if (!keySetting) throw new Error(tr(`Unknown provider: ${provider}`, `مزود غير معروف: ${provider}`));
|
|
227
242
|
return keySetting;
|
|
228
243
|
}
|
|
229
244
|
|
|
@@ -623,7 +638,15 @@ function withTimeout(promise, ms, label) {
|
|
|
623
638
|
promise,
|
|
624
639
|
new Promise((_, reject) => {
|
|
625
640
|
timer = setTimeout(
|
|
626
|
-
() =>
|
|
641
|
+
() =>
|
|
642
|
+
reject(
|
|
643
|
+
new Error(
|
|
644
|
+
tr(
|
|
645
|
+
`${label} took longer than ${Math.round(ms / 1000)} seconds`,
|
|
646
|
+
`${label} استغرقت العملية أكثر من ${Math.round(ms / 1000)} ثانية`,
|
|
647
|
+
),
|
|
648
|
+
),
|
|
649
|
+
),
|
|
627
650
|
ms,
|
|
628
651
|
);
|
|
629
652
|
}),
|
|
@@ -731,7 +754,11 @@ async function runProviderAgent(
|
|
|
731
754
|
// Same contract as the Gemini loop: the budget answer, not a lie.
|
|
732
755
|
return finish({
|
|
733
756
|
text:
|
|
734
|
-
parsed.text ||
|
|
757
|
+
parsed.text ||
|
|
758
|
+
tr(
|
|
759
|
+
"I ran the tools I have but couldn't reach a final answer. Try asking more clearly.",
|
|
760
|
+
"شغّلت الأدوات المتاحة بس مقدرتش أوصل لإجابة نهائية. جرّب تسأل بصيغة أوضح.",
|
|
761
|
+
),
|
|
735
762
|
canonical,
|
|
736
763
|
toolCalls,
|
|
737
764
|
steps,
|
|
@@ -778,7 +805,7 @@ async function runProviderAgent(
|
|
|
778
805
|
})),
|
|
779
806
|
});
|
|
780
807
|
|
|
781
|
-
if (status) await status.update("🤖 بجهّز الرد...");
|
|
808
|
+
if (status) await status.update(tr("🤖 Writing the answer...", "🤖 بجهّز الرد..."));
|
|
782
809
|
response = await send();
|
|
783
810
|
}
|
|
784
811
|
}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
const settings = require("../config/settings.cjs");
|
|
9
9
|
const { resolveCapabilities } = require("./modelRegistry.cjs");
|
|
10
|
+
const { tr } = require("../utils/i18n.cjs");
|
|
10
11
|
|
|
11
12
|
class BaseAIProvider {
|
|
12
13
|
constructor({
|
|
@@ -82,7 +83,7 @@ function getProvider(id = settings.get("ai_provider")) {
|
|
|
82
83
|
const key = String(id || "gemini").toLowerCase();
|
|
83
84
|
const provider = REGISTRY.get(key);
|
|
84
85
|
if (!provider) {
|
|
85
|
-
throw new Error(`مزود غير معروف: ${id}`);
|
|
86
|
+
throw new Error(tr(`Unknown provider: ${id}`, `مزود غير معروف: ${id}`));
|
|
86
87
|
}
|
|
87
88
|
return provider;
|
|
88
89
|
}
|
|
@@ -115,13 +116,20 @@ function detectModelCapabilities(provider = settings.get("ai_provider"), model =
|
|
|
115
116
|
const resolved = resolveCapabilities(p, m);
|
|
116
117
|
const caps = resolved.capabilities;
|
|
117
118
|
|
|
119
|
+
// "Unknown" is not "unsupported": for a model the registry has never seen
|
|
120
|
+
// (a gateway's own ids, a model newer than the catalog) the operator's
|
|
121
|
+
// toggles are the only gate, so report the capability as available and let
|
|
122
|
+
// ai_vision_enabled / ai_stt_enabled decide. Forcing it false here would
|
|
123
|
+
// silently drop every image/audio part on custom endpoints.
|
|
124
|
+
const known = resolved.capabilitySource === "seed" || resolved.capabilitySource === "provider";
|
|
125
|
+
|
|
118
126
|
return {
|
|
119
127
|
provider: p,
|
|
120
128
|
model: m,
|
|
121
129
|
capabilities: caps,
|
|
122
130
|
capabilitySource: resolved.capabilitySource,
|
|
123
|
-
supportsVision: Boolean(caps.vision),
|
|
124
|
-
supportsAudioStt: Boolean(caps.stt),
|
|
131
|
+
supportsVision: known ? Boolean(caps.vision) : true,
|
|
132
|
+
supportsAudioStt: known ? Boolean(caps.stt) : true,
|
|
125
133
|
visionRecommended: Boolean(caps.vision) && !settings.get("ai_vision_enabled"),
|
|
126
134
|
};
|
|
127
135
|
}
|