The binary started connectors, module routes, the starter workflow and the worker loops at boot whatever the database said, so an instance with no owner answered operational requests, the bootstrap admin token among them. The governance module now signals once the bootstrap registration has committed, and the composition root holds every operational route, connector and loop back until it does. Liveness stays up and readiness reports the missing owner.
663 lines
24 KiB
Go
663 lines
24 KiB
Go
// Package governance owns an organisation's settings. So far that is the
|
|
// agent model: the provider class, base URL and model every agent loop uses.
|
|
// Nothing an event or a tool result says can change where a model request
|
|
// goes, because the client is built only from these settings.
|
|
package governance
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/samcharles93/ai-sdk/chat"
|
|
"github.com/samcharles93/ai-sdk/runtime"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/modules/governance/internal/store"
|
|
)
|
|
|
|
// ErrNoAgentModel means the organisation hasn't chosen its agent model. There
|
|
// is no built-in default.
|
|
var ErrNoAgentModel = errors.New("no agent model is set for the organisation")
|
|
|
|
const (
|
|
roleAgent = "agent"
|
|
agentAliasPrefix = "agent:"
|
|
)
|
|
|
|
var agentAliasPattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,62}$`)
|
|
|
|
// ValidateAgentModelAlias accepts an empty alias for the default model.
|
|
func ValidateAgentModelAlias(alias string) error {
|
|
if alias != "" && !agentAliasPattern.MatchString(alias) {
|
|
return fmt.Errorf("invalid agent model alias %q: use 1-63 letters, digits, dots, underscores or hyphens, starting with a letter or digit", alias)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func agentModelRole(alias string) string {
|
|
if alias == "" || alias == "default" {
|
|
return roleAgent
|
|
}
|
|
return agentAliasPrefix + alias
|
|
}
|
|
|
|
// DefaultApprovalDeadline is how long an approval request waits for an answer
|
|
// when the organisation hasn't said.
|
|
const DefaultApprovalDeadline = 24 * time.Hour
|
|
|
|
// DefaultRaiseAfter is how many approvals in a row earn a proposal to raise an
|
|
// action class when the organisation hasn't said.
|
|
const DefaultRaiseAfter = 3
|
|
|
|
// maxRaiseAfter bounds the count: past it a class could never earn a raise.
|
|
const maxRaiseAfter = 100
|
|
|
|
// DefaultRecurrenceThreshold is how many incidents with one cause on one target
|
|
// it takes to propose a root-cause fix when the organisation hasn't said.
|
|
const DefaultRecurrenceThreshold = 5
|
|
|
|
// The bounds on the recurrence threshold. One incident is no pattern; past a
|
|
// hundred the history Archie keeps per target can't confirm it.
|
|
const (
|
|
minRecurrenceThreshold = 2
|
|
maxRecurrenceThreshold = 100
|
|
)
|
|
|
|
// DefaultAttemptsPerRun is how many steps an agent may take in one run when the
|
|
// organisation hasn't said.
|
|
const DefaultAttemptsPerRun = 20
|
|
|
|
// The bounds on attempts per run. An agent needs a few steps to look anything
|
|
// up; past two hundred a run is looping.
|
|
const (
|
|
minAttemptsPerRun = 2
|
|
maxAttemptsPerRun = 200
|
|
)
|
|
|
|
// The defaults and bounds on the action budgets.
|
|
const (
|
|
DefaultActionsPerRun = 10
|
|
DefaultActionsPerDay = 100
|
|
maxActionBudget = 10000
|
|
)
|
|
|
|
// The bounds on an approval deadline. Below a minute nobody could answer; past
|
|
// a month the request outlives the incident it was raised for.
|
|
const (
|
|
minApprovalDeadline = time.Minute
|
|
maxApprovalDeadline = 30 * 24 * time.Hour
|
|
)
|
|
|
|
// Settings is what the organisation decides about how Archie acts.
|
|
type Settings struct {
|
|
// ApprovalDeadline is how long an approval request waits for an answer
|
|
// before the run gives up on it.
|
|
ApprovalDeadline time.Duration
|
|
// RaiseAfter is how many approvals in a row, since an action class was last
|
|
// tightened, earn a proposal to raise it one level.
|
|
RaiseAfter int
|
|
// Frozen holds every action that changes something at propose and wait,
|
|
// without changing any class's level.
|
|
Frozen bool
|
|
// RecurrenceThreshold is how many incidents with the same cause on one
|
|
// target it takes, whatever their outcomes, to propose a root-cause fix.
|
|
RecurrenceThreshold int
|
|
// AttemptsPerRun is how many steps an agent may take in one run.
|
|
AttemptsPerRun int
|
|
// DefaultOwner is the id of the person who answers for what has no other
|
|
// owner, or empty.
|
|
DefaultOwner string
|
|
// Escalation is the people an unanswered request moves through, in order,
|
|
// each with how long the request waits on them.
|
|
Escalation []Step
|
|
// Timing is when changing actions may run without asking.
|
|
Timing Timing
|
|
// ActionsPerRun and ActionsPerDay are how many actions one run, and the
|
|
// organisation in a day, may take before a changing action waits for approval.
|
|
ActionsPerRun int
|
|
ActionsPerDay int
|
|
}
|
|
|
|
// Step is one stop on the escalation path: a person and how long an unanswered
|
|
// request waits on them before it moves to the next.
|
|
type Step struct {
|
|
// Person is the id of the person the request is passed to.
|
|
Person string
|
|
Deadline time.Duration
|
|
}
|
|
|
|
// stepRow is a step as stored.
|
|
type stepRow struct {
|
|
Person string `json:"person"`
|
|
DeadlineMS int64 `json:"deadline_ms"`
|
|
}
|
|
|
|
// stepRequest is a step as the API shows and sets it. Its deadline travels as
|
|
// a Go duration string, like the approval deadline.
|
|
type stepRequest struct {
|
|
Person string `json:"person"`
|
|
Deadline string `json:"deadline"`
|
|
}
|
|
|
|
// settingsRequest is the settings as the API shows and sets them. A duration
|
|
// travels as a Go duration string ("24h"), the same form every other duration
|
|
// in the config takes.
|
|
type settingsRequest struct {
|
|
ApprovalDeadline string `json:"approval_deadline"`
|
|
RaiseAfter int `json:"raise_after"`
|
|
Frozen bool `json:"frozen"`
|
|
RecurrenceThreshold int `json:"recurrence_threshold"`
|
|
AttemptsPerRun int `json:"attempts_per_run"`
|
|
DefaultOwner string `json:"default_owner"`
|
|
Escalation []stepRequest `json:"escalation"`
|
|
Timing Timing `json:"timing"`
|
|
ActionsPerRun int `json:"actions_per_run"`
|
|
ActionsPerDay int `json:"actions_per_day"`
|
|
}
|
|
|
|
func (s Settings) request() settingsRequest {
|
|
return settingsRequest{
|
|
ApprovalDeadline: s.ApprovalDeadline.String(), RaiseAfter: s.RaiseAfter, Frozen: s.Frozen,
|
|
RecurrenceThreshold: s.RecurrenceThreshold, AttemptsPerRun: s.AttemptsPerRun,
|
|
DefaultOwner: s.DefaultOwner, Escalation: stepRequests(s.Escalation),
|
|
Timing: s.Timing, ActionsPerRun: s.ActionsPerRun, ActionsPerDay: s.ActionsPerDay,
|
|
}
|
|
}
|
|
|
|
func stepRequests(steps []Step) []stepRequest {
|
|
out := make([]stepRequest, 0, len(steps))
|
|
for _, st := range steps {
|
|
out = append(out, stepRequest{Person: st.Person, Deadline: st.Deadline.String()})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// stepsOf reads the steps of a request, each with a deadline inside the range
|
|
// an answer can be given in.
|
|
func stepsOf(req []stepRequest) ([]Step, error) {
|
|
out := make([]Step, 0, len(req))
|
|
for _, r := range req {
|
|
d, err := time.ParseDuration(r.Deadline)
|
|
if err != nil {
|
|
return nil, errors.New("each escalation deadline is a duration, such as 4h")
|
|
}
|
|
out = append(out, Step{Person: r.Person, Deadline: d})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Model is the model chosen for a role.
|
|
type Model struct {
|
|
Class string `json:"class"`
|
|
BaseURL string `json:"base_url"`
|
|
Model string `json:"model"`
|
|
// APIKeyCredential names the credential whose token authenticates the
|
|
// provider. The dashboard shows the name, never the token.
|
|
APIKeyCredential string `json:"api_key_credential,omitempty"`
|
|
}
|
|
|
|
// Credentials resolves a credential's token by name. It is the connectors
|
|
// module's sealed store, so governance never holds a secret.
|
|
type Credentials interface {
|
|
BearerToken(ctx context.Context, credential string) (string, error)
|
|
}
|
|
|
|
// AgentProvider builds the chat client for model, resolving the credential it
|
|
// names whenever a run rebuilds the client.
|
|
func (m *Module) AgentProvider(ctx context.Context, model Model) (chat.Provider, chat.ModelInfo, error) {
|
|
return m.provider(ctx, model)
|
|
}
|
|
|
|
// provider builds the chat client for model, resolving its credential when it
|
|
// names one.
|
|
func (m *Module) provider(ctx context.Context, model Model) (chat.Provider, chat.ModelInfo, error) {
|
|
auth := runtime.AuthConfig{Type: runtime.AuthTypeNone}
|
|
if model.APIKeyCredential != "" {
|
|
if m.credentials == nil {
|
|
return nil, chat.ModelInfo{}, errors.New("no credential store is configured")
|
|
}
|
|
key, err := m.credentials.BearerToken(ctx, model.APIKeyCredential)
|
|
if err != nil {
|
|
return nil, chat.ModelInfo{}, fmt.Errorf("agent model credential %q: %w", model.APIKeyCredential, err)
|
|
}
|
|
auth = runtime.AuthConfig{Type: runtime.AuthTypeAPIKey, APIKey: key}
|
|
}
|
|
rt := runtime.NewRuntime(runtime.Config{Providers: map[string]runtime.ProviderConfig{
|
|
roleAgent: {ID: roleAgent, Class: model.Class, BaseURL: model.BaseURL, Auth: auth},
|
|
}})
|
|
ref := roleAgent + "/" + model.Model
|
|
provider, _, err := rt.ChatProvider(ctx, ref)
|
|
if err != nil {
|
|
return nil, chat.ModelInfo{}, fmt.Errorf("agent model %s: %w", ref, err)
|
|
}
|
|
info, err := rt.ChatModelInfo(ref)
|
|
if err != nil {
|
|
return nil, chat.ModelInfo{}, fmt.Errorf("agent model %s: %w", ref, err)
|
|
}
|
|
return provider, info, nil
|
|
}
|
|
|
|
// Module serves one organisation's settings.
|
|
type Module struct {
|
|
orgID string
|
|
store *store.Store
|
|
log *slog.Logger
|
|
credentials Credentials
|
|
authorizer Authorizer
|
|
oidc *oidcLogin
|
|
setup *SetupSignal
|
|
}
|
|
|
|
// NewModule returns the governance module for orgID. The ai-sdk provider
|
|
// classes must already be registered.
|
|
func NewModule(db *sqlx.DB, log *slog.Logger, orgID string) *Module {
|
|
return &Module{orgID: orgID, store: store.New(db), log: log, oidc: &oidcLogin{}, setup: NewSetupSignal()}
|
|
}
|
|
|
|
// UseCredentials gives the module the sealed store a model's key reference
|
|
// resolves through.
|
|
func (m *Module) UseCredentials(c Credentials) { m.credentials = c }
|
|
|
|
// UseAuthorizer gives the module the live policy chain that administrative
|
|
// writes are checked against.
|
|
func (m *Module) UseAuthorizer(a Authorizer) { m.authorizer = a }
|
|
|
|
// AgentModel returns the organisation's agent model.
|
|
func (m *Module) AgentModel(ctx context.Context) (Model, error) {
|
|
return m.AgentModelForAlias(ctx, "")
|
|
}
|
|
|
|
// AgentModelForAlias resolves a model without falling back for unknown aliases.
|
|
func (m *Module) AgentModelForAlias(ctx context.Context, alias string) (Model, error) {
|
|
if err := ValidateAgentModelAlias(alias); err != nil {
|
|
return Model{}, err
|
|
}
|
|
key := agentModelRole(alias)
|
|
role, ok, err := m.store.ModelRole(ctx, m.orgID, key)
|
|
if err != nil {
|
|
return Model{}, err
|
|
}
|
|
if !ok {
|
|
if key != roleAgent {
|
|
return Model{}, fmt.Errorf("agent model alias %q: %w", alias, ErrNoAgentModel)
|
|
}
|
|
return Model{}, ErrNoAgentModel
|
|
}
|
|
return Model{Class: role.Class, BaseURL: role.BaseURL, Model: role.Model, APIKeyCredential: role.APIKeyCredential}, nil
|
|
}
|
|
|
|
// Settings returns the organisation's settings, with a default for every one it
|
|
// hasn't set.
|
|
func (m *Module) Settings(ctx context.Context) (Settings, error) {
|
|
row, ok, err := m.store.Settings(ctx, m.orgID)
|
|
if err != nil {
|
|
return Settings{}, err
|
|
}
|
|
if !ok {
|
|
return Settings{
|
|
ApprovalDeadline: DefaultApprovalDeadline, RaiseAfter: DefaultRaiseAfter,
|
|
RecurrenceThreshold: DefaultRecurrenceThreshold, AttemptsPerRun: DefaultAttemptsPerRun,
|
|
Escalation: []Step{}, Timing: Timing{}.normalised(),
|
|
ActionsPerRun: DefaultActionsPerRun, ActionsPerDay: DefaultActionsPerDay,
|
|
}, nil
|
|
}
|
|
var rows []stepRow
|
|
if err := json.Unmarshal(jsontext.Value(row.Escalation), &rows); err != nil {
|
|
return Settings{}, fmt.Errorf("read escalation path: %w", err)
|
|
}
|
|
escalation := make([]Step, 0, len(rows))
|
|
for _, r := range rows {
|
|
escalation = append(escalation, Step{Person: r.Person, Deadline: time.Duration(r.DeadlineMS) * time.Millisecond})
|
|
}
|
|
var timing Timing
|
|
if err := json.Unmarshal(jsontext.Value(row.Timing), &timing); err != nil {
|
|
return Settings{}, fmt.Errorf("read timing: %w", err)
|
|
}
|
|
return Settings{
|
|
Timing: timing.normalised(), ActionsPerRun: row.ActionsPerRun, ActionsPerDay: row.ActionsPerDay,
|
|
ApprovalDeadline: time.Duration(row.ApprovalDeadlineMS) * time.Millisecond,
|
|
RaiseAfter: row.RaiseAfter,
|
|
Frozen: row.Frozen,
|
|
RecurrenceThreshold: row.RecurrenceThreshold,
|
|
AttemptsPerRun: row.AttemptsPerRun,
|
|
DefaultOwner: row.DefaultOwner,
|
|
Escalation: escalation,
|
|
}, nil
|
|
}
|
|
|
|
// RaiseAfter is how many approvals in a row earn a proposal to raise an action
|
|
// class.
|
|
func (m *Module) RaiseAfter(ctx context.Context) (int, error) {
|
|
set, err := m.Settings(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return set.RaiseAfter, nil
|
|
}
|
|
|
|
// Frozen reports whether the organisation has frozen its actions.
|
|
func (m *Module) Frozen(ctx context.Context) (bool, error) {
|
|
set, err := m.Settings(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return set.Frozen, nil
|
|
}
|
|
|
|
// RecurrenceThreshold is how many incidents with one cause on one target it
|
|
// takes to propose a root-cause fix.
|
|
func (m *Module) RecurrenceThreshold(ctx context.Context) (int, error) {
|
|
set, err := m.Settings(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return set.RecurrenceThreshold, nil
|
|
}
|
|
|
|
// AttemptsPerRun is how many steps an agent may take in one run.
|
|
func (m *Module) AttemptsPerRun(ctx context.Context) (int, error) {
|
|
set, err := m.Settings(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return set.AttemptsPerRun, nil
|
|
}
|
|
|
|
// ApprovalDeadline is how long an approval request waits for an answer.
|
|
func (m *Module) ApprovalDeadline(ctx context.Context) (time.Duration, error) {
|
|
set, err := m.Settings(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return set.ApprovalDeadline, nil
|
|
}
|
|
|
|
// Register adds the settings API: organisation settings, the agent model and
|
|
// people.
|
|
func (m *Module) Register(api huma.API) {
|
|
tags := []string{"Governance"}
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "get-settings", Method: http.MethodGet, Path: "/api/settings",
|
|
Summary: "Get the organisation settings", Tags: tags,
|
|
}, m.getSettings)
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "put-settings", Method: http.MethodPut, Path: "/api/settings",
|
|
Summary: "Change the organisation settings", Tags: tags,
|
|
}, m.putSettings)
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "list-agent-models", Method: http.MethodGet, Path: "/api/settings/agent-models",
|
|
Summary: "List agent models by alias", Tags: tags,
|
|
}, m.listAgentModels)
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "get-agent-model", Method: http.MethodGet, Path: "/api/settings/agent-model",
|
|
Summary: "Get the agent model", Tags: tags,
|
|
}, m.getAgentModel)
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "put-agent-model", Method: http.MethodPut, Path: "/api/settings/agent-model",
|
|
Summary: "Set the agent model", Tags: tags,
|
|
}, m.putAgentModel)
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "test-agent-model", Method: http.MethodPost, Path: "/api/settings/agent-model/test",
|
|
Summary: "Ask a model one question before saving it", Tags: tags,
|
|
}, m.testAgentModel)
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "list-people", Method: http.MethodGet, Path: "/api/people",
|
|
Summary: "List the people", Tags: tags,
|
|
}, m.getPeople)
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "put-people", Method: http.MethodPut, Path: "/api/people",
|
|
Summary: "Replace the people", Tags: tags,
|
|
}, m.putPeople)
|
|
}
|
|
|
|
func (m *Module) getSettings(ctx context.Context, _ *struct{}) (*httpapi.Reply[settingsRequest], error) {
|
|
set, err := m.Settings(ctx)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "read settings", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the settings")
|
|
}
|
|
return httpapi.OK(set.request()), nil
|
|
}
|
|
|
|
func (m *Module) putSettings(ctx context.Context, in *struct{ Body settingsRequest }) (*httpapi.Reply[settingsRequest], error) {
|
|
req := in.Body
|
|
deadline, err := time.ParseDuration(req.ApprovalDeadline)
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, "approval deadline is a duration, such as 24h")
|
|
}
|
|
steps, err := stepsOf(req.Escalation)
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
set := Settings{
|
|
ApprovalDeadline: deadline, RaiseAfter: req.RaiseAfter, Frozen: req.Frozen,
|
|
RecurrenceThreshold: req.RecurrenceThreshold, AttemptsPerRun: req.AttemptsPerRun,
|
|
DefaultOwner: req.DefaultOwner, Escalation: steps,
|
|
Timing: req.Timing.normalised(), ActionsPerRun: req.ActionsPerRun, ActionsPerDay: req.ActionsPerDay,
|
|
}
|
|
if err := set.validate(); err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
people, err := m.store.People(ctx, m.orgID)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "read people", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the settings")
|
|
}
|
|
if err := set.knows(people); err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
rows := make([]stepRow, 0, len(set.Escalation))
|
|
for _, st := range set.Escalation {
|
|
rows = append(rows, stepRow{Person: st.Person, DeadlineMS: st.Deadline.Milliseconds()})
|
|
}
|
|
escalation, err := json.Marshal(rows)
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the settings")
|
|
}
|
|
timing, err := json.Marshal(set.Timing)
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the settings")
|
|
}
|
|
if err := m.store.PutSettings(ctx, store.Settings{
|
|
OrgID: m.orgID, ApprovalDeadlineMS: set.ApprovalDeadline.Milliseconds(),
|
|
RaiseAfter: set.RaiseAfter, Frozen: set.Frozen, RecurrenceThreshold: set.RecurrenceThreshold,
|
|
AttemptsPerRun: set.AttemptsPerRun, DefaultOwner: set.DefaultOwner, Escalation: string(escalation),
|
|
Timing: string(timing), ActionsPerRun: set.ActionsPerRun, ActionsPerDay: set.ActionsPerDay,
|
|
}); err != nil {
|
|
m.log.ErrorContext(ctx, "put settings", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the settings")
|
|
}
|
|
return httpapi.OK(set.request()), nil
|
|
}
|
|
|
|
// validate keeps a deadline inside the range an answer can be given in, the
|
|
// count of approvals a raise needs inside the range a class can earn, and the
|
|
// recurrence threshold inside the range that means a pattern.
|
|
func (s Settings) validate() error {
|
|
if s.RecurrenceThreshold < minRecurrenceThreshold || s.RecurrenceThreshold > maxRecurrenceThreshold {
|
|
return fmt.Errorf("recurrence threshold must be between %d and %d", minRecurrenceThreshold, maxRecurrenceThreshold)
|
|
}
|
|
if s.ApprovalDeadline < minApprovalDeadline || s.ApprovalDeadline > maxApprovalDeadline {
|
|
return fmt.Errorf("approval deadline must be between %s and %s", minApprovalDeadline, maxApprovalDeadline)
|
|
}
|
|
if s.RaiseAfter < 1 || s.RaiseAfter > maxRaiseAfter {
|
|
return fmt.Errorf("raise after must be between 1 and %d approvals", maxRaiseAfter)
|
|
}
|
|
if s.AttemptsPerRun < minAttemptsPerRun || s.AttemptsPerRun > maxAttemptsPerRun {
|
|
return fmt.Errorf("attempts per run must be between %d and %d", minAttemptsPerRun, maxAttemptsPerRun)
|
|
}
|
|
if err := validDeadlines(s.Escalation); err != nil {
|
|
return err
|
|
}
|
|
if s.ActionsPerRun < 1 || s.ActionsPerRun > maxActionBudget || s.ActionsPerDay < s.ActionsPerRun || s.ActionsPerDay > maxActionBudget {
|
|
return fmt.Errorf("actions per run must be at least 1, and actions per day at least that and at most %d", maxActionBudget)
|
|
}
|
|
return s.Timing.validate()
|
|
}
|
|
|
|
// validDeadlines keeps each escalation step's deadline inside the range the
|
|
// approval deadline takes.
|
|
func validDeadlines(path []Step) error {
|
|
for _, st := range path {
|
|
if st.Deadline < minApprovalDeadline || st.Deadline > maxApprovalDeadline {
|
|
return fmt.Errorf("each escalation deadline must be between %s and %s", minApprovalDeadline, maxApprovalDeadline)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// knows checks that the default owner and every person on the escalation path
|
|
// is one of people, and that no one is on the path twice.
|
|
func (s Settings) knows(people []store.Person) error {
|
|
known := func(id string) bool {
|
|
return slices.ContainsFunc(people, func(p store.Person) bool { return p.ID.String() == id })
|
|
}
|
|
if s.DefaultOwner != "" && !known(s.DefaultOwner) {
|
|
return errors.New("the default owner must be one of the people")
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, st := range s.Escalation {
|
|
if !known(st.Person) {
|
|
return errors.New("everyone on the escalation path must be one of the people")
|
|
}
|
|
if seen[st.Person] {
|
|
return errors.New("a person can be on the escalation path once")
|
|
}
|
|
seen[st.Person] = true
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Module) listAgentModels(ctx context.Context, _ *struct{}) (*httpapi.Reply[map[string]Model], error) {
|
|
rows, err := m.store.AgentModelRoles(ctx, m.orgID)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "list agent models", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't list agent models")
|
|
}
|
|
out := make(map[string]Model, len(rows))
|
|
for _, row := range rows {
|
|
alias := strings.TrimPrefix(row.Role, agentAliasPrefix)
|
|
if row.Role == roleAgent {
|
|
alias = "default"
|
|
}
|
|
out[alias] = Model{Class: row.Class, BaseURL: row.BaseURL, Model: row.Model, APIKeyCredential: row.APIKeyCredential}
|
|
}
|
|
return httpapi.OK(out), nil
|
|
}
|
|
|
|
type agentModelAlias struct {
|
|
Alias string `query:"alias" doc:"Agent model alias; omit for the default model"`
|
|
}
|
|
|
|
func (m *Module) getAgentModel(ctx context.Context, in *agentModelAlias) (*httpapi.Reply[Model], error) {
|
|
if err := ValidateAgentModelAlias(in.Alias); err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
model, err := m.AgentModelForAlias(ctx, in.Alias)
|
|
switch {
|
|
case errors.Is(err, ErrNoAgentModel):
|
|
return nil, huma.NewError(http.StatusNotFound, err.Error())
|
|
case err != nil:
|
|
m.log.ErrorContext(ctx, "read agent model", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the agent model")
|
|
default:
|
|
return httpapi.OK(model), nil
|
|
}
|
|
}
|
|
|
|
type agentModelInput struct {
|
|
Alias string `query:"alias" doc:"Agent model alias; omit for the default model"`
|
|
Body Model
|
|
}
|
|
|
|
// putAgentModel stores the resolved base URL so saved settings are explicit.
|
|
func (m *Module) putAgentModel(ctx context.Context, in *agentModelInput) (*httpapi.Reply[Model], error) {
|
|
if err := ValidateAgentModelAlias(in.Alias); err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
req := in.Body
|
|
model, err := settle(req)
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
if _, _, err := m.provider(ctx, model); err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
if err := m.store.PutModelRole(ctx, store.ModelRole{
|
|
OrgID: m.orgID, Role: agentModelRole(in.Alias), Class: model.Class, BaseURL: model.BaseURL, Model: model.Model,
|
|
APIKeyCredential: model.APIKeyCredential,
|
|
}); err != nil {
|
|
m.log.ErrorContext(ctx, "put agent model", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the agent model")
|
|
}
|
|
return httpapi.OK(model), nil
|
|
}
|
|
|
|
// modelAnswer is a test call's answer.
|
|
type modelAnswer struct {
|
|
Model string `json:"model"`
|
|
Answer string `json:"answer"`
|
|
}
|
|
|
|
// testTimeout bounds a test call, so a slow endpoint answers the operator with
|
|
// a failure instead of holding the request open.
|
|
const testTimeout = 60 * time.Second
|
|
|
|
// testAgentModel asks the model one question and reports its answer, so the
|
|
// operator knows the endpoint, model and key work before saving them.
|
|
func (m *Module) testAgentModel(ctx context.Context, in *struct{ Body Model }) (*httpapi.Reply[modelAnswer], error) {
|
|
req := in.Body
|
|
model, err := settle(req)
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
provider, _, err := m.provider(ctx, model)
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, testTimeout)
|
|
defer cancel()
|
|
resp, err := provider.Chat(ctx, chat.Request{
|
|
Model: model.Model,
|
|
Messages: []chat.Message{{Role: chat.RoleUser, Content: "Reply with the single word OK."}},
|
|
MaxTokens: 32,
|
|
})
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusBadGateway, err.Error())
|
|
}
|
|
return httpapi.OK(modelAnswer{Model: model.Model, Answer: resp.Content}), nil
|
|
}
|
|
|
|
func settle(req Model) (Model, error) {
|
|
if _, ok := runtime.GetClass(req.Class); !ok {
|
|
return Model{}, fmt.Errorf("unknown provider class %q", req.Class)
|
|
}
|
|
if req.Model == "" {
|
|
return Model{}, errors.New("a model is required")
|
|
}
|
|
if req.BaseURL == "" {
|
|
base, ok := runtime.DefaultBaseURL(req.Class)
|
|
if !ok {
|
|
return Model{}, fmt.Errorf("provider class %q has no default base URL; give one", req.Class)
|
|
}
|
|
req.BaseURL = base
|
|
}
|
|
u, err := url.Parse(req.BaseURL)
|
|
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
|
|
return Model{}, fmt.Errorf("base URL %q must be an http or https URL", req.BaseURL)
|
|
}
|
|
return req, nil
|
|
}
|