Skip to content

austinwalter/codepal

Repository files navigation

Codepal

An exploration of AI agent system design and architecture, built from scratch in TypeScript. This project serves as a hands-on study of how modern coding assistants work under the hood—from agentic loops and tool orchestration to streaming interfaces and context management.

Tech Stack: TypeScript, Bun, React (Ink), Anthropic Claude API

Areas Explored:

  • Agentic Architecture — ReAct (Reasoning + Acting) loop with multi-turn conversation, automatic summarization, and token management
  • Tool Orchestration — Extensible registry pattern for file operations, code search, and sandboxed shell execution
  • Terminal UI — Real-time streaming interface using React/Ink with concurrent state management
  • Context Management — Session persistence, context windowing, and conversation export
  • Observability — Debug logging infrastructure with UI panel, log levels, and performance timing

Features

  • Interactive Chat - Multi-turn conversations with context
  • Streaming Responses - See text as it's generated in real-time
  • Extended Thinking - Enable Claude's extended thinking for complex problems
  • File Operations - Read, write, and edit files with diff display
  • Code Search - Find files by pattern and search contents with regex
  • Shell Integration - Execute bash commands with progress indicators
  • Session Persistence - Save and resume conversations across sessions
  • Token Tracking - Monitor token usage and estimated costs
  • Rich UI - Color themes, syntax highlighting, tree views, and markdown rendering
  • Auto-Summarization - Automatically condense long conversations

Roadmap

See FEATURES.md for the full feature roadmap. Planned features include:

  • Task Tool - Spawn sub-agents for complex, multi-step tasks
  • TodoWrite - Built-in task tracking and progress display
  • Hooks System - Pre/post tool execution hooks
  • Skills & Plugins - Extensibility via plugins with commands, agents, and MCP servers
  • Multi-Provider - AWS Bedrock, Google Vertex AI, Azure OpenAI support

Installation

bun install

Quick Start

# Interactive mode
bun run start

# Development mode with watch
bun run dev

# With initial prompt
bun run src/index.tsx "explain this codebase"

CLI Options

Option Description
-p, --print Print response and exit (non-interactive)
--model <model> Model to use ('sonnet', 'opus', 'haiku', or full name)
--json Output response as JSON (for scripting)
--max-cost <usd> Stop when estimated cost exceeds limit
--dry-run Preview tool actions without executing
--context <file> Include file in context (repeatable)
--thinking/--no-thinking Enable extended thinking mode
--thinking-budget <tokens> Max tokens for thinking (default: 10000)
--resume <session_id> Resume a previous session by ID
--continue Continue the most recent session
--theme <theme> UI color theme (default, dark, light, minimal)
--system-prompt Custom system prompt
--append-system-prompt Append to default system prompt
--allowed-tools Tools to allow (comma/space-separated)
--disallowed-tools Tools to deny (comma/space-separated)
--max-turns Maximum agent turns
-d, --debug Enable debug logging to stderr
--log-file Write debug logs to ~/.codepal/debug.log
--verbose Verbose error output
-v, --version Show version

Commands

Command Description
doctor Check installation health
tools List available tools

Session Commands

Use these commands during an interactive session:

Command Description
/help [topic] Show help (topics: tools, commands)
/save [name] Save current session
/load <id> Load a saved session
/sessions List all saved sessions
/export [format] Export conversation (markdown, json)
/debug Toggle debug panel (requires --debug)
/reset Clear conversation history
exit or quit Exit the program

Configuration

Set your API key:

export ANTHROPIC_API_KEY=your_key_here

Optional environment variables:

Variable Default Description
CLAUDE_AGENT_MODEL claude-sonnet-4-5-20250929 Model to use
CLAUDE_AGENT_MAX_TOKENS 8096 Max tokens per response
CLAUDE_AGENT_MAX_ITERATIONS 20 Max agent loop iterations
CLAUDE_AGENT_BASH_TIMEOUT_MS 120000 Bash timeout (ms)
CLAUDE_AGENT_THEME default UI theme (default, dark, light, minimal)
CLAUDE_AGENT_ENABLE_THINKING false Enable extended thinking
CLAUDE_AGENT_THINKING_BUDGET 10000 Max thinking tokens
CLAUDE_AGENT_AUTO_SAVE_SESSIONS true Auto-save on exit
CLAUDE_AGENT_AUTO_SUMMARIZE true Auto-summarize long contexts
CLAUDE_AGENT_SUMMARIZE_THRESHOLD 50000 Token threshold for summarization
CLAUDE_AGENT_MAX_COST (none) Max cost limit in USD
CLAUDE_AGENT_DRY_RUN false Preview without executing
DEBUG (none) Enable debug mode (1, true, or codepal)
CODEPAL_FILE_LOGGING (none) Write logs to ~/.codepal/debug.log

Available Tools

Tool Description
Read Read file contents
Write Create or replace files
Edit Search and replace edits
Glob Find files by pattern
Grep Search contents with regex
Bash Execute shell commands

Debug Mode

Enable debug mode to troubleshoot issues or understand agent behavior:

# Enable debug logging to stderr
bun run start --debug

# Also write logs to file
bun run start --debug --log-file

# Or via environment variable
DEBUG=1 bun run start

When debug mode is enabled:

  • Press ` (backtick) to toggle the debug panel
  • Use /debug command to toggle the panel
  • Filter logs by level: d (debug), i (info), w (warn), e (error)
  • Press c to clear the log buffer
  • Logs are written to stderr and optionally to ~/.codepal/debug.log

Development

# Install dependencies
bun install

# Run in development mode
bun run dev

# Run tests
bun test

# Type check
bun run typecheck

Adding New Features

  1. Research - Check reference/ for implementation patterns
  2. Design - Consider how it integrates with existing features
  3. Implement - Follow existing code patterns in src/
  4. Test - Add tests with bun test
  5. Document - Update FEATURES.md and ARCHITECTURE.md if needed

Reference Materials

The reference/ directory contains analysis of Claude Code v2.1.6:

  • DEPENDENCIES.md - Full dependency list with versions
  • TOOL_EXECUTION.md - Tool execution architecture
  • data/plugins/ - Plugin system examples
  • data.json - Configuration structure

Documentation

Document Purpose
PLAN.md Project instructions loaded into system prompt
FEATURES.md Feature checklist and roadmap
ARCHITECTURE.md Implementation details and refactoring plans

PLAN.md

PLAN.md is a project-specific instructions file that gets automatically loaded into the system prompt when working in this directory. It defines:

  • Coding conventions and patterns
  • Testing requirements and commands
  • Project-specific rules
  • Important files and architecture decisions
  • Development phases and priorities

Requirements

  • Bun runtime
  • Anthropic API key

About

An exploration of AI agent system design and architecture

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors