Welcome to the first chapter of our tutorial! Today, we are building the foundation for the clear command.
Imagine walking into a restaurant. You sit down and look at the menu. You see "Spaghetti Carbonara" listed with a description.
Does the chef immediately start cooking that pasta the moment you walk in the door? No! That would waste food and make the kitchen chaotic. The chef waits until you actually order it.
In our CLI (Command Line Interface) app, we face a similar challenge:
/clear, /help, /image) right when the app starts, the app will be slow to open.This chapter explains how to set up that menu item using Command Definition & Routing.
To tell the CLI framework that our command exists, we need to create a definition file. This is like printing the text on the restaurant menu.
We need to define:
clear).reset).
Here is how we define the basic identity of the command in index.ts.
// File: index.ts
import type { Command } from '../../commands.js'
const clear = {
type: 'local',
name: 'clear',
description: 'Clear conversation history and free up context',
aliases: ['reset', 'new'],
supportsNonInteractive: false,
// ... loading logic comes next
}
Explanation:
type: 'local': Tells the system this runs on your machine, not a remote server.aliases: ['reset', 'new']: If the user types /reset, it routes to this same command.supportsNonInteractive: Set to false because clearing a session usually implies an interactive start to a new one.Now for the magic trick. We want to route the user to the actual code, but only on demand.
We use a specific function called load. This function uses a JavaScript feature called "dynamic import". It promises to go fetch the file ./clear.js only when the command is actually triggered.
// File: index.ts (continued)
const clear = {
// ... previous metadata ...
// This is the "Routing" part
load: () => import('./clear.js'),
} satisfies Command
export default clear
Explanation:
() => import('./clear.js'): This is the lazy loader. It acts like a pointer. It says, "If anyone asks for clear, go read clear.ts right then."satisfies Command: This is a TypeScript helper to ensure we didn't forget any required fields.
What happens when a user actually types /clear? Let's visualize the flow.
/clear.index.ts). It sees a match!load() function.clear.ts.
When the router routes the request, it lands in clear.ts. This file must export a call function. This is where the actual work begins.
// File: clear.ts
import type { LocalCommandCall } from '../../types/command.js'
import { clearConversation } from './conversation.js'
export const call: LocalCommandCall = async (_, context) => {
// This is the "heavy lifting" we delayed loading
await clearConversation(context)
return { type: 'text', value: '' }
}
Explanation:
export const call: This is the standard entry point the framework looks for after loading the file.clearConversation(context): This function performs the actual logic of wiping the slate clean. We will build this in the next chapter, Conversation Clearing Orchestrator.return: We return an empty text value to signal the command finished successfully without printing extra noise.Congratulations! You have successfully defined the "Menu Item" for the Clear command.
What we learned:
/reset.Now that the routing is set up, we need to implement the actual logic that happens when the command runs.
Next Step: Let's look at how we orchestrate the cleanup process.
Next Chapter: Conversation Clearing Orchestrator
Generated by Code IQ