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.

2.3 KB
Raw Blame
import { PrStateEnumApi, TaskRunDetailDTOApi } from '../generated/api.schemas'

export type SpaceFeedStatusVariant = 'default' | 'info' | 'destructive' | 'success' | 'completed'

export interface SpaceFeedStatus {
    label: string
    variant: SpaceFeedStatusVariant
    /** The run is working now, so the badge shows a spinner. */
    running?: boolean
}

const RUN_STATUSES: Record<string, SpaceFeedStatus> = {
    not_started: { label: 'Not started', variant: 'default' },
    queued: { label: 'Queued', variant: 'default' },
    in_progress: { label: 'In progress', variant: 'info', running: true },
    completed: { label: 'Ready', variant: 'success' },
    failed: { label: 'Failed', variant: 'destructive' },
    cancelled: { label: 'Cancelled', variant: 'default' },
}

// Once a PR exists its GitHub state is the card's status, with PostHog Desktop's labels and colors.
const PR_STATUSES: Record<Exclude<PrStateEnumApi, 'unknown'>, SpaceFeedStatus> = {
    open: { label: 'PR ready', variant: 'info' },
    draft: { label: 'Draft PR', variant: 'default' },
    merged: { label: 'Merged', variant: 'completed' },
    closed: { label: 'Closed', variant: 'destructive' },
}

const TERMINAL_STATUSES = new Set(['completed', 'failed', 'cancelled'])

function hasPullRequest(output: TaskRunDetailDTOApi['output']): boolean {
    if (!output) {
        return false
    }
    const urls = Array.isArray(output.pr_urls) ? output.pr_urls : []
    return typeof output.pr_url === 'string' || urls.some((url) => typeof url === 'string')
}

export function spaceFeedStatus(
    run: TaskRunDetailDTOApi | null | undefined,
    prState?: PrStateEnumApi | null
): SpaceFeedStatus | null {
    if (!run) {
        return { label: 'Draft', variant: 'default' }
    }
    const stopped = run.status === 'failed' || run.status === 'cancelled'
    if (!stopped && hasPullRequest(run.output)) {
        return prState && prState !== 'unknown' ? PR_STATUSES[prState] : PR_STATUSES.open
    }
    if (run.environment !== 'cloud' && !TERMINAL_STATUSES.has(run.status)) {
        return null
    }
    // An interactive run stays `in_progress` between turns, so its turn output is ready for the reader.
    if (run.status === 'in_progress' && run.state?.mode === 'interactive') {
        return RUN_STATUSES.completed
    }
    return RUN_STATUSES[run.status] ?? null
}