Skip to main content

Interactive TUI

PraisonAI CLI provides a rich interactive terminal user interface (TUI) for seamless AI-assisted coding sessions. Built with prompt_toolkit and Rich, it offers command completion, history, and beautiful output formatting.

Overview

The Interactive TUI provides:
  • Command completion - Auto-complete for slash commands and files
  • History - Persistent command history with search
  • Syntax highlighting - Colorized input and output
  • Status display - Real-time session information
  • Keyboard shortcuts - Efficient navigation

Quick Start

# Start interactive mode
praisonai --interactive

# Or with a specific model
praisonai --interactive --model gpt-4o

Features

Command Completion

Type / and press Tab to see available commands:
>>> /he<Tab>
/help  /history

>>> @ma<Tab>
@main.py  @models/  @Makefile

History

Navigate through previous commands:
  • Up Arrow - Previous command
  • Down Arrow - Next command
  • Ctrl+R - Search history
>>> <Up Arrow>
>>> /cost  # Previous command appears

Multi-line Input

Enter multi-line prompts:
>>> Can you help me with this code:
... 
... def broken_function():
...     return x  # x is undefined
... 
... <Enter on empty line to submit>

Syntax Highlighting

Input is highlighted as you type:
>>> Fix the bug in `src/main.py` where the @config is not loaded
    ─────────────────────────────────────────────────────────────
    Highlighted: backticks, @mentions, keywords

Python API

Basic Usage

from praisonai.cli.features import InteractiveTUIHandler

# Create handler
handler = InteractiveTUIHandler(verbose=True)

# Define callbacks
def on_input(text):
    """Handle regular input."""
    return f"You said: {text}"

def on_command(cmd):
    """Handle slash commands."""
    if cmd == "/exit":
        return {"type": "exit"}
    return {"type": "command", "message": f"Executed: {cmd}"}

# Initialize and run
session = handler.initialize(
    on_input=on_input,
    on_command=on_command
)

handler.run()

Configuration

from praisonai.cli.features.interactive_tui import (
    InteractiveConfig,
    InteractiveTUIHandler
)

config = InteractiveConfig(
    prompt="🤖 >>> ",           # Custom prompt
    multiline=True,             # Enable multi-line input
    history_file="~/.praisonai_history",  # Persistent history
    max_history=1000,           # Max history entries
    enable_completions=True,    # Enable auto-complete
    enable_syntax_highlighting=True,  # Enable highlighting
    vi_mode=False,              # Use emacs keybindings
    auto_suggest=True,          # Show suggestions
    show_status_bar=True,       # Show status bar
    color_scheme="monokai"      # Color theme
)

handler = InteractiveTUIHandler()
session = handler.initialize(config=config)

Custom Completions

from praisonai.cli.features.interactive_tui import InteractiveSession

session = InteractiveSession()

# Add slash commands for completion
session.add_commands(["help", "exit", "cost", "model", "plan"])

# Add symbols from your codebase
session.add_symbols(["MyClass", "my_function", "CONFIG"])

# Refresh file completions
session.refresh_files(root=Path("/path/to/project"))

History Management

from praisonai.cli.features.interactive_tui import HistoryManager

# Create history manager
history = HistoryManager(
    history_file="~/.my_history",
    max_entries=500
)

# Add entries
history.add("first command")
history.add("second command")

# Navigate
prev = history.get_previous()  # "second command"
prev = history.get_previous()  # "first command"
next_cmd = history.get_next()  # "second command"

# Search
results = history.search("/help")  # Find commands starting with "/help"

# Clear
history.clear()

Status Display

from praisonai.cli.features.interactive_tui import StatusDisplay

display = StatusDisplay(show_status_bar=True)

# Set status items
display.set_status("model", "gpt-4o")
display.set_status("tokens", "1,234")
display.set_status("cost", "$0.05")

# Print formatted output
display.print_welcome(version="1.0.0")
display.print_response("Here's the solution...", title="AI Response")
display.print_error("Something went wrong")
display.print_info("Processing...")
display.print_success("Done!")

Keyboard Shortcuts

ShortcutAction
/ Navigate history
Ctrl+RSearch history
Ctrl+AMove to start of line
Ctrl+EMove to end of line
Ctrl+WDelete word backward

Editing

ShortcutAction
TabAuto-complete
Ctrl+CCancel current input
Ctrl+DExit (on empty line)
Ctrl+LClear screen

Multi-line

ShortcutAction
EnterNew line (in multi-line mode)
Enter on emptySubmit input
Ctrl+EnterSubmit immediately

VI Mode

Enable VI keybindings:
config = InteractiveConfig(vi_mode=True)
VI mode shortcuts:
  • Esc - Enter command mode
  • i - Insert mode
  • a - Append mode
  • dd - Delete line
  • / - Search

Customization

Custom Prompt

config = InteractiveConfig(
    prompt="🤖 praisonai> "
)

Dynamic Prompt

def get_prompt():
    branch = get_git_branch()
    return f"({branch}) >>> "

# Update prompt dynamically
session.config.prompt = get_prompt()

Custom Theme

from prompt_toolkit.styles import Style

custom_style = Style.from_dict({
    'prompt': '#00aa00 bold',
    'input': '#ffffff',
    'completion': 'bg:#333333 #ffffff',
})

# Apply to session
session._prompt_session.style = custom_style

Integration

With Slash Commands

from praisonai.cli.features import (
    InteractiveTUIHandler,
    SlashCommandHandler
)

# Create handlers
tui = InteractiveTUIHandler()
slash = SlashCommandHandler()

def on_command(cmd):
    if slash.is_command(cmd):
        return slash.execute(cmd)
    return None

session = tui.initialize(on_command=on_command)

With Cost Tracking

from praisonai.cli.features import (
    InteractiveTUIHandler,
    CostTrackerHandler
)

tui = InteractiveTUIHandler()
cost = CostTrackerHandler()
cost.initialize()

def on_input(text):
    # Process with AI...
    response = ai.chat(text)
    
    # Track costs
    cost.track_request("gpt-4o", input_tokens, output_tokens)
    
    # Update status
    tui._session.display.set_status("cost", f"${cost.get_cost():.4f}")
    
    return response

session = tui.initialize(on_input=on_input)

Fallback Mode

If prompt_toolkit is not available, a simple fallback is used:
# Without prompt_toolkit
>>> (Enter empty line to submit)
Hello, help me with my code

# Basic input still works, just without advanced features

Best Practices

  1. Use completions - Press Tab often for faster input
  2. Learn shortcuts - Ctrl+R for history search is powerful
  3. Multi-line for code - Use multi-line mode for code snippets
  4. Check status - Monitor costs and tokens in status bar

Troubleshooting

Completions Not Working

# Install prompt_toolkit
pip install prompt_toolkit

# Verify installation
python -c "import prompt_toolkit; print(prompt_toolkit.__version__)"

History Not Persisting

# Ensure history file path is writable
config = InteractiveConfig(
    history_file=os.path.expanduser("~/.praisonai_history")
)

Display Issues

# Set terminal type
export TERM=xterm-256color

# Or disable colors
config = InteractiveConfig(enable_syntax_highlighting=False)