返回提交历史
Modified
g4f/Provider/needs_auth/Antigravity.py
+8
-7
Modified
g4f/cookies.py
+9
-3
Modified
g4f/requests/__init__.py
+2
-2
XFEstudio/gpt4free
Refactor AntigravityProvider to improve retry logic on 503 errors; update BrowserConfig to load executable path and connection timeout from environment variables
0904b3d2
代码差异
3 个文件
+19
-12
@@ -1190,14 +1190,15 @@ class AntigravityProvider:
1190
1190
if not resp.ok:
1191
1191
if resp.status == 503:
1192
1192
try:
1193
max_retry_delay = int(max([d.get("retryDelay", 0) for d in (await resp.json(content_type=None)).get("error", {}).get("details", [])]))
1193
retry_delay = int(max([float(d.get("retryDelay", 0)) for d in (await resp.json(content_type=None)).get("error", {}).get("details", [])]))
1194
1194
except ValueError:
1195
max_retry_delay = 30 # Default retry delay if not specified
1196
debug.log(f"Received 503 error, retrying after {max_retry_delay}")
1197
await asyncio.sleep(max_retry_delay)
1198
resp = await session.post(url, json=req_body)
1199
if not resp.ok:
1200
debug.error(f"Retry after 503 failed with status {resp.status}")
1195
retry_delay = 30 # Default retry delay if not specified
1196
debug.log(f"Received 503 error, retrying after {retry_delay}")
1197
if retry_delay <= 120:
1198
await asyncio.sleep(retry_delay)
1199
resp = await session.post(url, json=req_body)
1200
if not resp.ok:
1201
debug.error(f"Retry after 503 failed with status {resp.status}")
1201
1202
if not resp.ok:
1202
1203
if resp.status == 401:
1203
1204
raise MissingAuthError("Unauthorized (401) from Antigravity API")
@@ -54,12 +54,19 @@ class BrowserConfig:
54
54
port: int = None
55
55
host: str = "127.0.0.1"
56
56
impersonate: str = "chrome"
57
executable_path: str = None
58
connection_timeout: float = 0.25
57
59
58
60
@staticmethod
59
61
async def stop_browser():
60
62
return None
61
63
62
browser_executable_path: str = None
64
@classmethod
65
def load_from_env(cls):
66
cls.port = os.environ.get("G4F_BROWSER_PORT", cls.port)
67
cls.host = os.environ.get("G4F_BROWSER_HOST", cls.host)
68
cls.executable_path = os.environ.get("G4F_BROWSER_EXECUTABLE_PATH", cls.executable_path)
69
cls.connection_timeout = float(os.environ.get("G4F_BROWSER_CONNECTION_TIMEOUT", cls.connection_timeout))
63
70
64
71
COOKIE_DOMAINS = (
65
72
".bing.com",
@@ -214,8 +221,7 @@ def read_cookie_files(dir_path: Optional[str] = None, domains_filter: Optional[L
214
221
215
222
AppConfig.load_from_env()
216
223
217
BrowserConfig.port = os.environ.get("G4F_BROWSER_PORT", BrowserConfig.port)
218
BrowserConfig.host = os.environ.get("G4F_BROWSER_HOST", BrowserConfig.host)
224
BrowserConfig.load_from_env()
219
225
if BrowserConfig.port:
220
226
BrowserConfig.port = int(BrowserConfig.port)
221
227
debug.log(f"Using browser: {BrowserConfig.host}:{BrowserConfig.port}")
@@ -167,7 +167,7 @@ async def get_nodriver(
167
167
'Install "zendriver" and "platformdirs" package | pip install -U zendriver platformdirs')
168
168
user_data_dir = user_config_dir(f"g4f-{user_data_dir}") if user_data_dir and has_platformdirs else None
169
169
if browser_executable_path is None:
170
browser_executable_path = BrowserConfig.browser_executable_path
170
browser_executable_path = BrowserConfig.executable_path
171
171
if browser_executable_path is None:
172
172
try:
173
173
browser_executable_path = find_executable()
@@ -216,7 +216,7 @@ async def get_nodriver(
216
216
browser_executable_path=browser_executable_path,
217
217
port=BrowserConfig.port,
218
218
host=BrowserConfig.host,
219
browser_connection_timeout=1,
219
connection_timeout=BrowserConfig.connection_timeout,
220
220
**kwargs
221
221
)
222
222
except FileNotFoundError as e: