Repository navigation
Expand file tree
/
Copy pathchat_commands_config.go
More file actions
110 lines (99 loc) · 3.76 KB
/
Copy pathchat_commands_config.go
File metadata and controls
110 lines (99 loc) · 3.76 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
package cmd
import (
"fmt"
"strings"
tea "charm.land/bubbletea/v2"
reflectconfig "github.com/GrayCodeAI/reflect/internal/config"
configfeature "github.com/GrayCodeAI/reflect/internal/features/config"
)
// handleConfigCommand handles /config policy through the feature parser and
// keeps only persistence, session synchronization, and TUI transitions here.
func (m *chatModel) handleConfigCommand(parts []string, text string) (tea.Model, tea.Cmd) {
command, err := configfeature.ParseCommand(parts)
if err != nil {
m.messages = append(m.messages, displayMsg{role: "error", content: err.Error()})
return m, nil
}
switch command.Action {
case configfeature.ActionProvider:
if err := reflectconfig.SetGlobalSetting("provider", command.Value); err != nil {
m.messages = append(m.messages, displayMsg{role: "error", content: err.Error()})
return m, nil
}
m.syncSessionSelection()
modelCacheMu.RLock()
cached, cacheHit := modelCache[m.session.Provider()]
modelCacheMu.RUnlock()
if cacheHit && len(cached) > 0 {
m.session.SetModel(cached[0].ID)
_ = reflectconfig.SetGlobalSetting("model", cached[0].ID)
}
m.messages = append(m.messages, displayMsg{role: "system", content: fmt.Sprintf("Provider set to: %s\nModel: %s\nSaved in relay (provider.json).", command.Value, m.session.Model())})
return m, nil
case configfeature.ActionModel:
value := command.Value
known := configModelChoices(m.configModelOptions, false)
if len(known) > 0 {
found := false
for i, name := range known {
if strings.EqualFold(name, value) || strings.EqualFold(m.configModelOptions[i].ID, value) {
value = m.configModelOptions[i].ID
found = true
break
}
}
if !found {
m.messages = append(m.messages, displayMsg{role: "error", content: "Unknown model: " + value + "\nUse /model to browse available models."})
return m, nil
}
}
if err := reflectconfig.SetGlobalSetting("model", value); err != nil {
m.messages = append(m.messages, displayMsg{role: "error", content: err.Error()})
return m, nil
}
m.syncSessionSelection()
m.messages = append(m.messages, displayMsg{role: "system", content: fmt.Sprintf("Model switched to: %s\nSaved in relay (provider.json).", m.session.Model())})
return m, nil
case configfeature.ActionKeys:
m.messages = append(m.messages, displayMsg{role: "system", content: apiKeyConfigSummary()})
return m, nil
case configfeature.ActionRemoveKey:
return m.openConfigRemoveKeyPanel()
case configfeature.ActionGet:
settings, err := loadEffectiveSettings()
if err != nil {
m.messages = append(m.messages, displayMsg{role: "error", content: err.Error()})
return m, nil
}
value, ok := reflectconfig.SettingValue(settings, command.Key)
if !ok {
m.messages = append(m.messages, displayMsg{role: "error", content: fmt.Sprintf("Unsupported setting key %q", command.Key)})
return m, nil
}
if strings.TrimSpace(value) == "" {
value = "(empty)"
}
m.messages = append(m.messages, displayMsg{role: "system", content: fmt.Sprintf("%s = %s", command.Key, value)})
return m, nil
case configfeature.ActionSet:
if err := reflectconfig.SetGlobalSetting(command.Key, command.Value); err != nil {
m.messages = append(m.messages, displayMsg{role: "error", content: err.Error()})
return m, nil
}
switch configfeature.NormalizeKey(command.Key) {
case "model", "provider":
m.syncSessionSelection()
}
m.messages = append(m.messages, displayMsg{role: "system", content: configfeature.UpdatedMessage(command.Key, command.Value)})
return m, nil
}
settings, err := loadEffectiveSettings()
if err != nil {
m.messages = append(m.messages, displayMsg{role: "error", content: err.Error()})
return m, nil
}
m.settings = settings
next, teaCmd := m.openConfigPanel()
*m = next
return m, teaCmd
}