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.

3.4 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, path, reducers, selectors } from 'kea'

import { preflightLogic } from 'lib/logic/preflightLogic'
import { userLogic } from 'scenes/userLogic'

import type { UserType } from '../../../../frontend/src/types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface debugLogsLogicValues {
    isDev: boolean | undefined // preflightLogic
    user: UserType | null // userLogic
    canControlDebugLogs: boolean
    debugLogsEnabled: boolean
    showDebugLogs: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface debugLogsLogicActions {
    setDebugLogsEnabled: (enabled: boolean) => {
        enabled: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface debugLogsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        canControlDebugLogs: (user: UserType | null, isDev: boolean | undefined) => boolean
        showDebugLogs: (user: UserType | null, canControlDebugLogs: boolean, debugLogsEnabled: boolean) => boolean
    }
}

export type debugLogsLogicType = MakeLogicType<
    debugLogsLogicValues,
    debugLogsLogicActions,
    Record<string, any>,
    debugLogsLogicMeta
>

/**
 * Staff-only preference for whether `_posthog/console` debug rows surface in the run thread. Persisted
 * to localStorage and shared across every run surface (singleton, unkeyed), off by default — debug rows
 * render only after the user explicitly opts in. An impersonated session always forces debug logs on,
 * independent of the persisted toggle, so an engineer debugging a customer's session never has them
 * silently hidden.
 */
export const debugLogsLogic = kea<debugLogsLogicType>([
    path(['products', 'posthog_ai', 'frontend', 'logics', 'debugLogsLogic']),
    connect(() => ({
        values: [userLogic, ['user'], preflightLogic, ['isDev']],
    })),
    actions({
        setDebugLogsEnabled: (enabled: boolean) => ({ enabled }),
    }),
    reducers({
        debugLogsEnabled: [
            false,
            { persist: true, storageKey: 'posthog_ai.debugLogsOptIn' },
            {
                setDebugLogsEnabled: (_, { enabled }) => enabled,
            },
        ],
    }),
    selectors({
        /**
         * Who may see and toggle debug logs: staff or local dev, but never an impersonated session.
         * Impersonation force-shows debug logs unconditionally (see `showDebugLogs`), so surfacing a
         * toggle there would be a no-op that contradicts that invariant — exclude it here instead.
         */
        canControlDebugLogs: [
            (s) => [s.user, s.isDev],
            (user: null | import('~/types').UserType, isDev: boolean | undefined): boolean =>
                !user?.is_impersonated && (!!user?.is_staff || !!isDev),
        ],
        /**
         * Whether debug rows should actually render. Impersonation always shows them; otherwise staff/dev
         * see them only after opting in via the persisted toggle (off by default); everyone else never does.
         */
        showDebugLogs: [
            (s) => [s.user, s.canControlDebugLogs, s.debugLogsEnabled],
            (
                user: null | import('~/types').UserType,
                canControlDebugLogs: boolean,
                debugLogsEnabled: boolean
            ): boolean => !!user?.is_impersonated || (canControlDebugLogs && debugLogsEnabled),
        ],
    }),
])