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.5 KB
Raw Blame
import React from 'react'

import { IconPlay } from '@posthog/icons'
import { LemonButton } from '@posthog/lemon-ui'

import { EventType } from '~/types'

import { AIDataLoading } from '../components/AIDataLoading'
import { buildInputSourceIndices } from '../extractSessionTurns'
import { useAIData } from '../hooks/useAIData'
import { normalizeMessages } from '../messageNormalization'
import { openInPlayground } from '../playground/llmPlaygroundPromptsLogic'
import { costContextFromProperties, selectAiValue } from '../utils'
import { ConversationMessagesDisplay } from './ConversationMessagesDisplay'
import { MetadataHeader } from './MetadataHeader'

export interface ConversationDisplayProps {
    eventProperties: EventType['properties']
    eventId: string
    eventName?: string
    /** Event timestamp, needed to fetch heavy props via TraceQuery when they've been stripped from `events`. */
    eventTimestamp?: string
}

export function ConversationDisplay({
    eventProperties,
    eventId,
    eventName,
    eventTimestamp,
}: ConversationDisplayProps): JSX.Element {
    const rawInput =
        eventName === '$ai_generation' || eventName === '$ai_embedding'
            ? eventProperties.$ai_input
            : eventProperties.$ai_input_state
    const rawOutput =
        eventName === '$ai_generation'
            ? selectAiValue(eventProperties.$ai_output_choices, eventProperties.$ai_output)
            : eventName === '$ai_embedding'
              ? 'Embedding vector generated'
              : eventProperties.$ai_output_state
    const rawTools = eventProperties.$ai_tools
    const { input, output, tools, isLoading } = useAIData({
        uuid: eventId,
        input: rawInput,
        output: rawOutput,
        tools: rawTools,
        traceId: eventProperties.$ai_trace_id,
        timestamp: eventTimestamp,
    })

    const handleOpenInPlayground = (): void => {
        openInPlayground({
            model: eventProperties.$ai_model,
            provider: eventProperties.$ai_provider,
            input,
            output,
        })
    }

    const showPlaygroundButton = eventProperties.$ai_model && input

    const inputSourceIndices = React.useMemo(() => buildInputSourceIndices(input, tools), [input, tools])

    return (
        <>
            <header className="mb-2 flex justify-between items-center">
                <MetadataHeader
                    inputTokens={eventProperties.$ai_input_tokens}
                    outputTokens={eventProperties.$ai_output_tokens}
                    cacheReadTokens={eventProperties.$ai_cache_read_input_tokens}
                    cacheWriteTokens={eventProperties.$ai_cache_creation_input_tokens}
                    costContext={costContextFromProperties(eventProperties)}
                    model={eventProperties.$ai_model}
                    latency={eventProperties.$ai_latency}
                    timeToFirstToken={eventProperties.$ai_time_to_first_token}
                    isStreaming={eventProperties.$ai_stream === true}
                    timestamp={eventProperties.timestamp}
                />

                {showPlaygroundButton && (
                    <LemonButton
                        type="secondary"
                        size="small"
                        icon={<IconPlay />}
                        onClick={handleOpenInPlayground}
                        tooltip="Open in Playground"
                        data-attr="llma-playground-open-from-conversation"
                    >
                        Open in Playground
                    </LemonButton>
                )}
            </header>
            {isLoading ? (
                <AIDataLoading variant="block" />
            ) : (
                <ConversationMessagesDisplay
                    inputNormalized={normalizeMessages(input, 'user', tools).messages}
                    outputNormalized={normalizeMessages(output, 'assistant').messages}
                    inputSourceIndices={inputSourceIndices}
                    errorData={eventProperties.$ai_error}
                    httpStatus={eventProperties.$ai_http_status}
                    raisedError={eventProperties.$ai_is_error}
                    outputTokens={eventProperties.$ai_output_tokens}
                    reasoningTokens={eventProperties.$ai_reasoning_tokens}
                    textOutputTokens={eventProperties.$ai_text_output_tokens}
                    stopReason={eventProperties.$ai_stop_reason}
                    bordered
                    traceId={eventProperties.$ai_trace_id}
                    eventId={eventId}
                />
            )}
        </>
    )
}