๐Ÿ’ฌ commands/context/ ยท 01_dual_mode_command_strategy.md

Chapter 1: Dual-Mode Command Strategy

๐Ÿ“„ commands/context/01_dual_mode_command_strategy.md

Chapter 1: Dual-Mode Command Strategy

Welcome to the Context Project! In this first chapter, we are going to look at the "Front Door" of our application.

Imagine you have a video game console.

  1. When you hook it up to a TV, it shows beautiful graphics and menus.
  2. When you hook it up to a diagnostic computer for repairs, it just sends simple text logs.

The hardware is the same, but the output changes based on the environment.

The context command works exactly like this. It uses a Dual-Mode Command Strategy to decide whether to show you a pretty interactive table or a raw text report.

The Problem: One Command, Two Environments

We want a command called /context that analyzes how many tokens (AI memory units) we are using.

How do we create one command that handles both perfectly?

The Solution: The Smart Switch

We define the command twice in our entry file (index.ts), but we add a logic gate so only one version is active at a time.

1. The Interactive Definition

This definition handles the "Human User" scenario.

// From index.ts
export const context: Command = {
  name: 'context',
  description: 'Visualize current context usage',
  // Active ONLY if a human is using the terminal
  isEnabled: () => !getIsNonInteractiveSession(),
  type: 'local-jsx', // Use React/Graphics
  load: () => import('./context.js'),
}

Explanation:

2. The Headless Definition

This definition handles the "Script/Bot" scenario.

// From index.ts
export const contextNonInteractive: Command = {
  name: 'context', // Same name!
  // Active ONLY if this is a script/headless session
  isEnabled: () => getIsNonInteractiveSession(),
  type: 'local', // Simple text output
  load: () => import('./context-noninteractive.js'),
}

Explanation:

Internal Implementation: How the Switch Works

Let's visualize what happens when the application starts up and registers these commands.

sequenceDiagram participant User participant System as Command Registry participant Env as Environment Check participant TUI as context.tsx participant Text as context-noninteractive.ts User->>System: Types "/context" System->>Env: Is this an interactive session? alt Yes (Human User) Env-->>System: true System->>TUI: Launch Graphical Interface TUI-->>User: Shows Interactive Grid else No (Headless Script) Env-->>System: false System->>Text: Run Text Report Text-->>User: Returns Markdown String end

Lazy Loading for Performance

You might have noticed the load property in the code snippets above:

load: () => import('./context.js'),

This is a specific design choice called Lazy Loading.

This keeps the application fast and lightweight. It ensures that a script running in the background doesn't waste memory loading React components it will never use.

Summary

In this chapter, we learned:

  1. Dual-Mode Strategy: We can have two commands with the same name, as long as they never activate at the same time.
  2. Environment Detection: We use getIsNonInteractiveSession() to determine if a human or a computer is running the command.
  3. Efficiency: We separate the code into two files (context.tsx and context-noninteractive.ts) and only load the one we need.

Now that we understand how the system chooses which file to run, let's dive into the "Human Mode" to see how the graphical interface is built.

Next Chapter: Interactive Visualization (TUI)


Generated by Code IQ