File size: 5,276 Bytes
adca48b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Application State Management
 */
import { Logger } from './logger.js';

export class AppState {
    constructor() {
        // Core session state
        this.currentSessionId = null;
        this.isSolving = false;
        this.currentIteration = 0;
        this.maxIterationsCount = 8;
        this.isAwaitingUserFeedback = false;
        
        // Session management state
        this.currentSessionData = null;
        this.selectedSessionId = null; // Currently loaded session (null means current/new session)
        this.sessionsExpanded = false;
        
        // Streaming and execution tracking
        this.streamingMessages = new Map(); // Track streaming messages by iteration
        this.executionSpinners = new Map(); // Track execution spinners by iteration
        
        // Timeout and connection monitoring
        this.solvingTimeoutId = null;
        this.connectionTimeoutId = null;
        this.lastHeartbeat = Date.now();
        this.connectionRetries = 0;
        
        // Constants
        this.SOLVING_TIMEOUT_MS = 300000; // 5 minutes timeout for solving
        this.CONNECTION_TIMEOUT_MS = 30000; // 30 seconds timeout for connection issues
        this.HEARTBEAT_INTERVAL_MS = 15000; // Send heartbeat every 15 seconds
        this.MAX_CONNECTION_RETRIES = 3;
        
        // Session storage
        this.SESSION_STORAGE_KEY = 'pips_sessions';

        
        // Event listeners for state changes
        this.listeners = {};
    }

    // Event system for state changes
    on(event, callback) {
        if (!this.listeners[event]) {
            this.listeners[event] = [];
        }
        this.listeners[event].push(callback);
    }

    emit(event, data) {
        if (this.listeners[event]) {
            this.listeners[event].forEach(callback => callback(data));
        }
    }

    // Session management
    setCurrentSession(sessionData) {
        this.currentSessionData = sessionData;
        this.emit('sessionChanged', sessionData?.id);
        Logger.debug('State', 'Current session updated', sessionData?.id);
    }

    setSelectedSession(sessionId) {
        this.selectedSessionId = sessionId;
        this.emit('selectedSessionChanged', sessionId);
        Logger.debug('State', 'Selected session changed', sessionId);
    }

    // Solving state
    setSolving(solving) {
        this.isSolving = solving;
        this.emit('solvingStateChanged', solving);
        Logger.debug('State', `Solving state: ${solving}`);
    }

    setIteration(iteration) {
        this.currentIteration = iteration;
        this.emit('iterationChanged', iteration);
    }

    // User feedback state (for interactive mode)
    setUserFeedback(awaiting) {
        this.isAwaitingUserFeedback = awaiting;
        this.emit('userFeedbackStateChanged', awaiting);
        Logger.debug('State', `User feedback state: ${awaiting}`);
    }

    // Connection state
    setConnectionRetries(retries) {
        this.connectionRetries = retries;
        this.emit('connectionRetriesChanged', retries);
    }

    updateLastHeartbeat() {
        this.lastHeartbeat = Date.now();
    }

    // Timeout management
    setSolvingTimeout(timeoutId) {
        this.clearSolvingTimeout();
        this.solvingTimeoutId = timeoutId;
    }

    clearSolvingTimeout() {
        if (this.solvingTimeoutId) {
            clearTimeout(this.solvingTimeoutId);
            this.solvingTimeoutId = null;
        }
    }

    setConnectionTimeout(timeoutId) {
        this.clearConnectionTimeout();
        this.connectionTimeoutId = timeoutId;
    }

    clearConnectionTimeout() {
        if (this.connectionTimeoutId) {
            clearTimeout(this.connectionTimeoutId);
            this.connectionTimeoutId = null;
        }
    }

    // Streaming management
    addStreamingMessage(id, element) {
        this.streamingMessages.set(id, element);
    }

    removeStreamingMessage(id) {
        this.streamingMessages.delete(id);
    }

    addExecutionSpinner(id, element) {
        this.executionSpinners.set(id, element);
    }

    removeExecutionSpinner(id) {
        this.executionSpinners.delete(id);
    }

    // Get current state snapshot
    getSnapshot() {
        return {
            currentSessionId: this.currentSessionId,
            isSolving: this.isSolving,
            currentIteration: this.currentIteration,
            selectedSessionId: this.selectedSessionId,
            connectionRetries: this.connectionRetries,
            lastHeartbeat: this.lastHeartbeat,
            streamingMessagesCount: this.streamingMessages.size,
            executionSpinnersCount: this.executionSpinners.size
        };
    }

    // Reset state (for new session)
    reset() {
        this.currentSessionId = null;
        this.isSolving = false;
        this.currentIteration = 0;
        this.isAwaitingUserFeedback = false;
        this.currentSessionData = null;
        this.selectedSessionId = null;
        this.clearSolvingTimeout();
        this.clearConnectionTimeout();
        this.streamingMessages.clear();
        this.executionSpinners.clear();
        this.emit('stateReset');
        Logger.debug('State', 'Application state reset');
    }
}

// Create singleton instance
export const appState = new AppState();