-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
423 lines (375 loc) · 10.7 KB
/
Copy pathmodel.go
File metadata and controls
423 lines (375 loc) · 10.7 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main
import (
"context"
"strings"
"time"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
)
type appState int
const (
stateIdle appState = iota
stateLoading // warte auf LLM-Antwort
stateConfirm // warte auf Benutzerbestätigung
stateExecuting // Befehl wird ausgeführt
stateConfig // Konfigurationseditor
stateEditPrompt // System-Prompt Multi-Line-Editor
stateDiscover // LLM-Profil per Portscan hinzufügen
)
// discStepType beschreibt den aktuellen Unterschritt im Discover-Flow.
type discStepType int
const (
discEnterHost discStepType = iota // IP/Hostname eingeben
discScanning // Portscan läuft
discPickModel // Modell aus Liste wählen
discEnterName // Profilname eingeben
)
// llmProfile beschreibt einen gespeicherten LLM-Endpunkt.
type llmProfile struct {
Name string
BaseURL string
Model string
APIKey string
Preferred bool
}
// appConfig hält alle zur Laufzeit änderbaren Einstellungen
type appConfig struct {
baseURL string
model string
apiKey string
autoAllow bool
customPrompt string // Zusatzanweisungen, die dem System-Prompt vorangestellt werden
shortcuts [9]string // F1–F9: Nachrichtentext der an den Agent geschickt wird
lang string // "de" | "en" | "zh"
profiles []llmProfile
activeProfileIdx int // -1 = kein Profil aktiv (manuelle Konfiguration)
saveSessions bool
autoUpdate string // "ask" | "auto" | "off"
}
// --- Tea-Nachrichten ---
type agentResponseMsg struct{ resp *AgentResponse }
type commandResultMsg struct {
callID string
output string
err error
cancelled bool
}
type errMsg struct{ err error }
type spinTickMsg struct{}
// --- Modell ---
const maxACVisible = 8
type model struct {
width, height int
viewport viewport.Model
input textinput.Model
promptEditor textarea.Model // für System-Prompt-Bearbeitung
messages []chatMessage
state appState
showAC bool
filtered []SlashCommand
acSel int
pendingTool *pendingTool
toolQueue []pendingTool
currentCmd string
// Konfiguration
cfg appConfig
configSel int // 0=lang,1=autoAllow,2=install,3=autoUpdate,4=saveSessions,5=customPrompt,6-14=F1-F9
configEditing bool // einzeiliger Textmodus aktiv
cfgSection int // 0=Profile, 1=Einstellungen (in stateConfig)
profileSel int // Auswahl in Profilliste (cfgSection==0)
profileSubSel int // -1=Profilzeile, 0=URL, 1=Modell, 2=API-Key (nur cfgSection==0)
// Profil-Discovery
discStep discStepType
discHost string
discModels []foundModel
discErr string
modelSel int
discModelOffset int // Schiebefenster-Start für Modellliste
tempProfile llmProfile
discEditProfile int // >=0: bestehendes Profil bearbeiten (Index), -1: neues Profil
pendingUpdate *updateInfo // gesetzt wenn Update verfügbar und autoUpdate=="ask"
nextUpdateAt time.Time // Zeitpunkt des nächsten geplanten Update-Checks
// Aktivitätsprotokoll
activities []activityEntry
agent *Agent
ready bool
spinner int
spinFrames []string
cancelFunc context.CancelFunc // Abbruch der laufenden LLM-Anfrage oder Befehlsausführung
inputHistory []string // gesendete Nutzernachrichten, neueste am Ende
historyIdx int // -1 = kein History-Modus; sonst Index in inputHistory
inputBeforeHistory string // gespeicherte Eingabe beim Einstieg in den History-Modus
}
func newModel() model {
// Sprache aus Config laden (vor allem anderen, da L.* überall genutzt wird)
cfg := loadConfig()
setLang(cfg.lang)
ti := textinput.New()
ti.Placeholder = L.InputPlaceholder
ti.Prompt = " > "
ti.PromptStyle = promptStyle
ti.TextStyle = inputTextStyle
ti.PlaceholderStyle = placeholderStyle
ti.CharLimit = 512
ti.Focus()
te := textarea.New()
te.Placeholder = L.TextareaPlaceholder
te.ShowLineNumbers = false
te.CharLimit = 4000
// Kein Rahmen – passt sich dem Layout an
noStyle := noStyleDef()
te.FocusedStyle.Base = noStyle
te.BlurredStyle.Base = noStyle
ag := newAgent()
ag.baseURL = cfg.baseURL
ag.model = cfg.model
ag.apiKey = cfg.apiKey
ag.customPrompt = cfg.customPrompt
return model{
input: ti,
promptEditor: te,
agent: ag,
state: stateIdle,
cfg: cfg,
spinFrames: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
historyIdx: -1,
profileSubSel: -1,
discEditProfile: -1,
}
}
func (m model) Init() tea.Cmd {
// cmdInitSize schickt sofort ein WindowSizeMsg — vermeidet Startup-Hänger auf Windows,
// wenn das Terminal das erste Resize-Event verzögert sendet.
cmds := []tea.Cmd{textinput.Blink, cmdInitSize()}
if m.cfg.saveSessions {
cmds = append(cmds, cmdLoadSession())
}
if m.cfg.activeProfileIdx >= 0 && m.cfg.activeProfileIdx < len(m.cfg.profiles) {
p := m.cfg.profiles[m.cfg.activeProfileIdx]
if p.Name != "" {
cmds = append(cmds, cmdHealthCheck(p.BaseURL, p.Name, m.cfg.profiles, m.cfg.activeProfileIdx))
}
}
if m.cfg.autoUpdate != "off" {
cmds = append(cmds, cmdCheckUpdate()) // Chain: checkUpdate → scheduleUpdate → checkUpdate …
}
return tea.Batch(cmds...)
}
// --- Hilfs-Methoden ---
type msgRole int
const (
roleUser msgRole = iota
roleAssistant
roleSystem
roleError
)
type chatMessage struct {
role msgRole
content string
}
type pendingTool struct {
id string
command string
explanation string
}
func (m *model) addMessage(role msgRole, content string) {
m.messages = append(m.messages, chatMessage{role: role, content: content})
}
func (m *model) updateViewport() {
m.recalcViewport()
m.viewport.SetContent(m.buildContent())
m.viewport.GotoBottom()
}
func (m *model) buildContent() string {
if len(m.messages) == 0 {
return dimStyle.Render(L.WelcomeMsg)
}
var sb strings.Builder
sb.WriteString("\n")
for _, msg := range m.messages {
switch msg.role {
case roleUser:
sb.WriteString(userLabelStyle.Render(L.LabelUser) + "\n")
sb.WriteString(wrapText(msg.content, m.width-4, " ") + "\n\n")
case roleAssistant:
sb.WriteString(assistantLabelStyle.Render(L.LabelAssistant) + "\n")
sb.WriteString(wrapText(msg.content, m.width-4, " ") + "\n\n")
case roleSystem:
sb.WriteString(systemMsgStyle.Render(indentText(msg.content, " ")) + "\n\n")
case roleError:
sb.WriteString(errorMsgStyle.Render(" ✗ "+msg.content) + "\n\n")
}
}
return sb.String()
}
// recalcViewport berechnet die Viewport-Höhe neu.
// View() = strings.Join([title, viewport, divider, bottom], "\n")
// Zeilenanzahl = 1 + h + 1 + b = h + b + 2 → h = height - 2 - bottomLines()
func (m *model) recalcViewport() {
if m.height == 0 {
return
}
h := m.height - 2 - m.bottomLines()
if h < 1 {
h = 1
}
m.viewport.Height = h
m.viewport.Width = m.width
}
func (m model) inputLineCount() int {
const promptWidth = 3
avail := m.width - promptWidth
if avail < 1 {
avail = 1
}
n := len([]rune(m.input.Value()))
if n == 0 {
return 1
}
return (n + avail - 1) / avail
}
// bottomLines gibt die Anzahl der Zeilen im unteren Bereich zurück.
func (m *model) bottomLines() int {
switch m.state {
case stateConfirm:
return 3
case stateLoading, stateExecuting:
return 2
case stateConfig:
if m.configEditing {
return 3 // "Bearbeite <feld>:" + input + hints
}
return 2
case stateEditPrompt:
return 2 // hints
case stateDiscover:
switch m.discStep {
case discEnterHost:
if m.discEditProfile >= 0 && m.discErr != "" {
return 2 // Fehlermeldung im View, nur Esc-Hint unten
}
return 3 // label+input, hint, trailing newline
case discEnterName:
return 3
default:
return 2 // spinner/nav + hint
}
}
// stateIdle
lines := 1 + m.inputLineCount()
if m.showAC && len(m.filtered) > 0 {
visible := minInt(len(m.filtered), maxACVisible)
lines += visible
if len(m.filtered) > maxACVisible {
lines++
}
}
return lines
}
func (m *model) updateAC() {
val := m.input.Value()
if strings.HasPrefix(val, "/") {
query := val[1:]
m.filtered = filterCommands(query)
m.showAC = len(m.filtered) > 0
if m.acSel >= len(m.filtered) {
m.acSel = 0
}
} else {
m.showAC = false
m.acSel = 0
}
m.recalcViewport()
}
func (m model) spinnerFrame() string {
return m.spinFrames[m.spinner%len(m.spinFrames)]
}
func (m *model) promptEditorHeight() int {
h := m.height - 2 - 2 // title(1) + divider(1) + hints(2)
if h < 3 {
h = 3
}
return h
}
// updateModelWindow passt discModelOffset an, sodass modelSel im sichtbaren Fenster bleibt.
func (m *model) updateModelWindow() {
const headerLines = 5 // blank + title+blank + found+blank
vis := m.viewport.Height - headerLines
if vis < 1 {
vis = 1
}
if m.modelSel < m.discModelOffset {
m.discModelOffset = m.modelSel
} else if m.modelSel >= m.discModelOffset+vis {
m.discModelOffset = m.modelSel - vis + 1
}
}
// --- Textformatierung ---
func wrapText(text string, width int, indent string) string {
if width <= len(indent)+4 {
return indent + text
}
var sb strings.Builder
for i, line := range strings.Split(text, "\n") {
if i > 0 {
sb.WriteByte('\n')
}
if len(line) <= width-len(indent) {
sb.WriteString(indent + line)
continue
}
words := strings.Fields(line)
cur := indent
for _, word := range words {
if len(cur)+len(word)+1 > width && cur != indent {
sb.WriteString(cur + "\n")
cur = indent + word
} else if cur == indent {
cur = indent + word
} else {
cur = cur + " " + word
}
}
sb.WriteString(cur)
}
return sb.String()
}
func indentText(text, indent string) string {
lines := strings.Split(text, "\n")
for i, l := range lines {
lines[i] = indent + l
}
return strings.Join(lines, "\n")
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
// configFieldLine berechnet die 0-basierte Zeilennummer des configSel-Felds i
// im gerendereten Config-Inhalt (cfgSection==1 ohne Sub-Felder). Für Viewport-Auto-Scroll.
func (m model) configFieldLine(i int) int {
nP := len(m.cfg.profiles)
// Struktur (cfgSection==1): "\n"(1) + Profil-Header"\n\n"(2) + nP Profile + Add-Button"\n\n"(2) + Einstellungen-Header"\n\n"(2) → base = 7+nP
base := 7 + nP
switch {
case i == 0: // Sprache
return base + 0
case i == 1: // Ausführmodus
return base + 1
case i == 2: // Kurzbefehl
return base + 3
case i == 3: // Auto-Update
return base + 4
case i == 4: // Sitzungen
return base + 5
case i == 5: // System-Prompt
return base + 7
case i >= 6 && i <= 14: // F1–F9
return base + 11 + (i - 6)
}
return 0
}