Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

use `CONFIG` instead of `Config()`

shenchuchengfac1cb0f595a
diff --git a/metagpt/tools/__init__.py b/metagpt/tools/__init__.py
index 6f418baf4..f9b7abc52 100644
--- a/metagpt/tools/__init__.py
+++ b/metagpt/tools/__init__.py
@@ -18,6 +18,6 @@ class SearchEngineType(Enum):
class WebBrowserEngineType(Enum):
- PLAYWRIGHT = auto()
- SELENIUM = auto()
- CUSTOM_ENGINE = auto()
+ PLAYWRIGHT = "playwright"
+ SELENIUM = "selenium"
+ CUSTOM = "custom"
diff --git a/metagpt/tools/web_browser_engine.py b/metagpt/tools/web_browser_engine.py
index f38b4bd8f..629ada195 100644
--- a/metagpt/tools/web_browser_engine.py
+++ b/metagpt/tools/web_browser_engine.py
@@ -6,7 +6,7 @@ import importlib
from typing import Any, Callable, Coroutine, overload
-from metagpt.config import Config
+from metagpt.config import CONFIG
from metagpt.tools import WebBrowserEngineType
from bs4 import BeautifulSoup
@@ -17,8 +17,7 @@ class WebBrowserEngine:
engine: WebBrowserEngineType | None = None,
run_func: Callable[..., Coroutine[Any, Any, str | list[str]]] | None = None,
):
- self.config = Config()
- engine = engine or self.config.web_browser_engine
+ engine = engine or CONFIG.web_browser_engine
if engine == WebBrowserEngineType.PLAYWRIGHT:
web_browser_engine = importlib.import_module("metagpt.tools.web_browser_engine_playwright")
@@ -26,7 +25,7 @@ class WebBrowserEngine:
elif engine == WebBrowserEngineType.SELENIUM:
web_browser_engine = importlib.import_module("metagpt.tools.web_browser_engine_selenium")
run_func = web_browser_engine.SeleniumWrapper().run
- elif engine == WebBrowserEngineType.CUSTOM_ENGINE:
+ elif engine == WebBrowserEngineType.CUSTOM:
run_func = run_func
else:
raise NotImplementedError
diff --git a/metagpt/tools/web_browser_engine_playwright.py b/metagpt/tools/web_browser_engine_playwright.py
index 6546d7a29..5fef7bd1c 100644
--- a/metagpt/tools/web_browser_engine_playwright.py
+++ b/metagpt/tools/web_browser_engine_playwright.py
@@ -6,7 +6,7 @@ from pathlib import Path
import sys
from typing import Literal
from playwright.async_api import async_playwright
-from metagpt.config import Config
+from metagpt.config import CONFIG
from metagpt.logs import logger
@@ -24,16 +24,14 @@ class PlaywrightWrapper:
launch_kwargs: dict | None = None,
**kwargs,
) -> None:
- config = Config()
- self.config = config
if browser_type is None:
- browser_type = config.playwright_browser_type
+ browser_type = CONFIG.playwright_browser_type
self.browser_type = browser_type
launch_kwargs = launch_kwargs or {}
- if config.global_proxy and "proxy" not in launch_kwargs:
+ if CONFIG.global_proxy and "proxy" not in launch_kwargs:
args = launch_kwargs.get("args", [])
if not any(str.startswith(i, "--proxy-server=") for i in args):
- launch_kwargs["proxy"] = {"server": config.global_proxy}
+ launch_kwargs["proxy"] = {"server": CONFIG.global_proxy}
self.launch_kwargs = launch_kwargs
context_kwargs = {}
if "ignore_https_errors" in kwargs:
@@ -70,8 +68,8 @@ class PlaywrightWrapper:
executable_path = Path(browser_type.executable_path)
if not executable_path.exists() and "executable_path" not in self.launch_kwargs:
kwargs = {}
- if self.config.global_proxy:
- kwargs["env"] = {"ALL_PROXY": self.config.global_proxy}
+ if CONFIG.global_proxy:
+ kwargs["env"] = {"ALL_PROXY": CONFIG.global_proxy}
await _install_browsers(self.browser_type, **kwargs)
if not executable_path.exists():
parts = executable_path.parts
diff --git a/metagpt/tools/web_browser_engine_selenium.py b/metagpt/tools/web_browser_engine_selenium.py
index d5d01daf2..f36f2dfbc 100644
--- a/metagpt/tools/web_browser_engine_selenium.py
+++ b/metagpt/tools/web_browser_engine_selenium.py
@@ -5,7 +5,7 @@ import asyncio
from copy import deepcopy
import importlib
from typing import Literal
-from metagpt.config import Config
+from metagpt.config import CONFIG
import asyncio
from selenium.webdriver.common.by import By
@@ -29,14 +29,12 @@ class SeleniumWrapper:
loop: asyncio.AbstractEventLoop | None = None,
executor: futures.Executor | None = None,
) -> None:
- config = Config()
- self.config = config
if browser_type is None:
- browser_type = config.selenium_browser_type
+ browser_type = CONFIG.selenium_browser_type
self.browser_type = browser_type
launch_kwargs = launch_kwargs or {}
- if config.global_proxy and "proxy-server" not in launch_kwargs:
- launch_kwargs["proxy-server"] = config.global_proxy
+ if CONFIG.global_proxy and "proxy-server" not in launch_kwargs:
+ launch_kwargs["proxy-server"] = CONFIG.global_proxy
self.executable_path = launch_kwargs.pop("executable_path", None)
self.launch_args = [f"--{k}={v}" for k, v in launch_kwargs.items()]
diff --git a/tests/metagpt/tools/test_web_browser_engine_playwright.py b/tests/metagpt/tools/test_web_browser_engine_playwright.py
index afca35d52..908f92112 100644
--- a/tests/metagpt/tools/test_web_browser_engine_playwright.py
+++ b/tests/metagpt/tools/test_web_browser_engine_playwright.py
@@ -1,5 +1,5 @@
import pytest
-from metagpt.config import Config
+from metagpt.config import CONFIG
from metagpt.tools import web_browser_engine_playwright
@@ -15,10 +15,9 @@ from metagpt.tools import web_browser_engine_playwright
)
async def test_scrape_web_page(browser_type, use_proxy, kwagrs, url, urls, proxy, capfd):
try:
- config = Config()
- global_proxy = config.global_proxy
+ global_proxy = CONFIG.global_proxy
if use_proxy:
- config.global_proxy = proxy
+ CONFIG.global_proxy = proxy
browser = web_browser_engine_playwright.PlaywrightWrapper(browser_type, **kwagrs)
result = await browser.run(url)
assert isinstance(result, str)
@@ -32,4 +31,4 @@ async def test_scrape_web_page(browser_type, use_proxy, kwagrs, url, urls, proxy
if use_proxy:
assert "Proxy:" in capfd.readouterr().out
finally:
- config.global_proxy = global_proxy
+ CONFIG.global_proxy = global_proxy
diff --git a/tests/metagpt/tools/test_web_browser_engine_selenium.py b/tests/metagpt/tools/test_web_browser_engine_selenium.py
index 752938766..5ea1e3083 100644
--- a/tests/metagpt/tools/test_web_browser_engine_selenium.py
+++ b/tests/metagpt/tools/test_web_browser_engine_selenium.py
@@ -1,5 +1,5 @@
import pytest
-from metagpt.config import Config
+from metagpt.config import CONFIG
from metagpt.tools import web_browser_engine_selenium
@@ -15,10 +15,9 @@ from metagpt.tools import web_browser_engine_selenium
)
async def test_scrape_web_page(browser_type, use_proxy, url, urls, proxy, capfd):
try:
- config = Config()
- global_proxy = config.global_proxy
+ global_proxy = CONFIG.global_proxy
if use_proxy:
- Config().global_proxy = proxy
+ CONFIG.global_proxy = proxy
browser = web_browser_engine_selenium.SeleniumWrapper(browser_type)
result = await browser.run(url)
assert isinstance(result, str)
@@ -32,4 +31,4 @@ async def test_scrape_web_page(browser_type, use_proxy, url, urls, proxy, capfd)
if use_proxy:
assert "Proxy:" in capfd.readouterr().out
finally:
- config.global_proxy = global_proxy
+ CONFIG.global_proxy = global_proxy