Agentic Loop
理解 LLM + System Prompt + Context + Tools 的核心循环 — 与 Claude Code、Cursor 相同的设计模式。
一个功能完整的 CLI Coding Agent,具备以下能力:
🤖 智能对话 — 基于 OpenAI 兼容 API 的多轮对话,支持思考过程展示与自动折叠
📂 文件操作 — 读写、编辑、搜索、Glob 匹配,Agent 自主操作你的代码库
⚡ 命令执行 — Shell 命令执行,带权限控制和用户确认机制
🧠 上下文管理 — Token 统计、自动压缩、会话持久化,突破上下文窗口限制
🎨 交互式 UI — 语法高亮、Markdown 渲染、主题切换、命令补全、代码块复制
🧩 可扩展架构 — MCP 协议、自定义命令、Hooks 系统,面向真实场景设计
┌──────────────────────────────────────────────────────────┐
│ CLI Entry (yargs) │
│ ↓ │
│ Agent Loop ←──────────────────────────────┐ │
│ ├─ Build Context (messages + tools) │ │
│ ├─ Call LLM (streaming) │ │
│ ├─ Parse Response │ │
│ │ ├─ text → render to UI │ │
│ │ └─ tool_calls → Execution Pipeline │ │
│ │ ├─ 1. Discovery │ │
│ │ ├─ 2. Permission │ │
│ │ ├─ 3. Pre-Hooks │ │
│ │ ├─ 4. Confirmation (UI) │ │
│ │ ├─ 5. Execute │ │
│ │ ├─ 6. Post-Hooks │ │
│ │ └─ 7. Format │ │
│ └─ Inject Results ──────────────────────┘ │
│ │
│ Store (Zustand) ← → UI (Ink/React) ← → Theme Manager │
│ Context Manager ← → Token Counter ← → Compaction │
│ MCP Registry ← → External Tool Servers │
└──────────────────────────────────────────────────────────┘不是玩具项目。参考 Anthropic Claude Code 的真实架构设计,包括权限系统、Hooks、MCP 集成。
从 Hello World 到完整 Agent,每章都有可运行的代码。理解每一行为什么存在。
TypeScript 严格模式、Zod 运行时验证、完整错误处理、AbortController 中断机制。
npm install -g clawdcode 直接使用,不只是教学项目 — 它本身就是一个可用的 Coding Agent。
# 安装
npm install -g clawdcode
# 配置
export OPENAI_API_KEY=sk-...
# 使用
clawdcode "帮我分析一下这个项目的架构"