[Update] new logic

This commit is contained in:
tv0924@icloud.com 2025-06-22 20:35:56 +09:00
commit 92967ed353
38 changed files with 1516 additions and 5209 deletions

View file

@ -0,0 +1,51 @@
import json
from pydantic import BaseModel
from browser_use import (
Agent,
Controller,
)
from lib.agents.run_agent import run_agent
from lib.utils.logger import logger
from lib.browser_use_utils.clean_resources import clean_agent_resources
from lib.browser_use_utils.create_google_ai import create_google_ai
from lib.config import GOOGLE_MODEL, GOOGLE_PLANNER_MODEL
NOT_FOUND_LOGIN_PAGE = 0
FOUND_LOGIN_PAGE = 1
class IsFound(BaseModel):
status: int
async def find_login_page(target_url, session):
initial_actions = [{"open_tab": {"url": target_url}}]
task = "Navigate to the login page, and stop"
extend_planner_system_message = "You are an expert in finding login pages. Your task is to navigate to the login page of the given URL and stop there."
controller = Controller(output_model=IsFound, exclude_actions=['search_google'])
agent = Agent(
browser_session=session,
initial_actions=initial_actions,
task=task,
llm=create_google_ai(GOOGLE_MODEL),
planner_llm=create_google_ai(GOOGLE_PLANNER_MODEL),
controller=controller,
extend_planner_system_message=extend_planner_system_message,
)
status, final_result = await run_agent(agent)
if status:
logger(f"⚠️ 스캔 실패: {target_url} | {final_result}")
print(f"⚠️ 스캔 실패: {target_url} | {final_result}")
return False, None;
data = json.loads(final_result)
try:
is_found = IsFound(**data)
if is_found.status == NOT_FOUND_LOGIN_PAGE:
return False, "로그인 페이지를 찾을 수 없습니다."
else:
return True, "로그인 페이지를 찾았습니다."
except Exception as e:
logger(f"⚠️ 결과 파싱 실패: {target_url} | {e}\n원본 결과: {final_result}")
print(f"⚠️ 결과 파싱 실패: {target_url} | {e}\n원본 결과: {final_result}")
return False, "결과 파싱 실패"

20
lib/agents/run_agent.py Normal file
View file

@ -0,0 +1,20 @@
from lib.browser_use_utils.clean_resources import clean_agent_resources
async def run_agent(agent):
try:
response = await agent.run()
final_result = response.final_result()
if final_result is None:
return -1, "최종 결과가 없습니다. 에이전트 실행 실패"
return 0, final_result
except Exception as e:
# API 쿼터 문제인지 확인
if "ResourceExhausted" in str(e) or "429" in str(e):
return 1, "API 쿼터 에러로 인한 실패"
# 일반 에러 처리
else:
return 2, "일반 에러로 인한 실패"
finally:
await clean_agent_resources(agent)
print("리소스 정리 완료")