Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ PORT=4000
#DEEPSEEKER_MAX_UPSTREAM_ATTEMPTS=3
# DeepSeek web client version sent as x-client-version (B11); bump it when
# the web app moves instead of carrying one stale string for all tokens
#DEEPSEEKER_CLIENT_VERSION=2.7.1
#DEEPSEEKER_CLIENT_VERSION=2.8.0
# Custom browser User-Agent for PC profile
#DEEPSEEKER_BROWSER_USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36"
# Browser platform for PC profile ('Windows', 'Linux', 'macOS')
Expand All @@ -59,17 +59,17 @@ PORT=4000
# --- Token pool scheduler v2 (B3; optional; defaults shown) ---
# Seconds a 429'd token cools down before it automatically rejoins the pool
#DEEPSEEKER_RATE_LIMIT_COOLDOWN=60
# Soft per-token concurrency cap: tokens at the cap are only used when no
# under-cap token is available (spreads load, prevents stampedes)
#DEEPSEEKER_TOKEN_CONCURRENCY=1
# Quiet window call gap (seconds, [gap/2, gap] randomized quiet window after each turn)
#DEEPSEEKER_CALL_GAP_SECONDS=4.0

# --- Token lifecycle: quarantine (P0-3; optional; default shown) ---
# --- Token lifecycle: quarantine & identity (P0-3; optional; default shown) ---
# Consecutive 403s (with no successful turn in between) that take a token out
# of rotation until it is restored from the dashboard. 0 disables escalation,
# leaving every failure on the cooldown path above. A 401 always quarantines.
#DEEPSEEKER_QUARANTINE_AFTER_403=3
# Enforce unique device ID across tokens (regenerates a new UUID if any two tokens share the same device_id)
# Enabled by default (true). Set to false to disable automatic deduplication.
#DEEPSEEKER_DEDUPLICATE_DEVICE_IDS=true

# --- Backup WAF cookie tunables (optional; only needed if reverting to backup cookie generation) ---
# Where the WAF cookie file is stored (Docker sets this to the volume)
Expand Down Expand Up @@ -120,11 +120,6 @@ PORT=4000
# Optional fixed custom response string
#DEEPSEEKER_AUTO_MODE_RESPONSE=

# --- Fast Startup mode (small VPS optimization) ---
# If enabled, generates a single shared Shumei device ID for all accounts at startup
# instead of isolating Shumei IDs per account. Default is false (per-account isolation).
#DEEPSEEKER_FAST_START=false

