From c9af0cfc0df9732bfde376ca9823e8787ee59e6e Mon Sep 17 00:00:00 2001 From: streetycat <305190374@qq.com> Date: Tue, 19 Sep 2023 08:29:55 +0000 Subject: [PATCH 1/5] add proxy in config --- src/aios_kernel/storage.py | 14 ++++++++++-- src/requirements.txt | 1 + src/service/aios_shell/aios_shell.py | 22 ++++++++++++------- src/service/aios_shell/proxy.py | 32 ++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 src/service/aios_shell/proxy.py diff --git a/src/aios_kernel/storage.py b/src/aios_kernel/storage.py index e982645..dfebefe 100644 --- a/src/aios_kernel/storage.py +++ b/src/aios_kernel/storage.py @@ -21,7 +21,15 @@ class UserConfigItem: self.desc = desc self.value = None self.user_set = False - + + def clone(self): + new_config_item = UserConfigItem() + new_config_item.default_value = self.default_value + new_config_item.is_optional = self.is_optional + new_config_item.desc = self.desc + new_config_item.item_type = self.item_type + new_config_item.value = self.value + return new_config_item class UserConfig: def __init__(self) -> None: @@ -89,7 +97,9 @@ class UserConfig: raise Exception("user config key %s not exist",key) if config_item.value is None: - return config_item.default_value + default_config_item = config_item.clone() + default_config_item.value = config_item.default_value + return default_config_item return config_item.value diff --git a/src/requirements.txt b/src/requirements.txt index 574b9ea..8427ade 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -20,3 +20,4 @@ google-cloud-texttospeech openai Pillow aiosqlite +PySocks==1.7.1 diff --git a/src/service/aios_shell/aios_shell.py b/src/service/aios_shell/aios_shell.py index 13a5ee8..de5675e 100644 --- a/src/service/aios_shell/aios_shell.py +++ b/src/service/aios_shell/aios_shell.py @@ -23,7 +23,7 @@ directory = os.path.dirname(__file__) sys.path.append(directory + '/../../') from aios_kernel import AIOS_Version,UserConfigItem,AIStorage,Workflow,AIAgent,AgentMsg,AgentMsgStatus,ComputeKernel,OpenAI_ComputeNode,AIBus,AIChatSession,AgentTunnel,TelegramTunnel,CalenderEnvironment,Environment,EmailTunnel,LocalLlama_ComputeNode - +import proxy sys.path.append(directory + '/../../component/') from agent_manager import AgentManager @@ -55,6 +55,7 @@ class AIOS_Shell: openai_node.declare_user_config() user_config.add_user_config("shell.current","last opened target and topic",True,"default@Jarvis") + proxy.declare_user_config() async def _handle_no_target_msg(self,bus:AIBus,msg:AgentMsg) -> bool: @@ -95,7 +96,7 @@ class AIOS_Shell: llama_ai_node = LocalLlama_ComputeNode() await llama_ai_node.start() - #ComputeKernel.get_instance().add_compute_node(llama_ai_node) + # ComputeKernel.get_instance().add_compute_node(llama_ai_node) await ComputeKernel.get_instance().start() @@ -192,13 +193,16 @@ class AIOS_Shell: show_text = FormattedText([("class:title", f"set config failed!")]) if len(args) == 1: key = args[0] - config_item = AIStorage.get_instance().get_user_config().get_config_item(key) - old_value = AIStorage.get_instance().get_user_config().get_value(key) + config_item = AIStorage.get_instance().get_user_config().get_user_config(key) + + old_value = 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) - AIStorage.get_instance().get_user_config().set_value(key,value) - await AIStorage.get_instance().get_user_config().save_to_user_config() - show_text = FormattedText([("class:title", f"set {key} to {value} success!")]) + old_value = config_item.value + + 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) + await AIStorage.get_instance().get_user_config().save_value_to_user_config() + show_text = FormattedText([("class:title", f"set {key} to {value} success!")]) return show_text case 'connect': @@ -390,6 +394,8 @@ async def main(): if is_daemon: return await main_daemon_loop(shell) + proxy.apply_storage() + #TODO: read last input config completer = WordCompleter(['/send $target $msg $topic', '/open $target $topic', diff --git a/src/service/aios_shell/proxy.py b/src/service/aios_shell/proxy.py new file mode 100644 index 0000000..7f8a352 --- /dev/null +++ b/src/service/aios_shell/proxy.py @@ -0,0 +1,32 @@ + +import sys +import os +import logging +import socket +import socks + +directory = os.path.dirname(__file__) +sys.path.append(directory + '/../../') + +from aios_kernel import AIStorage + +def apply_storage(): + proxy_cfg = AIStorage.get_instance().get_user_config().get_user_config("proxy") + if proxy_cfg is None: + return + + host_url = proxy_cfg.value + if host_url is not None: + (proxy_type, host) = host_url.split("@") + match proxy_type: + case "socks5": + (host, port) = host.split(":") + socks.set_default_proxy(socks.SOCKS5, host, int(port)) + socket.socket = socks.socksocket + print("proxy {host_url} will be used.") + case _: + print("the proxy type ({proxy_type}) has not support.") + +def declare_user_config(): + user_config = AIStorage.get_instance().get_user_config() + user_config.add_user_config("proxy", "set your proxy service as 'proxy_type@host:port', 'proxy_type' = 'socks5'", True, None) From c5184154b5a568c50746ff7f4dbfa043c30a52ea Mon Sep 17 00:00:00 2001 From: streetycat <305190374@qq.com> Date: Wed, 20 Sep 2023 07:46:42 +0000 Subject: [PATCH 2/5] proxy with authorize --- src/aios_kernel/local_llama_compute_node.py | 2 +- src/service/aios_shell/proxy.py | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/aios_kernel/local_llama_compute_node.py b/src/aios_kernel/local_llama_compute_node.py index 5cba82e..7e50661 100644 --- a/src/aios_kernel/local_llama_compute_node.py +++ b/src/aios_kernel/local_llama_compute_node.py @@ -47,7 +47,7 @@ class LocalLlama_ComputeNode(Queue_ComputeNode): "state": ComputeTaskState.ERROR, "error": { "code": response.status_code, - "message": "http request failed: " + response.status_code + "message": "http request failed: " + str(response.status_code) } } else: diff --git a/src/service/aios_shell/proxy.py b/src/service/aios_shell/proxy.py index 7f8a352..ec07eb3 100644 --- a/src/service/aios_shell/proxy.py +++ b/src/service/aios_shell/proxy.py @@ -16,17 +16,25 @@ def apply_storage(): return host_url = proxy_cfg.value - if host_url is not None: - (proxy_type, host) = host_url.split("@") + if host_url is not None and len(host_url) > 3: + url_fields = host_url.split("@") + proxy_type, host, username, password = url_fields[0], None, None, None + if len(url_fields) > 1: + host = url_fields[1] + if len(url_fields) > 2: + username = url_fields[2] + if len(url_fields) > 3: + password = url_fields[3] + match proxy_type: case "socks5": (host, port) = host.split(":") - socks.set_default_proxy(socks.SOCKS5, host, int(port)) + socks.set_default_proxy(socks.SOCKS5, host, int(port), username = username, password = password) socket.socket = socks.socksocket - print("proxy {host_url} will be used.") + print(f"proxy {host_url} will be used.") case _: - print("the proxy type ({proxy_type}) has not support.") + print(f"the proxy type ({proxy_type}) has not support.") def declare_user_config(): user_config = AIStorage.get_instance().get_user_config() - user_config.add_user_config("proxy", "set your proxy service as 'proxy_type@host:port', 'proxy_type' = 'socks5'", True, None) + user_config.add_user_config("proxy", "set your proxy service as 'proxy_type@host:port@username@password', 'proxy_type' = 'socks5'", True, None) From 0de5534723b15a34e6c9f6a9107a2f55441f3946 Mon Sep 17 00:00:00 2001 From: zhangzhen Date: Wed, 20 Sep 2023 16:17:06 +0800 Subject: [PATCH 3/5] adapt to new value name --- src/service/aios_shell/aios_shell.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/service/aios_shell/aios_shell.py b/src/service/aios_shell/aios_shell.py index de5675e..4d054fb 100644 --- a/src/service/aios_shell/aios_shell.py +++ b/src/service/aios_shell/aios_shell.py @@ -193,15 +193,15 @@ class AIOS_Shell: show_text = FormattedText([("class:title", f"set config failed!")]) if len(args) == 1: 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 = None if config_item is not None: old_value = config_item.value 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) - await AIStorage.get_instance().get_user_config().save_value_to_user_config() + AIStorage.get_instance().get_user_config().set_value(key,value) + await AIStorage.get_instance().get_user_config().save_to_user_config() show_text = FormattedText([("class:title", f"set {key} to {value} success!")]) return show_text From ee3f9ebceaf8b35941b41bf5111409c9989f1fdd Mon Sep 17 00:00:00 2001 From: streetycat <305190374@qq.com> Date: Wed, 20 Sep 2023 08:45:53 +0000 Subject: [PATCH 4/5] adapt to new value name --- aios_shell_history.txt | 21 +++++++++++++++++++++ src/aios_kernel/storage.py | 4 +--- src/service/aios_shell/aios_shell.py | 12 +++++------- src/service/aios_shell/proxy.py | 2 +- 4 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 aios_shell_history.txt diff --git a/aios_shell_history.txt b/aios_shell_history.txt new file mode 100644 index 0000000..830345a --- /dev/null +++ b/aios_shell_history.txt @@ -0,0 +1,21 @@ + +# 2023-09-20 08:41:50.636730 ++/set_config proxy + +# 2023-09-20 08:42:18.936143 ++/open math_school 小学老师 + +# 2023-09-20 08:42:23.884698 ++3+5=8 + +# 2023-09-20 08:43:13.219129 ++/set_config proxy + +# 2023-09-20 08:43:23.430306 ++socks5@192.168.100.91:1080@zhangzhen@123456 + +# 2023-09-20 08:43:35.319913 ++/open math_school 小学老师 + +# 2023-09-20 08:43:41.557234 ++3+5=8 diff --git a/src/aios_kernel/storage.py b/src/aios_kernel/storage.py index dfebefe..5119167 100644 --- a/src/aios_kernel/storage.py +++ b/src/aios_kernel/storage.py @@ -97,9 +97,7 @@ class UserConfig: raise Exception("user config key %s not exist",key) if config_item.value is None: - default_config_item = config_item.clone() - default_config_item.value = config_item.default_value - return default_config_item + return config_item.default_value return config_item.value diff --git a/src/service/aios_shell/aios_shell.py b/src/service/aios_shell/aios_shell.py index 4d054fb..41f24a0 100644 --- a/src/service/aios_shell/aios_shell.py +++ b/src/service/aios_shell/aios_shell.py @@ -194,15 +194,13 @@ class AIOS_Shell: if len(args) == 1: key = args[0] config_item = AIStorage.get_instance().get_user_config().get_config_item(key) + old_value = AIStorage.get_instance().get_user_config().get_value(key) - old_value = None if config_item is not None: - old_value = config_item.value - - 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_value(key,value) - await AIStorage.get_instance().get_user_config().save_to_user_config() - show_text = FormattedText([("class:title", f"set {key} to {value} success!")]) + 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_value(key,value) + await AIStorage.get_instance().get_user_config().save_to_user_config() + show_text = FormattedText([("class:title", f"set {key} to {value} success!")]) return show_text case 'connect': diff --git a/src/service/aios_shell/proxy.py b/src/service/aios_shell/proxy.py index ec07eb3..caf006d 100644 --- a/src/service/aios_shell/proxy.py +++ b/src/service/aios_shell/proxy.py @@ -11,7 +11,7 @@ sys.path.append(directory + '/../../') from aios_kernel import AIStorage def apply_storage(): - proxy_cfg = AIStorage.get_instance().get_user_config().get_user_config("proxy") + proxy_cfg = AIStorage.get_instance().get_user_config().get_config_item("proxy") if proxy_cfg is None: return From fdbb0fcc475a1b528e2caf659ab6b55a566ae2ad Mon Sep 17 00:00:00 2001 From: streetycat <305190374@qq.com> Date: Wed, 20 Sep 2023 08:52:15 +0000 Subject: [PATCH 5/5] remove local files --- .gitignore | 1 + aios_shell_history.txt | 21 --------------------- 2 files changed, 1 insertion(+), 21 deletions(-) delete mode 100644 aios_shell_history.txt diff --git a/.gitignore b/.gitignore index 99b413e..f616e40 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ rootfs/data venv aios_shell.log history.txt +aios_shell_history.txt math_school_env.db workflows.db diff --git a/aios_shell_history.txt b/aios_shell_history.txt deleted file mode 100644 index 830345a..0000000 --- a/aios_shell_history.txt +++ /dev/null @@ -1,21 +0,0 @@ - -# 2023-09-20 08:41:50.636730 -+/set_config proxy - -# 2023-09-20 08:42:18.936143 -+/open math_school 小学老师 - -# 2023-09-20 08:42:23.884698 -+3+5=8 - -# 2023-09-20 08:43:13.219129 -+/set_config proxy - -# 2023-09-20 08:43:23.430306 -+socks5@192.168.100.91:1080@zhangzhen@123456 - -# 2023-09-20 08:43:35.319913 -+/open math_school 小学老师 - -# 2023-09-20 08:43:41.557234 -+3+5=8