coding-proxy 0.2.0a2__tar.gz → 0.2.1a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of coding-proxy might be problematic. Click here for more details.
- coding_proxy-0.2.1a1/.github/workflows/release.yml +326 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/CHANGELOG.md +6 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/PKG-INFO +1 -1
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/pyproject.toml +1 -1
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/config/config.default.yaml +18 -2
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/quota_guard.py +9 -2
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/server/app.py +2 -5
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/server/request_normalizer.py +2 -117
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/antigravity.py +4 -4
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_antigravity.py +13 -8
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_token_logger.py +18 -4
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/uv.lock +1 -1
- coding_proxy-0.2.0a2/.github/workflows/promote.yml +0 -176
- coding_proxy-0.2.0a2/.github/workflows/release.yml +0 -159
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/.github/workflows/ci.yml +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/.github/workflows/coverage.yml +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/.gitignore +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/AGENTS.md +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/CLAUDE.md +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/LICENSE +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/README.md +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/docs/ci-cd.md +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/docs/framework.md +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/docs/user-guide.md +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/docs/zh-CN/README.md +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/__main__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/auth/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/auth/providers/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/auth/providers/base.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/auth/providers/github.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/auth/providers/google.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/auth/runtime.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/auth/store.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/cli/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/cli/auth_commands.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/cli/banner.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/compat/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/compat/canonical.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/compat/session_store.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/config/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/config/auth_schema.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/config/loader.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/config/resiliency.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/config/routing.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/config/schema.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/config/server.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/config/vendors.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/convert/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/convert/anthropic_to_gemini.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/convert/anthropic_to_openai.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/convert/gemini_sse_adapter.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/convert/gemini_to_anthropic.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/convert/openai_to_anthropic.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/logging/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/logging/db.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/logging/stats.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/model/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/model/auth.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/model/compat.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/model/constants.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/model/pricing.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/model/token.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/model/vendor.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/pricing.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/circuit_breaker.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/error_classifier.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/executor.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/model_mapper.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/rate_limit.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/retry.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/router.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/session_manager.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/tier.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/usage_parser.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/routing/usage_recorder.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/server/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/server/factory.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/server/responses.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/server/routes.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/streaming/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/streaming/anthropic_compat.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/alibaba.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/anthropic.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/base.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/copilot.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/copilot_models.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/copilot_token_manager.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/copilot_urls.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/doubao.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/kimi.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/minimax.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/mixins.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/native_anthropic.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/token_manager.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/xiaomi.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/src/coding/proxy/vendors/zhipu.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/__init__.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_app_routes.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_auto_login.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_banner.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_circuit_breaker.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_cli_usage.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_compat.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_config_init.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_config_loader.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_convert_request.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_convert_response.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_convert_sse.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_copilot.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_copilot_convert_request.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_copilot_convert_response.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_copilot_models.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_copilot_urls.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_currency.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_error_classifier.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_mixins.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_model_auth.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_model_compat.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_model_constants.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_model_mapper.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_model_pricing.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_model_token.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_model_vendor.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_native_vendors.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_parse_usage.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_pricing.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_quota_guard.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_rate_limit.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_request_normalizer.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_router_chain.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_router_executor.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_runtime_reauth.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_schema.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_streaming_anthropic_compat.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_tier.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_tiers_config.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_time_range.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_token_manager.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_types.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_vendor_streaming.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_vendors.py +0 -0
- {coding_proxy-0.2.0a2 → coding_proxy-0.2.1a1}/tests/test_zhipu.py +0 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
# =============================================================================
|
|
2
|
+
# coding-proxy: PyPI Publishing Pipeline (4-Stage Unified Pipeline)
|
|
3
|
+
# =============================================================================
|
|
4
|
+
# Trigger: GitHub Release publication event (release.types: [published])
|
|
5
|
+
#
|
|
6
|
+
# Architecture: 4-Stage Serial Pipeline (prerelease only — 所有生产发布均走预发布)
|
|
7
|
+
#
|
|
8
|
+
# Stage 1 (build): 矩阵构建 3.12/3.13/3.14,上传 artifacts
|
|
9
|
+
# Stage 2 (publish-testpypi): 发布到 TestPyPI 供验证
|
|
10
|
+
# Stage 3 (publish-to-pypi): ⏸ Production Approval Gate (environment: pypi)
|
|
11
|
+
# 审批通过后自动:Promote Release + Publish to PyPI
|
|
12
|
+
# Stage 4 (update-changelog): 从 Release notes 生成 Changelog PR → master
|
|
13
|
+
#
|
|
14
|
+
# 设计决策 — Stage 3 合并审批与发布的理由:
|
|
15
|
+
# pypa/gh-action-pypi-publish@release/v1 始终优先尝试 OIDC Trusted Publishing。
|
|
16
|
+
# 若将审批与发布拆分为两个 job,则无法同时满足:
|
|
17
|
+
# - 单一人工审批(不能两个 job 都声明 environment: pypi 触发二次审批)
|
|
18
|
+
# - OIDC 需要 environment: pypi 声明才能匹配 PyPI Trusted Publisher 配置
|
|
19
|
+
# 因此将 Approval Gate 与实际发布操作合并为同一 job,审批通过后顺序执行。
|
|
20
|
+
#
|
|
21
|
+
# Pre-requisites — 需要在 GitHub Settings 一次性手动配置:
|
|
22
|
+
# 1. repo → Settings → Environments → "pypi"
|
|
23
|
+
# → Required reviewers: 添加审批人员(Production Approval Gate 所需)
|
|
24
|
+
# 2. repo → Settings → Actions → General → Workflow permissions
|
|
25
|
+
# → "Read and write permissions"(Stage 4 推分支所需)
|
|
26
|
+
# → 勾选 "Allow GitHub Actions to create and approve pull requests"
|
|
27
|
+
#
|
|
28
|
+
# Authentication:
|
|
29
|
+
# - TEST_PYPI_API_TOKEN: TestPyPI API Token(repository secret)
|
|
30
|
+
# - PYPI Trusted Publisher: 已配置 OIDC(无需 API Token,通过 environment: pypi 自动认证)
|
|
31
|
+
#
|
|
32
|
+
# References:
|
|
33
|
+
# [1] https://packaging.python.org/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/
|
|
34
|
+
# [2] https://github.com/pypa/gh-action-pypi-publish
|
|
35
|
+
# [3] https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment
|
|
36
|
+
# =============================================================================
|
|
37
|
+
|
|
38
|
+
name: Release / Publish Pipeline
|
|
39
|
+
|
|
40
|
+
on:
|
|
41
|
+
release:
|
|
42
|
+
types: [published]
|
|
43
|
+
|
|
44
|
+
permissions:
|
|
45
|
+
contents: read # 全局最低权限,各 job 按需 override
|
|
46
|
+
|
|
47
|
+
env:
|
|
48
|
+
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
|
|
49
|
+
|
|
50
|
+
concurrency:
|
|
51
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
52
|
+
cancel-in-progress: false # 发布流水线不可被中断
|
|
53
|
+
|
|
54
|
+
jobs:
|
|
55
|
+
# ===========================================================================
|
|
56
|
+
# Stage 1: BUILD -- 矩阵构建,产出 sdist + wheel artifacts
|
|
57
|
+
# ===========================================================================
|
|
58
|
+
build:
|
|
59
|
+
name: Build distributions (py${{ matrix.python-version }})
|
|
60
|
+
runs-on: ubuntu-latest
|
|
61
|
+
timeout-minutes: 10
|
|
62
|
+
strategy:
|
|
63
|
+
matrix:
|
|
64
|
+
python-version: ["3.12", "3.13", "3.14"]
|
|
65
|
+
|
|
66
|
+
steps:
|
|
67
|
+
- name: Checkout repository
|
|
68
|
+
uses: actions/checkout@v4
|
|
69
|
+
with:
|
|
70
|
+
persist-credentials: false
|
|
71
|
+
|
|
72
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
73
|
+
uses: actions/setup-python@v5
|
|
74
|
+
with:
|
|
75
|
+
python-version: ${{ matrix.python-version }}
|
|
76
|
+
|
|
77
|
+
- name: Set up uv
|
|
78
|
+
uses: astral-sh/setup-uv@v4
|
|
79
|
+
with:
|
|
80
|
+
enable-cache: true
|
|
81
|
+
|
|
82
|
+
- name: Install build dependencies
|
|
83
|
+
run: uv pip install --system build twine
|
|
84
|
+
|
|
85
|
+
- name: Build sdist and wheel
|
|
86
|
+
run: python -m build
|
|
87
|
+
|
|
88
|
+
- name: Check package metadata
|
|
89
|
+
run: twine check dist/*
|
|
90
|
+
|
|
91
|
+
- name: Upload build artifacts
|
|
92
|
+
uses: actions/upload-artifact@v4
|
|
93
|
+
with:
|
|
94
|
+
name: dist-py${{ matrix.python-version }}
|
|
95
|
+
path: dist/
|
|
96
|
+
retention-days: 14
|
|
97
|
+
|
|
98
|
+
# ===========================================================================
|
|
99
|
+
# Stage 2: PUBLISH TO TESTPYPI -- prerelease 路径专用
|
|
100
|
+
# ===========================================================================
|
|
101
|
+
publish-testpypi:
|
|
102
|
+
name: Publish to TestPyPI
|
|
103
|
+
runs-on: ubuntu-latest
|
|
104
|
+
needs: build
|
|
105
|
+
if: github.event.release.prerelease == true
|
|
106
|
+
timeout-minutes: 10
|
|
107
|
+
environment:
|
|
108
|
+
name: testpypi
|
|
109
|
+
url: https://test.pypi.org/p/coding-proxy
|
|
110
|
+
permissions:
|
|
111
|
+
contents: read
|
|
112
|
+
|
|
113
|
+
steps:
|
|
114
|
+
- name: Download build artifacts
|
|
115
|
+
uses: actions/download-artifact@v4
|
|
116
|
+
with:
|
|
117
|
+
pattern: dist-py*
|
|
118
|
+
path: dist/
|
|
119
|
+
merge-multiple: true
|
|
120
|
+
|
|
121
|
+
- name: List artifacts
|
|
122
|
+
run: ls -la dist/
|
|
123
|
+
|
|
124
|
+
- name: Publish to TestPyPI
|
|
125
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
126
|
+
with:
|
|
127
|
+
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
|
128
|
+
repository-url: https://test.pypi.org/legacy/
|
|
129
|
+
skip-existing: true
|
|
130
|
+
attestations: false # TestPyPI 不支持 attestations,必须显式禁用
|
|
131
|
+
verbose: true
|
|
132
|
+
|
|
133
|
+
# ===========================================================================
|
|
134
|
+
# Stage 3: PUBLISH TO PYPI -- Production Approval Gate + Promote + Publish
|
|
135
|
+
# ===========================================================================
|
|
136
|
+
# 设计决策:将 Approval Gate 与实际发布操作合并为同一 job。
|
|
137
|
+
#
|
|
138
|
+
# 原因:pypa/gh-action-pypi-publish@release/v1 始终优先尝试 OIDC Trusted Publishing,
|
|
139
|
+
# 需要 id-token: write + environment: pypi 同时满足才能通过 PyPI 认证。
|
|
140
|
+
# 若拆分为两个 job,则无法避免二次审批或 OIDC 认证失败。
|
|
141
|
+
#
|
|
142
|
+
# 工作流程:
|
|
143
|
+
# 1. GitHub Actions pre-job 机制检测到 environment: pypi → 挂起等待审批
|
|
144
|
+
# 2. 审批人点击 Approve → job 开始执行
|
|
145
|
+
# 3. 顺序执行:Promote Release → Download Artifacts → Publish to PyPI
|
|
146
|
+
# ===========================================================================
|
|
147
|
+
publish-to-pypi:
|
|
148
|
+
name: "⏸ Production Approval Gate → Publish to PyPI"
|
|
149
|
+
runs-on: ubuntu-latest
|
|
150
|
+
needs: publish-testpypi
|
|
151
|
+
if: github.event.release.prerelease == true
|
|
152
|
+
timeout-minutes: 1440 # 24 小时(含审批等待 + 发布执行)
|
|
153
|
+
environment:
|
|
154
|
+
name: pypi
|
|
155
|
+
url: https://pypi.org/p/coding-proxy
|
|
156
|
+
permissions:
|
|
157
|
+
contents: write # 更新 GitHub Release 元数据(prerelease=false, make_latest=true)
|
|
158
|
+
id-token: write # PyPI OIDC Trusted Publishing 所需
|
|
159
|
+
|
|
160
|
+
steps:
|
|
161
|
+
- name: Approval granted — proceeding to PyPI production
|
|
162
|
+
run: |
|
|
163
|
+
echo "✅ Production deployment approved"
|
|
164
|
+
echo "Tag: ${{ github.ref_name }}"
|
|
165
|
+
echo "Release: ${{ github.event.release.name }}"
|
|
166
|
+
|
|
167
|
+
- name: Promote GitHub Release to stable
|
|
168
|
+
uses: actions/github-script@v7
|
|
169
|
+
with:
|
|
170
|
+
script: |
|
|
171
|
+
const { owner, repo } = context.repo;
|
|
172
|
+
const releaseId = ${{ github.event.release.id }};
|
|
173
|
+
const tagName = '${{ github.ref_name }}';
|
|
174
|
+
|
|
175
|
+
await github.rest.repos.updateRelease({
|
|
176
|
+
owner,
|
|
177
|
+
repo,
|
|
178
|
+
release_id: releaseId,
|
|
179
|
+
prerelease: false,
|
|
180
|
+
make_latest: true,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
core.notice(`✅ Release #${releaseId} (${tagName}) promoted: prerelease=false, make_latest=true`);
|
|
184
|
+
|
|
185
|
+
- name: Download all build artifacts
|
|
186
|
+
uses: actions/download-artifact@v4
|
|
187
|
+
with:
|
|
188
|
+
pattern: dist-py*
|
|
189
|
+
path: dist/
|
|
190
|
+
merge-multiple: true
|
|
191
|
+
|
|
192
|
+
- name: List artifacts
|
|
193
|
+
run: ls -la dist/
|
|
194
|
+
|
|
195
|
+
- name: Publish to PyPI (OIDC Trusted Publishing)
|
|
196
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
197
|
+
with:
|
|
198
|
+
skip-existing: true
|
|
199
|
+
verbose: true
|
|
200
|
+
|
|
201
|
+
# ===========================================================================
|
|
202
|
+
# Stage 5: UPDATE CHANGELOG -- 自动生成 Changelog PR → master
|
|
203
|
+
# ===========================================================================
|
|
204
|
+
update-changelog:
|
|
205
|
+
name: Update Changelog
|
|
206
|
+
runs-on: ubuntu-latest
|
|
207
|
+
needs: publish-to-pypi
|
|
208
|
+
if: github.event.release.prerelease == true
|
|
209
|
+
timeout-minutes: 10
|
|
210
|
+
permissions:
|
|
211
|
+
contents: write # git push 新分支
|
|
212
|
+
pull-requests: write # 创建 PR
|
|
213
|
+
|
|
214
|
+
steps:
|
|
215
|
+
- name: Checkout repository
|
|
216
|
+
uses: actions/checkout@v4
|
|
217
|
+
with:
|
|
218
|
+
fetch-depth: 0
|
|
219
|
+
persist-credentials: true
|
|
220
|
+
|
|
221
|
+
- name: Extract stable version from prerelease tag
|
|
222
|
+
id: version
|
|
223
|
+
run: |
|
|
224
|
+
PRERELEASE_TAG="${{ github.ref_name }}"
|
|
225
|
+
# v0.2.0a1 → v0.2.0(移除预发布后缀 a*/b*/rc*)
|
|
226
|
+
STABLE_VERSION=$(echo "$PRERELEASE_TAG" | sed 's/\(v[0-9]*\.[0-9]*\.[0-9]*\).*/\1/')
|
|
227
|
+
echo "prerelease_tag=$PRERELEASE_TAG" >> "$GITHUB_OUTPUT"
|
|
228
|
+
echo "stable_version=$STABLE_VERSION" >> "$GITHUB_OUTPUT"
|
|
229
|
+
echo "branch_name=chore/changelog-$STABLE_VERSION" >> "$GITHUB_OUTPUT"
|
|
230
|
+
echo "Extracted: $PRERELEASE_TAG → $STABLE_VERSION"
|
|
231
|
+
|
|
232
|
+
- name: Check if Changelog entry already exists
|
|
233
|
+
id: check
|
|
234
|
+
run: |
|
|
235
|
+
STABLE="${{ steps.version.outputs.stable_version }}"
|
|
236
|
+
if grep -qF "[$STABLE]" CHANGELOG.md; then
|
|
237
|
+
echo "exists=true" >> "$GITHUB_OUTPUT"
|
|
238
|
+
echo "ℹ️ Changelog entry for $STABLE already exists, skipping PR creation"
|
|
239
|
+
else
|
|
240
|
+
echo "exists=false" >> "$GITHUB_OUTPUT"
|
|
241
|
+
echo "✅ No existing entry for $STABLE, will create PR"
|
|
242
|
+
fi
|
|
243
|
+
|
|
244
|
+
- name: Fetch release body and update CHANGELOG.md
|
|
245
|
+
if: steps.check.outputs.exists == 'false'
|
|
246
|
+
uses: actions/github-script@v7
|
|
247
|
+
with:
|
|
248
|
+
script: |
|
|
249
|
+
const fs = require('fs');
|
|
250
|
+
const { owner, repo } = context.repo;
|
|
251
|
+
|
|
252
|
+
// 通过 API 获取 Release body,避免 shell 特殊字符注入问题
|
|
253
|
+
const release = await github.rest.repos.getReleaseByTag({
|
|
254
|
+
owner,
|
|
255
|
+
repo,
|
|
256
|
+
tag: '${{ steps.version.outputs.prerelease_tag }}',
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
const stable = '${{ steps.version.outputs.stable_version }}';
|
|
260
|
+
const date = new Date().toISOString().slice(0, 10);
|
|
261
|
+
const releaseUrl = `https://github.com/${owner}/${repo}/releases/tag/${{ steps.version.outputs.prerelease_tag }}`;
|
|
262
|
+
const body = release.data.body || '';
|
|
263
|
+
|
|
264
|
+
// 构建新条目
|
|
265
|
+
const newEntry = `\n## [${stable}](${releaseUrl}) — ${date}\n\n${body}\n`;
|
|
266
|
+
|
|
267
|
+
// 读取 CHANGELOG.md,在 ## [Unreleased] 行后插入新条目
|
|
268
|
+
let content = fs.readFileSync('CHANGELOG.md', 'utf8');
|
|
269
|
+
const marker = '## [Unreleased]';
|
|
270
|
+
const idx = content.indexOf(marker);
|
|
271
|
+
if (idx === -1) {
|
|
272
|
+
core.setFailed('CHANGELOG.md 中未找到 ## [Unreleased] 标记,无法插入条目');
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const insertPos = idx + marker.length;
|
|
276
|
+
content = content.slice(0, insertPos) + newEntry + content.slice(insertPos);
|
|
277
|
+
fs.writeFileSync('CHANGELOG.md', content, 'utf8');
|
|
278
|
+
core.info(`✅ CHANGELOG.md updated with entry for ${stable}`);
|
|
279
|
+
|
|
280
|
+
- name: Create branch and commit
|
|
281
|
+
if: steps.check.outputs.exists == 'false'
|
|
282
|
+
run: |
|
|
283
|
+
BRANCH="${{ steps.version.outputs.branch_name }}"
|
|
284
|
+
STABLE="${{ steps.version.outputs.stable_version }}"
|
|
285
|
+
|
|
286
|
+
git config user.name "github-actions[bot]"
|
|
287
|
+
git config user.email "github-actions[bot]@users.noreply.github.com"
|
|
288
|
+
|
|
289
|
+
git checkout -b "$BRANCH"
|
|
290
|
+
git add CHANGELOG.md
|
|
291
|
+
git commit -m "docs(changelog): add entry for $STABLE"
|
|
292
|
+
git push origin "$BRANCH"
|
|
293
|
+
|
|
294
|
+
- name: Create Pull Request
|
|
295
|
+
if: steps.check.outputs.exists == 'false'
|
|
296
|
+
uses: actions/github-script@v7
|
|
297
|
+
with:
|
|
298
|
+
script: |
|
|
299
|
+
const { owner, repo } = context.repo;
|
|
300
|
+
const stable = '${{ steps.version.outputs.stable_version }}';
|
|
301
|
+
const branch = '${{ steps.version.outputs.branch_name }}';
|
|
302
|
+
const prereleaseTag = '${{ steps.version.outputs.prerelease_tag }}';
|
|
303
|
+
const runUrl = `https://github.com/${owner}/${repo}/actions/runs/${{ github.run_id }}`;
|
|
304
|
+
|
|
305
|
+
const pr = await github.rest.pulls.create({
|
|
306
|
+
owner,
|
|
307
|
+
repo,
|
|
308
|
+
title: `docs(changelog): 补充 ${stable} 发版说明`,
|
|
309
|
+
body: [
|
|
310
|
+
`## 摘要`,
|
|
311
|
+
``,
|
|
312
|
+
`自动生成的 PR,为 **${stable}** 正式版补充 CHANGELOG 条目。`,
|
|
313
|
+
``,
|
|
314
|
+
`- 来源:[\`${prereleaseTag}\`](https://github.com/${owner}/${repo}/releases/tag/${prereleaseTag}) Release notes`,
|
|
315
|
+
`- 目标分支:\`master\``,
|
|
316
|
+
``,
|
|
317
|
+
`请审阅 CHANGELOG 条目后合并。`,
|
|
318
|
+
``,
|
|
319
|
+
`> 🤖 由 [发布流水线](${runUrl}) 自动生成`,
|
|
320
|
+
].join('\n'),
|
|
321
|
+
head: branch,
|
|
322
|
+
base: 'master',
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
core.notice(`✅ PR created: ${pr.data.html_url}`);
|
|
326
|
+
|
|
@@ -4,6 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
- **请求规范化死代码清理**:移除 `request_normalizer` 中永远不可达的 `tool_result` 重定位逻辑(Phase 1 剥离已覆盖全部场景,Phase 2 重定位因方向错误无法启用);更新 docstring 使之与实际行为(剥离)一致;
|
|
8
|
+
|
|
9
|
+
### 🐛 Bug 修复
|
|
10
|
+
|
|
11
|
+
- **fix(antigravity)**: 修复 Google OAuth token 刷新后 scope 校验过严导致 403 `ACCESS_TOKEN_SCOPE_INSUFFICIENT` 的问题。将 `_acquire()` 中严格的 5-scope 全量校验降级为 warning 日志,与参考项目 Antigravity-Manager 行为对齐。Google OAuth2 规范允许 refresh_token 返回的 access_token 仅包含部分已授权 scope,此前因校验过于严格导致有效 token 被拒绝使用,触发熔断器。
|
|
12
|
+
|
|
7
13
|
## [v0.2.0](https://github.com/ThreeFish-AI/coding-proxy/releases/tag/v0.2.0) — 2026-04-09
|
|
8
14
|
|
|
9
15
|
> [!IMPORTANT]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: coding-proxy
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1a1
|
|
4
4
|
Summary: A High-Availability, Transparent, and Smart Multi-Vendor Proxy for Claude Code. Support Claude Plans, GitHub Copilot, Google Antigravity, ZAI/GLM.
|
|
5
5
|
Project-URL: Source Code, https://github.com/ThreeFish-AI/coding-proxy
|
|
6
6
|
Project-URL: User Guide, https://github.com/ThreeFish-AI/coding-proxy/blob/master/docs/user-guide.md
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "coding-proxy"
|
|
3
|
-
version = "0.2.
|
|
3
|
+
version = "0.2.1a1"
|
|
4
4
|
description = "A High-Availability, Transparent, and Smart Multi-Vendor Proxy for Claude Code. Support Claude Plans, GitHub Copilot, Google Antigravity, ZAI/GLM."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.12"
|
|
@@ -232,11 +232,15 @@ model_mapping:
|
|
|
232
232
|
# Google Antigravity 模型映射
|
|
233
233
|
- pattern: "claude-sonnet-.*"
|
|
234
234
|
vendors: ["antigravity"]
|
|
235
|
-
target: "
|
|
235
|
+
target: "Claude Sonnet 4.6"
|
|
236
236
|
is_regex: true
|
|
237
237
|
- pattern: "claude-opus-.*"
|
|
238
238
|
vendors: ["antigravity"]
|
|
239
|
-
target: "
|
|
239
|
+
target: "Claude Opus 4.6 Thinking"
|
|
240
|
+
is_regex: true
|
|
241
|
+
- pattern: "claude-haiku-.*"
|
|
242
|
+
vendors: ["antigravity"]
|
|
243
|
+
target: "Gemini 3 Flash"
|
|
240
244
|
is_regex: true
|
|
241
245
|
# 智谱 GLM 映射(官方原生 Anthropic 兼容端点)
|
|
242
246
|
# glm-5v-turbo 完整承载 Claude Code 满血能力(tool calling / thinking / 多模态)
|
|
@@ -330,6 +334,12 @@ pricing:
|
|
|
330
334
|
output_cost_per_mtok: $25.0
|
|
331
335
|
cache_write_cost_per_mtok: $6.25
|
|
332
336
|
cache_read_cost_per_mtok: $0.50
|
|
337
|
+
- vendor: anthropic
|
|
338
|
+
model: Claude Opus 4.6
|
|
339
|
+
input_cost_per_mtok: $5.0
|
|
340
|
+
output_cost_per_mtok: $25.0
|
|
341
|
+
cache_write_cost_per_mtok: $6.25
|
|
342
|
+
cache_read_cost_per_mtok: $0.50
|
|
333
343
|
- vendor: anthropic
|
|
334
344
|
model: claude-sonnet-4-6
|
|
335
345
|
input_cost_per_mtok: $3.0
|
|
@@ -342,6 +352,12 @@ pricing:
|
|
|
342
352
|
output_cost_per_mtok: $5
|
|
343
353
|
cache_write_cost_per_mtok: $1.25
|
|
344
354
|
cache_read_cost_per_mtok: $0.10
|
|
355
|
+
- vendor: anthropic
|
|
356
|
+
model: Claude Haiku 4.5
|
|
357
|
+
input_cost_per_mtok: $1
|
|
358
|
+
output_cost_per_mtok: $5
|
|
359
|
+
cache_write_cost_per_mtok: $1.25
|
|
360
|
+
cache_read_cost_per_mtok: $0.10
|
|
345
361
|
# ── GitHub Copilot(透传 Claude,参照 Anthropic 定价估算) ──
|
|
346
362
|
- vendor: copilot
|
|
347
363
|
model: claude-opus-4.6
|
|
@@ -129,7 +129,7 @@ class QuotaGuard:
|
|
|
129
129
|
int(self._effective_probe_interval),
|
|
130
130
|
)
|
|
131
131
|
|
|
132
|
-
def load_baseline(self, total_tokens: int) -> None:
|
|
132
|
+
def load_baseline(self, total_tokens: int, vendor: str | None = None) -> None:
|
|
133
133
|
"""从数据库加载窗口历史用量基线."""
|
|
134
134
|
if not self._enabled or total_tokens <= 0:
|
|
135
135
|
return
|
|
@@ -137,7 +137,14 @@ class QuotaGuard:
|
|
|
137
137
|
midpoint = time.monotonic() - self._window / 2
|
|
138
138
|
self._entries.append((midpoint, total_tokens))
|
|
139
139
|
self._total += total_tokens
|
|
140
|
-
|
|
140
|
+
if vendor:
|
|
141
|
+
logger.info(
|
|
142
|
+
"Quota guard [%s]: loaded baseline %d tokens",
|
|
143
|
+
vendor,
|
|
144
|
+
total_tokens,
|
|
145
|
+
)
|
|
146
|
+
else:
|
|
147
|
+
logger.info("Quota guard: loaded baseline %d tokens", total_tokens)
|
|
141
148
|
|
|
142
149
|
def reset(self) -> None:
|
|
143
150
|
"""手动重置为 WITHIN_QUOTA 状态."""
|
|
@@ -60,17 +60,14 @@ async def lifespan(app: FastAPI):
|
|
|
60
60
|
tier.quota_guard.window_hours,
|
|
61
61
|
vendor=tier.name,
|
|
62
62
|
)
|
|
63
|
-
tier.quota_guard.load_baseline(total)
|
|
63
|
+
tier.quota_guard.load_baseline(total, vendor=tier.name)
|
|
64
64
|
if tier.weekly_quota_guard and tier.weekly_quota_guard.enabled:
|
|
65
65
|
total = await token_logger.query_window_total(
|
|
66
66
|
tier.weekly_quota_guard.window_hours,
|
|
67
67
|
vendor=tier.name,
|
|
68
68
|
)
|
|
69
|
-
tier.weekly_quota_guard.load_baseline(total)
|
|
69
|
+
tier.weekly_quota_guard.load_baseline(total, vendor=tier.name)
|
|
70
70
|
|
|
71
|
-
logger.info(
|
|
72
|
-
"coding-proxy started: host=%s port=%d", config.server.host, config.server.port
|
|
73
|
-
)
|
|
74
71
|
yield
|
|
75
72
|
await router.close()
|
|
76
73
|
await compat_session_store.close()
|
|
@@ -42,10 +42,9 @@ def normalize_anthropic_request(body: dict[str, Any]) -> NormalizationResult:
|
|
|
42
42
|
处理策略:
|
|
43
43
|
1. 移除供应商私有块(如 server_tool_use_delta)
|
|
44
44
|
2. 重写无效/非标准的 tool_use / tool_result ID
|
|
45
|
-
3.
|
|
45
|
+
3. **剥离错位的 tool_result 块**:Anthropic API 要求 ``tool_result`` 只能出现在
|
|
46
46
|
``user`` 消息中。当检测到非 user 消息中存在 ``tool_result`` 时,
|
|
47
|
-
|
|
48
|
-
防止上游返回 ``400 invalid_request_error`` 导致全链路降级失败。
|
|
47
|
+
直接剥离以防止上游返回 ``400 invalid_request_error`` 导致全链路降级失败。
|
|
49
48
|
"""
|
|
50
49
|
normalized = copy.deepcopy(body)
|
|
51
50
|
adaptations: list[str] = []
|
|
@@ -182,15 +181,6 @@ def normalize_anthropic_request(body: dict[str, Any]) -> NormalizationResult:
|
|
|
182
181
|
if stripped_misplaced:
|
|
183
182
|
_emit_misplaced_tool_result_summary(stripped_misplaced)
|
|
184
183
|
|
|
185
|
-
# ── 后处理:迁移错位的 tool_result 块 ──────────────────────
|
|
186
|
-
# Anthropic API 强制要求 tool_result 仅存在于 user 消息中。
|
|
187
|
-
# 多 vendor 场景下(尤其是降级恢复后的对话历史),可能出现
|
|
188
|
-
# tool_result 残留在 assistant / system 等非 user 消息中的情况,
|
|
189
|
-
# 导致 Anthropic 返回 400 invalid_request_error 并触发全链路降级。
|
|
190
|
-
relocated = _relocate_misplaced_tool_results(normalized, adaptations)
|
|
191
|
-
if relocated > 0:
|
|
192
|
-
adaptations.append(f"tool_result_relocated_from_non_user_messages({relocated})")
|
|
193
|
-
|
|
194
184
|
return NormalizationResult(
|
|
195
185
|
body=normalized,
|
|
196
186
|
adaptations=sorted(set(adaptations)),
|
|
@@ -258,108 +248,3 @@ def _emit_misplaced_tool_result_summary(
|
|
|
258
248
|
known_count,
|
|
259
249
|
", ".join(sorted(known_id_set)),
|
|
260
250
|
)
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
def _relocate_misplaced_tool_results(
|
|
264
|
-
body: dict[str, Any],
|
|
265
|
-
adaptations: list[str],
|
|
266
|
-
) -> int:
|
|
267
|
-
"""检测并将非 user 消息中的 tool_result 块迁移到合法位置.
|
|
268
|
-
|
|
269
|
-
策略:
|
|
270
|
-
1. 扫描所有消息,识别非 user 消息中的 tool_result 块
|
|
271
|
-
2. 将这些块从原消息中移除
|
|
272
|
-
3. 将它们挂载到最近的前置 user 消息末尾(或创建新 user 消息)
|
|
273
|
-
|
|
274
|
-
Returns:
|
|
275
|
-
被迁移的 tool_result 块数量。
|
|
276
|
-
"""
|
|
277
|
-
messages = body.get("messages", [])
|
|
278
|
-
if not messages:
|
|
279
|
-
return 0
|
|
280
|
-
|
|
281
|
-
displaced_results: list[tuple[int, dict[str, Any]]] = [] # (msg_idx, block)
|
|
282
|
-
|
|
283
|
-
for msg_idx, message in enumerate(messages):
|
|
284
|
-
if not isinstance(message, dict):
|
|
285
|
-
continue
|
|
286
|
-
role = message.get("role")
|
|
287
|
-
if role == "user":
|
|
288
|
-
continue
|
|
289
|
-
content = message.get("content")
|
|
290
|
-
if not isinstance(content, list):
|
|
291
|
-
continue
|
|
292
|
-
|
|
293
|
-
cleaned_content: list[Any] = []
|
|
294
|
-
for block in content:
|
|
295
|
-
if (
|
|
296
|
-
isinstance(block, dict)
|
|
297
|
-
and block.get("type") == "tool_result"
|
|
298
|
-
and block.get("tool_use_id")
|
|
299
|
-
):
|
|
300
|
-
displaced_results.append((msg_idx, dict(block)))
|
|
301
|
-
logger.debug(
|
|
302
|
-
"发现错位 tool_result: messages[%d], tool_use_id=%s, "
|
|
303
|
-
"将迁移至最近的 user 消息",
|
|
304
|
-
msg_idx,
|
|
305
|
-
block.get("tool_use_id", ""),
|
|
306
|
-
)
|
|
307
|
-
else:
|
|
308
|
-
cleaned_content.append(block)
|
|
309
|
-
message["content"] = cleaned_content
|
|
310
|
-
|
|
311
|
-
if not displaced_results:
|
|
312
|
-
return 0
|
|
313
|
-
|
|
314
|
-
# 查找或创建目标 user 消息:优先选择离错位块最近的前置 user 消息
|
|
315
|
-
first_displaced_idx = displaced_results[0][0]
|
|
316
|
-
target_msg_idx = _find_nearest_user_message(messages, first_displaced_idx)
|
|
317
|
-
|
|
318
|
-
if target_msg_idx is None:
|
|
319
|
-
# 无前置 user 消息:在消息列表头部插入一个新的 user 消息
|
|
320
|
-
messages.insert(
|
|
321
|
-
0,
|
|
322
|
-
{
|
|
323
|
-
"role": "user",
|
|
324
|
-
"content": [block for _, block in displaced_results],
|
|
325
|
-
},
|
|
326
|
-
)
|
|
327
|
-
logger.info(
|
|
328
|
-
"已创建新 user 消息(索引 0)以容纳 %d 个错位 tool_result 块",
|
|
329
|
-
len(displaced_results),
|
|
330
|
-
)
|
|
331
|
-
else:
|
|
332
|
-
target_msg = messages[target_msg_idx]
|
|
333
|
-
target_content = target_msg.get("content")
|
|
334
|
-
if not isinstance(target_content, list):
|
|
335
|
-
target_content = []
|
|
336
|
-
target_msg["content"] = target_content
|
|
337
|
-
for _, block in displaced_results:
|
|
338
|
-
target_content.append(block)
|
|
339
|
-
logger.info(
|
|
340
|
-
"已将 %d 个错位 tool_result 块迁移至 messages[%d] (role=user)",
|
|
341
|
-
len(displaced_results),
|
|
342
|
-
target_msg_idx,
|
|
343
|
-
)
|
|
344
|
-
|
|
345
|
-
return len(displaced_results)
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
def _find_nearest_user_message(
|
|
349
|
-
messages: list[dict[str, Any]],
|
|
350
|
-
from_index: int,
|
|
351
|
-
) -> int | None:
|
|
352
|
-
"""查找离指定索引最近的前置 user 消息.
|
|
353
|
-
|
|
354
|
-
Args:
|
|
355
|
-
messages: 消息列表
|
|
356
|
-
from_index: 起始搜索位置(不包含此位置)
|
|
357
|
-
|
|
358
|
-
Returns:
|
|
359
|
-
最近的前置 user 消息索引,若无则返回 None。
|
|
360
|
-
"""
|
|
361
|
-
for idx in range(from_index - 1, -1, -1):
|
|
362
|
-
msg = messages[idx]
|
|
363
|
-
if isinstance(msg, dict) and msg.get("role") == "user":
|
|
364
|
-
return idx
|
|
365
|
-
return None
|
|
@@ -97,10 +97,10 @@ class GoogleOAuthTokenManager(BaseTokenManager):
|
|
|
97
97
|
from ..auth.providers.google import GoogleOAuthProvider
|
|
98
98
|
|
|
99
99
|
if not GoogleOAuthProvider.has_required_scopes(scope):
|
|
100
|
-
|
|
101
|
-
"Google access_token
|
|
102
|
-
|
|
103
|
-
|
|
100
|
+
logger.warning(
|
|
101
|
+
"Google access_token scope 不完整(%s),"
|
|
102
|
+
"可能影响 Generative Language API 调用",
|
|
103
|
+
scope,
|
|
104
104
|
)
|
|
105
105
|
expires_in = data.get("expires_in", 3600)
|
|
106
106
|
logger.info("Google OAuth token refreshed, expires_in=%ds", expires_in)
|
|
@@ -14,7 +14,10 @@ from coding.proxy.config.schema import (
|
|
|
14
14
|
from coding.proxy.routing.model_mapper import ModelMapper
|
|
15
15
|
from coding.proxy.vendors.antigravity import AntigravityVendor, GoogleOAuthTokenManager
|
|
16
16
|
from coding.proxy.vendors.base import RequestCapabilities
|
|
17
|
-
from coding.proxy.vendors.token_manager import
|
|
17
|
+
from coding.proxy.vendors.token_manager import ( # noqa: F401
|
|
18
|
+
TokenAcquireError,
|
|
19
|
+
TokenErrorKind,
|
|
20
|
+
)
|
|
18
21
|
|
|
19
22
|
# --- GoogleOAuthTokenManager ---
|
|
20
23
|
|
|
@@ -130,8 +133,12 @@ async def test_token_manager_close():
|
|
|
130
133
|
|
|
131
134
|
|
|
132
135
|
@pytest.mark.asyncio
|
|
133
|
-
async def
|
|
134
|
-
"""refresh 成功但 scope
|
|
136
|
+
async def test_token_manager_partial_scope_warns_but_succeeds():
|
|
137
|
+
"""refresh 成功但 scope 不完整时,应发出警告但正常返回 token.
|
|
138
|
+
|
|
139
|
+
Google OAuth2 规范允许 refresh_token 返回的 access_token 仅包含部分已授权 scope,
|
|
140
|
+
这是正常行为。参考 Antigravity-Manager 项目,不做刷新后的严格 scope 校验。
|
|
141
|
+
"""
|
|
135
142
|
tm = GoogleOAuthTokenManager("cid", "secret", "refresh")
|
|
136
143
|
|
|
137
144
|
mock_response = MagicMock()
|
|
@@ -147,11 +154,9 @@ async def test_token_manager_insufficient_scope_requires_reauth():
|
|
|
147
154
|
mock_client.is_closed = False
|
|
148
155
|
tm._client = mock_client
|
|
149
156
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
assert exc_info.value.needs_reauth is True
|
|
154
|
-
assert exc_info.value.kind == TokenErrorKind.INSUFFICIENT_SCOPE
|
|
157
|
+
# 应正常返回 token,不再抛异常
|
|
158
|
+
token = await tm.get_token()
|
|
159
|
+
assert token == "goog_abc"
|
|
155
160
|
|
|
156
161
|
|
|
157
162
|
# --- AntigravityVendor ---
|
|
@@ -410,8 +410,22 @@ async def test_query_daily_days_boundary_exact(logger):
|
|
|
410
410
|
@pytest.mark.asyncio
|
|
411
411
|
async def test_query_daily_groups_by_local_date(logger):
|
|
412
412
|
"""UTC 时间 16:30 (= UTC+8 次日 00:30) 应归入本地次日."""
|
|
413
|
-
#
|
|
414
|
-
|
|
413
|
+
# 动态计算:取「今天本地日期」的前一天作为基准,
|
|
414
|
+
# 构造该日本地 00:30(= UTC 前日 16:30)的时间戳,
|
|
415
|
+
# 确保 days=7 窗口必然包含此数据。
|
|
416
|
+
now_local = datetime.now(_SHANGHAI)
|
|
417
|
+
target_local_date = now_local.date() - timedelta(days=1) # 昨天
|
|
418
|
+
target_local_dt = datetime(
|
|
419
|
+
target_local_date.year,
|
|
420
|
+
target_local_date.month,
|
|
421
|
+
target_local_date.day,
|
|
422
|
+
0,
|
|
423
|
+
30,
|
|
424
|
+
0,
|
|
425
|
+
tzinfo=_SHANGHAI,
|
|
426
|
+
)
|
|
427
|
+
utc_ts = target_local_dt.astimezone(UTC).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
|
428
|
+
|
|
415
429
|
await logger._db.execute(
|
|
416
430
|
"""INSERT INTO usage_log (ts, vendor, model_requested, model_served,
|
|
417
431
|
input_tokens, output_tokens)
|
|
@@ -424,8 +438,8 @@ async def test_query_daily_groups_by_local_date(logger):
|
|
|
424
438
|
rows = await logger.query_daily(days=7)
|
|
425
439
|
|
|
426
440
|
assert len(rows) == 1
|
|
427
|
-
# 在 UTC+8 下,UTC 16:30 是次日 00:30,应显示为
|
|
428
|
-
assert rows[0]["date"] == "
|
|
441
|
+
# 在 UTC+8 下,UTC 16:30 是次日 00:30,应显示为 target_local_date
|
|
442
|
+
assert rows[0]["date"] == target_local_date.strftime("%Y-%m-%d")
|
|
429
443
|
|
|
430
444
|
|
|
431
445
|
@pytest.mark.asyncio
|