1"""v0.80 — `agentos serve` API 服务器启动器。"""

2

3 from __future__ import annotations

4

5 import asyncio

6 import os

7 from dataclasses import dataclass, field

8

9

10@dataclass

11 class ServeConfig:

12 """API 服务配置。"""

13 host: str = "127.0.0.1"

14 port: int = 8000

15 reload: bool = False

16 workers: int = 1

17 log_level: str = "info"

18 env_file: str = ".env"

19

20

21 def start_api_server(config: ServeConfig | None = None) -> None:

22 """启动 AgentOS API 服务器（FastAPI + Uvicorn）。

23

24 Args:

25 config: ServeConfig 配置对象。

26 """

27 cfg = config or ServeConfig()

28

29 if cfg.env_file and os.path.exists(cfg.env_file):

30 from dotenv import load_dotenv

31 load_dotenv(cfg.env_file)

32

33 import uvicorn

34 uvicorn.run(

35 "agentos.api.server:app",

36 host=cfg.host,

37 port=cfg.port,

38 reload=cfg.reload,

39 workers=cfg.workers,

40 log_level=cfg.log_level,

41 )

42

43

44 async def start_api_server_async(config: ServeConfig | None = None) -> None:

45 """异步启动 API 服务器。"""

46 cfg = config or ServeConfig()

47 import uvicorn

48 server_config = uvicorn.Config(

49 "agentos.api.server:app",

50 host=cfg.host,

51 port=cfg.port,

52 reload=cfg.reload,

53 workers=cfg.workers,

54 log_level=cfg.log_level,

55 )

56 server = uvicorn.Server(server_config)

57 await server.serve()