| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {CommitRev, CommitStackState} from '../commitStackState'; |
| b69ab31 | | | 9 | import type {DiffCommit, PartiallySelectedDiffCommit} from '../diffSplitTypes'; |
| b69ab31 | | | 10 | import { |
| b69ab31 | | | 11 | bumpStackEditMetric, |
| b69ab31 | | | 12 | findStartEndRevs, |
| b69ab31 | | | 13 | SplitRangeRecord, |
| b69ab31 | | | 14 | type UseStackEditState, |
| b69ab31 | | | 15 | } from './stackEditState'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | import * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 18 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 19 | import {ButtonWithDropdownTooltip} from 'isl-components/ButtonWithDropdownTooltip'; |
| b69ab31 | | | 20 | import {InlineErrorBadge} from 'isl-components/ErrorNotice'; |
| b69ab31 | | | 21 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 22 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 23 | import {type ForwardedRef, forwardRef, useEffect, useRef, useState} from 'react'; |
| b69ab31 | | | 24 | import {randomId} from 'shared/utils'; |
| b69ab31 | | | 25 | import {MinHeightTextField} from '../../CommitInfoView/MinHeightTextField'; |
| b69ab31 | | | 26 | import {Column} from '../../ComponentUtils'; |
| b69ab31 | | | 27 | import {useGeneratedFileStatuses} from '../../GeneratedFile'; |
| b69ab31 | | | 28 | import {Internal} from '../../Internal'; |
| b69ab31 | | | 29 | import {tracker} from '../../analytics'; |
| b69ab31 | | | 30 | import {useFeatureFlagSync} from '../../featureFlags'; |
| b69ab31 | | | 31 | import {t, T} from '../../i18n'; |
| b69ab31 | | | 32 | import {GeneratedStatus} from '../../types'; |
| b69ab31 | | | 33 | import {applyDiffSplit, diffCommit} from '../diffSplit'; |
| b69ab31 | | | 34 | import {next} from '../revMath'; |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | const styles = stylex.create({ |
| b69ab31 | | | 37 | full: { |
| b69ab31 | | | 38 | minWidth: '300px', |
| b69ab31 | | | 39 | width: '100%', |
| b69ab31 | | | 40 | }, |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | type AISplitButtonProps = { |
| b69ab31 | | | 44 | stackEdit: UseStackEditState; |
| b69ab31 | | | 45 | commitStack: CommitStackState; |
| b69ab31 | | | 46 | subStack: CommitStackState; |
| b69ab31 | | | 47 | rev: CommitRev; |
| b69ab31 | | | 48 | }; |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | type AISplitButtonLoadingState = |
| b69ab31 | | | 51 | | {type: 'READY'} |
| b69ab31 | | | 52 | | {type: 'LOADING'; id: string} |
| b69ab31 | | | 53 | | {type: 'ERROR'; error: Error}; |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | export const AISplitButton = forwardRef( |
| b69ab31 | | | 56 | ( |
| b69ab31 | | | 57 | {stackEdit, commitStack, subStack, rev}: AISplitButtonProps, |
| b69ab31 | | | 58 | ref: ForwardedRef<HTMLButtonElement>, |
| b69ab31 | | | 59 | ) => { |
| b69ab31 | | | 60 | const {splitCommitWithAI} = Internal; |
| b69ab31 | | | 61 | const enableAICommitSplit = |
| b69ab31 | | | 62 | useFeatureFlagSync(Internal.featureFlags?.AICommitSplit) && splitCommitWithAI != null; |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | const [loadingState, setLoadingState] = useState<AISplitButtonLoadingState>({type: 'READY'}); |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | // Reset state if commitStack changes while in LOADING state. E.g., user manually updated commits locally. |
| b69ab31 | | | 67 | useEffect(() => { |
| b69ab31 | | | 68 | if (loadingState.type === 'LOADING') { |
| b69ab31 | | | 69 | setLoadingState({type: 'READY'}); |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | return () => { |
| b69ab31 | | | 72 | // Cancel loading state when unmounted |
| b69ab31 | | | 73 | setLoadingState({type: 'READY'}); |
| b69ab31 | | | 74 | }; |
| b69ab31 | | | 75 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| b69ab31 | | | 76 | }, [commitStack]); // Triggered when commitStack changes |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | const applyNewDiffSplitCommits = ( |
| b69ab31 | | | 79 | subStack: CommitStackState, |
| b69ab31 | | | 80 | rev: CommitRev, |
| b69ab31 | | | 81 | commits: ReadonlyArray<PartiallySelectedDiffCommit>, |
| b69ab31 | | | 82 | ) => { |
| b69ab31 | | | 83 | const [startRev, endRev] = findStartEndRevs(stackEdit); |
| b69ab31 | | | 84 | if (startRev != null && endRev != null) { |
| b69ab31 | | | 85 | // Replace the current, single rev with the new stack, which might have multiple revs. |
| b69ab31 | | | 86 | const newSubStack = applyDiffSplit(subStack, rev, commits); |
| b69ab31 | | | 87 | // Replace the [start, end+1] range with the new stack in the commit stack. |
| b69ab31 | | | 88 | const newCommitStack = commitStack.applySubStack(startRev, next(endRev), newSubStack); |
| b69ab31 | | | 89 | // Find the new split range. |
| b69ab31 | | | 90 | const endOffset = newCommitStack.size - commitStack.size; |
| b69ab31 | | | 91 | const startKey = newCommitStack.get(rev)?.key ?? ''; |
| b69ab31 | | | 92 | const endKey = newCommitStack.get(next(rev, endOffset))?.key ?? ''; |
| b69ab31 | | | 93 | const splitRange = SplitRangeRecord({startKey, endKey}); |
| b69ab31 | | | 94 | // Update the main stack state. |
| b69ab31 | | | 95 | stackEdit.push(newCommitStack, {name: 'splitWithAI'}, splitRange); |
| b69ab31 | | | 96 | } |
| b69ab31 | | | 97 | }; |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | const diffWithoutGeneratedFiles = useDiffWithoutGeneratedFiles(subStack, rev); |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | const [guidanceToAI, setGuidanceToAI] = useState(''); |
| b69ab31 | | | 102 | const fetch = async () => { |
| b69ab31 | | | 103 | if (loadingState.type === 'LOADING' || splitCommitWithAI == null) { |
| b69ab31 | | | 104 | return; |
| b69ab31 | | | 105 | } |
| b69ab31 | | | 106 | if (diffWithoutGeneratedFiles.files.length === 0) { |
| b69ab31 | | | 107 | return; |
| b69ab31 | | | 108 | } |
| b69ab31 | | | 109 | |
| b69ab31 | | | 110 | bumpStackEditMetric('clickedAiSplit'); |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | const id = randomId(); |
| b69ab31 | | | 113 | setLoadingState({type: 'LOADING', id}); |
| b69ab31 | | | 114 | const args = |
| b69ab31 | | | 115 | guidanceToAI == null || guidanceToAI.trim() === '' |
| b69ab31 | | | 116 | ? {} |
| b69ab31 | | | 117 | : { |
| b69ab31 | | | 118 | user_prompt: guidanceToAI.trim(), |
| b69ab31 | | | 119 | }; |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | try { |
| b69ab31 | | | 122 | const result: ReadonlyArray<PartiallySelectedDiffCommit> = await tracker.operation( |
| b69ab31 | | | 123 | 'AISplitButtonClick', |
| b69ab31 | | | 124 | 'SplitSuggestionError', |
| b69ab31 | | | 125 | undefined, |
| b69ab31 | | | 126 | () => splitCommitWithAI(diffWithoutGeneratedFiles, args), |
| b69ab31 | | | 127 | ); |
| b69ab31 | | | 128 | setLoadingState(prev => { |
| b69ab31 | | | 129 | if (prev.type === 'LOADING' && prev.id === id) { |
| b69ab31 | | | 130 | const commits = result.filter(c => c.files.length > 0); |
| b69ab31 | | | 131 | if (commits.length > 0) { |
| b69ab31 | | | 132 | applyNewDiffSplitCommits(subStack, rev, commits); |
| b69ab31 | | | 133 | } |
| b69ab31 | | | 134 | return {type: 'READY'}; |
| b69ab31 | | | 135 | } |
| b69ab31 | | | 136 | return prev; |
| b69ab31 | | | 137 | }); |
| b69ab31 | | | 138 | } catch (err) { |
| b69ab31 | | | 139 | if (err != null) { |
| b69ab31 | | | 140 | setLoadingState(prev => { |
| b69ab31 | | | 141 | if (prev.type === 'LOADING' && prev.id === id) { |
| b69ab31 | | | 142 | return {type: 'ERROR', error: err as Error}; |
| b69ab31 | | | 143 | } |
| b69ab31 | | | 144 | return prev; |
| b69ab31 | | | 145 | }); |
| b69ab31 | | | 146 | return; |
| b69ab31 | | | 147 | } |
| b69ab31 | | | 148 | } |
| b69ab31 | | | 149 | }; |
| b69ab31 | | | 150 | |
| b69ab31 | | | 151 | const cancel = () => { |
| b69ab31 | | | 152 | setLoadingState(prev => { |
| b69ab31 | | | 153 | const {type} = prev; |
| b69ab31 | | | 154 | if (type === 'LOADING' || type === 'ERROR') { |
| b69ab31 | | | 155 | return {type: 'READY'}; |
| b69ab31 | | | 156 | } |
| b69ab31 | | | 157 | return prev; |
| b69ab31 | | | 158 | }); |
| b69ab31 | | | 159 | }; |
| b69ab31 | | | 160 | |
| b69ab31 | | | 161 | if (!enableAICommitSplit) { |
| b69ab31 | | | 162 | return null; |
| b69ab31 | | | 163 | } |
| b69ab31 | | | 164 | |
| b69ab31 | | | 165 | switch (loadingState.type) { |
| b69ab31 | | | 166 | case 'READY': |
| b69ab31 | | | 167 | return ( |
| b69ab31 | | | 168 | <Tooltip title={t('Split this commit using AI')} placement="bottom"> |
| b69ab31 | | | 169 | <ButtonWithDropdownTooltip |
| b69ab31 | | | 170 | label={<T>AI Split</T>} |
| b69ab31 | | | 171 | data-testid="cwd-dropdown-button" |
| b69ab31 | | | 172 | kind="icon" |
| b69ab31 | | | 173 | icon={<Icon icon="sparkle" />} |
| b69ab31 | | | 174 | onClick={fetch} |
| b69ab31 | | | 175 | ref={ref} |
| b69ab31 | | | 176 | tooltip={ |
| b69ab31 | | | 177 | <DetailsDropdown |
| b69ab31 | | | 178 | loadingState={loadingState} |
| b69ab31 | | | 179 | submit={fetch} |
| b69ab31 | | | 180 | guidanceToAI={guidanceToAI} |
| b69ab31 | | | 181 | setGuidanceToAI={setGuidanceToAI} |
| b69ab31 | | | 182 | /> |
| b69ab31 | | | 183 | } |
| b69ab31 | | | 184 | /> |
| b69ab31 | | | 185 | </Tooltip> |
| b69ab31 | | | 186 | ); |
| b69ab31 | | | 187 | case 'LOADING': |
| b69ab31 | | | 188 | return ( |
| b69ab31 | | | 189 | <Tooltip title={t('Split is working, click to cancel')} placement="bottom"> |
| b69ab31 | | | 190 | <ButtonWithDropdownTooltip |
| b69ab31 | | | 191 | label={<T>Splitting</T>} |
| b69ab31 | | | 192 | data-testid="cwd-dropdown-button" |
| b69ab31 | | | 193 | kind="icon" |
| b69ab31 | | | 194 | icon={<Icon icon="loading" />} |
| b69ab31 | | | 195 | onClick={cancel} |
| b69ab31 | | | 196 | tooltip={ |
| b69ab31 | | | 197 | <DetailsDropdown |
| b69ab31 | | | 198 | loadingState={loadingState} |
| b69ab31 | | | 199 | submit={cancel} |
| b69ab31 | | | 200 | guidanceToAI={guidanceToAI} |
| b69ab31 | | | 201 | setGuidanceToAI={setGuidanceToAI} |
| b69ab31 | | | 202 | /> |
| b69ab31 | | | 203 | } |
| b69ab31 | | | 204 | /> |
| b69ab31 | | | 205 | </Tooltip> |
| b69ab31 | | | 206 | ); |
| b69ab31 | | | 207 | case 'ERROR': |
| b69ab31 | | | 208 | return ( |
| b69ab31 | | | 209 | <Column alignStart> |
| b69ab31 | | | 210 | <ButtonWithDropdownTooltip |
| b69ab31 | | | 211 | label={<T>AI Split</T>} |
| b69ab31 | | | 212 | data-testid="cwd-dropdown-button" |
| b69ab31 | | | 213 | kind="icon" |
| b69ab31 | | | 214 | icon={<Icon icon="sparkle" />} |
| b69ab31 | | | 215 | onClick={fetch} |
| b69ab31 | | | 216 | tooltip={ |
| b69ab31 | | | 217 | <DetailsDropdown |
| b69ab31 | | | 218 | loadingState={loadingState} |
| b69ab31 | | | 219 | submit={fetch} |
| b69ab31 | | | 220 | guidanceToAI={guidanceToAI} |
| b69ab31 | | | 221 | setGuidanceToAI={setGuidanceToAI} |
| b69ab31 | | | 222 | /> |
| b69ab31 | | | 223 | } |
| b69ab31 | | | 224 | /> |
| b69ab31 | | | 225 | <InlineErrorBadge error={loadingState.error} placement="bottom"> |
| b69ab31 | | | 226 | <T>AI Split Failed</T> |
| b69ab31 | | | 227 | </InlineErrorBadge> |
| b69ab31 | | | 228 | </Column> |
| b69ab31 | | | 229 | ); |
| b69ab31 | | | 230 | } |
| b69ab31 | | | 231 | }, |
| b69ab31 | | | 232 | ); |
| b69ab31 | | | 233 | |
| b69ab31 | | | 234 | function useDiffWithoutGeneratedFiles(subStack: CommitStackState, rev: CommitRev): DiffCommit { |
| b69ab31 | | | 235 | const diffForAllFiles = diffCommit(subStack, rev); |
| b69ab31 | | | 236 | const allFilePaths = diffForAllFiles.files.map(f => f.bPath); |
| b69ab31 | | | 237 | const generatedFileStatuses = useGeneratedFileStatuses(allFilePaths); |
| b69ab31 | | | 238 | const filesWithoutGeneratedFiles = diffForAllFiles.files.filter( |
| b69ab31 | | | 239 | f => generatedFileStatuses[f.bPath] !== GeneratedStatus.Generated, |
| b69ab31 | | | 240 | ); |
| b69ab31 | | | 241 | return { |
| b69ab31 | | | 242 | ...diffForAllFiles, |
| b69ab31 | | | 243 | files: filesWithoutGeneratedFiles, |
| b69ab31 | | | 244 | }; |
| b69ab31 | | | 245 | } |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | function DetailsDropdown({ |
| b69ab31 | | | 248 | loadingState, |
| b69ab31 | | | 249 | submit, |
| b69ab31 | | | 250 | guidanceToAI, |
| b69ab31 | | | 251 | setGuidanceToAI, |
| b69ab31 | | | 252 | }: { |
| b69ab31 | | | 253 | loadingState: AISplitButtonLoadingState; |
| b69ab31 | | | 254 | submit: () => unknown; |
| b69ab31 | | | 255 | guidanceToAI: string; |
| b69ab31 | | | 256 | setGuidanceToAI: React.Dispatch<React.SetStateAction<string>>; |
| b69ab31 | | | 257 | }) { |
| b69ab31 | | | 258 | const ref = useRef(null); |
| b69ab31 | | | 259 | return ( |
| b69ab31 | | | 260 | <Column alignStart> |
| b69ab31 | | | 261 | <MinHeightTextField |
| b69ab31 | | | 262 | ref={ref} |
| b69ab31 | | | 263 | keepNewlines |
| b69ab31 | | | 264 | containerXstyle={styles.full} |
| b69ab31 | | | 265 | value={guidanceToAI} |
| b69ab31 | | | 266 | onInput={e => setGuidanceToAI(e.currentTarget.value)}> |
| b69ab31 | | | 267 | <T>Provide additional instructions to AI (optional)</T> |
| b69ab31 | | | 268 | </MinHeightTextField> |
| b69ab31 | | | 269 | <Button onClick={submit} style={{alignSelf: 'end'}} primary> |
| b69ab31 | | | 270 | {loadingState.type === 'LOADING' ? <Icon icon="loading" /> : <Icon icon="sparkle" />} |
| b69ab31 | | | 271 | {loadingState.type === 'LOADING' ? <T>Splitting</T> : <T>AI Split</T>} |
| b69ab31 | | | 272 | </Button> |
| b69ab31 | | | 273 | </Column> |
| b69ab31 | | | 274 | ); |
| b69ab31 | | | 275 | } |