levix-bot 4.9.1 → 5.0.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 +57 -3
- package/SETUP.md +63 -7
- package/app.cjs +159 -46
- package/deploy/install.sh +10 -5
- package/package.json +9 -6
- package/public/dashboard/assets/40af6d88b63079a0-BZ8ojJNu.woff2 +0 -0
- package/public/dashboard/assets/481acf6760031cee-BypgxfGb.woff2 +0 -0
- package/public/dashboard/assets/6d3351fc535f8ef3-D2-eand5.woff2 +0 -0
- package/public/dashboard/assets/8ab0af53a3bda9d3-CyCXRvzh.woff2 +0 -0
- package/public/dashboard/assets/8f262393e9249e61-TQp-UTiE.woff2 +0 -0
- package/public/dashboard/assets/94d65887a83f1282-DmpS2jIq.woff2 +0 -0
- package/public/dashboard/assets/99e400330f36ae77-D3oMJlXt.woff2 +0 -0
- package/public/dashboard/assets/9d1f41b13e519d90-BVNSOH3d.woff2 +0 -0
- package/public/dashboard/assets/a005256cb957ecc7-CoYeBiSI.woff2 +0 -0
- package/public/dashboard/assets/b19c2080652a3c2b-DIC32ArD.woff2 +0 -0
- package/public/dashboard/assets/c4064400db3750ce-BehTv68k.woff2 +0 -0
- package/public/dashboard/assets/c69d345fa1068c2d-CmI34b-g.woff2 +0 -0
- package/public/dashboard/assets/cc27b30471250057-6fWv1k7M.woff2 +0 -0
- package/public/dashboard/assets/d8c30647abfeffe6-eXO_dkmS.woff2 +0 -0
- package/public/dashboard/assets/eb83d8d40af4411f-Buze_B52.woff2 +0 -0
- package/public/dashboard/assets/effc6711249e7349-qRpaaN48.woff2 +0 -0
- package/public/dashboard/assets/gateway-BDYet0pI.css +1 -0
- package/public/dashboard/assets/gateway-BYt6-s1O.js +1 -0
- package/public/dashboard/assets/index-B9wFpD_z.css +2 -0
- package/public/dashboard/assets/index-DWcQmu0_.js +124 -0
- package/public/dashboard/assets/index-Da-j89O-.js +458 -0
- package/public/dashboard/font-licenses/jetbrainsmono-OFL.txt +93 -0
- package/public/dashboard/font-licenses/plusjakartasans-OFL.txt +93 -0
- package/public/dashboard/font-licenses/tajawal-OFL.txt +93 -0
- package/public/dashboard/gateway.html +17 -0
- package/public/dashboard/index.html +3 -5
- package/public/ui-boot.js +33 -14
- package/scheduler.cjs +76 -23
- package/src/auth/auth-storage.cjs +54 -7
- package/src/auth/use-database-auth-state.js +70 -39
- package/src/automation/engine.js +412 -0
- package/src/automation/limits.cjs +6 -0
- package/src/automation/manage.js +290 -0
- package/src/automation/reactions.js +471 -0
- package/src/automation/rules.cjs +114 -0
- package/src/automation/status.cjs +76 -0
- package/src/backup/account.cjs +29 -0
- package/src/backup/activation.cjs +247 -0
- package/src/backup/content.cjs +36 -0
- package/src/backup/create.cjs +280 -0
- package/src/backup/extensions.cjs +101 -0
- package/src/backup/format.cjs +565 -0
- package/src/backup/jobs.cjs +111 -0
- package/src/backup/journal.cjs +41 -0
- package/src/backup/plan.cjs +223 -0
- package/src/backup/recovery.cjs +110 -0
- package/src/backup/restore.cjs +460 -0
- package/src/backup/uploads.cjs +146 -0
- package/src/bootstrap/core.js +50 -12
- package/src/bootstrap/panel.js +53 -2
- package/src/bootstrap/session-startup-policy.js +8 -1
- package/src/cli.js +163 -22
- package/src/commands/autodelete.cjs +41 -57
- package/src/commands/autoschedule.cjs +7 -33
- package/src/commands/blacklist.cjs +17 -35
- package/src/commands/block.cjs +4 -18
- package/src/commands/calc.cjs +22 -74
- package/src/commands/debt.cjs +28 -59
- package/src/commands/deleteschedule.cjs +7 -23
- package/src/commands/digit.cjs +5 -39
- package/src/commands/gemini.cjs +135 -218
- package/src/commands/group/add.cjs +28 -76
- package/src/commands/group/all.cjs +4 -15
- package/src/commands/group/antilink.cjs +28 -41
- package/src/commands/group/antispam.cjs +25 -35
- package/src/commands/group/approveall.cjs +7 -21
- package/src/commands/group/clearwarns.cjs +4 -18
- package/src/commands/group/deletenote.cjs +4 -18
- package/src/commands/group/demote.cjs +6 -23
- package/src/commands/group/flagged.cjs +10 -19
- package/src/commands/group/kick.cjs +26 -36
- package/src/commands/group/media.cjs +21 -36
- package/src/commands/group/members.cjs +16 -28
- package/src/commands/group/note.cjs +3 -14
- package/src/commands/group/policy.cjs +16 -37
- package/src/commands/group/promote.cjs +5 -22
- package/src/commands/group/removeall.cjs +6 -38
- package/src/commands/group/rules.cjs +4 -19
- package/src/commands/group/save.cjs +4 -18
- package/src/commands/group/setname.cjs +5 -19
- package/src/commands/group/setpp.cjs +8 -26
- package/src/commands/group/setrules.cjs +4 -15
- package/src/commands/group/setwarn.cjs +6 -23
- package/src/commands/group/tagadmins.cjs +5 -16
- package/src/commands/group/warn.cjs +5 -16
- package/src/commands/group/warns.cjs +12 -33
- package/src/commands/group/welcome.cjs +17 -34
- package/src/commands/group.cjs +91 -68
- package/src/commands/help.cjs +48 -89
- package/src/commands/lang.cjs +38 -97
- package/src/commands/listschedules.cjs +19 -30
- package/src/commands/loop.cjs +9 -55
- package/src/commands/md.cjs +2 -13
- package/src/commands/memory.cjs +53 -87
- package/src/commands/mod.cjs +40 -79
- package/src/commands/notes.cjs +4 -15
- package/src/commands/pack.cjs +26 -49
- package/src/commands/packs.cjs +8 -18
- package/src/commands/perm.cjs +48 -51
- package/src/commands/ping.cjs +2 -10
- package/src/commands/poll.cjs +5 -38
- package/src/commands/prayer.cjs +55 -36
- package/src/commands/qr.cjs +4 -18
- package/src/commands/rand.cjs +9 -58
- package/src/commands/restart.cjs +15 -33
- package/src/commands/schedule.cjs +5 -25
- package/src/commands/score.cjs +6 -27
- package/src/commands/send.cjs +4 -18
- package/src/commands/setprefix.cjs +11 -23
- package/src/commands/shortlink.cjs +13 -39
- package/src/commands/status.cjs +188 -76
- package/src/commands/sticker.cjs +5 -16
- package/src/commands/stickers.cjs +16 -20
- package/src/commands/stopbot.cjs +9 -25
- package/src/commands/stt.cjs +75 -107
- package/src/commands/todo.cjs +11 -31
- package/src/commands/toimage.cjs +11 -27
- package/src/commands/translate.cjs +53 -36
- package/src/commands/tts.cjs +46 -40
- package/src/commands/unblock.cjs +3 -17
- package/src/commands/update.cjs +23 -0
- package/src/commands/weather.cjs +40 -47
- package/src/config/ai-identity.cjs +14 -1
- package/src/config/baileys.config.js +16 -3
- package/src/config/data-dir.cjs +37 -0
- package/src/config/defaults.cjs +1 -0
- package/src/config/exit-codes.cjs +11 -0
- package/src/config/lock.cjs +55 -81
- package/src/config/official-links.cjs +61 -0
- package/src/config/paths.cjs +4 -33
- package/src/config/release.cjs +87 -0
- package/src/config/settings.cjs +199 -10
- package/src/core/connection.js +77 -9
- package/src/core/events.js +119 -33
- package/src/core/session.js +536 -84
- package/src/core/socket.js +81 -10
- package/src/db/copy.cjs +136 -0
- package/src/db/db.cjs +241 -36
- package/src/db/startup-guard.js +5 -0
- package/src/db/store.cjs +561 -54
- package/src/diagnostics/baileysLog.cjs +87 -0
- package/src/diagnostics/debugLog.cjs +16 -4
- package/src/diagnostics/device.cjs +4 -2
- package/src/diagnostics/nodeRuntime.cjs +131 -0
- package/src/diagnostics/signalSessionInspect.cjs +38 -0
- package/src/domain/command.js +24 -22
- package/src/extensions/autoReply.cjs +521 -0
- package/src/extensions/context.cjs +73 -0
- package/src/extensions/contract.cjs +69 -0
- package/src/extensions/entitlement/client.cjs +381 -0
- package/src/extensions/entitlement/index.cjs +629 -0
- package/src/extensions/entitlement/installation.cjs +117 -0
- package/src/extensions/entitlement/keys.cjs +72 -0
- package/src/extensions/entitlement/state.cjs +201 -0
- package/src/extensions/entitlement/target.cjs +7 -0
- package/src/extensions/entitlement/token.cjs +225 -0
- package/src/extensions/host.cjs +519 -0
- package/src/extensions/templateKeys.cjs +81 -0
- package/src/handlers/command.handler.js +191 -64
- package/src/handlers/group.handler.js +78 -18
- package/src/handlers/message.handler.js +56 -25
- package/src/i18n/catalog.generated.cjs +3537 -0
- package/src/index.js +51 -12
- package/src/legal/bundled/ar.json +102 -0
- package/src/legal/bundled/en.json +102 -0
- package/src/legal/bundled/es-419.json +102 -0
- package/src/legal/bundled/pt-BR.json +102 -0
- package/src/legal/cli.cjs +200 -0
- package/src/legal/consent.cjs +187 -0
- package/src/legal/document.cjs +156 -0
- package/src/legal/keys.cjs +100 -0
- package/src/legal/release.cjs +226 -0
- package/src/legal/remote.cjs +419 -0
- package/src/legal/terms.cjs +142 -0
- package/src/messaging/escape.cjs +31 -0
- package/src/messaging/health.cjs +65 -0
- package/src/messaging/identity.cjs +42 -0
- package/src/messaging/message.cjs +136 -0
- package/src/messaging/parser.cjs +88 -0
- package/src/messaging/router.cjs +217 -0
- package/src/middleware/permissions.middleware.js +34 -2
- package/src/panel/account-profile.cjs +489 -0
- package/src/panel/fallback-page.cjs +177 -0
- package/src/panel/password-reset.cjs +13 -14
- package/src/panel/recovery-email.cjs +3 -1
- package/src/routes/backup.api.esm.js +175 -0
- package/src/routes/dashboard.api.esm.js +183 -7
- package/src/routes/plus.api.esm.js +172 -0
- package/src/routes/reactions.api.esm.js +60 -0
- package/src/services/aiAgent.cjs +56 -43
- package/src/services/aiDocs.cjs +187 -0
- package/src/services/aiImage.cjs +183 -0
- package/src/services/aiMedia.cjs +146 -0
- package/src/services/aiProviders.cjs +76 -80
- package/src/services/aiRouter.cjs +2 -2
- package/src/services/aiToolAuth.cjs +76 -4
- package/src/services/aiTools.cjs +431 -105
- package/src/services/autoDelete.cjs +9 -1
- package/src/services/levix-docs.generated.json +2623 -0
- package/src/services/memoryPolicy.cjs +56 -0
- package/src/services/updateCheck.cjs +362 -0
- package/src/stickers/errors.cjs +53 -97
- package/src/stickers/library.cjs +13 -6
- package/src/stickers/owner.cjs +1 -1
- package/src/utils/commandContext.cjs +13 -0
- package/src/utils/commandDocs.cjs +42 -20
- package/src/utils/geminiHttpOptions.cjs +56 -0
- package/src/utils/geminiMedia.cjs +28 -3
- package/src/utils/groupMetadataCache.cjs +386 -21
- package/src/utils/httpsFetch.cjs +113 -0
- package/src/utils/i18n.cjs +117 -7
- package/src/utils/linkPreview.cjs +90 -0
- package/src/utils/logger.cjs +6 -0
- package/src/utils/markdownParser.cjs +3 -3
- package/src/utils/memory.cjs +375 -28
- package/src/utils/messageContent.cjs +7 -1
- package/src/utils/moderationAction.cjs +116 -73
- package/src/utils/moderationSummary.cjs +32 -56
- package/src/utils/permissionLevel.cjs +7 -28
- package/src/utils/permissions.cjs +23 -14
- package/src/utils/permissions.esm.js +99 -166
- package/src/utils/recentMessageCache.esm.js +241 -37
- package/src/utils/recurrence.cjs +78 -66
- package/src/utils/sendBotMessage.cjs +14 -29
- package/src/utils/sendBotMessage.esm.js +28 -13
- package/src/utils/statusMessage.cjs +94 -33
- package/src/utils/stickerBot.cjs +18 -16
- package/src/utils/storage.cjs +2 -0
- package/src/utils/storage.esm.js +2 -0
- package/src/utils/thumbnail.cjs +5 -21
- package/public/dashboard/assets/index-B1sOW2VX.js +0 -561
- package/public/dashboard/assets/index-CK8osB4o.css +0 -2
- package/public/dashboard.css +0 -3127
- package/public/dashboard.js +0 -1981
- package/public/gateway-i18n.js +0 -321
- package/public/gateway.js +0 -70
- package/views/dashboard.ejs +0 -775
- package/views/login.ejs +0 -408
- package/views/qr.ejs +0 -155
- package/views/setup.ejs +0 -118
package/src/utils/memory.cjs
CHANGED
|
@@ -38,19 +38,30 @@ const path = require("path");
|
|
|
38
38
|
const logger = require("./logger.cjs");
|
|
39
39
|
|
|
40
40
|
const { dataPath, PACKAGE_ROOT } = require("../config/paths.cjs");
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
//
|
|
41
|
+
const { fsyncDirectory } = require("../db/db.cjs");
|
|
42
|
+
|
|
43
|
+
// The active root is always <data>/memory, so a backup of the data directory
|
|
44
|
+
// includes the AI memory. An older install that still has files in
|
|
45
|
+
// PACKAGE_ROOT/memory is copied into the data directory once, on first
|
|
46
|
+
// memory access — never at require() time, because a copy failure must not
|
|
47
|
+
// crash the process during module load. The copy never writes back to the
|
|
48
|
+
// legacy folder (a packaged or global install may have it read-only).
|
|
44
49
|
const LEGACY_ROOT = path.join(PACKAGE_ROOT, "memory");
|
|
45
|
-
const ROOT =
|
|
50
|
+
const ROOT = dataPath("memory");
|
|
46
51
|
const CHATS_DIR = path.join(ROOT, "chats");
|
|
47
52
|
const GLOBAL_FILE = path.join(ROOT, "global.md");
|
|
53
|
+
// Present after a successful copy, and after unlink, so a later pairing
|
|
54
|
+
// cannot import the previous account's PACKAGE_ROOT/memory. Not a memory file.
|
|
55
|
+
const LEGACY_COPY_MARKER = ".legacy-copied";
|
|
56
|
+
// Present for the duration of an in-place copy so a crash mid-loop can resume
|
|
57
|
+
// instead of looking like a conflict. Not a memory file.
|
|
58
|
+
const LEGACY_COPYING_MARKER = ".legacy-copying";
|
|
48
59
|
|
|
49
60
|
// Guardrails so a runaway agent can't grow the prompt without bound.
|
|
50
61
|
const MAX_ENTRY_CHARS = 2000;
|
|
51
62
|
// Read per call so the dashboard can change it without a restart.
|
|
52
63
|
const settings = require("../config/settings.cjs");
|
|
53
|
-
const {
|
|
64
|
+
const { t } = require("./i18n.cjs");
|
|
54
65
|
const maxContextChars = () => settings.get("memory_context_chars");
|
|
55
66
|
|
|
56
67
|
// ---------------------------------------------------------------------------
|
|
@@ -58,6 +69,7 @@ const maxContextChars = () => settings.get("memory_context_chars");
|
|
|
58
69
|
// ---------------------------------------------------------------------------
|
|
59
70
|
|
|
60
71
|
function ensureDirs() {
|
|
72
|
+
maybeMigrateLegacy();
|
|
61
73
|
fs.mkdirSync(CHATS_DIR, { recursive: true });
|
|
62
74
|
}
|
|
63
75
|
|
|
@@ -76,6 +88,7 @@ function normalizeScope(scope) {
|
|
|
76
88
|
|
|
77
89
|
/** Absolute path of the markdown file backing a scope. */
|
|
78
90
|
function memoryFilePath(scope, chatId) {
|
|
91
|
+
maybeMigrateLegacy();
|
|
79
92
|
if (normalizeScope(scope) === "global") return GLOBAL_FILE;
|
|
80
93
|
if (!chatId) return GLOBAL_FILE;
|
|
81
94
|
return path.join(CHATS_DIR, `${safeFileName(chatId)}.md`);
|
|
@@ -108,6 +121,7 @@ function headerFor(scope, chatId, chatName) {
|
|
|
108
121
|
}
|
|
109
122
|
|
|
110
123
|
function readRaw(filePath) {
|
|
124
|
+
maybeMigrateLegacy();
|
|
111
125
|
try {
|
|
112
126
|
return fs.readFileSync(filePath, "utf8");
|
|
113
127
|
} catch {
|
|
@@ -120,6 +134,314 @@ function writeRaw(filePath, content) {
|
|
|
120
134
|
fs.writeFileSync(filePath, content, "utf8");
|
|
121
135
|
}
|
|
122
136
|
|
|
137
|
+
// ---------------------------------------------------------------------------
|
|
138
|
+
// legacy copy (PACKAGE_ROOT/memory -> <data>/memory)
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
let legacyCopyAttempted = false;
|
|
142
|
+
|
|
143
|
+
function emitLog(log, level, obj, msg) {
|
|
144
|
+
if (!log || typeof log[level] !== "function") return;
|
|
145
|
+
try {
|
|
146
|
+
log[level](obj, msg);
|
|
147
|
+
} catch {
|
|
148
|
+
// A log sink must not fail a copy or a skip.
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function legacyCopyMarkerPath(dir) {
|
|
153
|
+
return path.join(dir, LEGACY_COPY_MARKER);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function hasLegacyCopyMarker(dir) {
|
|
157
|
+
try {
|
|
158
|
+
fs.lstatSync(legacyCopyMarkerPath(dir));
|
|
159
|
+
return true;
|
|
160
|
+
} catch {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function writeLegacyCopyMarker(dir) {
|
|
166
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
167
|
+
const marker = legacyCopyMarkerPath(dir);
|
|
168
|
+
fs.writeFileSync(marker, "", "utf8");
|
|
169
|
+
const fd = fs.openSync(marker, "r");
|
|
170
|
+
try { fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
|
|
171
|
+
// Copied first, then drop the in-progress marker, so a crash between the
|
|
172
|
+
// two still skips on the next start instead of looking like a conflict.
|
|
173
|
+
removeLegacyCopyingMarker(dir);
|
|
174
|
+
fsyncDirectory(dir);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function legacyCopyingMarkerPath(dir) {
|
|
178
|
+
return path.join(dir, LEGACY_COPYING_MARKER);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function hasLegacyCopyingMarker(dir) {
|
|
182
|
+
try {
|
|
183
|
+
fs.lstatSync(legacyCopyingMarkerPath(dir));
|
|
184
|
+
return true;
|
|
185
|
+
} catch {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function writeLegacyCopyingMarker(dir) {
|
|
191
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
192
|
+
fs.writeFileSync(legacyCopyingMarkerPath(dir), "", "utf8");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function removeLegacyCopyingMarker(dir) {
|
|
196
|
+
try {
|
|
197
|
+
fs.unlinkSync(legacyCopyingMarkerPath(dir));
|
|
198
|
+
} catch {
|
|
199
|
+
// Already gone, or the in-place path never wrote it.
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Writes `.legacy-copied` so a later migrate will not import PACKAGE_ROOT/memory. */
|
|
204
|
+
function markLegacyMemoryHandled(dir = ROOT) {
|
|
205
|
+
writeLegacyCopyMarker(dir);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function isRegularMarkdownFile(filePath) {
|
|
209
|
+
if (!String(filePath).endsWith(".md")) return false;
|
|
210
|
+
try {
|
|
211
|
+
const st = fs.lstatSync(filePath);
|
|
212
|
+
return st.isFile() && !st.isSymbolicLink();
|
|
213
|
+
} catch {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function isLiveMemoryMarkdownName(name) {
|
|
219
|
+
return (
|
|
220
|
+
name.endsWith(".md") && name !== LEGACY_COPY_MARKER && name !== LEGACY_COPYING_MARKER
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function listMarkdownMemoryFiles(root) {
|
|
225
|
+
const files = [];
|
|
226
|
+
if (!root) return files;
|
|
227
|
+
|
|
228
|
+
if (isRegularMarkdownFile(path.join(root, "global.md"))) {
|
|
229
|
+
files.push("global.md");
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const chatsDir = path.join(root, "chats");
|
|
233
|
+
try {
|
|
234
|
+
const st = fs.lstatSync(chatsDir);
|
|
235
|
+
if (st.isSymbolicLink() || !st.isDirectory()) return files;
|
|
236
|
+
} catch {
|
|
237
|
+
return files;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
let names = [];
|
|
241
|
+
try {
|
|
242
|
+
names = fs.readdirSync(chatsDir);
|
|
243
|
+
} catch {
|
|
244
|
+
return files;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
for (const name of names) {
|
|
248
|
+
if (!isLiveMemoryMarkdownName(name)) continue;
|
|
249
|
+
if (isRegularMarkdownFile(path.join(chatsDir, name))) {
|
|
250
|
+
files.push(path.join("chats", name));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return files;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function siblingCopyPrefix(dataMemoryDir) {
|
|
257
|
+
return `${path.basename(dataMemoryDir)}.legacy-copy-`;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function removeSiblingCopyTemps(dataMemoryDir) {
|
|
261
|
+
const parent = path.dirname(dataMemoryDir);
|
|
262
|
+
const prefix = siblingCopyPrefix(dataMemoryDir);
|
|
263
|
+
let names = [];
|
|
264
|
+
try {
|
|
265
|
+
names = fs.readdirSync(parent);
|
|
266
|
+
} catch {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
for (const name of names) {
|
|
270
|
+
if (!name.startsWith(prefix)) continue;
|
|
271
|
+
try {
|
|
272
|
+
fs.rmSync(path.join(parent, name), { recursive: true, force: true });
|
|
273
|
+
} catch {
|
|
274
|
+
// Best effort; the copy may still succeed.
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function removeInPlaceCopyTemps(dataMemoryDir) {
|
|
280
|
+
const dirs = [dataMemoryDir, path.join(dataMemoryDir, "chats")];
|
|
281
|
+
for (const dir of dirs) {
|
|
282
|
+
let names = [];
|
|
283
|
+
try {
|
|
284
|
+
names = fs.readdirSync(dir);
|
|
285
|
+
} catch {
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
for (const name of names) {
|
|
289
|
+
if (!name.includes(".legacy-copy-")) continue;
|
|
290
|
+
try {
|
|
291
|
+
fs.rmSync(path.join(dir, name), { force: true });
|
|
292
|
+
} catch {
|
|
293
|
+
// Best effort; the copy may still succeed.
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function copyIntoNewDir(dataMemoryDir, legacyDir, files) {
|
|
300
|
+
const tmpDir = path.join(
|
|
301
|
+
path.dirname(dataMemoryDir),
|
|
302
|
+
`${siblingCopyPrefix(dataMemoryDir)}${process.pid}`,
|
|
303
|
+
);
|
|
304
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
305
|
+
fs.mkdirSync(tmpDir, { recursive: true });
|
|
306
|
+
try {
|
|
307
|
+
for (const rel of files) {
|
|
308
|
+
const dest = path.join(tmpDir, rel);
|
|
309
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
310
|
+
fs.copyFileSync(path.join(legacyDir, rel), dest);
|
|
311
|
+
}
|
|
312
|
+
writeLegacyCopyMarker(tmpDir);
|
|
313
|
+
fs.renameSync(tmpDir, dataMemoryDir);
|
|
314
|
+
} catch (err) {
|
|
315
|
+
try {
|
|
316
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
317
|
+
} catch {
|
|
318
|
+
// The original error is the one the caller needs.
|
|
319
|
+
}
|
|
320
|
+
throw err;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function destPathExists(filePath) {
|
|
325
|
+
try {
|
|
326
|
+
fs.lstatSync(filePath);
|
|
327
|
+
return true;
|
|
328
|
+
} catch {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function copyIntoExistingDir(dataMemoryDir, legacyDir, files, { afterFile } = {}) {
|
|
334
|
+
fs.mkdirSync(dataMemoryDir, { recursive: true });
|
|
335
|
+
// Before the first file, so a crash mid-loop resumes instead of conflicting.
|
|
336
|
+
writeLegacyCopyingMarker(dataMemoryDir);
|
|
337
|
+
for (const rel of files) {
|
|
338
|
+
const dest = path.join(dataMemoryDir, rel);
|
|
339
|
+
if (destPathExists(dest)) continue;
|
|
340
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
341
|
+
const tmp = path.join(
|
|
342
|
+
path.dirname(dest),
|
|
343
|
+
`.${path.basename(dest)}.legacy-copy-${process.pid}`,
|
|
344
|
+
);
|
|
345
|
+
try {
|
|
346
|
+
fs.copyFileSync(path.join(legacyDir, rel), tmp);
|
|
347
|
+
fs.renameSync(tmp, dest);
|
|
348
|
+
} catch (err) {
|
|
349
|
+
try {
|
|
350
|
+
fs.rmSync(tmp, { force: true });
|
|
351
|
+
} catch {
|
|
352
|
+
// The original error is the one the caller needs.
|
|
353
|
+
}
|
|
354
|
+
throw err;
|
|
355
|
+
}
|
|
356
|
+
// Test hook: throw after a committed file to simulate a crash. Production
|
|
357
|
+
// never passes afterFile.
|
|
358
|
+
if (typeof afterFile === "function") afterFile(rel);
|
|
359
|
+
}
|
|
360
|
+
writeLegacyCopyMarker(dataMemoryDir);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Decide whether a PACKAGE_ROOT/memory tree should be copied into the data
|
|
365
|
+
* directory. Pure: no writes. Tests pass temp folders so they never touch
|
|
366
|
+
* the real install.
|
|
367
|
+
*
|
|
368
|
+
* @returns {{action: "copy"|"skip"|"conflict", reason: string, files: string[]}}
|
|
369
|
+
*/
|
|
370
|
+
function planLegacyMemoryCopy({ dataMemoryDir, legacyDir } = {}) {
|
|
371
|
+
if (hasLegacyCopyMarker(dataMemoryDir)) {
|
|
372
|
+
return { action: "skip", reason: "marker", files: [] };
|
|
373
|
+
}
|
|
374
|
+
const destFiles = listMarkdownMemoryFiles(dataMemoryDir);
|
|
375
|
+
const srcFiles = listMarkdownMemoryFiles(legacyDir);
|
|
376
|
+
if (hasLegacyCopyingMarker(dataMemoryDir)) {
|
|
377
|
+
const destSet = new Set(destFiles);
|
|
378
|
+
const missing = srcFiles.filter((rel) => !destSet.has(rel));
|
|
379
|
+
return { action: "copy", reason: "resume", files: missing };
|
|
380
|
+
}
|
|
381
|
+
if (srcFiles.length && destFiles.length) {
|
|
382
|
+
return { action: "conflict", reason: "both", files: srcFiles };
|
|
383
|
+
}
|
|
384
|
+
if (!srcFiles.length) {
|
|
385
|
+
return { action: "skip", reason: "no-legacy", files: [] };
|
|
386
|
+
}
|
|
387
|
+
return { action: "copy", reason: "legacy-only", files: srcFiles };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function migrateLegacyMemory({ dataMemoryDir, legacyDir, log, afterFile } = {}) {
|
|
391
|
+
const plan = planLegacyMemoryCopy({ dataMemoryDir, legacyDir });
|
|
392
|
+
if (plan.action === "conflict") {
|
|
393
|
+
emitLog(
|
|
394
|
+
log,
|
|
395
|
+
"warn",
|
|
396
|
+
{ legacy: legacyDir, data: dataMemoryDir },
|
|
397
|
+
`[memory] ${legacyDir} still holds memory files; using ${dataMemoryDir}. Merge by hand if needed.`,
|
|
398
|
+
);
|
|
399
|
+
return { ...plan, copied: false };
|
|
400
|
+
}
|
|
401
|
+
if (plan.action !== "copy") {
|
|
402
|
+
return { ...plan, copied: false };
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
try {
|
|
406
|
+
removeSiblingCopyTemps(dataMemoryDir);
|
|
407
|
+
if (fs.existsSync(dataMemoryDir)) {
|
|
408
|
+
removeInPlaceCopyTemps(dataMemoryDir);
|
|
409
|
+
copyIntoExistingDir(dataMemoryDir, legacyDir, plan.files, { afterFile });
|
|
410
|
+
} else {
|
|
411
|
+
copyIntoNewDir(dataMemoryDir, legacyDir, plan.files);
|
|
412
|
+
}
|
|
413
|
+
emitLog(
|
|
414
|
+
log,
|
|
415
|
+
"info",
|
|
416
|
+
{ from: legacyDir, to: dataMemoryDir, files: plan.files.length },
|
|
417
|
+
`[memory] copied ${plan.files.length} file(s) from ${legacyDir} into ${dataMemoryDir}`,
|
|
418
|
+
);
|
|
419
|
+
return { ...plan, copied: true };
|
|
420
|
+
} catch (err) {
|
|
421
|
+
emitLog(
|
|
422
|
+
log,
|
|
423
|
+
"error",
|
|
424
|
+
{ err, from: legacyDir, to: dataMemoryDir },
|
|
425
|
+
"[memory] failed to copy legacy memory; continuing with the data directory",
|
|
426
|
+
);
|
|
427
|
+
return { ...plan, copied: false };
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function maybeMigrateLegacy() {
|
|
432
|
+
if (legacyCopyAttempted) return;
|
|
433
|
+
legacyCopyAttempted = true;
|
|
434
|
+
try {
|
|
435
|
+
migrateLegacyMemory({
|
|
436
|
+
dataMemoryDir: ROOT,
|
|
437
|
+
legacyDir: LEGACY_ROOT,
|
|
438
|
+
log: logger,
|
|
439
|
+
});
|
|
440
|
+
} catch (err) {
|
|
441
|
+
logger.error({ err }, "[memory] legacy copy failed");
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
123
445
|
// ---------------------------------------------------------------------------
|
|
124
446
|
// parsing
|
|
125
447
|
// ---------------------------------------------------------------------------
|
|
@@ -212,7 +534,7 @@ function addMemory({ scope = "chat", chatId, content, by, chatName } = {}) {
|
|
|
212
534
|
const text = String(content || "")
|
|
213
535
|
.trim()
|
|
214
536
|
.slice(0, MAX_ENTRY_CHARS);
|
|
215
|
-
if (!text) throw new Error(
|
|
537
|
+
if (!text) throw new Error(t("bot.ai.memoryStore.nothingToSave"));
|
|
216
538
|
|
|
217
539
|
const filePath = memoryFilePath(normalizedScope, chatId);
|
|
218
540
|
ensureDirs();
|
|
@@ -298,30 +620,38 @@ function clearMemory({ scope = "chat", chatId } = {}) {
|
|
|
298
620
|
* global facts may silently cross that account boundary.
|
|
299
621
|
*/
|
|
300
622
|
function clearAllMemoryFiles() {
|
|
623
|
+
// Unlink is an account boundary. Record that the copy is done before
|
|
624
|
+
// deleting .md files, so a later access cannot import PACKAGE_ROOT/memory
|
|
625
|
+
// from the previous account.
|
|
626
|
+
writeLegacyCopyMarker(ROOT);
|
|
627
|
+
|
|
628
|
+
maybeMigrateLegacy();
|
|
629
|
+
|
|
301
630
|
let removed = 0;
|
|
302
631
|
try {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
removed += 1;
|
|
306
|
-
}
|
|
632
|
+
fs.unlinkSync(GLOBAL_FILE);
|
|
633
|
+
removed += 1;
|
|
307
634
|
} catch (err) {
|
|
308
|
-
|
|
635
|
+
if (err.code !== "ENOENT") throw err;
|
|
309
636
|
}
|
|
310
637
|
|
|
638
|
+
let names;
|
|
311
639
|
try {
|
|
312
|
-
|
|
313
|
-
for (const name of fs.readdirSync(CHATS_DIR)) {
|
|
314
|
-
if (!name.endsWith(".md")) continue;
|
|
315
|
-
const filePath = path.join(CHATS_DIR, name);
|
|
316
|
-
const stat = fs.lstatSync(filePath);
|
|
317
|
-
if (!stat.isFile() || stat.isSymbolicLink()) continue;
|
|
318
|
-
fs.unlinkSync(filePath);
|
|
319
|
-
removed += 1;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
640
|
+
names = fs.readdirSync(CHATS_DIR);
|
|
322
641
|
} catch (err) {
|
|
323
|
-
|
|
642
|
+
if (err.code !== "ENOENT") throw err;
|
|
643
|
+
names = [];
|
|
644
|
+
}
|
|
645
|
+
for (const name of names) {
|
|
646
|
+
if (!isLiveMemoryMarkdownName(name)) continue;
|
|
647
|
+
const filePath = path.join(CHATS_DIR, name);
|
|
648
|
+
const stat = fs.lstatSync(filePath);
|
|
649
|
+
if (!stat.isFile() || stat.isSymbolicLink()) continue;
|
|
650
|
+
fs.unlinkSync(filePath);
|
|
651
|
+
removed += 1;
|
|
324
652
|
}
|
|
653
|
+
|
|
654
|
+
writeLegacyCopyMarker(ROOT);
|
|
325
655
|
return removed;
|
|
326
656
|
}
|
|
327
657
|
|
|
@@ -367,8 +697,8 @@ function clipTail(text, limitChars) {
|
|
|
367
697
|
* character budget when both have content so one busy file can't starve the
|
|
368
698
|
* other.
|
|
369
699
|
*/
|
|
370
|
-
function buildMemoryContext(chatId, { limitChars = maxContextChars() } = {}) {
|
|
371
|
-
const globalText = memoryFileContext(readRaw(GLOBAL_FILE));
|
|
700
|
+
function buildMemoryContext(chatId, { limitChars = maxContextChars(), includeGlobal = true } = {}) {
|
|
701
|
+
const globalText = includeGlobal ? memoryFileContext(readRaw(GLOBAL_FILE)) : "";
|
|
372
702
|
const chatText = chatId ? memoryFileContext(readRaw(memoryFilePath("chat", chatId))) : "";
|
|
373
703
|
if (!globalText && !chatText) return "";
|
|
374
704
|
|
|
@@ -394,11 +724,12 @@ function buildMemoryContext(chatId, { limitChars = maxContextChars() } = {}) {
|
|
|
394
724
|
|
|
395
725
|
/** Counts + file paths, for `!memory` and `!status`. */
|
|
396
726
|
function memoryStats(chatId) {
|
|
727
|
+
maybeMigrateLegacy();
|
|
397
728
|
const globalEntries = listMemory({ scope: "global" });
|
|
398
729
|
const chatEntries = chatId ? listMemory({ scope: "chat", chatId }) : [];
|
|
399
730
|
let chatFiles = 0;
|
|
400
731
|
try {
|
|
401
|
-
chatFiles = fs.readdirSync(CHATS_DIR).filter((f) => f
|
|
732
|
+
chatFiles = fs.readdirSync(CHATS_DIR).filter((f) => isLiveMemoryMarkdownName(f)).length;
|
|
402
733
|
} catch {
|
|
403
734
|
chatFiles = 0;
|
|
404
735
|
}
|
|
@@ -413,9 +744,22 @@ function memoryStats(chatId) {
|
|
|
413
744
|
}
|
|
414
745
|
|
|
415
746
|
module.exports = {
|
|
416
|
-
ROOT
|
|
417
|
-
|
|
418
|
-
|
|
747
|
+
// Getters so the dashboard's first read of GLOBAL_FILE / ROOT still runs
|
|
748
|
+
// the copy: those routes write files themselves and never call ensureDirs.
|
|
749
|
+
get ROOT() {
|
|
750
|
+
maybeMigrateLegacy();
|
|
751
|
+
return ROOT;
|
|
752
|
+
},
|
|
753
|
+
get CHATS_DIR() {
|
|
754
|
+
maybeMigrateLegacy();
|
|
755
|
+
return CHATS_DIR;
|
|
756
|
+
},
|
|
757
|
+
get GLOBAL_FILE() {
|
|
758
|
+
maybeMigrateLegacy();
|
|
759
|
+
return GLOBAL_FILE;
|
|
760
|
+
},
|
|
761
|
+
LEGACY_COPY_MARKER,
|
|
762
|
+
LEGACY_COPYING_MARKER,
|
|
419
763
|
ensureDirs,
|
|
420
764
|
normalizeScope,
|
|
421
765
|
memoryFilePath,
|
|
@@ -428,4 +772,7 @@ module.exports = {
|
|
|
428
772
|
buildMemoryContext,
|
|
429
773
|
memoryStats,
|
|
430
774
|
parseEntries,
|
|
775
|
+
planLegacyMemoryCopy,
|
|
776
|
+
migrateLegacyMemory,
|
|
777
|
+
markLegacyMemoryHandled,
|
|
431
778
|
};
|
|
@@ -56,7 +56,13 @@ function pickMedia(message) {
|
|
|
56
56
|
|
|
57
57
|
function quotedOf(msg) {
|
|
58
58
|
const inner = unwrapMessage(msg?.message);
|
|
59
|
-
for (const key of [
|
|
59
|
+
for (const key of [
|
|
60
|
+
"extendedTextMessage",
|
|
61
|
+
"imageMessage",
|
|
62
|
+
"videoMessage",
|
|
63
|
+
"audioMessage",
|
|
64
|
+
"documentMessage",
|
|
65
|
+
]) {
|
|
60
66
|
const quoted = inner[key]?.contextInfo?.quotedMessage;
|
|
61
67
|
if (quoted) return quoted;
|
|
62
68
|
}
|