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.1 KB
Raw Blame
import { useValues } from 'kea'
import posthog from 'posthog-js'
import { useMemo } from 'react'

import { IconAI } from '@posthog/icons'

import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { addProductIntent } from 'lib/utils/product-intents'
import { useMaxTool } from 'scenes/max/useMaxTool'
import { surveyLogic } from 'scenes/surveys/surveyLogic'

import { iconForType } from '~/layout/panel-layout/ProjectTree/defaultTree'
import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'

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

const NUM_OF_RESPONSES_FOR_MAX_ANALYSIS_TOOL = 5

function useSurveyAnalysisMaxTool(): ReturnType<typeof useMaxTool> {
    const { survey, formattedOpenEndedResponses } = useValues(surveyLogic)

    const maxToolContext = useMemo(
        () => ({
            survey_id: survey.id,
            survey_name: survey.name,
            formatted_responses: formattedOpenEndedResponses,
        }),
        [survey.id, survey.name, formattedOpenEndedResponses]
    )

    const shouldShowMaxAnalysisTool = useMemo(() => {
        const totalResponses = formattedOpenEndedResponses.reduce((acc, curr) => acc + curr.responses.length, 0)
        return totalResponses >= NUM_OF_RESPONSES_FOR_MAX_ANALYSIS_TOOL
    }, [formattedOpenEndedResponses])

    useAttachedContext(
        shouldShowMaxAnalysisTool
            ? [
                  { type: 'survey', key: survey.id, label: survey.name },
                  {
                      type: 'survey_open_ended_responses',
                      value: JSON.stringify(formattedOpenEndedResponses),
                      label: 'Open-ended responses',
                  },
              ]
            : null
    )

    return useMaxTool({
        identifier: 'analyze_survey_responses',
        context: maxToolContext,
        contextDescription: {
            text: survey.name,
            icon: iconForType('survey'),
        },
        active: shouldShowMaxAnalysisTool,
        initialMaxPrompt: `Analyze the survey responses for the survey "${survey.name}"`,
        callback(toolOutput) {
            addProductIntent({
                product_type: ProductKey.SURVEYS,
                intent_context: ProductIntentContext.SURVEY_ANALYZED,
                metadata: {
                    survey_id: survey.id,
                },
            })

            if (toolOutput?.error) {
                posthog.captureException(
                    toolOutput?.error || 'Undefined error when analyzing survey responses with PostHog AI',
                    {
                        action: 'max-ai-survey-analysis-failed',
                        survey_id: survey.id,
                        ...toolOutput,
                    }
                )
            }
        },
    })
}

export function AnalyzeResponsesButton(): JSX.Element | null {
    const { openMax } = useSurveyAnalysisMaxTool()

    if (!openMax) {
        return null
    }

    return (
        <LemonButton onClick={openMax} type="secondary" icon={<IconAI />}>
            Analyze responses
        </LemonButton>
    )
}