Spaces:
Running
Running
Update stateManager.js
Browse files- stateManager.js +44 -75
stateManager.js
CHANGED
|
@@ -1,94 +1,63 @@
|
|
| 1 |
// stateManager.js
|
| 2 |
-
import admin from 'firebase-admin';
|
| 3 |
-
|
| 4 |
-
// Initialize Firebase (Placeholder - expects serviceAccountKey.json)
|
| 5 |
-
// In production, uncomment the lines below and add your key file
|
| 6 |
-
/*
|
| 7 |
-
import serviceAccount from './serviceAccountKey.json' with { type: "json" };
|
| 8 |
-
admin.initializeApp({
|
| 9 |
-
credential: admin.credential.cert(serviceAccount),
|
| 10 |
-
databaseURL: "https://YOUR-PROJECT-ID.firebaseio.com"
|
| 11 |
-
});
|
| 12 |
-
const db = admin.database();
|
| 13 |
-
*/
|
| 14 |
-
|
| 15 |
-
// In-Memory Storage
|
| 16 |
const activeProjects = new Map();
|
| 17 |
|
| 18 |
export const StateManager = {
|
| 19 |
-
/**
|
| 20 |
-
* Retrieves a project. Checks memory first, then DB (mocked).
|
| 21 |
-
*/
|
| 22 |
getProject: async (projectId) => {
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
const data = activeProjects.get(projectId);
|
| 26 |
-
data.lastActive = Date.now();
|
| 27 |
-
return data;
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
// 2. Check DB (Mock implementation)
|
| 31 |
-
// const snapshot = await db.ref(`projects/${projectId}`).once('value');
|
| 32 |
-
// if (snapshot.exists()) {
|
| 33 |
-
// const data = snapshot.val();
|
| 34 |
-
// activeProjects.set(projectId, data);
|
| 35 |
-
// return data;
|
| 36 |
-
// }
|
| 37 |
-
|
| 38 |
-
return null;
|
| 39 |
},
|
| 40 |
|
| 41 |
-
/**
|
| 42 |
-
* Creates or Updates a project state.
|
| 43 |
-
*/
|
| 44 |
updateProject: async (projectId, data) => {
|
| 45 |
-
const
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
activeProjects.set(projectId, newData);
|
| 50 |
-
|
| 51 |
-
// Update DB (Mock implementation)
|
| 52 |
-
// await db.ref(`projects/${projectId}`).set(newData);
|
| 53 |
-
|
| 54 |
return newData;
|
| 55 |
},
|
| 56 |
|
| 57 |
-
/
|
| 58 |
-
|
| 59 |
-
*/
|
| 60 |
-
queueCommand: (projectId, commandResponse) => {
|
| 61 |
const project = activeProjects.get(projectId);
|
| 62 |
-
if (!project) return;
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
// Extract Markdown Code for the plugin
|
| 67 |
-
const codeMatch = commandResponse.match(/```lua([\s\S]*?)```/);
|
| 68 |
-
const code = codeMatch ? codeMatch[1].trim() : null;
|
| 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 |
};
|
|
|
|
| 1 |
// stateManager.js
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
const activeProjects = new Map();
|
| 3 |
|
| 4 |
export const StateManager = {
|
|
|
|
|
|
|
|
|
|
| 5 |
getProject: async (projectId) => {
|
| 6 |
+
if (activeProjects.has(projectId)) return activeProjects.get(projectId);
|
| 7 |
+
return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
},
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
updateProject: async (projectId, data) => {
|
| 11 |
+
const current = activeProjects.get(projectId) || {
|
| 12 |
+
commandQueue: [],
|
| 13 |
+
workerHistory: [],
|
| 14 |
+
pmHistory: [],
|
| 15 |
+
failureCount: 0
|
| 16 |
+
};
|
| 17 |
+
const newData = { ...current, ...data, lastActive: Date.now() };
|
| 18 |
activeProjects.set(projectId, newData);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
return newData;
|
| 20 |
},
|
| 21 |
|
| 22 |
+
// PARSE AI RESPONSE FOR COMMANDS
|
| 23 |
+
queueCommand: async (projectId, rawResponse) => {
|
|
|
|
|
|
|
| 24 |
const project = activeProjects.get(projectId);
|
|
|
|
| 25 |
|
| 26 |
+
let command = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
// 1. Check for Code Execution
|
| 29 |
+
const codeMatch = rawResponse.match(/```lua([\s\S]*?)```/);
|
| 30 |
+
if (codeMatch) {
|
| 31 |
+
command = { type: "EXECUTE", payload: codeMatch[1].trim() };
|
| 32 |
+
}
|
| 33 |
+
// 2. Check for Script Reading
|
| 34 |
+
else if (rawResponse.match(/\[READ_SCRIPT:\s*(.*?)\]/)) {
|
| 35 |
+
const match = rawResponse.match(/\[READ_SCRIPT:\s*(.*?)\]/);
|
| 36 |
+
command = { type: "READ_SCRIPT", payload: match[1].trim() };
|
| 37 |
+
}
|
| 38 |
+
// 3. Check for Hierarchy Reading
|
| 39 |
+
else if (rawResponse.match(/\[READ_HIERARCHY:\s*(.*?)\]/)) {
|
| 40 |
+
const match = rawResponse.match(/\[READ_HIERARCHY:\s*(.*?)\]/);
|
| 41 |
+
command = { type: "READ_HIERARCHY", payload: match[1].trim() };
|
| 42 |
+
}
|
| 43 |
+
// 4. Check for Logs
|
| 44 |
+
else if (rawResponse.includes("[READ_LOGS]")) {
|
| 45 |
+
command = { type: "READ_LOGS", payload: null };
|
| 46 |
+
}
|
| 47 |
|
| 48 |
+
if (command) {
|
| 49 |
+
project.commandQueue.push(command);
|
| 50 |
+
console.log(`[${projectId}] Queued Action: ${command.type}`);
|
| 51 |
+
} else {
|
| 52 |
+
console.log(`[${projectId}] AI Replied (No Action):`, rawResponse.substring(0, 50) + "...");
|
|
|
|
| 53 |
}
|
| 54 |
+
|
| 55 |
+
activeProjects.set(projectId, project);
|
| 56 |
+
},
|
| 57 |
+
|
| 58 |
+
popCommand: async (projectId) => {
|
| 59 |
+
const project = activeProjects.get(projectId);
|
| 60 |
+
if (!project || !project.commandQueue.length) return null;
|
| 61 |
+
return project.commandQueue.shift();
|
| 62 |
}
|
| 63 |
};
|