File size: 1,784 Bytes
75f5b98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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())
    )