# zhangzaibin/spagent architecture

> Architecture diagram of the GitHub repository zhangzaibin/spagent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zhangzaibin/spagent
- Repository: https://github.com/zhangzaibin/spagent

## Overview

SPAgent is a multimodal agent for grounded perception and spatial reasoning, with expert tools for 2D vision, 3D reconstruction, world modeling, and media generation. In the principal workflow, a user supplies images and a question; the agent uses a model to select tools, executes registered tools, renders their results, and returns an answer. The README also documents stateful memory, evaluation, and GRPO training with an offline Pi3X cache. Some tool families are represented at directory level because their individual implementations were not sampled.

Main entry points: [`spagent/core/spagent.py`](https://github.com/zhangzaibin/spagent/blob/main/spagent/core/spagent.py) (SPAgent), [`spagent/core/tool.py`](https://github.com/zhangzaibin/spagent/blob/main/spagent/core/tool.py) (Tool registry), [`spagent/tools/pi3x_tool.py`](https://github.com/zhangzaibin/spagent/blob/main/spagent/tools/pi3x_tool.py) (Pi3X tool), [`spagent/external_experts/Pi3/pi3x_client.py`](https://github.com/zhangzaibin/spagent/blob/main/spagent/external_experts/Pi3/pi3x_client.py) (Pi3X client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestration["Agent orchestration"]
  node_agent["SPAgent<br/>[spagent.py]"]
  node_model["Model interface<br/>[model.py]"]
  node_providers["Model providers"]
  node_registry["Tool registry<br/>[tool.py]"]
  node_memory["Conversation memory<br/>[memory.py]"]
  node_prompts["Prompt templates<br/>[prompts.py]"]
  node_render["Result rendering<br/>[render.py]"]
  node_contracts["Result contracts<br/>[tool_result.py]"]
end

subgraph group_experts["Expert capabilities"]
  node_perception["2D perception tools"]
  node_reconstruction["3D reconstruction tools"]
  node_pi3x["Pi3X tool<br/>[pi3x_tool.py]"]
  node_pi3x_client["Pi3X client<br/>[pi3x_client.py]"]
  node_world["World modeling"]
  node_generation["Media generation"]
end

subgraph group_evaluation["Evaluation"]
  node_eval["Benchmark evaluation"]
end

subgraph group_training["Training"]
  node_rl["GRPO rewards<br/>[plugin.py]"]
  node_cache[("Offline Pi3X cache")]
end

node_user(("User"))
node_result["Answer"]

node_user -->|"asks"| node_agent
node_agent -->|"queries"| node_model
node_agent -->|"uses prompts"| node_prompts
node_agent -->|"dispatches tools"| node_registry
node_agent -->|"records turns"| node_memory
node_agent -->|"renders results"| node_render
node_agent -->|"checks results"| node_contracts
node_providers -->|"implements"| node_model
node_registry -.->|"registers tools"| node_perception
node_registry -.->|"registers tools"| node_reconstruction
node_registry -.->|"registers tools"| node_world
node_registry -.->|"registers tools"| node_generation
node_pi3x -->|"calls client"| node_pi3x_client
node_pi3x_client -->|"returns reconstruction"| node_result
node_agent -->|"returns answer"| node_result
node_eval -.->|"evaluates"| node_agent
node_rl -.->|"trains policy"| node_agent
node_cache -->|"precomputes outputs"| node_pi3x

click node_agent "https://github.com/zhangzaibin/spagent/blob/main/spagent/core/spagent.py"
click node_model "https://github.com/zhangzaibin/spagent/blob/main/spagent/core/model.py"
click node_providers "https://github.com/zhangzaibin/spagent/tree/main/spagent/models"
click node_registry "https://github.com/zhangzaibin/spagent/blob/main/spagent/core/tool.py"
click node_memory "https://github.com/zhangzaibin/spagent/blob/main/spagent/core/memory.py"
click node_prompts "https://github.com/zhangzaibin/spagent/blob/main/spagent/core/prompts.py"
click node_render "https://github.com/zhangzaibin/spagent/blob/main/spagent/core/render.py"
click node_contracts "https://github.com/zhangzaibin/spagent/blob/main/spagent/core/tool_result.py"
click node_perception "https://github.com/zhangzaibin/spagent/tree/main/spagent/tools"
click node_reconstruction "https://github.com/zhangzaibin/spagent/tree/main/spagent/tools"
click node_pi3x "https://github.com/zhangzaibin/spagent/blob/main/spagent/tools/pi3x_tool.py"
click node_pi3x_client "https://github.com/zhangzaibin/spagent/blob/main/spagent/external_experts/Pi3/pi3x_client.py"
click node_world "https://github.com/zhangzaibin/spagent/tree/main/spagent/tools"
click node_generation "https://github.com/zhangzaibin/spagent/tree/main/spagent/tools"
click node_eval "https://github.com/zhangzaibin/spagent/blob/main/spagent/eval/spagent_vlmeval_model.py"
click node_rl "https://github.com/zhangzaibin/spagent/blob/main/plugin/plugin.py"
click node_cache "https://github.com/zhangzaibin/spagent/blob/main/train/precompute_pi3x_cache.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_agent,node_model,node_providers,node_registry,node_memory,node_prompts,node_render,node_contracts,node_user toneBlue
class node_perception,node_reconstruction,node_pi3x,node_pi3x_client,node_world,node_generation toneAmber
class node_eval toneMint
class node_rl,node_cache toneRose
class node_result toneIndigo
```

## Components

### Agent orchestration

- [SPAgent](https://github.com/zhangzaibin/spagent/blob/main/spagent/core/spagent.py): `spagent/core/spagent.py`
- [Model interface](https://github.com/zhangzaibin/spagent/blob/main/spagent/core/model.py): `spagent/core/model.py`
- [Model providers](https://github.com/zhangzaibin/spagent/tree/main/spagent/models): `spagent/models`
- [Tool registry](https://github.com/zhangzaibin/spagent/blob/main/spagent/core/tool.py): `spagent/core/tool.py`
- [Conversation memory](https://github.com/zhangzaibin/spagent/blob/main/spagent/core/memory.py): `spagent/core/memory.py`
- [Prompt templates](https://github.com/zhangzaibin/spagent/blob/main/spagent/core/prompts.py): `spagent/core/prompts.py`
- [Result rendering](https://github.com/zhangzaibin/spagent/blob/main/spagent/core/render.py): `spagent/core/render.py`
- [Result contracts](https://github.com/zhangzaibin/spagent/blob/main/spagent/core/tool_result.py): `spagent/core/tool_result.py`

### Expert capabilities

- [2D perception tools](https://github.com/zhangzaibin/spagent/tree/main/spagent/tools): `spagent/tools`
- [3D reconstruction tools](https://github.com/zhangzaibin/spagent/tree/main/spagent/tools): `spagent/tools`
- [Pi3X tool](https://github.com/zhangzaibin/spagent/blob/main/spagent/tools/pi3x_tool.py): `spagent/tools/pi3x_tool.py`
- [Pi3X client](https://github.com/zhangzaibin/spagent/blob/main/spagent/external_experts/Pi3/pi3x_client.py): `spagent/external_experts/Pi3/pi3x_client.py`
- [World modeling](https://github.com/zhangzaibin/spagent/tree/main/spagent/tools): `spagent/tools`
- [Media generation](https://github.com/zhangzaibin/spagent/tree/main/spagent/tools): `spagent/tools`

### Evaluation

- [Benchmark evaluation](https://github.com/zhangzaibin/spagent/blob/main/spagent/eval/spagent_vlmeval_model.py): `spagent/eval/spagent_vlmeval_model.py`

### Training

- [GRPO rewards](https://github.com/zhangzaibin/spagent/blob/main/plugin/plugin.py): `plugin/plugin.py`
- [Offline Pi3X cache](https://github.com/zhangzaibin/spagent/blob/main/train/precompute_pi3x_cache.py): `train/precompute_pi3x_cache.py`

### Other components

- **User**
- **Answer**

## Connections

- User → SPAgent: asks
- SPAgent → Model interface: queries (evidence: `spagent/core/spagent.py`)
- SPAgent → Prompt templates: uses prompts (evidence: `spagent/core/spagent.py`)
- SPAgent → Tool registry: dispatches tools (evidence: `spagent/core/spagent.py`)
- SPAgent → Conversation memory: records turns (evidence: `spagent/core/spagent.py`)
- SPAgent → Result rendering: renders results (evidence: `spagent/core/spagent.py`)
- SPAgent → Result contracts: checks results (evidence: `spagent/core/spagent.py`)
- Model providers → Model interface: implements (evidence: `spagent/models/qwen_vllm_model.py`)
- Tool registry → 2D perception tools: registers tools (evidence: `spagent/tools/pi3x_tool.py`)
- Tool registry → 3D reconstruction tools: registers tools (evidence: `spagent/tools/pi3x_tool.py`)
- Tool registry → World modeling: registers tools (evidence: `spagent/tools/pi3x_tool.py`)
- Tool registry → Media generation: registers tools (evidence: `spagent/tools/pi3x_tool.py`)
- Pi3X tool → Pi3X client: calls client (evidence: `spagent/tools/pi3x_tool.py`)
- Pi3X client → Answer: returns reconstruction (evidence: `spagent/external_experts/Pi3/pi3x_client.py`)
- SPAgent → Answer: returns answer (evidence: `spagent/core/spagent.py`)
- Benchmark evaluation → SPAgent: evaluates
- GRPO rewards → SPAgent: trains policy
- Offline Pi3X cache → Pi3X tool: precomputes outputs (evidence: `train/precompute_pi3x_cache.py`)
