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.

20.3 KB
Raw Blame
import { Combobox } from '@base-ui/react/combobox'
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'
import { memo, useId, useMemo, useState } from 'react'

import { IconPlusSmall, IconSearch } from '@posthog/icons'
import { LemonInput, LemonSkeleton, Tooltip } from '@posthog/lemon-ui'

import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { Link } from 'lib/lemon-ui/Link'
import { ScrollableShadows } from 'lib/lemon-ui/ScrollableShadows/ScrollableShadows'
import { Spinner } from 'lib/lemon-ui/Spinner'
import { ButtonPrimitive } from 'lib/ui/Button/ButtonPrimitives'
import { Collapsible } from 'lib/ui/Collapsible/Collapsible'
import { removeProjectIdIfPresent } from 'lib/utils/kea-router'
import { AiChatListItem } from 'scenes/max/components/List/AiChatListItem'
import { maxGlobalLogic } from 'scenes/max/maxGlobalLogic'
import { urls } from 'scenes/urls'

import type { Conversation } from '~/types'

import { tasksLogic } from 'products/posthog_ai/frontend/api/logics'
import { TaskAssigneeFilterMenu, TaskListItem } from 'products/posthog_ai/frontend/api/primitives'
import type { Task, TaskAssigneeFilter } from 'products/posthog_ai/frontend/api/types'

const DATE_GROUP_ORDER = ['Today', 'Yesterday', 'Last 7 days', 'Last 30 days', 'Older'] as const

function getDateGroupLabel(dateString: string | null): string {
    if (!dateString) {
        return 'Older'
    }

    const date = new Date(dateString)
    const now = new Date()
    const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
    const yesterday = new Date(today)
    yesterday.setDate(yesterday.getDate() - 1)
    const lastWeek = new Date(today)
    lastWeek.setDate(lastWeek.getDate() - 7)
    const lastMonth = new Date(today)
    lastMonth.setDate(lastMonth.getDate() - 30)

    if (date >= today) {
        return 'Today'
    } else if (date >= yesterday) {
        return 'Yesterday'
    } else if (date >= lastWeek) {
        return 'Last 7 days'
    } else if (date >= lastMonth) {
        return 'Last 30 days'
    }
    return 'Older'
}

export type AiHistoryItem =
    | {
          kind: 'conversation'
          key: string
          title: string
          searchableText: string
          updatedAt: string | null
          conversation: Conversation
      }
    | {
          kind: 'task'
          key: string
          title: string
          searchableText: string
          updatedAt: string
          task: Task
      }

export interface AiHistoryGroup {
    value: string
    items: AiHistoryItem[]
}

export function groupAiHistory(
    conversationHistory: Conversation[],
    tasks: Task[],
    filter: TaskAssigneeFilter = 'for_you'
): AiHistoryGroup[] {
    const items: AiHistoryItem[] = []
    const loadedTaskIds = new Set(tasks.map((task) => task.id))

    const showConversations = filter === 'for_you' || filter === 'posthog_ai' || filter === 'all_team'
    for (const conversation of showConversations ? conversationHistory : []) {
        if (!conversation) {
            continue
        }
        // A chat copied into a task shows as that task once the task list holds it. Until the task list
        // catches up, the chat row stays, so the chat is never missing from the list.
        if (conversation.task && loadedTaskIds.has(conversation.task.id)) {
            continue
        }
        const title = conversation.title || 'Untitled conversation'
        items.push({
            kind: 'conversation',
            key: `conversation:${conversation.id}`,
            title,
            searchableText: title,
            updatedAt: conversation.updated_at,
            conversation,
        })
    }

    for (const task of tasks) {
        const title = task.title || task.slug
        items.push({
            kind: 'task',
            key: `task:${task.id}`,
            title,
            // The slug rather than `title`, which only falls back to it for untitled tasks. The server
            // also matches a task by its number, through a slug-shaped query like `TASK-42`, and the
            // run header shows that slug for people to type back. Leaving it out of this filter hides
            // a titled row the server did return.
            searchableText: `${task.title} ${task.slug} ${task.description}`,
            // A task's row is often never edited after creation, so `updated_at` would bucket a
            // task that ran an hour ago by how long ago it was created.
            updatedAt: task.last_activity_at ?? task.updated_at,
            task,
        })
    }

    items.sort((left, right) => (Date.parse(right.updatedAt ?? '') || 0) - (Date.parse(left.updatedAt ?? '') || 0))

    const grouped: Record<string, AiHistoryItem[]> = {}
    for (const item of items) {
        const label = getDateGroupLabel(item.updatedAt)
        if (!grouped[label]) {
            grouped[label] = []
        }
        grouped[label].push(item)
    }

    return DATE_GROUP_ORDER.filter((label) => grouped[label]?.length > 0).map(
        (label): AiHistoryGroup => ({ value: label, items: grouped[label] })
    )
}

