: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 { useActions, useValues } from 'kea'
import { useState } from 'react'
import { IconChevronDown, IconCopy, IconLogomark, IconSparkles } from '@posthog/icons'
import { MCP_INSTALL_COMMAND } from 'lib/components/MCPHint/constants'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonMenuOverlay } from 'lib/lemon-ui/LemonMenu'
import { useKeyboardNavigation } from 'lib/lemon-ui/LemonMenu/useKeyboardNavigation'
import { preflightLogic } from 'lib/logic/preflightLogic'
import { ButtonPrimitive } from 'lib/ui/Button/ButtonPrimitives'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuItemIndicator,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from 'lib/ui/DropdownMenu/DropdownMenu'
import {
Button as QuillButton,
ButtonGroup as QuillButtonGroup,
ButtonGroupSeparator as QuillButtonGroupSeparator,
type ButtonProps as QuillButtonProps,
} from 'lib/ui/quill'
import { copyToClipboard } from 'lib/utils/copyToClipboard'
import { cn } from 'lib/utils/css-classes'
import { newInternalTab } from 'lib/utils/newInternalTab'
import { projectLogic } from 'scenes/projectLogic'
import { urls } from 'scenes/urls'
import { todayShellLogic } from '~/layout/today/todayShellLogic'
import { AgentLogo, claudeLogo, cursorLogo, openaiLogo } from './AgentLogo'
import { agentPromptButtonLogic } from './agentPromptButtonLogic'
export interface AgentPromptAction {
/** Stable key used for localStorage persistence */
key: string
label: string
icon?: React.ReactElement
/** Returns the prompt text for this action */
buildPrompt: () => string
/** Content that is not an agent prompt, like a raw stack trace, so it goes out without the PostHog MCP instructions. */
raw?: boolean
}
export type AgentPromptDestination =
| 'posthog-ai'
| 'posthog-code'
| 'claude-code'
| 'claude-desktop'
| 'claude-code-vscode'
| 'claude-code-web'
| 'cursor'
| 'codex'
| 'clipboard'
/** Quill button sizes, minus the icon-only variants (the dropdown trigger derives those automatically). */
type AgentPromptButtonSize = Exclude<NonNullable<QuillButtonProps['size']>, 'icon' | 'icon-xs' | 'icon-sm' | 'icon-lg'>
const ICON_SIZE: Record<AgentPromptButtonSize, QuillButtonProps['size']> = {
default: 'icon',
xs: 'icon-xs',
sm: 'icon-sm',
lg: 'icon-lg',
row: 'icon',
inline: 'icon-xs',
}
export interface AgentPromptButtonProps {
actions: AgentPromptAction[]
/** Agent always shown on the main button, in place of the one the user last picked. */
pinnedAgentKey?: AgentPromptDestination
/** Agents left out of the menu, for prompts a given agent cannot act on. */
excludeAgentKeys?: AgentPromptDestination[]
/** `destination` names the agent on the main button ("Open in Cursor") instead of the prompt ("Open Fix prompt"). */
labelMode?: 'action' | 'destination'
/** Extra classes for the dropdown menu, e.g. a higher z-index when the button sits inside a toast. */
menuClassName?: string
onOpenChange?: (open: boolean) => void
size?: AgentPromptButtonSize
variant?: NonNullable<QuillButtonProps['variant']>
/** `lemon` renders a LemonButton with a side menu, to sit beside other Lemon buttons. `size` and `variant` apply to `quill` only. */
appearance?: 'quill' | 'lemon'
/** LemonButton type for the `lemon` appearance. */
lemonType?: 'primary' | 'secondary'
/** Renders the dropdown open on first paint. Useful for visual regression snapshots. */
defaultOpen?: boolean
/** Fired whenever a combo runs (agent deeplink opened or clipboard copied). Useful for analytics. */
onRun?: (params: { actionKey: string; agentKey: string }) => void
/** GitHub `owner/repo` slug passed to agents that can open a specific repository (e.g. Claude Code, Codex). */
repository?: string
'data-attr'?: string
}
interface AgentDef {
key: AgentPromptDestination
name: string
/** Either a brand SVG URL (string from `import foo from './logos/foo.svg'`) or a React node */
logo: string | React.ReactElement
/** Extra classes applied to the rendered <img> for brand SVG logos (e.g. `dark:invert` for monochrome marks) */
logoClassName?: string
/** Verb shown on the main button when this provider is selected. */
verb: string
/** Opens the prompt in this agent. Some agents double-encode or truncate prompts due to URL length limits. */
open: (prompt: string, context: AgentOpenContext) => void
}
interface AgentOpenContext {
actionLabel: string
repository?: string
}
const CLAUDE_CODE_CLI_MAX_PROMPT_CHARS = 5_000
const CLAUDE_DESKTOP_MAX_PROMPT_CHARS = 14_000
/** Cap for apps that document no limit, sized to stay well inside OS URL handler limits. */
const UNDOCUMENTED_MAX_PROMPT_CHARS = 4_000
const CURSOR_MAX_URL_LENGTH = 10_000
/** Many web servers reject request lines over about 8 KB with a 414, so the claude.ai link stays under that. */
const CLAUDE_CODE_WEB_MAX_URL_LENGTH = 8_000
function truncatePrompt(prompt: string, maxChars: number): string {
if (prompt.length <= maxChars) {
return prompt
}
// A cut between the two halves of a surrogate pair (an emoji, for example) leaves a lone
// surrogate, and encodeURIComponent throws a URIError on it.
const lastCode = prompt.charCodeAt(maxChars - 1)
const end = lastCode >= 0xd800 && lastCode <= 0xdbff ? maxChars - 1 : maxChars
return prompt.slice(0, end)
}
function buildWithinUrlLength(prompt: string, maxUrlLength: number, build: (prompt: string) => string): string {
const full = build(prompt)
if (full.length <= maxUrlLength) {
return full
}
let low = 0
let high = prompt.length
while (low < high) {
const mid = Math.ceil((low + high) / 2)
if (build(truncatePrompt(prompt, mid)).length <= maxUrlLength) {
low = mid
} else {
high = mid - 1
}
}
return build(truncatePrompt(prompt, low))
}
function openDeepLink(buildDeepLink: (prompt: string, repository?: string) => string): AgentDef['open'] {
return (prompt, { repository }) => window.open(buildDeepLink(prompt, repository), '_blank')
}
export function buildPostHogCodeDeepLink(prompt: string, repository?: string): string {
const repoParam = repository ? `&repo=${encodeURIComponent(repository)}` : ''
return `posthog-code://new?prompt=${encodeURIComponent(prompt)}${repoParam}`
}
export function buildClaudeCodeDeepLink(prompt: string, repository?: string): string {
const query = encodeURIComponent(truncatePrompt(prompt, CLAUDE_CODE_CLI_MAX_PROMPT_CHARS))
const repoParam = repository ? `repo=${encodeURIComponent(repository)}&` : ''
return `claude-cli://open?${repoParam}q=${query}`
}
/** The Desktop link takes only an absolute folder path, so it cannot select the repository. */
export function buildClaudeDesktopDeepLink(prompt: string): string {
return `claude://code/new?q=${encodeURIComponent(truncatePrompt(prompt, CLAUDE_DESKTOP_MAX_PROMPT_CHARS))}`
}
export function buildClaudeCodeVSCodeDeepLink(prompt: string): string {
return `vscode://anthropic.claude-code/open?prompt=${encodeURIComponent(truncatePrompt(prompt, UNDOCUMENTED_MAX_PROMPT_CHARS))}`
}
export function buildClaudeCodeWebLink(prompt: string, repository?: string): string {
const repoParam = repository ? `&repositories=${encodeURIComponent(repository)}` : ''
return buildWithinUrlLength(
prompt,
CLAUDE_CODE_WEB_MAX_URL_LENGTH,
(text) => `https://claude.ai/code?prompt=${encodeURIComponent(text)}${repoParam}`
)
}
export function buildCursorDeepLink(prompt: string): string {
// Cursor decodes the full deeplink before parsing query params, so reserved chars need an extra escape layer.
return buildWithinUrlLength(
prompt,
CURSOR_MAX_URL_LENGTH,
(text) => `cursor://anysphere.cursor-deeplink/prompt?text=${encodeURIComponent(encodeURIComponent(text))}`
)
}
export function buildCodexDeepLink(prompt: string, repository?: string): string {
const originParam = repository ? `&originUrl=${encodeURIComponent(`https://github.com/${repository}`)}` : ''
return `codex://new?prompt=${encodeURIComponent(truncatePrompt(prompt, UNDOCUMENTED_MAX_PROMPT_CHARS))}${originParam}`
}
// pinned: each `key` is stored in localStorage and sent as the `agent` analytics property, so renaming one resets
// remembered choices and splits the analytics. Change `name` instead.
const AGENTS: AgentDef[] = [
{
key: 'posthog-ai',
name: 'PostHog AI',
logo: <IconSparkles className="size-4 shrink-0 text-ai" />,
verb: 'Open',
open: (prompt) => newInternalTab(urls.ai(undefined, prompt)),
},
{
key: 'posthog-code',
name: 'PostHog Desktop',
logo: <IconLogomark className="size-4 shrink-0" />,
verb: 'Open',
open: openDeepLink(buildPostHogCodeDeepLink),
},
{
key: 'claude-code',
name: 'Claude Code CLI',
logo: claudeLogo,
verb: 'Open',
open: openDeepLink(buildClaudeCodeDeepLink),
},
{
key: 'claude-desktop',
name: 'Claude Desktop',
logo: claudeLogo,
verb: 'Open',
open: openDeepLink(buildClaudeDesktopDeepLink),
},
{
key: 'claude-code-vscode',
name: 'Claude Code in VS Code',
logo: claudeLogo,
verb: 'Open',
open: openDeepLink(buildClaudeCodeVSCodeDeepLink),
},
{
key: 'claude-code-web',
name: 'Claude Code on the web',
logo: claudeLogo,
verb: 'Open',
open: (prompt, { repository }) =>
window.open(buildClaudeCodeWebLink(prompt, repository), '_blank', 'noopener,noreferrer'),
},
{
key: 'cursor',
name: 'Cursor',
logo: cursorLogo,
// Cursor wordmark is solid black; invert in dark mode so it stays visible
logoClassName: 'dark:invert',
verb: 'Open',
open: openDeepLink(buildCursorDeepLink),
},
{
key: 'codex',
name: 'Codex',
logo: openaiLogo,
verb: 'Open',
open: openDeepLink(buildCodexDeepLink),
},
{
key: 'clipboard',
name: 'Clipboard',
logo: <IconCopy className="size-4 shrink-0" />,
verb: 'Copy',
open: (prompt, { actionLabel }) => {
void copyToClipboard(prompt, actionLabel.toLowerCase())
},
},
]
export function buildAgentPrompt(
action: AgentPromptAction,
agentKey: AgentPromptDestination,
projectId: number | null,
isCloudOrDev: boolean
): string {
const prompt = action.buildPrompt()
// PostHog AI reads PostHog data directly, so it has no use for the MCP server.
// Self-hosted instances have no PostHog MCP server to connect to.
if (action.raw || agentKey === 'posthog-ai' || !isCloudOrDev) {
return prompt
}
const project = projectId ? ` in PostHog project ${projectId}` : ''
return `${prompt.trimEnd()}\n\nIf working with PostHog data would help with this task, use the PostHog MCP server${project}. If the server is not connected, ask me to install it by running \`${MCP_INSTALL_COMMAND}\` in a terminal.`
}
export function AgentPromptButton({
actions,
pinnedAgentKey,
excludeAgentKeys,
labelMode = 'action',
menuClassName,
onOpenChange,
size = 'default',
variant = 'default',
appearance = 'quill',
lemonType = 'secondary',
defaultOpen = false,
onRun,
repository,
'data-attr': dataAttr,
}: AgentPromptButtonProps): JSX.Element | null {
const actionSetKey = actions
.map((a) => a.key)
.sort()
.join(',')
const { rememberedAgentKey, rememberedActionKeys } = useValues(agentPromptButtonLogic)
const { rememberAgent, rememberAction } = useActions(agentPromptButtonLogic)
const [open, setOpen] = useState(defaultOpen)
const { todayRailEnabled } = useValues(todayShellLogic)
const { currentProjectId } = useValues(projectLogic)
const { isCloudOrDev } = useValues(preflightLogic)
const availableAgents = AGENTS.filter(
(agent) => !(todayRailEnabled && agent.key === 'posthog-ai') && !excludeAgentKeys?.includes(agent.key)
)
// The Lemon menu renders its overlay directly, so it wires LemonMenu's arrow-key focus handling itself.
const lemonKeyboard = useKeyboardNavigation<HTMLButtonElement>(
(actions.length > 1 ? actions.length : 0) + availableAgents.length
)
if (actions.length === 0 || availableAgents.length === 0) {
return null
}
const activeAction = actions.find((a) => a.key === rememberedActionKeys[actionSetKey]) ?? actions[0]
const activeAgent =
availableAgents.find((a) => a.key === (pinnedAgentKey ?? rememberedAgentKey)) ?? availableAgents[0]
const buttonLabel =
labelMode === 'destination' && activeAgent.key !== 'clipboard'
? `Open in ${activeAgent.name}`
: `${activeAgent.verb} ${activeAction.label}`
const selectAction = (actionKey: string): void => {
rememberAction(actionSetKey, actionKey)
}
const runCombo = (actionKey: string, agentKey: AgentPromptDestination): void => {
const action = actions.find((a) => a.key === actionKey) ?? actions[0]
const prompt = buildAgentPrompt(action, agentKey, currentProjectId, !!isCloudOrDev)
onRun?.({ actionKey, agentKey })
const agent = availableAgents.find((a) => a.key === agentKey)
if (!agent) {
return
}
agent.open(prompt, { actionLabel: action.label, repository })
}
const selectAgent = (agentKey: AgentPromptDestination): void => {
rememberAgent(agentKey)
runCombo(activeAction.key, agentKey)
setOpen(false)
}
const handleMainClick = (): void => {
runCombo(activeAction.key, activeAgent.key)
}
if (appearance === 'lemon') {
return (
// Focus and key events bubble up from the side action, which takes no handlers of its own.
// React also bubbles them up from the portaled menu, so only events from inside this subtree count.
<div
className="contents"
onFocus={(e) => e.currentTarget.contains(e.target) && lemonKeyboard.onTriggerFocus(e)}
onKeyDown={(e) => e.currentTarget.contains(e.target as Node) && lemonKeyboard.onTriggerKeyDown(e)}
>
<LemonButton
type={lemonType}
size="small"
icon={<AgentLogo logo={activeAgent.logo} logoClassName={activeAgent.logoClassName} />}
onClick={handleMainClick}
data-attr={dataAttr}
title={`Run: ${buttonLabel}`}
sideAction={{
'aria-label': actions.length > 1 ? 'Choose prompt and agent' : 'Open prompt in an agent',
dropdown: {
placement: 'bottom-end',
onVisibilityChange: onOpenChange,
overlay: (
<LemonMenuOverlay
itemsRef={lemonKeyboard.itemsRef}
onKeyDown={lemonKeyboard.onItemsKeyDown}
items={[
actions.length > 1 && {
title: 'Content',
items: actions.map((action) => ({
label: action.label,
icon: action.icon,
active: action.key === activeAction.key,
onClick: () => selectAction(action.key),
})),
},
{
title: 'Open in',
items: availableAgents.map((agent) => ({
label: agent.name,
icon: (
<AgentLogo logo={agent.logo} logoClassName={agent.logoClassName} />
),
onClick: () => selectAgent(agent.key),
})),
},
]}
/>
),
},
}}
>
<span className="truncate max-w-64">{buttonLabel}</span>
</LemonButton>
</div>
)
}
return (
<DropdownMenu
open={open}
onOpenChange={(nextOpen) => {
setOpen(nextOpen)
onOpenChange?.(nextOpen)
}}
>
<QuillButtonGroup>
<QuillButton
variant={variant}
size={size}
className="border-0"
onClick={handleMainClick}
data-attr={dataAttr}
title={`Run: ${buttonLabel}`}
>
<AgentLogo logo={activeAgent.logo} logoClassName={activeAgent.logoClassName} />
<span className="truncate max-w-64">{buttonLabel}</span>
</QuillButton>
<QuillButtonGroupSeparator />
<DropdownMenuTrigger asChild>
<QuillButton
variant={variant}
size={ICON_SIZE[size]}
className="border-0"
aria-label={actions.length > 1 ? 'Choose prompt and agent' : 'Open prompt in an agent'}
>
<IconChevronDown className="size-4 text-current" />
</QuillButton>
</DropdownMenuTrigger>
</QuillButtonGroup>
<DropdownMenuContent align="end" className={cn('w-60 max-w-none', menuClassName)}>
{actions.length > 1 && (
<>
<DropdownMenuLabel>Content</DropdownMenuLabel>
<DropdownMenuRadioGroup value={activeAction.key} onValueChange={selectAction}>
{actions.map((action) => (
<DropdownMenuRadioItem
key={action.key}
value={action.key}
asChild
onSelect={(e) => e.preventDefault()}
>
<ButtonPrimitive menuItem className="gap-1.5">
{action.icon}
<span className="truncate flex-1">{action.label}</span>
<DropdownMenuItemIndicator intent="radio" />
</ButtonPrimitive>
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
{/* Direct child of the padding-less menu inner — drop the separator's
default -mx-1 so it doesn't overflow and trigger scroll shadows */}
<DropdownMenuSeparator className="mx-0" />
</>
)}
<DropdownMenuLabel>Open in</DropdownMenuLabel>
<DropdownMenuGroup>
{availableAgents.map((agent) => (
<DropdownMenuItem key={agent.key} asChild onSelect={() => selectAgent(agent.key)}>
<ButtonPrimitive menuItem className="gap-1.5">
<AgentLogo logo={agent.logo} logoClassName={agent.logoClassName} />
<span className="truncate flex-1">{agent.name}</span>
</ButtonPrimitive>
</DropdownMenuItem>
))}
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
)
}