: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, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import type { FeatureFlagsSet } from 'lib/logic/featureFlagLogic'
import { preflightLogic } from 'lib/logic/preflightLogic'
import { newInternalTab } from 'lib/utils/newInternalTab'
import type { ToolRegistration } from 'scenes/max/max-constants'
import { urls } from 'scenes/urls'
import type { PhaiViewMode } from '~/layout/ai/aiPanelIds'
import type { PreflightStatus } from '~/types'
/** Which PostHog AI implementation the Max scene renders when the sandbox flag is on. */
const PHAI_VIEW_MODE_KEY = 'posthog_ai_view_mode'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface aiShellLogicValues {
featureFlags: FeatureFlagsSet // featureFlagLogic
isCloudOrDev: boolean | undefined // preflightLogic
preflight: PreflightStatus | null // preflightLogic
effectivePhaiView: PhaiViewMode
isMaxAvailable: boolean
isPhaiSandboxFlagOn: boolean
phaiViewMode: PhaiViewMode
registeredToolMap: Record<string, ToolRegistration>
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface aiShellLogicActions {
askSidePanelMax: (prompt: string) => {
prompt: string
}
deregisterTool: (key: string) => {
key: string
}
registerTool: (tool: ToolRegistration) => {
tool: ToolRegistration
}
setPhaiViewMode: (mode: PhaiViewMode) => {
mode: PhaiViewMode
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface aiShellLogicMeta {
__keaTypeGenInternalSelectorTypes: {
isMaxAvailable: (isCloudOrDev: boolean | undefined, preflight: PreflightStatus | null) => boolean
isPhaiSandboxFlagOn: (featureFlags: FeatureFlagsSet) => boolean
effectivePhaiView: (isPhaiSandboxFlagOn: boolean, phaiViewMode: PhaiViewMode) => PhaiViewMode
}
}
export type aiShellLogicType = MakeLogicType<
aiShellLogicValues,
aiShellLogicActions,
Record<string, any>,
aiShellLogicMeta
>
/**
* The PostHog AI state the app shell reads on every page. It must stay light: the shell loads it eagerly,
* so it must not import the Max scene or the posthog_ai run surface.
*/
export const aiShellLogic = kea<aiShellLogicType>([
path(['layout', 'ai', 'aiShellLogic']),
connect(() => ({
values: [featureFlagLogic, ['featureFlags'], preflightLogic, ['preflight', 'isCloudOrDev']],
})),
actions({
setPhaiViewMode: (mode: PhaiViewMode) => ({ mode }),
askSidePanelMax: (prompt: string) => ({ prompt }),
registerTool: (tool: ToolRegistration) => ({ tool }),
deregisterTool: (key: string) => ({ key }),
}),
reducers({
// The user's chosen implementation, persisted per device. Defaults to the new surface so a
// flagged user lands there; `effectivePhaiView` collapses this to legacy when the flag is off.
phaiViewMode: [
'new' as PhaiViewMode,
{ persist: true, storageKey: PHAI_VIEW_MODE_KEY },
{
setPhaiViewMode: (_, { mode }) => mode,
},
],
// The tools of the scene on screen. Scenes register here, not in the Max logics, so a tool registered
// before the Max chunk loads is still there when it does.
registeredToolMap: [
{} as Record<string, ToolRegistration>,
{
registerTool: (state, { tool }) => ({
...state,
[tool.identifier]: tool,
}),
deregisterTool: (state, { key }) => {
const newState = { ...state }
delete newState[key]
return newState
},
},
],
}),
selectors({
// On Cloud/dev a provider key is always present. On a self-hosted (hobby) instance Max only
// works once ANTHROPIC_API_KEY is configured, so we surface a "set the key" state instead.
// Treat a not-yet-loaded preflight (null) as available so the empty-state doesn't flash
// before preflight resolves — once loaded we gate on cloud/dev or the key being present.
isMaxAvailable: [
(s) => [s.isCloudOrDev, s.preflight],
(isCloudOrDev: boolean | undefined, preflight: PreflightStatus | null): boolean =>
!preflight || !!isCloudOrDev || !!preflight.anthropic_available,
],
// Gates the new-vs-legacy toggle: only flagged users can switch, and the new surface only
// exists for them.
isPhaiSandboxFlagOn: [
(s) => [s.featureFlags],
(featureFlags: FeatureFlagsSet): boolean => !!featureFlags[FEATURE_FLAGS.PHAI_SANDBOX_MODE],
],
// The implementation the Max scene actually renders: the user's choice while the flag is on,
// always legacy otherwise (so non-flagged users are unaffected regardless of any stored value).
effectivePhaiView: [
(s) => [s.isPhaiSandboxFlagOn, s.phaiViewMode],
(isPhaiSandboxFlagOn: boolean, phaiViewMode: PhaiViewMode): PhaiViewMode =>
isPhaiSandboxFlagOn ? phaiViewMode : 'legacy',
],
}),
listeners({
askSidePanelMax: ({ prompt }) => {
newInternalTab(urls.ai(undefined, prompt))
},
}),
])