Start a mirror download where it was asked for

Fetch on a repository opened in the explorer posted a form, and the
page that came back had closed the repository and lost the list's place.
The page's script now sends it asking for JSON: the outcome appears
under the button, and only the mirror's own table is fetched again,
every five seconds while anything in it is downloading. Without script
the form posts as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-09-29 09:21:21 +02:00
1 parent 4dba2a54ea
commit b18c9c3378
4 files changed
+119

No files matched your search

+19
View File
@@ -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)
}
+32
View File
@@ -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(), "<html") {
t.Error("a whole page came back")
}
w = send(url.Values{"csrf": {"tok"}, "model": {"x"}})
if w.Code != 422 || !strings.Contains(w.Body.String(), `"error"`) {
t.Errorf("a refusal: %d %s", w.Code, w.Body.String())
}
}
+1
View File
@@ -763,3 +763,4 @@ details.add-inline > 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); }
+67
View File
@@ -28,6 +28,7 @@ that <code>llmgw-mirror</code> is running there and that its management port is
reachable from this machine.</p>
{{else}}
<div id="held">
<h2>What the mirror holds</h2>
{{if .Rows}}
<table class="compact held">
@@ -190,6 +191,7 @@ document.addEventListener('click', function (e) {
});
</script>
{{else}}<p class="empty">The mirror is reachable and holds nothing yet.</p>{{end}}
</div>
{{if .HasRegistry}}
<h2>Explore models</h2>
@@ -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;
}
}
</script>
{{if .Hidden}}<p class="note">{{.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.</p>{{end}}