@@ -20,11 +20,14 @@ const createMinimalHistoryStore = immer<HistoryStore>((set, get) => {
2020 // TODO: We might want to unify primitives with the RagbitsHistoryStore later
2121 primitives : {
2222 getCurrentConversation : ( ) => {
23- const state = get ( ) ;
24- return (
25- state . conversations [ state . currentConversation ] ??
26- initialConversationValues ( )
27- ) ;
23+ const { currentConversation, conversations } = get ( ) ;
24+ const conversation = conversations [ currentConversation ] ;
25+
26+ if ( ! conversation ) {
27+ throw new Error ( "Tried to get conversation that doesn't exist." ) ;
28+ }
29+
30+ return conversation ;
2831 } ,
2932 addMessage : ( conversationId , message ) => {
3033 const id = uuidv4 ( ) ;
@@ -39,22 +42,36 @@ const createMinimalHistoryStore = immer<HistoryStore>((set, get) => {
3942 return id ;
4043 } ,
4144 deleteMessage : ( conversationId , messageId ) => {
42- set ( ( draft ) => {
43- const conv = draft . conversations [ conversationId ] ;
44- if ( ! conv ) return ;
45- delete conv . history [ messageId ] ;
46- } ) ;
45+ set (
46+ updateConversation ( conversationId , ( draft ) => {
47+ const { history } = draft ;
48+ const messageIds = Object . keys ( history ) ;
49+
50+ if ( messageIds . at ( - 1 ) === messageId ) {
51+ draft . lastMessageId = messageIds . at ( - 2 ) ?? null ;
52+ }
53+
54+ delete draft . history [ messageId ] ;
55+ } ) ,
56+ ) ;
4757 } ,
4858 restore : ( ) => { } , // No-op for minimal implementation
4959 stopAnswering : ( conversationId ) => {
50- const conv = get ( ) . conversations [ conversationId ] ;
51- conv ?. abortController ?. abort ( ) ;
52- set ( ( draft ) => {
53- const c = draft . conversations [ conversationId ] ;
54- if ( ! c ) return ;
55- c . isLoading = false ;
56- c . abortController = null ;
57- } ) ;
60+ const conversation = get ( ) . conversations [ conversationId ] ;
61+
62+ if ( ! conversation ) {
63+ throw new Error (
64+ "Tried to stop answering for conversation that doesn't exist" ,
65+ ) ;
66+ }
67+
68+ conversation . abortController ?. abort ( ) ;
69+ set (
70+ updateConversation ( conversationId , ( draft ) => {
71+ draft . abortController = null ;
72+ draft . isLoading = false ;
73+ } ) ,
74+ ) ;
5875 } ,
5976 } ,
6077
@@ -119,14 +136,35 @@ const createMinimalHistoryStore = immer<HistoryStore>((set, get) => {
119136
120137 selectConversation : ( conversationId : string ) => {
121138 set ( ( draft ) => {
139+ if ( draft . currentConversation === conversationId ) {
140+ return ;
141+ }
142+ const conversation = draft . conversations [ conversationId ] ;
143+ if ( ! conversation ) {
144+ throw new Error (
145+ `Tried to select conversation that doesn't exist, id: ${ conversationId } ` ,
146+ ) ;
147+ }
148+
122149 draft . currentConversation = conversationId ;
123150 } ) ;
124151 } ,
125152
126153 deleteConversation : ( conversationId : string ) => {
154+ const {
155+ actions : { newConversation } ,
156+ primitives : { stopAnswering } ,
157+ currentConversation,
158+ } = get ( ) ;
159+ stopAnswering ( conversationId ) ;
160+
127161 set ( ( draft ) => {
128162 delete draft . conversations [ conversationId ] ;
129163 } ) ;
164+
165+ if ( conversationId === currentConversation ) {
166+ return newConversation ( ) ;
167+ }
130168 } ,
131169
132170 // These are no-ops for minimal implementation
0 commit comments