Skip to content

Commit 428fda8

Browse files
committed
refactor: 整合timeout/sleep常量到timeouts.py
1 parent 959b663 commit 428fda8

21 files changed

+314
-486
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
🎤 Gemini 2.5 TTS 语音生成
2222
</p>
2323

24-
<img src="docs/demo.gif" alt="Demo GIF" width="100%" />
24+
<img src="docs/img/demo.gif" alt="Demo GIF" width="100%" />
2525

26-
<p align="center">
27-
<img src="docs/多worker并发和媒体模型支援.png" alt="多Worker并发与媒体模型支援" width="80%" />
28-
</p>
26+
<!-- <p align="center">
27+
<img src="docs/img/多worker并发和媒体模型支援.png" alt="多Worker并发与媒体模型支援" width="80%" />
28+
</p> -->
2929

3030
</div>
3131

README_en.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
🎤 Gemini 2.5 TTS Speech Synthesis
2222
</p>
2323

24-
<img src="docs/demo.gif" alt="Demo GIF" width="100%" />
24+
<img src="docs/img/demo.gif" alt="Demo GIF" width="100%" />
2525

26-
<p align="center">
27-
<img src="docs/多worker并发和媒体模型支援.png" alt="Multi-Worker Concurrency & Media Model Support" width="80%" />
28-
</p>
26+
<!-- <p align="center">
27+
<img src="docs/img/多worker并发和媒体模型支援.png" alt="Multi-Worker Concurrency & Media Model Support" width="80%" />
28+
</p> -->
2929

3030
</div>
3131

docs/dependency-versions.md

Lines changed: 0 additions & 252 deletions
This file was deleted.
File renamed without changes.

docs/installation-guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,5 @@ admin-key-for-testing
232232
- [环境变量配置指南](environment-configuration.md) - ⭐ 推荐先配置,简化后续使用
233233
- [首次运行与认证指南](authentication-setup.md)
234234
- [API使用指南](api-usage.md) - 包含详细的密钥管理说明
235+
- [项目结构说明](project-structure.md) - 了解代码组织
235236
- [故障排除指南](troubleshooting.md)

docs/project-structure.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,25 @@ AIStudio2API 是一个高性能代理服务,将 Google AI Studio 网页界面
7474
| 文件 | 描述 |
7575
|------|------|
7676
| `__init__.py` | 导出所有配置常量 |
77-
| `constants.py` | 全局常量定义 |
78-
| `settings.py` | 环境变量设置 |
79-
| `timeouts.py` | 超时配置 |
77+
| `constants.py` | 全局常量:模型名称、ID前缀、默认参数等 |
78+
| `settings.py` | 环境变量设置:调试开关、认证配置、路径定义 |
79+
| `timeouts.py` | **超时和时间常量**。包含所有 sleep/timeout/retry/delay 统一常量 |
8080
| `selectors.py` | **主选择器**。AI Studio 核心页面元素 CSS/XPath 选择器 |
8181
| `tts_selectors.py` | TTS 页面选择器 |
8282
| `imagen_selectors.py` | Imagen 页面选择器 |
8383
| `veo_selectors.py` | Veo 页面选择器 |
8484
| `nano_selectors.py` | Nano Banana 页面选择器 |
8585

86+
#### timeouts.py 常量分类
87+
88+
| 类别 | 示例常量 | 用途 |
89+
|------|----------|------|
90+
| Sleep | `SLEEP_TICK`, `SLEEP_RETRY`, `SLEEP_NAVIGATION` | UI操作等待时间 |
91+
| Delay | `DELAY_AFTER_CLICK`, `DELAY_AFTER_FILL` | 操作后延迟 |
92+
| Timeout | `TIMEOUT_ELEMENT_VISIBLE`, `TIMEOUT_PAGE_NAVIGATION` | 超时限制 |
93+
| Retry | `MAX_RETRIES`, `BASE_STREAM_RETRIES` | 重试次数 |
94+
| URL | `NEW_CHAT_URL` | 固定URL模板 |
95+
8696
---
8797

