74 lines
2.5 KiB
Go
74 lines
2.5 KiB
Go
package webui
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
// SetupStep is one item in the dashboard's getting-started checklist. Path is
|
|
// the page that owns the configuration the step is about, so an operator can
|
|
// follow the step to where it is fixed rather than guess a settings page.
|
|
type SetupStep struct {
|
|
Title string `json:"title"`
|
|
Detail string `json:"detail,omitempty"`
|
|
Done bool `json:"done"`
|
|
Path string `json:"path,omitempty"`
|
|
}
|
|
|
|
// ModelReader reads the model aliases the calling principal's org may use.
|
|
type ModelReader interface {
|
|
CallerModels(ctx context.Context) (org string, aliases map[string]string, err error)
|
|
}
|
|
|
|
// handleSetup reports which setup steps are still missing in the caller's
|
|
// organisation, from live state. Optional capabilities (a chat channel, a
|
|
// repository) are set up where they are used, not listed here.
|
|
func (s *Server) handleSetup(w http.ResponseWriter, r *http.Request) {
|
|
steps := []SetupStep{}
|
|
if s.Store == nil {
|
|
writeJSON(w, map[string]any{"steps": steps})
|
|
return
|
|
}
|
|
if done, ok := s.hasActiveAgent(r); ok {
|
|
steps = append(steps, SetupStep{Title: "Agent identity", Done: done, Path: "/org/agents"})
|
|
}
|
|
if count, err := s.Store.StatusCounts(r.Context()); err == nil {
|
|
total := 0
|
|
for _, n := range count {
|
|
total += n
|
|
}
|
|
steps = append(steps, SetupStep{Title: "First task", Done: total > 0, Path: "/tasks"})
|
|
}
|
|
if s.Models != nil {
|
|
// Shown only while the org has no model to run, so a working setup
|
|
// carries no extra step.
|
|
if _, aliases, err := s.Models.CallerModels(r.Context()); err == nil && len(aliases) == 0 {
|
|
steps = append(steps, SetupStep{Title: "Model", Path: "/org/models"})
|
|
}
|
|
}
|
|
writeJSON(w, map[string]any{"steps": steps})
|
|
}
|
|
|
|
// hasActiveAgent reports whether the caller's org has an active bot or
|
|
// service account assigned; ok is false when that cannot be read.
|
|
func (s *Server) hasActiveAgent(r *http.Request) (done, ok bool) {
|
|
reader, isReader := s.Orgs.(org.AgentAssignments)
|
|
if !isReader || s.Identities == nil {
|
|
return false, false
|
|
}
|
|
assignments, err := reader.ListOrgAgents(r.Context(), policyOrg(r))
|
|
if err != nil {
|
|
return false, false
|
|
}
|
|
for _, assignment := range assignments {
|
|
value, err := s.Identities.Get(r.Context(), assignment.IdentityID)
|
|
if err == nil && value.Kind.Agent() && value.Lifecycle == identity.LifecycleActive {
|
|
return true, true
|
|
}
|
|
}
|
|
return false, true
|
|
}
|