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.

2.1 KB
Raw Blame
import { DateTime } from 'luxon'

import { registerAsyncFunction } from '../async-function-registry'

registerAsyncFunction('produceToWarehouseWebhooks', {
    execute: (args, context, result) => {
        const [payload, explicitSchemaId] = args

        if (!payload || typeof payload !== 'object') {
            throw new Error('[HogFunction] - produceToWarehouseWebhooks requires an object payload')
        }

        if (result.warehouseWebhookPayloads.length > 0) {
            throw new Error(
                'produceToWarehouseWebhooks was called more than once. Only one call is allowed per function'
            )
        }

        // Use explicit schema_id from Hog code args, or fall back to inputs for backward compat
        const schemaId = explicitSchemaId ?? context.invocation.hogFunction.inputs?.schema_id?.value
        if (!schemaId || typeof schemaId !== 'string') {
            throw new Error(
                '[HogFunction] - produceToWarehouseWebhooks requires a schema_id (either as second argument or in hog function inputs)'
            )
        }

        result.warehouseWebhookPayloads.push({
            team_id: context.invocation.teamId,
            schema_id: schemaId,
            payload,
        })

        // Async handlers must leave a return value on the resumed VM stack so the trailing
        // POP from the expression statement has something to pop. fetch / sendEmail defer this
        // push to executeFetch / executeSendEmail once the real I/O completes; we finish our
        // work synchronously here, so we push immediately.
        result.invocation.state.vmState?.stack.push(null)
    },

    mock: (args, logs) => {
        logs.push({
            level: 'info',
            timestamp: DateTime.now(),
            message: `Async function 'produceToWarehouseWebhooks' was mocked with arguments:`,
        })
        logs.push({
            level: 'info',
            timestamp: DateTime.now(),
            message: `produceToWarehouseWebhooks(${JSON.stringify(args[0], null, 2)})`,
        })

        return { success: true }
    },
})