Update static/index.js
Browse files- static/index.js +115 -115
static/index.js
CHANGED
@@ -1,131 +1,131 @@
|
|
1 |
/* static/index.js
|
2 |
----------------------------------------------------------
|
3 |
-
AnyCoder
|
4 |
---------------------------------------------------------- */
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
-
/*
|
23 |
-
const $
|
24 |
-
const
|
25 |
-
const
|
26 |
-
const
|
27 |
-
const
|
28 |
-
const
|
29 |
-
const
|
30 |
-
const codeOut
|
31 |
-
const
|
32 |
-
const
|
33 |
-
const genBtn
|
34 |
-
const clearBtn
|
35 |
|
36 |
-
/*
|
37 |
-
for (const m of AVAILABLE_MODELS) {
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
}
|
44 |
|
45 |
-
/*
|
46 |
-
function activateTab(
|
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 |
-
historyList.prepend(li);
|
76 |
-
}
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
85 |
|
86 |
-
|
87 |
-
|
88 |
|
89 |
-
/*
|
90 |
-
|
91 |
-
prompt
|
92 |
-
|
93 |
-
language: langSelect.value,
|
94 |
-
web_search: webSearchChk.checked,
|
95 |
-
website_url: websiteInput.value || null
|
96 |
-
};
|
97 |
|
98 |
-
|
99 |
-
|
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 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
}
|
112 |
-
if (!res.ok) throw new Error(res.statusText);
|
113 |
-
const data = await res.json();
|
114 |
|
115 |
-
/*
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
previewFrame.srcdoc =
|
121 |
-
`<pre style="white-space:pre-wrap">${data.code.replace(/</g, '<')}</pre>`;
|
122 |
}
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
/* static/index.js
|
2 |
----------------------------------------------------------
|
3 |
+
AnyCoder AI – interactive front‑end
|
4 |
---------------------------------------------------------- */
|
5 |
|
6 |
+
(() => {
|
7 |
+
/* ---------- 1. MODEL CATALOG -------------------------------- */
|
8 |
+
const AVAILABLE_MODELS = [
|
9 |
+
{ name: "Qwen/Qwen3‑32B", id: "Qwen/Qwen3-32B" },
|
10 |
+
{ name: "Moonshot Kimi‑K2", id: "moonshotai/Kimi-K2-Instruct", provider: "groq" },
|
11 |
+
{ name: "DeepSeek V3", id: "deepseek-ai/DeepSeek-V3-0324" },
|
12 |
+
{ name: "DeepSeek R1", id: "deepseek-ai/DeepSeek-R1-0528" },
|
13 |
+
{ name: "ERNIE‑4.5‑VL", id: "baidu/ERNIE-4.5-VL-424B-A47B-Base-PT" },
|
14 |
+
{ name: "MiniMax M1", id: "MiniMaxAI/MiniMax-M1-80k" },
|
15 |
+
{ name: "Qwen3‑235B‑A22B", id: "Qwen/Qwen3-235B-A22B" },
|
16 |
+
{ name: "SmolLM3‑3B", id: "HuggingFaceTB/SmolLM3-3B" },
|
17 |
+
{ name: "GLM‑4.1V‑9B‑Thinking", id: "THUDM/GLM-4.1V-9B-Thinking" },
|
18 |
+
{ name: "OpenAI GPT‑4", id: "openai/gpt-4", provider: "openai" },
|
19 |
+
{ name: "Gemini Pro", id: "gemini/pro", provider: "gemini" },
|
20 |
+
{ name: "Fireworks V1", id: "fireworks-ai/fireworks-v1", provider: "fireworks" }
|
21 |
+
];
|
22 |
|
23 |
+
/* ---------- 2. DOM SHORTCUTS -------------------------------- */
|
24 |
+
const $ = (q) => document.querySelector(q);
|
25 |
+
const modelSel = $('#model');
|
26 |
+
const promptIn = $('#prompt');
|
27 |
+
const fileIn = $('#reference-file');
|
28 |
+
const urlIn = $('#website-url');
|
29 |
+
const langSel = $('#language');
|
30 |
+
const searchChk = $('#web-search');
|
31 |
+
const codeOut = $('#code-output');
|
32 |
+
const previewOut = $('#preview');
|
33 |
+
const histList = $('#history-list');
|
34 |
+
const genBtn = $('#generate');
|
35 |
+
const clearBtn = $('#clear');
|
36 |
|
37 |
+
/* ---------- 3. INIT MODEL DROPDOWN -------------------------- */
|
38 |
+
for (const m of AVAILABLE_MODELS) {
|
39 |
+
const opt = document.createElement('option');
|
40 |
+
opt.value = m.id;
|
41 |
+
opt.textContent = m.name;
|
42 |
+
opt.dataset.provider = m.provider || '';
|
43 |
+
modelSel.appendChild(opt);
|
44 |
+
}
|
45 |
|
46 |
+
/* ---------- 4. TABS ---------------------------------------- */
|
47 |
+
function activateTab(btn) {
|
48 |
+
const tabs = btn.parentElement.querySelectorAll('[role="tab"]');
|
49 |
+
const panels = btn.closest('section').querySelectorAll('[role="tabpanel"]');
|
50 |
+
tabs.forEach(t => {
|
51 |
+
const sel = t === btn;
|
52 |
+
t.setAttribute('aria-selected', sel);
|
53 |
+
t.tabIndex = sel ? 0 : -1;
|
54 |
+
panels.forEach(p => p.hidden = p.id !== t.getAttribute('aria-controls'));
|
55 |
+
});
|
56 |
+
}
|
57 |
+
document
|
58 |
+
.querySelectorAll('.tabs[role="tablist"] button')
|
59 |
+
.forEach(btn => btn.addEventListener('click', () => activateTab(btn)));
|
60 |
|
61 |
+
/* ---------- 5. UTILITIES ----------------------------------- */
|
62 |
+
const toBase64 = (file) =>
|
63 |
+
new Promise((res, rej) => {
|
64 |
+
const reader = new FileReader();
|
65 |
+
reader.onload = () => res(reader.result.split(',')[1]);
|
66 |
+
reader.onerror = rej;
|
67 |
+
reader.readAsDataURL(file);
|
68 |
+
});
|
|
|
69 |
|
70 |
+
const addHistory = (text) => {
|
71 |
+
const li = document.createElement('li');
|
72 |
+
li.textContent = `${new Date().toLocaleTimeString()} — ${text.slice(0, 40)}…`;
|
73 |
+
histList.prepend(li);
|
74 |
+
};
|
|
|
|
|
75 |
|
76 |
+
const resetUI = () => {
|
77 |
+
promptIn.value = '';
|
78 |
+
fileIn.value = '';
|
79 |
+
urlIn.value = '';
|
80 |
+
codeOut.textContent = '';
|
81 |
+
previewOut.srcdoc = '';
|
82 |
+
histList.innerHTML = '';
|
83 |
+
};
|
84 |
|
85 |
+
/* ---------- 6. CLEAR SESSION ------------------------------- */
|
86 |
+
clearBtn.addEventListener('click', resetUI);
|
87 |
|
88 |
+
/* ---------- 7. GENERATE (CALL BACK‑END) -------------------- */
|
89 |
+
genBtn.addEventListener('click', async () => {
|
90 |
+
const prompt = promptIn.value.trim();
|
91 |
+
if (!prompt) { alert('Please provide a prompt.'); return; }
|
|
|
|
|
|
|
|
|
92 |
|
93 |
+
genBtn.disabled = true;
|
94 |
+
genBtn.textContent = 'Generating…';
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
const payload = {
|
97 |
+
prompt,
|
98 |
+
model_name: modelSel.options[modelSel.selectedIndex].textContent,
|
99 |
+
language: langSel.value,
|
100 |
+
web_search: searchChk.checked,
|
101 |
+
website_url: urlIn.value || null
|
102 |
+
};
|
|
|
|
|
103 |
|
104 |
+
/* optional file */
|
105 |
+
if (fileIn.files.length) {
|
106 |
+
const file = fileIn.files[0];
|
107 |
+
payload.file_name = file.name;
|
108 |
+
payload.file_data = await toBase64(file);
|
|
|
|
|
109 |
}
|
110 |
+
|
111 |
+
try {
|
112 |
+
const res = await fetch('/predict', {
|
113 |
+
method: 'POST',
|
114 |
+
headers: { 'Content-Type': 'application/json' },
|
115 |
+
body: JSON.stringify(payload)
|
116 |
+
});
|
117 |
+
if (!res.ok) throw new Error(await res.text());
|
118 |
+
const { code, preview } = await res.json();
|
119 |
+
|
120 |
+
codeOut.textContent = code || '// (empty)';
|
121 |
+
previewOut.srcdoc = preview || `<pre>${code.replace(/</g, '<')}</pre>`;
|
122 |
+
addHistory(prompt);
|
123 |
+
} catch (err) {
|
124 |
+
console.error(err);
|
125 |
+
alert('Generation failed – see console for details.');
|
126 |
+
} finally {
|
127 |
+
genBtn.disabled = false;
|
128 |
+
genBtn.textContent = 'Generate Code';
|
129 |
+
}
|
130 |
+
});
|
131 |
+
})();
|