: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
import { AI_EVENTS_OUTPUT, AiEventOutput, EventOutput } from '~/common/outputs'
import { isAiEventName } from '~/ingestion/common/ai-event-types'
import { EventToEmit } from '~/ingestion/common/steps/event-processing/emit-event-step'
import { ok } from '~/ingestion/framework/results'
import { ProcessingStep } from '~/ingestion/framework/steps'
import { LARGE_AI_PROPERTIES } from '~/ingestion/pipelines/ai/large-ai-properties'
import { ProcessedEvent } from '~/types'
export interface SplitAiEventsStepInput {
eventsToEmit: EventToEmit<EventOutput>[]
teamId: number
}
export type SplitAiEventsStepOutput = Omit<SplitAiEventsStepInput, 'eventsToEmit'> & {
eventsToEmit: EventToEmit<EventOutput | AiEventOutput>[]
}
function hasLargeAiProperties(properties: Record<string, unknown>): boolean {
for (const key of LARGE_AI_PROPERTIES) {
if (key in properties) {
return true
}
}
return false
}
function splitAiEvent(entry: EventToEmit<EventOutput>): EventToEmit<EventOutput | AiEventOutput>[] {
const properties = entry.event.properties ?? {}
if (!isAiEventName(entry.event.event)) {
return [entry]
}
if (!hasLargeAiProperties(properties)) {
// Nothing heavy to strip — duplicate unchanged to both outputs.
return [entry, { event: entry.event, output: AI_EVENTS_OUTPUT }]
}
// The heavy AI properties live only on the dedicated ai_events table, so strip them
// from the events copy and send the full event to AI_EVENTS_OUTPUT.
const stripped: Record<string, unknown> = {}
for (const [key, value] of Object.entries(properties)) {
if (!LARGE_AI_PROPERTIES.has(key)) {
stripped[key] = value
}
}
const strippedEvent: ProcessedEvent = { ...entry.event, properties: stripped }
return [
{ event: strippedEvent, output: entry.output },
{ event: entry.event, output: AI_EVENTS_OUTPUT },
]
}
export function createSplitAiEventsStep<T extends SplitAiEventsStepInput>(): ProcessingStep<
T,
T & SplitAiEventsStepOutput
> {
return function splitAiEventsStep(input) {
return Promise.resolve(
ok({
...input,
eventsToEmit: input.eventsToEmit.flatMap((entry) => splitAiEvent(entry)),
})
)
}
}