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.

chore(user-interviews): move Vapi event handling out of the webhook view (#102148)

Julian Bez560f97cc40c9
diff --git a/products/user_interviews/backend/logic.py b/products/user_interviews/backend/logic.py
index 6d9f3134224e..233cc8fbfbbc 100644
--- a/products/user_interviews/backend/logic.py
+++ b/products/user_interviews/backend/logic.py
@@ -8,7 +8,10 @@ products must keep going through the facade.
import re
import uuid
-from uuid import UUID
+from typing import Any
+from uuid import UUID, uuid4
+
+from posthog.models.sharing_configuration import SharingConfiguration
from products.user_interviews.backend.facade.contracts import IntervieweeIdentity
from products.user_interviews.backend.models import (
@@ -86,6 +89,52 @@ def is_shared_interviewee_context(interviewee_identifier: str) -> bool:
return interviewee_identifier == SHARED_INTERVIEWEE_IDENTIFIER
+# Max lengths for the self-reported fields a shared-link respondent sends to start_call. These
+# are echoed into Vapi metadata and persisted on the UserInterview, so cap them defensively.
+RESPONDENT_NAME_MAX_CHARS = 200
+RESPONDENT_KEY_MAX_CHARS = 64
+
+# Every shared-link response is stored under an identifier carrying this prefix. It can NEVER equal a
+# personalised interviewee's identifier (an email or distinct_id), so an anonymous respondent can
+# neither be attributed to nor lock out a targeted invitee.
+SHARED_RESPONDENT_IDENTIFIER_PREFIX = "shared:"
+
+
+def clean_field(value: Any, max_chars: int) -> str:
+ return str(value).strip()[:max_chars] if value else ""
+
+
+def shared_interviewee_identifier(respondent_key: str) -> str:
+ """Namespaced, non-authoritative identity for a shared-link respondent.
+
+ Keyed on the stable per-browser ``respondent_key`` so a refreshed respondent keeps one identity;
+ falls back to a random id when no key was supplied so rows stay distinct. Deliberately independent
+ of any self-reported name or untrusted ``distinct_id`` — those never determine attribution."""
+ return f"{SHARED_RESPONDENT_IDENTIFIER_PREFIX}{respondent_key or uuid4().hex}"
+
+
+def resolve_share(access_token: str) -> SharingConfiguration | None:
+ """Resolve a share token to its `SharingConfiguration`.
+
+ Uses the same enabled/expiry predicate as `SharingViewerPageViewSet.get_object()`, so a
+ rotated token past its grace period is dead here exactly when it is dead on the public viewer.
+ """
+ try:
+ return (
+ SharingConfiguration.objects.select_related(
+ "team",
+ "team__organization",
+ "interviewee_context",
+ "interviewee_context__topic",
+ "interviewee_context__topic__created_by",
+ )
+ .filter(SharingConfiguration.tokens_active_q())
+ .get(access_token=access_token)
+ )
+ except SharingConfiguration.DoesNotExist:
+ return None
+
+
def parse_interviewee_identifier(identifier: str) -> IntervieweeIdentity:
"""Split an interviewee identifier into a display name and (optional) email.
diff --git a/products/user_interviews/backend/presentation/webhooks.py b/products/user_interviews/backend/presentation/webhooks.py
index be68142de703..0131e03cf7c2 100644
--- a/products/user_interviews/backend/presentation/webhooks.py
+++ b/products/user_interviews/backend/presentation/webhooks.py
@@ -14,16 +14,12 @@ import json
import string
import hashlib
from typing import Any
-from uuid import uuid4
from django.conf import settings
from django.db import transaction
-from django.db.models import Q
-from django.utils.timezone import now
import requests
import structlog
-import posthoganalytics
from rest_framework import status
from rest_framework.decorators import api_view, authentication_classes, permission_classes, throttle_classes
from rest_framework.permissions import AllowAny
@@ -31,19 +27,23 @@ from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.throttling import SimpleRateThrottle
-from posthog.schema import EmbeddingModelName
-
-from posthog.api.embedding_worker import emit_embedding_request
from posthog.constants import AvailableFeature
from posthog.egress.vapi import vapi_request
-from posthog.event_usage import groups
from posthog.models.sharing_configuration import SharingConfiguration
from posthog.models.team import Team
from posthog.rate_limit import IPThrottle
from posthog.storage.llm_prompt_cache import get_prompt_by_name_from_cache
from ..facade.api import derive_auto_classifications, is_shared_interviewee_context, valid_distinct_id, valid_session_id
-from ..models import UserInterview, UserInterviewClassification, UserInterviewTopic
+from ..logic import (
+ RESPONDENT_KEY_MAX_CHARS,
+ RESPONDENT_NAME_MAX_CHARS,
+ clean_field,
+ resolve_share,
+ shared_interviewee_identifier,
+)
+from ..models import UserInterview, UserInterviewClassification
+from ..vapi_events import capture_user_interview_event, collapse_abandoned_partials, emit_interview_embeddings
logger = structlog.get_logger(__name__)
@@ -152,82 +152,6 @@ class InterviewStartCallRespondentThrottle(_RateLimitMetricsMixin):
_VAPI_SIGNATURE_RE = re.compile(r"^[0-9a-f]{64}$")
-_EMBEDDING_MODELS = [m.value for m in EmbeddingModelName]
-
-# TODO: figure out a better story for transcripts that exceed our Kafka envelope
-# than head-truncation. Options: (a) chunk + emit multiple documents per type, or
-# (b) push large content to object storage and embed a reference. Truncation is a
-# stop-gap so a 90-minute interview doesn't silently lose its embeddings entirely.
-EMBEDDING_CONTENT_MAX_BYTES = 750_000
-
-
-def _emit_interview_embeddings(interview: UserInterview, topic: UserInterviewTopic) -> None:
- """Emit transcript and summary as two separate embedding documents so each can be
- searched independently. Failures are logged but never propagated: Vapi retries are
- idempotent on call.id, so a re-delivery would skip creation and never re-emit —
- making a thrown exception here strictly worse than a degraded but acknowledged row."""
- metadata = {
- "topic_id": str(topic.id),
- "interviewee_identifier": interview.interviewee_identifier,
- }
- for document_type, content in (("transcript", interview.transcript), ("summary", interview.summary)):
- if not content or not content.strip():
- continue
- content_bytes = content.encode("utf-8")
- if len(content_bytes) > EMBEDDING_CONTENT_MAX_BYTES:
- logger.warning(
- "user_interviews_embedding_content_truncated",
- team_id=interview.team_id,
- interview_id=str(interview.id),
- document_type=document_type,
- original_bytes=len(content_bytes),
- truncated_to_bytes=EMBEDDING_CONTENT_MAX_BYTES,
- )
- content = content_bytes[:EMBEDDING_CONTENT_MAX_BYTES].decode("utf-8", errors="ignore")
- try:
- emit_embedding_request(
- content=content,
- team_id=interview.team_id,
- product="user_interviews",
- document_type=document_type,
- rendering="plain",
- document_id=str(interview.id),
- models=_EMBEDDING_MODELS,
- metadata=metadata,
- )
- except Exception:
- logger.exception(
- "user_interviews_embedding_emit_failed",
- team_id=interview.team_id,
- interview_id=str(interview.id),
- document_type=document_type,
- )
-
-
-def _resolve_share(access_token: str) -> SharingConfiguration | None:
- """Resolve a share token to its `SharingConfiguration`, mirroring the filters
- used by `SharingViewerPageViewSet.get_object()`:
- * `enabled=True`
- * not expired (`expires_at` null OR in the future) — rotated tokens past their
- 5-minute grace period are excluded so this surface stays consistent with the
- public viewer.
- """
- try:
- return (
- SharingConfiguration.objects.select_related(
- "team",
- "team__organization",
- "interviewee_context",
- "interviewee_context__topic",
- "interviewee_context__topic__created_by",
- )
- .filter(Q(expires_at__isnull=True) | Q(expires_at__gt=now()))
- .get(access_token=access_token, enabled=True)
- )
- except SharingConfiguration.DoesNotExist:
- return None
-
-
def _public_sharing_disabled_for_org(sharing_config: SharingConfiguration) -> bool:
"""Mirror of `SharingViewerPageViewSet.retrieve()`'s org-level kill switch."""
organization = sharing_config.team.organization
@@ -304,30 +228,6 @@ def _build_first_message(
return rendered[:_FIRST_MESSAGE_MAX_CHARS]
-# Max lengths for the self-reported fields a shared-link respondent sends to start_call. These
-# are echoed into Vapi metadata and persisted on the UserInterview, so cap them defensively.
-_RESPONDENT_NAME_MAX_CHARS = 200
-_RESPONDENT_KEY_MAX_CHARS = 64
-
-# Every shared-link response is stored under an identifier carrying this prefix. It can NEVER equal a
-# personalised interviewee's identifier (an email or distinct_id), so an anonymous respondent can
-# neither be attributed to nor lock out a targeted invitee.
-SHARED_RESPONDENT_IDENTIFIER_PREFIX = "shared:"
-
-
-def _clean_field(value: Any, max_chars: int) -> str:
- return str(value).strip()[:max_chars] if value else ""
-
-
-def _shared_interviewee_identifier(respondent_key: str) -> str:
- """Namespaced, non-authoritative identity for a shared-link respondent.
-
- Keyed on the stable per-browser ``respondent_key`` so a refreshed respondent keeps one identity;
- falls back to a random id when no key was supplied so rows stay distinct. Deliberately independent
- of any self-reported name or untrusted ``distinct_id`` — those never determine attribution."""
- return f"{SHARED_RESPONDENT_IDENTIFIER_PREFIX}{respondent_key or uuid4().hex}"
-
-
VAPI_WEB_CALL_URL = "https://api.vapi.ai/call/web"
@@ -383,46 +283,6 @@ def _create_vapi_web_call(assistant_overrides: dict[str, Any]) -> dict[str, Any]
return web_call
-def _collapse_abandoned_partials(*, team: Team, topic: UserInterviewTopic, respondent_key: str, keep_pk: Any) -> None:
- """Delete the abandoned partial an accidental mid-call refresh leaves behind, when the same
- respondent (same ``respondent_key``) comes back and finishes — so the topic shows one response
- per respondent instead of a junk trail.
-
- Deletes only rows that STILL auto-derive as ``abandoned`` from their own transcript, rather than
- trusting the stored label. ``abandoned`` is user-mutable (writable via the update API and the MCP
- tool), so a real response a curator manually re-tagged ``abandoned`` would otherwise be
- permanently, unrecoverably deleted here. Re-deriving keeps this a cleanup of genuine AI-only
- partials and never touches a row that contains real interviewee content.
- """
- candidates = (
- UserInterview.objects.filter(
- team=team,
- topic=topic,
- respondent_key=respondent_key,
- classifications__contains=[UserInterviewClassification.ABANDONED],
- )
- .exclude(pk=keep_pk)
- .only("id", "transcript")
- )
- stale_pks = [
- c.pk for c in candidates if UserInterviewClassification.ABANDONED in derive_auto_classifications(c.transcript)
- ]
- if not stale_pks:
- return
- deleted_count, _ = UserInterview.objects.filter(
- team=team,
- topic=topic,
- respondent_key=respondent_key,
- pk__in=stale_pks,
- ).delete()
- logger.info(
- "user_interviews_collapsed_abandoned_partials",
- team_id=team.id,
- topic_id=str(topic.id),
- deleted_count=deleted_count,
- )
-
-
@api_view(["POST"])
@authentication_classes([])
@permission_classes([AllowAny])
@@ -469,7 +329,7 @@ def start_call(request: Request, access_token: str) -> Response:
)
return Response({"error": "invalid request"}, status=status.HTTP_400_BAD_REQUEST)
- sharing_config = _resolve_share(access_token)
+ sharing_config = resolve_share(access_token)
if sharing_config is None or sharing_config.interviewee_context is None:
logger.warning("user_interviews_start_call_unknown_access_token")
return Response({"error": "unknown access_token"}, status=status.HTTP_404_NOT_FOUND)
@@ -485,10 +345,10 @@ def start_call(request: Request, access_token: str) -> Response:
ic = sharing_config.interviewee_context
topic = ic.topic
if is_shared_interviewee_context(ic.interviewee_identifier):
- respondent_name = _clean_field(body.get("name"), _RESPONDENT_NAME_MAX_CHARS)
+ respondent_name = clean_field(body.get("name"), RESPONDENT_NAME_MAX_CHARS)
if not respondent_name:
return Response({"error": "name is required"}, status=status.HTTP_400_BAD_REQUEST)
- respondent_key = _clean_field(body.get("respondent_key"), _RESPONDENT_KEY_MAX_CHARS)
+ respondent_key = clean_field(body.get("respondent_key"), RESPONDENT_KEY_MAX_CHARS)
user_name = respondent_name or "there"
agent_context = topic.agent_context or ""
# The response is stored under a namespaced identifier that can never collide with a targeted
@@ -497,7 +357,7 @@ def start_call(request: Request, access_token: str) -> Response:
# targeted person out.
metadata: dict[str, str] = {
"topic_id": str(topic.id),
- "interviewee_identifier": _shared_interviewee_identifier(respondent_key),
+ "interviewee_identifier": shared_interviewee_identifier(respondent_key),
"sharing_access_token": access_token,
"shared": "true",
"respondent_name": respondent_name,
@@ -637,9 +497,9 @@ def vapi_webhook(request: Request) -> Response:
# capture the ended event from that branch where we already have the interview row.
call_status = message.get("status")
if call_status == "in-progress" and access_token:
- sharing_config = _resolve_share(access_token)
+ sharing_config = resolve_share(access_token)
if sharing_config is not None and sharing_config.interviewee_context is not None:
- _capture_user_interview_event(
+ capture_user_interview_event(
"user_interview_conversation_started",
sharing_config=sharing_config,
call_id=call_id,
@@ -672,7 +532,7 @@ def vapi_webhook(request: Request) -> Response:
status=status.HTTP_400_BAD_REQUEST,
)
- sharing_config = _resolve_share(access_token)
+ sharing_config = resolve_share(access_token)
if sharing_config is None:
logger.warning(
"user_interviews_vapi_webhook_unknown_access_token",
@@ -709,11 +569,11 @@ def vapi_webhook(request: Request) -> Response:
topic = interviewee_context.topic
if is_shared_interviewee_context(interviewee_context.interviewee_identifier):
- respondent_name = _clean_field(merged_metadata.get("respondent_name"), _RESPONDENT_NAME_MAX_CHARS)
- respondent_key = _clean_field(merged_metadata.get("respondent_key"), _RESPONDENT_KEY_MAX_CHARS)
+ respondent_name = clean_field(merged_metadata.get("respondent_name"), RESPONDENT_NAME_MAX_CHARS)
+ respondent_key = clean_field(merged_metadata.get("respondent_key"), RESPONDENT_KEY_MAX_CHARS)
# Recompute the identifier from respondent_key rather than trusting the echoed metadata, so it
# is always a namespaced shared marker and can never be steered onto a targeted invitee.
- interviewee_identifier = _shared_interviewee_identifier(respondent_key)
+ interviewee_identifier = shared_interviewee_identifier(respondent_key)
interviewee_emails = []
# Best-effort, untrusted person linkage. Re-validated here (defense in depth) and stored in its
# own column — never as the interviewee_identifier, so it can't forge attribution.
@@ -748,15 +608,15 @@ def vapi_webhook(request: Request) -> Response:
# respondent comes back (same respondent_key) and finishes, drop their earlier abandoned
# rows so the topic shows one response per respondent instead of a junk trail.
if respondent_key and UserInterviewClassification.ABANDONED not in classifications:
- _collapse_abandoned_partials(
+ collapse_abandoned_partials(
team=sharing_config.team,
topic=topic,
respondent_key=respondent_key,
keep_pk=interview.pk,
)
- transaction.on_commit(lambda: _emit_interview_embeddings(interview, topic))
+ transaction.on_commit(lambda: emit_interview_embeddings(interview, topic))
- _capture_user_interview_event(
+ capture_user_interview_event(
"user_interview_conversation_ended",
sharing_config=sharing_config,
call_id=call_id,
@@ -775,57 +635,3 @@ def vapi_webhook(request: Request) -> Response:
interview_id=str(interview.id),
)
return Response({"status": "created", "interview_id": str(interview.id)}, status=status.HTTP_201_CREATED)
-
-
-def _capture_user_interview_event(
- event: str,
- *,
- sharing_config: SharingConfiguration,
- call_id: str | None,
- session_id: str = "",
- extra_properties: dict[str, Any] | None = None,
-) -> None:
- """Fire a PostHog event for a user-interview lifecycle moment (conversation started/ended).
- Failures never propagate — analytics never blocks a webhook delivery.
-
- Vapi emits `status-update` per state transition and may re-fire `in-progress` after
- transient drops or warm-transfer flows, and end-of-call-report can be retried by Vapi
- until we ack. Set `$insert_id` to `<event>:<call_id>` so PostHog dedupes the second
- delivery at ingest — funnels see one start and one end per call.
-
- When a shared-link respondent supplied a valid session_id, it's attached as `$session_id` so
- the event (and thus the interview) associates with that session recording — this is how the
- session is linked without a dedicated DB column.
-
- The `distinct_id` is intentionally an opaque per-share UUID — *not* the interviewee's
- email/distinct_id — so these feature-usage events never create person profiles for the
- third-party interviewees themselves. The events report on the user_interviews feature, not
- the people being interviewed."""
- interviewee_context = sharing_config.interviewee_context
- if interviewee_context is None:
- return
- properties: dict[str, Any] = {
- "topic_id": str(interviewee_context.topic_id),
- "team_id": sharing_config.team_id,
- "call_id": call_id,
- }
- if session_id:
- properties["$session_id"] = session_id
- if call_id:
- properties["$insert_id"] = f"{event}:{call_id}"
- if extra_properties:
- properties.update(extra_properties)
- try:
- posthoganalytics.capture(
- distinct_id=f"user_interview:{interviewee_context.id}",
- event=event,
- properties=properties,
- groups=groups(organization=sharing_config.team.organization, team=sharing_config.team),
- )
- except Exception:
- logger.exception(
- "user_interviews_event_capture_failed",
- event=event,
- team_id=sharing_config.team_id,
- call_id=call_id,
- )
diff --git a/products/user_interviews/backend/test_interview_sharing.py b/products/user_interviews/backend/test_interview_sharing.py
index 52cd9f412662..098c03697d0b 100644
--- a/products/user_interviews/backend/test_interview_sharing.py
+++ b/products/user_interviews/backend/test_interview_sharing.py
@@ -26,12 +26,12 @@ from posthog.models.sharing_configuration import SharingConfiguration
from products.user_interviews.backend.models import IntervieweeContext, UserInterview, UserInterviewTopic
from products.user_interviews.backend.presentation.webhooks import (
DEFAULT_FIRST_MESSAGE_TEMPLATE,
- EMBEDDING_CONTENT_MAX_BYTES,
FIRST_MESSAGE_PROMPT_NAME,
_build_first_message,
_create_vapi_web_call,
_resolve_first_message_template,
)
+from products.user_interviews.backend.vapi_events import EMBEDDING_CONTENT_MAX_BYTES
def _mock_web_call(assistant_overrides: dict[str, Any]) -> dict[str, Any]:
@@ -790,7 +790,7 @@ class TestVapiWebhook(APIBaseTest):
self.assertEqual(response.json()["status"], "ignored")
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- @patch("products.user_interviews.backend.presentation.webhooks.posthoganalytics.capture")
+ @patch("products.user_interviews.backend.vapi_events.posthoganalytics.capture")
def test_webhook_status_update_in_progress_captures_started_event(self, mock_capture):
share = self._create_share()
self.client.logout()
@@ -817,7 +817,7 @@ class TestVapiWebhook(APIBaseTest):
self.assertEqual(kwargs["properties"]["call_id"], "call_xyz")
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- @patch("products.user_interviews.backend.presentation.webhooks.posthoganalytics.capture")
+ @patch("products.user_interviews.backend.vapi_events.posthoganalytics.capture")
def test_webhook_status_update_duplicate_in_progress_emits_same_insert_id(self, mock_capture):
# Vapi re-fires `status-update / in-progress` on transient drops + warm-transfer flows.
# We tag every started event with `$insert_id` = "user_interview_conversation_started:<call_id>"
@@ -840,7 +840,7 @@ class TestVapiWebhook(APIBaseTest):
@parameterized.expand([("ringing",), ("ended",), ("queued",), ("forwarding",), ("scheduled",)])
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- @patch("products.user_interviews.backend.presentation.webhooks.posthoganalytics.capture")
+ @patch("products.user_interviews.backend.vapi_events.posthoganalytics.capture")
def test_webhook_status_update_other_statuses_do_not_capture(self, call_status: str, mock_capture):
share = self._create_share()
self.client.logout()
@@ -858,7 +858,7 @@ class TestVapiWebhook(APIBaseTest):
mock_capture.assert_not_called()
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- @patch("products.user_interviews.backend.presentation.webhooks.posthoganalytics.capture")
+ @patch("products.user_interviews.backend.vapi_events.posthoganalytics.capture")
def test_webhook_end_of_call_report_captures_ended_event(self, mock_capture):
share = self._create_share()
self.client.logout()
@@ -887,7 +887,7 @@ class TestVapiWebhook(APIBaseTest):
self.assertEqual(UserInterview.objects.filter(team=self.team).count(), 1)
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- @patch("products.user_interviews.backend.presentation.webhooks.emit_embedding_request")
+ @patch("products.user_interviews.backend.vapi_events.emit_embedding_request")
def test_webhook_emits_transcript_and_summary_embeddings(self, mock_emit):
share = self._create_share()
self.client.logout()
@@ -923,7 +923,7 @@ class TestVapiWebhook(APIBaseTest):
]
)
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- @patch("products.user_interviews.backend.presentation.webhooks.emit_embedding_request")
+ @patch("products.user_interviews.backend.vapi_events.emit_embedding_request")
def test_webhook_skips_empty_content(self, _name, transcript, summary, expected_types, mock_emit):
share = self._create_share()
self.client.logout()
@@ -939,7 +939,7 @@ class TestVapiWebhook(APIBaseTest):
self.assertEqual(emitted_types, expected_types)
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- @patch("products.user_interviews.backend.presentation.webhooks.emit_embedding_request")
+ @patch("products.user_interviews.backend.vapi_events.emit_embedding_request")
def test_webhook_does_not_re_emit_on_duplicate(self, mock_emit):
share = self._create_share()
self.client.logout()
@@ -958,7 +958,7 @@ class TestVapiWebhook(APIBaseTest):
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
@patch(
- "products.user_interviews.backend.presentation.webhooks.emit_embedding_request",
+ "products.user_interviews.backend.vapi_events.emit_embedding_request",
side_effect=RuntimeError("kafka down"),
)
def test_webhook_succeeds_when_embedding_emit_fails(self, _mock_emit):
@@ -977,7 +977,7 @@ class TestVapiWebhook(APIBaseTest):
]
)
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- @patch("products.user_interviews.backend.presentation.webhooks.emit_embedding_request")
+ @patch("products.user_interviews.backend.vapi_events.emit_embedding_request")
def test_webhook_truncates_oversized_content_before_emit(
self, _name, oversize_transcript, oversize_summary, mock_emit
):
diff --git a/products/user_interviews/backend/test_shared_interview_link.py b/products/user_interviews/backend/test_shared_interview_link.py
index 2f33ced98ce0..87114987e46e 100644
--- a/products/user_interviews/backend/test_shared_interview_link.py
+++ b/products/user_interviews/backend/test_shared_interview_link.py
@@ -368,7 +368,7 @@ class TestSharedVapiWebhook(APIBaseTest):
assert interview.interviewee_emails == []
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- @patch("products.user_interviews.backend.presentation.webhooks.posthoganalytics.capture")
+ @patch("products.user_interviews.backend.vapi_events.posthoganalytics.capture")
def test_session_id_rides_on_the_lifecycle_event(self, mock_capture) -> None:
# session_id isn't persisted on the model — it's attached to the conversation event as
# $session_id so the interview associates with the session recording.
diff --git a/products/user_interviews/backend/vapi_events.py b/products/user_interviews/backend/vapi_events.py
new file mode 100644
index 000000000000..c4781b253627
--- /dev/null
+++ b/products/user_interviews/backend/vapi_events.py
@@ -0,0 +1,168 @@
+"""User interviews' handling of the inbound Vapi webhook.
+
+The endpoint in ``presentation/webhooks.py`` calls in here once a delivery is verified. The
+persistence side of the webhook lives beside ``logic.py`` rather than in the view file, so the
+view holds the HTTP surfaces and this module holds what the product does with a call.
+"""
+
+from typing import Any
+
+import structlog
+import posthoganalytics
+
+from posthog.schema import EmbeddingModelName
+
+from posthog.api.embedding_worker import emit_embedding_request
+from posthog.event_usage import groups
+from posthog.models.sharing_configuration import SharingConfiguration
+from posthog.models.team import Team
+
+from products.user_interviews.backend.classification import derive_auto_classifications
+from products.user_interviews.backend.models import UserInterview, UserInterviewClassification, UserInterviewTopic
+
+logger = structlog.get_logger(__name__)
+
+_EMBEDDING_MODELS = [m.value for m in EmbeddingModelName]
+
+# TODO: figure out a better story for transcripts that exceed our Kafka envelope
+# than head-truncation. Options: (a) chunk + emit multiple documents per type, or
+# (b) push large content to object storage and embed a reference. Truncation is a
+# stop-gap so a 90-minute interview doesn't silently lose its embeddings entirely.
+EMBEDDING_CONTENT_MAX_BYTES = 750_000
+
+
+def emit_interview_embeddings(interview: UserInterview, topic: UserInterviewTopic) -> None:
+ """Emit transcript and summary as two separate embedding documents so each can be
+ searched independently. Failures are logged but never propagated: Vapi retries are
+ idempotent on call.id, so a re-delivery would skip creation and never re-emit —
+ making a thrown exception here strictly worse than a degraded but acknowledged row."""
+ metadata = {
+ "topic_id": str(topic.id),
+ "interviewee_identifier": interview.interviewee_identifier,
+ }
+ for document_type, content in (("transcript", interview.transcript), ("summary", interview.summary)):
+ if not content or not content.strip():
+ continue
+ content_bytes = content.encode("utf-8")
+ if len(content_bytes) > EMBEDDING_CONTENT_MAX_BYTES:
+ logger.warning(
+ "user_interviews_embedding_content_truncated",
+ team_id=interview.team_id,
+ interview_id=str(interview.id),
+ document_type=document_type,
+ original_bytes=len(content_bytes),
+ truncated_to_bytes=EMBEDDING_CONTENT_MAX_BYTES,
+ )
+ content = content_bytes[:EMBEDDING_CONTENT_MAX_BYTES].decode("utf-8", errors="ignore")
+ try:
+ emit_embedding_request(
+ content=content,
+ team_id=interview.team_id,
+ product="user_interviews",
+ document_type=document_type,
+ rendering="plain",
+ document_id=str(interview.id),
+ models=_EMBEDDING_MODELS,
+ metadata=metadata,
+ )
+ except Exception:
+ logger.exception(
+ "user_interviews_embedding_emit_failed",
+ team_id=interview.team_id,
+ interview_id=str(interview.id),
+ document_type=document_type,
+ )
+
+
+def collapse_abandoned_partials(*, team: Team, topic: UserInterviewTopic, respondent_key: str, keep_pk: Any) -> None:
+ """Delete the abandoned partial an accidental mid-call refresh leaves behind, when the same
+ respondent (same ``respondent_key``) comes back and finishes — so the topic shows one response
+ per respondent instead of a junk trail.
+
+ Deletes only rows that STILL auto-derive as ``abandoned`` from their own transcript, rather than
+ trusting the stored label. ``abandoned`` is user-mutable (writable via the update API and the MCP
+ tool), so a real response a curator manually re-tagged ``abandoned`` would otherwise be
+ permanently, unrecoverably deleted here. Re-deriving keeps this a cleanup of genuine AI-only
+ partials and never touches a row that contains real interviewee content.
+ """
+ candidates = (
+ UserInterview.objects.filter(
+ team=team,
+ topic=topic,
+ respondent_key=respondent_key,
+ classifications__contains=[UserInterviewClassification.ABANDONED],
+ )
+ .exclude(pk=keep_pk)
+ .only("id", "transcript")
+ )
+ stale_pks = [
+ c.pk for c in candidates if UserInterviewClassification.ABANDONED in derive_auto_classifications(c.transcript)
+ ]
+ if not stale_pks:
+ return
+ deleted_count, _ = UserInterview.objects.filter(
+ team=team,
+ topic=topic,
+ respondent_key=respondent_key,
+ pk__in=stale_pks,
+ ).delete()
+ logger.info(
+ "user_interviews_collapsed_abandoned_partials",
+ team_id=team.id,
+ topic_id=str(topic.id),
+ deleted_count=deleted_count,
+ )
+
+
+def capture_user_interview_event(
+ event: str,
+ *,
+ sharing_config: SharingConfiguration,
+ call_id: str | None,
+ session_id: str = "",
+ extra_properties: dict[str, Any] | None = None,
+) -> None:
+ """Fire a PostHog event for a user-interview lifecycle moment (conversation started/ended).
+ Failures never propagate — analytics never blocks a webhook delivery.
+
+ Vapi emits `status-update` per state transition and may re-fire `in-progress` after
+ transient drops or warm-transfer flows, and end-of-call-report can be retried by Vapi
+ until we ack. Set `$insert_id` to `<event>:<call_id>` so PostHog dedupes the second
+ delivery at ingest — funnels see one start and one end per call.
+
+ When a shared-link respondent supplied a valid session_id, it's attached as `$session_id` so
+ the event (and thus the interview) associates with that session recording — this is how the
+ session is linked without a dedicated DB column.
+
+ The `distinct_id` is intentionally an opaque per-share UUID — *not* the interviewee's
+ email/distinct_id — so these feature-usage events never create person profiles for the
+ third-party interviewees themselves. The events report on the user_interviews feature, not
+ the people being interviewed."""
+ interviewee_context = sharing_config.interviewee_context
+ if interviewee_context is None:
+ return
+ properties: dict[str, Any] = {
+ "topic_id": str(interviewee_context.topic_id),
+ "team_id": sharing_config.team_id,
+ "call_id": call_id,
+ }
+ if session_id:
+ properties["$session_id"] = session_id
+ if call_id:
+ properties["$insert_id"] = f"{event}:{call_id}"
+ if extra_properties:
+ properties.update(extra_properties)
+ try:
+ posthoganalytics.capture(
+ distinct_id=f"user_interview:{interviewee_context.id}",
+ event=event,
+ properties=properties,
+ groups=groups(organization=sharing_config.team.organization, team=sharing_config.team),
+ )
+ except Exception:
+ logger.exception(
+ "user_interviews_event_capture_failed",
+ event=event,
+ team_id=sharing_config.team_id,
+ call_id=call_id,
+ )
diff --git a/pyproject.toml b/pyproject.toml
index ea3faca0244b..e0479c50540c 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -617,7 +617,15 @@ ignore_imports = [
"products.tracing.backend.presentation.views_api -> products.tracing.backend.models",
"products.user_interviews.backend.presentation.views -> products.user_interviews.backend.models",
"products.user_interviews.backend.presentation.views -> products.user_interviews.backend.invite_email",
+ # The public interview view still resolves a share token itself and reads the team, the
+ # interviewee context and the topic off the ORM rows, so it takes the share lookup and the
+ # respondent-field helpers from logic directly. A facade read would have to hand back the
+ # SharingConfiguration, which §5 forbids.
+ "products.user_interviews.backend.presentation.webhooks -> products.user_interviews.backend.logic",
"products.user_interviews.backend.presentation.webhooks -> products.user_interviews.backend.models",
+ # The webhook view still holds the request handling for the Vapi delivery, so it calls the
+ # event module directly. The line goes when the handling moves off the request path.
+ "products.user_interviews.backend.presentation.webhooks -> products.user_interviews.backend.vapi_events",
# logs presentation isolation (temporary): the moved viewsets still reach in-product
# internals directly. Removed by the follow-up that thins each viewset to go through
# the facade.