@@ -61,6 +61,46 @@ interface CommandSuggestion {
6161 description : string ;
6262}
6363
64+ function messageRequestsHistoryClear ( message : unknown ) : boolean {
65+ if ( ! message || typeof message !== "object" ) return false ;
66+ const metadata = ( message as Record < string , unknown > ) . metadata ;
67+ if ( ! metadata || typeof metadata !== "object" ) return false ;
68+
69+ const meta = metadata as Record < string , unknown > ;
70+ if ( meta . clear_history === true ) return true ;
71+
72+ const nested = meta . metadata ;
73+ return (
74+ ! ! nested &&
75+ typeof nested === "object" &&
76+ ( nested as Record < string , unknown > ) . clear_history === true
77+ ) ;
78+ }
79+
80+ function payloadRequestsHistoryClear ( payload : unknown ) : boolean {
81+ if ( ! payload || typeof payload !== "object" ) return false ;
82+
83+ const record = payload as Record < string , unknown > ;
84+ const candidates : unknown [ ] = [ ] ;
85+
86+ if ( record . object === "message" ) {
87+ candidates . push ( record ) ;
88+ }
89+
90+ if ( record . object === "response" && Array . isArray ( record . output ) ) {
91+ candidates . push ( ...record . output ) ;
92+ }
93+
94+ return candidates . some ( messageRequestsHistoryClear ) ;
95+ }
96+
97+ function payloadCompletesResponse ( payload : unknown ) : boolean {
98+ if ( ! payload || typeof payload !== "object" ) return false ;
99+
100+ const record = payload as Record < string , unknown > ;
101+ return record . object === "response" && record . status === "completed" ;
102+ }
103+
64104function renderSuggestionLabel ( command : string , description : string ) {
65105 return (
66106 < div className = { styles . suggestionLabel } >
@@ -433,11 +473,20 @@ export default function ChatPage() {
433473 const chatIdRef = useRef ( chatId ) ;
434474 const navigateRef = useRef ( navigate ) ;
435475 const chatRef = useRef < IAgentScopeRuntimeWebUIRef > ( null ) ;
476+ const pendingClearHistoryRef = useRef ( false ) ;
436477
437478 useMessageHistoryNavigation ( chatRef , isChatActive , isComposingRef ) ;
438479 chatIdRef . current = chatId ;
439480 navigateRef . current = navigate ;
440481
482+ const scheduleHistoryClear = useCallback ( ( ) => {
483+ queueMicrotask ( ( ) => {
484+ if ( ! pendingClearHistoryRef . current ) return ;
485+ pendingClearHistoryRef . current = false ;
486+ chatRef . current ?. messages . removeAllMessages ( ) ;
487+ } ) ;
488+ } , [ ] ) ;
489+
441490 // Tell sessionApi which session to put first in getSessionList, so the library's
442491 // useMount auto-selects the correct session without an extra getSession round-trip.
443492 if ( chatId && sessionApi . preferredChatId !== chatId ) {
@@ -777,6 +826,16 @@ export default function ChatPage() {
777826 api : {
778827 ...defaultConfig . api ,
779828 fetch : customFetch ,
829+ responseParser : ( chunk : string ) => {
830+ const payload = JSON . parse ( chunk ) as Record < string , unknown > ;
831+ if ( payloadRequestsHistoryClear ( payload ) ) {
832+ pendingClearHistoryRef . current = true ;
833+ if ( payloadCompletesResponse ( payload ) ) {
834+ scheduleHistoryClear ( ) ;
835+ }
836+ }
837+ return payload as any ;
838+ } ,
780839 replaceMediaURL : ( url : string ) => {
781840 return toDisplayUrl ( url ) ;
782841 } ,
@@ -824,7 +883,15 @@ export default function ChatPage() {
824883 replace : true ,
825884 } ,
826885 } as unknown as IAgentScopeRuntimeWebUIOptions ;
827- } , [ customFetch , copyResponse , handleFileUpload , t , isDark , multimodalCaps ] ) ;
886+ } , [
887+ customFetch ,
888+ copyResponse ,
889+ handleFileUpload ,
890+ t ,
891+ isDark ,
892+ multimodalCaps ,
893+ scheduleHistoryClear ,
894+ ] ) ;
828895
829896 return (
830897 < div
0 commit comments