Record implemented MySQL pilots and remaining app integration gates. Include the existing opt-in pages-cli agent skill source without installing it.
73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
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)
|
|
}
|
|
}
|