ai-agent/internal/tui/prompthistory.go
admin 8dc496b626
Some checks failed
CI / test (push) Has been cancelled
Release / release (push) Failing after 4m36s
first commit
2026-03-08 15:40:34 +07:00

51 lines
1.3 KiB
Go

package tui
import (
"encoding/json"
"os"
"path/filepath"
)
const promptHistoryMax = 100
// DefaultPromptHistoryPath returns the path for persistent prompt history.
func DefaultPromptHistoryPath() string {
home, err := os.UserHomeDir()
if err != nil {
return "prompt_history.json"
}
return filepath.Join(home, ".config", "ai-agent", "prompt_history.json")
}
// LoadPromptHistory reads saved prompt history from path. Returns nil on error or missing file.
func LoadPromptHistory(path string) ([]string, error) {
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
var list []string
if err := json.Unmarshal(data, &list); err != nil {
return nil, err
}
if len(list) > promptHistoryMax {
list = list[len(list)-promptHistoryMax:]
}
return list, nil
}
// SavePromptHistory writes prompt history to path, creating the directory if needed.
func SavePromptHistory(path string, items []string) error {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
data, err := json.MarshalIndent(items, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0o644)
}