@ducci/jarvis 1.0.54 → 1.0.56
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/channels/telegram/index.js +20 -0
- package/src/server/config.js +2 -1
- package/src/server/tools.js +14 -3
package/package.json
CHANGED
|
@@ -7,6 +7,18 @@ import { loadSession } from '../../server/sessions.js';
|
|
|
7
7
|
import { PATHS } from '../../server/config.js';
|
|
8
8
|
import { load, save } from './sessions.js';
|
|
9
9
|
|
|
10
|
+
function getTelegramChatLogPath(chatId, sessionId) {
|
|
11
|
+
const prefix = sessionId ? String(sessionId).slice(0, 8) : 'unknown';
|
|
12
|
+
return path.join(PATHS.telegramChatsDir, `${chatId}-${prefix}.log`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function appendTelegramChatLog(chatId, sessionId, direction, text) {
|
|
16
|
+
const logFile = getTelegramChatLogPath(chatId, sessionId);
|
|
17
|
+
const ts = new Date().toISOString();
|
|
18
|
+
const line = `${ts} [${direction}] ${String(text).replace(/\n/g, ' ')}\n`;
|
|
19
|
+
await fs.promises.appendFile(logFile, line, 'utf8').catch(() => {});
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
async function sendMessage(api, chatId, text, sessionId) {
|
|
11
23
|
const MAX_TG = 4096;
|
|
12
24
|
// Telegram HTML mode does not support <br> — replace with newlines before sending
|
|
@@ -86,6 +98,7 @@ export async function startTelegramChannel(config) {
|
|
|
86
98
|
|
|
87
99
|
const chatId = ctx.chat.id;
|
|
88
100
|
if (sessions[chatId]) {
|
|
101
|
+
await appendTelegramChatLog(chatId, sessions[chatId], 'SYSTEM', '--- /new: session reset ---');
|
|
89
102
|
delete sessions[chatId];
|
|
90
103
|
save(sessions);
|
|
91
104
|
console.log(`[telegram] session unlinked chat_id=${chatId}`);
|
|
@@ -136,12 +149,16 @@ export async function startTelegramChannel(config) {
|
|
|
136
149
|
console.log(`[telegram] session created sessionId=${result.sessionId.slice(0, 8)}`);
|
|
137
150
|
}
|
|
138
151
|
|
|
152
|
+
const captionText = ctx.message.caption || '[photo]';
|
|
153
|
+
await appendTelegramChatLog(chatId, result.sessionId, 'USER', `[photo] ${captionText}`);
|
|
154
|
+
|
|
139
155
|
try {
|
|
140
156
|
const rawResponse = typeof result.response === 'string'
|
|
141
157
|
? result.response
|
|
142
158
|
: result.response != null ? JSON.stringify(result.response, null, 2) : '';
|
|
143
159
|
const text = rawResponse.trim()
|
|
144
160
|
|| 'The agent encountered an error and could not produce a response. Please try again.';
|
|
161
|
+
await appendTelegramChatLog(chatId, result.sessionId, 'JARVIS', text);
|
|
145
162
|
await sendMessage(ctx.api, chatId, text, result.sessionId);
|
|
146
163
|
console.log(`[telegram] response sent chat_id=${chatId} length=${text.length}`);
|
|
147
164
|
} catch (e) {
|
|
@@ -188,6 +205,8 @@ export async function startTelegramChannel(config) {
|
|
|
188
205
|
console.log(`[telegram] session created sessionId=${result.sessionId.slice(0, 8)}`);
|
|
189
206
|
}
|
|
190
207
|
|
|
208
|
+
await appendTelegramChatLog(chatId, result.sessionId, 'USER', ctx.message.text);
|
|
209
|
+
|
|
191
210
|
try {
|
|
192
211
|
// Guard against empty or non-string response (e.g. model returns array instead of string)
|
|
193
212
|
const rawResponse = typeof result.response === 'string'
|
|
@@ -195,6 +214,7 @@ export async function startTelegramChannel(config) {
|
|
|
195
214
|
: result.response != null ? JSON.stringify(result.response, null, 2) : '';
|
|
196
215
|
const text = rawResponse.trim()
|
|
197
216
|
|| 'The agent encountered an error and could not produce a response. Please try again.';
|
|
217
|
+
await appendTelegramChatLog(chatId, result.sessionId, 'JARVIS', text);
|
|
198
218
|
await sendMessage(ctx.api, chatId, text, result.sessionId);
|
|
199
219
|
console.log(`[telegram] response sent chat_id=${chatId} length=${text.length}`);
|
|
200
220
|
} catch (e) {
|
package/src/server/config.js
CHANGED
|
@@ -18,6 +18,7 @@ export const PATHS = {
|
|
|
18
18
|
toolsDir: path.join(JARVIS_DIR, 'data', 'tools'),
|
|
19
19
|
toolsFile: path.join(JARVIS_DIR, 'data', 'tools', 'tools.json'),
|
|
20
20
|
logsDir: path.join(JARVIS_DIR, 'logs'),
|
|
21
|
+
telegramChatsDir: path.join(JARVIS_DIR, 'telegram-chats'),
|
|
21
22
|
userInfoFile: path.join(JARVIS_DIR, 'data', 'user-info.json'),
|
|
22
23
|
identityFile: path.join(JARVIS_DIR, 'data', 'identity.md'),
|
|
23
24
|
skillsDir: path.join(JARVIS_DIR, 'data', 'skills'),
|
|
@@ -26,7 +27,7 @@ export const PATHS = {
|
|
|
26
27
|
};
|
|
27
28
|
|
|
28
29
|
export function ensureDirectories() {
|
|
29
|
-
for (const dir of [PATHS.configDir, PATHS.conversationsDir, PATHS.toolsDir, PATHS.logsDir]) {
|
|
30
|
+
for (const dir of [PATHS.configDir, PATHS.conversationsDir, PATHS.toolsDir, PATHS.logsDir, PATHS.telegramChatsDir]) {
|
|
30
31
|
fs.mkdirSync(dir, { recursive: true });
|
|
31
32
|
}
|
|
32
33
|
}
|
package/src/server/tools.js
CHANGED
|
@@ -38,7 +38,7 @@ const SEED_TOOLS = {
|
|
|
38
38
|
},
|
|
39
39
|
},
|
|
40
40
|
code: `
|
|
41
|
-
const targetPath = path.resolve(
|
|
41
|
+
const _p = args.path; const targetPath = path.resolve(_p === '~' || _p.startsWith('~/') ? require('os').homedir() + _p.slice(1) : _p);
|
|
42
42
|
const raw = await fs.promises.readFile(targetPath, 'utf8');
|
|
43
43
|
const lines = raw.split('\\n');
|
|
44
44
|
const offset = args.offset ? args.offset - 1 : 0;
|
|
@@ -308,7 +308,7 @@ const SEED_TOOLS = {
|
|
|
308
308
|
},
|
|
309
309
|
},
|
|
310
310
|
code: `
|
|
311
|
-
const targetPath = path.resolve(
|
|
311
|
+
const _p = args.path; const targetPath = path.resolve(_p === '~' || _p.startsWith('~/') ? require('os').homedir() + _p.slice(1) : _p);
|
|
312
312
|
await fs.promises.mkdir(path.dirname(targetPath), { recursive: true });
|
|
313
313
|
await fs.promises.writeFile(targetPath, args.content, 'utf8');
|
|
314
314
|
if (args.mode) {
|
|
@@ -345,7 +345,7 @@ const SEED_TOOLS = {
|
|
|
345
345
|
},
|
|
346
346
|
},
|
|
347
347
|
code: `
|
|
348
|
-
const targetPath = path.resolve(
|
|
348
|
+
const _p = args.path; const targetPath = path.resolve(_p === '~' || _p.startsWith('~/') ? require('os').homedir() + _p.slice(1) : _p);
|
|
349
349
|
const content = await fs.promises.readFile(targetPath, 'utf8');
|
|
350
350
|
const count = content.split(args.old_string).length - 1;
|
|
351
351
|
if (count === 0) {
|
|
@@ -539,6 +539,17 @@ const SEED_TOOLS = {
|
|
|
539
539
|
throw e;
|
|
540
540
|
}
|
|
541
541
|
}
|
|
542
|
+
try {
|
|
543
|
+
const tgSessionsFile = path.join(process.env.HOME, '.jarvis/data/channels/telegram/sessions.json');
|
|
544
|
+
const tgSessions = JSON.parse(await fs.promises.readFile(tgSessionsFile, 'utf8').catch(() => '{}'));
|
|
545
|
+
const sessionId = tgSessions[String(chatId)];
|
|
546
|
+
const prefix = sessionId ? String(sessionId).slice(0, 8) : 'unknown';
|
|
547
|
+
const logDir = path.join(process.env.HOME, '.jarvis/telegram-chats');
|
|
548
|
+
const logFile = path.join(logDir, String(chatId) + '-' + prefix + '.log');
|
|
549
|
+
const ts = new Date().toISOString();
|
|
550
|
+
await fs.promises.mkdir(logDir, { recursive: true });
|
|
551
|
+
await fs.promises.appendFile(logFile, ts + ' [CRON] ' + String(args.message).replace(/\n/g, ' ') + '\n', 'utf8');
|
|
552
|
+
} catch {}
|
|
542
553
|
return { status: 'ok', chatId };
|
|
543
554
|
`,
|
|
544
555
|
},
|