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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
package main
import (
"fmt"
"slices"
"strconv"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// Define constants for cell types
const (
CellTypeCheckbox = "checkbox"
CellTypeDropdown = "dropdown"
CellTypeInput = "input"
CellTypeHeader = "header"
CellTypeListPopup = "listpopup"
)
// CellData holds additional data for each cell
type CellData struct {
Type string
Options []string
OnChange interface{}
}
// makePropsTable creates a table-based alternative to the props form
// This allows for better key bindings and immediate effect of changes
func makePropsTable(props map[string]float32) *tview.Table {
// Create a new table
table := tview.NewTable().
SetBorders(true).
SetSelectable(true, false).
SetSelectedStyle(tcell.StyleDefault.Background(tcell.ColorGray).Foreground(tcell.ColorWhite)) // Allow row selection but not column selection
table.SetTitle("Properties Configuration (Press 'x' to exit)").
SetTitleAlign(tview.AlignLeft)
row := 0
// Add a header or note row
headerCell := tview.NewTableCell("Props for llamacpp completion call").
SetTextColor(tcell.ColorYellow).
SetAlign(tview.AlignLeft).
SetSelectable(false)
table.SetCell(row, 0, headerCell)
table.SetCell(row, 1,
tview.NewTableCell("press 'x' to exit").
SetTextColor(tcell.ColorYellow).
SetSelectable(false))
row++
// Store cell data for later use in selection functions
cellData := make(map[string]*CellData)
var modelCellID string // will be set for the model selection row
// Helper function to add a checkbox-like row
addCheckboxRow := func(label string, initialValue bool, onChange func(bool)) {
table.SetCell(row, 0,
tview.NewTableCell(label).
SetTextColor(tcell.ColorWhite).
SetAlign(tview.AlignLeft).
SetSelectable(false))
valueText := "No"
if initialValue {
valueText = "Yes"
}
valueCell := tview.NewTableCell(valueText).
SetTextColor(tcell.ColorYellow).
SetAlign(tview.AlignCenter)
table.SetCell(row, 1, valueCell)
// Store cell data
cellID := fmt.Sprintf("checkbox_%d", row)
cellData[cellID] = &CellData{
Type: CellTypeCheckbox,
OnChange: onChange,
}
row++
}
// Helper function to add a dropdown-like row, that opens a list popup
addListPopupRow := func(label string, options []string, initialValue string, onChange func(string)) {
table.SetCell(row, 0,
tview.NewTableCell(label).
SetTextColor(tcell.ColorWhite).
SetAlign(tview.AlignLeft).
SetSelectable(false))
valueCell := tview.NewTableCell(initialValue).
SetTextColor(tcell.ColorYellow).
SetAlign(tview.AlignCenter)
table.SetCell(row, 1, valueCell)
// Store cell data
cellID := fmt.Sprintf("listpopup_%d", row)
cellData[cellID] = &CellData{
Type: CellTypeListPopup,
Options: options,
OnChange: onChange,
}
row++
}
// Helper function to add an input field row
addInputRow := func(label string, initialValue string, onChange func(string)) {
table.SetCell(row, 0,
tview.NewTableCell(label).
SetTextColor(tcell.ColorWhite).
SetAlign(tview.AlignLeft).
SetSelectable(false))
valueCell := tview.NewTableCell(initialValue).
SetTextColor(tcell.ColorYellow).
SetAlign(tview.AlignCenter)
table.SetCell(row, 1, valueCell)
// Store cell data
cellID := fmt.Sprintf("input_%d", row)
cellData[cellID] = &CellData{
Type: CellTypeInput,
OnChange: onChange,
}
row++
}
// Add checkboxes
addCheckboxRow("Insert <think> tag (/completion only)", cfg.ThinkUse, func(checked bool) {
cfg.ThinkUse = checked
})
addCheckboxRow("RAG use", cfg.RAGEnabled, func(checked bool) {
cfg.RAGEnabled = checked
})
addCheckboxRow("Inject role", injectRole, func(checked bool) {
injectRole = checked
})
addCheckboxRow("TTS Enabled", cfg.TTS_ENABLED, func(checked bool) {
cfg.TTS_ENABLED = checked
})
// Add dropdowns
logLevels := []string{"Debug", "Info", "Warn"}
addListPopupRow("Set log level", logLevels, GetLogLevel(), func(option string) {
setLogLevel(option)
})
// Helper function to get model list for a given API
getModelListForAPI := func(api string) []string {
if strings.Contains(api, "api.deepseek.com/") {
return []string{"deepseek-chat", "deepseek-reasoner"}
} else if strings.Contains(api, "openrouter.ai") {
return ORFreeModels
}
return LocalModels
}
var modelRowIndex int // will be set before model row is added
// Prepare API links dropdown - ensure current API is first, avoid duplicates
apiLinks := make([]string, 0, len(cfg.ApiLinks)+1)
apiLinks = append(apiLinks, cfg.CurrentAPI)
for _, api := range cfg.ApiLinks {
if api != cfg.CurrentAPI {
apiLinks = append(apiLinks, api)
}
}
addListPopupRow("Select an api", apiLinks, cfg.CurrentAPI, func(option string) {
cfg.CurrentAPI = option
// Update model list based on new API
newModelList := getModelListForAPI(cfg.CurrentAPI)
if modelCellID != "" {
if data := cellData[modelCellID]; data != nil {
data.Options = newModelList
}
}
// Ensure chatBody.Model is in the new list; if not, set to first available model
if len(newModelList) > 0 && !slices.Contains(newModelList, chatBody.Model) {
chatBody.Model = newModelList[0]
cfg.CurrentModel = chatBody.Model
// Update the displayed cell text - need to find model row
// Search for model row by label
for r := 0; r < table.GetRowCount(); r++ {
if cell := table.GetCell(r, 0); cell != nil && cell.Text == "Select a model" {
if valueCell := table.GetCell(r, 1); valueCell != nil {
valueCell.SetText(chatBody.Model)
}
break
}
}
}
})
// Prepare model list dropdown
modelRowIndex = row
modelCellID = fmt.Sprintf("listpopup_%d", modelRowIndex)
modelList := getModelListForAPI(cfg.CurrentAPI)
addListPopupRow("Select a model", modelList, chatBody.Model, func(option string) {
chatBody.Model = option
cfg.CurrentModel = chatBody.Model
})
// Role selection dropdown
addListPopupRow("Write next message as", listRolesWithUser(), cfg.WriteNextMsgAs, func(option string) {
cfg.WriteNextMsgAs = option
})
// Add input fields
addInputRow("New char to write msg as", "", func(text string) {
if text != "" {
cfg.WriteNextMsgAs = text
}
})
addInputRow("Username", cfg.UserRole, func(text string) {
if text != "" {
renameUser(cfg.UserRole, text)
cfg.UserRole = text
}
})
// Add property fields (the float32 values)
for propName, value := range props {
propName := propName // capture loop variable for closure
propValue := fmt.Sprintf("%v", value)
addInputRow(propName, propValue, func(text string) {
if val, err := strconv.ParseFloat(text, 32); err == nil {
props[propName] = float32(val)
}
})
}
// Set selection function to handle dropdown-like behavior
table.SetSelectedFunc(func(selectedRow, selectedCol int) {
// Only handle selection on the value column (column 1)
if selectedCol != 1 {
// If user selects the label column, move to the value column
if table.GetRowCount() > selectedRow && table.GetColumnCount() > 1 {
table.Select(selectedRow, 1)
}
return
}
// Get the cell and its corresponding data
cell := table.GetCell(selectedRow, selectedCol)
cellID := fmt.Sprintf("checkbox_%d", selectedRow)
// Check if it's a checkbox
if cellData[cellID] != nil && cellData[cellID].Type == CellTypeCheckbox {
data := cellData[cellID]
if onChange, ok := data.OnChange.(func(bool)); ok {
// Toggle the checkbox value
newValue := cell.Text == "No"
onChange(newValue)
if newValue {
cell.SetText("Yes")
} else {
cell.SetText("No")
}
}
return
}
// Check for dropdown
dropdownCellID := fmt.Sprintf("dropdown_%d", selectedRow)
if cellData[dropdownCellID] != nil && cellData[dropdownCellID].Type == CellTypeDropdown {
data := cellData[dropdownCellID]
if onChange, ok := data.OnChange.(func(string)); ok && data.Options != nil {
// Find current option and cycle to next
currentValue := cell.Text
currentIndex := -1
for i, opt := range data.Options {
if opt == currentValue {
currentIndex = i
break
}
}
// Move to next option (cycle back to 0 if at end)
nextIndex := (currentIndex + 1) % len(data.Options)
newValue := data.Options[nextIndex]
onChange(newValue)
cell.SetText(newValue)
}
return
}
// Check for listpopup
listPopupCellID := fmt.Sprintf("listpopup_%d", selectedRow)
if cellData[listPopupCellID] != nil && cellData[listPopupCellID].Type == CellTypeListPopup {
data := cellData[listPopupCellID]
if onChange, ok := data.OnChange.(func(string)); ok {
// Get label for context
labelCell := table.GetCell(selectedRow, 0)
label := "item"
if labelCell != nil {
label = labelCell.Text
}
// For model selection, always compute fresh options from current API
if label == "Select a model" {
freshOptions := getModelListForAPI(cfg.CurrentAPI)
data.Options = freshOptions
// Also update the cell data map
cellData[listPopupCellID].Options = freshOptions
}
// Handle nil options
if data.Options == nil {
logger.Error("options list is nil for", "label", label)
if err := notifyUser("Configuration error", "Options list is nil for "+label); err != nil {
logger.Error("failed to send notification", "error", err)
}
return
}
// Check for empty options list
if len(data.Options) == 0 {
logger.Warn("empty options list for", "label", label, "api", cfg.CurrentAPI, "localModelsLen", len(LocalModels), "orModelsLen", len(ORFreeModels))
message := "No options available for " + label
if label == "Select a model" {
if strings.Contains(cfg.CurrentAPI, "openrouter.ai") {
message = "No OpenRouter models available. Check token and connection."
} else if strings.Contains(cfg.CurrentAPI, "api.deepseek.com") {
message = "DeepSeek models should be available. Please report bug."
} else {
message = "No llama.cpp models loaded. Ensure llama.cpp server is running with models."
}
}
if err := notifyUser("Empty list", message); err != nil {
logger.Error("failed to send notification", "error", err)
}
return
}
// Create a list primitive
apiList := tview.NewList().ShowSecondaryText(false).
SetSelectedBackgroundColor(tcell.ColorGray)
apiList.SetTitle("Select " + label).SetBorder(true)
for i, api := range data.Options {
if api == cell.Text {
apiList.SetCurrentItem(i)
}
apiList.AddItem(api, "", 0, nil)
}
apiList.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) {
onChange(mainText)
cell.SetText(mainText)
pages.RemovePage("apiListPopup")
})
apiList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEscape {
pages.RemovePage("apiListPopup")
return nil
}
return event
})
modal := func(p tview.Primitive, width, height int) tview.Primitive {
return tview.NewFlex().
AddItem(nil, 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(p, height, 1, true).
AddItem(nil, 0, 1, false), width, 1, true).
AddItem(nil, 0, 1, false)
}
// Add modal page and make it visible
pages.AddPage("apiListPopup", modal(apiList, 80, 20), true, true)
app.SetFocus(apiList)
}
return
}
// Handle input fields by creating an input modal on selection
inputCellID := fmt.Sprintf("input_%d", selectedRow)
if cellData[inputCellID] != nil && cellData[inputCellID].Type == CellTypeInput {
data := cellData[inputCellID]
if onChange, ok := data.OnChange.(func(string)); ok {
// Create an input modal
currentValue := cell.Text
inputFld := tview.NewInputField()
inputFld.SetLabel("Edit value: ")
inputFld.SetText(currentValue)
inputFld.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
newText := inputFld.GetText()
onChange(newText)
cell.SetText(newText) // Update the table cell
}
pages.RemovePage("editModal")
})
// Create a simple modal with the input field
modalFlex := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(tview.NewBox(), 0, 1, false). // Spacer
AddItem(tview.NewFlex().
AddItem(tview.NewBox(), 0, 1, false). // Spacer
AddItem(inputFld, 30, 1, true). // Input field
AddItem(tview.NewBox(), 0, 1, false), // Spacer
0, 1, true).
AddItem(tview.NewBox(), 0, 1, false) // Spacer
// Add modal page and make it visible
pages.AddPage("editModal", modalFlex, true, true)
}
return
}
})
// Set input capture to handle 'x' key for exiting
table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyRune && event.Rune() == 'x' {
pages.RemovePage(propsPage)
updateStatusLine()
return nil
}
return event
})
return table
}
|