|
| 1 | +package storage |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "slices" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const maxHistoryItems = 500 |
| 14 | + |
| 15 | +// HistoryEntry is one line in the JSONL history file. |
| 16 | +type HistoryEntry struct { |
| 17 | + Display string `json:"display"` |
| 18 | + PastedContents map[string]any `json:"pastedContents"` |
| 19 | + Timestamp int64 `json:"timestamp"` |
| 20 | + Project string `json:"project"` |
| 21 | + SessionID string `json:"sessionId,omitempty"` |
| 22 | +} |
| 23 | + |
| 24 | +// History provides append-only input history with per-project filtering. |
| 25 | +// The backing file (~/.codebot/history.jsonl) is shared across all projects; |
| 26 | +// only entries matching the current project are surfaced. |
| 27 | +type History struct { |
| 28 | + path string |
| 29 | + project string |
| 30 | + sessionID string |
| 31 | + items []string // current-project entries, newest first (index 0 = most recent) |
| 32 | + mu sync.Mutex |
| 33 | +} |
| 34 | + |
| 35 | +// NewHistory loads history from path, filtering by project. |
| 36 | +func NewHistory(path, project, sessionID string) *History { |
| 37 | + h := &History{path: path, project: project, sessionID: sessionID} |
| 38 | + h.load() |
| 39 | + return h |
| 40 | +} |
| 41 | + |
| 42 | +// SetSessionID updates the session ID for subsequent entries (e.g. after session switch). |
| 43 | +func (h *History) SetSessionID(id string) { |
| 44 | + h.mu.Lock() |
| 45 | + h.sessionID = id |
| 46 | + h.mu.Unlock() |
| 47 | +} |
| 48 | + |
| 49 | +// Add appends text to history. Duplicates are moved to the front. |
| 50 | +func (h *History) Add(text string) { |
| 51 | + if text == "" { |
| 52 | + return |
| 53 | + } |
| 54 | + h.mu.Lock() |
| 55 | + defer h.mu.Unlock() |
| 56 | + |
| 57 | + // Deduplicate: remove existing occurrence, then prepend. |
| 58 | + if idx := slices.Index(h.items, text); idx >= 0 { |
| 59 | + h.items = slices.Delete(h.items, idx, idx+1) |
| 60 | + } |
| 61 | + h.items = slices.Insert(h.items, 0, text) |
| 62 | + if len(h.items) > maxHistoryItems { |
| 63 | + h.items = h.items[:maxHistoryItems] |
| 64 | + } |
| 65 | + |
| 66 | + h.appendFile(text) |
| 67 | +} |
| 68 | + |
| 69 | +// Get returns the history entry at index (0 = most recent). |
| 70 | +func (h *History) Get(index int) string { |
| 71 | + h.mu.Lock() |
| 72 | + defer h.mu.Unlock() |
| 73 | + if index < 0 || index >= len(h.items) { |
| 74 | + return "" |
| 75 | + } |
| 76 | + return h.items[index] |
| 77 | +} |
| 78 | + |
| 79 | +// Len returns the number of history entries for the current project. |
| 80 | +func (h *History) Len() int { |
| 81 | + h.mu.Lock() |
| 82 | + defer h.mu.Unlock() |
| 83 | + return len(h.items) |
| 84 | +} |
| 85 | + |
| 86 | +// load reads the JSONL file and populates items for the current project. |
| 87 | +func (h *History) load() { |
| 88 | + f, err := os.Open(h.path) |
| 89 | + if err != nil { |
| 90 | + return |
| 91 | + } |
| 92 | + defer f.Close() |
| 93 | + |
| 94 | + seen := make(map[string]struct{}) |
| 95 | + var all []string |
| 96 | + |
| 97 | + scanner := bufio.NewScanner(f) |
| 98 | + scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024) |
| 99 | + for scanner.Scan() { |
| 100 | + var e HistoryEntry |
| 101 | + if json.Unmarshal(scanner.Bytes(), &e) != nil { |
| 102 | + continue |
| 103 | + } |
| 104 | + if e.Project != h.project || e.Display == "" { |
| 105 | + continue |
| 106 | + } |
| 107 | + all = append(all, e.Display) |
| 108 | + } |
| 109 | + |
| 110 | + // Reverse so newest is first, then deduplicate. |
| 111 | + slices.Reverse(all) |
| 112 | + for _, text := range all { |
| 113 | + if _, ok := seen[text]; ok { |
| 114 | + continue |
| 115 | + } |
| 116 | + seen[text] = struct{}{} |
| 117 | + h.items = append(h.items, text) |
| 118 | + if len(h.items) >= maxHistoryItems { |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// appendFile appends a single entry to the JSONL file. |
| 125 | +func (h *History) appendFile(text string) { |
| 126 | + _ = os.MkdirAll(filepath.Dir(h.path), 0o755) |
| 127 | + f, err := os.OpenFile(h.path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644) |
| 128 | + if err != nil { |
| 129 | + return |
| 130 | + } |
| 131 | + defer f.Close() |
| 132 | + |
| 133 | + e := HistoryEntry{ |
| 134 | + Display: text, |
| 135 | + PastedContents: map[string]any{}, |
| 136 | + Timestamp: time.Now().UnixMilli(), |
| 137 | + Project: h.project, |
| 138 | + SessionID: h.sessionID, |
| 139 | + } |
| 140 | + data, err := json.Marshal(e) |
| 141 | + if err != nil { |
| 142 | + return |
| 143 | + } |
| 144 | + data = append(data, '\n') |
| 145 | + _, _ = f.Write(data) |
| 146 | +} |
0 commit comments