Skip to content

ZhaowTao/claw-code-study

 
 

Repository files navigation

Project Claw Code

项目概述

Project Claw Code 是一个基于 Claude Code 源代码的 Python 重写项目,旨在提供一个更快速、更安全的代理系统运行时。该项目在发布后仅2小时内就获得了超过50K的星标,成为历史上增长最快的GitHub仓库之一。

项目背景

在2026年3月31日,Claude Code 源代码被曝光,整个开发社区为之沸腾。项目创建者在压力下,使用 oh-my-codex (OmX) 工具在短时间内将核心功能从原始代码移植到 Python,并在日出前推送了代码。

项目目标

  • 提供 Claude Code 代理系统的干净室 Python 重写
  • 捕捉原始系统的架构模式,而不复制任何专有源代码
  • 开发一个更快、内存安全的运行时
  • 支持工具连接、任务编排和运行时上下文管理

项目架构

整体架构

Project Claw Code 采用模块化架构,主要分为以下几个部分:

  • 核心运行时:处理会话管理、工具执行和命令路由
  • 工具系统:提供各种工具的定义和执行机制
  • 命令系统:管理命令的定义和执行
  • 查询引擎:处理用户输入并生成响应
  • 会话管理:存储和加载会话数据
  • 权限系统:管理工具使用权限

模块结构

.
├── src/                                # Python 移植工作区
│   ├── __init__.py
│   ├── main.py                         # CLI 入口点
│   ├── port_manifest.py                # 工作区清单生成
│   ├── query_engine.py                 # 移植编排摘要层
│   ├── commands.py                     # 命令后备元数据
│   ├── tools.py                        # 工具后备元数据
│   ├── models.py                       # 共享数据类
│   ├── task.py                         # 任务级规划结构
│   ├── assistant/                      # 助手模块
│   ├── bootstrap/                      # 引导模块
│   ├── bridge/                         # 桥接模块
│   ├── buddy/                          # 伙伴模块
│   ├── cli/                            # 命令行界面模块
│   ├── components/                     # 组件模块
│   ├── constants/                      # 常量定义
│   ├── coordinator/                    # 协调器模块
│   ├── entrypoints/                    # 入口点模块
│   ├── hooks/                          # 钩子模块
│   ├── keybindings/                    # 键绑定模块
│   ├── memdir/                         # 内存目录模块
│   ├── migrations/                     # 迁移模块
│   ├── moreright/                      # 附加功能模块
│   ├── native_ts/                      # 原生 TypeScript 模块
│   ├── outputStyles/                   # 输出样式模块
│   ├── plugins/                        # 插件模块
│   ├── reference_data/                 # 参考数据
│   ├── remote/                         # 远程模块
│   ├── schemas/                        # 模式定义
│   ├── screens/                        # 屏幕模块
│   ├── server/                         # 服务器模块
│   ├── services/                       # 服务模块
│   ├── skills/                         # 技能模块
│   ├── state/                          # 状态管理
│   ├── types/                          # 类型定义
│   ├── upstreamproxy/                  # 上游代理
│   ├── utils/                          # 工具函数
│   ├── vim/                            # Vim 集成
│   └── voice/                          # 语音集成
├── rust/                               # Rust 实现(进行中)
│   ├── crates/                         # Rust  crate
│   └── Cargo.toml                      # Rust 依赖配置
├── tests/                              # Python 验证
└── assets/                             # 资源文件

快速开始

环境要求

  • Python 3.10+
  • 标准库(无需额外依赖)

基本命令

渲染 Python 移植摘要

python3 -m src.main summary

打印当前 Python 工作区清单

python3 -m src.main manifest

列出当前 Python 模块

python3 -m src.main subsystems --limit 16

运行验证

python3 -m unittest discover -s tests -v

对本地忽略的存档运行奇偶校验审计(当存在时)

python3 -m src.main parity-audit

检查镜像命令/工具清单

python3 -m src.main commands --limit 10
python3 -m src.main tools --limit 10

路由提示到合适的命令或工具

