|
| 1 | +# Claw Code (C++ Edition) 🚀 |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +A clean-room, highly-performant C++20 reimplementation of the **Claw Code AI Agent Harness**. |
| 8 | + |
| 9 | +Rather than being a simple chatbot that stops after a single reply, this is an **agentic loop architecture**. It integrates deeply with the Anthropic Messages API, sending explicit tool schemas to the model, and recursively executing local tools (like Bash and File I/O) on behalf of the AI until a task is completed. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## ⚡ Features |
| 14 | + |
| 15 | +- **True Agentic Tool Loop:** Context-aware routing and recursive API calls. |
| 16 | +- **Interactive REPL & One-Shot:** Native slash commands (`/quit`, `/clear`, `/usage`) and one-shot prompt scripts. |
| 17 | +- **Built-in Tooling Layer:** |
| 18 | + - `BashTool`: Execute local shell scripts with configurable timeouts and output truncation guards. |
| 19 | + - `FileReadTool`: Read files up to 512KB to inspect codebases safely. |
| 20 | + - `FileWriteTool`: Write strings to files with automatic parent-directory creation. |
| 21 | +- **Native Static Binary:** Builds to a fast, statically linked binary. Zero python/npm dependencies. |
| 22 | +- **Context Compaction:** Auto-trims history past a certain turn limit to keep token window budgets low. |
| 23 | + |
| 24 | +## 🏗️ Architecture Design |
| 25 | + |
| 26 | +```mermaid |
| 27 | +graph TD |
| 28 | + User["User Input"] --> CLI["CLI Layer<br/>main.cpp"] |
| 29 | + CLI --> |"REPL or one-shot"| Session["Runtime Session<br/>session.cpp"] |
| 30 | + Session --> |"messages + tool defs"| API["API Client<br/>client.cpp"] |
| 31 | + API --> |"HTTP POST"| Anthropic["Anthropic API"] |
| 32 | + Anthropic --> |"stop=end_turn"| Session |
| 33 | + Anthropic --> |"stop=tool_use"| ToolLoop["Agentic Tool Loop"] |
| 34 | + ToolLoop --> Registry["Tool Registry"] |
| 35 | + Registry --> Bash["BashTool"] |
| 36 | + Registry --> FileRead["FileReadTool"] |
| 37 | + Registry --> FileWrite["FileWriteTool"] |
| 38 | + Bash --> |"result"| ToolLoop |
| 39 | + FileRead --> |"result"| ToolLoop |
| 40 | + FileWrite --> |"result"| ToolLoop |
| 41 | + ToolLoop --> |"tool_result message"| Session |
| 42 | + Session --> |"final text"| CLI |
| 43 | + CLI --> |"display"| User |
| 44 | +``` |
| 45 | + |
| 46 | +## 📁 Workspace Layout |
| 47 | + |
| 48 | +``` |
| 49 | +claw-cpp-public/ |
| 50 | +├── CMakeLists.txt # Modern CMake build instructions |
| 51 | +├── LICENSE # MIT License |
| 52 | +├── README.md # This file |
| 53 | +└── src/ |
| 54 | + ├── api/ |
| 55 | + │ ├── client.cpp/hpp # HTTPS logic via cpp-httplib |
| 56 | + │ └── types.hpp # Rich API content block modeling (Variant) |
| 57 | + ├── cli/ |
| 58 | + │ └── main.cpp # REPL CLI Interface & entrypoint |
| 59 | + ├── runtime/ |
| 60 | + │ └── session.cpp/hpp # Core agentic recursion loop |
| 61 | + └── tools/ |
| 62 | + ├── itool.hpp # Modular Plugin interface |
| 63 | + ├── tool_registry.hpp # Maps plugin names to logic |
| 64 | + ├── bash_tool.cpp/hpp # Safe shell execution |
| 65 | + ├── file_read_tool.cpp/hpp |
| 66 | + └── file_write_tool.cpp/hpp |
| 67 | +``` |
| 68 | + |
| 69 | +## 🚀 Getting Started |
| 70 | + |
| 71 | +### Prerequisites |
| 72 | + |
| 73 | +You need a C++20 compatible compiler and CMake. |
| 74 | +For Windows users, you can use Winget: |
| 75 | +```powershell |
| 76 | +winget install Kitware.CMake |
| 77 | +winget install Microsoft.VisualStudio.2022.BuildTools --override "--wait --quiet --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended" |
| 78 | +winget install ShiningLight.OpenSSL # Needed for HTTPS network calls |
| 79 | +``` |
| 80 | + |
| 81 | +### Build Instructions |
| 82 | + |
| 83 | +```bash |
| 84 | +mkdir build |
| 85 | +cd build |
| 86 | + |
| 87 | +# Configure CMake (it will auto-download cpp-httplib, nlohmann-json, fmt, and CLI11) |
| 88 | +cmake .. |
| 89 | + |
| 90 | +# Build the release binary |
| 91 | +cmake --build . --config Release |
| 92 | +``` |
| 93 | + |
| 94 | +### Usage |
| 95 | + |
| 96 | +Export your API Key to the environment: |
| 97 | +```powershell |
| 98 | +# Windows |
| 99 | +$env:ANTHROPIC_API_KEY="sk-ant-..." |
| 100 | +
|
| 101 | +# macOS/Linux |
| 102 | +export ANTHROPIC_API_KEY="sk-ant-..." |
| 103 | +``` |
| 104 | + |
| 105 | +Run the REPL: |
| 106 | +```bash |
| 107 | +./Release/claw-cpp |
| 108 | +``` |
| 109 | + |
| 110 | +Run a one-shot query: |
| 111 | +```bash |
| 112 | +./Release/claw-cpp prompt "List the files in this directory and tell me what they are." |
| 113 | +``` |
0 commit comments