114 行
4.1 KiB
Python
114 行
4.1 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 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 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": [],
|
|
}
|
|
if profile["status"] != "confirmed_supported":
|
|
return {
|
|
"valid": False,
|
|
"status": profile["status"],
|
|
"errors": [
|
|
{
|
|
"code": ErrorCode.MODEL_CAPABILITY_UNCONFIRMED.value,
|
|
"message": "model capabilities are not confirmed",
|
|
}
|
|
],
|
|
"warnings": [],
|
|
"profile": profile,
|
|
}
|
|
supported = set(profile["capabilities"].get("supports", []))
|
|
missing = [capability for capability in required if capability not in supported]
|
|
return {
|
|
"valid": not missing,
|
|
"status": profile["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}"
|