export interface ConversationGroup {
    value: string
    items: Conversation[]
}

export function groupConversations(conversationHistory: Conversation[]): ConversationGroup[] {
    return groupAiHistory(conversationHistory, []).map((group) => ({
        value: group.value,
        items: group.items.flatMap((item) => (item.kind === 'conversation' ? [item.conversation] : [])),
    }))
}

function LoadTasksError({ onRetry }: { onRetry: () => void }): JSX.Element {
    return (
        <div className="mx-2 mt-2 flex items-center justify-between gap-2 rounded border border-danger bg-danger-highlight px-2 py-1.5 text-xs">
            <span>Tasks couldn't load.</span>
            <ButtonPrimitive size="xs" onClick={onRetry}>
                Retry
            </ButtonPrimitive>
        </div>
    )
}

// Every loaded row renders inside the collapsible groups, with no virtualizer, and a search opens
// every group. Memoized so a keystroke or a status poll re-renders only the rows whose item or
// selection changed, rather than every page the user has loaded.
const ConversationRow = memo(function ConversationRow({
    item,
    active,
}: {
    item: Extract<AiHistoryItem, { kind: 'conversation' }>
    active: boolean
}): JSX.Element {
    const href = AiChatListItem.getHref(item.conversation.id)
    return (
        <AiChatListItem.Root>
            <AiChatListItem.Group>
                <Combobox.Item
                    value={item}
                    render={(props) => (
                        <Tooltip title={item.title} placement="right">
                            <Link
                                {...props}
                                to={href}
                                data-attr="nav-chat-history-conversation"
                                buttonProps={{
                                    active,
                                    fullWidth: true,
                                    className: 'pr-0',
                                    menuItem: true,
                                }}
                                onClick={(e) => {
                                    e.preventDefault()
                                    router.actions.push(href)
                                }}
                            >
                                <AiChatListItem.Content
                                    title={item.conversation.title}
                                    status={item.conversation.status}
                                    updatedAt={item.conversation.updated_at}
                                />
                            </Link>
                        </Tooltip>
                    )}
                />
                <AiChatListItem.Trigger />
            </AiChatListItem.Group>
            <AiChatListItem.Actions conversationId={item.conversation.id} />
        </AiChatListItem.Root>
    )
})

const TaskRow = memo(function TaskRow({
    item,
    active,
}: {
    item: Extract<AiHistoryItem, { kind: 'task' }>
    active: boolean
}): JSX.Element {
    const href = TaskListItem.getHref(item.task.id)
    return (
        <TaskListItem.Root>
            <TaskListItem.Group>
                <Combobox.Item
                    value={item}
                    render={(props) => (
                        <Tooltip title={item.title} placement="right">
                            <Link
                                {...props}
                                to={href}
                                data-attr="nav-chat-history-task"
                                buttonProps={{
                                    active,
                                    fullWidth: true,
                                    className: 'pr-0',
                                    menuItem: true,
                                }}
                                onClick={(e) => {
                                    e.preventDefault()
                                    router.actions.push(href)
                                }}
                            >
                                <TaskListItem.Content task={item.task} />
                            </Link>
                        </Tooltip>
                    )}
                />
                <TaskListItem.Trigger />
            </TaskListItem.Group>
            <TaskListItem.Actions task={item.task} />
        </TaskListItem.Root>
    )
})

