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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
// ==========================================
|
| 9 |
// 1. CONFIGURATION & SETUP
|
|
@@ -468,4 +927,5 @@ app.listen(PORT, () => {
|
|
| 468 |
console.log(`CRON_REGISTRY_URL ${CRON_REGISTRY_URL}`);
|
| 469 |
console.log(`CRON_SECRET ${CRON_SECRET}`);
|
| 470 |
|
| 471 |
-
});
|
|
|
|
|
|
| 4 |
import path from 'path';
|
| 5 |
import { createClient } from '@supabase/supabase-js';
|
| 6 |
|
| 7 |
+
// ==========================================
|
| 8 |
+
// 1. CONFIGURATION & SETUP
|
| 9 |
+
// ==========================================
|
| 10 |
+
const PORT = 7860;
|
| 11 |
+
const SUPABASE_URL = process.env.SUPABASE_URL;
|
| 12 |
+
const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY;
|
| 13 |
+
const REMOTE_SERVER_URL = process.env.REMOTE_AI_URL || "http://localhost:11434";
|
| 14 |
+
const FRONT_URL = process.env.FRONT_URL;
|
| 15 |
+
const CRON_REGISTRY_URL = process.env.CRON_REGISTRY_URL || "http://localhost:7861";
|
| 16 |
+
const CRON_SECRET = process.env.CRON_SECRET || "default_secret";
|
| 17 |
+
|
| 18 |
+
// --- MODEL CONFIGURATION ---
|
| 19 |
+
const SMART_MODEL_ID = "claude";
|
| 20 |
+
const FAST_MODEL_ID = "gpt-5-mini";
|
| 21 |
+
|
| 22 |
+
if (!SUPABASE_URL || !SUPABASE_KEY) {
|
| 23 |
+
console.error("FATAL: Missing Supabase URL or Service Key.");
|
| 24 |
+
process.exit(1);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
const app = express();
|
| 28 |
+
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
|
| 29 |
+
|
| 30 |
+
app.use(express.json({ limit: '50mb' }));
|
| 31 |
+
app.use(cors());
|
| 32 |
+
|
| 33 |
+
// ==========================================
|
| 34 |
+
// 2. PROMPT MANAGEMENT
|
| 35 |
+
// ==========================================
|
| 36 |
+
let prompts = {};
|
| 37 |
+
try {
|
| 38 |
+
const promptsPath = path.resolve('./prompts.json');
|
| 39 |
+
if (fs.existsSync(promptsPath)) {
|
| 40 |
+
prompts = JSON.parse(fs.readFileSync(promptsPath, 'utf8'));
|
| 41 |
+
console.log("✅ Prompts loaded.");
|
| 42 |
+
} else {
|
| 43 |
+
throw new Error("prompts.json not found");
|
| 44 |
+
}
|
| 45 |
+
} catch (e) {
|
| 46 |
+
console.error("❌ Prompt Load Error:", e.message);
|
| 47 |
+
process.exit(1);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// ==========================================
|
| 51 |
+
// 3. STATE MANAGER
|
| 52 |
+
// ==========================================
|
| 53 |
+
const activeProjects = new Map();
|
| 54 |
+
|
| 55 |
+
const StateManager = {
|
| 56 |
+
getHistory: async (projectId) => {
|
| 57 |
+
if (activeProjects.has(projectId)) {
|
| 58 |
+
const cached = activeProjects.get(projectId);
|
| 59 |
+
cached.lastActive = Date.now();
|
| 60 |
+
return cached.history;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
const { data: chunks, error } = await supabase
|
| 64 |
+
.from('message_chunks')
|
| 65 |
+
.select('*')
|
| 66 |
+
.eq('project_id', projectId)
|
| 67 |
+
.order('chunk_index', { ascending: false })
|
| 68 |
+
.limit(10);
|
| 69 |
+
|
| 70 |
+
if (error) return [];
|
| 71 |
+
const fullHistory = (chunks || []).reverse().flatMap(c => c.payload || []);
|
| 72 |
+
|
| 73 |
+
activeProjects.set(projectId, { history: fullHistory, lastActive: Date.now(), isFrozen: false });
|
| 74 |
+
return fullHistory;
|
| 75 |
+
},
|
| 76 |
+
|
| 77 |
+
addHistory: async (projectId, role, text) => {
|
| 78 |
+
const newMessage = { role, parts: [{ text }] };
|
| 79 |
+
|
| 80 |
+
if (activeProjects.has(projectId)) {
|
| 81 |
+
const project = activeProjects.get(projectId);
|
| 82 |
+
project.history.push(newMessage);
|
| 83 |
+
project.lastActive = Date.now();
|
| 84 |
+
if (role === 'user') project.isFrozen = false;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
try {
|
| 88 |
+
const { data: latestChunk } = await supabase
|
| 89 |
+
.from('message_chunks')
|
| 90 |
+
.select('id, chunk_index, payload')
|
| 91 |
+
.eq('project_id', projectId)
|
| 92 |
+
.order('chunk_index', { ascending: false })
|
| 93 |
+
.limit(1)
|
| 94 |
+
.single();
|
| 95 |
+
|
| 96 |
+
const currentPayload = (latestChunk?.payload) || [];
|
| 97 |
+
|
| 98 |
+
if (latestChunk && currentPayload.length < 20) {
|
| 99 |
+
await supabase.from('message_chunks').update({
|
| 100 |
+
payload: [...currentPayload, newMessage]
|
| 101 |
+
}).eq('id', latestChunk.id);
|
| 102 |
+
} else {
|
| 103 |
+
await supabase.from('message_chunks').insert({
|
| 104 |
+
project_id: projectId,
|
| 105 |
+
lead_id: projectId,
|
| 106 |
+
chunk_index: (latestChunk?.chunk_index ?? -1) + 1,
|
| 107 |
+
payload: [newMessage]
|
| 108 |
+
});
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
if (role === 'user') await supabase.from('leads').update({ is_frozen: false }).eq('id', projectId);
|
| 112 |
+
} catch (e) { console.error("History Sync Error", e); }
|
| 113 |
+
},
|
| 114 |
+
|
| 115 |
+
isFrozen: async (projectId) => {
|
| 116 |
+
if (activeProjects.has(projectId)) return activeProjects.get(projectId).isFrozen;
|
| 117 |
+
const { data } = await supabase.from('leads').select('is_frozen').eq('id', projectId).single();
|
| 118 |
+
return data?.is_frozen || false;
|
| 119 |
+
},
|
| 120 |
+
|
| 121 |
+
setFrozen: async (projectId, status) => {
|
| 122 |
+
if (activeProjects.has(projectId)) activeProjects.get(projectId).isFrozen = status;
|
| 123 |
+
await supabase.from('leads').update({ is_frozen: status }).eq('id', projectId);
|
| 124 |
+
}
|
| 125 |
+
};
|
| 126 |
+
|
| 127 |
+
// ==========================================
|
| 128 |
+
// 4. AI ENGINE
|
| 129 |
+
// ==========================================
|
| 130 |
+
const callAI = async (history, input, contextData, systemPrompt, projectContext, modelId) => {
|
| 131 |
+
let contextStr = "";
|
| 132 |
+
try { contextStr = JSON.stringify(contextData, null, 2); } catch {}
|
| 133 |
+
|
| 134 |
+
const recentHistory = history.slice(-10).map(m =>
|
| 135 |
+
`${m.role === 'model' ? 'Assistant' : 'User'}: ${m.parts?.[0]?.text || ""}`
|
| 136 |
+
).join('\n');
|
| 137 |
+
|
| 138 |
+
const fullPrompt = `System: ${systemPrompt}\n\n${projectContext}\n\n[HISTORY]:\n${recentHistory}\n\n[CONTEXT]: ${contextStr}\n\nUser: ${input}\nAssistant:`;
|
| 139 |
+
|
| 140 |
+
try {
|
| 141 |
+
const response = await fetch(`${REMOTE_SERVER_URL}/api/generate`, {
|
| 142 |
+
method: 'POST',
|
| 143 |
+
headers: { 'Content-Type': 'application/json' },
|
| 144 |
+
body: JSON.stringify({ model: modelId, prompt: fullPrompt, system_prompt: systemPrompt })
|
| 145 |
+
});
|
| 146 |
+
|
| 147 |
+
if (!response.ok) throw new Error("AI API Error");
|
| 148 |
+
const result = await response.json();
|
| 149 |
+
return { text: result.data || result.text || "", usage: result.usage };
|
| 150 |
+
} catch (e) {
|
| 151 |
+
return { text: "<notification>AI Unreachable</notification>", usage: {} };
|
| 152 |
+
}
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
// ==========================================
|
| 156 |
+
// 5. COMMAND & CRON LOGIC
|
| 157 |
+
// ==========================================
|
| 158 |
+
function extractCommands(text) {
|
| 159 |
+
const commands = [];
|
| 160 |
+
const parse = (regex, type, isJson = true) => {
|
| 161 |
+
let match;
|
| 162 |
+
regex.lastIndex = 0;
|
| 163 |
+
// Added 'gi' flags for case-insensitive and global multiline matching
|
| 164 |
+
while ((match = regex.exec(text)) !== null) {
|
| 165 |
+
let rawPayload = match[1].trim();
|
| 166 |
+
try {
|
| 167 |
+
if (isJson) {
|
| 168 |
+
commands.push({ type, payload: JSON.parse(rawPayload) });
|
| 169 |
+
} else {
|
| 170 |
+
commands.push({ type, payload: rawPayload });
|
| 171 |
+
}
|
| 172 |
+
} catch (e) {
|
| 173 |
+
console.error(`⚠️ Parse Error (${type}):`, e.message);
|
| 174 |
+
|
| 175 |
+
// BULLETPROOF FALLBACK: If AI spits raw markdown instead of JSON, rescue it.
|
| 176 |
+
if (type === 'create_thrust') {
|
| 177 |
+
console.log(`[FALLBACK] Rescuing invalid JSON as raw markdown...`);
|
| 178 |
+
commands.push({
|
| 179 |
+
type,
|
| 180 |
+
payload: {
|
| 181 |
+
title: "System Thrust",
|
| 182 |
+
markdown_content: rawPayload,
|
| 183 |
+
tasks: []
|
| 184 |
+
}
|
| 185 |
+
});
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
};
|
| 190 |
+
|
| 191 |
+
parse(/<thrust_create>([\s\S]*?)<\/thrust_create>/gi, 'create_thrust');
|
| 192 |
+
parse(/<timeline_log>([\s\S]*?)<\/timeline_log>/gi, 'log_timeline');
|
| 193 |
+
parse(/<notification>([\s\S]*?)<\/notification>/gi, 'notification', false);
|
| 194 |
+
parse(/<update_requirements>([\s\S]*?)<\/update_requirements>/gi, 'update_requirements', false);
|
| 195 |
+
parse(/<schedule_briefing>([\s\S]*?)<\/schedule_briefing>/gi, 'schedule_briefing');
|
| 196 |
+
parse(/<freeze_project>([\s\S]*?)<\/freeze_project>/gi, 'freeze_project', false);
|
| 197 |
+
parse(/<thrust_complete>([\s\S]*?)<\/thrust_complete>/gi, 'thrust_complete', false);
|
| 198 |
+
|
| 199 |
+
return commands;
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
async function registerMorningCron(projectId, offset) {
|
| 203 |
+
const now = new Date();
|
| 204 |
+
const target = new Date(now);
|
| 205 |
+
const targetUtcHour = 6 - offset;
|
| 206 |
+
|
| 207 |
+
target.setUTCHours(targetUtcHour, 0, 0, 0);
|
| 208 |
+
if (target <= now) target.setDate(target.getDate() + 1);
|
| 209 |
+
|
| 210 |
+
const delayMs = target.getTime() - now.getTime();
|
| 211 |
+
const intervalMs = 24 * 60 * 60 * 1000;
|
| 212 |
+
|
| 213 |
+
console.log(`⏰ Scheduling Briefing for ${projectId}. Next run in ${(delayMs/1000/60).toFixed(1)} mins.`);
|
| 214 |
+
|
| 215 |
+
await fetch(`${CRON_REGISTRY_URL}/register`, {
|
| 216 |
+
method: 'POST',
|
| 217 |
+
headers: { 'Content-Type': 'application/json' },
|
| 218 |
+
body: JSON.stringify({
|
| 219 |
+
secret: CRON_SECRET,
|
| 220 |
+
jobId: `briefing_${projectId}`,
|
| 221 |
+
intervalMs: intervalMs,
|
| 222 |
+
initialDelay: delayMs,
|
| 223 |
+
webhookUrl: `https://everydaytok-thrust-core-server.hf.space/automated-briefing`,
|
| 224 |
+
leadId: projectId,
|
| 225 |
+
payload: { projectId, timezoneOffset: offset }
|
| 226 |
+
})
|
| 227 |
+
}).catch(e => console.error("Cron Reg Error", e));
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
async function executeCommands(userId, projectId, commands) {
|
| 231 |
+
let flags = { shouldReload: false, thrustComplete: false };
|
| 232 |
+
|
| 233 |
+
for (const cmd of commands) {
|
| 234 |
+
console.log(`[CMD] ${cmd.type} -> Project: ${projectId}`);
|
| 235 |
+
|
| 236 |
+
try {
|
| 237 |
+
if (cmd.type === 'create_thrust') {
|
| 238 |
+
const { data: thrust } = await supabase.from('thrusts').insert({
|
| 239 |
+
lead_id: projectId,
|
| 240 |
+
title: cmd.payload.title,
|
| 241 |
+
markdown_content: cmd.payload.markdown_content,
|
| 242 |
+
status: 'active'
|
| 243 |
+
}).select().single();
|
| 244 |
+
|
| 245 |
+
if (thrust && cmd.payload.tasks && cmd.payload.tasks.length > 0) {
|
| 246 |
+
const tasks = cmd.payload.tasks.map(t => ({ thrust_id: thrust.id, title: t }));
|
| 247 |
+
await supabase.from('thrust_tasks').insert(tasks);
|
| 248 |
+
}
|
| 249 |
+
flags.shouldReload = true;
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
if (cmd.type === 'log_timeline') {
|
| 253 |
+
await supabase.from('timeline_events').insert({
|
| 254 |
+
lead_id: projectId,
|
| 255 |
+
title: cmd.payload.title,
|
| 256 |
+
description: cmd.payload.description,
|
| 257 |
+
type: (cmd.payload.type || 'system').toLowerCase(),
|
| 258 |
+
});
|
| 259 |
+
flags.shouldReload = true;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
if (cmd.type === 'update_requirements') {
|
| 263 |
+
await supabase.from('leads').update({ requirements_doc: cmd.payload }).eq('id', projectId);
|
| 264 |
+
flags.shouldReload = true;
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
if (cmd.type === 'schedule_briefing') {
|
| 268 |
+
const offset = cmd.payload.timezone_offset || 0;
|
| 269 |
+
await registerMorningCron(projectId, offset);
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
if (cmd.type === 'freeze_project') {
|
| 273 |
+
const isFrozen = cmd.payload === 'true';
|
| 274 |
+
await StateManager.setFrozen(projectId, isFrozen);
|
| 275 |
+
console.log(`❄️ Project ${projectId} Frozen: ${isFrozen}`);
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
if (cmd.type === 'thrust_complete') {
|
| 279 |
+
flags.thrustComplete = true;
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
if (cmd.type === 'notification' && FRONT_URL) {
|
| 283 |
+
fetch(`${FRONT_URL}/internal/notify`, {
|
| 284 |
+
method: 'POST',
|
| 285 |
+
headers: { 'Content-Type': 'application/json' },
|
| 286 |
+
body: JSON.stringify({ user_id: userId, type: 'toast', message: cmd.payload })
|
| 287 |
+
}).catch(() => {});
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
} catch (e) { console.error("Exec Error", e); }
|
| 291 |
+
}
|
| 292 |
+
return flags;
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
// ==========================================
|
| 296 |
+
// 6. ENDPOINTS
|
| 297 |
+
// ==========================================
|
| 298 |
+
|
| 299 |
+
// --- INIT ---
|
| 300 |
+
app.post('/init-project', async (req, res) => {
|
| 301 |
+
const { userId, name, description, localPath } = req.body;
|
| 302 |
+
|
| 303 |
+
const { data: lead, error } = await supabase.from('leads').insert({
|
| 304 |
+
user_id: userId, name, description, local_path: localPath, status: 'active', requirements_doc: "Init..."
|
| 305 |
+
}).select().single();
|
| 306 |
+
|
| 307 |
+
if (error || !lead) return res.status(500).json({ error: "Failed to create project" });
|
| 308 |
+
|
| 309 |
+
res.json({ success: true, leadId: lead.id });
|
| 310 |
+
|
| 311 |
+
// Run AI processing in background
|
| 312 |
+
setImmediate(async () => {
|
| 313 |
+
try {
|
| 314 |
+
const initInput = `PROJECT: ${name}\nDESC: ${description}\nTask: Init PRD, First Thrust, Schedule Morning Briefing.`;
|
| 315 |
+
const aiResult = await callAI([], initInput, {}, prompts.init_system_prompt, "", SMART_MODEL_ID);
|
| 316 |
+
|
| 317 |
+
// Explicitly force a success notification command so the UI reacts
|
| 318 |
+
aiResult.text += `\n<notification>Project '${name}' initialized successfully!</notification>`;
|
| 319 |
+
|
| 320 |
+
await StateManager.addHistory(lead.id, 'user', initInput);
|
| 321 |
+
await StateManager.addHistory(lead.id, 'model', aiResult.text);
|
| 322 |
+
|
| 323 |
+
const cmds = extractCommands(aiResult.text);
|
| 324 |
+
await executeCommands(userId, lead.id, cmds);
|
| 325 |
+
} catch (err) {
|
| 326 |
+
console.error("❌ Background Init Failed:", err);
|
| 327 |
+
// Notify frontend of failure
|
| 328 |
+
if (FRONT_URL) {
|
| 329 |
+
fetch(`${FRONT_URL}/internal/notify`, {
|
| 330 |
+
method: 'POST',
|
| 331 |
+
headers: { 'Content-Type': 'application/json' },
|
| 332 |
+
body: JSON.stringify({ user_id: userId, type: 'toast', message: "Initialization AI Error." })
|
| 333 |
+
}).catch(() => {});
|
| 334 |
+
}
|
| 335 |
+
}
|
| 336 |
+
});
|
| 337 |
+
});
|
| 338 |
+
|
| 339 |
+
// --- MAIN PROCESS ---
|
| 340 |
+
app.post('/process', async (req, res) => {
|
| 341 |
+
const { userId, projectId, prompt, context, task_type = 'chat' } = req.body;
|
| 342 |
+
|
| 343 |
+
if (task_type === 'chat') await StateManager.setFrozen(projectId, false);
|
| 344 |
+
|
| 345 |
+
let selectedModel = (task_type === 'log_ingestion') ? FAST_MODEL_ID : SMART_MODEL_ID;
|
| 346 |
+
let sysPrompt = (task_type === 'log_ingestion') ? prompts.log_analyst_prompt : prompts.director_system_prompt;
|
| 347 |
+
|
| 348 |
+
try {
|
| 349 |
+
const { data: lead } = await supabase.from('leads').select('requirements_doc').eq('id', projectId).single();
|
| 350 |
+
const { data: activeThrust } = await supabase.from('thrusts')
|
| 351 |
+
.select('title, tasks:thrust_tasks(title, status)')
|
| 352 |
+
.eq('lead_id', projectId)
|
| 353 |
+
.eq('status', 'active')
|
| 354 |
+
.order('created_at', { ascending: false })
|
| 355 |
+
.limit(1)
|
| 356 |
+
.single();
|
| 357 |
+
|
| 358 |
+
const { data: timeline } = await supabase.from('timeline_events')
|
| 359 |
+
.select('title, type, description, created_at')
|
| 360 |
+
.eq('lead_id', projectId)
|
| 361 |
+
.order('created_at', { ascending: false })
|
| 362 |
+
.limit(10);
|
| 363 |
+
|
| 364 |
+
const projectContext = `
|
| 365 |
+
[PRD]: ${lead?.requirements_doc?.substring(0, 3000)}...
|
| 366 |
+
|
| 367 |
+
[CURRENT THRUST]:
|
| 368 |
+
${activeThrust ? JSON.stringify(activeThrust, null, 2) : "None"}
|
| 369 |
+
|
| 370 |
+
[RECENT TIMELINE]:
|
| 371 |
+
${JSON.stringify(timeline || [], null, 2)}
|
| 372 |
+
`;
|
| 373 |
+
|
| 374 |
+
const history = await StateManager.getHistory(projectId);
|
| 375 |
+
if (task_type === 'chat') await StateManager.addHistory(projectId, 'user', prompt);
|
| 376 |
+
|
| 377 |
+
let aiResult = await callAI(history, prompt, context, sysPrompt, projectContext, selectedModel);
|
| 378 |
+
let cmds = extractCommands(aiResult.text);
|
| 379 |
+
let flags = await executeCommands(userId, projectId, cmds);
|
| 380 |
+
|
| 381 |
+
if (flags.thrustComplete && task_type === 'log_ingestion') {
|
| 382 |
+
console.log(`🚀 Thrust Complete detected by Analyst. Escalating to Director...`);
|
| 383 |
+
|
| 384 |
+
const escalationPrompt = "The previous thrust is complete based on logs. Generate the next Thrust immediately to keep momentum.";
|
| 385 |
+
const smartResult = await callAI(history, escalationPrompt, context, prompts.director_system_prompt, projectContext, SMART_MODEL_ID);
|
| 386 |
+
|
| 387 |
+
aiResult.text += `\n\n[DIRECTOR INTERVENTION]:\n${smartResult.text}`;
|
| 388 |
+
|
| 389 |
+
const smartCmds = extractCommands(smartResult.text);
|
| 390 |
+
await executeCommands(userId, projectId, smartCmds);
|
| 391 |
+
|
| 392 |
+
await StateManager.addHistory(projectId, 'model', aiResult.text);
|
| 393 |
+
} else {
|
| 394 |
+
await StateManager.addHistory(projectId, 'model', aiResult.text);
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
const cleanText = aiResult.text.replace(/<[^>]+>[\s\S]*?<\/[^>]+>/g, '').trim();
|
| 398 |
+
res.json({ text: cleanText, should_reload: flags.shouldReload });
|
| 399 |
+
|
| 400 |
+
} catch (e) {
|
| 401 |
+
console.error(e);
|
| 402 |
+
res.status(500).json({ error: "Processing Error" });
|
| 403 |
+
}
|
| 404 |
+
});
|
| 405 |
+
|
| 406 |
+
// --- AUTOMATED MORNING BRIEFING ---
|
| 407 |
+
app.post('/automated-briefing', async (req, res) => {
|
| 408 |
+
const { projectId } = req.body;
|
| 409 |
+
console.log(`☀️ Morning Briefing Triggered: ${projectId}`);
|
| 410 |
+
|
| 411 |
+
try {
|
| 412 |
+
const isFrozen = await StateManager.isFrozen(projectId);
|
| 413 |
+
|
| 414 |
+
const { data: lastThrust } = await supabase.from('thrusts')
|
| 415 |
+
.select('created_at')
|
| 416 |
+
.eq('lead_id', projectId)
|
| 417 |
+
.order('created_at', { ascending: false })
|
| 418 |
+
.limit(1)
|
| 419 |
+
.single();
|
| 420 |
+
|
| 421 |
+
const lastThrustDate = lastThrust ? new Date(lastThrust.created_at).getDate() : 0;
|
| 422 |
+
const todayDate = new Date().getDate();
|
| 423 |
+
|
| 424 |
+
if (isFrozen) return res.json({ status: "skipped_frozen" });
|
| 425 |
+
if (lastThrustDate === todayDate) return res.json({ status: "skipped_exists" });
|
| 426 |
+
|
| 427 |
+
const prompt = "It is 6 AM. Generate the Morning Briefing (New Thrust). If the project has been idle, use <freeze_project>true</freeze_project> after this.";
|
| 428 |
+
|
| 429 |
+
const { data: lead } = await supabase.from('leads').select('*').eq('id', projectId).single();
|
| 430 |
+
const { data: timeline } = await supabase.from('timeline_events')
|
| 431 |
+
.select('*').eq('lead_id', projectId).order('created_at', { ascending: false }).limit(5);
|
| 432 |
+
|
| 433 |
+
const projectContext = `[PRD]: ${lead.requirements_doc}\n[TIMELINE]: ${JSON.stringify(timeline)}`;
|
| 434 |
+
const history = await StateManager.getHistory(projectId);
|
| 435 |
+
|
| 436 |
+
const aiResult = await callAI(history, prompt, {}, prompts.director_system_prompt, projectContext, SMART_MODEL_ID);
|
| 437 |
+
|
| 438 |
+
await StateManager.addHistory(projectId, 'model', aiResult.text);
|
| 439 |
+
const cmds = extractCommands(aiResult.text);
|
| 440 |
+
await executeCommands(lead.user_id, projectId, cmds);
|
| 441 |
+
|
| 442 |
+
await StateManager.setFrozen(projectId, true);
|
| 443 |
+
|
| 444 |
+
res.json({ success: true });
|
| 445 |
+
|
| 446 |
+
} catch (e) {
|
| 447 |
+
console.error("Morning Briefing Error", e);
|
| 448 |
+
res.status(500).json({ error: e.message });
|
| 449 |
+
}
|
| 450 |
+
});
|
| 451 |
+
|
| 452 |
+
app.get('/', async (req, res) => {
|
| 453 |
+
res.status(200).json({ status: "Alive" });
|
| 454 |
+
});
|
| 455 |
+
|
| 456 |
+
app.listen(PORT, () => {
|
| 457 |
+
console.log(`✅ Core Online: ${PORT} | Smart: ${SMART_MODEL_ID} | Fast: ${FAST_MODEL_ID}`);
|
| 458 |
+
});
|
| 459 |
+
|
| 460 |
+
/* import express from 'express';
|
| 461 |
+
import cors from 'cors';
|
| 462 |
+
import fs from 'fs';
|
| 463 |
+
import path from 'path';
|
| 464 |
+
import { createClient } from '@supabase/supabase-js';
|
| 465 |
+
|
| 466 |
|
| 467 |
// ==========================================
|
| 468 |
// 1. CONFIGURATION & SETUP
|
|
|
|
| 927 |
console.log(`CRON_REGISTRY_URL ${CRON_REGISTRY_URL}`);
|
| 928 |
console.log(`CRON_SECRET ${CRON_SECRET}`);
|
| 929 |
|
| 930 |
+
});
|
| 931 |
+
*/
|