feat: +unit test

This commit is contained in:
莘权 马 2023-12-29 14:52:21 +08:00
parent 5510df5f96
commit 681068edc9
27 changed files with 357 additions and 548 deletions

View file

@ -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

View file

@ -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"""

View file

@ -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}")

View file

@ -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

View file

@ -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}")

View file

@ -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)