Completes the objective deletion gate for the UI Service (archie-core-8cda.5.6): go list -deps ./cmd/archie-ui now links zero banned packages -- no modernc.org/sqlite, no internal/store, gateway, channels, memory, secret, agentexec, tools, skill, yaegiutil, curator, or the workflow engine. The closure shrinks from 68 to 26 archie-core packages. Each cut is a producer-side move; no consumer-side defensive shims. - Store contracts (TaskStore family, value types, sentinels) moved from internal/store to internal/domain/storecontract -- the approved contract location per state-store-contract rev. 2d, and NOT internal/contracts: that tree is buf generate output and tools/proto.sh check deletes hand-written files there. internal/store keeps aliases; sentinel message strings are byte-identical and remain the wire contract. - Chat contracts (ChatContract, sessions, turns, streaming, commands) moved from internal/gateway to internal/domain/messaging; gateway keeps aliases. ToolCallEvent rendering helpers become gateway functions (methods cannot live on alias types). - Task vocabulary (Task, statuses, mid-run Store, Definition) split into internal/domain/workflow/task; the pipeline engine stays in package workflow with aliases, so daemon-side callers are unchanged. - overlay.DeniedKeys moved to internal/infrastructure/configuration: webui referenced the constant and that dragged the overlay SQLite store in. - webui.Server's concrete runtime handles become webui-owned view interfaces with daemon-side bootstrap adapters: channel lifecycle (internal/channels/status), MemoryStatus, CuratorStatus, and a SkillCatalog seam replacing the direct skill.DefaultRoots/CatalogRoots calls. Each degrades to an empty page when unwired. - config.SecretRef moved into internal/config (config vocabulary); internal/secret keeps an alias and Registry.Resolve absorbs the zero-value short-circuit. Docs: state-store-contract rev. 2d relocation record; ui-service-boundary deletion gate marked satisfied; ARCHITECTURE.md package map rows.
237 lines
7.5 KiB
Go
237 lines
7.5 KiB
Go
package webui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/binding"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
// bindingRequest is the wire shape POST /api/bindings and
|
|
// PATCH /api/bindings/{id} both accept. ID/Version/Status/CreatedAt/UpdatedAt
|
|
// are server-assigned (or server-driven), never taken from the request body.
|
|
type bindingRequest struct {
|
|
Name string `json:"name"`
|
|
Matcher binding.Matcher `json:"matcher"`
|
|
MappingID int64 `json:"mapping_id"`
|
|
Workflow string `json:"workflow"`
|
|
// Owner and Repo optionally pin the binding to one configured repo,
|
|
// for multi-repo deployments. Both empty is valid (falls back to the
|
|
// daemon's single-configured-repo behaviour); binding.Validate
|
|
// rejects setting only one.
|
|
Owner string `json:"owner"`
|
|
Repo string `json:"repo"`
|
|
Secret string `json:"secret"`
|
|
}
|
|
|
|
func (s *Server) handleBindingsList(w http.ResponseWriter, r *http.Request) {
|
|
if s.Bindings == nil {
|
|
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
bindings, err := s.Bindings.ListBindings(r.Context())
|
|
if err != nil {
|
|
s.Log.Error("list bindings", "err", err)
|
|
http.Error(w, "list bindings failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
stripBindingSecrets(bindings)
|
|
writeJSON(w, map[string]any{"bindings": bindings})
|
|
}
|
|
|
|
func (s *Server) handleBindingCreate(w http.ResponseWriter, r *http.Request) {
|
|
if s.Bindings == nil {
|
|
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
var req bindingRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if !s.hasWorkflow(req.Workflow) {
|
|
http.Error(w, "binding: workflow not found: "+req.Workflow, http.StatusBadRequest)
|
|
return
|
|
}
|
|
b := binding.Binding{
|
|
Name: req.Name,
|
|
Matcher: req.Matcher,
|
|
MappingID: req.MappingID,
|
|
Workflow: req.Workflow,
|
|
Owner: req.Owner,
|
|
Repo: req.Repo,
|
|
Secret: req.Secret,
|
|
Status: binding.Normalize(binding.StatusDraft),
|
|
}
|
|
if err := b.Validate(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
id, err := s.Bindings.InsertBinding(r.Context(), b)
|
|
if err != nil {
|
|
if errors.Is(err, storecontract.ErrBindingOverlap) {
|
|
http.Error(w, "binding source overlaps an existing binding", http.StatusConflict)
|
|
return
|
|
}
|
|
s.Log.Error("insert binding", "err", err)
|
|
http.Error(w, "create binding failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
created, err := s.Bindings.GetBinding(r.Context(), id)
|
|
if err != nil || created == nil {
|
|
s.Log.Error("get binding after insert", "err", err, "id", id)
|
|
http.Error(w, "create binding failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
created.Secret = ""
|
|
w.WriteHeader(http.StatusCreated)
|
|
writeJSON(w, created)
|
|
}
|
|
|
|
func (s *Server) handleBindingGet(w http.ResponseWriter, r *http.Request) {
|
|
if s.Bindings == nil {
|
|
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
http.Error(w, "invalid binding id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
b, err := s.Bindings.GetBinding(r.Context(), id)
|
|
if err != nil {
|
|
s.Log.Error("get binding", "err", err, "id", id)
|
|
http.Error(w, "get binding failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if b == nil {
|
|
http.Error(w, "binding not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
b.Secret = ""
|
|
writeJSON(w, b)
|
|
}
|
|
|
|
func (s *Server) handleBindingUpdate(w http.ResponseWriter, r *http.Request) {
|
|
if s.Bindings == nil {
|
|
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
http.Error(w, "invalid binding id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
var req bindingRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if !s.hasWorkflow(req.Workflow) {
|
|
http.Error(w, "binding: workflow not found: "+req.Workflow, http.StatusBadRequest)
|
|
return
|
|
}
|
|
b := binding.Binding{
|
|
ID: id,
|
|
Name: req.Name,
|
|
Matcher: req.Matcher,
|
|
MappingID: req.MappingID,
|
|
Workflow: req.Workflow,
|
|
Owner: req.Owner,
|
|
Repo: req.Repo,
|
|
Secret: req.Secret,
|
|
}
|
|
if err := b.ValidateForUpdate(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := s.Bindings.UpdateBinding(r.Context(), b); err != nil {
|
|
switch {
|
|
case errors.Is(err, storecontract.ErrBindingNotFound):
|
|
http.Error(w, "binding not found", http.StatusNotFound)
|
|
case errors.Is(err, storecontract.ErrBindingOverlap):
|
|
http.Error(w, "binding source overlaps an existing binding", http.StatusConflict)
|
|
default:
|
|
s.Log.Error("update binding", "err", err, "id", id)
|
|
http.Error(w, "update binding failed", http.StatusInternalServerError)
|
|
}
|
|
return
|
|
}
|
|
updated, err := s.Bindings.GetBinding(r.Context(), id)
|
|
if err != nil || updated == nil {
|
|
s.Log.Error("get binding after update", "err", err, "id", id)
|
|
http.Error(w, "update binding failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
updated.Secret = ""
|
|
writeJSON(w, updated)
|
|
}
|
|
|
|
func (s *Server) handleBindingDelete(w http.ResponseWriter, r *http.Request) {
|
|
if s.Bindings == nil {
|
|
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
http.Error(w, "invalid binding id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := s.Bindings.DeleteBinding(r.Context(), id); err != nil {
|
|
if errors.Is(err, storecontract.ErrBindingNotFound) {
|
|
http.Error(w, "binding not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
s.Log.Error("delete binding", "err", err, "id", id)
|
|
http.Error(w, "delete binding failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) handleBindingApprove(w http.ResponseWriter, r *http.Request) {
|
|
if s.Bindings == nil {
|
|
http.Error(w, "bindings not configured", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
http.Error(w, "invalid binding id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := s.Bindings.ApproveBinding(r.Context(), id); err != nil {
|
|
switch {
|
|
case errors.Is(err, storecontract.ErrBindingNotFound):
|
|
http.Error(w, "binding not found", http.StatusNotFound)
|
|
case errors.Is(err, storecontract.ErrBindingTransition):
|
|
http.Error(w, "binding cannot be approved from its current state", http.StatusConflict)
|
|
case errors.Is(err, storecontract.ErrBindingOverlap):
|
|
http.Error(w, "binding source overlaps an existing binding", http.StatusConflict)
|
|
default:
|
|
s.Log.Error("approve binding", "err", err, "id", id)
|
|
http.Error(w, "approve binding failed", http.StatusInternalServerError)
|
|
}
|
|
return
|
|
}
|
|
updated, err := s.Bindings.GetBinding(r.Context(), id)
|
|
if err != nil || updated == nil {
|
|
s.Log.Error("get binding after approve", "err", err, "id", id)
|
|
http.Error(w, "approve binding failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
updated.Secret = ""
|
|
writeJSON(w, updated)
|
|
}
|
|
|
|
// stripBindingSecrets zeroes the Secret field on every binding in the slice
|
|
// so the list handler never leaks a shared HMAC secret through /api/bindings.
|
|
// Single-binding reads do the same inline; this covers the list path.
|
|
func stripBindingSecrets(bs []binding.Binding) {
|
|
for i := range bs {
|
|
bs[i].Secret = ""
|
|
}
|
|
}
|