165 行
6.3 KiB
Python
165 行
6.3 KiB
Python
"""Safe API discovery and render-plan validation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
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.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]:
|
|
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")
|
|
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": "unconfirmed",
|
|
"capabilities": payload.get("capabilities") or {},
|
|
"probe_result": {
|
|
"mode": "safe",
|
|
"message": "safe probe recorded metadata only; no external API call was made",
|
|
},
|
|
}
|
|
)
|
|
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}"
|