@@ -2,7 +2,12 @@ import {
22 HISTORY_ASK_STATE_KEY ,
33 HISTORY_CODE_COMPLETIONS_STATE_KEY ,
44 HISTORY_EDIT_STATE_KEY ,
5- HISTORY_NO_CONTEXT_STATE_KEY
5+ HISTORY_NO_CONTEXT_STATE_KEY ,
6+ HistoryEntry ,
7+ PINNED_HISTORY_ASK_STATE_KEY ,
8+ PINNED_HISTORY_CODE_COMPLETIONS_STATE_KEY ,
9+ PINNED_HISTORY_EDIT_STATE_KEY ,
10+ PINNED_HISTORY_NO_CONTEXT_STATE_KEY
611} from '@/constants/state-keys'
712import { ViewProvider } from '@/view/backend/view-provider'
813import { SaveHistoryMessage } from '@/view/types/messages'
@@ -12,21 +17,75 @@ export const handle_save_history = async (
1217 message : SaveHistoryMessage
1318) : Promise < void > => {
1419 let key : string | undefined
20+ let pinned_key : string | undefined
1521 switch ( message . mode ) {
1622 case 'ask' :
1723 key = HISTORY_ASK_STATE_KEY
24+ pinned_key = PINNED_HISTORY_ASK_STATE_KEY
1825 break
1926 case 'edit-context' :
2027 key = HISTORY_EDIT_STATE_KEY
28+ pinned_key = PINNED_HISTORY_EDIT_STATE_KEY
2129 break
2230 case 'no-context' :
2331 key = HISTORY_NO_CONTEXT_STATE_KEY
32+ pinned_key = PINNED_HISTORY_NO_CONTEXT_STATE_KEY
2433 break
2534 case 'code-completions' :
2635 key = HISTORY_CODE_COMPLETIONS_STATE_KEY
36+ pinned_key = PINNED_HISTORY_CODE_COMPLETIONS_STATE_KEY
2737 break
2838 }
2939 if ( key ) {
30- await provider . context . workspaceState . update ( key , message . messages )
40+ const text_history : string [ ] = message . messages
41+ if ( text_history . length === 0 ) {
42+ await provider . context . workspaceState . update ( key , [ ] )
43+ return
44+ }
45+
46+ const old_history =
47+ provider . context . workspaceState . get < HistoryEntry [ ] > ( key , [ ] ) || [ ]
48+ const old_history_map = new Map (
49+ old_history . map ( ( entry ) => [ entry . text , entry ] )
50+ )
51+
52+ const new_history : HistoryEntry [ ] = [ ]
53+
54+ // Using a Set to ensure no duplicates are added, though frontend should handle this.
55+ const processedTexts = new Set < string > ( )
56+
57+ for ( const text of text_history ) {
58+ if ( processedTexts . has ( text ) ) continue
59+ processedTexts . add ( text )
60+
61+ // The first item in text_history is the one that was just used.
62+ // Its timestamp is updated to reflect recent use.
63+ if ( text === text_history [ 0 ] ) {
64+ new_history . push ( { text, createdAt : Date . now ( ) } )
65+ } else {
66+ const old_entry = old_history_map . get ( text )
67+ if ( old_entry ) {
68+ new_history . push ( old_entry )
69+ } else {
70+ // Fallback for entries not in old history
71+ new_history . push ( { text, createdAt : Date . now ( ) } )
72+ }
73+ }
74+ }
75+
76+ await provider . context . workspaceState . update ( key , new_history )
77+
78+ if ( pinned_key ) {
79+ const used_text = text_history [ 0 ]
80+ const pinned_history =
81+ provider . context . workspaceState . get < HistoryEntry [ ] > ( pinned_key , [ ] ) ||
82+ [ ]
83+ const pinned_index = pinned_history . findIndex ( ( p ) => p . text === used_text )
84+
85+ if ( pinned_index > - 1 ) {
86+ pinned_history [ pinned_index ] . createdAt = Date . now ( )
87+ await provider . context . workspaceState . update ( pinned_key , pinned_history )
88+ }
89+ }
3190 }
3291}
0 commit comments