8898
### tts/ - TTS 语音生成模块
@@ -190,6 +200,7 @@ AIStudio2API 是一个高性能代理服务,将 Google AI Studio 网页界面
190200
| `multi-worker-guide.md` | 多 Worker 模式说明 |
191201
| `docker-deployment.md` | Docker 部署指南 |
192202
| `development-guide.md` | 开发指南 |
203+
| `project-structure.md` | 项目结构说明 (本文档) |
193204
| `dependency-versions.md` | 依赖版本说明 |
194205
| `reverse-engineering-internals.md` | 逆向工程内部文档 |
195206
| `client-stop-mechanisms-analysis.md` | 客户端中断机制分析 |

src/api/request_processor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from fastapi.responses import JSONResponse, StreamingResponse
1111
from playwright.async_api import Page as AsyncPage, Locator, Error as PlaywrightAsyncError, expect as expect_async
1212
from config import *
13+
from config.timeouts import STREAM_CHUNK_SIZE
1314
from models import ChatCompletionRequest, ClientDisconnectedError
1415
from browser import switch_ai_studio_model, save_error_snapshot
1516
from .utils import validate_chat_request, prepare_combined_prompt, generate_sse_chunk, generate_sse_stop_chunk, use_stream_response, calculate_usage_stats, request_manager, calculate_stream_max_retries
@@ -548,7 +549,7 @@ async def create_response_stream_generator():
548549
pass
549550
break
550551
if line:
551-
chunk_size = 50
552+
chunk_size = STREAM_CHUNK_SIZE
552553
for i in range(0, len(line), chunk_size):
553554
chunk = line[i:i + chunk_size]
554555
yield generate_sse_chunk(chunk, req_id, current_ai_studio_model_id or MODEL_NAME)

src/api/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import hashlib
1313
import threading
1414

15+
from config.timeouts import BASE_STREAM_RETRIES
16+
1517
class RequestCancellationManager:
1618

1719
def __init__(self):
@@ -49,7 +51,7 @@ def get_active_requests(self) -> List[Dict[str, Any]]:
4951
request_manager = RequestCancellationManager()
5052

5153
def calculate_stream_max_retries(messages: List[Message]) -> int:
52-
base_retries = 300
54+
base_retries = BASE_STREAM_RETRIES
5355
total_token_estimate = 0
5456
image_count = 0
5557

src/browser/model_management.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Optional, Set
77
from playwright.async_api import Page as AsyncPage, expect as expect_async, Error as PlaywrightAsyncError
88
from config import *
9+
from config.timeouts import MAX_RETRIES, SLEEP_VIDEO_POLL
910
from models import ClientDisconnectedError
1011
logger = logging.getLogger('AIStudioProxyServer')
1112

@@ -367,7 +368,7 @@ async def _handle_initial_model_state_and_storage(page: AsyncPage):
367368
await _set_model_from_page_display(page, set_storage=True)
368369
current_page_url = page.url
369370
logger.info(f' 步骤 2: 重新加载页面 ({current_page_url}) 以应用 isAdvancedOpen=true...')
370-
max_retries = 3
371+
max_retries = MAX_RETRIES
371372
for attempt in range(max_retries):
372373
try:
373374
logger.info(f' 尝试重新加载页面 (第 {attempt + 1}/{max_retries} 次): {current_page_url}')
@@ -385,7 +386,7 @@ async def _handle_initial_model_state_and_storage(page: AsyncPage):
385386
logger.warning(f' ⚠️ 页面重新加载尝试 {attempt + 1}/{max_retries} 失败: {reload_err}')
386387
if attempt < max_retries - 1:
387388
logger.info(f' 将在5秒后重试...')
388-
await asyncio.sleep(5)
389+
await asyncio.sleep(SLEEP_VIDEO_POLL)
389390
else:
390391
logger.error(f' ❌ 页面重新加载在 {max_retries} 次尝试后最终失败: {reload_err}. 后续模型状态可能不准确。', exc_info=True)
391392
from .operations import save_error_snapshot

0 commit comments

Comments
 (0)