File size: 1,693 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * Tracks user activity state to determine when memory monitoring should be active
 */
export class ActivityDetector {
  private lastActivityTime: number = Date.now();
  private readonly idleThresholdMs: number;

  constructor(idleThresholdMs: number = 30000) {
    this.idleThresholdMs = idleThresholdMs;
  }

  /**
   * Record user activity (called by CLI when user types, adds messages, etc.)
   */
  recordActivity(): void {
    this.lastActivityTime = Date.now();
  }

  /**
   * Check if user is currently active (activity within idle threshold)
   */
  isUserActive(): boolean {
    const timeSinceActivity = Date.now() - this.lastActivityTime;
    return timeSinceActivity < this.idleThresholdMs;
  }

  /**
   * Get time since last activity in milliseconds
   */
  getTimeSinceLastActivity(): number {
    return Date.now() - this.lastActivityTime;
  }

  /**
   * Get last activity timestamp
   */
  getLastActivityTime(): number {
    return this.lastActivityTime;
  }
}

// Global activity detector instance (eagerly created with default threshold)
const globalActivityDetector: ActivityDetector = new ActivityDetector();

/**
 * Get global activity detector instance
 */
export function getActivityDetector(): ActivityDetector {
  return globalActivityDetector;
}

/**
 * Record user activity (convenience function for CLI to call)
 */
export function recordUserActivity(): void {
  globalActivityDetector.recordActivity();
}

/**
 * Check if user is currently active (convenience function)
 */
export function isUserActive(): boolean {
  return globalActivityDetector.isUserActive();
}