clear Command
The clear
command provides a way to reset the current conversation, clear the message history, and free up context space.
Implementation
The command is implemented in commands/clear.ts
as a type: 'local' command, which means it doesn't render a UI component but rather performs an operation directly:
import { Command } from "../commands";
import { getMessagesSetter } from "../messages";
import { getContext } from "../context";
import { getCodeStyle } from "../utils/style";
import { clearTerminal } from "../utils/terminal";
import { getOriginalCwd, setCwd } from "../utils/state";
import { Message } from "../query";
export async function clearConversation(context: {
setForkConvoWithMessagesOnTheNextRender: (
forkConvoWithMessages: Message[]
) => void;
}) {
await clearTerminal();
getMessagesSetter()([]);
context.setForkConvoWithMessagesOnTheNextRender([]);
getContext.cache.clear?.();
getCodeStyle.cache.clear?.();
await setCwd(getOriginalCwd());
}
const clear = {
type: "local",
name: "clear",
description: "Clear conversation history and free up context",
isEnabled: true,
isHidden: false,
async call(_, context) {
clearConversation(context);
return "";
},
userFacingName() {
return "clear";
},
} satisfies Command;
export default clear;
Functionality
The clear
command performs several key operations:
-
Terminal Cleaning: Uses
clearTerminal()
to visually reset the terminal display. -
Message History Reset: Sets the message array to empty using
getMessagesSetter()([])
. -
Context Clearing: Clears the cached context information using
getContext.cache.clear()
. -
Style Cache Reset: Clears the cached code style information with
getCodeStyle.cache.clear()
. -
Working Directory Reset: Resets the current working directory to the original one using
setCwd(getOriginalCwd())
. -
Conversation Forking Reset: Clears any pending conversation forks via the context parameter.
Technical Implementation Notes
The clear
command makes use of several architectural patterns in Claude Code:
-
Getter/Setter Pattern: Uses message setter functions obtained through
getMessagesSetter()
rather than directly manipulating a message store, allowing for changes to be reactive across the UI. -
Cache Invalidation: Explicitly clears caches for context and code style information to ensure fresh data when the user continues.
-
State Management: Demonstrates how state (like current working directory) is reset when clearing a conversation.
-
Context Parameter: Receives a context object from the command system that allows it to interact with the component rendering the REPL.
-
Separate Function: The core functionality is extracted into a separate
clearConversation
function, which allows it to be used by other parts of the system if needed.
User Experience Considerations
From a UX perspective, the clear
command provides several benefits:
-
Context Space Management: Allows users to free up context space when they hit limitations with model context windows.
-
Fresh Start: Provides a clean slate for starting a new conversation without entirely restarting the CLI.
-
Visual Reset: The terminal clearing provides immediate visual feedback that the conversation has been reset.
The clear
command is simple but vital for long-running CLI sessions, where context management becomes crucial for effective use of the LLM.