: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(conversations): mark which ticket messages were sent to the customer (#109784)
Christiaan Hendriksenb92118291e03
diff --git a/products/conversations/backend/api/tests/test_tickets.py b/products/conversations/backend/api/tests/test_tickets.pyindex ca8a57d1c78c..0d23d49892e3 100644--- a/products/conversations/backend/api/tests/test_tickets.py+++ b/products/conversations/backend/api/tests/test_tickets.py@@ -2653,11 +2653,14 @@ class TestTicketMessagesAPI(APIBaseTest):assert body[0]["author_type"] == "customer"assert body[0]["author_name"] == "Alice"assert body[0]["is_private"] is False+ assert body[0]["message_type"] == "customer_message"assert body[1]["content"] == "Hi there!"assert body[1]["author_type"] == "support"assert body[1]["is_private"] is False+ assert body[1]["message_type"] == "sent_reply"assert body[2]["content"] == "Internal note"assert body[2]["is_private"] is True+ assert body[2]["message_type"] == "internal_note"def test_messages_includes_private_notes(self, mock_on_commit):Comment.objects.create(@@ -2713,6 +2716,7 @@ class TestTicketMessagesAPI(APIBaseTest):msg = response.json()["results"][0]assert set(msg.keys()) == {"id",+ "message_type","content","rich_content","author_type",diff --git a/products/conversations/backend/api/tickets.py b/products/conversations/backend/api/tickets.pyindex b236bb57e65d..cb5e40a92682 100644--- a/products/conversations/backend/api/tickets.py+++ b/products/conversations/backend/api/tickets.py@@ -95,8 +95,9 @@ from products.conversations.backend.models import (TicketAssignment,TicketView,)-from products.conversations.backend.models.constants import Channel, ChannelDetail, Status+from products.conversations.backend.models.constants import Channel, ChannelDetail, Status, TicketMessageTypefrom products.conversations.backend.person_lookup import _get_persons_by_email+from products.conversations.backend.services.messages import ticket_message_typefrom .. import reply_dedupe@@ -128,6 +129,18 @@ class TicketMessageSerializer(serializers.Serializer):"""A single message in a ticket thread (output-only)."""id = serializers.UUIDField(read_only=True, help_text="Message (comment) UUID.")+ message_type = serializers.ChoiceField(+ choices=TicketMessageType.choices,+ read_only=True,+ help_text=(+ "What the message is, and whether it was sent to the customer. "+ "customer_message: written by the customer. "+ "sent_reply: a reply sent to the customer by a teammate, a workflow or the AI. "+ "It does not confirm that the customer received it, because delivery can fail. "+ "internal_note: a note for the team only. It was never sent to the customer. "+ "ai_draft: a reply or question the AI wrote for a teammate to review. It was never sent to the customer."+ ),+ )content = serializers.CharField(read_only=True, help_text="Plain-text message body.")rich_content = serializers.JSONField(read_only=True, allow_null=True, help_text="TipTap rich content JSON, if any.")author_type = serializers.CharField(read_only=True, help_text="One of: customer, support, AI.")@@ -1494,6 +1507,7 @@ class TicketViewSet(TaggedItemViewSetMixin, TeamAndOrgViewSetMixin, AccessControreturn {"id": comment.id,+ "message_type": ticket_message_type(item_context, comment.created_by_id),"content": comment.content,"rich_content": comment.rich_content,"author_type": author_type,diff --git a/products/conversations/backend/models/constants.py b/products/conversations/backend/models/constants.pyindex eeb3524e9f45..63fb9e56876c 100644--- a/products/conversations/backend/models/constants.py+++ b/products/conversations/backend/models/constants.py@@ -44,6 +44,13 @@ class Priority(models.TextChoices):CRITICAL = "critical", "Critical"+class TicketMessageType(models.TextChoices):+ CUSTOMER_MESSAGE = "customer_message", "Customer message"+ SENT_REPLY = "sent_reply", "Sent reply"+ INTERNAL_NOTE = "internal_note", "Internal note"+ AI_DRAFT = "ai_draft", "AI draft"++class OrganizationIdSource(models.TextChoices):PERSON = "person", "Requester identity"SLACK_CHANNEL_ACCOUNT = "slack_channel_account", "Slack channel account"diff --git a/products/conversations/backend/services/messages.py b/products/conversations/backend/services/messages.pyindex 03fdf6192bd1..beac660ebece 100644--- a/products/conversations/backend/services/messages.py+++ b/products/conversations/backend/services/messages.py@@ -2,12 +2,32 @@ from django.db.models import Q, QuerySetfrom posthog.models.comment import Comment+from products.conversations.backend.models.constants import TicketMessageType+# Ticket messages are stored as comments under this scope.TICKET_MESSAGE_SCOPE = "conversations_ticket"# "team" is analytics-only. "AI" and "customer" are not learning evidence.PUBLIC_HUMAN_AUTHOR_TYPES = ("support", "human")+AI_DRAFT_PERSIST_AS = frozenset({"reply", "clarification"})+++def ticket_message_type(item_context: object, created_by_id: int | None) -> TicketMessageType:+ context = item_context if isinstance(item_context, dict) else {}+ if context.get("is_private") is not True:+ if context.get("author_type", "customer") == "customer":+ return TicketMessageType.CUSTOMER_MESSAGE+ return TicketMessageType.SENT_REPLY+ if (+ created_by_id is None+ and context.get("author_type") == "AI"+ and "internal_note_key" not in context+ and context.get("persist_as", "reply") in AI_DRAFT_PERSIST_AS+ ):+ return TicketMessageType.AI_DRAFT+ return TicketMessageType.INTERNAL_NOTE+def _public_ticket_message_context() -> Q:# isnull keeps comments with no is_private key, because ~Q alone drops SQL NULL.diff --git a/products/conversations/backend/services/test_messages.py b/products/conversations/backend/services/test_messages.pynew file mode 100644index 000000000000..3ef052dd9df0--- /dev/null+++ b/products/conversations/backend/services/test_messages.py@@ -0,0 +1,65 @@+from django.test import SimpleTestCase++from parameterized import parameterized++from products.conversations.backend.models.constants import TicketMessageType+from products.conversations.backend.services.messages import ticket_message_type+++class TestTicketMessageType(SimpleTestCase):+ @parameterized.expand(+ [+ (+ "published_ai_reply",+ {"author_type": "AI", "is_private": False, "persist_as": "reply"},+ None,+ TicketMessageType.SENT_REPLY,+ ),+ (+ "imported_team_reply",+ {"author_type": "support", "is_private": False},+ None,+ TicketMessageType.SENT_REPLY,+ ),+ (+ "ai_draft_reply",+ {"author_type": "AI", "is_private": True, "persist_as": "reply"},+ None,+ TicketMessageType.AI_DRAFT,+ ),+ (+ "ai_suggested_question",+ {"author_type": "AI", "is_private": True, "persist_as": "clarification"},+ None,+ TicketMessageType.AI_DRAFT,+ ),+ (+ "ai_draft_without_persist_as",+ {"author_type": "AI", "is_private": True},+ None,+ TicketMessageType.AI_DRAFT,+ ),+ (+ "ai_findings",+ {"author_type": "AI", "is_private": True, "persist_as": "findings"},+ None,+ TicketMessageType.INTERNAL_NOTE,+ ),+ (+ "automation_note",+ {"author_type": "AI", "is_private": True, "internal_note_key": "signals_report:example"},+ None,+ TicketMessageType.INTERNAL_NOTE,+ ),+ (+ "teammate_note_labeled_ai",+ {"author_type": "AI", "is_private": True},+ 1,+ TicketMessageType.INTERNAL_NOTE,+ ),+ ]+ )+ def test_ticket_message_type(+ self, _name: str, item_context: dict, created_by_id: int | None, expected: TicketMessageType+ ) -> None:+ assert ticket_message_type(item_context, created_by_id) == expecteddiff --git a/products/conversations/frontend/generated/api.schemas.ts b/products/conversations/frontend/generated/api.schemas.tsindex 979d4e4308b3..3161c5816ee6 100644--- a/products/conversations/frontend/generated/api.schemas.ts+++ b/products/conversations/frontend/generated/api.schemas.ts@@ -460,12 +460,34 @@ export interface TicketErrorApi {error_type?: string}+/**+ * * `customer_message` - Customer message+ * * `sent_reply` - Sent reply+ * * `internal_note` - Internal note+ * * `ai_draft` - AI draft+ */+export type TicketMessageTypeEnumApi = (typeof TicketMessageTypeEnumApi)[keyof typeof TicketMessageTypeEnumApi]++export const TicketMessageTypeEnumApi = {+ CustomerMessage: 'customer_message',+ SentReply: 'sent_reply',+ InternalNote: 'internal_note',+ AiDraft: 'ai_draft',+} as const+/*** A single message in a ticket thread (output-only).*/export interface TicketMessageApi {/** Message (comment) UUID. */readonly id: string+ /** What the message is, and whether it was sent to the customer. customer_message: written by the customer. sent_reply: a reply sent to the customer by a teammate, a workflow or the AI. It does not confirm that the customer received it, because delivery can fail. internal_note: a note for the team only. It was never sent to the customer. ai_draft: a reply or question the AI wrote for a teammate to review. It was never sent to the customer.+ *+ * * `customer_message` - Customer message+ * * `sent_reply` - Sent reply+ * * `internal_note` - Internal note+ * * `ai_draft` - AI draft */+ readonly message_type: TicketMessageTypeEnumApi/** Plain-text message body. */readonly content: string/** TipTap rich content JSON, if any. */diff --git a/products/conversations/mcp/tools.yaml b/products/conversations/mcp/tools.yamlindex fd315581996b..4a7eb8bdf7b2 100644--- a/products/conversations/mcp/tools.yaml+++ b/products/conversations/mcp/tools.yaml@@ -84,10 +84,15 @@ tools:title: Read support ticket messagesdescription: >Return a support ticket's message thread, ordered chronologically. The response is paginated (default 50,- max 200 per page) — use the limit and offset query params to page through long threads, and read count/next- from the response envelope. Includes all messages from the customer, team members, and AI, as well as- private internal notes. Each message contains author_type, author_name, content, and an is_private flag- indicating internal notes.+ max 200 per page): use the limit and offset query params to page through long threads, and read count/next+ from the response envelope. The thread includes messages that were never sent to the customer, so read each+ message's message_type before you say what the team has sent to the customer. customer_message is written by+ the customer. sent_reply is a reply sent to the customer by a teammate, a workflow, or the AI. A sent_reply+ does not confirm delivery, because delivery can fail, so say the reply was sent, not that the customer+ received it. internal_note is a note for the team only. ai_draft is a reply or question the AI wrote for a+ teammate to review: it can read like a finished reply, but it was never sent, and a teammate may have sent+ their own version later as a separate sent_reply. internal_note and ai_draft messages both have is_private+ set to true. Never describe an internal_note or an ai_draft as sent to the customer.conversations-tickets-notes-create:operation: conversations_tickets_notes_createenabled: falsediff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.jsonindex 892e476b3bf0..27f7482d3972 100644--- a/services/mcp/schema/generated-tool-definitions.json+++ b/services/mcp/schema/generated-tool-definitions.json@@ -2802,7 +2802,7 @@}},"conversations-tickets-messages-retrieve": {- "description": "Return a support ticket's message thread, ordered chronologically. The response is paginated (default 50, max 200 per page) — use the limit and offset query params to page through long threads, and read count/next from the response envelope. Includes all messages from the customer, team members, and AI, as well as private internal notes. Each message contains author_type, author_name, content, and an is_private flag indicating internal notes.",+ "description": "Return a support ticket's message thread, ordered chronologically. The response is paginated (default 50, max 200 per page): use the limit and offset query params to page through long threads, and read count/next from the response envelope. The thread includes messages that were never sent to the customer, so read each message's message_type before you say what the team has sent to the customer. customer_message is written by the customer. sent_reply is a reply sent to the customer by a teammate, a workflow, or the AI. A sent_reply does not confirm delivery, because delivery can fail, so say the reply was sent, not that the customer received it. internal_note is a note for the team only. ai_draft is a reply or question the AI wrote for a teammate to review: it can read like a finished reply, but it was never sent, and a teammate may have sent their own version later as a separate sent_reply. internal_note and ai_draft messages both have is_private set to true. Never describe an internal_note or an ai_draft as sent to the customer.","category": "Conversations","feature": "conversations","summary": "Read support ticket messages",diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.jsonindex b69993ea2e6c..12767ae667db 100644--- a/services/mcp/schema/tool-definitions-all.json+++ b/services/mcp/schema/tool-definitions-all.json@@ -2817,7 +2817,7 @@}},"conversations-tickets-messages-retrieve": {- "description": "Return a support ticket's message thread, ordered chronologically. The response is paginated (default 50, max 200 per page) — use the limit and offset query params to page through long threads, and read count/next from the response envelope. Includes all messages from the customer, team members, and AI, as well as private internal notes. Each message contains author_type, author_name, content, and an is_private flag indicating internal notes.",+ "description": "Return a support ticket's message thread, ordered chronologically. The response is paginated (default 50, max 200 per page): use the limit and offset query params to page through long threads, and read count/next from the response envelope. The thread includes messages that were never sent to the customer, so read each message's message_type before you say what the team has sent to the customer. customer_message is written by the customer. sent_reply is a reply sent to the customer by a teammate, a workflow, or the AI. A sent_reply does not confirm delivery, because delivery can fail, so say the reply was sent, not that the customer received it. internal_note is a note for the team only. ai_draft is a reply or question the AI wrote for a teammate to review: it can read like a finished reply, but it was never sent, and a teammate may have sent their own version later as a separate sent_reply. internal_note and ai_draft messages both have is_private set to true. Never describe an internal_note or an ai_draft as sent to the customer.","category": "Conversations","feature": "conversations","summary": "Read support ticket messages",diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex c23146b4393f..b8a6ea1d3e33 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -71495,12 +71495,35 @@ export namespace Schemas {results: Ticket[];}+ /**+ * * `customer_message` - Customer message+ * * `sent_reply` - Sent reply+ * * `internal_note` - Internal note+ * * `ai_draft` - AI draft+ */+ export type TicketMessageTypeEnum = typeof TicketMessageTypeEnum[keyof typeof TicketMessageTypeEnum];+++ export const TicketMessageTypeEnum = {+ CustomerMessage: 'customer_message',+ SentReply: 'sent_reply',+ InternalNote: 'internal_note',+ AiDraft: 'ai_draft',+ } as const;+/*** A single message in a ticket thread (output-only).*/export interface TicketMessage {/** Message (comment) UUID. */readonly id: string;+ /** What the message is, and whether it was sent to the customer. customer_message: written by the customer. sent_reply: a reply sent to the customer by a teammate, a workflow or the AI. It does not confirm that the customer received it, because delivery can fail. internal_note: a note for the team only. It was never sent to the customer. ai_draft: a reply or question the AI wrote for a teammate to review. It was never sent to the customer.+ *+ * * `customer_message` - Customer message+ * * `sent_reply` - Sent reply+ * * `internal_note` - Internal note+ * * `ai_draft` - AI draft */+ readonly message_type: TicketMessageTypeEnum;/** Plain-text message body. */readonly content: string;/** TipTap rich content JSON, if any. */