File size: 8,144 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import { UnifiedGraphRAG, unifiedGraphRAG } from './UnifiedGraphRAG.js';
import { UnifiedMemorySystem, unifiedMemorySystem } from './UnifiedMemorySystem.js';

export type AgentNodeType = 'router' | 'planner' | 'researcher' | 'coder' | 'reviewer' | 'end';

export interface AgentState {
    id: string;
    messages: { role: string; content: string }[];
    context: any;
    currentNode: AgentNodeType;
    history: AgentNodeType[];
    scratchpad: any; // Shared working memory for agents
    status: 'active' | 'completed' | 'failed' | 'waiting';
}

interface Checkpoint {
    id: string;
    state: AgentState;
    timestamp: Date;
    metadata?: any;
}

export class StateGraphRouter {
    private graphRag: UnifiedGraphRAG;
    private memory: UnifiedMemorySystem;
    private checkpoints: Map<string, Checkpoint> = new Map();

    constructor() {
        this.graphRag = unifiedGraphRAG;
        this.memory = unifiedMemorySystem;
    }

    /**
     * Initialize a new state for a task
     */
    public initState(taskId: string, initialInput: string): AgentState {
        return {
            id: taskId,
            messages: [{ role: 'user', content: initialInput }],
            context: {},
            currentNode: 'router',
            history: [],
            scratchpad: {},
            status: 'active'
        };
    }

    /**
     * Save checkpoint for time-travel debugging
     */
    private saveCheckpoint(state: AgentState, metadata?: any): string {
        const checkpointId = `${state.id}-${Date.now()}`;
        this.checkpoints.set(checkpointId, {
            id: checkpointId,
            state: JSON.parse(JSON.stringify(state)), // Deep clone
            timestamp: new Date(),
            metadata
        });
        
        // Keep only last 50 checkpoints per task
        const taskCheckpoints = Array.from(this.checkpoints.entries())
            .filter(([_, cp]) => cp.state.id === state.id)
            .sort((a, b) => b[1].timestamp.getTime() - a[1].timestamp.getTime())
            .slice(50);
        
        this.checkpoints.clear();
        taskCheckpoints.forEach(([id, cp]) => this.checkpoints.set(id, cp));
        
        return checkpointId;
    }

    /**
     * Time-travel: restore to previous checkpoint
     */
    public async timeTravel(checkpointId: string): Promise<AgentState | null> {
        const checkpoint = this.checkpoints.get(checkpointId);
        if (!checkpoint) {
            return null;
        }
        
        console.log(`⏪ [StateGraph] Time-traveling to checkpoint: ${checkpointId}`);
        return JSON.parse(JSON.stringify(checkpoint.state)); // Deep clone
    }

    /**
     * Get all checkpoints for a task
     */
    public getCheckpoints(taskId: string): Checkpoint[] {
        return Array.from(this.checkpoints.values())
            .filter(cp => cp.state.id === taskId)
            .sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime());
    }

    /**
     * Route the state to the next node
     */
    public async route(state: AgentState): Promise<AgentState> {
        console.log(`🔄 [StateGraph] Routing from ${state.currentNode}...`);
        
        // Save checkpoint before routing
        this.saveCheckpoint(state, { action: 'before_routing', node: state.currentNode });
        
        // Update history
        if (state.history[state.history.length - 1] !== state.currentNode) {
            state.history.push(state.currentNode);
        }

        let newState: AgentState;
        
        switch (state.currentNode) {
            case 'router':
                newState = await this.handleRouterNode(state);
                break;
            
            case 'planner':
                newState = await this.handlePlannerNode(state);
                break;

            case 'researcher':
                newState = await this.handleResearcherNode(state);
                break;

            case 'reviewer':
                newState = await this.handleReviewerNode(state);
                break;

            case 'end':
                newState = state;
                break;

            default:
                console.error(`Unknown node: ${state.currentNode}`);
                state.status = 'failed';
                newState = state;
        }
        
        // Save checkpoint after routing
        this.saveCheckpoint(newState, { action: 'after_routing', node: newState.currentNode });
        
        return newState;
    }

    /**
     * Router Logic: Decides the initial path based on query complexity
     */
    private async handleRouterNode(state: AgentState): Promise<AgentState> {
        const lastMessage = state.messages[state.messages.length - 1].content;

        // 1. Use GraphRAG to understand context
        const ragResult = await this.graphRag.query(lastMessage, { 
            userId: 'system', 
            orgId: 'default' 
        });

        state.context.ragReasoning = ragResult;

        // 2. Heuristic routing (Will be replaced by LLM classifier later)
        if (ragResult.confidence < 0.3 || lastMessage.length > 100) {
            // Low confidence or complex query -> Needs Planning
            console.log('  -> Routing to Planner (Complex/Unknown)');
            state.currentNode = 'planner';
        } else {
            // High confidence -> Direct Research or Execution (Simplified)
            console.log('  -> Routing to Researcher (Simple)');
            state.currentNode = 'researcher';
        }

        return state;
    }

    /**
     * Planner Node: Break down complex tasks
     */
    private async handlePlannerNode(state: AgentState): Promise<AgentState> {
        console.log('  -> Planner: Analyzing task...');
        
        const lastMessage = state.messages[state.messages.length - 1].content;
        
        // Use GraphRAG to understand context and dependencies
        const ragResult = await this.graphRag.query(`Plan: ${lastMessage}`, {
            userId: 'system',
            orgId: 'default'
        });
        
        // Simple planning logic (can be enhanced with LLM)
        const plan = [
            `Step 1: Analyze requirements (confidence: ${ragResult.confidence.toFixed(2)})`,
            `Step 2: Identify dependencies (${ragResult.nodes.length} related concepts found)`,
            'Step 3: Execute plan',
            'Step 4: Review results'
        ];
        
        state.scratchpad.plan = plan;
        state.scratchpad.ragContext = ragResult;
        state.currentNode = 'researcher';
        
        return state;
    }

    /**
     * Researcher Node: Gather information
     */
    private async handleResearcherNode(state: AgentState): Promise<AgentState> {
        console.log('  -> Researcher: Gathering information...');
        
        const lastMessage = state.messages[state.messages.length - 1].content;
        
        // Use UnifiedMemorySystem to search for relevant information
        const searchResults = await this.memory.getWorkingMemory({
            userId: 'system',
            orgId: 'default'
        });
        
        state.scratchpad.research = {
            query: lastMessage,
            foundContext: searchResults.recentEvents?.slice(0, 5) || [],
            foundFeatures: searchResults.recentFeatures?.slice(0, 3) || []
        };
        
        state.currentNode = 'reviewer';
        return state;
    }

    /**
     * Reviewer Node: Validate and finalize
     */
    private async handleReviewerNode(state: AgentState): Promise<AgentState> {
        console.log('  -> Reviewer: Validating results...');
        
        // Simple validation (can be enhanced)
        const hasPlan = state.scratchpad.plan && state.scratchpad.plan.length > 0;
        const hasResearch = state.scratchpad.research;
        
        if (hasPlan && hasResearch) {
            state.status = 'completed';
            state.currentNode = 'end';
        } else {
            // If missing info, go back to researcher
            state.currentNode = 'researcher';
        }
        
        return state;
    }
}

export const stateGraphRouter = new StateGraphRouter();