: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, afterMount, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import { urlToAction } from 'kea-router'
import api from 'lib/api'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cliLiveLogicValues {
error: string
port: string
projects: {
id: number
name: string
}[]
projectsLoading: boolean
redirected: boolean
selectedProjectId: number | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cliLiveLogicActions {
loadProjects: () => any
loadProjectsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadProjectsSuccess: (
projects: {
id: number
name: string
}[],
payload?: any
) => {
projects: {
id: number
name: string
}[]
payload?: any
}
selectProject: (projectId: number) => {
projectId: number
}
setError: (error: string) => {
error: string
}
setPort: (port: string) => {
port: string
}
setRedirected: (redirected: boolean) => {
redirected: boolean
}
}
export type cliLiveLogicType = MakeLogicType<cliLiveLogicValues, cliLiveLogicActions>
export const cliLiveLogic = kea<cliLiveLogicType>([
path(['scenes', 'authentication', 'cli', 'cliLiveLogic']),
actions({
setPort: (port: string) => ({ port }),
selectProject: (projectId: number) => ({ projectId }),
setError: (error: string) => ({ error }),
setRedirected: (redirected: boolean) => ({ redirected }),
}),
reducers({
port: [
'' as string,
{
setPort: (_, { port }) => port,
},
],
selectedProjectId: [
null as number | null,
{
selectProject: (_, { projectId }) => projectId,
},
],
error: [
'' as string,
{
setError: (_, { error }) => error,
},
],
redirected: [
false,
{
setRedirected: (_, { redirected }) => redirected,
},
],
}),
loaders(() => ({
projects: [
[] as { id: number; name: string }[],
{
loadProjects: async () => {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
const response = await api.get('api/projects/')
return response.results || []
},
},
],
})),
listeners(({ actions, values }) => ({
loadProjectsSuccess: () => {
if (values.projects.length === 1) {
actions.selectProject(values.projects[0].id)
}
},
selectProject: async ({ projectId }) => {
if (!values.port) {
actions.setError('Missing port parameter')
return
}
const portNum = parseInt(values.port, 10)
if (isNaN(portNum) || portNum < 1 || portNum > 65535 || String(portNum) !== String(values.port)) {
actions.setError('Invalid port parameter')
return
}
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use organizationsProjectsRetrieve() from '~/generated/core/api' instead.
const team = await api.get(`api/projects/${projectId}/`)
const token = team.live_events_token
if (!token) {
actions.setError('Live events token not available for this project')
return
}
const teamName = encodeURIComponent(team.name || '')
const teamId = team.id
const apiHost = encodeURIComponent(window.location.origin)
window.location.href = `http://127.0.0.1:${portNum}/callback?token=${token}&team_name=${teamName}&team_id=${teamId}&api_host=${apiHost}`
actions.setRedirected(true)
} catch (e: any) {
actions.setError(e?.detail || 'Failed to fetch project details')
}
},
})),
urlToAction(({ actions }) => ({
'/cli/live': (_, searchParams) => {
const port = searchParams.port
if (port) {
actions.setPort(String(port)) // Ensure stored as string, `searchParams` sometimes automatically parses number
}
},
})),
afterMount(({ actions }) => {
actions.loadProjects()
}),
])