159 lines
5.1 KiB
Go
159 lines
5.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// chatModer is the optional permission-mode surface of a chat harness.
|
|
type chatModer interface {
|
|
Modes(ctx context.Context, sessionID string) ([]protocol.ChatMode, error)
|
|
SetMode(ctx context.Context, sessionID, mode string) error
|
|
}
|
|
|
|
// chatModeRegistry stores each operator chat's chosen mode.
|
|
type chatModeRegistry interface {
|
|
SetChatMode(ctx context.Context, id, mode string) error
|
|
ChatMode(ctx context.Context, id string) (string, error)
|
|
}
|
|
|
|
var errChatModesUnsupported = errors.New("this chat harness does not support changing modes")
|
|
|
|
// Modes lists the modes the session's harness offers.
|
|
func (m *chatManager) Modes(ctx context.Context, sessionID string) ([]protocol.ChatMode, error) {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
moder, ok := adapter.(chatModer)
|
|
if !ok {
|
|
return nil, errChatModesUnsupported
|
|
}
|
|
return moder.Modes(ctx, sessionID)
|
|
}
|
|
|
|
// SetMode applies a mode to the harness now and stores it, so it survives a
|
|
// restart and is re-applied before every later turn.
|
|
func (m *chatManager) SetMode(ctx context.Context, sessionID, mode string) error {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
moder, ok := adapter.(chatModer)
|
|
if !ok {
|
|
return errChatModesUnsupported
|
|
}
|
|
registry, ok := m.registry.(chatModeRegistry)
|
|
if !ok {
|
|
return errors.New("durable chat modes are not configured")
|
|
}
|
|
if err := moder.SetMode(ctx, sessionID, mode); err != nil {
|
|
return err
|
|
}
|
|
return registry.SetChatMode(ctx, sessionID, mode)
|
|
}
|
|
|
|
// CurrentMode is the session's stored mode, "" for the harness default.
|
|
func (m *chatManager) CurrentMode(ctx context.Context, sessionID string) string {
|
|
registry, ok := m.registry.(chatModeRegistry)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
mode, err := registry.ChatMode(ctx, sessionID)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return mode
|
|
}
|
|
|
|
// withMode names the session's mode on a listed row: the stored mode, else the
|
|
// harness default (listed first). A harness without modes leaves both empty.
|
|
func (m *chatManager) withMode(ctx context.Context, session protocol.ChatSession) protocol.ChatSession {
|
|
moder, ok := m.adapter(session.Backend).(chatModer)
|
|
if !ok {
|
|
return session
|
|
}
|
|
modes, err := moder.Modes(ctx, session.ID)
|
|
if err != nil || len(modes) == 0 {
|
|
return session
|
|
}
|
|
stored := m.CurrentMode(ctx, session.ID)
|
|
chosen := modes[0]
|
|
for _, mode := range modes {
|
|
if mode.ID == stored {
|
|
chosen = mode
|
|
}
|
|
}
|
|
session.Mode, session.ModeName = chosen.ID, chosen.Name
|
|
return session
|
|
}
|
|
|
|
// applyStoredMode hands the stored mode to the harness before a turn. Adapters
|
|
// treat a repeat as a no-op, and this covers a server restart that lost the
|
|
// adapter's in-memory choice.
|
|
func (m *chatManager) applyStoredMode(ctx context.Context, adapter chatBroker, sessionID string) error {
|
|
moder, ok := adapter.(chatModer)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
mode := m.CurrentMode(ctx, sessionID)
|
|
if mode == "" {
|
|
return nil
|
|
}
|
|
return moder.SetMode(ctx, sessionID, mode)
|
|
}
|
|
|
|
// handleChatModes lists a session's modes and reports the current one.
|
|
func handleChatModes(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
|
|
query := request.ChatQuery
|
|
if query == nil || !validChatSessionID(query.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
|
|
return
|
|
}
|
|
moder, ok := broker.(chatModer)
|
|
if !ok {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: errChatModesUnsupported.Error()})
|
|
return
|
|
}
|
|
modes, err := moder.Modes(ctx, query.SessionID)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
current := protocol.ChatModeRequest{SessionID: query.SessionID}
|
|
if reporter, ok := broker.(interface {
|
|
CurrentMode(context.Context, string) string
|
|
}); ok {
|
|
current.Mode = reporter.CurrentMode(ctx, query.SessionID)
|
|
}
|
|
_ = write(protocol.Message{Type: protocol.ChatModes, ID: request.ID, ChatModes: modes, ModeRequest: ¤t})
|
|
}
|
|
|
|
// handleChatModeSwitch changes one session's mode and echoes the stored mode.
|
|
func handleChatModeSwitch(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
|
|
body := request.ModeRequest
|
|
if body == nil || !validChatSessionID(body.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
|
|
return
|
|
}
|
|
mode := strings.TrimSpace(body.Mode)
|
|
if mode == "" {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a mode is required"})
|
|
return
|
|
}
|
|
moder, ok := broker.(chatModer)
|
|
if !ok {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: errChatModesUnsupported.Error()})
|
|
return
|
|
}
|
|
if err := moder.SetMode(ctx, body.SessionID, mode); err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
reply := protocol.ChatModeRequest{SessionID: body.SessionID, Mode: mode}
|
|
_ = write(protocol.Message{Type: protocol.ChatModeSelect, ID: request.ID, ModeRequest: &reply})
|
|
}
|