File size: 4,055 Bytes
afe04f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
document.addEventListener('DOMContentLoaded', () => {
    // Toggle Sidebar on Mobile
    const toggleBtn = document.getElementById('sidebar-toggle');
    const sidebar = document.getElementById('sidebar');
    
    if(toggleBtn && sidebar) {
        toggleBtn.addEventListener('click', () => {
            sidebar.classList.toggle('-translate-x-full');
        });
    }

    // Simulate Global Event Bus for Components
    window.appEvents = new EventTarget();

    // Mock API Keys (In real app, these would be env vars)
    window.API_KEYS = {
        TAVILY: 'tvly-XXXXXXXXX',
        HUGGINGFACE: 'hf_XXXXXXXXX'
    };

    // Mock Memory (LocalStorage)
    const MEMORY_KEY = 'synapse_memory_db';
    
    window.MemoryDB = {
        save: (role, content) => {
            const history = JSON.parse(localStorage.getItem(MEMORY_KEY) || '[]');
            history.push({ role, content, timestamp: new Date().toISOString() });
            localStorage.setItem(MEMORY_KEY, JSON.stringify(history));
        },
        load: () => {
            return JSON.parse(localStorage.getItem(MEMORY_KEY) || '[]');
        },
        clear: () => {
            localStorage.removeItem(MEMORY_KEY);
            const event = new Event('memory-cleared');
            window.appEvents.dispatchEvent(event);
        }
    };

    // AI Simulation Logic
    window.Agent = {
        respond: async (message, context = {}) => {
            return new Promise((resolve) => {
                // Determine if it needs tools (mock)
                const lowerMsg = message.toLowerCase();
                let response = "";
                let toolUsed = null;

                if (lowerMsg.includes('search') || lowerMsg.includes('find')) {
                    toolUsed = 'web_search';
                    response = `I've searched the web for "${message}".\n\n**Found:**\n- *Latest AI Trends 2024* - The rise of multimodal agents is reshaping interfaces...\n- *Quantum Computing* - Breakthrough in error correction...\n\nSource: https://tech-news.daily/ai-trends`;
                } else if (lowerMsg.includes('code') || lowerMsg.includes('function') || lowerMsg.includes('python')) {
                    toolUsed = 'code_editor';
                    response = `I've generated the Python code you requested. Check the code editor panel.\n\nHere is a summary of the logic:\n1. Initialize the class.\n2. Define the main loop.\n3. Handle exceptions gracefully.`;
                    // Trigger code injection event
                    setTimeout(() => {
                        const codeEvent = new CustomEvent('update-code', { 
                            detail: { 
                                code: `import os\n\nclass Agent:\n    def __init__(self, name):\n        self.name = name\n        print(f"Agent {name} initialized")\n\n    def run(self):\n        print("Running logic...")\n\nif __name__ == "__main__":\n    agent = Agent("Synapse")\n    agent.run()` 
                            } 
                        });
                        window.appEvents.dispatchEvent(codeEvent);
                        
                        // Trigger tab switch
                        const switchTabEvent = new CustomEvent('switch-tab', { detail: { index: 1 } });
                        window.appEvents.dispatchEvent(switchTabEvent);
                    }, 1000);
                } else if (lowerMsg.includes('doc') || lowerMsg.includes('file')) {
                    response = "I've analyzed the uploaded documents. It appears to be a specification sheet for the API integration. Key points include the endpoint structure and authentication method.";
                } else {
                    response = `I understand. You said: "${message}".\n\nAs an AI Agent, I can help you search the web, analyze documents, or write code. How would you like to proceed?`;
                }

                // Simulate Network Delay
                setTimeout(() => {
                    resolve(response);
                }, 1500);
            });
        }
    };
});