python3 -m src.main route "your prompt"

运行小型有状态回合循环

python3 -m src.main turn-loop "your prompt" --max-turns 3

核心功能

1. 查询引擎

查询引擎是项目的核心组件,负责处理用户输入、管理会话和生成响应。

from src.query_engine import QueryEnginePort

# 初始化查询引擎
engine = QueryEnginePort.from_workspace()

# 提交消息
result = engine.submit_message("review code")

# 持久化会话
session_path = engine.persist_session()

2. 运行时

运行时环境负责管理命令和工具的执行,以及会话的引导和路由。

from src.runtime import PortRuntime

# 初始化运行时
runtime = PortRuntime()

# 路由提示
matches = runtime.route_prompt("review code", limit=5)

# 引导会话
session = runtime.bootstrap_session("review code", limit=5)

# 运行回合循环
results = runtime.run_turn_loop("review code", max_turns=3)

3. 命令和工具系统

命令和工具系统负责管理命令和工具的定义和执行。

from src.commands import execute_command
from src.tools import execute_tool

# 执行命令
command_result = execute_command("review", "inspect security review")

# 执行工具
tool_result = execute_tool("MCPTool", "fetch resource list")

示例用法

基本示例

#!/usr/bin/env python3
"""
Project Claw Code 示例用法脚本
"""

from src.query_engine import QueryEnginePort
from src.runtime import PortRuntime

def main():
    print("=== Project Claw Code 示例用法 ===")
    
    # 1. 初始化查询引擎
    engine = QueryEnginePort.from_workspace()
    print(f"会话 ID: {engine.session_id}")
    
    # 2. 提交消息
    prompt = "review code for security issues"
    result = engine.submit_message(prompt)
    print(f"提示: {result.prompt}")
    print(f"输出: {result.output}")
    
    # 3. 运行路由
    runtime = PortRuntime()
    matches = runtime.route_prompt(prompt, limit=3)
    print("路由匹配结果:")
    for match in matches:
        print(f"- [{match.kind}] {match.name} (得分: {match.score}) — {match.source_hint}")
    
    # 4. 引导会话
    session = runtime.bootstrap_session(prompt, limit=3)
    print(f"会话路径: {session.persisted_session_path}")
    
    # 5. 运行回合循环
    results = runtime.run_turn_loop(prompt, limit=3, max_turns=2)
    for idx, result in enumerate(results, start=1):
        print(f"回合 {idx}:")
        print(f"- 提示: {result.prompt}")
        print(f"- 输出: {result.output}")
    
    # 6. 持久化会话
    session_path = engine.persist_session()
    print(f"会话已保存到: {session_path}")

if __name__ == "__main__":
    main()

测试

项目包含完整的测试套件,确保功能正常运行:

python3 -m unittest discover -s tests -v

技术亮点

  • 模块化设计:清晰的模块分离,便于维护和扩展
  • 会话管理:支持会话的持久化和加载
  • 工具和命令系统:灵活的工具和命令注册与执行机制
  • 查询引擎:强大的查询处理和响应生成
  • 权限系统:管理工具使用权限
  • Rust 实现:提供更安全、更高效的运行时(进行中)

开发工作流

项目使用 oh-my-codex (OmX) 进行 AI 辅助开发和编排:

  • $team 模式:用于协调并行审查和架构反馈
  • $ralph 模式:用于持续执行、验证和完成纪律
  • Codex 驱动的工作流:用于将主要 src/ 树转变为 Python 优先的移植工作区

社区与贡献

加入 instructkr Discord — 最好的韩语模型社区。来聊天关于 LLMs、 harness 工程、代理工作流等一切。

免责声明

此存储库不声称拥有原始 Claude Code 源材料的所有权。 此存储库不与 Anthropic 相关联、认可或维护。

About

The fastest repo in history to surpass 50K stars ⭐, reaching the milestone in just 2 hours after publication. Better Harness Tools that make real things done. Now writing in Rust using oh-my-codex.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Rust 89.3%
  • Python 10.7%