: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, afterMount, connect, kea, key, path, props, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { teamLogic } from 'scenes/teamLogic'
import { mcpAnalyticsIntentClustersRetrieve } from '../generated/api'
import type { MCPIntentClusterApi, MCPIntentClusterSnapshotApi, MCPToolPivotApi } from '../generated/api.schemas'
export interface MCPToolIntentsLogicProps {
toolName: string
}
const EMPTY_SNAPSHOT: MCPIntentClusterSnapshotApi = {
status: 'idle',
error_message: '',
last_computed_at: null,
last_computed_by_email: '',
clusters: [],
tools: [],
tool_overlaps: [],
computed_with: null,
}
// The generated client types the retrieve endpoint as returning an array, but the view actually
// returns a single object. drf-spectacular assumes ViewSet `list` actions return arrays. Normalize.
function normalizeSnapshot(
response: MCPIntentClusterSnapshotApi | readonly MCPIntentClusterSnapshotApi[] | null | undefined
): MCPIntentClusterSnapshotApi {
if (!response) {
return EMPTY_SNAPSHOT
}
if (Array.isArray(response)) {
return response[0] ?? EMPTY_SNAPSHOT
}
return response as MCPIntentClusterSnapshotApi
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpToolIntentsLogicValues {
currentProjectId: number | string // teamLogic
clusters: readonly MCPIntentClusterApi[]
hasSnapshot: boolean
snapshot: MCPIntentClusterSnapshotApi
snapshotLoading: boolean
tool: MCPToolPivotApi | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpToolIntentsLogicActions {
loadSnapshot: () => any
loadSnapshotFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadSnapshotSuccess: (
snapshot: MCPIntentClusterSnapshotApi,
payload?: any
) => {
snapshot: MCPIntentClusterSnapshotApi
payload?: any
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpToolIntentsLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
tool: (snapshot: MCPIntentClusterSnapshotApi) => MCPToolPivotApi | null
clusters: (snapshot: MCPIntentClusterSnapshotApi) => readonly MCPIntentClusterApi[]
hasSnapshot: (snapshot: MCPIntentClusterSnapshotApi) => boolean
}
}
export type mcpToolIntentsLogicType = MakeLogicType<
mcpToolIntentsLogicValues,
mcpToolIntentsLogicActions,
MCPToolIntentsLogicProps,
mcpToolIntentsLogicMeta
>
/**
* One tool's slice of the intent cluster snapshot, for the tool detail page.
*
* Separate from mcpClusteringLogic on purpose: that logic loads the whole
* snapshot — every cluster plus every tool's pivot — and starts a poll while a
* run is computing. Mounting it to render one section made every tool page view
* pay for the entire blob, so this asks the API for the tool's slice instead.
*/
export const mcpToolIntentsLogic = kea<mcpToolIntentsLogicType>([
path(['products', 'mcp_analytics', 'frontend', 'clustering', 'mcpToolIntentsLogic']),
key((props: MCPToolIntentsLogicProps) => props.toolName),
props({} as MCPToolIntentsLogicProps),
connect(() => ({
values: [teamLogic, ['currentProjectId']],
})),
loaders(({ props, values }) => ({
snapshot: [
EMPTY_SNAPSHOT as MCPIntentClusterSnapshotApi,
{
loadSnapshot: async () => {
if (!values.currentProjectId) {
return EMPTY_SNAPSHOT
}
const response = await mcpAnalyticsIntentClustersRetrieve(String(values.currentProjectId), {
tool: props.toolName,
})
return normalizeSnapshot(response)
},
},
],
})),
selectors({
tool: [
(s) => [s.snapshot],
(snapshot: MCPIntentClusterSnapshotApi): MCPToolPivotApi | null => snapshot.tools?.[0] ?? null,
],
clusters: [
(s) => [s.snapshot],
(snapshot: MCPIntentClusterSnapshotApi): readonly MCPIntentClusterApi[] => snapshot.clusters,
],
hasSnapshot: [
(s) => [s.snapshot],
(snapshot: MCPIntentClusterSnapshotApi): boolean => snapshot.last_computed_at !== null,
],
}),
afterMount(({ actions }) => {
actions.loadSnapshot()
}),
])