Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.5 KB
Raw Blame
from django.db import IntegrityError

from drf_spectacular.utils import extend_schema
from rest_framework import mixins, serializers
from rest_framework.viewsets import GenericViewSet

from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.exceptions import Conflict

from products.posthog_ai.backend.models.assistant import CORE_MEMORY_MAX_CHARACTERS, CoreMemory


class MaxCoreMemorySerializer(serializers.ModelSerializer):
    class Meta:
        model = CoreMemory
        fields = ["id", "text", "scraping_status"]

    text = serializers.CharField(
        allow_blank=True,
        max_length=CORE_MEMORY_MAX_CHARACTERS,
        help_text="What Max remembers about the project, as free-form text.",
    )

    def create(self, validated_data):
        try:
            validated_data["team"] = self.context["get_team"]()
            validated_data["initial_text"] = validated_data["text"]
            validated_data["scraping_status"] = CoreMemory.ScrapingStatus.COMPLETED
            return super().create(validated_data)
        except IntegrityError:
            raise Conflict("Core memory already exists for this environment.")


@extend_schema(extensions={"x-product": "posthog_ai"})
class MaxCoreMemoryViewSet(
    TeamAndOrgViewSetMixin,
    mixins.CreateModelMixin,
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
    mixins.UpdateModelMixin,
    GenericViewSet,
):
    scope_object = "INTERNAL"
    serializer_class = MaxCoreMemorySerializer
    queryset = CoreMemory.objects.all()