feat(governance): the agent model names a credential and answers a test call (#2026)
This commit is contained in:
4 files changed
+98
-15
No files matched your search
@@ -0,0 +1,7 @@
|
||||
-- +goose Up
|
||||
-- A model role names the credential whose token authenticates it. The token
|
||||
-- stays sealed in the connectors module; only the name is stored here.
|
||||
ALTER TABLE governance.model_roles ADD COLUMN api_key_credential text NOT NULL DEFAULT '';
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE governance.model_roles DROP COLUMN api_key_credential;
|
||||
@@ -180,15 +180,41 @@ 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"`
|
||||
}
|
||||
|
||||
// Provider builds the chat client for m. Only keyless providers work until
|
||||
// secret engines can supply a provider key.
|
||||
func (m Model) Provider(ctx context.Context) (chat.Provider, chat.ModelInfo, error) {
|
||||
// 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: m.Class, BaseURL: m.BaseURL, Auth: runtime.AuthConfig{Type: runtime.AuthTypeNone}},
|
||||
roleAgent: {ID: roleAgent, Class: model.Class, BaseURL: model.BaseURL, Auth: auth},
|
||||
}})
|
||||
ref := roleAgent + "/" + m.Model
|
||||
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)
|
||||
@@ -202,9 +228,10 @@ func (m Model) Provider(ctx context.Context) (chat.Provider, chat.ModelInfo, err
|
||||
|
||||
// Module serves one organisation's settings.
|
||||
type Module struct {
|
||||
orgID string
|
||||
store *store.Store
|
||||
log *slog.Logger
|
||||
orgID string
|
||||
store *store.Store
|
||||
log *slog.Logger
|
||||
credentials Credentials
|
||||
}
|
||||
|
||||
// NewModule returns the governance module for orgID. The ai-sdk provider
|
||||
@@ -213,6 +240,10 @@ func NewModule(db *sqlx.DB, log *slog.Logger, orgID string) *Module {
|
||||
return &Module{orgID: orgID, store: store.New(db), log: log}
|
||||
}
|
||||
|
||||
// UseCredentials gives the module the sealed store a model's key reference
|
||||
// resolves through.
|
||||
func (m *Module) UseCredentials(c Credentials) { m.credentials = c }
|
||||
|
||||
// AgentModel returns the organisation's agent model.
|
||||
func (m *Module) AgentModel(ctx context.Context) (Model, error) {
|
||||
role, ok, err := m.store.ModelRole(ctx, m.orgID, roleAgent)
|
||||
@@ -222,7 +253,7 @@ func (m *Module) AgentModel(ctx context.Context) (Model, error) {
|
||||
if !ok {
|
||||
return Model{}, ErrNoAgentModel
|
||||
}
|
||||
return Model{Class: role.Class, BaseURL: role.BaseURL, Model: role.Model}, nil
|
||||
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
|
||||
@@ -315,6 +346,7 @@ func (m *Module) ApprovalDeadline(ctx context.Context) (time.Duration, error) {
|
||||
func (m *Module) Routes(mux *http.ServeMux, guard func(http.Handler) http.Handler) {
|
||||
mux.Handle("GET /api/settings/agent-model", guard(http.HandlerFunc(m.getAgentModel)))
|
||||
mux.Handle("PUT /api/settings/agent-model", guard(http.HandlerFunc(m.putAgentModel)))
|
||||
mux.Handle("POST /api/settings/agent-model/test", guard(http.HandlerFunc(m.testAgentModel)))
|
||||
mux.Handle("GET /api/settings", guard(http.HandlerFunc(m.getSettings)))
|
||||
mux.Handle("PUT /api/settings", guard(http.HandlerFunc(m.putSettings)))
|
||||
mux.Handle("GET /api/people", guard(http.HandlerFunc(m.getPeople)))
|
||||
@@ -478,12 +510,13 @@ func (m *Module) putAgentModel(w http.ResponseWriter, r *http.Request) {
|
||||
httpjson.Error(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if _, _, err := model.Provider(r.Context()); err != nil {
|
||||
if _, _, err := m.provider(r.Context(), model); err != nil {
|
||||
httpjson.Error(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if err := m.store.PutModelRole(r.Context(), store.ModelRole{
|
||||
OrgID: m.orgID, Role: roleAgent, Class: model.Class, BaseURL: model.BaseURL, Model: model.Model,
|
||||
APIKeyCredential: model.APIKeyCredential,
|
||||
}); err != nil {
|
||||
m.log.ErrorContext(r.Context(), "put agent model", "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't save the agent model")
|
||||
@@ -492,6 +525,42 @@ func (m *Module) putAgentModel(w http.ResponseWriter, r *http.Request) {
|
||||
httpjson.Write(w, http.StatusOK, model)
|
||||
}
|
||||
|
||||
// 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(w http.ResponseWriter, r *http.Request) {
|
||||
var req Model
|
||||
if err := httpjson.Read(r, &req); err != nil {
|
||||
httpjson.Error(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
model, err := settle(req)
|
||||
if err != nil {
|
||||
httpjson.Error(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
provider, _, err := m.provider(r.Context(), model)
|
||||
if err != nil {
|
||||
httpjson.Error(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 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 {
|
||||
httpjson.Error(w, http.StatusBadGateway, err.Error())
|
||||
return
|
||||
}
|
||||
httpjson.Write(w, http.StatusOK, map[string]string{"model": model.Model, "answer": resp.Content})
|
||||
}
|
||||
|
||||
func settle(req Model) (Model, error) {
|
||||
if _, ok := runtime.GetClass(req.Class); !ok {
|
||||
return Model{}, fmt.Errorf("unknown provider class %q", req.Class)
|
||||
|
||||
@@ -18,6 +18,9 @@ type ModelRole struct {
|
||||
Class string `db:"class"`
|
||||
BaseURL string `db:"base_url"`
|
||||
Model string `db:"model"`
|
||||
// APIKeyCredential names the credential whose token authenticates the
|
||||
// model provider, or empty when it needs no key.
|
||||
APIKeyCredential string `db:"api_key_credential"`
|
||||
}
|
||||
|
||||
// Store is the governance module's Postgres store.
|
||||
@@ -33,7 +36,7 @@ func New(db *sqlx.DB) *Store { return &Store{db: db} }
|
||||
func (s *Store) ModelRole(ctx context.Context, orgID, role string) (ModelRole, bool, error) {
|
||||
var out ModelRole
|
||||
err := s.db.GetContext(ctx, &out, `
|
||||
SELECT org_id, role, class, base_url, model FROM governance.model_roles
|
||||
SELECT org_id, role, class, base_url, model, api_key_credential FROM governance.model_roles
|
||||
WHERE org_id = $1 AND role = $2`, orgID, role)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ModelRole{}, false, nil
|
||||
@@ -47,10 +50,11 @@ func (s *Store) ModelRole(ctx context.Context, orgID, role string) (ModelRole, b
|
||||
// PutModelRole records the organisation's model for a role.
|
||||
func (s *Store) PutModelRole(ctx context.Context, m ModelRole) error {
|
||||
if _, err := s.db.NamedExecContext(ctx, `
|
||||
INSERT INTO governance.model_roles (org_id, role, class, base_url, model)
|
||||
VALUES (:org_id, :role, :class, :base_url, :model)
|
||||
INSERT INTO governance.model_roles (org_id, role, class, base_url, model, api_key_credential)
|
||||
VALUES (:org_id, :role, :class, :base_url, :model, :api_key_credential)
|
||||
ON CONFLICT (org_id, role) DO UPDATE
|
||||
SET class = EXCLUDED.class, base_url = EXCLUDED.base_url, model = EXCLUDED.model, updated_at = now()`, m); err != nil {
|
||||
SET class = EXCLUDED.class, base_url = EXCLUDED.base_url, model = EXCLUDED.model,
|
||||
api_key_credential = EXCLUDED.api_key_credential, updated_at = now()`, m); err != nil {
|
||||
return fmt.Errorf("put model role %q: %w", m.Role, err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -30,6 +30,9 @@ import (
|
||||
// model it runs, and the escalation path an approval request takes.
|
||||
type Settings interface {
|
||||
AgentModel(ctx context.Context) (governance.Model, error)
|
||||
// AgentProvider builds the chat client for a model, resolving the
|
||||
// credential it names. The model is journaled; the client is rebuilt.
|
||||
AgentProvider(ctx context.Context, model governance.Model) (chat.Provider, chat.ModelInfo, error)
|
||||
EscalationPath(ctx context.Context, owner string, needAuthority bool) ([]governance.PathStep, error)
|
||||
RecurrenceThreshold(ctx context.Context) (int, error)
|
||||
AttemptsPerRun(ctx context.Context) (int, error)
|
||||
@@ -182,7 +185,7 @@ func (d Deps) pin(rc *Context, p *progress, withMemory bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
provider, info, err := model.Provider(rc.Context())
|
||||
provider, info, err := d.Settings.AgentProvider(rc.Context(), model)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user