Elffuss BraidHub

kikocis/continue public

b0f6b05692ac · 4.3 KiBRawPermalink
1import { createAsyncThunk } from "@reduxjs/toolkit";
2import { JSONContent } from "@tiptap/core";
3import {
4 MessageModes,
5 RangeInFileWithContents,
6 SetCodeToEditPayload,
7} from "core";
8import { stripImages } from "core/util/messageContent";
9import { resolveEditorContent } from "../../components/mainInput/TipTapEditor";
10import {
11 clearCodeToEdit,
12 INITIAL_EDIT_APPLY_STATE,
13 setPreviousModeEditorContent,
14 setReturnToModeAfterEdit,
15 updateEditStateApplyState,
16} from "../slices/editState";
17import {
18 newSession,
19 setActive,
20 setInactive,
21 setIsInEdit,
22 setMainEditorContentTrigger,
23 setMode,
24} from "../slices/sessionSlice";
25import { ThunkApiType } from "../store";
26import { loadLastSession, saveCurrentSession } from "./session";
27import { streamThunkWrapper } from "./streamThunkWrapper";
28
29export 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
76export const exitEdit = createAsyncThunk<
77 void,
78 { goToMode?: MessageModes; openNewSession?: boolean },
79 ThunkApiType
80>(
81 "edit/exit",
82 async ({ goToMode, openNewSession }, { dispatch, extra, getState }) => {
83 const state = getState();
84 const codeToEdit = state.editModeState.codeToEdit;
85 const isInEdit = state.session.isInEdit;
86 const previousModeEditorContent =
87 state.editModeState.previousModeEditorContent;
88
89 if (!isInEdit) {
90 return;
91 }
92
93 if (codeToEdit[0] && state.editModeState.applyState.numDiffs) {
94 extra.ideMessenger.post("rejectDiff", {
95 filepath: codeToEdit[0].filepath,
96 });
97 }
98
99 extra.ideMessenger.post("edit/clearDecorations", undefined);
100
101 dispatch(clearCodeToEdit());
102 dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE));
103 dispatch(setIsInEdit(false));
104
105 // Restore the previous editor content if available
106 if (previousModeEditorContent) {
107 dispatch(setMainEditorContentTrigger(previousModeEditorContent));
108 dispatch(setPreviousModeEditorContent(undefined));
109 }
110
111 if (openNewSession || state.editModeState.lastNonEditSessionWasEmpty) {
112 dispatch(newSession());
113 } else {
114 await dispatch(
115 loadLastSession({
116 saveCurrentSession: false,
117 }),
118 );
119 }
120
121 dispatch(setMode(goToMode ?? state.editModeState.returnToMode));
122 },
123);
124
125export const enterEdit = createAsyncThunk<
126 void,
127 { returnToMode?: MessageModes; editorContent?: JSONContent },
128 ThunkApiType
129>(
130 "edit/enter",
131 async ({ returnToMode, editorContent }, { dispatch, extra, getState }) => {
132 const state = getState();
133 const isInEdit = state.session.isInEdit;
134
135 if (isInEdit) {
136 return;
137 }
138
139 dispatch(setMainEditorContentTrigger({}));
140 dispatch(setPreviousModeEditorContent(editorContent));
141
142 dispatch(setReturnToModeAfterEdit(returnToMode ?? state.session.mode));
143 dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE));
144
145 await dispatch(
146 saveCurrentSession({
147 openNewSession: true,
148 // Because this causes a lag before Edit is focused. TODO just have that happen in background
149 generateTitle: false,
150 }),
151 );
152
153 dispatch(setIsInEdit(true));
154
155 if (!state.editModeState.codeToEdit[0]) {
156 extra.ideMessenger.post("edit/addCurrentSelection", undefined);
157 }
158 },
159);