feat(chat): add Claude Code chat backend
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/claude"
|
||||
"git.a-wan.net/alan/pipelines/internal/codex"
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
)
|
||||
@@ -20,9 +22,12 @@ type interactiveWebChat interface {
|
||||
|
||||
var _ interactiveWebChat = (*opencode.Chat)(nil)
|
||||
var _ interactiveWebChat = (*codex.Chat)(nil)
|
||||
var _ interactiveWebChat = (*claude.Chat)(nil)
|
||||
|
||||
func TestAdvertisedChatHarnessesExposeInteractiveWebContract(t *testing.T) {
|
||||
m := &chatManager{opencode: opencode.NewChat(&opencode.Connection{}), codex: &codex.Chat{}}
|
||||
claudeChat := claude.New(context.Background())
|
||||
defer claudeChat.Close()
|
||||
m := &chatManager{opencode: opencode.NewChat(&opencode.Connection{}), codex: &codex.Chat{}, claude: claudeChat}
|
||||
for _, backend := range m.Backends() {
|
||||
if _, ok := m.adapter(backend).(interactiveWebChat); !ok {
|
||||
t.Errorf("advertised harness %s lacks web tool controls", backend)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"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"
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
type chatManager struct {
|
||||
opencode chatBroker
|
||||
codex chatBroker
|
||||
claude chatBroker
|
||||
registry operatorRegistry
|
||||
|
||||
mu sync.Mutex
|
||||
@@ -32,6 +34,9 @@ func NewChatManager(ctx context.Context, open chatBroker, registry operatorRegis
|
||||
m.codex = client
|
||||
}
|
||||
}
|
||||
if claude.Available() {
|
||||
m.claude = claude.New(ctx)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -40,6 +45,9 @@ 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.
|
||||
@@ -51,6 +59,9 @@ func (m *chatManager) Backends() []string {
|
||||
if m.codex != nil {
|
||||
backends = append(backends, codex.Backend)
|
||||
}
|
||||
if m.claude != nil {
|
||||
backends = append(backends, claude.Backend)
|
||||
}
|
||||
sort.Strings(backends)
|
||||
return backends
|
||||
}
|
||||
@@ -61,6 +72,8 @@ func (m *chatManager) adapter(backend string) chatBroker {
|
||||
return m.opencode
|
||||
case codex.Backend:
|
||||
return m.codex
|
||||
case claude.Backend:
|
||||
return m.claude
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
@@ -127,8 +140,8 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if m.backendFor(ctx, sessionID) == codex.Backend {
|
||||
model, err := m.codexPromptModel(ctx, adapter, sessionID)
|
||||
if isModelBackend(m.backendFor(ctx, sessionID)) {
|
||||
model, err := m.promptModel(ctx, adapter, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -137,7 +150,7 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
|
||||
PromptWithMessageIDModel(context.Context, string, string, string, string) error
|
||||
})
|
||||
if !ok {
|
||||
return errors.New("this Codex adapter cannot apply the saved next-turn model")
|
||||
return errors.New("this chat adapter cannot apply the saved next-turn model")
|
||||
}
|
||||
return withModel.PromptWithMessageIDModel(ctx, sessionID, text, messageID, model)
|
||||
}
|
||||
@@ -148,7 +161,13 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
|
||||
return adapter.Prompt(ctx, sessionID, text)
|
||||
}
|
||||
|
||||
func (m *chatManager) codexPromptModel(ctx context.Context, adapter chatBroker, sessionID string) (string, error) {
|
||||
// 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
|
||||
@@ -158,7 +177,7 @@ func (m *chatManager) codexPromptModel(ctx context.Context, adapter chatBroker,
|
||||
return "", err
|
||||
}
|
||||
if !found || session.ModelProvider != provider {
|
||||
return "", errors.New("saved model provider no longer matches the Codex thread; select a model again")
|
||||
return "", errors.New("saved model provider no longer matches the chat harness; select a model again")
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
@@ -173,8 +192,8 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if m.backendFor(ctx, sessionID) == codex.Backend {
|
||||
model, err := m.codexPromptModel(ctx, adapter, sessionID)
|
||||
if isModelBackend(m.backendFor(ctx, sessionID)) {
|
||||
model, err := m.promptModel(ctx, adapter, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -183,7 +202,7 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t
|
||||
PromptWithMessageIDModelFiles(context.Context, string, string, string, string, []opencode.PromptFile) error
|
||||
})
|
||||
if !ok {
|
||||
return errors.New("this Codex adapter cannot apply the saved next-turn model with attachments")
|
||||
return errors.New("this chat adapter cannot apply the saved next-turn model with attachments")
|
||||
}
|
||||
return withModelFiles.PromptWithMessageIDModelFiles(ctx, sessionID, text, messageID, model, files)
|
||||
}
|
||||
@@ -258,7 +277,7 @@ func (m *chatManager) SwitchModel(ctx context.Context, sessionID, provider, mode
|
||||
if !ok {
|
||||
return errors.New("this chat harness does not support changing models")
|
||||
}
|
||||
if m.backendFor(ctx, sessionID) == codex.Backend {
|
||||
if isModelBackend(m.backendFor(ctx, sessionID)) {
|
||||
registry, ok := m.registry.(chatModelSelectionRegistry)
|
||||
if !ok {
|
||||
return errors.New("durable chat model selection is not configured")
|
||||
@@ -277,7 +296,7 @@ type chatModelSelectionRegistry interface {
|
||||
}
|
||||
|
||||
func (m *chatManager) ModelSelectionMode(ctx context.Context, id string) string {
|
||||
if m.backendFor(ctx, id) == codex.Backend {
|
||||
if isModelBackend(m.backendFor(ctx, id)) {
|
||||
return "next_turn"
|
||||
}
|
||||
return ""
|
||||
@@ -291,7 +310,7 @@ func (m *chatManager) chatModelSelection(ctx context.Context, id string) (string
|
||||
}
|
||||
|
||||
func (m *chatManager) withModelSelection(ctx context.Context, session protocol.ChatSession) (protocol.ChatSession, error) {
|
||||
if session.Backend != codex.Backend {
|
||||
if !isModelBackend(session.Backend) {
|
||||
return session, nil
|
||||
}
|
||||
provider, model, err := m.chatModelSelection(ctx, session.ID)
|
||||
@@ -332,7 +351,7 @@ func (m *chatManager) Messages(ctx context.Context, sessionID string, limit int,
|
||||
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} {
|
||||
for _, adapter := range []chatBroker{m.opencode, m.codex, m.claude} {
|
||||
if adapter == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user