mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-24 14:15:17 +02:00
feat: +unit test
fixbug: func has no value
This commit is contained in:
parent
5510df5f96
commit
6f039d004d
28 changed files with 367 additions and 552 deletions
|
|
@ -47,10 +47,10 @@ class WriteDesign(Action):
|
|||
)
|
||||
|
||||
async def run(self, with_messages: Message, schema: str = CONFIG.prompt_schema):
|
||||
# Use `git diff` to identify which PRD documents have been modified in the `docs/prds` directory.
|
||||
# Use `git status` to identify which PRD documents have been modified in the `docs/prds` directory.
|
||||
prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO)
|
||||
changed_prds = prds_file_repo.changed_files
|
||||
# Use `git diff` to identify which design documents in the `docs/system_designs` directory have undergone
|
||||
# Use `git status` to identify which design documents in the `docs/system_designs` directory have undergone
|
||||
# changes.
|
||||
system_design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO)
|
||||
changed_system_designs = system_design_file_repo.changed_files
|
||||
|
|
|
|||
|
|
@ -157,7 +157,7 @@ class BrainMemory(BaseModel):
|
|||
if left == 0:
|
||||
break
|
||||
m.content = m.content[0:left]
|
||||
msgs.append(m.model_dump())
|
||||
msgs.append(m)
|
||||
break
|
||||
msgs.append(m)
|
||||
total_length += delta
|
||||
|
|
@ -171,8 +171,8 @@ class BrainMemory(BaseModel):
|
|||
|
||||
@staticmethod
|
||||
def to_metagpt_history_format(history) -> str:
|
||||
mmsg = [SimpleMessage(role=m.role, content=m.content) for m in history]
|
||||
return json.dumps(mmsg)
|
||||
mmsg = [SimpleMessage(role=m.role, content=m.content).model_dump() for m in history]
|
||||
return json.dumps(mmsg, ensure_ascii=False)
|
||||
|
||||
async def get_title(self, llm, max_words=5, **kwargs) -> str:
|
||||
"""Generate text title"""
|
||||
|
|
|
|||
|
|
@ -132,8 +132,8 @@ class Assistant(Role):
|
|||
def get_memory(self) -> str:
|
||||
return self.memory.model_dump_json()
|
||||
|
||||
def load_memory(self, jsn):
|
||||
def load_memory(self, m):
|
||||
try:
|
||||
self.memory = BrainMemory(**jsn)
|
||||
self.memory = BrainMemory(**m)
|
||||
except Exception as e:
|
||||
logger.exception(f"load error:{e}, data:{jsn}")
|
||||
|
|
|
|||
|
|
@ -235,7 +235,9 @@ class Engineer(Role):
|
|||
task_doc = await task_file_repo.get(i.name)
|
||||
elif str(i.parent) == SYSTEM_DESIGN_FILE_REPO:
|
||||
design_doc = await design_file_repo.get(i.name)
|
||||
# FIXME: design doc没有加载进来,是None
|
||||
if not task_doc or not design_doc:
|
||||
logger.error(f'Detected source code "{filename}" from an unknown origin.')
|
||||
raise ValueError(f'Detected source code "{filename}" from an unknown origin.')
|
||||
context = CodingContext(filename=filename, design_doc=design_doc, task_doc=task_doc, code_doc=old_code_doc)
|
||||
return context
|
||||
|
||||
|
|
|
|||
|
|
@ -343,16 +343,21 @@ class MessageQueue(BaseModel):
|
|||
return "[]"
|
||||
|
||||
lst = []
|
||||
msgs = []
|
||||
try:
|
||||
while True:
|
||||
item = await wait_for(self._queue.get(), timeout=1.0)
|
||||
if item is None:
|
||||
break
|
||||
lst.append(item.dict(exclude_none=True))
|
||||
msgs.append(item)
|
||||
lst.append(item.dump())
|
||||
self._queue.task_done()
|
||||
except asyncio.TimeoutError:
|
||||
logger.debug("Queue is empty, exiting...")
|
||||
return json.dumps(lst)
|
||||
finally:
|
||||
for m in msgs:
|
||||
self._queue.put_nowait(m)
|
||||
return json.dumps(lst, ensure_ascii=False)
|
||||
|
||||
@staticmethod
|
||||
def load(data) -> "MessageQueue":
|
||||
|
|
@ -361,7 +366,7 @@ class MessageQueue(BaseModel):
|
|||
try:
|
||||
lst = json.loads(data)
|
||||
for i in lst:
|
||||
msg = Message(**i)
|
||||
msg = Message.load(i)
|
||||
queue.push(msg)
|
||||
except JSONDecodeError as e:
|
||||
logger.warning(f"JSON load failed: {data}, error:{e}")
|
||||
|
|
|
|||
|
|
@ -528,18 +528,18 @@ def role_raise_decorator(func):
|
|||
|
||||
|
||||
@handle_exception
|
||||
async def aread(file_path: str) -> str:
|
||||
async def aread(filename: str | Path, encoding=None) -> str:
|
||||
"""Read file asynchronously."""
|
||||
async with aiofiles.open(str(file_path), mode="r") as reader:
|
||||
async with aiofiles.open(str(filename), mode="r", encoding=encoding) as reader:
|
||||
content = await reader.read()
|
||||
return content
|
||||
|
||||
|
||||
async def awrite(filename: str | Path, data: str):
|
||||
async def awrite(filename: str | Path, data: str, encoding=None):
|
||||
"""Write file asynchronously."""
|
||||
pathname = Path(filename)
|
||||
pathname.parent.mkdir(parents=True, exist_ok=True)
|
||||
async with aiofiles.open(str(pathname), mode="w", encoding="utf-8") as writer:
|
||||
async with aiofiles.open(str(pathname), mode="w", encoding=encoding) as writer:
|
||||
await writer.write(data)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -49,6 +49,14 @@ def get_docstring_statement(body: DocstringNode) -> cst.SimpleStatementLine:
|
|||
return statement
|
||||
|
||||
|
||||
def has_decorator(node: DocstringNode, name: str) -> bool:
|
||||
return hasattr(node, "decorators") and any(
|
||||
(hasattr(i.decorator, "value") and i.decorator.value == name)
|
||||
or (hasattr(i.decorator, "func") and hasattr(i.decorator.func, "value") and i.decorator.func.value == name)
|
||||
for i in node.decorators
|
||||
)
|
||||
|
||||
|
||||
class DocstringCollector(cst.CSTVisitor):
|
||||
"""A visitor class for collecting docstrings from a CST.
|
||||
|
||||
|
|
@ -82,7 +90,7 @@ class DocstringCollector(cst.CSTVisitor):
|
|||
def _leave(self, node: DocstringNode) -> None:
|
||||
key = tuple(self.stack)
|
||||
self.stack.pop()
|
||||
if hasattr(node, "decorators") and any(i.decorator.value == "overload" for i in node.decorators):
|
||||
if has_decorator(node, "overload"):
|
||||
return
|
||||
|
||||
statement = get_docstring_statement(node)
|
||||
|
|
@ -127,9 +135,7 @@ class DocstringTransformer(cst.CSTTransformer):
|
|||
key = tuple(self.stack)
|
||||
self.stack.pop()
|
||||
|
||||
if hasattr(updated_node, "decorators") and any(
|
||||
(i.decorator.value == "overload") for i in updated_node.decorators
|
||||
):
|
||||
if has_decorator(updated_node, "overload"):
|
||||
return updated_node
|
||||
|
||||
statement = self.docstrings.get(key)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue