Welcome to Chapter 3!
In the previous chapter, Chapter 2: Lazy Module Loading, we learned how to be efficient. We taught our CLI to keep the "heavy winter coat" (the code) in the attic until the user actually asks for it.
Now, imagine the user has asked for the command. We have fetched the file. But a file is just a list of instructions. Someone needs to actually read those instructions and start working.
That "Someone" is the Local JSX Execution Handler.
Think of your command file (agents.tsx) as a recipe.
Think of the CLI System as the Restaurant Manager.
The Manager (CLI) has handed the Recipe (File) to the kitchen. But a recipe cannot cook itself. You need a Head Chef to:
In our project, the Local JSX Execution Handler is that Head Chef.
call Function
In every command file we write, we must export a specific function named call. This function is the entry pointβthe moment the command actually starts running.
Its job is simple but critical: Receive Context (Ingredients) -> Return UI (The Dish).
Let's look at the file agents.tsx. This is the file we "lazy loaded" in the last chapter.
First, the chef needs to know what tools are available in the kitchen.
// agents.tsx
import * as React from 'react';
// We import the UI component (The Dish)
import { AgentsMenu } from '../../components/agents/AgentsMenu.js';
// We import helpers to get ingredients
import { getTools } from '../../tools.js';
Explanation: We import React (to build the UI), the specific menu component we want to show (AgentsMenu), and a helper to get tools.
Now we define the Head Chef function. In our code, this is named call.
// agents.tsx
export async function call(onDone, context) {
// Logic goes here...
}
Explanation:
export: Makes this function available to the CLI system.async: Tells the system "I might need a moment to fetch data, please wait."onDone: A special button we give the UI to say "I'm finished, close the app."context: The "Pantry." It contains all the data about the app (permissions, state, etc.).Before we can serve the food, we need to prep the ingredients.
// Inside the call function...
const appState = context.getAppState();
// We need to know what tools (functions) we are allowed to use
const permissionContext = appState.toolPermissionContext;
// Fetch the actual list of tools based on permissions
const tools = getTools(permissionContext);
Explanation:
context (Pantry) to get the current state of the application.getTools to prepare the list of actions the agent can perform.Finally, the chef serves the result. In our case, the result is a React Component.
// Inside the call function...
return (
<AgentsMenu
tools={tools}
onExit={onDone}
/>
);
Explanation:
<AgentsMenu />. This is JSX (JavaSript XML).tools we just gathered as a "prop" (property).onDone so the menu knows how to close itself when the user selects "Exit."Here is the complete code for our handler. It is short, clean, and does exactly one job: Coordination.
// agents.tsx
export async function call(onDone, context) {
const appState = context.getAppState();
const tools = getTools(appState.toolPermissionContext);
// Serve the UI
return <AgentsMenu tools={tools} onExit={onDone} />;
}
What happens in the split second when you run the command?
load() function (from Chapter 2).call export in the loaded file.call(), passing in the global context.<AgentsMenu />.You might ask: "Why didn't we just put all this logic in the Registry file from Chapter 1?"
Separation of concerns!
This handler allows us to do complex data fetching (like checking database connections or API keys) before we ever show a pixel on the screen.
You have successfully implemented the "Head Chef" logic!
You learned that the Local JSX Execution Handler:
call.context (the pantry) and onDone (the exit strategy).However, we glazed over a very important part. We kept saying "Context is the Pantry" and "get ingredients from Context." But what exactly is inside that Context, and how does it get there?
To understand how our Chef gets the ingredients, we need to explore Context-Aware State Injection.
Next Chapter: Context-Aware State Injection
Generated by Code IQ