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.7 KB
Raw Blame
import { MakeLogicType, actions, afterMount, kea, key, listeners, path, props, reducers } from 'kea'

import type { EvaluationRun } from './evaluations/types'
import { queryEvaluationRuns } from './utils'

// Session verdicts land after the session settles, up to the 7-day session max age. This is
// generously past both that and ai_events retention, and exists so the query prunes partitions at
// all — without it every session page view scans the team's whole $ai_evaluation history.
const SESSION_EVALUATION_LOOKBACK_DAYS = 90

export interface SessionEvaluationsLogicProps {
    sessionId: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface aiObservabilitySessionEvaluationsLogicValues {
    sessionEvaluations: EvaluationRun[]
    sessionEvaluationsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface aiObservabilitySessionEvaluationsLogicActions {
    loadSessionEvaluations: () => {
        value: true
    }
    loadSessionEvaluationsFailure: () => {
        value: true
    }
    loadSessionEvaluationsSuccess: (evaluations: EvaluationRun[]) => {
        evaluations: EvaluationRun[]
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface aiObservabilitySessionEvaluationsLogicMeta {
    key: string
}

export type aiObservabilitySessionEvaluationsLogicType = MakeLogicType<
    aiObservabilitySessionEvaluationsLogicValues,
    aiObservabilitySessionEvaluationsLogicActions,
    SessionEvaluationsLogicProps,
    aiObservabilitySessionEvaluationsLogicMeta
>

export const aiObservabilitySessionEvaluationsLogic = kea<aiObservabilitySessionEvaluationsLogicType>([
    path(['scenes', 'ai-observability', 'aiObservabilitySessionEvaluationsLogic']),
    props({} as SessionEvaluationsLogicProps),
    key((props) => props.sessionId),

    actions({
        loadSessionEvaluations: true,
        loadSessionEvaluationsSuccess: (evaluations: EvaluationRun[]) => ({ evaluations }),
        loadSessionEvaluationsFailure: true,
    }),

    reducers({
        sessionEvaluations: [
            [] as EvaluationRun[],
            {
                loadSessionEvaluationsSuccess: (_, { evaluations }) => evaluations,
                loadSessionEvaluationsFailure: () => [],
            },
        ],
        sessionEvaluationsLoading: [
            false,
            {
                loadSessionEvaluations: () => true,
                loadSessionEvaluationsSuccess: () => false,
                loadSessionEvaluationsFailure: () => false,
            },
        ],
    }),

    listeners(({ actions, props }) => ({
        loadSessionEvaluations: async () => {
            const { sessionId } = props
            if (!sessionId) {
                actions.loadSessionEvaluationsFailure()
                return
            }

            // Session verdicts carry no $ai_trace_id, so SessionQueryRunner's `trace_id != ''`
            // filter drops them; they have to be read by $ai_session_id. Going through the shared
            // query keeps the row shape, the result normalizer and the badge components identical
            // to the trace and generation surfaces.
            try {
                const runs = await queryEvaluationRuns({
                    sessionId,
                    lookbackDays: SESSION_EVALUATION_LOOKBACK_DAYS,
                })
                actions.loadSessionEvaluationsSuccess(runs)
            } catch (error) {
                console.error('Error loading session evaluations:', error)
                actions.loadSessionEvaluationsFailure()
            }
        },
    })),

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