Files
archie/internal/gateway/persona.go
T
sam 02720c8e81 feat(controlplane): runtime administration over the State Store's gRPC server
The Web UI and messaging channels now read and write Archie's runtime
settings through one versioned, audited control-plane API hosted on the
State Store's existing gRPC server: catalog, query, command, watch. Each
feature owns its resource, validation and commands; storage only writes
versions and audit records.

Workflows move with it. Definitions become portable YAML compiled through
a closed, typed step registry and stored as a control-plane resource, so
the yaegi interpretation paths (internal/gate, wfeval, wfextract,
gateeval, gateextract, skillbuild) are deleted rather than carried.

The UI process reads definitions as data: the JSON projection lives in
internal/domain/workflow/task so a dashboard decodes the stored
collection without linking the workflow engine, which cmd/archie-ui's
deletion gate forbids. Shipped definitions ride the catalog descriptor
rather than a bespoke REST route, keeping the API to its four operations.

archie-messaging gains -state-store-target/-state-store-token and reads
channel settings from the database, failing closed when the State Store
is unreachable: the database is authoritative for those settings.
2026-09-21 08:17:42 +10:00

167 lines
4.6 KiB
Go

package gateway
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"github.com/samcharles93/archie-core/internal/domain/agent"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// Persona is a named communication style that modifies the system prompt.
type Persona struct {
Name string
Prompt string // prepended to the system prompt
}
// PersonaRegistry holds available personas and tracks the active one
// per session. Safe for concurrent use.
type PersonaRegistry struct {
mu sync.RWMutex
personas map[string]Persona
active map[string]string // sessionKey -> persona name
defaultName string
}
// NewPersonaRegistry creates a registry pre-populated with the given
// personas. The first persona is the default for new sessions.
func NewPersonaRegistry(personas []Persona) *PersonaRegistry {
r := &PersonaRegistry{
personas: make(map[string]Persona, len(personas)),
active: make(map[string]string),
}
for index, p := range personas {
r.personas[p.Name] = p
if index == 0 {
r.defaultName = p.Name
}
}
return r
}
// DefaultPersonas returns Archie as the baseline identity followed by
// optional communication styles.
func DefaultPersonas() []Persona {
shipped := agent.ShippedPersonas()
personas := make([]Persona, 0, len(shipped.Personas))
for _, persona := range shipped.Personas {
personas = append(personas, Persona{Name: persona.Name, Prompt: persona.Prompt})
}
return personas
}
// List returns all available persona names.
func (r *PersonaRegistry) List() []string {
r.mu.RLock()
defer r.mu.RUnlock()
names := make([]string, 0, len(r.personas))
for _, p := range r.personas {
names = append(names, p.Name)
}
sort.Strings(names)
return names
}
// Replace atomically changes the available definitions. Sessions whose
// selection no longer exists use the new default.
func (r *PersonaRegistry) Replace(personas []Persona, defaultName string) {
r.mu.Lock()
defer r.mu.Unlock()
next := make(map[string]Persona, len(personas))
for _, persona := range personas {
next[persona.Name] = persona
}
for session, name := range r.active {
if _, exists := next[name]; !exists {
delete(r.active, session)
}
}
r.personas = next
r.defaultName = defaultName
}
// SetActive sets the active persona for a session.
func (r *PersonaRegistry) SetActive(sessionKey, name string) bool {
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.personas[name]; !ok {
return false
}
r.active[sessionKey] = name
return true
}
// GetActive returns the active persona's prompt for a session, or an
// empty string when no persona is selected.
func (r *PersonaRegistry) GetActive(sessionKey string) string {
r.mu.RLock()
defer r.mu.RUnlock()
name, ok := r.active[sessionKey]
if !ok {
name = r.defaultName
}
p, ok := r.personas[name]
if !ok {
return ""
}
if name == r.defaultName {
return p.Prompt
}
if baseline, ok := r.personas[r.defaultName]; ok && baseline.Prompt != "" {
return baseline.Prompt + "\n\n" + p.Prompt
}
return p.Prompt
}
// ActiveName returns the selected persona name for a session, falling back to
// the registry default. It is used by channel adapters that render a compact
// selector summary rather than the full prompt.
func (r *PersonaRegistry) ActiveName(sessionKey string) string {
r.mu.RLock()
defer r.mu.RUnlock()
if name := r.active[sessionKey]; name != "" {
return name
}
return r.defaultName
}
// Get returns a persona by name, or false when not found.
func (r *PersonaRegistry) Get(name string) (Persona, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
p, ok := r.personas[name]
return p, ok
}
func (r *Router) handlePersonality(ctx context.Context, msg messaging.Message, rest string) (string, error) {
if r.Personas == nil {
return "Personality switching is not configured.", nil
}
name := strings.ToLower(strings.TrimSpace(rest))
if name == "" {
return fmt.Sprintf("Active personality: %s\nAvailable: %s", r.Personas.ActiveName(r.sessionKey(ctx, msg)), strings.Join(r.Personas.List(), ", ")), nil
}
if !r.Personas.SetActive(r.sessionKey(ctx, msg), name) {
return fmt.Sprintf("Unknown personality %q. Available: %s", name, strings.Join(r.Personas.List(), ", ")), nil
}
return fmt.Sprintf("Personality set to %q.", name), nil
}
func (r *Router) sessionKey(ctx context.Context, msg messaging.Message) string {
if r.sessionTracker == nil {
return msg.ConversationID.ChannelID
}
key, err := r.ResolveSessionKey(ctx, Inbound{Message: msg})
if err != nil {
return msg.ConversationID.ChannelID
}
return key
}
func (r *Router) helpText() string {
return "Send a message to chat with Archie. Available commands:\n" + strings.Join(LocalCommands(), " ")
}