Project Brain 使用教程

1. 创建虚拟环境

# 进入项目目录
cd C:\Users\silin\Desktop\code-momory-for-ohos\code-memory

# 用 uv 创建(推荐)
uv venv .venv --python 3.11

# 或用 Python 自带 venv
python -m venv .venv

2. 安装依赖

# 用 uv 安装(推荐,自动读取 uv.lock)
uv sync

# 或用 pip 安装
.venv\Scripts\pip install -e .

# 激活虚拟环境
.venv\Scripts\activate

安装后自带语义搜索(FAISS + fastembed),无需额外操作。

3. 验证安装

bot-brain --version
# 输出: bot-brain 0.7.0

4. 配置 config.yaml(可选)

在目标项目根目录创建 .botbrain/config.yaml:

# 先创建目录
mkdir C:\你的项目路径\.botbrain

完整配置示例:

# 项目配置
project:
  name: "my-project"
  languages: [python, rust]        # 留空则自动检测
  exclude:
    - "vendor/"
    - "*.generated.*"
    - "node_modules/"

# 搜索配置
search:
  strategy: hybrid                  # hybrid(默认,BM25+向量)| semantic | keyword
  index_body: true                  # 索引函数体,提升召回
  model_precision: int8             # int8(小/快)| full(大/准)

# 图谱 Profile
graph:
  profile: full                     # full(全部符号)| mini(仅公开 API + 文档)
  public_api:
    include_public_methods: true
    include_tests: false
    include_support_types: true
    include_module_dependencies: true

# 文档快照(可选,指定文档保存完整正文供离线读取)
documents:
  stored_content:
    include:
      - "README.md"
      - "docs/**"
    exclude:
      - "docs/archive/**"
    max_file_bytes: 2097152         # 单文件 2MB
    max_total_bytes: 33554432       # 总计 32MB
    on_limit: error                 # error | omit

# 文件监听
watcher:
  enabled: true
  debounce_seconds: 5
  observer_type: auto               # auto | polling | native | inotify
  reconcile_interval_seconds: 3600
  full_rebuild_threshold: 0.1

# 存储
storage:
  backend: auto                     # auto | json | sqlite

# Git 历史
history:
  max_commits: 10000

# Wiki 拉取(pull_wiki 工具 / pull-wiki 命令)
wiki:
  enabled: false              # 是否启用 wiki 拉取
  cookie_file: ""             # 登录 cookie,留空默认 ~/.botbrain/wiki_cookies.json
  output_dir: "docs/wiki"     # 下载目录(项目相对路径)

不创建 config.yaml 也可以用,所有选项都有默认值。

5. 构建知识图谱

# 基本构建(使用当前目录,默认 full + hybrid)
bot-brain build

# 指定项目路径
bot-brain --project C:\你的项目路径 build

# 构建 mini 图谱(只含公开 API + 文档)
bot-brain build --profile mini

# 构建时索引函数体(提升搜索召回)
bot-brain build --index-body

# 构建时启用语义搜索(如果 config 里没配)
bot-brain build --semantic

# 查看当前 profile 会保留什么(不实际构建)
bot-brain build --explain-profile

# 为指定文档保存完整正文快照
bot-brain build --store-doc-content "README.md" --store-doc-content "docs/**"

6. 查看项目信息

bot-brain info

输出示例:

Project: C:\my-project
Graph profile: full
Search strategy: hybrid
Storage backend: auto

Knowledge Graph:
  Total nodes: 1234
  Total edges: 5678
  class: 45
  concept: 12
  document: 8
  file: 120
  function: 340
  module: 30
  section: 89

7. 命令行查询

# 关键词搜索
bot-brain search "订单超时"
bot-brain search "timeout" --top-k 5

# 按名定位符号
bot-brain locate OrderService
bot-brain locate Poll --kind class

# 符号详情
bot-brain symbol fn_abc123 --include callers

# 调用图
bot-brain calls spawn --direction callers --depth 2

