|
| 1 | +import styled from '@emotion/styled'; |
| 2 | + |
| 3 | +import {Alert} from 'sentry/components/core/alert'; |
| 4 | +import {Badge} from 'sentry/components/core/badge'; |
| 5 | +import {Button} from 'sentry/components/core/button'; |
| 6 | +import EmptyMessage from 'sentry/components/emptyMessage'; |
| 7 | +import ErrorBoundary from 'sentry/components/errorBoundary'; |
| 8 | +import LoadingIndicator from 'sentry/components/loadingIndicator'; |
| 9 | +import {useReplayContext} from 'sentry/components/replays/replayContext'; |
| 10 | +import {t} from 'sentry/locale'; |
| 11 | +import {space} from 'sentry/styles/space'; |
| 12 | +import type {ApiQueryKey} from 'sentry/utils/queryClient'; |
| 13 | +import {useApiQuery} from 'sentry/utils/queryClient'; |
| 14 | +import {decodeScalar} from 'sentry/utils/queryString'; |
| 15 | +import useLocationQuery from 'sentry/utils/url/useLocationQuery'; |
| 16 | +import useOrganization from 'sentry/utils/useOrganization'; |
| 17 | +import useProjectFromId from 'sentry/utils/useProjectFromId'; |
| 18 | +import BreadcrumbRow from 'sentry/views/replays/detail/breadcrumbs/breadcrumbRow'; |
| 19 | +import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight'; |
| 20 | +import TabItemContainer from 'sentry/views/replays/detail/tabItemContainer'; |
| 21 | +import TimestampButton from 'sentry/views/replays/detail/timestampButton'; |
| 22 | +import type {ReplayRecord} from 'sentry/views/replays/types'; |
| 23 | + |
| 24 | +interface Props { |
| 25 | + replayRecord: ReplayRecord | undefined; |
| 26 | +} |
| 27 | + |
| 28 | +interface SummaryResponse { |
| 29 | + data: { |
| 30 | + summary: string; |
| 31 | + time_ranges: Array<{period_end: number; period_start: number; period_title: string}>; |
| 32 | + title: string; |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +function createAISummaryQueryKey( |
| 37 | + orgSlug: string, |
| 38 | + projectSlug: string | undefined, |
| 39 | + replayId: string |
| 40 | +): ApiQueryKey { |
| 41 | + return [ |
| 42 | + `/projects/${orgSlug}/${projectSlug}/replays/${replayId}/summarize/breadcrumbs/`, |
| 43 | + ]; |
| 44 | +} |
| 45 | + |
| 46 | +export default function Ai({replayRecord}: Props) { |
| 47 | + return ( |
| 48 | + <PaddedFluidHeight> |
| 49 | + <TabItemContainer data-test-id="replay-details-ai-summary-tab"> |
| 50 | + <ErrorBoundary mini> |
| 51 | + <AiContent replayRecord={replayRecord} /> |
| 52 | + </ErrorBoundary> |
| 53 | + </TabItemContainer> |
| 54 | + </PaddedFluidHeight> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +function AiContent({replayRecord}: Props) { |
| 59 | + const {replay} = useReplayContext(); |
| 60 | + const organization = useOrganization(); |
| 61 | + const {project: project_id} = useLocationQuery({ |
| 62 | + fields: {project: decodeScalar}, |
| 63 | + }); |
| 64 | + const project = useProjectFromId({project_id}); |
| 65 | + |
| 66 | + const { |
| 67 | + data: summaryData, |
| 68 | + isPending, |
| 69 | + isError, |
| 70 | + isRefetching, |
| 71 | + refetch, |
| 72 | + } = useApiQuery<SummaryResponse>( |
| 73 | + createAISummaryQueryKey(organization.slug, project?.slug, replayRecord?.id ?? ''), |
| 74 | + { |
| 75 | + staleTime: 0, |
| 76 | + enabled: Boolean( |
| 77 | + replayRecord?.id && |
| 78 | + project?.slug && |
| 79 | + organization.features.includes('replay-ai-summary') |
| 80 | + ), |
| 81 | + retry: false, |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + if (!organization.features.includes('replay-ai-summary')) { |
| 86 | + return ( |
| 87 | + <SummaryContainer> |
| 88 | + <Alert type="info"> |
| 89 | + {t('Replay AI summary is not available for this organization.')} |
| 90 | + </Alert> |
| 91 | + </SummaryContainer> |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + if (isPending || isRefetching) { |
| 96 | + return ( |
| 97 | + <LoadingContainer> |
| 98 | + <LoadingIndicator /> |
| 99 | + </LoadingContainer> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + if (isError) { |
| 104 | + return ( |
| 105 | + <SummaryContainer> |
| 106 | + <Alert type="error">{t('Failed to load AI summary')}</Alert>; |
| 107 | + </SummaryContainer> |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + if (!summaryData) { |
| 112 | + return ( |
| 113 | + <SummaryContainer> |
| 114 | + <Alert type="info">{t('No summary available for this replay.')}</Alert> |
| 115 | + </SummaryContainer> |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + const chapterData = summaryData?.data.time_ranges.map( |
| 120 | + ({period_title, period_start, period_end}) => ({ |
| 121 | + title: period_title, |
| 122 | + start: period_start * 1000, |
| 123 | + end: period_end * 1000, |
| 124 | + breadcrumbs: |
| 125 | + replay |
| 126 | + ?.getChapterFrames() |
| 127 | + .filter( |
| 128 | + breadcrumb => |
| 129 | + breadcrumb.timestampMs >= period_start * 1000 && |
| 130 | + breadcrumb.timestampMs <= period_end * 1000 |
| 131 | + ) ?? [], |
| 132 | + }) |
| 133 | + ); |
| 134 | + |
| 135 | + return ( |
| 136 | + <ErrorBoundary mini> |
| 137 | + <SummaryContainer> |
| 138 | + <SummaryHeader> |
| 139 | + <SummaryHeaderTitle> |
| 140 | + <span>{t('Replay Summary')}</span> |
| 141 | + <Badge type="internal">{t('Internal')}</Badge> |
| 142 | + </SummaryHeaderTitle> |
| 143 | + <Button priority="primary" size="xs" onClick={() => refetch()}> |
| 144 | + {t('Regenerate')} |
| 145 | + </Button> |
| 146 | + </SummaryHeader> |
| 147 | + <SummaryText>{summaryData.data.summary}</SummaryText> |
| 148 | + <div> |
| 149 | + {chapterData.map(({title, start, breadcrumbs}, i) => ( |
| 150 | + <Details key={i}> |
| 151 | + <Summary> |
| 152 | + <SummaryTitle> |
| 153 | + <span>{title}</span> |
| 154 | + |
| 155 | + <ReplayTimestamp> |
| 156 | + <TimestampButton |
| 157 | + startTimestampMs={replay?.getStartTimestampMs() ?? 0} |
| 158 | + timestampMs={start} |
| 159 | + /> |
| 160 | + </ReplayTimestamp> |
| 161 | + </SummaryTitle> |
| 162 | + </Summary> |
| 163 | + <div> |
| 164 | + {!breadcrumbs.length && ( |
| 165 | + <EmptyMessage>{t('No breadcrumbs for this chapter')}</EmptyMessage> |
| 166 | + )} |
| 167 | + {breadcrumbs.map((breadcrumb, j) => ( |
| 168 | + <BreadcrumbRow |
| 169 | + frame={breadcrumb} |
| 170 | + index={j} |
| 171 | + onClick={() => {}} |
| 172 | + onInspectorExpanded={() => {}} |
| 173 | + onShowSnippet={() => {}} |
| 174 | + showSnippet={false} |
| 175 | + allowShowSnippet={false} |
| 176 | + startTimestampMs={breadcrumb.timestampMs} |
| 177 | + key={`breadcrumb-${j}`} |
| 178 | + style={{}} |
| 179 | + /> |
| 180 | + ))} |
| 181 | + </div> |
| 182 | + </Details> |
| 183 | + ))} |
| 184 | + </div> |
| 185 | + </SummaryContainer> |
| 186 | + </ErrorBoundary> |
| 187 | + ); |
| 188 | +} |
| 189 | + |
| 190 | +const PaddedFluidHeight = styled(FluidHeight)` |
| 191 | + padding-top: ${space(1)}; |
| 192 | +`; |
| 193 | + |
| 194 | +const LoadingContainer = styled('div')` |
| 195 | + display: flex; |
| 196 | + justify-content: center; |
| 197 | + padding: ${space(4)}; |
| 198 | +`; |
| 199 | + |
| 200 | +const SummaryContainer = styled('div')` |
| 201 | + padding: ${space(2)}; |
| 202 | + overflow: auto; |
| 203 | +`; |
| 204 | + |
| 205 | +const SummaryHeader = styled('h3')` |
| 206 | + display: flex; |
| 207 | + align-items: center; |
| 208 | + gap: ${space(1)}; |
| 209 | + justify-content: space-between; |
| 210 | +`; |
| 211 | + |
| 212 | +const SummaryHeaderTitle = styled('div')` |
| 213 | + display: flex; |
| 214 | + align-items: center; |
| 215 | + gap: ${space(1)}; |
| 216 | +`; |
| 217 | + |
| 218 | +const Details = styled('details')` |
| 219 | + &[open] > summary::before { |
| 220 | + content: '-'; |
| 221 | + } |
| 222 | +`; |
| 223 | + |
| 224 | +const Summary = styled('summary')` |
| 225 | + cursor: pointer; |
| 226 | + display: list-item; |
| 227 | + padding: ${space(1)} 0; |
| 228 | + font-size: ${p => p.theme.fontSizeLarge}; |
| 229 | +
|
| 230 | + /* sorry */ |
| 231 | + &:focus-visible { |
| 232 | + outline: none; |
| 233 | + } |
| 234 | +
|
| 235 | + list-style-type: none; |
| 236 | + &::-webkit-details-marker { |
| 237 | + display: none; |
| 238 | + } |
| 239 | + &::before { |
| 240 | + content: '+'; |
| 241 | + float: left; |
| 242 | + display: inline-block; |
| 243 | + width: 14px; |
| 244 | + margin-right: ${space(1)}; |
| 245 | + font-size: ${p => p.theme.fontSizeExtraLarge}; |
| 246 | + } |
| 247 | +`; |
| 248 | + |
| 249 | +const SummaryTitle = styled('div')` |
| 250 | + display: flex; |
| 251 | + align-items: center; |
| 252 | + gap: ${space(1)}; |
| 253 | + justify-content: space-between; |
| 254 | +`; |
| 255 | + |
| 256 | +const SummaryText = styled('p')` |
| 257 | + line-height: 1.6; |
| 258 | + white-space: pre-wrap; |
| 259 | +`; |
| 260 | + |
| 261 | +// Copied from breadcrumbItem |
| 262 | +const ReplayTimestamp = styled('div')` |
| 263 | + color: ${p => p.theme.textColor}; |
| 264 | + font-size: ${p => p.theme.fontSizeSmall}; |
| 265 | +`; |
0 commit comments