🚧 editorState in redux
This commit is contained in:
parent
09c45041ef
commit
41557d379e
7 changed files with 84 additions and 40 deletions
1
core/index.d.ts
vendored
1
core/index.d.ts
vendored
|
|
@ -146,6 +146,7 @@ export interface ContextItemWithId {
|
|||
|
||||
export interface ChatHistoryItem {
|
||||
message: ChatMessage;
|
||||
editorState?: any;
|
||||
contextItems: ContextItemWithId[];
|
||||
promptLogs?: [string, string][]; // [prompt, completion]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
>
|
||||
<TipTapEditor
|
||||
editorState={props.editorState}
|
||||
content={props.content}
|
||||
onEnter={props.onEnter}
|
||||
isMainInput={props.isMainInput}
|
||||
availableContextProviders={availableContextProviders}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import Mention from "@tiptap/extension-mention";
|
|||
import Paragraph from "@tiptap/extension-paragraph";
|
||||
import Placeholder from "@tiptap/extension-placeholder";
|
||||
import Text from "@tiptap/extension-text";
|
||||
import { EditorContent, useEditor } from "@tiptap/react";
|
||||
import { EditorContent, JSONContent, useEditor } from "@tiptap/react";
|
||||
import { IContextProvider } from "core";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
|
|
@ -22,7 +22,6 @@ import { postToIde } from "../../util/ide";
|
|||
import { SlashCommand } from "./CommandsExtension";
|
||||
import InputToolbar from "./InputToolbar";
|
||||
import "./TipTapEditor.css";
|
||||
import resolveEditorContent from "./collectInput";
|
||||
import { getCommandSuggestion, getMentionSuggestion } from "./getSuggestion";
|
||||
import { ComboBoxItem } from "./types";
|
||||
|
||||
|
|
@ -58,7 +57,10 @@ interface TipTapEditorProps {
|
|||
availableContextProviders: IContextProvider[];
|
||||
availableSlashCommands: ComboBoxItem[];
|
||||
isMainInput: boolean;
|
||||
onEnter: (input: string) => 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());
|
||||
}}
|
||||
/>
|
||||
</InputBoxDiv>
|
||||
|
|
|
|||
|
|
@ -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<string> {
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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" ? (
|
||||
<ContinueInputBox
|
||||
onEnter={(value) => {
|
||||
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}
|
||||
></ContinueInputBox>
|
||||
) : (
|
||||
<TimelineItem
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { createSlice } from "@reduxjs/toolkit";
|
||||
import { JSONContent } from "@tiptap/react";
|
||||
import {
|
||||
ChatMessage,
|
||||
ContextItemId,
|
||||
|
|
@ -149,26 +150,32 @@ export const stateSlice = createSlice({
|
|||
};
|
||||
}
|
||||
},
|
||||
appendMessage: (state, action) => {
|
||||
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,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue