forked from glebkudr/shotgun_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory_manager.go
More file actions
187 lines (157 loc) · 4.51 KB
/
history_manager.go
File metadata and controls
187 lines (157 loc) · 4.51 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
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"time"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
type PromptHistoryItem struct {
ID string `json:"id"`
Timestamp time.Time `json:"timestamp"`
UserTask string `json:"userTask"`
ConstructedPrompt string `json:"constructedPrompt"`
Response string `json:"response"`
APICall string `json:"apiCall,omitempty"`
}
type PromptHistory struct {
Items []PromptHistoryItem `json:"items"`
}
type HistoryManager struct {
app *App
historyPath string
history PromptHistory
mu sync.Mutex
}
func NewHistoryManager(app *App) *HistoryManager {
return &HistoryManager{
app: app,
history: PromptHistory{Items: []PromptHistoryItem{}},
}
}
func (hm *HistoryManager) getHistoryFilePath() (string, error) {
if hm.historyPath != "" {
return hm.historyPath, nil
}
// Use the same directory as settings.json
if hm.app.configPath != "" {
dir := filepath.Dir(hm.app.configPath)
hm.historyPath = filepath.Join(dir, "prompt_history.json")
return hm.historyPath, nil
}
return "", errors.New("config path not initialized in App")
}
func (hm *HistoryManager) LoadHistory() error {
hm.mu.Lock()
defer hm.mu.Unlock()
path, err := hm.getHistoryFilePath()
if err != nil {
return err
}
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
// No history yet, start empty
hm.history = PromptHistory{Items: []PromptHistoryItem{}}
return nil
}
return err
}
err = json.Unmarshal(data, &hm.history)
if err != nil {
wailsRuntime.LogErrorf(hm.app.ctx, "Error unmarshalling history: %v", err)
return err
}
return nil
}
func (hm *HistoryManager) SaveHistory() error {
hm.mu.Lock()
defer hm.mu.Unlock()
path, err := hm.getHistoryFilePath()
if err != nil {
return err
}
data, err := json.MarshalIndent(hm.history, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
func (hm *HistoryManager) AddItem(userTask, constructedPrompt, response, apiCall string) PromptHistoryItem {
hm.mu.Lock()
// Generate simple ID based on timestamp
now := time.Now()
item := PromptHistoryItem{
ID: fmt.Sprintf("%d", now.UnixNano()),
Timestamp: now,
UserTask: userTask,
ConstructedPrompt: constructedPrompt,
Response: response,
APICall: apiCall,
}
// Prepend to keep newest first
hm.history.Items = append([]PromptHistoryItem{item}, hm.history.Items...)
hm.mu.Unlock()
// Save asynchronously to avoid blocking UI too much
go func() {
if err := hm.SaveHistory(); err != nil {
wailsRuntime.LogError(hm.app.ctx, "Failed to save history: "+err.Error())
}
}()
return item
}
func (hm *HistoryManager) GetItems() []PromptHistoryItem {
hm.mu.Lock()
defer hm.mu.Unlock()
// Return a copy to avoid race conditions if modified elsewhere
items := make([]PromptHistoryItem, len(hm.history.Items))
copy(items, hm.history.Items)
return items
}
func (hm *HistoryManager) Clear() error {
hm.mu.Lock()
hm.history.Items = []PromptHistoryItem{}
hm.mu.Unlock()
return hm.SaveHistory()
}
// --- App Methods Binding ---
func (a *App) ExecuteLLMPrompt(userTask, finalPrompt string) (PromptHistoryItem, error) {
if !a.HasActiveLlmKey() {
return PromptHistoryItem{}, errors.New("no active LLM configuration found")
}
cfg := buildProviderConfig(a.settings.LLMSettings)
providerInstance, err := a.getOrCreateProvider(cfg)
if err != nil {
return PromptHistoryItem{}, fmt.Errorf("failed to create provider: %w", err)
}
wailsRuntime.LogInfof(a.ctx, "Executing LLM prompt via %s (%s)...", cfg.Provider, cfg.Model)
// Use provider.Generate. Note: we don't have streaming here yet, so it waits for full response.
response, apiCall, err := providerInstance.Generate(a.ctx, finalPrompt)
var historyItem PromptHistoryItem
if a.historyManager != nil {
historyResponse := response
if err != nil {
historyResponse = fmt.Sprintf("ERROR during prompt execution: %v", err)
}
historyItem = a.historyManager.AddItem(userTask, finalPrompt, historyResponse, apiCall)
}
if err != nil {
return PromptHistoryItem{}, fmt.Errorf("LLM generation failed: %w", err)
}
return historyItem, nil
}
func (a *App) GetPromptHistory() []PromptHistoryItem {
if a.historyManager == nil {
return []PromptHistoryItem{}
}
return a.historyManager.GetItems()
}
func (a *App) ClearPromptHistory() error {
if a.historyManager == nil {
return nil
}
return a.historyManager.Clear()
}