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.

7.4 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'

import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'

import type { ClusteringJob } from './types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clusteringJobsLogicValues {
    currentTeamIdStrict: number | string // teamLogic
    editingJob: Partial<ClusteringJob> | null
    isJobsPanelOpen: boolean
    jobs: ClusteringJob[]
    jobsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clusteringJobsLogicActions {
    closeJobsPanel: () => {
        value: true
    }
    createJob: (payload: Partial<ClusteringJob>) => Partial<ClusteringJob>
    createJobFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    createJobSuccess: (
        jobs: ClusteringJob[],
        payload?: Partial<ClusteringJob>
    ) => {
        jobs: ClusteringJob[]
        payload?: Partial<ClusteringJob>
    }
    deleteJob: (jobId: number) => {
        jobId: number
    }
    loadJobs: () => any
    loadJobsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadJobsSuccess: (
        jobs: ClusteringJob[],
        payload?: any
    ) => {
        jobs: ClusteringJob[]
        payload?: any
    }
    openJobsPanel: () => {
        value: true
    }
    setEditingJob: (job: Partial<ClusteringJob> | null) => {
        job: Partial<ClusteringJob> | null
    }
    updateJob: (
        payload: Partial<ClusteringJob> & {
            id: number
        }
    ) => Partial<ClusteringJob> & {
        id: number
    }
    updateJobFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    updateJobSuccess: (
        jobs: ClusteringJob[],
        payload?: Partial<ClusteringJob> & {
            id: number
        }
    ) => {
        jobs: ClusteringJob[]
        payload?: Partial<ClusteringJob> & {
            id: number
        }
    }
}

export type clusteringJobsLogicType = MakeLogicType<clusteringJobsLogicValues, clusteringJobsLogicActions>

export const clusteringJobsLogic = kea<clusteringJobsLogicType>([
    path(['products', 'ai_observability', 'frontend', 'clusters', 'clusteringJobsLogic']),
    connect(() => ({ values: [teamLogic, ['currentTeamIdStrict']] })),

    actions({
        openJobsPanel: true,
        closeJobsPanel: true,
        setEditingJob: (job: Partial<ClusteringJob> | null) => ({ job }),
        deleteJob: (jobId: number) => ({ jobId }),
    }),

    loaders(({ values }) => ({
        jobs: [
            [] as ClusteringJob[],
            {
                loadJobs: async () => {
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use llmAnalyticsClusteringJobsList() from 'products/ai_observability/frontend/generated/api' instead.
                    const response = await api.get(
                        `api/projects/${values.currentTeamIdStrict}/llm_analytics/clustering_jobs/`
                    )
                    return (response.results ?? response) as ClusteringJob[]
                },
                createJob: async (payload: Partial<ClusteringJob>) => {
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use llmAnalyticsClusteringJobsCreate() from 'products/ai_observability/frontend/generated/api' instead.
                    await api.create(
                        `api/projects/${values.currentTeamIdStrict}/llm_analytics/clustering_jobs/`,
                        payload
                    )
                    lemonToast.success('Clustering job created')
                    // Reload to get server-assigned fields
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use llmAnalyticsClusteringJobsList() from 'products/ai_observability/frontend/generated/api' instead.
                    const response = await api.get(
                        `api/projects/${values.currentTeamIdStrict}/llm_analytics/clustering_jobs/`
                    )
                    return (response.results ?? response) as ClusteringJob[]
                },
                updateJob: async (payload: Partial<ClusteringJob> & { id: number }) => {
                    const { id, ...data } = payload
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use llmAnalyticsClusteringJobsPartialUpdate() from 'products/ai_observability/frontend/generated/api' instead.
                    await api.update(
                        `api/projects/${values.currentTeamIdStrict}/llm_analytics/clustering_jobs/${id}/`,
                        data
                    )
                    lemonToast.success('Clustering job updated')
                    // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use llmAnalyticsClusteringJobsList() from 'products/ai_observability/frontend/generated/api' instead.
                    const response = await api.get(
                        `api/projects/${values.currentTeamIdStrict}/llm_analytics/clustering_jobs/`
                    )
                    return (response.results ?? response) as ClusteringJob[]
                },
            },
        ],
    })),

    reducers({
        isJobsPanelOpen: [
            false,
            {
                openJobsPanel: () => true,
                closeJobsPanel: () => false,
            },
        ],
        editingJob: [
            null as Partial<ClusteringJob> | null,
            {
                setEditingJob: (_, { job }) => job,
                closeJobsPanel: () => null,
            },
        ],
    }),

    listeners(({ actions, values }) => ({
        openJobsPanel: () => {
            posthog.capture('llma clustering jobs panel opened')
        },
        deleteJob: async ({ jobId }) => {
            try {
                // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. llmAnalyticsClusteringJobsDestroy() from 'products/ai_observability/frontend/generated/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
                await api.delete(`api/projects/${values.currentTeamIdStrict}/llm_analytics/clustering_jobs/${jobId}/`)
                lemonToast.success('Clustering job deleted')
                posthog.capture('llma clustering job deleted', { job_id: jobId })
                actions.loadJobs()
            } catch {
                lemonToast.error('Failed to delete clustering job')
            }
        },
        createJobSuccess: ({ jobs }) => {
            posthog.capture('llma clustering job created', { total_jobs_count: jobs.length })
            actions.setEditingJob(null)
        },
        updateJobSuccess: () => {
            posthog.capture('llma clustering job updated')
            actions.setEditingJob(null)
        },
    })),

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