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