-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.go
More file actions
253 lines (229 loc) · 6.48 KB
/
app.go
File metadata and controls
253 lines (229 loc) · 6.48 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
package main
import (
"context"
"encoding/base64"
"fmt"
"os"
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/runtime"
"go.mongodb.org/mongo-driver/v2/mongo"
)
type App struct {
ctx context.Context // Original Wails context
wailsCtx context.Context // Store the original Wails context
cancelFunc context.CancelFunc
operationCtx context.Context // Separate context for operations
log logger.Logger
llamaCliArgs *LlamaCliArgs
llamaEmbedArgs *LlamaEmbedArgs
appArgs *DefaultAppArgs
database *mongo.Database
}
// CancelProcess Update the method
func (app *App) CancelProcess() string {
if app.cancelFunc != nil {
app.log.Info("Canceling current process...")
app.cancelFunc() // Actually cancel the context
return "Process canceled successfully"
}
return "No active process to cancel"
}
// Startup Update to store both contexts
func (app *App) Startup(ctx context.Context) {
app.log.Info("App startup called")
app.ctx = ctx
app.log.Info("Setting up event listeners...")
app.SetupEventListeners()
app.log.Info("Startup complete")
}
// Reset context for operations only
func (app *App) resetContext() {
// Create a cancelable context from the original Wails context
cancelableCtx, cancel := context.WithCancel(app.wailsCtx)
app.operationCtx = cancelableCtx
app.cancelFunc = cancel
}
// NewApp creates a new App application struct
func NewApp(logger logger.Logger, llamaCliArgs *LlamaCliArgs, llamaEmbedArgs *LlamaEmbedArgs, appArgs *DefaultAppArgs, database *mongo.Database) *App {
return &App{
log: logger,
llamaCliArgs: llamaCliArgs,
llamaEmbedArgs: llamaEmbedArgs,
appArgs: appArgs,
database: database,
}
}
func (app *App) Shutdown(ctx context.Context) {
app.ctx = ctx
}
func (app *App) Domready(ctx context.Context) {
app.wailsCtx = ctx
}
func (app *App) BeforeClose(ctx context.Context) (prevent bool) {
dialog, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: "Quit?",
Message: "Are you sure you want to quit?",
})
if err != nil {
return false
}
return dialog != "Yes"
}
func (app *App) GetInferenceHistory() []map[string]interface{} {
data, err := GetInferenceHistory(app.appArgs)
if err != nil {
app.log.Error(err.Error())
return nil
}
result := make([]map[string]interface{}, len(data))
for i, item := range data {
result[i] = map[string]interface{}{
"id": item.ID,
"response": item.Response,
"args": item.Args,
"question": item.Question,
"createdAt": item.CreatedAt,
}
}
return result
}
func (app *App) GetPDFAsBase64(filePath string) (string, error) {
// Validate a file path and check if a file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return "", fmt.Errorf("file does not exist: %s", filePath)
}
// Read the PDF file
data, err := os.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("failed to read file: %v", err)
}
// Encode to base64
return base64.StdEncoding.EncodeToString(data), nil
}
// ChooseFile opens a directory selection dialog
func (app *App) ChooseFile() string {
opts := runtime.OpenDialogOptions{
DefaultDirectory: app.appArgs.DocumentPath,
Title: "Select a file",
Filters: []runtime.FileFilter{
{
DisplayName: "PDF (*.pdf)",
Pattern: "*.pdf",
}, {
DisplayName: "Text (*.txt;*.text)",
Pattern: "*.txt;*.text",
},
{
DisplayName: "CSV (*.csv;*.txt)",
Pattern: "*.csv;*.txt",
},
},
ShowHiddenFiles: false,
CanCreateDirectories: false,
ResolvesAliases: false,
TreatPackagesAsDirectories: false,
}
ret, err := runtime.OpenFileDialog(app.ctx, opts)
if err != nil {
app.log.Error(err.Error())
return err.Error()
}
return ret
}
func (app *App) ChooseFileOrFolderWithToggle() string {
for {
// Show file dialog first
opts := runtime.OpenDialogOptions{
DefaultDirectory: app.appArgs.DocumentPath,
Title: "Select a file (or cancel to choose folder)",
Filters: []runtime.FileFilter{
{
DisplayName: "PDF (*.pdf)",
Pattern: "*.pdf",
}, {
DisplayName: "Text (*.txt;*.text)",
Pattern: "*.txt;*.text",
},
{
DisplayName: "CSV (*.csv;*.txt)",
Pattern: "*.csv;*.txt",
},
},
ShowHiddenFiles: false,
CanCreateDirectories: false,
ResolvesAliases: false,
TreatPackagesAsDirectories: false,
}
ret, err := runtime.OpenFileDialog(app.ctx, opts)
if err != nil {
app.log.Error(err.Error())
return err.Error()
}
// If user canceled file dialog, ask if they want to select a folder instead
if ret == "" {
dialog, err := runtime.MessageDialog(app.ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: "Select Folder Instead?",
Message: "No file selected. Would you like to select a folder instead?",
Buttons: []string{"Yes", "No"},
})
if err != nil || dialog != "Yes" {
return "" // User chose not to select folder or error occurred
}
// Open folder dialog
return app.ChooseFolder()
}
return ret // Return selected file
}
}
func (app *App) ChooseFolder() string {
opts := runtime.OpenDialogOptions{
DefaultDirectory: app.appArgs.DocumentPath,
Title: "Select a folder",
ShowHiddenFiles: false,
CanCreateDirectories: true,
ResolvesAliases: false,
TreatPackagesAsDirectories: false,
}
ret, err := runtime.OpenDirectoryDialog(app.ctx, opts)
if err != nil {
app.log.Error(err.Error())
return err.Error()
}
return ret
}
func (app *App) ChooseImageFile() string {
opts := runtime.OpenDialogOptions{
DefaultDirectory: app.appArgs.DocumentPath,
Title: "Select an image file",
Filters: []runtime.FileFilter{
{
DisplayName: "Image Files (*.png;*.jpg;*.jpeg;*.tiff;*.bmp)",
Pattern: "*.png;*.jpg;*.jpeg;*.tiff;*.bmp",
},
{
DisplayName: "PNG (*.png)",
Pattern: "*.png",
},
{
DisplayName: "JPEG (*.jpg;*.jpeg)",
Pattern: "*.jpg;*.jpeg",
},
},
ShowHiddenFiles: false,
CanCreateDirectories: false,
ResolvesAliases: false,
TreatPackagesAsDirectories: false,
}
ret, err := runtime.OpenFileDialog(app.ctx, opts)
if err != nil {
app.log.Error(err.Error())
return err.Error()
}
return ret
}
func (app *App) SetupEventListeners() {
eventHandler := NewEventHandler(app)
eventHandler.SetupAllEventListeners()
}