diff --git a/chatapp_common.py b/frontends/chatapp_common.py similarity index 98% rename from chatapp_common.py rename to frontends/chatapp_common.py index 88d345a..0355ac6 100644 --- a/chatapp_common.py +++ b/frontends/chatapp_common.py @@ -94,7 +94,7 @@ def require_runtime(agent, label, **required): def redirect_log(script_file, log_name, label, allowed): - log_dir = os.path.join(os.path.dirname(script_file), "temp") + log_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(script_file))), "temp") os.makedirs(log_dir, exist_ok=True) logf = open(os.path.join(log_dir, log_name), "a", encoding="utf-8", buffering=1) sys.stdout = sys.stderr = logf diff --git a/dingtalkapp.py b/frontends/dingtalkapp.py similarity index 98% rename from dingtalkapp.py rename to frontends/dingtalkapp.py index bcb2faa..2213005 100644 --- a/dingtalkapp.py +++ b/frontends/dingtalkapp.py @@ -1,7 +1,7 @@ import asyncio, json, os, sys, threading, time import requests -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from agentmain import GeneraticAgent from chatapp_common import AgentChatMixin, ensure_single_instance, public_access, redirect_log, require_runtime, split_text from llmcore import mykeys diff --git a/fsapp.py b/frontends/fsapp.py similarity index 99% rename from fsapp.py rename to frontends/fsapp.py index 062f915..ce783d9 100644 --- a/fsapp.py +++ b/frontends/fsapp.py @@ -1,22 +1,14 @@ -import glob -import json -import os -import queue as Q -import re -import sys -import threading -import time +import glob, json, os, queue as Q, re, sys, threading, time -PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) +PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, PROJECT_ROOT) os.chdir(PROJECT_ROOT) +from agentmain import GeneraticAgent +from llmcore import mykeys import lark_oapi as lark from lark_oapi.api.im.v1 import * -from agentmain import GeneraticAgent -from llmcore import mykeys - _TAG_PATS = [r"<" + t + r">.*?" for t in ("thinking", "summary", "tool_use", "file_content")] _IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".ico", ".tiff", ".tif"} _AUDIO_EXTS = {".opus", ".mp3", ".wav", ".m4a", ".aac"} diff --git a/qqapp.py b/frontends/qqapp.py similarity index 98% rename from qqapp.py rename to frontends/qqapp.py index cd4223f..9631c9d 100644 --- a/qqapp.py +++ b/frontends/qqapp.py @@ -1,7 +1,7 @@ import asyncio, os, sys, threading, time from collections import deque -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from agentmain import GeneraticAgent from chatapp_common import AgentChatMixin, ensure_single_instance, public_access, redirect_log, require_runtime, split_text from llmcore import mykeys diff --git a/stapp.py b/frontends/stapp.py similarity index 100% rename from stapp.py rename to frontends/stapp.py diff --git a/tgapp.py b/frontends/tgapp.py similarity index 96% rename from tgapp.py rename to frontends/tgapp.py index 3064c0e..4198a9d 100644 --- a/tgapp.py +++ b/frontends/tgapp.py @@ -1,5 +1,5 @@ import os, sys, re, threading, asyncio, queue as Q, socket, time -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from agentmain import GeneraticAgent try: from telegram import Update @@ -120,7 +120,7 @@ if __name__ == '__main__': if not ALLOWED: print('[Telegram] ERROR: tg_allowed_users in mykey.py is empty or missing. Set it to avoid unauthorized access.') sys.exit(1) - _logf = open(os.path.join(os.path.dirname(__file__), 'temp', 'tgapp.log'), 'a', encoding='utf-8', buffering=1) + _logf = open(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'temp', 'tgapp.log'), 'a', encoding='utf-8', buffering=1) sys.stdout = sys.stderr = _logf print('[NEW] New process starting, the above are history infos ...') threading.Thread(target=agent.run, daemon=True).start() diff --git a/wecomapp.py b/frontends/wecomapp.py similarity index 98% rename from wecomapp.py rename to frontends/wecomapp.py index bd37ddd..5e026cd 100644 --- a/wecomapp.py +++ b/frontends/wecomapp.py @@ -1,7 +1,7 @@ import asyncio, os, sys, threading from collections import deque -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from agentmain import GeneraticAgent from chatapp_common import AgentChatMixin, ensure_single_instance, public_access, redirect_log, require_runtime, split_text from llmcore import mykeys diff --git a/launch.pyw b/launch.pyw index 845f229..6945023 100644 --- a/launch.pyw +++ b/launch.pyw @@ -2,6 +2,9 @@ import webview, threading, subprocess, sys, time, os, ctypes, atexit, socket, ra WINDOW_WIDTH, WINDOW_HEIGHT, RIGHT_PADDING, TOP_PADDING = 600, 900, 0, 100 +script_dir = os.path.dirname(os.path.abspath(__file__)) +frontends_dir = os.path.join(script_dir, "frontends") + def find_free_port(lo=18501, hi=18599): ports = list(range(lo, hi+1)); random.shuffle(ports) for p in ports: @@ -15,8 +18,7 @@ def get_screen_width(): def start_streamlit(port): global proc - script_dir = os.path.dirname(os.path.abspath(__file__)) - cmd = [sys.executable, "-m", "streamlit", "run", os.path.join(script_dir, "stapp.py"), "--server.port", str(port), "--server.address", "localhost", "--server.headless", "true", "--theme.base", "dark"] # 暗黑模式 + cmd = [sys.executable, "-m", "streamlit", "run", os.path.join(frontends_dir, "stapp.py"), "--server.port", str(port), "--server.address", "localhost", "--server.headless", "true", "--theme.base", "dark"] # 暗黑模式 proc = subprocess.Popen(cmd) atexit.register(proc.kill) @@ -77,36 +79,31 @@ if __name__ == '__main__': threading.Thread(target=start_streamlit, args=(port,), daemon=True).start() if args.tg: - script_dir = os.path.dirname(os.path.abspath(__file__)) - tgproc = subprocess.Popen([sys.executable, os.path.join(script_dir, "tgapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) + tgproc = subprocess.Popen([sys.executable, os.path.join(frontends_dir, "tgapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) atexit.register(tgproc.kill) print('[Launch] Telegram Bot started') else: print('[Launch] Telegram Bot not enabled (use --tg to start)') if args.qq: - script_dir = os.path.dirname(os.path.abspath(__file__)) - qqproc = subprocess.Popen([sys.executable, os.path.join(script_dir, "qqapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) + qqproc = subprocess.Popen([sys.executable, os.path.join(frontends_dir, "qqapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) atexit.register(qqproc.kill) print('[Launch] QQ Bot started') else: print('[Launch] QQ Bot not enabled (use --qq to start)') if args.feishu: - script_dir = os.path.dirname(os.path.abspath(__file__)) - fsproc = subprocess.Popen([sys.executable, os.path.join(script_dir, "fsapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) + fsproc = subprocess.Popen([sys.executable, os.path.join(frontends_dir, "fsapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) atexit.register(fsproc.kill) print('[Launch] Feishu Bot started') else: print('[Launch] Feishu Bot not enabled (use --feishu to start)') if args.wecom: - script_dir = os.path.dirname(os.path.abspath(__file__)) - wcproc = subprocess.Popen([sys.executable, os.path.join(script_dir, "wecomapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) + wcproc = subprocess.Popen([sys.executable, os.path.join(frontends_dir, "wecomapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) atexit.register(wcproc.kill) print('[Launch] WeCom Bot started') else: print('[Launch] WeCom Bot not enabled (use --wecom to start)') if args.dingtalk: - script_dir = os.path.dirname(os.path.abspath(__file__)) - dtproc = subprocess.Popen([sys.executable, os.path.join(script_dir, "dingtalkapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) + dtproc = subprocess.Popen([sys.executable, os.path.join(frontends_dir, "dingtalkapp.py")], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0) atexit.register(dtproc.kill) print('[Launch] DingTalk Bot started') else: print('[Launch] DingTalk Bot not enabled (use --dingtalk to start)') @@ -114,7 +111,6 @@ if __name__ == '__main__': if not args.no_sched: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM); sock.bind(('127.0.0.1', 45762)); sock.listen(1) - script_dir = os.path.dirname(os.path.abspath(__file__)) scheduler_proc = subprocess.Popen([sys.executable, os.path.join(script_dir, "agentmain.py"), "--reflect", os.path.join(script_dir, "reflect", "scheduler.py"), "--llm_no", str(args.llm_no)], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0); atexit.register(lambda: (scheduler_proc.kill(), sock.close())) print('[Launch] Task Scheduler started')