levix-bot 4.7.0 → 4.8.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.
@@ -1,4 +1,5 @@
1
1
  const path = require("path");
2
+ const fs = require("node:fs");
2
3
  const pino = require("pino");
3
4
 
4
5
  const { ensureDataDir } = require("../config/paths.cjs");
@@ -7,10 +8,7 @@ const { ensureDataDir } = require("../config/paths.cjs");
7
8
  // global install never tries to write next to its own code.
8
9
  const logDir = ensureDataDir("logs");
9
10
 
10
- // Pino's `transport` option runs each target in a worker thread, and a worker
11
- // loads its target by module path — which a single-file executable can't
12
- // provide. Same three destinations either way; packaged builds just wire them
13
- // as plain streams in this thread.
11
+ // A packaged executable cannot load a worker target by module path.
14
12
  function isPackaged() {
15
13
  try {
16
14
  return require("node:sea").isSea() === true;
@@ -44,35 +42,91 @@ const PRETTY = {
44
42
  const combined = path.join(logDir, "combined.log");
45
43
  const errors = path.join(logDir, "error.log");
46
44
 
47
- const logger = useInProcessLogs()
48
- ? pino(
49
- base,
50
- pino.multistream([
51
- { level: "trace", stream: pino.destination({ dest: combined, mkdir: true }) },
52
- { level: "error", stream: pino.destination({ dest: errors, mkdir: true }) },
53
- { level: "trace", stream: require("pino-pretty")(PRETTY) },
54
- ]),
55
- )
56
- : pino({
57
- ...base,
58
- transport: {
59
- targets: [
60
- // Combined log file (all levels)
61
- {
62
- target: "pino/file",
63
- level: "trace",
64
- options: { destination: combined, mkdir: true },
65
- },
66
- // Error log file (errors only)
67
- {
68
- target: "pino/file",
69
- level: "error",
70
- options: { destination: errors, mkdir: true },
71
- },
72
- // Pretty console output
73
- { target: "pino-pretty", level: "trace", options: PRETTY },
74
- ],
75
- },
76
- });
45
+ // Own file writes in this thread so clearing has a precise boundary. Async
46
+ // SonicBoom flushSync() does not wait for an already in-flight write, which
47
+ // could otherwise restore an old entry after truncation. Sync destinations
48
+ // are ready immediately and leave no pending file writes behind.
49
+ const combinedStream = pino.destination({ dest: combined, mkdir: true, sync: true });
50
+ const errorStream = pino.destination({ dest: errors, mkdir: true, sync: true });
51
+ const logger = pino(
52
+ base,
53
+ pino.multistream([
54
+ { level: "trace", stream: combinedStream },
55
+ { level: "error", stream: errorStream },
56
+ {
57
+ level: "trace",
58
+ stream: useInProcessLogs()
59
+ ? require("pino-pretty")(PRETTY)
60
+ : pino.transport({ target: "pino-pretty", options: PRETTY }),
61
+ },
62
+ ]),
63
+ );
64
+
65
+ const MAX_READ_BYTES = 256 * 1024;
66
+
67
+ /** Read only the bounded tail of the operational log, never arbitrary files. */
68
+ logger.readLogs = function readLogs(limit = 500) {
69
+ const requested = Number(limit);
70
+ const count = Number.isFinite(requested)
71
+ ? Math.max(1, Math.min(500, Math.trunc(requested)))
72
+ : 500;
73
+ const fd = fs.openSync(combined, "r");
74
+ let text;
75
+ try {
76
+ const size = fs.fstatSync(fd).size;
77
+ const start = Math.max(0, size - MAX_READ_BYTES);
78
+ const buffer = Buffer.alloc(Math.min(size, MAX_READ_BYTES));
79
+ const bytesRead = fs.readSync(fd, buffer, 0, buffer.length, start);
80
+ text = buffer.toString("utf8", 0, bytesRead);
81
+ // The byte boundary can land inside a UTF-8 character or a JSON record.
82
+ if (start > 0) {
83
+ const newline = text.indexOf("\n");
84
+ text = newline < 0 ? "" : text.slice(newline + 1);
85
+ }
86
+ } finally {
87
+ fs.closeSync(fd);
88
+ }
89
+
90
+ const entries = [];
91
+ for (const line of text.split("\n")) {
92
+ if (!line) continue;
93
+ let entry;
94
+ try {
95
+ entry = JSON.parse(line);
96
+ } catch {
97
+ continue; // A truncated or malformed record must not hide newer logs.
98
+ }
99
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) continue;
100
+ const level = typeof entry.level === "number" ? entry.level : 30;
101
+ const normalizedLevel =
102
+ level >= 50 ? "error" : level >= 40 ? "warn" : level >= 30 ? "info" : "debug";
103
+ const date =
104
+ typeof entry.time === "number" || typeof entry.time === "string"
105
+ ? new Date(entry.time)
106
+ : new Date(NaN);
107
+ const time = Number.isFinite(date.getTime()) ? date.toISOString() : "";
108
+ let msg =
109
+ typeof entry.msg === "string"
110
+ ? entry.msg
111
+ : entry.msg == null
112
+ ? ""
113
+ : JSON.stringify(entry.msg);
114
+ const error = entry.err ?? entry.error;
115
+ if (error && typeof error === "object") {
116
+ const detail = error.stack || error.message;
117
+ if (typeof detail === "string" && detail !== msg) msg = msg ? `${msg}\n${detail}` : detail;
118
+ }
119
+ entries.push({ time, level: normalizedLevel, msg });
120
+ }
121
+ return entries.slice(-count);
122
+ };
123
+
124
+ /** Truncate the open files in place, keeping later writes on the same files. */
125
+ logger.clearLogs = function clearLogs() {
126
+ combinedStream.flushSync();
127
+ errorStream.flushSync();
128
+ fs.ftruncateSync(combinedStream.fd, 0);
129
+ fs.ftruncateSync(errorStream.fd, 0);
130
+ };
77
131
 
78
132
  module.exports = logger;
@@ -1,161 +1,233 @@
1
+ // Markdown from any provider -> WhatsApp text. This is a display boundary:
2
+ // stored AI history, code contents, identifiers and URLs remain untouched.
1
3
  const { tr } = require("./i18n.cjs");
2
4
 
3
- // file: src/utils/markdownParser.cjs
4
- //
5
- // Converts standard Markdown output (especially from LLMs) into human-readable
6
- // WhatsApp formatting:
7
- // - **bold** -> *bold*
8
- // - ~~strike~~ -> ~strike~
9
- // - __italic__ -> _italic_
10
- // - # Headings -> *Headings*
11
- // - Markdown tables (| col1 | col2 |) -> clean, itemized WhatsApp cards
12
-
13
- /**
14
- * Parses markdown table text into structured data:
15
- * { headers: string[], rows: string[][] }
16
- */
17
- function parseMarkdownTable(tableLines) {
18
- const cleanCells = (line) => {
19
- return line
20
- .trim()
21
- .replace(/^\|/, "")
22
- .replace(/\|$/, "")
23
- .split("|")
24
- .map((c) => c.trim());
25
- };
26
-
27
- if (tableLines.length < 2) return null;
28
-
29
- const headers = cleanCells(tableLines[0]);
30
- // Line 1 is usually the separator: |---|---|
31
- const separatorLine = tableLines[1];
32
- if (!/^[\s|:-]+$/.test(separatorLine)) return null;
33
-
34
- const rows = [];
35
- for (let i = 2; i < tableLines.length; i++) {
36
- const line = tableLines[i].trim();
37
- if (!line) continue;
38
- rows.push(cleanCells(line));
5
+ /** Split table cells without splitting escaped pipes or pipes inside code. */
6
+ function tableCells(line) {
7
+ const cells = [];
8
+ let cell = "";
9
+ let ticks = 0;
10
+ const text = line.trim();
11
+ for (let i = 0; i < text.length; i++) {
12
+ if (text[i] === "\\" && text[i + 1] === "|") {
13
+ cell += "|";
14
+ i++;
15
+ } else if (text[i] === "`") {
16
+ let end = i + 1;
17
+ while (text[end] === "`") end++;
18
+ const run = end - i;
19
+ if (!ticks) ticks = run;
20
+ else if (ticks === run) ticks = 0;
21
+ cell += text.slice(i, end);
22
+ i = end - 1;
23
+ } else if (text[i] === "|" && !ticks) {
24
+ cells.push(cell.trim());
25
+ cell = "";
26
+ } else cell += text[i];
39
27
  }
40
-
41
- return { headers, rows };
28
+ cells.push(cell.trim());
29
+ if (text.startsWith("|")) cells.shift();
30
+ if (text.endsWith("|") && !text.endsWith("\\|")) cells.pop();
31
+ return cells;
42
32
  }
43
33
 
44
- /**
45
- * Formats parsed table data into a clean WhatsApp-friendly list of cards.
46
- */
47
- function formatTableForWhatsApp({ headers, rows }) {
48
- if (!headers.length || !rows.length) return "";
49
-
50
- const cards = [];
34
+ function parseMarkdownTable(lines) {
35
+ if (lines.length < 2 || !lines[0].includes("|")) return null;
36
+ const headers = tableCells(lines[0]);
37
+ const separators = tableCells(lines[1]);
38
+ if (
39
+ !headers.length ||
40
+ headers.length !== separators.length ||
41
+ !separators.every((cell) => /^:?-{3,}:?$/.test(cell))
42
+ )
43
+ return null;
44
+ return { headers, rows: lines.slice(2).map(tableCells) };
45
+ }
51
46
 
52
- rows.forEach((row, rowIndex) => {
53
- const primaryLabel = row[0] || tr(`Item ${rowIndex + 1}`, `عنصر ${rowIndex + 1}`);
54
- const fields = [];
47
+ function bold(value) {
48
+ // The whole heading/label is bold; flatten inner bold spans before wrapping.
49
+ const text = value.trim().replace(/\*([^*\n]+)\*/g, "$1");
50
+ return `*${text}*`;
51
+ }
55
52
 
56
- // If there's more than 1 column, list the remaining fields with bullet points
57
- if (headers.length > 1) {
58
- for (let c = 0; c < headers.length; c++) {
59
- const headerName = headers[c] || tr(`Column ${c + 1}`, `عمود ${c + 1}`);
60
- const cellValue = row[c] || "—";
61
- fields.push(` • *${headerName}:* ${cellValue}`);
53
+ function tableText({ headers, rows }) {
54
+ if (!rows.length) return headers.map(bold).join(" · ");
55
+ if (headers.length === 1 && rows.every((row) => row.length <= 1)) {
56
+ return `${bold(headers[0])}\n${rows.map((row) => `• ${row[0] || "—"}`).join("\n")}`;
57
+ }
58
+ return rows
59
+ .map((row, index) => {
60
+ const title = row[0] || tr(`Item ${index + 1}`, `عنصر ${index + 1}`);
61
+ const fields = [];
62
+ for (let c = 1; c < Math.max(headers.length, row.length); c++) {
63
+ const header = headers[c] || tr(`Column ${c + 1}`, `عمود ${c + 1}`);
64
+ const label = header.replace(/^\*([^*]+)\*$/, "$1");
65
+ const value = (row[c] || "—").replace(/<br\s*\/?>/gi, "\n");
66
+ fields.push(` • ${bold(`${label}:`)} ${value.replace(/\n/g, "\n ")}`);
62
67
  }
63
- } else {
64
- fields.push(` • ${primaryLabel}`);
65
- }
66
-
67
- cards.push(`📋 *${primaryLabel}*\n${fields.join("\n")}`);
68
- });
69
-
70
- return cards.join("\n\n");
68
+ return `📋 ${bold(title)}\n${fields.join("\n")}`;
69
+ })
70
+ .join("\n\n");
71
71
  }
72
72
 
73
- /**
74
- * Replaces markdown table blocks with WhatsApp-friendly formatted cards.
75
- */
73
+ /** Only a header followed by a real delimiter row starts a table. */
76
74
  function convertMarkdownTables(text) {
77
75
  const lines = text.split("\n");
78
76
  const output = [];
79
- let currentTable = [];
80
-
81
- const flushTable = () => {
82
- if (currentTable.length >= 2) {
83
- const parsed = parseMarkdownTable(currentTable);
84
- if (parsed) {
85
- output.push(formatTableForWhatsApp(parsed));
86
- } else {
87
- output.push(...currentTable);
88
- }
89
- } else {
90
- output.push(...currentTable);
77
+ for (let i = 0; i < lines.length; i++) {
78
+ const parsed = parseMarkdownTable(lines.slice(i, i + 2));
79
+ if (!parsed) {
80
+ output.push(lines[i]);
81
+ continue;
82
+ }
83
+ i += 2;
84
+ while (i < lines.length && lines[i].trim() && lines[i].includes("|")) {
85
+ // A second header/delimiter pair belongs to a separate table.
86
+ if (parseMarkdownTable(lines.slice(i, i + 2))) break;
87
+ parsed.rows.push(tableCells(lines[i++]));
91
88
  }
92
- currentTable = [];
93
- };
89
+ i--;
90
+ output.push(tableText(parsed));
91
+ }
92
+ return output.join("\n");
93
+ }
94
94
 
95
- for (const line of lines) {
96
- if (line.trim().startsWith("|") && line.trim().endsWith("|")) {
97
- currentTable.push(line);
95
+ function shieldCode(text, store) {
96
+ const runs = /`+|~{3,}/g;
97
+ let output = "";
98
+ let cursor = 0;
99
+ for (let match = runs.exec(text); match; match = runs.exec(text)) {
100
+ const marker = match[0];
101
+ const start = match.index;
102
+ const lineStart = text.lastIndexOf("\n", start - 1) + 1;
103
+ const lineEnd = text.indexOf("\n", runs.lastIndex);
104
+ const info = lineEnd < 0 ? "" : text.slice(runs.lastIndex, lineEnd);
105
+ const fenced =
106
+ marker.length >= 3 &&
107
+ lineEnd >= 0 &&
108
+ /^ {0,3}$/.test(text.slice(lineStart, start)) &&
109
+ !info.includes(marker[0]);
110
+ let rendered;
111
+ let end;
112
+ if (fenced) {
113
+ const close = new RegExp(`^ {0,3}${marker[0]}{${marker.length},}[ \\t]*$`, "gm");
114
+ close.lastIndex = lineEnd + 1;
115
+ const ending = close.exec(text);
116
+ let body = text.slice(lineEnd + 1, ending ? ending.index : text.length);
117
+ if (ending && body.endsWith("\n")) body = body.slice(0, -1);
118
+ rendered = `\`\`\`\n${body}\n\`\`\``;
119
+ end = ending ? ending.index + ending[0].length : text.length;
98
120
  } else {
99
- flushTable();
100
- output.push(line);
121
+ if (marker[0] !== "`") continue;
122
+ const close = new RegExp(`(?<!\`)\`{${marker.length}}(?!\`)`, "g");
123
+ close.lastIndex = runs.lastIndex;
124
+ const ending = close.exec(text);
125
+ if (!ending) continue;
126
+ end = ending.index + marker.length;
127
+ rendered =
128
+ marker.length === 2
129
+ ? `\`\`\`${text.slice(runs.lastIndex, ending.index)}\`\`\``
130
+ : text.slice(start, end);
101
131
  }
132
+ output += text.slice(cursor, start) + store(rendered);
133
+ cursor = end;
134
+ runs.lastIndex = end;
102
135
  }
103
- flushTable();
136
+ return output + text.slice(cursor);
137
+ }
104
138
 
105
- return output.join("\n");
139
+ function links(text, store) {
140
+ let output = "";
141
+ let cursor = 0;
142
+ let skipUntil = 0;
143
+ for (const match of text.matchAll(/!?\[([^[\]\n]*)\]\(/g)) {
144
+ if (match.index < cursor || match.index < skipUntil) continue;
145
+ const start = match.index + match[0].length;
146
+ let depth = 1;
147
+ let end = start;
148
+ for (; end < text.length && text[end] !== "\n"; end++) {
149
+ if (text[end] === "\\") {
150
+ end++;
151
+ continue;
152
+ }
153
+ if (text[end] === "(") depth++;
154
+ if (text[end] === ")" && --depth === 0) break;
155
+ }
156
+ if (depth) {
157
+ skipUntil = end;
158
+ continue;
159
+ }
160
+ const destination = text.slice(start, end).match(/^<?(\S+?)>?(?:[ \t]+["'][\s\S]*["'])?$/)?.[1];
161
+ if (!destination) continue;
162
+ const name = match[1].trim();
163
+ output +=
164
+ text.slice(cursor, match.index) +
165
+ (name && name !== destination ? `${name}: ` : "") +
166
+ store(destination);
167
+ cursor = end + 1;
168
+ }
169
+ return output + text.slice(cursor);
106
170
  }
107
171
 
108
- /**
109
- * Main parser: converts standard Markdown to WhatsApp formatting.
110
- */
111
172
  function formatMarkdownForWhatsApp(content) {
112
- if (!content || typeof content !== "string") return content;
113
-
114
- // 1. Protect code blocks and inline code using null-byte tokens
115
- const codeBlocks = [];
116
- let masked = content.replace(/```([\s\S]*?)```/g, (_, code) => {
117
- const placeholder = `\x00CODEBLOCK${codeBlocks.length}\x00`;
118
- codeBlocks.push(`\`\`\`${code}\`\`\``);
119
- return placeholder;
120
- });
121
-
122
- const inlineCodes = [];
123
- masked = masked.replace(/`([^`]+)`/g, (_, code) => {
124
- const placeholder = `\x00INLINECODE${inlineCodes.length}\x00`;
125
- inlineCodes.push(`\`${code}\``);
126
- return placeholder;
127
- });
128
-
129
- // 2. Convert markdown tables into WhatsApp cards
130
- masked = convertMarkdownTables(masked);
131
-
132
- // 3. Convert Headings: # Heading -> *Heading*
133
- masked = masked.replace(/(^|\n|\s)(#{1,6})\s+([^\n]+)/g, (match, lead, hashes, title) => {
134
- return `${lead}*${title.trim()}*`;
135
- });
136
-
137
- // 4. Convert Strikethrough: ~~text~~ -> ~text~
138
- masked = masked.replace(/~~([^~\n]+)~~/g, "~$1~");
139
-
140
- // 5. Convert Bold: **text** -> *text*
141
- masked = masked.replace(/\*\*([^*\n]+)\*\*/g, "*$1*");
142
-
143
- // 6. Convert Italic: __text__ -> _text_
144
- masked = masked.replace(/__([^_\n]+)__/g, "_$1_");
145
-
146
- // 7. Restore inline code and code blocks safely
147
- inlineCodes.forEach((code, idx) => {
148
- masked = masked.split(`\x00INLINECODE${idx}\x00`).join(code);
149
- });
150
- codeBlocks.forEach((block, idx) => {
151
- masked = masked.split(`\x00CODEBLOCK${idx}\x00`).join(block);
173
+ if (typeof content !== "string" || !content) return content;
174
+ // Pick an unused namespace: user text resembling a placeholder is not lost.
175
+ let prefix = "\x00WA";
176
+ while (content.includes(prefix)) prefix += "X";
177
+ const saved = [];
178
+ const store = (text) => `${prefix}${saved.push(text) - 1}\x00`;
179
+ let text = shieldCode(content.replace(/\r\n?/g, "\n"), store);
180
+ text = links(text, store);
181
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: Stop URLs before internal shielding tokens.
182
+ text = text.replace(/(?:https?:\/\/|www\.)[^\s<>\x00]+/gi, (url, offset, source) => {
183
+ for (const marker of ["***", "**", "__", "~~", "*", "_"]) {
184
+ if (source.slice(0, offset).endsWith(marker) && url.endsWith(marker)) {
185
+ return store(url.slice(0, -marker.length)) + marker;
186
+ }
187
+ }
188
+ return store(url);
152
189
  });
153
-
154
- return masked;
190
+ text = text.replace(/\\([\\`*{}[\]()#+.!_>~|-])/g, (_, literal) => store(literal));
191
+ text = text
192
+ .replace(/\*\*\*([^*\n]+)\*\*\*/g, "*_$1_*")
193
+ .replace(/(?<![\p{L}\p{N}_])___([^_\n]+)___(?![\p{L}\p{N}_])/gu, "*_$1_*")
194
+ .replace(/\*\*([^*\n]+)\*\*/g, "*$1*")
195
+ .replace(/(?<![\p{L}\p{N}_])__([^_\n]+)__(?![\p{L}\p{N}_])/gu, "_$1_")
196
+ .replace(/~~([^~\n]+)~~/g, "~$1~");
197
+ text = convertMarkdownTables(text);
198
+ const lines = text.split("\n");
199
+ const output = [];
200
+ for (let i = 0; i < lines.length; i++) {
201
+ let line = lines[i];
202
+ const heading = line.match(/^ {0,3}#{1,6}[ \t]+(.+?)(?:[ \t]+#+)?[ \t]*$/);
203
+ if (heading) line = bold(heading[1]);
204
+ else if (
205
+ line.trim() &&
206
+ !line.includes(prefix) &&
207
+ /^ {0,3}(?:={3,}|-{3,})[ \t]*$/.test(lines[i + 1] || "")
208
+ ) {
209
+ line = bold(line);
210
+ i++;
211
+ } else if (/^ {0,3}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/.test(line)) {
212
+ line = "──────────";
213
+ } else {
214
+ line = line
215
+ .replace(
216
+ /^(\s*)[-+*][ \t]+\[([ xX])\][ \t]+/,
217
+ (_, indent, check) => `${indent}${check === " " ? "☐" : "☑"} `,
218
+ )
219
+ .replace(/^(\s*)[-+*][ \t]+/, "$1• ")
220
+ .replace(/^(\s*)(\d+)\)[ \t]+/, "$1$2. ")
221
+ .replace(/^ {0,3}(?:>[ \t]*)+/, "> ");
222
+ }
223
+ output.push(line.replace(/[ \t]+$/, ""));
224
+ }
225
+ text = output.join("\n");
226
+ // A link can contain an already-protected code label, so restore inside out.
227
+ const token = new RegExp(`${prefix}(\\d+)\x00`, "g");
228
+ const restore = (whole, id) => saved[id]?.replace(token, restore) ?? whole;
229
+ text = text.replace(token, restore);
230
+ return text;
155
231
  }
156
232
 
157
- module.exports = {
158
- formatMarkdownForWhatsApp,
159
- convertMarkdownTables,
160
- parseMarkdownTable,
161
- };
233
+ module.exports = { formatMarkdownForWhatsApp, convertMarkdownTables, parseMarkdownTable };