File size: 3,934 Bytes
724e171
 
4cd3bb5
724e171
 
 
 
 
 
 
 
 
 
 
 
 
4cd3bb5
724e171
4cd3bb5
 
 
 
 
724e171
4cd3bb5
724e171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cd3bb5
 
724e171
 
 
4cd3bb5
 
 
 
724e171
 
 
 
4cd3bb5
 
 
724e171
 
 
 
4cd3bb5
724e171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cd3bb5
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.14.0';

env.allowLocalModels = false; 

const chatContainer = document.getElementById('chat-container');
const inputField = document.getElementById('user-input');
const sendBtn = document.getElementById('send-btn');
const verificationModal = document.getElementById('verification-modal');
const codeBlock = document.getElementById('code-block');
const approveBtn = document.getElementById('btn-approve');
const denyBtn = document.getElementById('btn-deny');

let generator;
let isModelLoaded = false;

async function initModel() {
    appendMessage('System', 'Loading lightweight quantized neural weights into browser memory...');
    try {
        // Switching to a smaller, 4-bit quantized model (SmolLM-135M) to fix the "RangeError: offset out of bounds" 
        // This is a placeholder for the real Ares until you finish training the PyTorch model.
        generator = await pipeline('text-generation', 'Xenova/SmolLM-135M-Instruct', {
            quantized: true
        }); 
        isModelLoaded = true;
        appendMessage('System', 'Ares (Demo Core) Ready.');
    } catch (e) {
        appendMessage('System', 'Error loading weights: ' + e.message);
    }
}

function appendMessage(sender, text, type = 'system') {
    const msgDiv = document.createElement('div');
    msgDiv.classList.add('message', type);
    msgDiv.innerHTML = `<strong>[${sender}]</strong> ${text}`;
    chatContainer.appendChild(msgDiv);
    chatContainer.scrollTop = chatContainer.scrollHeight;
}

async function handleAresLogic(userText) {
    if (userText.toLowerCase().includes("build a tool") || userText.toLowerCase().includes("code")) {
        triggerXiphos(userText);
        return;
    }

    if (!isModelLoaded) {
        appendMessage('Ares', 'Neural pathways are still initializing...', 'ares');
        return;
    }

    appendMessage('Ares', '*Thinking...*', 'ares');
    
    // Formatting prompt for SmolLM Instruct
    const prompt = `<|im_start|>user\n${userText}<|im_end|>\n<|im_start|>assistant\n`;
    
    try {
        const output = await generator(prompt, {
            max_new_tokens: 64,
            temperature: 0.7,
            repetition_penalty: 1.1,
            do_sample: true
        });
        
        chatContainer.lastChild.remove();
        
        let responseText = output[0].generated_text.split('<|im_start|>assistant\n')[1] || output[0].generated_text;
        responseText = responseText.replace(/<\|im_end\|>/g, '').trim();
        
        appendMessage('Ares', responseText, 'ares');
        
    } catch(e) {
        chatContainer.lastChild.remove();
        appendMessage('System', 'Inference failed: ' + e.message, 'system');
    }
}

function triggerXiphos(task) {
    appendMessage('Xiphos', `I am analyzing your request to build a tool for: "${task}"`, 'xiphos');
    
    setTimeout(() => {
        const mockCode = `def dynamic_tool():\n    # Built by Xiphos\n    print("Executing new capability...")\n    return True`;
        codeBlock.innerText = mockCode;
        verificationModal.classList.remove('hidden');
    }, 1000);
}

sendBtn.addEventListener('click', () => {
    const text = inputField.value.trim();
    if (!text) return;
    
    appendMessage('User', text, 'user');
    inputField.value = '';
    
    handleAresLogic(text);
});

inputField.addEventListener('keypress', (e) => {
    if (e.key === 'Enter') sendBtn.click();
});

approveBtn.addEventListener('click', () => {
    verificationModal.classList.add('hidden');
    appendMessage('Xiphos', 'Code verified by User. Tool added to database.', 'xiphos');
    appendMessage('Ares', 'I have integrated the new tool. Ready to proceed.', 'ares');
});

denyBtn.addEventListener('click', () => {
    verificationModal.classList.add('hidden');
    appendMessage('Xiphos', 'Code rejected. Scrapping blueprint.', 'xiphos');
});

initModel();