feat: 리팩토링, User Data

This commit is contained in:
암냥 2025-07-02 20:18:01 +09:00
commit f5ee676468
7 changed files with 95 additions and 516 deletions

View file

@ -9,40 +9,6 @@ from dotenv import load_dotenv
load_dotenv(override=True)
async def setup_storage_state():
"""Setup browser storage state for session persistence."""
# Get the script directory to ensure correct path resolution
script_dir = Path(__file__).parent.parent.parent.parent
storage_state_path = script_dir / "data" / "storage_state.json"
storage_state_temp_path = script_dir / "data" / "storage_state_temp.json"
print(f"📂 Storage state path: {storage_state_path}")
print(f"📂 Temp storage state path: {storage_state_temp_path}")
if storage_state_path.exists():
try:
if storage_state_temp_path.exists():
storage_state_temp_path.unlink()
with open(storage_state_path, "r") as f:
storage_data = json.load(f)
with open(storage_state_temp_path, "w") as f:
json.dump(storage_data, f, indent=4)
print(f"🔄 Using existing storage state: {storage_state_temp_path}")
return str(storage_state_temp_path)
except Exception as e:
print(f"⚠️ Error processing storage state: {e}")
if storage_state_temp_path.exists():
storage_state_temp_path.unlink()
return None
print("⚠️ No existing storage state found")
return None
def setup_proxy():
"""Configure proxy settings from environment variables."""
proxy_host = os.getenv("PROXY_HOST")

View file

@ -1,42 +1,37 @@
import os
import shutil
import tempfile
from lib.browser_use.func import *
from lib.utils.config import USER_DATA_DIR
# Initialize configuration
proxy_url = setup_proxy()
async def GetProfile():
storage_state_path = await setup_storage_state()
# Handle potential encoding issues with storage state file
try:
if storage_state_path and os.path.exists(storage_state_path):
# Test if file can be read properly, if not, skip it
with open(storage_state_path, "r", encoding="utf-8") as f:
f.read()
storage_state = storage_state_path
else:
print(
"⚠️ Storage state file not found or inaccessible, proceeding without it."
)
storage_state = None
except (UnicodeDecodeError, FileNotFoundError):
# If there's an encoding error, don't use the storage state
storage_state = None
async def GetProfile(headless=False):
user_data_dir = None
if USER_DATA_DIR and os.path.isdir(USER_DATA_DIR):
try:
tmp_user_data_dir = tempfile.mkdtemp()
shutil.copytree(USER_DATA_DIR, tmp_user_data_dir, dirs_exist_ok=True)
user_data_dir = tmp_user_data_dir
print(f"✅ Copied user data dir to temporary location: {user_data_dir}")
except Exception as e:
print(f"❌ Failed to copy user data dir: {e}")
profile = BrowserProfile(
# Security settings
disable_security=True,
stealth=True,
# Display settings
headless=False,
headless=headless,
device_scale_factor=1,
window_size={"width": 1600, "height": 900},
viewport={"width": 1600, "height": 900},
# Data persistence
user_data_dir=None,
storage_state=storage_state,
user_data_dir=user_data_dir,
#storage_state=storage_state,
# Network settings
proxy={"server": proxy_url} if proxy_url else None,
# Additional arguments

View file

@ -14,6 +14,6 @@ def CreateChatGoogle(model: str):
return ChatGoogle(
model=model,
temperature=0.0,
temperature=0.0
# Browser Use는 내부적으로 재시도 로직을 처리합니다
)

View file

@ -8,3 +8,4 @@ BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:11081")
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
GOOGLE_MODEL = os.getenv("GOOGLE_MODEL", "gemini-2.5-flash")
GOOGLE_PLANNER_MODEL = os.getenv("GOOGLE_PLANNER_MODEL")
USER_DATA_DIR = os.getenv("USER_DATA_DIR", "./data/user_data")