Elffuss BraidHub

kikocis/continue public

7d19f8698b5d · 4.8 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 );
104
105 // Store in history for UI display
106 dispatch(
107 setAppliedRulesAtIndex({
108 index: inputIndex,
109 appliedRules: applicableRules,
110 }),
111 );
112
113 const messageMode = getState().session.mode;
114 const baseChatOrAgentSystemMessage = getBaseSystemMessage(
115 selectedChatModel,
116 messageMode,
117 );
118
119 const messages = constructMessages(
120 messageMode,
121 [...updatedHistory],
122 baseChatOrAgentSystemMessage,
123 applicableRules,
124 );
125
126 posthog.capture("step run", {
127 step_name: "User Input",
128 params: {},
129 });
130 posthog.capture("userInput", {});
131
132 if (slashCommandWithInput) {
133 posthog.capture("step run", {
134 step_name: slashCommandWithInput.command.name,
135 params: {},
136 });
137 }
138
139 unwrapResult(
140 await dispatch(
141 streamNormalInput({
142 messages,
143 legacySlashCommandData: slashCommandWithInput
144 ? {
145 command: slashCommandWithInput.command,
146 contextItems: selectedContextItems,
147 historyIndex: inputIndex,
148 input: slashCommandWithInput.input,
149 selectedCode,
150 }
151 : undefined,
152 }),
153 ),
154 );
155 }),
156 );
157 },
158);