kernelbot 1.0.11 โ 1.0.12
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/bin/kernel.js +1 -1
- package/package.json +1 -1
- package/src/bot.js +40 -5
- package/src/conversation.js +51 -4
- package/src/tools/git.js +2 -1
package/bin/kernel.js
CHANGED
|
@@ -133,7 +133,7 @@ async function startBotFlow(config) {
|
|
|
133
133
|
const conversationManager = new ConversationManager(config);
|
|
134
134
|
const agent = new Agent({ config, conversationManager });
|
|
135
135
|
|
|
136
|
-
startBot(config, agent);
|
|
136
|
+
startBot(config, agent, conversationManager);
|
|
137
137
|
showStartupComplete();
|
|
138
138
|
return true;
|
|
139
139
|
}
|
package/package.json
CHANGED
package/src/bot.js
CHANGED
|
@@ -21,10 +21,16 @@ function splitMessage(text, maxLength = 4096) {
|
|
|
21
21
|
return chunks;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export function startBot(config, agent) {
|
|
24
|
+
export function startBot(config, agent, conversationManager) {
|
|
25
25
|
const logger = getLogger();
|
|
26
26
|
const bot = new TelegramBot(config.telegram.bot_token, { polling: true });
|
|
27
27
|
|
|
28
|
+
// Load previous conversations from disk
|
|
29
|
+
const loaded = conversationManager.load();
|
|
30
|
+
if (loaded) {
|
|
31
|
+
logger.info('Loaded previous conversations from disk');
|
|
32
|
+
}
|
|
33
|
+
|
|
28
34
|
logger.info('Telegram bot started with polling');
|
|
29
35
|
|
|
30
36
|
bot.on('message', async (msg) => {
|
|
@@ -41,7 +47,36 @@ export function startBot(config, agent) {
|
|
|
41
47
|
return;
|
|
42
48
|
}
|
|
43
49
|
|
|
44
|
-
|
|
50
|
+
const text = msg.text.trim();
|
|
51
|
+
|
|
52
|
+
// Handle commands
|
|
53
|
+
if (text === '/clean' || text === '/clear' || text === '/reset') {
|
|
54
|
+
conversationManager.clear(chatId);
|
|
55
|
+
logger.info(`Conversation cleared for chat ${chatId} by ${username}`);
|
|
56
|
+
await bot.sendMessage(chatId, '๐งน Conversation cleared. Starting fresh.');
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (text === '/history') {
|
|
61
|
+
const count = conversationManager.getMessageCount(chatId);
|
|
62
|
+
await bot.sendMessage(chatId, `๐ This chat has *${count}* messages in memory.`, { parse_mode: 'Markdown' });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (text === '/help') {
|
|
67
|
+
await bot.sendMessage(chatId, [
|
|
68
|
+
'*KernelBot Commands*',
|
|
69
|
+
'',
|
|
70
|
+
'/clean โ Clear conversation and start fresh',
|
|
71
|
+
'/history โ Show message count in memory',
|
|
72
|
+
'/help โ Show this help message',
|
|
73
|
+
'',
|
|
74
|
+
'Or just send any message to chat with the agent.',
|
|
75
|
+
].join('\n'), { parse_mode: 'Markdown' });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
logger.info(`Message from ${username} (${userId}): ${text.slice(0, 100)}`);
|
|
45
80
|
|
|
46
81
|
// Show typing and keep refreshing it
|
|
47
82
|
const typingInterval = setInterval(() => {
|
|
@@ -50,8 +85,8 @@ export function startBot(config, agent) {
|
|
|
50
85
|
bot.sendChatAction(chatId, 'typing').catch(() => {});
|
|
51
86
|
|
|
52
87
|
try {
|
|
53
|
-
const onUpdate = async (
|
|
54
|
-
const parts = splitMessage(
|
|
88
|
+
const onUpdate = async (update) => {
|
|
89
|
+
const parts = splitMessage(update);
|
|
55
90
|
for (const part of parts) {
|
|
56
91
|
try {
|
|
57
92
|
await bot.sendMessage(chatId, part, { parse_mode: 'Markdown' });
|
|
@@ -61,7 +96,7 @@ export function startBot(config, agent) {
|
|
|
61
96
|
}
|
|
62
97
|
};
|
|
63
98
|
|
|
64
|
-
const reply = await agent.processMessage(chatId,
|
|
99
|
+
const reply = await agent.processMessage(chatId, text, {
|
|
65
100
|
id: userId,
|
|
66
101
|
username,
|
|
67
102
|
}, onUpdate);
|
package/src/conversation.js
CHANGED
|
@@ -1,14 +1,52 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { homedir } from 'os';
|
|
4
|
+
|
|
5
|
+
function getConversationsPath() {
|
|
6
|
+
const dir = join(homedir(), '.kernelbot');
|
|
7
|
+
mkdirSync(dir, { recursive: true });
|
|
8
|
+
return join(dir, 'conversations.json');
|
|
9
|
+
}
|
|
10
|
+
|
|
1
11
|
export class ConversationManager {
|
|
2
12
|
constructor(config) {
|
|
3
13
|
this.maxHistory = config.conversation.max_history;
|
|
4
14
|
this.conversations = new Map();
|
|
15
|
+
this.filePath = getConversationsPath();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
load() {
|
|
19
|
+
if (!existsSync(this.filePath)) return false;
|
|
20
|
+
try {
|
|
21
|
+
const raw = readFileSync(this.filePath, 'utf-8');
|
|
22
|
+
const data = JSON.parse(raw);
|
|
23
|
+
for (const [chatId, messages] of Object.entries(data)) {
|
|
24
|
+
this.conversations.set(String(chatId), messages);
|
|
25
|
+
}
|
|
26
|
+
return this.conversations.size > 0;
|
|
27
|
+
} catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
save() {
|
|
33
|
+
try {
|
|
34
|
+
const data = {};
|
|
35
|
+
for (const [chatId, messages] of this.conversations) {
|
|
36
|
+
data[chatId] = messages;
|
|
37
|
+
}
|
|
38
|
+
writeFileSync(this.filePath, JSON.stringify(data, null, 2));
|
|
39
|
+
} catch {
|
|
40
|
+
// Silent fail โ don't crash the bot over persistence
|
|
41
|
+
}
|
|
5
42
|
}
|
|
6
43
|
|
|
7
44
|
getHistory(chatId) {
|
|
8
|
-
|
|
9
|
-
|
|
45
|
+
const key = String(chatId);
|
|
46
|
+
if (!this.conversations.has(key)) {
|
|
47
|
+
this.conversations.set(key, []);
|
|
10
48
|
}
|
|
11
|
-
return this.conversations.get(
|
|
49
|
+
return this.conversations.get(key);
|
|
12
50
|
}
|
|
13
51
|
|
|
14
52
|
addMessage(chatId, role, content) {
|
|
@@ -24,13 +62,22 @@ export class ConversationManager {
|
|
|
24
62
|
while (history.length > 0 && history[0].role !== 'user') {
|
|
25
63
|
history.shift();
|
|
26
64
|
}
|
|
65
|
+
|
|
66
|
+
this.save();
|
|
27
67
|
}
|
|
28
68
|
|
|
29
69
|
clear(chatId) {
|
|
30
|
-
this.conversations.delete(chatId);
|
|
70
|
+
this.conversations.delete(String(chatId));
|
|
71
|
+
this.save();
|
|
31
72
|
}
|
|
32
73
|
|
|
33
74
|
clearAll() {
|
|
34
75
|
this.conversations.clear();
|
|
76
|
+
this.save();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getMessageCount(chatId) {
|
|
80
|
+
const history = this.getHistory(chatId);
|
|
81
|
+
return history.length;
|
|
35
82
|
}
|
|
36
83
|
}
|
package/src/tools/git.js
CHANGED
|
@@ -164,7 +164,8 @@ export const handlers = {
|
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
const branch = (await git.branchLocal()).current;
|
|
167
|
-
const options =
|
|
167
|
+
const options = ['-u'];
|
|
168
|
+
if (force) options.push('--force');
|
|
168
169
|
await git.push('origin', branch, options);
|
|
169
170
|
return { success: true, branch };
|
|
170
171
|
} catch (err) {
|