mirror of
https://github.com/j93es/browser-use-oauth.git
synced 2026-06-04 14:01:52 +09:00
feat: 코드베이스 리팩터링
* `run.py`에서 `main.py` 경로를 명시적으로 지정하고, 명령줄 인자를 보다 사용하기 쉽게 조정했습니다. * 에이전트, 리소스 정리, 공통 함수, 모델 등을 포함하는 브라우저 유틸리티용 신규 모듈 구조를 만들었습니다. * `agents.py`에 비동기 에이전트 실행 및 재시도 로직을 구현했습니다. * `scanner.py`에 OAuth URL 추출 및 로그인 테스트 기능을 추가했습니다. * 전반적인 코드베이스에 걸쳐 에러 핸들링 및 로깅을 강화했습니다. * 백엔드 URL과 Google API 키 등의 관리를 위한 환경변수 기반 설정 시스템을 도입했습니다. * 스캐닝 중 진행 상태 추적 및 시그널 핸들링을 통한 정상 종료 처리를 개선했습니다. * 텍스트 파일 읽기 및 HTML 콘텐츠 여부 확인을 위한 유틸리티 함수를 추가했습니다. * LLM과의 상호작용을 위한 구조화된 프롬프트 시스템을 구축했습니다.
This commit is contained in:
parent
1ddc3c41bc
commit
069dbf446d
29 changed files with 453 additions and 452 deletions
167
src/lib/browser_use/agents.py
Normal file
167
src/lib/browser_use/agents.py
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
import asyncio
|
||||
import os
|
||||
import json
|
||||
|
||||
from browser_use import Agent, BrowserSession, Controller
|
||||
from patchright.async_api import async_playwright as async_patchright
|
||||
|
||||
from lib.browser_use import (
|
||||
GetProfile,
|
||||
GetSensitiveData,
|
||||
clean_resources,
|
||||
)
|
||||
from lib.utils import (
|
||||
logger,
|
||||
config,
|
||||
)
|
||||
from lib.llm import CreateChatGoogleGenerativeAI, get_prompt
|
||||
import lib.browser_use.model as model
|
||||
|
||||
# Exponential backoff settings
|
||||
INITIAL_BACKOFF = int(os.getenv("INITIAL_BACKOFF", "60")) # seconds
|
||||
MAX_BACKOFF = int(os.getenv("MAX_BACKOFF", "600")) # seconds
|
||||
|
||||
async def _run_agent_with_retry(agent_config):
|
||||
"""Agent 실행을 위한 내부 헬퍼 함수 (재시도 로직 포함)"""
|
||||
agent = None
|
||||
session = None
|
||||
try_cnt = 0
|
||||
url = agent_config["url"]
|
||||
|
||||
while try_cnt < 3:
|
||||
try:
|
||||
session = BrowserSession(
|
||||
playwright=(await async_patchright().start()),
|
||||
browser_profile=await GetProfile(),
|
||||
)
|
||||
|
||||
agent = Agent(
|
||||
browser_session=session,
|
||||
**agent_config["agent_params"]
|
||||
)
|
||||
|
||||
response = await agent.run()
|
||||
await clean_resources(agent, session)
|
||||
return response
|
||||
|
||||
except Exception as e:
|
||||
await clean_resources(agent, session)
|
||||
|
||||
if "ResourceExhausted" in str(e) or "429" in str(e):
|
||||
wait = min(INITIAL_BACKOFF * (2**try_cnt), MAX_BACKOFF)
|
||||
print(f"⚠️ API 쿼터 에러: {e}. {wait}초 대기 후 재시도합니다...")
|
||||
await asyncio.sleep(wait)
|
||||
try_cnt += 1
|
||||
if try_cnt >= 3:
|
||||
error_msg = f"API 쿼터 문제가 지속됩니다."
|
||||
logger(f"❌ {url} - {agent_config['log_context']} 실패: {error_msg}: {e}")
|
||||
print(f"❌ {url} - {agent_config['log_context']} 실패: {error_msg}")
|
||||
return None
|
||||
continue
|
||||
|
||||
# 일반 에러 처리
|
||||
try_cnt += 1
|
||||
if try_cnt >= 3:
|
||||
error_msg = f"최대 재시도 횟수 초과."
|
||||
logger(f"❌ {url} - {agent_config['log_context']} 실패: {error_msg}: {e}")
|
||||
print(f"❌ {url} - {agent_config['log_context']} 실패: {error_msg}")
|
||||
return None
|
||||
|
||||
print(f"⚠️ 에러 발생: {e}. {try_cnt}번째 재시도 중...")
|
||||
await asyncio.sleep(30)
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
async def extract_oauth_list(url: str):
|
||||
"""첫 번째 Agent: 로그인 페이지를 찾고 OAuth 리스트만 추출"""
|
||||
target_url = url if url.startswith("http") else f"https://{url}"
|
||||
print(f"🔎 OAuth 리스트 추출 시작: {target_url}")
|
||||
|
||||
agent_config = {
|
||||
"url": target_url,
|
||||
"log_context": "OAuth 리스트 추출",
|
||||
"agent_params": {
|
||||
"initial_actions": [{"open_tab": {"url": target_url}}],
|
||||
"sensitive_data": GetSensitiveData(),
|
||||
"task": (
|
||||
"Navigate to the login page and identify all OAuth provider buttons (excluding Passkey). "
|
||||
"DO NOT click any OAuth buttons or attempt to login. "
|
||||
"Just find and list all available OAuth providers with their button texts or provider names. "
|
||||
"Return a list of OAuth providers found on the login page."
|
||||
),
|
||||
"llm": CreateChatGoogleGenerativeAI(config.GOOGLE_MODEL),
|
||||
"planner_llm": (
|
||||
CreateChatGoogleGenerativeAI(config.GOOGLE_PLANNER_MODEL)
|
||||
if config.GOOGLE_PLANNER_MODEL
|
||||
else None
|
||||
),
|
||||
"controller": Controller(
|
||||
output_model=model.OAuthList,
|
||||
exclude_actions=["search_google", "unknown_action", "unkown"],
|
||||
),
|
||||
"extend_planner_system_message": get_prompt("auth"),
|
||||
}
|
||||
}
|
||||
|
||||
response = await _run_agent_with_retry(agent_config)
|
||||
|
||||
if not response:
|
||||
return []
|
||||
|
||||
final_result = response.final_result()
|
||||
if not final_result:
|
||||
print("OAuth 리스트 추출 결과가 없습니다.")
|
||||
return []
|
||||
|
||||
try:
|
||||
data = json.loads(final_result)
|
||||
oauth_providers = data.get("oauth_providers", [])
|
||||
return [model.OAuth(provider=provider) for provider in oauth_providers]
|
||||
except (json.JSONDecodeError, KeyError) as e:
|
||||
print(f"❌ 결과 파싱 실패: {e}")
|
||||
logger(f"❌ {url} 결과 파싱 실패: {final_result}")
|
||||
return []
|
||||
|
||||
|
||||
async def test_oauth_login(url: str, oauth_provider: str):
|
||||
"""두 번째 Agent: 특정 OAuth 제공자로 로그인 시도"""
|
||||
target_url = url if url.startswith("http") else f"https://{url}"
|
||||
print(f"🔐 {oauth_provider} 로그인 시작: {target_url}")
|
||||
|
||||
agent_config = {
|
||||
"url": target_url,
|
||||
"log_context": f"{oauth_provider} 로그인",
|
||||
"agent_params": {
|
||||
"initial_actions": [{"open_tab": {"url": target_url}}],
|
||||
"sensitive_data": GetSensitiveData(),
|
||||
"task": (
|
||||
f"Navigate to the login page, find and click the {oauth_provider} OAuth button, "
|
||||
f"then follow the complete OAuth login flow as far as possible with a real user account. "
|
||||
f"Capture the final redirect URL after login completion. "
|
||||
f"If login fails or encounters errors, report the issue. "
|
||||
f"Focus only on {oauth_provider} - ignore other OAuth providers."
|
||||
),
|
||||
"llm": CreateChatGoogleGenerativeAI(config.GOOGLE_MODEL),
|
||||
"planner_llm": (
|
||||
CreateChatGoogleGenerativeAI(config.GOOGLE_PLANNER_MODEL)
|
||||
if config.GOOGLE_PLANNER_MODEL and os.getenv("ENABLE_PLANNER_MODEL_OAUTH_LOGIN")
|
||||
else None
|
||||
),
|
||||
"controller": Controller(
|
||||
exclude_actions=["search_google", "unknown_action", "unkown"],
|
||||
),
|
||||
"extend_planner_system_message": get_prompt(oauth_provider),
|
||||
}
|
||||
}
|
||||
|
||||
response = await _run_agent_with_retry(agent_config)
|
||||
|
||||
if response and response.final_result():
|
||||
final_result = response.final_result()
|
||||
print(f"✅ {oauth_provider} 로그인 완료")
|
||||
logger(f"✅ {url} - {oauth_provider} 로그인 결과: {final_result}")
|
||||
return True
|
||||
|
||||
print(f"❌ {oauth_provider} 로그인 실패")
|
||||
return False
|
||||
Loading…
Add table
Add a link
Reference in a new issue