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.

feat(max): add hands-free voice mode for mobile chat (#59188)

Paul D'Ambraba20420e40ca
diff --git a/.env.example b/.env.example
index 8e35ed919ada..c697e2a10cf6 100644
--- a/.env.example
+++ b/.env.example
@@ -26,3 +26,10 @@ DEBUG=1
# GITHUB_APP_CLIENT_SECRET=
# GITHUB_APP_SLUG=
# GITHUB_APP_PRIVATE_KEY=
+
+# Max hands-free mode (ElevenLabs Scribe for STT + ElevenLabs TTS for voice playback)
+ELEVENLABS_API_KEY=
+# ELEVENLABS_API_BASE_URL=https://api.elevenlabs.io
+# ELEVENLABS_STT_MODEL_ID=scribe_v1
+# ELEVENLABS_VOICE_ID=21m00Tcm4TlvDq8ikWAM
+# ELEVENLABS_TTS_MODEL_ID=eleven_turbo_v2_5
diff --git a/ee/api/hands_free.py b/ee/api/hands_free.py
new file mode 100644
index 000000000000..0e72328242b4
--- /dev/null
+++ b/ee/api/hands_free.py
@@ -0,0 +1,232 @@
+"""Backend support for Max's hands-free mode.
+
+Hands-free mode lets a user talk to Max with their hands and eyes off the screen — built
+for mobile-web (gym, walking, driving). Speech-to-text runs via ElevenLabs Scribe directly
+from the browser using single-use tokens minted here so the ElevenLabs API key never
+reaches the client. Text-to-speech is proxied through the synthesize endpoint, again to
+keep the API key server-side; mp3 bytes stream back to the browser.
+"""
+
+from typing import Any
+
+from django.conf import settings
+from django.http import StreamingHttpResponse
+
+import requests
+import structlog
+from drf_spectacular.types import OpenApiTypes
+from drf_spectacular.utils import extend_schema
+from prometheus_client import Counter
+from rest_framework import serializers, status
+from rest_framework.decorators import action
+from rest_framework.exceptions import APIException
+from rest_framework.parsers import JSONParser
+from rest_framework.permissions import IsAuthenticated
+from rest_framework.request import Request
+from rest_framework.response import Response
+from rest_framework.viewsets import GenericViewSet
+
+from posthog.api.routing import TeamAndOrgViewSetMixin
+from posthog.permissions import PostHogFeatureFlagPermission
+from posthog.rate_limit import (
+ MaxHandsFreeSynthesizeBurstRateThrottle,
+ MaxHandsFreeSynthesizeSustainedRateThrottle,
+ MaxHandsFreeTokenBurstRateThrottle,
+ MaxHandsFreeTokenSustainedRateThrottle,
+)
+
+from ee.models.assistant import Conversation
+
+logger = structlog.get_logger(__name__)
+
+ELEVENLABS_TIMEOUT_SECONDS = (5, 10)
+# Generous enough for any Max summary, tight enough that an attacker minting at the
+# throttle ceiling can't ramp Scribe cost into the stratosphere.
+TTS_MAX_INPUT_CHARS = 2000
+
+# One counter, partitioned by outcome — lets us track abuse signal (provider rejection
+# spikes, repeated empty tokens) and steady-state cost (ok counts ≈ Scribe spend) without
+# blowing up cardinality via per-team labels.
+HANDS_FREE_TOKEN_COUNTER = Counter(
+ "max_hands_free_token_total",
+ "Outcomes for ElevenLabs Scribe single-use token mints from the hands-free endpoint.",
+ labelnames=["outcome"],
+)
+HANDS_FREE_SYNTHESIZE_COUNTER = Counter(
+ "max_hands_free_synthesize_total",
+ "Outcomes for ElevenLabs TTS proxy requests from the hands-free endpoint.",
+ labelnames=["outcome"],
+)
+HANDS_FREE_SYNTHESIZE_CHARS_COUNTER = Counter(
+ "max_hands_free_synthesize_chars_total",
+ "Total characters submitted for ElevenLabs TTS — proxy for TTS spend.",
+)
+
+
+class HandsFreeNotConfigured(APIException):
+ status_code = status.HTTP_503_SERVICE_UNAVAILABLE
+ default_detail = "Hands-free mode is not configured on this PostHog instance."
+ default_code = "hands_free_not_configured"
+
+
+class HandsFreeProviderError(APIException):
+ status_code = status.HTTP_502_BAD_GATEWAY
+ default_detail = "The hands-free provider rejected the request."
+ default_code = "hands_free_provider_error"
+
+
+class SynthesizeSerializer(serializers.Serializer):
+ text = serializers.CharField(
+ required=True,
+ max_length=TTS_MAX_INPUT_CHARS,
+ help_text="The text the assistant should speak aloud.",
+ )
+
+
+def _require_api_key(counter: Counter) -> str:
+ api_key = settings.ELEVENLABS_API_KEY
+ if not api_key:
+ counter.labels(outcome="missing_key").inc()
+ logger.warning("max_hands_free_api_key_missing")
+ raise HandsFreeNotConfigured()
+ return api_key
+
+
+class MaxHandsFreeViewSet(TeamAndOrgViewSetMixin, GenericViewSet):
+ scope_object = "INTERNAL"
+ # Hands-free actions are list-level and don't operate on any model — DRF's GenericViewSet
+ # needs a queryset attribute for the routing scaffolding but it is never read.
+ queryset = Conversation.objects.none()
+ # drf-spectacular requires every viewset to declare a serializer_class for OpenAPI
+ # generation. The `token` action takes no body and `synthesize` overrides this via
+ # its @extend_schema decorator, so SynthesizeSerializer is purely the schema-default
+ # — never instantiated by the viewset's own get_serializer().
+ serializer_class = SynthesizeSerializer
+ # Server-side gate on the same feature flag the frontend checks — without it an
+ # authenticated project member could POST directly to /token/ or /synthesize/ and
+ # rack up ElevenLabs spend even when their org doesn't have hands-free enabled.
+ posthog_feature_flag = "max-hands-free"
+ permission_classes = [IsAuthenticated, PostHogFeatureFlagPermission]
+
+ @extend_schema(responses={200: OpenApiTypes.OBJECT})
+ @action(
+ detail=False,
+ methods=["POST"],
+ url_path="token",
+ throttle_classes=[MaxHandsFreeTokenBurstRateThrottle, MaxHandsFreeTokenSustainedRateThrottle],
+ )
+ def token(self, request: Request, *args: Any, **kwargs: Any) -> Response:
+ """Mint a single-use ElevenLabs Scribe realtime token.
+
+ The browser uses the token to open a WebSocket directly to ElevenLabs — audio never
+ transits PostHog infrastructure. Tokens are time-bound (15 min) and single-use; the
+ per-team rate limit on this endpoint caps how often a user can mint new ones.
+
+ Never logs the upstream response body — provider error responses can echo PII back and
+ we don't want any of that landing in structured logs.
+ """
+ api_key = _require_api_key(HANDS_FREE_TOKEN_COUNTER)
+ try:
+ upstream = requests.post(
+ f"{settings.ELEVENLABS_API_BASE_URL}/v1/single-use-token/realtime_scribe",
+ headers={"xi-api-key": api_key},
+ timeout=ELEVENLABS_TIMEOUT_SECONDS,
+ )
+ except requests.RequestException:
+ HANDS_FREE_TOKEN_COUNTER.labels(outcome="provider_unreachable").inc()
+ logger.exception("max_hands_free_token_failed")
+ raise HandsFreeProviderError("Failed to reach the hands-free provider.")
+
+ if upstream.status_code != status.HTTP_200_OK:
+ HANDS_FREE_TOKEN_COUNTER.labels(outcome="provider_rejected").inc()
+ logger.warning("max_hands_free_token_rejected", status_code=upstream.status_code)
+ raise HandsFreeProviderError(f"Hands-free provider returned {upstream.status_code}.")
+
+ try:
+ body = upstream.json()
+ if not isinstance(body, dict):
+ raise ValueError("Expected JSON object")
+ token: str = body.get("token", "")
+ except ValueError:
+ HANDS_FREE_TOKEN_COUNTER.labels(outcome="provider_rejected").inc()
+ logger.warning("max_hands_free_token_malformed_json", status_code=upstream.status_code)
+ raise HandsFreeProviderError("Hands-free provider returned a malformed response.")
+ if not token:
+ HANDS_FREE_TOKEN_COUNTER.labels(outcome="empty_token").inc()
+ logger.warning("max_hands_free_token_empty")
+ raise HandsFreeProviderError("Hands-free provider returned an empty token.")
+ HANDS_FREE_TOKEN_COUNTER.labels(outcome="ok").inc()
+ return Response({"token": token})
+
+ @extend_schema(request=SynthesizeSerializer, responses={200: OpenApiTypes.BINARY})
+ @action(
+ detail=False,
+ methods=["POST"],
+ url_path="synthesize",
+ parser_classes=[JSONParser],
+ throttle_classes=[MaxHandsFreeSynthesizeBurstRateThrottle, MaxHandsFreeSynthesizeSustainedRateThrottle],
+ )
+ def synthesize(self, request: Request, *args: Any, **kwargs: Any) -> StreamingHttpResponse:
+ """Proxy text-to-speech to ElevenLabs, streaming mp3 audio back to the browser.
+
+ The viewset has no per-action `parser_classes` other than this one because the
+ token endpoint takes no body. Putting JSONParser here keeps the rest of the
+ viewset parser-free.
+ """
+ serializer = SynthesizeSerializer(data=request.data)
+ serializer.is_valid(raise_exception=True)
+ text = serializer.validated_data["text"]
+
+ api_key = _require_api_key(HANDS_FREE_SYNTHESIZE_COUNTER)
+ voice_id = settings.ELEVENLABS_VOICE_ID
+ if not voice_id:
+ HANDS_FREE_SYNTHESIZE_COUNTER.labels(outcome="missing_voice_id").inc()
+ logger.warning("max_hands_free_voice_id_missing")
+ raise HandsFreeNotConfigured("Voice ID is not configured.")
+ try:
+ upstream = requests.post(
+ f"{settings.ELEVENLABS_API_BASE_URL}/v1/text-to-speech/{voice_id}",
+ headers={
+ "xi-api-key": api_key,
+ "Accept": "audio/mpeg",
+ "Content-Type": "application/json",
+ },
+ json={"text": text, "model_id": settings.ELEVENLABS_TTS_MODEL_ID},
+ timeout=ELEVENLABS_TIMEOUT_SECONDS,
+ stream=True,
+ )
+ except requests.RequestException:
+ HANDS_FREE_SYNTHESIZE_COUNTER.labels(outcome="provider_unreachable").inc()
+ logger.exception("max_hands_free_synthesize_failed")
+ raise HandsFreeProviderError("Failed to reach the hands-free provider.")
+
+ if upstream.status_code != status.HTTP_200_OK:
+ HANDS_FREE_SYNTHESIZE_COUNTER.labels(outcome="provider_rejected").inc()
+ # ElevenLabs 4xx responses are JSON describing the account/quota state — not the
+ # user's input. Safe to log a snippet so devs can see "quota_exceeded" vs
+ # "voice_not_found" without hitting the dashboard.
+ body_preview = ""
+ try:
+ body_preview = upstream.text[:300].replace("\n", " ")
+ except Exception:
+ pass
+ logger.warning(
+ "max_hands_free_synthesize_rejected",
+ status_code=upstream.status_code,
+ body_preview=body_preview,
+ )
+ upstream.close()
+ raise HandsFreeProviderError(f"Hands-free provider returned {upstream.status_code}.")
+
+ HANDS_FREE_SYNTHESIZE_COUNTER.labels(outcome="ok").inc()
+ HANDS_FREE_SYNTHESIZE_CHARS_COUNTER.inc(len(text))
+
+ def stream_and_close() -> Any:
+ try:
+ yield from upstream.iter_content(chunk_size=4096)
+ finally:
+ upstream.close()
+
+ response = StreamingHttpResponse(stream_and_close(), content_type="audio/mpeg")
+ response["Cache-Control"] = "no-store"
+ return response
diff --git a/ee/api/test/test_hands_free.py b/ee/api/test/test_hands_free.py
new file mode 100644
index 000000000000..4c6850652e35
--- /dev/null
+++ b/ee/api/test/test_hands_free.py
@@ -0,0 +1,130 @@
+from posthog.test.base import APIBaseTest
+from unittest.mock import MagicMock, patch
+
+from django.test import override_settings
+
+
+@override_settings(
+ ELEVENLABS_API_KEY="test-key",
+ ELEVENLABS_API_BASE_URL="https://api.elevenlabs.example",
+)
+class TestMaxHandsFreeAPI(APIBaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ # The viewset gates on the max-hands-free feature flag server-side; force it on
+ # so tests exercise the endpoint logic rather than the entitlement check.
+ self._ff_patcher = patch("posthoganalytics.feature_enabled", return_value=True)
+ self._ff_patcher.start()
+ self.addCleanup(self._ff_patcher.stop)
+
+ def _token_url(self) -> str:
+ return f"/api/environments/{self.team.id}/max_hands_free/token/"
+
+ @patch("ee.api.hands_free.requests.post")
+ def test_token_returns_signed_token_from_elevenlabs(self, mock_post: MagicMock) -> None:
+ mock_post.return_value = MagicMock(
+ status_code=200,
+ json=MagicMock(return_value={"token": "single-use-token-abc"}),
+ )
+
+ response = self.client.post(self._token_url(), format="json")
+
+ assert response.status_code == 200
+ assert response.json() == {"token": "single-use-token-abc"}
+ assert mock_post.call_args.args[0].endswith("/v1/single-use-token/realtime_scribe")
+ assert mock_post.call_args.kwargs["headers"]["xi-api-key"] == "test-key"
+
+ @override_settings(ELEVENLABS_API_KEY="")
+ def test_returns_503_when_api_key_missing(self) -> None:
+ response = self.client.post(self._token_url(), format="json")
+ assert response.status_code == 503
+
+ @patch("ee.api.hands_free.requests.post")
+ def test_returns_502_when_provider_errors(self, mock_post: MagicMock) -> None:
+ mock_post.return_value = MagicMock(status_code=429, text="rate limited")
+ response = self.client.post(self._token_url(), format="json")
+ assert response.status_code == 502
+
+ @patch("ee.api.hands_free.requests.post")
+ def test_returns_502_when_provider_returns_empty_token(self, mock_post: MagicMock) -> None:
+ mock_post.return_value = MagicMock(status_code=200, json=MagicMock(return_value={"token": ""}))
+ response = self.client.post(self._token_url(), format="json")
+ assert response.status_code == 502
+
+ @patch("ee.api.hands_free.requests.post")
+ def test_provider_error_does_not_log_response_body(self, mock_post: MagicMock) -> None:
+ upstream = MagicMock(status_code=500, text="ECHOED API KEY OR PII")
+ mock_post.return_value = upstream
+
+ with patch("ee.api.hands_free.logger") as mock_logger:
+ response = self.client.post(self._token_url(), format="json")
+
+ assert response.status_code == 502
+ log_calls = mock_logger.warning.call_args_list + mock_logger.exception.call_args_list
+ for call in log_calls:
+ for value in list(call.args) + list(call.kwargs.values()):
+ assert "ECHOED API KEY OR PII" not in str(value)
+
+ def test_requires_authentication(self) -> None:
+ self.client.logout()
+ response = self.client.post(self._token_url(), format="json")
+ assert response.status_code in (401, 403)
+
+ def _synthesize_url(self) -> str:
+ return f"/api/environments/{self.team.id}/max_hands_free/synthesize/"
+
+ @patch("ee.api.hands_free.requests.post")
+ def test_synthesize_streams_audio_from_elevenlabs(self, mock_post: MagicMock) -> None:
+ mock_post.return_value = MagicMock(
+ status_code=200,
+ iter_content=MagicMock(return_value=iter([b"mp3-chunk-1", b"mp3-chunk-2"])),
+ )
+
+ response = self.client.post(self._synthesize_url(), data={"text": "hello"}, format="json")
+
+ assert response.status_code == 200
+ assert response["Content-Type"] == "audio/mpeg"
+ assert response["Cache-Control"] == "no-store"
+ assert b"".join(response.streaming_content) == b"mp3-chunk-1mp3-chunk-2" # type: ignore[attr-defined]
+ mock_post.return_value.close.assert_called_once()
+
+ def test_synthesize_rejects_text_over_limit(self) -> None:
+ response = self.client.post(
+ self._synthesize_url(),
+ data={"text": "x" * 2001},
+ format="json",
+ )
+ assert response.status_code == 400
+
+ def test_synthesize_rejects_empty_text(self) -> None:
+ response = self.client.post(self._synthesize_url(), data={"text": ""}, format="json")
+ assert response.status_code == 400
+
+ @patch("ee.api.hands_free.requests.post")
+ def test_synthesize_502_when_provider_errors(self, mock_post: MagicMock) -> None:
+ mock_post.return_value = MagicMock(status_code=429, text="quota exceeded")
+ response = self.client.post(self._synthesize_url(), data={"text": "hello"}, format="json")
+ assert response.status_code == 502
+ mock_post.return_value.close.assert_called_once()
+
+ @override_settings(ELEVENLABS_API_KEY="")
+ def test_synthesize_503_when_api_key_missing(self) -> None:
+ response = self.client.post(self._synthesize_url(), data={"text": "hello"}, format="json")
+ assert response.status_code == 503
+
+ def test_endpoints_403_when_feature_flag_disabled_for_org(self) -> None:
+ # Stop the class-level patch so the real PostHogFeatureFlagPermission runs against
+ # an org that doesn't have the flag enabled — expect 403, no upstream calls.
+ self._ff_patcher.stop()
+ with patch("posthoganalytics.feature_enabled", return_value=False):
+ token_response = self.client.post(self._token_url(), format="json")
+ synth_response = self.client.post(self._synthesize_url(), data={"text": "hello"}, format="json")
+ # Restart so addCleanup doesn't double-stop.
+ self._ff_patcher.start()
+ assert token_response.status_code == 403
+ assert synth_response.status_code == 403
+
+ def test_synthesize_requires_authentication(self) -> None:
+ self.client.logout()
+ response = self.client.post(self._synthesize_url(), data={"text": "hello"}, format="json")
+ assert response.status_code in (401, 403)
diff --git a/ee/urls.py b/ee/urls.py
index db971760445a..3880f9f4fd2a 100644
--- a/ee/urls.py
+++ b/ee/urls.py
@@ -46,7 +46,7 @@ def extend_api_router() -> None:
router as root_router,
)
- from ee.api import max_tools, session_summaries
+ from ee.api import hands_free, max_tools, session_summaries
root_router.register(r"billing", billing.BillingViewset, "billing")
root_router.register(r"license", license.LicenseViewSet)
@@ -98,6 +98,10 @@ def extend_api_router() -> None:
environments_router.register(r"max_tools", max_tools.MaxToolsViewSet, "environment_max_tools", ["team_id"])
+ environments_router.register(
+ r"max_hands_free", hands_free.MaxHandsFreeViewSet, "environment_max_hands_free", ["team_id"]
+ )
+
environments_router.register(
r"session_summaries", session_summaries.SessionSummariesViewSet, "environment_session_summaries", ["team_id"]
)
diff --git a/frontend/package.json b/frontend/package.json
index 3afe5452ead9..de6eec0125e2 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -55,6 +55,7 @@
"@dnd-kit/modifiers": "^6.0.1",
"@dnd-kit/sortable": "^7.0.2",
"@dnd-kit/utilities": "^3.2.1",
+ "@elevenlabs/client": "^1.4.0",
"@floating-ui/react": "^0.27.19",
"@marsidev/react-turnstile": "^1.4.2",
"@mathjax/src": "^4.1.1",
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 8798a2e5293d..b2c984f42cfe 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -5322,6 +5322,14 @@ snapshots:
hash: v1.k794b7964.1c953319963cc8cf02830f6a32d827a568450c7fb616e821d529680f6460580f.0sXugUKUJMaa2I1_M5Bm6Lyrwu1qp4WXn5ZP_VykSZs
scenes-app-posthog-ai--welcome-with-latest-conversations--light:
hash: v1.k794b7964.e748e62b24fe96ae9cd96facb3d679d9649615f7b092f31dc1e3a67aef15c490._-KVj3kzPaGvcu9N4RgpmLPK1ovZANsm4YM9t1s4IUc
+ scenes-app-posthog-ai-hands-free-surface--speaking--dark:
+ hash: v1.k794b7964.48aaf5c134a71a9785a39769a4e692844d4f65f8886a589cdcd839a477333aa0.4Cm1e9dqys5vNwnfcvQctrRtB7POvdHdQlL1UFxA4LA
+ scenes-app-posthog-ai-hands-free-surface--speaking--light:
+ hash: v1.k794b7964.a59ea311f928f55fd230493b29d54170a002d46170a9882a4602922da5819f90.LUh0Yr6hiwR3UTrmy6cqna9EYTalFuan6zEnwJ-h7ig
+ scenes-app-posthog-ai-hands-free-surface--with-error--dark:
+ hash: v1.k794b7964.38aeb671b882ed7442192a236a3c3e28f8a4ff0d860a83a50d2618b923ba708f.HNuM32DwEhnFE_3a1YNCx3XpKflFOOk-vdkvGTHb-gs
+ scenes-app-posthog-ai-hands-free-surface--with-error--light:
+ hash: v1.k794b7964.67f7224ae95951037f9dc1fdb139381cf660906a8aa74bf0b38d71ddafe32c36.jfQuDmVYqBzDD9CH64NSAlzaWkaNp8qhvPpvFZajfGc
scenes-app-project-homepage--no-primary-dashboard--dark:
hash: v1.k794b7964.83d267b40bfc2dca4b211cb28537f4b958822ee469c611e60535af2ab79e1621.2YhseEUdyiKAro1A2SILMNhqo09MkyLxq58IMRPK-E8
scenes-app-project-homepage--no-primary-dashboard--light:
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 8d890ffc92ac..c9805b762c91 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -1714,6 +1714,11 @@ export class ApiRequest {
return this.environmentsDetail(teamId).addPathComponent('conversations').addPathComponent(id)
}
+ // Max hands-free mode (ElevenLabs Scribe token proxy)
+ public maxHandsFree(teamId?: TeamType['id']): ApiRequest {
+ return this.environmentsDetail(teamId).addPathComponent('max_hands_free')
+ }
+
// Conversations (Support product)
public conversationsTickets(teamId?: TeamType['id']): ApiRequest {
return this.projectsDetail(teamId).addPathComponent('conversations').addPathComponent('tickets')
@@ -6415,6 +6420,23 @@ const api = {
},
},
+ maxHandsFree: {
+ async token(options?: ApiMethodOptions): Promise<{ token: string }> {
+ return await api.create(
+ new ApiRequest().maxHandsFree().withAction('token').assembleFullUrl(),
+ undefined,
+ options
+ )
+ },
+ async synthesize(text: string, options?: ApiMethodOptions): Promise<Response> {
+ return await api.createResponse(
+ new ApiRequest().maxHandsFree().withAction('synthesize').assembleFullUrl(),
+ { text },
+ options
+ )
+ },
+ },
+
conversations: {
async stream(
data: {
diff --git a/frontend/src/lib/components/AppShortcuts/appShortcutLogic.tsx b/frontend/src/lib/components/AppShortcuts/appShortcutLogic.tsx
index d95b07e1418d..12ccfd972661 100644
--- a/frontend/src/lib/components/AppShortcuts/appShortcutLogic.tsx
+++ b/frontend/src/lib/components/AppShortcuts/appShortcutLogic.tsx
@@ -178,8 +178,8 @@ export const appShortcutLogic = kea<appShortcutLogicType>([
return
}
- // Handle sequence shortcuts (no modifier keys, not in editable elements)
- if (isEditableElement(event) || event.altKey) {
+ // Handle sequence shortcuts (no modifier keys, not in editable elements, not mid-IME-composition)
+ if (isEditableElement(event) || event.altKey || event.isComposing) {
return
}
diff --git a/frontend/src/lib/components/AppShortcuts/shortcuts.ts b/frontend/src/lib/components/AppShortcuts/shortcuts.ts
index c3b00a373bf4..310d1999fbed 100644
--- a/frontend/src/lib/components/AppShortcuts/shortcuts.ts
+++ b/frontend/src/lib/components/AppShortcuts/shortcuts.ts
@@ -38,6 +38,7 @@ export const keyBinds: Record<string, string[]> = {
tab9: [...baseModifier, '9'],
zenMode: [...baseModifier, 'z'],
newChat: ['g', 'then', 'n'],
+ maxHandsFree: ['h', 'then', 'f'],
allChats: ['g', 'then', '1'],
allApps: ['g', 'then', '2'],
theme: ['g', 'then', 't'],
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsx
index 0d9239a554df..aad323e2b090 100644
--- a/frontend/src/lib/constants.tsx
+++ b/frontend/src/lib/constants.tsx
@@ -368,6 +368,7 @@ export const FEATURE_FLAGS = {
MAX_AI_INSIGHT_SEARCH: 'max-ai-insight-search', // owner: #team-posthog-ai
MAX_BILLING_CONTEXT: 'max-billing-context', // owner: @pawel-cebula #team-billing
MAX_DEEP_RESEARCH: 'max-deep-research', // owner: @kappa90 #team-posthog-ai
+ MAX_HANDS_FREE: 'max-hands-free', // owner: #team-posthog-ai
MCP_ANALYTICS: 'mcp-analytics', // owner: #project-mcp-analytics
MCP_ANALYTICS_INTENT_ROUTING: 'mcp-analytics-intent-routing', // owner: #project-mcp-analytics
MCP_HINTS: 'mcp-hints', // owner: @rafaeelaudibert #team-growth, gates in-app MCP hint toasts and empty-state cards
diff --git a/frontend/src/scenes/max/components/HandsFreeButton.tsx b/frontend/src/scenes/max/components/HandsFreeButton.tsx
new file mode 100644
index 000000000000..6602192e6bf5
--- /dev/null
+++ b/frontend/src/scenes/max/components/HandsFreeButton.tsx
@@ -0,0 +1,43 @@
+import { useActions, useValues } from 'kea'
+
+import { IconMicrophone } from '@posthog/icons'
+import { LemonButton } from '@posthog/lemon-ui'
+
+import { AppShortcut } from 'lib/components/AppShortcuts/AppShortcut'
+import { keyBinds } from 'lib/components/AppShortcuts/shortcuts'
+import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
+
+import { handsFreeLogic } from '../handsFreeLogic'
+
+interface HandsFreeButtonProps {
+ tabId: string
+}
+
+export function HandsFreeButton({ tabId }: HandsFreeButtonProps): JSX.Element | null {
+ const flagEnabled = useFeatureFlag('MAX_HANDS_FREE')
+ const { status, canUseHandsFree } = useValues(handsFreeLogic({ tabId }))
+ const { toggleHandsFree } = useActions(handsFreeLogic({ tabId }))
+
+ if (!flagEnabled || !canUseHandsFree || status !== 'off') {
+ return null
+ }
+
+ return (
+ <AppShortcut
+ name="maxHandsFree"
+ keybind={[keyBinds.maxHandsFree]}
+ intent="Enter hands-free"
+ interaction="click"
+ >
+ <LemonButton
+ data-attr="max-hands-free-toggle"
+ size="small"
+ type="tertiary"
+ icon={<IconMicrophone />}
+ onClick={toggleHandsFree}
+ tooltip="Enter hands-free"
+ aria-label="Enter hands-free"
+ />
+ </AppShortcut>
+ )
+}
diff --git a/frontend/src/scenes/max/components/HandsFreeSurface.scss b/frontend/src/scenes/max/components/HandsFreeSurface.scss
new file mode 100644
index 000000000000..d23c09e4e2b2
--- /dev/null
+++ b/frontend/src/scenes/max/components/HandsFreeSurface.scss
@@ -0,0 +1,203 @@
+.hands-free-surface {
+ display: grid;
+ grid-template-rows: minmax(2.5rem, auto) auto minmax(1.5rem, auto);
+ gap: 1rem;
+ place-items: center center;
+ width: 100%;
+ min-height: 11rem;
+ padding: 1.25rem 1rem;
+ text-align: center;
+}
+
+.hands-free-surface__top {
+ display: flex;
+ flex-direction: column;
+ gap: 0.25rem;
+ align-items: center;
+ justify-content: center;
+ width: 100%;
+ min-height: 2.5rem;
+}
+
+.hands-free-surface__partial {
+ max-width: 32rem;
+ margin: 0;
+ font-size: 1rem;
+ line-height: 1.4;
+ color: var(--color-text-primary);
+ word-wrap: break-word;
+}
+
+.hands-free-surface__hint {
+ max-width: 32rem;
+ margin: 0;
+ font-size: 0.8125rem;
+ font-style: italic;
+ color: var(--color-text-secondary);
+}
+
+.hands-free-surface__error {
+ margin: 0;
+ font-size: 0.8125rem;
+ color: var(--color-danger);
+}
+
+.hands-free-surface__mic {
+ position: relative;
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ width: 3rem;
+ height: 3rem;
+ color: var(--color-text-primary);
+ appearance: none;
+ cursor: pointer;
+ background: var(--color-bg-fill-secondary);
+ border: none;
+ border-radius: 50%;
+ box-shadow: 0 1px 2px rgb(0 0 0 / 20%);
+ transition:
+ transform 120ms ease,
+ background 120ms ease;
+
+ svg {
+ width: 1.25rem;
+ height: 1.25rem;
+ }
+
+ &:hover {
+ transform: scale(1.04);
+ }
+
+ &:active {
+ transform: scale(0.97);
+ }
+}
+
+.hands-free-surface__mic--listening {
+ color: var(--color-text-on-fill);
+ background: var(--color-success);
+}
+
+.hands-free-surface__mic--speaking {
+ color: var(--color-text-on-fill);
+ background: var(--color-ai);
+}
+
+.hands-free-surface__mic--thinking,
+.hands-free-surface__mic--starting,
+.hands-free-surface__mic--reconnecting {
+ color: var(--color-text-on-fill);
+ background: var(--color-warning);
+}
+
+.hands-free-surface__mic--error {
+ color: var(--color-text-on-fill);
+ background: var(--color-danger);
+}
+
+// Larger tap target on phones — the primary affordance of a hands-free product,
+// and gym/walking users need more than the 48px desktop default.
+@media (max-width: 480px) {
+ .hands-free-surface__mic {
+ width: 4rem;
+ height: 4rem;
+
+ svg {
+ width: 1.75rem;
+ height: 1.75rem;
+ }
+ }
+}
+
+// Pulse applied only when status === 'listening' (your turn to talk).
+.hands-free-surface__mic--pulsing {
+ animation: HandsFreeSurface__micPulse 1.6s ease-in-out infinite;
+}
+
+// Spinner ring around the mic when thinking/starting/reconnecting — keeps the mic icon
+// visible so the user still knows the button is tappable to exit.
+.hands-free-surface__mic--thinking::after,
+.hands-free-surface__mic--starting::after,
+.hands-free-surface__mic--reconnecting::after {
+ position: absolute;
+ inset: -4px;
+ content: '';
+ border: 2px solid transparent;
+ border-top-color: currentColor;
+ border-radius: 50%;
+ animation: HandsFreeSurface__micSpin 1s linear infinite;
+}
+
+@keyframes HandsFreeSurface__micSpin {
+ to {
+ transform: rotate(360deg);
+ }
+}
+
+@keyframes HandsFreeSurface__micPulse {
+ 0%,
+ 100% {
+ box-shadow: 0 0 0 0 rgb(0 0 0 / 0%);
+ }
+
+ 50% {
+ box-shadow: 0 0 0 0.75rem rgb(255 255 255 / 8%);
+ }
+}
+
+.hands-free-surface__bottom {
+ display: inline-flex;
+ gap: 0.5rem;
+ align-items: center;
+}
+
+.hands-free-surface__dot {
+ display: inline-block;
+ flex-shrink: 0;
+ width: 0.5rem;
+ height: 0.5rem;
+ background: var(--color-bg-fill-tertiary);
+ border-radius: 50%;
+}
+
+.hands-free-surface__dot--listening {
+ background: var(--color-success);
+}
+
+.hands-free-surface__dot--speaking {
+ background: var(--color-ai);
+}
+
+.hands-free-surface__dot--thinking,
+.hands-free-surface__dot--starting,
+.hands-free-surface__dot--reconnecting {
+ background: var(--color-warning);
+}
+
+.hands-free-surface__dot--error {
+ background: var(--color-danger);
+}
+
+.hands-free-surface__dot--pulsing {
+ animation: HandsFreeSurface__pulse 1.4s ease-in-out infinite;
+}
+
+@keyframes HandsFreeSurface__pulse {
+ 0%,
+ 100% {
+ opacity: 1;
+ transform: scale(1);
+ }
+
+ 50% {
+ opacity: 0.5;
+ transform: scale(1.4);
+ }
+}
+
+.hands-free-surface__label {
+ font-size: 0.875rem;
+ font-weight: 600;
+ color: var(--color-text-primary);
+}
diff --git a/frontend/src/scenes/max/components/HandsFreeSurface.stories.tsx b/frontend/src/scenes/max/components/HandsFreeSurface.stories.tsx
new file mode 100644
index 000000000000..eb4710adcec2
--- /dev/null
+++ b/frontend/src/scenes/max/components/HandsFreeSurface.stories.tsx
@@ -0,0 +1,101 @@
+import { Meta, StoryObj } from '@storybook/react'
+import { useActions, useMountedLogic } from 'kea'
+import { useEffect } from 'react'
+
+import { HandsFreeConnection, HandsFreeStatus, handsFreeLogic } from '../handsFreeLogic'
+import { maxLogic } from '../maxLogic'
+import { HandsFreeSurface } from './HandsFreeSurface'
+
+const TAB_ID = 'hands-free-storybook'
+
+interface DriverProps {
+ status: HandsFreeStatus
+ connection?: HandsFreeConnection
+ partialTranscript?: string
+ error?: string | null
+}
+
+// Mounts maxLogic + handsFreeLogic and drives them into a target state so each story
+// captures a single visual configuration without spinning up real WebSockets or audio.
+function HandsFreeSurfaceStory({ status, connection, partialTranscript, error }: DriverProps): JSX.Element {
+ useMountedLogic(maxLogic({ tabId: TAB_ID }))
+ const logic = handsFreeLogic({ tabId: TAB_ID })
+ useMountedLogic(logic)
+ const { setStatus, setConnection, setPartialTranscript, setError } = useActions(logic)
+
+ useEffect(() => {
+ setStatus(status)
+ if (connection) {
+ setConnection(connection)
+ }
+ if (partialTranscript) {
+ setPartialTranscript(partialTranscript)
+ }
+ if (error) {
+ setError(error)
+ }
+ }, [status, connection, partialTranscript, error, setStatus, setConnection, setPartialTranscript, setError])
+
+ return (
+ <div className="max-w-md mx-auto p-4 bg-bg-light border rounded">
+ <HandsFreeSurface tabId={TAB_ID} />
+ </div>
+ )
+}
+
+const meta: Meta<typeof HandsFreeSurfaceStory> = {
+ title: 'Scenes-App/PostHog AI/Hands-free surface',
+ component: HandsFreeSurfaceStory,
+ parameters: {
+ layout: 'centered',
+ },
+}
+export default meta
+
+type Story = StoryObj<typeof HandsFreeSurfaceStory>
+
+// Stories whose mic ring/pulse animates are excluded from visual regression — the
+// running CSS animation makes the snapshot diff flake. Static visual states (speaking,
+// error) keep their snapshot so we still catch unintended colour/layout drift.
+const ANIMATED_SKIP_TAGS = ['test-skip']
+
+export const Starting: Story = {
+ args: { status: 'starting', connection: 'connecting' },
+ tags: ANIMATED_SKIP_TAGS,
+}
+
+export const Listening: Story = {
+ args: { status: 'listening', connection: 'connected' },
+ tags: ANIMATED_SKIP_TAGS,
+}
+
+export const ListeningWithPartialTranscript: Story = {
+ args: {
+ status: 'listening',
+ connection: 'connected',
+ partialTranscript: 'how are the daily active users tracking against',
+ },
+ tags: ANIMATED_SKIP_TAGS,
+}
+
+export const Thinking: Story = {
+ args: { status: 'thinking', connection: 'connected' },
+ tags: ANIMATED_SKIP_TAGS,
+}
+
+export const Speaking: Story = {
+ args: { status: 'speaking', connection: 'connected' },
+}
+
+export const Reconnecting: Story = {
+ args: { status: 'listening', connection: 'reconnecting' },
+ tags: ANIMATED_SKIP_TAGS,
+}
+
+export const WithError: Story = {
+ args: {
+ status: 'listening',
+ connection: 'connected',
+ error: 'Microphone access was denied.',
+ },
+}
diff --git a/frontend/src/scenes/max/components/HandsFreeSurface.tsx b/frontend/src/scenes/max/components/HandsFreeSurface.tsx
new file mode 100644
index 000000000000..cd18e353028f
--- /dev/null
+++ b/frontend/src/scenes/max/components/HandsFreeSurface.tsx
@@ -0,0 +1,124 @@
+import './HandsFreeSurface.scss'
+
+import { useActions, useValues } from 'kea'
+
+import { IconMicrophone } from '@posthog/icons'
+
+import { keyBinds } from 'lib/components/AppShortcuts/shortcuts'
+import { useAppShortcut } from 'lib/components/AppShortcuts/useAppShortcut'
+import { cn } from 'lib/utils/css-classes'
+
+import { HandsFreeStatus, handsFreeLogic } from '../handsFreeLogic'
+
+interface HandsFreeSurfaceProps {
+ tabId: string
+}
+
+const STATUS_LABEL: Record<HandsFreeStatus, string> = {
+ off: '',
+ starting: 'Connecting',
+ listening: 'Listening',
+ thinking: 'Thinking',
+ speaking: 'Speaking',
+}
+
+const STATUS_HINT: Record<HandsFreeStatus, string> = {
+ off: '',
+ starting: 'One moment, getting microphone ready',
+ listening: 'I will send your question when you pause speaking',
+ thinking: 'Working on your answer',
+ speaking: 'Start talking to interrupt',
+}
+
+function HandsFreeTopline({
+ tabId,
+ status,
+ isReconnecting,
+}: {
+ tabId: string
+ status: HandsFreeStatus
+ isReconnecting: boolean
+}): JSX.Element {
+ const { partialTranscript, error } = useValues(handsFreeLogic({ tabId }))
+ const hint = isReconnecting ? 'Reconnecting your microphone' : STATUS_HINT[status]
+ return (
+ <div className="hands-free-surface__top">
+ {partialTranscript ? (
+ <p className="hands-free-surface__partial">{partialTranscript}</p>
+ ) : (
+ <p className="hands-free-surface__hint">{hint}</p>
+ )}
+ {error && <p className="hands-free-surface__error">{error}</p>}
+ </div>
+ )
+}
+
+export function HandsFreeSurface({ tabId }: HandsFreeSurfaceProps): JSX.Element | null {
+ const { status, connection, error } = useValues(handsFreeLogic({ tabId }))
+ const { toggleHandsFree } = useActions(handsFreeLogic({ tabId }))
+
+ // Register the v-then-m exit shortcut while the surface is mounted.
+ // HandsFreeButton owns the same shortcut for the "enter" path; same-name
+ // re-registration in appShortcutLogic handles the handover cleanly.
+ useAppShortcut({
+ name: 'maxHandsFree',
+ keybind: [keyBinds.maxHandsFree],
+ intent: 'Exit hands-free',
+ interaction: 'function',
+ callback: toggleHandsFree,
+ })
+
+ if (status === 'off') {
+ return null
+ }
+
+ const isListening = status === 'listening'
+ const isReconnecting = connection === 'reconnecting'
+ const hasError = !!error
+ // Reconnecting / error visuals take precedence over the listening pulse so the user
+ // isn't shown a green "listening" mic when the underlying connection is dead.
+ const visualState: HandsFreeStatus | 'reconnecting' | 'error' = hasError
+ ? 'error'
+ : isReconnecting
+ ? 'reconnecting'
+ : status
+ const label = hasError ? 'Error' : isReconnecting ? 'Reconnecting' : STATUS_LABEL[status]
+ const pulseClass = isListening && !isReconnecting && !hasError
+
+ return (
+ <div
+ className="hands-free-surface"
+ data-attr="max-hands-free-surface"
+ data-status={status}
+ data-connection={connection}
+ >
+ <HandsFreeTopline tabId={tabId} status={status} isReconnecting={isReconnecting} />
+
+ <button
+ type="button"
+ onClick={toggleHandsFree}
+ aria-label="Exit hands-free"
+ data-attr="max-hands-free-exit"
+ className={cn(
+ 'hands-free-surface__mic',
+ `hands-free-surface__mic--${visualState}`,
+ pulseClass && 'hands-free-surface__mic--pulsing'
+ )}
+ >
+ <IconMicrophone />
+ </button>
+
+ <div className="hands-free-surface__bottom">
+ <span
+ className={cn(
+ 'hands-free-surface__dot',
+ `hands-free-surface__dot--${visualState}`,
+ pulseClass && 'hands-free-surface__dot--pulsing'
+ )}
+ aria-hidden
+ />
+ <span className="hands-free-surface__label">{label}</span>
+ </div>
+ </div>
+ )
+}
diff --git a/frontend/src/scenes/max/components/QuestionInput.tsx b/frontend/src/scenes/max/components/QuestionInput.tsx
index 80b683368c3f..35fde019b81f 100644
--- a/frontend/src/scenes/max/components/QuestionInput.tsx
+++ b/frontend/src/scenes/max/components/QuestionInput.tsx
@@ -1,7 +1,6 @@
import './QuestionInput.scss'
import { offset } from '@floating-ui/react'
-import clsx from 'clsx'
import { useActions, useValues } from 'kea'
import posthog from 'posthog-js'
import React, { ReactNode, useEffect, useMemo, useRef, useState } from 'react'
@@ -9,6 +8,8 @@ import React, { ReactNode, useEffect, useMemo, useRef, useState } from 'react'
import { IconArrowRight, IconCheck, IconPencil, IconStopFilled, IconTrash, IconX } from '@posthog/icons'
import { LemonButton, LemonSwitch, LemonTextArea, Spinner } from '@posthog/lemon-ui'
+import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
+import { cn } from 'lib/utils/css-classes'
import { AIConsentPopoverWrapper } from 'scenes/settings/organization/AIConsentPopoverWrapper'
import { userLogic } from 'scenes/userLogic'
@@ -17,10 +18,13 @@ import { AgentMode } from '~/queries/schema/schema-assistant-messages'
import { ConversationQueueMessage } from '~/types'
import { ContextDisplay } from '../Context'
+import { handsFreeLogic } from '../handsFreeLogic'
import { maxGlobalLogic } from '../maxGlobalLogic'
import { maxLogic } from '../maxLogic'
import { maxThreadLogic } from '../maxThreadLogic'
import { MAX_SLASH_COMMANDS } from '../slash-commands'
+import { HandsFreeButton } from './HandsFreeButton'
+import { HandsFreeSurface } from './HandsFreeSurface'
import { SlashCommandAutocomplete } from './SlashCommandAutocomplete'
interface QuestionInputProps {
@@ -140,7 +144,7 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputProps
ref
) {
const { dataProcessingAccepted, dataProcessingApprovalDisabledReason } = useValues(maxGlobalLogic)
- const { question } = useValues(maxLogic)
+ const { question, tabId: maxTabId } = useValues(maxLogic)
const { setQuestion } = useActions(maxLogic)
const { user } = useValues(userLogic)
const {
@@ -162,6 +166,12 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputProps
} = useValues(maxThreadLogic)
const { askMax, stopGeneration, completeThreadGeneration, setSupportOverrideEnabled, updateQueuedMessage } =
useActions(maxThreadLogic)
+ const { isActive: handsFreeActive } = useValues(handsFreeLogic({ tabId: maxTabId }))
+ // Only the hands-free row needs bottom-aligned pills — it has the mic + submit pair
+ // pinned to the bottom and pills sitting in normal flow look misaligned next to them.
+ // Keep the legacy items-start layout when the flag is off so existing screenshots
+ // (and the layout users without hands-free see) don't change.
+ const handsFreeFlagEnabled = useFeatureFlag('MAX_HANDS_FREE')
// Show info banner for conversations created during impersonation (marked as internal)
const isImpersonatedInternalConversation = user?.is_impersonated && conversation?.is_internal
@@ -202,16 +212,16 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputProps
return (
<div
- className={clsx(
- containerClassName,
- !isSticky && !isThreadVisible
- ? 'px-3 w-[min(40rem,100%)]'
- : 'sticky bottom-0 z-10 w-full max-w-180 self-center'
+ className={cn(
+ 'w-full px-3',
+ (isSticky || isThreadVisible) && 'sticky bottom-0 z-10 max-w-180 self-center',
+ // containerClassName last so callers can override (e.g. sidePanel's px-0).
+ containerClassName
)}
ref={ref}
>
<div
- className={clsx(
+ className={cn(
'flex flex-col items-center',
isSticky && 'border border-primary rounded-lg backdrop-blur-sm bg-glass-bg-3000'
)}
@@ -253,7 +263,7 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputProps
)}
<label
htmlFor="question-input"
- className={clsx(
+ className={cn(
'input-like flex flex-col cursor-text',
'border border-primary',
'bg-[var(--color-bg-fill-input)]',
@@ -262,90 +272,118 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputProps
!streamingActive && '[--input-ring-color:var(--color-ai)]'
)}
>
- <SlashCommandAutocomplete visible={showAutocomplete} onClose={() => setShowAutocomplete(false)}>
- <div className="relative w-full">
- {!question && (
- <div
- id="textarea-hint"
- className="text-secondary absolute top-4 left-4 text-sm pointer-events-none"
- >
- {conversation && isSharedThread ? (
- `This thread was shared with you by ${conversation.user.first_name} ${conversation.user.last_name}`
- ) : threadLoading ? (
- 'Thinking…'
- ) : isThreadVisible ? (
- placeholder || (
+ {handsFreeActive ? (
+ <HandsFreeSurface tabId={maxTabId} />
+ ) : (
+ <SlashCommandAutocomplete
+ visible={showAutocomplete}
+ onClose={() => setShowAutocomplete(false)}
+ >
+ <div className="relative w-full">
+ {!question && (
+ <div
+ id="textarea-hint"
+ className="text-secondary absolute top-4 left-4 text-sm pointer-events-none"
+ >
+ {conversation && isSharedThread ? (
+ `This thread was shared with you by ${conversation.user.first_name} ${conversation.user.last_name}`
+ ) : threadLoading ? (
+ 'Thinking…'
+ ) : isThreadVisible ? (
+ placeholder || (
+ <>
+ Ask follow-up{' '}
+ <span className="text-tertiary opacity-80 contrast-more:opacity-100">
+ or / for commands
+ </span>
+ </>
+ )
+ ) : (
<>
- Ask follow-up{' '}
+ Ask a question{' '}
<span className="text-tertiary opacity-80 contrast-more:opacity-100">
or / for commands
</span>
</>
- )
- ) : (
- <>
- Ask a question{' '}
- <span className="text-tertiary opacity-80 contrast-more:opacity-100">
- or / for commands
- </span>
- </>
- )}
- </div>
- )}
- <LemonTextArea
- aria-describedby={!question ? 'textarea-hint' : undefined}
- id="question-input"
- data-attr="max-chat-input"
- ref={textAreaRef}
- value={isSharedThread ? '' : question}
- onChange={(value) => setQuestion(value)}
- onPressEnter={() => {
- if (
- hasQuestion &&
- !submissionDisabledReason &&
- (!threadLoading || queueingEnabled)
- ) {
- onSubmit?.()
- askMax(question)
- }
- }}
- onKeyDown={(event) => {
- if (
- event.key === 'ArrowUp' &&
- !question.trim() &&
- queuedMessages.length > 0 &&
- !editingQueueId
- ) {
- const target = event.currentTarget
- const atStart = target.selectionStart === 0 && target.selectionEnd === 0
- const isSingleLine = target.value.split('\n').length <= 1
- if (!atStart || !isSingleLine) {
- return
+ )}
+ </div>
+ )}
+ <LemonTextArea
+ aria-describedby={!question ? 'textarea-hint' : undefined}
+ id="question-input"
+ data-attr="max-chat-input"
+ ref={textAreaRef}
+ value={isSharedThread ? '' : question}
+ onChange={(value) => setQuestion(value)}
+ onPressEnter={() => {
+ if (
+ hasQuestion &&
+ !submissionDisabledReason &&
+ (!threadLoading || queueingEnabled)
+ ) {
+ onSubmit?.()
+ askMax(question)
}
- const nextMessageId = queuedMessages[0]?.id
- if (!nextMessageId) {
- return
+ }}
+ onKeyDown={(event) => {
+ if (
+ event.key === 'ArrowUp' &&
+ !question.trim() &&
+ queuedMessages.length > 0 &&
+ !editingQueueId
+ ) {
+ const target = event.currentTarget
+ const atStart = target.selectionStart === 0 && target.selectionEnd === 0
+ const isSingleLine = target.value.split('\n').length <= 1
+ if (!atStart || !isSingleLine) {
+ return
+ }
+ const nextMessageId = queuedMessages[0]?.id
+ if (!nextMessageId) {
+ return
+ }
+ event.preventDefault()
+ setEditingQueueId(nextMessageId)
}
- event.preventDefault()
- setEditingQueueId(nextMessageId)
- }
- }}
- disabled={inputDisabled}
- minRows={1}
- maxRows={10}
- className="!border-none !bg-transparent min-h-16 py-2 pl-2 pr-12 resize-none"
- hideFocus
- />
- </div>
- </SlashCommandAutocomplete>
+ }}
+ disabled={inputDisabled}
+ minRows={1}
+ maxRows={10}
+ className={cn(
+ '!border-none !bg-transparent min-h-16 py-2 pl-2 resize-none',
+ handsFreeFlagEnabled ? 'pr-20' : 'pr-12'
+ )}
+ hideFocus
+ />
+ </div>
+ </SlashCommandAutocomplete>
+ )}
- {!isSharedThread && (
- <div className="pb-2 pr-12">
+ {!isSharedThread && !handsFreeActive && (
+ // When the hands-free flag is on, reserve ~80px (pr-20) so the chip
+ // row doesn't wrap under the absolutely-positioned mic + send pair.
+ // Without the flag the row only has send and the legacy pr-12 is
+ // enough — keep it so non-flagged users see the original layout.
+ <div className={cn('pb-2', handsFreeFlagEnabled ? 'pr-20' : 'pr-12')}>
{!isThreadVisible ? (
- <div className="flex items-start justify-between">
+ <div
+ className={cn(
+ 'flex justify-between',
+ handsFreeFlagEnabled ? 'items-end flex-wrap gap-1' : 'items-start'
+ )}
+ >
<ContextDisplay size={contextDisplaySize} />
- <div className="flex items-start gap-1 h-full mt-1 mr-1">{topActions}</div>
+ <div
+ className={cn(
+ 'flex mr-1',
+ handsFreeFlagEnabled
+ ? 'items-end gap-1'
+ : 'items-start gap-1 h-full mt-1'
+ )}
+ >
+ {topActions}
+ </div>
</div>
) : (
<ContextDisplay size={contextDisplaySize} />
@@ -354,78 +392,82 @@ export const QuestionInput = React.forwardRef<HTMLDivElement, QuestionInputProps
)}
</label>
<div
- className={clsx(
+ className={cn(
'absolute flex items-center',
+ handsFreeFlagEnabled && 'gap-1',
isSharedThread && 'hidden',
isThreadVisible ? 'bottom-[9px] right-[9px]' : 'bottom-[7px] right-[7px]'
)}
>
- <AIConsentPopoverWrapper
- placement="bottom-end"
- showArrow
- ignoreDismissal
- onApprove={() => askMax(pendingPrompt || question)}
- onDismiss={() => completeThreadGeneration()}
- middleware={[
- offset((state) => ({
- mainAxis: state.placement.includes('top') ? 30 : 1,
- })),
- ]}
- hidden={!isAdmin || (!threadLoading && !pendingPrompt)}
- >
- <LemonButton
- data-attr={showStopButton ? 'max-stop-generation' : 'max-send-message'}
- type={(isThreadVisible && !hasQuestion) || showStopButton ? 'secondary' : 'primary'}
- onClick={() => {
- if (threadLoading) {
- if (isQueueingSubmission) {
- if (submissionDisabledReason) {
- textAreaRef?.current?.focus()
+ <HandsFreeButton tabId={maxTabId} />
+ {!handsFreeActive && (
+ <AIConsentPopoverWrapper
+ placement="bottom-end"
+ showArrow
+ ignoreDismissal
+ onApprove={() => askMax(pendingPrompt || question)}
+ onDismiss={() => completeThreadGeneration()}
+ middleware={[
+ offset((state) => ({
+ mainAxis: state.placement.includes('top') ? 30 : 1,
+ })),
+ ]}
+ hidden={!isAdmin || (!threadLoading && !pendingPrompt)}
+ >
+ <LemonButton
+ data-attr={showStopButton ? 'max-stop-generation' : 'max-send-message'}
+ type={(isThreadVisible && !hasQuestion) || showStopButton ? 'secondary' : 'primary'}
+ onClick={() => {
+ if (threadLoading) {
+ if (isQueueingSubmission) {
+ if (submissionDisabledReason) {
+ textAreaRef?.current?.focus()
+ return
+ }
+ askMax(question)
return
}
- askMax(question)
+ stopGeneration()
return
}
- stopGeneration()
- return
+ if (submissionDisabledReason) {
+ textAreaRef?.current?.focus()
+ return
+ }
+ askMax(question)
+ }}
+ tooltip={
+ disabledReason ? (
+ disabledReason
+ ) : showStopButton ? (
+ <>
+ Let's bail <KeyboardShortcut enter />
+ </>
+ ) : isQueueingSubmission ? (
+ <>
+ Queue message <KeyboardShortcut enter />
+ </>
+ ) : (
+ <>
+ Let's go! <KeyboardShortcut enter />
+ </>
+ )
}
- if (submissionDisabledReason) {
- textAreaRef?.current?.focus()
- return
+ loading={threadLoading && !dataProcessingAccepted}
+ disabledReason={disabledReason}
+ className={disabledReason ? 'opacity-[0.5]' : ''}
+ size="small"
+ icon={
+ showStopButton ? (
+ <IconStopFilled />
+ ) : (
+ MAX_SLASH_COMMANDS.find((cmd) => cmd.name === question.split(' ', 1)[0])
+ ?.icon || <IconArrowRight />
+ )
}
- askMax(question)
- }}
- tooltip={
- disabledReason ? (
- disabledReason
- ) : showStopButton ? (
- <>
- Let's bail <KeyboardShortcut enter />
- </>
- ) : isQueueingSubmission ? (
- <>
- Queue message <KeyboardShortcut enter />
- </>
- ) : (
- <>
- Let's go! <KeyboardShortcut enter />
- </>
- )
- }
- loading={threadLoading && !dataProcessingAccepted}
- disabledReason={disabledReason}
- className={disabledReason ? 'opacity-[0.5]' : ''}
- size="small"
- icon={
- showStopButton ? (
- <IconStopFilled />
- ) : (
- MAX_SLASH_COMMANDS.find((cmd) => cmd.name === question.split(' ', 1)[0])
- ?.icon || <IconArrowRight />
- )
- }
- />
- </AIConsentPopoverWrapper>
+ />
+ </AIConsentPopoverWrapper>
+ )}
</div>
</div>
{/* Info banner for conversations created during impersonation (marked as internal) */}
diff --git a/frontend/src/scenes/max/components/SidebarQuestionInput.tsx b/frontend/src/scenes/max/components/SidebarQuestionInput.tsx
index ae07c5d2efe7..dcd83460aa94 100644
--- a/frontend/src/scenes/max/components/SidebarQuestionInput.tsx
+++ b/frontend/src/scenes/max/components/SidebarQuestionInput.tsx
@@ -75,7 +75,7 @@ export function SidebarQuestionInput({
<QuestionInput
isSticky={isSticky}
textAreaRef={textAreaRef}
- containerClassName={cn('w-full px-3 mx-auto self-center backdrop-blur-sm z-50', sidePanel && 'px-0')}
+ containerClassName={cn('mx-auto self-center backdrop-blur-sm z-50', sidePanel && 'px-0')}
isThreadVisible={threadVisible}
>
<SuggestionsList />
diff --git a/frontend/src/scenes/max/handsFreeLogic.test.ts b/frontend/src/scenes/max/handsFreeLogic.test.ts
new file mode 100644
index 000000000000..638bae7964e4
--- /dev/null
+++ b/frontend/src/scenes/max/handsFreeLogic.test.ts
@@ -0,0 +1,110 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { classifyPartial, handsFreeLogic, type SuppressionReason } from './handsFreeLogic'
+import { maxLogic } from './maxLogic'
+import { maxMocks } from './testUtils'
+
+describe('handsFreeLogic state machine', () => {
+ let logic: ReturnType<typeof handsFreeLogic.build>
+ let parent: ReturnType<typeof maxLogic.build>
+
+ beforeEach(() => {
+ useMocks(maxMocks)
+ initKeaTests()
+ parent = maxLogic({ tabId: 'hands-free-test-tab' })
+ parent.mount()
+ logic = handsFreeLogic({ tabId: 'hands-free-test-tab' })
+ logic.mount()
+ })
+
+ afterEach(() => {
+ logic.unmount()
+ parent.unmount()
+ })
+
+ it('starts off with idle connection and exposes isActive=false', async () => {
+ await expectLogic(logic).toMatchValues({ status: 'off', connection: 'idle', isActive: false })
+ })
+
+ it('exitHandsFree from off keeps status off (defensive)', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.exitHandsFree('test')
+ }).toMatchValues({ status: 'off', connection: 'idle' })
+ })
+
+ it('setStatus("listening") then exitHandsFree resets back to off + connection idle', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.setStatus('listening')
+ logic.actions.setConnection('connected')
+ }).toMatchValues({ status: 'listening', connection: 'connected', isActive: true })
+
+ await expectLogic(logic, () => {
+ logic.actions.exitHandsFree('test')
+ }).toMatchValues({ status: 'off', connection: 'idle', isActive: false })
+ })
+
+ it('setError stores the error string; entering hands-free clears it', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.setError('something broke')
+ }).toMatchValues({ error: 'something broke' })
+
+ await expectLogic(logic, () => {
+ logic.actions.setError(null)
+ }).toMatchValues({ error: null })
+ })
+
+ it('setPartialTranscript stores the in-flight transcript; commitTranscript clears it', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.setPartialTranscript('how is the prod')
+ }).toMatchValues({ partialTranscript: 'how is the prod' })
+
+ // commit triggers askMax via the listener; thread isn't mounted here so it'll error out,
+ // but the reducer for partialTranscript clears regardless
+ await expectLogic(logic, () => {
+ logic.actions.commitTranscript('how is the product doing today')
+ }).toMatchValues({ partialTranscript: '' })
+ })
+
+ it('exitHandsFree clears any in-flight partial transcript', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.setPartialTranscript('mid sentence')
+ }).toMatchValues({ partialTranscript: 'mid sentence' })
+
+ await expectLogic(logic, () => {
+ logic.actions.exitHandsFree('test')
+ }).toMatchValues({ partialTranscript: '' })
+ })
+
+ it('speakAssistantResponse with empty summary while off is a no-op', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.speakAssistantResponse({ text: '', vizCount: 0 })
+ }).toMatchValues({ status: 'off' })
+ })
+})
+
+describe('classifyPartial barge-in heuristic', () => {
+ const spoken = 'lets look at the daily active users for the last week'
+ const cases: { name: string; spoken: string; partial: string; expected: SuppressionReason }[] = [
+ { name: 'empty partial -> too_short', spoken, partial: '', expected: 'too_short' },
+ { name: '1-char partial -> too_short', spoken, partial: 'a', expected: 'too_short' },
+ { name: '3-char unrelated -> too_short', spoken, partial: 'xyz', expected: 'too_short' },
+ { name: 'allowlist "stop" bypasses too_short', spoken, partial: 'stop', expected: null },
+ { name: 'allowlist "no" bypasses too_short', spoken, partial: 'no', expected: null },
+ { name: 'allowlist "wait" bypasses too_short', spoken, partial: 'wait', expected: null },
+ { name: 'allowlist "max" bypasses too_short', spoken, partial: 'max', expected: null },
+ { name: 'substring of spoken text -> substring', spoken, partial: 'daily active', expected: 'substring' },
+ { name: 'unrelated longer phrase -> not suppressed', spoken, partial: 'change the chart type', expected: null },
+ {
+ name: 'partial matches mid-word run in spoken -> substring',
+ spoken,
+ partial: 'active users',
+ expected: 'substring',
+ },
+ ]
+ it.each(cases)('$name', ({ spoken, partial, expected }) => {
+ expect(classifyPartial(spoken, partial)).toBe(expected)
+ })
+})
diff --git a/frontend/src/scenes/max/handsFreeLogic.ts b/frontend/src/scenes/max/handsFreeLogic.ts
new file mode 100644
index 000000000000..875ab9a2ea63
--- /dev/null
+++ b/frontend/src/scenes/max/handsFreeLogic.ts
@@ -0,0 +1,592 @@
+// Hands-free conversational mode for Max — STT via ElevenLabs Scribe (browser → WS direct),
+// TTS via the browser's native speechSynthesis. Built for mobile-web use (gym, walking).
+import { actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
+import posthog from 'posthog-js'
+
+import api from 'lib/api'
+import { FEATURE_FLAGS } from 'lib/constants'
+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
+
+import type { handsFreeLogicType } from './handsFreeLogicType'
+import { AssistantSummary, buildSpokenText } from './handsFreeUtils'
+import { maxLogic } from './maxLogic'
+import { maxThreadLogic } from './maxThreadLogic'
+
+export type HandsFreeStatus = 'off' | 'starting' | 'listening' | 'thinking' | 'speaking'
+export type HandsFreeConnection = 'idle' | 'connecting' | 'connected' | 'reconnecting' | 'closed'
+
+const SCRIBE_REALTIME_MODEL_ID = 'scribe_v2_realtime'
+
+export interface HandsFreeLogicProps {
+ tabId: string
+}
+
+interface ScribeConnection {
+ close: () => void
+ on: (event: string, listener: (payload: any) => void) => void
+ off: (event: string, listener: (payload: any) => void) => void
+}
+
+interface ScribeNamespace {
+ connect: (options: {
+ token: string
+ modelId: string
+ commitStrategy?: string
+ microphone?: { echoCancellation?: boolean; noiseSuppression?: boolean }
+ }) => ScribeConnection
+}
+
+type RealtimeEventsEnum = Record<string, string>
+type CommitStrategyEnum = { MANUAL: string; VAD: string }
+
+let cachedSdkPromise: Promise<{
+ Scribe: ScribeNamespace
+ RealtimeEvents: RealtimeEventsEnum
+ CommitStrategy: CommitStrategyEnum
+}> | null = null
+
+async function loadScribeSdk(): Promise<{
+ Scribe: ScribeNamespace
+ RealtimeEvents: RealtimeEventsEnum
+ CommitStrategy: CommitStrategyEnum
+}> {
+ if (!cachedSdkPromise) {
+ cachedSdkPromise = import(/* webpackChunkName: "elevenlabs-client" */ '@elevenlabs/client')
+ .then((mod) => ({
+ Scribe: (mod as unknown as { Scribe: ScribeNamespace }).Scribe,
+ RealtimeEvents: (mod as unknown as { RealtimeEvents: RealtimeEventsEnum }).RealtimeEvents,
+ CommitStrategy: (mod as unknown as { CommitStrategy: CommitStrategyEnum }).CommitStrategy,
+ }))
+ .catch((err) => {
+ cachedSdkPromise = null
+ throw err
+ })
+ }
+ return cachedSdkPromise
+}
+
+export const handsFreeLogic = kea<handsFreeLogicType>([
+ props({} as HandsFreeLogicProps),
+ key((props) => props.tabId),
+ path((key) => ['scenes', 'max', 'handsFreeLogic', key]),
+
+ connect(({ tabId }: HandsFreeLogicProps) => ({
+ values: [maxLogic({ tabId }), ['threadLogicKey']],
+ })),
+
+ actions({
+ enterHandsFree: true,
+ exitHandsFree: (reason?: string) => ({ reason: reason ?? 'user' }),
+ toggleHandsFree: true,
+ setStatus: (status: HandsFreeStatus) => ({ status }),
+ setConnection: (connection: HandsFreeConnection) => ({ connection }),
+ setError: (error: string | null) => ({ error }),
+ setPartialTranscript: (text: string) => ({ text }),
+ commitTranscript: (text: string) => ({ text }),
+ speakAssistantResponse: (summary: AssistantSummary) => ({ summary }),
+ cancelSpeaking: true,
+ // Stop TTS playback and go back to listening — fired only when Scribe transcribes
+ // genuine user speech over Max's TTS (voice barge-in). The mic button no longer
+ // tap-interrupts; tapping it always exits hands-free entirely.
+ interruptSpeaking: true,
+ setSdkAvailable: (available: boolean) => ({ available }),
+ }),
+
+ reducers({
+ status: [
+ 'off' as HandsFreeStatus,
+ {
+ setStatus: (_, { status }) => status,
+ },
+ ],
+ connection: [
+ 'idle' as HandsFreeConnection,
+ {
+ setConnection: (_, { connection }) => connection,
+ exitHandsFree: () => 'idle' as HandsFreeConnection,
+ },
+ ],
+ error: [
+ null as string | null,
+ {
+ setError: (_, { error }) => error,
+ enterHandsFree: () => null,
+ exitHandsFree: () => null,
+ },
+ ],
+ partialTranscript: [
+ '' as string,
+ {
+ setPartialTranscript: (_, { text }) => text,
+ commitTranscript: () => '',
+ exitHandsFree: () => '',
+ },
+ ],
+ // null = probe not yet finished, true = Scribe present, false = SDK missing or load failed
+ sdkAvailable: [
+ null as boolean | null,
+ {
+ setSdkAvailable: (_, { available }) => available,
+ },
+ ],
+ }),
+
+ selectors({
+ isActive: [(s) => [s.status], (status) => status !== 'off'],
+ // Render the mic button when the probe hasn't completed yet (null) or has succeeded (true).
+ // Hide it only once we've confirmed the SDK can't be used.
+ canUseHandsFree: [(s) => [s.sdkAvailable], (sdkAvailable) => sdkAvailable !== false],
+ }),
+
+ listeners(({ actions, values, cache, props }) => ({
+ toggleHandsFree: () => {
+ if (values.status === 'off') {
+ actions.enterHandsFree()
+ } else {
+ actions.exitHandsFree()
+ }
+ },
+
+ enterHandsFree: async () => {
+ if (values.status !== 'off') {
+ return
+ }
+ actions.setStatus('starting')
+ actions.setConnection('connecting')
+
+ // Per-session analytics counters, captured at exit so a single PostHog query can
+ // build session-length and turn-count distributions without re-aggregating events.
+ cache.sessionStartedAt = Date.now()
+ cache.userTurnCount = 0
+ cache.assistantTurnCount = 0
+ cache.interruptedCount = 0
+
+ // iOS Safari requires the first audio playback (HTMLAudioElement.play()) to be
+ // initiated from a user-gesture handler. The mic-button click IS that gesture, so
+ // we prime a silent Audio element here. Subsequent .play() calls (driven by Max
+ // responses arriving over the network) then succeed without re-prompting.
+ try {
+ const primer = new Audio()
+ primer.muted = true
+ primer.src =
+ 'data:audio/mpeg;base64,SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU4LjI5LjEwMAAAAAAAAAAAAAAA//tQxAADB8AhSmxhhgAAAAAAAAAAAExBTUUzLjEwMACqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq//tSxAADB8AhSmxhhgAAAAAAAAAAAExBTUUzLjEwMKqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq'
+ void primer.play().catch(() => undefined)
+ } catch {
+ // best-effort
+ }
+
+ let sdk
+ try {
+ sdk = await loadScribeSdk()
+ } catch (err) {
+ posthog.captureException(err)
+ actions.setError('Could not load the hands-free SDK.')
+ lemonToast.error('Hands-free could not load. Please try again.')
+ actions.exitHandsFree('sdk_load_failed')
+ return
+ }
+ if (values.status === 'off') {
+ return
+ }
+
+ const { Scribe, RealtimeEvents, CommitStrategy } = sdk
+ if (!Scribe || typeof Scribe.connect !== 'function') {
+ // Should be unreachable — the mount-time probe should have already hidden the
+ // button when the SDK doesn't expose Scribe. If we get here, capture so we can
+ // see it in PostHog and bail without confusing the user.
+ posthog.captureException(new Error('handsFree: Scribe namespace missing from @elevenlabs/client'))
+ actions.setSdkAvailable(false)
+ actions.exitHandsFree('sdk_missing_scribe')
+ return
+ }
+
+ cache.reconnectAttempts = 0
+
+ // Mints a fresh token, opens a Scribe connection, and wires event handlers.
+ // Called for the initial session and on reconnect after an unexpected close.
+ // Returns true on success, false when the caller should give up.
+ const establishConnection = async (isReconnect: boolean): Promise<boolean> => {
+ let token: string
+ try {
+ const response = await api.maxHandsFree.token()
+ token = response.token
+ } catch (err) {
+ posthog.captureException(err)
+ if (!isReconnect) {
+ actions.setError('Could not start hands-free session.')
+ lemonToast.error('Hands-free could not start. Please try again.')
+ }
+ actions.exitHandsFree(isReconnect ? 'reconnect_token_failed' : 'token_failed')
+ return false
+ }
+ if (values.status === 'off') {
+ return false
+ }
+ let connection: ScribeConnection
+ try {
+ connection = Scribe.connect({
+ token,
+ modelId: SCRIBE_REALTIME_MODEL_ID,
+ // SDK defaults to MANUAL — we need VAD so silence on the mic auto-commits
+ // the speech segment and triggers askMax. Without this it isn't hands-free.
+ commitStrategy: CommitStrategy?.VAD ?? 'vad',
+ microphone: { echoCancellation: true, noiseSuppression: true },
+ })
+ } catch (err) {
+ posthog.captureException(err)
+ const message = (err as Error)?.message ?? ''
+ const isPermissionError =
+ /permission|notallowed|denied/i.test(message) ||
+ (err as { name?: string })?.name === 'NotAllowedError'
+ if (isPermissionError) {
+ actions.setError('Microphone access was denied.')
+ lemonToast.error('Microphone access was denied. Hands-free disabled.')
+ actions.exitHandsFree('mic_permission_denied')
+ } else if (isReconnect) {
+ actions.exitHandsFree('reconnect_failed')
+ } else {
+ actions.setError(`Hands-free connection failed: ${message || 'unknown error'}`)
+ lemonToast.error('Hands-free failed to start. See console for details.')
+ actions.exitHandsFree('connection_failed')
+ }
+ return false
+ }
+ cache.connection = connection
+ wireConnectionEvents(connection)
+ return true
+ }
+
+ const onOpen = (): void => actions.setConnection('connected')
+ const onSessionStarted = (): void => {
+ const wasStarting = values.status === 'starting'
+ const wasReconnecting = cache.isReconnecting === true
+ cache.isReconnecting = false
+ if (wasStarting || wasReconnecting) {
+ actions.setStatus('listening')
+ }
+ if (wasStarting) {
+ posthog.capture('max hands-free entered')
+ }
+ if (wasReconnecting) {
+ posthog.capture('max hands-free reconnected', {
+ attempts: cache.reconnectAttempts ?? 1,
+ })
+ }
+ }
+ // While 'speaking', the Scribe mic stream stays open (we can't cheaply pause it)
+ // and the speakers' TTS audio bleeds back into the mic — Scribe transcribes its
+ // own voice. To support real barge-in (user talks over Max -> Max shuts up and
+ // listens) without self-triggering, we compare incoming partials to what Max is
+ // currently saying. If the partial is a substring of the spoken text, it's
+ // self-transcription bleed; otherwise the user is talking over Max.
+ const onPartial = (payload: any): void => {
+ const text: string = payload?.text ?? payload?.transcript ?? ''
+ if (values.status === 'speaking') {
+ const partialLower = normaliseForBargeInMatch(text)
+ const spokenLower: string = cache.spokenTextLower ?? ''
+ const reason = classifyPartial(spokenLower, partialLower)
+ if (reason) {
+ posthog.capture('max hands-free partial suppressed', {
+ reason,
+ partial_chars: partialLower.length,
+ spoken_chars: spokenLower.length,
+ phase: 'partial',
+ })
+ return
+ }
+ // User is talking over Max — barge in: stop TTS, flip to listening, then
+ // let the partial-transcript reducer below pick up the user's words.
+ actions.interruptSpeaking()
+ }
+ if (values.status === 'listening') {
+ actions.setPartialTranscript(text)
+ }
+ }
+ const onCommitted = (payload: any): void => {
+ const text: string = (payload?.text ?? payload?.transcript ?? '').trim()
+ if (!text) {
+ return
+ }
+ if (values.status === 'speaking') {
+ const committedLower = normaliseForBargeInMatch(text)
+ const spokenLower: string = cache.spokenTextLower ?? ''
+ const reason = classifyPartial(spokenLower, committedLower)
+ if (reason) {
+ posthog.capture('max hands-free partial suppressed', {
+ reason,
+ partial_chars: committedLower.length,
+ spoken_chars: spokenLower.length,
+ phase: 'committed',
+ })
+ return
+ }
+ actions.interruptSpeaking()
+ }
+ if (values.status !== 'listening') {
+ return
+ }
+ actions.commitTranscript(text)
+ }
+ const onClose = (): void => {
+ if (values.status === 'off') {
+ return
+ }
+ // Single retry-with-fresh-token on unexpected close — mobile network blips
+ // (the exact gym/walking use case this targets) should not tear down the
+ // session. Beyond one attempt we give up to avoid a reconnect storm.
+ const attempts = (cache.reconnectAttempts ?? 0) + 1
+ cache.reconnectAttempts = attempts
+ if (attempts > 1) {
+ actions.setConnection('closed')
+ actions.exitHandsFree('connection_closed')
+ return
+ }
+ actions.setConnection('reconnecting')
+ cache.isReconnecting = true
+ posthog.capture('max hands-free reconnect attempted', { attempt: attempts })
+ cache.connection = undefined
+ void establishConnection(true)
+ }
+ const onError = (payload: any): void => {
+ posthog.captureException(new Error(`scribe error: ${JSON.stringify(payload ?? {})}`))
+ actions.setError('Hands-free connection error.')
+ actions.exitHandsFree('scribe_error')
+ }
+
+ const wireConnectionEvents = (connection: ScribeConnection): void => {
+ connection.on(RealtimeEvents.OPEN, onOpen)
+ connection.on(RealtimeEvents.SESSION_STARTED, onSessionStarted)
+ connection.on(RealtimeEvents.PARTIAL_TRANSCRIPT, onPartial)
+ connection.on(RealtimeEvents.COMMITTED_TRANSCRIPT, onCommitted)
+ connection.on(RealtimeEvents.CLOSE, onClose)
+ connection.on(RealtimeEvents.ERROR, onError)
+ }
+
+ await establishConnection(false)
+ },
+
+ exitHandsFree: ({ reason }) => {
+ posthog.capture('max hands-free exited', {
+ reason,
+ duration_ms: cache.sessionStartedAt ? Date.now() - cache.sessionStartedAt : null,
+ user_turns: cache.userTurnCount ?? 0,
+ assistant_turns: cache.assistantTurnCount ?? 0,
+ interruptions: cache.interruptedCount ?? 0,
+ })
+ actions.cancelSpeaking()
+ const connection = cache.connection as ScribeConnection | undefined
+ if (connection) {
+ try {
+ connection.close()
+ } catch {
+ // best-effort
+ }
+ cache.connection = undefined
+ }
+ actions.setStatus('off')
+ },
+
+ commitTranscript: ({ text }) => {
+ const threadKey = values.threadLogicKey
+ const threadLogic = threadKey
+ ? maxThreadLogic.findMounted({ tabId: props.tabId, conversationId: threadKey })
+ : null
+ if (!threadLogic) {
+ actions.setError('Could not find an active conversation to send the message to.')
+ actions.exitHandsFree('no_thread')
+ return
+ }
+ actions.setStatus('thinking')
+ cache.userTurnCount = (cache.userTurnCount ?? 0) + 1
+ posthog.capture('max hands-free user spoke', { transcript_chars: text.length })
+ threadLogic.actions.askMax(text)
+ },
+
+ speakAssistantResponse: async ({ summary }) => {
+ if (values.status === 'off') {
+ return
+ }
+ // guard re-entry — if two assistant turns complete back-to-back, tear down the
+ // previous playback (revokes blob URL, aborts inflight request) before queuing a
+ // new one. Otherwise the previous audio leaks and overlaps.
+ teardownSpeaking(cache)
+
+ const spokenText = buildSpokenText(summary)
+ if (!spokenText) {
+ actions.setStatus('listening')
+ return
+ }
+ actions.setStatus('speaking')
+
+ // Store what we're saying so onPartial can match incoming transcripts against it
+ // and tell self-transcription bleed apart from real user barge-in.
+ cache.spokenTextLower = normaliseForBargeInMatch(spokenText)
+
+ const controller = new AbortController()
+ cache.speakAbortController = controller
+
+ try {
+ const response = await api.maxHandsFree.synthesize(spokenText, { signal: controller.signal })
+ if (!response.ok) {
+ throw new Error(`TTS returned ${response.status}`)
+ }
+ const audioBlob = await response.blob()
+ if (controller.signal.aborted || values.status !== 'speaking') {
+ return
+ }
+ const url = URL.createObjectURL(audioBlob)
+ const audioElement = new Audio(url)
+ cache.speakAudioElement = audioElement
+ cache.speakAudioUrl = url
+
+ const finished = new Promise<void>((resolve, reject) => {
+ audioElement.onended = () => resolve()
+ audioElement.onerror = () => reject(new Error('audio playback failed'))
+ })
+
+ await audioElement.play()
+ // Capture once playback has actually started — distinguishes "Max attempted to
+ // speak" from "Max successfully voiced a response." Counted per session via
+ // assistant_turns on the exit event.
+ cache.assistantTurnCount = (cache.assistantTurnCount ?? 0) + 1
+ posthog.capture('max hands-free assistant spoke', {
+ text_chars: spokenText.length,
+ viz_count: summary.vizCount,
+ })
+ await finished
+ } catch (err) {
+ if ((err as { name?: string }).name !== 'AbortError') {
+ posthog.captureException(err)
+ }
+ } finally {
+ if (cache.speakAbortController === controller) {
+ teardownSpeaking(cache)
+ if (values.status === 'speaking') {
+ actions.setStatus('listening')
+ }
+ }
+ }
+ },
+
+ cancelSpeaking: () => {
+ teardownSpeaking(cache)
+ },
+
+ interruptSpeaking: () => {
+ // Only meaningful while we're actually playing audio — guard against stray
+ // fires from elsewhere accidentally jolting us through state transitions.
+ if (values.status !== 'speaking') {
+ return
+ }
+ teardownSpeaking(cache)
+ actions.setStatus('listening')
+ cache.interruptedCount = (cache.interruptedCount ?? 0) + 1
+ posthog.capture('max hands-free tts interrupted')
+ },
+ })),
+
+ afterMount(({ actions, cache }) => {
+ // Probe the SDK once on mount so the mic button can decide whether to render itself.
+ // We capture failures to PostHog instead of surfacing dev-facing errors to end users.
+ // Skip the probe entirely when the feature flag is off — the logic mounts for every
+ // Max session, so an unconditional dynamic import would download the SDK chunk for
+ // every user even though only flagged users can ever use it.
+ if (!posthog.isFeatureEnabled(FEATURE_FLAGS.MAX_HANDS_FREE)) {
+ actions.setSdkAvailable(false)
+ return
+ }
+ cache.disposables.add(() => {
+ let cancelled = false
+ void loadScribeSdk()
+ .then((sdk) => {
+ if (cancelled) {
+ return
+ }
+ const available = !!sdk?.Scribe && typeof sdk.Scribe.connect === 'function'
+ if (!available) {
+ posthog.captureException(
+ new Error('handsFree: @elevenlabs/client loaded but Scribe is missing')
+ )
+ }
+ actions.setSdkAvailable(available)
+ })
+ .catch((err) => {
+ if (cancelled) {
+ return
+ }
+ posthog.captureException(err)
+ actions.setSdkAvailable(false)
+ })
+ return () => {
+ cancelled = true
+ }
+ }, 'sdkProbe')
+ }),
+])
+
+// Normalises a string to lowercase letters and digits separated by single spaces. Used to
+// match an incoming Scribe partial against the TTS content for self-transcription detection.
+function normaliseForBargeInMatch(text: string): string {
+ return text
+ .toLowerCase()
+ .replace(/[^a-z0-9]+/g, ' ')
+ .trim()
+}
+
+// Short words a gym/walking user is likely to shout to stop Max mid-sentence. These
+// always trigger barge-in even though they'd otherwise fall under the too-short or
+// substring suppression — a wrong barge-in is recoverable (user just keeps talking),
+// a swallowed "stop" is not.
+const BARGE_IN_TRIGGER_WORDS: ReadonlySet<string> = new Set(['stop', 'wait', 'cancel', 'no', 'max', 'shh', 'hey'])
+
+export type SuppressionReason = 'too_short' | 'substring' | null
+
+// Returns null when the partial should be treated as real user speech (barge in), or a
+// reason string when it should be suppressed as TTS bleed. Callers should emit the
+// reason to PostHog so we can measure misfire rate before tuning the heuristic.
+export function classifyPartial(spokenLower: string, partialLower: string): SuppressionReason {
+ if (!partialLower) {
+ return 'too_short'
+ }
+ if (BARGE_IN_TRIGGER_WORDS.has(partialLower)) {
+ return null
+ }
+ if (partialLower.length < 4) {
+ return 'too_short'
+ }
+ if (spokenLower.includes(partialLower)) {
+ return 'substring'
+ }
+ return null
+}
+
+function teardownSpeaking(cache: Record<string, any>): void {
+ const controller = cache.speakAbortController as AbortController | undefined
+ if (controller) {
+ try {
+ controller.abort()
+ } catch {
+ // best-effort
+ }
+ cache.speakAbortController = undefined
+ }
+ const audio = cache.speakAudioElement as HTMLAudioElement | undefined
+ if (audio) {
+ try {
+ audio.pause()
+ audio.src = ''
+ } catch {
+ // best-effort
+ }
+ cache.speakAudioElement = undefined
+ }
+ if (cache.speakAudioUrl) {
+ try {
+ URL.revokeObjectURL(cache.speakAudioUrl)
+ } catch {
+ // best-effort
+ }
+ cache.speakAudioUrl = undefined
+ }
+ cache.spokenTextLower = undefined
+}
diff --git a/frontend/src/scenes/max/handsFreeUtils.test.ts b/frontend/src/scenes/max/handsFreeUtils.test.ts
new file mode 100644
index 000000000000..995c028b302f
--- /dev/null
+++ b/frontend/src/scenes/max/handsFreeUtils.test.ts
@@ -0,0 +1,120 @@
+import { AssistantMessageType } from '~/queries/schema/schema-assistant-messages'
+
+import { buildSpokenText, stripMarkdown, summariseAssistantThread } from './handsFreeUtils'
+
+type StubMessage = Parameters<typeof summariseAssistantThread>[0][number]
+
+const human = (content = 'hi'): StubMessage => ({ type: AssistantMessageType.Human, content })
+const ai = (content: string): StubMessage => ({ type: AssistantMessageType.Assistant, content })
+const viz = (): StubMessage => ({ type: AssistantMessageType.Visualization })
+const multiViz = (): StubMessage => ({ type: AssistantMessageType.MultiVisualization })
+const artifact = (): StubMessage => ({ type: AssistantMessageType.Artifact })
+
+describe('handsFreeUtils', () => {
+ describe('summariseAssistantThread()', () => {
+ it('returns the assistant prose message and no viz suffix when none present', () => {
+ const result = summariseAssistantThread([human(), ai('here is the answer')])
+ expect(result).toEqual({ text: 'here is the answer', vizCount: 0 })
+ })
+
+ it.each([
+ ['Visualization', [ai('see chart'), viz()], 1],
+ ['MultiVisualization', [ai('see charts'), multiViz()], 1],
+ ['Artifact', [ai('see notebook'), artifact()], 1],
+ ['mix of viz types', [ai('see charts'), viz(), multiViz(), artifact()], 3],
+ ] as const)('counts %s blocks since the last human message', (_label, tail, expectedCount) => {
+ const result = summariseAssistantThread([human(), ...tail])
+ expect(result.vizCount).toBe(expectedCount)
+ })
+
+ it('only counts messages produced after the most recent human message', () => {
+ const result = summariseAssistantThread([
+ human('first ask'),
+ ai('old answer'),
+ viz(),
+ human('second ask'),
+ ai('new answer'),
+ viz(),
+ ])
+ expect(result).toEqual({ text: 'new answer', vizCount: 1 })
+ })
+
+ it('returns the LATEST prose message when the assistant emits several in one turn', () => {
+ const result = summariseAssistantThread([
+ human(),
+ ai('first part of the answer'),
+ ai('second part of the answer'),
+ ai('final summary'),
+ ])
+ expect(result).toEqual({ text: 'final summary', vizCount: 0 })
+ })
+
+ it('returns empty text when only viz messages followed the human turn', () => {
+ const result = summariseAssistantThread([human(), viz(), multiViz()])
+ expect(result).toEqual({ text: '', vizCount: 2 })
+ })
+
+ it('returns empty text and zero count when thread has no assistant turn', () => {
+ const result = summariseAssistantThread([human()])
+ expect(result).toEqual({ text: '', vizCount: 0 })
+ })
+
+ it('handles a thread that never had a human turn (e.g. agent-initiated)', () => {
+ const result = summariseAssistantThread([ai('hello there'), viz()])
+ expect(result).toEqual({ text: 'hello there', vizCount: 1 })
+ })
+ })
+
+ describe('buildSpokenText()', () => {
+ it.each([
+ [{ text: 'just words', vizCount: 0 }, 'just words'],
+ [{ text: 'words', vizCount: 1 }, "words I've added 1 chart to the chat."],
+ [{ text: 'words', vizCount: 3 }, "words I've added 3 charts to the chat."],
+ [{ text: '', vizCount: 2 }, "I've added 2 charts to the chat."],
+ [{ text: '', vizCount: 0 }, ''],
+ ])('builds %p -> %p', (summary, expected) => {
+ expect(buildSpokenText(summary)).toBe(expected)
+ })
+ })
+
+ describe('stripMarkdown()', () => {
+ it.each([
+ ['# Heading', 'Heading'],
+ ['**bold text**', 'bold text'],
+ ['_italic_', 'italic'],
+ ['*emphasised*', 'emphasised'],
+ ['~~struck~~', 'struck'],
+ ['`inline code`', 'inline code'],
+ ['1. first item\n2. second item', 'first item second item'],
+ ['- bullet\n- bullet', 'bullet bullet'],
+ ['[link text](https://example.com)', 'link text'],
+ ['![alt](https://example.com/x.png) caption', ' caption'],
+ ['> quoted line', 'quoted line'],
+ ])('strips %p -> %p', (input, expected) => {
+ expect(stripMarkdown(input)).toBe(expected)
+ })
+
+ it('leaves identifiers like foo_bar_baz alone (bold underscore guard)', () => {
+ expect(stripMarkdown('the var is foo_bar_baz here')).toBe('the var is foo_bar_baz here')
+ })
+
+ it('omits fenced code blocks rather than reading them verbatim', () => {
+ const input = 'before\n```ts\nconst x = 1\n```\nafter'
+ expect(stripMarkdown(input)).toContain('code block omitted')
+ expect(stripMarkdown(input)).not.toContain('const x')
+ })
+
+ it('strips table separator rows and pipes', () => {
+ const input = '| col a | col b |\n| --- | --- |\n| one | two |'
+ const result = stripMarkdown(input)
+ expect(result).not.toContain('---')
+ expect(result).not.toContain('|')
+ expect(result).toContain('col a')
+ expect(result).toContain('one')
+ })
+
+ it('collapses runs of whitespace so TTS does not over-pause', () => {
+ expect(stripMarkdown('one\n\n\ntwo')).toBe('one two')
+ })
+ })
+})
diff --git a/frontend/src/scenes/max/handsFreeUtils.ts b/frontend/src/scenes/max/handsFreeUtils.ts
new file mode 100644
index 000000000000..ba42e32dbf7b
--- /dev/null
+++ b/frontend/src/scenes/max/handsFreeUtils.ts
@@ -0,0 +1,85 @@
+import { AssistantMessageType } from '~/queries/schema/schema-assistant-messages'
+
+const VIZ_TYPES = new Set<AssistantMessageType>([
+ AssistantMessageType.Visualization,
+ AssistantMessageType.MultiVisualization,
+ AssistantMessageType.Artifact,
+])
+
+export interface AssistantSummary {
+ text: string
+ vizCount: number
+}
+
+export function summariseAssistantThread(
+ messages: { type: AssistantMessageType; content?: unknown }[]
+): AssistantSummary {
+ let humanCutoff = -1
+ for (let i = messages.length - 1; i >= 0; i--) {
+ if (messages[i].type === AssistantMessageType.Human) {
+ humanCutoff = i
+ break
+ }
+ }
+
+ let latestProse = ''
+ let vizCount = 0
+ for (let i = humanCutoff + 1; i < messages.length; i++) {
+ const msg = messages[i]
+ if (VIZ_TYPES.has(msg.type)) {
+ vizCount += 1
+ continue
+ }
+ if (msg.type === AssistantMessageType.Assistant && typeof msg.content === 'string' && msg.content) {
+ latestProse = msg.content
+ }
+ }
+ return { text: latestProse, vizCount }
+}
+
+export function buildSpokenText(summary: AssistantSummary): string {
+ const stripped = stripMarkdown(summary.text).trim()
+ if (summary.vizCount === 0) {
+ return stripped
+ }
+ const suffix =
+ summary.vizCount === 1
+ ? "I've added 1 chart to the chat."
+ : `I've added ${summary.vizCount} charts to the chat.`
+ return stripped ? `${stripped} ${suffix}` : suffix
+}
+
+export function stripMarkdown(text: string): string {
+ return (
+ text
+ // fenced code blocks - replaced wholesale; never spoken
+ .replace(/```[\s\S]*?```/g, ' (code block omitted) ')
+ // inline code
+ .replace(/`([^`]+)`/g, '$1')
+ // images
+ .replace(/!\[[^\]]*\]\([^)]+\)/g, '')
+ // links - keep link text
+ .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
+ // tables - drop row-separator lines (| --- |) and pipe characters elsewhere
+ .replace(/^\s*\|?\s*:?-{2,}:?(\s*\|\s*:?-{2,}:?)*\s*\|?\s*$/gm, '')
+ .replace(/\|/g, ' ')
+ // headings
+ .replace(/^#+\s*/gm, '')
+ // bold - **foo** or __foo__ - run twice to safely handle nested markers
+ .replace(/\*\*([^*]+?)\*\*/g, '$1')
+ .replace(/__([^_]+?)__/g, '$1')
+ // italic - *foo* or _foo_ - but only when the marker is at a word boundary so
+ // identifiers like foo_bar_baz aren't mangled
+ .replace(/(^|[^A-Za-z0-9_])[*_]([^*_\n]+?)[*_](?=$|[^A-Za-z0-9_])/g, '$1$2')
+ // strikethrough
+ .replace(/~~([^~]+?)~~/g, '$1')
+ // blockquotes
+ .replace(/^\s*>\s?/gm, '')
+ // bulleted lists
+ .replace(/^\s*[-*+]\s+/gm, '')
+ // ordered lists
+ .replace(/^\s*\d+[.)]\s+/gm, '')
+ // collapse runs of whitespace - TTS reads runs of "\n\n" as awkward pauses
+ .replace(/\s+/g, ' ')
+ )
+}
diff --git a/frontend/src/scenes/max/maxThreadLogic.tsx b/frontend/src/scenes/max/maxThreadLogic.tsx
index 5f1d39af680a..5cc23c2a5151 100644
--- a/frontend/src/scenes/max/maxThreadLogic.tsx
+++ b/frontend/src/scenes/max/maxThreadLogic.tsx
@@ -66,6 +66,8 @@ import {
import { LogEntry, parseLogEvent } from 'products/tasks/frontend/lib/parse-logs'
+import { handsFreeLogic } from './handsFreeLogic'
+import { summariseAssistantThread } from './handsFreeUtils'
import { MODE_DEFINITIONS, ToolRegistration } from './max-constants'
import { MaxBillingContext, MaxBillingContextSubscriptionLevel, maxBillingContextLogic } from './maxBillingContextLogic'
import { maxGlobalLogic } from './maxGlobalLogic'
@@ -1164,6 +1166,11 @@ export const maxThreadLogic = kea<maxThreadLogicType>([
},
completeThreadGeneration: () => {
+ const handsFree = handsFreeLogic.findMounted({ tabId: props.tabId })
+ if (handsFree?.values.isActive) {
+ handsFree.actions.speakAssistantResponse(summariseAssistantThread(values.threadRaw))
+ }
+
// Update the conversation history to include the new conversation
actions.loadConversationHistory({ doNotUpdateCurrentThread: true })
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 1d49b997a399..0a79d75af805 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -559,6 +559,9 @@ importers:
'@dnd-kit/utilities':
specifier: ^3.2.1
version: 3.2.1([email protected])
+ '@elevenlabs/client':
+ specifier: ^1.4.0
+ version: 1.7.0(@types/[email protected])
'@floating-ui/react':
specifier: ^0.27.19
@@ -5193,6 +5196,9 @@ packages:
'@braintree/[email protected]':
resolution: {integrity: sha512-jigsZK+sMF/cuiB7sERuo9V7N9jx+dhmHHnQyDSVdpZwVutaBu7WvNYqMDLSgFgfB30n452TP3vjDAvFC973mA==}
+ '@bufbuild/[email protected]':
+ resolution: {integrity: sha512-wJ8ReQbHxsAfXhrf9ixl0aYbZorRuOWpBNzm8pL8ftmSxQx/wnJD5Eg861NwJU/czy2VXFIebCeZnZrI9rktIQ==}
+
'@bufbuild/[email protected]':
resolution: {integrity: sha512-i5GE2Dk5ekdlK1TR7SugY4LWRrKSfb5T1Qn4unpIMbfxoeGKERKQ59HG3iYewacGD10SR7UzevfPnh6my4tNmQ==}
@@ -5671,6 +5677,12 @@ packages:
peerDependencies:
react: 18.3.1
+ '@elevenlabs/[email protected]':
+ resolution: {integrity: sha512-uLlR2GHM3lBAVZ+0zyACBYQ3hTJ5kcRh3H6fj/EMy8+N6pO43ru7hJhorMQbda19eY2sHwivjD5Vys0O0vR1Og==}
+
+ '@elevenlabs/[email protected]':
+ resolution: {integrity: sha512-4FdEv9YYR1EHl1KJwUFNqJcm9e1zuHi92Ky3wQgCM4UkzCiWNA24SyhLGchbXxv/Hc4w4arqBObQQ6SMj3jsKA==}
+
'@emnapi/[email protected]':
resolution: {integrity: sha512-pVGjBIt1Y6gg3EJN8jTcfpP/+uuRksIo055oE/OBkDNcjZqVbfkWCksG1Jp4yZnj3iKWyWX8fdG/j6UDYPbFog==}
@@ -7349,6 +7361,12 @@ packages:
resolution: {integrity: sha512-pu0K1jCIdnQ12aWNaAVU5bzi7Bd1w54J3ECgANPmYLtQKP0HBj2cE/5coBD66MT10xbtIuUr7tg0Shbsvk0mDA==}
+ '@livekit/[email protected]':
+ resolution: {integrity: sha512-EsshAucklmpuUAfkABPxJNhzj9v2sG7JuzFDL4ML1oJQSV14sqrpTYnsaOudMAw9yOaW53NU3QQTlUQoRs4czw==}
+
+ '@livekit/[email protected]':
+ resolution: {integrity: sha512-Q+l57E7w/xxOBFVWzdX5rkAZO7ffyF+rlDzNUYq2SU114+5aTyCq+PK4unaEVDNd4952Af7wteKr3sOgasGuaA==}
+
resolution: {integrity: sha512-KPDeVScZgA1oq0CiPBcOa3kHIqU+pTOwRFDIhxvmf8CTNvqdZQYp5cCKW0bUk69VygB2PuTiINFWbY78aR2pQw==}
cpu: [arm64]
@@ -12148,6 +12166,9 @@ packages:
resolution: {integrity: sha512-eOIHzCUSH7SMfonMG1LsC2f8vxBFtho6NGBznK41R84YzPuvSBzrhEps33IsQiOW9+VL6NQ9DbjQJznk/S4uRA==}
+ '@types/[email protected]':
+ resolution: {integrity: sha512-mUMZLK3NvwRLcAAT9qmcK+9p7tpU2FHdDsntR3YI4+GY88XrgG4XiE7u1Q2LAN2/FZOz/tdMDC3GQCR4T8nFuw==}
+
resolution: {integrity: sha512-odiGr/9/qMqjcBOe5UhcNLOFHSYmKFOyr+bJ/Xu3Qp4k1pNPAlNLUVNNLcLfjQI7+W7ObX58EdD3H+3p3voOvA==}
@@ -18226,6 +18247,11 @@ packages:
resolution: {integrity: sha512-iyAZCeyD+c1gPyE9qpFu8af0Y+MRtmKOncdGoA2S5EY8iFq99dmmvkNnHiWo+pj0s7yH7l3KPIgee77tKpXPWQ==}
engines: {node: '>=18.0.0'}
+ resolution: {integrity: sha512-aolY1XDAtx0nHKBNm29W9OhzBnSz1CP5kq3phvRhFfi1NbvMXs8tcACjAkZTnIKgihkp+BiJScZZ3tZv0Gz8sA==}
+ peerDependencies:
+ '@types/dom-mediacapture-record': ^1
+
resolution: {integrity: sha512-9bMdFfc80S+vSldBmG3HOuLVHnxRdNTlpzR6QDnzqCQtCzGUEAGTzBKYMeIM+I/sU4oZfgbcbS7X7F65/z/oxQ==}
hasBin: true
@@ -18360,6 +18386,10 @@ packages:
resolution: {integrity: sha512-9ie8ItPR6tjY5uYJh8K/Zrv/RMZ5VOlOWvtZdEHYSTFKZfIBPQa9tOAEeAWhd+AnIneLJ22w5fjOYtoutpWq5w==}
engines: {node: '>=18'}
+ resolution: {integrity: sha512-HgMmCqIJSAKqo68l0rS2AanEWfkxaZ5wNiEFb5ggm08lDs9Xl2KxBlX3PTcaD2chBM1gXAYf491/M2Rv8Jwayg==}
+ engines: {node: '>= 0.6.0'}
+
resolution: {integrity: sha512-BFRuQUqc7x2NWxfJBCyUrN8iYUYznzL9JROmRz1gZ6KlOIgmoD+njPVbb+VNn2nGMKggMsK79iUNErillsrx7w==}
@@ -21641,6 +21671,13 @@ packages:
resolution: {integrity: sha512-49oNpRjWRvnU8NyGVmUaYG4jtTkNonFZI86MmGRDqBphEK2EXT9gdEUoQPZhuBM8yWHxCWbobltqYO5M4XrUvQ==}
+ resolution: {integrity: sha512-KrOH82c/W+GYQ0LHqtr3caRpM3ITglq3ljGUIb8LTki7ByacJZ9z+piSGiwZDsRyhQbYBOBJgr2k6X4BZXi3Kw==}
+ hasBin: true
+
+ resolution: {integrity: sha512-xZocWwfyp4hkbN4hLWxMjmv2Q8aNa9MhmOZ7L9aCZPT+dZsgRr6wZRrSYE3HTdyk/2pZKPSgqI7ns7Een1xMSA==}
+
resolution: {integrity: sha512-AckIIV90rPDcBcglUwXPF3kg0P0qmPsPXAj6BBEENQE1p5yA1xfmDJzfi1Tappj37Pv2mVbKpL3Z1T+Nn7k1Qw==}
@@ -22913,6 +22950,9 @@ packages:
resolution: {integrity: sha512-3KS2b+kL7fsuk/eJZ7EQdnEmQoaho/r6KUef7hxvltNA5DR8NAUM+8wJMbJyZ4G9/7i3v5zPBIMN5aybAh2/Jg==}
engines: {node: '>= 0.4'}
+ resolution: {integrity: sha512-g/KzbYKbH5C2vPkaXGu8DJlHrGKHLsM25Zg9WuC9pMGfuvT+X25tZQWo5fK1BjBm8+UrVE9LDCvaY0CQk+fXDA==}
+
resolution: {integrity: sha512-uzR+FzI8qrUEIu96oaeBJmd9E7CFEiQ3goA5qCVgc4s5llSubcfGHq9yUstZx/k4s9dXHVKsE35YWoFyvEqEHA==}
@@ -23649,6 +23689,10 @@ packages:
webpack-cli:
optional: true
+ resolution: {integrity: sha512-U9vjByy/sK2OMXu5mmfuZFKTMIUQe34c0JXRO+oDrxJTsntdYT2iIFwYMOV7HhMTuktcZLGf2W1N/OcSf9ssWg==}
+ engines: {node: '>=6.0.0', npm: '>=3.10.0'}
+
resolution: {integrity: sha512-AMcUeyXAhbACL8S2hqqdqOLqvJ8ylmIbNwUIqQujRSouf4+eUFaXbG6F1Rbu+srlJMmxQWsiU7mOJi0nMBfM1g==}
@@ -28154,6 +28198,8 @@ snapshots:
'@braintree/[email protected]': {}
+ '@bufbuild/[email protected]': {}
+
'@bufbuild/[email protected]': {}
'@bufbuild/[email protected]': {}
@@ -28597,6 +28643,15 @@ snapshots:
react: 18.3.1
tslib: 2.8.1
+ '@elevenlabs/[email protected](@types/[email protected])':
+ dependencies:
+ '@elevenlabs/types': 0.13.0
+ livekit-client: 2.19.0(@types/[email protected])
+ transitivePeerDependencies:
+ - '@types/dom-mediacapture-record'
+
+ '@elevenlabs/[email protected]': {}
+
'@emnapi/[email protected]':
dependencies:
'@emnapi/wasi-threads': 1.0.1
@@ -30229,6 +30284,12 @@ snapshots:
dependencies:
'@lezer/common': 1.2.3
+ '@livekit/[email protected]': {}
+
+ '@livekit/[email protected]':
+ dependencies:
+ '@bufbuild/protobuf': 1.10.1
+
optional: true
@@ -36411,6 +36472,8 @@ snapshots:
'@types/[email protected]': {}
+ '@types/[email protected]': {}
+
dependencies:
'@types/trusted-types': 2.0.4
@@ -44568,6 +44631,19 @@ snapshots:
rfdc: 1.4.1
wrap-ansi: 9.0.2
+ dependencies:
+ '@livekit/mutex': 1.1.1
+ '@livekit/protocol': 1.45.8
+ '@types/dom-mediacapture-record': 1.0.22
+ events: 3.3.0
+ jose: 6.1.3
+ loglevel: 1.9.2
+ sdp-transform: 2.15.0
+ tslib: 2.8.1
+ typed-emitter: 2.1.0
+ webrtc-adapter: 9.0.5
+
dependencies:
msgpackr: 1.11.2
@@ -44696,6 +44772,8 @@ snapshots:
strip-ansi: 7.2.0
wrap-ansi: 9.0.2
+
@@ -49070,6 +49148,10 @@ snapshots:
dependencies:
compute-scroll-into-view: 3.1.1
+
+
@@ -50624,6 +50706,10 @@ snapshots:
possible-typed-array-names: 1.0.0
reflect.getprototypeof: 1.0.10
+ optionalDependencies:
+ rxjs: 7.8.1
+
@@ -51504,6 +51590,10 @@ snapshots:
- esbuild
- uglify-js
+ dependencies:
+ sdp: 3.2.2
+
diff --git a/posthog/rate_limit.py b/posthog/rate_limit.py
index f7cc8741a699..42c23898cb95 100644
--- a/posthog/rate_limit.py
+++ b/posthog/rate_limit.py
@@ -424,6 +424,45 @@ class AIResearchBurstRateThrottle(_AIThrottleBase):
action_name = "ai research burst rate limited"
+# Hands-free throttles are split per-action so a burst on /token/ (e.g. a flapping
+# mobile network triggering the reconnect path) can't lock the user out of /synthesize/
+# and vice versa. Each action's burst/sustained bucket caps a different cost surface:
+# token mints bound Scribe (STT) spend; synthesize calls bound ElevenLabs TTS spend.
+class MaxHandsFreeTokenBurstRateThrottle(_AIThrottleBase):
+ # 10/min covers normal reconnects (network blip, tab refocus) and stays well below
+ # any plausible legitimate flow. Dev gets a far larger ceiling for the dev loop.
+ scope = "max_hands_free_token_burst"
+ rate = "1000/minute" if settings.DEBUG else "10/minute"
+ action_name = "max hands-free token burst rate limited"
+
+
+class MaxHandsFreeTokenSustainedRateThrottle(_AIThrottleBase):
+ # Each token grants ~15 min of Scribe usage. A 2-hour gym session is ~8 tokens at
+ # minimum plus reconnects, call it 15. 60/day covers several long sessions a day
+ # with headroom and bounds worst-case Scribe spend per compromised account.
+ scope = "max_hands_free_token_sustained"
+ rate = "10000/day" if settings.DEBUG else "60/day"
+ action_name = "max hands-free token sustained rate limited"
+
+
+class MaxHandsFreeSynthesizeBurstRateThrottle(_AIThrottleBase):
+ # TTS calls fire once per assistant turn so the burst rate has to keep pace with
+ # rapid back-and-forth. 20/min still well above any natural conversation cadence
+ # but tight enough to slow a runaway client looping on synthesize.
+ scope = "max_hands_free_synthesize_burst"
+ rate = "1000/minute" if settings.DEBUG else "20/minute"
+ action_name = "max hands-free synthesize burst rate limited"
+
+
+class MaxHandsFreeSynthesizeSustainedRateThrottle(_AIThrottleBase):
+ # Bounds TTS spend per account per day. 150 calls * 2000 chars max ~= 300k chars
+ # ceiling/day — comfortable for power users, hard cap on a compromised account's
+ # daily ElevenLabs bill until the broader char-budget kill switch lands.
+ scope = "max_hands_free_synthesize_sustained"
+ rate = "10000/day" if settings.DEBUG else "150/day"
+ action_name = "max hands-free synthesize sustained rate limited"
+
+
class AIResearchSustainedRateThrottle(_AIThrottleBase):
scope = "ai_research_sustained"
rate = "10/day"
diff --git a/posthog/settings/integrations.py b/posthog/settings/integrations.py
index a4b156db5f5c..47e366facbe7 100644
--- a/posthog/settings/integrations.py
+++ b/posthog/settings/integrations.py
@@ -98,6 +98,20 @@ CLOUDFLARE_TURNSTILE_SITE_KEY = get_from_env("CLOUDFLARE_TURNSTILE_SITE_KEY", ""
RECALL_AI_API_KEY = get_from_env("RECALL_AI_API_KEY", "")
RECALL_AI_API_URL = get_from_env("RECALL_AI_API_URL", "https://us-west-2.recall.ai")
+# ElevenLabs (Max hands-free mode)
+# STT goes browser ↔ ElevenLabs over a single-use Scribe WebSocket token (backend just mints).
+# TTS goes browser → PostHog → ElevenLabs → audio stream (backend proxies the key to ElevenLabs).
+ELEVENLABS_API_KEY = get_from_env("ELEVENLABS_API_KEY", "")
+ELEVENLABS_API_BASE_URL = get_from_env("ELEVENLABS_API_BASE_URL", "https://api.elevenlabs.io")
+# Rachel is ElevenLabs' default voice — neutral, clear at gym pace. Override if you want a
+# different feel without redeploying.
+ELEVENLABS_VOICE_ID = get_from_env("ELEVENLABS_VOICE_ID", "21m00Tcm4TlvDq8ikWAM")
+# Turbo v2.5 has ~300ms TTFB latency, sits in the free tier, and sounds clean enough for
+# gym-pace narration. Flash v2.5 is marginally faster but requires Creator ($22/mo) or above
+# on the ElevenLabs side, so devs running on free quota hit a 402 from the TTS proxy.
+# Override at the env level if you're on a paid tier and want the extra polish.
+ELEVENLABS_TTS_MODEL_ID = get_from_env("ELEVENLABS_TTS_MODEL_ID", "eleven_turbo_v2_5")
+
# PandaDoc (for legal documents: BAA/DPA). One template per document variant.
# Each call needs the matching template id, so we keep them as separate env vars —
# rotating one (e.g., when Legal updates the DPA copy) doesn't touch the others.
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 9d5698bc0891..bbcc45c140b2 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -35571,6 +35571,14 @@ export namespace Schemas {
rates: SurveyStatsResponseRates;
}
+ export interface Synthesize {
+ /**
+ * The text the assistant should speak aloud.
+ * @maxLength 2000
+ */
+ text: string;
+ }
+
export interface TaggerCreate {
/** @maxLength 400 */
name: string;
@@ -40686,6 +40694,8 @@ export namespace Schemas {
offset?: number;
};
+ export type MaxHandsFreeTokenCreate200 = { [key: string]: unknown };
+
export type MaxToolsCreateAndQueryInsightCreate200 = { [key: string]: unknown };
export type McpAnalyticsFeedbackListParams = {