Improve UserConfig Model, let aios shell remember last target~

This commit is contained in:
Liu Zhicong
2023-09-19 00:53:13 -07:00
parent 56d15fd003
commit feca072a4f
3 changed files with 32 additions and 14 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ class OpenAI_ComputeNode(ComputeNode):
if os.getenv("OPENAI_API_KEY") is not None: if os.getenv("OPENAI_API_KEY") is not None:
self.openai_api_key = os.getenv("OPENAI_API_KEY") self.openai_api_key = os.getenv("OPENAI_API_KEY")
else: else:
self.openai_api_key = AIStorage.get_instance().get_user_config().get_user_config("openai_api_key").value self.openai_api_key = AIStorage.get_instance().get_user_config().get_value("openai_api_key")
if self.openai_api_key is None: if self.openai_api_key is None:
logger.error("openai_api_key is None!") logger.error("openai_api_key is None!")
+12 -5
View File
@@ -55,7 +55,7 @@ class UserConfig:
except Exception as e: except Exception as e:
logger.warn(f"load user config from {file_path} failed!") logger.warn(f"load user config from {file_path} failed!")
async def save_value_to_user_config(self) -> None: async def save_to_user_config(self) -> None:
will_save_config = {} will_save_config = {}
for key,value in self.config_table.items(): for key,value in self.config_table.items():
if value.user_set: if value.user_set:
@@ -76,7 +76,14 @@ class UserConfig:
return True return True
def get_user_config(self,key:str) -> Any: def get_config_item(self,key:str) -> Any:
config_item = self.config_table.get(key)
if config_item is None:
raise Exception("user config key %s not exist",key)
return config_item
def get_value(self,key:str)->Any:
config_item = self.config_table.get(key) config_item = self.config_table.get(key)
if config_item is None: if config_item is None:
raise Exception("user config key %s not exist",key) raise Exception("user config key %s not exist",key)
@@ -84,9 +91,9 @@ class UserConfig:
if config_item.value is None: if config_item.value is None:
return config_item.default_value return config_item.default_value
return config_item return config_item.value
def set_user_config(self,key:str,value:Any) -> None: def set_value(self,key:str,value:Any) -> None:
config_item = self.config_table.get(key) config_item = self.config_table.get(key)
if config_item is None: if config_item is None:
logger.warning("user config key %s not exist",key) logger.warning("user config key %s not exist",key)
@@ -97,7 +104,7 @@ class UserConfig:
#TODO: save to file? #TODO: save to file?
def check_user_config(self) -> None: def check_config(self) -> None:
check_result = {} check_result = {}
for key,config_item in self.config_table.items(): for key,config_item in self.config_table.items():
if config_item.value is None and not config_item.is_optional: if config_item.value is None and not config_item.is_optional:
+18 -7
View File
@@ -53,6 +53,8 @@ class AIOS_Shell:
openai_node = OpenAI_ComputeNode.get_instance() openai_node = OpenAI_ComputeNode.get_instance()
openai_node.declare_user_config() openai_node.declare_user_config()
user_config.add_user_config("shell.current","last opened target and topic",True,"default@Jarvis")
async def _handle_no_target_msg(self,bus:AIBus,msg:AgentMsg) -> bool: async def _handle_no_target_msg(self,bus:AIBus,msg:AgentMsg) -> bool:
target_id = msg.target.split(".")[0] target_id = msg.target.split(".")[0]
@@ -147,12 +149,12 @@ class AIOS_Shell:
show_text = FormattedText([("class:title", f"set config failed!")]) show_text = FormattedText([("class:title", f"set config failed!")])
if len(args) == 1: if len(args) == 1:
key = args[0] key = args[0]
config_item = AIStorage.get_instance().get_user_config().get_user_config(key) config_item = AIStorage.get_instance().get_user_config().get_config_item(key)
old_value = config_item.value old_value = AIStorage.get_instance().get_user_config().get_value(key)
if config_item is not None: if config_item is not None:
value = await session.prompt_async(f"{key} : {config_item.desc} \nCurrent : {old_value}\nPlease input new value:",style=shell_style) value = await session.prompt_async(f"{key} : {config_item.desc} \nCurrent : {old_value}\nPlease input new value:",style=shell_style)
AIStorage.get_instance().get_user_config().set_user_config(key,value) AIStorage.get_instance().get_user_config().set_value(key,value)
await AIStorage.get_instance().get_user_config().save_value_to_user_config() await AIStorage.get_instance().get_user_config().save_to_user_config()
show_text = FormattedText([("class:title", f"set {key} to {value} success!")]) show_text = FormattedText([("class:title", f"set {key} to {value} success!")])
return show_text return show_text
@@ -167,11 +169,14 @@ class AIOS_Shell:
self.current_target = target_id self.current_target = target_id
self.current_topic = topic self.current_topic = topic
show_text = FormattedText([("class:title", f"current session switch to {topic}@{target_id}")]) show_text = FormattedText([("class:title", f"current session switch to {topic}@{target_id}")])
AIStorage.get_instance().get_user_config().set_value("shell.current",f"{self.current_topic}@{self.current_target}")
await AIStorage.get_instance().get_user_config().save_to_user_config()
return show_text return show_text
case 'login': case 'login':
if len(args) >= 1: if len(args) >= 1:
self.username = args[0] self.username = args[0]
AIBus().get_default_bus().register_message_handler(self.username,self._user_process_msg) AIBus().get_default_bus().register_message_handler(self.username,self._user_process_msg)
return self.username + " login success!" return self.username + " login success!"
case 'history': case 'history':
num = 10 num = 10
@@ -221,9 +226,9 @@ async def get_user_config_from_input(check_result:dict) -> bool:
for key,item in check_result.items(): for key,item in check_result.items():
user_input = await session.prompt_async(f"{key} ({item.desc}) not define! \nPlease input:",style=shell_style) user_input = await session.prompt_async(f"{key} ({item.desc}) not define! \nPlease input:",style=shell_style)
if len(user_input) > 0: if len(user_input) > 0:
AIStorage.get_instance().get_user_config().set_user_config(key,user_input) AIStorage.get_instance().get_user_config().set_value(key,user_input)
await AIStorage.get_instance().get_user_config().save_value_to_user_config() await AIStorage.get_instance().get_user_config().save_to_user_config()
return True return True
async def main_daemon_loop(shell:AIOS_Shell): async def main_daemon_loop(shell:AIOS_Shell):
@@ -286,7 +291,7 @@ async def main():
shell = AIOS_Shell("user") shell = AIOS_Shell("user")
shell.declare_all_user_config() shell.declare_all_user_config()
await AIStorage.get_instance().initial() await AIStorage.get_instance().initial()
check_result = AIStorage.get_instance().get_user_config().check_user_config() check_result = AIStorage.get_instance().get_user_config().check_config()
if check_result is not None: if check_result is not None:
if is_daemon: if is_daemon:
logger.error(check_result) logger.error(check_result)
@@ -319,6 +324,12 @@ async def main():
'/show', '/show',
'/exit', '/exit',
'/help'], ignore_case=True) '/help'], ignore_case=True)
current = AIStorage.get_instance().get_user_config().get_value("shell.current")
current = current.split("@")
shell.current_target = current[1]
shell.current_topic = current[0]
await asyncio.sleep(0.2) await asyncio.sleep(0.2)
while True: while True:
user_input = await session.prompt_async(f"{shell.username}<->{shell.current_topic}@{shell.current_target}$",completer=completer,style=shell_style) user_input = await session.prompt_async(f"{shell.username}<->{shell.current_topic}@{shell.current_target}$",completer=completer,style=shell_style)