@@ -530,6 +530,61 @@ export class WebBridge {
530530 this . dispatch ( 'chat/createNewChat' , undefined ) ;
531531 }
532532
533+ /**
534+ * Clear a chat's message history while keeping the chat itself.
535+ *
536+ * Calls the REST endpoint and mirrors the `chat:cleared` SSE handler
537+ * locally for an immediate UI update — the server's SSE echo (if any)
538+ * is idempotent.
539+ */
540+ async clearChatHistory ( chatId : string ) : Promise < void > {
541+ try {
542+ await this . api . clearChat ( chatId ) ;
543+ } catch ( err ) {
544+ console . error ( '[Bridge] Failed to clear chat history:' , err ) ;
545+ return ;
546+ }
547+ this . dispatch ( 'chat/cleared' , { chatId, messages : true } ) ;
548+ this . loadedChatIds . delete ( chatId ) ;
549+ messageCache . invalidate ( this . host , chatId ) ;
550+ }
551+
552+ /**
553+ * Close (delete) a chat on the server. Mirrors eca-vscode: the chat is
554+ * removed entirely via `DELETE /chats/:id` with no confirmation prompt.
555+ *
556+ * When the closed chat is the active one, selection advances to the most
557+ * recent remaining chat (or a fresh chat if none remain) so the webview
558+ * doesn't keep showing the deleted chat. Local cleanup mirrors the
559+ * `chat:deleted` SSE handler; the server's SSE echo (if any) is idempotent.
560+ */
561+ async closeChat ( chatId : string ) : Promise < void > {
562+ try {
563+ await this . api . deleteChat ( chatId ) ;
564+ } catch ( err ) {
565+ console . error ( '[Bridge] Failed to close chat:' , err ) ;
566+ return ;
567+ }
568+
569+ // If the active chat is being closed, switch away first so the webview
570+ // doesn't keep rendering a chat that no longer exists.
571+ if ( this . currentChatId === chatId ) {
572+ const next = [ ...this . chatEntries ] . reverse ( ) . find ( ( e ) => e . id !== chatId ) ;
573+ if ( next ) {
574+ await this . selectChat ( next . id ) ;
575+ } else {
576+ this . currentChatId = null ;
577+ this . newChat ( ) ;
578+ }
579+ }
580+
581+ this . dispatch ( 'chat/deleted' , chatId ) ;
582+ this . removeChatEntry ( chatId ) ;
583+ this . loadedChatIds . delete ( chatId ) ;
584+ messageCache . invalidate ( this . host , chatId ) ;
585+ this . notifyChatListChange ( ) ;
586+ }
587+
533588
534589
535590 // ---------------------------------------------------------------------------
0 commit comments