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.

10.6 KB
Raw Blame
import type { Monaco } from '@monaco-editor/react'
import {
    MakeLogicType,
    actions,
    connect,
    kea,
    key,
    listeners,
    path,
    props,
    propsChanged,
    reducers,
    selectors,
} from 'kea'
import { combineUrl } from 'kea-router'
// Note: we can only import types and not values from monaco-editor, because otherwise some Monaco code breaks
// auto reload in development. Specifically, on this line:
// `export const suggestWidgetStatusbarMenu = new MenuId('suggestWidgetStatusBar')`
// `new MenuId('suggestWidgetStatusBar')` causes the app to crash, because it cannot be called twice in the same
// JS context, and that's exactly what happens on auto-reload when the new script chunks are loaded. Unfortunately
// esbuild doesn't support manual chunks as of 2023, so we can't just put Monaco in its own chunk, which would prevent
// re-importing. As for @monaco-editor/react, it does some lazy loading and doesn't have this problem.
import type { editor } from 'monaco-editor'

import { LemonDialog, LemonInput } from '@posthog/lemon-ui'

import api from 'lib/api'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { preflightLogic } from 'lib/logic/preflightLogic'
import { dataWarehouseViewsLogic } from 'scenes/data-warehouse/saved_queries/dataWarehouseViewsLogic'
import { validateSavedQueryName } from 'scenes/data-warehouse/saved_queries/savedQueryNameValidation'
import { dataWarehouseSettingsSceneLogic } from 'scenes/data-warehouse/settings/dataWarehouseSettingsSceneLogic'
import { teamLogic } from 'scenes/teamLogic'

import { DataNode, HogQLQuery, NodeKind } from '~/queries/schema/schema-general'

import type { FeatureFlagsSet } from '../../../lib/logic/featureFlagLogic'
import type { DataWarehouseSavedQuery } from '../../../types'

