oqllm-api 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- adapters/__init__.py +0 -0
- adapters/asgi_python/__init__.py +0 -0
- adapters/asgi_python/app.py +1154 -0
- adapters/plesk_php/auth.php +942 -0
- adapters/plesk_php/decide.php +303 -0
- adapters/plesk_php/health.php +11 -0
- adapters/plesk_php/index.php +24 -0
- adapters/plesk_php/mesh.php +201 -0
- adapters/plesk_php/models.php +11 -0
- adapters/plesk_php/poa.php +619 -0
- adapters/plesk_php/speech.php +350 -0
- adapters/plesk_php/vision.php +379 -0
- oqllm_api-0.2.0.dist-info/METADATA +165 -0
- oqllm_api-0.2.0.dist-info/RECORD +17 -0
- oqllm_api-0.2.0.dist-info/WHEEL +4 -0
- oqllm_api-0.2.0.dist-info/licenses/LICENSE +109 -0
- oqllm_api-0.2.0.dist-info/licenses/LICENSE.md +109 -0
|
@@ -0,0 +1,1154 @@
|
|
|
1
|
+
"""ASGI application for oqllm API gateway."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import binascii
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
import secrets
|
|
11
|
+
import time
|
|
12
|
+
import urllib.error
|
|
13
|
+
import urllib.parse
|
|
14
|
+
import urllib.request
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import Any, Optional
|
|
17
|
+
|
|
18
|
+
from fastapi import FastAPI, HTTPException, Header, Query, Request, Response, status
|
|
19
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
20
|
+
from fastapi.responses import HTMLResponse, JSONResponse
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
from auth import default_auth_manager
|
|
24
|
+
except ImportError:
|
|
25
|
+
try:
|
|
26
|
+
from ...auth import default_auth_manager
|
|
27
|
+
except Exception:
|
|
28
|
+
from oqllm_proxy.auth import default_auth_manager
|
|
29
|
+
|
|
30
|
+
MAX_SPEECH_AUDIO_BYTES = 7 * 1024 * 1024
|
|
31
|
+
MAX_SPEECH_RESPONSE_BYTES = 12 * 1024 * 1024
|
|
32
|
+
MAX_VISION_IMAGE_BYTES = 3 * 1024 * 1024
|
|
33
|
+
MAX_VISION_DATA_URL_CHARS = 4_000_000
|
|
34
|
+
MAX_VISION_RESPONSE_BYTES = 2 * 1024 * 1024
|
|
35
|
+
ALLOWED_SPEECH_MEDIA_TYPES = {"audio/wav", "audio/x-wav", "audio/ogg", "audio/webm"}
|
|
36
|
+
ALLOWED_IMAGE_MEDIA_TYPES = {"image/png", "image/jpeg", "image/webp", "image/gif"}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class _NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
40
|
+
def redirect_request(self, *_args: Any) -> None:
|
|
41
|
+
raise urllib.error.URLError("Upstream redirects are forbidden")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _validated_upstream_url(raw: str, default_path: str) -> Optional[str]:
|
|
45
|
+
candidate = raw.strip()
|
|
46
|
+
if not candidate:
|
|
47
|
+
return None
|
|
48
|
+
parts = urllib.parse.urlsplit(candidate)
|
|
49
|
+
scheme = parts.scheme.lower()
|
|
50
|
+
host = (parts.hostname or "").lower()
|
|
51
|
+
if not host or parts.username or parts.password or parts.query or parts.fragment:
|
|
52
|
+
return None
|
|
53
|
+
loopback = host in ("127.0.0.1", "localhost", "::1")
|
|
54
|
+
if scheme != "https" and not (scheme == "http" and loopback):
|
|
55
|
+
return None
|
|
56
|
+
path = parts.path.rstrip("/") or default_path
|
|
57
|
+
netloc = f"[{host}]" if ":" in host else host
|
|
58
|
+
if parts.port is not None:
|
|
59
|
+
netloc = f"{netloc}:{parts.port}"
|
|
60
|
+
return f"{scheme}://{netloc}{path}"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _upstream_target(prefix: str, default_path: str) -> tuple[Optional[str], str]:
|
|
64
|
+
raw_url = os.environ.get(f"OQLLM_{prefix}_UPSTREAM_URL") or os.environ.get("OQLLM_INFERENCE_UPSTREAM_URL", "")
|
|
65
|
+
url = _validated_upstream_url(raw_url, default_path)
|
|
66
|
+
if url is not None and not raw_url.strip().rstrip("/").endswith(default_path):
|
|
67
|
+
parts = urllib.parse.urlsplit(url)
|
|
68
|
+
url = f"{parts.scheme}://{parts.netloc}{default_path}"
|
|
69
|
+
token = (
|
|
70
|
+
os.environ.get(f"OQLLM_{prefix}_UPSTREAM_TOKEN")
|
|
71
|
+
or os.environ.get("OQLLM_INFERENCE_UPSTREAM_TOKEN")
|
|
72
|
+
or ""
|
|
73
|
+
).strip()
|
|
74
|
+
return url, token
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _authenticate_api_key_only(
|
|
78
|
+
authorization: Optional[str] = None,
|
|
79
|
+
x_api_key: Optional[str] = None,
|
|
80
|
+
x_interface_type: Optional[str] = None,
|
|
81
|
+
required_scopes: tuple[str, ...] = ("all",),
|
|
82
|
+
rate_format: Optional[str] = "nl",
|
|
83
|
+
) -> dict[str, Any]:
|
|
84
|
+
token: Optional[str] = None
|
|
85
|
+
if isinstance(authorization, str) and authorization.startswith("Bearer "):
|
|
86
|
+
token = authorization[7:].strip()
|
|
87
|
+
elif isinstance(x_api_key, str) and x_api_key.strip():
|
|
88
|
+
token = x_api_key.strip()
|
|
89
|
+
if not token or token.startswith("sess_"):
|
|
90
|
+
raise HTTPException(
|
|
91
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
92
|
+
detail="An API key is required. Obtain one at https://user.oqllm.com",
|
|
93
|
+
)
|
|
94
|
+
key_info = default_auth_manager.validate_api_key(token)
|
|
95
|
+
if not key_info:
|
|
96
|
+
raise HTTPException(
|
|
97
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
98
|
+
detail="Invalid or revoked API key.",
|
|
99
|
+
)
|
|
100
|
+
req_interface = x_interface_type.strip().lower() if isinstance(x_interface_type, str) and x_interface_type.strip() else "rest"
|
|
101
|
+
if not default_auth_manager.has_interface(key_info, req_interface):
|
|
102
|
+
raise HTTPException(
|
|
103
|
+
status_code=status.HTTP_403_FORBIDDEN,
|
|
104
|
+
detail=f"API key lacks permission for interface '{req_interface}'.",
|
|
105
|
+
)
|
|
106
|
+
if required_scopes and not any(default_auth_manager.has_scope(key_info, scope) for scope in required_scopes):
|
|
107
|
+
raise HTTPException(
|
|
108
|
+
status_code=status.HTTP_403_FORBIDDEN,
|
|
109
|
+
detail=f"API key lacks required scope '{required_scopes[0]}'.",
|
|
110
|
+
)
|
|
111
|
+
if rate_format is not None:
|
|
112
|
+
allowed, msg, _metrics = default_auth_manager.check_rate_limit(key_info, format=rate_format)
|
|
113
|
+
if not allowed:
|
|
114
|
+
raise HTTPException(
|
|
115
|
+
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
|
116
|
+
detail=msg or f"Rate limit exceeded for format '{rate_format}'.",
|
|
117
|
+
)
|
|
118
|
+
return key_info
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _relay_json(
|
|
122
|
+
url: str,
|
|
123
|
+
*,
|
|
124
|
+
method: str,
|
|
125
|
+
payload: Optional[dict[str, Any]],
|
|
126
|
+
upstream_token: str,
|
|
127
|
+
timeout: float,
|
|
128
|
+
max_bytes: int,
|
|
129
|
+
) -> tuple[int, dict[str, Any]]:
|
|
130
|
+
headers = {"Accept": "application/json"}
|
|
131
|
+
data: Optional[bytes] = None
|
|
132
|
+
if payload is not None:
|
|
133
|
+
headers["Content-Type"] = "application/json"
|
|
134
|
+
data = json.dumps(payload, allow_nan=False).encode("utf-8")
|
|
135
|
+
if upstream_token:
|
|
136
|
+
headers["Authorization"] = f"Bearer {upstream_token}"
|
|
137
|
+
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
138
|
+
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), _NoRedirect())
|
|
139
|
+
try:
|
|
140
|
+
with opener.open(req, timeout=timeout) as resp:
|
|
141
|
+
raw = resp.read(max_bytes + 1)
|
|
142
|
+
code = resp.status
|
|
143
|
+
except urllib.error.HTTPError as exc:
|
|
144
|
+
with exc:
|
|
145
|
+
raw = exc.read(max_bytes + 1)
|
|
146
|
+
try:
|
|
147
|
+
err_body = json.loads(raw) if len(raw) <= max_bytes else None
|
|
148
|
+
except (ValueError, UnicodeDecodeError):
|
|
149
|
+
err_body = None
|
|
150
|
+
if isinstance(err_body, dict):
|
|
151
|
+
return exc.code, err_body
|
|
152
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Upstream rejected request.")
|
|
153
|
+
except TimeoutError:
|
|
154
|
+
raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail="Inference upstream timed out.")
|
|
155
|
+
except (urllib.error.URLError, OSError):
|
|
156
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Inference upstream is unreachable.")
|
|
157
|
+
if len(raw) > max_bytes:
|
|
158
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Upstream response exceeds size limit.")
|
|
159
|
+
try:
|
|
160
|
+
parsed = json.loads(raw)
|
|
161
|
+
except (ValueError, UnicodeDecodeError):
|
|
162
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Upstream returned invalid JSON.")
|
|
163
|
+
if not isinstance(parsed, dict):
|
|
164
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Upstream returned non-object JSON.")
|
|
165
|
+
return code, parsed
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _validated_timeout(raw: Any, default: float, maximum: float) -> float:
|
|
169
|
+
value = default if raw is None else raw
|
|
170
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)) or not 0 < float(value) <= maximum:
|
|
171
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"timeout must be in (0, {maximum:g}].")
|
|
172
|
+
return float(value)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _resolve_image_url(payload: dict[str, Any]) -> str:
|
|
176
|
+
image_url = payload.get("image_url")
|
|
177
|
+
image_b64 = payload.get("image_base64")
|
|
178
|
+
if isinstance(image_url, str) and image_url.strip():
|
|
179
|
+
url = image_url.strip()
|
|
180
|
+
if not (url.startswith("https://") or url.startswith("data:image/")):
|
|
181
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="image_url must be https:// or data:image/*.")
|
|
182
|
+
if url.startswith("data:image/") and len(url) > MAX_VISION_DATA_URL_CHARS:
|
|
183
|
+
raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="image_url exceeds size limit.")
|
|
184
|
+
return url
|
|
185
|
+
if isinstance(image_b64, str) and image_b64.strip():
|
|
186
|
+
media_type = str(payload.get("media_type") or "").split(";", 1)[0].strip().lower()
|
|
187
|
+
if media_type not in ALLOWED_IMAGE_MEDIA_TYPES:
|
|
188
|
+
raise HTTPException(status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, detail="Unsupported image media_type.")
|
|
189
|
+
cleaned = image_b64.strip()
|
|
190
|
+
if len(cleaned) > MAX_VISION_DATA_URL_CHARS:
|
|
191
|
+
raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="image_base64 exceeds size limit.")
|
|
192
|
+
try:
|
|
193
|
+
raw = base64.b64decode(cleaned, validate=True)
|
|
194
|
+
except (binascii.Error, ValueError):
|
|
195
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid base64 image_base64.")
|
|
196
|
+
if not 16 <= len(raw) <= MAX_VISION_IMAGE_BYTES:
|
|
197
|
+
raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="Image bytes outside supported bounds.")
|
|
198
|
+
return f"data:{media_type};base64,{cleaned}"
|
|
199
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Either image_url or image_base64 with media_type is required.")
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _extract_chat_image_urls(messages: Any) -> list[str]:
|
|
203
|
+
urls: list[str] = []
|
|
204
|
+
if not isinstance(messages, list):
|
|
205
|
+
return urls
|
|
206
|
+
for message in messages:
|
|
207
|
+
if not isinstance(message, dict):
|
|
208
|
+
continue
|
|
209
|
+
content = message.get("content")
|
|
210
|
+
if not isinstance(content, list):
|
|
211
|
+
continue
|
|
212
|
+
for part in content:
|
|
213
|
+
if not isinstance(part, dict) or part.get("type") != "image_url":
|
|
214
|
+
continue
|
|
215
|
+
img = part.get("image_url")
|
|
216
|
+
url = img if isinstance(img, str) else (str(img.get("url") or "") if isinstance(img, dict) else "")
|
|
217
|
+
if not (url.startswith("https://") or url.startswith("data:image/")):
|
|
218
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="image_url must be https:// or data:image/*.")
|
|
219
|
+
if url.startswith("data:image/") and len(url) > MAX_VISION_DATA_URL_CHARS:
|
|
220
|
+
raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="image_url exceeds size limit.")
|
|
221
|
+
urls.append(url)
|
|
222
|
+
return urls
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _extract_chat_text(content: Any) -> str:
|
|
226
|
+
if isinstance(content, str):
|
|
227
|
+
return content
|
|
228
|
+
if isinstance(content, list):
|
|
229
|
+
parts = [str(p.get("text") or "") for p in content if isinstance(p, dict) and p.get("type") == "text"]
|
|
230
|
+
return "\n".join(p for p in parts if p)
|
|
231
|
+
return ""
|
|
232
|
+
|
|
233
|
+
app = FastAPI(
|
|
234
|
+
title="oqllm API & POA Gateway",
|
|
235
|
+
version="0.2.0",
|
|
236
|
+
description="Unified multi-format gateway for Procedure-Oriented Architecture (POA), LLM proxy, API key auth, and mesh federation.",
|
|
237
|
+
docs_url="/docs",
|
|
238
|
+
redoc_url="/redoc",
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
app.add_middleware(
|
|
242
|
+
CORSMiddleware,
|
|
243
|
+
allow_origins=["*"],
|
|
244
|
+
allow_credentials=True,
|
|
245
|
+
allow_methods=["*"],
|
|
246
|
+
allow_headers=["*"],
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
REGISTRY_PATH = Path(__file__).resolve().parents[2] / "schemas" / "registry.json"
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _load_registry() -> dict[str, Any]:
|
|
253
|
+
if REGISTRY_PATH.exists():
|
|
254
|
+
with open(REGISTRY_PATH, encoding="utf-8") as f:
|
|
255
|
+
return json.load(f)
|
|
256
|
+
return {"version": "1.0.0", "total": 0, "operations": []}
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def _authenticate(
|
|
260
|
+
authorization: Optional[str] = None,
|
|
261
|
+
x_api_key: Optional[str] = None,
|
|
262
|
+
x_session_token: Optional[str] = None,
|
|
263
|
+
x_interface_type: Optional[str] = None,
|
|
264
|
+
check_limits: bool = True,
|
|
265
|
+
format: str = "nl",
|
|
266
|
+
required_scope: Optional[str] = None,
|
|
267
|
+
) -> dict[str, Any]:
|
|
268
|
+
token = None
|
|
269
|
+
if isinstance(authorization, str) and authorization.startswith("Bearer "):
|
|
270
|
+
token = authorization[7:].strip()
|
|
271
|
+
elif isinstance(x_api_key, str) and x_api_key:
|
|
272
|
+
token = x_api_key.strip()
|
|
273
|
+
elif isinstance(x_session_token, str) and x_session_token:
|
|
274
|
+
token = x_session_token.strip()
|
|
275
|
+
|
|
276
|
+
if not token:
|
|
277
|
+
raise HTTPException(
|
|
278
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
279
|
+
detail="Authentication required. Please sign in and obtain an API key at https://user.oqllm.com",
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
user_info = default_auth_manager.get_user_by_token(token)
|
|
283
|
+
if not user_info:
|
|
284
|
+
raise HTTPException(
|
|
285
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
286
|
+
detail="Invalid or revoked token / API key.",
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
# Check interface type permission if requested
|
|
290
|
+
req_interface = x_interface_type.strip().lower() if isinstance(x_interface_type, str) and x_interface_type.strip() else "rest"
|
|
291
|
+
if not default_auth_manager.has_interface(user_info, req_interface):
|
|
292
|
+
raise HTTPException(
|
|
293
|
+
status_code=status.HTTP_403_FORBIDDEN,
|
|
294
|
+
detail=f"API key lacks permission for interface '{req_interface}'. Allowed interfaces: {user_info.get('allowed_interfaces', ['rest','mcp','grpc','ws','uds'])}",
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
# Check required scope
|
|
298
|
+
if required_scope and not default_auth_manager.has_scope(user_info, required_scope):
|
|
299
|
+
raise HTTPException(
|
|
300
|
+
status_code=status.HTTP_403_FORBIDDEN,
|
|
301
|
+
detail=f"API key lacks required scope '{required_scope}'. Assigned scopes: {user_info.get('scopes', ['all'])}",
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
# If it is an API key and check_limits is enabled, check rate limit
|
|
305
|
+
if "key_id" in user_info and not str(user_info["key_id"]).startswith("sess_"):
|
|
306
|
+
if check_limits:
|
|
307
|
+
allowed, msg, metrics = default_auth_manager.check_rate_limit(user_info, format=format)
|
|
308
|
+
if not allowed:
|
|
309
|
+
raise HTTPException(
|
|
310
|
+
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
|
311
|
+
detail=msg or "Rate limit exceeded.",
|
|
312
|
+
)
|
|
313
|
+
else:
|
|
314
|
+
user_info["key_id"] = f"sess_{user_info.get('id', 1)}"
|
|
315
|
+
user_info["user_id"] = user_info.get("id", 1)
|
|
316
|
+
|
|
317
|
+
return user_info
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
@app.get("/health/")
|
|
321
|
+
async def health(request: Request) -> dict[str, Any]:
|
|
322
|
+
return {
|
|
323
|
+
"ok": True,
|
|
324
|
+
"service": "oqllm",
|
|
325
|
+
"status": "healthy",
|
|
326
|
+
"version": "0.2.0",
|
|
327
|
+
"domain": request.headers.get("host", "api.oqllm.com"),
|
|
328
|
+
"timestamp": int(time.time()),
|
|
329
|
+
"mesh_enabled": True,
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
# --- Auth Endpoints ---
|
|
334
|
+
|
|
335
|
+
@app.post("/v1/auth/request-code")
|
|
336
|
+
async def request_code(payload: dict[str, Any], request: Request) -> dict[str, Any]:
|
|
337
|
+
email = payload.get("email", "")
|
|
338
|
+
client_ip = request.headers.get("x-forwarded-for", "").split(",")[0].strip() or (request.client.host if request.client else "127.0.0.1")
|
|
339
|
+
try:
|
|
340
|
+
return default_auth_manager.request_code(email, ip_address=client_ip)
|
|
341
|
+
except ValueError as e:
|
|
342
|
+
msg = str(e)
|
|
343
|
+
status_code = status.HTTP_429_TOO_MANY_REQUESTS if "rate limit" in msg.lower() else status.HTTP_400_BAD_REQUEST
|
|
344
|
+
raise HTTPException(status_code=status_code, detail=msg)
|
|
345
|
+
except Exception as e:
|
|
346
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
@app.post("/v1/auth/verify")
|
|
350
|
+
async def verify_code(payload: dict[str, Any]) -> dict[str, Any]:
|
|
351
|
+
email = payload.get("email", "")
|
|
352
|
+
code = payload.get("code", "")
|
|
353
|
+
try:
|
|
354
|
+
return default_auth_manager.verify_code(email, code)
|
|
355
|
+
except Exception as e:
|
|
356
|
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=str(e))
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
@app.get("/v1/auth/keys")
|
|
360
|
+
async def list_keys(x_session_token: Optional[str] = Header(None)) -> dict[str, Any]:
|
|
361
|
+
user = default_auth_manager.get_user_by_session(x_session_token or "")
|
|
362
|
+
if not user:
|
|
363
|
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Valid session required.")
|
|
364
|
+
keys = default_auth_manager.list_api_keys(user["email"])
|
|
365
|
+
return {"ok": True, "email": user["email"], "keys": keys}
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
@app.post("/v1/auth/keys", status_code=status.HTTP_201_CREATED)
|
|
369
|
+
async def create_key(payload: dict[str, Any], x_session_token: Optional[str] = Header(None)) -> dict[str, Any]:
|
|
370
|
+
user = default_auth_manager.get_user_by_session(x_session_token or "")
|
|
371
|
+
if not user:
|
|
372
|
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Valid session required.")
|
|
373
|
+
name = payload.get("name", "Default API Key")
|
|
374
|
+
tier = payload.get("tier", user.get("tier", "developer"))
|
|
375
|
+
format_limits = payload.get("format_limits")
|
|
376
|
+
scopes = payload.get("scopes")
|
|
377
|
+
allowed_interfaces = payload.get("allowed_interfaces")
|
|
378
|
+
transport_policy = payload.get("transport_policy")
|
|
379
|
+
try:
|
|
380
|
+
return default_auth_manager.create_api_key(
|
|
381
|
+
user["email"],
|
|
382
|
+
name=name,
|
|
383
|
+
tier=tier,
|
|
384
|
+
format_limits=format_limits,
|
|
385
|
+
scopes=scopes,
|
|
386
|
+
allowed_interfaces=allowed_interfaces,
|
|
387
|
+
transport_policy=transport_policy,
|
|
388
|
+
)
|
|
389
|
+
except Exception as e:
|
|
390
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
# --- Model Context Protocol (MCP) JSON-RPC 2.0 Endpoint ---
|
|
394
|
+
|
|
395
|
+
@app.post("/mcp")
|
|
396
|
+
@app.post("/mcp/")
|
|
397
|
+
async def mcp_endpoint(
|
|
398
|
+
payload: dict[str, Any],
|
|
399
|
+
authorization: Optional[str] = Header(None),
|
|
400
|
+
x_api_key: Optional[str] = Header(None),
|
|
401
|
+
x_session_token: Optional[str] = Header(None),
|
|
402
|
+
) -> dict[str, Any]:
|
|
403
|
+
user = _authenticate(authorization, x_api_key, x_session_token, x_interface_type="mcp", check_limits=False)
|
|
404
|
+
reg = _load_registry()
|
|
405
|
+
ops = reg.get("operations", [])
|
|
406
|
+
method = payload.get("method", "")
|
|
407
|
+
rpc_id = payload.get("id", 1)
|
|
408
|
+
|
|
409
|
+
if method == "initialize":
|
|
410
|
+
return {
|
|
411
|
+
"jsonrpc": "2.0",
|
|
412
|
+
"id": rpc_id,
|
|
413
|
+
"result": {
|
|
414
|
+
"protocolVersion": "2024-11-05",
|
|
415
|
+
"serverInfo": {"name": "oqllm-mcp", "version": "0.2.0"},
|
|
416
|
+
"capabilities": {"tools": {}, "resources": {}},
|
|
417
|
+
},
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if method == "tools/list":
|
|
421
|
+
tools = []
|
|
422
|
+
for op in ops:
|
|
423
|
+
uri = op.get("uri", "")
|
|
424
|
+
raw_name = uri.replace("proc://", "").replace("urn:", "").replace("subllm://", "").replace("dockuri://", "")
|
|
425
|
+
name = re.sub(r'[^a-zA-Z0-9_-]', '_', raw_name).strip('_')
|
|
426
|
+
tools.append({
|
|
427
|
+
"name": name,
|
|
428
|
+
"description": op.get("desc", f"Execute {uri}"),
|
|
429
|
+
"inputSchema": {
|
|
430
|
+
"type": "object",
|
|
431
|
+
"properties": {
|
|
432
|
+
"query": {"type": "string", "default": uri},
|
|
433
|
+
},
|
|
434
|
+
},
|
|
435
|
+
})
|
|
436
|
+
return {"jsonrpc": "2.0", "id": rpc_id, "result": {"tools": tools}}
|
|
437
|
+
|
|
438
|
+
if method == "tools/call":
|
|
439
|
+
params = payload.get("params", {})
|
|
440
|
+
tool_name = params.get("name", "")
|
|
441
|
+
args = params.get("arguments", {})
|
|
442
|
+
query = args.get("query") or tool_name.replace("_", "/")
|
|
443
|
+
return {
|
|
444
|
+
"jsonrpc": "2.0",
|
|
445
|
+
"id": rpc_id,
|
|
446
|
+
"result": {
|
|
447
|
+
"content": [{"type": "text", "text": f"Dispatched MCP operation '{tool_name}' ({query})"}],
|
|
448
|
+
},
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return {"jsonrpc": "2.0", "id": rpc_id, "error": {"code": -32601, "message": f"Method '{method}' not found"}}
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
@app.delete("/v1/auth/keys/{key_id}")
|
|
455
|
+
async def revoke_key(key_id: str, x_session_token: Optional[str] = Header(None)) -> dict[str, Any]:
|
|
456
|
+
user = default_auth_manager.get_user_by_session(x_session_token or "")
|
|
457
|
+
if not user:
|
|
458
|
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Valid session required.")
|
|
459
|
+
if default_auth_manager.revoke_api_key(user["email"], key_id):
|
|
460
|
+
return {"ok": True, "revoked_key_id": key_id}
|
|
461
|
+
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Key not found or already revoked.")
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
@app.get("/v1/auth/usage")
|
|
465
|
+
async def get_usage(
|
|
466
|
+
authorization: Optional[str] = Header(None),
|
|
467
|
+
x_api_key: Optional[str] = Header(None),
|
|
468
|
+
x_session_token: Optional[str] = Header(None),
|
|
469
|
+
) -> dict[str, Any]:
|
|
470
|
+
user = _authenticate(authorization, x_api_key, x_session_token, required_scope="user:config")
|
|
471
|
+
user_id = user.get("user_id") or user.get("id", 1)
|
|
472
|
+
return default_auth_manager.get_user_usage_and_quotas(user_id)
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
# --- Mesh Endpoints ---
|
|
476
|
+
|
|
477
|
+
@app.post("/v1/mesh/register")
|
|
478
|
+
@app.post("/v1/mesh/")
|
|
479
|
+
@app.post("/v1/mesh")
|
|
480
|
+
@app.post("/v1/mesh/nodes")
|
|
481
|
+
async def register_mesh_node(
|
|
482
|
+
payload: dict[str, Any],
|
|
483
|
+
authorization: Optional[str] = Header(None),
|
|
484
|
+
x_api_key: Optional[str] = Header(None),
|
|
485
|
+
x_session_token: Optional[str] = Header(None),
|
|
486
|
+
) -> dict[str, Any]:
|
|
487
|
+
user = _authenticate(authorization, x_api_key, x_session_token, required_scope="mesh:manage")
|
|
488
|
+
user_id = user.get("user_id") or user.get("id", 1)
|
|
489
|
+
return default_auth_manager.register_mesh_node(
|
|
490
|
+
user_id=user_id,
|
|
491
|
+
node_id=payload.get("node_id", f"node-{secrets.token_hex(4)}"),
|
|
492
|
+
hostname=payload.get("hostname", "unknown"),
|
|
493
|
+
ip=payload.get("ip", "127.0.0.1"),
|
|
494
|
+
port=int(payload.get("port", 11437)),
|
|
495
|
+
capabilities=payload.get("capabilities", []),
|
|
496
|
+
ping_ms=float(payload.get("ping_ms", 0.0)),
|
|
497
|
+
)
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
@app.get("/v1/mesh/nodes")
|
|
501
|
+
@app.get("/v1/mesh/")
|
|
502
|
+
@app.get("/v1/mesh")
|
|
503
|
+
async def list_mesh_nodes(
|
|
504
|
+
authorization: Optional[str] = Header(None),
|
|
505
|
+
x_api_key: Optional[str] = Header(None),
|
|
506
|
+
x_session_token: Optional[str] = Header(None),
|
|
507
|
+
) -> dict[str, Any]:
|
|
508
|
+
user = _authenticate(authorization, x_api_key, x_session_token, required_scope="mesh:manage")
|
|
509
|
+
user_id = user.get("user_id") or user.get("id", 1)
|
|
510
|
+
nodes = default_auth_manager.list_mesh_nodes(user_id)
|
|
511
|
+
return {"ok": True, "count": len(nodes), "nodes": nodes}
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
# --- Models & POA Endpoints ---
|
|
515
|
+
|
|
516
|
+
@app.get("/v1/models/")
|
|
517
|
+
async def list_models() -> dict[str, Any]:
|
|
518
|
+
return {
|
|
519
|
+
"object": "list",
|
|
520
|
+
"data": [
|
|
521
|
+
{
|
|
522
|
+
"id": "oqlos/system-one-router",
|
|
523
|
+
"object": "model",
|
|
524
|
+
"created": 1793881200,
|
|
525
|
+
"owned_by": "oqlos",
|
|
526
|
+
},
|
|
527
|
+
{
|
|
528
|
+
"id": "subllm/auto",
|
|
529
|
+
"object": "model",
|
|
530
|
+
"created": 1793881200,
|
|
531
|
+
"owned_by": "subllm",
|
|
532
|
+
},
|
|
533
|
+
{
|
|
534
|
+
"id": "poa/v1",
|
|
535
|
+
"object": "model",
|
|
536
|
+
"created": 1793881200,
|
|
537
|
+
"owned_by": "heyasa",
|
|
538
|
+
},
|
|
539
|
+
],
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
@app.post("/v1/chat/completions")
|
|
544
|
+
@app.post("/v1/chat/completions/")
|
|
545
|
+
@app.post("/chat/completions")
|
|
546
|
+
async def chat_completions(
|
|
547
|
+
payload: dict[str, Any],
|
|
548
|
+
authorization: Optional[str] = Header(None),
|
|
549
|
+
x_api_key: Optional[str] = Header(None),
|
|
550
|
+
x_session_token: Optional[str] = Header(None),
|
|
551
|
+
x_interface_type: Optional[str] = Header(None),
|
|
552
|
+
) -> Any:
|
|
553
|
+
messages = payload.get("messages", [])
|
|
554
|
+
image_urls = _extract_chat_image_urls(messages)
|
|
555
|
+
if image_urls:
|
|
556
|
+
key_info = _authenticate_api_key_only(
|
|
557
|
+
authorization,
|
|
558
|
+
x_api_key,
|
|
559
|
+
x_interface_type=x_interface_type or "rest",
|
|
560
|
+
required_scopes=("vision", "dsl"),
|
|
561
|
+
rate_format="nl",
|
|
562
|
+
)
|
|
563
|
+
upstream_url, upstream_token = _upstream_target("VISION", "/v1/chat/completions")
|
|
564
|
+
if upstream_url is None:
|
|
565
|
+
raise HTTPException(
|
|
566
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
567
|
+
detail="No vision inference upstream is configured on this host.",
|
|
568
|
+
)
|
|
569
|
+
timeout = _validated_timeout(payload.get("timeout"), 120.0, 600.0)
|
|
570
|
+
t0 = time.perf_counter()
|
|
571
|
+
code, reply = _relay_json(
|
|
572
|
+
upstream_url,
|
|
573
|
+
method="POST",
|
|
574
|
+
payload=payload,
|
|
575
|
+
upstream_token=upstream_token,
|
|
576
|
+
timeout=timeout + 5.0,
|
|
577
|
+
max_bytes=MAX_VISION_RESPONSE_BYTES,
|
|
578
|
+
)
|
|
579
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000, 2)
|
|
580
|
+
usage = reply.get("usage") if isinstance(reply.get("usage"), dict) else {}
|
|
581
|
+
default_auth_manager.record_usage(
|
|
582
|
+
user_id=key_info.get("user_id", 1),
|
|
583
|
+
key_id=key_info.get("key_id", "unknown"),
|
|
584
|
+
endpoint="/v1/chat/completions",
|
|
585
|
+
query_text="",
|
|
586
|
+
format="nl",
|
|
587
|
+
selected_operation=str(payload.get("model") or "vision"),
|
|
588
|
+
input_tokens=int(usage.get("prompt_tokens", usage.get("input_tokens", 0)) or 0),
|
|
589
|
+
output_tokens=int(usage.get("completion_tokens", usage.get("output_tokens", 0)) or 0),
|
|
590
|
+
duration_ms=elapsed_ms,
|
|
591
|
+
status="ok" if code == 200 else "error",
|
|
592
|
+
)
|
|
593
|
+
return JSONResponse(status_code=code, content=reply)
|
|
594
|
+
|
|
595
|
+
_authenticate(
|
|
596
|
+
authorization,
|
|
597
|
+
x_api_key,
|
|
598
|
+
x_session_token,
|
|
599
|
+
x_interface_type=x_interface_type or "rest",
|
|
600
|
+
check_limits=True,
|
|
601
|
+
format="dsl",
|
|
602
|
+
required_scope="dsl",
|
|
603
|
+
)
|
|
604
|
+
model_name = payload.get("model", "oqlos/system-one-router")
|
|
605
|
+
user_query = ""
|
|
606
|
+
if isinstance(messages, list):
|
|
607
|
+
for m in reversed(messages):
|
|
608
|
+
if isinstance(m, dict) and m.get("role") == "user":
|
|
609
|
+
content = m.get("content", "")
|
|
610
|
+
user_query = content if isinstance(content, str) else json.dumps(content)
|
|
611
|
+
break
|
|
612
|
+
if not user_query:
|
|
613
|
+
user_query = payload.get("prompt", "")
|
|
614
|
+
|
|
615
|
+
t0 = time.perf_counter()
|
|
616
|
+
reg = _load_registry()
|
|
617
|
+
ops = reg.get("operations", [])
|
|
618
|
+
norm = user_query.strip().lower()
|
|
619
|
+
matched = None
|
|
620
|
+
for op in ops:
|
|
621
|
+
if norm == op.get("uri", "").lower():
|
|
622
|
+
matched = op
|
|
623
|
+
break
|
|
624
|
+
for tr in op.get("triggers", []):
|
|
625
|
+
if tr.lower() in norm:
|
|
626
|
+
matched = op
|
|
627
|
+
break
|
|
628
|
+
if matched:
|
|
629
|
+
break
|
|
630
|
+
|
|
631
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000, 2)
|
|
632
|
+
if matched:
|
|
633
|
+
resp_content = json.dumps({
|
|
634
|
+
"action": "route",
|
|
635
|
+
"selected_operation": matched.get("uri"),
|
|
636
|
+
"system": matched.get("system", "oqllm"),
|
|
637
|
+
"confidence": 0.95,
|
|
638
|
+
"decision_ms": elapsed_ms,
|
|
639
|
+
"description": matched.get("desc", ""),
|
|
640
|
+
}, indent=2)
|
|
641
|
+
else:
|
|
642
|
+
resp_content = json.dumps({
|
|
643
|
+
"action": "route",
|
|
644
|
+
"selected_operation": None,
|
|
645
|
+
"query": user_query,
|
|
646
|
+
"status": "unresolved_nl",
|
|
647
|
+
"decision_ms": elapsed_ms,
|
|
648
|
+
}, indent=2)
|
|
649
|
+
|
|
650
|
+
in_tok = max(1, len(user_query.split()))
|
|
651
|
+
out_tok = max(1, len(resp_content.split()))
|
|
652
|
+
|
|
653
|
+
return {
|
|
654
|
+
"id": f"chatcmpl-{secrets.token_hex(8)}",
|
|
655
|
+
"object": "chat.completion",
|
|
656
|
+
"created": int(time.time()),
|
|
657
|
+
"model": model_name,
|
|
658
|
+
"choices": [
|
|
659
|
+
{
|
|
660
|
+
"index": 0,
|
|
661
|
+
"message": {
|
|
662
|
+
"role": "assistant",
|
|
663
|
+
"content": resp_content,
|
|
664
|
+
},
|
|
665
|
+
"finish_reason": "stop",
|
|
666
|
+
}
|
|
667
|
+
],
|
|
668
|
+
"usage": {
|
|
669
|
+
"prompt_tokens": in_tok,
|
|
670
|
+
"completion_tokens": out_tok,
|
|
671
|
+
"total_tokens": in_tok + out_tok,
|
|
672
|
+
},
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
@app.get("/v1/poa/")
|
|
677
|
+
@app.get("/v1/poa")
|
|
678
|
+
async def get_poa_registry() -> dict[str, Any]:
|
|
679
|
+
return _load_registry()
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
@app.post("/v1/poa/")
|
|
683
|
+
@app.post("/v1/poa")
|
|
684
|
+
@app.post("/v1/poa/route")
|
|
685
|
+
@app.post("/v1/poa/route/")
|
|
686
|
+
async def route_poa(
|
|
687
|
+
payload: dict[str, Any],
|
|
688
|
+
action: str = Query("route"),
|
|
689
|
+
authorization: Optional[str] = Header(None),
|
|
690
|
+
x_api_key: Optional[str] = Header(None),
|
|
691
|
+
x_session_token: Optional[str] = Header(None),
|
|
692
|
+
x_interface_type: Optional[str] = Header(None),
|
|
693
|
+
) -> dict[str, Any]:
|
|
694
|
+
key_info = _authenticate(authorization, x_api_key, x_session_token, x_interface_type=x_interface_type, check_limits=False)
|
|
695
|
+
t0 = time.perf_counter()
|
|
696
|
+
reg = _load_registry()
|
|
697
|
+
ops = reg.get("operations", [])
|
|
698
|
+
|
|
699
|
+
raw_query = payload.get("query") or payload.get("input") or ""
|
|
700
|
+
detected_format = payload.get("format") or "auto"
|
|
701
|
+
norm = raw_query.strip().lower()
|
|
702
|
+
|
|
703
|
+
matched = None
|
|
704
|
+
confidence = 0.5
|
|
705
|
+
|
|
706
|
+
if norm.startswith(("proc://", "app://", "api://")):
|
|
707
|
+
detected_format = "uri"
|
|
708
|
+
for op in ops:
|
|
709
|
+
if op.get("uri") == norm or norm in op.get("aliases", []):
|
|
710
|
+
matched = op
|
|
711
|
+
confidence = 1.0
|
|
712
|
+
break
|
|
713
|
+
elif norm.startswith("urn:"):
|
|
714
|
+
detected_format = "urn"
|
|
715
|
+
for op in ops:
|
|
716
|
+
if op.get("urn") == norm:
|
|
717
|
+
matched = op
|
|
718
|
+
confidence = 1.0
|
|
719
|
+
break
|
|
720
|
+
else:
|
|
721
|
+
best_score = 0
|
|
722
|
+
for op in ops:
|
|
723
|
+
score = sum(10 for t in op.get("triggers", []) if t.lower() in norm)
|
|
724
|
+
if score > best_score:
|
|
725
|
+
best_score = score
|
|
726
|
+
matched = op
|
|
727
|
+
confidence = min(0.95, 0.6 + (score * 0.05))
|
|
728
|
+
|
|
729
|
+
if norm.startswith(("hey asa", "asa")):
|
|
730
|
+
detected_format = "voice"
|
|
731
|
+
else:
|
|
732
|
+
detected_format = "nl"
|
|
733
|
+
|
|
734
|
+
# Enforce scope authorization for the detected channel/format
|
|
735
|
+
if not default_auth_manager.has_scope(key_info, detected_format):
|
|
736
|
+
raise HTTPException(
|
|
737
|
+
status_code=status.HTTP_403_FORBIDDEN,
|
|
738
|
+
detail=f"API key lacks required scope '{detected_format}' for this channel. Assigned scopes: {key_info.get('scopes', ['all'])}",
|
|
739
|
+
)
|
|
740
|
+
|
|
741
|
+
# Enforce format-specific and global rate limits
|
|
742
|
+
if "key_id" in key_info and not str(key_info["key_id"]).startswith("sess_"):
|
|
743
|
+
allowed, msg, rate_metrics = default_auth_manager.check_rate_limit(key_info, format=detected_format)
|
|
744
|
+
if not allowed:
|
|
745
|
+
headers = {
|
|
746
|
+
"X-RateLimit-Limit": str(rate_metrics.get("rpm_limit", 120)),
|
|
747
|
+
"X-RateLimit-Remaining": str(rate_metrics.get("global_remaining", 0)),
|
|
748
|
+
f"X-RateLimit-Limit-{detected_format.capitalize()}": str(rate_metrics.get("format_limit", 60)),
|
|
749
|
+
f"X-RateLimit-Remaining-{detected_format.capitalize()}": str(rate_metrics.get("format_remaining", 0)),
|
|
750
|
+
"X-RateLimit-Format": detected_format,
|
|
751
|
+
}
|
|
752
|
+
raise HTTPException(
|
|
753
|
+
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
|
754
|
+
detail=msg or f"Rate limit exceeded for format '{detected_format}'.",
|
|
755
|
+
headers=headers,
|
|
756
|
+
)
|
|
757
|
+
|
|
758
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000, 2)
|
|
759
|
+
user_id = key_info.get("user_id") or key_info.get("id", 1)
|
|
760
|
+
key_id = key_info.get("key_id", "sess")
|
|
761
|
+
|
|
762
|
+
# Record user usage journal
|
|
763
|
+
default_auth_manager.record_usage(
|
|
764
|
+
user_id=user_id,
|
|
765
|
+
key_id=key_id,
|
|
766
|
+
endpoint="/v1/poa/dispatch" if (action == "dispatch" or "dispatch" in payload) else "/v1/poa/route",
|
|
767
|
+
query_text=raw_query,
|
|
768
|
+
format=detected_format,
|
|
769
|
+
selected_operation=matched.get("uri") if matched else None,
|
|
770
|
+
input_tokens=max(1, len(raw_query) // 4),
|
|
771
|
+
output_tokens=15 if (action == "dispatch" or "dispatch" in payload) else 10,
|
|
772
|
+
duration_ms=elapsed_ms,
|
|
773
|
+
status="queued" if (action == "dispatch" or "dispatch" in payload) else "ok",
|
|
774
|
+
)
|
|
775
|
+
|
|
776
|
+
if action == "dispatch" or "dispatch" in payload:
|
|
777
|
+
return {
|
|
778
|
+
"ok": True,
|
|
779
|
+
"action": "dispatch",
|
|
780
|
+
"query": raw_query,
|
|
781
|
+
"format": detected_format,
|
|
782
|
+
"decision_ms": elapsed_ms,
|
|
783
|
+
"selected_operation": matched.get("uri") if matched else None,
|
|
784
|
+
"operation": matched,
|
|
785
|
+
"status": "queued",
|
|
786
|
+
"dispatch_id": f"disp-{int(time.time()*1000):x}",
|
|
787
|
+
"target_system": matched.get("system") if matched else None,
|
|
788
|
+
"user_key": key_id,
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
return {
|
|
792
|
+
"ok": True,
|
|
793
|
+
"action": "route",
|
|
794
|
+
"query": raw_query,
|
|
795
|
+
"detected_format": detected_format,
|
|
796
|
+
"confidence": confidence,
|
|
797
|
+
"decision_ms": elapsed_ms,
|
|
798
|
+
"selected_operation": matched.get("uri") if matched else None,
|
|
799
|
+
"operation": matched,
|
|
800
|
+
"user_key": key_id,
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
@app.post("/v1/poa/dispatch")
|
|
805
|
+
@app.post("/v1/poa/dispatch/")
|
|
806
|
+
async def dispatch_poa(
|
|
807
|
+
payload: dict[str, Any],
|
|
808
|
+
authorization: Optional[str] = Header(None),
|
|
809
|
+
x_api_key: Optional[str] = Header(None),
|
|
810
|
+
x_session_token: Optional[str] = Header(None),
|
|
811
|
+
x_interface_type: Optional[str] = Header(None),
|
|
812
|
+
) -> dict[str, Any]:
|
|
813
|
+
return await route_poa(payload, action="dispatch", authorization=authorization, x_api_key=x_api_key, x_session_token=x_session_token, x_interface_type=x_interface_type)
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
@app.get("/v1/ai/capabilities")
|
|
817
|
+
@app.get("/v1/ai/capabilities/")
|
|
818
|
+
async def ai_capabilities(
|
|
819
|
+
authorization: Optional[str] = Header(None),
|
|
820
|
+
x_api_key: Optional[str] = Header(None),
|
|
821
|
+
x_interface_type: Optional[str] = Header(None),
|
|
822
|
+
) -> Any:
|
|
823
|
+
_authenticate_api_key_only(
|
|
824
|
+
authorization,
|
|
825
|
+
x_api_key,
|
|
826
|
+
x_interface_type=x_interface_type,
|
|
827
|
+
required_scopes=("voice",),
|
|
828
|
+
rate_format=None,
|
|
829
|
+
)
|
|
830
|
+
upstream_url, upstream_token = _upstream_target("SPEECH", "/v1/ai/capabilities")
|
|
831
|
+
if upstream_url is None:
|
|
832
|
+
raise HTTPException(
|
|
833
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
834
|
+
detail="No speech upstream is configured on this host.",
|
|
835
|
+
)
|
|
836
|
+
code, reply = _relay_json(
|
|
837
|
+
upstream_url,
|
|
838
|
+
method="GET",
|
|
839
|
+
payload=None,
|
|
840
|
+
upstream_token=upstream_token,
|
|
841
|
+
timeout=30.0,
|
|
842
|
+
max_bytes=MAX_SPEECH_RESPONSE_BYTES,
|
|
843
|
+
)
|
|
844
|
+
if code == 200 and reply.get("schema") != "subllm.ai-capabilities/v1":
|
|
845
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid capabilities schema from upstream.")
|
|
846
|
+
return JSONResponse(status_code=code, content=reply)
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
@app.post("/v1/speech/transcribe")
|
|
850
|
+
@app.post("/v1/speech/transcribe/")
|
|
851
|
+
async def speech_transcribe(
|
|
852
|
+
payload: dict[str, Any],
|
|
853
|
+
authorization: Optional[str] = Header(None),
|
|
854
|
+
x_api_key: Optional[str] = Header(None),
|
|
855
|
+
x_interface_type: Optional[str] = Header(None),
|
|
856
|
+
) -> Any:
|
|
857
|
+
key_info = _authenticate_api_key_only(
|
|
858
|
+
authorization,
|
|
859
|
+
x_api_key,
|
|
860
|
+
x_interface_type=x_interface_type,
|
|
861
|
+
required_scopes=("voice",),
|
|
862
|
+
rate_format="voice",
|
|
863
|
+
)
|
|
864
|
+
model = payload.get("model")
|
|
865
|
+
if not isinstance(model, str) or not model.strip() or len(model) > 2048:
|
|
866
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="A model identifier is required.")
|
|
867
|
+
encoded = payload.get("audio_base64")
|
|
868
|
+
if not isinstance(encoded, str) or not encoded.strip():
|
|
869
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="audio_base64 is required.")
|
|
870
|
+
if len(encoded) > (MAX_SPEECH_AUDIO_BYTES * 4 // 3 + 16):
|
|
871
|
+
raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="audio_base64 exceeds size limit.")
|
|
872
|
+
try:
|
|
873
|
+
raw_audio = base64.b64decode(encoded, validate=True)
|
|
874
|
+
except (binascii.Error, ValueError):
|
|
875
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid base64 audio_base64.")
|
|
876
|
+
if not 44 <= len(raw_audio) <= MAX_SPEECH_AUDIO_BYTES:
|
|
877
|
+
raise HTTPException(status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, detail="Audio size outside supported bounds.")
|
|
878
|
+
media_type = payload.get("media_type")
|
|
879
|
+
if media_type is not None:
|
|
880
|
+
normalized_mt = str(media_type).split(";", 1)[0].strip().lower()
|
|
881
|
+
if normalized_mt not in ALLOWED_SPEECH_MEDIA_TYPES:
|
|
882
|
+
raise HTTPException(status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, detail="Unsupported audio media_type.")
|
|
883
|
+
timeout = _validated_timeout(payload.get("timeout"), 120.0, 600.0)
|
|
884
|
+
upstream_url, upstream_token = _upstream_target("SPEECH", "/v1/speech/transcribe")
|
|
885
|
+
if upstream_url is None:
|
|
886
|
+
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="No speech upstream is configured on this host.")
|
|
887
|
+
t0 = time.perf_counter()
|
|
888
|
+
code, reply = _relay_json(
|
|
889
|
+
upstream_url,
|
|
890
|
+
method="POST",
|
|
891
|
+
payload=payload,
|
|
892
|
+
upstream_token=upstream_token,
|
|
893
|
+
timeout=timeout + 5.0,
|
|
894
|
+
max_bytes=MAX_SPEECH_RESPONSE_BYTES,
|
|
895
|
+
)
|
|
896
|
+
if code == 200 and not isinstance(reply.get("text"), str):
|
|
897
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Speech upstream returned invalid transcription.")
|
|
898
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000, 2)
|
|
899
|
+
default_auth_manager.record_usage(
|
|
900
|
+
user_id=key_info.get("user_id", 1),
|
|
901
|
+
key_id=key_info.get("key_id", "unknown"),
|
|
902
|
+
endpoint="/v1/speech/transcribe",
|
|
903
|
+
query_text="",
|
|
904
|
+
format="voice",
|
|
905
|
+
selected_operation=model.strip(),
|
|
906
|
+
input_tokens=0,
|
|
907
|
+
output_tokens=0,
|
|
908
|
+
duration_ms=elapsed_ms,
|
|
909
|
+
status="ok" if code == 200 else "error",
|
|
910
|
+
)
|
|
911
|
+
return JSONResponse(status_code=code, content=reply)
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
@app.post("/v1/speech/synthesize")
|
|
915
|
+
@app.post("/v1/speech/synthesize/")
|
|
916
|
+
async def speech_synthesize(
|
|
917
|
+
payload: dict[str, Any],
|
|
918
|
+
authorization: Optional[str] = Header(None),
|
|
919
|
+
x_api_key: Optional[str] = Header(None),
|
|
920
|
+
x_interface_type: Optional[str] = Header(None),
|
|
921
|
+
) -> Any:
|
|
922
|
+
key_info = _authenticate_api_key_only(
|
|
923
|
+
authorization,
|
|
924
|
+
x_api_key,
|
|
925
|
+
x_interface_type=x_interface_type,
|
|
926
|
+
required_scopes=("voice",),
|
|
927
|
+
rate_format="voice",
|
|
928
|
+
)
|
|
929
|
+
model = payload.get("model")
|
|
930
|
+
if not isinstance(model, str) or not model.strip() or len(model) > 2048:
|
|
931
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="A model identifier is required.")
|
|
932
|
+
text = payload.get("text")
|
|
933
|
+
if not isinstance(text, str) or not text.strip() or len(text) > 4096:
|
|
934
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="text must contain 1 to 4096 characters.")
|
|
935
|
+
timeout = _validated_timeout(payload.get("timeout"), 120.0, 600.0)
|
|
936
|
+
upstream_url, upstream_token = _upstream_target("SPEECH", "/v1/speech/synthesize")
|
|
937
|
+
if upstream_url is None:
|
|
938
|
+
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="No speech upstream is configured on this host.")
|
|
939
|
+
t0 = time.perf_counter()
|
|
940
|
+
code, reply = _relay_json(
|
|
941
|
+
upstream_url,
|
|
942
|
+
method="POST",
|
|
943
|
+
payload=payload,
|
|
944
|
+
upstream_token=upstream_token,
|
|
945
|
+
timeout=timeout + 5.0,
|
|
946
|
+
max_bytes=MAX_SPEECH_RESPONSE_BYTES,
|
|
947
|
+
)
|
|
948
|
+
if code == 200 and (not isinstance(reply.get("audio_base64"), str) or not reply["audio_base64"]):
|
|
949
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Speech upstream returned invalid synthesis audio.")
|
|
950
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000, 2)
|
|
951
|
+
default_auth_manager.record_usage(
|
|
952
|
+
user_id=key_info.get("user_id", 1),
|
|
953
|
+
key_id=key_info.get("key_id", "unknown"),
|
|
954
|
+
endpoint="/v1/speech/synthesize",
|
|
955
|
+
query_text="",
|
|
956
|
+
format="voice",
|
|
957
|
+
selected_operation=model.strip(),
|
|
958
|
+
input_tokens=0,
|
|
959
|
+
output_tokens=0,
|
|
960
|
+
duration_ms=elapsed_ms,
|
|
961
|
+
status="ok" if code == 200 else "error",
|
|
962
|
+
)
|
|
963
|
+
return JSONResponse(status_code=code, content=reply)
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
@app.post("/v1/vision/analyze")
|
|
967
|
+
@app.post("/v1/vision/analyze/")
|
|
968
|
+
async def vision_analyze(
|
|
969
|
+
payload: dict[str, Any],
|
|
970
|
+
authorization: Optional[str] = Header(None),
|
|
971
|
+
x_api_key: Optional[str] = Header(None),
|
|
972
|
+
x_interface_type: Optional[str] = Header(None),
|
|
973
|
+
) -> Any:
|
|
974
|
+
key_info = _authenticate_api_key_only(
|
|
975
|
+
authorization,
|
|
976
|
+
x_api_key,
|
|
977
|
+
x_interface_type=x_interface_type,
|
|
978
|
+
required_scopes=("vision", "dsl"),
|
|
979
|
+
rate_format="nl",
|
|
980
|
+
)
|
|
981
|
+
model = payload.get("model")
|
|
982
|
+
if not isinstance(model, str) or not model.strip() or len(model) > 2048:
|
|
983
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="A model identifier is required.")
|
|
984
|
+
prompt = payload.get("prompt")
|
|
985
|
+
if not isinstance(prompt, str) or not prompt.strip() or len(prompt) > 8192:
|
|
986
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="prompt must contain 1 to 8192 characters.")
|
|
987
|
+
detail = payload.get("detail", "auto")
|
|
988
|
+
if detail not in ("auto", "low", "high"):
|
|
989
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="detail must be one of auto, low, high.")
|
|
990
|
+
max_tokens = payload.get("max_tokens", 512)
|
|
991
|
+
if isinstance(max_tokens, bool) or not isinstance(max_tokens, int) or not 1 <= max_tokens <= 4096:
|
|
992
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="max_tokens must be in [1, 4096].")
|
|
993
|
+
timeout = _validated_timeout(payload.get("timeout"), 120.0, 600.0)
|
|
994
|
+
resolved_url = _resolve_image_url(payload)
|
|
995
|
+
|
|
996
|
+
raw_env = (os.environ.get("OQLLM_VISION_UPSTREAM_URL") or os.environ.get("OQLLM_INFERENCE_UPSTREAM_URL") or "").strip()
|
|
997
|
+
direct_vision = raw_env.rstrip("/").endswith("/v1/vision/analyze")
|
|
998
|
+
default_path = "/v1/vision/analyze" if direct_vision else "/v1/chat/completions"
|
|
999
|
+
upstream_url, upstream_token = _upstream_target("VISION", default_path)
|
|
1000
|
+
if upstream_url is None:
|
|
1001
|
+
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="No vision upstream is configured on this host.")
|
|
1002
|
+
|
|
1003
|
+
if direct_vision:
|
|
1004
|
+
fwd_payload = {
|
|
1005
|
+
"model": model.strip(),
|
|
1006
|
+
"prompt": prompt,
|
|
1007
|
+
"image_url": resolved_url,
|
|
1008
|
+
"detail": detail,
|
|
1009
|
+
"max_tokens": max_tokens,
|
|
1010
|
+
"timeout": timeout,
|
|
1011
|
+
}
|
|
1012
|
+
else:
|
|
1013
|
+
fwd_payload = {
|
|
1014
|
+
"model": model.strip(),
|
|
1015
|
+
"messages": [
|
|
1016
|
+
{
|
|
1017
|
+
"role": "user",
|
|
1018
|
+
"content": [
|
|
1019
|
+
{"type": "text", "text": prompt},
|
|
1020
|
+
{"type": "image_url", "image_url": {"url": resolved_url, "detail": detail}},
|
|
1021
|
+
],
|
|
1022
|
+
}
|
|
1023
|
+
],
|
|
1024
|
+
"max_tokens": max_tokens,
|
|
1025
|
+
"stream": False,
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
t0 = time.perf_counter()
|
|
1029
|
+
code, reply = _relay_json(
|
|
1030
|
+
upstream_url,
|
|
1031
|
+
method="POST",
|
|
1032
|
+
payload=fwd_payload,
|
|
1033
|
+
upstream_token=upstream_token,
|
|
1034
|
+
timeout=timeout + 5.0,
|
|
1035
|
+
max_bytes=MAX_VISION_RESPONSE_BYTES,
|
|
1036
|
+
)
|
|
1037
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000, 2)
|
|
1038
|
+
usage = reply.get("usage") if isinstance(reply.get("usage"), dict) else {}
|
|
1039
|
+
in_tok = int(usage.get("input_tokens", usage.get("prompt_tokens", 0)) or 0)
|
|
1040
|
+
out_tok = int(usage.get("output_tokens", usage.get("completion_tokens", 0)) or 0)
|
|
1041
|
+
|
|
1042
|
+
if code != 200:
|
|
1043
|
+
default_auth_manager.record_usage(
|
|
1044
|
+
user_id=key_info.get("user_id", 1),
|
|
1045
|
+
key_id=key_info.get("key_id", "unknown"),
|
|
1046
|
+
endpoint="/v1/vision/analyze",
|
|
1047
|
+
query_text="",
|
|
1048
|
+
format="nl",
|
|
1049
|
+
selected_operation=model.strip(),
|
|
1050
|
+
input_tokens=in_tok,
|
|
1051
|
+
output_tokens=out_tok,
|
|
1052
|
+
duration_ms=elapsed_ms,
|
|
1053
|
+
status="error",
|
|
1054
|
+
)
|
|
1055
|
+
return JSONResponse(status_code=code, content=reply)
|
|
1056
|
+
|
|
1057
|
+
analysis = reply.get("analysis")
|
|
1058
|
+
if not isinstance(analysis, str):
|
|
1059
|
+
choices = reply.get("choices")
|
|
1060
|
+
if isinstance(choices, list) and choices and isinstance(choices[0], dict):
|
|
1061
|
+
msg = choices[0].get("message")
|
|
1062
|
+
if isinstance(msg, dict):
|
|
1063
|
+
analysis = _extract_chat_text(msg.get("content"))
|
|
1064
|
+
if not isinstance(analysis, str) or not analysis.strip():
|
|
1065
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Vision upstream returned empty analysis.")
|
|
1066
|
+
|
|
1067
|
+
default_auth_manager.record_usage(
|
|
1068
|
+
user_id=key_info.get("user_id", 1),
|
|
1069
|
+
key_id=key_info.get("key_id", "unknown"),
|
|
1070
|
+
endpoint="/v1/vision/analyze",
|
|
1071
|
+
query_text="",
|
|
1072
|
+
format="nl",
|
|
1073
|
+
selected_operation=model.strip(),
|
|
1074
|
+
input_tokens=in_tok,
|
|
1075
|
+
output_tokens=out_tok,
|
|
1076
|
+
duration_ms=elapsed_ms,
|
|
1077
|
+
status="ok",
|
|
1078
|
+
)
|
|
1079
|
+
return {
|
|
1080
|
+
"schema": "oqllm.vision-analysis/v1",
|
|
1081
|
+
"ok": True,
|
|
1082
|
+
"model": str(reply.get("model") or model.strip()),
|
|
1083
|
+
"provider": str(reply.get("provider") or "subllm"),
|
|
1084
|
+
"analysis": analysis,
|
|
1085
|
+
"usage": {"input_tokens": in_tok, "output_tokens": out_tok},
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
@app.post("/v1/decide")
|
|
1090
|
+
@app.post("/v1/decide/")
|
|
1091
|
+
async def decide(
|
|
1092
|
+
payload: dict[str, Any],
|
|
1093
|
+
authorization: Optional[str] = Header(None),
|
|
1094
|
+
x_api_key: Optional[str] = Header(None),
|
|
1095
|
+
x_interface_type: Optional[str] = Header(None),
|
|
1096
|
+
) -> Any:
|
|
1097
|
+
key_info = _authenticate_api_key_only(
|
|
1098
|
+
authorization,
|
|
1099
|
+
x_api_key,
|
|
1100
|
+
x_interface_type=x_interface_type,
|
|
1101
|
+
required_scopes=("decide", "dsl"),
|
|
1102
|
+
rate_format="dsl",
|
|
1103
|
+
)
|
|
1104
|
+
questions = payload.get("questions")
|
|
1105
|
+
if not isinstance(questions, list) or not 1 <= len(questions) <= 8:
|
|
1106
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="A JSON object with 1-8 questions is required.")
|
|
1107
|
+
for item in questions:
|
|
1108
|
+
if not isinstance(item, dict) or not isinstance(item.get("id"), str) or not isinstance(item.get("options"), (list, dict)) or len(item["options"]) < 2:
|
|
1109
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Each question needs an id and at least two options.")
|
|
1110
|
+
timeout = _validated_timeout(payload.get("timeout"), 30.0, 120.0)
|
|
1111
|
+
upstream_url, upstream_token = _upstream_target("DECIDE", "/v1/decide")
|
|
1112
|
+
if upstream_url is None:
|
|
1113
|
+
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="No decision upstream is configured on this host.")
|
|
1114
|
+
t0 = time.perf_counter()
|
|
1115
|
+
code, reply = _relay_json(
|
|
1116
|
+
upstream_url,
|
|
1117
|
+
method="POST",
|
|
1118
|
+
payload=payload,
|
|
1119
|
+
upstream_token=upstream_token,
|
|
1120
|
+
timeout=timeout + 5.0,
|
|
1121
|
+
max_bytes=1024 * 1024,
|
|
1122
|
+
)
|
|
1123
|
+
elapsed_ms = round((time.perf_counter() - t0) * 1000, 2)
|
|
1124
|
+
usage = reply.get("usage") if isinstance(reply.get("usage"), dict) else {}
|
|
1125
|
+
in_tok = int(usage.get("input_tokens", usage.get("prompt_tokens", 0)) or 0)
|
|
1126
|
+
out_tok = int(usage.get("output_tokens", usage.get("completion_tokens", 0)) or 0)
|
|
1127
|
+
if code == 200 and (
|
|
1128
|
+
not isinstance(reply.get("model"), str)
|
|
1129
|
+
or not isinstance(reply.get("provider"), str)
|
|
1130
|
+
or not isinstance(reply.get("answers"), dict)
|
|
1131
|
+
or not isinstance(reply.get("probabilities"), dict)
|
|
1132
|
+
):
|
|
1133
|
+
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Decision upstream returned an invalid response.")
|
|
1134
|
+
default_auth_manager.record_usage(
|
|
1135
|
+
user_id=key_info.get("user_id", 1),
|
|
1136
|
+
key_id=key_info.get("key_id", "unknown"),
|
|
1137
|
+
endpoint="/v1/decide",
|
|
1138
|
+
query_text="",
|
|
1139
|
+
format="dsl",
|
|
1140
|
+
selected_operation=str(payload.get("model") or "d1:3b"),
|
|
1141
|
+
input_tokens=in_tok,
|
|
1142
|
+
output_tokens=out_tok,
|
|
1143
|
+
duration_ms=elapsed_ms,
|
|
1144
|
+
status="ok" if code == 200 else "error",
|
|
1145
|
+
)
|
|
1146
|
+
return JSONResponse(status_code=code, content=reply)
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
@app.get("/", response_class=HTMLResponse)
|
|
1150
|
+
async def portal_page() -> Response:
|
|
1151
|
+
portal_path = Path(__file__).resolve().parents[2] / "portal" / "index.html"
|
|
1152
|
+
if portal_path.exists():
|
|
1153
|
+
return HTMLResponse(portal_path.read_text(encoding="utf-8"))
|
|
1154
|
+
return HTMLResponse("<h1>oqllm API Gateway</h1>")
|