Chat: offer the session's Claude Code skills and slash commands in the palette
The server reads the commands Claude Code's initialize response lists (bundled, user, project, plugin and claude.ai-synced skills plus CLI commands, with descriptions and argument hints), drops terminal-only ones, and serves them per chat over a new chat_commands request without waiting for a CLI start. The composer palette lists them after the recorded commands; choosing one inserts "/<name> " and sending passes it to the harness as an ordinary prompt. Codex, OpenCode and Pi chats reply with an empty list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -250,6 +250,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
||||
case protocol.ChatModels:
|
||||
handleChatModels(ctx, request, broker, write)
|
||||
return true
|
||||
case protocol.ChatCommands:
|
||||
handleChatCommands(ctx, request, broker, write)
|
||||
return true
|
||||
case protocol.ChatModelSelect:
|
||||
handleChatModelSwitch(ctx, request, broker, write)
|
||||
return true
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// chatCommander is the optional slash-command surface of a chat harness:
|
||||
// the skills and commands its session loaded, which the operator invokes by
|
||||
// sending "/<name> args" as an ordinary prompt. Claude Code implements it; a
|
||||
// harness without one offers an empty list rather than an error.
|
||||
type chatCommander interface {
|
||||
Commands(ctx context.Context, sessionID string) ([]protocol.ChatCommand, error)
|
||||
}
|
||||
|
||||
// Commands lists the slash commands the session's harness offers.
|
||||
func (m *chatManager) Commands(ctx context.Context, sessionID string) ([]protocol.ChatCommand, error) {
|
||||
adapter, err := m.brokerFor(ctx, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commander, ok := adapter.(chatCommander)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
return commander.Commands(ctx, sessionID)
|
||||
}
|
||||
|
||||
// handleChatCommands answers chat_commands with the session's slash commands.
|
||||
func handleChatCommands(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
|
||||
}
|
||||
var commands []protocol.ChatCommand
|
||||
if commander, ok := broker.(chatCommander); ok {
|
||||
var err error
|
||||
if commands, err = commander.Commands(ctx, query.SessionID); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatCommands, ID: request.ID, ChatCommands: commands})
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// commandChat is a harness that reports its session's slash commands.
|
||||
type commandChat struct {
|
||||
*fakeChat
|
||||
asked []string
|
||||
}
|
||||
|
||||
func (c *commandChat) Commands(_ context.Context, id string) ([]protocol.ChatCommand, error) {
|
||||
c.asked = append(c.asked, id)
|
||||
return []protocol.ChatCommand{{Name: "code-review", Description: "Review the diff", ArgumentHint: "[low|high]"}, {Name: "probe-skill"}}, nil
|
||||
}
|
||||
|
||||
func TestChatCommandsRouteToTheSessionHarness(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "chat.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
claudeID := "01900000-0000-7000-8000-000000000011"
|
||||
codexID := "01900000-0000-7000-8000-000000000012"
|
||||
for id, backend := range map[string]string{claudeID: "claude", codexID: "codex"} {
|
||||
if err := store.RecordOperatorSession(ctx, id, "/fixture", "chat", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.SetOperatorSessionBackend(ctx, id, backend); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
claude := &commandChat{fakeChat: scriptedChat()}
|
||||
manager := &chatManager{claude: claude, codex: scriptedChat(), registry: store, cache: map[string]string{}}
|
||||
var reply protocol.Message
|
||||
capture := func(msg protocol.Message) error { reply = msg; return nil }
|
||||
|
||||
handleChatCommands(ctx, protocol.Message{ID: "c1", ChatQuery: &protocol.ChatQuery{SessionID: claudeID}}, manager, capture)
|
||||
if reply.Type != protocol.ChatCommands || reply.ID != "c1" || len(reply.ChatCommands) != 2 || len(claude.asked) != 1 || claude.asked[0] != claudeID {
|
||||
t.Fatalf("claude reply = %+v, asked %v", reply, claude.asked)
|
||||
}
|
||||
wire, err := json.Marshal(reply)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(wire), `"chat_commands":[{"name":"code-review","description":"Review the diff","argument_hint":"[low|high]"},{"name":"probe-skill"}]`) {
|
||||
t.Fatalf("wire = %s", wire)
|
||||
}
|
||||
|
||||
// A harness without commands answers with an empty list, not an error.
|
||||
handleChatCommands(ctx, protocol.Message{ID: "c2", ChatQuery: &protocol.ChatQuery{SessionID: codexID}}, manager, capture)
|
||||
if reply.Type != protocol.ChatCommands || reply.ID != "c2" || len(reply.ChatCommands) != 0 {
|
||||
t.Fatalf("codex reply = %+v", reply)
|
||||
}
|
||||
handleChatCommands(ctx, protocol.Message{ID: "c3", ChatQuery: &protocol.ChatQuery{SessionID: "not a session"}}, manager, capture)
|
||||
if reply.Type != protocol.Error {
|
||||
t.Fatalf("invalid id reply = %+v", reply)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user