export function NavTabChat(): JSX.Element {
    const searchInputId = useId()
    const {
        conversationHistory,
        conversationHistoryLoading,
        currentConversationId,
        isPhaiSandboxFlagOn,
        effectivePhaiView,
    } = useValues(maxGlobalLogic)
    const {
        tasks,
        tasksLoading,
        tasksError,
        tasksNext,
        tasksLoadingMore,
        tasksSearchPending,
        searchQuery,
        taskListParams,
        assigneeFilter,
    } = useValues(tasksLogic)
    const { loadTasks, loadMoreTasks, setSearchQuery } = useActions(tasksLogic)
    const { location, searchParams } = useValues(router)
    const tasksEnabled = useFeatureFlag('TASKS') || isPhaiSandboxFlagOn
    const [chatSearch, setChatSearch] = useState('')
    const inputValue = tasksEnabled ? searchQuery : chatSearch
    const selectedTaskId =
        removeProjectIdIfPresent(location.pathname) === urls.ai() && typeof searchParams.task === 'string'
            ? searchParams.task
            : (location.pathname.match(/\/tasks\/([^/]+)/)?.[1] ?? null)

    const historyGroups = useMemo(
        () => groupAiHistory(conversationHistory, tasksEnabled ? tasks : [], tasksEnabled ? assigneeFilter : 'for_you'),
        [conversationHistory, tasks, tasksEnabled, assigneeFilter]
    )
    const initialLoading = historyGroups.length === 0 && (conversationHistoryLoading || (tasksEnabled && tasksLoading))
    // Typing moves the client-side filter at once, but the matching tasks are a debounce plus a round
    // trip behind it. Until they land, an empty filtered list means "still loading", not "none found".
    const taskResultsPending = tasksEnabled && (tasksSearchPending || tasksLoading)

    const setInputValue = (value: string): void => {
        if (tasksEnabled) {
            setSearchQuery(value)
        } else {
            setChatSearch(value)
        }
    }

    return (
        <div className="flex flex-col flex-1 overflow-hidden">
            <Combobox.Root
                items={historyGroups}
                itemToStringLabel={(item: AiHistoryItem) => item.searchableText}
                itemToStringValue={(item: AiHistoryItem) => item.searchableText}
                open
                autoHighlight
                inline
                inputValue={inputValue}
                onInputValueChange={setInputValue}
            >
                <div className="flex flex-col h-full min-h-0">
                    <div className="flex items-center gap-1 p-1 shrink-0">
                        <LemonInput
                            inputComponent={Combobox.Input}
                            id={searchInputId}
                            type="search"
                            size="small"
                            className="flex-1 min-w-0 min-h-[30px]"
                            placeholder="Filter chats"
                            aria-label="Filter chats"
                            value={inputValue}
                            onChange={setInputValue}
                            fullWidth
                            data-attr="nav-chat-search"
                            prefix={
                                <div className="flex items-center justify-center size-4 ml-[2px] mr-px">
                                    <IconSearch className="size-4" />
                                </div>
                            }
                        />
                        {tasksEnabled && <TaskAssigneeFilterMenu />}
                        <Link
                            to={urls.ai()}
                            data-attr="nav-chat-new"
                            buttonProps={{ iconOnly: true, variant: 'outline', className: 'text-ai' }}
                            // Only the new PostHog AI view puts the task composer behind this link; every
                            // other cohort lands on the legacy chat, which can only start a conversation.
                            tooltip={effectivePhaiView === 'new' ? 'New chat or task' : 'New chat'}
                        >
                            <IconPlusSmall className="size-4" />
                        </Link>
                    </div>

                    <ScrollableShadows
                        direction="vertical"
                        className="flex flex-col flex-1 min-h-0 overflow-hidden"
                        innerClassName="flex flex-col px-1 pb-4 -mx-1 scroll-pt-8 focus-visible:outline-accent -outline-offset-2"
                        styledScrollbars
                    >
                        {initialLoading ? (
                            <div className="flex flex-col gap-1 px-1">
                                <LemonSkeleton className="h-8" />
                                <LemonSkeleton className="h-8 opacity-60" />
                                <LemonSkeleton className="h-8 opacity-30" />
                            </div>
                        ) : (
                            <>
                                <Combobox.List className="flex flex-col">
                                    {(group: AiHistoryGroup) => (
                                        <Collapsible
                                            key={`${group.value}-${!!inputValue}`}
                                            defaultOpen={
                                                !!inputValue || group.value === 'Today' || historyGroups.length === 1
                                            }
                                        >
                                            <Combobox.Group items={group.items}>
                                                <Combobox.GroupLabel
                                                    render={
                                                        <Collapsible.Trigger className="sticky top-0 bg-surface-tertiary z-4 pl-3" />
                                                    }
                                                >
                                                    {group.value}
                                                </Combobox.GroupLabel>
                                                <Collapsible.Panel className="p-1 pl-3">
                                                    <Combobox.Collection>
                                                        {(item: AiHistoryItem) =>
                                                            item.kind === 'conversation' ? (
                                                                <ConversationRow
                                                                    key={item.key}
                                                                    item={item}
                                                                    active={
                                                                        !selectedTaskId &&
                                                                        item.conversation.id === currentConversationId
                                                                    }
                                                                />
                                                            ) : (
                                                                <TaskRow
                                                                    key={item.key}
                                                                    item={item}
                                                                    active={item.task.id === selectedTaskId}
                                                                />
                                                            )
                                                        }
                                                    </Combobox.Collection>
                                                </Collapsible.Panel>
                                            </Combobox.Group>
                                        </Collapsible>
                                    )}
                                </Combobox.List>
                                <div className="p-2 empty:hidden">
                                    <Combobox.Empty className="empty:hidden">
                                        <div className="flex flex-col items-center justify-center text-center py-8 text-muted border border-dashed rounded-md">
                                            {taskResultsPending ? (
                                                <span className="flex items-center gap-2 text-xs">
                                                    <Spinner className="size-3" />
                                                    Loading tasks…
                                                </span>
                                            ) : (
                                                <p className="text-xs mb-0">
                                                    {tasksEnabled ? 'No chats or tasks found' : 'No chats found'}
                                                </p>
                                            )}
                                        </div>
                                    </Combobox.Empty>
                                </div>
                            </>
                        )}
                        {/* Deliberately not gated on an empty list. A failed load keeps the previous page,
                            so that gate would leave the user reading the old filter's rows with no error
                            and no retry. Nothing else reports this failure: there is no toast for it. */}
                        {tasksEnabled && tasksError && !tasksLoading && (
                            <LoadTasksError onRetry={() => loadTasks(taskListParams)} />
                        )}
                        {tasksEnabled && tasksLoading && tasks.length === 0 && historyGroups.length > 0 && (
                            <div className="flex items-center gap-2 px-3 py-2 text-xs text-muted">
                                <Spinner className="size-3" />
                                Loading tasks…
                            </div>
                        )}
                        {tasksEnabled && tasksNext && (
                            <div className="px-2 pt-2">
                                <ButtonPrimitive
                                    fullWidth
                                    size="sm"
                                    disabled={tasksLoadingMore}
                                    onClick={() => loadMoreTasks()}
                                >
                                    {tasksLoadingMore && <Spinner className="size-3" />}
                                    {/* Wrapped so the label is not a bare text node beside the conditional
                                        spinner. A page translator swaps that node for a `<font>` element, and
                                        React then inserts the spinner against a node the DOM no longer holds. */}
                                    <span>Load more tasks</span>
                                </ButtonPrimitive>
                            </div>
                        )}
                    </ScrollableShadows>
                </div>
            </Combobox.Root>
        </div>
    )
}