|
| 1 | +from drf_spectacular.utils import OpenApiResponse |
| 2 | +from rest_framework import serializers |
| 3 | +from rest_framework.decorators import action |
| 4 | +from rest_framework.exceptions import NotFound |
| 5 | +from rest_framework.response import Response |
| 6 | +from rest_framework.viewsets import GenericViewSet |
| 7 | + |
| 8 | +from posthog.api.mixins import ValidatedRequest, validated_request |
| 9 | +from posthog.api.routing import TeamAndOrgViewSetMixin |
| 10 | + |
| 11 | +from products.posthog_ai.backend.turn_suggestions.offer_ledger import TurnSuggestionResolution |
| 12 | +from products.posthog_ai.backend.turn_suggestions.service import resolve_turn_suggestion, turn_suggestion_state |
| 13 | +from products.tasks.backend.facade.api import task_visible |
| 14 | + |
| 15 | + |
| 16 | +class ResolveTurnSuggestionSerializer(serializers.Serializer): |
| 17 | + task_id = serializers.UUIDField( |
| 18 | + help_text="ID of the PostHog AI conversation (task) the suggestion card belongs to." |
| 19 | + ) |
| 20 | + turn_index = serializers.IntegerField( |
| 21 | + min_value=0, help_text="Zero-based index of the conversation turn the suggestion card was shown under." |
| 22 | + ) |
| 23 | + resolution = serializers.ChoiceField( |
| 24 | + choices=TurnSuggestionResolution.choices, |
| 25 | + help_text="What the user did with the card: `dismissed` mutes suggestions for the rest of the conversation, `accepted` means the offered scout, notebook, alert or subscription was created.", |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +class ResolveTurnSuggestionResponseSerializer(serializers.Serializer): |
| 30 | + recorded = serializers.BooleanField( |
| 31 | + help_text="Whether a suggestion card existed for that turn and this call recorded its outcome. A card keeps the first outcome recorded for it." |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +class TurnSuggestionStateQuerySerializer(serializers.Serializer): |
| 36 | + task_id = serializers.UUIDField( |
| 37 | + help_text="ID of the PostHog AI conversation (task) to read suggestion outcomes for." |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +class TurnSuggestionStateSerializer(serializers.Serializer): |
| 42 | + muted = serializers.BooleanField( |
| 43 | + help_text="Whether the user dismissed a suggestion card in this conversation, which stops further cards." |
| 44 | + ) |
| 45 | + resolved_turns = serializers.ListField( |
| 46 | + child=serializers.IntegerField(min_value=0), |
| 47 | + help_text="Zero-based indexes of the turns whose suggestion card the user already dismissed or accepted.", |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +class TurnSuggestionsViewSet(TeamAndOrgViewSetMixin, GenericViewSet): |
| 52 | + scope_object = "task" |
| 53 | + |
| 54 | + @validated_request( |
| 55 | + query_serializer=TurnSuggestionStateQuerySerializer, |
| 56 | + responses={200: OpenApiResponse(response=TurnSuggestionStateSerializer)}, |
| 57 | + summary="Read which PostHog AI turn suggestion cards the user already resolved", |
| 58 | + ) |
| 59 | + @action(detail=False, methods=["get"], required_scopes=["task:read"]) |
| 60 | + def state(self, request: ValidatedRequest, *args, **kwargs) -> Response: |
| 61 | + task_id = str(request.validated_query_data["task_id"]) |
| 62 | + if not task_visible(task_id, self.team_id, request.user.id): |
| 63 | + raise NotFound() |
| 64 | + state = turn_suggestion_state(task_id, self.team_id) |
| 65 | + return Response( |
| 66 | + TurnSuggestionStateSerializer({"muted": state.muted, "resolved_turns": list(state.resolved_turns)}).data |
| 67 | + ) |
| 68 | + |
| 69 | + @validated_request( |
| 70 | + request_serializer=ResolveTurnSuggestionSerializer, |
| 71 | + responses={200: OpenApiResponse(response=ResolveTurnSuggestionResponseSerializer)}, |
| 72 | + summary="Record what the user did with a PostHog AI turn suggestion card", |
| 73 | + ) |
| 74 | + @action(detail=False, methods=["post"], required_scopes=["task:write"]) |
| 75 | + def resolve(self, request: ValidatedRequest, *args, **kwargs) -> Response: |
| 76 | + task_id = str(request.validated_data["task_id"]) |
| 77 | + # A dismissal mutes the conversation for its creator, so a teammate who can only read it must not resolve cards. |
| 78 | + if not task_visible(task_id, self.team_id, request.user.id, for_control=True): |
| 79 | + raise NotFound() |
| 80 | + recorded = resolve_turn_suggestion( |
| 81 | + task_id, |
| 82 | + self.team_id, |
| 83 | + turn_index=request.validated_data["turn_index"], |
| 84 | + resolution=TurnSuggestionResolution(request.validated_data["resolution"]), |
| 85 | + ) |
| 86 | + return Response(ResolveTurnSuggestionResponseSerializer({"recorded": recorded}).data) |
0 commit comments