: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
fix(workflows): reload the broadcast wizard after posthog ai edits it (#109311)
Daniel Marchuke4b299c5fbf8
diff --git a/products/workflows/frontend/Broadcasts/BroadcastWizardStepper.tsx b/products/workflows/frontend/Broadcasts/BroadcastWizardStepper.tsxindex 3cc1f6a85c1d..d4b9294f5da9 100644--- a/products/workflows/frontend/Broadcasts/BroadcastWizardStepper.tsx+++ b/products/workflows/frontend/Broadcasts/BroadcastWizardStepper.tsx@@ -3,15 +3,7 @@ import { Tooltip } from '@posthog/lemon-ui'import { cn } from 'lib/utils/css-classes'-import { BROADCAST_WIZARD_STEPS, BroadcastWizardStep } from './broadcastWizardLogic'--const STEP_LABELS: Record<BroadcastWizardStep, string> = {- recipients: 'Recipients',- goal: 'Goal',- content: 'Content',- schedule: 'Schedule',- review: 'Review',-}+import { BROADCAST_WIZARD_STEPS, BROADCAST_WIZARD_STEP_LABELS, BroadcastWizardStep } from './broadcastWizardLogic'interface BroadcastWizardStepperProps {currentStep: BroadcastWizardStep@@ -84,7 +76,7 @@ export function BroadcastWizardStepper({!isCompleted && !isCurrent && 'text-secondary')}>- {STEP_LABELS[step]}+ {BROADCAST_WIZARD_STEP_LABELS[step]}</span></button>)diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.tsindex cd9d8888b995..d2047a436b55 100644--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts@@ -74,6 +74,15 @@ const EMAIL_AUTOSAVE_RETRY_MS = 2000export const BROADCAST_WIZARD_STEPS: BroadcastWizardStep[] = ['recipients', 'goal', 'content', 'schedule', 'review']+// Shown in the stepper, and named to PostHog AI so it points the user at the step they can see.+export const BROADCAST_WIZARD_STEP_LABELS: Record<BroadcastWizardStep, string> = {+ recipients: 'Recipients',+ goal: 'Goal',+ content: 'Content',+ schedule: 'Schedule',+ review: 'Review',+}+export type BroadcastScheduleMode = 'now' | 'later' | 'recurring'// The value stored in the email action's `inputs.email.value`, mirroring the@@ -281,6 +290,9 @@ export interface broadcastWizardLogicActions {broadcast: HogFlowApi | nullpayload?: any}+ loadExternalEdit: () => {+ value: true+ }moveToDraft: () => {value: true}@@ -487,6 +499,7 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([draftAutosaved: (broadcast: HogFlowApi) => ({ broadcast }),applyExternalEdit: (broadcast: HogFlowApi, base: HogFlowApi | null) => ({ broadcast, base }),replayDeferredEdit: true,+ loadExternalEdit: true,expandRun: (runId: string) => ({ runId }),collapseRun: (runId: string) => ({ runId }),setExpandedRunOverride: (runIds: string[]) => ({ runIds }),@@ -1041,6 +1054,10 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([})},applyExternalEdit: ({ broadcast, base }) => {+ // PostHog AI can change the recipients, so the audience size shown must follow.+ if (changedElsewhere(broadcast, base, readAudience)) {+ actions.loadBlastRadius()+ }const composerDraft = loadComposerDraft(broadcast.id)if (!composerDraft) {return@@ -1196,7 +1213,7 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([actions.resourceEdited(deferred)}},- resourceEdited: async ({ event }, breakpoint) => {+ resourceEdited: ({ event }) => {const broadcast = values.broadcastif (!broadcast ||@@ -1206,6 +1223,14 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([) {return}+ actions.loadExternalEdit()+ },+ loadExternalEdit: async (_, breakpoint) => {+ const broadcast = values.broadcast+ if (!broadcast || broadcast.status !== 'draft' || !values.currentProjectId) {+ return+ }+ await getSaveQueue(cache, values).whenIdle()await breakpoint(200)const fresh = await hogFlowsRetrieve(String(values.currentProjectId), broadcast.id).catch(() => null)breakpoint()diff --git a/products/workflows/frontend/Broadcasts/useBroadcastAgentPanel.ts b/products/workflows/frontend/Broadcasts/useBroadcastAgentPanel.tsindex 2a62d9b1a44f..5cfd81d6316d 100644--- a/products/workflows/frontend/Broadcasts/useBroadcastAgentPanel.ts+++ b/products/workflows/frontend/Broadcasts/useBroadcastAgentPanel.ts@@ -1,4 +1,4 @@-import { useValues } from 'kea'+import { useActions, useValues } from 'kea'import { useMemo } from 'react'import { useDebouncedValue } from 'lib/hooks/useDebouncedValue'@@ -7,11 +7,17 @@ import { useSceneAgentPanel } from 'scenes/max/useSceneAgentPanel'import { HogFunctionTemplateType } from '~/types'+import { resolveToolCall, useToolStreamListener } from 'products/posthog_ai/frontend/api/logics'import { AttachedContextItem } from 'products/posthog_ai/frontend/api/types'import type { HogFlow } from '../Workflows/hogflows/types'import { EMAIL_EDITOR_AGENT_HEADLINES, buildWorkflowAgentContext } from '../Workflows/workflowAgentContext'-import { EMAIL_ACTION_ID, broadcastWizardLogic } from './broadcastWizardLogic'+import {+ BROADCAST_WIZARD_STEPS,+ BROADCAST_WIZARD_STEP_LABELS,+ EMAIL_ACTION_ID,+ broadcastWizardLogic,+} from './broadcastWizardLogic'// The context builder redacts every input of a step whose template it cannot find. The broadcast's only// function step uses template-email, whose single input is the email itself and holds no secret.@@ -22,17 +28,30 @@ const BROADCAST_TEMPLATES: Record<string, HogFunctionTemplateType> = {} as unknown as HogFunctionTemplateType,}-// Static text, so it is safe as a trusted instruction. The wizard rewrites the graph on every save,-// so graph edits would be lost, and launching belongs to the wizard's review step.+const BROADCAST_EDIT_TOOLS = [+ 'workflows-patch-action-email',+ 'workflows-patch-graph',+ 'workflows-update',+ 'workflows-restore-revision',+ 'workflows-discard-draft',+]++// Static text, so it is safe as a trusted instruction. The wizard reads an edit made elsewhere back into its+// recipients and email, but it rewrites the rest of the graph on save, and launching belongs to its Review step.const BROADCAST_CONTEXT_ITEM: AttachedContextItem = {type: 'instructions',hidden: true,dismissGroup: 'broadcast-content',value:- 'This workflow is a broadcast: a batch trigger, one email step and an exit. Change only the email step ' +- '(its content, subject and preheader). Do not add, remove or reorder steps, and do not enable or ' +- 'publish it. The user launches it from the broadcast wizard. An email edit only saves once the step has ' +- 'a sender, so if from.integrationId is empty, ask the user to pick one in the From field first.',+ 'This workflow is a broadcast. The user edits it in a wizard with these steps, in order: ' ++ `${BROADCAST_WIZARD_STEPS.map((step) => BROADCAST_WIZARD_STEP_LABELS[step]).join(', ')}. ` ++ 'Use these step names when you point the user to a step. You can change two things. The recipients: ' ++ 'update the batch trigger step with workflows-patch-graph, setting config.filters.properties to person ' ++ 'property conditions and cohort references only, because behavioral (event) conditions are not ' ++ 'supported. The email: change only the email step (its content, subject and preheader). Do not add, ' ++ 'remove or reorder steps, do not change the goal or the schedule, and do not enable or publish it. The ' ++ 'user launches it from the Review step. An email edit only saves once the step has a sender, so if ' ++ 'from.integrationId is empty, ask the user to pick one in the From field on the Content step first.',}/**@@ -42,6 +61,7 @@ const BROADCAST_CONTEXT_ITEM: AttachedContextItem = {export function useBroadcastAgentPanel(): void {const { broadcastAsWorkflow, broadcastId, currentStep } = useValues(broadcastWizardLogic)const { sceneIntegrationEnabled } = useValues(sceneAgentPanelLogic)+ const { loadExternalEdit } = useActions(broadcastWizardLogic)// Debounced so each keystroke does not re-serialize the email into the agent context.const debouncedWorkflow = useDebouncedValue(broadcastAsWorkflow, 500)const agentContextItems = useMemo(@@ -68,4 +88,19 @@ export function useBroadcastAgentPanel(): void {active: !!broadcastId,autoOpen: currentStep === 'content',})+ // The edited-elsewhere stream can miss the agent's write, so reload after each edit it makes here.+ // A plain listener, not an apply-back: a composer run starts before this wizard mounts, and the+ // reload is safe to repeat.+ useToolStreamListener({+ tools: BROADCAST_EDIT_TOOLS,+ onEvent: (event) => {+ if (event.phase !== 'completed' || !broadcastId) {+ return+ }+ const targetId = resolveToolCall(event.invocation).innerInput?.id+ if (typeof targetId !== 'string' || targetId === broadcastId) {+ loadExternalEdit()+ }+ },+ })}