fix plan & task bugs, add task experience, update editor & browser, test issue fixing ability

This commit is contained in:
garylin2099 2024-05-13 15:11:43 +08:00
parent 91b11a2ab9
commit cb6484d01d
9 changed files with 102 additions and 29 deletions

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from playwright.async_api import async_playwright
from metagpt.const import DEFAULT_WORKSPACE_ROOT
@ -5,10 +7,10 @@ from metagpt.tools.tool_registry import register_tool
from metagpt.utils.report import BrowserReporter
@register_tool()
@register_tool(tags=["web", "browse", "scrape"])
class Browser:
"""
A tool for browsing the web. Don't initialize a new instance of this class if one already exists.
A tool for browsing the web and scraping. Don't initialize a new instance of this class if one already exists.
Note: Combine searching and scrolling together to achieve most effective browsing. DON'T stick to one method.
"""
@ -31,7 +33,7 @@ class Browser:
self.current_page = page
self.current_page_url = url
print("Now on page ", url)
print(await self._view())
await self._view()
async def open_new_page(self, url: str):
"""open a new page in the browser and view the page"""
@ -51,6 +53,12 @@ class Browser:
else:
print(f"Page not found: {url}")
async def _view_page_html(self, keep_len: int = 5000) -> str:
"""view the HTML content of current page, return the HTML content as a string. When executed, the content will be printed out"""
html = await self.current_page.content()
html_content = html.strip()[:keep_len]
return html_content
async def search_content_all(self, search_term: str) -> list[dict]:
"""search all occurences of search term in the current page and return the search results with their position.
Useful if you have a keyword or sentence in mind and want to quickly narrow down the content relevant to it.
@ -142,10 +150,12 @@ class Browser:
await self.current_page.screenshot(path=path)
print(f"Screenshot saved to: {path}")
async def _view(self) -> str:
async def _view(self, keep_len: int = 5000) -> str:
"""simulate human viewing the current page, return the visible text with links"""
visible_text_with_links = await self.current_page.evaluate(VIEW_CONTENT_JS)
return visible_text_with_links
print("The visible text and their links (if any): ", visible_text_with_links[:keep_len])
# html_content = await self._view_page_html(keep_len=keep_len)
# print("The html content: ", html_content)
async def scroll_current_page(self, offset: int = 500):
"""scroll the current page by offset pixels, negative value means scrolling up, will print out observed content after scrolling"""

View file

@ -16,7 +16,9 @@ class FileBlock(BaseModel):
block_start_line: int
block_end_line: int
symbol: str = Field(default="", description="The symbol of interest in the block, empty if not applicable.")
symbol_line: int = Field(default=-1, description="The line number of the symbol in the file, -1 if not applicable")
symbol_start_line: int = Field(
default=-1, description="The line number of the symbol in the file, -1 if not applicable"
)
@register_tool()
@ -57,7 +59,7 @@ class Editor:
block_start_line: int
block_end_line: int
symbol: str = Field(default="", description="The symbol of interest in the block, empty if not applicable.")
symbol_line: int = Field(default=-1, description="The line number of the symbol in the file, -1 if not applicable")
symbol_start_line: int = Field(default=-1, description="The line number of the symbol in the file, -1 if not applicable")
"""
if not os.path.exists(root_path):
print(f"Currently at {os.getcwd()}. Path {root_path} does not exist.")
@ -83,7 +85,7 @@ class Editor:
block_start_line=start + 1,
block_end_line=end + 1,
symbol=symbol,
symbol_line=i + 1,
symbol_start_line=i + 1,
)
self.resource.report(result.file_path, "path")
return result
@ -98,7 +100,7 @@ class Editor:
1. If the new block content is empty, the original block will be deleted.
2. If the new block content is not empty and end_line < start_line (e.g. set end_line = -1) the new block content will be inserted at start_line.
3. If the new block content is not empty and end_line >= start_line, the original block from start_line to end_line (both inclusively) will be replaced by the new block content.
This function can sometimes be used given a FileBlock upstream. Think carefully if you want to use block_start_line or symbol_line in the FileBlock as your start_line input.
This function can sometimes be used given a FileBlock upstream. Think carefully if you want to use block_start_line or symbol_start_line in the FileBlock as your start_line input. Your new_block_content will be placed at the start_line.
Args:
file_path (str): The file path to write the new block content.

View file

@ -57,7 +57,7 @@ async def git_push(
return branch
@register_tool(tags=["software development", "git", "create a git pull/merge request"])
@register_tool(tags=["software development", "git", "create a git pull request or merge request"])
async def git_create_pull(
base: str,
head: str,