| 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} from '../common'; |
| b69ab31 | | | 9 | import type {PartiallySelectedDiffCommit} from '../diffSplitTypes'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {List} from 'immutable'; |
| b69ab31 | | | 12 | import {getDefaultStore} from 'jotai'; |
| b69ab31 | | | 13 | import serverAPI from '../../ClientToServerAPI'; |
| b69ab31 | | | 14 | import {readAtom, writeAtom} from '../../jotaiUtils'; |
| b69ab31 | | | 15 | import {registerDisposable} from '../../utils'; |
| b69ab31 | | | 16 | import {applyDiffSplit} from '../diffSplit'; |
| b69ab31 | | | 17 | import {next} from '../revMath'; |
| b69ab31 | | | 18 | import {editingStackIntentionHashes, SplitRangeRecord, stackEditState} from './stackEditState'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | // Helper function to apply the AI split commits to the stack edit state |
| b69ab31 | | | 21 | function applyAISplitCommits(commits: PartiallySelectedDiffCommit[]) { |
| b69ab31 | | | 22 | // Get the current stack edit state |
| b69ab31 | | | 23 | const state = readAtom(stackEditState); |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | if (state.history.state !== 'hasValue') { |
| b69ab31 | | | 26 | throw new Error('Stack edit state is not loaded'); |
| b69ab31 | | | 27 | } |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | const history = state.history.value; |
| b69ab31 | | | 30 | const commitStack = history.current.state; |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | // When the intention is 'split', we're splitting a single commit |
| b69ab31 | | | 33 | const intention = state.intention; |
| b69ab31 | | | 34 | if (intention !== 'split') { |
| b69ab31 | | | 35 | throw new Error('Cannot apply AI split when not in split mode'); |
| b69ab31 | | | 36 | } |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | // In split mode, startRev is 1 and endRev is size-1 |
| b69ab31 | | | 39 | const startRev = 1 as CommitRev; |
| b69ab31 | | | 40 | const endRev = (commitStack.size - 1) as CommitRev; |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | // Extract a dense substack containing only the commit(s) to be split |
| b69ab31 | | | 43 | // For split mode with a single commit, this is just [startRev] |
| b69ab31 | | | 44 | const subStack = commitStack.denseSubStack(List([startRev])); |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | // Apply the diff split to the first commit in the subStack (position 0 relative to subStack) |
| b69ab31 | | | 47 | const newSubStack = applyDiffSplit(subStack, 0 as CommitRev, commits); |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | // Replace the [start, end+1] range with the new stack in the commit stack |
| b69ab31 | | | 50 | const newCommitStack = commitStack.applySubStack(startRev, next(endRev), newSubStack); |
| b69ab31 | | | 51 | |
| b69ab31 | | | 52 | // Calculate the split range for UI selection |
| b69ab31 | | | 53 | const endOffset = newCommitStack.size - commitStack.size; |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | // The split commits start at position startRev (the first new split commit) |
| b69ab31 | | | 56 | // and end at position startRev + endOffset |
| b69ab31 | | | 57 | const startKey = newCommitStack.get(startRev)?.key ?? ''; |
| b69ab31 | | | 58 | const endKey = newCommitStack.get(next(startRev, endOffset))?.key ?? ''; |
| b69ab31 | | | 59 | const splitRange = SplitRangeRecord({startKey, endKey}); |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | // Update the state with the new split |
| b69ab31 | | | 62 | writeAtom(stackEditState, prev => { |
| b69ab31 | | | 63 | if (prev.history.state !== 'hasValue') { |
| b69ab31 | | | 64 | return prev; |
| b69ab31 | | | 65 | } |
| b69ab31 | | | 66 | |
| b69ab31 | | | 67 | return { |
| b69ab31 | | | 68 | ...prev, |
| b69ab31 | | | 69 | history: { |
| b69ab31 | | | 70 | state: 'hasValue' as const, |
| b69ab31 | | | 71 | value: prev.history.value.push(newCommitStack, {name: 'splitWithAI'}, {splitRange}), |
| b69ab31 | | | 72 | }, |
| b69ab31 | | | 73 | }; |
| b69ab31 | | | 74 | }); |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | // Handle openSplitViewForCommit message - opens the split UI for a specific commit |
| b69ab31 | | | 78 | // and optionally applies AI split commits after the stack is loaded |
| b69ab31 | | | 79 | registerDisposable( |
| b69ab31 | | | 80 | serverAPI, |
| b69ab31 | | | 81 | serverAPI.onMessageOfType('openSplitViewForCommit', event => { |
| b69ab31 | | | 82 | // Open the split panel by setting the editing stack intention |
| b69ab31 | | | 83 | // Use getDefaultStore().set() directly since editingStackIntentionHashes has a custom write type |
| b69ab31 | | | 84 | const store = getDefaultStore(); |
| b69ab31 | | | 85 | store.set(editingStackIntentionHashes, ['split', new Set([event.commitHash])]); |
| b69ab31 | | | 86 | |
| b69ab31 | | | 87 | // If commits are provided, wait for the stack to load and then apply the split |
| b69ab31 | | | 88 | if (event.commits && event.commits.length > 0) { |
| b69ab31 | | | 89 | const commitsToApply = event.commits; |
| b69ab31 | | | 90 | let hasApplied = false; |
| b69ab31 | | | 91 | const unsubscribe = store.sub(stackEditState, () => { |
| b69ab31 | | | 92 | if (hasApplied) { |
| b69ab31 | | | 93 | return; |
| b69ab31 | | | 94 | } |
| b69ab31 | | | 95 | const state = store.get(stackEditState); |
| b69ab31 | | | 96 | if (state.history.state === 'hasValue' && state.intention === 'split') { |
| b69ab31 | | | 97 | // Stack is loaded, apply the AI split commits |
| b69ab31 | | | 98 | hasApplied = true; |
| b69ab31 | | | 99 | unsubscribe(); |
| b69ab31 | | | 100 | applyAISplitCommits(commitsToApply); |
| b69ab31 | | | 101 | } |
| b69ab31 | | | 102 | }); |
| b69ab31 | | | 103 | } |
| b69ab31 | | | 104 | }), |
| b69ab31 | | | 105 | ); |