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.

4.1 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path } from 'kea'

import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'

import { type AttachedContextItem, NEW_CHAT_KEY } from '~/layout/ai/attachedContext'
import { attachedContextLogic } from '~/layout/ai/attachedContextLogic'

/** The group types the composer's @-picker offers. Keep in sync with `taxonomicItemToAttachedContext`. */
export const CONTEXT_PICKER_GROUP_TYPES: TaxonomicFilterGroupType[] = [
    TaxonomicFilterGroupType.Events,
    TaxonomicFilterGroupType.Actions,
    TaxonomicFilterGroupType.Insights,
    TaxonomicFilterGroupType.Dashboards,
    TaxonomicFilterGroupType.Notebooks,
    TaxonomicFilterGroupType.ErrorTrackingIssues,
]

/** Structural subset of the taxonomic item shapes the picker consumes — no scene-tree type imports. */
export interface PickableTaxonomicItem {
    id?: string | number
    short_id?: string
    name?: string | null
    title?: string | null
}

/**
 * Projects a TaxonomicFilter selection down to the surface's abstract `AttachedContextItem`.
 * Refs only — no entity loading; the agent fetches details via its read tools.
 * Returns null for group types the picker doesn't offer.
 */
export function taxonomicItemToAttachedContext(
    value: string | number,
    groupType: TaxonomicFilterGroupType,
    item: PickableTaxonomicItem
): AttachedContextItem | null {
    switch (groupType) {
        case TaxonomicFilterGroupType.Events:
            // Events are keyed by name everywhere (API, HogQL, taxonomy) — `item.id` is the
            // EventDefinition UUID, which the agent's read tools can't resolve.
            return { type: 'event', key: item.name ?? value, label: item.name ?? undefined }
        case TaxonomicFilterGroupType.Actions:
            return { type: 'action', key: item.id ?? value, label: item.name ?? undefined }
        case TaxonomicFilterGroupType.Insights:
            return { type: 'insight', key: item.short_id ?? value, label: item.name ?? undefined }
        case TaxonomicFilterGroupType.Dashboards:
            return { type: 'dashboard', key: item.id ?? value, label: item.name ?? undefined }
        case TaxonomicFilterGroupType.Notebooks:
            return { type: 'notebook', key: item.short_id ?? value, label: item.title ?? undefined }
        case TaxonomicFilterGroupType.ErrorTrackingIssues:
            return { type: 'error_tracking_issue', key: item.id ?? value, label: item.name ?? undefined }
        default:
            return null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface contextPickerLogicActions {
    pickItem: (
        item: AttachedContextItem,
        taskId?: string | undefined
    ) => {
        item: AttachedContextItem
        taskId: string
    } // attachedContextLogic
    handleTaxonomicFilterChange: (
        value: number | string,
        groupType: TaxonomicFilterGroupType,
        item: PickableTaxonomicItem,
        taskId?: string
    ) => {
        groupType: TaxonomicFilterGroupType
        item: PickableTaxonomicItem
        taskId: string
        value: number | string
    }
}

export type contextPickerLogicType = MakeLogicType<{}, contextPickerLogicActions>

export const contextPickerLogic = kea<contextPickerLogicType>([
    path(['products', 'posthog_ai', 'frontend', 'logics', 'contextPickerLogic']),

    connect(() => ({
        actions: [attachedContextLogic, ['pickItem']],
    })),

    actions({
        /** Signature matches `TaxonomicPopover.onChange` so the component can pass it straight through. */
        handleTaxonomicFilterChange: (
            value: string | number,
            groupType: TaxonomicFilterGroupType,
            item: PickableTaxonomicItem,
            taskId: string = NEW_CHAT_KEY
        ) => ({ value, groupType, item, taskId }),
    }),

    listeners(({ actions }) => ({
        handleTaxonomicFilterChange: ({ value, groupType, item, taskId }) => {
            const attached = taxonomicItemToAttachedContext(value, groupType, item)
            if (attached) {
                actions.pickItem(attached, taskId)
            }
        },
    })),
])