ToDoAgent / LLM /orchestrator.py
Siyu Wang
updated to KK_Server
84ed1d1
import time
import yaml
import threading
import importlib
from pathlib import Path
import sys
# import yaml # Removed yaml import
import mysql.connector as pymysql # Use mysql-connector-python alias
# Import config loader from todogen_LLM
from todogen_LLM.config_loader import get_mysql_config
# Removed local load_config function and CONFIG/MYSQL_CONFIG globals
# # 配置
def load_config():
config_path = Path(__file__).parent / "todogen_LLM" / "todogen_LLM_config.yaml"
with open(config_path, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
CONFIG = load_config()
MYSQL_CONFIG = CONFIG['mysql']
# 数据库
def get_db_conn():
mysql_config = get_mysql_config()
return pymysql.connect(
host=mysql_config['host'],
port=mysql_config.get('port', 3306),
user=mysql_config['user'],
password=mysql_config['password'],
database=mysql_config['database'],
ssl_ca=mysql_config['ssl_ca'], # path
ssl_disabled=False, # enabled
charset='utf8mb4',
autocommit=True
)
def get_latest_update_time(conn):
with conn.cursor() as cursor:
cursor.execute("SELECT MAX(date) FROM Messages")
result = cursor.fetchone()
return result[0]
#filter_llm主入口
def run_filter_llm():
sys.path.append(str(Path(__file__).parent / 'filter_llm'))
main = importlib.import_module('main')
if hasattr(main, 'main'):
main.main()
else:
raise RuntimeError('filter_llm.main.py未找到main函数')
#todogen_LLM主入口
def run_todogen_llm():
sys.path.append(str(Path(__file__).parent / 'todogen_LLM'))
todogen = importlib.import_module('todogen_llm')
if hasattr(todogen, 'main'):
todogen.main()
else:
if hasattr(todogen, 'load_formatted_data') and hasattr(todogen, 'process_data'):
data = todogen.load_formatted_data()
todogen.process_data(data)
else:
raise RuntimeError('todogen_llm.py未找到main或核心处理函数')
# 调用Notify主入口
#// def run_notify():
#// sys.path.append(str(Path(__file__).parent / 'Notify')) # Path needs update if kept
#// notify = importlib.import_module('notifyMain')
#// if hasattr(notify, 'main'):
#// notify.main()
#// else:
#// raise RuntimeError('Notify/notifyMain.py未找到main函数')
# 监听messages表并联动
def monitor_and_orchestrate(interval=5):
conn = get_db_conn()
last_update = get_latest_update_time(conn)
print(f"初始messages表更新时间: {last_update}")
while True:
time.sleep(interval)
try:
current_update = get_latest_update_time(conn)
if current_update != last_update:
print(f"检测到messages表有更新: {current_update}, 开始联动执行...")
run_filter_llm()
run_todogen_llm()
last_update = current_update
else:
print("无更新,继续监听...")
except Exception as e:
print(f"监听或执行过程中发生错误: {e}")
if __name__ == "__main__":
monitor_and_orchestrate(interval=5)