Files
archie/internal/gateway/persona.go
T
sam 68ba3456ee feat(webui): chat from the dashboard
Brings the chat surface to the Web UI: /api/chat with its own handlers
and tests, a self-contained ui/src/chat feature folder, and the shared
plumbing to reach the same router Telegram uses.

Includes a dangerous-command approval path (internal/gateway/dangerous.go,
internal/webui/dangerous.go, and the Telegram counterpart) so /rollback
and /stop are gated the same way on both surfaces, persona handling for
web sessions, and release-update formatting split out of the Telegram
service so both can render it.

web_parity_test.go pins that the two surfaces expose the same commands.

Authored in a parallel session; committed here so it lands with the rest
rather than sitting uncommitted while other work builds on top of it.
task check passes with it in place.

Screenshots used while building are gitignored (*.png) rather than
committed, matching the call made on the earlier design reference.
2026-08-05 10:48:51 +10:00

152 lines
4.5 KiB
Go

package gateway
import (
"context"
"fmt"
"strings"
"sync"
)
// 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 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 Message) string {
if r.sessionTracker == nil {
return msg.ChannelID
}
key, err := r.ResolveSessionKey(ctx, msg)
if err != nil {
return msg.ChannelID
}
return key
}
func (r *Router) helpText() string {
return "Send a message to chat with Archie. Available commands:\n" + strings.Join(LocalCommands(), " ")
}