-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
302 lines (251 loc) · 7.65 KB
/
app.go
File metadata and controls
302 lines (251 loc) · 7.65 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
vibecoding "github.com/startvibecoding/prader/vibecoding-gui/internal/vibecoding"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
agent *vibecoding.AgentManager
settings *vibecoding.VibeCodingSettings
}
// NewApp creates a new App application struct
func NewApp(config *vibecoding.SessionConfig) (*App, error) {
// Load VibeCoding settings
settings, err := vibecoding.LoadVibeCodingSettings()
if err != nil {
return nil, fmt.Errorf("load settings: %w", err)
}
// Find VibeCoding binary
vibecodingPath := findVibeCodingBinary()
if vibecodingPath == "" {
return nil, fmt.Errorf("vibecoding binary not found")
}
agent, err := vibecoding.NewAgentManager(vibecodingPath, config)
if err != nil {
return nil, fmt.Errorf("create agent manager: %w", err)
}
return &App{
agent: agent,
settings: settings,
}, nil
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
// GetConfig returns the current configuration
func (a *App) GetConfig() *vibecoding.SessionConfig {
return a.agent.GetConfig()
}
// GetSettings returns the VibeCoding settings
func (a *App) GetSettings() *vibecoding.VibeCodingSettings {
return a.settings
}
// SaveSettings saves the VibeCoding settings
func (a *App) SaveSettings(settings *vibecoding.VibeCodingSettings) error {
if err := vibecoding.SaveSettings(settings); err != nil {
return err
}
a.settings = settings
return nil
}
// GetProviders returns available providers
func (a *App) GetProviders() []map[string]string {
return a.settings.GetProviders()
}
// GetModels returns available models for a provider
func (a *App) GetModels(providerID string) []map[string]interface{} {
return a.settings.GetModels(providerID)
}
// UpdateConfig updates the configuration and restarts the ACP client
func (a *App) UpdateConfig(config *vibecoding.SessionConfig) error {
// Close existing agent
if a.agent != nil {
a.agent.Close()
}
// Create new agent with updated config
vibecodingPath := findVibeCodingBinary()
if vibecodingPath == "" {
return fmt.Errorf("vibecoding binary not found")
}
agent, err := vibecoding.NewAgentManager(vibecodingPath, config)
if err != nil {
return fmt.Errorf("create agent manager: %w", err)
}
a.agent = agent
// Notify frontend
runtime.EventsEmit(a.ctx, "config:updated", config)
return nil
}
// CreateSession creates a new session
func (a *App) CreateSession() (string, error) {
return a.agent.CreateSession()
}
// RestoreSession creates a new ACP session with the specified cwd
// Used to restore a saved session on app restart
func (a *App) RestoreSession(cwd string) (string, error) {
return a.agent.CreateSessionWithCwd(cwd)
}
// SendMessage sends a message and streams response
func (a *App) SendMessage(sessionID, message string) error {
events, err := a.agent.Chat(a.ctx, sessionID, message)
if err != nil {
return err
}
// Process events in background
go func() {
for event := range events {
runtime.EventsEmit(a.ctx, "chat:event", event)
}
}()
return nil
}
// Abort aborts the current operation
func (a *App) Abort(sessionID string) error {
return a.agent.Abort(sessionID)
}
// SendPermissionResponse sends a permission response
func (a *App) SendPermissionResponse(requestID string, optionID string) error {
return a.agent.SendPermissionResponse(requestID, optionID)
}
// ShowNotification shows a notification
func (a *App) ShowNotification(title, message string) {
runtime.EventsEmit(a.ctx, "notification", map[string]string{
"title": title,
"message": message,
})
}
// OpenFileDialog opens a file dialog
func (a *App) OpenFileDialog() (string, error) {
return runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select File",
Filters: []runtime.FileFilter{
{
DisplayName: "All Files",
Pattern: "*.*",
},
},
})
}
// OpenDirectoryDialog opens a directory dialog
func (a *App) OpenDirectoryDialog() (string, error) {
return runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select Directory",
})
}
// SaveSessionConfig saves session configuration
func (a *App) SaveSessionConfig(sessions []map[string]interface{}) error {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("get home dir: %w", err)
}
configDir := filepath.Join(home, ".vibecoding-gui")
if err := os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("create config dir: %w", err)
}
configPath := filepath.Join(configDir, "sessions.json")
data, err := json.MarshalIndent(sessions, "", " ")
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
return os.WriteFile(configPath, data, 0644)
}
// LoadSessionConfig loads session configuration
func (a *App) LoadSessionConfig() ([]map[string]interface{}, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("get home dir: %w", err)
}
configPath := filepath.Join(home, ".vibecoding-gui", "sessions.json")
data, err := os.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
return []map[string]interface{}{}, nil
}
return nil, fmt.Errorf("read config: %w", err)
}
var sessions []map[string]interface{}
if err := json.Unmarshal(data, &sessions); err != nil {
return nil, fmt.Errorf("parse config: %w", err)
}
return sessions, nil
}
// LoadUIConfig loads UI configuration from ~/.vibecoding-gui/ui.json
func (a *App) LoadUIConfig() (map[string]interface{}, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("get home dir: %w", err)
}
configPath := filepath.Join(home, ".vibecoding-gui", "ui.json")
data, err := os.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {
return map[string]interface{}{"theme": "dark"}, nil
}
return nil, fmt.Errorf("read config: %w", err)
}
var config map[string]interface{}
if err := json.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("parse config: %w", err)
}
return config, nil
}
// SaveUIConfig saves UI configuration to ~/.vibecoding-gui/ui.json
func (a *App) SaveUIConfig(config map[string]interface{}) error {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("get home dir: %w", err)
}
configDir := filepath.Join(home, ".vibecoding-gui")
if err := os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("create config dir: %w", err)
}
configPath := filepath.Join(configDir, "ui.json")
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
return os.WriteFile(configPath, data, 0644)
}
// findVibeCodingBinary finds the VibeCoding binary
func findVibeCodingBinary() string {
// Check common locations
locations := []string{
"vibecoding",
"/usr/local/bin/vibecoding",
filepath.Join(os.Getenv("HOME"), ".local/bin/vibecoding"),
filepath.Join(os.Getenv("HOME"), "go/bin/vibecoding"),
}
// Check current directory
cwd, err := os.Getwd()
if err == nil {
// Check parent directories
for dir := cwd; dir != "/"; dir = filepath.Dir(dir) {
locations = append(locations, filepath.Join(dir, "vibecoding", "build", "bin", "vibecoding"))
locations = append(locations, filepath.Join(dir, "vibecoding", "vibecoding"))
}
}
for _, loc := range locations {
if _, err := os.Stat(loc); err == nil {
return loc
}
}
// Try PATH
path, err := exec.LookPath("vibecoding")
if err == nil {
return path
}
return ""
}