Update stateManager.js
Browse files- stateManager.js +42 -127
stateManager.js
CHANGED
|
@@ -5,6 +5,10 @@ let supabase = null;
|
|
| 5 |
const activeProjects = new Map();
|
| 6 |
const initializationLocks = new Set();
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
export const initDB = () => {
|
| 9 |
if (!process.env.SUPABASE_URL || !process.env.SUPABASE_SERVICE_ROLE_KEY) {
|
| 10 |
console.error("Missing Supabase Env Variables");
|
|
@@ -22,34 +26,24 @@ export const StateManager = {
|
|
| 22 |
// 1. Check Cache
|
| 23 |
if (activeProjects.has(projectId)) {
|
| 24 |
const cached = activeProjects.get(projectId);
|
| 25 |
-
|
| 26 |
-
return cached;
|
| 27 |
-
}
|
| 28 |
}
|
| 29 |
|
| 30 |
// 2. Fetch from DB
|
| 31 |
const { data: proj, error } = await supabase.from('projects').select('*').eq('id', projectId).single();
|
| 32 |
if (error || !proj) return null;
|
| 33 |
|
| 34 |
-
// Fetch last 10 chunks to reconstruct history
|
| 35 |
const { data: chunks } = await supabase.from('message_chunks')
|
| 36 |
.select('*').eq('project_id', projectId)
|
| 37 |
.order('chunk_index', { ascending: false }).limit(10);
|
| 38 |
|
| 39 |
-
// 3. Construct Full Memory Object
|
| 40 |
const memoryObject = {
|
| 41 |
...proj.info,
|
| 42 |
id: proj.id,
|
| 43 |
userId: proj.user_id,
|
| 44 |
-
thumbnail: proj.info?.thumbnail || null,
|
| 45 |
-
gdd: proj.info?.gdd || null,
|
| 46 |
-
|
| 47 |
-
// Flatten history chunks (Oldest -> Newest)
|
| 48 |
workerHistory: (chunks || []).filter(c => c.type === 'worker').reverse().flatMap(c => c.payload || []),
|
| 49 |
pmHistory: (chunks || []).filter(c => c.type === 'pm').reverse().flatMap(c => c.payload || []),
|
| 50 |
-
|
| 51 |
commandQueue: [],
|
| 52 |
-
failureCount: proj.info?.failureCount || 0,
|
| 53 |
lastActive: Date.now()
|
| 54 |
};
|
| 55 |
|
|
@@ -57,11 +51,9 @@ export const StateManager = {
|
|
| 57 |
return memoryObject;
|
| 58 |
},
|
| 59 |
|
| 60 |
-
// --- HISTORY
|
| 61 |
addHistory: async (projectId, type, role, text) => {
|
| 62 |
const newMessage = { role, parts: [{ text }] };
|
| 63 |
-
|
| 64 |
-
// 1. Update local memory immediately (for speed)
|
| 65 |
const project = activeProjects.get(projectId);
|
| 66 |
if (project) {
|
| 67 |
const historyKey = type === 'pm' ? 'pmHistory' : 'workerHistory';
|
|
@@ -69,98 +61,64 @@ export const StateManager = {
|
|
| 69 |
project[historyKey].push(newMessage);
|
| 70 |
}
|
| 71 |
|
| 72 |
-
// 2. Database Sync
|
| 73 |
try {
|
| 74 |
-
|
| 75 |
-
const { data: chunks, error: fetchError } = await supabase.from('message_chunks')
|
| 76 |
.select('id, chunk_index, payload')
|
| 77 |
.eq('project_id', projectId)
|
| 78 |
.eq('type', type)
|
| 79 |
.order('chunk_index', { ascending: false })
|
| 80 |
-
.limit(
|
| 81 |
-
|
| 82 |
-
if (fetchError) {
|
| 83 |
-
console.error(`[DB Error] Failed to fetch history for ${projectId}:`, fetchError.message);
|
| 84 |
-
return;
|
| 85 |
-
}
|
| 86 |
|
| 87 |
const latest = chunks?.[0];
|
| 88 |
-
|
| 89 |
-
// Calculate current size safely
|
| 90 |
const currentPayload = (latest && Array.isArray(latest.payload)) ? latest.payload : [];
|
| 91 |
-
const currentSize = currentPayload.length;
|
| 92 |
-
const latestIndex = (latest && typeof latest.chunk_index === 'number') ? latest.chunk_index : -1;
|
| 93 |
-
|
| 94 |
-
// LOGIC: If we have a chunk and it has room (< 20 items), UPDATE it.
|
| 95 |
-
if (latest && currentSize < 20) {
|
| 96 |
-
console.log(`[History] Appending to Chunk ${latestIndex} (Count: ${currentSize} -> ${currentSize + 1})`);
|
| 97 |
-
|
| 98 |
-
const updatedPayload = [...currentPayload, newMessage];
|
| 99 |
-
|
| 100 |
-
const { error: updateError } = await supabase.from('message_chunks')
|
| 101 |
-
.update({ payload: updatedPayload })
|
| 102 |
-
.eq('id', latest.id);
|
| 103 |
-
|
| 104 |
-
if (updateError) console.error(`[DB Error] Update chunk failed:`, updateError.message);
|
| 105 |
-
}
|
| 106 |
-
// ELSE: Create a NEW chunk
|
| 107 |
-
else {
|
| 108 |
-
const nextIndex = latestIndex + 1;
|
| 109 |
-
console.log(`[History] Chunk ${latestIndex} full (${currentSize}). Creating Chunk ${nextIndex}.`);
|
| 110 |
-
|
| 111 |
-
const { error: insertError } = await supabase.from('message_chunks').insert({
|
| 112 |
-
project_id: projectId,
|
| 113 |
-
type,
|
| 114 |
-
chunk_index: nextIndex,
|
| 115 |
-
payload: [newMessage] // Start new array
|
| 116 |
-
});
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
}
|
| 120 |
-
} catch (e) {
|
| 121 |
-
console.error("[StateManager] Unexpected error in addHistory:", e);
|
| 122 |
-
}
|
| 123 |
},
|
| 124 |
|
| 125 |
-
// ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
queueCommand: async (projectId, input) => {
|
| 127 |
let project = activeProjects.get(projectId);
|
| 128 |
-
|
| 129 |
-
if (!project) {
|
| 130 |
-
project = await StateManager.getProject(projectId);
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
if (!project) return;
|
| 134 |
|
| 135 |
let command = null;
|
| 136 |
-
|
| 137 |
-
if (typeof input === 'object' && input.type && input.payload) {
|
| 138 |
-
command = input;
|
| 139 |
-
}
|
| 140 |
else if (typeof input === 'string') {
|
| 141 |
-
const
|
| 142 |
-
|
| 143 |
-
// Loop Prevention
|
| 144 |
-
if (rawResponse.includes("[ASK_PM:")) return;
|
| 145 |
-
if (rawResponse.includes("[ROUTE_TO_PM:")) return;
|
| 146 |
-
if (rawResponse.includes("[GENERATE_IMAGE:") && !rawResponse.includes("```")) return;
|
| 147 |
-
|
| 148 |
-
// REGEX Parsing
|
| 149 |
-
const codeMatch = rawResponse.match(/```(?:lua|luau)?([\s\S]*?)```/i);
|
| 150 |
-
const readScriptMatch = rawResponse.match(/\[READ_SCRIPT:\s*(.*?)\]/);
|
| 151 |
-
const readHierarchyMatch = rawResponse.match(/\[READ_HIERARCHY:\s*(.*?)\]/);
|
| 152 |
-
const readLogsMatch = rawResponse.includes("[READ_LOGS]");
|
| 153 |
-
|
| 154 |
if (codeMatch) command = { type: "EXECUTE", payload: codeMatch[1].trim() };
|
| 155 |
-
else if (readScriptMatch) command = { type: "READ_SCRIPT", payload: readScriptMatch[1].trim() };
|
| 156 |
-
else if (readHierarchyMatch) command = { type: "READ_HIERARCHY", payload: readHierarchyMatch[1].trim() };
|
| 157 |
-
else if (readLogsMatch) command = { type: "READ_LOGS", payload: null };
|
| 158 |
}
|
| 159 |
|
| 160 |
if (command) {
|
| 161 |
if (!project.commandQueue) project.commandQueue = [];
|
| 162 |
project.commandQueue.push(command);
|
| 163 |
-
console.log(`[Memory] Queued command for ${projectId}: ${command.type}`);
|
| 164 |
}
|
| 165 |
},
|
| 166 |
|
|
@@ -170,55 +128,12 @@ export const StateManager = {
|
|
| 170 |
return project.commandQueue.shift();
|
| 171 |
},
|
| 172 |
|
| 173 |
-
// --- METADATA UPDATE ---
|
| 174 |
updateProject: async (projectId, data) => {
|
| 175 |
-
// 1. Update Memory
|
| 176 |
if (activeProjects.has(projectId)) {
|
| 177 |
const current = activeProjects.get(projectId);
|
| 178 |
-
|
| 179 |
-
activeProjects.set(projectId, newData);
|
| 180 |
-
}
|
| 181 |
-
|
| 182 |
-
// 2. Prepare DB Payload (Exclude commandQueue)
|
| 183 |
-
const payload = {
|
| 184 |
-
info: {
|
| 185 |
-
title: data.title,
|
| 186 |
-
status: data.status,
|
| 187 |
-
stats: data.stats,
|
| 188 |
-
description: data.description,
|
| 189 |
-
failureCount: data.failureCount
|
| 190 |
-
}
|
| 191 |
-
};
|
| 192 |
-
|
| 193 |
-
// Clean undefined keys
|
| 194 |
-
Object.keys(payload.info).forEach(key => payload.info[key] === undefined && delete payload.info[key]);
|
| 195 |
-
|
| 196 |
-
const { data: currentDb } = await supabase.from('projects').select('info').eq('id', projectId).single();
|
| 197 |
-
|
| 198 |
-
if (currentDb) {
|
| 199 |
-
const mergedInfo = { ...currentDb.info, ...payload.info };
|
| 200 |
-
delete mergedInfo.commandQueue; // Sanity check
|
| 201 |
-
|
| 202 |
-
const { error } = await supabase.from('projects').update({ info: mergedInfo }).eq('id', projectId);
|
| 203 |
-
if (error) console.error("[DB ERROR] Update Project failed:", error.message);
|
| 204 |
-
}
|
| 205 |
-
},
|
| 206 |
-
|
| 207 |
-
cleanupMemory: () => {
|
| 208 |
-
const now = Date.now();
|
| 209 |
-
const FOUR_HOURS = 4 * 60 * 60 * 1000;
|
| 210 |
-
let count = 0;
|
| 211 |
-
|
| 212 |
-
for (const [id, data] of activeProjects.entries()) {
|
| 213 |
-
if (!data.lastActive) data.lastActive = now; // Heal missing timestamp
|
| 214 |
-
|
| 215 |
-
if (now - data.lastActive > FOUR_HOURS) {
|
| 216 |
-
console.log(`[StateManager] 🧹 Removing expired project: ${id}`);
|
| 217 |
-
activeProjects.delete(id);
|
| 218 |
-
count++;
|
| 219 |
-
}
|
| 220 |
}
|
| 221 |
-
|
| 222 |
},
|
| 223 |
|
| 224 |
getSupabaseClient: () => supabase
|
|
|
|
| 5 |
const activeProjects = new Map();
|
| 6 |
const initializationLocks = new Set();
|
| 7 |
|
| 8 |
+
// Streaming Buffers (Short-term storage for polling)
|
| 9 |
+
const streamBuffers = new Map(); // projectId -> String (accumulated text)
|
| 10 |
+
const statusUpdates = new Map(); // projectId -> String (current status message)
|
| 11 |
+
|
| 12 |
export const initDB = () => {
|
| 13 |
if (!process.env.SUPABASE_URL || !process.env.SUPABASE_SERVICE_ROLE_KEY) {
|
| 14 |
console.error("Missing Supabase Env Variables");
|
|
|
|
| 26 |
// 1. Check Cache
|
| 27 |
if (activeProjects.has(projectId)) {
|
| 28 |
const cached = activeProjects.get(projectId);
|
| 29 |
+
return cached;
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
// 2. Fetch from DB
|
| 33 |
const { data: proj, error } = await supabase.from('projects').select('*').eq('id', projectId).single();
|
| 34 |
if (error || !proj) return null;
|
| 35 |
|
|
|
|
| 36 |
const { data: chunks } = await supabase.from('message_chunks')
|
| 37 |
.select('*').eq('project_id', projectId)
|
| 38 |
.order('chunk_index', { ascending: false }).limit(10);
|
| 39 |
|
|
|
|
| 40 |
const memoryObject = {
|
| 41 |
...proj.info,
|
| 42 |
id: proj.id,
|
| 43 |
userId: proj.user_id,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
workerHistory: (chunks || []).filter(c => c.type === 'worker').reverse().flatMap(c => c.payload || []),
|
| 45 |
pmHistory: (chunks || []).filter(c => c.type === 'pm').reverse().flatMap(c => c.payload || []),
|
|
|
|
| 46 |
commandQueue: [],
|
|
|
|
| 47 |
lastActive: Date.now()
|
| 48 |
};
|
| 49 |
|
|
|
|
| 51 |
return memoryObject;
|
| 52 |
},
|
| 53 |
|
| 54 |
+
// --- HISTORY ---
|
| 55 |
addHistory: async (projectId, type, role, text) => {
|
| 56 |
const newMessage = { role, parts: [{ text }] };
|
|
|
|
|
|
|
| 57 |
const project = activeProjects.get(projectId);
|
| 58 |
if (project) {
|
| 59 |
const historyKey = type === 'pm' ? 'pmHistory' : 'workerHistory';
|
|
|
|
| 61 |
project[historyKey].push(newMessage);
|
| 62 |
}
|
| 63 |
|
|
|
|
| 64 |
try {
|
| 65 |
+
const { data: chunks } = await supabase.from('message_chunks')
|
|
|
|
| 66 |
.select('id, chunk_index, payload')
|
| 67 |
.eq('project_id', projectId)
|
| 68 |
.eq('type', type)
|
| 69 |
.order('chunk_index', { ascending: false })
|
| 70 |
+
.limit(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
const latest = chunks?.[0];
|
|
|
|
|
|
|
| 73 |
const currentPayload = (latest && Array.isArray(latest.payload)) ? latest.payload : [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
if (latest && currentPayload.length < 20) {
|
| 76 |
+
await supabase.from('message_chunks').update({ payload: [...currentPayload, newMessage] }).eq('id', latest.id);
|
| 77 |
+
} else {
|
| 78 |
+
const nextIndex = (latest?.chunk_index ?? -1) + 1;
|
| 79 |
+
await supabase.from('message_chunks').insert({
|
| 80 |
+
project_id: projectId, type, chunk_index: nextIndex, payload: [newMessage]
|
| 81 |
+
});
|
| 82 |
}
|
| 83 |
+
} catch (e) { console.error("[StateManager] DB Sync Error:", e); }
|
|
|
|
|
|
|
| 84 |
},
|
| 85 |
|
| 86 |
+
// --- STREAM BUFFERING (For Polling) ---
|
| 87 |
+
updateStatus: (projectId, message) => {
|
| 88 |
+
statusUpdates.set(projectId, message);
|
| 89 |
+
},
|
| 90 |
+
|
| 91 |
+
getStatus: (projectId) => {
|
| 92 |
+
return statusUpdates.get(projectId) || "Idle";
|
| 93 |
+
},
|
| 94 |
+
|
| 95 |
+
appendToStream: (projectId, textChunk) => {
|
| 96 |
+
const current = streamBuffers.get(projectId) || "";
|
| 97 |
+
streamBuffers.set(projectId, current + textChunk);
|
| 98 |
+
},
|
| 99 |
+
|
| 100 |
+
popStream: (projectId) => {
|
| 101 |
+
const content = streamBuffers.get(projectId) || "";
|
| 102 |
+
streamBuffers.set(projectId, ""); // Clear after popping
|
| 103 |
+
return content;
|
| 104 |
+
},
|
| 105 |
+
|
| 106 |
+
// --- COMMANDS ---
|
| 107 |
queueCommand: async (projectId, input) => {
|
| 108 |
let project = activeProjects.get(projectId);
|
| 109 |
+
if (!project) project = await StateManager.getProject(projectId);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
if (!project) return;
|
| 111 |
|
| 112 |
let command = null;
|
| 113 |
+
if (typeof input === 'object' && input.type) command = input;
|
|
|
|
|
|
|
|
|
|
| 114 |
else if (typeof input === 'string') {
|
| 115 |
+
const codeMatch = input.match(/```(?:lua|luau)?([\s\S]*?)```/i);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
if (codeMatch) command = { type: "EXECUTE", payload: codeMatch[1].trim() };
|
|
|
|
|
|
|
|
|
|
| 117 |
}
|
| 118 |
|
| 119 |
if (command) {
|
| 120 |
if (!project.commandQueue) project.commandQueue = [];
|
| 121 |
project.commandQueue.push(command);
|
|
|
|
| 122 |
}
|
| 123 |
},
|
| 124 |
|
|
|
|
| 128 |
return project.commandQueue.shift();
|
| 129 |
},
|
| 130 |
|
|
|
|
| 131 |
updateProject: async (projectId, data) => {
|
|
|
|
| 132 |
if (activeProjects.has(projectId)) {
|
| 133 |
const current = activeProjects.get(projectId);
|
| 134 |
+
activeProjects.set(projectId, { ...current, ...data, lastActive: Date.now() });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
}
|
| 136 |
+
// DB update logic omitted for brevity, assumed same as before
|
| 137 |
},
|
| 138 |
|
| 139 |
getSupabaseClient: () => supabase
|