| 1 | import { createAsyncThunk } from "@reduxjs/toolkit"; |
| 2 | import { JSONContent } from "@tiptap/core"; |
| 3 | import { |
| 4 | MessageModes, |
| 5 | RangeInFileWithContents, |
| 6 | SetCodeToEditPayload, |
| 7 | } from "core"; |
| 8 | import { stripImages } from "core/util/messageContent"; |
| 9 | import { resolveEditorContent } from "../../components/mainInput/TipTapEditor"; |
| 10 | import { |
| 11 | clearCodeToEdit, |
| 12 | INITIAL_EDIT_APPLY_STATE, |
| 13 | setPreviousModeEditorContent, |
| 14 | setReturnToModeAfterEdit, |
| 15 | updateEditStateApplyState, |
| 16 | } from "../slices/editState"; |
| 17 | import { |
| 18 | newSession, |
| 19 | setActive, |
| 20 | setIsInEdit, |
| 21 | setMainEditorContentTrigger, |
| 22 | setMode, |
| 23 | } from "../slices/sessionSlice"; |
| 24 | import { ThunkApiType } from "../store"; |
| 25 | import { loadLastSession, saveCurrentSession } from "./session"; |
| 26 | import { streamThunkWrapper } from "./streamThunkWrapper"; |
| 27 | |
| 28 | export const streamEditThunk = createAsyncThunk< |
| 29 | void, |
| 30 | { |
| 31 | editorState: JSONContent; |
| 32 | codeToEdit: SetCodeToEditPayload[]; |
| 33 | }, |
| 34 | ThunkApiType |
| 35 | >( |
| 36 | "chat/streamResponse", |
| 37 | async ({ editorState, codeToEdit }, { dispatch, extra }) => { |
| 38 | await dispatch( |
| 39 | streamThunkWrapper(async () => { |
| 40 | dispatch(setActive()); |
| 41 | const [contextItems, __, userInstructions, _] = |
| 42 | await resolveEditorContent({ |
| 43 | editorState, |
| 44 | modifiers: { |
| 45 | noContext: true, |
| 46 | useCodebase: false, |
| 47 | }, |
| 48 | ideMessenger: extra.ideMessenger, |
| 49 | defaultContextProviders: [], |
| 50 | availableSlashCommands: [], |
| 51 | dispatch, |
| 52 | }); |
| 53 | |
| 54 | const prompt = [ |
| 55 | ...contextItems.map((item) => item.content), |
| 56 | stripImages(userInstructions), |
| 57 | ].join("\n\n"); |
| 58 | |
| 59 | const response = await extra.ideMessenger.request("edit/sendPrompt", { |
| 60 | prompt, |
| 61 | range: codeToEdit[0] as RangeInFileWithContents, |
| 62 | }); |
| 63 | |
| 64 | if (response.status === "error") { |
| 65 | throw new Error(response.error); |
| 66 | } |
| 67 | }), |
| 68 | ); |
| 69 | }, |
| 70 | ); |
| 71 | |
| 72 | export const exitEdit = createAsyncThunk< |
| 73 | void, |
| 74 | { goToMode?: MessageModes; openNewSession?: boolean }, |
| 75 | ThunkApiType |
| 76 | >( |
| 77 | "edit/exit", |
| 78 | async ({ goToMode, openNewSession }, { dispatch, extra, getState }) => { |
| 79 | const state = getState(); |
| 80 | const codeToEdit = state.editModeState.codeToEdit; |
| 81 | const isInEdit = state.session.isInEdit; |
| 82 | const previousModeEditorContent = |
| 83 | state.editModeState.previousModeEditorContent; |
| 84 | |
| 85 | if (!isInEdit) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (codeToEdit[0] && state.editModeState.applyState.numDiffs) { |
| 90 | extra.ideMessenger.post("rejectDiff", { |
| 91 | filepath: codeToEdit[0].filepath, |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | extra.ideMessenger.post("edit/clearDecorations", undefined); |
| 96 | |
| 97 | dispatch(clearCodeToEdit()); |
| 98 | dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE)); |
| 99 | dispatch(setIsInEdit(false)); |
| 100 | |
| 101 | // Restore the previous editor content if available |
| 102 | if (previousModeEditorContent) { |
| 103 | dispatch(setMainEditorContentTrigger(previousModeEditorContent)); |
| 104 | dispatch(setPreviousModeEditorContent(undefined)); |
| 105 | } |
| 106 | |
| 107 | if (openNewSession || state.editModeState.lastNonEditSessionWasEmpty) { |
| 108 | dispatch(newSession()); |
| 109 | } else { |
| 110 | await dispatch( |
| 111 | loadLastSession({ |
| 112 | saveCurrentSession: false, |
| 113 | }), |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | dispatch(setMode(goToMode ?? state.editModeState.returnToMode)); |
| 118 | }, |
| 119 | ); |
| 120 | |
| 121 | export const enterEdit = createAsyncThunk< |
| 122 | void, |
| 123 | { returnToMode?: MessageModes; editorContent?: JSONContent }, |
| 124 | ThunkApiType |
| 125 | >( |
| 126 | "edit/enter", |
| 127 | async ({ returnToMode, editorContent }, { dispatch, extra, getState }) => { |
| 128 | const state = getState(); |
| 129 | const isInEdit = state.session.isInEdit; |
| 130 | |
| 131 | if (isInEdit) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | dispatch(setMainEditorContentTrigger({})); |
| 136 | dispatch(setPreviousModeEditorContent(editorContent)); |
| 137 | |
| 138 | dispatch(setReturnToModeAfterEdit(returnToMode ?? state.session.mode)); |
| 139 | dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE)); |
| 140 | |
| 141 | await dispatch( |
| 142 | saveCurrentSession({ |
| 143 | openNewSession: true, |
| 144 | // Because this causes a lag before Edit is focused. TODO just have that happen in background |
| 145 | generateTitle: false, |
| 146 | }), |
| 147 | ); |
| 148 | |
| 149 | dispatch(setIsInEdit(true)); |
| 150 | |
| 151 | if (!state.editModeState.codeToEdit[0]) { |
| 152 | extra.ideMessenger.post("edit/addCurrentSelection", undefined); |
| 153 | } |
| 154 | }, |
| 155 | ); |