SPB Git

Repository Public

playground.js 43.2 KB 853 lines javascript

1/**2 * =============================================================================3 * SP BOUCHER | SOURCE FILE4 * =============================================================================5 * Project      : MacLustr LLM API6 * File         : web/assets/site/playground.js7 * Author       : Simon-Pierre Boucher | https://spboucher.ai8 * Contact      : admin@spboucher.ai9 * Copyright    : (c) 2026 Simon-Pierre Boucher10 * License      : None specified (no license file in this repository)11 * =============================================================================12 * END OF SP BOUCHER SOURCE HEADER13 * =============================================================================14 */1516// Playground: talks to the real API only (POST /v1/chat/completions). Nothing is simulated.17// The API key lives in sessionStorage only; the conversation is persisted in sessionStorage too.18import { $, $$, h, initChrome, session, fetchJSON, getStatus, fmtInt, fmtNum, announce, copyText, wireTabs, markTabs, ICONS } from "/assets/site/core.js";19import { snippet } from "/assets/site/codeblock.js";20import { highlight } from "/assets/site/highlight.js";21import { renderMarkdown } from "/assets/site/markdown.js";2223initChrome();2425const KEY_STORE = "mllm-pg-key";26const STATE_STORE = "mllm-pg-state";27const PLACEHOLDER = "YOUR_MACLUSTR_API_KEY";2829const DEFAULT_TOOLS = JSON.stringify([{30  type: "function",31  function: {32    name: "get_weather",33    description: "Current weather for a city.",34    parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] },35  },36}], null, 2);37const DEFAULT_SCHEMA = JSON.stringify({38  type: "object",39  properties: { answer: { type: "integer" } },40  required: ["answer"],41}, null, 2);4243// ------------------------------------------------------------------ state44const fresh = () => ({45  model: null, system: "", messages: [],46  params: { temperature: "", top_p: "", max_tokens: "", seed: "", stop: "" },47  thinking: false, toolsOn: false, toolChoice: "auto", toolsText: DEFAULT_TOOLS, jsonMode: "off", schemaText: DEFAULT_SCHEMA,48});49function load() {50  try {51    const saved = JSON.parse(session.get(STATE_STORE, "null"));52    if (!saved || !Array.isArray(saved.messages)) return fresh();53    const s = { ...fresh(), ...saved, params: { ...fresh().params, ...(saved.params || {}) } };54    for (const m of s.messages) { delete m.pending; }55    return s;56  } catch { return fresh(); }57}58const state = load();59function save() {60  session.set(STATE_STORE, JSON.stringify(state, (k, v) => (k === "pending" ? undefined : v)));61  scheduleExport();62}6364const auth = { mode: null, key: null, csrf: null, adminAvailable: false };65let models = [];            // [{id, display_name, context_window, max_output_tokens?, capabilities?}]66let modelsSource = "none";  // "v1" (authenticated) | "status" (public) | "none"67let running = null;         // {ctrl, msg}68let lastSent = null;69let uid = Date.now();70const nextId = () => `m${++uid}`;7172const el = {73  model: $("[data-model]"), messages: $("[data-messages]"), empty: $("[data-empty]"), emptyAuth: $("[data-empty-auth]"), scroll: $("[data-scroll]"),74  input: $("[data-input]"), send: $("[data-send]"), stop: $("[data-stop]"), composer: $("[data-composer]"), hint: $("[data-hint]"),75  system: $("[data-system]"), systemPreview: $("[data-system-preview]"), systemBox: $("[data-system-box]"),76  authBtn: $("[data-auth-open]"), authState: $("[data-auth-state]"), authDlg: $("#pg-auth"), key: $("[data-key]"), keyMsg: $("[data-key-msg]"),77  ctxMeter: $("[data-ctx-meter]"), ctxFill: $("[data-ctx-fill]"), ctxText: $("[data-ctx-text]"),78  panel: $("[data-panel]"), backdrop: $("[data-panel-backdrop]"), chat: $("[data-chat]"),79};8081// ------------------------------------------------------------------ auth + API82function authHeaders() {83  if (auth.mode === "key") return { Authorization: `Bearer ${auth.key}` };84  if (auth.mode === "admin") return { "X-CSRF-Token": auth.csrf || "" };85  return {};86}87async function refreshAdmin() {88  try {89    const r = await fetchJSON("/admin/api/session", { cache: "no-store" });90    auth.adminAvailable = !!(r.ok && r.body && r.body.authenticated === true);91    if (auth.adminAvailable) auth.csrf = r.body.csrf_token || null;92    else if (auth.mode === "admin") { auth.mode = null; auth.csrf = null; }93  } catch { auth.adminAvailable = false; }94  return auth.adminAvailable;95}96/** fetch with auth; with an admin session, a 403 csrf_failed refreshes the token once and retries. */97async function api(path, init = {}, retried = false) {98  const res = await fetch(path, { ...init, credentials: "same-origin", headers: { ...(init.headers || {}), ...authHeaders() } });99  if (res.status === 403 && auth.mode === "admin" && !retried) {100    const body = await res.clone().json().catch(() => null);101    if (body?.error?.code === "csrf_failed" && await refreshAdmin()) return api(path, init, true);102  }103  return res;104}105106async function errorFrom(res) {107  let body = null;108  try { body = await res.json(); } catch { body = null; }109  const e = body && body.error && typeof body.error === "object" ? body.error : {};110  const retry = Number(res.headers.get("retry-after"));111  return { status: res.status, code: e.code || null, type: e.type || null, message: e.message || `HTTP ${res.status}`, param: e.param || null,112    requestId: e.request_id || res.headers.get("x-request-id") || null, retryAfter: Number.isFinite(retry) && retry > 0 ? retry : null, at: Date.now() };113}114115function describeAuth() {116  if (auth.mode === "key") return `Key …${auth.key.slice(-4)}`;117  if (auth.mode === "admin") return "Admin session";118  return "Connect";119}120function renderAuth() {121  el.authState.textContent = describeAuth();122  el.authBtn.dataset.ok = auth.mode ? "true" : "false";123  el.authBtn.setAttribute("aria-label", auth.mode ? `Authentication: ${describeAuth()}. Change` : "Connect to the API");124  $("[data-admin-box]").hidden = !auth.adminAvailable;125  const cur = $("[data-current-auth]");126  cur.hidden = !auth.mode;127  $("[data-current-auth-text]").textContent = auth.mode === "key" ? `Using API key ending in ${auth.key.slice(-4)}` : "Using the admin session";128  el.emptyAuth.replaceChildren();129  if (!auth.mode) {130    el.emptyAuth.append("Connect with an API key to start. ", h("br"),131      h("button", { type: "button", class: "btn btn-primary btn-sm", onclick: openAuth }, "Connect"));132  }133  updateComposer();134}135function openAuth() {136  el.keyMsg.textContent = ""; el.keyMsg.className = "field-msg";137  el.key.value = "";138  refreshAdmin().then(renderAuth);139  el.authDlg.showModal();140  el.key.focus();141}142143async function loadModels() {144  let list = null;145  let failure = null;146  if (auth.mode) {147    try {148      const res = await api("/v1/models");149      if (res.ok) { const body = await res.json(); if (Array.isArray(body.data)) { list = body.data; modelsSource = "v1"; } }150      else failure = await errorFrom(res);151    } catch { failure = { status: 0, message: "Network error" }; }152  }153  if (!list) {154    const { data } = await getStatus();155    if (data && Array.isArray(data.models)) { list = data.models; modelsSource = "status"; } else { list = []; modelsSource = "none"; }156  }157  models = list;158  el.model.replaceChildren(...(models.length159    ? models.map((m) => h("option", { value: m.id }, m.display_name ? `${m.id} — ${m.display_name}` : m.id))160    : [h("option", { value: "" }, "No model available")]));161  el.model.disabled = !models.length;162  if (!models.some((m) => m.id === state.model)) state.model = models[0]?.id || null;163  if (state.model) el.model.value = state.model;164  renderModelFacts();165  renderContext();166  return failure;167}168const currentModel = () => models.find((m) => m.id === state.model) || null;169170function renderModelFacts() {171  const m = currentModel();172  const dl = $("[data-model-facts]");173  const row = (k, v) => h("div", null, h("dt", null, k), h("dd", null, v));174  if (!m) { dl.replaceChildren(row("Model", "—")); return; }175  const caps = m.capabilities || null;176  dl.replaceChildren(177    row("Context window", typeof m.context_window === "number" ? `${fmtInt(m.context_window)} tokens` : "—"),178    row("Max output", typeof m.max_output_tokens === "number" ? `${fmtInt(m.max_output_tokens)} tokens` : "—"),179    row("Quantization", m.quantization || "—"),180    row("Capabilities", caps ? Object.entries(caps).filter(([, v]) => v).map(([k, v]) => (v === true ? k : `${k}: ${v}`)).join(", ") || "—" : "—"),181    row("Source", modelsSource === "v1" ? "GET /v1/models" : modelsSource === "status" ? "GET /api/status (connect for details)" : "—"));182  const note = $("[data-thinking-note]");183  note.textContent = caps && caps.thinking === false184    ? "This model does not report the thinking capability: the flag is sent but ignored by the server."185    : "Returned as reasoning_content when the model supports it.";186}187188// ------------------------------------------------------------------ request building189function parseJSONField(text, kind) {190  let value;191  try { value = JSON.parse(text); } catch (e) { return { error: `Invalid JSON: ${e.message}` }; }192  if (kind === "tools") {193    if (!Array.isArray(value) || !value.length) return { error: "Expected a non-empty JSON array of tool definitions." };194    for (const t of value) {195      if (!t || t.type !== "function" || !t.function || typeof t.function.name !== "string") {196        return { error: 'Each tool needs {"type": "function", "function": {"name": …}}.' };197      }198    }199  } else if (value === null || typeof value !== "object" || Array.isArray(value)) return { error: "Expected a JSON Schema object." };200  return { value };201}202const toolsActive = () => state.toolsOn && state.jsonMode === "off";203204function pendingToolCalls() {205  const last = state.messages[state.messages.length - 1];206  return last && last.role === "assistant" && last.tool_calls?.length && !last.error ? last : null;207}208209/** Builds the exact body Send would post. `draft` = unsent composer text. Returns {body} or {error}. */210function buildBody(draft = "") {211  const messages = [];212  if (state.system.trim()) messages.push({ role: "system", content: state.system });213  const list = state.messages;214  for (let i = 0; i < list.length; i++) {215    const m = list[i];216    if (m.role === "user") messages.push({ role: "user", content: m.content });217    else if (m.role === "tool") messages.push({ role: "tool", tool_call_id: m.tool_call_id, content: m.content });218    else if (m.role === "assistant") {219      const calls = m.tool_calls?.length ? m.tool_calls : null;220      // Tool calls are only replayed when their results follow; otherwise the turn is sent as plain text.221      const answered = calls && list[i + 1]?.role === "tool";222      if (calls && answered) {223        messages.push({ role: "assistant", content: m.content || null,224          tool_calls: calls.map((c) => ({ id: c.id, type: "function", function: { name: c.function.name, arguments: c.function.arguments } })) });225      } else if (m.content) messages.push({ role: "assistant", content: m.content });226    }227  }228  if (draft.trim()) messages.push({ role: "user", content: draft });229  if (!state.model) return { error: "No model is available." };230  const body = { model: state.model, messages };231232  const p = state.params;233  const numeric = (key, int) => {234    if (p[key] === "" || p[key] === null || p[key] === undefined) return;235    const v = Number(p[key]);236    if (Number.isFinite(v)) body[key] = int ? Math.trunc(v) : v;237  };238  numeric("temperature"); numeric("top_p"); numeric("max_tokens", true); numeric("seed", true);239  const stops = String(p.stop || "").split("\n").map((s) => s.replace(/\r$/, "")).filter((s) => s.length);240  if (stops.length) body.stop = stops.length === 1 ? stops[0] : stops.slice(0, 8);241  if (state.thinking) body.enable_thinking = true;242243  if (toolsActive()) {244    const t = parseJSONField(state.toolsText, "tools");245    if (t.error) return { error: `Tools: ${t.error}`, tab: "tools" };246    body.tools = t.value;247    body.tool_choice = state.toolChoice;248  }249  if (state.jsonMode === "json_object") body.response_format = { type: "json_object" };250  else if (state.jsonMode === "json_schema") {251    const s = parseJSONField(state.schemaText, "schema");252    if (s.error) return { error: `JSON Schema: ${s.error}`, tab: "json" };253    body.response_format = { type: "json_schema", json_schema: { name: "response", schema: s.value } };254  }255  if (state.jsonMode === "off") { body.stream = true; body.stream_options = { include_usage: true }; }256  else body.stream = false;257  return { body };258}259260// ------------------------------------------------------------------ sending261async function send({ regenerate = false } = {}) {262  if (running) return;263  if (!auth.mode) { openAuth(); return; }264  const text = el.input.value;265  const pending = pendingToolCalls();266  if (!regenerate) {267    if (pending) submitToolResults(pending, false);268    if (text.trim()) state.messages.push({ id: nextId(), role: "user", content: text });269    else if (!pending) return;270  }271  const built = buildBody();272  if (built.error) {273    if (!regenerate && text.trim()) state.messages.pop();274    announce(built.error);275    showFormError(built);276    return;277  }278  if (!built.body.messages.some((m) => m.role !== "system")) return;279  el.input.value = ""; autosize();280  const msg = { id: nextId(), role: "assistant", content: "", reasoning: "", tool_calls: [], pending: true };281  state.messages.push(msg);282  const ctrl = new AbortController();283  running = { ctrl, msg };284  lastSent = built.body;285  renderMessages(); updateComposer(); scrollToEnd(true);286287  const t0 = performance.now();288  let tFirst = null;289  const mark = () => { if (tFirst === null) tFirst = performance.now(); };290  try {291    const res = await api("/v1/chat/completions", { method: "POST", signal: ctrl.signal,292      headers: { "Content-Type": "application/json", Accept: built.body.stream ? "text/event-stream" : "application/json" }, body: JSON.stringify(built.body) });293    msg.requestId = res.headers.get("x-request-id");294    const rl = (n) => { const v = res.headers.get(n); return v === null ? null : Number(v); };295    msg.rate = { reqLeft: rl("x-ratelimit-remaining-requests"), reqLimit: rl("x-ratelimit-limit-requests"), tokLeft: rl("x-ratelimit-remaining-tokens"), tokLimit: rl("x-ratelimit-limit-tokens") };296    if (!res.ok) msg.error = await errorFrom(res);297    else if (built.body.stream) await readStream(res, msg, mark);298    else {299      const data = await res.json();300      const m = data.choices?.[0]?.message || {};301      msg.content = m.content || "";302      msg.reasoning = m.reasoning_content || "";303      msg.tool_calls = (m.tool_calls || []).map((c) => ({ id: c.id, function: { name: c.function?.name || "", arguments: c.function?.arguments || "" } }));304      msg.finish = data.choices?.[0]?.finish_reason || null;305      msg.usage = data.usage || null;306    }307  } catch (e) {308    if (e.name === "AbortError") msg.stopped = true;309    else msg.error = { status: 0, code: "network_error", type: "client", message: "The request could not be completed: the connection failed or was interrupted.", client: true, at: Date.now() };310  }311  const tEnd = performance.now();312  delete msg.pending;313  msg.streamed = built.body.stream;314  msg.metrics = computeMetrics(msg, t0, tFirst, tEnd);315  running = null;316  save(); renderMessages(); updateComposer(); renderContext(); scrollToEnd();317  announce(msg.error ? `Error: ${errorTitle(msg.error)}` : msg.stopped ? "Generation stopped" : "Response complete");318}319320function computeMetrics(msg, t0, tFirst, tEnd) {321  const u = msg.usage;322  const out = { totalMs: tEnd - t0, ttftMs: msg.streamed && tFirst !== null ? tFirst - t0 : null, tps: null, tpsKind: null };323  if (u && typeof u.completion_tokens === "number" && u.completion_tokens > 0) {324    // Below 20 ms the generation window is too short for a meaningful rate (the whole answer arrived at once).325    if (msg.streamed && tFirst !== null && tEnd - tFirst >= 20) { out.tps = u.completion_tokens / ((tEnd - tFirst) / 1000); out.tpsKind = "generation"; }326    else if (!msg.streamed && tEnd - t0 > 0) { out.tps = u.completion_tokens / ((tEnd - t0) / 1000); out.tpsKind = "overall"; }327  }328  return out;329}330331async function readStream(res, msg, mark) {332  const reader = res.body.getReader();333  const decoder = new TextDecoder();334  let buffer = "";335  let done = false;336  const handle = (block) => {337    const data = [];338    for (const line of block.split("\n")) {339      if (!line || line.startsWith(":")) continue; // SSE comment, e.g. ": keepalive"340      if (line.startsWith("data:")) data.push(line.slice(5).replace(/^ /, ""));341    }342    if (!data.length) return;343    const payload = data.join("\n");344    if (payload === "[DONE]") { done = true; return; }345    let evt;346    try { evt = JSON.parse(payload); } catch { return; }347    if (evt.error) {348      const e = evt.error;349      msg.error = { status: 200, inStream: true, code: e.code || null, type: e.type || null, message: e.message || "Stream error", requestId: e.request_id || msg.requestId, at: Date.now() };350      return;351    }352    if (evt.usage) msg.usage = evt.usage;353    const choice = evt.choices && evt.choices[0];354    if (!choice) return;355    const d = choice.delta || {};356    if (d.reasoning_content) { mark(); msg.reasoning += d.reasoning_content; }357    if (d.content) { mark(); msg.content += d.content; }358    for (const tc of d.tool_calls || []) {359      mark();360      const existing = typeof tc.index === "number" ? msg.tool_calls[tc.index] : null;361      if (existing && !tc.id) existing.function.arguments += tc.function?.arguments || ""; // tolerate fragmented calls362      else msg.tool_calls.push({ id: tc.id, function: { name: tc.function?.name || "", arguments: tc.function?.arguments || "" } });363    }364    if (choice.finish_reason) msg.finish = choice.finish_reason;365    queueStreamRender(msg);366  };367  for (;;) {368    const { value, done: eof } = await reader.read();369    if (value) buffer += decoder.decode(value, { stream: true }).replace(/\r\n/g, "\n");370    let i;371    while ((i = buffer.indexOf("\n\n")) >= 0) { handle(buffer.slice(0, i)); buffer = buffer.slice(i + 2); }372    if (eof) break;373  }374  if (buffer.trim()) handle(buffer);375  if (!done && !msg.error) {376    msg.error = { status: 200, inStream: true, client: true, code: "incomplete_stream", type: "client",377      message: "The connection closed before the [DONE] marker. The response above is incomplete.", requestId: msg.requestId, at: Date.now() };378  }379}380381function stop() { if (running) running.ctrl.abort(); }382383// ------------------------------------------------------------------ conversation actions384function submitToolResults(asst, thenSend = true) {385  const i = state.messages.indexOf(asst);386  const results = asst.tool_calls.map((c) => ({ id: nextId(), role: "tool", tool_call_id: c.id, name: c.function.name, content: (asst.toolDrafts && asst.toolDrafts[c.id]) || "" }));387  delete asst.toolDrafts;388  state.messages.splice(i + 1, 0, ...results);389  save();390  if (thenSend) { renderMessages(); send({ regenerate: true }); }391}392function regenerate(msg) {393  if (running) return;394  const i = state.messages.indexOf(msg);395  if (i < 0) return;396  state.messages.splice(i); // drop this assistant turn and everything after it397  save();398  send({ regenerate: true });399}400function editAndResend(msg, text) {401  if (running || !text.trim()) return;402  const i = state.messages.indexOf(msg);403  msg.content = text;404  state.messages.splice(i + 1);405  save();406  send({ regenerate: true });407}408function clearAll() {409  if (running) stop();410  state.messages = [];411  lastSent = null;412  save(); renderMessages(); renderContext(); updateComposer();413  announce("Conversation cleared");414  el.input.focus();415}416417// ------------------------------------------------------------------ rendering418const ERROR_TITLES = {419  401: "Authentication failed", 403: "Not allowed", 404: "Not found", 400: "The request was rejected", 413: "Request too large",420  422: "JSON validation failed", 429: "Rate limit reached", 500: "Server error", 502: "Inference engine error", 503: "Service unavailable", 504: "Timed out",421};422function errorTitle(e) {423  if (e.inStream) return e.client ? "The stream ended unexpectedly" : "The stream failed mid-response";424  if (e.status === 0) return "Network error";425  if (e.code === "insufficient_quota") return "Token quota exhausted";426  if (e.code === "concurrency_limit_exceeded") return "Too many simultaneous requests";427  if (e.status === 503 && e.code === "engine_overloaded") return "Server at capacity";428  return ERROR_TITLES[e.status] || `HTTP ${e.status}`;429}430function errorHelp(e) {431  if (e.status === 401) return "The API key is missing, invalid, revoked or expired.";432  if (e.status === 429) return "This is a limit of your key, not of the server. Wait, then send again.";433  if (e.status === 503) return "The server cannot take the request right now. Your request is fine: retry after the delay.";434  if (e.status === 422) return "The model did not produce valid JSON, even after the server's automatic repair attempt.";435  if (e.inStream && !e.client) return "The HTTP status was already 200 when the failure happened, so the API reported it as an event in the stream.";436  return null;437}438439function errorCard(e, msg) {440  const warn = e.status === 429 || e.status === 503;441  const box = h("div", { class: `err ${warn ? "warn" : ""}`, role: "alert", html: ICONS.alert });442  const body = h("div", null, h("strong", null, `${errorTitle(e)}${e.status && !e.inStream ? ` · ${e.status}` : ""}`));443  const help = errorHelp(e);444  if (help) body.append(h("p", null, help));445  body.append(h("p", null, e.client ? e.message : `Server message: ${e.message}`));446  const meta = [e.code ? `code ${e.code}` : null, e.type && !e.client ? `type ${e.type}` : null, e.param ? `param ${e.param}` : null, e.requestId ? `request ${e.requestId}` : null].filter(Boolean);447  if (meta.length) body.append(h("p", { class: "err-meta" }, meta.join(" · ")));448  const actions = h("div", { class: "err-actions" });449  if (e.status === 401) actions.append(h("button", { type: "button", class: "btn btn-sm", onclick: openAuth }, "Change key"));450  const retryBtn = h("button", { type: "button", class: "btn btn-sm", onclick: () => regenerate(msg) }, "Retry");451  if (e.retryAfter) {452    const until = e.at + e.retryAfter * 1000;453    const label = h("span", { class: "tabular" });454    const tick = () => {455      const left = Math.ceil((until - Date.now()) / 1000);456      if (!box.isConnected) return;457      if (left > 0) { retryBtn.disabled = true; label.textContent = `Retry-After: ${e.retryAfter} s · you can retry in ${left} s`; setTimeout(tick, 500); }458      else { retryBtn.disabled = false; label.textContent = `Retry-After: ${e.retryAfter} s · elapsed, you can retry now`; }459    };460    body.append(h("p", null, label));461    setTimeout(tick, 0);462  }463  if (e.status !== 401 && e.status !== 400 && e.status !== 403 && e.status !== 404) actions.append(retryBtn);464  if (actions.children.length) body.append(actions);465  box.append(body);466  return box;467}468469function metricsRow(msg) {470  const m = msg.metrics;471  if (!m || (msg.error && !msg.usage && !msg.content)) return null;472  const u = msg.usage || {};473  const item = (label, value, title) => h("li", { title: title || null }, label, " ", h("b", null, value));474  const items = [];475  items.push(item("TTFT", m.ttftMs === null ? "—" : `${fmtNum(m.ttftMs, 0)} ms`,476    msg.streamed ? "Time from sending the request to the first content, reasoning or tool-call delta, measured in this browser" : "Not measurable for a non-streaming response"));477  items.push(item("Speed", m.tps === null ? "—" : `${fmtNum(m.tps, 1)} tok/s`,478    m.tpsKind === "overall" ? "completion_tokens ÷ total request time (non-streaming: includes queueing and prompt processing)"479      : m.tps === null && msg.streamed && typeof u.completion_tokens === "number" ? "The whole completion arrived in a single instant: the generation window is too short to measure a rate"480        : "completion_tokens from the usage chunk ÷ time between the first delta and the end of the stream"));481  items.push(item("Prompt", typeof u.prompt_tokens === "number" ? fmtInt(u.prompt_tokens) : "—", "usage.prompt_tokens"));482  items.push(item("Completion", typeof u.completion_tokens === "number" ? fmtInt(u.completion_tokens) : "—", "usage.completion_tokens"));483  const cached = u.prompt_tokens_details?.cached_tokens;484  items.push(item("Cached", typeof cached === "number" ? fmtInt(cached) : "—", "usage.prompt_tokens_details.cached_tokens (prefix cache)"));485  items.push(item("Total", `${fmtNum(m.totalMs / 1000, 2)} s`, "Wall-clock time of the request, measured in this browser"));486  if (msg.finish) items.push(item("Finish", msg.finish, "finish_reason"));487  if (!msg.streamed) items.push(item("Mode", "non-streaming"));488  return h("ul", { class: "metrics", "aria-label": "Response metrics" }, items);489}490491function prettyArgs(args) {492  try { return JSON.stringify(JSON.parse(args), null, 2); } catch { return args; }493}494495function assistantBody(msg) {496  const parts = [];497  const live = !!msg.pending;498  if (msg.reasoning) {499    parts.push(h("details", { class: "reasoning", open: live && !msg.content ? true : null },500      h("summary", null, "Reasoning", h("small", null, "reasoning_content")), h("div", null, msg.reasoning)));501  }502  if (msg.content) {503    const md = h("div", { class: "msg-body md" }, renderMarkdown(msg.content));504    if (live) (md.lastElementChild?.matches("p, li, ul, ol") ? md.lastElementChild : md).classList.add("caret");505    parts.push(md);506  } else if (live && !msg.tool_calls.length) {507    parts.push(h("div", { class: "msg-body" }, h("span", { class: "msg-wait" }, h("i"), h("i"), h("i"), msg.reasoning ? "Reasoning…" : "Waiting for the first token…")));508  } else if (!live && !msg.tool_calls.length && !msg.error) {509    parts.push(h("div", { class: "msg-body muted" }, msg.stopped ? "Stopped before any content arrived." : "The model returned an empty message."));510  }511  if (msg.tool_calls.length) {512    const i = state.messages.indexOf(msg);513    const answered = state.messages[i + 1]?.role === "tool";514    const open = !live && !answered && !msg.error && i === state.messages.length - 1;515    msg.toolDrafts ||= {};516    for (const c of msg.tool_calls) {517      const card = h("div", { class: "tool-card" },518        h("div", { class: "tool-card-head" }, h("span", { class: "role-tag tool" }, "Tool call"), h("strong", null, `${c.function.name}()`), h("small", null, c.id || "")),519        h("pre", { tabindex: "0", "aria-label": "Arguments" }, h("code", { html: highlight(prettyArgs(c.function.arguments), "json") })));520      if (open) {521        const id = `tr-${msg.id}-${c.id}`;522        const ta = h("textarea", { id, rows: "2", placeholder: '{"temperature_c": -4, "conditions": "light snow"}', spellcheck: "false" });523        ta.value = msg.toolDrafts[c.id] || "";524        ta.addEventListener("input", () => { msg.toolDrafts[c.id] = ta.value; save(); });525        card.append(h("div", { class: "field" }, h("label", { for: id }, "Tool result", h("span", null, "what your function returned")), ta));526      }527      parts.push(card);528    }529    if (open) {530      parts.push(h("div", { class: "tool-submit" },531        h("button", { type: "button", class: "btn btn-primary btn-sm", onclick: () => submitToolResults(msg) }, "Send tool results"),532        h("small", null, "Adds one tool message per call, then asks the model to continue.")));533    }534  }535  if (msg.stopped) parts.push(h("p", { class: "stopped-note" }, "Stopped by you. The connection was closed, so the server stopped generating; no usage chunk was received."));536  if (msg.error) parts.push(errorCard(msg.error, msg));537  const metrics = !live ? metricsRow(msg) : null;538  if (metrics) parts.push(metrics);539  return parts;540}541542function actionBtn(label, onclick) { return h("button", { type: "button", onclick }, label); }543function copyAction(getText) {544  const btn = h("button", { type: "button" }, h("span", { dataset: { label: "Copy" } }, "Copy"));545  btn.addEventListener("click", () => copyText(getText(), btn));546  return btn;547}548549function messageNode(msg) {550  const li = h("li", { class: `msg ${msg.role}`, dataset: { id: msg.id } });551  const actions = h("div", { class: "msg-actions" });552  const head = h("div", { class: "msg-head" }, h("span", { class: `role-tag ${msg.role}` }, msg.role === "tool" ? `Tool · ${msg.name || "result"}` : msg.role), actions);553  li.append(head);554  if (msg.role === "user") {555    const body = h("div", { class: "msg-body plain" }, msg.content);556    li.append(body);557    actions.append(actionBtn("Edit", () => {558      if (running) return;559      const ta = h("textarea", { rows: "3", "aria-label": "Edit message" });560      ta.value = msg.content;561      const box = h("div", { class: "edit-box msg-body" }, ta, h("div", { class: "edit-actions" },562        h("button", { type: "button", class: "btn btn-primary btn-sm", onclick: () => editAndResend(msg, ta.value) }, "Save and resend"),563        h("button", { type: "button", class: "btn btn-sm", onclick: () => renderMessages() }, "Cancel")));564      body.replaceWith(box);565      ta.focus();566    }));567    actions.append(copyAction(() => msg.content));568  } else if (msg.role === "tool") {569    li.append(h("div", { class: "msg-body plain" }, msg.content || h("span", { class: "muted" }, "(empty result)")),570      h("p", { class: "stopped-note" }, `tool_call_id ${msg.tool_call_id}`));571  } else {572    const holder = h("div", { class: "asst", dataset: { holder: "" } }, assistantBody(msg));573    li.append(holder);574    if (!msg.pending) {575      if (msg.content) actions.append(copyAction(() => msg.content));576      actions.append(actionBtn("Regenerate", () => regenerate(msg)));577    }578  }579  return li;580}581582function renderMessages() {583  el.messages.replaceChildren(...state.messages.map(messageNode));584  el.empty.hidden = state.messages.length > 0;585}586let streamQueued = false;587function queueStreamRender(msg) {588  if (streamQueued) return;589  streamQueued = true;590  requestAnimationFrame(() => {591    streamQueued = false;592    const holder = el.messages.querySelector(`[data-id="${msg.id}"] [data-holder]`);593    if (!holder) return;594    const stick = nearEnd();595    const open = holder.querySelector("details.reasoning")?.open;596    holder.replaceChildren(...assistantBody(msg));597    const d = holder.querySelector("details.reasoning");598    if (d && open !== undefined && msg.content) d.open = open;599    if (stick) scrollToEnd();600  });601}602const nearEnd = () => el.scroll.scrollHeight - el.scroll.scrollTop - el.scroll.clientHeight < 120;603function scrollToEnd(force) { if (force || nearEnd()) el.scroll.scrollTop = el.scroll.scrollHeight; }604605function renderContext() {606  const m = currentModel();607  const win = m && typeof m.context_window === "number" ? m.context_window : null;608  let used = null;609  for (let i = state.messages.length - 1; i >= 0; i--) {610    const u = state.messages[i].usage;611    if (u && typeof u.total_tokens === "number") { used = u.total_tokens; break; }612  }613  const pct = used !== null && win ? Math.min(100, (used / win) * 100) : 0;614  el.ctxFill.style.width = `${pct}%`;615  el.ctxMeter.setAttribute("aria-valuenow", String(Math.round(pct)));616  el.ctxMeter.setAttribute("aria-valuetext", used !== null && win ? `${used} of ${win} tokens` : "No usage reported yet");617  el.ctxMeter.classList.toggle("warn", pct >= 75 && pct < 90);618  el.ctxMeter.classList.toggle("full", pct >= 90);619  el.ctxText.textContent = `${used === null ? "—" : fmtInt(used)} / ${win ? fmtInt(win) : "—"}${used !== null && win ? ` · ${pct < 10 ? fmtNum(pct, 1) : Math.round(pct)}%` : ""}`;620}621622function updateComposer() {623  const busy = !!running;624  el.send.hidden = busy; el.stop.hidden = !busy;625  const pending = pendingToolCalls();626  el.input.placeholder = !auth.mode ? "Connect to send a message" : pending ? "Tool results, or a message" : "Send a message";627  el.hint.textContent = busy ? "Generating… Esc or the stop button cancels the request"628    : state.jsonMode !== "off" ? `JSON mode (${state.jsonMode}) · non-streaming, validated by the server`629      : toolsActive() ? "Tools enabled · Enter to send · Shift + Enter for a new line" : "Enter to send · Shift + Enter for a new line";630}631function autosize() {632  el.input.style.height = "auto";633  el.input.style.height = `${Math.min(el.input.scrollHeight, window.innerHeight * 0.4)}px`;634}635636function showFormError(built) {637  if (built.tab) selectTab(built.tab);638  if (window.matchMedia("(max-width: 860px)").matches && built.tab) openPanel();639  const target = built.tab === "tools" ? $("[data-tools-msg]") : built.tab === "json" ? $("[data-schema-msg]") : null;640  if (target) { target.textContent = built.error; target.className = "field-msg bad"; }641}642643// ------------------------------------------------------------------ settings panel644const tablist = $(".pg-tabs");645function selectTab(value) {646  markTabs(tablist, value);647  for (const p of $$('[role="tabpanel"]', el.panel)) p.hidden = p.id !== `pp-${value}`;648  if (value === "request") renderExport();649}650wireTabs(tablist, selectTab);651new ResizeObserver(() => markTabs(tablist, $('[aria-selected="true"]', tablist).dataset.value)).observe(tablist);652653const sheet = window.matchMedia("(max-width: 860px)");654let panelOpener = null;655function openPanel() {656  if (!sheet.matches || el.panel.classList.contains("open")) return;657  panelOpener = document.activeElement;658  el.panel.classList.add("open");659  el.backdrop.hidden = false;660  el.chat.inert = true;661  $(".site-header").inert = true;662  el.panel.setAttribute("role", "dialog"); el.panel.setAttribute("aria-modal", "true");663  $("[data-panel-open]").setAttribute("aria-expanded", "true");664  markTabs(tablist, $('[aria-selected="true"]', tablist).dataset.value);665  $('[role="tab"][aria-selected="true"]', el.panel).focus();666}667function closePanel() {668  if (!el.panel.classList.contains("open")) return;669  el.panel.classList.remove("open");670  el.backdrop.hidden = true;671  el.chat.inert = false;672  $(".site-header").inert = false;673  el.panel.removeAttribute("role"); el.panel.removeAttribute("aria-modal");674  $("[data-panel-open]").setAttribute("aria-expanded", "false");675  (panelOpener || $("[data-panel-open]")).focus();676}677$("[data-panel-open]").addEventListener("click", openPanel);678$("[data-panel-close]").addEventListener("click", closePanel);679el.backdrop.addEventListener("click", closePanel);680sheet.addEventListener("change", () => { if (!sheet.matches) closePanel(); });681el.panel.addEventListener("keydown", (e) => {682  if (e.key !== "Tab" || !el.panel.classList.contains("open")) return;683  const f = $$('button, input, select, textarea, [tabindex="0"]', el.panel).filter((x) => !x.disabled && x.offsetParent !== null);684  if (!f.length) return;685  if (e.shiftKey && document.activeElement === f[0]) { e.preventDefault(); f[f.length - 1].focus(); }686  else if (!e.shiftKey && document.activeElement === f[f.length - 1]) { e.preventDefault(); f[0].focus(); }687});688689function validateEditors() {690  const tm = $("[data-tools-msg]");691  if (state.toolsOn) {692    const t = parseJSONField(state.toolsText, "tools");693    tm.textContent = t.error || `${t.value.length} tool${t.value.length === 1 ? "" : "s"} defined: ${t.value.map((x) => x.function.name).join(", ")}`;694    tm.className = `field-msg ${t.error ? "bad" : "good"}`;695  } else { tm.textContent = "Tools are not sent."; tm.className = "field-msg"; }696  const sm = $("[data-schema-msg]");697  if (state.jsonMode === "json_schema") {698    const s = parseJSONField(state.schemaText, "schema");699    sm.textContent = s.error || "Valid JSON. The server also checks that it is a valid JSON Schema.";700    sm.className = `field-msg ${s.error ? "bad" : "good"}`;701  }702  $("[data-schema-field]").hidden = state.jsonMode !== "json_schema";703  $("[data-tools-conflict]").hidden = !(state.toolsOn && state.jsonMode !== "off");704}705706function bindSettings() {707  for (const input of $$("[data-param]")) {708    input.value = state.params[input.dataset.param] ?? "";709    input.addEventListener("input", () => { state.params[input.dataset.param] = input.value; save(); });710  }711  const thinking = $("[data-thinking]");712  thinking.checked = state.thinking;713  thinking.addEventListener("change", () => { state.thinking = thinking.checked; save(); });714  const toolsOn = $("[data-tools-on]"), tools = $("[data-tools]"), choice = $("[data-tool-choice]");715  toolsOn.checked = state.toolsOn; tools.value = state.toolsText; choice.value = state.toolChoice;716  toolsOn.addEventListener("change", () => { state.toolsOn = toolsOn.checked; validateEditors(); updateComposer(); save(); });717  tools.addEventListener("input", () => { state.toolsText = tools.value; validateEditors(); save(); });718  choice.addEventListener("change", () => { state.toolChoice = choice.value; save(); });719  $("[data-tools-reset]").addEventListener("click", () => { tools.value = state.toolsText = DEFAULT_TOOLS; validateEditors(); save(); });720  const schema = $("[data-schema]");721  schema.value = state.schemaText;722  schema.addEventListener("input", () => { state.schemaText = schema.value; validateEditors(); save(); });723  for (const r of $$("[data-json-mode]")) {724    r.checked = r.value === state.jsonMode;725    r.addEventListener("change", () => { if (r.checked) { state.jsonMode = r.value; validateEditors(); updateComposer(); save(); } });726  }727  el.system.value = state.system;728  const preview = () => { el.systemPreview.textContent = state.system.trim() ? state.system.trim().replace(/\s+/g, " ") : "No system prompt"; };729  el.system.addEventListener("input", () => { state.system = el.system.value; preview(); save(); });730  preview();731  el.model.addEventListener("change", () => { state.model = el.model.value; renderModelFacts(); renderContext(); save(); });732  validateEditors();733}734735// ------------------------------------------------------------------ export (cURL / Python / JSON)736let reqSource = "next", reqFormat = "curl", exportTimer = null;737function toPython(v, indent = 0) {738  const pad = "    ".repeat(indent), pad1 = "    ".repeat(indent + 1);739  if (v === null) return "None";740  if (v === true) return "True";741  if (v === false) return "False";742  if (typeof v === "number") return String(v);743  if (typeof v === "string") return JSON.stringify(v);744  if (Array.isArray(v)) return v.length ? `[\n${v.map((x) => pad1 + toPython(x, indent + 1)).join(",\n")},\n${pad}]` : "[]";745  const keys = Object.keys(v);746  return keys.length ? `{\n${keys.map((k) => `${pad1}${JSON.stringify(k)}: ${toPython(v[k], indent + 1)}`).join(",\n")},\n${pad}}` : "{}";747}748function exportText(body, format) {749  const base = `${location.origin}/v1`;750  if (format === "json") return JSON.stringify(body, null, 2);751  if (format === "curl") {752    return [`curl ${body.stream ? "--no-buffer " : ""}${base}/chat/completions \\`, `  -H "Authorization: Bearer ${PLACEHOLDER}" \\`, `  -H "Content-Type: application/json" \\`,753      `  -d '${JSON.stringify(body, null, 2).replace(/'/g, "'\\''")}'`].join("\n");754  }755  const { enable_thinking: thinking, ...std } = body;756  const args = Object.entries(std).map(([k, v]) => `    ${k}=${toPython(v, 1)},`);757  if (thinking !== undefined) args.push(`    extra_body={"enable_thinking": ${toPython(thinking)}},  # MacLustr extension`);758  const tail = body.stream759    ? ["for chunk in response:", "    if chunk.choices and chunk.choices[0].delta.content:", '        print(chunk.choices[0].delta.content, end="", flush=True)', "    if chunk.usage:", "        print(chunk.usage)"]760    : ["print(response.choices[0].message.content)", "print(response.usage)"];761  return ["from openai import OpenAI", "", `client = OpenAI(base_url="${base}", api_key="${PLACEHOLDER}")`, "", "response = client.chat.completions.create(", ...args, ")", "", ...tail].join("\n");762}763function renderExport() {764  const out = $("[data-req-out]");765  if ($("#pp-request").hidden) return;766  for (const b of $$("[data-req-source]")) b.setAttribute("aria-pressed", b.dataset.reqSource === reqSource ? "true" : "false");767  for (const b of $$("[data-req-format]")) b.setAttribute("aria-pressed", b.dataset.reqFormat === reqFormat ? "true" : "false");768  let body = null, problem = null;769  if (reqSource === "last") { body = lastSent; if (!body) problem = "No request has been sent in this tab yet."; }770  else { const built = buildBody(el.input.value); body = built.body || null; problem = built.error || null;771    if (body && !body.messages.some((m) => m.role !== "system")) problem = "Type a message to preview the next request."; }772  if (problem && !body) { out.replaceChildren(h("p", { class: "pg-note" }, problem)); return; }773  const title = reqSource === "last" ? "Last request sent" : "Request that Send would post now";774  const block = snippet(exportText(body, reqFormat), reqFormat === "curl" ? "bash" : reqFormat, title);775  out.replaceChildren(...(problem ? [h("p", { class: "pg-note" }, problem)] : []), block);776}777function scheduleExport() { clearTimeout(exportTimer); exportTimer = setTimeout(renderExport, 200); }778for (const b of $$("[data-req-source]")) b.addEventListener("click", () => { reqSource = b.dataset.reqSource; renderExport(); });779for (const b of $$("[data-req-format]")) b.addEventListener("click", () => { reqFormat = b.dataset.reqFormat; renderExport(); });780781// ------------------------------------------------------------------ events782el.composer.addEventListener("submit", (e) => { e.preventDefault(); send(); });783el.input.addEventListener("keydown", (e) => {784  if (e.key === "Enter" && !e.shiftKey && !e.isComposing && !window.matchMedia("(pointer: coarse)").matches) { e.preventDefault(); send(); }785});786el.input.addEventListener("input", () => { autosize(); scheduleExport(); });787el.stop.addEventListener("click", stop);788document.addEventListener("keydown", (e) => {789  if (e.key !== "Escape") return;790  if (el.panel.classList.contains("open")) closePanel();791  else if (running && !el.authDlg.open) stop();792});793$("[data-clear]").addEventListener("click", clearAll);794el.authBtn.addEventListener("click", openAuth);795el.authDlg.addEventListener("click", (e) => { if (e.target === el.authDlg) el.authDlg.close(); });796797$("[data-key-form]").addEventListener("submit", async (e) => {798  e.preventDefault();799  const key = el.key.value.trim();800  if (!key) { el.keyMsg.textContent = "Paste an API key."; el.keyMsg.className = "field-msg bad"; return; }801  const previous = { ...auth };802  auth.mode = "key"; auth.key = key;803  el.keyMsg.textContent = "Checking the key with GET /v1/models…"; el.keyMsg.className = "field-msg";804  const failure = await loadModels();805  if (failure) {806    Object.assign(auth, previous);807    el.keyMsg.textContent = failure.status === 401 ? "The API rejected this key (401)." : failure.status === 429 ? "Too many attempts (429). Wait before trying again."808      : failure.status === 0 ? "The API could not be reached." : `The API answered ${failure.status}${failure.code ? ` (${failure.code})` : ""}.`;809    el.keyMsg.className = "field-msg bad";810    await loadModels();811    renderAuth();812    return;813  }814  session.set(KEY_STORE, key);815  el.key.value = "";816  renderAuth();817  el.authDlg.close();818  announce("Connected with API key");819  el.input.focus();820});821$("[data-use-admin]").addEventListener("click", async () => {822  if (!(await refreshAdmin())) { renderAuth(); return; }823  auth.mode = "admin"; auth.key = null;824  session.remove(KEY_STORE);825  await loadModels();826  renderAuth();827  el.authDlg.close();828  announce("Connected with the admin session");829});830$("[data-forget]").addEventListener("click", async () => {831  auth.mode = null; auth.key = null;832  session.remove(KEY_STORE);833  await loadModels();834  renderAuth();835  announce("Disconnected");836});837838// ------------------------------------------------------------------ boot839(async function boot() {840  bindSettings();841  renderMessages();842  autosize();843  const stored = session.get(KEY_STORE);844  if (stored) { auth.mode = "key"; auth.key = stored; }845  await refreshAdmin();846  renderAuth();847  const failure = await loadModels();848  if (failure && failure.status === 401 && auth.mode === "key") { auth.mode = null; auth.key = null; session.remove(KEY_STORE); await loadModels(); }849  renderAuth();850  selectTab("params");851  scrollToEnd(true);852})();853
Files main
Menu

Theme