export interface HogQLQueryEditorLogicProps {
    key: string | number
    query: HogQLQuery
    setQuery?: (query: HogQLQuery) => void
    onChange?: (query: string) => void
    monaco?: Monaco | null
    editor?: editor.IStandaloneCodeEditor | null
    metadataSource?: DataNode
    queryResponse?: Record<string, any>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogQLQueryEditorLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    currentProjectId: number | string // teamLogic
    aiAvailable: boolean | undefined
    prompt: string
    promptError: string | null
    promptLoading: boolean
    queryInput: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogQLQueryEditorLogicActions {
    updateView: (
        query: string,
        types: string[][]
    ) => {
        query: string
        types: string[][]
    } // dataWarehouseSettingsSceneLogic
    createDataWarehouseSavedQuery: (
        payload: Partial<DataWarehouseSavedQuery> & {
            deferCreatedToast?: boolean
            folder_id?: string | null
            types: string[][]
        }
    ) => Partial<DataWarehouseSavedQuery> & {
        deferCreatedToast?: boolean
        folder_id?: string | null
        types: string[][]
    } // dataWarehouseViewsLogic
    draftFromMetadataFix: (prompt: string) => {
        prompt: string
    }
    draftFromPrompt: () => {
        value: true
    }
    draftFromPromptComplete: () => {
        value: true
    }
    onUpdateView: () => {
        value: true
    }
    saveAsView: () => {
        value: true
    }
    saveAsViewSuccess: (name: string) => {
        name: string
    }
    saveQuery: (queryOverride?: string) => {
        queryOverride: string | undefined
    }
    setPrompt: (prompt: string) => {
        prompt: string
    }
    setPromptError: (error: string | null) => {
        error: string | null
    }
    setQueryInput: (queryInput: string) => {
        queryInput: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface hogQLQueryEditorLogicMeta {
    key: number | string
    __keaTypeGenInternalSelectorTypes: {
        aiAvailable: (preflight: null | import('../../../types').PreflightStatus) => boolean | undefined
    }
}

export type hogQLQueryEditorLogicType = MakeLogicType<
    hogQLQueryEditorLogicValues,
    hogQLQueryEditorLogicActions,
    HogQLQueryEditorLogicProps,
    hogQLQueryEditorLogicMeta
>

export const hogQLQueryEditorLogic = kea<hogQLQueryEditorLogicType>([
    path(['queries', 'nodes', 'HogQLQuery', 'hogQLQueryEditorLogic']),
    props({} as HogQLQueryEditorLogicProps),
    key((props) => props.key),
    propsChanged(({ actions, props }, oldProps) => {
        const selection = props.editor?.getSelection()
        const model = props.editor?.getModel()
        const highlightedQuery = selection && model ? model.getValueInRange(selection) : null

        if (highlightedQuery && props.query.query === highlightedQuery) {
            return
        }

        if (
            typeof props.query.query === 'string' &&
            (props.query.query !== oldProps.query.query || props.editor !== oldProps.editor)
        ) {
            actions.setQueryInput(props.query.query)
        }
    }),
    connect(() => ({
        values: [featureFlagLogic, ['featureFlags'], teamLogic, ['currentProjectId']],
        actions: [
            dataWarehouseViewsLogic,
            ['createDataWarehouseSavedQuery'],
            dataWarehouseSettingsSceneLogic,
            ['updateView'],
        ],
    })),
    actions({
        saveQuery: (queryOverride?: string) => ({ queryOverride }),
        setQueryInput: (queryInput: string) => ({ queryInput }),
        setPrompt: (prompt: string) => ({ prompt }),
        setPromptError: (error: string | null) => ({ error }),
        draftFromPrompt: true,
        draftFromMetadataFix: (prompt: string) => ({ prompt }),
        draftFromPromptComplete: true,
        saveAsView: true,
        saveAsViewSuccess: (name: string) => ({ name }),
        onUpdateView: true,
    }),
    reducers(({ props }) => ({
        queryInput: [
            typeof props.query.query === 'string' ? props.query.query : '',
            { setQueryInput: (_, { queryInput }) => queryInput },
        ],
        prompt: ['', { setPrompt: (_, { prompt }) => prompt }],
        promptError: [
            null as string | null,
            { setPromptError: (_, { error }) => error, draftFromPrompt: () => null, saveQuery: () => null },
        ],
        promptLoading: [
            false,
            { draftFromPrompt: () => true, draftFromMetadataFix: () => true, draftFromPromptComplete: () => false },
        ],
    })),
    selectors({
        aiAvailable: [
            () => [preflightLogic.selectors.preflight],
            (preflight: null | import('../../../types').PreflightStatus) => preflight?.openai_available,
        ],
    }),
    listeners(({ actions, props, values }) => ({
        saveQuery: ({ queryOverride }) => {
            const query = values.queryInput
            // TODO: Is below line necessary if the only way for queryInput to change is already through setQueryInput?
            actions.setQueryInput(query)

            props.setQuery?.({
                ...props.query,
                query: queryOverride ?? query,
                variables: Object.fromEntries(
                    Object.entries(props.query.variables ?? {}).filter(([_, variable]) =>
                        query.includes(`{variables.${variable.code_name}}`)
                    )
                ),
            })
        },
        setQueryInput: async ({ queryInput }) => {
            props.onChange?.(queryInput)
        },
        draftFromPrompt: async () => {
            if (!values.aiAvailable) {
                throw new Error(
                    'To use AI features, configure environment variable OPENAI_API_KEY for this instance of PostHog'
                )
            }
            try {
                // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
                const result = await api.get(
                    combineUrl(`api/projects/${values.currentProjectId}/query/draft_sql/`, {
                        prompt: values.prompt,
                        current_query: values.queryInput,
                    }).url
                )
                const { sql } = result
                actions.setQueryInput(sql)
            } catch (e) {
                actions.setPromptError((e as { code: string; detail: string }).detail)
            } finally {
                actions.draftFromPromptComplete()
            }
        },
        draftFromMetadataFix: async ({ prompt }) => {
            if (!values.aiAvailable) {
                throw new Error(
                    'To use AI features, configure environment variable OPENAI_API_KEY for this instance of PostHog'
                )
            }
            try {
                // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
                const result = await api.get(
                    combineUrl(`api/projects/@current/query/draft_sql/`, {
                        prompt,
                        current_query: values.queryInput,
                    }).url
                )
                const { sql } = result
                actions.setQueryInput(sql)
            } catch (e) {
                actions.setPromptError((e as { code: string; detail: string }).detail)
            } finally {
                actions.draftFromPromptComplete()
            }
        },
        saveAsView: async () => {
            LemonDialog.openForm({
                title: 'Save as view',
                initialValues: { viewName: '' },
                content: (
                    <LemonField name="viewName">
                        <LemonInput placeholder="Please enter the name of the view" autoFocus />
                    </LemonField>
                ),
                errors: {
                    viewName: validateSavedQueryName,
                },
                onSubmit: ({ viewName }) => actions.saveAsViewSuccess(viewName),
            })
        },
        saveAsViewSuccess: async ({ name }) => {
            const query: HogQLQuery = {
                kind: NodeKind.HogQLQuery,
                query: values.queryInput,
            }
            const types = props.queryResponse?.types ?? []

            await dataWarehouseViewsLogic.asyncActions.createDataWarehouseSavedQuery({ name, query, types })
        },
        onUpdateView: async () => {
            const types = props.queryResponse?.types ?? []
            actions.updateView(values.queryInput, types)
        },
    })),
])