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`