diff --git a/core/index.d.ts b/core/index.d.ts index d0863cbfd..ae0b5da4b 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -146,6 +146,7 @@ export interface ContextItemWithId { export interface ChatHistoryItem { message: ChatMessage; + editorState?: any; contextItems: ContextItemWithId[]; promptLogs?: [string, string][]; // [prompt, completion] } diff --git a/gui/src/components/mainInput/ContinueInputBox.tsx b/gui/src/components/mainInput/ContinueInputBox.tsx index 4556dbc54..9e6326246 100644 --- a/gui/src/components/mainInput/ContinueInputBox.tsx +++ b/gui/src/components/mainInput/ContinueInputBox.tsx @@ -1,3 +1,4 @@ +import { JSONContent } from "@tiptap/react"; import { useSelector } from "react-redux"; import styled, { keyframes } from "styled-components"; import { defaultBorderRadius, vscBackground } from ".."; @@ -47,7 +48,10 @@ const GradientBorder = styled.div<{ interface ContinueInputBoxProps { isLastUserInput: boolean; isMainInput?: boolean; - onEnter: (input: string) => void; + onEnter: (editorState: JSONContent) => void; + + editorState?: JSONContent; + content?: string; } function ContinueInputBox(props: ContinueInputBoxProps) { @@ -88,6 +92,8 @@ function ContinueInputBox(props: ContinueInputBoxProps) { borderRadius={defaultBorderRadius} > void; + onEnter: (editorState: JSONContent) => void; + + editorState?: JSONContent; + content?: string; } function TipTapEditor(props: TipTapEditorProps) { @@ -137,15 +139,15 @@ function TipTapEditor(props: TipTapEditorProps) { }, }), ], - content: "", editorProps: { attributes: { class: "outline-none -mt-1 overflow-hidden", style: "font-size: 14px;", }, }, + content: props.editorState || props.content || "", }, - [props.availableContextProviders] + [props.availableContextProviders, historyLength] ); const [inputFocused, setInputFocused] = useState(false); @@ -204,7 +206,7 @@ function TipTapEditor(props: TipTapEditorProps) { } }} onEnter={async () => { - props.onEnter(await resolveEditorContent(editor, contextProviders)); + props.onEnter(editor.getJSON()); }} /> diff --git a/gui/src/components/mainInput/collectInput.ts b/gui/src/components/mainInput/collectInput.ts index a154e340e..dce579763 100644 --- a/gui/src/components/mainInput/collectInput.ts +++ b/gui/src/components/mainInput/collectInput.ts @@ -1,4 +1,4 @@ -import { Editor, JSONContent } from "@tiptap/react"; +import { JSONContent } from "@tiptap/react"; import { IContextProvider } from "core"; import { getContextItems } from "../../hooks/useContextProviders"; @@ -10,13 +10,12 @@ import { getContextItems } from "../../hooks/useContextProviders"; */ async function resolveEditorContent( - editor: Editor, + editorState: JSONContent, contextProviders: IContextProvider[] ): Promise { - const content = editor.getJSON(); let paragraphs = []; let contextItems = []; - for (const p of content?.content) { + for (const p of editorState?.content) { if (p.type === "paragraph") { const [text, ctxItems] = resolveParagraph(p); paragraphs.push(text); diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index cd1692ad8..11e5882cc 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -1,11 +1,13 @@ import { Dispatch } from "@reduxjs/toolkit"; +import { JSONContent } from "@tiptap/react"; import { ChatHistory, ChatHistoryItem, ChatMessage, SlashCommand } from "core"; import { ExtensionIde } from "core/ide"; import { constructMessages } from "core/llm/constructMessages"; import { usePostHog } from "posthog-js/react"; import { useEffect, useRef } from "react"; import { useSelector } from "react-redux"; +import resolveEditorContent from "../components/mainInput/collectInput"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; import { addContextItems, @@ -31,7 +33,9 @@ function useChatHandler(dispatch: Dispatch) { (store: RootStore) => store.state.contextItems ); const history = useSelector((store: RootStore) => store.state.history); - + const contextProviders = useSelector( + (store: RootStore) => store.state.config.contextProviders || [] + ); const active = useSelector((store: RootStore) => store.state.active); const activeRef = useRef(active); useEffect(() => { @@ -95,11 +99,13 @@ function useChatHandler(dispatch: Dispatch) { } } - async function streamResponse(input: string, index?: number) { + async function streamResponse(editorState: JSONContent, index?: number) { + const content = await resolveEditorContent(editorState, contextProviders); + try { const message: ChatMessage = { role: "user", - content: input, + content, }; const historyItem: ChatHistoryItem = { message, @@ -107,16 +113,17 @@ function useChatHandler(dispatch: Dispatch) { typeof index === "number" ? history[index].contextItems : contextItems, + editorState, }; let newHistory: ChatHistory = []; if (typeof index === "number") { newHistory = [...history.slice(0, index), historyItem]; - dispatch(resubmitAtIndex({ index, content: input })); + dispatch(resubmitAtIndex({ index, content, editorState })); } else { newHistory = [...history, historyItem]; console.log("Submitting message"); - dispatch(submitMessage(message)); + dispatch(submitMessage({ message, editorState })); } // TODO: hacky way to allow rerender @@ -125,17 +132,17 @@ function useChatHandler(dispatch: Dispatch) { posthog.capture("step run", { step_name: "User Input", params: { - user_input: input, + user_input: content, }, }); posthog.capture("userInput", { - input, + input: content, }); const messages = constructMessages(newHistory); // Determine if the input is a slash command - let commandAndInput = getSlashCommandForInput(input); + let commandAndInput = getSlashCommandForInput(content); const logs = []; const writeLog = async (log: string) => { diff --git a/gui/src/pages/gui.tsx b/gui/src/pages/gui.tsx index 263e8511e..41aa732cc 100644 --- a/gui/src/pages/gui.tsx +++ b/gui/src/pages/gui.tsx @@ -3,6 +3,7 @@ import { CodeBracketSquareIcon, ExclamationTriangleIcon, } from "@heroicons/react/24/outline"; +import { JSONContent } from "@tiptap/react"; import { usePostHog } from "posthog-js/react"; import { Fragment, @@ -27,6 +28,7 @@ import ErrorStepContainer from "../components/gui/ErrorStepContainer"; import StepContainer from "../components/gui/StepContainer"; import TimelineItem from "../components/gui/TimelineItem"; import ContinueInputBox from "../components/mainInput/ContinueInputBox"; +import resolveEditorContent from "../components/mainInput/collectInput"; import useChatHandler from "../hooks/useChatHandler"; import useHistory from "../hooks/useHistory"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; @@ -129,6 +131,9 @@ function GUI(props: GUIProps) { const defaultModel = useSelector(defaultModelSelector); const active = useSelector((state: RootStore) => state.state.active); + const contextProviders = useSelector( + (state: RootStore) => state.state.config.contextProviders || [] + ); // #endregion @@ -225,11 +230,10 @@ function GUI(props: GUIProps) { const { streamResponse } = useChatHandler(dispatch); const sendInput = useCallback( - (input: string) => { + (editorState: JSONContent) => { if ( defaultModel.providerName === "openai-free-trial" && - defaultModel?.apiKey === "" && - (!input.startsWith("/") || input.startsWith("/edit")) + defaultModel?.apiKey === "" ) { const ftc = localStorage.getItem("ftc"); if (ftc) { @@ -247,7 +251,7 @@ function GUI(props: GUIProps) { } } - streamResponse(input); + streamResponse(editorState); // Increment localstorage counter for popup const counter = localStorage.getItem("mainTextEntryCounter"); @@ -387,12 +391,20 @@ function GUI(props: GUIProps) { > {item.message.role === "user" ? ( { - dispatch(resubmitAtIndex({ index, content: value })); - streamResponse(value, index); + onEnter={async (editorState) => { + const content = await resolveEditorContent( + editorState, + contextProviders + ); + dispatch( + resubmitAtIndex({ index, content, editorState }) + ); + streamResponse(editorState, index); }} isLastUserInput={isLastUserInput(index)} isMainInput={false} + editorState={item.editorState} + content={item.message.content} > ) : ( { - return { - ...state, - history: [...state.history, action.payload], - }; - }, + addContextItems: (state, action) => { return { ...state, contextItems: [...state.contextItems, ...action.payload], }; }, - resubmitAtIndex: (state, action) => { - if (action.payload.index < state.history.length) { - state.history[action.payload.index].message.content = - action.payload.content; + resubmitAtIndex: ( + state, + { + payload, + }: { + payload: { + index: number; + content: string; + editorState: JSONContent; + }; + } + ) => { + if (payload.index < state.history.length) { + state.history[payload.index].message.content = payload.content; + state.history[payload.index].editorState = payload.editorState; // Cut off history after the resubmitted message state.history = [ - ...state.history.slice(0, action.payload.index + 1), + ...state.history.slice(0, payload.index + 1), { message: { role: "assistant", @@ -182,10 +189,21 @@ export const stateSlice = createSlice({ state.active = true; } }, - submitMessage: (state, { payload }: { payload: ChatMessage }) => { + submitMessage: ( + state, + { + payload, + }: { + payload: { + message: ChatMessage; + editorState: JSONContent; + }; + } + ) => { state.history.push({ - message: payload, + message: payload.message, contextItems: state.contextItems, + editorState: payload.editorState, }); state.history.push({ message: { @@ -382,7 +400,6 @@ export const stateSlice = createSlice({ export const { addContextItemsAtIndex, - appendMessage, addContextItems, submitMessage, setInactive,