XAUUSDAITradingBot/EchoHeirloom
0
1import express from 'express';2import cors from 'cors';3import dotenv from 'dotenv';4import { AssemblyAI } from 'assemblyai';5import { Groq } from 'groq-sdk';6import { GoogleGenAI } from '@google/genai';7 8dotenv.config();9 10const app = express();11app.use(cors());12app.use(express.json());13 14// Initialize the Hybrid AI Stack15const aaiClient = new AssemblyAI({ apiKey: process.env.ASSEMBLYAI_API_KEY });16const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });17const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });18 19/**20 * 1. ASSEMBLYAI TEMPORARY TOKEN ENDPOINT21 * Generates a short-lived token so the frontend client can connect securely22 * directly to the AssemblyAI Real-Time Voice Agent WebSocket without leaking master keys.23 */24app.post('/api/auth/session', async (req, res) => {25 try {26 const tokenResponse = await aaiClient.realtime.createTemporaryToken({27 expires_in: 3600 // 1 Hour session window28 });29 res.json({ token: tokenResponse.token });30 } catch (error) {31 console.error('Failed to generate AAI token:', error);32 res.status(500).json({ error: 'Authentication orchestration failed' });33 }34});35 36/**37 * 2. REAL-TIME PIPELINE (GROQ GPT-OSS-20B & GEMINI FLASH-LITE)38 * Called rapidly by the frontend during the stream to process incoming 39 * transcript chunks for instant visual UI updates.40 */41app.post('/api/stream/process-chunk', async (req, res) => {42 const { partialText } = req.body;43 44 try {45 // Run parallel, ultra-low latency tasks for live UI updates46 const [titleUpdate, entityMap] = await Promise.all([47 // Groq 20B generates a lightning-fast dynamic topic/tag48 groq.chat.completions.create({49 model: 'gpt-oss-20b',50 messages: [{ role: 'user', content: `Extract a 3-word structural topic/tag from this spoken text snippet: "${partialText}"` }],51 max_tokens: 1052 }),53 // Gemini 2.5 Flash-Lite quickly checks for mentions of proper nouns/people54 ai.models.generateContent({55 model: 'gemini-2.5-flash-lite',56 contents: `Identify any family names or locations mentioned in this sentence: "${partialText}". Output as a brief comma-separated list only.`57 })58 ]);59 60 res.json({61 tag: titleUpdate.choices[0].message.content.trim(),62 entities: entityMap.text.trim()63 });64 } catch (error) {65 res.status(500).json({ error: 'Stream augmentation dropped' });66 }67});68 69/**70 * 3. HEAVY ASYNCHRONOUS POST-PROCESSING (GROQ GPT-OSS-120B & GEMINI 3.5 FLASH)71 * Triggered automatically via Webhook when AssemblyAI fires the "SessionCompleted" event.72 * This does the deep heavy lifting to generate the final beautiful "Museum Archive".73 */74app.post('/api/webhooks/session-complete', async (req, res) => {75 const { transcript_id, status } = req.body;76 77 if (status !== 'completed') return res.sendStatus(200);78 79 try {80 // Fetch the final crystal-clear transcript generated by AssemblyAI81 const transcript = await aaiClient.transcripts.get(transcript_id);82 const rawText = transcript.text;83 84 console.log(`Processing complete narrative ecosystem for transcript: ${transcript_id}`);85 86 // Execute the massive heavy models in parallel in the background87 const [biographyBook, historicalContext] = await Promise.all([88 // Groq 120B synthesizes deep, beautiful biographical narrative voice chapters89 groq.chat.completions.create({90 model: 'gpt-oss-120b',91 messages: [92 { 93 role: 'system', 94 content: 'You are an elite master biographer. Take the raw interview transcript and rewrite it into a highly compelling, emotionally moving first-person autobiography chapter. Preserve original idioms but elevate structural flow.' 95 },96 { role: 'user', content: rawText }97 ]98 }),99 // Gemini 3.5 Flash searches text to inject actual historical context/timelines100 ai.models.generateContent({101 model: 'gemini-3.5-flash',102 contents: `Analyze this raw oral history text: "${rawText}". Identify the prominent historical decade mentioned, and output 3 real-world historical context facts/events that occurred during that time to cross-reference their personal memories.`103 })104 ]);105 106 // Construct the final comprehensive "Heirloom Archive" payload to store in your DB107 const heirloomArchive = {108 transcriptId: transcript_id,109 chapters: biographyBook.choices[0].message.content,110 historicalContext: historicalContext.text,111 processedAt: new Date().toISOString()112 };113 114 // Save heirloomArchive to your database (MongoDB, PostgreSQL, or local JSON store)115 console.log('Successfully saved ecosystem archive to database.');116 117 res.status(200).json({ success: true });118 } catch (error) {119 console.error('Critical background failure:', error);120 res.status(500).json({ error: 'Asynchronous pipeline pipeline broken' });121 }122});123 124const PORT = process.env.PORT || 5000;125app.listen(PORT, () => console.log(`๐ EchoHeirloom Hybrid-AI Nexus active on port ${PORT}`));126 