Spaces:
Sleeping
Sleeping
File size: 18,571 Bytes
0065b67 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | # Claw-Web Developer Guide
**Complete documentation for the Claw-Web project β a web-based port of Claude Code (Rust CLI) to a full-stack TypeScript web application.**
**Total codebase:** ~32,700 lines of TypeScript/TSX across 126 source files.
---
## Table of Contents
1. [Architecture Overview](#architecture-overview)
2. [Project Structure](#project-structure)
3. [Tech Stack](#tech-stack)
4. [Getting Started](#getting-started)
5. [Server Runtime Modules](#server-runtime-modules)
6. [Client Components](#client-components)
7. [Buddy Companion Pet System](#buddy-companion-pet-system)
8. [Tools and Executor](#tools-and-executor)
9. [MCP Integration](#mcp-integration)
10. [Configuration System](#configuration-system)
11. [Permissions System](#permissions-system)
12. [Plugin System](#plugin-system)
13. [Deployment](#deployment)
14. [Parity Status with Original](#parity-status-with-original)
15. [Known Issues and TODOs](#known-issues-and-todos)
---
## Architecture Overview
Claw-Web is a **full-stack TypeScript application** that ports the original Claude Code Rust CLI to a web interface. It uses a client-server architecture where:
- **Client** (React + Vite + TailwindCSS) provides the chat UI, settings, file manager, terminal, and buddy companion
- **Server** (Express + tRPC + esbuild) runs the agent loop, tool executor, LLM API calls, and all runtime modules
- **Database** (MySQL/TiDB via Drizzle ORM) stores sessions, messages, settings, and user data
- **LLM** (HuggingFace Router API, OpenAI-compatible) powers the AI agent with tool calling
The agent loop in `server/runtime/agent.ts` is a direct port of the original `conversation.rs` Rust module, implementing the same iteration model with `Number.MAX_SAFE_INTEGER` max iterations (matching Rust's `usize::MAX`).
---
## Project Structure
```
claw-web/
βββ client/ # Frontend (React + Vite)
β βββ src/
β βββ buddy/ # Companion pet system (6 modules)
β β βββ types.ts # Type definitions (BuddyState, BuddyStats, etc.)
β β βββ sprites.ts # Sprite configs (6 stages x 10 moods)
β β βββ companion.ts # State management, XP, leveling, evolution
β β βββ prompt.ts # Personality, messages, achievements
β β βββ useBuddyNotification.tsx # React hook for buddy state
β β βββ index.ts # Barrel export
β βββ components/ # UI components
β β βββ BuddySprite.tsx # Companion pet UI (stats panel, interactions)
β β βββ ChatInput.tsx # Message input with slash commands
β β βββ FileManagerPanel.tsx # File browser
β β βββ MessageBubble.tsx # Chat message rendering
β β βββ PluginMarketplace.tsx # Plugin management UI
β β βββ SettingsPanel.tsx # Settings (Model, Parameters, Permissions, MCP, etc.)
β β βββ Sidebar.tsx # Session sidebar
β β βββ TaskManagerPanel.tsx # Task/todo management
β β βββ TerminalPanel.tsx # Embedded terminal
β β βββ ThinkingBlock.tsx # AI thinking display
β β βββ ToolCallCard.tsx # Tool call visualization
β β βββ ui/ # shadcn/ui components (70+ files)
β βββ contexts/ # React contexts
β βββ hooks/ # Custom hooks (useChat, useMobile, etc.)
β βββ pages/ # Page components (Home, NotFound, etc.)
β βββ lib/ # Utilities (trpc client, utils)
βββ server/ # Backend
β βββ _core/ # Framework core (tRPC, auth, LLM, etc.)
β β βββ llm.ts # LLM API client (OpenAI-compatible)
β β βββ oauth.ts # OAuth handling
β β βββ ... # Other core modules
β βββ runtime/ # Agent runtime (ported from Rust)
β β βββ agent.ts # Main agent loop (β conversation.rs)
β β βββ bootstrap.ts # Project bootstrap/onboarding
β β βββ chat-endpoint.ts # Chat SSE endpoint + slash commands
β β βββ compact.ts # Context compaction (527 lines)
β β βββ config.ts # Config loading + deep merge
β β βββ lsp-client.ts # LSP client for diagnostics
β β βββ mcp-client.ts # MCP server manager
β β βββ permissions.ts # Permission policy system
β β βββ plugins.ts # Plugin manager
β β βββ remote.ts # Remote/proxy configuration
β β βββ sandbox.ts # Container sandbox detection
β β βββ session.ts # Session serialization/validation
β β βββ system-prompt.ts # System prompt + TOOL_DEFINITIONS
β β βββ usage.ts # Token/cost tracking
β βββ tools/ # Tool implementations
β β βββ executor.ts # 30+ tool implementations (1600+ lines)
β β βββ file-ops.ts # File operation utilities
β βββ db.ts # Database schema (Drizzle)
β βββ routers.ts # tRPC routers
β βββ storage.ts # File storage
βββ shared/ # Shared types between client/server
β βββ claw-types.ts # Core shared types
β βββ types.ts # Additional types
βββ drizzle/ # Database migrations
βββ package.json
βββ vite.config.ts
βββ tsconfig.json
```
---
## Tech Stack
| Layer | Technology |
|-------|-----------|
| Frontend | React 18, Vite, TailwindCSS, shadcn/ui |
| Backend | Express, tRPC, esbuild |
| Database | MySQL/TiDB via Drizzle ORM |
| Auth | Manus OAuth (session-based) |
| LLM | HuggingFace Router API (OpenAI-compatible) |
| Deployment | HuggingFace Spaces (Docker) |
| Language | TypeScript throughout |
---
## Getting Started
### Prerequisites
- Node.js 22+
- pnpm
- MySQL/TiDB database
### Development
```bash
cd claw-web
pnpm install
pnpm dev # Starts both client (Vite) and server (esbuild)
```
### Build
```bash
pnpm build # Builds client (Vite) + server (esbuild)
```
### Environment Variables
| Variable | Description |
|----------|-------------|
| `DATABASE_URL` | MySQL/TiDB connection string |
| `HF_TOKEN` | HuggingFace API token for LLM |
| `SESSION_SECRET` | Session encryption key |
---
## Server Runtime Modules
### agent.ts β Main Agent Loop
**Original:** `conversation.rs` (Rust)
**Parity:** ~95%
The core agent loop that:
1. Builds system prompt with `TOOL_DEFINITIONS` + dynamically injected MCP tools
2. Calls LLM API with streaming
3. Processes tool calls from LLM response
4. Executes tools via `executor.ts`
5. Runs pre/post-tool hooks
6. Appends results to conversation
7. Loops until LLM stops calling tools or max iterations reached
Key constants:
- `MAX_ITERATIONS = Number.MAX_SAFE_INTEGER` (matches Rust `usize::MAX`)
- Tools are dynamically merged: `TOOL_DEFINITIONS + mcpTools`
### compact.ts β Context Compaction
**Original:** `compact.rs` (Rust)
**Parity:** ~99%
Full implementation (527 lines) of context window compaction:
- `shouldCompact()` β checks if conversation exceeds token threshold
- `compactSession()` β summarizes old messages, preserves recent context
- Triggered manually via `/compact` slash command
### config.ts β Configuration System
**Original:** `config.rs` (Rust)
**Parity:** ~90%
Loads configuration from multiple sources with deep merge:
- `.claw/settings.json` (project-level)
- `~/.claw/settings.json` (user-level)
- `deepMergeObjects()` β recursive merge for nested configs
- `extendUnique()` / `pushUnique()` β deduplicated array merging for hooks
- `mergeHooksConfig()` β proper PreToolUse/PostToolUse hook merging
### permissions.ts β Permission Policy
**Original:** `permissions.rs` (Rust)
**Parity:** ~85%
Permission modes (ordered by severity, matching Rust `derive(PartialOrd, Ord)`):
```typescript
ReadOnly = 0 // Can only read files
WorkspaceWrite = 1 // Can write within workspace
DangerFullAccess = 2 // Can execute dangerous operations
Prompt = 3 // Must prompt user for approval
Allow = 4 // Auto-approve everything
```
### mcp-client.ts β MCP Server Manager
**Original:** `mcp.rs` (Rust)
**Parity:** ~80%
Model Context Protocol integration:
- `McpServerManager` β manages multiple MCP server connections
- `connectAndListAllTools()` β discovers tools from all configured servers
- `normalizeNameForMcp()` β name normalization (preserves case, handles `claude.ai` prefix)
- `callTool()` β executes MCP tool calls via JSON-RPC
- Dynamic tool injection into agent's `TOOL_DEFINITIONS`
### sandbox.ts β Container Sandbox Detection
**Original:** `sandbox.rs` (Rust)
**Parity:** ~85%
Detects container environments:
- Docker (`.dockerenv`, cgroup)
- Podman (`/run/.containerenv`)
- Kubernetes (`KUBERNETES_SERVICE_HOST`)
- containerd (cgroup)
- libpod (cgroup)
### session.ts β Session Serialization
**Original:** Session handling in Rust
**Parity:** ~90%
Validated deserialization with `SessionError` class:
- `validateSessionData()` β validates all required fields
- `deserializeSession()` β safe JSON parsing with error handling
- `serializeSession()` β serialization with validation
### usage.ts β Token/Cost Tracking
**Original:** `usage.rs` (Rust)
**Parity:** ~85%
Tracks API usage:
- `UsageTracker` β accumulates token counts and costs
- `formatUsd()` β formats costs with 4 decimal places (matches Rust `format!("${:.4}")`)
- `summaryLines()` β generates usage summary
---
## Client Components
### Home.tsx β Main Page (~1020 lines)
The primary page component containing:
- Chat interface with message bubbles
- Sidebar with session management
- Settings panel
- File manager, terminal, task manager panels
- Buddy companion pet integration
- Plan mode with step visualization
- Ask-user dialog for agent questions
- Export/import session functionality
### SettingsPanel.tsx β Settings UI
Tabs: **Model, Parameters, Permissions, MCP, Costs, Memory**
Note: API tab was intentionally removed (built-in HuggingFace Router).
### BuddySprite.tsx β Companion Pet UI
Full companion pet with:
- Evolution-stage sprites (egg β baby β junior β senior β master β legendary)
- Interactive stats panel (XP, happiness, energy, hunger)
- Achievement system (17 achievements)
- Notification system (level-ups, evolutions, achievements)
- Feed and Pet interactions
- Rename functionality
---
## Buddy Companion Pet System
The buddy system is a **6-module subsystem** matching the original Claude Code architecture:
| Module | Lines | Description |
|--------|-------|-------------|
| `types.ts` | 95 | Full type system: BuddyState, BuddyStats, evolution stages, XP rewards |
| `sprites.ts` | 160 | Sprite configs for 6 evolution stages x 10 moods, colors, animations |
| `companion.ts` | 250 | State management, XP/leveling, evolution, persistence, time decay |
| `prompt.ts` | 210 | Personality messages, stage greetings, 17 achievements, stats formatting |
| `useBuddyNotification.tsx` | 140 | React hook: state loading, event handlers, mood calculation, decay timer |
| `BuddySprite.tsx` | 300 | Full UI: sprite, speech bubbles, stats panel, achievements, notifications |
### Evolution Stages
| Stage | Level Range | Description |
|-------|------------|-------------|
| Egg | 0-2 | Just hatched, minimal interaction |
| Baby | 3-5 | Learning, simple faces |
| Junior | 6-10 | Growing, more expressive |
| Senior | 11-20 | Experienced, accessories |
| Master | 21-50 | Expert, special effects |
| Legendary | 51+ | Transcended, full effects |
### XP Rewards
| Action | XP |
|--------|-----|
| Tool call | 5 |
| File created | 10 |
| File edited | 3 |
| Bug fixed | 25 |
| Test passed | 15 |
| Session completed | 20 |
| Streak day | 50 |
| First session | 100 |
---
## Tools and Executor
`server/tools/executor.ts` implements **30+ tools** (1600+ lines):
| Tool | Description |
|------|-------------|
| `bash` | Shell command execution with sandbox support |
| `read_file` | Read file contents with line ranges |
| `write_file` | Create/overwrite files |
| `edit_file` | Surgical file edits (find/replace) |
| `multi_edit` | Multiple edits in one call |
| `grep_search` | Regex search across files |
| `glob_search` | File path pattern matching |
| `list_dir` | Directory listing |
| `TodoRead` / `TodoWrite` | Task management |
| `NotebookRead` / `NotebookEdit` | Jupyter notebook support |
| `Agent` | Sub-agent spawning with presets |
| `WebSearch` / `WebFetch` | Web browsing |
| `REPL` | Python REPL execution |
| `mcp__*` | Dynamic MCP tool routing |
### MCP Tool Routing
When the LLM calls a tool with prefix `mcp__`, the executor:
1. Splits the name: `mcp__servername__toolname`
2. Finds the MCP server by name
3. Calls `McpServerManager.callTool(serverName, toolName, args)`
4. Returns the result to the LLM
---
## MCP Integration
### Configuration
MCP servers are configured in `.claw/settings.json`:
```json
{
"mcpServers": {
"my-server": {
"command": "node",
"args": ["./mcp-server.js"],
"env": {}
}
}
}
```
### Dynamic Tool Injection
At agent loop start:
1. `initializeMcpFromConfig(workDir)` loads MCP config
2. `connectAndListAllTools()` discovers tools from all servers
3. MCP tools are converted to OpenAI function calling format
4. Merged with static `TOOL_DEFINITIONS`
5. Full tool list sent to LLM
---
## Configuration System
### Config Loading Order
1. `.claw/settings.json` (project-level)
2. `~/.claw/settings.json` (user-level)
3. Database settings (per-session)
4. Environment variables
### Deep Merge
Uses `deepMergeObjects()` for recursive merging. Arrays in hooks use `extendUnique()` for deduplication (matching original Rust `extend_unique`).
---
## Permissions System
### Permission Modes
```
ReadOnly (0) < WorkspaceWrite (1) < DangerFullAccess (2) < Prompt (3) < Allow (4)
```
This ordering matches the original Rust `derive(PartialOrd, Ord)` enum ordering.
### Tool Permission Mapping
Each tool has a required permission level. The agent checks `modeGte(toolPermission, currentMode)` before executing.
---
## Plugin System
### Plugin Structure
```
.claw-plugins/
βββ my-plugin/
βββ .claw-plugin/
β βββ plugin.json # Manifest
βββ hooks/
βββ pre.sh # Pre-tool hook
βββ post.sh # Post-tool hook
```
### Hook Environment Variables
Pre-tool hooks receive:
- `HOOK_EVENT_NAME` β "PreToolUse"
- `HOOK_TOOL_NAME` β tool name
- `HOOK_TOOL_INPUT` β tool input (first 10KB)
- `HOOK_TOOL_INPUT_JSON` β full JSON input
- `HOOK_SESSION_ID` β session identifier
- `HOOK_WORKING_DIR` β working directory
Post-tool hooks additionally receive:
- `HOOK_TOOL_OUTPUT` β tool output (first 10KB)
- `HOOK_TOOL_RESULT_IS_ERROR` β "true" or "false"
---
## Deployment
### HuggingFace Spaces
The project deploys to HuggingFace Spaces via git push:
```bash
git remote add space https://huggingface.co/spaces/drt2221143/claw-web
git push space main
```
The Space uses a Docker container with Node.js 22.
### Live URL
https://drt2221143-claw-web.hf.space/
---
## Parity Status with Original
### Overall: ~92%
| Module | Parity | Notes |
|--------|--------|-------|
| agent.ts (conversation.rs) | 95% | Full loop, unlimited iterations, MCP injection |
| compact.ts | 99% | Full implementation |
| config.ts | 90% | Deep merge + extendUnique for hooks |
| permissions.ts | 85% | Correct ordering, authorize logic |
| mcp-client.ts | 80% | Full normalize, dynamic injection, callTool |
| session.ts | 90% | Validated deserialization |
| sandbox.ts | 85% | Full container detection |
| remote.ts | 85% | All 8 proxy env keys |
| usage.ts | 85% | formatUsd matches original |
| executor.ts | 85% | 30+ tools, MCP routing, hook payloads |
| lsp-client.ts | 85% | stderr drain, graceful shutdown |
| plugins.ts | 70% | Install/execute work, update/uninstall basic |
| bootstrap.ts | 100% | Full implementation |
| buddy system | 90% | 6/6 modules implemented |
### Not Ported (CLI-specific, not needed for web)
- `coordinator mode` β multi-agent CLI coordination
- `vim mode` β terminal vim keybindings
- `voice mode` β microphone input (CLI-specific)
- `auto updater` β self-update (not applicable to web)
- `magic docs` β auto-documentation (future enhancement)
- `prompt suggestion` β smart suggestions (future enhancement)
---
## Known Issues and TODOs
### High Priority
1. **Duplicate `/context` case** in `chat-endpoint.ts` (line 645 and 2070) β causes build warning
2. **Plugin update/uninstall** β basic stubs, need full implementation
3. **LSP diagnostics enrichment** β LSP works but doesn't enrich agent context
### Medium Priority
4. **Session history/resume** β no conversation resume across page reloads
5. **Cost tracker hooks** β real-time cost tracking not integrated
6. **Agent summary** β no session summary generation
7. **Output styles** β basic implementation, needs loadOutputStylesDir
### Low Priority
8. **OAuth PKCE/refresh** β web app uses session auth, not needed
9. **Sandbox OverlayFS** β mount isolation not enforced
10. **Keybindings** β minimal, original has 14 modules
---
## Contributing
### Code Style
- TypeScript strict mode
- ESLint + Prettier
- Functional components with hooks (React)
- tRPC for type-safe API calls
### Adding a New Tool
1. Add tool definition to `TOOL_DEFINITIONS` in `system-prompt.ts`
2. Add case handler in `executor.ts` switch statement
3. Set required permission level
4. Add pre/post hook support if needed
### Adding an MCP Server
1. Configure in `.claw/settings.json` under `mcpServers`
2. Tools are automatically discovered and injected
3. No code changes needed
---
*Last updated: April 2026*
*Maintained by: drt2221143*
|