mirror of
https://github.com/open-webui/computer.git
synced 2026-11-20 21:41:54 +03:00
refac
This commit is contained in:
+48
-34
@@ -1,4 +1,6 @@
|
||||
import asyncio
|
||||
import time
|
||||
from contextlib import asynccontextmanager, suppress
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
@@ -31,11 +33,10 @@ from cptr.utils.config import check_access, load_config
|
||||
from cptr.utils.db import init_db
|
||||
|
||||
START_TIME = time.time()
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
from cptr.utils.logger import setup_logging
|
||||
|
||||
setup_logging()
|
||||
@@ -49,6 +50,7 @@ async def startup():
|
||||
_logging.getLogger(__name__).info("truststore: using system certificate store")
|
||||
|
||||
await init_db()
|
||||
app.state.MODELS = {}
|
||||
from cptr.env import STARTUP_TOKEN
|
||||
|
||||
app.state.startup_token = STARTUP_TOKEN
|
||||
@@ -60,50 +62,62 @@ async def startup():
|
||||
|
||||
await reconcile_chat_state()
|
||||
|
||||
from cptr.routers.chat import warm_model_cache
|
||||
|
||||
await warm_model_cache(app.state)
|
||||
|
||||
# Start automation scheduler
|
||||
import asyncio
|
||||
from cptr.utils.automations import scheduler_worker_loop
|
||||
|
||||
asyncio.create_task(scheduler_worker_loop(app))
|
||||
app.state.scheduler_task = asyncio.create_task(scheduler_worker_loop(app))
|
||||
|
||||
# Start messaging bots
|
||||
from cptr.utils.bridge import BotManager
|
||||
|
||||
app.state.bot_manager = BotManager()
|
||||
asyncio.create_task(app.state.bot_manager.start_all())
|
||||
await app.state.bot_manager.start_all()
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown():
|
||||
bot_manager = getattr(app.state, "bot_manager", None)
|
||||
if bot_manager:
|
||||
await bot_manager.stop_all()
|
||||
try:
|
||||
from cptr.utils.async_subagents import cancel_all_async_subagents
|
||||
yield
|
||||
finally:
|
||||
scheduler_task = getattr(app.state, "scheduler_task", None)
|
||||
if scheduler_task:
|
||||
scheduler_task.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await scheduler_task
|
||||
|
||||
await cancel_all_async_subagents(reason="shutdown")
|
||||
except Exception:
|
||||
pass
|
||||
# Clean up browser sessions and launched Chrome used by agent tools.
|
||||
try:
|
||||
from cptr.utils.browser.session import session_manager
|
||||
from cptr.utils.browser.launcher import shutdown_browser
|
||||
from cptr.utils.browser.proxy import manager as browser_proxy_manager
|
||||
from cptr.utils.browser.viewer import manager as chrome_viewer_manager
|
||||
bot_manager = getattr(app.state, "bot_manager", None)
|
||||
if bot_manager:
|
||||
await bot_manager.stop_all()
|
||||
try:
|
||||
from cptr.utils.async_subagents import cancel_all_async_subagents
|
||||
|
||||
await chrome_viewer_manager.close_all()
|
||||
await browser_proxy_manager.close_all()
|
||||
await session_manager.close_all()
|
||||
await shutdown_browser()
|
||||
except Exception:
|
||||
pass
|
||||
# Clean up stdio MCP server processes
|
||||
try:
|
||||
from cptr.utils.mcp.stdio_manager import stdio_manager
|
||||
await cancel_all_async_subagents(reason="shutdown")
|
||||
except Exception:
|
||||
pass
|
||||
# Clean up browser sessions and launched Chrome used by agent tools.
|
||||
try:
|
||||
from cptr.utils.browser.session import session_manager
|
||||
from cptr.utils.browser.launcher import shutdown_browser
|
||||
from cptr.utils.browser.proxy import manager as browser_proxy_manager
|
||||
from cptr.utils.browser.viewer import manager as chrome_viewer_manager
|
||||
|
||||
await stdio_manager.disconnect_all()
|
||||
except Exception:
|
||||
pass
|
||||
await chrome_viewer_manager.close_all()
|
||||
await browser_proxy_manager.close_all()
|
||||
await session_manager.close_all()
|
||||
await shutdown_browser()
|
||||
except Exception:
|
||||
pass
|
||||
# Clean up stdio MCP server processes
|
||||
try:
|
||||
from cptr.utils.mcp.stdio_manager import stdio_manager
|
||||
|
||||
await stdio_manager.disconnect_all()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
|
||||
# Auth middleware
|
||||
|
||||
+22
-9
@@ -243,8 +243,21 @@ async def _get_connection_models(conn: dict, app_state) -> list[str]:
|
||||
return cache[conn_id]
|
||||
|
||||
models = await _fetch_provider_models(conn)
|
||||
cache[conn_id] = models
|
||||
return models
|
||||
if models is not None:
|
||||
cache[conn_id] = models
|
||||
return models
|
||||
return cache.get(conn_id, [])
|
||||
|
||||
|
||||
async def warm_model_cache(app_state) -> None:
|
||||
"""Pre-fetch chat model sources before the application accepts requests."""
|
||||
connections = [c for c in await _get_connections() if c.get("enabled", True)]
|
||||
tasks = [_get_connection_models(conn, app_state) for conn in connections]
|
||||
|
||||
from cptr.utils.agents.detection import get_available_agent_model_entries
|
||||
|
||||
tasks.append(get_available_agent_model_entries(app_state))
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
|
||||
def invalidate_model_cache(app_state):
|
||||
@@ -294,18 +307,18 @@ async def get_models(request: Request):
|
||||
return {"models": models, "default": default_model}
|
||||
|
||||
|
||||
async def _fetch_provider_models(conn: dict) -> list[str]:
|
||||
async def _fetch_provider_models(conn: dict) -> list[str] | None:
|
||||
"""Discover models from a provider's /models endpoint."""
|
||||
import httpx
|
||||
|
||||
from cptr.utils.ai import _openrouter_headers
|
||||
|
||||
secret = _get_jwt_secret()
|
||||
api_key = decrypt_key(conn.get("api_key", ""), secret) if conn.get("api_key") else None
|
||||
provider = conn.get("provider", "")
|
||||
base_url = conn.get("base_url")
|
||||
|
||||
try:
|
||||
secret = _get_jwt_secret()
|
||||
api_key = decrypt_key(conn.get("api_key", ""), secret) if conn.get("api_key") else None
|
||||
provider = conn.get("provider", "")
|
||||
base_url = conn.get("base_url")
|
||||
|
||||
if provider == "anthropic":
|
||||
url = (base_url or "https://api.anthropic.com/v1") + "/models"
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
@@ -357,7 +370,7 @@ async def _fetch_provider_models(conn: dict) -> list[str]:
|
||||
except Exception:
|
||||
log.exception("Model auto-discovery error for connection %s", conn.get("id", "?"))
|
||||
|
||||
return []
|
||||
return None
|
||||
|
||||
|
||||
# ── Get a chat with all messages ────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user