refactor: 重构错误处理并优化路由与服务结构

主要变更:
- 新增 `app/handler/error_handler.py`,引入 `handle_route_errors` 异步上下文管理器,用于统一处理路由中的错误和日志记录。
- 在 `openai_routes` 和 `openai_compatiable_routes` 中应用 `handle_route_errors`,移除冗余的 try-except 块,简化路由逻辑。
- 将 `OpenAICompatiableService` 移动到 `app/service/openai_compatiable/` 目录下。
- 将 `StatsService` 移动到 `app/service/stats/` 目录下,并更新相关导入路径。
- 修复 `response_handler` 中处理 Gemini API 响应时 `inlineData` 字段的错误(原为 `inline_data`)。
- 修复 `openai_routes` 和 `openai_compatiable_routes` 中处理图像生成聊天(如 imagen3-chat)时未正确使用付费 API key 的问题。
- 在 `requirements.txt` 中将 `httpx` 更改为 `httpx[socks]`,以增加 SOCKS 代理支持。
This commit is contained in:
snaily
2025-05-02 01:20:05 +08:00
parent 7c9b721164
commit 2072f54ca1
11 changed files with 216 additions and 204 deletions

View File

@@ -172,7 +172,7 @@ def _extract_result(
text = _format_execution_result(parts[0]["executableCodeResult"])
elif "codeExecutionResult" in parts[0]:
text = _format_execution_result(parts[0]["codeExecutionResult"])
elif "inline_data" in parts[0]:
elif "inlineData" in parts[0]:
text = _extract_image_data(parts[0])
else:
text = ""
@@ -203,7 +203,7 @@ def _extract_result(
for part in candidate["content"]["parts"]:
if "text" in part:
text += part["text"]
elif "inline_data" in part:
elif "inlineData" in part:
text += _extract_image_data(part)
text = _add_search_link_text(model, candidate, text)
@@ -233,7 +233,7 @@ def _extract_image_data(part: dict) -> str:
)
current_date = time.strftime("%Y/%m/%d")
filename = f"{current_date}/{uuid.uuid4().hex[:8]}.png"
base64_data = part["inline_data"]["data"]
base64_data = part["inlineData"]["data"]
# 将base64_data转成bytes数组
bytes_data = base64.b64decode(base64_data)
upload_response = image_uploader.upload(bytes_data, filename)