Update static/index.js
Browse files- static/index.js +124 -48
static/index.js
CHANGED
@@ -1,55 +1,131 @@
|
|
1 |
-
/*
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
section[aria-labelledby]::before {
|
6 |
-
content: attr(aria-labelledby);
|
7 |
-
position: absolute;
|
8 |
-
top: -1.2rem;
|
9 |
-
left: 0;
|
10 |
-
font-size: 0.85rem;
|
11 |
-
color: var(--text-muted);
|
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 |
-
background: var(--bg-secondary);
|
37 |
-
}
|
38 |
|
39 |
-
/*
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
43 |
}
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
}
|
|
|
|
|
48 |
|
49 |
-
/*
|
50 |
-
.
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* static/index.js
|
2 |
+
----------------------------------------------------------
|
3 |
+
AnyCoder AI front‑end logic
|
4 |
+
---------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
/* ~~~~~~~~~~~~~~~~~~~~~~~ 1. MODEL LIST ~~~~~~~~~~~~~~~~~~~~~~~ */
|
7 |
+
const AVAILABLE_MODELS = [
|
8 |
+
{ name: "Qwen/Qwen3‑32B", id: "Qwen/Qwen3-32B" },
|
9 |
+
{ name: "Moonshot Kimi‑K2", id: "moonshotai/Kimi-K2-Instruct", provider: "groq" },
|
10 |
+
{ name: "DeepSeek V3", id: "deepseek-ai/DeepSeek-V3-0324" },
|
11 |
+
{ name: "DeepSeek R1", id: "deepseek-ai/DeepSeek-R1-0528" },
|
12 |
+
{ name: "ERNIE‑4.5‑VL", id: "baidu/ERNIE-4.5-VL-424B-A47B-Base-PT" },
|
13 |
+
{ name: "MiniMax M1", id: "MiniMaxAI/MiniMax-M1-80k" },
|
14 |
+
{ name: "Qwen3‑235B‑A22B", id: "Qwen/Qwen3-235B-A22B" },
|
15 |
+
{ name: "SmolLM3‑3B", id: "HuggingFaceTB/SmolLM3-3B" },
|
16 |
+
{ name: "GLM‑4.1V‑9B‑Thinking", id: "THUDM/GLM-4.1V-9B-Thinking" },
|
17 |
+
{ name: "OpenAI GPT‑4", id: "openai/gpt-4", provider: "openai" },
|
18 |
+
{ name: "Gemini Pro", id: "gemini/pro", provider: "gemini" },
|
19 |
+
{ name: "Fireworks V1", id: "fireworks-ai/fireworks-v1", provider: "fireworks" }
|
20 |
+
];
|
21 |
|
22 |
+
/* ~~~~~~~~~~~~~~~~~~~~~~~ 2. DOM HOOKS ~~~~~~~~~~~~~~~~~~~~~~~ */
|
23 |
+
const $ = (q) => document.querySelector(q);
|
24 |
+
const modelSelect = $('#model');
|
25 |
+
const promptInput = $('#prompt');
|
26 |
+
const fileInput = $('#reference-file');
|
27 |
+
const websiteInput = $('#website-url');
|
28 |
+
const langSelect = $('#language');
|
29 |
+
const webSearchChk = $('#web-search');
|
30 |
+
const codeOut = $('#code-output');
|
31 |
+
const previewFrame = $('#preview');
|
32 |
+
const historyList = $('#history-list');
|
33 |
+
const genBtn = $('#generate');
|
34 |
+
const clearBtn = $('#clear');
|
|
|
|
|
35 |
|
36 |
+
/* ~~~~~~~~~~~~~~~~~~~~~~~ 3. POPULATE MODELS ~~~~~~~~~~~~~~~~~~ */
|
37 |
+
for (const m of AVAILABLE_MODELS) {
|
38 |
+
const opt = document.createElement('option');
|
39 |
+
opt.value = m.id;
|
40 |
+
opt.textContent = m.name;
|
41 |
+
opt.dataset.provider = m.provider || '';
|
42 |
+
modelSelect.appendChild(opt);
|
43 |
}
|
44 |
+
|
45 |
+
/* ~~~~~~~~~~~~~~~~~~~~~~~ 4. TAB HANDLING ~~~~~~~~~~~~~~~~~~~~~ */
|
46 |
+
function activateTab(tabBtn) {
|
47 |
+
const tabList = tabBtn.parentElement;
|
48 |
+
const tabs = tabList.querySelectorAll('[role="tab"]');
|
49 |
+
const panels = tabList.parentElement.querySelectorAll('[role="tabpanel"]');
|
50 |
+
tabs.forEach((t) => {
|
51 |
+
const selected = t === tabBtn;
|
52 |
+
t.setAttribute('aria-selected', selected);
|
53 |
+
panels
|
54 |
+
.forEach((p) => (p.hidden = p.id !== t.getAttribute('aria-controls')));
|
55 |
+
});
|
56 |
}
|
57 |
+
document.querySelectorAll('.tabs[role="tablist"] button')
|
58 |
+
.forEach((btn) => btn.addEventListener('click', () => activateTab(btn)));
|
59 |
|
60 |
+
/* ~~~~~~~~~~~~~~~~~~~~~~~ 5. CLEAR SESSION ~~~~~~~~~~~~~~~~~~~~ */
|
61 |
+
clearBtn.addEventListener('click', () => {
|
62 |
+
promptInput.value = '';
|
63 |
+
fileInput.value = '';
|
64 |
+
websiteInput.value = '';
|
65 |
+
codeOut.textContent = '';
|
66 |
+
previewFrame.srcdoc = '';
|
67 |
+
historyList.innerHTML = '';
|
68 |
+
});
|
69 |
+
|
70 |
+
/* ~~~~~~~~~~~~~~~~~~~~~~~ 6. HELPERS ~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
71 |
+
function addHistoryEntry(prompt) {
|
72 |
+
const li = document.createElement('li');
|
73 |
+
li.textContent =
|
74 |
+
`${new Date().toLocaleTimeString()} – ${prompt.slice(0, 40)}…`;
|
75 |
+
historyList.prepend(li);
|
76 |
}
|
77 |
+
|
78 |
+
/* ~~~~~~~~~~~~~~~~~~~~~~~ 7. GENERATE (CALL BACKEND) ~~~~~~~~~~ */
|
79 |
+
genBtn.addEventListener('click', async () => {
|
80 |
+
const prompt = promptInput.value.trim();
|
81 |
+
if (!prompt) {
|
82 |
+
alert('Please provide a prompt.');
|
83 |
+
return;
|
84 |
+
}
|
85 |
+
|
86 |
+
genBtn.disabled = true;
|
87 |
+
genBtn.textContent = 'Generating…';
|
88 |
+
|
89 |
+
/* Build request payload */
|
90 |
+
const body = {
|
91 |
+
prompt,
|
92 |
+
model_id: modelSelect.value,
|
93 |
+
language: langSelect.value,
|
94 |
+
web_search: webSearchChk.checked,
|
95 |
+
website_url: websiteInput.value || null
|
96 |
+
};
|
97 |
+
|
98 |
+
/* File upload (optional) */
|
99 |
+
if (fileInput.files.length) {
|
100 |
+
const file = fileInput.files[0];
|
101 |
+
body.file_name = file.name;
|
102 |
+
body.file_data = await file.arrayBuffer(); // could base64 if needed
|
103 |
+
}
|
104 |
+
|
105 |
+
try {
|
106 |
+
/* ---- POST to /predict ---------------------------------- */
|
107 |
+
const res = await fetch('/predict', {
|
108 |
+
method: 'POST',
|
109 |
+
headers: { 'Content-Type': 'application/json' },
|
110 |
+
body: JSON.stringify(body)
|
111 |
+
});
|
112 |
+
if (!res.ok) throw new Error(res.statusText);
|
113 |
+
const data = await res.json();
|
114 |
+
|
115 |
+
/* ---- Display results ----------------------------------- */
|
116 |
+
codeOut.textContent = data.code;
|
117 |
+
if (langSelect.value === 'html') {
|
118 |
+
previewFrame.srcdoc = data.code;
|
119 |
+
} else {
|
120 |
+
previewFrame.srcdoc =
|
121 |
+
`<pre style="white-space:pre-wrap">${data.code.replace(/</g, '<')}</pre>`;
|
122 |
+
}
|
123 |
+
addHistoryEntry(prompt);
|
124 |
+
} catch (err) {
|
125 |
+
console.error(err);
|
126 |
+
alert('Error generating code – check console.');
|
127 |
+
} finally {
|
128 |
+
genBtn.disabled = false;
|
129 |
+
genBtn.textContent = 'Generate Code';
|
130 |
+
}
|
131 |
+
});
|