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