Elffuss BraidHub

kikocis/continue public

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