Spaces:
Running
Running
Update apps/aura_measure.js
Browse files- apps/aura_measure.js +41 -21
apps/aura_measure.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
// apps/aura_measure.js
|
| 2 |
import express from 'express';
|
| 3 |
import { generateCompletion } from '../ai_engine.js';
|
|
|
|
| 4 |
|
| 5 |
const router = express.Router();
|
| 6 |
|
|
@@ -20,45 +21,64 @@ The "analysis_markdown" string MUST follow this structure:
|
|
| 20 |
## 🔴 Aura Lost
|
| 21 |
* **[Element]**: -[Points] ([Reason])
|
| 22 |
|
| 23 |
-
## 💬 The Play
|
|
|
|
| 24 |
|
| 25 |
RULES:
|
| 26 |
- Speak in modern internet/Gen-Z slang (aura, rizz, cooked, standing on business, nonchalant, etc.).
|
| 27 |
- Be brutally honest but funny.`;
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
router.post('/analyze', async (req, res) => {
|
| 30 |
try {
|
| 31 |
const { media_base64, context } = req.body;
|
| 32 |
if (!media_base64) throw new Error("No media provided.");
|
| 33 |
-
|
| 34 |
-
console.log(`[AURA MEASURE] Analyzing vibe...`);
|
| 35 |
-
|
| 36 |
-
const prompt = context
|
| 37 |
-
? `Context: ${context}\nAnalyze this image/screenshot and calculate my Aura. Return JSON.`
|
| 38 |
-
: `Analyze this image/screenshot and calculate my Aura. Return JSON.`;
|
| 39 |
-
|
| 40 |
const aiResult = await generateCompletion({
|
| 41 |
model: "qwen",
|
| 42 |
-
prompt:
|
| 43 |
system_prompt: AURA_SYSTEM_PROMPT,
|
| 44 |
-
images:[media_base64]
|
| 45 |
});
|
| 46 |
-
|
| 47 |
if (!aiResult.success) throw new Error(aiResult.error);
|
| 48 |
-
|
| 49 |
-
// 🚨 Clean, flawless JSON parsing. No more Regex!
|
| 50 |
const parsedData = JSON.parse(aiResult.data);
|
| 51 |
-
|
| 52 |
-
res.json({
|
| 53 |
-
success: true,
|
| 54 |
-
score: parsedData.score,
|
| 55 |
-
analysis: parsedData.analysis_markdown
|
| 56 |
-
});
|
| 57 |
-
|
| 58 |
} catch (err) {
|
| 59 |
-
console.error("[AURA ERROR]", err.message);
|
| 60 |
res.status(500).json({ success: false, error: err.message });
|
| 61 |
}
|
| 62 |
});
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
export default router;
|
|
|
|
| 1 |
// apps/aura_measure.js
|
| 2 |
import express from 'express';
|
| 3 |
import { generateCompletion } from '../ai_engine.js';
|
| 4 |
+
import { supabase } from '../config/supabaseClient.js';
|
| 5 |
|
| 6 |
const router = express.Router();
|
| 7 |
|
|
|
|
| 21 |
## 🔴 Aura Lost
|
| 22 |
* **[Element]**: -[Points] ([Reason])
|
| 23 |
|
| 24 |
+
## 💬 The Play
|
| 25 |
+
[One piece of advice to increase their aura tomorrow, or a nonchalant reply they should send if it's a text screenshot.]
|
| 26 |
|
| 27 |
RULES:
|
| 28 |
- Speak in modern internet/Gen-Z slang (aura, rizz, cooked, standing on business, nonchalant, etc.).
|
| 29 |
- Be brutally honest but funny.`;
|
| 30 |
|
| 31 |
+
// ── LEADERBOARD CACHE ──
|
| 32 |
+
let leaderboardCache = null;
|
| 33 |
+
let lastCacheUpdate = 0;
|
| 34 |
+
const CACHE_DURATION = 2 * 60 * 1000; // 2 minutes
|
| 35 |
+
|
| 36 |
router.post('/analyze', async (req, res) => {
|
| 37 |
try {
|
| 38 |
const { media_base64, context } = req.body;
|
| 39 |
if (!media_base64) throw new Error("No media provided.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
const aiResult = await generateCompletion({
|
| 41 |
model: "qwen",
|
| 42 |
+
prompt: context || "Analyze this image and calculate my Aura. Return JSON.",
|
| 43 |
system_prompt: AURA_SYSTEM_PROMPT,
|
| 44 |
+
images: [media_base64]
|
| 45 |
});
|
|
|
|
| 46 |
if (!aiResult.success) throw new Error(aiResult.error);
|
|
|
|
|
|
|
| 47 |
const parsedData = JSON.parse(aiResult.data);
|
| 48 |
+
res.json({ success: true, score: parsedData.score, analysis: parsedData.analysis_markdown });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
} catch (err) {
|
|
|
|
| 50 |
res.status(500).json({ success: false, error: err.message });
|
| 51 |
}
|
| 52 |
});
|
| 53 |
|
| 54 |
+
router.get('/leaderboard', async (req, res) => {
|
| 55 |
+
try {
|
| 56 |
+
const now = Date.now();
|
| 57 |
+
if (leaderboardCache && (now - lastCacheUpdate < CACHE_DURATION)) {
|
| 58 |
+
return res.json({ success: true, data: leaderboardCache });
|
| 59 |
+
}
|
| 60 |
+
const { data, error } = await supabase
|
| 61 |
+
.from('aura_leaderboard')
|
| 62 |
+
.select('username, avatar, total_score')
|
| 63 |
+
.order('total_score', { ascending: false })
|
| 64 |
+
.limit(50);
|
| 65 |
+
if (error) throw error;
|
| 66 |
+
leaderboardCache = data;
|
| 67 |
+
lastCacheUpdate = now;
|
| 68 |
+
res.json({ success: true, data });
|
| 69 |
+
} catch (err) { res.status(500).json({ success: false, error: err.message }); }
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
router.post('/sync', async (req, res) => {
|
| 73 |
+
try {
|
| 74 |
+
const { device_id, username, avatar, total_score } = req.body;
|
| 75 |
+
const { error } = await supabase.from('aura_leaderboard').upsert({
|
| 76 |
+
device_id, username, avatar, total_score, updated_at: new Date()
|
| 77 |
+
}, { onConflict: 'device_id' });
|
| 78 |
+
if (error) throw error;
|
| 79 |
+
lastCacheUpdate = 0; // Invalidate cache
|
| 80 |
+
res.json({ success: true });
|
| 81 |
+
} catch (err) { res.status(500).json({ success: false, error: err.message }); }
|
| 82 |
+
});
|
| 83 |
+
|
| 84 |
export default router;
|