Elffuss BraidHub

kikocis/continue public

cf6f8dc05b27 · 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 });
68
69 // symbols for both context items AND selected codeblocks
70 const filesForSymbols = [
71 ...selectedContextItems
72 .filter((item) => item.uri?.type === "file" && item?.uri?.value)
73 .map((item) => item.uri!.value),
74 ...selectedCode.map((rif) => rif.filepath),
75 ];
76 void dispatch(updateFileSymbolsFromFiles(filesForSymbols));
77
78 dispatch(
79 updateHistoryItemAtIndex({
80 index: inputIndex,
81 updates: {
82 message: {
83 role: "user",
84 content,
85 id: uuidv4(),
86 },
87 contextItems: selectedContextItems,
88 },
89 }),
90 );
91
92 posthog.capture("step run", {
93 step_name: "User Input",
94 params: {},
95 });
96 posthog.capture("userInput", {});
97
98 if (legacyCommandWithInput) {
99 posthog.capture("step run", {
100 step_name: legacyCommandWithInput.command.name,
101 params: {},
102 });
103 }
104
105 unwrapResult(
106 await dispatch(
107 streamNormalInput({
108 legacySlashCommandData: legacyCommandWithInput
109 ? {
110 command: legacyCommandWithInput.command,
111 contextItems: selectedContextItems,
112 historyIndex: inputIndex,
113 input: legacyCommandWithInput.input,
114 selectedCode,
115 }
116 : undefined,
117 }),
118 ),
119 );
120 }),
121 );
122 },
123);