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.

2.9 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers } from 'kea'

import { MAX_SIDE_PANEL_ID } from '~/layout/ai/aiPanelIds'
import { type ActiveCreation, runnerPanelLogic } from '~/layout/ai/runnerPanelLogic'

import { TodayPanelChatTask, runnerCreationFor } from './todayPanelChat'
import { projectSessionKey, readSessionValue, writeSessionValue } from './todaySessionStorage'

const CHAT_SESSION_KEY = 'today-context-panel-chat'

function chatOf(creation: ActiveCreation | null): TodayPanelChatTask | null {
    return creation?.taskId && creation.runId ? { taskId: creation.taskId, runId: creation.runId } : null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayPanelChatSessionLogicValues {
    activeCreation: ActiveCreation | null // runnerPanelLogic
    previousChatsHintQueued: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayPanelChatSessionLogicActions {
    clearActiveCreation: () => {
        value: true
    } // runnerPanelLogic
    setActiveCreation: (creation: ActiveCreation) => {
        creation: ActiveCreation
    } // runnerPanelLogic
    previousChatsHintShown: () => {
        value: true
    }
    queuePreviousChatsHint: () => {
        value: true
    }
}

export type todayPanelChatSessionLogicType = MakeLogicType<
    todayPanelChatSessionLogicValues,
    todayPanelChatSessionLogicActions
>

/**
 * Keeps the PostHog AI panel's chat across a reload of this tab. Mounted for the whole session, so it also keeps the
 * panel's runner state alive while a chat moves off /ai and the page changes.
 */
export const todayPanelChatSessionLogic = kea<todayPanelChatSessionLogicType>([
    path(['layout', 'today', 'todayPanelChatSessionLogic']),
    connect(() => ({
        values: [runnerPanelLogic({ panelId: MAX_SIDE_PANEL_ID }), ['activeCreation']],
        actions: [runnerPanelLogic({ panelId: MAX_SIDE_PANEL_ID }), ['setActiveCreation', 'clearActiveCreation']],
    })),
    actions({
        queuePreviousChatsHint: true,
        previousChatsHintShown: true,
    }),
    reducers({
        // Kept here, not on the panel, because the panel can still be closed when a new chat replaces the open one.
        previousChatsHintQueued: [
            false,
            {
                queuePreviousChatsHint: () => true,
                previousChatsHintShown: () => false,
            },
        ],
    }),
    listeners(({ values }) => ({
        setActiveCreation: () => writeSessionValue(projectSessionKey(CHAT_SESSION_KEY), chatOf(values.activeCreation)),
        clearActiveCreation: () => writeSessionValue(projectSessionKey(CHAT_SESSION_KEY), null),
    })),
    afterMount(({ actions }) => {
        const saved = readSessionValue<TodayPanelChatTask>(projectSessionKey(CHAT_SESSION_KEY))
        if (saved) {
            actions.setActiveCreation(runnerCreationFor(saved))
        }
    }),
])