Skip to content

Commit 40fdb76

Browse files
committed
support thread deletion
1 parent b733ea1 commit 40fdb76

5 files changed

Lines changed: 58 additions & 74 deletions

File tree

go.mod

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ module github.com/aavshr/panda
33
go 1.23
44

55
require (
6+
github.com/adrg/xdg v0.5.0
67
github.com/charmbracelet/bubbles v0.18.0
78
github.com/charmbracelet/bubbletea v0.25.0
89
github.com/charmbracelet/lipgloss v0.9.1
910
github.com/jmoiron/sqlx v1.3.5
1011
github.com/matoous/go-nanoid/v2 v2.1.0
1112
github.com/mattn/go-sqlite3 v1.14.22
13+
github.com/sashabaranov/go-openai v1.28.2
1214
golang.org/x/term v0.23.0
1315
)
1416

1517
require (
16-
github.com/adrg/xdg v0.5.0 // indirect
1718
github.com/atotto/clipboard v0.1.4 // indirect
1819
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
1920
github.com/containerd/console v1.0.4 // indirect
@@ -27,11 +28,6 @@ require (
2728
github.com/muesli/termenv v0.15.2 // indirect
2829
github.com/rivo/uniseg v0.4.7 // indirect
2930
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
30-
github.com/sashabaranov/go-openai v1.28.2 // indirect
31-
golang.design/x/clipboard v0.7.0 // indirect
32-
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
33-
golang.org/x/image v0.6.0 // indirect
34-
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
3531
golang.org/x/sync v0.6.0 // indirect
3632
golang.org/x/sys v0.23.0 // indirect
3733
golang.org/x/text v0.14.0 // indirect

go.sum

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
21
github.com/adrg/xdg v0.5.0 h1:dDaZvhMXatArP1NPHhnfaQUqWBLBsmx1h1HXQdMoFCY=
32
github.com/adrg/xdg v0.5.0/go.mod h1:dDdY4M4DF9Rjy4kHPeNL+ilVF+p2lK8IdM9/rTSGcI4=
43
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
@@ -57,62 +56,15 @@ github.com/sashabaranov/go-openai v1.28.2 h1:Q3pi34SuNYNN7YrqpHlHbpeYlf75ljgHOAV
5756
github.com/sashabaranov/go-openai v1.28.2/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
5857
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
5958
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
60-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
61-
golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo=
62-
golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E=
63-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
64-
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
65-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
66-
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU=
67-
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
68-
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
69-
golang.org/x/image v0.6.0 h1:bR8b5okrPI3g/gyZakLZHeWxAR8Dn5CyxXv1hLH5g/4=
70-
golang.org/x/image v0.6.0/go.mod h1:MXLdDR43H7cDJq5GEGXEVeeNhPgi+YYEQ2pC1byI1x0=
71-
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
72-
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c h1:Gk61ECugwEHL6IiyyNLXNzmu8XslmRP2dS0xjIYhbb4=
73-
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY=
74-
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
75-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
76-
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
77-
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
78-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
79-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
80-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
81-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
82-
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
83-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
84-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
85-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
8659
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
8760
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
88-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
89-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
90-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
91-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
92-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
93-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9461
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
95-
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9662
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9763
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
9864
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
99-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
100-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
101-
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
10265
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
10366
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
104-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
105-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
106-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
107-
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
108-
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
10967
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
11068
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
111-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
112-
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
113-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
114-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
115-
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
116-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
11769
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
11870
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/ui/components/list.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ type ListSelectMsg struct {
1717
Index int
1818
}
1919

20+
type ListDeleteMsg struct {
21+
Index int
22+
}
23+
2024
func ListEnterCmd(selectedIndex int) func() tea.Msg {
2125
return func() tea.Msg {
2226
return ListEnterMsg{
@@ -33,6 +37,14 @@ func ListSelectCmd(selectedIndex int) func() tea.Msg {
3337
}
3438
}
3539

40+
func ListDeleteCmd(selectedIndex int) func() tea.Msg {
41+
return func() tea.Msg {
42+
return ListDeleteMsg{
43+
Index: selectedIndex,
44+
}
45+
}
46+
}
47+
3648
type ListModel struct {
3749
inner list.Model
3850
}
@@ -56,6 +68,7 @@ func NewListModel(i *NewListModelInput) ListModel {
5668
// no item should be selected by default
5769
model.Select(-1)
5870
model.Styles.NoItems.Padding(0, 0, 1, 2)
71+
model.ShowHelp()
5972

6073
// TODO: what if title is not plural
6174
model.SetStatusBarItemName(strings.TrimSuffix(strings.ToLower(i.Title), "s"), strings.ToLower(i.Title))
@@ -112,7 +125,12 @@ func (m *ListModel) Update(msg tea.Msg) (ListModel, tea.Cmd) {
112125
case tea.KeyEnter:
113126
index := m.inner.Index()
114127
if index >= 0 {
115-
return *m, ListEnterCmd(m.inner.Index())
128+
return *m, ListEnterCmd(index)
129+
}
130+
case tea.KeyCtrlD:
131+
index := m.inner.Index()
132+
if index >= 0 {
133+
return *m, ListDeleteCmd(index)
116134
}
117135
}
118136
if key.Matches(msg, m.inner.KeyMap.CursorUp) || key.Matches(msg, m.inner.KeyMap.CursorDown) {

internal/ui/handlers.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/aavshr/panda/internal/ui/styles"
1515
"github.com/aavshr/panda/internal/utils"
1616
tea "github.com/charmbracelet/bubbletea"
17-
"golang.design/x/clipboard"
1817
)
1918

2019
func (m *Model) handleKeyMsg(keyMsg tea.KeyMsg) (tea.Model, tea.Cmd) {
@@ -193,35 +192,52 @@ func (m *Model) handleListEnterMsg(msg components.ListEnterMsg) tea.Cmd {
193192
}
194193
m.setSelectedComponent(components.ComponentMessages)
195194
m.setFocusedComponent(components.ComponentMessages)
196-
case components.ComponentMessages:
197-
// TODO: how to convey to the user that the message is copied or there was an error
198-
if err := clipboard.Init(); err == nil {
199-
if msg.Index >= len(m.messages) {
200-
return m.cmdError(fmt.Errorf("invalid message index"))
201-
}
202-
message := m.messages[msg.Index].Content
203-
clipboard.Write(clipboard.FmtText, []byte(message))
204-
}
205195
}
206196
return nil
207197
}
208198

199+
func (m *Model) selectActiveThread(index int) error {
200+
m.setActiveThreadIndex(index)
201+
202+
if len(m.threads) == 0 || index >= len(m.threads) {
203+
return fmt.Errorf("invalid thread index")
204+
}
205+
threadId := m.threads[index].ID
206+
messages, err := m.store.ListMessagesByThreadIDPaginated(threadId, 0, m.conf.MessagesLimit)
207+
if err != nil {
208+
return fmt.Errorf("store.ListMessagesByThreadIDPaginated: %w", err)
209+
}
210+
m.setMessages(messages)
211+
return nil
212+
}
213+
209214
func (m *Model) handleListSelectMsg(msg components.ListSelectMsg) tea.Cmd {
210215
switch m.focusedComponent {
211216
case components.ComponentHistory:
212-
m.setActiveThreadIndex(msg.Index)
217+
if err := m.selectActiveThread(msg.Index); err != nil {
218+
return m.cmdError(err)
219+
}
220+
}
221+
return nil
222+
}
213223

214-
if len(m.threads) == 0 || msg.Index >= len(m.threads) {
215-
return m.cmdError(fmt.Errorf("invalid thread index"))
224+
func (m *Model) handleListDeleteMsg(msg components.ListDeleteMsg) tea.Cmd {
225+
switch m.focusedComponent {
226+
case components.ComponentHistory:
227+
// first item is always for new thread so no deletion
228+
if msg.Index == 0 {
229+
return nil
216230
}
217-
threadId := m.threads[msg.Index].ID
218-
messages, err := m.store.ListMessagesByThreadIDPaginated(threadId, 0, m.conf.MessagesLimit)
219-
if err != nil {
231+
if err := m.store.DeleteThread(m.threads[msg.Index].ID); err != nil {
232+
return m.cmdError(err)
233+
}
234+
m.setThreads(append(m.threads[:msg.Index], m.threads[msg.Index+1:]...))
235+
if msg.Index >= len(m.threads) {
236+
msg.Index = len(m.threads) - 1
237+
}
238+
if err := m.selectActiveThread(msg.Index); err != nil {
220239
return m.cmdError(err)
221240
}
222-
m.setMessages(messages)
223-
// TODO: how should we handle selecting a message?
224-
case components.ComponentMessages:
225241
}
226242
return nil
227243
}

internal/ui/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
259259
cmd = m.handleListEnterMsg(msg)
260260
case components.ListSelectMsg:
261261
cmd = m.handleListSelectMsg(msg)
262+
case components.ListDeleteMsg:
263+
cmd = m.handleListDeleteMsg(msg)
262264
case ForwardChatCompletionStreamMsg:
263265
cmd = m.handleForwardChatCompletionStreamMsg(msg)
264266
case error:

0 commit comments

Comments
 (0)