Files
archie/internal/gateway/persona.go
T
sam e3b1a859a8 feat(gateway): flip channel boundary onto messaging.Message
Replaced gateway.Message as the currency at the channel-facing boundary
with the canonical internal/domain/messaging.Message. Channel adapters
now build the record directly (RoleUser, ConversationID, Sender) and the
Router hands it back unchanged, so nothing converts between two shapes in
process. gateway.Message is deleted; the dashboard route that had no home
in the record lives in a new Inbound envelope (Message + transport-only
Page), which reaches the system prompt and is never persisted.

Role is now set at construction instead of derived at the store boundary,
and the assistant reply is a direct messaging.Message (RoleAssistant,
Sender=botUser). The gRPC proto is unchanged: pb.Message still carries no
role, and both sides keep deriving it from the owning session's bot
identity via gateway.RoleForSender (ToStoredMessage/FromStoredMessage are
deleted).

Adds tests that the old shape could not express: Page reaches the system
prompt but not the persisted record, and a channel-built Inbound round-trips
with the Role the channel set.

Refs: archie-core-9g5e
2026-09-09 01:05:36 +10:00

154 lines
4.7 KiB
Go

package gateway
import (
"context"
"fmt"
"strings"
"sync"
"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 {
return []Persona{
{
Name: "archie",
Prompt: "You are Archie, a capable coding and project assistant. " +
"Be direct without being brusque, grounded in the actual workspace state, and use available tools when asked. " +
"Do not claim tools, files, memories, actions, or results you have not verified. " +
"Do not impersonate another assistant, provider, or vendor.",
},
{Name: "helpful", Prompt: "You are a helpful, friendly AI assistant."},
{Name: "concise", Prompt: "You are a concise assistant. Keep responses brief and to the point."},
{Name: "technical", Prompt: "You are a technical expert. Provide detailed, accurate technical information."},
{Name: "creative", Prompt: "You are a creative assistant. Think outside the box and offer innovative solutions."},
{Name: "teacher", Prompt: "You are a patient teacher. Explain concepts clearly with examples."},
}
}
// 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)
}
return names
}
// 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(), " ")
}