-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKeyboardHandlers.js
More file actions
352 lines (312 loc) · 10.7 KB
/
Copy pathKeyboardHandlers.js
File metadata and controls
352 lines (312 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* Keyboard Handlers - Extracted from StreamTelegramBot
* Handles keyboard UI and button press routing
*/
class KeyboardHandlers {
constructor(bot, mainBotInstance) {
this.bot = bot;
this.mainBot = mainBotInstance; // Reference to main bot for delegation
}
/**
* Create persistent reply keyboard with useful buttons
*/
createReplyKeyboard(userId = null) {
// Determine concat buttons based on mode and buffer status
let concatButtons = [{ text: '🔗 Concat On' }];
if (userId && this.mainBot.getConcatModeStatus && this.mainBot.getConcatModeStatus(userId)) {
// When concat mode is enabled, show both Send and Cancel buttons
concatButtons = [
{ text: '📤 Concat Send' },
{ text: '❌ Concat Cancel' }
];
}
return {
keyboard: [
[
{ text: '🛑 STOP' },
{ text: '📊 Status' },
{ text: '📂 Projects' }
],
[
{ text: '🔄 New Session' },
{ text: '📝 Sessions' },
{ text: '⚡ Commands' }
],
[
{ text: '📍 Path' },
{ text: '📁 Git' },
{ text: '🌐 Web App' }
],
[
{ text: '⚙️ Settings' },
...concatButtons,
{ text: '🔄 Restart Bot' }
]
],
resize_keyboard: true,
persistent: true
};
}
/**
* Handle keyboard button presses
*/
async handleKeyboardButton(msg) {
const text = msg.text;
const chatId = msg.chat.id;
const userId = msg.from.id;
const username = msg.from.username || 'Unknown';
// Helper function to log keyboard button press - only called when actually matched
const logKeyboardButton = () => {
console.log(`[KEYBOARD_BUTTON] User ${userId} (@${username}) pressed keyboard button: "${text}" in chat ${chatId}`);
};
switch (text) {
case '🛑 STOP':
logKeyboardButton();
console.log(`[COMPONENT] SessionManager.cancelUserSession - chatId: ${chatId}`);
await this.mainBot.sessionManager.cancelUserSession(chatId);
await this.mainBot.safeSendMessage(chatId, '🛑 **Emergency Stop**\n\nAll processes stopped.', {
forceNotification: true, // Critical user action
reply_markup: this.createReplyKeyboard(userId)
});
return true;
case '📊 Status':
logKeyboardButton();
console.log(`[COMPONENT] SessionManager.showSessionStatus - chatId: ${chatId}`);
await this.mainBot.sessionManager.showSessionStatus(chatId);
return true;
case '📂 Projects':
logKeyboardButton();
console.log(`[COMPONENT] ProjectNavigator.showProjectSelection - chatId: ${chatId}`);
await this.mainBot.projectNavigator.showProjectSelection(chatId);
return true;
case '🔄 New Session':
logKeyboardButton();
console.log(`[COMPONENT] SessionManager.startNewSession - chatId: ${chatId}`);
await this.mainBot.sessionManager.startNewSession(chatId);
await this.mainBot.safeSendMessage(chatId, '🔄 **New Session**\n\nOld session ended, new session started.', {
forceNotification: true, // Important session action
reply_markup: this.createReplyKeyboard(userId)
});
return true;
case '📝 Sessions':
logKeyboardButton();
console.log(`[COMPONENT] SessionManager.showSessionHistory - chatId: ${chatId}`);
await this.mainBot.sessionManager.showSessionHistory(chatId);
return true;
case '📍 Path': {
logKeyboardButton();
console.log(`[COMPONENT] SessionManager.getCurrentDirectory - userId: ${userId}`);
const currentDir = this.mainBot.sessionManager.getCurrentDirectory(msg.from.id);
await this.mainBot.safeSendMessage(chatId, `📍 **Current Path:**\n\n\`${currentDir}\``, {
reply_markup: this.createReplyKeyboard(userId)
});
return true;
}
case '⚡ Commands':
logKeyboardButton();
console.log(`[COMPONENT] CommandsHandler.showCommandsMenu - chatId: ${chatId}`);
await this.mainBot.commandsHandler.showCommandsMenu(chatId);
return true;
case '📁 Git':
logKeyboardButton();
console.log(`[COMPONENT] GitManager.showGitOverview - chatId: ${chatId}`);
await this.mainBot.gitManager.showGitOverview(chatId);
return true;
case '🌐 Web App':
logKeyboardButton();
console.log(`[COMPONENT] StreamTelegramBot.handleFilesCommand - chatId: ${chatId}`);
await this.mainBot.handleFilesCommand(chatId);
return true;
case '🔄 Restart Bot':
logKeyboardButton();
console.log(`[COMPONENT] StreamTelegramBot.restartBot - userId: ${userId}, chatId: ${chatId}`);
// Check if user is admin
if (!this.mainBot.authorizedUsers.has(userId)) {
await this.mainBot.safeSendMessage(chatId,
'❌ **Access Denied**\n\n' +
'Only administrators can restart the bot.\n' +
'👤 This action requires admin privileges.',
{
forceNotification: true,
reply_markup: this.createReplyKeyboard(userId)
}
);
} else {
await this.mainBot.restartBot(chatId, userId);
}
return true;
case '🔗 Concat On':
logKeyboardButton();
console.log(`[COMPONENT] StreamTelegramBot.enableConcatMode - userId: ${userId}, chatId: ${chatId}`);
await this.mainBot.enableConcatMode(userId, chatId);
return true;
case '❌ Concat Cancel':
logKeyboardButton();
console.log(`[COMPONENT] StreamTelegramBot.disableConcatMode - userId: ${userId}, chatId: ${chatId}`);
await this.mainBot.disableConcatMode(userId, chatId, true); // true = clear buffer
return true;
case '⚙️ Settings':
logKeyboardButton();
console.log(`[COMPONENT] SettingsMenuHandler.showSettingsMenu - chatId: ${chatId}`);
await this.mainBot.settingsHandler.showSettingsMenu(chatId);
return true;
default:
// Check if it's a "Concat Send" button with count
if (text.startsWith('📤 Concat Send')) {
logKeyboardButton();
console.log(`[COMPONENT] StreamTelegramBot.sendConcatenatedMessage - userId: ${userId}, chatId: ${chatId}`);
await this.mainBot.sendConcatenatedMessage(userId, chatId);
return true;
}
return false; // Not a keyboard button
}
}
/**
* Create inline keyboard for session history navigation
*/
createSessionHistoryKeyboard(page = 0, totalPages = 1, sessions = []) {
const keyboard = {
inline_keyboard: []
};
// Add resume buttons for sessions (up to 5 per page)
if (sessions.length > 0) {
const resumeRow = sessions.slice(0, 5).map(sessionId => ({
text: `📄 ${sessionId.slice(-8)}`,
callback_data: `resume_session:${sessionId}`
}));
keyboard.inline_keyboard.push(resumeRow);
}
// Add pagination if needed
if (totalPages > 1) {
const paginationRow = [];
if (page > 0) {
paginationRow.push({
text: '◀️ Previous',
callback_data: `sessions_page:${page - 1}`
});
}
paginationRow.push({
text: `📄 ${page + 1}/${totalPages}`,
callback_data: 'noop'
});
if (page < totalPages - 1) {
paginationRow.push({
text: 'Next ▶️',
callback_data: `sessions_page:${page + 1}`
});
}
keyboard.inline_keyboard.push(paginationRow);
}
return keyboard;
}
/**
* Create inline keyboard for model selection
*/
createModelSelectionKeyboard() {
return {
inline_keyboard: [
[
{ text: '🚀 Sonnet (Fast)', callback_data: 'model:claude-3-5-sonnet-20241022' },
{ text: '🎯 Haiku (Quick)', callback_data: 'model:claude-3-5-haiku-20241022' }
],
[
{ text: '🧠 Opus (Smart)', callback_data: 'model:claude-3-opus-20240229' }
],
[
{ text: '❌ Cancel', callback_data: 'model:cancel' }
]
]
};
}
/**
* Create inline keyboard for thinking mode selection
*/
createThinkingModeKeyboard() {
// Create keyboard with thinking modes (2 buttons per row)
const keyboard = {
inline_keyboard: []
};
const modes = [
{ text: '💭 Standard', data: 'think:standard' },
{ text: '🤔 Deep Think', data: 'think:deep' },
{ text: '🧠 Ultra Think', data: 'think:ultra' },
{ text: '⚡ Quick', data: 'think:quick' },
{ text: '🎯 Focused', data: 'think:focused' },
{ text: '🔍 Analysis', data: 'think:analysis' }
];
// Group modes into rows of 2
for (let i = 0; i < modes.length; i += 2) {
const row = modes.slice(i, i + 2);
keyboard.inline_keyboard.push(row.map(mode => ({
text: mode.text,
callback_data: mode.data
})));
}
keyboard.inline_keyboard.push([
{ text: '❌ Cancel', callback_data: 'think:cancel' }
]);
return keyboard;
}
/**
* Create inline keyboard for git diff navigation
*/
createGitDiffKeyboard(options = {}) {
const {
showOverview = true,
showFileList = true,
hasNextChunk = false,
hasPrevChunk = false,
currentChunk = 0,
totalChunks = 1
} = options;
const keyboard = {
inline_keyboard: []
};
// File navigation row
const fileRow = [];
if (showOverview) {
fileRow.push({ text: '📋 Overview', callback_data: 'diff:overview' });
}
if (showFileList) {
fileRow.push({ text: '📁 Files', callback_data: 'diff:files' });
}
if (fileRow.length > 0) {
keyboard.inline_keyboard.push(fileRow);
}
// Chunk navigation for large diffs
if (totalChunks > 1) {
const chunkRow = [];
if (hasPrevChunk) {
chunkRow.push({ text: '◀️ Prev', callback_data: `diff:chunk:${currentChunk - 1}` });
}
chunkRow.push({ text: `${currentChunk + 1}/${totalChunks}`, callback_data: 'noop' });
if (hasNextChunk) {
chunkRow.push({ text: 'Next ▶️', callback_data: `diff:chunk:${currentChunk + 1}` });
}
keyboard.inline_keyboard.push(chunkRow);
}
// Options row
const optionsRow = [
{ text: '🔄 Refresh', callback_data: 'diff:refresh' },
{ text: '❌ Close', callback_data: 'diff:close' }
];
keyboard.inline_keyboard.push(optionsRow);
return keyboard;
}
/**
* Get reply keyboard markup for sending with messages
*/
getReplyKeyboardMarkup(userId = null) {
return this.createReplyKeyboard(userId);
}
/**
* Get stats for debugging
*/
getStats() {
return {
keyboardType: 'persistent_reply',
buttonsCount: 9 // 3x3 grid
};
}
}
module.exports = KeyboardHandlers;