# Run browser in non-headless (visible) mode during startup warmup / cookie generation
# for maximum anti-bot stealth (requires a display or xvfb). Default is false (runs headless).
#DEEPSEEKER_WARMUP_NON_HEADLESS=false
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ deeperseeker.db-shm
.python-version
.pytest_cache/
.ruff_cache
server.log
static/fonts/
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,38 @@ Copy `.env.example` to `.env` and edit your secret values. The table below is th
| `DEEPSEEKER_MAX_SUMMARY_TOKENS` | Budget for the model-generated handoff summary | `4096` |
| `DEEPSEEKER_PER_TOOL_RESULT_TOKENS` | Per-result tool-output cap (so one giant output can't eat the budget) | `2000` |
| `DEEPSEEKER_RATE_LIMIT_COOLDOWN` | Seconds a rate-limited token cools before it rejoins the pool automatically | `60` |
| `DEEPSEEKER_TOKEN_CONCURRENCY` | Soft per-token in-flight cap; at-cap tokens are used only when nothing else is free | `8` |
| `DEEPSEEKER_QUARANTINE_AFTER_403` | Consecutive 403s that take a token out of rotation until you restore it (`0` disables escalation) | `3` |
| `DEEPSEEKER_DEDUPLICATE_DEVICE_IDS` | Enforce unique device ID across tokens (regenerates a new UUID if any two tokens share the same device_id) | `true` |
| `DEEPSEEKER_MAX_UPSTREAM_ATTEMPTS` | Retries of one turn across the pool before the upstream error is surfaced to the client | `3` |
| `DEEPSEEKER_MAX_USER_MESSAGE_TOKENS` | Hard cap for the latest user message inside a prompt; above it the message is tail-truncated with a marker instead of failing upstream | `900000` |
| `DEEPSEEKER_PROMPT_OVERHEAD_TOKENS` | Flat overhead added to the rollover estimate for tool schemas, system expansion and summaries the raw history does not show | `4096` |
| `DEEPSEEKER_FAST_START` | Shared Shumei ID on startup across accounts (fast boot) | `false` |
| `DEEPSEEKER_MAX_SIG_LOCKS` | Cap in-memory per-signature creation locks (memory-leak guard) | `4096` |
| `DEEPSEEKER_MAX_SESSIONS` | Cap stored session signatures + prune frequency (DB-growth guard) | `20000` |
| `DEEPSEEKER_PRUNE_EVERY` | Frequency of session signature pruning | `500` |
| `TRUSTED_PROXIES` | Comma-separated CIDR ranges of reverse proxies allowed to set `X-Forwarded-For` | unset |
| `DEEPSEEKER_UPSTREAM_BASE` | Primary upstream endpoint (override only for testing/proxying) | `https://chat.deepseek.com` |
| `DEEPSEEKER_UPSTREAM_FALLBACK` | Optional fallback upstream endpoint replayed on connect error/timeout/HTTP 5xx | unset |
| `DEEPSEEKER_MAX_CHAT_LOCKS` | Cap in-memory per-chat locks (session-collision guard, memory-leak guard) | `4096` |
| `DEEPSEEKER_CLIENT_VERSION` | DeepSeek web client version sent as `x-client-version` | `2.8.0` |
| `DEEPSEEKER_BROWSER_USER_AGENT` | Custom browser User-Agent for PC profile | `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36` |
| `DEEPSEEKER_BROWSER_PLATFORM` | Browser platform for PC profile (`Windows`, `Linux`, `macOS`) | `Windows` |
| `DEEPSEEKER_CALL_GAP_SECONDS` | Randomized quiet window call gap after each turn in seconds | `4.0` |
| `DEEPSEEKER_COOKIE_PATH` | Where the WAF cookie file is stored | `/app/data/aws_cookies_deepseek.json` |
| `DEEPSEEKER_WAF_COOKIE` | Optional manual WAF cookie string or JSON | unset |
| `DEEPSEEKER_COOKIE_HEADLESS` | Playwright headless mode | `true` |
| `DEEPSEEKER_COOKIE_ATTEMPTS` | Cookie regeneration attempts per cycle | `2` |
| `DEEPSEEKER_COOKIE_TIMEOUT` | Cookie regeneration per-cycle timeout in seconds | `120` |
| `DEEPSEEKER_COOKIE_COOLDOWN` | Cookie regeneration fail-fast cooldown in seconds | `20` |
| `DEEPSEEKER_TOOLS_SCHEMA_AS_IS` | Raw JSON schema tools format | `false` |
| `DEEPSEEKER_MIRROR_SYSTEM` | Mirror all system prompt messages into `[SYSTEM]` on continuation turns | `false` |
| `DEEPSEEKER_HIF_URL` | URL for fetching `x-hif-leim` token | `https://hif-leim.deepseek.com/query` |
| `DEEPSEEKER_HIF_DLIQ_URL` | URL for fetching `x-hif-dliq` token | `https://hif-dliq.deepseek.com/query` |
| `DEEPSEEKER_HIF_ENABLED` | Whether HIF token fetching is enabled | `true` |
| `DEEPSEEKER_AUTO_MODE_MOCK` | Intercept security monitor prompts from Claude Code auto mode and mock severity | `false` |
| `DEEPSEEKER_AUTO_MODE_SEVERITY` | Optional fixed severity (0-9) instead of random | unset |
| `DEEPSEEKER_AUTO_MODE_RESPONSE` | Optional fixed custom response string | unset |
| `DEEPSEEKER_WARMUP_NON_HEADLESS` | Run browser in non-headless (visible) mode during startup warmup / cookie generation for maximum stealth | `false` |
| `DEEPSEEKER_PERSIST_SESSIONS` | Persist sessions/session_map to SQLite (`false` keeps in RAM until restart) | `true` |
| `DEEPSEEKER_MODE` | `bridge` (reverse-engineered web session) or `official` (official API passthrough). Explicit only — nothing falls back on its own | `bridge` |
| `DEEPSEEKER_OFFICIAL_API_KEY` | Official API key used when mode is `official` (from platform.deepseek.com). Required in official mode | unset |
| `DEEPSEEKER_OFFICIAL_BASE` | Official API base URL override in official mode | `https://api.deepseek.com` |
Expand Down Expand Up @@ -263,15 +288,17 @@ docker run --rm -u root -v deeperseeker_deeperseeker_data:/app/data alpine \
- **Empty or blank replies, then an HTTP 502** — *Symptom:* the client gets `Empty response from DeepSeek (no parseable SSE content)` (or an upstream error event) and finally a `502` with an `error.request_id` (OpenAI surface) or a `[trace: …]` suffix in the message (Anthropic surface). *What the service does now:* the turn is retried across the pool — each attempt on a different token, jittered backoff between attempts — up to `DEEPSEEKER_MAX_UPSTREAM_ATTEMPTS` tries; when every attempt comes back empty, the designed 502 is returned with the correlation id stamped in the body and the `X-Request-ID` header, and nothing upstream-derived reaches the body unredacted. *What to check:* open the dashboard pool view and look for tokens stuck `RATE_LIMITED`/`QUARANTINED`; match the `request_id`/`trace` against the access log line to see which states the turn traversed.
- **"Context limit" errors in an agent harness** (Claude Code, Cline, coding harnesses) — *Symptom:* the harness reports a context-limit failure while plain chat clients keep working. *Root cause:* the upstream context belongs to the **chat session**, not the token — the old plain retry re-sent the same oversized context (a harness replays its whole history every turn) and every attempt failed the same way. *What the service does now:* (1) a context-class failure (upstream context wording, or the empty-SSE-with-context-hint error) makes the retry **force a summarize-and-rollover** — the fresh chat is seeded with a model-generated handoff summary, so the turn recovers with full continuity instead of dying or amputating the history; (2) the latest user message is hard-capped (`DEEPSEEKER_MAX_USER_MESSAGE_TOKENS`, tail kept) so a first message above the ~975K upstream ceiling passes instead of failing on every attempt; (3) the rollover estimate counts tool schemas and prompt overhead (`DEEPSEEKER_PROMPT_OVERHEAD_TOKENS`) so the proactive rollover fires **before** the remembered session dies, not after.

## Pricing (per 1M tokens, as of 2026-09-06)
## Pricing (per 1M tokens, as of 2026-11-15)

Flat peak-hour rates for the single default model:

| Model Tier | Input Cost (Cache Miss) | Output Cost |
|---|---|---|
| **DeepSeek V4.1 Flash** (`v4.1flash`) | $0.44 | $1.32 |
| Model Tier | Input Cost (Cache Miss) | Input Cost (Cache Hit) | Output Cost |
|---|---|---|---|
| **DeepSeek V4.1 Flash** (`v4.1flash`) | $0.30 | $0.006 | $1.20 |

The `cost` reported in API responses uses these flat rates.
The `cost` reported in API responses uses these flat rates. A turn that
continues a chat this gateway already holds locally is billed as a cache hit;
new chats (first turn, rotation, context rollover) bill input at the miss rate.

## Project docs

Expand Down
40 changes: 33 additions & 7 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,38 @@ python3 app.py
| `DEEPSEEKER_MAX_SUMMARY_TOKENS` | 模型生成的交接摘要预算 | `4096` |
| `DEEPSEEKER_PER_TOOL_RESULT_TOKENS` | 单条工具输出的上限(防止一条巨型输出吃光预算) | `2000` |
| `DEEPSEEKER_RATE_LIMIT_COOLDOWN` | 被限流的 token 冷却多少秒后自动回到轮换池 | `60` |
| `DEEPSEEKER_TOKEN_CONCURRENCY` | 单 token 软并发上限;满载的 token 只在无其他空闲时使用 | `8` |
| `DEEPSEEKER_QUARANTINE_AFTER_403` | 连续多少次 403 会把 token 移出轮换,直到手动恢复(`0` 关闭升级) | `3` |
| `DEEPSEEKER_DEDUPLICATE_DEVICE_IDS` | 强制 token 间 device_id 唯一(若发现重复则强制生成新 UUID) | `true` |
| `DEEPSEEKER_MAX_UPSTREAM_ATTEMPTS` | 一轮对话在池内的重试次数,用尽后才把上游错误返回给客户端 | `3` |
| `DEEPSEEKER_MAX_USER_MESSAGE_TOKENS` | 提示内最新一条用户消息的硬上限;超过后按尾部截断并加标记,而不是让上游失败 | `900000` |
| `DEEPSEEKER_PROMPT_OVERHEAD_TOKENS` | 滚动估算时加上的固定开销,覆盖工具模式、系统扩写与原始历史中看不到的摘要 | `4096` |
| `DEEPSEEKER_FAST_START` | 启动时跨账号共享数美设备 ID(快速启动) | `false` |
| `DEEPSEEKER_MAX_SIG_LOCKS` | 内存中会话签名锁上限(防内存泄漏) | `4096` |
| `DEEPSEEKER_MAX_SESSIONS` | 存储会话签名上限及清理频率(防数据库膨胀) | `20000` |
| `DEEPSEEKER_PRUNE_EVERY` | 会话签名清理频率 | `500` |
| `TRUSTED_PROXIES` | 允许设置 `X-Forwarded-For` 的反向代理 CIDR 范围(逗号分隔) | 未设置 |
| `DEEPSEEKER_UPSTREAM_BASE` | 主上游端点(仅在测试/代理时覆盖) | `https://chat.deepseek.com` |
| `DEEPSEEKER_UPSTREAM_FALLBACK` | 备选上游端点(主上游连接错误/超时/HTTP 5xx 时重试) | 未设置 |
| `DEEPSEEKER_MAX_CHAT_LOCKS` | 内存中每会话锁上限(防会话冲突和内存泄漏) | `4096` |
| `DEEPSEEKER_CLIENT_VERSION` | 作为 `x-client-version` 发送的网页客户端版本 | `2.8.0` |
| `DEEPSEEKER_BROWSER_USER_AGENT` | PC 端自定义浏览器 User-Agent | `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36` |
| `DEEPSEEKER_BROWSER_PLATFORM` | PC 端浏览器平台(`Windows`、`Linux`、`macOS`) | `Windows` |
| `DEEPSEEKER_CALL_GAP_SECONDS` | 每轮对话后的随机静默间隔秒数 | `4.0` |
| `DEEPSEEKER_COOKIE_PATH` | WAF cookie 文件存储路径 | `/app/data/aws_cookies_deepseek.json` |
| `DEEPSEEKER_WAF_COOKIE` | 可选的手动 WAF cookie 字符串或 JSON | 未设置 |
| `DEEPSEEKER_COOKIE_HEADLESS` | Playwright 无头模式 | `true` |
| `DEEPSEEKER_COOKIE_ATTEMPTS` | 每次 cookie 重新生成尝试次数 | `2` |
| `DEEPSEEKER_COOKIE_TIMEOUT` | cookie 生成超时秒数 | `120` |
| `DEEPSEEKER_COOKIE_COOLDOWN` | cookie 生成快速失败冷却秒数 | `20` |
| `DEEPSEEKER_TOOLS_SCHEMA_AS_IS` | 原始 JSON schema 工具格式 | `false` |
| `DEEPSEEKER_MIRROR_SYSTEM` | 在后续轮次中将系统提示镜像到 `[SYSTEM]` | `false` |
| `DEEPSEEKER_HIF_URL` | 获取 `x-hif-leim` token 的 URL | `https://hif-leim.deepseek.com/query` |
| `DEEPSEEKER_HIF_DLIQ_URL` | 获取 `x-hif-dliq` token 的 URL | `https://hif-dliq.deepseek.com/query` |
| `DEEPSEEKER_HIF_ENABLED` | 是否启用 HIF token 获取 | `true` |
| `DEEPSEEKER_AUTO_MODE_MOCK` | 拦截 Claude Code auto 模式安全监控提示并模拟严重级别 | `false` |
| `DEEPSEEKER_AUTO_MODE_SEVERITY` | 可选的固定严重级别(0-9) | 未设置 |
| `DEEPSEEKER_AUTO_MODE_RESPONSE` | 可选的固定自定义响应文本 | 未设置 |
| `DEEPSEEKER_WARMUP_NON_HEADLESS` | 启动预热/采集时以有头(可见)模式运行浏览器,获得最强抗检测能力 | `false` |
| `DEEPSEEKER_PERSIST_SESSIONS` | 是否持久化会话到 SQLite(`false` 则保存在内存直到重启) | `true` |
| `DEEPSEEKER_MODE` | `bridge`(逆向网页会话)或 `official`(官方 API 直通)。必须显式设置——没有任何自动回退 | `bridge` |
| `DEEPSEEKER_OFFICIAL_API_KEY` | `official` 模式使用的官方 API 密钥(来自 platform.deepseek.com)。官方模式下必填 | 未设置 |
| `DEEPSEEKER_OFFICIAL_BASE` | 官方模式下官方 API 基址覆盖 | `https://api.deepseek.com` |
Expand Down Expand Up @@ -230,15 +255,16 @@ docker run --rm -u root -v deeperseeker_deeperseeker_data:/app/data alpine \
- **回复为空或空白,最后收到 HTTP 502** —— *症状:* 客户端收到 `Empty response from DeepSeek (no parseable SSE content)`(或上游错误事件),最终收到一个带 `error.request_id`(OpenAI 面)或消息尾部 `[trace: …]`(Anthropic 面)的 `502`。*服务现在的行为:* 该轮在池内重试——每次换一个 token,尝试间抖动退避——最多 `DEEPSEEKER_MAX_UPSTREAM_ATTEMPTS` 次;当所有尝试都返回空时,返回设计好的 502,相关 id 盖在响应体和 `X-Request-ID` 头里,上游来源的信息不会未脱敏地进入响应体。*要查什么:* 打开仪表盘池视图,找卡在 `RATE_LIMITED`/`QUARANTINED` 的 token;拿 `request_id`/`trace` 对照访问日志行,看该轮走过了哪些状态。
- **Agent 框架(Claude Code、Cline 等编程 harness)报 "Context limit" 错误** —— *症状:* harness 上报上下文超限失败,而普通聊天客户端一直正常。*根因:* 上游上下文属于**聊天会话**而不是 token——旧的朴素重试原样重发同一个超长上下文(harness 每轮都会重放全部历史),每次尝试都以同样方式失败。*服务现在的行为:*(1)上下文类失败(上游上下文措辞,或带上下文提示的空 SSE 错误)会让重试**强制走摘要滚动**——新会话用模型生成的交接摘要做种子,轮次带着完整连续性恢复,而不是死掉或截肢历史;(2)最新用户消息有硬上限(`DEEPSEEKER_MAX_USER_MESSAGE_TOKENS`,保留尾部),超过约 975K 上游上限的首条消息也能通过,而不是每次尝试都失败;(3)滚动估算把工具模式和提示开销计入(`DEEPSEEKER_PROMPT_OVERHEAD_TOKENS`),让主动滚动在记忆会话死掉**之前**触发,而不是之后。

## 价格(每 1M token,截至 2026-09-06)
## 价格(每 1M token,截至 2026-11-15)

唯一默认模型的统一高峰时段费率:

| 模型层 | 输入成本(缓存未命中) | 输出成本 |
|---|---|---|
| **DeepSeek V4.1 Flash**(`v4.1flash`) | $0.44 | $1.32 |
| 模型层 | 输入成本(缓存未命中) | 输入成本(缓存命中) | 输出成本 |
|---|---|---|---|
| **DeepSeek V4.1 Flash**(`v4.1flash`) | $0.30 | $0.006 | $1.20 |

API 响应里的 `cost` 就用这套统一费率。
API 响应里的 `cost` 就用这套统一费率。续接网关本地已有会话的轮次按缓存命中计费;
新建会话(首轮、轮换、上下文滚动)的输入按未命中费率计费。

## 项目文档

Expand Down
Loading