360 行
15 KiB
Python
360 行
15 KiB
Python
"""Safe API discovery and render-plan validation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
from typing import Any
|
|
from uuid import uuid4
|
|
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
from core.model_router import SUPPORTED, UNSUPPORTED, route_from_capability_profile, select_model_route
|
|
from database.db_helper import load_config_payload
|
|
from database.repositories.capability_repository import CapabilityRepository
|
|
|
|
|
|
DIALECTS = [
|
|
{
|
|
"dialect": "openai_images",
|
|
"label": "OpenAI Images compatible",
|
|
"capabilities": ["text_to_image", "image_edit", "mask_edit"],
|
|
},
|
|
{
|
|
"dialect": "comfyui",
|
|
"label": "ComfyUI workflow",
|
|
"capabilities": ["text_to_image", "workflow_graph"],
|
|
},
|
|
{
|
|
"dialect": "unknown",
|
|
"label": "Unconfirmed backend",
|
|
"capabilities": [],
|
|
},
|
|
]
|
|
|
|
|
|
class ApiDiscoveryService:
|
|
def __init__(self, repository: CapabilityRepository):
|
|
self.repository = repository
|
|
|
|
def dialects(self) -> list[dict[str, Any]]:
|
|
return DIALECTS
|
|
|
|
def probe_provider(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
if payload.get("use_saved_provider"):
|
|
payload = _saved_provider_probe_payload(payload)
|
|
provider_id = str(payload.get("provider_id") or "").strip()
|
|
model_id = str(payload.get("model_id") or "").strip()
|
|
if not provider_id:
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "provider_id is required", {"field": "provider_id"})
|
|
if not model_id:
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "model_id is required", {"field": "model_id"})
|
|
dialect = str(payload.get("dialect") or "unknown")
|
|
live_result = _live_probe(payload) if payload.get("live_probe") else {
|
|
"mode": "safe",
|
|
"message": "safe probe recorded metadata only; no external API call was made",
|
|
}
|
|
capabilities = dict(payload.get("capabilities") or {})
|
|
if live_result.get("models"):
|
|
capabilities["discovered_models"] = live_result["models"]
|
|
if live_result.get("supported_sizes"):
|
|
capabilities["supported_sizes"] = live_result["supported_sizes"]
|
|
capabilities["max_tested_size"] = live_result["supported_sizes"][-1]
|
|
profile = self.repository.upsert_profile(
|
|
{
|
|
"capability_profile_id": payload.get("capability_profile_id") or new_capability_profile_id(),
|
|
"provider_id": provider_id,
|
|
"model_id": model_id,
|
|
"dialect": dialect,
|
|
"status": "confirmed_supported" if live_result.get("connected") else "unconfirmed",
|
|
"capabilities": capabilities,
|
|
"probe_result": live_result,
|
|
}
|
|
)
|
|
return profile
|
|
|
|
def probe_model(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
profile = self.probe_provider(payload)
|
|
profile["probe_result"] = {
|
|
**profile.get("probe_result", {}),
|
|
"model_probe": "safe_metadata_only",
|
|
}
|
|
return self.repository.update_profile(
|
|
profile["capability_profile_id"],
|
|
{"probe_result": profile["probe_result"]},
|
|
) or profile
|
|
|
|
def manual_override(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
provider_id = str(payload.get("provider_id") or "").strip()
|
|
model_id = str(payload.get("model_id") or "").strip()
|
|
status = str(payload.get("status") or "").strip()
|
|
if status not in {"confirmed_supported", "confirmed_unsupported", "unconfirmed"}:
|
|
raise ApiError(
|
|
ErrorCode.VALIDATION_FAILED,
|
|
"unsupported capability status",
|
|
{"field": "status"},
|
|
)
|
|
profile = self.repository.get_profile(provider_id, model_id)
|
|
if profile is None:
|
|
profile = self.repository.upsert_profile(
|
|
{
|
|
"capability_profile_id": payload.get("capability_profile_id") or new_capability_profile_id(),
|
|
"provider_id": provider_id,
|
|
"model_id": model_id,
|
|
"dialect": payload.get("dialect", "unknown"),
|
|
"status": status,
|
|
"capabilities": payload.get("capabilities") or {},
|
|
"probe_result": {"manual": True},
|
|
}
|
|
)
|
|
return self.repository.update_profile(
|
|
profile["capability_profile_id"],
|
|
{
|
|
"status": status,
|
|
"dialect": payload.get("dialect", profile["dialect"]),
|
|
"capabilities": payload.get("capabilities", profile["capabilities"]),
|
|
"probe_result": {**profile.get("probe_result", {}), "manual_override": True},
|
|
},
|
|
) or profile
|
|
|
|
def capabilities(self) -> list[dict[str, Any]]:
|
|
return self.repository.list_profiles()
|
|
|
|
def validate_render_plan(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
provider_id = str(payload.get("provider_id") or "").strip()
|
|
model_id = str(payload.get("model_id") or "").strip()
|
|
required = [str(item) for item in payload.get("required_capabilities") or []]
|
|
profile = self.repository.get_profile(provider_id, model_id) if provider_id and model_id else None
|
|
if profile is None:
|
|
return {
|
|
"valid": False,
|
|
"status": "unconfirmed",
|
|
"errors": [
|
|
{
|
|
"code": ErrorCode.MODEL_CAPABILITY_UNCONFIRMED.value,
|
|
"message": "model capability profile is unconfirmed",
|
|
}
|
|
],
|
|
"warnings": [],
|
|
}
|
|
route = route_from_capability_profile(profile)
|
|
selection = select_model_route([route])
|
|
if selection.status != SUPPORTED:
|
|
code = (
|
|
ErrorCode.MODEL_CAPABILITY_UNSUPPORTED
|
|
if selection.status == UNSUPPORTED
|
|
else ErrorCode.MODEL_CAPABILITY_UNCONFIRMED
|
|
)
|
|
return {
|
|
"valid": False,
|
|
"status": selection.status,
|
|
"errors": [
|
|
{
|
|
"code": code.value,
|
|
"message": selection.reason or "model capabilities are not confirmed",
|
|
}
|
|
],
|
|
"warnings": [],
|
|
"profile": profile,
|
|
}
|
|
missing = [capability for capability in required if capability not in route.capabilities]
|
|
return {
|
|
"valid": not missing,
|
|
"status": selection.status,
|
|
"errors": [
|
|
{
|
|
"code": ErrorCode.MODEL_CAPABILITY_UNSUPPORTED.value,
|
|
"message": "required capability is not supported",
|
|
"capability": capability,
|
|
}
|
|
for capability in missing
|
|
],
|
|
"warnings": [],
|
|
"profile": profile,
|
|
}
|
|
|
|
|
|
def new_capability_profile_id() -> str:
|
|
return f"cap_{uuid4().hex}"
|
|
|
|
|
|
def _saved_provider_probe_payload(payload: dict[str, Any]) -> dict[str, Any]:
|
|
provider_id = str(payload.get("provider_id") or "").strip()
|
|
config = load_config_payload()
|
|
generation = config.get("generation") if isinstance(config.get("generation"), dict) else {}
|
|
adapters = generation.get("adapters") if isinstance(generation, dict) else []
|
|
saved: dict[str, Any] | None = None
|
|
if isinstance(adapters, list):
|
|
saved = next(
|
|
(
|
|
dict(item)
|
|
for item in adapters
|
|
if isinstance(item, dict)
|
|
and str(item.get("id") or item.get("provider_id") or "").strip() == provider_id
|
|
),
|
|
None,
|
|
)
|
|
if saved is None and isinstance(generation, dict):
|
|
provider = generation.get("provider")
|
|
if isinstance(provider, dict):
|
|
candidate_id = str(provider.get("id") or provider.get("provider_id") or "").strip()
|
|
if not provider_id or candidate_id == provider_id:
|
|
saved = dict(provider)
|
|
if saved is None:
|
|
raise ApiError(
|
|
ErrorCode.VALIDATION_FAILED,
|
|
"saved generation adapter was not found",
|
|
{"provider_id": provider_id},
|
|
)
|
|
# A caller-supplied key must win over the stored one. The settings UI offers
|
|
# a "probe API key" field promising the value is "only used for this page
|
|
# session ... not written to config or logs"; honouring it here is what lets
|
|
# the frontend keep that promise, instead of persisting the key through
|
|
# settings/save just to make the probe authenticate.
|
|
session_api_key = str(
|
|
payload.get("api_key_plaintext") or payload.get("api_key") or ""
|
|
).strip()
|
|
return {
|
|
**saved,
|
|
**({"api_key_plaintext": session_api_key} if session_api_key else {}),
|
|
"provider_id": provider_id or str(saved.get("id") or saved.get("provider_id") or "").strip(),
|
|
"model_id": str(saved.get("model_id") or payload.get("model_id") or "auto-discovery"),
|
|
"dialect": payload.get("dialect") or "openai_images",
|
|
"live_probe": True,
|
|
"allow_lightweight_test": bool(payload.get("allow_lightweight_test")),
|
|
"use_saved_provider": True,
|
|
}
|
|
|
|
|
|
def _live_probe(payload: dict[str, Any]) -> dict[str, Any]:
|
|
base_url = str(payload.get("base_url") or "").strip().rstrip("/")
|
|
if not base_url.startswith(("http://", "https://")):
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "valid base_url is required", {"field": "base_url"})
|
|
timeout = min(max(int(payload.get("timeout_seconds") or 20), 3), 60)
|
|
env_name = str(payload.get("api_key_env_var") or "").strip()
|
|
literal_api_key = str(payload.get("api_key_plaintext") or payload.get("api_key") or "").strip()
|
|
api_key = literal_api_key or (os.getenv(env_name, "") if env_name else "")
|
|
custom_headers = payload.get("custom_headers")
|
|
custom_headers = custom_headers if isinstance(custom_headers, dict) else {}
|
|
headers = {
|
|
"Accept": "application/json",
|
|
"User-Agent": "AstrBot-ImageStudio/1.0.10",
|
|
**{str(key): str(value) for key, value in custom_headers.items()},
|
|
**({"Authorization": f"Bearer {api_key}"} if api_key else {}),
|
|
}
|
|
result: dict[str, Any] = {
|
|
"mode": "live",
|
|
"connected": False,
|
|
"models": [],
|
|
"supported_sizes": [],
|
|
"warnings": [],
|
|
"auth_source": "request" if literal_api_key else "environment" if env_name else "none",
|
|
}
|
|
api_prefix = base_url if base_url.lower().endswith("/v1") else f"{base_url}/v1"
|
|
models_url = f"{api_prefix}/models"
|
|
result["resolved_api_base"] = api_prefix
|
|
result["image_endpoint"] = "/images/generations" if base_url.lower().endswith("/v1") else "/v1/images/generations"
|
|
try:
|
|
status, raw_body, response_headers = _http_request(
|
|
"GET",
|
|
models_url,
|
|
headers=headers,
|
|
timeout=timeout,
|
|
max_bytes=2 * 1024 * 1024,
|
|
)
|
|
body = raw_body.decode("utf-8-sig", errors="replace")
|
|
result["http_status"] = status
|
|
if 200 <= status < 300:
|
|
parsed = json.loads(body)
|
|
items = parsed.get("data", []) if isinstance(parsed, dict) else []
|
|
result["models"] = [str(item.get("id")) for item in items if isinstance(item, dict) and item.get("id")][:200]
|
|
result["connected"] = True
|
|
else:
|
|
result["error"] = f"models endpoint returned HTTP {status}"
|
|
result["error_body"] = _redact_probe_error(body[:500])
|
|
content_type = response_headers.get("content-type")
|
|
if content_type:
|
|
result["content_type"] = content_type
|
|
location = response_headers.get("location")
|
|
if location:
|
|
redirect_target = urllib.parse.urljoin(models_url, location)
|
|
result["redirect_target"] = redirect_target
|
|
result["warnings"].append(
|
|
{
|
|
"message": "models endpoint redirected; authorization was not forwarded across origins",
|
|
"target": redirect_target,
|
|
}
|
|
)
|
|
except (urllib.error.URLError, TimeoutError, json.JSONDecodeError, UnicodeDecodeError, OSError) as exc:
|
|
result["error"] = str(exc)
|
|
|
|
if payload.get("allow_lightweight_test") and result["connected"]:
|
|
endpoint = str(payload.get("image_endpoint") or result["image_endpoint"])
|
|
if base_url.lower().endswith("/v1") and endpoint.lower().startswith("/v1/"):
|
|
endpoint = endpoint[3:]
|
|
endpoint_url = f"{base_url}{endpoint if endpoint.startswith('/') else '/' + endpoint}"
|
|
model_id = str(payload.get("model_id") or "").strip()
|
|
for size in ("1920x1080", "2560x1440", "3840x2160"):
|
|
data = json.dumps({"model": model_id, "prompt": "A plain gray circle on white background, capability test", "size": size, "n": 1, "response_format": "b64_json"}).encode("utf-8")
|
|
try:
|
|
status, _, _ = _http_request(
|
|
"POST",
|
|
endpoint_url,
|
|
headers={**headers, "Content-Type": "application/json"},
|
|
timeout=timeout,
|
|
data=data,
|
|
max_bytes=1024,
|
|
)
|
|
if not 200 <= status < 300:
|
|
raise RuntimeError(f"HTTP {status}")
|
|
else:
|
|
result["supported_sizes"].append(size)
|
|
except Exception as exc: # Provider dialects expose inconsistent errors; retain a short diagnostic only.
|
|
result["warnings"].append({"size": size, "message": str(exc)[:180]})
|
|
return result
|
|
|
|
|
|
def _http_request(
|
|
method: str,
|
|
url: str,
|
|
*,
|
|
headers: dict[str, str],
|
|
timeout: int,
|
|
data: bytes | None = None,
|
|
max_bytes: int,
|
|
) -> tuple[int, bytes, dict[str, str]]:
|
|
try:
|
|
import httpx
|
|
except ImportError:
|
|
request = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=timeout) as response:
|
|
return response.status, response.read(max_bytes), {key.lower(): value for key, value in response.headers.items()}
|
|
except urllib.error.HTTPError as exc:
|
|
return exc.code, exc.read(max_bytes), {key.lower(): value for key, value in exc.headers.items()}
|
|
|
|
with httpx.stream(
|
|
method,
|
|
url,
|
|
headers=headers,
|
|
content=data,
|
|
timeout=timeout,
|
|
follow_redirects=False,
|
|
) as response:
|
|
chunks: list[bytes] = []
|
|
total = 0
|
|
for chunk in response.iter_bytes():
|
|
if total >= max_bytes:
|
|
break
|
|
remaining = max_bytes - total
|
|
chunks.append(chunk[:remaining])
|
|
total += min(len(chunk), remaining)
|
|
return response.status_code, b"".join(chunks), {key.lower(): value for key, value in response.headers.items()}
|
|
|
|
|
|
def _redact_probe_error(value: str) -> str:
|
|
redacted = re.sub(r"(?i)bearer\s+[a-z0-9._-]+", "Bearer ***", value)
|
|
return re.sub(r"(?i)sk[-_][a-z0-9._-]{8,}", "sk-***", redacted)
|