跳转至

开发指南

开发环境设置

前置要求

  • Rust 工具链:rustup + cargo
  • 构建工具:
  • Linux: build-essential pkg-config
  • macOS: Xcode Command Line Tools
  • Windows: Visual Studio Build Tools

克隆项目

git clone https://github.com/zeroclaw-labs/zeroclaw.git
cd zeroclaw

编译

# 开发构建(最快)
cargo build

# Release 构建(优化)
cargo build --release

# 快速 Release 构建(需要更多内存)
cargo build --profile release-fast

安装

# 本地安装到 ~/.cargo/bin
cargo install --path .

# 使用锁定文件确保一致性
cargo install --path . --locked

项目结构

zeroclaw/
├── src/
│   ├── main.rs          # CLI 入口
│   ├── agent/           # 代理逻辑
│   ├── gateway/         # HTTP 网关
│   ├── security/        # 安全策略
│   ├── providers/       # AI 提供商
│   ├── channels/        # 通讯渠道
│   ├── tools/           # 工具实现
│   ├── memory/          # 内存系统
│   └── config/          # 配置管理
├── tests/              # 集成测试
└── examples/           # 示例代码

测试

运行测试

# 运行所有测试
cargo test

# 运行特定测试
cargo test test_agent

# 显示输出
cargo test -- --nocapture

# 运行 1017 个测试(完整测试套件)

编码规范检查

# 格式检查
cargo fmt --check

# Lint 检查(零警告)
cargo clippy -- -D warnings

# 自动修复
cargo clippy --fix

Pre-push Hook

# 启用 git hooks
git config core.hooksPath .githooks

扩展 ZeroClaw

添加新的 AI 提供商

src/providers/ 实现新的提供商:

// src/providers/myprovider.rs
use crate::providers::traits::Provider;
use async_trait::async_trait;

pub struct MyProvider {
    api_key: String,
}

#[async_trait]
impl Provider for MyProvider {
    async fn chat(&self, messages: &[Message]) -> Result<String, Error> {
        // 实现调用逻辑
    }
}

// 注册到工厂
// src/providers/mod.rs
pub fn create_provider(name: &str) -> Box<dyn Provider> {
    match name {
        "myprovider" => Box::new(MyProvider::new(config)),
        _ => panic!("Unknown provider")
    }
}

添加新的通讯渠道

src/channels/ 实现新的渠道:

// src/channels/mychannel.rs
use crate::channels::traits::Channel;

#[async_trait]
impl Channel for MyChannel {
    async fn send(&self, message: &str) -> Result<()> {
        // 发送消息
    }

    async fn listen(&self) -> Result<String> {
        // 接收消息
    }
}

添加新的工具

src/tools/ 实现新的工具:

// src/tools/mytool.rs
use crate::tools::traits::Tool;

pub struct MyTool;

impl Tool for MyTool {
    fn name(&self) -> &str {
        "mytool"
    }

    fn description(&self) -> &str {
        "我的工具描述"
    }

    async fn execute(&self, args: Value) -> Result<ToolResult, Error> {
        // 执行工具逻辑
    }
}

调试

启用日志

# 设置日志级别
export RUST_LOG=debug

# 启动时显示详细日志
zeroclaw agent --log-level debug

使用 LLDB/GDB

# 编译 with debug symbols
cargo build

# 启动调试器
lldb target/debug/zeroclaw

# 或使用 GDB
gdb target/debug/zeroclaw

贡献

  1. Fork 项目
  2. 创建功能分支
  3. 提交更改
  4. 推送到分支
  5. 创建 Pull Request

资源