-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
212 lines (185 loc) · 4.87 KB
/
Copy pathmain.go
File metadata and controls
212 lines (185 loc) · 4.87 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"github.com/saadnvd1/hydra/internal/config"
"github.com/saadnvd1/hydra/internal/proxy"
"github.com/saadnvd1/hydra/internal/session"
)
const version = "0.2.0"
func main() {
if len(os.Args) < 2 {
cfg := loadConfig()
runWithProvider(cfg, "")
return
}
cmd := os.Args[1]
switch cmd {
case "--version", "-v":
fmt.Println("hydra", version)
case "--help", "-h":
printUsage()
case "--continue", "-c", "continue":
runContinue()
case "switch":
runSwitch()
case "config":
runConfigInfo()
case "status":
runStatus()
default:
cfg := loadConfig()
// Check if first arg matches a provider name
for _, p := range cfg.Providers {
if p.Name == cmd {
runWithProvider(cfg, p.Name, os.Args[2:]...)
return
}
}
// Otherwise start primary provider, pass ALL args through
runWithProvider(cfg, "", os.Args[1:]...)
}
}
func runWithProvider(cfg *config.Config, providerName string, extraArgs ...string) {
startIdx := 0
if providerName != "" {
for i, p := range cfg.Providers {
if p.Name == providerName {
startIdx = i
break
}
}
}
p := proxy.New(cfg, startIdx, extraArgs)
p.Run()
}
func runContinue() {
cfg := loadConfig()
sess, err := session.LoadLast()
if err != nil {
// No previous session, just start fresh
runWithProvider(cfg, "")
return
}
fmt.Fprintf(os.Stderr, "\033[33mResuming with context from previous session\033[0m\n")
fmt.Fprintf(os.Stderr, "Last provider: %s\n\n", sess.LastProvider)
// Find next provider after the last one used
startIdx := 0
for i, p := range cfg.Providers {
if p.Name == sess.LastProvider {
startIdx = i + 1
break
}
}
if startIdx >= len(cfg.Providers) {
startIdx = 0
}
// Copy context to clipboard
context := sess.BuildContinuationPrompt()
if err := copyToClipboard(context); err == nil {
fmt.Fprintf(os.Stderr, "\033[32m✓ Context copied to clipboard\033[0m\n\n")
}
p := proxy.New(cfg, startIdx, nil)
p.Run()
}
func runSwitch() {
pids := proxy.ReadAllPIDs()
if len(pids) == 0 {
fmt.Fprintln(os.Stderr, "No running hydra sessions found.")
os.Exit(1)
}
signaled := 0
for _, pid := range pids {
proc, err := os.FindProcess(pid)
if err != nil {
continue
}
if err := proc.Signal(syscall.SIGUSR1); err != nil {
fmt.Fprintf(os.Stderr, "Failed to signal pid %d: %v\n", pid, err)
continue
}
signaled++
}
fmt.Fprintf(os.Stderr, "Sent switch signal to %d hydra session(s)\n", signaled)
}
func runStatus() {
sess, err := session.LoadLast()
if err != nil {
fmt.Fprintln(os.Stderr, "No previous session.")
os.Exit(1)
}
fmt.Printf("Last provider: %s\n", sess.LastProvider)
fmt.Printf("Limit hit: %v\n", sess.LimitHit)
if sess.RecentOutput != "" {
lines := strings.Split(sess.RecentOutput, "\n")
show := lines
if len(show) > 10 {
show = show[len(show)-10:]
}
fmt.Printf("Recent output:\n%s\n", strings.Join(show, "\n"))
}
}
func runConfigInfo() {
cfg := loadConfig()
fmt.Printf("Config: %s\n\n", cfg.Path)
fmt.Printf("Providers (%d):\n", len(cfg.Providers))
for i, p := range cfg.Providers {
role := "primary"
if i > 0 {
role = fmt.Sprintf("fallback-%d", i)
}
fmt.Printf(" [%s] %s → %s %s\n", role, p.Name, p.Command, strings.Join(p.Args, " "))
}
fmt.Printf("\nSwitch key: %s\n", cfg.SwitchKey)
fmt.Printf("Limit patterns: %d configured\n", len(cfg.LimitPatterns))
}
func loadConfig() *config.Config {
path := os.Getenv("HYDRA_CONFIG")
if path == "" {
home, _ := os.UserHomeDir()
path = filepath.Join(home, ".config", "hydra", "config.yaml")
}
cfg, err := config.Load(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Config error (%s): %v\n", path, err)
os.Exit(1)
}
return cfg
}
func copyToClipboard(text string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("pbcopy")
case "windows":
cmd = exec.Command("clip.exe")
default:
if _, err := exec.LookPath("xclip"); err == nil {
cmd = exec.Command("xclip", "-selection", "clipboard")
} else {
cmd = exec.Command("xsel", "--clipboard", "--input")
}
}
cmd.Stdin = strings.NewReader(text)
return cmd.Run()
}
func printUsage() {
fmt.Println(`hydra - unified AI coding CLI with automatic fallback
Usage:
hydra Start with primary provider (interactive)
hydra <provider> Start with specific provider (e.g. hydra codex)
hydra switch Switch provider (run from another terminal)
hydra --continue, -c Resume from last session with context
hydra config Show config
hydra status Show last session
To switch providers while a session is running:
Open another terminal and run: hydra switch
When a usage limit is detected automatically, you'll also be prompted.
Context is captured and copied to your clipboard on switch.
Config: ~/.config/hydra/config.yaml`)
}