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

import { REPORT_AI_PANEL, SCOUT_AI_PANEL, SIDE_PANEL_PANEL_ID } from '~/layout/ai/aiPanelIds'
import { runnerPanelLogic } from '~/layout/ai/runnerPanelLogic'
import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'
import { SidePanelTab } from '~/types'

import { ComposerSeed, composerSeedLogic } from 'products/posthog_ai/frontend/api/logics'

import { maxGlobalLogic } from './maxGlobalLogic'
import { maxLogic, parseCommandString } from './maxLogic'

export interface PhaiSidePanelComposerSeedLogicProps {
    /** The embedded composer's panel key — the same one passed to its `taskTrackerSceneLogic`/`composerSeedLogic`. */
    panelId: string
}

// Parse the side-panel option string exactly as legacy Max does (`mode=` stripped, leading `!` = auto-run)
// and forward the prompt to the surface seam. Reusing `parseCommandString` keeps the `!` convention
// byte-for-byte identical to the legacy consumer; only the prompt + auto-submit flag cross into the surface.
function forwardSeed(
    options: string | null | undefined,
    dataProcessingAccepted: boolean,
    setSeed: (seed: ComposerSeed) => void
): void {
    if (typeof options !== 'string' || options === REPORT_AI_PANEL || options === SCOUT_AI_PANEL) {
        return
    }
    const { autoRun, question } = parseCommandString(options)
    if (!question) {
        return
    }
    // Same org-level AI data-processing consent gate as the legacy `askMax` path: without approval an
    // auto-run seed only prefills the composer — it never starts an agent run. The user's own send then
    // goes through the composer's consent flow.
    setSeed({ prompt: question, autoSubmit: autoRun && dataProcessingAccepted })
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface phaiSidePanelComposerSeedLogicValues {
    dataProcessingAccepted: boolean // maxGlobalLogic
    selectedTab: SidePanelTab | null // sidePanelStateLogic
    selectedTabOptions: string | null // sidePanelStateLogic
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface phaiSidePanelComposerSeedLogicActions {
    setSeed: (seed: ComposerSeed) => {
        seed: ComposerSeed
    } // composerSeedLogic
    handOffToClassic: (prompt: string) => {
        prompt: string
    } // runnerPanelLogic
}

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

export type phaiSidePanelComposerSeedLogicType = MakeLogicType<
    phaiSidePanelComposerSeedLogicValues,
    phaiSidePanelComposerSeedLogicActions,
    PhaiSidePanelComposerSeedLogicProps,
    phaiSidePanelComposerSeedLogicMeta
>

/**
 * Bridges the legacy `initialMaxPrompt` side-panel option into the new PostHog AI composer. `openMax` (and any
 * other producer) still rides the prompt on `openSidePanel(SidePanelTab.Max, prompt)` — the option only the
 * legacy `maxLogic` used to read. Mounted by the new view's panel (`PhaiSidePanelChat`), this reads the same
 * option and seeds the surface's `composerSeedLogic`, so a single consumer fixes every producer without
 * touching `openMax` or the legacy consumption path. The two orderings both hold: a CTA that fires before this
 * panel mounts leaves the option on `sidePanelStateLogic` for the afterMount read; a CTA fired while the panel
 * is already open is caught by the `openSidePanel` listener.
 */
export const phaiSidePanelComposerSeedLogic = kea<phaiSidePanelComposerSeedLogicType>([
    path(['scenes', 'max', 'phaiSidePanelComposerSeedLogic']),
    props({} as PhaiSidePanelComposerSeedLogicProps),
    key((props) => props.panelId),

    connect((props: PhaiSidePanelComposerSeedLogicProps) => ({
        values: [
            sidePanelStateLogic,
            ['selectedTab', 'selectedTabOptions'],
            maxGlobalLogic,
            ['dataProcessingAccepted'],
        ],
        actions: [
            composerSeedLogic({ panelId: props.panelId }),
            ['setSeed'],
            runnerPanelLogic({ panelId: props.panelId }),
            ['handOffToClassic'],
        ],
    })),

    listeners(({ actions, values }) => ({
        // The panel swaps to the legacy chat next, and that chat reads its draft from the side panel `maxLogic`.
        handOffToClassic: ({ prompt }) => {
            maxLogic.findMounted({ panelId: SIDE_PANEL_PANEL_ID })?.actions.setQuestion(prompt)
        },
        [sidePanelStateLogic.actionTypes.openSidePanel]: ({ tab, options }) => {
            if (tab !== SidePanelTab.Max) {
                return
            }
            forwardSeed(options, values.dataProcessingAccepted, actions.setSeed)
        },
    })),

    afterMount(({ actions, values }) => {
        // A CTA fires `openSidePanel` before this panel mounts, so the prompt is already sitting on
        // `sidePanelStateLogic` by the time we mount — pick it up (mirrors legacy maxLogic's afterMount read).
        if (values.selectedTab === SidePanelTab.Max) {
            forwardSeed(values.selectedTabOptions, values.dataProcessingAccepted, actions.setSeed)
        }
    }),
])