File size: 5,208 Bytes
96267f5
25bf9bb
96267f5
973db88
96267f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3380f8e
25bf9bb
96267f5
973db88
96267f5
 
 
 
3380f8e
ca40114
96267f5
 
ca950c4
96267f5
 
 
 
ca950c4
25bf9bb
96267f5
 
 
065cfda
96267f5
 
 
 
 
 
 
 
25bf9bb
 
065cfda
96267f5
 
 
 
 
 
 
 
25bf9bb
96267f5
ca950c4
973db88
065cfda
96267f5
 
ca950c4
96267f5
065cfda
ca950c4
25bf9bb
96267f5
 
ca950c4
 
25bf9bb
96267f5
3380f8e
065cfda
96267f5
 
 
 
 
 
 
 
 
25bf9bb
96267f5
 
 
 
25bf9bb
96267f5
 
ca950c4
 
25bf9bb
 
ca950c4
96267f5
 
25bf9bb
ca950c4
25bf9bb
96267f5
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
/* static/index.js — AnyCoder front‑end logic */

// ────── 1. MODEL LIST ──────
const MODELS = [
  { name: "Moonshot Kimi‑K2",                   id: "moonshotai/Kimi-K2-Instruct",           provider: "groq"      },
  { name: "DeepSeek V3",                        id: "deepseek-ai/DeepSeek-V3-0324"                            },
  { name: "DeepSeek R1",                        id: "deepseek-ai/DeepSeek-R1-0528"                            },
  { name: "ERNIE‑4.5‑VL",                      id: "baidu/ERNIE-4.5-VL-424B-A47B-Base-PT"                    },
  { name: "MiniMax M1",                         id: "MiniMaxAI/MiniMax-M1-80k"                                },
  { name: "Qwen3‑235B‑A22B",                    id: "Qwen/Qwen3-235B-A22B"                                    },
  { name: "Qwen3‑235B‑A22B‑Instruct‑2507",      id: "Qwen/Qwen3-235B-A22B-Instruct-2507"                      },
  { name: "Qwen3‑235B‑A22B‑Thinking",           id: "Qwen/Qwen3-235B-A22B-Thinking"                           },
  { name: "Qwen3‑Coder‑480B‑A35B",              id: "Qwen/Qwen3-Coder-480B-A35B-Instruct"                     },
  { name: "Qwen3‑32B",                          id: "Qwen/Qwen3-32B"                                          },
  { name: "SmolLM3‑3B",                         id: "HuggingFaceTB/SmolLM3-3B"                                },
  { name: "GLM‑4.1V‑9B‑Thinking",               id: "THUDM/GLM-4.1V-9B-Thinking"                              },
  /* optional premium providers — keep but hide if you don't have keys */
  { name: "OpenAI GPT‑4",                       id: "openai/gpt-4",                    provider: "openai"      },
  { name: "Gemini Pro",                         id: "gemini/pro",                      provider: "gemini"      },
  { name: "Fireworks V1",                       id: "fireworks-ai/fireworks-v1",       provider: "fireworks"   }
];

// ────── 2. LANGUAGE LIST ──────
const LANGS = [
  "python","c","cpp","markdown","latex","json","html","css","javascript",
  "jinja2","typescript","yaml","dockerfile","shell","r","sql","sql-msSQL",
  "sql-mySQL","sql-mariaDB","sql-sqlite","sql-cassandra","sql-plSQL",
  "sql-hive","sql-pgSQL","sql-gql","sql-gpSQL","sql-sparkSQL","sql-esper"
];

// ────── 3. DOM SHORTCUTS ──────
const $ = s => document.querySelector(s);
const els = {
  model: $("#model"), lang : $("#lang"), prompt: $("#prompt"),
  file : $("#file"),  url  : $("#url"),  search: $("#search"),
  code : $("#code"),  prev : $("#preview"), hist : $("#hist"),
  gen  : $("#gen"),   clr  : $("#clear")
};

// ────── 4. POPULATE SELECTS ──────
MODELS.forEach(m => els.model.append(new Option(m.name, m.id)));
LANGS .forEach(l => els.lang .append(new Option(l.toUpperCase(), l)));

// ────── 5. SIMPLE TAB SWITCHER ──────
document.querySelectorAll(".tabs[role=tablist]").forEach(tl => {
  tl.addEventListener("click", ev => {
    if (ev.target.role !== "tab") return;
    tl.querySelectorAll("[role=tab]").forEach(t =>
      t.setAttribute("aria-selected", t === ev.target));
    tl.parentElement.querySelectorAll("[role=tabpanel]").forEach(p =>
      p.hidden = (p.id !== ev.target.getAttribute("aria-controls")));
  });
});

// ────── 6. CLEAR SESSION ──────
els.clr.onclick = () => {
  els.prompt.value = ""; els.file.value = ""; els.url.value = "";
  els.code.textContent = ""; els.prev.srcdoc = ""; els.hist.innerHTML = "";
};

// ────── 7. ADD HISTORY LINE ──────
const addHist = txt => {
  const li = document.createElement("li");
  li.textContent = `${new Date().toLocaleTimeString()}${txt.slice(0,40)}…`;
  els.hist.prepend(li);
};

// ────── 8. GENERATE (calls /run/predict) ──────
els.gen.onclick = async () => {
  const prompt = els.prompt.value.trim();
  if (!prompt) return alert("Provide a prompt first.");

  els.gen.disabled = true; els.gen.textContent = "Generating…";

  // optional file upload
  let file_path = null;
  if (els.file.files.length) {
    const fd = new FormData(); fd.append("file", els.file.files[0]);
    const up = await fetch("/upload", { method:"POST", body: fd });
    file_path = (await up.json()).file;
  }

  const body = {
    prompt,
    model_id     : els.model.value,
    language     : els.lang.value,
    enable_search: els.search.checked,
    website_url  : els.url.value || null,
    file_path
  };

  try {
    const r = await fetch("/run/predict", {
      method:"POST",
      headers:{ "Content-Type":"application/json" },
      body: JSON.stringify(body)
    });
    if (!r.ok) throw new Error(r.statusText);
    const [code] = await r.json();            // returns [code, history]
    els.code.textContent = code;
    els.prev.srcdoc = (els.lang.value === "html")
      ? code
      : `<pre style="white-space:pre-wrap">${code.replace(/</g,"&lt;")}</pre>`;
    addHist(prompt);
  } catch (err) {
    console.error(err); alert("Generation failed (see console).");
  } finally {
    els.gen.disabled = false; els.gen.textContent = "Generate code";
  }
};