Skip to content

Commit f87446c

Browse files
committed
Add session duration tracking from user message to AI completion
- Track session timing from processUserMessage start to execution-result - Add sessionStartTime and sessionDuration fields to session object - Display session duration in completion message as "⏰ Session Time: X.Xs" - Provides visibility into total time from user input to AI response
1 parent 17e1941 commit f87446c

3 files changed

Lines changed: 120 additions & 110 deletions

File tree

SessionManager.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ class SessionManager {
8787
lastHealthCheck: Date.now(),
8888
isContinuation: !!previousTokenUsage,
8989
sessionTitle: sessionTitle,
90-
autoCompactInProgress: false
90+
autoCompactInProgress: false,
91+
// Session duration tracking
92+
sessionStartTime: null,
93+
sessionDuration: null
9194
};
9295

9396
// Setup event handlers for this processor
@@ -212,6 +215,12 @@ class SessionManager {
212215
processor.on('execution-result', async (data) => {
213216
console.log(`[User ${userId}] Execution complete: ${data.success}`);
214217

218+
// Calculate session duration if timing was started
219+
if (session.sessionStartTime) {
220+
session.sessionDuration = Date.now() - session.sessionStartTime;
221+
console.log(`[User ${userId}] Session duration: ${session.sessionDuration}ms`);
222+
}
223+
215224
// Update activity and token tracking
216225
this.updateSessionActivity(session);
217226
this.updateTokenUsage(session, data);
@@ -226,7 +235,13 @@ class SessionManager {
226235
const ImageHandler = require('./ImageHandler');
227236
ImageHandler.cleanupTempFile(session, userId);
228237

229-
const formatted = this.formatter.formatExecutionResult(data, session.sessionId);
238+
// Add duration to the data for formatting
239+
const dataWithDuration = {
240+
...data,
241+
sessionDuration: session.sessionDuration
242+
};
243+
244+
const formatted = this.formatter.formatExecutionResult(dataWithDuration, session.sessionId);
230245
await this.mainBot.safeSendMessage(chatId, formatted);
231246

232247
// Check for title changes after Claude completes processing
@@ -540,6 +555,17 @@ class SessionManager {
540555
return this.userSessions.get(userId);
541556
}
542557

558+
/**
559+
* Start timing for session duration
560+
*/
561+
startSessionTiming(userId) {
562+
const session = this.getUserSession(userId);
563+
if (session) {
564+
session.sessionStartTime = Date.now();
565+
console.log(`[User ${userId}] Session timing started`);
566+
}
567+
}
568+
543569
/**
544570
* Delete user session
545571
*/

0 commit comments

Comments
 (0)