Spaces:
Running
Running
| import express from 'express'; | |
| import cors from 'cors'; | |
| import fs from 'fs'; | |
| import path from 'path'; | |
| import { createClient } from '@supabase/supabase-js'; | |
| const PORT = 7860; | |
| const SUPABASE_URL = process.env.SUPABASE_URL; | |
| const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY; | |
| const REMOTE_SERVER_URL = process.env.REMOTE_AI_URL || "http://localhost:11434"; | |
| const FRONT_URL = process.env.FRONT_URL; | |
| const CRON_REGISTRY_URL = process.env.CRON_REGISTRY_URL || "http://localhost:7861"; | |
| const CRON_SECRET = process.env.CRON_SECRET || "default_secret"; | |
| const SMART_MODEL_ID = "claude"; | |
| const FAST_MODEL_ID = "gpt-5-mini"; | |
| // The Utility Server that handles the Email Dispatching | |
| const UTILITY_SERVER_URL = process.env.UTILITY_SERVER_URL || "https://lu5zfciin5mk34fowavmhz7dt40pkhhg.lambda-url.us-east-1.on.aws"; | |
| if (!SUPABASE_URL || !SUPABASE_KEY) process.exit(1); | |
| const app = express(); | |
| const supabase = createClient(SUPABASE_URL, SUPABASE_KEY); | |
| app.use(express.json({ limit: '50mb' })); | |
| app.use(cors()); | |
| let prompts = {}; | |
| try { | |
| prompts = JSON.parse(fs.readFileSync(path.resolve('./prompts.json'), 'utf8')); | |
| } catch (e) { process.exit(1); } | |
| const activeProjects = new Map(); | |
| const StateManager = { | |
| getHistory: async (projectId) => { | |
| if (activeProjects.has(projectId)) return activeProjects.get(projectId).history; | |
| const { data: chunks } = await supabase.from('message_chunks').select('*').eq('project_id', projectId).order('chunk_index', { ascending: false }).limit(10); | |
| const fullHistory = (chunks || []).reverse().flatMap(c => c.payload || []); | |
| activeProjects.set(projectId, { history: fullHistory, isFrozen: false }); | |
| return fullHistory; | |
| }, | |
| addHistory: async (projectId, role, text) => { | |
| const newMessage = { role, parts: [{ text }] }; | |
| if (activeProjects.has(projectId)) activeProjects.get(projectId).history.push(newMessage); | |
| try { | |
| const { data: latestChunk } = await supabase.from('message_chunks').select('id, chunk_index, payload').eq('project_id', projectId).order('chunk_index', { ascending: false }).limit(1).single(); | |
| const currentPayload = (latestChunk?.payload) || []; | |
| if (latestChunk && currentPayload.length < 20) { | |
| await supabase.from('message_chunks').update({ payload: [...currentPayload, newMessage] }).eq('id', latestChunk.id); | |
| } else { | |
| await supabase.from('message_chunks').insert({ project_id: projectId, lead_id: projectId, chunk_index: (latestChunk?.chunk_index ?? -1) + 1, payload: [newMessage] }); | |
| } | |
| } catch (e) {} | |
| }, | |
| setFrozen: async (projectId, status) => { | |
| if (activeProjects.has(projectId)) activeProjects.get(projectId).isFrozen = status; | |
| await supabase.from('leads').update({ is_frozen: status }).eq('id', projectId); | |
| }, | |
| isFrozen: async (projectId) => { | |
| if (activeProjects.has(projectId)) return activeProjects.get(projectId).isFrozen; | |
| const { data } = await supabase.from('leads').select('is_frozen').eq('id', projectId).single(); | |
| return data?.is_frozen || false; | |
| } | |
| }; | |
| const callAI = async (history, input, contextData, images, systemPrompt, projectContext, modelId) => { | |
| let contextStr = ""; | |
| try { contextStr = JSON.stringify(contextData, null, 2); } catch {} | |
| const recentHistory = history.slice(-10).map(m => `${m.role === 'model' ? 'Assistant' : 'User'}: ${m.parts?.[0]?.text || ""}`).join('\n'); | |
| const fullPrompt = `System: ${systemPrompt}\n\n${projectContext}\n\n[HISTORY]:\n${recentHistory}\n\n[CONTEXT]: ${contextStr}\n\nUser: ${input}\nAssistant:`; | |
| try { | |
| const response = await fetch(`${REMOTE_SERVER_URL}/api/generate`, { | |
| method: 'POST', headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ model: modelId, prompt: fullPrompt, system_prompt: systemPrompt, images: images || [] }) | |
| }); | |
| const result = await response.json(); | |
| return { text: result.data || result.text || "", usage: result.usage }; | |
| } catch (e) { return { text: "<notification>AI Unreachable</notification>", usage: {} }; } | |
| }; | |
| function extractCommands(text) { | |
| const commands = []; | |
| const parse = (regex, type, isJson = true) => { | |
| let match; | |
| regex.lastIndex = 0; | |
| while ((match = regex.exec(text)) !== null) { | |
| let rawPayload = match[1].trim(); | |
| try { | |
| commands.push({ type, payload: isJson ? JSON.parse(rawPayload) : rawPayload }); | |
| } catch (e) { | |
| if (type === 'create_thrust') commands.push({ type, payload: { title: "System Thrust", markdown_content: rawPayload, tasks: [] } }); | |
| } | |
| } | |
| }; | |
| parse(/<thrust_create>([\s\S]*?)<\/thrust_create>/gi, 'create_thrust'); | |
| parse(/<timeline_log>([\s\S]*?)<\/timeline_log>/gi, 'log_timeline'); | |
| parse(/<notification>([\s\S]*?)<\/notification>/gi, 'notification', false); | |
| parse(/<update_requirements>([\s\S]*?)<\/update_requirements>/gi, 'update_requirements', false); | |
| parse(/<schedule_briefing>([\s\S]*?)<\/schedule_briefing>/gi, 'schedule_briefing'); | |
| parse(/<freeze_project>([\s\S]*?)<\/freeze_project>/gi, 'freeze_project', false); | |
| parse(/<thrust_complete>([\s\S]*?)<\/thrust_complete>/gi, 'thrust_complete', false); | |
| parse(/<complete_task>([\s\S]*?)<\/complete_task>/gi, 'complete_task', false); | |
| return commands; | |
| } | |
| // --- BULLETPROOF CRON MATH --- | |
| async function registerMorningCron(projectId, offset) { | |
| const now = new Date(); | |
| const target = new Date(now); | |
| const targetUtcMinutes = (5 * 60) - (offset * 60); | |
| target.setUTCHours(0, targetUtcMinutes, 0, 0); | |
| if (target <= now) target.setDate(target.getDate() + 1); | |
| const delayMs = target.getTime() - now.getTime(); | |
| console.log(`⏰ Briefing Scheduled for ${projectId}. Local 6AM occurs in ${(delayMs/1000/60/60).toFixed(2)} hours.`); | |
| fetch(`${CRON_REGISTRY_URL}/register`, { | |
| method: 'POST', headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| secret: CRON_SECRET, | |
| jobId: `briefing_${projectId}`, | |
| intervalMs: 86400000, | |
| initialDelay: delayMs, | |
| webhookUrl: `https://everydaytok-thrust-core-server.hf.space/automated-briefing`, | |
| leadId: projectId, | |
| payload: { projectId, timezoneOffset: offset } | |
| }) | |
| }).catch(()=>{}); | |
| } | |
| // --- EMAIL DISPATCHER HELPER --- | |
| async function dispatchEmail(userId, projectId, projectName, briefingId, markdownContent) { | |
| console.log(`📧 Dispatching email to Utility Server for Project: ${projectName}`); | |
| fetch(`${UTILITY_SERVER_URL}/api/email/send-briefing`, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ userId, projectId, projectName, briefingId, markdownContent }) | |
| }).catch(e => console.error("Email Dispatch Error:", e.message)); | |
| } | |
| async function executeCommands(userId, projectId, commands) { | |
| // TRACK THE NEW THRUST SO WE CAN EMAIL IT LATER | |
| let flags = { shouldReload: false, thrustComplete: false, newThrustId: null, newThrustMarkdown: null }; | |
| for (const cmd of commands) { | |
| try { | |
| if (cmd.type === 'create_thrust') { | |
| // Wipe old thrust | |
| await supabase.from('thrusts').delete().eq('lead_id', projectId); | |
| const { data: thrust } = await supabase.from('thrusts').insert({ lead_id: projectId, title: cmd.payload.title, markdown_content: cmd.payload.markdown_content, status: 'active' }).select().single(); | |
| if (thrust && cmd.payload.tasks && cmd.payload.tasks.length > 0) { | |
| const tasks = cmd.payload.tasks.map(t => ({ thrust_id: thrust.id, title: t })); | |
| await supabase.from('thrust_tasks').insert(tasks); | |
| } | |
| flags.shouldReload = true; | |
| // Capture data for email dispatch | |
| if (thrust) { | |
| flags.newThrustId = thrust.id; | |
| flags.newThrustMarkdown = cmd.payload.markdown_content; | |
| } | |
| } | |
| if (cmd.type === 'log_timeline') { | |
| await supabase.from('timeline_events').insert({ lead_id: projectId, title: cmd.payload.title, description: cmd.payload.description, type: (cmd.payload.type || 'system').toLowerCase() }); | |
| flags.shouldReload = true; | |
| } | |
| if (cmd.type === 'update_requirements') { | |
| await supabase.from('leads').update({ requirements_doc: cmd.payload }).eq('id', projectId); | |
| flags.shouldReload = true; | |
| } | |
| if (cmd.type === 'schedule_briefing') { | |
| await registerMorningCron(projectId, cmd.payload.timezone_offset || 0); | |
| } | |
| if (cmd.type === 'complete_task') { | |
| const { data: active } = await supabase.from('thrusts').select('id').eq('lead_id', projectId).eq('status', 'active').single(); | |
| if (active) { | |
| await supabase.from('thrust_tasks').update({ status: 'done' }).eq('thrust_id', active.id).ilike('title', `%${cmd.payload}%`); | |
| flags.shouldReload = true; | |
| } | |
| } | |
| if (cmd.type === 'thrust_complete') { | |
| const { data: active } = await supabase.from('thrusts').select('id').eq('lead_id', projectId).eq('status', 'active').single(); | |
| if (active) { | |
| await supabase.from('thrusts').update({ status: 'completed' }).eq('id', active.id); | |
| await supabase.from('thrust_tasks').delete().eq('thrust_id', active.id); | |
| flags.thrustComplete = true; | |
| } | |
| } | |
| if (cmd.type === 'freeze_project') { | |
| const isFrozen = cmd.payload === 'true'; | |
| await StateManager.setFrozen(projectId, isFrozen); | |
| } | |
| if (cmd.type === 'notification' && FRONT_URL) { | |
| fetch(`${FRONT_URL}/internal/notify`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user_id: userId, type: 'toast', message: cmd.payload }) }).catch(() => {}); | |
| } | |
| } catch (e) {} | |
| } | |
| return flags; | |
| } | |
| app.post('/init-project', async (req, res) => { | |
| // Note: timezoneOffset added here to capture frontend's data | |
| const { userId, name, description, localPath, timezoneOffset = 0 } = req.body; | |
| const { data: lead } = await supabase.from('leads').insert({ user_id: userId, name, description, local_path: localPath, status: 'active', requirements_doc: "Init..." }).select().single(); | |
| res.json({ success: true, leadId: lead.id }); | |
| setImmediate(async () => { | |
| try { | |
| const initInput = `PROJECT: ${name}\nDESC: ${description}\nUSER TIMEZONE OFFSET: ${timezoneOffset}\nTask: Init PRD, First Thrust, Schedule Morning Briefing.`; | |
| const aiResult = await callAI([], initInput, {}, [], prompts.init_system_prompt, "", SMART_MODEL_ID); | |
| aiResult.text += `\n<notification>Project '${name}' initialized successfully!</notification>`; | |
| await StateManager.addHistory(lead.id, 'user', initInput); | |
| await StateManager.addHistory(lead.id, 'model', aiResult.text); | |
| const cmds = extractCommands(aiResult.text); | |
| await executeCommands(userId, lead.id, cmds); | |
| // NO EMAIL DISPATCH HERE - strictly for morning briefings! | |
| } catch (err) {} | |
| }); | |
| }); | |
| app.post('/process', async (req, res) => { | |
| const { userId, projectId, prompt, context, images, task_type = 'chat' } = req.body; | |
| if (task_type === 'chat') await StateManager.setFrozen(projectId, false); | |
| let selectedModel = (task_type === 'log_ingestion') ? FAST_MODEL_ID : SMART_MODEL_ID; | |
| let sysPrompt = (task_type === 'log_ingestion') ? prompts.log_analyst_prompt : prompts.director_system_prompt; | |
| try { | |
| const { data: lead } = await supabase.from('leads').select('requirements_doc').eq('id', projectId).single(); | |
| const { data: activeThrust } = await supabase.from('thrusts').select('title, tasks:thrust_tasks(title, status)').eq('lead_id', projectId).eq('status', 'active').order('created_at', { ascending: false }).limit(1).single(); | |
| const { data: timeline } = await supabase.from('timeline_events').select('title, type, description, created_at').eq('lead_id', projectId).order('created_at', { ascending: false }).limit(10); | |
| const projectContext = `[PRD]: ${lead?.requirements_doc?.substring(0, 3000)}...\n[CURRENT THRUST]: ${activeThrust ? JSON.stringify(activeThrust) : "None"}\n[RECENT TIMELINE]: ${JSON.stringify(timeline || [])}`; | |
| const history = await StateManager.getHistory(projectId); | |
| let aiResult = await callAI(history, prompt, context, images, sysPrompt, projectContext, selectedModel); | |
| let cmds = extractCommands(aiResult.text); | |
| let flags = await executeCommands(userId, projectId, cmds); | |
| if (flags.thrustComplete && task_type === 'log_ingestion') { | |
| const escalationPrompt = "The previous thrust is complete based on logs. Generate the next Thrust immediately to keep momentum."; | |
| const smartResult = await callAI(history, escalationPrompt, context, [], prompts.director_system_prompt, projectContext, SMART_MODEL_ID); | |
| aiResult.text += `\n\n[DIRECTOR INTERVENTION]:\n${smartResult.text}`; | |
| const smartCmds = extractCommands(smartResult.text); | |
| await executeCommands(userId, projectId, smartCmds); | |
| // NO EMAIL DISPATCH HERE - strictly for morning briefings! | |
| await StateManager.addHistory(projectId, 'model', aiResult.text); | |
| } else { | |
| await StateManager.addHistory(projectId, 'model', aiResult.text); | |
| } | |
| const cleanText = aiResult.text.replace(/<[^>]+>[\s\S]*?<\/[^>]+>/g, '').trim(); | |
| res.json({ text: cleanText, should_reload: flags.shouldReload }); | |
| } catch (e) { res.status(500).json({ error: "Processing Error" }); } | |
| }); | |
| app.post('/automated-briefing', async (req, res) => { | |
| const { projectId } = req.body; | |
| try { | |
| const isFrozen = await StateManager.isFrozen(projectId); | |
| const { data: lastThrust } = await supabase.from('thrusts').select('created_at').eq('lead_id', projectId).order('created_at', { ascending: false }).limit(1).single(); | |
| const lastThrustDate = lastThrust ? new Date(lastThrust.created_at).getDate() : 0; | |
| const today = new Date(); | |
| const todayDate = today.getDate(); | |
| if (isFrozen) return res.json({ status: "skipped_frozen" }); | |
| if (lastThrustDate === todayDate) return res.json({ status: "skipped_exists" }); | |
| const currentDateString = today.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); | |
| const prompt = `[SYSTEM TIME: ${currentDateString} at 5:00 AM]\nIt is morning. Generate today's Morning Briefing (New Thrust). Look at the RECENT TIMELINE to see what was accomplished yesterday. Adopt a highly conversational, proactive tone in the markdown (e.g., 'Morning! You finished X yesterday. Today, the priority is Y.'). If the project has been idle for days, use <freeze_project>true</freeze_project>.`; | |
| const { data: lead } = await supabase.from('leads').select('*').eq('id', projectId).single(); | |
| const { data: timeline } = await supabase.from('timeline_events').select('*').eq('lead_id', projectId).order('created_at', { ascending: false }).limit(5); | |
| const projectContext = `[PRD]: ${lead.requirements_doc}\n[RECENT TIMELINE]: ${JSON.stringify(timeline)}`; | |
| const history = await StateManager.getHistory(projectId); | |
| const aiResult = await callAI(history, prompt, {}, [], prompts.director_system_prompt, projectContext, SMART_MODEL_ID); | |
| await StateManager.addHistory(projectId, 'model', aiResult.text); | |
| const cmds = extractCommands(aiResult.text); | |
| const flags = await executeCommands(lead.user_id, projectId, cmds); | |
| // 👉 EMAIL IS DISPATCHED ONLY HERE | |
| if (flags.newThrustId) { | |
| await dispatchEmail(lead.user_id, projectId, lead.name, flags.newThrustId, flags.newThrustMarkdown); | |
| } | |
| await StateManager.setFrozen(projectId, true); | |
| res.json({ success: true }); | |
| } catch (e) { res.status(500).json({ error: e.message }); } | |
| }); | |
| app.get('/', async (req, res) => res.status(200).json({ status: "Alive" })); | |
| app.listen(PORT, () => console.log(`✅ Core Online: ${PORT}`)); |