diff --git a/internal/web/admin_mirror.go b/internal/web/admin_mirror.go index bec61ec..da50e16 100644 --- a/internal/web/admin_mirror.go +++ b/internal/web/admin_mirror.go @@ -2,6 +2,7 @@ package web import ( "context" + "encoding/json" "errors" "fmt" "html/template" @@ -322,14 +323,26 @@ func (p *AccountPages) mirrorAction(m MirrorAdmin, return } msg, err := do(r.Context(), admin, r) + // Asked for from the page's own script — a fetch started from a + // repository opened in the list — the answer is the outcome alone, + // so the list, and the repository open in it, stay where they are. + wantJSON := strings.Contains(r.Header.Get("Accept"), "application/json") if err != nil { p.log.Warn("a mirror action was refused", "admin", admin.Email, "mirror", m.Addr(), "err", err) + if wantJSON { + writeJSONStatus(w, http.StatusUnprocessableEntity, map[string]string{"error": err.Error()}) + return + } p.renderMirror(w, r, m, "", err.Error()) return } p.log.Info("the mirror was changed", "admin", admin.Email, "mirror", m.Addr(), "what", msg) + if wantJSON { + writeJSONStatus(w, http.StatusOK, map[string]string{"message": msg}) + return + } p.renderMirror(w, r, m, msg, "") }) } @@ -649,3 +662,9 @@ func revOrMain(rev string) string { } return rev } + +func writeJSONStatus(w http.ResponseWriter, code int, v any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + _ = json.NewEncoder(w).Encode(v) +} diff --git a/internal/web/mirror_explore_test.go b/internal/web/mirror_explore_test.go index 7011c41..a822b45 100644 --- a/internal/web/mirror_explore_test.go +++ b/internal/web/mirror_explore_test.go @@ -3,6 +3,9 @@ package web import ( "errors" "html" + "net/http" + "net/http/httptest" + "net/url" "strings" "testing" "time" @@ -163,3 +166,32 @@ func TestAnImportanceMatrixIsNotOfferedAsAModel(t *testing.T) { t.Errorf("only the model should have a fetch form: %s", body) } } + +func TestAFetchFromTheListAnswersWithItsOutcomeAlone(t *testing.T) { + // Started from a repository opened in the list, a fetch answers the + // page's script with the outcome, not a whole new page that would close + // the repository and lose the list's place. + m := &fakeMirror{} + mux := mirrorPage(t, m, true) + send := func(form url.Values) *httptest.ResponseRecorder { + r := httptest.NewRequest("POST", "/admin/mirror/fetch", strings.NewReader(form.Encode())) + r.Header.Set("Content-Type", "application/x-www-form-urlencoded") + r.Header.Set("Accept", "application/json") + r.AddCookie(&http.Cookie{Name: SessionCookie, Value: "good"}) + r.AddCookie(&http.Cookie{Name: csrfCookie, Value: "tok"}) + w := httptest.NewRecorder() + mux.ServeHTTP(w, r) + return w + } + w := send(url.Values{"csrf": {"tok"}, "source": {"http://up/x.gguf"}, "model": {"x"}, "version": {"v1"}, "force": {"1"}}) + if w.Code != 200 || !strings.Contains(w.Body.String(), `"message":"started downloading x/v1`) { + t.Errorf("%d %s", w.Code, w.Body.String()) + } + if strings.Contains(w.Body.String(), " summary { list-style: none; cursor: pointer; display: inlin details.add-inline > summary::-webkit-details-marker { display: none; } details.add-inline[open] > summary { color: var(--muted); } details.add-inline form.add { margin-top: .3rem; } +.fetch-note { margin-top: .3rem; font-size: .78rem; white-space: normal; } .fetch-note.bad { color: var(--bad); } diff --git a/internal/web/templates/admin_mirror.html b/internal/web/templates/admin_mirror.html index 17b7345..4f7dce7 100644 --- a/internal/web/templates/admin_mirror.html +++ b/internal/web/templates/admin_mirror.html @@ -28,6 +28,7 @@ that llmgw-mirror is running there and that its management port is reachable from this machine.

{{else}} +

What the mirror holds

{{if .Rows}} @@ -190,6 +191,7 @@ document.addEventListener('click', function (e) { }); {{else}}

The mirror is reachable and holds nothing yet.

{{end}} + {{if .HasRegistry}}

Explore models

@@ -272,6 +274,71 @@ document.querySelector("table.explore").addEventListener("click", async function td.textContent = "Could not load it: " + err; } }); + +// A fetch started from an opened repository answers where it was asked: +// the outcome under its button, and the mirror's table above brought up to +// date and followed until the download is done. A form post would reload +// the page, closing the repository and losing the list's place. +document.querySelector("table.explore").addEventListener("submit", async function (e) { + const form = e.target.closest('form[action="/admin/mirror/fetch"]'); + if (!form) return; + e.preventDefault(); + const btn = form.querySelector("button[type=submit]"); + const was = btn.textContent; + btn.disabled = true; + btn.textContent = "Starting…"; + let note = form.parentElement.querySelector(".fetch-note"); + if (!note) { + note = document.createElement("div"); + note.className = "fetch-note note"; + form.after(note); + } + try { + const resp = await fetch(form.action, { + method: "POST", body: new URLSearchParams(new FormData(form)), + headers: { Accept: "application/json" }, + }); + const out = await resp.json().catch(() => ({ error: "(" + resp.status + ")" })); + if (!resp.ok) { + note.textContent = out.error || "refused"; + note.classList.add("bad"); + btn.disabled = false; + btn.textContent = was; + return; + } + note.classList.remove("bad"); + note.textContent = "✓ " + out.message + " — its progress is in the table above."; + btn.textContent = "Started"; + followHeld(); + } catch (err) { + note.textContent = "Could not reach the gateway: " + err; + note.classList.add("bad"); + btn.disabled = false; + btn.textContent = was; + } +}); + +// Refreshes only the mirror's own table, every five seconds while anything +// in it is downloading. +let following = false; +async function followHeld() { + if (following) return; + following = true; + try { + for (;;) { + const r = await fetch(location.pathname + location.search, { headers: { Accept: "text/html" } }); + if (!r.ok) break; + const doc = new DOMParser().parseFromString(await r.text(), "text/html"); + const fresh = doc.getElementById("held"), held = document.getElementById("held"); + if (!fresh || !held) break; + held.replaceWith(fresh); + if (!fresh.querySelector(".state-starting")) break; + await new Promise((ok) => setTimeout(ok, 5000)); + } + } finally { + following = false; + } +} {{if .Hidden}}

{{.Hidden}} more {{if eq .Hidden 1}}is{{else}}are{{end}} hidden: their architecture is one the nodes' llama.cpp cannot load. Untick “only what these nodes can run” to see them.

{{end}}