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.

5.2 KB
Raw Blame
import { MakeLogicType, afterMount, kea, path } from 'kea'
import { loaders } from 'kea-loaders'

import { ApiConfig } from '~/lib/api'
import { urls } from '~/scenes/urls'

import { llmSkillsList } from 'products/skills/frontend/generated/api'

/** URL/UI key for the default tab — the uncategorized skills (everything not pulled into a category tab). */
export const DEFAULT_SKILLS_TAB_KEY: string = 'skills'

/** Sub-title shown for the default "Skills" tab. Category tabs carry their own copy (below). */
export const DEFAULT_SKILLS_TAB_DESCRIPTION = 'Manage skills that are not assigned to a product category.'

export interface SkillCategoryTab {
    /** Tab key, also the `/skills/<key>` URL segment. */
    key: string
    /** The `LLMSkill.category` value this tab filters to. */
    category: string
    label: string
    /** Sub-title shown under the scene title while this tab is active. */
    description: string
}

/**
 * Category-backed tabs shown alongside the default "Skills" tab. Each entry surfaces one
 * `LLMSkill.category` value as its own tab and removes it from the default list (which only shows
 * uncategorized skills). To add a tab — e.g. official AI-plugin skills — add an entry here, register
 * the matching `/skills/<key>` route in manifest.tsx, and have the producer stamp that `category`.
 */
export const SKILL_CATEGORY_TABS: SkillCategoryTab[] = [
    {
        key: 'scouts',
        category: 'scout',
        label: 'Scout skills',
        description:
            "Scout skills define scheduled agents that scan your project and surface findings in your inbox. This includes PostHog's canonical scouts and custom scouts.",
    },
    {
        key: 'review-hog',
        category: 'review_hog',
        label: 'Code review skills',
        description:
            'Code review skills define the specialist perspectives and validation criteria that PostHog Review uses when it reviews a pull request. Edit a skill to tune it for your team.',
    },
]

export function skillCategoryForTabKey(tabKey: string): string {
    return SKILL_CATEGORY_TABS.find((tab) => tab.key === tabKey)?.category ?? ''
}

export function skillTabDescription(tabKey: string): string {
    return SKILL_CATEGORY_TABS.find((tab) => tab.key === tabKey)?.description ?? DEFAULT_SKILLS_TAB_DESCRIPTION
}

/** The `/skills` or `/skills/<categoryKey>` path for a tab key. */
export function skillTabUrl(tabKey: string): string {
    return tabKey === DEFAULT_SKILLS_TAB_KEY ? urls.skills() : urls.skillsCategoryTab(tabKey)
}

/** A category tab is shown only once the team has at least one skill in that category. The active
 * tab is always kept visible so direct navigation to /skills/<key> (or deleting the last skill while
 * viewing the tab) doesn't strand the user on a tab that isn't listed. */
export function visibleCategoryTabs(categoryCounts: Record<string, number>, activeTabKey: string): SkillCategoryTab[] {
    return SKILL_CATEGORY_TABS.filter((tab) => (categoryCounts[tab.category] ?? 0) > 0 || tab.key === activeTabKey)
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface skillTabsLogicValues {
    categoryCounts: Record<string, number>
    categoryCountsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface skillTabsLogicActions {
    loadCategoryCounts: () => any
    loadCategoryCountsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadCategoryCountsSuccess: (
        categoryCounts: {
            [k: string]: number
        },
        payload?: any
    ) => {
        categoryCounts: {
            [k: string]: number
        }
        payload?: any
    }
}

export type skillTabsLogicType = MakeLogicType<skillTabsLogicValues, skillTabsLogicActions>

/**
 * Which category tabs the skills tab bar shows. Kept apart from `llmSkillsLogic` so the Community
 * scene can render the same tab bar without mounting the skills list and its loaders.
 */
export const skillTabsLogic = kea<skillTabsLogicType>([
    path(['scenes', 'skills', 'skillTabsLogic']),

    loaders({
        // Per-category skill counts, used to decide which category tabs to show. Loaded once on
        // mount (and after a delete) with a cheap limit=1 probe per category — we only read `count`.
        categoryCounts: [
            {} as Record<string, number>,
            {
                loadCategoryCounts: async () => {
                    const teamId = String(ApiConfig.getCurrentTeamId())
                    const entries = await Promise.all(
                        SKILL_CATEGORY_TABS.map(async (tab) => {
                            const { count } = await llmSkillsList(teamId, {
                                category: tab.category,
                                limit: 1,
                                offset: 0,
                            })
                            return [tab.category, count] as const
                        })
                    )
                    return Object.fromEntries(entries)
                },
            },
        ],
    }),

    afterMount(({ actions }) => {
        actions.loadCategoryCounts()
    }),
])