Files
archie/internal/app/controlplane/model_settings.go
T
sam a19cd24b83 feat(controlplane): derive resource schemas from the documents they describe
Twelve definitions each advertised a bare {"type":"object"} or {"type":"array"},
so no resource descriptor carried a single field: the Web UI had nothing to
label, describe or format a field with, and the one component that parsed a
schema was dead code. The constants could not be kept honest either -- a schema
written beside a struct drifts the moment a field is added, and nothing compares
the two.

Definition now names its document type and Descriptor derives the schema from it.
json tags give the keys, falling back to the Go field name because that is what
encoding/json stores; the Go type gives type and format; and a field's own
title, format and doc tags override. A duration is a string with format
"duration", which is how every duration field in the catalogue -- poll_interval,
max_uptime, rate_limit.window -- gets a client-side affordance from one rule
instead of twelve hand-written ones.

Deliberately structural: it emits no prose of its own. Field descriptions already
have one home (internal/webui/config_schema.go, keyed by dotted config path), and
a second would be two places to keep the same fact true. It also never emits
"required", because an empty string is legitimate for nearly every field here --
a blank workspace, a disabled rate limit -- so requiring most of them would
invite a client to enforce something the document does not mean.

Guarded by TestEveryResourceDescriptorDerivesProperties, which walks the catalogue
the production constructor builds and fails if any kind describes no fields --
the assertion that would have caught all twelve. It accepts a keyed collection's
additionalProperties as described, because a map document has no fixed keys.

Verified: controlplane, workflow, webui, archieui and staterpc suites, go vet,
go build ./..., golangci-lint run ./... (0 issues), docs:check.
2026-09-22 03:50:53 +10:00

66 lines
2.3 KiB
Go

package controlplane
import (
"fmt"
"net/url"
"strings"
"github.com/samcharles93/archie-core/internal/config"
)
const (
ProviderSettingsKind = "provider-settings"
ModelRoleAssignmentsKind = "model-role-assignments"
)
type providerDocument struct {
Class string `json:"class"`
APIKeyEnv string `json:"api_key_env,omitempty"`
APIKey config.SecretRef `json:"api_key_ref"`
BaseURL string `json:"base_url,omitempty"`
HasCredential bool `json:"has_credential"`
}
func modelDefinitions() []Definition {
return []Definition{
{Kind: ProviderSettingsKind, Title: "Providers", ApplyMode: "restart-required", Document: map[string]providerDocument{}, Seed: seedProviders, Validate: validateProviders},
{Kind: ModelRoleAssignmentsKind, Title: "Model role assignments", ApplyMode: "restart-required", Document: map[string]string{}, Seed: func(cfg config.Config) any { return cfg.Models }, Validate: validateModelRoles},
}
}
func seedProviders(cfg config.Config) any {
out := make(map[string]providerDocument, len(cfg.Providers))
for name, provider := range cfg.Providers {
out[name] = providerDocument{Class: provider.Class, APIKeyEnv: provider.APIKeyEnv, APIKey: provider.APIKey, BaseURL: provider.BaseURL, HasCredential: provider.APIKeyEnv != "" || provider.APIKey != (config.SecretRef{})}
}
return out
}
func validateProviders(input []byte) error {
return validateAs(input, func(providers map[string]providerDocument) error {
for name, provider := range providers {
if strings.TrimSpace(name) == "" || strings.TrimSpace(provider.Class) == "" {
return fmt.Errorf("provider name and class are required")
}
if provider.BaseURL != "" {
parsed, err := url.Parse(provider.BaseURL)
if err != nil || parsed.Scheme == "" || parsed.Host == "" || parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" {
return fmt.Errorf("provider %q has invalid base_url", name)
}
}
}
return nil
})
}
func validateModelRoles(input []byte) error {
return validateAs(input, func(models map[string]string) error {
for role, model := range models {
if strings.TrimSpace(role) == "" || !strings.Contains(model, "/") {
return fmt.Errorf("model role %q must reference provider/model", role)
}
}
return nil
})
}