Files
archie/internal/app/controlplane/channel_settings.go
T
sam e5105280ad chore(config): remove token_env aliases for forge, artifacts and telegram
Each credential is now only a {engine, key} secret reference. A stale
token_env in a file is reported at startup as an unrecognised key.
nats.token_env stays (no secret-ref form), and providers.api_key_env stays:
it is the carrier the resolver writes each resolved key's env name into.
2026-09-24 23:58:26 +10:00

67 lines
3.1 KiB
Go

package controlplane
import (
"encoding/json"
"fmt"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
)
// The channel-settings DOCUMENT lives in internal/infrastructure/controlplanerpc,
// beside the client that projects it, so a process that only reads stored
// settings does not link this package's store-backed server and workflow engine
// (archie-core-1ng1). These aliases keep the store-backed side reading the same
// types by the same names it always used, so there is one definition of the
// document rather than a copy on each side of the boundary.
type (
channelSettings = controlplanerpc.ChannelSettings
telegramSettings = controlplanerpc.TelegramSettings
webhookChannelSettings = controlplanerpc.WebhookChannelSettings
emailSettings = controlplanerpc.EmailSettings
rateLimitSettings = controlplanerpc.RateLimitSettings
channelDuration = controlplanerpc.ChannelDuration
)
// ChannelSettingsKind is re-exported so the store-backed side and its callers
// keep naming one kind by one name.
const ChannelSettingsKind = controlplanerpc.ChannelSettingsKind
func seedChannels(cfg config.Config) any {
chat := cfg.Chat
return channelSettings{
Operator: chat.Operator, Workspace: chat.Workspace, UnrestrictedFilesystem: chat.UnrestrictedFilesystem, ShowToolCalls: chat.ShowToolCalls, MaxSteps: chat.MaxSteps, Models: chat.Models,
Email: emailSettings{ListenAddr: chat.Email.ListenAddr, RelayAddr: chat.Email.RelayAddr},
WebhookAddr: chat.WebhookAddr,
Telegram: telegramSettings{AllowedUserIDs: chat.Telegram.AllowedUserIDs, Token: chat.Telegram.Token, CredentialConfigured: chat.Telegram.Token != (config.SecretRef{})},
Webhook: webhookChannelSettings{Path: chat.Webhook.Path, Secret: chat.Webhook.Secret, CredentialConfigured: chat.Webhook.Secret != (config.SecretRef{}), Template: chat.Webhook.Template, DeliverTo: chat.Webhook.DeliverTo},
RateLimit: rateLimitSettings{Window: channelDuration(chat.RateLimit.Window), MaxRequests: chat.RateLimit.MaxRequests},
}
}
func validateChannels(input []byte) error {
return validateAs(input, func(settings channelSettings) error {
if settings.MaxSteps < 0 || settings.RateLimit.MaxRequests < 0 || settings.RateLimit.Window < 0 {
return fmt.Errorf("channel limits must not be negative")
}
if settings.Webhook.DeliverTo != "" && settings.Webhook.DeliverTo != "origin" {
return fmt.Errorf("webhook deliver_to must be empty or origin")
}
return nil
})
}
// normalizeChannels canonicalizes the stored document: decoding into the shape
// above and re-encoding writes the snake_case keys and the string duration,
// dropping the Go-cased keys an earlier revision stored. A document that still
// carries them converges the next time it is written; reads tolerate them
// meanwhile (emailSettings, rateLimitSettings), so nothing has to be re-saved
// before it can be read again.
func normalizeChannels(input []byte) ([]byte, error) {
var settings channelSettings
if err := json.Unmarshal(input, &settings); err != nil {
return nil, err
}
return json.Marshal(settings)
}