Elffuss BraidHub

kikocis/continue public

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