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(workflows): show premade workflow ideas instead of generating when a project has them (#119724)

Daniel Marchuk09165a458a3d
diff --git a/products/workflows/backend/facade/contracts.py b/products/workflows/backend/facade/contracts.py
index 0da05ca54a1c..ad8a510fccec 100644
--- a/products/workflows/backend/facade/contracts.py
+++ b/products/workflows/backend/facade/contracts.py
@@ -793,6 +793,8 @@ class DataSuggestion:
weekly_count: int
stage: LifecycleStage
step_outline: tuple[OutlineKind, ...]
+ # Prepared ahead of time with its workflow already built, so a click creates the draft without a model call.
+ premade: bool = False
@frozen
@@ -826,10 +828,23 @@ class BuiltSuggestion:
missing_inputs: tuple[MissingInput, ...] = ()
+@frozen
+class PreparedIdeas:
+ """What `prepare_workflow_ideas` did for one project."""
+
+ status: DataSuggestionsStatus
+ stored: tuple[str, ...] = ()
+ failed_builds: int = 0
+
+
class DataSuggestionNotFound(Exception):
"""The suggestion id is unknown, usually because the cached suggestions expired."""
+class WorkflowIdeaAlreadyResolved(Exception):
+ """The premade idea was already used."""
+
+
class EmailDraftSourceNotFound(Exception):
"""The project has no entity with that ID for the requested source."""
diff --git a/products/workflows/backend/facade/data_suggestions.py b/products/workflows/backend/facade/data_suggestions.py
index 64234d2d7b0b..b030541997ae 100644
--- a/products/workflows/backend/facade/data_suggestions.py
+++ b/products/workflows/backend/facade/data_suggestions.py
@@ -5,14 +5,19 @@ from posthog.models import Team, User
from products.workflows.backend.facade.contracts import BuiltSuggestion, DataSuggestionsResult
from products.workflows.backend.services.data_suggestions import service
+from products.workflows.backend.services.data_suggestions.premade import waiting_premade_count
from products.workflows.backend.services.data_suggestions.service import (
FEATURE_FLAG_KEY,
build_caller,
count_waiting_data_suggestions,
+ data_suggestions_acting_user,
email_sender_ready,
get_data_suggestions,
+ get_premade_workflow,
needs_new_build,
needs_new_generation,
+ prepare_premade_ideas,
+ save_premade_draft,
)
from products.workflows.backend.tasks.data_suggestions import build_data_suggestion, generate_data_suggestions
@@ -20,12 +25,17 @@ __all__ = [
"FEATURE_FLAG_KEY",
"build_caller",
"count_waiting_data_suggestions",
+ "data_suggestions_acting_user",
"email_sender_ready",
"get_data_suggestions",
+ "get_premade_workflow",
"needs_new_build",
"needs_new_generation",
+ "prepare_premade_ideas",
+ "save_premade_draft",
"start_build",
"start_data_suggestions",
+ "waiting_premade_count",
]
diff --git a/products/workflows/backend/management/commands/prepare_workflow_ideas.py b/products/workflows/backend/management/commands/prepare_workflow_ideas.py
new file mode 100644
index 000000000000..a9f3bea8ff9d
--- /dev/null
+++ b/products/workflows/backend/management/commands/prepare_workflow_ideas.py
@@ -0,0 +1,52 @@
+from django.core.management.base import BaseCommand
+
+from posthog.event_usage import EventSource
+from posthog.models import Team
+from posthog.models.scoping import team_scope
+
+from products.workflows.backend.facade.data_suggestions import (
+ data_suggestions_acting_user,
+ prepare_premade_ideas,
+ waiting_premade_count,
+)
+from products.workflows.backend.presentation.views.data_suggestion_drafts import validated_draft
+
+
+class Command(BaseCommand):
+ help = (
+ "Generate workflow ideas for the given projects and build each one now, so a click creates the draft at once. "
+ "This runs the models."
+ )
+
+ def add_arguments(self, parser):
+ parser.add_argument("--team-ids", type=int, nargs="+", required=True, help="Project IDs.")
+
+ def handle(self, *args, **options):
+ team_ids: list[int] = options["team_ids"]
+ teams = list(
+ Team.objects.select_related("organization")
+ .filter(id__in=team_ids, parent_team_id__isnull=True)
+ .order_by("id")
+ )
+ for missing in sorted(set(team_ids) - {team.id for team in teams}):
+ self.stdout.write(f"team {missing}: skipped, no such project")
+ for team in teams:
+ self.stdout.write(f"team {team.id}: {self._prepare(team)}")
+
+ def _prepare(self, team: Team) -> str:
+ user = data_suggestions_acting_user(team)
+ if user is None:
+ return f"skipped, organization {team.organization_id} has no active owner or admin"
+ waiting = waiting_premade_count(team.id)
+ if waiting:
+ return f"skipped, {waiting} ideas already waiting"
+ # The draft check reads team-scoped rows, and a command has no request to set the team.
+ with team_scope(team.id):
+ result = prepare_premade_ideas(
+ team=team,
+ user=user,
+ validate_draft=lambda draft_team, workflow: validated_draft(draft_team, workflow, EventSource.WEB),
+ )
+ if result.status != "ready":
+ return f"no ideas, suggestions are {result.status}"
+ return f"stored {', '.join(result.stored) or 'nothing'}, {result.failed_builds} failed to build"
diff --git a/products/workflows/backend/presentation/views/workflow_data_suggestions.py b/products/workflows/backend/presentation/views/workflow_data_suggestions.py
index 622fe62424af..7ab325d47c1e 100644
--- a/products/workflows/backend/presentation/views/workflow_data_suggestions.py
+++ b/products/workflows/backend/presentation/views/workflow_data_suggestions.py
@@ -2,30 +2,41 @@ from typing import Any, cast
from django.db import models
+import structlog
import posthoganalytics
from drf_spectacular.utils import extend_schema, extend_schema_field
-from rest_framework import serializers, viewsets
+from rest_framework import serializers, status, viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import NotFound, PermissionDenied
from rest_framework.request import Request
from rest_framework.response import Response
from posthog.api.routing import TeamAndOrgViewSetMixin
-from posthog.event_usage import get_event_source
+from posthog.event_usage import EventSource, get_event_source, report_user_action
from posthog.models import User
from posthog.rate_limit import AIBurstRateThrottle, AISustainedRateThrottle
-from products.workflows.backend.facade.contracts import DataSuggestionNotFound, DataSuggestionsResult
+from products.workflows.backend.facade.contracts import (
+ BuiltSuggestion,
+ DataSuggestionNotFound,
+ DataSuggestionsResult,
+ WorkflowIdeaAlreadyResolved,
+)
from products.workflows.backend.facade.data_suggestions import (
FEATURE_FLAG_KEY,
build_caller,
email_sender_ready,
get_data_suggestions,
+ get_premade_workflow,
needs_new_build,
needs_new_generation,
+ save_premade_draft,
start_build,
start_data_suggestions,
)
+from products.workflows.backend.presentation.views.data_suggestion_drafts import save_draft, validated_draft
+
+logger = structlog.get_logger(__name__)
class DataSuggestionStepKind(models.TextChoices):
@@ -75,6 +86,10 @@ class DataSuggestionSerializer(serializers.Serializer):
child=serializers.ChoiceField(choices=DataSuggestionStepKind.choices),
help_text="The main planned step kinds after the trigger, in order.",
)
+ premade = serializers.BooleanField(
+ help_text="Whether PostHog prepared this suggestion ahead of time with its workflow already built. Build it "
+ "with save_draft to save the draft and stop offering the suggestion."
+ )
class DataSuggestionsResponseSerializer(serializers.Serializer):
@@ -231,19 +246,71 @@ class WorkflowDataSuggestionsViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):
user = self._user_with_feature(request)
payload = DataSuggestionBuildRequestSerializer(data=request.data)
payload.is_valid(raise_exception=True)
+ suggestion_id = payload.validated_data["suggestion_id"]
+ save = payload.validated_data["save_draft"]
+ event_source = get_event_source(request)
try:
- built = start_build(
- team=self.team,
- user=user,
- suggestion_id=payload.validated_data["suggestion_id"],
- retry=payload.validated_data["retry"],
- save=payload.validated_data["save_draft"],
- event_source=get_event_source(request),
- )
+ built = self._premade_build(user, suggestion_id, save=save, event_source=event_source)
+ if built is None:
+ built = start_build(
+ team=self.team,
+ user=user,
+ suggestion_id=suggestion_id,
+ retry=payload.validated_data["retry"],
+ save=save,
+ event_source=event_source,
+ )
+ except WorkflowIdeaAlreadyResolved:
+ return Response({"detail": "This suggestion was already used."}, status=status.HTTP_409_CONFLICT)
except DataSuggestionNotFound:
raise NotFound("This suggestion has expired. Refresh the page to get new ones.")
return Response(DataSuggestionBuildResponseSerializer(built).data)
+ def _premade_build(
+ self, user: User, suggestion_id: str, *, save: bool, event_source: EventSource
+ ) -> BuiltSuggestion | None:
+ if not save:
+ return get_premade_workflow(
+ team=self.team,
+ idea_id=suggestion_id,
+ validate_draft=lambda team, workflow: validated_draft(team, workflow, event_source),
+ )
+ saved = save_premade_draft(
+ team=self.team,
+ user=user,
+ idea_id=suggestion_id,
+ caller=build_caller(event_source.value, save=True, user_id=user.id),
+ # A saved draft uses the editor's draft rules for every caller, like a saved generated build, so blank
+ # settings stay blank and are listed in missing_inputs. An unsaved draft uses the caller's own rules.
+ validate_draft=lambda team, workflow: validated_draft(team, workflow, EventSource.WEB),
+ save_draft=save_draft,
+ )
+ if saved is not None and saved.workflow_id is not None and saved.workflow is not None:
+ self._report_created(saved.workflow_id, saved.workflow)
+ return saved
+
+ def _report_created(self, workflow_id: str, workflow: dict[str, Any]) -> None:
+ # The hog_flows create endpoint sends this for every other new workflow, and a premade save skips that
+ # endpoint. The properties match it, so creation metrics count both paths the same way.
+ try:
+ report_user_action(
+ self.request.user,
+ "hog_flow_created",
+ {
+ "workflow_id": workflow_id,
+ "workflow_name": workflow.get("name"),
+ "origin_product": workflow.get("origin_product"),
+ "team_id": str(self.team_id),
+ "organization_id": str(self.organization.id),
+ "edges_count": len(workflow.get("edges") or []),
+ "actions_count": len(workflow.get("actions") or []),
+ },
+ team=self.team,
+ request=self.request,
+ )
+ except Exception as e:
+ logger.warning("workflows.data_suggestions.created_event_failed", team_id=self.team_id, error=str(e))
+
def _user_with_feature(self, request: Request) -> User:
user = request.user
if (
diff --git a/products/workflows/backend/services/data_suggestions/README.md b/products/workflows/backend/services/data_suggestions/README.md
index 2501e28a0f2e..b5c22673606d 100644
--- a/products/workflows/backend/services/data_suggestions/README.md
+++ b/products/workflows/backend/services/data_suggestions/README.md
@@ -33,6 +33,15 @@ Building a draft (`_plan_and_build` in `service.py`):
2. The builder turns the plan into a workflow and rejects plans that do not hold up.
3. The draft is checked the way the caller's own create request checks it.
+## Premade ideas
+
+`python manage.py prepare_workflow_ideas --team-ids <id> ...` runs the card run and a build for each card now, then stores each built card as a `WorkflowIdea` row with its workflow in `definition`.
+While a project has such ideas waiting, `current` and `generate` return only those cards, read from Postgres, and no run starts.
+The consent and privacy checks apply to premade cards too, so a project that loses AI approval stops seeing them.
+A build with `save_draft` saves the stored workflow as a draft and marks the idea accepted in one transaction.
+A build without `save_draft` returns the stored workflow, checked with the caller's own create rules, and the idea stays offered.
+Once every idea is used, the project gets the generated cards again.
+
Agents use the same endpoints through three MCP tools in `products/workflows/mcp/tools.yaml`: `workflows-data-suggestions-list`, `-get` and `-create`.
The create tool always sends `save_draft`, so the build task saves the draft itself with the editor's draft rules and returns its `workflow_id`.
Blank settings come back as `missing_inputs`, for the agent to ask the user about.
@@ -42,6 +51,7 @@ Blank settings come back as `missing_inputs`, for the agent to ask the user abou
| File | What it does |
| ----------------- | ------------------------------------------------------------------------------------------------------ |
| `service.py` | Runs both jobs: caching, locks, consent checks, and the order of the steps above. Start here. |
+| `premade.py` | Premade ideas: storing them, their cards, and locking one to save its draft. |
| `event_volume.py` | Counts events in ClickHouse. Samples large teams and recounts what the sample missed. |
| `stages.py` | Asks Jev for each event's lifecycle stage, with confidence rules. |
| `ranking.py` | Stage weights, scoring, and which events reach Jev and Claude. |
diff --git a/products/workflows/backend/services/data_suggestions/premade.py b/products/workflows/backend/services/data_suggestions/premade.py
new file mode 100644
index 000000000000..2ff2e458783b
--- /dev/null
+++ b/products/workflows/backend/services/data_suggestions/premade.py
@@ -0,0 +1,139 @@
+"""Workflow ideas prepared ahead of time by `prepare_workflow_ideas`, each stored with its built workflow.
+
+A project with waiting premade ideas sees only those cards, read from Postgres. No generation run starts for it.
+"""
+
+import copy
+import hashlib
+from collections.abc import Sequence
+from typing import Any, cast
+from uuid import UUID
+
+from django.db import transaction
+from django.db.models import QuerySet
+from django.utils import timezone
+
+from posthog.models import Team, User
+
+from products.workflows.backend.facade.contracts import BuiltSuggestion, DataSuggestion, WorkflowIdeaAlreadyResolved
+from products.workflows.backend.facade.enums import WorkflowIdeaStatus
+from products.workflows.backend.models import HogFlow, WorkflowIdea
+from products.workflows.backend.services.data_suggestions.builder import EmailSender
+
+
+def premade_suggestions(team: Team) -> tuple[DataSuggestion, ...]:
+ # A stored workflow can be large, and the cards only need the metadata.
+ rows = _waiting(team.id).defer("definition").order_by("created_at")
+ return tuple(premade_card(row) for row in rows)
+
+
+def waiting_premade_count(team_id: int) -> int:
+ return _waiting(team_id).count()
+
+
+def is_premade_idea(team_id: int, idea_id: str) -> bool:
+ return _is_uuid(idea_id) and _premade(team_id).filter(id=idea_id).exists()
+
+
+def store_premade_ideas(*, team: Team, built: Sequence[tuple[DataSuggestion, BuiltSuggestion]]) -> tuple[str, ...]:
+ """Stores each built suggestion as a waiting idea and returns the stored titles. A key the project already
+ accepted or dismissed is not offered again."""
+ stored: list[str] = []
+ with transaction.atomic():
+ for suggestion, build in built:
+ if build.workflow is None:
+ continue
+ _, created = WorkflowIdea.objects.for_team(team.id).get_or_create(
+ team_id=team.id,
+ # Event names can be longer than the key column.
+ key="event:" + hashlib.sha256(suggestion.trigger_event.encode()).hexdigest()[:32],
+ defaults={
+ "title": suggestion.name,
+ "rationale": suggestion.reason,
+ "definition": build.workflow,
+ "evidence": {
+ "description": suggestion.description,
+ "trigger_event": suggestion.trigger_event,
+ "weekly_count": suggestion.weekly_count,
+ "stage": suggestion.stage,
+ "step_outline": list(suggestion.step_outline),
+ "uses_saved_template": build.uses_saved_template,
+ },
+ },
+ )
+ if created:
+ stored.append(suggestion.name)
+ return tuple(stored)
+
+
+def lock_premade_idea(team_id: int, idea_id: str) -> WorkflowIdea | None:
+ """The waiting premade idea, locked until the transaction ends, or None when the id is not a premade idea."""
+ if not _is_uuid(idea_id):
+ return None
+ row = _premade(team_id).select_for_update().filter(id=idea_id).first()
+ if row is not None and row.status != WorkflowIdeaStatus.SUGGESTED:
+ raise WorkflowIdeaAlreadyResolved()
+ return row
+
+
+def waiting_premade_idea(team_id: int, idea_id: str) -> WorkflowIdea | None:
+ if not _is_uuid(idea_id):
+ return None
+ return _waiting(team_id).filter(id=idea_id).first()
+
+
+def mark_accepted(row: WorkflowIdea, *, team_id: int, hog_flow_id: str, user: User) -> None:
+ row.status = WorkflowIdeaStatus.ACCEPTED
+ # The draft is not committed yet, so a replica read would not find it.
+ row.hog_flow = HogFlow.objects.using("default").get(team_id=team_id, id=hog_flow_id)
+ row.resolved_at = timezone.now()
+ row.resolved_by = user
+ row.save(update_fields=["status", "hog_flow", "resolved_at", "resolved_by"])
+
+
+def premade_card(row: WorkflowIdea) -> DataSuggestion:
+ evidence = row.evidence or {}
+ return DataSuggestion(
+ id=str(row.id),
+ name=row.title,
+ description=str(evidence.get("description") or row.rationale),
+ reason=row.rationale,
+ trigger_event=str(evidence.get("trigger_event") or ""),
+ weekly_count=int(evidence.get("weekly_count") or 0),
+ stage=evidence.get("stage") or "other",
+ step_outline=tuple(evidence.get("step_outline") or ()),
+ premade=True,
+ )
+
+
+def with_sender(workflow: dict[str, Any], sender: EmailSender | None) -> dict[str, Any]:
+ """Fills each blank email sender, because the project may have verified one after the idea was built."""
+ if sender is None:
+ return workflow
+ filled = copy.deepcopy(workflow)
+ for action in filled.get("actions") or []:
+ if action.get("type") != "function_email":
+ continue
+ email = cast(dict[str, Any], ((action.get("config") or {}).get("inputs") or {}).get("email") or {})
+ value = email.get("value")
+ if isinstance(value, dict) and not (value.get("from") or {}).get("email"):
+ value["from"] = {"integrationId": sender.integration_id, "email": sender.email, "name": sender.name}
+ return filled
+
+
+def _premade(team_id: int) -> QuerySet[WorkflowIdea]:
+ # for_team() also matches the parent project's ideas from a child environment, which an environment-scoped key
+ # must not read.
+ return WorkflowIdea.objects.for_team(team_id).filter(team_id=team_id).exclude(definition={})
+
+
+def _waiting(team_id: int) -> QuerySet[WorkflowIdea]:
+ return _premade(team_id).filter(status=WorkflowIdeaStatus.SUGGESTED)
+
+
+def _is_uuid(value: str) -> bool:
+ try:
+ UUID(value)
+ except ValueError:
+ return False
+ return True
diff --git a/products/workflows/backend/services/data_suggestions/service.py b/products/workflows/backend/services/data_suggestions/service.py
index e48e20363254..df295580223e 100644
--- a/products/workflows/backend/services/data_suggestions/service.py
+++ b/products/workflows/backend/services/data_suggestions/service.py
@@ -8,16 +8,19 @@ from typing import Any
from django.conf import settings
from django.core.cache import cache
+from django.db import transaction
import structlog
import posthoganalytics
from posthog.cloud_utils import is_cloud
from posthog.errors import CH_TRANSIENT_ERRORS, InternalCHQueryError
+from posthog.event_usage import EventSource
from posthog.exceptions import ClickHouseQueryMemoryLimitExceeded, ClickHouseQueryTimeOut
from posthog.exceptions_capture import capture_exception
from posthog.llm.system_one_client import system_one_configured
from posthog.models import Team, User
+from posthog.models.organization import OrganizationMembership
from posthog.ph_client import get_client, ph_scoped_capture
from posthog.utils import get_instance_region
@@ -28,7 +31,9 @@ from products.workflows.backend.facade.contracts import (
DataSuggestionNotFound,
DataSuggestionsResult,
MissingInput,
+ PreparedIdeas,
)
+from products.workflows.backend.models import WorkflowIdea as WorkflowIdeaRow
from products.workflows.backend.models.hog_flow.hog_flow import HogFlow
from products.workflows.backend.services.data_suggestions import team_context
from products.workflows.backend.services.data_suggestions.builder import (
@@ -47,6 +52,16 @@ from products.workflows.backend.services.data_suggestions.planner import (
plan_steps,
suggest_ideas,
)
+from products.workflows.backend.services.data_suggestions.premade import (
+ is_premade_idea,
+ lock_premade_idea,
+ mark_accepted,
+ premade_card,
+ premade_suggestions,
+ store_premade_ideas,
+ waiting_premade_idea,
+ with_sender,
+)
from products.workflows.backend.services.data_suggestions.ranking import (
pick_one_per_stage,
select_prompt_events,
@@ -95,9 +110,13 @@ NO_SENDER_FIELD = "Email sender (no verified sender yet: add a sending domain un
def get_data_suggestions(*, team: Team) -> DataSuggestionsResult:
"""The cards as they are now. This never runs a query or a model, so a read-only caller cannot cause spend."""
+ # Checked before the premade cards too, because the models wrote them from the project's events.
unavailable = _unavailable_reason(team)
if unavailable is not None:
return unavailable
+ premade = _premade_result(team)
+ if premade is not None:
+ return premade
# Checked before the cache, so a refresh in progress reads as generating rather than as the old cards.
if cache.get(_generation_lock_key(team)) is not None:
return _GENERATING
@@ -116,6 +135,21 @@ def is_data_suggestions_enabled(user: User, organization_id: str) -> bool:
)
+def data_suggestions_acting_user(team: Team) -> User | None:
+ """The organization's longest-standing owner or admin, who runs the models when ideas are prepared ahead of time."""
+ membership = (
+ OrganizationMembership.objects.filter(
+ organization_id=team.organization_id,
+ level__gte=OrganizationMembership.Level.ADMIN,
+ user__is_active=True,
+ )
+ .select_related("user")
+ .order_by("-level", "joined_at")
+ .first()
+ )
+ return membership.user if membership is not None else None
+
+
def count_waiting_data_suggestions(*, team: Team, user: User) -> int:
"""The ideas this person would see on the empty workflows page. Like `get_data_suggestions`, it never runs a model."""
if HogFlow.objects.filter(team_id=team.id).exclude(status=HogFlow.State.ARCHIVED).exists():
@@ -127,10 +161,14 @@ def count_waiting_data_suggestions(*, team: Team, user: User) -> int:
def start_data_suggestions(*, team: Team, refresh: bool, enqueue: Callable[[str], None]) -> DataSuggestionsResult:
- """Returns the cached cards, or starts a background run through `enqueue` and reports that it is generating."""
+ """Returns the cached cards, or starts a background run through `enqueue` and reports that it is generating.
+ A project with premade ideas gets only those, and no run starts."""
unavailable = _unavailable_reason(team)
if unavailable is not None:
return unavailable
+ premade = _premade_result(team)
+ if premade is not None:
+ return premade
cached = _read_cache(team)
if cached is not None and not refresh:
return cached
@@ -153,6 +191,8 @@ def email_sender_ready(*, team: Team) -> bool:
def needs_new_generation(*, team: Team, refresh: bool) -> bool:
"""Whether a generate call would start a run, the only case that costs model time."""
+ if _premade_result(team) is not None:
+ return False
# A refresh always counts, because the run it joins can end before the request reaches the lock.
return refresh or (_read_cache(team) is None and cache.get(_generation_lock_key(team)) is None)
@@ -213,6 +253,64 @@ def start_build(
return _read_build(team.id, suggestion_id, caller) or _BUILDING
+def save_premade_draft(
+ *,
+ team: Team,
+ user: User,
+ idea_id: str,
+ caller: str,
+ validate_draft: Callable[[Team, dict[str, Any]], dict[str, Any] | None],
+ save_draft: Callable[[Team, User, dict[str, Any]], str],
+) -> BuiltSuggestion | None:
+ """Saves a premade idea's workflow as a draft and marks the idea accepted, or returns None when the id is not a
+ premade idea. The idea row stays locked until both writes commit, so a second call cannot save a second draft."""
+ if _unavailable_reason(team) is not None:
+ raise DataSuggestionNotFound()
+ with transaction.atomic():
+ row = lock_premade_idea(team.id, idea_id)
+ if row is None:
+ return None
+ workflow = with_sender(row.definition, team_context.email_sender(team))
+ validated = validate_draft(team, workflow)
+ if validated is None:
+ return _BUILD_FAILED
+ workflow_id = save_draft(team, user, validated)
+ mark_accepted(row, team_id=team.id, hog_flow_id=workflow_id, user=user)
+ result = dataclasses.replace(_premade_build(team, row, workflow), workflow_id=workflow_id)
+ # The web page sends this event itself, with the surface the card was on.
+ if not caller.startswith(f"{EventSource.WEB.value}:"):
+ # The process-wide client sends in the background, so the request does not wait for a flush.
+ _capture_saved_build(
+ capture=posthoganalytics.capture, user=user, suggestion=premade_card(row), result=result, caller=caller
+ )
+ return result
+
+
+def get_premade_workflow(
+ *, team: Team, idea_id: str, validate_draft: Callable[[Team, dict[str, Any]], dict[str, Any] | None]
+) -> BuiltSuggestion | None:
+ """A waiting premade idea's workflow without saving it, or None when the id is not a waiting premade idea. The
+ idea stays offered until a call saves it."""
+ if _unavailable_reason(team) is not None:
+ raise DataSuggestionNotFound()
+ row = waiting_premade_idea(team.id, idea_id)
+ if row is None:
+ return None
+ workflow = with_sender(row.definition, team_context.email_sender(team))
+ if validate_draft(team, workflow) is None:
+ return _BUILD_FAILED
+ return _premade_build(team, row, workflow)
+
+
+def _premade_build(team: Team, row: WorkflowIdeaRow, workflow: dict[str, Any]) -> BuiltSuggestion:
+ return BuiltSuggestion(
+ status="ready",
+ workflow=workflow,
+ uses_saved_template=bool(row.evidence.get("uses_saved_template")),
+ missing_inputs=_missing_inputs(team, workflow),
+ )
+
+
def build_caller(event_source: str, *, save: bool, user_id: int) -> str:
"""The key a build is stored under. A saved build is checked with the editor's draft rules, so it differs from an
unsaved build for the same caller. A saved build is also kept per user, because its draft can carry access
@@ -222,6 +320,8 @@ def build_caller(event_source: str, *, save: bool, user_id: int) -> str:
def needs_new_build(*, team: Team, suggestion_id: str, caller: str, retry: bool) -> bool:
"""Whether a build call would start planning, the only case that costs model time."""
+ if is_premade_idea(team.id, suggestion_id):
+ return False
stored = _current_build(team, suggestion_id, caller)
reusable = stored is not None and not (retry and stored.status == "failed")
return not reusable and cache.get(_build_lock_key(team.id, suggestion_id, caller)) is None
@@ -266,11 +366,43 @@ def run_build(
cache.set(_build_key(team_id, suggestion_id, caller), dataclasses.asdict(result), ttl)
# Sent after the result is stored, so a slow analytics delivery never keeps a saved draft reading as building.
if result.workflow_id is not None and suggestion is not None:
- _capture_saved_build(user=user, suggestion=suggestion, result=result, caller=caller)
+ with ph_scoped_capture() as capture:
+ _capture_saved_build(capture=capture, user=user, suggestion=suggestion, result=result, caller=caller)
finally:
_release(lock_key, lock_token)
+def prepare_premade_ideas(
+ *,
+ team: Team,
+ user: User,
+ validate_draft: Callable[[Team, dict[str, Any]], dict[str, Any] | None],
+) -> PreparedIdeas:
+ """Generates the cards and builds each one now, then stores them as premade ideas. Nothing is cached, so the
+ project's own generation runs are not affected."""
+ unavailable = _unavailable_reason(team)
+ if unavailable is not None:
+ return PreparedIdeas(status=unavailable.status)
+ with _analytics_client() as analytics_client:
+ result = _generate(team=team, user=user, analytics_client=analytics_client)
+ if result.status != "ready":
+ return PreparedIdeas(status=result.status)
+ built: list[tuple[DataSuggestion, BuiltSuggestion]] = []
+ for suggestion in result.suggestions:
+ try:
+ build, _ = _plan_and_build(
+ team=team, user=user, suggestion=suggestion, events=result.events, validate_draft=validate_draft
+ )
+ except Exception as error:
+ logger.exception("workflows.data_suggestions.prepare_build_failed", team_id=team.id)
+ capture_exception(error, {"team_id": team.id})
+ build = _BUILD_FAILED
+ if build.status == "ready":
+ built.append((suggestion, build))
+ stored = store_premade_ideas(team=team, built=built)
+ return PreparedIdeas(status="ready", stored=stored, failed_builds=len(result.suggestions) - len(built))
+
+
def _plan_and_build(
*,
team: Team,
@@ -343,6 +475,11 @@ def _plan_and_build(
return result, validated
+def _premade_result(team: Team) -> DataSuggestionsResult | None:
+ cards = premade_suggestions(team)
+ return DataSuggestionsResult(status="ready", suggestions=cards) if cards else None
+
+
def _current_build(team: Team, suggestion_id: str, caller: str) -> BuiltSuggestion | None:
"""The stored build. A saved draft is read again, so a deleted draft gets a new build and the blank settings
match the draft as it is now, for example after the team added a sender."""
@@ -373,22 +510,23 @@ def _input_schemas(workflow: dict[str, Any]) -> dict[str, list[dict[str, Any]]]:
return schemas
-def _capture_saved_build(*, user: User, suggestion: DataSuggestion, result: BuiltSuggestion, caller: str) -> None:
+def _capture_saved_build(
+ *, capture: Callable[..., object], user: User, suggestion: DataSuggestion, result: BuiltSuggestion, caller: str
+) -> None:
# The web page sends this event from the browser. A saved build has no browser, so the funnel gets it from here.
- with ph_scoped_capture() as capture:
- # pinned: analytics event name - renaming breaks dashboards
- capture(
- distinct_id=user.distinct_id,
- event="workflows data suggestion built",
- properties={
- "workflow_id": result.workflow_id,
- "source": caller.split(":")[0],
- "trigger_event": suggestion.trigger_event,
- "stage": suggestion.stage,
- "uses_saved_template": result.uses_saved_template,
- "missing_inputs": len(result.missing_inputs),
- },
- )
+ # pinned: analytics event name - renaming breaks dashboards
+ capture(
+ distinct_id=user.distinct_id,
+ event="workflows data suggestion built",
+ properties={
+ "workflow_id": result.workflow_id,
+ "source": caller.split(":")[0],
+ "trigger_event": suggestion.trigger_event,
+ "stage": suggestion.stage,
+ "uses_saved_template": result.uses_saved_template,
+ "missing_inputs": len(result.missing_inputs),
+ },
+ )
def _chosen_template_bodies(
diff --git a/products/workflows/backend/tests/api/test_workflow_data_suggestions.py b/products/workflows/backend/tests/api/test_workflow_data_suggestions.py
index b9d54cbd73f2..e233974b0aca 100644
--- a/products/workflows/backend/tests/api/test_workflow_data_suggestions.py
+++ b/products/workflows/backend/tests/api/test_workflow_data_suggestions.py
@@ -1,8 +1,12 @@
+from io import StringIO
+
import time_machine
from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events
from unittest.mock import MagicMock, patch
from django.core.cache import cache
+from django.core.management import call_command
+from django.core.management.base import CommandError
from django.db import OperationalError
from django.utils import timezone
@@ -13,13 +17,16 @@ from posthog.errors import InternalCHQueryError
from posthog.event_usage import EventSource
from posthog.exceptions import ClickHouseQueryTimeOut
from posthog.models.integration import Integration
+from posthog.models.organization import OrganizationMembership
from posthog.models.personal_api_key import PersonalAPIKey, hash_key_value
+from posthog.models.team import Team
from posthog.models.user import User
from posthog.models.utils import generate_random_token_personal
from products.cdp.backend.templates.hog_function_template import sync_template_to_db
from products.messaging.backend.facade.testing import create_message_template_for_test
from products.workflows.backend.facade.contracts import DataSuggestionsResult
+from products.workflows.backend.models import WorkflowIdea as WorkflowIdeaModel
from products.workflows.backend.models.hog_flow.hog_flow import HogFlow
from products.workflows.backend.models.team_workflows_config import TeamWorkflowsConfig
from products.workflows.backend.presentation.views.data_suggestion_drafts import validated_draft
@@ -848,3 +855,99 @@ class TestWorkflowDataSuggestions(ClickhouseTestMixin, APIBaseTest):
flag.return_value = False
response = self.client.get(f"/api/projects/{self.team.id}/workflow_data_suggestions/current/")
assert response.status_code == 403
+
+ @parameterized.expand(
+ [
+ ("one_project", ""),
+ ("with_an_unknown_project", "team 999999: skipped, no such project\n"),
+ ]
+ )
+ @patch(f"{SERVICE}.posthoganalytics.capture")
+ @patch(f"{SERVICE}.plan_steps")
+ @patch(f"{SERVICE}.suggest_ideas")
+ def test_premade_ideas_replace_the_generated_cards_until_one_click_saves_them(
+ self,
+ _name: str,
+ missing: str,
+ suggest_ideas: MagicMock,
+ plan_steps: MagicMock,
+ capture: MagicMock,
+ flag: MagicMock,
+ ) -> None:
+ suggest_ideas.return_value = [_idea("trial_started")]
+ plan_steps.return_value = PlannedWorkflow(first_step="welcome", steps=[_email_step("welcome", "Welcome")])
+ self.organization_membership.level = OrganizationMembership.Level.ADMIN
+ self.organization_membership.save()
+
+ args = ["--team-ids", str(self.team.id), *(["999999"] if missing else [])]
+
+ def prepare() -> str:
+ out = StringIO()
+ call_command("prepare_workflow_ideas", *args, stdout=out)
+ return out.getvalue()
+
+ with self.assertRaises(CommandError):
+ call_command("prepare_workflow_ideas")
+ flag.return_value = True
+ assert prepare() == missing + f"team {self.team.id}: stored Follow up on trial_started, 0 failed to build\n"
+ assert prepare() == missing + f"team {self.team.id}: skipped, 1 ideas already waiting\n"
+ suggest_ideas.reset_mock()
+ plan_steps.reset_mock()
+ sender = Integration.objects.create(
+ team=self.team, kind="email", config={"email": "[email protected]", "verified": True}
+ )
+ idea = WorkflowIdeaModel.objects.for_team(self.team.id).get()
+
+ cards = self._list(refresh=True)
+
+ assert [(card["id"], card["premade"]) for card in cards["suggestions"]] == [(str(idea.id), True)]
+ assert suggest_ideas.call_count == 0
+ assert cache.get(_cache_key(self.team)) is None
+
+ save = {"suggestion_id": str(idea.id), "save_draft": True}
+ build_url = f"/api/projects/{self.team.id}/workflow_data_suggestions/build/"
+ unsaved = self.client.post(build_url, {"suggestion_id": str(idea.id)}, format="json")
+ self.organization.is_ai_data_processing_approved = False
+ self.organization.save()
+ withdrawn = (self._list()["status"], self.client.post(build_url, save, format="json").status_code)
+ self.organization.is_ai_data_processing_approved = True
+ self.organization.save()
+ other_team = Team.objects.create(organization=self.organization)
+ foreign = self.client.post(
+ f"/api/projects/{other_team.id}/workflow_data_suggestions/build/", save, format="json"
+ )
+ child = Team.objects.create(organization=self.organization, parent_team=self.team)
+ from_child = self.client.post(
+ f"/api/environments/{child.id}/workflow_data_suggestions/build/", save, format="json"
+ )
+ key = generate_random_token_personal()
+ PersonalAPIKey.objects.create(
+ label="agent", user=self.user, secure_value=hash_key_value(key), scopes=["hog_flow:write", "query:read"]
+ )
+ self.client.logout()
+ auth = {"authorization": f"Bearer {key}"}
+ saved = self.client.post(build_url, save, format="json", headers=auth)
+ repeated = self.client.post(build_url, save, format="json", headers=auth)
+
+ assert (unsaved.status_code, unsaved.json()["status"], unsaved.json()["workflow_id"]) == (200, "ready", None)
+ assert withdrawn == ("ai_not_approved", 404)
+ assert (foreign.status_code, from_child.status_code, saved.status_code, repeated.status_code) == (
+ 404,
+ 404,
+ 200,
+ 409,
+ )
+ draft = HogFlow.objects.get(team=self.team)
+ idea.refresh_from_db()
+ assert (idea.status, idea.hog_flow_id) == ("accepted", draft.id)
+ assert saved.json()["workflow_id"] == str(draft.id)
+ # The sender was verified after the idea was built, so the save fills it in.
+ assert draft.actions[1]["config"]["inputs"]["email"]["value"]["from"]["integrationId"] == sender.id
+ assert plan_steps.call_count == 0
+ events = {call.kwargs.get("event"): call.kwargs.get("properties", {}) for call in capture.call_args_list}
+ assert events["workflows data suggestion built"]["source"] == EventSource.API.value
+ assert events["hog_flow_created"]["workflow_id"] == str(draft.id)
+
+ self.client.force_login(self.user)
+ suggest_ideas.return_value = [_idea("user_registered")]
+ assert [card["premade"] for card in self._list()["suggestions"]] == [False]
diff --git a/products/workflows/frontend/generated/api.schemas.ts b/products/workflows/frontend/generated/api.schemas.ts
index e1086f48d632..e5397ac5177f 100644
--- a/products/workflows/frontend/generated/api.schemas.ts
+++ b/products/workflows/frontend/generated/api.schemas.ts
@@ -2404,6 +2404,8 @@ export interface DataSuggestionApi {
stage: DataSuggestionStageEnumApi
/** The main planned step kinds after the trigger, in order. */
step_outline: DataSuggestionStepKindEnumApi[]
+ /** Whether PostHog prepared this suggestion ahead of time with its workflow already built. Build it with save_draft to save the draft and stop offering the suggestion. */
+ premade: boolean
}
export interface DataSuggestionsResponseApi {
diff --git a/products/workflows/frontend/setupGuide/suggestions/suggestionCards.ts b/products/workflows/frontend/setupGuide/suggestions/suggestionCards.ts
index e9dc724b51ec..1f66ed49df5e 100644
--- a/products/workflows/frontend/setupGuide/suggestions/suggestionCards.ts
+++ b/products/workflows/frontend/setupGuide/suggestions/suggestionCards.ts
@@ -16,6 +16,7 @@ export interface SuggestionCard {
weeklyCount: number
steps: SuggestionStepKind[]
suggestionId: string
+ premade: boolean
}
export function cardFromSuggestion(suggestion: DataSuggestionApi): SuggestionCard {
@@ -28,5 +29,6 @@ export function cardFromSuggestion(suggestion: DataSuggestionApi): SuggestionCar
weeklyCount: suggestion.weekly_count,
steps: suggestion.step_outline,
suggestionId: suggestion.id,
+ premade: suggestion.premade,
}
}
diff --git a/products/workflows/frontend/setupGuide/suggestions/workflowDataSuggestionsLogic.test.ts b/products/workflows/frontend/setupGuide/suggestions/workflowDataSuggestionsLogic.test.ts
index cd7e0b82aa19..47ac1a0e7723 100644
--- a/products/workflows/frontend/setupGuide/suggestions/workflowDataSuggestionsLogic.test.ts
+++ b/products/workflows/frontend/setupGuide/suggestions/workflowDataSuggestionsLogic.test.ts
@@ -32,6 +32,7 @@ const card: SuggestionCard = {
weeklyCount: 42,
steps: ['email'],
suggestionId: 'suggestion-1',
+ premade: false,
}
describe('workflowDataSuggestionsLogic', () => {
@@ -65,6 +66,7 @@ describe('workflowDataSuggestionsLogic', () => {
weekly_count: 42,
stage: 'signup',
step_outline: ['email'],
+ premade: false,
},
],
})
@@ -85,15 +87,16 @@ describe('workflowDataSuggestionsLogic', () => {
})
it.each([
- ['empty_state', false],
- ['onboarding_wizard', true],
- ] as const)('opens the saved draft after a successful build on the %s', async (surface, finishesWizard) => {
+ ['empty_state', false, false],
+ ['onboarding_wizard', true, false],
+ ['empty_state', false, true],
+ ] as const)('opens the saved draft on the %s, wizard %s, premade %s', async (surface, finishesWizard, premade) => {
const capture = jest.spyOn(posthog, 'capture')
jest.mocked(workflowDataSuggestionsBuild).mockResolvedValue({
status: 'ready',
workflow: { name: 'Welcome new users' },
uses_saved_template: false,
- workflow_id: null,
+ workflow_id: premade ? 'workflow-1' : null,
missing_inputs: [],
})
jest.mocked(hogFlowsCreate).mockResolvedValue({ id: 'workflow-1' } as any)
@@ -103,12 +106,15 @@ describe('workflowDataSuggestionsLogic', () => {
const logic = workflowDataSuggestionsLogic({ surface })
logic.mount()
- await logic.asyncActions.startSuggestion(card)
+ await logic.asyncActions.startSuggestion({ ...card, premade })
- expect(hogFlowsCreate).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({ name: 'Welcome new users', status: 'draft' }),
- expect.objectContaining({ signal: expect.anything() })
+ expect(jest.mocked(workflowDataSuggestionsBuild).mock.calls.map(([, body]) => body)).toEqual([
+ premade
+ ? { suggestion_id: 'suggestion-1', save_draft: true }
+ : { suggestion_id: 'suggestion-1', retry: true },
+ ])
+ expect(jest.mocked(hogFlowsCreate).mock.calls.map(([, body]) => body)).toEqual(
+ premade ? [] : [expect.objectContaining({ name: 'Welcome new users', status: 'draft' })]
)
expect(router.values.location.pathname).toContain('workflow-1')
expect(loadEntrySource('workflow-1')).toBe('data_suggestion')
diff --git a/products/workflows/frontend/setupGuide/suggestions/workflowDataSuggestionsLogic.ts b/products/workflows/frontend/setupGuide/suggestions/workflowDataSuggestionsLogic.ts
index 718397855011..eebe583b62e5 100644
--- a/products/workflows/frontend/setupGuide/suggestions/workflowDataSuggestionsLogic.ts
+++ b/products/workflows/frontend/setupGuide/suggestions/workflowDataSuggestionsLogic.ts
@@ -221,7 +221,7 @@ export const workflowDataSuggestionsLogic = kea<workflowDataSuggestionsLogicType
// pinned: analytics event name - renaming breaks dashboards
posthog.capture('workflows data suggestions shown', {
surface: props.surface,
- source: 'ai',
+ source: aiResult.suggestions.some((suggestion) => suggestion.premade) ? 'premade' : 'ai',
trigger_events: aiResult.suggestions.map((suggestion) => suggestion.trigger_event),
stages: aiResult.suggestions.map((suggestion) => suggestion.stage),
})
@@ -240,7 +240,7 @@ export const workflowDataSuggestionsLogic = kea<workflowDataSuggestionsLogicType
// pinned: analytics event name - renaming breaks dashboards
posthog.capture('workflows data suggestion clicked', {
surface: props.surface,
- source: 'ai',
+ source: card.premade ? 'premade' : 'ai',
trigger_event: card.triggerEvent,
weekly_count: card.weeklyCount,
stage: card.stage,
@@ -256,7 +256,20 @@ export const workflowDataSuggestionsLogic = kea<workflowDataSuggestionsLogicType
{ suggestion_id: card.suggestionId, retry },
{ signal: AbortSignal.timeout(Math.max(deadline - Date.now(), 0)) }
)
- try {
+ // A premade card is saved and linked to its idea in one request, so a failure cannot leave an
+ // unlinked draft behind for the next click to duplicate.
+ const saveDraft = async (): Promise<{ workflowId: string; usesSavedTemplate: boolean }> => {
+ const saved = await workflowDataSuggestionsBuild(
+ projectId,
+ { suggestion_id: card.suggestionId, save_draft: true },
+ { signal: AbortSignal.timeout(SAVE_REQUEST_TIMEOUT_MS) }
+ )
+ if (saved.status !== 'ready' || !saved.workflow_id) {
+ throw new Error(BUILD_FAILED_MESSAGE)
+ }
+ return { workflowId: saved.workflow_id, usesSavedTemplate: saved.uses_saved_template }
+ }
+ const buildAndSave = async (): Promise<{ workflowId: string; usesSavedTemplate: boolean }> => {
// The build runs in the background. Asking again is free while it runs, and breakpoint stops
// the loop, the save and the redirect once the person leaves the page.
let built = await requestBuild(true)
@@ -287,21 +300,25 @@ export const workflowDataSuggestionsLogic = kea<workflowDataSuggestionsLogicType
>[1],
{ signal: AbortSignal.timeout(SAVE_REQUEST_TIMEOUT_MS) }
)
+ return { workflowId: workflow.id, usesSavedTemplate: built.uses_saved_template }
+ }
+ try {
+ const { workflowId, usesSavedTemplate } = card.premade ? await saveDraft() : await buildAndSave()
breakpoint()
// pinned: entry source value - 'workflow enabled' reports it, so renaming breaks the funnel
- saveEntrySource(workflow.id, 'data_suggestion')
+ saveEntrySource(workflowId, 'data_suggestion')
// pinned: analytics event name - renaming breaks dashboards
posthog.capture('workflows data suggestion built', {
- workflow_id: workflow.id,
+ workflow_id: workflowId,
surface: props.surface,
trigger_event: card.triggerEvent,
stage: card.stage,
- uses_saved_template: built.uses_saved_template,
+ uses_saved_template: usesSavedTemplate,
})
if (props.surface === 'onboarding_wizard') {
workflowsOnboardingWizardLogic.findMounted()?.actions.finishWithSuggestion()
}
- router.actions.push(urls.workflow(workflow.id, 'workflow'))
+ router.actions.push(urls.workflow(workflowId, 'workflow'))
} catch (error: any) {
if (isBreakpoint(error)) {
throw error
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index a887a162cc06..7330d20d293b 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -30007,6 +30007,8 @@ export namespace Schemas {
stage: DataSuggestionStageEnum;
/** The main planned step kinds after the trigger, in order. */
step_outline: DataSuggestionStepKindEnum[];
+ /** Whether PostHog prepared this suggestion ahead of time with its workflow already built. Build it with save_draft to save the draft and stop offering the suggestion. */
+ premade: boolean;
}
export interface DataSuggestionBuildRequest {