package claude import ( "context" "encoding/json" "errors" "strings" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // parseInitializeCommands reads the slash commands Claude Code's initialize // response lists: bundled skills (code-review), ~/.claude and project skills, // plugin and claude.ai-synced skills, and the CLI's own commands, each with its // description and argument hint. The operator sends "/ args" as an // ordinary prompt and the CLI expands it. Internal entries ("__…") and // duplicates are dropped; a response without commands yields none. func parseInitializeCommands(raw json.RawMessage) []protocol.ChatCommand { var reply struct { Commands []struct { Name string `json:"name"` Description string `json:"description"` ArgumentHint string `json:"argumentHint"` } `json:"commands"` } if len(raw) == 0 || json.Unmarshal(raw, &reply) != nil { return nil } seen := map[string]bool{} commands := make([]protocol.ChatCommand, 0, len(reply.Commands)) for _, c := range reply.Commands { name := strings.TrimPrefix(strings.TrimSpace(c.Name), "/") if name == "" || strings.HasPrefix(name, "_") || strings.ContainsAny(name, " \t\r\n") || seen[name] { continue } seen[name] = true commands = append(commands, protocol.ChatCommand{ Name: name, Description: strings.TrimSpace(c.Description), ArgumentHint: strings.TrimSpace(c.ArgumentHint), }) } return commands } // noteTerminalCommands records the commands the CLI's init line reports as // terminal-only (terminal_slash_commands): they do nothing in a stream-json // session, so the chat does not offer them. func (s *session) noteTerminalCommands(names []string) { if len(names) == 0 { return } s.mu.Lock() s.terminalCommands = map[string]bool{} for _, name := range names { s.terminalCommands[strings.TrimPrefix(name, "/")] = true } directory, commands := s.directory, s.commandsLocked() s.mu.Unlock() s.chat.rememberCommands(directory, commands) } // rememberCommands keeps the newest command list a CLI in directory reported, // so a chat there lists commands before its own CLI has started. func (c *Chat) rememberCommands(directory string, commands []protocol.ChatCommand) { if commands == nil { return } c.catalogMu.Lock() defer c.catalogMu.Unlock() if c.commandCatalog == nil { c.commandCatalog = map[string][]protocol.ChatCommand{} } c.commandCatalog[directory] = append([]protocol.ChatCommand(nil), commands...) } func (c *Chat) cachedCommands(directory string) []protocol.ChatCommand { c.catalogMu.Lock() defer c.catalogMu.Unlock() return append([]protocol.ChatCommand(nil), c.commandCatalog[directory]...) } // commandsLocked is the session's offered commands. Callers hold s.mu. func (s *session) commandsLocked() []protocol.ChatCommand { commands := make([]protocol.ChatCommand, 0, len(s.commands)) for _, c := range s.commands { if !s.terminalCommands[c.Name] { commands = append(commands, c) } } return commands } // Commands lists the slash commands and skills the session's CLI loaded at the // initialize handshake. It never waits for a CLI to start: requests on a // connection are answered in order, and a cold start would stall them. With no // live CLI it starts one in the background (the next prompt needs it anyway) // and answers from the list a CLI in the same directory last reported, empty // when none has; the client asks again later. func (c *Chat) Commands(_ context.Context, id string) ([]protocol.ChatCommand, error) { s := c.session(id) if s == nil { return nil, errors.New("unknown Claude session " + id) } s.mu.Lock() live := s.proc != nil commands := s.commandsLocked() directory := s.directory s.mu.Unlock() if live { return commands, nil } go func() { ctx, cancel := context.WithTimeout(c.ctx, initializeTimeout+5*time.Second) defer cancel() _, _ = s.ensureProcess(ctx) }() return c.cachedCommands(directory), nil }