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.

4.3 KB
Raw Blame
import { MakeLogicType, kea, key, listeners, path, props, reducers } from 'kea'
import { loaders } from 'kea-loaders'

import { teamLogic } from 'scenes/teamLogic'

import { signalsScoutConfigPrecheckTest } from 'products/signals/frontend/generated/api'
import type { SignalScoutPrecheckTestApi } from 'products/signals/frontend/generated/api.schemas'

import { captureScoutAction, type ScoutSurface } from '../inboxAnalytics'

export interface ScoutPrecheckLogicProps {
    configId: string
    skillName: string
    surface: ScoutSurface
}

export interface ScoutPrecheckTestRequest {
    query: string
}

export interface ScoutPrecheckTestRun extends ScoutPrecheckTestRequest {
    result: SignalScoutPrecheckTestApi
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface scoutPrecheckLogicValues {
    testRun: ScoutPrecheckTestRun | null
    testRunFailed: boolean
    testRunLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface scoutPrecheckLogicActions {
    clearTestRun: () => any
    clearTestRunFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    clearTestRunSuccess: (
        testRun: null,
        payload?: any
    ) => {
        testRun: null
        payload?: any
    }
    testPrecheck: ({ query }: ScoutPrecheckTestRequest) => ScoutPrecheckTestRequest
    testPrecheckFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    testPrecheckSuccess: (
        testRun: {
            query: string
            result: SignalScoutPrecheckTestApi
        } | null,
        payload?: ScoutPrecheckTestRequest
    ) => {
        testRun: {
            query: string
            result: SignalScoutPrecheckTestApi
        } | null
        payload?: ScoutPrecheckTestRequest
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface scoutPrecheckLogicMeta {
    key: string
}

export type scoutPrecheckLogicType = MakeLogicType<
    scoutPrecheckLogicValues,
    scoutPrecheckLogicActions,
    ScoutPrecheckLogicProps,
    scoutPrecheckLogicMeta
>

/**
 * The "Test query" button of one scout's pre-check. It runs the query once with the `{since}` and
 * `{now}` values of the next scheduled run, and starts no run and saves nothing.
 */
export const scoutPrecheckLogic = kea<scoutPrecheckLogicType>([
    path((key) => ['scenes', 'inbox', 'logics', 'scoutPrecheckLogic', key]),
    props({} as ScoutPrecheckLogicProps),
    key((props) => props.configId),

    loaders(({ props }) => ({
        testRun: [
            null as ScoutPrecheckTestRun | null,
            {
                testPrecheck: async ({ query }: ScoutPrecheckTestRequest, breakpoint) => {
                    const teamId = teamLogic.values.currentTeamId
                    if (!teamId) {
                        return null
                    }
                    const result = await signalsScoutConfigPrecheckTest(String(teamId), props.configId, {
                        precheck_query: query,
                    })
                    // A second test can start while the first is in flight. Only the newest answers.
                    breakpoint()
                    return { query, result }
                },
                clearTestRun: () => null,
            },
        ],
    })),

    reducers({
        testRunFailed: [
            false,
            {
                testPrecheck: () => false,
                testPrecheckSuccess: () => false,
                testPrecheckFailure: () => true,
                clearTestRun: () => false,
            },
        ],
    }),

    listeners(({ props }) => ({
        testPrecheckSuccess: ({ testRun }) => {
            if (!testRun) {
                return
            }
            captureScoutAction({
                actionType: 'test_precheck',
                surface: props.surface,
                skillName: props.skillName,
                extra: {
                    would_run: testRun.result.would_run,
                    reason: testRun.result.reason,
                    row_count: testRun.result.row_count,
                    has_error: testRun.result.error !== null,
                },
            })
        },
    })),
])