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.

fix(workflows): expose event global in batch step editor (#69943)

Harley Alexander8f4d94e0c79a
diff --git a/products/workflows/frontend/Workflows/hogflows/steps/components/HogFlowFunctionConfiguration.test.ts b/products/workflows/frontend/Workflows/hogflows/steps/components/HogFlowFunctionConfiguration.test.ts
new file mode 100644
index 000000000000..f51af2a64fda
--- /dev/null
+++ b/products/workflows/frontend/Workflows/hogflows/steps/components/HogFlowFunctionConfiguration.test.ts
@@ -0,0 +1,28 @@
+import { buildSampleGlobals } from './HogFlowFunctionConfiguration'
+
+describe('buildSampleGlobals', () => {
+ it.each([
+ ['event', { event: true, person: true, groups: true, request: false }],
+ ['batch', { event: true, person: true, groups: false, request: false }],
+ ['webhook', { event: false, person: false, groups: false, request: true }],
+ ])('exposes the right globals for a %s trigger', (triggerType, present) => {
+ const globals = buildSampleGlobals(triggerType, undefined)
+ Object.entries(present).forEach(([key, shouldExist]) => {
+ expect(key in globals).toBe(shouldExist)
+ })
+ })
+
+ // Batch runs have no external event, but the worker backfills event.distinct_id at dequeue, so the
+ // editor must expose event.distinct_id for batch or {event.distinct_id} wrongly warns as unknown.
+ it.each(['event', 'batch'])('exposes event.distinct_id for a %s trigger', (triggerType) => {
+ expect(buildSampleGlobals(triggerType, undefined).event).toHaveProperty('distinct_id')
+ })
+
+ it('maps workflow variables to typed placeholders', () => {
+ const globals = buildSampleGlobals(undefined, [
+ { key: 'name', type: 'string' },
+ { key: 'count', type: 'number' },
+ ])
+ expect(globals.variables).toEqual({ name: 'example_value', count: 123 })
+ })
+})
diff --git a/products/workflows/frontend/Workflows/hogflows/steps/components/HogFlowFunctionConfiguration.tsx b/products/workflows/frontend/Workflows/hogflows/steps/components/HogFlowFunctionConfiguration.tsx
index 96acf37746d3..f2ce594ed150 100644
--- a/products/workflows/frontend/Workflows/hogflows/steps/components/HogFlowFunctionConfiguration.tsx
+++ b/products/workflows/frontend/Workflows/hogflows/steps/components/HogFlowFunctionConfiguration.tsx
@@ -15,6 +15,80 @@ import { CyclotronJobInputType, HogFunctionMappingType } from '~/types'
import { workflowLogic } from '../../../workflowLogic'
import { HogFlowFunctionMappings } from './HogFlowFunctionMappings'
+// Builds the sample globals the input editor uses for autocomplete and unknown-global warnings.
+// The available globals depend on the trigger type: batch runs have no external triggering event,
+// but the worker backfills a real event.distinct_id at dequeue, so batch must expose `event` too or
+// the editor wrongly flags {event.distinct_id} as unknown.
+export function buildSampleGlobals(
+ triggerType: string | undefined,
+ variables: Array<Record<string, any>> | undefined | null
+): Record<string, any> {
+ const workflowVariables: Record<string, any> = {}
+ variables?.forEach((variable) => {
+ // Use placeholder values based on variable type
+ if (variable.type === 'string') {
+ workflowVariables[variable.key] = 'example_value'
+ } else if (variable.type === 'number') {
+ workflowVariables[variable.key] = 123
+ } else if (variable.type === 'boolean') {
+ workflowVariables[variable.key] = true
+ } else if (variable.type === 'dictionary' || variable.type === 'json') {
+ workflowVariables[variable.key] = {}
+ } else {
+ workflowVariables[variable.key] = null
+ }
+ })
+
+ const sampleGlobals: Record<string, any> = {
+ variables: workflowVariables,
+ }
+
+ if (triggerType === 'webhook') {
+ sampleGlobals.request = {
+ method: 'POST',
+ headers: {},
+ body: {},
+ params: {},
+ }
+ } else if (triggerType === 'event') {
+ sampleGlobals.event = {
+ event: 'example_event',
+ distinct_id: 'user123',
+ properties: {
+ $current_url: 'https://example.com',
+ },
+ timestamp: '2024-01-01T12:00:00Z',
+ }
+ sampleGlobals.person = {
+ id: 'person123',
+ properties: {
+ email: '[email protected]',
+ name: 'John Doe',
+ },
+ }
+ sampleGlobals.groups = {}
+ } else if (triggerType === 'batch') {
+ // Batch runs carry a synthesized event whose distinct_id the worker backfills from the
+ // person, so {event.distinct_id} resolves at runtime. Expose it here so the editor
+ // doesn't flag it as an unknown global.
+ sampleGlobals.event = {
+ event: '$batch_hog_flow_invocation',
+ distinct_id: 'user123',
+ properties: {},
+ timestamp: '2024-01-01T12:00:00Z',
+ }
+ sampleGlobals.person = {
+ id: 'person123',
+ properties: {
+ email: '[email protected]',
+ name: 'John Doe',
+ },
+ }
+ }
+
+ return sampleGlobals
+}
+
export function HogFlowFunctionConfiguration({
templateId,
inputs,
@@ -64,64 +138,7 @@ export function HogFlowFunctionConfiguration({
}
const triggerType = workflow?.trigger?.type
-
- // Build workflow variables object for autocomplete
- const workflowVariables: Record<string, any> = {}
- if (workflow?.variables) {
- workflow.variables.forEach((variable) => {
- // Use placeholder values based on variable type
- if (variable.type === 'string') {
- workflowVariables[variable.key] = 'example_value'
- } else if (variable.type === 'number') {
- workflowVariables[variable.key] = 123
- } else if (variable.type === 'boolean') {
- workflowVariables[variable.key] = true
- } else if (variable.type === 'dictionary' || variable.type === 'json') {
- workflowVariables[variable.key] = {}
- } else {
- workflowVariables[variable.key] = null
- }
- })
- }
-
- const sampleGlobals: Record<string, any> = {
- variables: workflowVariables,
- }
-
- if (triggerType === 'webhook') {
- sampleGlobals.request = {
- method: 'POST',
- headers: {},
- body: {},
- params: {},
- }
- } else if (triggerType === 'event') {
- // Event-based triggers
- sampleGlobals.event = {
- event: 'example_event',
- distinct_id: 'user123',
- properties: {
- $current_url: 'https://example.com',
- },
- timestamp: '2024-01-01T12:00:00Z',
- }
- sampleGlobals.person = {
- id: 'person123',
- properties: {
- email: '[email protected]',
- name: 'John Doe',
- },
- }
- sampleGlobals.groups = {}
- } else if (triggerType === 'batch') {
- sampleGlobals.person = {
- id: 'person123',
- properties: {
- email: '[email protected]',
- name: 'John Doe',
- },
- }
- }
+ const sampleGlobals = buildSampleGlobals(triggerType, workflow?.variables)
return (
<>