feat: Antigravity 和 Codex 服务支持

- 新增 Antigravity 服务:签名缓存、URL 可用性检测、信封处理
- 新增 Codex 服务:信封处理、元数据收集器
- 重构 provider transport 支持新的服务架构
- 新增 stream_bridge 和 upstream_stream_bridge 处理流式响应
- 优化 OAuth 工具函数
- 添加相关测试用例
This commit is contained in:
fawney19
2026-02-05 15:57:52 +08:00
parent ed2ff5c1d7
commit 440721368f
44 changed files with 3498 additions and 134 deletions

View File

@@ -13,6 +13,7 @@ Thinking 整流器Rectifier
"""
import copy
import json
from typing import Any
from src.core.logger import logger
@@ -67,6 +68,103 @@ class ThinkingRectifier:
return rectified_body, modified
@staticmethod
def rectify_signature_sensitive_blocks(
request_body: dict[str, Any],
) -> tuple[dict[str, Any], bool]:
"""Second-stage rectification for signature-related failures.
This is a more aggressive fallback than `rectify()`:
- Removes all thinking/redacted_thinking blocks
- Removes signature fields on remaining blocks
- Degrades tool_use/tool_result blocks into plain text blocks
- Disables top-level `thinking` when enabled
"""
if not request_body:
return request_body, False
rectified_body = copy.deepcopy(request_body)
modified = False
messages = rectified_body.get("messages", [])
if isinstance(messages, list) and messages:
new_messages: list[Any] = []
for message in messages:
if not isinstance(message, dict):
new_messages.append(message)
continue
new_message = dict(message)
content = message.get("content")
if isinstance(content, list):
new_content: list[Any] = []
for block in content:
if not isinstance(block, dict):
new_content.append(block)
continue
block_type = block.get("type")
if block_type in ("thinking", "redacted_thinking"):
modified = True
continue
if block_type == "tool_use":
# Degrade into text to avoid strict structure/signature validation.
name = block.get("name")
inp = block.get("input")
try:
inp_text = json.dumps(inp, ensure_ascii=False)
except Exception:
inp_text = str(inp)
new_content.append(
{
"type": "text",
"text": f"[tool_use] name={name} input={inp_text}",
}
)
modified = True
continue
if block_type == "tool_result":
raw = block.get("content")
try:
raw_text = json.dumps(raw, ensure_ascii=False)
except Exception:
raw_text = str(raw)
new_content.append(
{
"type": "text",
"text": f"[tool_result] {raw_text}",
}
)
modified = True
continue
# Remove signature field (for any non-thinking block).
if "signature" in block:
new_block = {k: v for k, v in block.items() if k != "signature"}
new_content.append(new_block)
modified = True
continue
new_content.append(block)
new_message["content"] = new_content
new_messages.append(new_message)
rectified_body["messages"] = new_messages
# Stage-2: disable top-level thinking unconditionally when enabled.
thinking_param = rectified_body.get("thinking")
if isinstance(thinking_param, dict) and thinking_param.get("type") == "enabled":
del rectified_body["thinking"]
modified = True
logger.info("ThinkingRectifier(stage2): 已移除顶层 thinking 参数")
return rectified_body, modified
@staticmethod
def _rectify_messages(messages: list[dict[str, Any]]) -> tuple[list[dict[str, Any]], bool]:
"""