# 模块内容
bot-brain module src

# 影响分析
bot-brain impact calculatePrice

# 文档列表
bot-brain docs

# 查看能力(含语义搜索状态)
bot-brain capabilities

# 输出 JSON(适合脚本)
bot-brain search "config" --json

用 bot-brain --help 查看所有命令。

拉取华为内源 Wiki

# 拉取单个 wiki 页面(默认不含引用/子页面)并增量更新图谱
bot-brain pull-wiki --url "https://wiki.huawei.com/domains/12345/wiki/67890/ABCDEF123"

# 拉取整个看板下所有文档
bot-brain pull-wiki --url "https://wiki.huawei.com/domains/12345/wiki/67890" -all

# 指定输出目录、JSON 输出
bot-brain pull-wiki --url "..." --output docs/my-wiki --json

下载期间文件监听自动暂停(不触发逐文件重建),下载完成后恢复监听并对下载文件触发增量图谱更新。需有效的 wiki 登录 cookie(wiki.cookie_file 配置,默认 ~/.botbrain/wiki_cookies.json)。cookie 过期时可用 pip install "bot-brain[wiki]" 安装 playwright 后刷新登录态。

8. 启动 MCP 服务(stdio 模式)

# 默认 stdio(给 Claude Code / Cursor / OpenCode 用)
bot-brain serve

AI 工具集成

Claude Code — ~/.claude/claude_desktop_config.json:

{
  "mcpServers": {
    "bot-brain": {
      "command": "bot-brain",
      "args": ["serve"]
    }
  }
}

Cursor — .cursor/mcp.json:

{
  "mcpServers": {
    "bot-brain": {
      "command": "bot-brain",
      "args": ["serve"]
    }
  }
}

OpenCode — ~/.config/opencode/opencode.json:

{
  "mcp": {
    "bot-brain": {
      "type": "local",
      "command": ["bot-brain", "--project", "/path/to/project", "serve"],
      "enabled": true
    }
  }
}

9. 启动 MCP 服务(HTTP 模式)

# 本机访问(默认 127.0.0.1:8000)
bot-brain serve --transport streamable-http --stateless --json-response

# 指定端口
bot-brain serve --transport streamable-http --port 9000 --stateless --json-response

# 局域网访问(需显式确认风险 + 配置 allowed-host)
bot-brain serve --transport streamable-http \
  --host 0.0.0.0 \
  --insecure-allow-remote \
  --allowed-host 192.168.1.20:8000 \
  --stateless --json-response

客户端连接端点:http://127.0.0.1:8000/mcp 健康检查:GET http://127.0.0.1:8000/healthz

通过 HTTP 连接 AI 工具(Claude Code / Cursor):

{
  "mcpServers": {
    "bot-brain": {
      "type": "streamable-http",
      "url": "http://127.0.0.1:8000/mcp"
    }
  }
}

⚠️ HTTP 局域网模式无认证,可被局域网内任意主机访问源码和文档。推荐用 Caddy/Nginx 反向代理提供 TLS 和认证。

10. REPL 交互模式

bot-brain repl
Project Brain — C:\my-project
Loading graph... ready. Type a command, or 'help' to list, 'quit' to exit.

pb> search timeout --top-k 5
pb> locate Poll --kind class
pb> impact calculatePrice
pb> quit

11. 常用场景

场景 A:只分析公开 API

bot-brain build --profile mini
bot-brain module src --exported-only

场景 B:离线文档检索

# 构建时保存文档正文
bot-brain build --store-doc-content "README.md" --store-doc-content "docs/**"

# 搜索文档内容
bot-brain search "架构设计"

# 读取完整文档
bot-brain code doc_abc123

场景 C:给 AI Agent 用

# 1. 构建图谱
bot-brain build --semantic

# 2. 启动 MCP 服务
bot-brain serve

# 3. 在 AI 工具中配置 MCP(见第 8 步)
# AI Agent 即可调用 20 个工具查询代码知识图谱