import os, json, logging from pathlib import Path from typing import List from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from dotenv import load_dotenv # env load_dotenv(dotenv_path=Path(__file__).resolve().parents[1] / ".env") HOST = os.getenv("TOOLSERVER_HOST", "127.0.0.1") PORT = int(os.getenv("TOOLSERVER_PORT", "18080")) LOG_DIR = Path(os.getenv("LOG_DIR", "./logs")) SECRETS_DIR = Path(os.getenv("SECRETS_DIR", "/data/adaptai/secrets")) CORS_ORIGINS = [o.strip() for o in os.getenv("CORS_ORIGINS", "*").split(",")] LOG_DIR.mkdir(parents=True, exist_ok=True) # logging logger = logging.getLogger("toolserver") logger.setLevel(logging.INFO) fh = logging.FileHandler(LOG_DIR / "server.log") fh.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(name)s - %(message)s')) logger.addHandler(fh) app = FastAPI( title="ADAPT AI Tool Server", version="0.1.0", description="Portable OpenAPI tool server for Open WebUI.", ) # CORS app.add_middleware( CORSMiddleware, allow_origins=["*"] if "*" in CORS_ORIGINS else CORS_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Schemas class PingRequest(BaseModel): message: str class PingResponse(BaseModel): echoed: str server_version: str cwd: str # Health @app.get("/health") def health(): return {"status": "ok", "version": app.version} # Example tool endpoint (Open WebUI will call this via OpenAPI) @app.post("/tools/ping", response_model=PingResponse) def tool_ping(req: PingRequest): logger.info("ping called with: %s", req.message) return PingResponse( echoed=req.message, server_version=app.version, cwd=str(Path.cwd()) )