532 lines
16 KiB
Go
532 lines
16 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sort"
|
|
"sync"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/claude"
|
|
"git.a-wan.net/alan/pipelines/internal/codex"
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// chatManager routes one session to the harness that owns it. OpenCode remains
|
|
// the default; Codex is attached when its app-server control socket exists.
|
|
// The operator registry is the durable record of the backend, so a restart does
|
|
// not have to guess from a session id.
|
|
type chatManager struct {
|
|
opencode chatBroker
|
|
codex chatBroker
|
|
claude chatBroker
|
|
registry operatorRegistry
|
|
|
|
mu sync.Mutex
|
|
cache map[string]string // session id -> backend
|
|
}
|
|
|
|
func NewChatManager(ctx context.Context, open chatBroker, registry operatorRegistry) *chatManager {
|
|
m := &chatManager{opencode: open, registry: registry, cache: map[string]string{}}
|
|
if codex.Available() {
|
|
client, err := codex.Open(ctx)
|
|
if err == nil {
|
|
m.codex = client
|
|
}
|
|
}
|
|
if claude.Available() {
|
|
m.claude = claude.New(ctx)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// Close releases the owned Codex client, if any.
|
|
func (m *chatManager) Close() {
|
|
if closer, ok := m.codex.(interface{ Close() }); ok {
|
|
closer.Close()
|
|
}
|
|
if closer, ok := m.claude.(interface{ Close() }); ok {
|
|
closer.Close()
|
|
}
|
|
}
|
|
|
|
// Backends lists the harnesses available for a new session.
|
|
func (m *chatManager) Backends() []string {
|
|
backends := []string{}
|
|
if m.opencode != nil {
|
|
backends = append(backends, "opencode")
|
|
}
|
|
if m.codex != nil {
|
|
backends = append(backends, codex.Backend)
|
|
}
|
|
if m.claude != nil {
|
|
backends = append(backends, claude.Backend)
|
|
}
|
|
sort.Strings(backends)
|
|
return backends
|
|
}
|
|
|
|
// AdoptSession finds an existing session on [backend], or on the first
|
|
// harness that knows it when [backend] is blank (OpenCode, Claude Code, Codex),
|
|
// and remembers which harness owns it.
|
|
func (m *chatManager) AdoptSession(ctx context.Context, sessionID, backend string) (protocol.ChatSession, string, bool, error) {
|
|
candidates := []string{backend}
|
|
if backend == "" {
|
|
candidates = []string{"opencode", claude.Backend, codex.Backend}
|
|
}
|
|
var firstErr error
|
|
for _, name := range candidates {
|
|
adapter := m.adapter(name)
|
|
if adapter == nil {
|
|
if backend != "" {
|
|
return protocol.ChatSession{}, "", false, errors.New("chat backend " + backend + " is not available")
|
|
}
|
|
continue
|
|
}
|
|
session, found, err := adapter.Session(ctx, sessionID)
|
|
if err != nil {
|
|
if firstErr == nil {
|
|
firstErr = err
|
|
}
|
|
continue
|
|
}
|
|
if !found {
|
|
continue
|
|
}
|
|
if name == "" {
|
|
name = "opencode"
|
|
}
|
|
session.Backend = name
|
|
m.mu.Lock()
|
|
m.cache[sessionID] = name
|
|
m.mu.Unlock()
|
|
return session, name, true, nil
|
|
}
|
|
return protocol.ChatSession{}, "", false, firstErr
|
|
}
|
|
|
|
func (m *chatManager) adapter(backend string) chatBroker {
|
|
switch backend {
|
|
case "", "opencode":
|
|
return m.opencode
|
|
case codex.Backend:
|
|
return m.codex
|
|
case claude.Backend:
|
|
return m.claude
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (m *chatManager) backendFor(ctx context.Context, sessionID string) string {
|
|
m.mu.Lock()
|
|
backend := m.cache[sessionID]
|
|
m.mu.Unlock()
|
|
if backend != "" {
|
|
return backend
|
|
}
|
|
if m.registry != nil {
|
|
if row, found, err := m.registry.OperatorSession(ctx, sessionID); err == nil && found && row.Backend != "" {
|
|
m.mu.Lock()
|
|
m.cache[sessionID] = row.Backend
|
|
m.mu.Unlock()
|
|
return row.Backend
|
|
}
|
|
}
|
|
return "opencode"
|
|
}
|
|
|
|
func (m *chatManager) brokerFor(ctx context.Context, sessionID string) (chatBroker, error) {
|
|
backend := m.backendFor(ctx, sessionID)
|
|
adapter := m.adapter(backend)
|
|
if adapter == nil {
|
|
return nil, errors.New("chat backend " + backend + " is not available")
|
|
}
|
|
return adapter, nil
|
|
}
|
|
|
|
// CreateBackend creates a session on the requested harness. An empty backend
|
|
// keeps the OpenCode default.
|
|
func (m *chatManager) CreateBackend(ctx context.Context, title, directory, backend string) (protocol.ChatSession, error) {
|
|
if backend == "" {
|
|
backend = "opencode"
|
|
}
|
|
adapter := m.adapter(backend)
|
|
if adapter == nil {
|
|
return protocol.ChatSession{}, errors.New("chat backend " + backend + " is not available")
|
|
}
|
|
session, err := adapter.Create(ctx, title, directory)
|
|
if err != nil {
|
|
return protocol.ChatSession{}, err
|
|
}
|
|
session.Backend = backend
|
|
m.mu.Lock()
|
|
m.cache[session.ID] = backend
|
|
m.mu.Unlock()
|
|
return session, nil
|
|
}
|
|
|
|
func (m *chatManager) Create(ctx context.Context, title, directory string) (protocol.ChatSession, error) {
|
|
return m.CreateBackend(ctx, title, directory, "opencode")
|
|
}
|
|
|
|
func (m *chatManager) Prompt(ctx context.Context, sessionID, text string) error {
|
|
return m.PromptWithMessageID(ctx, sessionID, text, "")
|
|
}
|
|
|
|
func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text, messageID string) error {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if isModelBackend(m.backendFor(ctx, sessionID)) {
|
|
model, err := m.promptModel(ctx, adapter, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if model != "" {
|
|
withModel, ok := adapter.(interface {
|
|
PromptWithMessageIDModel(context.Context, string, string, string, string) error
|
|
})
|
|
if !ok {
|
|
return errors.New("this chat adapter cannot apply the saved next-turn model")
|
|
}
|
|
return withModel.PromptWithMessageIDModel(ctx, sessionID, text, messageID, model)
|
|
}
|
|
}
|
|
if withID, ok := adapter.(chatPromptWithMessageID); ok {
|
|
return withID.PromptWithMessageID(ctx, sessionID, text, messageID)
|
|
}
|
|
return adapter.Prompt(ctx, sessionID, text)
|
|
}
|
|
|
|
// isModelBackend reports a harness whose model choice is stored by Pipelines
|
|
// and applied at the next turn (Codex and Claude Code).
|
|
func isModelBackend(backend string) bool {
|
|
return backend == codex.Backend || backend == claude.Backend
|
|
}
|
|
|
|
func (m *chatManager) promptModel(ctx context.Context, adapter chatBroker, sessionID string) (string, error) {
|
|
provider, model, err := m.chatModelSelection(ctx, sessionID)
|
|
if err != nil || model == "" {
|
|
return model, err
|
|
}
|
|
session, found, err := adapter.Session(ctx, sessionID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !found || session.ModelProvider != provider {
|
|
return "", errors.New("saved model provider no longer matches the chat harness; select a model again")
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// PromptWithMessageIDFiles forwards a prompt with the private local files the
|
|
// server wrote for browser attachments. Never silently discard files.
|
|
func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) error {
|
|
if len(files) == 0 {
|
|
return m.PromptWithMessageID(ctx, sessionID, text, messageID)
|
|
}
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if isModelBackend(m.backendFor(ctx, sessionID)) {
|
|
model, err := m.promptModel(ctx, adapter, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if model != "" {
|
|
withModelFiles, ok := adapter.(interface {
|
|
PromptWithMessageIDModelFiles(context.Context, string, string, string, string, []opencode.PromptFile) error
|
|
})
|
|
if !ok {
|
|
return errors.New("this chat adapter cannot apply the saved next-turn model with attachments")
|
|
}
|
|
return withModelFiles.PromptWithMessageIDModelFiles(ctx, sessionID, text, messageID, model, files)
|
|
}
|
|
}
|
|
if withFiles, ok := adapter.(chatPromptWithFiles); ok {
|
|
return withFiles.PromptWithMessageIDFiles(ctx, sessionID, text, messageID, files)
|
|
}
|
|
return errChatAttachmentsUnsupported
|
|
}
|
|
|
|
var errChatAttachmentsUnsupported = errors.New("this chat harness does not support attachments")
|
|
var errChatFormReadbackUnsupported = errors.New("this chat harness does not support pending question readback")
|
|
|
|
func (m *chatManager) Forms(ctx context.Context, sessionID string) ([]protocol.ChatForm, error) {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if reader, ok := adapter.(chatFormReader); ok {
|
|
return reader.Forms(ctx, sessionID)
|
|
}
|
|
return nil, errChatFormReadbackUnsupported
|
|
}
|
|
|
|
var errChatInstructionsUnsupported = errors.New("this chat harness does not support session instructions")
|
|
|
|
func (m *chatManager) CheckAttachments(ctx context.Context, sessionID string) error {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, ok := adapter.(chatPromptWithFiles); !ok {
|
|
return errChatAttachmentsUnsupported
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetInstruction forwards one preloaded behavior instruction to the session's
|
|
// harness when it supports durable instruction entries.
|
|
func (m *chatManager) SetInstruction(ctx context.Context, sessionID, key string, value any) error {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
instr, ok := adapter.(chatInstructioner)
|
|
if !ok {
|
|
return errChatInstructionsUnsupported
|
|
}
|
|
return instr.SetInstruction(ctx, sessionID, key, value)
|
|
}
|
|
|
|
// Models lists the models the session's harness offers for switching.
|
|
func (m *chatManager) Models(ctx context.Context, sessionID string) ([]protocol.ChatModel, error) {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
modeler, ok := adapter.(chatModeler)
|
|
if !ok {
|
|
return nil, errors.New("this chat harness does not support changing models")
|
|
}
|
|
return modeler.Models(ctx, sessionID)
|
|
}
|
|
|
|
// SwitchModel changes the session's model for subsequent turns.
|
|
func (m *chatManager) SwitchModel(ctx context.Context, sessionID, provider, model string) error {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
modeler, ok := adapter.(chatModeler)
|
|
if !ok {
|
|
return errors.New("this chat harness does not support changing models")
|
|
}
|
|
if isModelBackend(m.backendFor(ctx, sessionID)) {
|
|
registry, ok := m.registry.(chatModelSelectionRegistry)
|
|
if !ok {
|
|
return errors.New("durable chat model selection is not configured")
|
|
}
|
|
if err := modeler.SwitchModel(ctx, sessionID, provider, model); err != nil {
|
|
return err
|
|
}
|
|
return registry.SetChatModelSelection(ctx, sessionID, provider, model)
|
|
}
|
|
return modeler.SwitchModel(ctx, sessionID, provider, model)
|
|
}
|
|
|
|
type chatModelSelectionRegistry interface {
|
|
SetChatModelSelection(context.Context, string, string, string) error
|
|
ChatModelSelection(context.Context, string) (string, string, error)
|
|
}
|
|
|
|
func (m *chatManager) ModelSelectionMode(ctx context.Context, id string) string {
|
|
if isModelBackend(m.backendFor(ctx, id)) {
|
|
return "next_turn"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (m *chatManager) chatModelSelection(ctx context.Context, id string) (string, string, error) {
|
|
if registry, ok := m.registry.(chatModelSelectionRegistry); ok {
|
|
return registry.ChatModelSelection(ctx, id)
|
|
}
|
|
return "", "", nil
|
|
}
|
|
|
|
func (m *chatManager) withModelSelection(ctx context.Context, session protocol.ChatSession) (protocol.ChatSession, error) {
|
|
if !isModelBackend(session.Backend) {
|
|
return session, nil
|
|
}
|
|
provider, model, err := m.chatModelSelection(ctx, session.ID)
|
|
if err != nil {
|
|
return session, err
|
|
}
|
|
if model != "" {
|
|
session.ModelProvider, session.Model = provider, model
|
|
session.ModelSelection = "next_turn"
|
|
}
|
|
return session, nil
|
|
}
|
|
|
|
func (m *chatManager) Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return adapter.Stream(ctx, sessionID, ready, emit)
|
|
}
|
|
|
|
func (m *chatManager) Interrupt(ctx context.Context, sessionID string) error {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return adapter.Interrupt(ctx, sessionID)
|
|
}
|
|
|
|
func (m *chatManager) Messages(ctx context.Context, sessionID string, limit int, cursor string) (protocol.ChatHistoryPage, error) {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return protocol.ChatHistoryPage{}, err
|
|
}
|
|
return adapter.Messages(ctx, sessionID, limit, cursor)
|
|
}
|
|
|
|
func (m *chatManager) Sessions(ctx context.Context) ([]protocol.ChatSession, error) {
|
|
sessions := []protocol.ChatSession{}
|
|
seen := map[string]bool{}
|
|
for _, adapter := range []chatBroker{m.opencode, m.codex, m.claude} {
|
|
if adapter == nil {
|
|
continue
|
|
}
|
|
list, err := adapter.Sessions(ctx)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, session := range list {
|
|
if session.ID == "" || seen[session.ID] {
|
|
continue
|
|
}
|
|
seen[session.ID] = true
|
|
sessions = append(sessions, session)
|
|
}
|
|
}
|
|
// A backend that cannot enumerate its own store still has registry rows.
|
|
// Resolve those individually so an older/restarted backend never silently
|
|
// drops a chat from the list.
|
|
if m.registry != nil {
|
|
if rows, err := m.registry.OperatorSessions(ctx); err == nil {
|
|
for _, row := range rows {
|
|
if seen[row.ID] || row.Backend == "" || row.Backend == "opencode" {
|
|
continue
|
|
}
|
|
adapter := m.adapter(row.Backend)
|
|
if adapter == nil {
|
|
continue
|
|
}
|
|
if session, found, err := adapter.Session(ctx, row.ID); err == nil && found {
|
|
seen[row.ID] = true
|
|
sessions = append(sessions, session)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for i := range sessions {
|
|
var err error
|
|
sessions[i], err = m.withModelSelection(ctx, sessions[i])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return sessions, nil
|
|
}
|
|
|
|
func (m *chatManager) Session(ctx context.Context, sessionID string) (protocol.ChatSession, bool, error) {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return protocol.ChatSession{}, false, err
|
|
}
|
|
session, found, err := adapter.Session(ctx, sessionID)
|
|
if err != nil || !found {
|
|
return session, found, err
|
|
}
|
|
session, err = m.withModelSelection(ctx, session)
|
|
return session, found, err
|
|
}
|
|
|
|
func (m *chatManager) Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error) {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if permissions, ok := adapter.(interface {
|
|
Permissions(context.Context, string) ([]protocol.ChatPermissionFrame, error)
|
|
}); ok {
|
|
return permissions.Permissions(ctx, sessionID)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *chatManager) Shells(ctx context.Context, sessionID string) ([]protocol.ChatShell, error) {
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if shells, ok := adapter.(interface {
|
|
Shells(context.Context, string) ([]protocol.ChatShell, error)
|
|
}); ok {
|
|
return shells.Shells(ctx, sessionID)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *chatManager) Reply(ctx context.Context, request protocol.ChatReplyRequest) error {
|
|
adapter, err := m.brokerFor(ctx, request.SessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return adapter.Reply(ctx, request)
|
|
}
|
|
|
|
func (m *chatManager) ActivateSkill(ctx context.Context, sessionID, skill string) error {
|
|
// Skill bootstrap is an OpenCode chat concept. Other harnesses return from
|
|
// create without a manager skill and must not fail the create.
|
|
if m.backendFor(ctx, sessionID) != "opencode" {
|
|
return nil
|
|
}
|
|
adapter, err := m.brokerFor(ctx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return adapter.ActivateSkill(ctx, sessionID, skill)
|
|
}
|
|
|
|
func (m *chatManager) Answer(ctx context.Context, request protocol.ChatAnswerRequest) error {
|
|
adapter, err := m.brokerFor(ctx, request.SessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
answerer, ok := adapter.(chatAnswerer)
|
|
if !ok {
|
|
return errors.New("this chat backend cannot answer questions")
|
|
}
|
|
return answerer.Answer(ctx, request)
|
|
}
|
|
|
|
func (m *chatManager) ShellOutput(ctx context.Context, query protocol.ChatShellOutputQuery) (protocol.ChatShellOutputPage, error) {
|
|
adapter, err := m.brokerFor(ctx, query.SessionID)
|
|
if err != nil {
|
|
return protocol.ChatShellOutputPage{}, err
|
|
}
|
|
reader, ok := adapter.(chatShellOutputReader)
|
|
if !ok {
|
|
return protocol.ChatShellOutputPage{}, errors.New("this chat backend has no shell output")
|
|
}
|
|
return reader.ShellOutput(ctx, query)
|
|
}
|
|
|
|
func (m *chatManager) ReleaseShells(sessionID string) {
|
|
m.mu.Lock()
|
|
backend := m.cache[sessionID]
|
|
m.mu.Unlock()
|
|
adapter := m.adapter(backend)
|
|
if releaser, ok := adapter.(chatShellReleaser); ok {
|
|
releaser.ReleaseShells(sessionID)
|
|
}
|
|
}
|