feat(ml): add domain settings UI and document rollout boundaries
Record implemented MySQL pilots and remaining app integration gates. Include the existing opt-in pages-cli agent skill source without installing it.
This commit is contained in:
1 parent
6a67fd9afe
commit
37c1443219
17 files changed
+1007
-1
No files matched your search
@@ -21,6 +21,7 @@ func configPanelGroups(locale string, plugins []adapterPlugin) []configPanelGrou
|
||||
for _, plugin := range plugins {
|
||||
groups[4].Panels = append(groups[4].Panels, configPanelLink{"adapter-" + plugin.ID, plugin.Name})
|
||||
}
|
||||
groups[5].Panels = append(groups[5].Panels, configPanelLink{"machine-learning", "Machine Learning"})
|
||||
groups = append(groups, configPanelGroup{"extensions", translate(locale, "extension.title"), []configPanelLink{link("extensions", "extension.catalog"), link("diary", "periodical.diary"), link("blog", "periodical.blog")}})
|
||||
groups = append(groups, configPanelGroup{"organizer", translate(locale, "organizer.title"), []configPanelLink{link("organizer", "organizer.settings")}})
|
||||
return groups
|
||||
|
||||
@@ -58,6 +58,7 @@ func configFixture(t *testing.T) (*Server, string, string) {
|
||||
s.mux.HandleFunc("/-/sys/config/auth", s.handleAuthAdmin)
|
||||
s.mux.HandleFunc("/-/sys/config/adapters", s.handleAdapterPluginConfig)
|
||||
s.mux.HandleFunc("/_/api/v1/assets", s.handleAssetsAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/ml/settings", s.handleMachineLearningSettings)
|
||||
s.mux.HandleFunc("/_/api/v1/adapter", s.handleAdapterAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/queue", s.handleQueueAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/queue/", s.handleQueueAPI)
|
||||
@@ -313,7 +314,7 @@ func TestConfigChromiumSmoke(t *testing.T) {
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
for _, panel := range []string{"assets", "events", "queue", "scheduler", "logs", "resources", "bookmarks", "adapter-hosted.test", "extensions"} {
|
||||
for _, panel := range []string{"assets", "events", "queue", "scheduler", "logs", "resources", "bookmarks", "adapter-hosted.test", "extensions", "machine-learning"} {
|
||||
t.Run(panel, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
|
||||
defer cancel()
|
||||
@@ -339,6 +340,9 @@ func TestConfigChromiumSmoke(t *testing.T) {
|
||||
if panel == "assets" && !strings.Contains(body, `data-asset-copy-hash=`) {
|
||||
t.Fatalf("assets did not load: %s", body)
|
||||
}
|
||||
if panel == "machine-learning" && !strings.Contains(body, `id="ml-scope_uuid"`) {
|
||||
t.Fatalf("ML settings did not load: %s", body)
|
||||
}
|
||||
if panel == "resources" && !strings.Contains(body, `class="chart-line"`) {
|
||||
t.Fatalf("resource chart did not load: %s", body)
|
||||
}
|
||||
|
||||
@@ -70,6 +70,8 @@ var endpointPolicies = map[string]endpointPolicy{
|
||||
"POST /_/api/v1/gc/zombies/purge": {Name: "gc.purge", Capability: "delete", Target: targetDomain},
|
||||
"POST /v/upload": {Name: "document.upload", Capability: "upload", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/assets": {Name: "asset.list", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/ml/settings": {Name: "ml.settings.read", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"PUT /_/api/v1/ml/settings": {Name: "ml.settings.write", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"POST /_/api/v1/assets": {Name: "asset.create", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"DELETE /_/api/v1/assets": {Name: "asset.delete", Capability: "manage", Target: targetDomain, Resolver: resolveHandler},
|
||||
"GET /_/api/v1/notifications": {Name: "notifications.list", Capability: "read", Target: targetDomain, Resolver: resolveHandler},
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
|
||||
"pageswiki/internal/machinelearning"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) handleMachineLearningSettings(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
domain := s.domainForRequest(r)
|
||||
principal := s.requestPrincipal(r)
|
||||
if domain == nil || principal == nil || principal.State != "active" ||
|
||||
(!s.store.IsBuiltinGroupAdmin(domain.UUID, principal.UUID, "domain_admin") && !s.store.IsCampusAdmin(domain.CampusUUID, principal.UUID)) {
|
||||
writeEventJSON(w, 403, map[string]string{"error": "domain_admin_required"})
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodGet {
|
||||
p, err := s.store.MachineLearningPolicy(r.Context(), domain.UUID)
|
||||
if err != nil {
|
||||
writeEventJSON(w, 503, map[string]string{"error": "ml_settings_unavailable"})
|
||||
return
|
||||
}
|
||||
writeEventJSON(w, 200, p)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPut {
|
||||
w.Header().Set("Allow", "GET, PUT")
|
||||
writeEventJSON(w, 405, map[string]string{"error": "method_not_allowed"})
|
||||
return
|
||||
}
|
||||
if !validBrowserMutationOrigin(r) {
|
||||
rejectBrowserMutation(w, r)
|
||||
return
|
||||
}
|
||||
contentType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil || contentType != "application/json" {
|
||||
writeEventJSON(w, 415, map[string]string{"error": "json_required"})
|
||||
return
|
||||
}
|
||||
var request struct {
|
||||
BaseGeneration string `json:"base_generation"`
|
||||
Policy machinelearning.Policy `json:"policy"`
|
||||
}
|
||||
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10))
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(&request) != nil || decoder.Decode(new(any)) != io.EOF {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "ml_invalid_settings"})
|
||||
return
|
||||
}
|
||||
// The store assigns the new generation; do not accept an ambiguous second base.
|
||||
if request.Policy.Generation != "" {
|
||||
writeEventJSON(w, 400, map[string]string{"error": "ml_invalid_settings"})
|
||||
return
|
||||
}
|
||||
p, err := s.store.SaveMachineLearningPolicy(r.Context(), domain.UUID, request.BaseGeneration, request.Policy)
|
||||
switch {
|
||||
case errors.Is(err, store.ErrMachineLearningConflict):
|
||||
writeEventJSON(w, 409, map[string]string{"error": "ml_settings_conflict"})
|
||||
case errors.Is(err, machinelearning.ErrPolicy):
|
||||
writeEventJSON(w, 422, map[string]string{"error": "ml_invalid_settings"})
|
||||
case err != nil:
|
||||
writeEventJSON(w, 503, map[string]string{"error": "ml_settings_unavailable"})
|
||||
default:
|
||||
writeEventJSON(w, 200, p)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/machinelearning"
|
||||
)
|
||||
|
||||
func TestMachineLearningSettingsAPI(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
call := func(auth, method, body, origin string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(method, "http://localhost/_/api/v1/ml/settings", strings.NewReader(body))
|
||||
if auth != "" {
|
||||
r.Header.Set("Authorization", "Bearer "+auth)
|
||||
}
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
if origin != "" {
|
||||
r.Header.Set("Origin", origin)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
w := call(token, "GET", "", "")
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("read: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
|
||||
t.Fatal("cacheable settings")
|
||||
}
|
||||
p := machinelearning.Policy{Enabled: true, ScopeUUID: "22222222-2222-4222-8222-222222222222", Provider: "doc2vec", Model: strings.Repeat("a", 64), Dimensions: 128, Prefixes: []string{"/lab"}, MaxBatchBytes: 6000}
|
||||
encode := func(base string) string {
|
||||
b, e := json.Marshal(map[string]any{"base_generation": base, "policy": p})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
body := encode("")
|
||||
if w = call(token, "PUT", body, "https://attacker.example"); w.Code != 403 {
|
||||
t.Fatal("cross origin", w.Code)
|
||||
}
|
||||
user, e := s.store.CreatePasswordAccount(domain, "ml-user", "synthetic long password", "User", "")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
userToken, e := s.store.CreateAuthSession(domain, user.UUID)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for _, method := range []string{"GET", "PUT"} {
|
||||
if w = call(userToken, method, body, ""); w.Code != 403 {
|
||||
t.Fatal("non admin", method, w.Code)
|
||||
}
|
||||
}
|
||||
if w = call("", "GET", "", ""); w.Code == 200 {
|
||||
t.Fatal("anonymous read")
|
||||
}
|
||||
w = call(token, "PUT", body, "")
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("save: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
var saved machinelearning.Policy
|
||||
if json.Unmarshal(w.Body.Bytes(), &saved) != nil || saved.Generation == "" {
|
||||
t.Fatal("missing generation")
|
||||
}
|
||||
if w = call(token, "PUT", body, ""); w.Code != 409 {
|
||||
t.Fatal("stale save", w.Code)
|
||||
}
|
||||
if w = call(token, "PUT", encode(saved.Generation)+" {}", ""); w.Code != 400 {
|
||||
t.Fatal("trailing JSON", w.Code)
|
||||
}
|
||||
p.Enabled = false
|
||||
w = call(token, "PUT", encode(saved.Generation), "")
|
||||
if w.Code != 200 {
|
||||
t.Fatal("disable", w.Code)
|
||||
}
|
||||
if json.Unmarshal(w.Body.Bytes(), &saved) != nil {
|
||||
t.Fatal("response")
|
||||
}
|
||||
p.Enabled = true
|
||||
p.Provider = "openai"
|
||||
p.Model = "text-embedding-3-small"
|
||||
if w = call(token, "PUT", encode(saved.Generation), ""); w.Code != 422 {
|
||||
t.Fatal("external consent bypass", w.Code)
|
||||
}
|
||||
page := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=machine-learning", nil)
|
||||
if page.Code != 200 || !strings.Contains(page.Body.String(), `id="panel-machine-learning"`) || !strings.Contains(page.Body.String(), "ml-settings.js") {
|
||||
t.Fatal("settings panel missing", page.Code)
|
||||
}
|
||||
}
|
||||
@@ -252,6 +252,7 @@ func (s *Server) registerRoutes() {
|
||||
s.mux.HandleFunc("/_/obj/", s.handleImmutableObject)
|
||||
s.mux.HandleFunc("/v/json/", s.handleJSONStructure)
|
||||
s.mux.HandleFunc("/_/api/v1/assets", s.handleAssetsAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/ml/settings", s.handleMachineLearningSettings)
|
||||
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
|
||||
s.mux.HandleFunc("/_/api/v1/browser/navigation", s.handleBrowserNavigationAPI)
|
||||
s.mux.HandleFunc("/u/", s.handleUserProfile)
|
||||
|
||||
Reference in new issue
Block a user