summaryrefslogtreecommitdiff
path: root/internal/lsp/chat_commands.go
blob: b2da7d402350dd45c6deec64f09bab2221357665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package lsp

import (
	"fmt"
	"strings"

	"codeberg.org/snonux/hexai/internal/runtimeconfig"
)

type chatCommandResult struct {
	message string
}

func (s *Server) chatCommandResponse(uri string, lineIdx int, prompt string) (chatCommandResult, bool) {
	trimmed := strings.TrimSpace(s.stripTrailingTrigger(prompt))
	if trimmed == "" || !strings.HasPrefix(trimmed, "/") {
		return chatCommandResult{}, false
	}

	switch {
	case strings.HasPrefix(trimmed, "/reload"):
		return s.handleReloadCommand(), true
	case strings.HasPrefix(trimmed, "/help"):
		return s.handleHelpCommand(), true
	default:
		return chatCommandResult{message: fmt.Sprintf("Unknown command %q. Try /help?>", trimmed)}, true
	}
}

func (s *Server) handleHelpCommand() chatCommandResult {
	lines := []string{
		"Available slash commands:",
		"- /reload?> reload configuration from file (ignores env overrides)",
	}
	return chatCommandResult{message: strings.Join(lines, "\n")}
}

func (s *Server) handleReloadCommand() chatCommandResult {
	if s.configStore == nil {
		return chatCommandResult{message: "Reload unavailable: no config store"}
	}
	loadOpts := s.configLoadOpts
	loadOpts.IgnoreEnv = true
	changes, err := s.configStore.Reload(s.logger, loadOpts)
	if err != nil {
		s.logger.Printf("config reload failed: %v", err)
		return chatCommandResult{message: fmt.Sprintf("Reload failed: %v", err)}
	}
	summary := runtimeconfig.FormatSummary("Reloaded config", changes)
	s.logger.Print(summary)
	return chatCommandResult{message: summary}
}