Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

2.6 KB
Raw Blame
"""Single source of truth for group-shareable resource models.

Both the org-membership cleanup signals and the group blast-radius view need to
know "what is shareable". Keeping two hardcoded lists let them drift (the cloud
``AgenticProject`` was missing from one). This registry holds string-only
descriptors so it is import-cheap and cycle-free; consumers resolve the actual
model lazily via ``django.apps.apps.get_model`` and skip apps absent from a
given deployment (e.g. the cloud-only agentic app in pure OSS).
"""

from dataclasses import dataclass
from typing import Any


@dataclass(frozen=True)
class ShareableResource:
    """Static descriptor for one group-shareable resource model."""

    app_label: str
    model_name: str
    kind: str  # serialization key surfaced in API payloads
    name_field: str  # human-readable display field
    id_field: str  # primary-key field name


# ``agentic_studio_v1`` and ``lookups`` are cloud-only; consumers resolve them
# lazily and skip them when the app is not installed.
SHAREABLE_RESOURCES: tuple[ShareableResource, ...] = (
    ShareableResource("workflow_v2", "Workflow", "workflow", "workflow_name", "id"),
    ShareableResource("pipeline_v2", "Pipeline", "pipeline", "pipeline_name", "id"),
    ShareableResource("api_v2", "APIDeployment", "api_deployment", "display_name", "id"),
    ShareableResource(
        "connector_v2", "ConnectorInstance", "connector_instance", "connector_name", "id"
    ),
    ShareableResource(
        "adapter_processor_v2",
        "AdapterInstance",
        "adapter_instance",
        "adapter_name",
        "id",
    ),
    ShareableResource(
        "prompt_studio_core_v2", "CustomTool", "custom_tool", "tool_name", "tool_id"
    ),
    ShareableResource(
        "agentic_studio_v1",
        "AgenticProject",
        "agentic_project",
        "name",
        "id",
    ),
    ShareableResource("lookups", "LookupDefinition", "lookup", "name", "lookup_id"),
)


def descriptor_for_kind(kind: str) -> ShareableResource | None:
    """Look up a descriptor by its ``kind`` key."""
    return next((r for r in SHAREABLE_RESOURCES if r.kind == kind), None)


def kind_for_instance(instance: Any) -> str | None:
    """Reverse lookup: the ``kind`` of a resource instance, ``None`` if unlisted.

    Matches on the model's app label + class name so callers holding an
    instance (e.g. the share endpoint) don't hardcode a type check per
    resource.
    """
    meta = instance._meta
    return next(
        (
            r.kind
            for r in SHAREABLE_RESOURCES
            if r.app_label == meta.app_label and r.model_name == meta.object_name
        ),
        None,
    )