summaryrefslogtreecommitdiff
path: root/internal/tmuxedit/config_agent.go
blob: 0c52c3db41d34c7e1083c560bf69c95fd7769baf (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package tmuxedit

import (
	"strings"

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

// configAgent uses baseAgent defaults for all operations. It serves
// user-defined agents from TOML config and simple built-ins (amp, aider)
// that don't need specialized extraction or clearing logic.
type configAgent struct{ baseAgent }

// builtinAgents returns the default set of agent implementations. Order
// matters: agents with distinctive UI elements (box-drawing, etc.) are
// checked first to avoid false positives from model names like "Claude
// 4.5 Sonnet" appearing in other agents' panes.
// Claude Code is not included here: it now supports opening the prompt
// in an external editor natively via Ctrl+G (like OpenAI Codex CLI).
func builtinAgents() []Agent {
	return []Agent{
		newCursorAgent(),
		&configAgent{baseAgent{
			name:          "amp",
			displayName:   "Amp",
			detectPattern: `(?i)(amp|sourcegraph)`,
			promptPat:     `(?m)│\s*(.+?)\s*│\s*$`,
			clearFirst:    true,
			clearKeys:     "C-u",
			newlineKeys:   "S-Enter",
			submitKeys:    "Enter",
		}},
		&configAgent{baseAgent{
			name:          "aider",
			displayName:   "Aider",
			detectPattern: `(?i)aider`,
			promptPat:     `(?m)>\s*(.+)$`,
			clearFirst:    true,
			clearKeys:     "C-u",
			newlineKeys:   "",
			submitKeys:    "Enter",
		}},
	}
}

// genericAgent returns a fallback agent with no detection or prompt extraction.
// The user gets a blank editor and text is sent verbatim.
func genericAgent() Agent {
	return &configAgent{baseAgent{
		name:        "generic",
		displayName: "Generic",
		newlineKeys: "",
		submitKeys:  "Enter",
	}}
}

// resolveAgents merges built-in agent defaults with user-provided overrides
// from config. Agents are matched by name (case-insensitive); user config
// wins field-by-field over builtins. The Configurable interface provides
// access to baseAgent fields for merging.
func resolveAgents(cfgAgents []appconfig.TmuxEditAgentCfg) []Agent {
	agents := builtinAgents()
	for _, ca := range cfgAgents {
		merged := false
		for i, a := range agents {
			if !strings.EqualFold(a.Name(), ca.Name) {
				continue
			}
			if c, ok := a.(Configurable); ok {
				mergeAgentConfig(c.Base(), ca)
			}
			merged = true
			_ = i // index not needed; we modify through the pointer
			break
		}
		if !merged {
			agents = append(agents, agentFromConfig(ca))
		}
	}
	return agents
}

// mergeAgentConfig overrides fields in base with non-zero values from cfg.
// It modifies the baseAgent in place via pointer.
func mergeAgentConfig(base *baseAgent, cfg appconfig.TmuxEditAgentCfg) {
	if s := strings.TrimSpace(cfg.DisplayName); s != "" {
		base.displayName = s
	}
	if s := strings.TrimSpace(cfg.DetectPattern); s != "" {
		base.detectPattern = s
	}
	if s := strings.TrimSpace(cfg.SectionPattern); s != "" {
		base.sectionPat = s
	}
	if s := strings.TrimSpace(cfg.PromptPattern); s != "" {
		base.promptPat = s
	}
	if len(cfg.StripPatterns) > 0 {
		base.stripPatterns = cfg.StripPatterns
	}
	if cfg.ClearFirst != nil {
		base.clearFirst = *cfg.ClearFirst
	}
	if s := strings.TrimSpace(cfg.ClearKeys); s != "" {
		base.clearKeys = s
	}
	if s := strings.TrimSpace(cfg.NewlineKeys); s != "" {
		base.newlineKeys = s
	}
	if s := strings.TrimSpace(cfg.SubmitKeys); s != "" {
		base.submitKeys = s
	}
}

// agentFromConfig creates a new configAgent from a user config entry.
func agentFromConfig(cfg appconfig.TmuxEditAgentCfg) Agent {
	b := baseAgent{
		name:          strings.TrimSpace(cfg.Name),
		displayName:   strings.TrimSpace(cfg.DisplayName),
		detectPattern: strings.TrimSpace(cfg.DetectPattern),
		sectionPat:    strings.TrimSpace(cfg.SectionPattern),
		promptPat:     strings.TrimSpace(cfg.PromptPattern),
		stripPatterns: cfg.StripPatterns,
		clearKeys:     strings.TrimSpace(cfg.ClearKeys),
		newlineKeys:   strings.TrimSpace(cfg.NewlineKeys),
		submitKeys:    strings.TrimSpace(cfg.SubmitKeys),
	}
	if cfg.ClearFirst != nil {
		b.clearFirst = *cfg.ClearFirst
	}
	if b.displayName == "" {
		b.displayName = b.name
	}
	return &configAgent{b}
}