Elffuss BraidHub

kikocis/continue public

16439cbc6ccd · 4.9 KiBRawPermalink
1import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
2import { JSONContent } from "@tiptap/core";
3import { InputModifiers, ToolResultChatMessage, UserChatMessage } from "core";
4import { constructMessages } from "core/llm/constructMessages";
5import { getApplicableRules } from "core/llm/rules/getSystemMessageWithRules";
6import posthog from "posthog-js";
7import { v4 as uuidv4 } from "uuid";
8import { getBaseSystemMessage } from "../../util";
9import { selectSelectedChatModel } from "../slices/configSlice";
10import {
11 setAppliedRulesAtIndex,
12 submitEditorAndInitAtIndex,
13 updateHistoryItemAtIndex,
14} from "../slices/sessionSlice";
15import { ThunkApiType } from "../store";
16import { gatherContext } from "./gatherContext";
17import { resetStateForNewMessage } from "./resetStateForNewMessage";
18import { streamNormalInput } from "./streamNormalInput";
19import { streamThunkWrapper } from "./streamThunkWrapper";
20import { updateFileSymbolsFromFiles } from "./updateFileSymbols";
21
22export const streamResponseThunk = createAsyncThunk<
23 void,
24 {
25 editorState: JSONContent;
26 modifiers: InputModifiers;
27 index?: number;
28 promptPreamble?: string;
29 },
30 ThunkApiType
31>(
32 "chat/streamResponse",
33 async (
34 { editorState, modifiers, index, promptPreamble },
35 { dispatch, extra, getState },
36 ) => {
37 await dispatch(
38 streamThunkWrapper(async () => {
39 const state = getState();
40 const selectedChatModel = selectSelectedChatModel(state);
41 const inputIndex = index ?? state.session.history.length; // Either given index or concat to end
42
43 if (!selectedChatModel) {
44 throw new Error("No chat model selected");
45 }
46
47 dispatch(
48 submitEditorAndInitAtIndex({ index: inputIndex, editorState }),
49 );
50 resetStateForNewMessage();
51
52 const result = await dispatch(
53 gatherContext({
54 editorState,
55 modifiers,
56 promptPreamble,
57 }),
58 );
59 const {
60 selectedContextItems,
61 selectedCode,
62 content,
63 slashCommandWithInput,
64 } = unwrapResult(result);
65
66 // symbols for both context items AND selected codeblocks
67 const filesForSymbols = [
68 ...selectedContextItems
69 .filter((item) => item.uri?.type === "file" && item?.uri?.value)
70 .map((item) => item.uri!.value),
71 ...selectedCode.map((rif) => rif.filepath),
72 ];
73 dispatch(updateFileSymbolsFromFiles(filesForSymbols));
74
75 dispatch(
76 updateHistoryItemAtIndex({
77 index: inputIndex,
78 updates: {
79 message: {
80 role: "user",
81 content,
82 id: uuidv4(),
83 },
84 contextItems: selectedContextItems,
85 },
86 }),
87 );
88
89 // Get updated history after the update
90 const updatedHistory = getState().session.history;
91
92 // Determine which rules apply to this message
93 const userMsg = updatedHistory[inputIndex].message;
94 const rules = getState().config.config.rules;
95
96 // Calculate applicable rules once
97 // We need to check the message type to match what getApplicableRules expects
98 const applicableRules = getApplicableRules(
99 userMsg.role === "user" || userMsg.role === "tool"
100 ? (userMsg as UserChatMessage | ToolResultChatMessage)
101 : undefined,
102 rules,
103 selectedContextItems,
104 );
105
106 // Store in history for UI display
107 dispatch(
108 setAppliedRulesAtIndex({
109 index: inputIndex,
110 appliedRules: applicableRules,
111 }),
112 );
113
114 const messageMode = getState().session.mode;
115 const baseChatOrAgentSystemMessage = getBaseSystemMessage(
116 selectedChatModel,
117 messageMode,
118 );
119
120 const messages = constructMessages(
121 messageMode,
122 [...updatedHistory],
123 baseChatOrAgentSystemMessage,
124 applicableRules,
125 );
126
127 posthog.capture("step run", {
128 step_name: "User Input",
129 params: {},
130 });
131 posthog.capture("userInput", {});
132
133 if (slashCommandWithInput) {
134 posthog.capture("step run", {
135 step_name: slashCommandWithInput.command.name,
136 params: {},
137 });
138 }
139
140 unwrapResult(
141 await dispatch(
142 streamNormalInput({
143 messages,
144 legacySlashCommandData: slashCommandWithInput
145 ? {
146 command: slashCommandWithInput.command,
147 contextItems: selectedContextItems,
148 historyIndex: inputIndex,
149 input: slashCommandWithInput.input,
150 selectedCode,
151 }
152 : undefined,
153 }),
154 ),
155 );
156 }),
157 );
158 },
159);