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
|
package hexaicli
import (
"context"
"fmt"
"io"
"log"
"strings"
"time"
"codeberg.org/snonux/hexai/internal/appconfig"
"codeberg.org/snonux/hexai/internal/editor"
"codeberg.org/snonux/hexai/internal/llm"
"codeberg.org/snonux/hexai/internal/logging"
"codeberg.org/snonux/hexai/internal/stats"
"codeberg.org/snonux/hexai/internal/tmux"
)
type cliConfigLoader func(context.Context, *log.Logger) appconfig.App
type cliEditorOpener func([]byte) (string, error)
type cliClientFactory func(appconfig.App) (llm.Client, error)
type cliStatusSink interface {
SetLLMStart(provider, model string) error
SetGlobal(snapshot stats.Snapshot, provider, model string, scopeRPM float64, scopeReq int64) error
}
// Runner executes the CLI with injectable configuration, editor, client, and status dependencies.
type Runner struct {
loadConfig cliConfigLoader
openEditor cliEditorOpener
newClient cliClientFactory
statusSink cliStatusSink
}
type tmuxCLIStatusSink struct{}
func (tmuxCLIStatusSink) SetLLMStart(provider, model string) error {
return tmux.SetStatus(tmux.FormatLLMStartStatus(provider, model))
}
func (tmuxCLIStatusSink) SetGlobal(snapshot stats.Snapshot, provider, model string, scopeRPM float64, scopeReq int64) error {
return tmux.SetStatus(tmux.FormatGlobalStatusColored(
snapshot.Global.Reqs,
snapshot.RPM,
snapshot.Global.Sent,
snapshot.Global.Recv,
provider,
model,
scopeRPM,
scopeReq,
snapshot.Window,
))
}
// NewRunner builds a CLI runner with production dependencies.
func NewRunner() *Runner {
return &Runner{
loadConfig: loadConfigFromContext,
openEditor: editor.OpenTempAndEdit,
newClient: newClientFromApp,
statusSink: tmuxCLIStatusSink{},
}
}
func (r *Runner) Run(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
runner := normalizeRunner(r)
if spec, ok, err := tpsSimulationFromContext(ctx); err != nil {
_, _ = fmt.Fprintln(stderr, logging.AnsiBase+err.Error()+logging.AnsiReset)
return err
} else if ok {
input, inputErr := readSimulationInput(stdin, args)
if inputErr != nil {
_, _ = fmt.Fprintln(stderr, logging.AnsiBase+inputErr.Error()+logging.AnsiReset)
return inputErr
}
return runTPSSimulation(ctx, spec, input, stdout)
}
logger := log.New(io.Discard, "", 0)
cfg := runner.loadConfig(ctx, logger)
if cfg.StatsWindowMinutes > 0 {
stats.SetWindow(time.Duration(cfg.StatsWindowMinutes) * time.Minute)
}
jobs, err := buildCLIJobs(cfg)
if err != nil {
_, _ = fmt.Fprintf(stderr, logging.AnsiBase+"hexai: LLM disabled: %v"+logging.AnsiReset+"\n", err)
return err
}
if selected := selectionFromContext(ctx); len(selected) > 0 {
jobs, err = filterJobsBySelection(jobs, selected)
if err != nil {
_, _ = fmt.Fprintf(stderr, logging.AnsiBase+"hexai: %v"+logging.AnsiReset+"\n", err)
return err
}
}
if len(jobs) == 0 {
return fmt.Errorf("hexai: no CLI providers configured")
}
input, rerr := readInput(stdin, args)
if rerr != nil && len(args) == 0 {
if prompt, eerr := runner.openEditor(nil); eerr == nil && strings.TrimSpace(prompt) != "" {
args = []string{prompt}
input, rerr = readInput(stdin, args)
}
}
if rerr != nil {
_, _ = fmt.Fprintln(stderr, logging.AnsiBase+rerr.Error()+logging.AnsiReset)
return rerr
}
msgs := buildMessagesFromConfig(cfg, input)
if err := runCLIJobs(ctx, jobs, msgs, input, stdout, stderr, runner.newClient, runner.statusSink); err != nil {
_, _ = fmt.Fprintf(stderr, logging.AnsiBase+"hexai: error: %v"+logging.AnsiReset+"\n", err)
return err
}
return nil
}
func (r *Runner) RunWithClient(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer, client llm.Client) error {
runner := normalizeRunner(r)
input, err := readInput(stdin, args)
if err != nil {
_, _ = fmt.Fprintln(stderr, logging.AnsiBase+err.Error()+logging.AnsiReset)
return err
}
req := requestArgs{model: strings.TrimSpace(client.DefaultModel())}
printProviderInfo(stderr, client, req.model)
msgs := buildMessages(input)
if err := runChatWithStatus(runner.statusSink, ctx, client, req, msgs, input, stdout, stderr); err != nil {
_, _ = fmt.Fprintf(stderr, logging.AnsiBase+"hexai: error: %v"+logging.AnsiReset+"\n", err)
return err
}
return nil
}
func normalizeRunner(r *Runner) Runner {
if r == nil {
return *NewRunner()
}
runner := *r
if runner.loadConfig == nil {
runner.loadConfig = loadConfigFromContext
}
if runner.openEditor == nil {
runner.openEditor = editor.OpenTempAndEdit
}
if runner.newClient == nil {
runner.newClient = newClientFromApp
}
if runner.statusSink == nil {
runner.statusSink = tmuxCLIStatusSink{}
}
return runner
}
func loadConfigFromContext(ctx context.Context, logger *log.Logger) appconfig.App {
return appconfig.LoadWithOptions(logger, appconfig.LoadOptions{ConfigPath: configPathFromContext(ctx)})
}
|