diff --git a/buildin/agents/english-teacher/agent.cfg b/buildin/agents/english-teacher/agent.cfg new file mode 100644 index 0000000..e69de29 diff --git a/doc/mvp/pkg_system_demo/projectA/.pkg/pkg_nameB.zip b/doc/mvp/pkg_system_demo/projectA/.pkg/pkg_nameB.zip new file mode 100644 index 0000000..15cb0ec Binary files /dev/null and b/doc/mvp/pkg_system_demo/projectA/.pkg/pkg_nameB.zip differ diff --git a/doc/mvp/pkg_system_demo/projectA/pkg.cfg.toml b/doc/mvp/pkg_system_demo/projectA/pkg.cfg.toml new file mode 100644 index 0000000..e69de29 diff --git a/doc/mvp/pkg_system_demo/projectA/pkg.lock b/doc/mvp/pkg_system_demo/projectA/pkg.lock new file mode 100644 index 0000000..e69de29 diff --git a/doc/mvp/pkg_system_demo/projectA/pkg_index.toml b/doc/mvp/pkg_system_demo/projectA/pkg_index.toml new file mode 100644 index 0000000..e69de29 diff --git a/src/component/agent_manager/__init__.py b/src/component/agent_manager/__init__.py new file mode 100644 index 0000000..863c4d1 --- /dev/null +++ b/src/component/agent_manager/__init__.py @@ -0,0 +1,3 @@ +from .agent_manager import agent_manager,agent_manager_client +from .agent import ai_agent +from .templete import ai_agent_templete \ No newline at end of file diff --git a/src/component/agent_manager/agent.py b/src/component/agent_manager/agent.py new file mode 100644 index 0000000..26f8b71 --- /dev/null +++ b/src/component/agent_manager/agent.py @@ -0,0 +1,3 @@ +class ai_agent: + def __init__(self) -> None: + pass \ No newline at end of file diff --git a/src/component/agent_manager/agent_manager.py b/src/component/agent_manager/agent_manager.py new file mode 100644 index 0000000..d837175 --- /dev/null +++ b/src/component/agent_manager/agent_manager.py @@ -0,0 +1,40 @@ + +from .agent import ai_agent +from .templete import ai_agent_templete + +class agent_manager: + _instance = None + + def __init__(self) -> None: + pass + + def __new__(cls): + if cls._instance is None: + cls._instance = super(agent_manager, cls).__new__(cls) + return cls._instance + + + def initial(self,root_dir:str) -> None: + pass + + + def get(self,agent_id) -> ai_agent: + pass + + + def get_templete(self,templete_id) -> ai_agent_templete: + pass + + def install(self,templete_id) -> int: + + installer = None + #installer.install(templete_id) + pass + + def create(self,template,agent_name,agent_last_name,agent_introduce) -> ai_agent: + pass + + +class agent_manager_client: + def __init__(self) -> None: + pass \ No newline at end of file diff --git a/src/component/agent_manager/templete.py b/src/component/agent_manager/templete.py new file mode 100644 index 0000000..87540bf --- /dev/null +++ b/src/component/agent_manager/templete.py @@ -0,0 +1,3 @@ +class ai_agent_templete: + def __init__(self) -> None: + pass \ No newline at end of file diff --git a/src/component/ndn_client/__init__.py b/src/component/ndn_client/__init__.py new file mode 100644 index 0000000..6b4585c --- /dev/null +++ b/src/component/ndn_client/__init__.py @@ -0,0 +1 @@ +from .cid import content_id \ No newline at end of file diff --git a/src/component/ndn_client/cid.py b/src/component/ndn_client/cid.py new file mode 100644 index 0000000..981df3b --- /dev/null +++ b/src/component/ndn_client/cid.py @@ -0,0 +1,8 @@ + + +class content_id: + def __init__(self) -> None: + pass + + def create_from_str(cid_str:str): + pass \ No newline at end of file diff --git a/src/component/package_manager/__init__.py b/src/component/package_manager/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/component/package_manager/env.py b/src/component/package_manager/env.py new file mode 100644 index 0000000..a5a463b --- /dev/null +++ b/src/component/package_manager/env.py @@ -0,0 +1,124 @@ + +import logging +import toml + +from .pkg import pkg_info,pkg_media_info +from .installer import pkg_installer + +logger = logging.getLogger(__name__) + +#pkg_env_mgr,以cfg_path为key管理多个pkg_env,是个单件 +class pkg_env_mgr: + _instance = None + def __new__(cls): + if cls._instance is None: + cls._instance = super(pkg_env_mgr, cls).__new__(cls) + return cls._instance + + def __init__(self) -> None: + self._pkg_envs = {} + pass + + def get_env(self,cfg_path:str) -> pkg_env: + if cfg_path in self._pkg_envs: + return self._pkg_envs[cfg_path] + else: + pkg_env = pkg_env(cfg_path) + self._pkg_envs[cfg_path] = pkg_env + return pkg_env + + def get_user_env(self) -> pkg_env: + pass + + def get_system_env(self) -> pkg_env: + pass + +class pkg_env: + def __init__(self,cfg_path:str,inheritance = True) -> None: + self.prefixs : list[str]= [] + self.is_strict : bool = True + self.parent_envs : list[pkg_env] = [pkg_env_mgr().get_user_env()] + self.index_dbs = None + + self.cfg_path = cfg_path + self._load_pkg_cfg(cfg_path) + pass + + def load(self,pkg_name:str,cid:None) -> pkg_media_info: + if cid is None: + #lookup pkg's cid from indexDb + pkg_id,verion_str,cid = pkg_info.parse_pkg_name(pkg_name) + if cid is None: + #lookup include pkg-index-db search + pkg_info = self.lookup(pkg_id,verion_str) + if pkg_info is not None: + return self.load(pkg_id,pkg_info.cid) + + if self.is_strict is False: + #load pkg not in index-db, easy for debug + for prefix in self.prefixs: + fullpath = prefix + pkg_name + logger.debug("try load {pkg_name} at {fullpath} ...") + if pkg_env.is_valied_media(fullpath): + return self.load_pkg_media_info(fullpath) + + logger.warn(f"load {pkg_name} failed: pkg not found in index-db and pkg_dirs") + return None + else: + #got cid here~ + return self.load(pkg_id,cid) + else: + #search pkg_id#cid and load pkg_media_info + for prefix in self.prefixs: + fullpath =f"{prefix}{pkg_id}#{cid}" + logger.debug(f"try load {pkg_name} from {fullpath}") + if pkg_env.is_valied_media(fullpath): + media_info = self.load_pkg_media_info(fullpath) + return media_info + + logger.warn(f"load {pkg_id}#{cid} error,not found") + + + def get_pkg_media_info(self,pkg_name:str)->pkg_media_info: + pass + + def load_pkg_media_info(self,pkg_full_path:str) -> pkg_media_info: + pass + + + def lookup(self,pkg_id:str,version_str:str) -> pkg_info: + + pass + + def get_installer(self) -> pkg_installer: + pass + + @classmethod + def is_valied_media(pkg_full_path:str) -> bool: + pass + + + def _load_pkg_cfg(self,cfg_path:str): + if cfg_path is None: + return + + cfg = None + if len(cfg_path) < 1: + return + try: + cfg = toml.load(cfg_path) + except Exception as e: + logger.error(f"read pkg cfg from {cfg_path} failed! unexpected error occurred: {str(e)}") + return + + if cfg: + if cfg.env: + if cfg.env.is_strict is not None: + self.is_strict = cfg.env.is_strict + + if cfg.env.prefixs is not None: + self.prefixs = self._preprocess_prefixs(cfg.env.prefixs) + + + def _preprocess_prefixs(self,prefixs): + pass \ No newline at end of file diff --git a/src/component/package_manager/index_db.py b/src/component/package_manager/index_db.py new file mode 100644 index 0000000..e69de29 diff --git a/src/component/package_manager/index_syncer.py b/src/component/package_manager/index_syncer.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/component/package_manager/index_syncer.py @@ -0,0 +1 @@ + diff --git a/src/component/package_manager/installer.py b/src/component/package_manager/installer.py new file mode 100644 index 0000000..83ed220 --- /dev/null +++ b/src/component/package_manager/installer.py @@ -0,0 +1,102 @@ +# installer download pkg by cid, than install it to target dir +import logging +import asyncio +import aiohttp +import aiofiles + +from threading import RLock + +from ndn_client import content_id +from .pkg import pkg_info +from .env import pkg_env + +logger = logging.getLogger(__name__) + +INSTALL_TASK_STATE_DONE = 0 +INSTALL_TASK_STATE_CHECK_DEPENDENCY = 1 +INSTALL_TASK_STATE_INSTALL_DEPENDENCY = 2 +INSTALL_TASK_STATE_DOWNLOADING = 3 +INSTALL_TASK_STATE_INSTALLING = 4 +INSTALL_TAKS_STATE_ERROR = 5 + +class install_task: + def __init__(self,owner:pkg_env) -> None: + self.owner = owner + self.state = INSTALL_TASK_STATE_CHECK_DEPENDENCY + self.working_task = None + self.error_str = None + + + +class pkg_installer: + def __init__(self,owner_env:pkg_env) -> None: + self.all_tasks = {} + self.owner_env = owner_env + + def install(self,pkg_name:str, + install_from_dependency = False, can_upgrade = True,options = None)->Tuple[install_task,str]: + media_info = self.owner_env.get_media_info(pkg_name) + is_upgrade = False + if media_info is not None: + is_upgrade = True + if can_upgrade is False: + error_str = f"pkg:{pkg_name} already installed and can't upgrade" + logger.error(error_str) + return None,error_str + + pkg_id,version_str,content_id = pkg_info.parse_pkg_name(pkg_name) + + the_pkg_info = self.owner_env.lookup(pkg_id,version_str,content_id) + if the_pkg_info is None: + error_str = f"pkg:{pkg_name} ,content_id:{content_id} not found in index db" + logger.error(error_str) + return None,error_str + + result_task = self.all_tasks.get(the_pkg_info.cid) + if result_task is not None: + return result_task,None + + result_task = install_task() + self.all_tasks[the_pkg_info.cid] = result_task + async def install_pkg(): + # check dependency + if await self.check_dependency(the_pkg_info) is False: + error_str = f"pkg:{pkg_name} check dependency failed" + logger.error(error_str) + result_task.error_str = error_str + result_task.state = INSTALL_TAKS_STATE_ERROR + return + + # install dependency + result_task.state = INSTALL_TASK_STATE_INSTALL_DEPENDENCY + for depend_pkg_name in the_pkg_info.depends: + # TODO:这里可能需要用安装队列,直接等待安装完成可能会太慢,也可能会导致循环等待 + sub_task,err_str = self.install(depend_pkg_name,True,can_upgrade,options) + + + result_task.working_task = asyncio.create_task(install_pkg()) + return result_task,None + + + def uninstall(self): + pass + + async def check_dependency(self,pkg:pkg_info) -> bool: + #TODO:这里可能会有循环依赖导致的递归无法退出的问题,需要记录已经检查过的pkg + for depend_pkg_name in pkg.depends: + depend_pkg_info = self.owner_env.lookup(depend_pkg_name) + if depend_pkg_info is None: + logger.warn(f"{pkg.name}'s depend pkg {depend_pkg_name} not found in index db") + return False + + if await self.check_dependency(depend_pkg_info) is False: + return False + + return True + + + + + + + diff --git a/src/component/package_manager/media_reader.py b/src/component/package_manager/media_reader.py new file mode 100644 index 0000000..e69de29 diff --git a/src/component/package_manager/pkg.py b/src/component/package_manager/pkg.py new file mode 100644 index 0000000..abaeb25 --- /dev/null +++ b/src/component/package_manager/pkg.py @@ -0,0 +1,22 @@ + +class pkg_info: + def __init__(self) -> None: + self.name = "" + self.cid = None + self.depends : list[str] = None + pass + + def parse_pkg_name(pkg_name:str) -> Tuple[str, str, str]: + #return pkg_id,version_str,cid + pass + + @property + def cid(self) -> str: + return self.cid + +class pkg_media_info: + def __init__(self) -> None: + pass + + + diff --git a/test/agent_test.py b/test/agent_test.py new file mode 100644 index 0000000..e0fd3e3 --- /dev/null +++ b/test/agent_test.py @@ -0,0 +1,35 @@ +import sys +sys.path.append('../src/component/') + +from agent_manager import agent_manager + +def clean_agent(): + print("clean_agent") + +def clean_agent_templete(): + print("clean_agent_templte") + +def test_agent(): + am = agent_manager() + am.initial("root_dir") + agent = am.get("english_teacher") + if agent is None: + agent_templete = am.get_templete("english_teacher") + + if agent_templete is None : + op = am.install("english_teacher") + #wait install done + + agent = am.create(agent_templete,"Tracy","Wang","Tracy Wang is my english teacher") + + print("Agent Tracy Wang load success!"); + + #print(agent.get_introduce()); + + #chat_session = agent.get_default_chat_session("master"); + #chat_session.chat("给我讲一个英文笑话!"); + #chat_session.wait_response(); + #print(chat_session.last_msg()); + +if __name__ == "__main__": + test_agent() \ No newline at end of file diff --git a/test/pkg.cfg.toml b/test/pkg.cfg.toml new file mode 100644 index 0000000..46acf0a --- /dev/null +++ b/test/pkg.cfg.toml @@ -0,0 +1,15 @@ + +[authors] +"buckyos.org" = {public="aaaa",name="BuckyOS Core Dev Team"} +[authors."cyfs.com"] +public="bbbb" +name="CYFS Core Dev Team" + +[env] +is_strict = false +prefixs = ["default","../m/s","abc"] +obj2 = {k1="bb",k2=300} +[env.obj] +k1="aa" +k2=200 + diff --git a/test/spider_test.py b/test/spider_test.py new file mode 100644 index 0000000..296f382 --- /dev/null +++ b/test/spider_test.py @@ -0,0 +1,23 @@ + + +def test_main(): + sm = service_manager() + + knownlege_base_service = sm.get('knowlege_base') + knownlege_base_service.start() + + email_spider = sm.get('email_spider') + email_spider.start() + + doc_embeding_service = sm.get('doc_embeding_service') + doc_embeding_service.start() + + ia = agents_manager().get("ai_info_assistor") + chat_session = ia.get_default_chat_session("master"); + chat_session.chat("Who responded to my issue last week"); + chat_session.wait_response(); + #print(chat_session.last_msg()); + +if __name__ == '__main__': + test_main() + \ No newline at end of file