In the previous chapter, Context Analysis Integration, we learned how to send data to our "Accountant" to get token counts.
But there is a catch.
If you hand the Accountant your raw chat history, they might give you the wrong number. Why? Because what you see in your terminal is not exactly what the AI sees.
To save money and memory, our system acts like a "Prep Chef." It chops, cleans, and squeezes your messages before sending them to the AI.
This chapter is about Model-View Transformation: the pipeline that turns the Human View (your screen) into the Model View (the API payload).
Imagine packing for a vacation.
If we calculated shipping costs based on the Human View (10 suitcases), we would think it's expensive. But the airline actually sees the Model View (1 suitcase).
To show the user accurate stats, we must replicate this "Space Bag" process exactly.
The transformation happens in a specific sequence. Let's visualize the assembly line.
toApiView)The first step is filtering out things that simply don't exist for the AI anymore.
If you have a chat history of 10,000 messages, the system might have "forgotten" the first 5,000 to save space (this is called the Compact Boundary). Even though you can scroll up and see them, the AI cannot.
We use a helper function called toApiView to simulate this.
// Inside context.tsx
function toApiView(messages: Message[]): Message[] {
// 1. Cut off messages that are behind the "Compact Boundary"
let view = getMessagesAfterCompactBoundary(messages);
// 2. (Optional) Apply Context Collapse
if (feature('CONTEXT_COLLAPSE')) {
view = projectView(view);
}
return view;
}
Explanation:
getMessagesAfterCompactBoundary: This acts like a pair of scissors. It cuts off the "forgotten" history so we don't count it.projectView: If "Context Collapse" is active, this turns 100 old messages into 1 summary message.microcompactMessages)Now that we have the correct list of messages, we need to squeeze the text itself.
The system uses a technique called Micro-Compaction. It removes unnecessary newlines and whitespace. A message that looks "airy" to a human might be a dense block of text to the machine.
// Inside context.tsx
export async function call(onDone, context) {
const { messages } = context;
// 1. Get the filtered view
const apiView = toApiView(messages);
// 2. Squeeze it!
const { messages: compactedMessages } =
await microcompactMessages(apiView);
// Now we are ready to analyze compactedMessages...
}
Explanation:
microcompactMessages: This is our "Vacuum Seal." It takes the apiView and returns compactedMessages.compactedMessages contains the exact characters (byte-for-byte) that will be sent to the AI.
You noticed projectView in Step 1. This is a powerful feature for long conversations.
Imagine a conversation with 500 lines.
The Model-View Transformation ensures that when you run /context, you see the token count for the Summary, not the original 400 lines. This ensures your dashboard matches reality.
Let's look at how this fits into the files we studied in previous chapters.
Whether we are in the TUI (Interactive Visualization (TUI)) or the Text Report (Headless Reporting (Markdown)), we perform this transformation before calling the analyzer.
/context.context.messages (The User View).toApiView (Removes hidden/forgotten items).microcompactMessages (Squeezes whitespace).analyzeContextUsage (Chapter 4).
Notice how the code is identical in both context.tsx and context-noninteractive.ts. This duplication is intentional to keep the "View" logic separate, but they share the same transformation logic.
In context.tsx (Visual Mode):
const apiView = toApiView(messages);
const { messages: compactedMessages } = await microcompactMessages(apiView);
const data = await analyzeContextUsage(compactedMessages, ...);
// -> Draw Graphics
In context-noninteractive.ts (Text Mode):
let apiView = getMessagesAfterCompactBoundary(messages);
// ... logic for collapse ...
const { messages: compactedMessages } = await microcompactMessages(apiView);
const data = await analyzeContextUsage(compactedMessages, ...);
// -> Print Markdown
In this final chapter, we closed the loop on how the /context command works.
toApiView to handle history boundaries and microcompactMessages to handle whitespace optimization.
Congratulations! You have explored the entire architecture of the context command.
You now understand how to build a production-grade terminal tool that is beautiful for humans, friendly for robots, and accurate for everyone.
Generated by Code IQ