@@ -34,17 +34,20 @@ struct ConversationEntry {
3434struct Conversation {
3535 ConversationEntry * entries ;
3636 int deleted_entries ;
37+ int nulled_entries ;
3738 int entry_count ;
3839 int entry_allocated ;
3940 char thread_id [37 ];
4041};
4142
4243static ConversationEntry * prv_create_entry (Conversation * conversation );
4344void prv_destroy_entry (ConversationEntry * entry );
45+ const char * prv_type_to_string (EntryType type );
4446
4547Conversation * conversation_create () {
4648 Conversation * conversation = bmalloc (sizeof (Conversation ));
4749 conversation -> deleted_entries = 0 ;
50+ conversation -> nulled_entries = 0 ;
4851 conversation -> entry_count = 0 ;
4952 conversation -> entry_allocated = 30 ;
5053 conversation -> entries = bmalloc (sizeof (ConversationEntry ) * conversation -> entry_allocated );
@@ -62,6 +65,9 @@ void conversation_destroy(Conversation* conversation) {
6265
6366void prv_destroy_entry (ConversationEntry * entry ) {
6467 switch (entry -> type ) {
68+ case EntryTypeDeleted :
69+ // Nothing to do here.
70+ break ;
6571 case EntryTypePrompt :
6672 free (entry -> content .prompt -> prompt );
6773 free (entry -> content .prompt );
@@ -132,6 +138,7 @@ void prv_destroy_entry(ConversationEntry *entry) {
132138 free (entry -> content .widget );
133139 break ;
134140 }
141+ entry -> type = EntryTypeDeleted ;
135142}
136143
137144static ConversationEntry * prv_create_entry (Conversation * conversation ) {
@@ -141,6 +148,7 @@ static ConversationEntry* prv_create_entry(Conversation* conversation) {
141148 ++ conversation -> entry_allocated ;
142149 conversation -> entries = new_entries ;
143150 }
151+ memset (& conversation -> entries [conversation -> entry_count ], 0 , sizeof (ConversationEntry ));
144152 return & conversation -> entries [conversation -> entry_count ++ ];
145153}
146154
@@ -261,10 +269,29 @@ void conversation_add_widget(Conversation* conversation, ConversationWidget* wid
261269}
262270
263271void conversation_delete_first_entry (Conversation * conversation ) {
264- prv_destroy_entry (& conversation -> entries [conversation -> deleted_entries ]);
272+ ConversationEntry * entry = & conversation -> entries [conversation -> deleted_entries ];
273+ while (entry -> type == EntryTypeDeleted && conversation -> deleted_entries < conversation -> entry_count ) {
274+ conversation -> deleted_entries ++ ;
275+ conversation -> nulled_entries -- ;
276+ entry = & conversation -> entries [conversation -> deleted_entries ];
277+ }
278+ prv_destroy_entry (entry );
265279 conversation -> deleted_entries ++ ;
266280}
267281
282+
283+ void conversation_delete_last_thought (Conversation * conversation ) {
284+ BOBBY_LOG (APP_LOG_LEVEL_DEBUG , "Deleting last thought" );
285+ for (int i = conversation -> entry_count - 2 ; i >= conversation -> deleted_entries ; -- i ) {
286+ ConversationEntry * entry = & conversation -> entries [i ];
287+ if (entry -> type == EntryTypeThought ) {
288+ BOBBY_LOG (APP_LOG_LEVEL_DEBUG , "Deleting thought %d" , i );
289+ prv_destroy_entry (entry );
290+ return ;
291+ }
292+ }
293+ }
294+
268295ConversationEntry * conversation_entry_at_index (Conversation * conversation , int index ) {
269296 if (index >= conversation -> entry_count ) {
270297 BOBBY_LOG (APP_LOG_LEVEL_WARNING , "Caller asked for entry %d, but only %d exist." , index , conversation -> entry_count );
@@ -293,6 +320,8 @@ ConversationEntry* conversation_get_last_of_type(Conversation* conversation, Ent
293320
294321const char * prv_type_to_string (EntryType type ) {
295322 switch (type ) {
323+ case EntryTypeDeleted :
324+ return "EntryTypeDeleted" ;
296325 case EntryTypePrompt :
297326 return "EntryTypePrompt" ;
298327 case EntryTypeResponse :
@@ -371,7 +400,7 @@ const char* conversation_get_thread_id(Conversation* conversation) {
371400}
372401
373402int conversation_length (Conversation * conversation ) {
374- return conversation -> entry_count - conversation -> deleted_entries ;
403+ return conversation -> entry_count - conversation -> deleted_entries - conversation -> nulled_entries ;
375404}
376405
377406bool conversation_is_idle (Conversation * conversation ) {
@@ -393,6 +422,7 @@ bool conversation_is_idle(Conversation* conversation) {
393422
394423static bool prv_entry_type_is_assistant (ConversationEntry * entry ) {
395424 switch (entry -> type ) {
425+ case EntryTypeDeleted :
396426 case EntryTypePrompt :
397427 case EntryTypeError :
398428 case EntryTypeThought :
0 commit comments