231 lines
9.2 KiB
Go
231 lines
9.2 KiB
Go
package controlplanestore
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
|
)
|
|
|
|
// resourceReader returns a resource kind's value and version; found is false
|
|
// when the store holds none.
|
|
|
|
// RuntimeConfig layers stored resources over base and returns each kind's
|
|
// version.
|
|
func (c *Client) RuntimeConfig(ctx context.Context, base config.Config) (config.Config, map[string]int64, error) {
|
|
return runtimeConfigFrom(ctx, c, base)
|
|
}
|
|
|
|
// RuntimeChatConfig layers the stored channel settings over the file document's
|
|
// chat section.
|
|
|
|
// StoredRuntimeConfig is RuntimeConfig over this server's own store. Kinds
|
|
// not stored are read as their seed.
|
|
func (s *Server) StoredRuntimeConfig(ctx context.Context, base config.Config) (config.Config, map[string]int64, error) {
|
|
seeds, err := s.seededValues(base)
|
|
if err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
return runtimeConfigFrom(ctx, storeReader{resources: s.store, seeds: seeds}, base)
|
|
}
|
|
|
|
// storeReader reads this server's store, returning the seed for kinds it
|
|
// does not hold.
|
|
type storeReader struct {
|
|
resources ResourceStore
|
|
seeds map[string][]byte
|
|
}
|
|
|
|
func (r storeReader) Query(ctx context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
|
resource, err := r.resources.Resource(ctx, storecontract.DefaultOrgID, kind)
|
|
if errors.Is(err, storecontract.ErrResourceNotFound) {
|
|
seed, ok := r.seeds[kind]
|
|
if !ok {
|
|
return 0, false, fmt.Errorf("read %s: %w", kind, storecontract.ErrResourceNotFound)
|
|
}
|
|
if err := decode(seed); err != nil {
|
|
return 0, false, fmt.Errorf("decode %s seed: %w", kind, err)
|
|
}
|
|
// No version: nothing has been stored for this kind, so nothing has
|
|
// been applied either. It is still a value to layer in -- the seed --
|
|
// which is why this reader never reports the kind absent.
|
|
return 0, true, nil
|
|
}
|
|
if err != nil {
|
|
return 0, false, fmt.Errorf("read %s: %w", kind, err)
|
|
}
|
|
if err := decode(resource.Value); err != nil {
|
|
return 0, false, fmt.Errorf("decode %s: %w", kind, err)
|
|
}
|
|
return resource.Version, true, nil
|
|
}
|
|
|
|
func runtimeConfigFrom(ctx context.Context, reader controlplanerpc.ResourceReader, base config.Config) (config.Config, map[string]int64, error) {
|
|
out := base.Clone()
|
|
versions := map[string]int64{}
|
|
if err := layerResource(ctx, reader, versions, ProviderSettingsKind, func(providers map[string]providerDocument) error {
|
|
out.Providers = make(map[string]config.Provider, len(providers))
|
|
for name, provider := range providers {
|
|
if err := rejectBootDerivedProviderEnv(name, provider); err != nil {
|
|
return err
|
|
}
|
|
out.Providers[name] = config.Provider{Class: provider.Class, APIKeyEnv: provider.APIKeyEnv, APIKey: provider.APIKey, BaseURL: provider.BaseURL}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
// Stored aliases replace cfg.Models outright.
|
|
if err := layerResource(ctx, reader, versions, ModelAliasesKind, func(aliases map[string]string) error {
|
|
out.Models = aliases
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
if err := layerResourceJSON(ctx, reader, versions, RepositoryPoliciesKind, &out.Repos); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
// The instance's own forge is its root connection: the file's [forge]
|
|
// section migrated into that resource, so a connection the operator
|
|
// edits or removes is what the daemon reaches the forge with. Intake and
|
|
// the webhook secret are instance infrastructure and stay the file's.
|
|
if err := layerResource(ctx, reader, versions, ForgeConnectionsKind, func(connections []config.ForgeConnection) error {
|
|
forge := out.Forge
|
|
forge.Type, forge.Host, forge.Token = "", "", config.SecretRef{}
|
|
if connection, ok := rootConnection(connections); ok {
|
|
forge.Type, forge.Host, forge.Token = connection.Type, connection.Host, connection.Token
|
|
}
|
|
out.Forge = forge
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
chat, chatVersion, err := controlplanerpc.RuntimeChatConfigFrom(ctx, reader, out.Chat)
|
|
if err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
out.Chat, versions[ChannelSettingsKind] = chat, chatVersion
|
|
if err := layerResource(ctx, reader, versions, SchedulingPolicyKind, func(policy schedulingPolicy) error {
|
|
interval, err := time.ParseDuration(policy.PollInterval)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out.PollInterval, out.MaxRetries, out.Dispatch = config.Duration(interval), policy.MaxRetries, policy.Dispatch
|
|
// A stored label replaces the file's.
|
|
if policy.Label != nil {
|
|
out.Label = *policy.Label
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
return runtimeToolConfigFrom(ctx, reader, versions, out)
|
|
}
|
|
|
|
func runtimeToolConfigFrom(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, out config.Config) (config.Config, map[string]int64, error) {
|
|
policy := taskPolicyOf(out)
|
|
if err := layerResourceJSON(ctx, reader, versions, TaskPolicyKind, &policy); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
out.Notify = config.Notify{Webhook: policy.NotifyWebhook, MinSeverity: policy.NotifyMinSeverity, Secret: policy.NotifySecret}
|
|
|
|
if err := layerResource(ctx, reader, versions, ToolSettingsKind, func(settings toolSettings) error {
|
|
headers := make(map[string]map[string]string, len(out.Tools.MCPServers))
|
|
fileParallelToolCalls := make(map[string]bool, len(out.Tools.MCPServers))
|
|
for _, server := range out.Tools.MCPServers {
|
|
headers[server.Name] = server.Headers
|
|
fileParallelToolCalls[server.Name] = server.ParallelToolCalls
|
|
}
|
|
servers := make([]config.MCPServer, 0, len(settings.MCPServers))
|
|
for _, server := range settings.MCPServers {
|
|
// cfg.Tools is replaced wholesale, so keep file-owned fields: headers, and
|
|
// ParallelToolCalls when not stored.
|
|
parallelToolCalls := fileParallelToolCalls[server.Name]
|
|
if server.ParallelToolCalls != nil {
|
|
parallelToolCalls = *server.ParallelToolCalls
|
|
}
|
|
servers = append(servers, config.MCPServer{
|
|
Name: server.Name,
|
|
Transport: server.Transport,
|
|
Command: server.Command,
|
|
Args: server.Args,
|
|
WorkDir: server.WorkDir,
|
|
URL: server.URL,
|
|
Headers: headers[server.Name],
|
|
SSEEndpoint: server.SSEEndpoint,
|
|
MessageEndpoint: server.MessageEndpoint,
|
|
ParallelToolCalls: parallelToolCalls,
|
|
})
|
|
}
|
|
out.Tools = config.ToolsConfig{MCPServers: servers, Policy: settings.Policy, WebFetch: settings.WebFetch, Minimax: config.MinimaxConfig{Enabled: settings.Minimax.Enabled, APIKey: settings.Minimax.APIKey, BaseURL: settings.Minimax.BaseURL}}
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
if err := layerResource(ctx, reader, versions, PluginSettingsKind, func(settings pluginSettings) error {
|
|
out.SkillsDir = settings.SkillsDir
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
if err := layerResource(ctx, reader, versions, ContainerRuntimePoliciesKind, func(policies containerRuntimePolicies) error {
|
|
// Keep the file-owned Profiles and RegistryAuth across the replacement.
|
|
profiles := out.Containers.Profiles
|
|
registryAuth := out.Containers.RegistryAuth
|
|
out.Containers = policies.settings()
|
|
out.Containers.Profiles = profiles
|
|
out.Containers.RegistryAuth = registryAuth
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
// Stored profiles replace the file's outright.
|
|
if err := layerResource(ctx, reader, versions, AgentProfileKind, func(profiles map[string]agentProfile) error {
|
|
out.Containers.Profiles = agentProfilesSettings(profiles)
|
|
return nil
|
|
}); err != nil {
|
|
return config.Config{}, nil, err
|
|
}
|
|
err := layerCredentials(ctx, reader, versions, &out)
|
|
return out, versions, err
|
|
}
|
|
|
|
// layerResource decodes a stored JSON resource into a T, applies it and
|
|
// records its version. A kind with no stored value is skipped.
|
|
func layerResource[T any](ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, kind string, apply func(T) error) error {
|
|
return layerRaw(ctx, reader, versions, kind, func(value []byte) error {
|
|
var v T
|
|
if err := json.Unmarshal(value, &v); err != nil {
|
|
return err
|
|
}
|
|
return apply(v)
|
|
})
|
|
}
|
|
|
|
// layerResourceJSON decodes a stored resource over target, keeping fields
|
|
// the stored document omits.
|
|
func layerResourceJSON(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, kind string, target any) error {
|
|
return layerRaw(ctx, reader, versions, kind, func(value []byte) error { return json.Unmarshal(value, target) })
|
|
}
|
|
|
|
func layerRaw(ctx context.Context, reader controlplanerpc.ResourceReader, versions map[string]int64, kind string, decode func([]byte) error) error {
|
|
version, found, err := reader.Query(ctx, kind, decode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if found {
|
|
versions[kind] = version
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// query returns the version of the resource it decoded, so callers that layer
|
|
// a resource in can report the version they applied. A kind the store holds no
|
|
// value for is reported as not found rather than as an error: the caller leaves
|
|
// the file document's value in effect (see resourceReader).
|