9.7 KiB
9.7 KiB
DeepSeek CLI Architecture
This document provides an overview of the DeepSeek CLI architecture for developers and contributors.
High-Level Overview
┌─────────────────────────────────────────────────────────────────┐
│ User Interface │
│ ┌─────────────────┐ ┌─────────────────┐ ┌────────────────┐ │
│ │ TUI (ratatui) │ │ One-shot Mode │ │ Config/CLI │ │
│ └────────┬────────┘ └────────┬────────┘ └────────┬───────┘ │
└───────────┼─────────────────────┼────────────────────┼──────────┘
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────────────────────────────┐
│ Core Engine │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Agent Loop (core/engine.rs) │ │
│ │ ┌─────────┐ ┌─────────────┐ ┌──────────────────────┐ │ │
│ │ │ Session │ │ Turn Mgmt │ │ Tool Orchestration │ │ │
│ │ └─────────┘ └─────────────┘ └──────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────────────────────────────┐
│ Tool & Extension Layer │
│ ┌──────────┐ ┌──────────┐ ┌─────────┐ ┌────────────────┐ │
│ │ Tools │ │ Skills │ │ Hooks │ │ MCP Servers │ │
│ │ (shell, │ │ (plugins)│ │ (pre/ │ │ (external) │ │
│ │ file) │ │ │ │ post) │ │ │ │
│ └──────────┘ └──────────┘ └─────────┘ └────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────────────────────────────┐
│ LLM Layer │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ LLM Client Abstraction (llm_client.rs) │ │
│ │ ┌─────────────────┐ ┌─────────────────────────────┐ │ │
│ │ │ DeepSeek Client │ │ Compatible Client (DeepSeek)│ │ │
│ │ │ (client.rs) │ │ (client.rs) │ │ │
│ │ └─────────────────┘ └─────────────────────────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
Module Organization
Entry Point
main.rs- CLI argument parsing (clap), configuration loading, entry point routing
Core Components
core/- Main engine componentsengine.rs- Agent loop, message processing, tool execution orchestrationsession.rs- Session state managementturn.rs- Turn-based conversation handlingevents.rs- Event system for UI updatesops.rs- Core operations
Configuration
config.rs- Configuration loading, profiles, environment variablessettings.rs- Runtime settings management
LLM Integration
client.rs- HTTP client for DeepSeek's OpenAI-compatible Responses API (with chat fallback)llm_client.rs- Abstract LLM client trait with retry logicmodels.rs- Data structures for API requests/responses
DeepSeek API Endpoints
DeepSeek exposes OpenAI-compatible endpoints. The CLI uses:
https://api.deepseek.com/v1/responses- preferred Responses APIhttps://api.deepseek.com/v1/chat/completions- fallback if Responses is unavailable
The engine uses handle_deepseek_turn() to drive the agent loop against the
Responses API (with automatic fallback if needed).
Tool System
tools/- Built-in tool implementationsmod.rs- Tool registry and common typesshell.rs- Shell command executionfile.rs- File read/write operationstodo.rs- Todo list managementplan.rs- Planning toolssubagent.rs- Sub-agent spawningspec.rs- Tool specifications
Extension Systems
mcp.rs- Model Context Protocol client for external tool serversskills.rs- Plugin/skill loading and executionhooks.rs- Pre/post execution hooks with conditions
User Interface
-
tui/- Terminal UI components (ratatui-based)app.rs- Application state and message handlingui.rs- Rendering logicapproval.rs- Tool approval dialogclipboard.rs- Clipboard handlingstreaming.rs- Streaming text collector
-
ui.rs- Legacy/simple UI utilities
Security
sandbox/- macOS sandboxing supportmod.rs- Sandbox type definitionspolicy.rs- Sandbox policy configurationseatbelt.rs- macOS Seatbelt profile generation
Utilities
utils.rs- Common utilitieslogging.rs- Logging infrastructurecompaction.rs- Context compaction for long conversationspricing.rs- Cost estimationprompts.rs- System prompt templatesproject_doc.rs- Project documentation handlingsession.rs- Session serialization
Data Flow
Interactive Session
- User input received in TUI
- Input processed by
core/engine.rs - Message sent to LLM via
llm_client.rs - Response streamed back, parsed in
client.rs - Tool calls extracted and executed via
tools/ - Hooks triggered before/after tool execution
- Results aggregated and sent back to LLM
- Final response rendered in TUI
Tool Execution
- LLM requests tool via
tool_usecontent block - Tool registry looks up handler
- Pre-execution hooks run
- Approval requested if needed (non-yolo mode)
- Tool executed (possibly sandboxed on macOS)
- Post-execution hooks run
- Result returned to agent loop
Extension Points
Adding a New Tool
- Create handler in
tools/ - Register in
tools/registry.rs - Add tool specification (name, description, input schema)
Adding an MCP Server
- Configure in
~/.deepseek/mcp.json - Server auto-discovered at startup
- Tools exposed to LLM automatically
Creating a Skill
- Create skill directory with
SKILL.md - Define skill prompt and optional scripts
- Place in
~/.deepseek/skills/
Adding Hooks
Configure in ~/.deepseek/config.toml:
[[hooks]]
event = "tool_call_before"
command = "echo 'Running tool: $TOOL_NAME'"
Key Design Decisions
- Streaming-first: All LLM responses stream for responsiveness
- Tool safety: Non-yolo mode requires approval for destructive operations
- Extensibility: MCP, skills, and hooks allow customization without code changes
- Cross-platform: Core works on Linux/macOS/Windows, sandboxing macOS-only
- Minimal dependencies: Careful dependency selection for build speed
Configuration Files
~/.deepseek/config.toml- Main configuration~/.deepseek/mcp.json- MCP server configuration~/.deepseek/skills/- User skills directory~/.deepseek/sessions/- Session history