Skip to content

The platform for continuously improving AI agents.

7.5 KB
Raw Blame
"""``POST`` / ``GET`` ``/api/v1/sync`` — two-way ``overmind.toml`` sync."""

from __future__ import annotations

from drf_spectacular.utils import OpenApiParameter, extend_schema
from rest_framework import serializers, status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from overbae.api.authentication import APITokenBackend
from overbae.api.scoping import project_ids_for
from overbae.core.errors import InputValidationError
from overbae.innermind import checks
from overbae.models import APIToken, Project
from overbae.services.sync import apply_snapshot, snapshot_of

TRACE_PROVIDERS = ("overmind", "langfuse", "langsmith", "opentelemetry")
METRIC_TYPES = ("llm_judge_custom", "managed", "deterministic", "custom_judge")


class EvalMetricSerializer(serializers.Serializer):
    name = serializers.CharField()
    type = serializers.ChoiceField(choices=METRIC_TYPES, default="llm_judge_custom")
    prompt = serializers.CharField(required=False, allow_blank=True)
    measures = serializers.CharField(required=False, allow_blank=True)
    rationale = serializers.CharField(required=False, allow_blank=True)
    rubric = serializers.CharField(required=False, allow_blank=True)
    requires_reference = serializers.BooleanField(default=False)
    managed_name = serializers.CharField(required=False, allow_blank=True)


class DeclaredExpectationSerializer(serializers.Serializer):
    """One ``expectations=`` sentence read from a decorator in the synced checkout."""

    expectation = serializers.CharField(max_length=1000)
    span_name = serializers.CharField(max_length=255, required=False, allow_blank=True, default="")


class CapabilitySerializer(serializers.Serializer):
    id = serializers.UUIDField(required=False, allow_null=True)
    slug = serializers.SlugField(max_length=255)
    name = serializers.CharField(max_length=255)
    description = serializers.CharField(required=False, allow_blank=True)
    entrypoint_fn = serializers.CharField(required=False, allow_blank=True, max_length=255)
    model = serializers.CharField(required=False, allow_blank=True, max_length=128)
    source_path = serializers.CharField(required=False, allow_blank=True, max_length=512)
    system_prompt = serializers.CharField(required=False, allow_blank=True)
    tools_summary = serializers.CharField(required=False, allow_blank=True)
    decision_logic = serializers.CharField(required=False, allow_blank=True)
    policy_markdown = serializers.CharField(required=False, allow_blank=True)
    eval_metrics = EvalMetricSerializer(many=True, required=False)
    capability_card = serializers.DictField(required=False)
    eval_matrix = serializers.ListField(child=serializers.DictField(), required=False)
    expectations = DeclaredExpectationSerializer(
        many=True, required=False, write_only=True, max_length=200
    )
    archived = serializers.BooleanField(required=False, default=False)
    status = serializers.CharField(read_only=True)

    def validate_eval_matrix(self, entries):
        for entry in entries:
            if entry.get("type") != "deterministic":
                continue
            try:
                checks.render(str(entry.get("check") or ""), entry.get("params") or {})
            except ValueError as exc:
                raise serializers.ValidationError(f"{entry.get('name') or 'entry'}: {exc}") from exc
        return entries


class RepositorySnapshotSerializer(serializers.Serializer):
    repository = serializers.CharField(max_length=1024)
    directory = serializers.CharField(max_length=1024)
    branch = serializers.CharField(max_length=255, allow_blank=True)
    commit = serializers.RegexField(r"\A(?:[0-9a-f]{40}|[0-9a-f]{64})?\Z", allow_blank=True)
    dirty = serializers.BooleanField()
    fingerprint = serializers.RegexField(r"\A[0-9a-f]{64}\Z")
    scanned_at = serializers.DateTimeField()


class SyncSnapshotSerializer(serializers.Serializer):
    """Wire form of ``overmind.toml`` — the unit of two-way sync."""

    project_id = serializers.UUIDField()
    repo_summary = serializers.CharField(required=False, allow_blank=True)
    trace_provider = serializers.ChoiceField(choices=TRACE_PROVIDERS, default="overmind")
    version = serializers.CharField()
    repository_snapshot = RepositorySnapshotSerializer(required=False, allow_null=True)
    last_synced_at = serializers.DateTimeField(read_only=True, allow_null=True)
    capabilities = CapabilitySerializer(many=True)

    def validate_capabilities(self, capabilities):
        # Two blocks naming one row would be folded into it, the last one winning.
        for field in ("slug", "id"):
            named = [body[field] for body in capabilities if body.get(field)]
            if len(named) != len(set(named)):
                raise serializers.ValidationError(f"Each capability {field} may appear once.")
        return capabilities


class SyncView(APIView):
    """Two-way ``overmind.toml`` sync.

    POST pushes the local snapshot. The server reconciles capability identity —
    carry / leftover / remount — and never deletes: absence sets
    ``status=leftover`` (observed rows stay). ``archived=true`` keeps leftover.
    POST response is the incoming set with ids filled in. GET returns leftovers
    with ``archived=true`` so toml round-trips them without listing them in the
    console.

    Auth: ``X-Api-Key`` (``APIToken``). Project keys must match ``project_id``;
    account keys may sync any project the user belongs to.
    """

    authentication_classes = [APITokenBackend]
    permission_classes = [IsAuthenticated]

    @extend_schema(
        parameters=[
            OpenApiParameter(
                name="project_id",
                type=str,
                location=OpenApiParameter.QUERY,
                required=True,
            ),
        ],
        responses={200: SyncSnapshotSerializer},
    )
    def get(self, request):
        project = _project_for(request, request.query_params.get("project_id"))
        if project is None:
            return Response(
                {"detail": "project_id is required"}, status=status.HTTP_400_BAD_REQUEST
            )
        if isinstance(project, Response):
            return project
        return Response(SyncSnapshotSerializer(snapshot_of(project)).data)

    @extend_schema(
        request=SyncSnapshotSerializer,
        responses={200: SyncSnapshotSerializer},
    )
    def post(self, request):
        ser = SyncSnapshotSerializer(data=request.data)
        ser.is_valid(raise_exception=True)
        data = ser.validated_data
        project = _project_for(request, data["project_id"])
        if isinstance(project, Response):
            return project
        try:
            applied = apply_snapshot(project, data)
        except InputValidationError as exc:
            raise serializers.ValidationError({"capabilities": exc.detail}) from exc
        return Response(SyncSnapshotSerializer(snapshot_of(project, capabilities=applied)).data)


def _project_for(request, project_id):
    token = request.auth
    if not isinstance(token, APIToken):
        return Response({"detail": "API key required"}, status=status.HTTP_401_UNAUTHORIZED)
    if not project_id:
        return None
    if str(project_id) not in {str(pid) for pid in project_ids_for(token.user, token)}:
        return Response(
            {"detail": "API key does not match project_id"},
            status=status.HTTP_403_FORBIDDEN,
        )
    try:
        return Project.objects.get(pk=project_id)
    except Project.DoesNotExist:
        return Response({"detail": "Not found"}, status=status.HTTP_404_NOT_FOUND)