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.3 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 { llmSkillsList } from 'products/skills/frontend/generated/api'

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

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmSkillsSetupLogicActions {
    setDetectedStatus: (status: ProductSetupStatus) => {
        status: ProductSetupStatus
    } // productSetupStatusLogic
    loadSkillCount: (_: void) => void
    loadSkillCountFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSkillCountSuccess: (
        skillCount: number,
        payload?: void
    ) => {
        skillCount: number
        payload?: void
    }
}

export type llmSkillsSetupLogicType = MakeLogicType<llmSkillsSetupLogicValues, llmSkillsSetupLogicActions>

/**
 * Setup detection for the skills empty state. Skills are a creation-first
 * product, so "set up" simply means the project has at least one skill in any
 * category - no event or traffic signal involved. Re-checks on every mount (the
 * scene gate mounts it), which covers returning from the skill creation page.
 */
export const llmSkillsSetupLogic = kea<llmSkillsSetupLogicType>([
    path(['products', 'skills', 'frontend', 'emptyState', 'llmSkillsSetupLogic']),
    connect(() => ({
        actions: [productSetupStatusLogic({ productKey: ProductKey.SKILLS }), ['setDetectedStatus']],
        values: [
            productSetupStatusLogic({ productKey: ProductKey.SKILLS }),
            ['status as setupStatus'],
            teamLogic,
            ['currentProjectId'],
        ],
    })),
    loaders(({ values }) => ({
        skillCount: [
            null as number | null,
            {
                loadSkillCount: async (_: void, breakpoint): Promise<number> => {
                    // No category filter, so skills of every category count as data.
                    const response = await llmSkillsList(String(values.currentProjectId), { limit: 1, offset: 0 })
                    breakpoint()
                    return response.count
                },
            },
        ],
    })),
    listeners(({ actions, values }) => ({
        loadSkillCountSuccess: ({ skillCount }) => {
            actions.setDetectedStatus(skillCount !== null && skillCount > 0 ? 'has-data' : 'needs-setup')
        },
        loadSkillCountFailure: () => {
            // 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.loadSkillCount()
    }),
])