Elffuss BraidHub

kikocis/continue public

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