提交版本
This commit is contained in:
54
AlgoriAgent/projects/algoriAgent/tools/file_tools.py
Normal file
54
AlgoriAgent/projects/algoriAgent/tools/file_tools.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from agentscope.service.service_toolkit import ServiceFunction
|
||||
from agentscope.service import (
|
||||
ServiceToolkit,
|
||||
ServiceResponse,
|
||||
ServiceExecStatus,
|
||||
)
|
||||
import os
|
||||
def read_file(filepath:str, root_path:str):
|
||||
"""Read the file and return the content. Remember use 'judge' function if you want to judge file, instead of this function.
|
||||
|
||||
Args:
|
||||
filepath (`str`): The path to the file.
|
||||
root_path (`str`): The root path of now student's workspace.
|
||||
|
||||
Returns:
|
||||
content (`str`): The content of the file.
|
||||
"""
|
||||
# 从books/code_tests/name/中获取所有.in文件
|
||||
|
||||
filepath = os.path.join('..', root_path, filepath)
|
||||
try:
|
||||
with open(filepath, 'r') as f:
|
||||
content = f.read()
|
||||
except Exception as e:
|
||||
status = ServiceExecStatus.ERROR
|
||||
content = str(e)
|
||||
return ServiceResponse(status, (content))
|
||||
|
||||
status = ServiceExecStatus.SUCCESS
|
||||
return ServiceResponse(status, ('```\n'+content+'```\n'))
|
||||
|
||||
|
||||
def write_file(filepath:str, content:str, root_path:str):
|
||||
"""Write content to the file.
|
||||
|
||||
Args:
|
||||
filepath (`str`): The path to the file.
|
||||
content (`str`): The content to be written to the file.
|
||||
root_path (`str`): The root path of now student's workspace.
|
||||
|
||||
Returns:
|
||||
None.
|
||||
"""
|
||||
filepath = os.path.join('..', root_path, filepath)
|
||||
try:
|
||||
with open(filepath, 'w') as f:
|
||||
f.write(content)
|
||||
except Exception as e:
|
||||
status = ServiceExecStatus.ERROR
|
||||
return ServiceResponse(status, None)
|
||||
status = ServiceExecStatus.SUCCESS
|
||||
return ServiceResponse(status, None)
|
||||
|
||||
file_tools = [read_file, write_file]
|
||||
Reference in New Issue
Block a user