Elffuss BraidHub

kikocis/continue public

ec79a4d62a4e · 3.8 KiBRawPermalink
1import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
2import { JSONContent } from "@tiptap/core";
3import { InputModifiers } from "core";
4import posthog from "posthog-js";
5import { v4 as uuidv4 } from "uuid";
6import { resolveEditorContent } from "../../components/mainInput/TipTapEditor/utils/resolveEditorContent";
7import { selectSelectedChatModel } from "../slices/configSlice";
8import {
9 submitEditorAndInitAtIndex,
10 updateHistoryItemAtIndex,
11} from "../slices/sessionSlice";
12import { ThunkApiType } from "../store";
13import { resetStateForNewMessage } from "./resetStateForNewMessage";
14import { streamNormalInput } from "./streamNormalInput";
15import { streamThunkWrapper } from "./streamThunkWrapper";
16import { updateFileSymbolsFromFiles } from "./updateFileSymbols";
17
18export const streamResponseThunk = createAsyncThunk<
19 void,
20 {
21 editorState: JSONContent;
22 modifiers: InputModifiers;
23 index?: number;
24 },
25 ThunkApiType
26>(
27 "chat/streamResponse",
28 async ({ editorState, modifiers, index }, { dispatch, extra, getState }) => {
29 await dispatch(
30 streamThunkWrapper(async () => {
31 const state = getState();
32 const selectedChatModel = selectSelectedChatModel(state);
33 const inputIndex = index ?? state.session.history.length; // Either given index or concat to end
34
35 if (!selectedChatModel) {
36 throw new Error("No chat model selected");
37 }
38
39 dispatch(
40 submitEditorAndInitAtIndex({ index: inputIndex, editorState }),
41 );
42 resetStateForNewMessage();
43
44 const defaultContextProviders =
45 state.config.config.experimental?.defaultContext ?? [];
46
47 if (!selectedChatModel) {
48 console.error(
49 "gatherContext thunk: Cannot gather context, no model selected",
50 );
51 throw new Error("No chat model selected");
52 }
53
54 // Resolve context providers and construct new history
55 const {
56 selectedContextItems,
57 selectedCode,
58 content,
59 legacyCommandWithInput,
60 } = await resolveEditorContent({
61 editorState,
62 modifiers,
63 ideMessenger: extra.ideMessenger,
64 defaultContextProviders,
65 availableSlashCommands: state.config.config.slashCommands,
66 dispatch,
67 getState,
68 });
69
70 // symbols for both context items AND selected codeblocks
71 const filesForSymbols = [
72 ...selectedContextItems
73 .filter((item) => item.uri?.type === "file" && item?.uri?.value)
74 .map((item) => item.uri!.value),
75 ...selectedCode.map((rif) => rif.filepath),
76 ];
77 void dispatch(updateFileSymbolsFromFiles(filesForSymbols));
78
79 dispatch(
80 updateHistoryItemAtIndex({
81 index: inputIndex,
82 updates: {
83 message: {
84 role: "user",
85 content,
86 id: uuidv4(),
87 },
88 contextItems: selectedContextItems,
89 },
90 }),
91 );
92
93 posthog.capture("step run", {
94 step_name: "User Input",
95 params: {},
96 });
97 posthog.capture("userInput", {});
98
99 if (legacyCommandWithInput) {
100 posthog.capture("step run", {
101 step_name: legacyCommandWithInput.command.name,
102 params: {},
103 });
104 }
105
106 unwrapResult(
107 await dispatch(
108 streamNormalInput({
109 legacySlashCommandData: legacyCommandWithInput
110 ? {
111 command: legacyCommandWithInput.command,
112 contextItems: selectedContextItems,
113 historyIndex: inputIndex,
114 input: legacyCommandWithInput.input,
115 selectedCode,
116 }
117 : undefined,
118 }),
119 ),
120 );
121 }),
122 );
123 },
124);