Elffuss BraidHub

kikocis/continue public

e8f6b7fe8bda · 4.2 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 }) => {
39 await dispatch(
40 streamThunkWrapper(async () => {
41 dispatch(setActive());
42 const { selectedContextItems, content } = 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 ...selectedContextItems.map((item) => item.content),
56 stripImages(content),
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 dispatch(setInactive());
65
66 if (response.status === "error") {
67 throw new Error(response.error);
68 }
69 }),
70 );
71 },
72);
73
74export const exitEdit = createAsyncThunk<
75 void,
76 { goToMode?: MessageModes; openNewSession?: boolean },
77 ThunkApiType
78>(
79 "edit/exit",
80 async ({ goToMode, 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(goToMode ?? state.editModeState.returnToMode));
120 },
121);
122
123export const enterEdit = createAsyncThunk<
124 void,
125 { returnToMode?: MessageModes; editorContent?: JSONContent },
126 ThunkApiType
127>(
128 "edit/enter",
129 async ({ returnToMode, editorContent }, { dispatch, extra, getState }) => {
130 const state = getState();
131 const isInEdit = state.session.isInEdit;
132
133 if (isInEdit) {
134 return;
135 }
136
137 dispatch(setMainEditorContentTrigger({}));
138 dispatch(setPreviousModeEditorContent(editorContent));
139
140 dispatch(setReturnToModeAfterEdit(returnToMode ?? state.session.mode));
141 dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE));
142
143 await dispatch(
144 saveCurrentSession({
145 openNewSession: true,
146 // Because this causes a lag before Edit is focused. TODO just have that happen in background
147 generateTitle: false,
148 }),
149 );
150
151 dispatch(setIsInEdit(true));
152
153 if (!state.editModeState.codeToEdit[0]) {
154 extra.ideMessenger.post("edit/addCurrentSelection", undefined);
155 }
156 },
157);