/ src / commands / clear.ts
clear.ts
 1  import { Command } from '../commands.js'
 2  import { getMessagesSetter } from '../messages.js'
 3  import { getContext } from '../context.js'
 4  import { getCodeStyle } from '../utils/style.js'
 5  import { clearTerminal } from '../utils/terminal.js'
 6  import { getOriginalCwd, setCwd } from '../utils/state.js'
 7  import { Message } from '../query.js'
 8  
 9  export async function clearConversation(context: {
10    setForkConvoWithMessagesOnTheNextRender: (
11      forkConvoWithMessages: Message[],
12    ) => void
13  }) {
14    await clearTerminal()
15    getMessagesSetter()([])
16    context.setForkConvoWithMessagesOnTheNextRender([])
17    getContext.cache.clear?.()
18    getCodeStyle.cache.clear?.()
19    await setCwd(getOriginalCwd())
20  }
21  
22  const clear = {
23    type: 'local',
24    name: 'clear',
25    description: 'Clear conversation history and free up context',
26    isEnabled: true,
27    isHidden: false,
28    async call(_, context) {
29      clearConversation(context)
30      return ''
31    },
32    userFacingName() {
33      return 'clear'
34    },
35  } satisfies Command
36  
37  export default clear