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.

feat(replay-vision): fix chart and copy summaries (#74073)

Hayne Ryu59ceb3badd05
diff --git a/products/replay_vision/frontend/components/ObservationCard.tsx b/products/replay_vision/frontend/components/ObservationCard.tsx
index 0e960f24558c..0ab820dd3bda 100644
--- a/products/replay_vision/frontend/components/ObservationCard.tsx
+++ b/products/replay_vision/frontend/components/ObservationCard.tsx
@@ -1,8 +1,9 @@
import { useValues } from 'kea'
-import { IconRefresh, IconRewindPlay, IconSparkles } from '@posthog/icons'
+import { IconCopy, IconRefresh, IconRewindPlay, IconSparkles } from '@posthog/icons'
import { LemonButton, LemonTag, Link, Spinner, Tooltip } from '@posthog/lemon-ui'
+import { copyToClipboard } from 'lib/utils/copyToClipboard'
import { colonDelimitedDuration } from 'lib/utils/durations'
import { urls } from 'scenes/urls'
@@ -19,7 +20,7 @@ import {
scannerTypeLabel,
} from '../replay_scanners/types'
import { getReplayVisionEditDisabledReason } from '../utils/accessControl'
-import { parseCitedSegments } from '../utils/citations'
+import { citedTextToPlainText, parseCitedSegments } from '../utils/citations'
import { ObservationProgressBar } from './ObservationProgressBar'
export function ObservationStatusTag({
@@ -121,6 +122,7 @@ export function ObservationPrimaryOutput({
showPrompt = true,
onSeek,
expandSummary = false,
+ copyable = false,
}: {
observation: ReplayObservationApi
compact?: boolean
@@ -129,6 +131,8 @@ export function ObservationPrimaryOutput({
onSeek?: (timestampMs: number) => void
/** When true (dock/detail), summarizer body wraps in full; when false (table), single-line truncate. */
expandSummary?: boolean
+ /** Shows a copy button on summarizer output: the clipboard gets the title plus the summary with citations as plain timestamps. */
+ copyable?: boolean
}): JSX.Element | null {
const snapshot = observation.scanner_snapshot
const result = readResult(observation)
@@ -168,9 +172,31 @@ export function ObservationPrimaryOutput({
if (scannerType === 'summarizer') {
const title = typeof result.title === 'string' ? result.title : null
const summary = typeof result.summary === 'string' ? result.summary : null
+ const showCopy = copyable && summary !== null
return (
<div className="flex flex-col gap-1">
- {title && <span className="font-semibold text-sm">{title}</span>}
+ {(title || showCopy) && (
+ <div className="flex items-start justify-between gap-2">
+ {title && <span className="font-semibold text-sm">{title}</span>}
+ {showCopy && (
+ <LemonButton
+ size="xsmall"
+ icon={<IconCopy />}
+ tooltip="Copy summary"
+ className="ml-auto -my-1"
+ onClick={() =>
+ void copyToClipboard(
+ [title, citedTextToPlainText(summary, result.summary_segments)]
+ .filter(Boolean)
+ .join('\n\n'),
+ 'summary'
+ )
+ }
+ data-attr="vision-copy-summary"
+ />
+ )}
+ </div>
+ )}
{summary && (
<span className={summaryClass}>
<CitedText text={summary} segments={result.summary_segments} onSeek={onSeek} />
@@ -412,7 +438,7 @@ export function ObservationDockCard({
)}
{observation.status === 'succeeded' && snapshot && result && (
- <ObservationPrimaryOutput observation={observation} compact onSeek={onSeek} expandSummary />
+ <ObservationPrimaryOutput observation={observation} compact onSeek={onSeek} expandSummary copyable />
)}
{(observation.status === 'pending' || observation.status === 'running') && (
diff --git a/products/replay_vision/frontend/observations/ReplayObservation.tsx b/products/replay_vision/frontend/observations/ReplayObservation.tsx
index df3136ca92f9..1ebbd56f6cc2 100644
--- a/products/replay_vision/frontend/observations/ReplayObservation.tsx
+++ b/products/replay_vision/frontend/observations/ReplayObservation.tsx
@@ -407,6 +407,7 @@ export function ReplayObservationSceneComponent(): JSX.Element {
observation={observation}
showPrompt={false}
onSeek={seekEmbeddedPlayer}
+ copyable
/>
</LabeledRow>
{observation.completed_at && (
diff --git a/products/replay_vision/frontend/replay_scanners/components/VisionUsageTab.tsx b/products/replay_vision/frontend/replay_scanners/components/VisionUsageTab.tsx
index 74da2b30c558..8ac4b0db778f 100644
--- a/products/replay_vision/frontend/replay_scanners/components/VisionUsageTab.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/VisionUsageTab.tsx
@@ -8,12 +8,12 @@ import { LemonTableColumns } from 'lib/lemon-ui/LemonTable'
import { urls } from 'scenes/urls'
import { InsightVizNode, NodeKind, ProductKey } from '~/queries/schema/schema-general'
-import { ChartDisplayType, InsightLogicProps, PropertyMathType } from '~/types'
+import { BaseMathType, ChartDisplayType, InsightLogicProps, PropertyFilterType, PropertyOperator } from '~/types'
import { visionQuotaLogic } from '../../logics/visionQuotaLogic'
import { formatCredits } from '../../utils/credits'
import { exhaustionForecast, hasCreditLimit, projectQuota } from '../../utils/quotaProjection'
-import { ReplayScanner, modelName } from '../types'
+import { OBSERVATION_CREDITS_BY_MODEL, ReplayScanner, modelName } from '../types'
import { SpendChartInterval, visionUsageLogic } from '../visionUsageLogic'
import { VisionInsightChart } from './VisionInsightChart'
@@ -33,6 +33,27 @@ const SPEND_CHART_DATE_FROM: Record<Exclude<SpendChartInterval, 'day'>, string>
year: 'all',
}
+// Spend is counted per model and priced in the formula rather than summing the `credits` event property:
+// events emitted before that property existed would otherwise read as zero spend. Pricing counts at current
+// rates matches how the table's totals are computed. Retired models missing from the price table are omitted.
+const SPEND_CHART_MODEL_PRICES = Object.entries(OBSERVATION_CREDITS_BY_MODEL)
+const SPEND_CHART_SERIES = SPEND_CHART_MODEL_PRICES.map(([model]) => ({
+ kind: NodeKind.EventsNode as const,
+ event: RECORDING_OBSERVED_EVENT,
+ math: BaseMathType.TotalCount,
+ properties: [
+ {
+ type: PropertyFilterType.Event as const,
+ key: 'model_used',
+ operator: PropertyOperator.Exact as const,
+ value: model,
+ },
+ ],
+}))
+const SPEND_CHART_FORMULA = `(${SPEND_CHART_MODEL_PRICES.map(
+ ([, credits], index) => `${String.fromCharCode(65 + index)}*${credits}`
+).join(' + ')}) / 100`
+
export function VisionUsageTab(): JSX.Element {
const { usageScanners, usageScannersLoading, spendChartInterval } = useValues(visionUsageLogic)
const { setSpendChartInterval } = useActions(visionUsageLogic)
@@ -56,18 +77,11 @@ export function VisionUsageTab(): JSX.Element {
kind: NodeKind.InsightVizNode,
source: {
kind: NodeKind.TrendsQuery,
- series: [
- {
- kind: NodeKind.EventsNode,
- event: RECORDING_OBSERVED_EVENT,
- math: PropertyMathType.Sum,
- math_property: 'credits',
- },
- ],
+ series: SPEND_CHART_SERIES,
trendsFilter: {
display: ChartDisplayType.ActionsLineGraph,
- // The credits event property is 1 credit = $0.01; chart in dollars to match the table.
- formulaNodes: [{ formula: 'A / 100', custom_name: 'Spend' }],
+ // 1 credit = $0.01; the /100 in the formula charts dollars to match the table.
+ formulaNodes: [{ formula: SPEND_CHART_FORMULA, custom_name: 'Spend' }],
aggregationAxisPrefix: '$',
},
dateRange: {
diff --git a/products/replay_vision/frontend/utils/citations.test.ts b/products/replay_vision/frontend/utils/citations.test.ts
index 0a48f0134aaf..bc80bbb1e391 100644
--- a/products/replay_vision/frontend/utils/citations.test.ts
+++ b/products/replay_vision/frontend/utils/citations.test.ts
@@ -1,4 +1,4 @@
-import { type Segment, parseCitedSegments } from './citations'
+import { type Segment, citedTextToPlainText, parseCitedSegments } from './citations'
describe('parseCitedSegments', () => {
const text = (value: string): Segment => ({ kind: 'text', value })
@@ -39,3 +39,38 @@ describe('parseCitedSegments', () => {
expect(parseCitedSegments(inputText, segments)).toEqual(expected)
})
})
+
+describe('citedTextToPlainText', () => {
+ it.each<{ name: string; text: string; segments: unknown; expected: string }>([
+ {
+ name: 'renders a leaked marker as a readable timestamp',
+ text: 'The user retried (t 12) twice.',
+ segments: undefined,
+ expected: 'The user retried (00:12) twice.',
+ },
+ {
+ name: 'splits comma-joined markers into one timestamp each',
+ text: 'Clicked twice (t 39, t 57) before it responded',
+ segments: [],
+ expected: 'Clicked twice (00:39) (00:57) before it responded',
+ },
+ {
+ name: 'serializes persisted chip segments',
+ text: 'ignored when segments exist',
+ segments: [
+ { kind: 'text', value: 'Abandoned the cart' },
+ { kind: 'chip', timestamp_ms: 161_000 },
+ { kind: 'text', value: ' and left.' },
+ ],
+ expected: 'Abandoned the cart (02:41) and left.',
+ },
+ {
+ name: 'keeps text without citations untouched',
+ text: 'No citations here.',
+ segments: [],
+ expected: 'No citations here.',
+ },
+ ])('$name', ({ text, segments, expected }) => {
+ expect(citedTextToPlainText(text, segments)).toBe(expected)
+ })
+})
diff --git a/products/replay_vision/frontend/utils/citations.ts b/products/replay_vision/frontend/utils/citations.ts
index a6ea5802b9c5..cf330d850fe0 100644
--- a/products/replay_vision/frontend/utils/citations.ts
+++ b/products/replay_vision/frontend/utils/citations.ts
@@ -1,3 +1,5 @@
+import { colonDelimitedDuration } from 'lib/utils/durations'
+
// `uuid` is legacy (only old event-uuid citations carry it). Timestamp citations use `timestamp_ms` alone.
export type Segment = { kind: 'text'; value: string } | { kind: 'chip'; timestamp_ms: number; uuid?: string }
@@ -53,3 +55,21 @@ export function parseCitedSegments(text: string, segments: unknown): Segment[] {
}
return persisted.flatMap((segment) => (segment.kind === 'text' ? splitLeakedCitations(segment.value) : [segment]))
}
+
+/** Plain-text rendering of a cited field for the clipboard: citation chips become readable `(mm:ss)` timestamps. */
+export function citedTextToPlainText(text: string, segments: unknown): string {
+ const list = parseCitedSegments(text, segments)
+ if (list.length === 0) {
+ return text
+ }
+ let out = ''
+ for (const segment of list) {
+ if (segment.kind === 'text') {
+ out += segment.value
+ continue
+ }
+ const label = colonDelimitedDuration(Math.max(0, Math.floor(segment.timestamp_ms / 1000)), null)
+ out += `${out && !/\s$/.test(out) ? ' ' : ''}(${label})`
+ }
+ return out
+}