File size: 14,137 Bytes
8d3471e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | # DS2API Architecture & Project Layout
Language: [δΈζ](ARCHITECTURE.md) | [English](ARCHITECTURE.en.md)
> This file is the single architecture source for directory layout, module boundaries, and execution flow.
## 1. Top-level Layout (core directories)
> Notes: this lists the main business directories (excluding metadata/dependency dirs such as `.git/` and `webui/node_modules/`), with each folder annotated by purpose. Newly added directories should be verified from the code tree rather than treated as a per-file inventory here.
```text
ds2api/
βββ .github/ # GitHub collaboration and CI config
β βββ ISSUE_TEMPLATE/ # Issue templates
β βββ workflows/ # GitHub Actions workflows
βββ api/ # Serverless entrypoints (Vercel Go/Node)
βββ app/ # Application-level handler assembly
βββ artifacts/ # Debug artifacts (raw-stream-sim, stream-debug, etc.)
βββ cmd/ # Executable entrypoints
β βββ ds2api/ # Main service bootstrap
β βββ ds2api-tests/ # E2E testsuite CLI bootstrap
βββ docs/ # Project documentation
βββ internal/ # Core implementation (non-public packages)
β βββ account/ # Account pool, inflight slots, waiting queue
β βββ auth/ # Auth/JWT/credential resolution
β βββ chathistory/ # Server-side conversation history storage/query
β βββ claudeconv/ # Claude message conversion helpers
β βββ compat/ # Compatibility and regression helpers
β βββ assistantturn/ # Upstream output to canonical assistant turn / stream event semantics
β βββ completionruntime/ # Shared Go DeepSeek completion startup, non-stream collection, and retry
β βββ config/ # Config loading/validation/hot reload
β βββ deepseek/ # DeepSeek upstream client/protocol/transport
β β βββ client/ # Login/session/completion/upload/delete calls
β β βββ protocol/ # DeepSeek URLs, constants, skip path/pattern
β β βββ transport/ # DeepSeek transport details
β βββ devcapture/ # Dev capture and troubleshooting
β βββ format/ # Response formatting layer
β β βββ claude/ # Claude output formatting
β β βββ openai/ # OpenAI output formatting
β βββ httpapi/ # HTTP surfaces: OpenAI/Claude/Gemini/Admin
β β βββ admin/ # Admin API root assembly and resource packages
β β βββ claude/ # Claude HTTP protocol adapter
β β βββ gemini/ # Gemini HTTP protocol adapter
β β βββ openai/ # OpenAI HTTP surface
β β β βββ chat/ # Chat Completions execution entrypoint
β β β βββ responses/ # Responses API and response store
β β β βββ files/ # Files API and inline-file preprocessing
β β β βββ embeddings/ # Embeddings API
β β β βββ history/ # OpenAI context file handling
β β β βββ shared/ # OpenAI HTTP errors/models/tool formatting
β β βββ requestbody/ # HTTP body reading and UTF-8/JSON validation helpers
β βββ js/ # Node runtime related logic
β β βββ chat-stream/ # Node streaming bridge
β β βββ helpers/ # JS helper modules
β β β βββ stream-tool-sieve/ # JS implementation of tool sieve
β β βββ shared/ # Shared semantics between Go/Node
β βββ prompt/ # Prompt composition
β βββ promptcompat/ # API request -> DeepSeek web-chat plain-text compatibility
β βββ rawsample/ # Raw sample read/write and management
β βββ server/ # Router and middleware assembly
β β βββ data/ # Router/runtime helper data
β βββ sse/ # SSE parsing utilities
β βββ stream/ # Unified stream consumption engine
β βββ testsuite/ # Testsuite execution framework
β βββ textclean/ # Text cleanup
β βββ toolcall/ # Tool-call parsing and repair
β βββ toolstream/ # Go streaming tool-call anti-leak and delta detection
β βββ translatorcliproxy/ # Vercel/fallback/test protocol translation bridge
β βββ util/ # Shared utility helpers
β βββ version/ # Version query/compare
β βββ webui/ # WebUI static hosting logic
βββ plans/ # Stage plans and manual QA records
βββ pow/ # PoW standalone implementation + benchmarks
βββ scripts/ # Build/release helper scripts
βββ static/ # Build artifacts (admin static resources)
βββ tests/ # Test assets and scripts
β βββ compat/ # Compatibility fixtures + expected outputs
β β βββ expected/ # Expected output samples
β β βββ fixtures/ # Fixture inputs
β β βββ sse_chunks/ # SSE chunk fixtures
β β βββ toolcalls/ # Tool-call fixtures
β βββ node/ # Node unit tests
β βββ raw_stream_samples/ # Upstream raw SSE samples
β β βββ continue-thinking-snapshot-replay-20260405/ # Continue-thinking sample
β β βββ longtext-deepseek-v4-flash-20260429/ # Flash long-text/file-upload sample
β β βββ longtext-deepseek-v4-pro-20260429/ # Pro long-text/file-upload sample
β β βββ markdown-format-example-20260405/ # Markdown sample
β β βββ markdown-format-example-20260405-spacefix/ # Space-fix sample
β βββ scripts/ # Test entry scripts
β βββ tools/ # Testing helper tools
βββ webui/ # React admin console source
βββ public/ # Static assets
βββ src/ # Frontend source code
βββ app/ # Routing/state scaffolding
βββ components/ # Shared UI components
βββ features/ # Feature modules
β βββ account/ # Account management page
β βββ apiTester/ # API tester page
β βββ chatHistory/ # Server-side conversation history page
β βββ proxy/ # Proxy management page
β βββ settings/ # Settings page
β βββ vercel/ # Vercel sync page
βββ layout/ # Layout components
βββ locales/ # i18n strings
βββ utils/ # Frontend utilities
```
## 2. Primary Request Flow
```mermaid
flowchart LR
C[Client / SDK] --> R[internal/server/router.go]
subgraph HTTP[HTTP API surface]
OA[internal/httpapi/openai]
CHAT[openai/chat]
RESP[openai/responses]
FILES[openai/files + embeddings]
CA[internal/httpapi/claude]
GA[internal/httpapi/gemini]
AD[internal/httpapi/admin/*]
WEB[internal/webui static admin]
end
subgraph COMPAT[Prompt compatibility core]
PC[internal/promptcompat]
PROMPT[internal/prompt]
HIST[internal/httpapi/openai/history]
end
subgraph RUNTIME[Shared runtime]
AUTH[internal/auth]
POOL[internal/account queue + concurrency]
CR[internal/completionruntime]
TURN[internal/assistantturn]
STREAM[internal/stream + internal/sse]
TOOL[internal/toolcall + internal/toolstream]
FMT[internal/format/openai + claude]
DS[internal/deepseek/client]
POW[pow + internal/deepseek/protocol]
end
subgraph NODE[Vercel Node Runtime]
NCS[api/chat-stream.js]
JS[internal/js/chat-stream + stream-tool-sieve]
end
R --> OA --> CHAT
OA --> RESP
OA --> FILES
R --> CA
R --> GA
R --> AD
R --> WEB
R -.Vercel stream.-> NCS
CA --> PC
GA --> PC
CHAT --> PC
RESP --> PC
PC --> PROMPT
PC -.long history.-> HIST
PC --> AUTH
PC --> CR
NCS -.Go prepare/release.-> CHAT
NCS --> JS
JS --> TOOL
AUTH --> POOL
CHAT --> CR
RESP --> CR
CA --> CR
GA --> CR
CR --> DS
CR --> STREAM
CR --> TURN
STREAM --> TURN
STREAM --> TOOL
TURN --> FMT
POOL --> CR
DS --> POW
DS --> U[DeepSeek upstream]
```
## 3. Responsibilities in `internal/`
- `internal/server`: router tree + middlewares (health, protocol routes, Admin/WebUI).
- `internal/httpapi/openai/*`: OpenAI HTTP surface split into chat, responses, files, embeddings, history, and shared packages; chat/responses share the promptcompat, stream, and toolcall semantics.
- `internal/httpapi/{claude,gemini}`: protocol adapters that normalize into the same prompt compatibility semantics; normal direct paths must share DeepSeek session/PoW/completion execution through `completionruntime`, while `translatorcliproxy` is reserved for Vercel prepare/release, missing-backend fallback, and regression tests.
- `internal/httpapi/requestbody`: shared HTTP body reading, JSON pre-validation, and UTF-8 error helpers across protocol adapters.
- `internal/promptcompat`: compatibility core for turning OpenAI/Claude/Gemini requests into DeepSeek web-chat plain-text context.
- `internal/assistantturn`: Go output-side canonical semantics, converting DeepSeek SSE collection results and stream finalization state into assistant turns and centralizing thinking, tool call, citation, usage, stop/error behavior.
- `internal/completionruntime`: shared Go completion execution helpers for DeepSeek session/PoW/call startup, non-stream collection, and empty-output retry; streaming paths use it to start upstream requests, continue to use `internal/stream` for real-time consumption, and use `assistantturn` during finalization.
- `internal/translatorcliproxy`: bridge compatibility layer for Claude/Gemini and OpenAI shape translation; it is not the main business protocol conversion center.
- `internal/deepseek/{client,protocol,transport}`: upstream requests, sessions, PoW adaptation, protocol constants, and transport details.
- `internal/js/chat-stream` + `api/chat-stream.js`: Vercel Node streaming bridge; Go prepare/release owns auth, account lease, and completion payload assembly, while Node relays real-time SSE with Go-aligned finalization and tool sieve semantics.
- `internal/stream` + `internal/sse`: Go stream parsing and incremental assembly.
- `internal/toolcall` + `internal/toolstream`: DSML shell compatibility plus canonical XML tool-call parsing and anti-leak sieve; DSML is normalized back to XML at the entrypoint, and internal parsing remains XML-based.
- `internal/httpapi/admin/*`: Admin API root assembly plus auth/accounts/config/settings/proxies/rawsamples/vercel/history/devcapture/version resource packages.
- `internal/chathistory`: server-side conversation history persistence, pagination, detail lookup, and retention policy.
- `internal/config`: config loading/validation + runtime settings hot-reload.
- `internal/account`: managed account pool, inflight slots, waiting queue.
- `internal/textclean`: text cleanup helpers, e.g. stripping `[reference: N]` markers.
- `internal/claudeconv`: Claude API request to DeepSeek format conversion.
- `internal/compat`: compatibility regression tests using SSE fixtures to verify output consistency.
- `internal/rawsample`: upstream raw response capture, read/write, and management.
- `internal/devcapture`: developer debug capture, storing HTTP request/response for troubleshooting.
- `internal/util`: cross-package utilities including JSON writing, type conversion, token counting, thinking parsing, etc.
- `internal/version`: version query and comparison, supporting build-time injection and runtime resolution.
## 4. WebUI Runtime Relation
- `webui/` stores frontend source (Vite + React).
- Runtime serves static output from `static/admin`.
- On first local startup, if `static/admin` is missing, DS2API may auto-build it (Node.js required).
## 5. Documentation Split Strategy
- Onboarding & quick start: `README.MD` / `README.en.md`
- Architecture & layout: `docs/ARCHITECTURE*.md` (this file)
- API contracts: `API.md` / `API.en.md`
- Deployment/testing/contributing: `docs/DEPLOY*`, `docs/TESTING.md`, `docs/CONTRIBUTING*`
- Deep topics: `docs/toolcall-semantics.md`, `docs/DeepSeekSSEθ‘δΈΊη»ζθ―΄ζ-2026-04-05.md`
|