Skip to content

Commit 5ccb070

Browse files
committed
fix(history): handle missing thread
1 parent 0052bf3 commit 5ccb070

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

history/manager.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package history
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/kardolus/chatgpt-cli/api"
7+
"os"
68
"strings"
79
)
810

@@ -26,12 +28,19 @@ func (h *Manager) ParseUserHistory(thread string) ([]string, error) {
2628

2729
historyEntries, err := h.store.ReadThread(thread)
2830
if err != nil {
31+
// Gracefully handle missing file
32+
if errors.Is(err, os.ErrNotExist) {
33+
return []string{}, nil
34+
}
35+
// Return any other error
2936
return nil, err
3037
}
3138

3239
for _, entry := range historyEntries {
3340
if entry.Role == userRole {
34-
result = append(result, entry.Content.(string))
41+
if s, ok := entry.Content.(string); ok {
42+
result = append(result, s)
43+
}
3544
}
3645
}
3746

history/manager_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
. "github.com/onsi/gomega"
99
"github.com/sclevine/spec"
1010
"github.com/sclevine/spec/report"
11+
"os"
1112
"testing"
1213
)
1314

@@ -39,7 +40,10 @@ func testHistory(t *testing.T, when spec.G, it spec.S) {
3940
const threadName = "threadName"
4041

4142
it("returns an error when store fails", func() {
42-
mockHistoryStore.EXPECT().ReadThread(threadName).Return(nil, errors.New("store error")).Times(1)
43+
mockHistoryStore.EXPECT().
44+
ReadThread(threadName).
45+
Return(nil, errors.New("store error")).
46+
Times(1)
4347

4448
_, err := subject.ParseUserHistory(threadName)
4549
Expect(err).To(MatchError("store error"))
@@ -53,7 +57,10 @@ func testHistory(t *testing.T, when spec.G, it spec.S) {
5357
{Message: api.Message{Role: "system", Content: "ignore this"}},
5458
}
5559

56-
mockHistoryStore.EXPECT().ReadThread(threadName).Return(historyEntries, nil).Times(1)
60+
mockHistoryStore.EXPECT().
61+
ReadThread(threadName).
62+
Return(historyEntries, nil).
63+
Times(1)
5764

5865
result, err := subject.ParseUserHistory(threadName)
5966
Expect(err).NotTo(HaveOccurred())
@@ -66,7 +73,22 @@ func testHistory(t *testing.T, when spec.G, it spec.S) {
6673
{Message: api.Message{Role: "system", Content: "setup"}},
6774
}
6875

69-
mockHistoryStore.EXPECT().ReadThread(threadName).Return(historyEntries, nil).Times(1)
76+
mockHistoryStore.EXPECT().
77+
ReadThread(threadName).
78+
Return(historyEntries, nil).
79+
Times(1)
80+
81+
result, err := subject.ParseUserHistory(threadName)
82+
Expect(err).NotTo(HaveOccurred())
83+
Expect(result).To(BeEmpty())
84+
})
85+
86+
it("returns an empty list when the thread does not exist", func() {
87+
// Simulate store returning a file-not-found error
88+
mockHistoryStore.EXPECT().
89+
ReadThread(threadName).
90+
Return(nil, os.ErrNotExist).
91+
Times(1)
7092

7193
result, err := subject.ParseUserHistory(threadName)
7294
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)