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.

7.4 KB
Raw Blame
import { HogFunctionTemplate } from '~/cdp/types'

import { hogApiErrorMessageFn } from '../../hog-helpers'

export const template: HogFunctionTemplate = {
    free: true,
    status: 'hidden',
    type: 'destination',
    id: 'template-posthog-create-task',
    name: 'Create AI task',
    description:
        'Start an AI agent task from a workflow. The agent works in the background and the task shows up in Tasks.',
    icon_url: '/static/posthog-icon.svg',
    category: ['Custom'],
    code_language: 'hog',
    code: `
${hogApiErrorMessageFn}

if (empty(inputs.prompt)) {
  throw Error('Instructions are required')
}

let payload := { 'prompt': inputs.prompt, 'event': event }

if (not empty(inputs.title)) {
  payload.title := inputs.title
}

if (not empty(inputs.model) and not empty(inputs.model.model)) {
  payload.model := inputs.model.model
  if (not empty(inputs.model.reasoning_effort)) {
    payload.reasoning_effort := inputs.model.reasoning_effort
  }
}

if (not empty(inputs.repository)) {
  // Steps saved before the picker allowed several repositories hold one owner/repo string.
  payload.repositories := typeof(inputs.repository) == 'array' ? inputs.repository : [inputs.repository]
}

if (not empty(inputs.connectors)) {
  payload.connectors := inputs.connectors
}

if (not empty(inputs.channel)) {
  payload.channel := inputs.channel
}

if (not empty(inputs.skills)) {
  payload.skills := inputs.skills
}

if (not empty(inputs.posthog_mcp_scopes)) {
  payload.posthog_mcp_scopes := inputs.posthog_mcp_scopes
}

if (not empty(inputs.max_parallel_tasks)) {
  payload.max_parallel_tasks := inputs.max_parallel_tasks
}

if (inputs.reply_in_slack_thread != false and event.event == '$slack_message_received' and not empty(event.properties.channel) and not empty(event.properties.ts)) {
  payload.slack_context := {
    'integration_id': event.properties.integration_id,
    'channel': event.properties.channel,
    'thread_ts': event.properties.thread_ts ?? event.properties.ts,
    'message_ts': event.properties.ts,
    'slack_user_id': event.properties.user ?? '',
    'slack_team_id': event.properties.slack_team_id ?? '',
    'is_ext_shared_channel': event.properties.is_ext_shared_channel ?? false
  }
}

let response := postHogCreateTask(payload)

if (response.status >= 400) {
  throw Error(f'Failed to create task ({response.status}): {apiErrorMessage(response)}')
}

let task := response.body
if (not empty(task.run_id)) {
  // Park the workflow step until the run finishes: the tasks runtime cap plus slack for its wake.
  task.await := { 'max_wait': '190m', 'label': 'task' }
}
return task
`,
    inputs_schema: [
        {
            key: 'prompt',
            type: 'string',
            label: 'Instructions',
            secret: false,
            required: true,
            description: 'What the agent should do. Supports variable templating.',
        },
        {
            key: 'title',
            type: 'string',
            label: 'Task title',
            secret: false,
            required: false,
            description: 'Name shown in the task list. Leave empty to name the task from the instructions.',
        },
        {
            key: 'model',
            type: 'task_model',
            label: 'Model',
            secret: false,
            required: false,
            description: 'Model and reasoning effort for the agent. Leave empty to use the default model.',
        },
        {
            key: 'repository',
            type: 'task_repository',
            label: 'Repositories',
            secret: false,
            required: false,
            description:
                'GitHub repositories the agent can work in, e.g. your-org/your-repo. Leave empty to give the agent no repository access.',
        },
        {
            key: 'connectors',
            type: 'task_mcp_installations',
            label: 'Connectors',
            secret: false,
            required: false,
            description:
                'MCP servers the agent can use. Only servers shared with everyone in this project can be selected.',
        },
        {
            key: 'skills',
            type: 'task_skills',
            label: 'Skills',
            secret: false,
            required: false,
            description:
                'Skills from your project’s skills store. The agent gets a short summary of each one and reads the full skill when it needs it. Always the latest saved version.',
        },
        {
            key: 'posthog_mcp_scopes',
            type: 'choice',
            label: 'PostHog access',
            secret: false,
            required: false,
            default: 'read_only',
            choices: [
                { label: 'Read only', value: 'read_only' },
                { label: 'Full access', value: 'full' },
            ],
            description: 'What the agent can do in your PostHog project. Read only blocks changes.',
        },
        {
            key: 'max_parallel_tasks',
            type: 'number',
            label: 'Maximum parallel tasks',
            secret: false,
            required: false,
            default: 5,
            description:
                'New runs are skipped while this many tasks from this workflow are still running. Protects against a burst of trigger events starting too many agents at once. Daily limits on how many tasks a workflow and project can create also apply.',
        },
        {
            // A space is a Tasks concept the workflow editor has no picker for, so the field is
            // hidden and set by the clients that know about spaces. The display name rides along
            // after a pipe, like the Slack channel picker's "C123|#name": the API keeps the id and
            // the clients read the name back without a second input to keep in step.
            key: 'channel',
            type: 'string',
            label: 'Space',
            secret: false,
            required: false,
            hidden: true,
            templating: false,
            description:
                'Space the created task is filed into, as its id, optionally followed by "|" and the space name. Leave empty to file the task in no space.',
        },
        {
            // Only meaningful on a Slack-triggered workflow; the builder hides it for other
            // triggers, and the hog code above no-ops when the trigger event isn't a Slack message.
            // Off, the backend sees no slack_context and ends the run when the agent finishes;
            // on, the run stays open for its idle window so the thread reply can relay.
            key: 'reply_in_slack_thread',
            type: 'boolean',
            label: 'Reply in the Slack thread',
            secret: false,
            required: false,
            default: true,
            templating: false,
            description:
                'The agent posts its updates as replies in the Slack thread that started this workflow. Replies in that thread are sent to the agent. The run stays open for about 2 minutes after the agent finishes so replies can reach it.',
        },
        {
            // Allow 409 through to the template so it can surface the API's detailed limit
            // message as the step error rather than the executor's generic fetch failure.
            key: 'non_failure_status_codes',
            type: 'non_failure_status_codes',
            label: 'Non-failure status codes',
            secret: false,
            required: false,
            hidden: true,
            default: [409],
        },
    ],
}