Pretor/main.py

68 lines
2.2 KiB
Python

import asyncio
import ray
from pretor.utils.banner import print_banner
from pretor.core.database.postgres import PostgresDatabase
from pretor.core.global_state_machine.global_state_machine import GlobalStateMachine
from pretor.core.individual.supervisory_node.supervisory_node import SupervisoryNode
from pretor.core.individual.consciousness_node.consciousness_node import ConsciousnessNode
from pretor.core.individual.control_node.control_node import ControlNode
from pretor.core.workflow.workflow_runner import WorkflowRunningEngine
from pretor.core.api import PretorGateway
from ray import serve
async def start_system():
# 1. 初始化 Ray
env_vars = {
"POSTGRES_USER": "postgres",
"POSTGRES_PASSWORD": "postgres",
"POSTGRES_HOST": "127.0.0.1",
"POSTGRES_PORT": "5432",
"POSTGRES_DB": "postgres",
"SECRET_KEY": "yoursecretkey"
}
ray.init(ignore_reinit_error=True, runtime_env={"env_vars": env_vars})
# 2. 启动数据库组件
postgres_database = PostgresDatabase.options(name='postgres_database').remote()
await postgres_database.init_db.remote()
# 3. 启动全局状态机
global_state_machine = GlobalStateMachine.options(name='global_state_machine').remote(postgres_database)
# 4. 启动核心节点
supervisory_node = SupervisoryNode.options(name='supervisory_node').remote()
consciousness_node = ConsciousnessNode.options(name='consciousness_node').remote()
control_node = ControlNode.options(name='control_node').remote()
# 5. 启动工作流运行引擎
workflow_engine = WorkflowRunningEngine.options(name='workflow_running_engine').remote(
consciousness_node=consciousness_node,
control_node=control_node,
supervisory_node=supervisory_node
)
# 异步拉起 runner 协程群
workflow_engine.run.remote()
# 6. 启动 FastAPI 网关 (使用 Ray Serve)
serve.start(http_options={"host": "0.0.0.0", "port": 8000})
serve.run(PretorGateway.bind())
# 挂起主线程以保持系统运行
while True:
await asyncio.sleep(3600)
def main():
print_banner()
try:
asyncio.run(start_system())
except KeyboardInterrupt:
print("系统已退出。")
if __name__ == '__main__':
main()