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.

4.8 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'

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

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmEvaluationExecutionLogicValues {
    currentTeamId: number | null // teamLogic
    evaluationRun: {
        workflow_id: string
    } | null
    evaluationRunLoading: boolean
    lastRunWorkflowId: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmEvaluationExecutionLogicActions {
    runEvaluation: (
        evaluationId: string,
        targetEventId: string,
        timestamp: string,
        event: string,
        distinctId?: string
    ) => {
        distinctId: string | undefined
        evaluationId: string
        event: string
        targetEventId: string
        timestamp: string
    }
    runEvaluationFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    runEvaluationSuccess: (
        evaluationRun: {
            evaluation: {
                id: string
                name: string
            }
            status: string
            target_event_id: string
            workflow_id: string
        },
        payload?: {
            distinctId: string | undefined
            evaluationId: string
            event: string
            targetEventId: string
            timestamp: string
        }
    ) => {
        evaluationRun: {
            evaluation: {
                id: string
                name: string
            }
            status: string
            target_event_id: string
            workflow_id: string
        }
        payload?: {
            distinctId: string | undefined
            evaluationId: string
            event: string
            targetEventId: string
            timestamp: string
        }
    }
}

export type llmEvaluationExecutionLogicType = MakeLogicType<
    llmEvaluationExecutionLogicValues,
    llmEvaluationExecutionLogicActions
>

export const llmEvaluationExecutionLogic = kea<llmEvaluationExecutionLogicType>([
    path(['products', 'ai_observability', 'frontend', 'llmEvaluationExecutionLogic']),
    connect(() => ({
        values: [teamLogic, ['currentTeamId']],
    })),
    actions({
        runEvaluation: (
            evaluationId: string,
            targetEventId: string,
            timestamp: string,
            event: string,
            distinctId?: string
        ) => ({
            evaluationId,
            targetEventId,
            timestamp,
            event,
            distinctId,
        }),
    }),
    loaders(({ values }) => ({
        evaluationRun: [
            null as { workflow_id: string } | null,
            {
                runEvaluation: async ({ evaluationId, targetEventId, timestamp, event, distinctId }) => {
                    if (!values.currentTeamId) {
                        throw new Error('No team selected')
                    }

                    try {
                        const payload: {
                            evaluation_id: string
                            target_event_id: string
                            timestamp: string
                            event: string
                            distinct_id?: string
                        } = {
                            evaluation_id: evaluationId,
                            target_event_id: targetEventId,
                            timestamp: timestamp,
                            event: event,
                        }

                        if (distinctId) {
                            payload.distinct_id = distinctId
                        }

                        const response = await api.evaluationRuns.create(payload)

                        lemonToast.success('Evaluation started successfully')
                        return response
                    } catch (error) {
                        // `initKea`'s loader handler renders `detail` or `statusText`, so toasting
                        // here as well would repeat it. A reply carrying neither leaves that
                        // handler silent, which is the case this covers.
                        if (!(error instanceof ApiError) || !(error.detail || error.statusText)) {
                            lemonToast.error('Failed to start evaluation')
                        }
                        throw error
                    }
                },
            },
        ],
    })),
    reducers({
        lastRunWorkflowId: [
            null as string | null,
            {
                runEvaluationSuccess: (_, { evaluationRun }) => evaluationRun?.workflow_id || null,
            },
        ],
    }),
])