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

import { urls } from 'scenes/urls'

import { runnerPanelLogic } from '~/layout/ai/runnerPanelLogic'

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

import type { ComposerSeed } from '../../../../products/posthog_ai/frontend/logics/composerSeedLogic'
import { maxGlobalLogic } from './maxGlobalLogic'
import { maxLogic } from './maxLogic'

export interface PhaiAiComposerSeedLogicProps {
    /** The scene tab of the hosting AI scene, which also keys its classic `maxLogic`. */
    tabId?: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface phaiAiComposerSeedLogicValues {
    dataProcessingAccepted: boolean // maxGlobalLogic
}

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

export type phaiAiComposerSeedLogicType = MakeLogicType<
    phaiAiComposerSeedLogicValues,
    phaiAiComposerSeedLogicActions,
    PhaiAiComposerSeedLogicProps
>

/**
 * Forwards the existing `/ai?ask=...` deep link into the new task composer. This logic is mounted only while
 * the new PostHog AI view is rendered, leaving the legacy `maxLogic` query handling unchanged.
 */
export const phaiAiComposerSeedLogic = kea<phaiAiComposerSeedLogicType>([
    path(['scenes', 'max', 'phaiAiComposerSeedLogic']),
    props({} as PhaiAiComposerSeedLogicProps),

    connect({
        values: [maxGlobalLogic, ['dataProcessingAccepted']],
        actions: [composerSeedLogic, ['setSeed'], runnerPanelLogic({}), ['handOffToClassic']],
    }),

    listeners(({ props }) => ({
        // The scene swaps to the legacy chat next, and that chat reads its draft from this tab's `maxLogic`.
        handOffToClassic: ({ prompt }) => {
            maxLogic.findMounted({ panelId: props.tabId })?.actions.setQuestion(prompt)
        },
    })),

    urlToAction(({ actions, values }) => ({
        [urls.ai()]: (_, search, hash) => {
            if (search.ask && !search.chat && !search.task) {
                const { ask, ...remainingSearch } = search
                // Hash changes also run this handler. Remove the prompt before handing it off so they
                // cannot submit it again.
                router.actions.replace(router.values.location.pathname, remainingSearch, hash)
                // `ask` is URL-controlled — same org-level AI data-processing consent gate as the legacy
                // `askMax` path: without approval the seed only prefills the composer, and the user's own
                // send goes through the composer's consent flow.
                actions.setSeed({ prompt: String(ask), autoSubmit: values.dataProcessingAccepted })
            }
        },
    })),
])