这份文档真正覆盖什么
架构文档用来梳理主要子系统,让你看清提示词、运行时状态、工具、传输层和 UI 边界到底在哪里交汇。
怎么用这页
- 先看右侧目录,直接跳到你当前最关心的小节。
- 如果你只是来解决具体问题,优先读正文里的相关标题,再回站内对应 hub。
- 如果你要核对原始来源,可以直接打开 GitHub 原文链接。
架构文档用来梳理主要子系统,让你看清提示词、运行时状态、工具、传输层和 UI 边界到底在哪里交汇。
架构文档用来梳理主要子系统,让你看清提示词、运行时状态、工具、传输层和 UI 边界到底在哪里交汇。
This document provides an overview of the DeepSeek TUI architecture for developers and contributors.
Current boundary note (v0.8.6):
crates/tui is still the live end-user runtime for the TUI, runtime API, task manager, and tool execution loop.crates/tui/src/lsp/) is fully wired into the engine's post-tool-execution path (core/engine/lsp_hooks.rs), providing inline diagnostics after every edit_file/apply_patch/write_file.┌─────────────────────────────────────────────────────────────────┐
│ 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) │ │ │ │
│ └──────────┘ └──────────┘ └─────────┘ └────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────────────────────────────┐
│ Runtime API + Task Management │
│ ┌─────────────────────────────┐ ┌──────────────────────────┐ │
│ │ HTTP/SSE Runtime API │ │ Persistent Task Manager │ │
│ │ (runtime_api.rs) │ │ (task_manager.rs) │ │
│ └─────────────────────────────┘ └──────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│ │
▼ ▼
┌─────────────────────────────────────────────────────────────────┐
│ LLM Layer │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ LLM Client Abstraction (llm_client.rs) │ │
│ │ ┌─────────────────┐ ┌─────────────────────────────┐ │ │
│ │ │ DeepSeek Client │ │ Compatible Client (DeepSeek)│ │ │
│ │ │ (client.rs) │ │ (client.rs) │ │ │
│ │ └─────────────────┘ └─────────────────────────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
main.rs - CLI argument parsing (clap), configuration loading, entry point routingcore/ - Main engine componentsengine.rs - Engine state, operation handling, message processingengine/turn_loop.rs - Streaming turn loop and tool execution orchestrationengine/capacity_flow.rs - Capacity guardrail checkpoints and interventionssession.rs - Session state managementturn.rs - Turn-based conversation handlingevents.rs - Event system for UI updatesops.rs - Core operationsconfig.rs - Configuration loading, profiles, environment variablessettings.rs - Runtime settings managementcrates/tools - Shared tool invocation primitives, including tool result/error/capability types used by the TUI runtime.crates/agent - Model/provider registry (ModelRegistry) for resolving model IDs to provider endpoints.crates/app-server - HTTP/SSE + JSON-RPC app server transport for headless agent workflows.crates/config - Config loading, profiles, environment variable precedence, CLI runtime overrides.crates/core - Agent loop, session management, turn orchestration, capacity flow guardrails.crates/execpolicy - Approval/sandbox policy engine for tool execution decisions.crates/hooks - Lifecycle hooks (stdout, jsonl, webhook) for pre/post tool events.crates/mcp - MCP client + stdio server for Model Context Protocol tool servers.crates/protocol - Request/response framing and protocol types.crates/secrets - OS keyring integration for API key storage.crates/state - SQLite thread/session persistence layer.crates/tui-core - Event-driven TUI state machine scaffold.client.rs - HTTP client for DeepSeek's documented OpenAI-compatible Chat Completions APIllm_client.rs - Abstract LLM client trait with retry logicmodels.rs - Data structures for API requests/responsesDeepSeek exposes OpenAI-compatible endpoints. The CLI uses:
https://api.deepseek.com/v1/chat/completions - normal and streaming model turnshttps://api.deepseek.com/v1/models - live model discovery and health checkshttps://api.deepseek.com/v1 is accepted for OpenAI SDK compatibility, and https://api.deepseek.com/beta can be configured for beta-only features such as strict tool mode, chat prefix completion, and FIM completion. The public DeepSeek docs do not document a Responses API path for this workflow; the engine drives turns through Chat Completions.
tools/ - Built-in tool implementationsmod.rs - Tool registry and common typesshell.rs - Shell command executionfile.rs - File read/write operationstodo.rs - Checklist tools plus legacy todo aliasestasks.rs - Model-visible durable task, gate, background shell, and PR-attempt toolsgithub.rs - Read-only GitHub context and guarded comment/closure tools backed by ghautomation.rs - Model-visible scheduling tools over AutomationManagerplan.rs - Planning toolssubagent.rs - Sub-agent spawning (replaces the removed agent_swarm surface)spec.rs - Tool specificationsrlm.rs - Recursive Language Model (RLM) tool — sandboxed Python REPL with llm_query() helpersmcp.rs - Model Context Protocol client for external tool serversskills.rs - Plugin/skill loading and executionhooks.rs - Pre/post execution hooks with conditionstui/ - Terminal UI components (ratatui-based)app.rs - Application state and message handlingui.rs - Event handling, streaming state, and rendering logicapproval.rs - Tool approval dialogclipboard.rs - Clipboard handlingstreaming.rs - Streaming text collectorui.rs - Legacy/simple UI utilitieslsp/ - Post-edit diagnostics injection (#136)mod.rs - LspManager — lazy per-language transport pool + configclient.rs - StdioLspTransport — JSON-RPC over stdio with didOpen/didChange/publishDiagnosticsdiagnostics.rs - Diagnostic types, severity, and HTML-block rendererregistry.rs - Language detection and default server map (rust-analyzer, pyright, gopls, clangd, typescript-language-server)core/engine/lsp_hooks.rs — called after every successful editsandbox/ - macOS sandboxing supportmod.rs - Sandbox type definitionspolicy.rs - Sandbox policy configurationseatbelt.rs - macOS Seatbelt profile generationutils.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 serializationruntime_api.rs - HTTP/SSE runtime API (deepseek serve --http)runtime_threads.rs - Durable thread/turn/item store + replayable event timelinetask_manager.rs - Durable queue, worker pool, task timelines and artifactscore/engine.rsllm_client.rsclient.rstools/~/.deepseek/sessions/checkpoints/latest.json--resume/--continue (or Ctrl+R in TUI)~/.deepseek/sessions/checkpoints/offline_queue.json/queue ...) are persisted continuously so drafts and queued prompts survive restarts~/.deepseek/snapshots/<project_hash>/<worktree_hash>/.git; /restore N and revert_turn restore file state without changing conversation history or the user's .gittool_use content blockedit_file/apply_patch/write_file and LSP is enabled, the engine runs run_post_edit_lsp_hook() to collect diagnosticsflush_pending_lsp_diagnostics() injects any collected errors as a synthetic user message/task add ... or POST /v1/tasks)task_manager.rs persists task + queue entry under ~/.deepseek/tasksrunningruntime_threads.rs persists thread/turn/item records + monotonic event sequencecompleted|failed|canceled) is durable and queryable via TUI/APIModel-visible durable task tools are a surface over this same manager. They do not introduce a parallel work system: task_create enqueues normal tasks, checklist_* updates task-local progress, task_gate_run and completed task_shell_wait attach verification evidence, and automation runs enqueue ordinary durable tasks.
/v1/threads*)/v1/threads/{id}/turns)item.started|item.delta|item.completed)context_compaction item lifecycle/v1/threads/{id}/events?since_seq=<n>session_manager.rs, runtime_threads.rs, and task_manager.rs embed schema_version on persisted records.tools/tools/registry.rs~/.deepseek/mcp.jsonSKILL.md~/.deepseek/skills/Configure in ~/.deepseek/config.toml:
[[hooks]]
event = "tool_call_before"
command = "echo 'Running tool: $TOOL_NAME'"
crates/tui runtime today~/.deepseek/config.toml - Main configuration/etc/deepseek/managed_config.toml - Optional managed defaults layer (Unix)/etc/deepseek/requirements.toml - Optional allowed-policy constraints (Unix)~/.deepseek/mcp.json - MCP server configuration~/.deepseek/skills/ - User skills directory~/.deepseek/sessions/ - Session history~/.deepseek/sessions/checkpoints/ - Crash checkpoint + offline queue persistence~/.deepseek/snapshots/ - Side-git pre/post-turn workspace snapshots for /restore and revert_turn~/.deepseek/tasks/ - Background task records, queue, timelines, artifacts~/.deepseek/audit.log - Append-only audit events for credential + approval/elevation actions