Repository navigation
feat(gateway): P0-B 网关转发 + 路由故障转移 + 计量账本闭环 (v0.2.1) - #73
Merged
Merged
Conversation
- 网关端点:POST /v1/chat/completions(OpenAI 兼容)+ POST /anthropic/v1/messages + GET /api/models(市场页,需认证) - 路由故障转移(src/router.rs,architecture §4.2.1):初始随机 / 粘性复用 / 401/403/429/5xx 与网络错误冷却 5s / 上限 3 次切换 → 503 - 计量账本(src/billing.rs,§4.3/4.4):单价折算点数、消费者扣点、分享者 90% 分成、transactions consume/earn 双写、usage_records、keys.used 累计; settle 事务性回滚(幽灵 key 拒绝入账) - models 表启动种子(data/models.example.json + config.price_overrides upsert) - 测试 33 个全过(路由 6 / 计费 6 / 网关 7 + 既有 14),clippy + fmt 干净; 冒烟:无 token 401、placeholder 上游 503、/api/models 列表 Rant: 2026-08-18T09:55:57(P0-B 网关转发 + 路由故障转移 + 计量账本闭环)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
P0-B backend implementation (rant
2026-08-18T09:55:57— 网关转发 + 路由故障转移 + 计量账本闭环). Completes the core loop: request in → route to key → forward upstream → meter points → write ledger per architecture v0.2 §4.1/4.2/4.3/4.4. Non-streaming only (SSE deferred to P0-C).Changes
src/gateway.rs):POST /v1/chat/completions(OpenAI-compatible, Beareratk_API key auth)POST /anthropic/v1/messages(Anthropic-compatible)GET /api/models(marketplace list with key availability, auth required)prompt/completion_tokensvsinput/output_tokens)src/router.rs, §4.2.1): initial random pick from healthy keys (status='on', not in 5s cooldown), sticky reuse per user+model (in-memory HashMap), unhealthy marking on 401/403/429/5xx/network error, max 3 switches →503 {"error":{"message":"该模型暂无可用 key"}}; other 4xx passthrough without switchingsrc/billing.rs, §4.3/4.4): cost = tokens × per-M price (currency converted topoints.anchor_currency), points = cost ×points_per_unit; consumer balance deducted (pre-check → 402 when ≤0), sharer earns 90% (10% platform cut);transactionsconsume+earn pair,usage_records,keys.used += tokens; settle is transactional — rolls back on failure (phantom key refused)src/db.rs): startup upsert fromdata/models.example.json+config.price_overridesmacrosfeature enabled (debug_handler)Tests
cargo build+cargo clippy --all-targets -- -D warnings+cargo fmt --checkcleanGET /api/modelsreturns listNotes
Closes rant
2026-08-18T09:55:57.804810(P0-B).