fix bugs.

This commit is contained in:
Liu Zhicong
2024-01-17 20:34:39 -08:00
parent 0bc2aaf45b
commit 9034eb175c
12 changed files with 606 additions and 472 deletions
+103 -258
View File
@@ -12,11 +12,9 @@ import datetime
import copy
import sys
from ..proto.agent_msg import AgentMsg
from ..proto.ai_function import *
from ..proto.agent_task import AgentTaskState,AgentTask,AgentTodo,AgentTodoResult
from ..proto.agent_task import AgentTaskState,AgentTask,AgentTodo, AgentTodoState
from ..proto.compute_task import *
from .agent_base import *
@@ -110,8 +108,6 @@ class AIAgent(BaseAIAgent):
self.behaviors:Dict[str,BaseLLMProcess] = {}
async def initial(self,params:Dict = None):
self.memory = AgentMemory(self.agent_id,self.chat_db)
self.prviate_workspace = AgentWorkspace(self.agent_id)
@@ -172,9 +168,6 @@ class AIAgent(BaseAIAgent):
else:
logger.error(f"load LLMProcess {process_config_name} failed!")
return False
return True
def get_id(self) -> str:
@@ -201,7 +194,6 @@ class AIAgent(BaseAIAgent):
def get_agent_prompt(self) -> LLMPrompt:
return self.agent_prompt
async def _get_context_info(self) -> Dict:
context_info = {}
@@ -252,7 +244,10 @@ class AIAgent(BaseAIAgent):
llm_process : BaseLLMProcess = self.behaviors.get("triage_tasks")
if llm_process:
if self.prviate_workspace:
tasklist = await self.prviate_workspace.task_mgr.list_task()
filter = {}
filter["state"] = AgentTaskState.TASK_STATE_WAIT
tasklist = await self.prviate_workspace.task_mgr.list_task(filter)
if tasklist:
input_parms = {
"tasklist":tasklist,
@@ -265,7 +260,7 @@ class AIAgent(BaseAIAgent):
logger.info(f"llm process triage_tasks ignore!")
else:
logger.info(f"llm process triage_tasks ok!,think is:{llm_result.resp}")
self.agent_energy -= 5
self.agent_energy -= 3
# for agent_task in tasklist:
# if self.agent_energy <= 0:
@@ -287,235 +282,76 @@ class AIAgent(BaseAIAgent):
# logger.info(f"llm process review_task ok!,think is:{determine}")
# self.agent_energy -= 1
async def _llm_run_todo_list(self, todo_list_type: TodoListType):
workspace : WorkspaceEnvironment = self.get_workspace_by_msg(None)
logger.info(f"agent {self.agent_id} do my work start!")
# review todolist
#if await self.need_review_todolist():
# await self._llm_review_todolist(workspace)
todo_list = workspace.todo_list[todo_list_type]
need_todo = await todo_list.get_todo_list(self.agent_id)
check_count = 0
do_count = 0
review_count = 0
for todo in need_todo:
if self.agent_energy <= 0:
break
do_prompts = self._can_do_todo(todo_list_type, todo)
if do_prompts:
prompt : LLMPrompt = LLMPrompt()
prompt.append(self.agent_prompt)
prompt.append(workspace.get_role_prompt(self.agent_id))
prompt.append(do_prompts)
prompt.append(todo.to_prompt())
do_result : AgentTodoResult = await self._llm_do_todo(todo, prompt, workspace)
todo.last_do_time = datetime.datetime.now().timestamp()
todo.retry_count += 1
match do_result.result_code:
case AgentTodoResult.TODO_RESULT_CODE_LLM_ERROR:
continue
case AgentTodoResult.TODO_RESULT_CODE_OK:
todo.result = do_result
await todo_list.update_todo(todo.todo_id,AgentTodo.TODO_STATE_WAITING_CHECK)
case AgentTodoResult.TODO_RESULT_CODE_EXEC_OP_ERROR:
await todo_list.update_todo(todo.todo_id,AgentTodo.TODO_STATE_EXEC_FAILED)
await todo_list.append_worklog(todo,do_result)
self.agent_energy -= 2
do_count += 1
# review_result = await self._llm_review_todo(todo,workspace)
# todo.last_review_time = datetime.datetime.now().timestamp()
continue
check_prompts = self._can_check_todo(todo_list_type, todo)
if check_prompts:
prompt : LLMPrompt = LLMPrompt()
prompt.append(self.agent_prompt)
prompt.append(workspace.get_role_prompt(self.agent_id))
prompt.append(check_prompts)
if todo.last_check_result:
prompt.append(LLMPrompt(todo.last_check_result))
prompt.append(todo.detail)
prompt.append(todo.result)
check_result: AgentTodoResult = await self._llm_check_todo(todo, prompt, workspace)
todo.last_check_time = datetime.datetime.now().timestamp()
match check_result.result_code:
case AgentTodoResult.TODO_RESULT_CODE_LLM_ERROR:
continue
case AgentTodoResult.TODO_RESULT_CODE_OK:
await todo_list.update_todo(todo.todo_id,AgentTodo.TODO_STATE_DONE)
case AgentTodoResult.TODO_RESULT_CODE_EXEC_OP_ERROR:
await todo_list.update_todo(todo.todo_id,AgentTodo.TDDO_STATE_CHECKFAILED)
await todo_list.append_worklog(todo, check_result)
self.agent_energy -= 1
check_count += 1
continue
review_prompts = self._can_review_todo(todo_list_type, todo)
if review_prompts:
prompt.append(workspace.get_prompt())
prompt.append(workspace.get_role_prompt(self.agent_id))
prompt.append(review_prompts)
todo_tree = todo_list.get_todo_tree("/")
prompt.append(LLMPrompt(todo_tree))
do_result : AgentTodoResult = await self._llm_review_todo(todo, prompt, workspace)
todo.last_review_time = datetime.datetime.now().timestamp()
match do_result.result_code:
case AgentTodoResult.TODO_RESULT_CODE_LLM_ERROR:
continue
case AgentTodoResult.TODO_RESULT_CODE_EXEC_OP_ERROR:
continue
case AgentTodoResult.TODO_RESULT_CODE_OK:
await todo_list.update_todo(todo.todo_id,AgentTodo.TODO_STATE_REVIEWED)
await todo_list.append_worklog(todo,do_result)
self.agent_energy -= 1
review_count += 1
continue
logger.info(f"agent {self.agent_id} ,check:{check_count} todo,do:{do_count} todo.")
def _can_review_todo(self, todo_list_type: TodoListType, todo:AgentTodo) -> LLMPrompt:
do_prompts = self.todo_prompts[todo_list_type].get("review")
if not do_prompts:
return None
if todo.can_review() is False:
return None
return do_prompts
def _can_check_todo(self, todo_list_type: TodoListType, todo:AgentTodo) -> LLMPrompt:
do_prompts = self.todo_prompts[todo_list_type].get("check")
if not do_prompts:
return None
if todo.can_check() is False:
return None
if todo.checker is not None:
if todo.checker != self.agent_id:
return None
else:
if self.can_do_unassigned_task is False:
return None
async def llm_do_todo(self, todo: AgentTodo):
llm_process : BaseLLMProcess = self.behaviors.get("do")
logger.info(f"agent {self.agent_id} DO todo {todo.todo_path} start!")
if llm_process:
input_parms = {
"todo":todo,
"context_info": await self._get_context_info()
}
llm_result : LLMResult = await llm_process.process(input_parms)
if llm_result.state == LLMResultStates.ERROR:
logger.error(f"llm process do_todo error:{llm_result.compute_error_str}")
elif llm_result.state == LLMResultStates.IGNORE:
logger.info(f"llm process do_todo ignore!")
else:
todo.checker = self.agent_id
logger.info(f"llm process do_todo ok!,think is:{llm_result.resp}")
self.agent_energy -= 1
return do_prompts
def _can_do_todo(self, todo_list_type: TodoListType, todo:AgentTodo) -> LLMPrompt:
do_prompts = self.todo_prompts[todo_list_type].get("do")
if not do_prompts:
return None
if todo.can_do() is False:
return None
if todo.worker is not None:
if todo.worker != self.agent_id:
return None
else:
if self.can_do_unassigned_task is False:
return None
async def llm_check_todo(self, todo: AgentTodo):
llm_process : BaseLLMProcess = self.behaviors.get("check")
logger.info(f"agent {self.agent_id} CHECK todo {todo.todo_path} start!")
if llm_process:
input_parms = {
"todo":todo,
"context_info": await self._get_context_info()
}
llm_result : LLMResult = await llm_process.process(input_parms)
if llm_result.state == LLMResultStates.ERROR:
logger.error(f"llm process check_todo error:{llm_result.compute_error_str}")
elif llm_result.state == LLMResultStates.IGNORE:
logger.info(f"llm process check_todo ignore!")
else:
todo.worker = self.agent_id
logger.info(f"llm process check_todo ok!,think is:{llm_result.resp}")
self.agent_energy -= 1
return do_prompts
return
async def _llm_do_todo(self, todo: AgentTodo, prompt: LLMPrompt, workspace: WorkspaceEnvironment) -> AgentTodoResult:
result = AgentTodoResult()
task_result:ComputeTaskResult = await self.do_llm_complection(prompt, is_json_resp=True)
if task_result.error_str is not None:
logger.error(f"_llm_do compute error:{task_result.error_str}")
result.result_code = AgentTodoResult.TODO_RESULT_CODE_LLM_ERROR
result.error_str = task_result.error_str
return result
async def llm_plan_task(self,task:AgentTask):
llm_process : BaseLLMProcess = self.behaviors.get("plan_task")
logger.info(f"agent {self.agent_id} PLAN task {task.task_path} start!")
if llm_process:
input_parms = {
"task":task,
"context_info": await self._get_context_info()
}
llm_result : LLMResult = await llm_process.process(input_parms)
if llm_result.state == LLMResultStates.ERROR:
logger.error(f"llm process plan_task error:{llm_result.compute_error_str}")
elif llm_result.state == LLMResultStates.IGNORE:
logger.info(f"llm process plan_task ignore!")
else:
logger.info(f"llm process plan_task ok!,think is:{llm_result.resp}")
self.agent_energy -= 1
llm_result = LLMResult.from_str(task_result.result_str)
# result_str is the explain of how to do this todo
result.result_str = llm_result.resp
result.op_list = llm_result.op_list
if llm_result.post_msgs is not None:
for msg in llm_result.post_msgs:
msg.sender = self.agent_id
msg.topic = f"{todo.title}##{todo.todo_id}"
#msg.prev_msg_id = todo.todo_id
chatsession = AIChatSession.get_session(self.agent_id,f"{msg.target}#{msg.topic}",self.chat_db)
chatsession.append(msg)
resp = await AIBus.get_default_bus().post_message(msg)
logging.info(f"agent {self.agent_id} send msg to {msg.target} result:{resp}")
async def llm_review_task(self,task:AgentTask):
llm_process : BaseLLMProcess = self.behaviors.get("review_task")
logger.info(f"agent {self.agent_id} REVIEW task {task.task_path} start!")
if llm_process:
input_parms = {
"task":task,
"context_info": await self._get_context_info()
}
llm_result : LLMResult = await llm_process.process(input_parms)
if llm_result.state == LLMResultStates.ERROR:
logger.error(f"llm process review_task error:{llm_result.compute_error_str}")
elif llm_result.state == LLMResultStates.IGNORE:
logger.info(f"llm process review_task ignore!")
else:
logger.info(f"llm process review_task ok!,think is:{llm_result.resp}")
self.agent_energy -= 1
result_str, have_error = await workspace.exec_op_list(llm_result.action_list, self.agent_id)
if have_error:
result.result_code = AgentTodoResult.TODO_RESULT_CODE_EXEC_OP_ERROR
#result.error_str = error_str
return result
result.result_str = result_str
return result
async def _llm_check_todo(self, todo: AgentTodo, prompt: LLMPrompt, workspace: WorkspaceEnvironment) -> AgentTodoResult:
result = AgentTodoResult()
inner_functions,_ = BaseAIAgent.get_inner_functions(workspace)
task_result:ComputeTaskResult = await self.do_llm_complection(prompt,inner_functions=inner_functions,is_json_resp=True)
if task_result.error_str is not None:
logger.error(f"_llm_do compute error:{task_result.error_str}")
result.result_code = AgentTodoResult.TODO_RESULT_CODE_LLM_ERROR
result.error_str = task_result.error_str
return result
result.result_str = task_result.result_str
todo.last_check_result = task_result.result_str
return result
async def _llm_review_todo(self, todo:AgentTodo, prompt: LLMPrompt, workspace: WorkspaceEnvironment):
inner_functions,_ = BaseAIAgent.get_inner_functions(workspace)
task_result:ComputeTaskResult = await self.do_llm_complection(prompt,inner_functions=inner_functions)
if task_result.result_code != ComputeTaskResultCode.OK:
logger.error(f"_llm_review_todos compute error:{task_result.error_str}")
return
return
# async def do_blance_knowledge_base(selft):
# # 整理自己的知识库(让分类更平衡,更由于自己以后的工作),并尝试更新学习目标
# current_path = "/"
# current_list = kb.get_list(current_path)
# self_assessment_with_goal = self.get_self_assessment_with_goal()
# learn_goal = {}
# llm_blance_knowledge_base(current_path,current_list,self_assessment_with_goal,learn_goal,learn_power)
# # 主动学习
# # 方法目前只有使用搜索引擎一种?
# for goal in learn_goal.items():
# self.llm_learn_with_search_engine(kb,goal,learn_power)
# if learn_power <= 0:
# break
async def do_self_think(self):
session_id_list = AIChatSession.list_session(self.agent_id,self.chat_db)
@@ -524,34 +360,24 @@ class AIAgent(BaseAIAgent):
break
used_energy = await self.think_chatsession(session_id)
self.agent_energy -= used_energy
# todo_logs = await self.get_todo_logs()
# for todo_log in todo_logs:
# if self.agent_energy <= 0:
# break
# used_energy = await self.think_todo_log(todo_log)
# self.agent_energy -= used_energy
return
#async def think_todo_log(self,todo_log:AgentWorkLog):
# pass
def need_self_think(self) -> bool:
return False
async def _self_imporve(self):
if self.need_self_think():
await self.do_self_think()
def wake_up(self) -> None:
if self.agent_task is None:
self.agent_task = asyncio.create_task(self._on_timer())
else:
logger.warning(f"agent {self.agent_id} is already wake up!")
# agent loop
async def _on_timer(self):
while True:
await asyncio.sleep(15)
await asyncio.sleep(5)
try:
now = time.time()
if self.last_recover_time is None:
@@ -565,17 +391,36 @@ class AIAgent(BaseAIAgent):
continue
await self.llm_triage_tasklist()
# complete & check todo
#await self._llm_run_todo_list(TodoListType.TO_WORK)
##await self._llm_run_todo_list(TodoListType.TO_LEARN)
if self.need_self_think():
await self.do_self_think()
task_list:List[AgentTask] = await self.prviate_workspace.task_mgr.list_task()
for task in task_list:
if self.agent_energy <= 0:
break
if task.can_plan():
# PLAN Task
await self.llm_plan_task(task)
if task.state == AgentTaskState.TASK_STATE_DOING:
# DO or Check Todo
can_review = False
todolist = await self.prviate_workspace.task_mgr.get_sub_todos(task.task_id)
for todo in todolist:
if self.agent_energy <= 0:
break
if todo.state == AgentTodoState.TODO_STATE_WAITING or todo.state == AgentTodoState.TODO_STATE_EXEC_FAILED:
await self.llm_do_todo(todo)
if todo.state == AgentTodoState.TODO_STATE_EXEC_OK:
await self.llm_check_todo(todo)
if can_review:
task.state = AgentTaskState.TASK_STATE_WAITING_REVIEW
if task.state == AgentTaskState.TASK_STATE_WAITING_REVIEW:
await self.llm_review_task(task)
await self._self_imporve()
# review other's todo
# self.review_other_works()
except Exception as e:
tb_str = traceback.format_exc()
logger.error(f"agent {self.agent_id} on timer error:{e},{tb_str}")
+3 -3
View File
@@ -135,7 +135,7 @@ class AgentMemory:
c.execute(query, tuple(params))
rows = c.fetchall()
conn.close()
return [self.from_db_row(row) for row in rows]
@@ -154,7 +154,7 @@ class AgentMemory:
)
''')
conn.commit()
conn.close()
#conn.close()
@classmethod
def from_db_row(self,row):
@@ -174,7 +174,7 @@ class AgentMemory:
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (log.logid, log.owner_id, log.work_type, log.timestamp, log.content, log.result, meta_str, log.operator))
conn.commit()
conn.close()
#conn.close()
async def get_contact_summary(self,contact_id:str) -> str:
if contact_id is None:
+9
View File
@@ -19,6 +19,9 @@ class LLMProcessContext:
@staticmethod
def aifunctions_to_inner_functions(all_inner_function:List[AIFunction]) -> List[Dict]:
if all_inner_function is None:
return []
result_func = []
result_len = 0
for inner_func in all_inner_function:
@@ -279,6 +282,9 @@ class SimpleLLMContext(LLMProcessContext):
return None
def get_function_set(self,set_name:str = None) -> List[AIFunction]:
if self.functions is None:
return None
if set_name is None:
return self.functions.values()
else:
@@ -296,6 +302,9 @@ class SimpleLLMContext(LLMProcessContext):
return None
def get_action_set(self,set_name:str = None) -> List[AIFunction]:
if self.actions is None:
return None
if set_name is None:
return self.actions.values()
else:
+5 -4
View File
@@ -268,9 +268,10 @@ class LLMAgentBaseProcess(BaseLLMProcess):
#prompt.append_system_message(self.reply_format)
## Context
context = self._format_content_by_env_value(self.context,context_info)
system_prompt_dict["context"] = context
#prompt.append_system_message(context)
if self.context:
context = self._format_content_by_env_value(self.context,context_info)
system_prompt_dict["context"] = context
#prompt.append_system_message(context)
system_prompt_dict["support_actions"] = self.get_action_desc()
@@ -371,7 +372,7 @@ class AgentMessageProcess(LLMAgentBaseProcess):
return await self.memory.load_chatlogs(msg)
async def get_log_summary(self,msg:AgentMsg)->str:
return await self.memory.get_log_summary(msg)
return None
async def get_extend_known_info(self,msg:AgentMsg,prompt:LLMPrompt)->str:
+235 -25
View File
@@ -87,7 +87,7 @@ class LocalAgentTaskManger(AgentTaskManager):
async def set_todos(self,owner_task_id:str,todos:List[AgentTodo]):
async def set_todos(self,owner_task_id:str,todos:List[Dict]):
owner_task_path = self._get_obj_path(owner_task_id)
if owner_task_path is None:
return f"owner task {owner_task_id} not found"
@@ -107,12 +107,13 @@ class LocalAgentTaskManger(AgentTaskManager):
try:
step_order = 0
for todo in todos:
todo.step_order = step_order
todo.owner_taskid = owner_task_id
todo_path = f"{self.root_path}/{owner_task_path}/#{step_order} {todo.title}.todo"
self._save_obj_path(todo.todo_id,todo_path)
todo_obj = AgentTodo.from_dict(todo)
todo_obj.step_order = step_order
todo_obj.owner_taskid = owner_task_id
todo_path = f"{self.root_path}/{owner_task_path}/#{step_order} {todo_obj.title}.todo"
self._save_obj_path(todo_obj.todo_id,todo_path)
async with aiofiles.open(todo_path, mode='w', encoding="utf-8") as f:
await f.write(json.dumps(todo.to_dict(),ensure_ascii=False))
await f.write(json.dumps(todo_obj.to_dict(),ensure_ascii=False))
logger.info("create_todos at %s OK!",todo_path)
step_order += 1
except Exception as e:
@@ -212,9 +213,10 @@ class LocalAgentTaskManger(AgentTaskManager):
async def get_sub_tasks(self,task_id:str) -> List[AgentTask]:
task_path = self._get_obj_path(task_id)
if task_path is None:
return []
task_path = f"{self.root_path}/{task_path}"
sub_tasks = []
for sub_item in os.listdir(task_path):
if sub_item.startswith("."):
@@ -235,23 +237,36 @@ class LocalAgentTaskManger(AgentTaskManager):
task_path = self._get_obj_path(task_id)
if task_path is None:
return []
task_path = f"{self.root_path}/{task_path}"
sub_todos = []
for sub_item in os.listdir(task_path):
if sub_item.startswith("."):
continue
if sub_item == "workspace":
continue
if sub_item == "details":
continue
full_path = os.path.join(task_path, sub_item)
if os.path.isfile(full_path) and sub_item.endswith(".todo"):
sub_todo = await self.get_todo_by_path(f"{task_path}/{sub_item}")
sub_todo = await self.get_todo_by_path(full_path)
if sub_todo:
sub_todos.append(sub_todo)
return sub_todos
async def get_todo_by_path(self,todo_path:str) -> AgentTodo:
async with aiofiles.open(todo_path, mode='r', encoding="utf-8") as f:
s = await f.read()
todo_dict = json.loads(s)
result_todo:AgentTodo = AgentTodo.from_dict(todo_dict)
if result_todo:
result_todo.todo_path = todo_path
else:
logger.error("get_todo_by_path:%s,parse failed!",todo_path)
return result_todo
#async def get_task_depends(self,task_id:str) -> List[AgentTask]:
# pass
@@ -259,6 +274,10 @@ class LocalAgentTaskManger(AgentTaskManager):
async def list_task(self,filter:Optional[dict] = None ) -> List[AgentTask]:
directory_path = self.root_path
result_list:List[AgentTask] = []
special_state = None
if filter:
special_state = filter.get("state")
#agent_id = filter.get("agent_id")
for entry in os.scandir(directory_path):
if not entry.is_dir():
@@ -269,8 +288,15 @@ class LocalAgentTaskManger(AgentTaskManager):
continue
task_item = await self._get_task_by_fullpath(entry.path)
if task_item:
if not task_item.is_finish():
result_list.append(task_item)
if task_item.is_finish():
continue
if special_state:
if task_item.state != special_state:
continue
result_list.append(task_item)
return result_list
@@ -309,22 +335,85 @@ class LocalAgentTaskManger(AgentTaskManager):
# pass
#todo共享其所在task的文件夹
# if task_id is none, means root folder in workspace
def _get_taskfile_path(self,task_id:str,path:str)->str:
root_path = self.root_path
if task_id is None:
root_path = f"{root_path}/workspace"
else:
task_path = self._get_obj_path(task_id)
if task_path is None:
return None
root_path = f"{task_path}/wrorkspace"
file_path = f"{root_path}/{path}"
return file_path
async def get_task_file(self,task_id:str,path:str)->str:
#return fileid
pass
async def read_task_file(self,task_id:str,path:str)->str:
file_path = self._get_taskfile_path(task_id,path)
if not os.path.exists(file_path):
return None
try:
async with aiofiles.open(file_path, mode='r', encoding="utf-8") as f:
content = await f.read()
return content
except Exception as e:
logger.error("read_task_file failed:%s",e)
return None
async def write_task_file(self,task_id:str,path:str,content:str):
file_path = self._get_taskfile_path(task_id,path)
# write file
try:
dir_name = os.path.dirname(file_path)
os.makedirs(dir_name)
async with aiofiles.open(file_path, mode='w', encoding="utf-8") as f:
await f.write(content)
except Exception as e:
logger.error("write_task_file failed:%s",e)
return str(e)
async def append_task_file(self,task_id:str,path:str,content:str):
file_path = self._get_taskfile_path(task_id,path)
# append file
try:
async with aiofiles.open(file_path, mode='a', encoding="utf-8") as f:
await f.write(content)
except Exception as e:
logger.error("append_task_file failed:%s",e)
return str(e)
async def list_task_dir(self,task_id:str,path:str) -> List[str]:
dir_path = self._get_taskfile_path(task_id,path)
if not os.path.exists(dir_path):
return None
try:
result_node = os.listdir(dir_path)
result = []
for name in result_node:
if name.startswith("."):
continue
result.append(name)
return result
except Exception as e:
logger.error("list_task_dir failed:%s",e)
return None
async def set_task_file(self,task_id:str,path:str,fileid:str):
pass
async def list_task_file(self,task_id:str,path:str):
pass
async def remove_task_file(self,task_id:str,path:str):
pass
file_path = self._get_taskfile_path(task_id,path)
try:
os.remove(file_path)
except Exception as e:
logger.error("remove_task_file failed:%s",e)
return str(e)
return None
@@ -448,7 +537,7 @@ class AgentWorkspace:
task : AgentTask = await _workspace.task_mgr.get_task(task_id)
if task is None:
return f"task {task_id} not found"
task.state = "cancel"
task.state = AgentTaskState.TASK_STATE_CANCEL
await _workspace.task_mgr.update_task(task)
return "canncel task ok"
@@ -464,6 +553,34 @@ class AgentWorkspace:
GlobaToolsLibrary.get_instance().register_tool_function(cancel_task_action)
async def confirm_task(parameters):
_workspace = parameters.get("_workspace")
if _workspace is None:
return "_workspace not found"
task_id = parameters.get("task_id")
task : AgentTask = await _workspace.task_mgr.get_task(task_id)
if task is None:
return f"task {task_id} not found"
if parameters.get("priority"):
task.priority = parameters.get("priority")
if parameters.get("next_do_date"):
task.next_do_date = parameters.get("next_do_date")
task.state = AgentTaskState.TASK_STATE_CONFIRMED
await _workspace.task_mgr.update_task(task)
return "confirm task ok"
parameters = ParameterDefine.create_parameters({
"task_id": {"type": "string", "description": "task id which want to confirm"},
"next_do_date": {"type": "string", "description": "optional,confirm task next do date"},
"priority": {"type": "int", "description": "optional,task priority from 1-10"},
})
confirm_task_action = SimpleAIFunction(
"agent.workspace.confirm_task",
"Confirm this task",
confirm_task,
parameters
)
GlobaToolsLibrary.get_instance().register_tool_function(confirm_task_action)
async def list_task(parameters):
_workspace = parameters.get("_workspace")
if _workspace is None:
@@ -565,3 +682,96 @@ class AgentWorkspace:
"update todo to new state",
update_todo,parameters)
GlobaToolsLibrary.get_instance().register_tool_function(update_todo_ai_function)
# write file
async def write_task_file(parameters):
_workspace : AgentWorkspace= parameters.get("_workspace")
if _workspace is None:
return "_workspace not found"
task_id = parameters.get("task_id")
path = parameters.get("filename")
content = parameters.get("content")
await _workspace.task_mgr.write_task_file(task_id,path,content)
return "write task file ok"
parameters = ParameterDefine.create_parameters({
"filename": {"type": "string", "description": "filename"},
"content": {"type": "string", "description": "file content"},
})
write_task_file_ai_function = SimpleAIFunction("agent.workspace.write_file",
"write file for task",
write_task_file,parameters)
GlobaToolsLibrary.get_instance().register_tool_function(write_task_file_ai_function)
# append file
async def append_task_file(parameters):
_workspace : AgentWorkspace= parameters.get("_workspace")
if _workspace is None:
return "_workspace not found"
task_id = parameters.get("task_id")
path = parameters.get("filename")
content = parameters.get("content")
await _workspace.task_mgr.append_task_file(task_id,path,content)
return "append task file ok"
parameters = ParameterDefine.create_parameters({
"filename": {"type": "string", "description": "filename"},
"content": {"type": "string", "description": "file content"},
})
append_task_file_ai_function = SimpleAIFunction("agent.workspace.append_file",
"append file for task",
append_task_file,parameters)
GlobaToolsLibrary.get_instance().register_tool_function(append_task_file_ai_function)
# read file
async def read_task_file(parameters):
_workspace : AgentWorkspace= parameters.get("_workspace")
if _workspace is None:
return "_workspace not found"
task_id = parameters.get("task_id")
path = parameters.get("filename")
content = await _workspace.task_mgr.read_task_file(task_id,path)
return content
parameters = ParameterDefine.create_parameters({
"filename": {"type": "string", "description": "filename"},
})
read_task_file_ai_function = SimpleAIFunction("agent.workspace.read_file",
"read file for task",
read_task_file,parameters)
GlobaToolsLibrary.get_instance().register_tool_function(read_task_file_ai_function)
# list dir
async def list_task_dir(parameters):
_workspace : AgentWorkspace= parameters.get("_workspace")
if _workspace is None:
return "_workspace not found"
task_id = parameters.get("task_id")
path = parameters.get("path")
content = await _workspace.task_mgr.list_task_dir(task_id,path)
return content
parameters = ParameterDefine.create_parameters({
"path": {"type": "string", "description": "The relative path of the dir"},
})
list_task_dir_ai_function = SimpleAIFunction("agent.workspace.list_dir",
"list dir in task workspace",
list_task_dir,parameters)
GlobaToolsLibrary.get_instance().register_tool_function(list_task_dir_ai_function)
# remove file
async def remove_task_file(parameters):
_workspace : AgentWorkspace= parameters.get("_workspace")
if _workspace is None:
return "_workspace not found"
task_id = parameters.get("task_id")
path = parameters.get("filename")
content = await _workspace.task_mgr.remove_task_file(task_id,path)
return content
parameters = ParameterDefine.create_parameters({
"filename": {"type": "string", "description": "filename"},
})
remove_task_file_ai_function = SimpleAIFunction("agent.workspace.remove_file",
"remove file for task",
remove_task_file,parameters)
GlobaToolsLibrary.get_instance().register_tool_function(remove_task_file_ai_function)