diff --git a/internal/web/admin_mirror.go b/internal/web/admin_mirror.go
index bec61ecc..da50e169 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 7011c413..a822b450 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 17b7345e..4f7dce72 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.