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

// The optimistic run opened on send, before the task/run exist. `streamKey` is the client key the pending
// `RunSurface` (and its seeded `runStreamLogic`) bind to; `taskId`/`runId` are filled once known (reserved
// for a future zero-flash in-place handoff — today the scene navigates to the detail page once the run exists).
export interface ActiveCreation {
    streamKey: string
    interactionKey?: string
    composerWasFocused?: boolean
    taskId?: string
    runId?: string
    draft?: string
}

// `panelId` is set only by an embedded instance (e.g. Max's side panel runner), which mounts this logic
// under its own key rather than a scene's default singleton. Pass the same `panelId` a paired
// `taskTrackerSceneLogic` instance uses so the two stay keyed together.
export interface RunnerPanelLogicProps {
    panelId?: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface runnerPanelLogicValues {
    activeCreation: ActiveCreation | null
    cameFromHistory: boolean
    canGoBack: boolean
    historyExpanded: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface runnerPanelLogicActions {
    clearActiveCreation: () => {
        value: true
    }
    goBack: () => {
        value: true
    }
    handOffToClassic: (prompt: string) => {
        prompt: string
    }
    setActiveCreation: (creation: ActiveCreation) => {
        creation: ActiveCreation
    }
    setCameFromHistory: (cameFromHistory: boolean) => {
        cameFromHistory: boolean
    }
    setHistoryExpanded: (expanded: boolean) => {
        expanded: boolean
    }
    setStartupDraft: (draft: string) => {
        draft: string
    }
    toggleHistory: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface runnerPanelLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        canGoBack: (activeCreation: ActiveCreation | null, historyExpanded: boolean) => boolean
    }
}

export type runnerPanelLogicType = MakeLogicType<
    runnerPanelLogicValues,
    runnerPanelLogicActions,
    RunnerPanelLogicProps,
    runnerPanelLogicMeta
>

export const runnerPanelLogic = kea<runnerPanelLogicType>([
    path(['layout', 'ai', 'runnerPanelLogic']),
    props({} as RunnerPanelLogicProps),
    // No `panelId` resolves to the same 'scene' key `taskTrackerSceneLogic` falls back to, so an unkeyed
    // caller pairs with the scene's own singleton.
    key((props) => props.panelId ?? 'scene'),

    actions({
        setActiveCreation: (creation: ActiveCreation) => ({ creation }),
        setStartupDraft: (draft: string) => ({ draft }),
        clearActiveCreation: true,
        toggleHistory: true,
        setHistoryExpanded: (expanded: boolean) => ({ expanded }),
        goBack: true,
        setCameFromHistory: (cameFromHistory: boolean) => ({ cameFromHistory }),
        // The server refused a 2.0 send, and the host is about to swap this panel to classic PostHog AI.
        // The host gives the prompt to classic as its draft.
        handOffToClassic: (prompt: string) => ({ prompt }),
    }),

    reducers({
        // The in-flight optimistic create. While set (and no task is selected) the panel shows the pending
        // run thread instead of the composer.
        activeCreation: [
            null as ActiveCreation | null,
            {
                setActiveCreation: (state, { creation }) =>
                    state?.streamKey === creation.streamKey ? { ...state, ...creation } : creation,
                setStartupDraft: (state, { draft }) => (state ? { ...state, draft } : state),
                clearActiveCreation: () => null,
            },
        ],
        // Whether the panel is showing the full task history list instead of the composer/run.
        historyExpanded: [
            false,
            {
                toggleHistory: (state) => !state,
                setHistoryExpanded: (_, { expanded }) => expanded,
            },
        ],
        // Whether the currently active creation was opened from the expanded history view, so `goBack`
        // knows to return there instead of falling through to the composer — mirrors legacy Max's
        // `backToScreen: 'history'`.
        cameFromHistory: [
            false,
            {
                setCameFromHistory: (_, { cameFromHistory }) => cameFromHistory,
            },
        ],
    }),

    selectors({
        canGoBack: [
            (s) => [s.activeCreation, s.historyExpanded],
            (activeCreation: ActiveCreation | null, historyExpanded: boolean): boolean =>
                !!activeCreation || historyExpanded,
        ],
    }),

    listeners(({ actions, values }) => ({
        // Opening a task (from the composer or from history) collapses the history list so the run takes
        // over the panel, and remembers whether it came from there — read-then-clear works because
        // reducers for this same dispatch run before listeners.
        setActiveCreation: () => {
            if (values.historyExpanded) {
                actions.setCameFromHistory(true)
                actions.setHistoryExpanded(false)
            } else {
                actions.setCameFromHistory(false)
            }
        },
        // Back walks run -> history-or-composer -> composer.
        goBack: () => {
            if (values.activeCreation) {
                actions.clearActiveCreation()
                if (values.cameFromHistory) {
                    actions.setHistoryExpanded(true)
                }
            } else if (values.historyExpanded) {
                actions.setHistoryExpanded(false)
            }
        },
    })),
])