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.

4.3 KB
Raw Blame
import { IconCopy, IconWarning } from '@posthog/icons'
import { LemonButton, Spinner } from '@posthog/lemon-ui'

import { copyToClipboard } from 'lib/utils/copyToClipboard'

import { MarkdownMessage } from '../messages/MarkdownMessage'
import type { RunConnectionState } from '../types/streamTypes'
import { Activity } from './ActivityPrimitives'
import { QuillRunAlertCard } from './quill/QuillRunAlertCard'
import { useQuillThread } from './quill/quillThreadContext'
import { RUN_ALERT_TITLES } from './runAlertTitles'

interface RunAlertActivityProps extends RunConnectionState {
    /** Stable id for the markdown message. Defaults per kind. */
    id?: string
    onRetry?: () => void
    /** A follow-up message failed to reach the agent because of this error. */
    undeliveredMessage?: boolean
    /** Text for the "Copy details" action: run and task ids, trace id, and the reason. */
    copyDetails?: string
}

/**
 * The card for every run stream/connection alert, used in two places:
 * - the thread footer (selector-driven, `runStreamLogic.runConnectionState`) for the live `reconnecting`
 *   line (attempt counter + backoff) and its terminal `connection_failed` state, and
 * - inline in the thread (`ThreadRow` via `RunErrorRow`) for a stopped run, a crash, or an undelivered message.
 *
 * Reconnecting stays a quiet `Activity` line. Every failed kind is a bordered card with the reason and
 * the actions under one left edge; red is limited to the icon.
 */
export function RunAlertActivity({
    kind,
    id,
    attempt,
    maxAttempts,
    message,
    retryable,
    onRetry,
    undeliveredMessage,
    copyDetails,
}: RunAlertActivityProps): JSX.Element {
    const activityId = id ?? `run-alert-${kind}`
    const quill = useQuillThread()

    if (kind === 'reconnecting') {
        const subtitle = attempt && maxAttempts ? `Attempt ${attempt} of ${maxAttempts}` : 'Loading…'
        return (
            <Activity
                id={activityId}
                title={RUN_ALERT_TITLES.reconnecting}
                subtitle={subtitle}
                status="in_progress"
                icon={<Spinner className="size-3" />}
            />
        )
    }

    if (quill) {
        return (
            <QuillRunAlertCard
                kind={kind}
                id={activityId}
                message={message}
                retryable={retryable}
                onRetry={onRetry}
                undeliveredMessage={undeliveredMessage}
                copyDetails={copyDetails}
            />
        )
    }

    // A stopped run gets a bordered card, not a tinted banner: red only on the icon, and the reason and
    // the actions share the title's left edge.
    return (
        <div
            className="max-w-4/5 rounded border border-border-secondary bg-surface-primary px-3 py-2.5 flex flex-col gap-1.5"
            data-attr="run-alert-card"
        >
            <div className="flex items-center gap-2 font-semibold">
                <IconWarning className="size-4 shrink-0 text-danger" />
                <span className="grow">{RUN_ALERT_TITLES[kind]}</span>
                {copyDetails && (
                    <LemonButton
                        type="tertiary"
                        size="xsmall"
                        className="-my-1 -mr-1"
                        icon={<IconCopy />}
                        tooltip="Copy run details for support"
                        onClick={() => void copyToClipboard(copyDetails, 'run details')}
                        data-attr="run-error-copy-details"
                    />
                )}
            </div>
            <div className="pl-6 flex flex-col gap-1 text-secondary break-words min-w-0">
                {message ? <MarkdownMessage content={message} id={`${activityId}-message`} /> : null}
                {undeliveredMessage && kind !== 'message_undelivered' ? (
                    <div>Your last message was not delivered.</div>
                ) : null}
            </div>
            {kind === 'connection_failed' && retryable && onRetry && (
                <div className="pl-6 flex flex-wrap items-center gap-2">
                    <LemonButton type="secondary" size="small" onClick={onRetry} data-attr="agent-stream-retry">
                        Retry
                    </LemonButton>
                </div>
            )}
        </div>
    )
}