Files
archie/internal/infrastructure/controlplanestore/channel_settings.go
T
sam af7577625f
deploy / plan (push) Successful in 11s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
deploy / gate (push) Canceled after 7m26s
refactor(messaging): store Telegram's connection in extension-settings (#1802)
2026-11-08 17:12:59 +11:00

72 lines
2.8 KiB
Go

package controlplanestore
import (
"encoding/json"
"fmt"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
)
// Aliases for the channel-settings document defined in controlplanerpc.
type (
channelSettings = controlplanerpc.ChannelSettings
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,
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")
}
return nil
})
}
// normalizeChannels re-encodes the stored document with snake_case keys.
func normalizeChannels(input []byte) ([]byte, error) {
var settings channelSettings
if err := json.Unmarshal(input, &settings); err != nil {
return nil, err
}
return json.Marshal(settings)
}
// checkChannelWrite keeps an org to the settings its own channel consumes.
// The workspace, filesystem, step, rate-limit and operator settings configure
// the instance's gateway, and an org's bot takes its token from the org's
// credential binding named telegram. The system org's channel acts for no one,
// so it names no agent and no bot users: the instance's bot is the telegram
// package, configured in its extension settings.
func checkChannelWrite(orgID string, value []byte) error {
var doc channelSettings
if err := json.Unmarshal(value, &doc); err != nil {
return err
}
if orgID == storecontract.DefaultOrgID {
if doc.Agent != "" || len(doc.AllowedUserIDs) > 0 {
return fmt.Errorf("%w: the instance channel acts for no agent and its Telegram users are the telegram extension's settings", ErrValidation)
}
return nil
}
if doc.Operator != "" || doc.Workspace != "" || doc.UnrestrictedFilesystem || doc.MaxSteps != 0 ||
doc.RateLimit != (rateLimitSettings{}) {
return fmt.Errorf("%w: operator, workspace, filesystem, step and rate-limit settings are the instance's; an org sets its Telegram users, tool-call display and agent, and binds its bot token as the credential telegram", ErrValidation)
}
return nil
}