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.5 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, listeners, path } from 'kea'
import { loaders } from 'kea-loaders'

import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'
import type { ProductSetupStatus } from 'lib/components/ProductEmptyState/types'
import { teamLogic } from 'scenes/teamLogic'

import { ProductKey } from '~/queries/schema/schema-general'

import { llmPromptsList } from 'products/ai_observability/frontend/generated/api'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmPromptsSetupLogicValues {
    setupStatus: ProductSetupStatus // productSetupStatusLogic
    currentProjectId: number | string // teamLogic
    promptCount: number | null
    promptCountLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmPromptsSetupLogicActions {
    setDetectedStatus: (status: ProductSetupStatus) => {
        status: ProductSetupStatus
    } // productSetupStatusLogic
    loadPromptCount: (_: void) => void
    loadPromptCountFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPromptCountSuccess: (
        promptCount: number,
        payload?: void
    ) => {
        promptCount: number
        payload?: void
    }
}

export type llmPromptsSetupLogicType = MakeLogicType<llmPromptsSetupLogicValues, llmPromptsSetupLogicActions>

/**
 * Setup detection for the prompts empty state. Prompts are a creation-first
 * product, so "set up" simply means the project has at least one prompt - no
 * event or traffic signal involved. Re-checks on every mount (the scene gate
 * mounts it), which covers returning from the prompt creation page.
 */
export const llmPromptsSetupLogic = kea<llmPromptsSetupLogicType>([
    path(['products', 'ai_observability', 'frontend', 'emptyState', 'llmPromptsSetupLogic']),
    connect(() => ({
        actions: [productSetupStatusLogic({ productKey: ProductKey.LLM_PROMPTS }), ['setDetectedStatus']],
        values: [
            productSetupStatusLogic({ productKey: ProductKey.LLM_PROMPTS }),
            ['status as setupStatus'],
            teamLogic,
            ['currentProjectId'],
        ],
    })),
    loaders(({ values }) => ({
        promptCount: [
            null as number | null,
            {
                loadPromptCount: async (_: void, breakpoint): Promise<number> => {
                    // content: 'none' skips prompt bodies, so the count probe stays cheap.
                    const response = await llmPromptsList(String(values.currentProjectId), {
                        limit: 1,
                        offset: 0,
                        content: 'none',
                    })
                    breakpoint()
                    return response.count
                },
            },
        ],
    })),
    listeners(({ actions, values }) => ({
        loadPromptCountSuccess: ({ promptCount }) => {
            actions.setDetectedStatus(promptCount !== null && promptCount > 0 ? 'has-data' : 'needs-setup')
        },
        loadPromptCountFailure: () => {
            // Never strand the gate on its spinner: if nothing has answered yet, fail
            // open to the real scene. Don't downgrade an existing answer on a blip.
            if (values.setupStatus === 'loading') {
                actions.setDetectedStatus('unknown')
            }
        },
    })),
    afterMount(({ actions }) => {
        actions.loadPromptCount()
    }),
])