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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
package hexaiaction
import (
"context"
"strings"
"time"
"codeberg.org/snonux/hexai/internal/appconfig"
"codeberg.org/snonux/hexai/internal/llm"
"codeberg.org/snonux/hexai/internal/llmutils"
"codeberg.org/snonux/hexai/internal/stats"
"codeberg.org/snonux/hexai/internal/textutil"
"codeberg.org/snonux/hexai/internal/tmux"
)
// Render performs simple {{var}} replacement like LSP.
func Render(t string, vars map[string]string) string { return textutil.RenderTemplate(t, vars) }
// StripFences removes surrounding markdown code fences.
func StripFences(s string) string { return textutil.StripCodeFences(s) }
type chatDoer interface {
Chat(ctx context.Context, msgs []llm.Message, opts ...llm.RequestOption) (string, error)
DefaultModel() string
}
type providerNamer interface{ Name() string }
type requestArgs struct {
model string
options []llm.RequestOption
}
func providerOf(c any) string {
if n, ok := c.(providerNamer); ok {
return n.Name()
}
return "llm"
}
func canonicalProvider(name string) string {
return llmutils.CanonicalProvider(name)
}
func selectActionTemperature(cfg actionConfig, provider string, entry appconfig.SurfaceConfig, model string) (float64, bool) {
core := cfg.CoreSection()
if entry.Temperature != nil {
return *entry.Temperature, true
}
if core.CodingTemperature != nil {
temp := *core.CodingTemperature
if provider == "openai" && strings.HasPrefix(strings.ToLower(model), "gpt-5") && temp == 0.2 {
temp = 1.0
}
return temp, true
}
if provider == "openai" && strings.HasPrefix(strings.ToLower(model), "gpt-5") {
return 1.0, true
}
return 0, false
}
func runRewrite(ctx context.Context, cfg actionConfig, client chatDoer, instruction, selection string) (string, error) {
prompts := cfg.PromptSection()
sys := prompts.PromptCodeActionRewriteSystem
user := Render(prompts.PromptCodeActionRewriteUser, map[string]string{"instruction": instruction, "selection": selection})
return runOnce(ctx, client, sys, user, reqOptsFrom(cfg))
}
func runDiagnostics(ctx context.Context, cfg actionConfig, client chatDoer, diags []string, selection string) (string, error) {
var b strings.Builder
for i, d := range diags {
if strings.TrimSpace(d) == "" {
continue
}
b.WriteString(strings.TrimSpace(d))
if i < len(diags)-1 {
b.WriteString("\n")
}
}
prompts := cfg.PromptSection()
sys := prompts.PromptCodeActionDiagnosticsSystem
user := Render(prompts.PromptCodeActionDiagnosticsUser, map[string]string{"diagnostics": b.String(), "selection": selection})
return runOnce(ctx, client, sys, user, reqOptsFrom(cfg))
}
func runDocument(ctx context.Context, cfg actionConfig, client chatDoer, selection string) (string, error) {
prompts := cfg.PromptSection()
sys := prompts.PromptCodeActionDocumentSystem
user := Render(prompts.PromptCodeActionDocumentUser, map[string]string{"selection": selection})
return runOnce(ctx, client, sys, user, reqOptsFrom(cfg))
}
func runSimplify(ctx context.Context, cfg actionConfig, client chatDoer, selection string) (string, error) {
prompts := cfg.PromptSection()
sys := prompts.PromptCodeActionSimplifySystem
user := Render(prompts.PromptCodeActionSimplifyUser, map[string]string{"selection": selection})
return runOnce(ctx, client, sys, user, reqOptsFrom(cfg))
}
func runGoTest(ctx context.Context, cfg actionConfig, client chatDoer, funcCode string) (string, error) {
prompts := cfg.PromptSection()
sys := prompts.PromptCodeActionGoTestSystem
user := Render(prompts.PromptCodeActionGoTestUser, map[string]string{"function": funcCode})
return runOnce(ctx, client, sys, user, reqOptsFrom(cfg))
}
func runCustom(ctx context.Context, cfg actionConfig, client chatDoer, ca appconfig.CustomAction, parts InputParts) (string, error) {
prompts := cfg.PromptSection()
// If user template is provided, prefer it and optional system
if strings.TrimSpace(ca.User) != "" {
sys := prompts.PromptCodeActionRewriteSystem
if strings.TrimSpace(ca.System) != "" {
sys = ca.System
}
// Currently only selection is available in tmux path; diagnostics list not wired
user := Render(ca.User, map[string]string{"selection": parts.Selection, "diagnostics": strings.Join(parts.Diagnostics, "\n")})
return runOnce(ctx, client, sys, user, reqOptsFrom(cfg))
}
// Else, use fixed instruction through rewrite template
return runRewrite(ctx, cfg, client, ca.Instruction, parts.Selection)
}
// runOnce sends a single system+user prompt pair to the LLM, strips code
// fences from the response, records stats, and updates the tmux status line.
// Pass a zero-value requestArgs{} when no extra options are needed.
func runOnce(ctx context.Context, client chatDoer, sys, user string, req requestArgs) (string, error) {
msgs := []llm.Message{{Role: "system", Content: sys}, {Role: "user", Content: user}}
txt, err := client.Chat(ctx, msgs, req.options...)
if err != nil {
return "", err
}
out := strings.TrimSpace(StripFences(txt))
// Contribute to global stats and update tmux status
sent := 0
for _, m := range msgs {
sent += len(m.Content)
}
recv := len(out)
model := strings.TrimSpace(req.model)
if model == "" {
model = client.DefaultModel()
}
_ = stats.Update(ctx, providerOf(client), model, sent, recv)
if snap, err := stats.TakeSnapshot(); err == nil {
minsWin := snap.Window.Minutes()
if minsWin <= 0 {
minsWin = 0.001
}
scopeReqs := int64(0)
if pe, ok := snap.Providers[providerOf(client)]; ok {
if mc, ok2 := pe.Models[model]; ok2 {
scopeReqs = mc.Reqs
}
}
scopeRPM := float64(scopeReqs) / minsWin
_ = tmux.SetStatus(tmux.FormatGlobalStatusColored(snap.Global.Reqs, snap.RPM, snap.Global.Sent, snap.Global.Recv, providerOf(client), model, scopeRPM, scopeReqs, snap.Window))
}
return out, nil
}
// reqOptsFrom builds LLM request options similar to LSP behavior.
func reqOptsFrom(cfg actionConfig) requestArgs {
core := cfg.CoreSection()
providers := cfg.ProviderSection()
fullCfg := actionConfigAsApp(cfg)
opts := make([]llm.RequestOption, 0, 3)
if core.MaxTokens > 0 {
opts = append(opts, llm.WithMaxTokens(core.MaxTokens))
}
provider := canonicalProvider(core.Provider)
entries := providers.CodeActionConfigs
if len(entries) == 0 {
entries = []appconfig.SurfaceConfig{{Provider: core.Provider, Model: strings.TrimSpace(llmutils.DefaultModelForProvider(fullCfg, provider))}}
}
primary := entries[0]
if strings.TrimSpace(primary.Provider) != "" {
provider = canonicalProvider(primary.Provider)
}
model := strings.TrimSpace(primary.Model)
if model == "" {
model = strings.TrimSpace(llmutils.DefaultModelForProvider(fullCfg, provider))
}
if strings.TrimSpace(primary.Model) != "" {
opts = append(opts, llm.WithModel(strings.TrimSpace(primary.Model)))
}
if temp, ok := selectActionTemperature(cfg, provider, primary, model); ok {
opts = append(opts, llm.WithTemperature(temp))
}
return requestArgs{model: model, options: opts}
}
// Timeout helpers to mirror LSP behavior.
func timeout10s(parent context.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(parent, 20*time.Second)
}
func timeout8s(parent context.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(parent, 18*time.Second)
}
|